vexflow 5.1.2 → 5.3.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 (162) hide show
  1. package/README.md +7 -6
  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 +5325 -1730
  7. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  8. package/build/cjs/vexflow-debug.js +4228 -1545
  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/accidental.js +13 -3
  13. package/build/esm/src/articulation.js +51 -20
  14. package/build/esm/src/beam.js +170 -30
  15. package/build/esm/src/bend.js +24 -19
  16. package/build/esm/src/clef.js +3 -0
  17. package/build/esm/src/clefnote.js +5 -1
  18. package/build/esm/src/curve.js +27 -53
  19. package/build/esm/src/dot.js +14 -5
  20. package/build/esm/src/easyscore/composition.js +336 -0
  21. package/build/esm/src/easyscore/elementindex.js +81 -0
  22. package/build/esm/src/easyscore/elements.js +324 -0
  23. package/build/esm/src/easyscore/policies.js +97 -0
  24. package/build/esm/src/easyscore/rhythm.js +42 -0
  25. package/build/esm/src/easyscore.js +448 -26
  26. package/build/esm/src/element.js +17 -2
  27. package/build/esm/src/engravinggeometry.js +150 -0
  28. package/build/esm/src/engravingprofile.js +44 -0
  29. package/build/esm/src/factory.js +268 -64
  30. package/build/esm/src/formatter.js +40 -4
  31. package/build/esm/src/gracenote.js +43 -38
  32. package/build/esm/src/gracenotegroup.js +53 -56
  33. package/build/esm/src/index.js +2 -0
  34. package/build/esm/src/keysignature.js +10 -2
  35. package/build/esm/src/keysignote.js +2 -2
  36. package/build/esm/src/modifiercontext.js +11 -0
  37. package/build/esm/src/note.js +5 -0
  38. package/build/esm/src/notehead.js +11 -6
  39. package/build/esm/src/ornament.js +38 -18
  40. package/build/esm/src/parenthesis.js +1 -1
  41. package/build/esm/src/parser.js +6 -1
  42. package/build/esm/src/proportionalformatter.js +234 -0
  43. package/build/esm/src/registry.js +27 -0
  44. package/build/esm/src/sheet.js +20 -3
  45. package/build/esm/src/stave.js +9 -2
  46. package/build/esm/src/stavenote.js +140 -127
  47. package/build/esm/src/stavetie.js +39 -7
  48. package/build/esm/src/stem.js +26 -11
  49. package/build/esm/src/stemmablenote.js +6 -1
  50. package/build/esm/src/tickable.js +5 -0
  51. package/build/esm/src/tickcontext.js +19 -0
  52. package/build/esm/src/timesignature.js +5 -0
  53. package/build/esm/src/tuplet.js +60 -19
  54. package/build/esm/src/version.js +3 -3
  55. package/build/esm/src/vexflow.js +6 -1
  56. package/build/esm/tests/beam_tests.js +35 -0
  57. package/build/esm/tests/bend_tests.js +14 -1
  58. package/build/esm/tests/curve_tests.js +14 -4
  59. package/build/esm/tests/demo_repertoire_tests.js +27 -0
  60. package/build/esm/tests/demos/drums.js +54 -0
  61. package/build/esm/tests/demos/electric.js +63 -0
  62. package/build/esm/tests/demos/greensleeves.js +90 -0
  63. package/build/esm/tests/demos/joy.js +61 -0
  64. package/build/esm/tests/demos/kesh.js +80 -0
  65. package/build/esm/tests/demos/prelude.js +59 -0
  66. package/build/esm/tests/demos/quartertones.js +50 -0
  67. package/build/esm/tests/demos/saints.js +65 -0
  68. package/build/esm/tests/easyscore_tests.js +40 -0
  69. package/build/esm/tests/easyscore_types.js +65 -0
  70. package/build/esm/tests/engraving/score-layout.mjs +75 -0
  71. package/build/esm/tests/engraving/score-persistence.mjs +262 -0
  72. package/build/esm/tests/engraving/studies.mjs +279 -0
  73. package/build/esm/tests/formatter_tests.js +51 -1
  74. package/build/esm/tests/index.js +1 -0
  75. package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
  76. package/build/esm/tests/node/easyscore-rendering-tests.mjs +332 -0
  77. package/build/esm/tests/node/easyscore-tests.mjs +1024 -0
  78. package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
  79. package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
  80. package/build/esm/tests/node/proportional-spacing-tests.mjs +278 -0
  81. package/build/esm/tests/node/rendering-tests.mjs +6 -2
  82. package/build/esm/tests/threevoice_tests.js +11 -1
  83. package/build/esm/tests/vexflow_test_helpers.js +19 -10
  84. package/build/esm/tools/audit-dependencies.mjs +61 -0
  85. package/build/esm/tools/easyscore-api-audit.mjs +58 -0
  86. package/build/esm/tools/easyscore-composition.mjs +62 -0
  87. package/build/esm/tools/easyscore-example.mjs +16 -0
  88. package/build/esm/tools/easyscore-families.mjs +61 -0
  89. package/build/esm/tools/easyscore-references-example.mjs +26 -0
  90. package/build/esm/tools/easyscore-rhythm.mjs +58 -0
  91. package/build/esm/tools/easyscore-structure.mjs +88 -0
  92. package/build/esm/tools/explorer/catalog/entries.mjs +6 -0
  93. package/build/esm/tools/explorer/catalog/expression.mjs +68 -0
  94. package/build/esm/tools/explorer/catalog/helpers.mjs +29 -0
  95. package/build/esm/tools/explorer/catalog/layout.mjs +109 -0
  96. package/build/esm/tools/explorer/catalog/notes.mjs +61 -0
  97. package/build/esm/tools/explorer/catalog/staves.mjs +85 -0
  98. package/build/esm/tools/explorer/catalog/strings.mjs +25 -0
  99. package/build/esm/tools/explorer/catalog.mjs +14 -0
  100. package/build/esm/tools/score-fixtures.mjs +23 -6
  101. package/build/types/src/accidental.d.ts +6 -0
  102. package/build/types/src/articulation.d.ts +12 -1
  103. package/build/types/src/beam.d.ts +16 -0
  104. package/build/types/src/clef.d.ts +2 -0
  105. package/build/types/src/curve.d.ts +9 -0
  106. package/build/types/src/dot.d.ts +5 -0
  107. package/build/types/src/easyscore/composition.d.ts +126 -0
  108. package/build/types/src/easyscore/elementindex.d.ts +30 -0
  109. package/build/types/src/easyscore/elements.d.ts +156 -0
  110. package/build/types/src/easyscore/policies.d.ts +12 -0
  111. package/build/types/src/easyscore/rhythm.d.ts +9 -0
  112. package/build/types/src/easyscore.d.ts +153 -11
  113. package/build/types/src/element.d.ts +7 -1
  114. package/build/types/src/engravinggeometry.d.ts +31 -0
  115. package/build/types/src/engravingprofile.d.ts +20 -0
  116. package/build/types/src/factory.d.ts +97 -9
  117. package/build/types/src/formatter.d.ts +7 -1
  118. package/build/types/src/gracenote.d.ts +7 -0
  119. package/build/types/src/gracenotegroup.d.ts +7 -2
  120. package/build/types/src/index.d.ts +2 -0
  121. package/build/types/src/keysignature.d.ts +3 -0
  122. package/build/types/src/keysignote.d.ts +1 -1
  123. package/build/types/src/modifiercontext.d.ts +4 -0
  124. package/build/types/src/note.d.ts +2 -0
  125. package/build/types/src/notehead.d.ts +5 -0
  126. package/build/types/src/ornament.d.ts +7 -0
  127. package/build/types/src/parser.d.ts +5 -0
  128. package/build/types/src/proportionalformatter.d.ts +39 -0
  129. package/build/types/src/registry.d.ts +4 -0
  130. package/build/types/src/stave.d.ts +2 -0
  131. package/build/types/src/stavenote.d.ts +21 -3
  132. package/build/types/src/stavetie.d.ts +4 -0
  133. package/build/types/src/stem.d.ts +10 -1
  134. package/build/types/src/stemmablenote.d.ts +6 -1
  135. package/build/types/src/tickable.d.ts +2 -0
  136. package/build/types/src/tickcontext.d.ts +5 -0
  137. package/build/types/src/timesignature.d.ts +2 -0
  138. package/build/types/src/tuplet.d.ts +5 -48
  139. package/build/types/src/vexflow.d.ts +6 -1
  140. package/changelog/CHANGELOG.md +52 -0
  141. package/package.json +13 -3
  142. package/tests/all.html +277 -0
  143. package/tests/demo_petzold_tests.ts +435 -0
  144. package/tests/demos/drums.ts +55 -0
  145. package/tests/demos/drums.webp +0 -0
  146. package/tests/demos/electric.ts +70 -0
  147. package/tests/demos/electric.webp +0 -0
  148. package/tests/demos/greensleeves.ts +97 -0
  149. package/tests/demos/greensleeves.webp +0 -0
  150. package/tests/demos/joy.ts +65 -0
  151. package/tests/demos/joy.webp +0 -0
  152. package/tests/demos/kesh.ts +86 -0
  153. package/tests/demos/kesh.webp +0 -0
  154. package/tests/demos/prelude.ts +65 -0
  155. package/tests/demos/prelude.webp +0 -0
  156. package/tests/demos/quartertones.ts +51 -0
  157. package/tests/demos/quartertones.webp +0 -0
  158. package/tests/demos/saints.ts +70 -0
  159. package/tests/demos/saints.webp +0 -0
  160. package/tests/flow.html +550 -239
  161. package/tests/fonts.css +493 -0
  162. package/tests/minuet.webp +0 -0
