UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

82 lines (81 loc) 3.4 kB
import * as util from '../util'; import { Point, Rect } from '../spatial'; import { Fragment } from './fragment'; import { Pen } from './pen'; export 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(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 = 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(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(this.config, this.log, this.document, key, Point.origin(), null).render(); widths.push(fragmentRender.rect.w); } } const total = util.sum(widths); return widths.map((w) => (w / total) * this.width); } }