vexflow 5.3.0 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +2 -0
  2. package/build/cjs/vexflow-bravura.js +2 -2
  3. package/build/cjs/vexflow-bravura.js.map +1 -1
  4. package/build/cjs/vexflow-core.js +2 -2
  5. package/build/cjs/vexflow-core.js.map +1 -1
  6. package/build/cjs/vexflow-debug-with-tests.js +4189 -772
  7. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  8. package/build/cjs/vexflow-debug.js +4323 -906
  9. package/build/cjs/vexflow-debug.js.map +1 -1
  10. package/build/cjs/vexflow.js +2 -2
  11. package/build/cjs/vexflow.js.map +1 -1
  12. package/build/esm/src/alternatingtremolo.js +68 -0
  13. package/build/esm/src/annotation.js +19 -3
  14. package/build/esm/src/articulation.js +5 -1
  15. package/build/esm/src/beam.js +13 -0
  16. package/build/esm/src/bend.js +57 -6
  17. package/build/esm/src/clef.js +3 -0
  18. package/build/esm/src/continuouslayout.js +681 -0
  19. package/build/esm/src/continuousspan.js +159 -0
  20. package/build/esm/src/curve.js +21 -2
  21. package/build/esm/src/easyscore/composition.js +13 -2
  22. package/build/esm/src/easyscore/policies.js +6 -3
  23. package/build/esm/src/engravinggeometry.js +8 -1
  24. package/build/esm/src/factory.js +16 -4
  25. package/build/esm/src/font.js +9 -1
  26. package/build/esm/src/fretdiagram.js +77 -0
  27. package/build/esm/src/guitargesture.js +33 -0
  28. package/build/esm/src/guitarmodifiers.js +26 -0
  29. package/build/esm/src/index.js +8 -0
  30. package/build/esm/src/instrumentprofile.js +123 -0
  31. package/build/esm/src/keysignature.js +23 -1
  32. package/build/esm/src/notationspelling.js +73 -0
  33. package/build/esm/src/note.js +3 -0
  34. package/build/esm/src/ornament.js +5 -0
  35. package/build/esm/src/pagebreaks.js +58 -0
  36. package/build/esm/src/pedalmarking.js +39 -17
  37. package/build/esm/src/phraselayout.js +503 -0
  38. package/build/esm/src/phraseribbon.js +28 -0
  39. package/build/esm/src/publication.js +402 -0
  40. package/build/esm/src/publicationview.js +114 -0
  41. package/build/esm/src/scorenavigation.js +172 -0
  42. package/build/esm/src/sheet.js +14 -2
  43. package/build/esm/src/stave.js +33 -16
  44. package/build/esm/src/stavebarline.js +5 -7
  45. package/build/esm/src/staveconnector.js +1 -0
  46. package/build/esm/src/stavehairpin.js +53 -27
  47. package/build/esm/src/stavenote.js +6 -1
  48. package/build/esm/src/stavetie.js +27 -2
  49. package/build/esm/src/strokes.js +4 -0
  50. package/build/esm/src/system.js +19 -4
  51. package/build/esm/src/systembreaks.js +42 -0
  52. package/build/esm/src/tabnote.js +6 -1
  53. package/build/esm/src/textbracket.js +24 -5
  54. package/build/esm/src/timesignature.js +15 -6
  55. package/build/esm/src/tuplet.js +1 -0
  56. package/build/esm/src/version.js +3 -3
  57. package/build/esm/src/vexflow.js +16 -0
  58. package/build/esm/src/voice.js +7 -3
  59. package/build/esm/tests/engraving/continuous-studies.mjs +140 -0
  60. package/build/esm/tests/engraving/instrument-studies.mjs +273 -0
  61. package/build/esm/tests/engraving/phrase-studies.mjs +63 -0
  62. package/build/esm/tests/engraving/publication-studies.mjs +87 -0
  63. package/build/esm/tests/engraving/specialist-studies.mjs +347 -0
  64. package/build/esm/tests/engraving/studies.mjs +88 -0
  65. package/build/esm/tests/node/continuous-score-tests.mjs +282 -0
  66. package/build/esm/tests/node/instrument-profile-tests.mjs +224 -0
  67. package/build/esm/tests/node/phrase-layout-tests.mjs +281 -0
  68. package/build/esm/tests/node/publication-tests.mjs +219 -0
  69. package/build/esm/tools/easyscore-structure.mjs +1 -1
  70. package/build/esm/tools/explorer/catalog/entries.mjs +2 -1
  71. package/build/esm/tools/explorer/catalog/expression.mjs +14 -9
  72. package/build/esm/tools/explorer/catalog/guitar.mjs +238 -0
  73. package/build/esm/tools/explorer/catalog/layout.mjs +8 -6
  74. package/build/esm/tools/explorer/catalog/notes.mjs +16 -14
  75. package/build/esm/tools/explorer/catalog/staves.mjs +12 -6
  76. package/build/esm/tools/explorer/catalog/strings.mjs +10 -7
  77. package/build/types/src/alternatingtremolo.d.ts +13 -0
  78. package/build/types/src/annotation.d.ts +9 -0
  79. package/build/types/src/articulation.d.ts +3 -0
  80. package/build/types/src/beam.d.ts +2 -0
  81. package/build/types/src/bend.d.ts +8 -0
  82. package/build/types/src/clef.d.ts +3 -0
  83. package/build/types/src/continuouslayout.d.ts +103 -0
  84. package/build/types/src/continuousspan.d.ts +60 -0
  85. package/build/types/src/curve.d.ts +11 -0
  86. package/build/types/src/easyscore/composition.d.ts +8 -0
  87. package/build/types/src/easyscore/policies.d.ts +1 -1
  88. package/build/types/src/fretdiagram.d.ts +23 -0
  89. package/build/types/src/guitargesture.d.ts +26 -0
  90. package/build/types/src/guitarmodifiers.d.ts +3 -0
  91. package/build/types/src/index.d.ts +10 -0
  92. package/build/types/src/instrumentprofile.d.ts +124 -0
  93. package/build/types/src/keysignature.d.ts +9 -0
  94. package/build/types/src/notationspelling.d.ts +25 -0
  95. package/build/types/src/note.d.ts +15 -0
  96. package/build/types/src/ornament.d.ts +3 -1
  97. package/build/types/src/pagebreaks.d.ts +26 -0
  98. package/build/types/src/pedalmarking.d.ts +9 -0
  99. package/build/types/src/phraselayout.d.ts +87 -0
  100. package/build/types/src/phraseribbon.d.ts +7 -0
  101. package/build/types/src/publication.d.ts +61 -0
  102. package/build/types/src/publicationview.d.ts +27 -0
  103. package/build/types/src/scorenavigation.d.ts +33 -0
  104. package/build/types/src/sheet.d.ts +5 -0
  105. package/build/types/src/stave.d.ts +13 -0
  106. package/build/types/src/stavehairpin.d.ts +18 -0
  107. package/build/types/src/stavetie.d.ts +5 -0
  108. package/build/types/src/system.d.ts +8 -1
  109. package/build/types/src/systembreaks.d.ts +9 -0
  110. package/build/types/src/textbracket.d.ts +7 -0
  111. package/build/types/src/timesignature.d.ts +4 -0
  112. package/build/types/src/vexflow.d.ts +16 -0
  113. package/build/types/src/voice.d.ts +2 -0
  114. package/changelog/CHANGELOG.md +8 -0
  115. package/package.json +9 -2
