UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

86 lines (85 loc) 2.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Pen = void 0; const util = __importStar(require("../util")); const spatial_1 = require("../spatial"); const util_1 = require("../util"); /** * Pen is a position tracker. * * The term "cursor" is overloaded in the codebase, so this is a simple alternative. */ class Pen { positions = new util_1.Stack(); constructor(initialPosition = spatial_1.Point.origin()) { this.positions.push(initialPosition); } get x() { return this.position().x; } get y() { return this.position().y; } position() { const position = this.positions.peek(); util.assertDefined(position); return position; } moveTo(point) { this.positions.pop(); this.positions.push(new spatial_1.Point(point.x, point.y)); } moveBy(opts) { const dx = opts.dx ?? 0; const dy = opts.dy ?? 0; const position = this.position(); this.moveTo({ x: position.x + dx, y: position.y + dy }); } clone() { const pen = new Pen(); pen.positions = this.positions.clone(); return pen; } save() { this.positions.push(this.position()); } restore() { if (this.positions.size() > 1) { this.positions.pop(); } } } exports.Pen = Pen;