UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

119 lines (118 loc) 4.95 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.Measure = void 0; const util = __importStar(require("../util")); const spatial_1 = require("../spatial"); const fragment_1 = require("./fragment"); const pen_1 = require("./pen"); class Measure { config; log; document; key; position; width; constructor(config, log, document, key, position, width) { this.config = config; this.log = log; this.document = document; this.key = key; this.position = position; this.width = width; } render() { const pen = new pen_1.Pen(this.position); const absoluteIndex = this.document.getAbsoluteMeasureIndex(this.key); const multiRestCount = this.document.getMeasureMultiRestCount(this.key); const fragmentRenders = this.renderFragments(pen); const jumps = this.document.getMeasure(this.key).jumps; const rect = spatial_1.Rect.merge(fragmentRenders.map((fragment) => fragment.rect)); return { type: 'measure', key: this.key, rect, fragmentRenders, multiRestCount, absoluteIndex, jumps, }; } renderFragments(pen) { const fragmentWidths = this.getFragmentWidths(); const fragmentCount = this.document.getFragmentCount(this.key); const fragmentRenders = new Array(); for (let fragmentIndex = 0; fragmentIndex < fragmentCount; fragmentIndex++) { const key = { ...this.key, fragmentIndex }; const width = fragmentWidths?.at(fragmentIndex) ?? null; const fragmentRender = new fragment_1.Fragment(this.config, this.log, this.document, key, pen.position(), width).render(); fragmentRenders.push(fragmentRender); // If the width is compressed too much, the voices will exceed the stave boundaries. To adhere to the model, we // move to the edge of _any_ stave's intrinsic rect. const staveRender = fragmentRender.partRenders.flatMap((p) => p.staveRenders).at(0); if (staveRender) { const upperRight = staveRender.intrinsicRect.topRight(); pen.moveTo({ x: upperRight.x, y: pen.y }); } else { // If this happens, the fragment staves may not be aligned. There could be a gap or overlap. this.log.warn('found a fragment render without staves, using fragment rect for spacing', key); pen.moveBy({ dx: fragmentRender.rect.w }); } } return fragmentRenders; } getFragmentWidths() { if (this.width === null) { return null; } const fragmentCount = this.document.getFragmentCount(this.key); const widths = new Array(); for (let fragmentIndex = 0; fragmentIndex < fragmentCount; fragmentIndex++) { const key = { ...this.key, fragmentIndex }; const fragment = this.document.getFragment(key); if (typeof fragment.minWidth === 'number' && fragment.minWidth > 0) { widths.push(fragment.minWidth); } else { const fragmentRender = new fragment_1.Fragment(this.config, this.log, this.document, key, spatial_1.Point.origin(), null).render(); widths.push(fragmentRender.rect.w); } } const total = util.sum(widths); return widths.map((w) => (w / total) * this.width); } } exports.Measure = Measure;