@@ -0,0 +1,681 @@
1
+ import { Accidental } from './accidental.js';
2
+ import { Clef } from './clef.js';
3
+ import { continuationPitch, drawSpan, fitSpan, spanAbove, spanLabel, } from './continuousspan.js';
4
+ import { Element } from './element.js';
5
+ import { measureEngraving } from './engravinggeometry.js';
6
+ import { Fraction } from './fraction.js';
7
+ import { Glyphs } from './glyphs.js';
8
+ import { KeySignature } from './keysignature.js';
9
+ import { MultiMeasureRest } from './multimeasurerest.js';
10
+ import { Note } from './note.js';
11
+ import { Barline, BarlineType } from './stavebarline.js';
12
+ import { StaveConnector } from './staveconnector.js';
13
+ import { StaveModifierPosition as Position } from './stavemodifier.js';
14
+ import { StaveNote } from './stavenote.js';
15
+ import { Stem } from './stem.js';
16
+ import { chooseSystemBreaks } from './systembreaks.js';
17
+ import { TabNote } from './tabnote.js';
18
+ import { TimeSignature } from './timesignature.js';
19
+ import { RuntimeError } from './util.js';
20
+ function invalid(message) {
21
+ throw new RuntimeError('BadContinuousScore', message);
22
+ }
23
+ export class ContinuousLayout {
24
+ headerLabel(text, symbol = false) {
25
+ const key = (symbol ? 'glyph:' : 'text:') + text;
26
+ let mark = this.headerLabels.get(key);
27
+ if (!mark) {
28
+ mark = new Element(symbol ? 'Repetition' : 'Annotation').setText(text);
29
+ if (symbol)
30
+ mark.setFontSize(18);
31
+ this.headerLabels.set(key, mark);
32
+ }
33
+ return mark;
34
+ }
35
+ headerParts(i, atStart) {
36
+ var _a;
37
+ const boundary = this.boundaries.get(i), kind = (_a = boundary === null || boundary === void 0 ? void 0 : boundary.navigation) === null || _a === void 0 ? void 0 : _a.kind;
38
+ const nav = kind
39
+ ? { segno: Glyphs.segno, coda: Glyphs.coda, dalSegno: 'D.S.', toCoda: 'To Coda', daCapo: 'D.C.', fine: 'Fine' }[kind]
40
+ : '';
41
+ const texts = [
42
+ this.options.measureNumbers && atStart ? String(i + 1) : '',
43
+ boundary === null || boundary === void 0 ? void 0 : boundary.rehearsal,
44
+ ...(!atStart ? this.spec.staves.map((s) => { var _a; return (_a = s.measures[i].instrument) === null || _a === void 0 ? void 0 : _a.name; }) : []),
45
+ nav,
46
+ ];
47
+ return texts.flatMap((text, k) => text
48
+ ? [
49
+ {
50
+ mark: this.headerLabel(text, k === texts.length - 1 && (kind === 'segno' || kind === 'coda')),
51
+ boxed: k === 1,
52
+ },
53
+ ]
54
+ : []);
55
+ }
56
+ constructor(score, spec, starts, ends, placed) {
57
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
58
+ this.score = score;
59
+ this.spec = spec;
60
+ this.hidden = new Set();
61
+ this.diagnostics = [];
62
+ this.fragments = [];
63
+ this.rows = [];
64
+ this.boundaries = new Map();
65
+ this.spans = [];
66
+ this.drawing = [];
67
+ this.headerLabels = new Map();
68
+ this.splitChords = [];
69
+ this.spelledSpans = new Set();
70
+ this.arrivals = new Map();
71
+ this.options = (_a = spec.continuous) !== null && _a !== void 0 ? _a : {};
72
+ for (const voice of score.voices.flat(2))
73
+ voice.preserveStaveAssignments = true;
74
+ if (this.options.mode && !['greedy', 'balanced'].includes(this.options.mode))
75
+ invalid('Unknown break mode.');
76
+ const ids = spec.staves.map((s) => s.id);
77
+ for (const b of (_b = this.options.boundaries) !== null && _b !== void 0 ? _b : []) {
78
+ if (!Number.isInteger(b.measure) ||
79
+ b.measure < 1 ||
80
+ b.measure > score.systems.length ||
81
+ this.boundaries.has(b.measure - 1))
82
+ invalid('Expected a unique, in-range boundary measure.');
83
+ if (b.breakBefore && !['force', 'keep'].includes(b.breakBefore))
84
+ invalid('Unknown break constraint.');
85
+ this.boundaries.set(b.measure - 1, Object.assign({}, b));
86
+ }
87
+ const destinations = new Map();
88
+ for (const { navigation: n } of this.boundaries.values())
89
+ if (n && 'id' in n) {
90
+ if (!n.id || destinations.has(n.id))
91
+ invalid('Duplicate or empty navigation destination.');
92
+ destinations.set(n.id, n.kind);
93
+ }
94
+ for (const { navigation: n } of this.boundaries.values())
95
+ if (n && 'target' in n && destinations.get(n.target) !== (n.kind === 'toCoda' ? 'coda' : 'segno'))
96
+ invalid('Navigation instruction needs a matching destination.');
97
+ this.groups = ((_c = this.options.groups) !== null && _c !== void 0 ? _c : []).map((g) => {
98
+ const first = ids.indexOf(g.first), last = ids.indexOf(g.last);
99
+ if (first < 0 || last <= first || !['brace', 'bracket'].includes(g.type))
100
+ invalid('Invalid staff group.');
101
+ return Object.assign(Object.assign({}, g), { first, last });
102
+ });
103
+ const label = (text) => (text ? new Element('Annotation').setText(text) : undefined);
104
+ const names = spec.staves.map((s) => { var _a; return ({ full: label(s.name), short: label((_a = s.shortName) !== null && _a !== void 0 ? _a : s.name) }); });
105
+ this.names = score.systems.map((_, i) => spec.staves.map((s, j) => {
106
+ var _a;
107
+ const change = s.measures[i].instrument;
108
+ if (change)
109
+ names[j] = { full: label(change.name), short: label((_a = change.shortName) !== null && _a !== void 0 ? _a : change.name) };
110
+ return names[j];
111
+ }));
112
+ this.marks = starts.map((states, i) => states.map((state, j) => {
113
+ var _a;
114
+ const stave = score.staves[i][j];
115
+ const boundary = this.boundaries.get(i);
116
+ if (boundary === null || boundary === void 0 ? void 0 : boundary.repeatStart)
117
+ stave.setBegBarType(BarlineType.REPEAT_BEGIN);
118
+ if (boundary === null || boundary === void 0 ? void 0 : boundary.repeatEnd)
119
+ stave.setEndBarType(BarlineType.REPEAT_END);
120
+ const start = [], end = [];
121
+ const present = (category) => stave.getModifiers(Position.BEGIN, category).length > 0;
122
+ if (!present(Clef.CATEGORY))
123
+ start.push(new Clef(state.clef));
124
+ if (state.clef !== 'tab' && state.key !== 'C' && !present(KeySignature.CATEGORY))
125
+ start.push(new KeySignature(state.key));
126
+ const next = (_a = starts[i + 1]) === null || _a === void 0 ? void 0 : _a[j], current = ends[i][j];
127
+ if (next && this.options.courtesy !== false) {
128
+ if (next.clef !== current.clef)
129
+ end.push(new Clef(next.clef, 'small'));
130
+ if (next.clef !== 'tab' && next.key !== current.key)
131
+ end.push(new KeySignature(next.key, current.key));
132
+ if (next.time !== current.time)
133
+ end.push(new TimeSignature(next.time));
134
+ }
135
+ end.forEach((m) => m.setPosition(Position.END));
136
+ return { start, end };
137
+ }));
138
+ const locations = new Map();
139
+ score.notes.forEach((parts, measure) => parts.flat(2).forEach((n) => {
140
+ if (n instanceof Note)
141
+ locations.set(n, { note: n, measure, staff: score.staves[measure].indexOf(n.checkStave()) });
142
+ }));
143
+ const seen = new Set();
144
+ const onset = (n) => {
145
+ const time = new Fraction(0, 1);
146
+ for (const tick of n.getVoice().getTickables()) {
147
+ if (tick === n)
148
+ break;
149
+ if (!tick.shouldIgnoreTicks())
150
+ time.add(tick.getTicks());
151
+ }
152
+ return time;
153
+ };
154
+ for (const sp of (_d = spec.spans) !== null && _d !== void 0 ? _d : []) {
155
+ const from = sp.from && locations.get(score.getNote(sp.from)), to = sp.to && locations.get(score.getNote(sp.to));
156
+ if (from && to)
157
+ for (let i = from.measure; i <= to.measure; i++)
158
+ this.spelledSpans.add(i);
159
+ }
160
+ for (const chord of (_e = this.options.splitChords) !== null && _e !== void 0 ? _e : []) {
161
+ const a = locations.get(score.getNote(chord.upper)), b = locations.get(score.getNote(chord.lower));
162
+ if (!chord.id ||
163
+ seen.has(chord.id) ||
164
+ !a ||
165
+ !b ||
166
+ a.measure !== b.measure ||
167
+ a.staff >= b.staff ||
168
+ !(a.note instanceof StaveNote) ||
169
+ !(b.note instanceof StaveNote) ||
170
+ a.note.isRest() ||
171
+ b.note.isRest() ||
172
+ !['h', 'q', '2', '4'].includes(a.note.getDuration()) ||
173
+ a.note.getDuration() !== b.note.getDuration() ||
174
+ a.note.getStemDirection() !== b.note.getStemDirection() ||
175
+ !a.note.getTicks().equals(b.note.getTicks()) ||
176
+ !onset(a.note).equals(onset(b.note)))
177
+ invalid('A split stem needs matching simultaneous half or quarter notes on ordered staves.');
178
+ seen.add(chord.id);
179
+ this.splitChords.push({ id: chord.id, measure: a.measure, upper: a.note, lower: b.note });
180
+ }
181
+ const kinds = [
182
+ 'tie',
183
+ 'slur',
184
+ 'crescendo',
185
+ 'diminuendo',
186
+ 'pedal',
187
+ 'lyric',
188
+ 'hyphen',
189
+ 'volta',
190
+ 'ottava',
191
+ 'trill',
192
+ 'tuplet',
193
+ 'palmMute',
194
+ ];
195
+ for (const span of (_f = this.options.spans) !== null && _f !== void 0 ? _f : []) {
196
+ const first = locations.get(score.getNote(span.from)), last = locations.get(score.getNote(span.to));
197
+ if (!span.id ||
198
+ seen.has(span.id) ||
199
+ !kinds.includes(span.kind) ||
200
+ !first ||
201
+ !last ||
202
+ first.measure > last.measure)
203
+ invalid('Expected a unique span ID and ordered composition anchors.');
204
+ if (span.kind === 'palmMute') {
205
+ if (!(first.note instanceof TabNote) || !(last.note instanceof TabNote) || first.staff !== last.staff)
206
+ invalid('A palm mute needs two TAB anchors on the same staff.');
207
+ if (!((_g = span.strings) === null || _g === void 0 ? void 0 : _g.length) ||
208
+ new Set(span.strings).size !== span.strings.length ||
209
+ !span.strings.every((n) => Number.isInteger(n) && n > 0 && n <= first.note.checkStave().getNumLines()))
210
+ invalid('A palm mute needs distinct string numbers on its staff.');
211
+ for (const note of [first.note, last.note])
212
+ if (!note.getPositions().some((p) => span.strings.includes(p.str)))
213
+ invalid('Each palm-mute anchor must play a targeted string.');
214
+ }
215
+ else {
216
+ if (!(first.note instanceof StaveNote) || !(last.note instanceof StaveNote))
217
+ invalid('Continuous spans need staff-note anchors.');
218
+ for (const [head, n] of [
219
+ [(_h = span.fromHead) !== null && _h !== void 0 ? _h : 0, first.note],
220
+ [(_j = span.toHead) !== null && _j !== void 0 ? _j : 0, last.note],
221
+ ])
222
+ if (!Number.isInteger(head) || head < 0 || head >= n.keys.length)
223
+ invalid('Invalid span head index.');
224
+ }
225
+ if (first.measure === last.measure && onset(first.note).greaterThan(onset(last.note)))
226
+ invalid('Reversed span.');
227
+ if (span.kind === 'tie' && first.measure !== last.measure) {
228
+ const fi = (_k = span.fromHead) !== null && _k !== void 0 ? _k : 0, li = (_l = span.toHead) !== null && _l !== void 0 ? _l : 0;
229
+ if (first.note.getKeys()[fi].toLowerCase() !== last.note.getKeys()[li].toLowerCase())
230
+ invalid('Tied heads must have the same written pitch.');
231
+ const mark = last.note
232
+ .getModifiers()
233
+ .find((m) => m instanceof Accidental && m.getIndex() === li && !!(placed === null || placed === void 0 ? void 0 : placed.has(m)));
234
+ if (mark) {
235
+ const entries = (_m = this.arrivals.get(last.measure)) !== null && _m !== void 0 ? _m : [];
236
+ entries.push({ note: last.note, mark, index: last.note.getModifiers().indexOf(mark) });
237
+ this.arrivals.set(last.measure, entries);
238
+ }
239
+ }
240
+ seen.add(span.id);
241
+ const spec = span.kind === 'palmMute' ? Object.assign(Object.assign({}, span), { strings: [...span.strings] }) : Object.assign({}, span);
242
+ this.spans.push({ spec, first, last, label: spanLabel(spec) });
243
+ }
244
+ }
245
+ plainRest(i) {
246
+ const boundary = this.boundaries.get(i);
247
+ if (boundary ||
248
+ this.spelledSpans.has(i) ||
249
+ this.spec.staves.some((staff) => staff.measures[i].instrument) ||
250
+ this.spans.some((s) => s.first.measure <= i && i <= s.last.measure))
251
+ return false;
252
+ if (this.score.staves[i].some((s) => s
253
+ .getModifiers()
254
+ .some((m) => !(m instanceof Barline) || ![BarlineType.SINGLE, BarlineType.NONE].includes(m.getType()))))
255
+ return false;
256
+ return this.score.notes[i]
257
+ .flat(2)
258
+ .every((n) => n instanceof StaveNote && n.isRest() && !n.getModifiers().length && !n.getBeam() && !n.getTupletStack().length);
259
+ }
260
+ boundaryWidth(i, start, end) {
261
+ var _a;
262
+ for (const { note, mark, index } of (_a = this.arrivals.get(i)) !== null && _a !== void 0 ? _a : []) {
263
+ const mods = note.getModifiers(), current = mods.indexOf(mark);
264
+ if (start && current < 0)
265
+ mods.splice(Math.min(index, mods.length), 0, mark);
266
+ else if (!start && current >= 0)
267
+ mods.splice(current, 1);
268
+ }
269
+ const staves = this.score.staves[i];
270
+ return Math.max(...staves.map((s, j) => {
271
+ var _a, _b;
272
+ const marks = this.marks[i][j];
273
+ s.setBoundaryModifiers([...(start ? marks.start : []), ...(end ? marks.end : [])]);
274
+ const bar = s.getModifiers(Position.END, Barline.CATEGORY)[0];
275
+ return Math.max(s.getX() + s.getWidth() - s.getNoteEndX(), -((_b = (_a = bar === null || bar === void 0 ? void 0 : bar.getLayoutMetrics()) === null || _a === void 0 ? void 0 : _a.xMin) !== null && _b !== void 0 ? _b : 0));
276
+ }));
277
+ }
278
+ units(padding) {
279
+ var _a;
280
+ this.score.staves.flat().forEach((s) => s.setBoundaryModifiers([]));
281
+ const result = [];
282
+ for (let i = 0; i < this.score.systems.length; i++) {
283
+ let last = i;
284
+ if (this.options.compressRests && this.plainRest(i))
285
+ while (last + 1 < this.score.systems.length && this.plainRest(last + 1))
286
+ last++;
287
+ const sys = this.score.systems[i];
288
+ sys.setX(0).setY(0).setWidth(1);
289
+ const measure = (start, end) => {
290
+ const tail = this.boundaryWidth(i, start, end);
291
+ sys.setWidth().format({ endPadding: tail });
292
+ return sys.getWidth();
293
+ };
294
+ const base = measure(false, false);
295
+ const prefix = measure(true, false) - base;
296
+ const tail = this.endWidth(i, last);
297
+ const reserve = this.spans
298
+ .filter((s) => s.first.measure === i && s.label)
299
+ .reduce((w, s) => Math.max(w, s.label.getWidth() + 12), 0);
300
+ result.push({
301
+ first: i,
302
+ last,
303
+ width: Math.max(base + padding, last > i ? 68 : 0, reserve, ...[false, true].map((atStart) => this.headerParts(i, atStart).reduce((w, p) => w + p.mark.getWidth() + (p.boxed ? 12 : 7), 0))),
304
+ start: Math.max(0, prefix),
305
+ end: tail,
306
+ breakBefore: (_a = this.boundaries.get(i)) === null || _a === void 0 ? void 0 : _a.breakBefore,
307
+ });
308
+ i = last;
309
+ }
310
+ return result;
311
+ }
312
+ endWidth(i, last) {
313
+ const plain = this.boundaryWidth(i, false, false);
314
+ return Math.max(0, this.install(i, last, false, true) - plain);
315
+ }
316
+ install(i, last, start, end) {
317
+ if (i === last)
318
+ return this.boundaryWidth(i, start, end);
319
+ return Math.max(...this.score.staves[i].map((stave, j) => {
320
+ const m = this.marks[i][j];
321
+ stave.setBoundaryModifiers([...(start ? m.start : []), ...(end ? this.marks[last][j].end : [])]);
322
+ return stave.getX() + stave.getWidth() - stave.getNoteEndX();
323
+ }));
324
+ }
325
+ indent(measure) {
326
+ const widths = this.names[measure].map((n, j) => { var _a, _b; return (_b = (_a = (measure === 0 || this.spec.staves[j].measures[measure].instrument ? n.full : n.short)) === null || _a === void 0 ? void 0 : _a.getWidth()) !== null && _b !== void 0 ? _b : 0; });
327
+ const w = Math.max(0, ...widths);
328
+ return (w ? w + 10 : 0) + this.groups.length * 9;
329
+ }
330
+ visible(first, last) {
331
+ const indices = this.spec.staves.map((_, i) => i);
332
+ if (!this.options.hideEmptyStaves)
333
+ return indices;
334
+ const used = new Set();
335
+ for (let i = first; i <= last; i++) {
336
+ this.score.notes[i].flat(2).forEach((n) => {
337
+ if (n instanceof Note && (!(n instanceof StaveNote) || !n.isRest() || n.getModifiers().length))
338
+ used.add(this.score.staves[i].indexOf(n.checkStave()));
339
+ });
340
+ }
341
+ for (const s of this.spans)
342
+ if (s.first.measure <= last && first <= s.last.measure) {
343
+ used.add(s.first.staff);
344
+ used.add(s.last.staff);
345
+ }
346
+ return used.size ? indices.filter((i) => used.has(i)) : [0];
347
+ }
348
+ suppress(i, visible, compressed, omitted) {
349
+ if (omitted)
350
+ this.hidden.add(this.score.systems[i]);
351
+ this.score.staves[i].forEach((s, j) => {
352
+ if (omitted || !visible.includes(j))
353
+ this.hidden.add(s);
354
+ });
355
+ for (const voice of this.score.voices[i].flat()) {
356
+ const ticks = voice.getTickables();
357
+ if (omitted || compressed || ticks.every((n) => { var _a; return this.hidden.has((_a = n.getStave()) !== null && _a !== void 0 ? _a : voice.getStave()); })) {
358
+ this.hidden.add(voice);
359
+ ticks.forEach((n) => this.hidden.add(n));
360
+ }
361
+ }
362
+ }
363
+ format(options) {
364
+ var _a;
365
+ this.hidden.clear();
366
+ this.diagnostics.length = 0;
367
+ this.fragments.length = 0;
368
+ this.rows.length = 0;
369
+ this.drawing = [];
370
+ const units = this.units(options.measurePadding);
371
+ const groups = chooseSystemBreaks(units, options.width, options.measureGap, (i) => this.indent(units[i].first), (_a = this.options.mode) !== null && _a !== void 0 ? _a : 'balanced');
372
+ let y = options.y;
373
+ for (const indices of groups) {
374
+ const row = this.layoutRow(indices.map((i) => units[i]), options, y);
375
+ this.rows.push(row);
376
+ y += row.height + options.rowGap;
377
+ }
378
+ for (const s of this.spans) {
379
+ const parts = this.fragments.filter((f) => f.id === s.spec.id);
380
+ const total = parts.reduce((a, f) => a + f.x2 - f.x1, 0);
381
+ let prior = 0;
382
+ for (const f of parts) {
383
+ f.progress = [prior / total, (prior + f.x2 - f.x1) / total];
384
+ prior += f.x2 - f.x1;
385
+ this.drawing.push((ctx) => drawSpan(ctx, f, s.label));
386
+ }
387
+ }
388
+ return this.rows;
389
+ }
390
+ measureStaffInk(positions, visible) {
391
+ var _a;
392
+ const ink = this.spec.staves.map(() => ({ top: Infinity, bottom: -Infinity }));
393
+ for (const { u } of positions)
394
+ for (const j of visible) {
395
+ const stave = this.score.staves[u.first][j], base = stave.getY();
396
+ const take = (top, bottom) => {
397
+ ink[j].top = Math.min(ink[j].top, top - base);
398
+ ink[j].bottom = Math.max(ink[j].bottom, bottom - base);
399
+ };
400
+ take(stave.getYForLine(0) - stave.space(1.6), stave.getYForLine(stave.getNumLines() - 1) + stave.space(1.6));
401
+ for (const m of stave.getModifiers()) {
402
+ const boxes = m instanceof Clef || m instanceof TimeSignature
403
+ ? [m.getInkBounds()]
404
+ : m instanceof KeySignature
405
+ ? m.getInkParts()
406
+ : [];
407
+ for (const box of boxes)
408
+ take(box.y, box.y + box.h);
409
+ }
410
+ if (u.first === u.last)
411
+ for (const n of this.score.notes[u.first].flat(2)) {
412
+ if (n instanceof TabNote && n.getStave() === stave && n.hasStem()) {
413
+ const { topY, baseY } = n.getStemExtents();
414
+ take(Math.min(topY, baseY), Math.max(topY, baseY));
415
+ }
416
+ if (!(n instanceof StaveNote) || n.getStave() !== stave)
417
+ continue;
418
+ const bridge = (_a = n
419
+ .getBeam()) === null || _a === void 0 ? void 0 : _a.getNotes().some((p) => p.getStave() !== stave);
420
+ for (const box of bridge ? n.getHeadInk() : measureEngraving(n).parts)
421
+ take(box.y, box.y + box.h);
422
+ if (n.getBeam() && !bridge) {
423
+ const box = n.getBeam().getBoundingBox();
424
+ take(box.y, box.y + box.h);
425
+ }
426
+ for (const t of n.getTupletStack()) {
427
+ const box = t.getBoundingBox();
428
+ take(box.y, box.y + box.h);
429
+ }
430
+ }
431
+ }
432
+ return ink;
433
+ }
434
+ alignStaves(positions, visible, origin, offsets) {
435
+ for (let pass = 0; pass < 4; pass++) {
436
+ const next = offsets.slice();
437
+ for (const { sys, endPadding } of positions) {
438
+ sys.setY(origin).format({ staffOffsets: offsets, endPadding });
439
+ const staves = sys.getStaves();
440
+ for (let k = 1; k < visible.length; k++) {
441
+ const a = visible[k - 1], b = visible[k];
442
+ next[b] = Math.max(next[b], next[a] + staves[b].getY() - staves[a].getY());
443
+ }
444
+ }
445
+ if (visible.every((j) => Math.abs(next[j] - offsets[j]) < 0.01))
446
+ return true;
447
+ if (pass === 3)
448
+ return false;
449
+ next.forEach((v, j) => (offsets[j] = v));
450
+ }
451
+ return false;
452
+ }
453
+ layoutRow(units, options, y) {
454
+ var _a, _b;
455
+ const first = units[0].first, last = units[units.length - 1].last;
456
+ const visible = this.visible(first, last);
457
+ const spans = this.spans.filter((s) => s.first.measure <= last && first <= s.last.measure);
458
+ const indent = this.indent(first);
459
+ const minimum = indent +
460
+ units.reduce((sum, u) => sum + u.width, 0) +
461
+ units[0].start +
462
+ units[units.length - 1].end +
463
+ (units.length - 1) * options.measureGap;
464
+ const extra = options.justify ? Math.max(0, options.width - minimum) / units.length : 0;
465
+ let x = options.x + indent;
466
+ const offsets = this.spec.staves.map(() => 0);
467
+ const positions = units.map((u, k) => {
468
+ const begin = k === 0, end = k === units.length - 1;
469
+ const sys = this.score.systems[u.first];
470
+ sys.setX(x).setY(0);
471
+ const endPadding = this.install(u.first, u.last, begin, end);
472
+ const width = u.width + extra + (begin ? u.start : 0) + (end ? u.end : 0);
473
+ sys.setWidth(width).format({ endPadding });
474
+ for (let j = 1; j < visible.length; j++) {
475
+ const staves = sys.getStaves(), b = visible[j];
476
+ const nominal = staves[b].getY() - staves[b - 1].getY();
477
+ offsets[b] = Math.max(offsets[b], nominal);
478
+ }
479
+ for (let i = u.first; i <= u.last; i++)
480
+ this.suppress(i, visible, u.first !== u.last, i !== u.first);
481
+ x += width + options.measureGap;
482
+ return { u, sys, endPadding };
483
+ });
484
+ const ink = this.measureStaffInk(positions, visible);
485
+ const lanes = new Map();
486
+ const laneEnds = new Map();
487
+ for (const s of spans) {
488
+ if (s.spec.kind === 'tie' || s.spec.kind === 'slur')
489
+ continue;
490
+ const side = spanAbove(s.spec), staff = s.first.staff, key = staff + '-' + side;
491
+ const ends = (_a = laneEnds.get(key)) !== null && _a !== void 0 ? _a : [];
492
+ let lane = ends.findIndex((v) => v < s.first.measure);
493
+ if (lane < 0)
494
+ lane = ends.length;
495
+ ends[lane] = s.last.measure;
496
+ laneEnds.set(key, ends);
497
+ lanes.set(s, lane);
498
+ }
499
+ const limits = ink.map((bounds, j) => {
500
+ var _a, _b, _c, _d;
501
+ const space = this.score.staves[first][j].space(1);
502
+ const above = (_b = (_a = laneEnds.get(j + '-true')) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0, below = (_d = (_c = laneEnds.get(j + '-false')) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0;
503
+ return { top: bounds.top - space * (2 + above * 2.5), bottom: bounds.bottom + space * (1 + below * 2.5) };
504
+ });
505
+ for (let j = 1; j < visible.length; j++) {
506
+ const a = visible[j - 1], b = visible[j];
507
+ offsets[b] = offsets[a] + Math.max(offsets[b], limits[a].bottom - limits[b].top + 8);
508
+ }
509
+ const instrumentChange = units.some((u, k) => k > 0 && this.spec.staves.some((s) => s.measures[u.first].instrument));
510
+ const header = instrumentChange ||
511
+ units.some((u) => { var _a, _b; return ((_a = this.boundaries.get(u.first)) === null || _a === void 0 ? void 0 : _a.rehearsal) || ((_b = this.boundaries.get(u.first)) === null || _b === void 0 ? void 0 : _b.navigation); }) ||
512
+ this.options.measureNumbers
513
+ ? 20
514
+ : 0;
515
+ const origin = y + header - limits[visible[0]].top;
516
+ const stable = this.alignStaves(positions, visible, origin, offsets);
517
+ const measures = Array.from({ length: last - first + 1 }, (_, i) => first + i + 1);
518
+ if (!stable)
519
+ this.diagnostics.push({ code: 'StaffSpacingLimit', measures });
520
+ const row = {
521
+ systems: positions.map((p) => p.sys),
522
+ x: options.x,
523
+ y,
524
+ width: x - options.measureGap - options.x,
525
+ height: origin - y + offsets[visible[visible.length - 1]] + limits[visible[visible.length - 1]].bottom,
526
+ overflow: Math.max(0, minimum - options.width),
527
+ measures,
528
+ visibleStaves: visible.map((j) => this.spec.staves[j].id),
529
+ restGroups: units
530
+ .filter((u) => u.last > u.first)
531
+ .map((u) => Array.from({ length: u.last - u.first + 1 }, (_, k) => u.first + k + 1)),
532
+ };
533
+ if (row.overflow)
534
+ this.diagnostics.push({ code: 'RowOverflow', measures });
535
+ this.decorate(units, visible, first === 0, y + 15);
536
+ for (const chord of this.splitChords) {
537
+ if (chord.measure < first || chord.measure > last)
538
+ continue;
539
+ const a = chord.upper, b = chord.lower, x = a.getStemX(), top = Math.max(...a.getYs()), bottom = Math.min(...b.getYs());
540
+ if (Math.abs(x - b.getStemX()) > Stem.WIDTH)
541
+ this.diagnostics.push({ code: 'SplitStemAlignment', measures: [chord.measure + 1], source: chord.id });
542
+ else
543
+ this.drawing.push((ctx) => {
544
+ ctx.fillRect(x - Stem.WIDTH / 2, top, Stem.WIDTH, bottom - top);
545
+ });
546
+ }
547
+ for (const s of spans) {
548
+ const staff = s.first.staff;
549
+ const lane = (_b = lanes.get(s)) !== null && _b !== void 0 ? _b : 0;
550
+ const yBase = origin +
551
+ offsets[staff] +
552
+ (spanAbove(s.spec) ? ink[staff].top - lane * 25 - 4 : ink[staff].bottom + lane * 25 + 14);
553
+ this.addFragment(s, units, row, yBase);
554
+ }
555
+ return row;
556
+ }
557
+ addFragment(s, units, row, baseline) {
558
+ var _a, _b;
559
+ const first = units[0].first, last = units[units.length - 1].first;
560
+ const starts = s.first.measure >= first, ends = s.last.measure <= units[units.length - 1].last;
561
+ const { kind } = s.spec, above = spanAbove(s.spec);
562
+ const startStaff = this.score.staves[first][s.first.staff], endStaff = this.score.staves[last][ends ? s.last.staff : s.first.staff];
563
+ const space = startStaff.space(1), sign = above ? -1 : 1;
564
+ let x1 = starts ? s.first.note.getAbsoluteX() : startStaff.getNoteStartX() + 2;
565
+ let x2 = ends ? s.last.note.getAbsoluteX() + s.last.note.getGlyphWidth() : endStaff.getNoteEndX() - 3;
566
+ let y1 = baseline, y2 = baseline;
567
+ if (kind === 'tie' || kind === 'slur') {
568
+ if (starts)
569
+ x1 = s.first.note.getTieRightX();
570
+ if (ends)
571
+ x2 = s.last.note.getTieLeftX();
572
+ y1 =
573
+ (starts
574
+ ? s.first.note.getYs()[(_a = s.spec.fromHead) !== null && _a !== void 0 ? _a : 0]
575
+ : continuationPitch(s.first, startStaff, s.spec.fromHead)) +
576
+ sign * space * (starts ? 0.5 : 1.1);
577
+ y2 =
578
+ (ends ? s.last.note.getYs()[(_b = s.spec.toHead) !== null && _b !== void 0 ? _b : 0] : continuationPitch(s.last, endStaff, s.spec.toHead)) +
579
+ sign * space * (ends ? 0.5 : 1.1);
580
+ }
581
+ if (kind === 'volta') {
582
+ x1 = starts ? this.score.staves[s.first.measure][s.first.staff].getX() + 1 : startStaff.getX() + 1;
583
+ x2 = ends ? this.score.staves[s.last.measure][s.last.staff].getNoteEndX() : endStaff.getNoteEndX();
584
+ }
585
+ if (kind === 'palmMute' && starts && s.label) {
586
+ x1 = Math.max(startStaff.getX(), Math.min(x1, x2 - s.label.getWidth() - space));
587
+ }
588
+ if (kind === 'hyphen' && ends)
589
+ x2 = s.last.note.getAbsoluteX() - space * 0.3;
590
+ if (x2 <= x1 || (starts && s.label && x1 + s.label.getWidth() > x2)) {
591
+ this.diagnostics.push({ code: 'SpanOverflow', measures: row.measures, source: s.spec.id });
592
+ if (x2 <= x1)
593
+ return;
594
+ }
595
+ const fragment = Object.assign(Object.assign({ id: s.spec.id, kind, row: this.rows.length, part: starts ? (ends ? 'single' : 'first') : ends ? 'last' : 'middle', x1,
596
+ x2,
597
+ y1,
598
+ y2, progress: [0, 1] }, (kind === 'palmMute' && Object.assign(Object.assign({ strings: s.spec.strings }, (starts && { from: s.spec.from })), (ends && { to: s.spec.to })))), { above,
599
+ space });
600
+ if (kind === 'tie' || kind === 'slur') {
601
+ const events = units.flatMap((u) => {
602
+ const staves = this.score.staves[u.first];
603
+ return this.score.notes[u.first]
604
+ .flat(2)
605
+ .filter((n) => n instanceof StaveNote &&
606
+ (n.getStave() === staves[s.first.staff] || n.getStave() === staves[s.last.staff]));
607
+ });
608
+ const obstacles = events
609
+ .filter((n) => n !== s.first.note && n !== s.last.note)
610
+ .flatMap((n) => measureEngraving(n).parts);
611
+ if (kind === 'slur') {
612
+ const endpoint = (n, y) => n.hasStem() && n.getStemDirection() === -sign ? n.getStemExtents().topY + sign * space * 0.4 : y;
613
+ if (starts)
614
+ fragment.y1 = endpoint(s.first.note, y1);
615
+ if (ends)
616
+ fragment.y2 = endpoint(s.last.note, y2);
617
+ const within = obstacles.filter((b) => b.x + b.w > x1 && b.x < x2);
618
+ const edge = above
619
+ ? Math.min(y1, y2, ...within.map((b) => b.y)) - space * 0.6
620
+ : Math.max(y1, y2, ...within.map((b) => b.y + b.h)) + space * 0.6;
621
+ if (!starts)
622
+ fragment.y1 = edge;
623
+ if (!ends)
624
+ fragment.y2 = edge;
625
+ }
626
+ if (!fitSpan(fragment, obstacles))
627
+ this.diagnostics.push({ code: 'CurveCollision', measures: row.measures, source: s.spec.id });
628
+ }
629
+ this.fragments.push(fragment);
630
+ }
631
+ decorate(units, visible, firstRow, headerY) {
632
+ const head = this.score.staves[units[0].first];
633
+ for (const j of visible) {
634
+ const label = firstRow || this.spec.staves[j].measures[units[0].first].instrument
635
+ ? this.names[units[0].first][j].full
636
+ : this.names[units[0].first][j].short;
637
+ if (label)
638
+ this.drawing.push((ctx) => label.renderText(ctx, head[j].getX() - this.groups.length * 9 - 7 - label.getWidth(), head[j].getYForLine(2) + 4));
639
+ }
640
+ for (const [index, g] of this.groups.entries()) {
641
+ const members = visible.filter((j) => j >= g.first && j <= g.last);
642
+ if (members.length < 2)
643
+ continue;
644
+ const top = members[0], bottom = members[members.length - 1];
645
+ const connector = new StaveConnector(head[top], head[bottom]).setType(g.type).setXShift(-index * 8);
646
+ this.drawing.push((ctx) => connector.setContext(ctx).drawWithStyle());
647
+ if (g.barlines)
648
+ for (const u of units) {
649
+ const staves = this.score.staves[u.first];
650
+ const line = new StaveConnector(staves[top], staves[bottom]).setType('singleRight');
651
+ this.drawing.push((ctx) => line.setContext(ctx).drawWithStyle());
652
+ }
653
+ }
654
+ for (const u of units) {
655
+ const staves = this.score.staves[u.first];
656
+ let x = staves[visible[0]].getX() + 2;
657
+ for (const { mark, boxed } of this.headerParts(u.first, u === units[0])) {
658
+ const left = x + (boxed ? 3 : 0);
659
+ this.drawing.push((ctx) => {
660
+ if (boxed) {
661
+ const box = mark.getTextInk(left, headerY);
662
+ ctx.beginPath();
663
+ ctx.rect(box.x - 3, box.y - 3, box.w + 6, box.h + 6);
664
+ ctx.stroke();
665
+ }
666
+ mark.renderText(ctx, left, headerY);
667
+ });
668
+ x += mark.getWidth() + (boxed ? 12 : 7);
669
+ }
670
+ if (u.last > u.first)
671
+ for (const j of visible) {
672
+ const count = u.last - u.first + 1, rest = new MultiMeasureRest(count, { numberOfMeasures: count }).setStave(staves[j]);
673
+ this.drawing.push((ctx) => rest.setContext(ctx).drawWithStyle());
674
+ }
675
+ }
676
+ }
677
+ draw(context) {
678
+ for (const draw of this.drawing)
679
+ draw(context);
680
+ }
681
+ }