@@ -77,41 +77,37 @@ export class StaveNote extends StemmableNote {
77
77
  note: notes[i],
78
78
  });
79
79
  }
80
- let voices = 0;
81
- let noteU = undefined;
82
- let noteM = undefined;
83
- let noteL = undefined;
84
- const draw = [false, false, false];
85
- for (let i = 0; i < notesList.length; i++) {
86
- draw[i] = notesList[i].note.renderOptions.draw !== false;
87
- }
88
- if (draw[0] && draw[1] && draw[2]) {
89
- voices = 3;
90
- noteU = notesList[0];
91
- noteM = notesList[1];
92
- noteL = notesList[2];
93
- }
94
- else if (draw[0] && draw[1]) {
95
- voices = 2;
96
- noteU = notesList[0];
97
- noteL = notesList[1];
98
- }
99
- else if (draw[0] && draw[2]) {
100
- voices = 2;
101
- noteU = notesList[0];
102
- noteL = notesList[2];
103
- }
104
- else if (draw[1] && draw[2]) {
105
- voices = 2;
106
- noteU = notesList[1];
107
- noteL = notesList[2];
108
- }
109
- else {
80
+ const visible = notesList.filter((item) => item.note.renderOptions.draw !== false);
81
+ if (visible.length < 2)
82
+ return true;
83
+ if (visible.length > 3) {
84
+ const columns = [];
85
+ for (const item of visible.sort((a, b) => b.maxLine - a.maxLine || b.line - a.line)) {
86
+ let column = columns.find((entry) => item.maxLine + 0.5 < entry.bottom);
87
+ if (!column) {
88
+ column = { bottom: item.minLine, width: 0, members: [] };
89
+ columns.push(column);
90
+ }
91
+ column.bottom = item.minLine;
92
+ column.width = Math.max(column.width, item.voiceShift);
93
+ column.members.push(item);
94
+ }
95
+ let x = 0;
96
+ for (const column of columns) {
97
+ for (const item of column.members)
98
+ item.note.setXShift(x);
99
+ if (column !== columns[columns.length - 1])
100
+ x += column.width + 2;
101
+ }
102
+ state.rightShift += x;
110
103
  return true;
111
104
  }
105
+ const voices = visible.length;
106
+ let noteU = visible[0];
107
+ let noteL = visible[voices - 1];
108
+ const noteM = voices === 3 ? visible[1] : undefined;
112
109
  if (voices === 2 && noteU.stemDirection === -1 && noteL.stemDirection === 1) {
113
- noteU = notesList[1];
114
- noteL = notesList[0];
110
+ [noteU, noteL] = [noteL, noteU];
115
111
  }
116
112
  const voiceXShift = Math.max(noteU.voiceShift, noteL.voiceShift);
117
113
  let xShift = 0;
@@ -157,7 +153,7 @@ export class StaveNote extends StemmableNote {
157
153
  else {
158
154
  if (noteL.stemDirection === 1) {
159
155
  noteL.stemDirection = -1;
160
- noteL.note.setStemDirection(-1);
156
+ noteL.note.setStemDirection(-1, noteL.note.isAutoStem());
161
157
  }
162
158
  }
163
159
  }
@@ -174,11 +170,11 @@ export class StaveNote extends StemmableNote {
174
170
  }
175
171
  else if (noteU.note.hasStem()) {
176
172
  noteU.stemDirection = -noteU.note.getStemDirection();
177
- noteU.note.setStemDirection(noteU.stemDirection);
173
+ noteU.note.setStemDirection(noteU.stemDirection, noteU.note.isAutoStem());
178
174
  }
179
175
  else if (noteL.note.hasStem()) {
180
176
  noteL.stemDirection = -noteL.note.getStemDirection();
181
- noteL.note.setStemDirection(noteL.stemDirection);
177
+ noteL.note.setStemDirection(noteL.stemDirection, noteL.note.isAutoStem());
182
178
  }
183
179
  }
184
180
  }
@@ -199,11 +195,11 @@ export class StaveNote extends StemmableNote {
199
195
  noteM.note.setXShift(xShift);
200
196
  if (noteL.note.hasBeam() === false) {
201
197
  noteL.stemDirection = -1;
202
- noteL.note.setStemDirection(-1);
198
+ noteL.note.setStemDirection(-1, noteL.note.isAutoStem());
203
199
  }
204
200
  if (noteU.minLine <= noteL.maxLine && noteU.note.hasBeam() === false) {
205
201
  noteU.stemDirection = 1;
206
- noteU.note.setStemDirection(1);
202
+ noteU.note.setStemDirection(1, noteU.note.isAutoStem());
207
203
  }
208
204
  }
209
205
  state.rightShift += xShift;
@@ -233,11 +229,11 @@ export class StaveNote extends StemmableNote {
233
229
  noteM.note.setXShift(xShift);
234
230
  if (noteL.note.hasBeam() === false) {
235
231
  noteL.stemDirection = -1;
236
- noteL.note.setStemDirection(-1);
232
+ noteL.note.setStemDirection(-1, noteL.note.isAutoStem());
237
233
  }
238
234
  if (noteU.minLine <= noteL.maxLine && noteU.note.hasBeam() === false) {
239
235
  noteU.stemDirection = 1;
240
- noteU.note.setStemDirection(1);
236
+ noteU.note.setStemDirection(1, noteU.note.isAutoStem());
241
237
  }
242
238
  }
243
239
  state.rightShift += xShift;
@@ -250,19 +246,24 @@ export class StaveNote extends StemmableNote {
250
246
  return true;
251
247
  }
252
248
  constructor(noteStruct) {
253
- var _a, _b, _c;
249
+ var _a, _b, _c, _d, _e;
254
250
  super(noteStruct);
255
251
  this.minLine = 0;
256
252
  this.maxLine = 0;
253
+ this.useDefaultHeadX = false;
254
+ this.extraHeadColumns = 0;
257
255
  this.sortedKeyProps = [];
256
+ this.engraving = noteStruct.engraving;
257
+ const musicFont = (_a = noteStruct.musicFont) !== null && _a !== void 0 ? _a : (_b = this.engraving) === null || _b === void 0 ? void 0 : _b.musicFont;
258
+ if (musicFont)
259
+ this.setFont(Object.assign(Object.assign({}, this.fontInfo), { family: musicFont }));
258
260
  this.ledgerLineStyle = {};
259
- this.clef = (_a = noteStruct.clef) !== null && _a !== void 0 ? _a : 'treble';
260
- this.octaveShift = (_b = noteStruct.octaveShift) !== null && _b !== void 0 ? _b : 0;
261
+ this.clef = (_c = noteStruct.clef) !== null && _c !== void 0 ? _c : 'treble';
262
+ this.octaveShift = (_d = noteStruct.octaveShift) !== null && _d !== void 0 ? _d : 0;
261
263
  this.glyphProps = Note.getGlyphProps(this.duration, this.noteType);
262
264
  defined(this.glyphProps, 'BadArguments', `No glyph found for duration '${this.duration}' and type '${this.noteType}'`);
263
265
  this.displaced = false;
264
266
  this.dotShiftY = 0;
265
- this.useDefaultHeadX = false;
266
267
  this._noteHeads = [];
267
268
  this.modifiers = [];
268
269
  this.renderOptions = Object.assign(Object.assign({}, this.renderOptions), { strokePx: noteStruct.strokePx || StaveNote.LEDGER_LINE_OFFSET });
@@ -272,7 +273,7 @@ export class StaveNote extends StemmableNote {
272
273
  this.autoStem();
273
274
  }
274
275
  else {
275
- this.setStemDirection((_c = noteStruct.stemDirection) !== null && _c !== void 0 ? _c : Stem.UP);
276
+ this.setStemDirection((_e = noteStruct.stemDirection) !== null && _e !== void 0 ? _e : Stem.UP);
276
277
  }
277
278
  }
278
279
  reset() {
@@ -309,9 +310,8 @@ export class StaveNote extends StemmableNote {
309
310
  this._noteHeads = [];
310
311
  const stemDirection = this.getStemDirection();
311
312
  const keys = this.getKeys();
312
- let lastLine = undefined;
313
- let lineDiff = undefined;
314
- let displaced = false;
313
+ const lastLineInColumn = [];
314
+ this.extraHeadColumns = 0;
315
315
  let start;
316
316
  let end;
317
317
  let step;
@@ -328,24 +328,17 @@ export class StaveNote extends StemmableNote {
328
328
  for (let i = start; i !== end; i += step) {
329
329
  const noteProps = this.sortedKeyProps[i].keyProps;
330
330
  const line = noteProps.line;
331
- if (lastLine === undefined) {
332
- lastLine = line;
333
- }
334
- else {
335
- lineDiff = Math.abs(lastLine - line);
336
- if (lineDiff === 0 || lineDiff === 0.5) {
337
- displaced = !displaced;
338
- }
339
- else {
340
- displaced = false;
341
- this.useDefaultHeadX = true;
342
- }
343
- }
344
- lastLine = line;
331
+ let column = lastLineInColumn.findIndex((previous) => Math.abs(previous - line) >= 1);
332
+ if (i !== start && Math.abs(this.sortedKeyProps[i - step].keyProps.line - line) >= 1)
333
+ this.useDefaultHeadX = true;
334
+ if (column < 0)
335
+ column = lastLineInColumn.length;
336
+ lastLineInColumn[column] = line;
337
+ this.extraHeadColumns = Math.max(this.extraHeadColumns, column);
345
338
  const notehead = new NoteHead({
346
339
  duration: this.duration,
347
340
  noteType: this.noteType,
348
- displaced,
341
+ displacementColumn: column,
349
342
  stemDirection,
350
343
  customGlyphCode: noteProps.code,
351
344
  line: noteProps.line,
@@ -357,7 +350,7 @@ export class StaveNote extends StemmableNote {
357
350
  return this._noteHeads;
358
351
  }
359
352
  autoStem() {
360
- this.setStemDirection(this.calculateOptimalStemDirection());
353
+ this.setStemDirection(this.calculateOptimalStemDirection(), true);
361
354
  }
362
355
  calculateOptimalStemDirection() {
363
356
  this.minLine = this.sortedKeyProps[0].keyProps.line;
@@ -369,7 +362,6 @@ export class StaveNote extends StemmableNote {
369
362
  }
370
363
  calculateKeyProps() {
371
364
  var _a;
372
- let lastLine;
373
365
  for (let i = 0; i < this.keys.length; ++i) {
374
366
  const key = this.keys[i];
375
367
  const options = { octaveShift: (_a = this.octaveShift) !== null && _a !== void 0 ? _a : 0, duration: this.duration };
@@ -385,33 +377,44 @@ export class StaveNote extends StemmableNote {
385
377
  props.line = 3;
386
378
  }
387
379
  }
388
- const line = props.line;
389
- if (lastLine === undefined) {
390
- lastLine = line;
391
- }
392
- else {
393
- if (Math.abs(lastLine - line) < 1) {
394
- this.displaced = true;
395
- props.displaced = true;
396
- if (this.keyProps.length > 0) {
397
- this.keyProps[i - 1].displaced = true;
398
- }
399
- }
400
- }
401
- lastLine = line;
402
380
  this.keyProps.push(props);
403
381
  }
404
382
  this.keyProps.forEach((keyProps, index) => {
405
383
  this.sortedKeyProps.push({ keyProps, index });
406
384
  });
407
385
  this.sortedKeyProps.sort((a, b) => a.keyProps.line - b.keyProps.line);
386
+ for (let i = 1; i < this.sortedKeyProps.length; i++) {
387
+ const lower = this.sortedKeyProps[i - 1].keyProps;
388
+ const upper = this.sortedKeyProps[i].keyProps;
389
+ if (upper.line - lower.line < 1) {
390
+ this.displaced = true;
391
+ lower.displaced = upper.displaced = true;
392
+ }
393
+ }
394
+ }
395
+ getHeadInk() {
396
+ return this._noteHeads.map((head) => {
397
+ const displacement = head.getDisplacementX();
398
+ return head.getTextInk(this.getNoteHeadBeginX() + displacement, head.getY());
399
+ });
408
400
  }
409
- getBoundingBox() {
401
+ getFlagInk() {
402
+ if (!this.shouldDrawFlag())
403
+ return;
404
+ const { x, y } = this.getFlagPosition();
405
+ return this.flag.getTextInk(x, y);
406
+ }
407
+ getStemSegment() {
408
+ var _a;
409
+ if (!((_a = this.stem) === null || _a === void 0 ? void 0 : _a.isVisible()) || (!this.hasStem() && !this.beam))
410
+ return;
411
+ const adjustment = this.shouldDrawFlag() ? Metrics.get('Stem.heightAdjustmentForFlag', -3) : undefined;
412
+ return this.checkStem().getSegment(this.getStemX(), adjustment);
413
+ }
414
+ getBoundingBox(includeModifiers = true) {
410
415
  const boundingBox = new BoundingBox(this.getAbsoluteX(), this.ys[0], 0, 0);
411
416
  this._noteHeads.forEach((notehead) => {
412
- const displacement = notehead.isDisplaced()
413
- ? (notehead.getWidth() - Stem.WIDTH / 2) * this.getStemDirection()
414
- : 0;
417
+ const displacement = notehead.getDisplacementX();
415
418
  const box = notehead.getBoundingBox();
416
419
  box.setX(this.getNoteHeadBeginX() + displacement + notehead.getXShift());
417
420
  boundingBox.mergeWith(box);
@@ -431,9 +434,9 @@ export class StaveNote extends StemmableNote {
431
434
  bbFlag.setY(y + this.flag.getYShift() - this.flag.getTextMetrics().actualBoundingBoxAscent);
432
435
  boundingBox.mergeWith(bbFlag);
433
436
  }
434
- for (let i = 0; i < this.modifiers.length; i++) {
435
- boundingBox.mergeWith(this.modifiers[i].getBoundingBox());
436
- }
437
+ if (includeModifiers)
438
+ for (const modifier of this.modifiers)
439
+ boundingBox.mergeWith(modifier.getBoundingBox());
437
440
  return boundingBox;
438
441
  }
439
442
  getLineNumber(isTopNote) {
@@ -468,10 +471,21 @@ export class StaveNote extends StemmableNote {
468
471
  return super.hasFlag() && !this.isRest();
469
472
  }
470
473
  getStemX() {
474
+ var _a;
471
475
  if (this.noteType === 'r') {
472
476
  return this.getCenterGlyphX();
473
477
  }
474
478
  else {
479
+ const up = this.stemDirection === Stem.UP;
480
+ const head = this.engraving && this._noteHeads.find((h) => !h.isDisplaced());
481
+ if (head && ((_a = this.engraving) === null || _a === void 0 ? void 0 : _a.musicFont) === head.fontInfo.family) {
482
+ const anchor = this.engraving.stemAnchor(head.text, up ? 'up' : 'down');
483
+ if (anchor)
484
+ return (this.getNoteHeadBeginX() +
485
+ head.getXShift() +
486
+ (anchor[0] * head.fontSizeInPixels) / 4 +
487
+ ((up ? -1 : 1) * Stem.WIDTH) / 2);
488
+ }
475
489
  return super.getStemX() + (this.stemDirection ? Stem.WIDTH / (2 * -this.stemDirection) : 0);
476
490
  }
477
491
  }
@@ -620,11 +634,12 @@ export class StaveNote extends StemmableNote {
620
634
  return this.keyProps[index].line;
621
635
  }
622
636
  getVoiceShiftWidth() {
623
- return this.getGlyphWidth() * (this.displaced ? 2 : 1);
637
+ return this.getGlyphWidth() * (this.displaced ? 1 + Math.max(1, this.extraHeadColumns) : 1);
624
638
  }
625
639
  calcNoteDisplacements() {
626
- this.setLeftDisplacedHeadPx(this.displaced && this.stemDirection === Stem.DOWN ? this.getGlyphWidth() : 0);
627
- this.setRightDisplacedHeadPx(!this.hasFlag() && this.displaced && this.stemDirection === Stem.UP ? this.getGlyphWidth() : 0);
640
+ const extra = this.getVoiceShiftWidth() - this.getGlyphWidth();
641
+ this.setLeftDisplacedHeadPx(this.stemDirection === Stem.DOWN ? extra : 0);
642
+ this.setRightDisplacedHeadPx(this.stemDirection === Stem.UP ? Math.max(0, extra - (this.hasFlag() ? this.getGlyphWidth() : 0)) : 0);
628
643
  }
629
644
  preFormat() {
630
645
  if (this.preFormatted)
@@ -650,7 +665,10 @@ export class StaveNote extends StemmableNote {
650
665
  this._noteHeads.forEach((nh) => {
651
666
  const offset = Tables.noteHeadStemYOffsets[nh.text];
652
667
  const anchor = offset ? offset[anchorKey] : 0;
653
- const y = nh.getY() - anchor * Tables.STAVE_LINE_DISTANCE;
668
+ const fontAnchor = this.engraving && this.engraving.musicFont === nh.fontInfo.family
669
+ ? this.engraving.stemAnchor(nh.text, anchorKey)
670
+ : undefined;
671
+ const y = nh.getY() - (fontAnchor ? (fontAnchor[1] * nh.fontSizeInPixels) / 4 : anchor * Tables.STAVE_LINE_DISTANCE);
654
672
  yTop = Math.min(y, yTop);
655
673
  yBottom = Math.max(y, yBottom);
656
674
  });
@@ -660,7 +678,7 @@ export class StaveNote extends StemmableNote {
660
678
  }
661
679
  return { yTop, yBottom };
662
680
  }
663
- getNoteHeadBounds() {
681
+ getNoteHeadBounds(layoutX = false) {
664
682
  let yTop = +Infinity;
665
683
  let yBottom = -Infinity;
666
684
  let nonDisplacedX;
@@ -677,10 +695,10 @@ export class StaveNote extends StemmableNote {
677
695
  yTop = Math.min(y, yTop);
678
696
  yBottom = Math.max(y, yBottom);
679
697
  if (displacedX === undefined && notehead.isDisplaced()) {
680
- displacedX = notehead.getAbsoluteX();
698
+ displacedX = layoutX ? this.getNoteHeadBeginX() + notehead.getDisplacementX() : notehead.getAbsoluteX();
681
699
  }
682
700
  if (nonDisplacedX === undefined && !notehead.isDisplaced()) {
683
- nonDisplacedX = notehead.getAbsoluteX();
701
+ nonDisplacedX = layoutX ? this.getNoteHeadBeginX() : notehead.getAbsoluteX();
684
702
  }
685
703
  highestLine = Math.max(line, highestLine);
686
704
  lowestLine = Math.min(line, lowestLine);
@@ -716,47 +734,42 @@ export class StaveNote extends StemmableNote {
716
734
  get noteHeads() {
717
735
  return this._noteHeads.slice();
718
736
  }
719
- drawLedgerLines() {
737
+ getLedgerLines() {
738
+ const lines = [];
720
739
  const stave = this.checkStave();
721
- const { renderOptions: { strokePx }, } = this;
722
- const ctx = this.checkContext();
723
- const width = this.getGlyphWidth() + strokePx * 2;
724
- const doubleWidth = 2 * (this.getGlyphWidth() + strokePx) - Stem.WIDTH / 2;
740
+ const { strokePx } = this.renderOptions;
725
741
  if (this.isRest())
742
+ return lines;
743
+ const { highestLine, lowestLine } = this.getNoteHeadBounds(true);
744
+ const addLedger = (line, heads) => {
745
+ if (!heads.length)
746
+ return;
747
+ const left = Math.min(...heads.map((head) => head.getDisplacementX()));
748
+ const right = Math.max(...heads.map((head) => head.getDisplacementX() + head.getWidth()));
749
+ lines.push({
750
+ x: this.getNoteHeadBeginX() + left - strokePx,
751
+ y: stave.getYForNote(line),
752
+ width: right - left + 2 * strokePx,
753
+ });
754
+ };
755
+ for (let line = 6; line <= highestLine; ++line)
756
+ addLedger(line, this._noteHeads.filter((head) => head.getLine() >= line));
757
+ for (let line = 0; line >= lowestLine; --line)
758
+ addLedger(line, this._noteHeads.filter((head) => head.getLine() <= line));
759
+ return lines;
760
+ }
761
+ drawLedgerLines() {
762
+ const lines = this.getLedgerLines();
763
+ if (!lines.length)
726
764
  return;
727
- if (!ctx) {
728
- throw new RuntimeError('NoCanvasContext', "Can't draw without a canvas context.");
729
- }
730
- const { highestLine, lowestLine, highestDisplacedLine, highestNonDisplacedLine, lowestDisplacedLine, lowestNonDisplacedLine, displacedX, nonDisplacedX, } = this.getNoteHeadBounds();
731
- if (highestLine < 6 && lowestLine > 0)
732
- return;
733
- const minX = Math.min(displacedX !== null && displacedX !== void 0 ? displacedX : 0, nonDisplacedX !== null && nonDisplacedX !== void 0 ? nonDisplacedX : 0);
734
- const drawLedgerLine = (y, normal, displaced) => {
735
- let x;
736
- if (displaced && normal)
737
- x = minX - strokePx;
738
- else if (normal)
739
- x = (nonDisplacedX !== null && nonDisplacedX !== void 0 ? nonDisplacedX : 0) - strokePx;
740
- else
741
- x = (displacedX !== null && displacedX !== void 0 ? displacedX : 0) - strokePx;
742
- const ledgerWidth = normal && displaced ? doubleWidth : width;
765
+ const ctx = this.checkContext();
766
+ ctx.save();
767
+ this.applyStyle(ctx, Object.assign(Object.assign({}, this.checkStave().getDefaultLedgerLineStyle()), this.getLedgerLineStyle()));
768
+ for (const { x, y, width } of lines) {
743
769
  ctx.beginPath();
744
770
  ctx.moveTo(x, y);
745
- ctx.lineTo(x + ledgerWidth, y);
771
+ ctx.lineTo(x + width, y);
746
772
  ctx.stroke();
747
- };
748
- const style = Object.assign(Object.assign({}, stave.getDefaultLedgerLineStyle()), this.getLedgerLineStyle());
749
- ctx.save();
750
- this.applyStyle(ctx, style);
751
- for (let line = 6; line <= highestLine; ++line) {
752
- const normal = nonDisplacedX !== undefined && line <= highestNonDisplacedLine;
753
- const displaced = highestDisplacedLine !== undefined && line <= highestDisplacedLine;
754
- drawLedgerLine(stave.getYForNote(line), normal, displaced);
755
- }
756
- for (let line = 0; line >= lowestLine; --line) {
757
- const normal = nonDisplacedX !== undefined && line >= lowestNonDisplacedLine;
758
- const displaced = lowestDisplacedLine !== undefined && line >= lowestDisplacedLine;
759
- drawLedgerLine(stave.getYForNote(line), normal, displaced);
760
773
  }
761
774
  ctx.restore();
762
775
  }
@@ -1,3 +1,4 @@
1
+ import { BoundingBox } from './boundingbox.js';
1
2
  import { Element } from './element.js';
2
3
  import { RuntimeError } from './util.js';
3
4
  export class StaveTie extends Element {
@@ -58,11 +59,10 @@ export class StaveTie extends Element {
58
59
  isPartial() {
59
60
  return !this.notes.firstNote || !this.notes.lastNote;
60
61
  }
61
- renderTie(params) {
62
+ *tieCurves(params) {
62
63
  if (params.firstYs.length === 0 || params.lastYs.length === 0) {
63
64
  throw new RuntimeError('BadArguments', 'No Y-values to render');
64
65
  }
65
- const ctx = this.checkContext();
66
66
  let cp1 = this.renderOptions.cp1;
67
67
  let cp2 = this.renderOptions.cp2;
68
68
  if (Math.abs(params.lastX - params.firstX) < this.renderOptions.shortTieCutoff) {
@@ -74,8 +74,6 @@ export class StaveTie extends Element {
74
74
  const yShift = this.renderOptions.yShift * params.direction;
75
75
  const firstIndexes = this.notes.firstIndexes;
76
76
  const lastIndexes = this.notes.lastIndexes;
77
- const clsAttribute = this.getAttribute('class');
78
- ctx.openGroup('stavetie' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
79
77
  for (let i = 0; i < firstIndexes.length; ++i) {
80
78
  const cpX = (params.lastX + lastXShift + (params.firstX + firstXShift)) / 2;
81
79
  const firstY = params.firstYs[firstIndexes[i]] + yShift;
@@ -85,10 +83,44 @@ export class StaveTie extends Element {
85
83
  }
86
84
  const topControlPointY = (firstY + lastY) / 2 + cp1 * params.direction;
87
85
  const bottomControlPointY = (firstY + lastY) / 2 + cp2 * params.direction;
86
+ yield {
87
+ x1: params.firstX + firstXShift,
88
+ x2: params.lastX + lastXShift,
89
+ firstY,
90
+ lastY,
91
+ cpX,
92
+ topControlPointY,
93
+ bottomControlPointY,
94
+ };
95
+ }
96
+ }
97
+ getRibbonBounds() {
98
+ this.synchronizeIndexes();
99
+ const curves = this.tieCurves({
100
+ firstX: this.getFirstX(),
101
+ lastX: this.getLastX(),
102
+ firstYs: this.getFirstYs(),
103
+ lastYs: this.getLastYs(),
104
+ direction: this.getDirection(),
105
+ });
106
+ let box;
107
+ for (const { x1, x2, firstY, lastY, topControlPointY, bottomControlPointY } of curves) {
108
+ const top = Math.min(firstY, lastY, topControlPointY, bottomControlPointY);
109
+ const bottom = Math.max(firstY, lastY, topControlPointY, bottomControlPointY);
110
+ const part = new BoundingBox(Math.min(x1, x2), top, Math.abs(x2 - x1), bottom - top);
111
+ box = box ? box.mergeWith(part) : part;
112
+ }
113
+ return box !== null && box !== void 0 ? box : new BoundingBox(this.getFirstX(), this.getFirstYs()[0], 0, 0);
114
+ }
115
+ renderTie(params) {
116
+ const ctx = this.checkContext();
117
+ const clsAttribute = this.getAttribute('class');
118
+ ctx.openGroup('stavetie' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
119
+ for (const { x1, x2, firstY, lastY, cpX, topControlPointY, bottomControlPointY } of this.tieCurves(params)) {
88
120
  ctx.beginPath();
89
- ctx.moveTo(params.firstX + firstXShift, firstY);
90
- ctx.quadraticCurveTo(cpX, topControlPointY, params.lastX + lastXShift, lastY);
91
- ctx.quadraticCurveTo(cpX, bottomControlPointY, params.firstX + firstXShift, firstY);
121
+ ctx.moveTo(x1, firstY);
122
+ ctx.quadraticCurveTo(cpX, topControlPointY, x2, lastY);
123
+ ctx.quadraticCurveTo(cpX, bottomControlPointY, x1, firstY);
92
124
  ctx.closePath();
93
125
  ctx.fill();
94
126
  }
@@ -1,7 +1,8 @@
1
+ import { BoundingBox } from './boundingbox.js';
1
2
  import { Element } from './element.js';
2
3
  import { Metrics } from './metrics.js';
3
4
  import { Tables } from './tables.js';
4
- import { log, RuntimeError } from './util.js';
5
+ import { log } from './util.js';
5
6
  function L(...args) {
6
7
  if (Stem.DEBUG)
7
8
  log('VexFlow.Stem', args);
@@ -71,9 +72,6 @@ export class Stem extends Element {
71
72
  const unsignedHeight = this.yBottom - this.yTop + (Stem.HEIGHT - yOffset + this.stemExtension);
72
73
  return unsignedHeight * this.stemDirection;
73
74
  }
74
- getBoundingBox() {
75
- throw new RuntimeError('NotImplemented', 'getBoundingBox() not implemented.');
76
- }
77
75
  getExtents() {
78
76
  const isStemUp = this.stemDirection === Stem.UP;
79
77
  const ys = [this.yTop, this.yBottom];
@@ -83,6 +81,9 @@ export class Stem extends Element {
83
81
  const stemTipY = innerMostNoteheadY + stemHeight * -this.stemDirection;
84
82
  return { topY: stemTipY, baseY: outerMostNoteheadY };
85
83
  }
84
+ isVisible() {
85
+ return !this.hide;
86
+ }
86
87
  setVisibility(isVisible) {
87
88
  this.hide = !isVisible;
88
89
  return this;
@@ -98,11 +99,7 @@ export class Stem extends Element {
98
99
  adjustHeightForBeam() {
99
100
  this.renderHeightAdjustment = -Stem.WIDTH / 2;
100
101
  }
101
- draw() {
102
- this.setRendered();
103
- if (this.hide)
104
- return;
105
- const ctx = this.checkContext();
102
+ getSegment(x, adjustment = this.renderHeightAdjustment) {
106
103
  let stemX;
107
104
  let stemY;
108
105
  const stemDirection = this.stemDirection;
@@ -120,12 +117,30 @@ export class Stem extends Element {
120
117
  const stemHeight = this.getHeight();
121
118
  L('Rendering stem - ', 'Top Y: ', this.yTop, 'Bottom Y: ', this.yBottom);
122
119
  const stemletYOffset = this.isStemlet ? stemHeight - this.stemletHeight * this.stemDirection : 0;
120
+ return {
121
+ x: x !== null && x !== void 0 ? x : stemX,
122
+ baseY: stemY - stemletYOffset + yBaseOffset,
123
+ tipY: stemY - stemHeight - adjustment * stemDirection,
124
+ };
125
+ }
126
+ getBoundingBox() {
127
+ const { x, baseY, tipY } = this.getSegment();
128
+ if (this.hide)
129
+ return new BoundingBox(x, baseY, 0, 0);
130
+ return new BoundingBox(x - Stem.WIDTH / 2, Math.min(baseY, tipY), Stem.WIDTH, Math.abs(baseY - tipY));
131
+ }
132
+ draw() {
133
+ this.setRendered();
134
+ if (this.hide)
135
+ return;
136
+ const ctx = this.checkContext();
137
+ const { x, baseY, tipY } = this.getSegment();
123
138
  const clsAttribute = this.getAttribute('class');
124
139
  ctx.openGroup('stem' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
125
140
  ctx.beginPath();
126
141
  ctx.setLineWidth(Stem.WIDTH);
127
- ctx.moveTo(stemX, stemY - stemletYOffset + yBaseOffset);
128
- ctx.lineTo(stemX, stemY - stemHeight - this.renderHeightAdjustment * stemDirection);
142
+ ctx.moveTo(x, baseY);
143
+ ctx.lineTo(x, tipY);
129
144
  ctx.stroke();
130
145
  ctx.closeGroup();
131
146
  }
@@ -10,6 +10,7 @@ export class StemmableNote extends Note {
10
10
  }
11
11
  constructor(noteStruct) {
12
12
  super(noteStruct);
13
+ this.autoStemEnabled = false;
13
14
  this.flag = new Flag();
14
15
  }
15
16
  getStem() {
@@ -99,13 +100,17 @@ export class StemmableNote extends Note {
99
100
  throw new RuntimeError('NoStem', 'No stem attached to this note.');
100
101
  return this.stemDirection;
101
102
  }
102
- setStemDirection(direction) {
103
+ isAutoStem() {
104
+ return this.autoStemEnabled;
105
+ }
106
+ setStemDirection(direction, autoStem = false) {
103
107
  if (!direction)
104
108
  direction = Stem.UP;
105
109
  if (direction !== Stem.UP && direction !== Stem.DOWN) {
106
110
  throw new RuntimeError('BadArgument', `Invalid stem direction: ${direction}`);
107
111
  }
108
112
  this.stemDirection = direction;
113
+ this.autoStemEnabled = autoStem;
109
114
  this.reset();
110
115
  if (this.hasFlag()) {
111
116
  this.buildFlag();
@@ -80,6 +80,11 @@ export class Tickable extends Element {
80
80
  setVoice(voice) {
81
81
  this.voice = voice;
82
82
  }
83
+ clearVoice() {
84
+ this.voice = undefined;
85
+ this.preFormatted = false;
86
+ return this;
87
+ }
83
88
  getTuplet() {
84
89
  return this.tupletStack.at(-1);
85
90
  }
@@ -1,6 +1,25 @@
1
1
  import { Fraction } from './fraction.js';
2
2
  import { RuntimeError, sumArray } from './util.js';
3
3
  export class TickContext {
4
+ static linkTimeline(contexts) {
5
+ contexts.forEach((context, i) => {
6
+ context.nextAttack = contexts[i + 1];
7
+ });
8
+ }
9
+ *through(last) {
10
+ if (this.tickID > last.tickID)
11
+ return;
12
+ yield this;
13
+ if (this === last)
14
+ return;
15
+ let context = this.nextAttack;
16
+ while (context && context.tickID <= last.tickID) {
17
+ yield context;
18
+ if (context === last)
19
+ return;
20
+ context = context.nextAttack;
21
+ }
22
+ }
4
23
  static getNextContext(tContext) {
5
24
  const contexts = tContext.tContexts;
6
25
  const index = contexts.indexOf(tContext);
@@ -34,6 +34,11 @@ export class TimeSignature extends StaveModifier {
34
34
  this.setTimeSig(timeSpec);
35
35
  this.setPadding(padding);
36
36
  }
37
+ setMusicFont(family) {
38
+ for (const element of [this, this.topText, this.botText])
39
+ element.setFont(Object.assign(Object.assign({}, element.fontInfo), { family }));
40
+ return this.setTimeSig(this.timeSpec);
41
+ }
37
42
  static getTimeSigCode(key, smallSig = false) {
38
43
  let code = Glyphs.null;
39
44
  switch (key) {