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
@@ -2,6 +2,7 @@ import { Beam } from './beam.js';
2
2
  import { Fraction } from './fraction.js';
3
3
  import { Metrics } from './metrics.js';
4
4
  import { ModifierContext } from './modifiercontext.js';
5
+ import { ProportionalFormatter } from './proportionalformatter.js';
5
6
  import { Stave } from './stave.js';
6
7
  import { StaveConnector } from './staveconnector.js';
7
8
  import { Tables } from './tables.js';
@@ -139,6 +140,9 @@ export class Formatter {
139
140
  }
140
141
  constructor(options) {
141
142
  this.formatterOptions = Object.assign({ globalSoftmax: false, softmaxFactor: Tables.SOFTMAX_FACTOR, maxIterations: 5 }, options);
143
+ if ((options === null || options === void 0 ? void 0 : options.spacing) && !['proportional', 'optical'].includes(options.spacing.mode)) {
144
+ throw new RuntimeError('BadArgument', 'Unknown formatter spacing mode.');
145
+ }
142
146
  this.justifyWidth = 0;
143
147
  this.totalCost = 0;
144
148
  this.totalShift = 0;
@@ -166,6 +170,12 @@ export class Formatter {
166
170
  voices.forEach((voice) => Formatter.AlignRestsToNotes(voice.getTickables(), alignAllNotes));
167
171
  }
168
172
  preCalculateMinTotalWidth(voices) {
173
+ if (this.formatterOptions.spacing) {
174
+ this.createTickContexts(voices);
175
+ this.minTotalWidth = this.proportional.minimumWidth(this.formatterOptions.spacing);
176
+ this.hasMinTotalWidth = true;
177
+ return this.minTotalWidth;
178
+ }
169
179
  const unalignedPadding = Metrics.get('Stave.unalignedNotePadding');
170
180
  let unalignedCtxCount = 0;
171
181
  let wsum = 0;
@@ -249,7 +259,8 @@ export class Formatter {
249
259
  staveTickToContextMap[integerTicks] = newContext;
250
260
  }
251
261
  tickable.addToModifierContext(staveTickToContextMap[integerTicks]);
252
- ticksUsed.add(tickable.getTicks());
262
+ if (!this.formatterOptions.spacing || !tickable.shouldIgnoreTicks())
263
+ ticksUsed.add(tickable.getTicks());
253
264
  });
254
265
  });
255
266
  this.modifierContexts.push({
@@ -260,6 +271,8 @@ export class Formatter {
260
271
  }
261
272
  createTickContexts(voices) {
262
273
  if (voices.length === 0) {
274
+ if (this.formatterOptions.spacing)
275
+ throw new RuntimeError('BadArgument', 'No voices to format');
263
276
  return {
264
277
  map: {},
265
278
  array: [],
@@ -267,6 +280,11 @@ export class Formatter {
267
280
  resolutionMultiplier: 0,
268
281
  };
269
282
  }
283
+ if (this.formatterOptions.spacing) {
284
+ this.proportional = new ProportionalFormatter(voices, Formatter.getResolutionMultiplier(voices));
285
+ this.tickContexts = this.proportional.contexts;
286
+ return this.tickContexts;
287
+ }
270
288
  const tickToContextMap = {};
271
289
  const tickList = [];
272
290
  const tickContexts = [];
@@ -292,6 +310,7 @@ export class Formatter {
292
310
  list: tickList.sort((a, b) => a - b),
293
311
  resolutionMultiplier,
294
312
  };
313
+ TickContext.linkTimeline(contexts.list.map((tick) => contexts.map[tick]));
295
314
  this.tickContexts = contexts;
296
315
  const contextArray = this.tickContexts.array;
297
316
  contextArray.forEach((context) => {
@@ -312,6 +331,13 @@ export class Formatter {
312
331
  if (voicesParam && stave) {
313
332
  voicesParam.forEach((voice) => voice.setStave(stave).preFormat());
314
333
  }
334
+ if (this.proportional && this.formatterOptions.spacing) {
335
+ const spacing = this.formatterOptions.spacing;
336
+ this.minTotalWidth = this.proportional.minimumWidth(spacing);
337
+ this.justifyWidth = this.proportional.format(spacing, justifyWidth);
338
+ this.hasMinTotalWidth = true;
339
+ return this.evaluate();
340
+ }
315
341
  let x = 0;
316
342
  let shift = 0;
317
343
  this.minTotalWidth = 0;
@@ -539,6 +565,8 @@ export class Formatter {
539
565
  }
540
566
  tune(options) {
541
567
  var _a;
568
+ if (this.proportional)
569
+ return this.totalCost;
542
570
  const contexts = this.tickContexts;
543
571
  if (!contexts) {
544
572
  return 0;
@@ -572,9 +600,10 @@ export class Formatter {
572
600
  this.modifierContexts.forEach((modifierContexts) => {
573
601
  modifierContexts.array.forEach((mc) => mc.postFormat());
574
602
  });
575
- this.tickContexts.list.forEach((tick) => {
576
- this.tickContexts.map[tick].postFormat();
577
- });
603
+ const contexts = this.proportional
604
+ ? this.tickContexts.array
605
+ : this.tickContexts.list.map((tick) => this.tickContexts.map[tick]);
606
+ contexts.forEach((context) => context.postFormat());
578
607
  return this;
579
608
  }
580
609
  joinVoices(voices) {
@@ -583,8 +612,15 @@ export class Formatter {
583
612
  return this;
584
613
  }
585
614
  format(voices, justifyWidth, options) {
615
+ var _a;
586
616
  const opts = Object.assign({ alignRests: false }, options);
587
617
  this.voices = voices;
618
+ for (const voice of voices) {
619
+ for (const tickable of voice.getTickables()) {
620
+ if (isNote(tickable))
621
+ (_a = tickable.getBeam()) === null || _a === void 0 ? void 0 : _a.resetLayout();
622
+ }
623
+ }
588
624
  const softmaxFactor = this.formatterOptions.softmaxFactor;
589
625
  if (softmaxFactor) {
590
626
  this.voices.forEach((v) => v.setSoftmaxFactor(softmaxFactor));
@@ -24,47 +24,52 @@ export class GraceNote extends StaveNote {
24
24
  ret = Stem.HEIGHT * this.getFontScale() - Stem.HEIGHT + ret;
25
25
  return ret;
26
26
  }
27
- draw() {
28
- super.draw();
29
- this.setRendered();
30
- const stem = this.stem;
31
- if (this.slash && stem) {
32
- const scale = this.getFontScale();
33
- let slashBBox = undefined;
34
- const beam = this.beam;
35
- if (beam) {
36
- if (!beam.postFormatted) {
37
- beam.postFormat();
38
- }
39
- slashBBox = this.calcBeamedNotesSlashBBox(8 * scale, 8 * scale, {
40
- stem: 6 * scale,
41
- beam: 5 * scale,
42
- });
27
+ getSlash() {
28
+ if (!this.slash || !this.stem)
29
+ return;
30
+ const scale = this.getFontScale();
31
+ let slashBBox = undefined;
32
+ const beam = this.beam;
33
+ if (beam) {
34
+ if (!beam.postFormatted) {
35
+ beam.postFormat();
36
+ }
37
+ slashBBox = this.calcBeamedNotesSlashBBox(8 * scale, 8 * scale, {
38
+ stem: 6 * scale,
39
+ beam: 5 * scale,
40
+ });
41
+ }
42
+ else {
43
+ const stemDirection = this.getStemDirection();
44
+ const noteHeadBounds = this.getNoteHeadBounds();
45
+ const noteHeadWidth = this.noteHeads[0].getWidth();
46
+ const x = stemDirection === Stem.DOWN ? this.getAbsoluteX() : this.getAbsoluteX() + noteHeadWidth;
47
+ const defaultOffsetY = (Tables.STEM_HEIGHT * scale) / 2;
48
+ const y = stemDirection === Stem.DOWN ? noteHeadBounds.yBottom + defaultOffsetY : noteHeadBounds.yTop - defaultOffsetY;
49
+ if (stemDirection === Stem.DOWN) {
50
+ slashBBox = {
51
+ x1: x - noteHeadWidth,
52
+ y1: y - noteHeadWidth,
53
+ x2: x + noteHeadWidth,
54
+ y2: y + noteHeadWidth,
55
+ };
43
56
  }
44
57
  else {
45
- const stemDirection = this.getStemDirection();
46
- const noteHeadBounds = this.getNoteHeadBounds();
47
- const noteHeadWidth = this.noteHeads[0].getWidth();
48
- const x = stemDirection === Stem.DOWN ? this.getAbsoluteX() : this.getAbsoluteX() + noteHeadWidth;
49
- const defaultOffsetY = (Tables.STEM_HEIGHT * scale) / 2;
50
- const y = stemDirection === Stem.DOWN ? noteHeadBounds.yBottom + defaultOffsetY : noteHeadBounds.yTop - defaultOffsetY;
51
- if (stemDirection === Stem.DOWN) {
52
- slashBBox = {
53
- x1: x - noteHeadWidth,
54
- y1: y - noteHeadWidth,
55
- x2: x + noteHeadWidth,
56
- y2: y + noteHeadWidth,
57
- };
58
- }
59
- else {
60
- slashBBox = {
61
- x1: x - noteHeadWidth,
62
- y1: y + noteHeadWidth,
63
- x2: x + noteHeadWidth,
64
- y2: y - noteHeadWidth,
65
- };
66
- }
58
+ slashBBox = {
59
+ x1: x - noteHeadWidth,
60
+ y1: y + noteHeadWidth,
61
+ x2: x + noteHeadWidth,
62
+ y2: y - noteHeadWidth,
63
+ };
67
64
  }
65
+ }
66
+ return { x1: slashBBox.x1, y1: slashBBox.y1, x2: slashBBox.x2, y2: slashBBox.y2 };
67
+ }
68
+ draw() {
69
+ super.draw();
70
+ const slashBBox = this.getSlash();
71
+ if (slashBBox) {
72
+ const scale = this.getFontScale();
68
73
  const ctx = this.checkContext();
69
74
  ctx.setLineWidth(1 * scale);
70
75
  ctx.beginPath();
@@ -16,55 +16,32 @@ export class GraceNoteGroup extends Modifier {
16
16
  static get CATEGORY() {
17
17
  return "GraceNoteGroup";
18
18
  }
19
- static format(gracenoteGroups, state) {
20
- const groupSpacingStave = 4;
21
- const groupSpacingTab = 0;
22
- if (!gracenoteGroups || gracenoteGroups.length === 0)
19
+ static format(groups, state) {
20
+ if (!(groups === null || groups === void 0 ? void 0 : groups.length))
23
21
  return false;
24
- const groupList = [];
25
- let prevNote = null;
26
- let shift = 0;
27
- for (let i = 0; i < gracenoteGroups.length; ++i) {
28
- const gracenoteGroup = gracenoteGroups[i];
29
- const note = gracenoteGroup.getNote();
30
- const isStavenote = isStaveNote(note);
31
- const spacing = isStavenote ? groupSpacingStave : groupSpacingTab;
32
- if (isStavenote && note !== prevNote) {
33
- for (let n = 0; n < note.keys.length; ++n) {
34
- shift = Math.max(note.getLeftDisplacedHeadPx(), shift);
35
- }
36
- prevNote = note;
37
- }
38
- groupList.push({ shift: shift, gracenoteGroup, spacing });
39
- }
40
- let groupShift = groupList[0].shift;
41
- let formatWidth;
42
- let right = false;
43
- let left = false;
44
- for (let i = 0; i < groupList.length; ++i) {
45
- const gracenoteGroup = groupList[i].gracenoteGroup;
46
- if (gracenoteGroup.position === Modifier.Position.RIGHT)
47
- right = true;
48
- else
49
- left = true;
50
- gracenoteGroup.preFormat();
51
- formatWidth = gracenoteGroup.getWidth() + groupList[i].spacing;
52
- groupShift = Math.max(formatWidth, groupShift);
53
- }
54
- for (let i = 0; i < groupList.length; ++i) {
55
- const gracenoteGroup = groupList[i].gracenoteGroup;
56
- formatWidth = gracenoteGroup.getWidth() + groupList[i].spacing;
57
- gracenoteGroup.setSpacingFromNextModifier(groupShift - Math.min(formatWidth, groupShift) + StaveNote.minNoteheadPadding);
22
+ const entries = groups.map((group) => {
23
+ group.preFormat();
24
+ const note = group.getNote();
25
+ const width = group.getWidth() + (isStaveNote(note) ? 4 : 0);
26
+ return { group, note, width, after: group.position === Modifier.Position.RIGHT };
27
+ });
28
+ const afterWidth = Math.max(0, ...entries.filter((e) => e.after).map((e) => e.width));
29
+ const beforeWidth = Math.max(0, ...entries.filter((e) => !e.after).map((e) => Math.max(e.width, e.note.getLeftDisplacedHeadPx())));
30
+ for (const { group, width, after, note } of entries) {
31
+ group.afterWidth = afterWidth;
32
+ group.afterOffset =
33
+ note.getGlyphWidth() + note.getRightDisplacedHeadPx() + state.rightShift + StaveNote.minNoteheadPadding;
34
+ group.setSpacingFromNextModifier((after ? 0 : beforeWidth - width) + StaveNote.minNoteheadPadding);
58
35
  }
59
- if (right)
60
- state.rightShift += groupShift;
61
- if (left)
62
- state.leftShift += groupShift;
36
+ state.leftShift += beforeWidth;
37
+ state.rightShift += afterWidth;
63
38
  return true;
64
39
  }
65
40
  constructor(graceNotes, showSlur) {
66
41
  super();
67
42
  this.preFormatted = false;
43
+ this.afterWidth = 0;
44
+ this.afterOffset = 0;
68
45
  this.position = Modifier.Position.LEFT;
69
46
  this.graceNotes = graceNotes;
70
47
  this.width = 0;
@@ -117,6 +94,14 @@ export class GraceNoteGroup extends Modifier {
117
94
  note.checkStave();
118
95
  this.preFormat();
119
96
  this.alignSubNotesWithNote(this.getGraceNotes(), note, this.position);
97
+ if (this.afterWidth) {
98
+ for (const grace of this.graceNotes) {
99
+ const tick = grace.getTickContext();
100
+ tick.setXOffset(this.position === Modifier.Position.RIGHT
101
+ ? note.getTickContext().getX() + this.afterOffset
102
+ : tick.getXOffset() + this.afterWidth);
103
+ }
104
+ }
120
105
  this.beams.forEach((beam) => beam.postFormat());
121
106
  }
122
107
  getBoundingBox() {
@@ -129,27 +114,39 @@ export class GraceNoteGroup extends Modifier {
129
114
  }
130
115
  return boundingBox;
131
116
  }
132
- draw() {
133
- const ctx = this.checkContext();
117
+ getLayoutElements() {
118
+ var _a, _b;
134
119
  const note = this.checkAttachedNote();
135
- this.setRendered();
136
- L('Drawing grace note group for:', note);
137
120
  this.positionGraceNotes();
138
- this.graceNotes.forEach((graceNote) => graceNote.setContext(ctx).drawWithStyle());
139
- this.beams.forEach((beam) => beam.setContext(ctx).drawWithStyle());
140
- if (this.showSlur) {
121
+ if (this.showSlur && this.graceNotes.length) {
141
122
  const isStavenote = isStaveNote(note);
142
123
  const TieClass = isStavenote ? StaveTie : TabTie;
143
- this.slur = new TieClass({
144
- lastNote: this.graceNotes[0],
145
- firstNote: note,
146
- firstIndexes: [0],
147
- lastIndexes: [0],
148
- });
124
+ const after = this.position === Modifier.Position.RIGHT;
125
+ const end = after ? this.graceNotes[this.graceNotes.length - 1] : this.graceNotes[0];
126
+ if (((_a = this.slur) === null || _a === void 0 ? void 0 : _a.getNotes().firstNote) !== note || ((_b = this.slur) === null || _b === void 0 ? void 0 : _b.getNotes().lastNote) !== end)
127
+ this.slur = new TieClass({
128
+ lastNote: end,
129
+ firstNote: note,
130
+ firstIndexes: [0],
131
+ lastIndexes: [0],
132
+ });
133
+ if (this.afterWidth && isStavenote) {
134
+ const hostEdge = note.getNoteHeadBeginX() + (after ? note.getGlyphWidth() : 0);
135
+ this.slur.renderOptions.firstXShift = hostEdge - note.getTieRightX();
136
+ }
137
+ else {
138
+ this.slur.renderOptions.firstXShift = 0;
139
+ }
149
140
  this.slur.renderOptions.cp2 = 12;
150
141
  this.slur.renderOptions.yShift = (isStavenote ? 7 : 5) + this.renderOptions.slurYShift;
151
- this.slur.setContext(ctx).drawWithStyle();
152
142
  }
143
+ return [...this.graceNotes, ...this.beams, ...(this.slur ? [this.slur] : [])];
144
+ }
145
+ draw() {
146
+ const ctx = this.checkContext();
147
+ this.setRendered();
148
+ L('Drawing grace note group for:', this.checkAttachedNote());
149
+ this.getLayoutElements().forEach((element) => element.setContext(ctx).drawWithStyle());
153
150
  }
154
151
  }
155
152
  GraceNoteGroup.DEBUG = false;
@@ -14,6 +14,8 @@ export * from './curve.js';
14
14
  export * from './dot.js';
15
15
  export * from './easyscore.js';
16
16
  export * from './element.js';
17
+ export * from './engravinggeometry.js';
18
+ export * from './engravingprofile.js';
17
19
  export * from './factory.js';
18
20
  export * from './font.js';
19
21
  export * from './flag.js';
@@ -20,7 +20,7 @@ export class KeySignature extends StaveModifier {
20
20
  convertToGlyph(acc, nextAcc, stave) {
21
21
  const code = Tables.accidentalCodes(acc.type);
22
22
  const glyph = new Element("KeySignature");
23
- glyph.setText(code);
23
+ glyph.setFont(this.fontInfo).setText(code);
24
24
  let extraWidth = 1;
25
25
  glyph.setYShift(stave.getYForLine(acc.line));
26
26
  if (this.children.length > 0) {
@@ -151,6 +151,13 @@ export class KeySignature extends StaveModifier {
151
151
  this.format();
152
152
  return this.width;
153
153
  }
154
+ setClef(clef) {
155
+ if (clef !== undefined)
156
+ Tables.clefProperties(clef);
157
+ this.clefOverride = clef;
158
+ this.formatted = false;
159
+ return this;
160
+ }
154
161
  setKeySig(keySpec, cancelKeySpec, alterKeySpec) {
155
162
  this.formatted = false;
156
163
  this.keySpec = keySpec;
@@ -172,6 +179,7 @@ export class KeySignature extends StaveModifier {
172
179
  }
173
180
  }
174
181
  format() {
182
+ var _a;
175
183
  let stave = this.getStave();
176
184
  if (!stave) {
177
185
  stave = new Stave(0, 0, 100);
@@ -190,7 +198,7 @@ export class KeySignature extends StaveModifier {
190
198
  this.convertToAlterAccList(this.alterKeySpec);
191
199
  }
192
200
  if (this.accList.length > 0) {
193
- const clef = (this.position === StaveModifierPosition.END ? stave.getEndClef() : stave.getClef()) || stave.getClef();
201
+ const clef = (_a = this.clefOverride) !== null && _a !== void 0 ? _a : ((this.position === StaveModifierPosition.END ? stave.getEndClef() : stave.getClef()) || stave.getClef());
194
202
  if (cancelAccList) {
195
203
  this.convertAccLines(clef, cancelAccList.type, cancelAccList.accList);
196
204
  }
@@ -4,9 +4,9 @@ export class KeySigNote extends Note {
4
4
  static get CATEGORY() {
5
5
  return "KeySigNote";
6
6
  }
7
- constructor(keySpec, cancelKeySpec, alterKeySpec) {
7
+ constructor(keySpec, cancelKeySpec, alterKeySpec, clef) {
8
8
  super({ duration: 'b' });
9
- this.keySignature = new KeySignature(keySpec, cancelKeySpec, alterKeySpec);
9
+ this.keySignature = new KeySignature(keySpec, cancelKeySpec, alterKeySpec).setClef(clef);
10
10
  this.ignoreTicks = true;
11
11
  }
12
12
  addToModifierContext(mc) {
@@ -33,6 +33,8 @@ export class ModifierContext {
33
33
  this.width = 0;
34
34
  this.spacing = 0;
35
35
  this.annotationRightShift = 0;
36
+ this.measuredTopLines = 0;
37
+ this.measuredBottomLines = 0;
36
38
  }
37
39
  addModifier(member) {
38
40
  L('addModifier is deprecated, use addMember instead.');
@@ -68,6 +70,9 @@ export class ModifierContext {
68
70
  getRightShiftForTie() {
69
71
  return this.state.rightShift - this.annotationRightShift;
70
72
  }
73
+ getUnmeasuredTextLines(above) {
74
+ return above ? this.state.topTextLine - this.measuredTopLines : this.state.textLine - this.measuredBottomLines;
75
+ }
71
76
  getState() {
72
77
  return this.state;
73
78
  }
@@ -103,12 +108,18 @@ export class ModifierContext {
103
108
  annotation.getVerticalJustification() === Annotation.VerticalJustify.BOTTOM;
104
109
  Annotation.format(annotations.filter((annotation) => !isOutsideNote(annotation)), state);
105
110
  const rightShiftBeforeAnnotations = state.rightShift;
111
+ const topBefore = state.topTextLine;
112
+ const bottomBefore = state.textLine;
106
113
  Annotation.format(annotations.filter(isOutsideNote), state);
114
+ const topAfter = state.topTextLine;
115
+ const bottomAfter = state.textLine;
107
116
  const rightShiftWithAnnotations = state.rightShift;
108
117
  state.rightShift = rightShiftBeforeAnnotations;
109
118
  ChordSymbol.format(members["ChordSymbol"], state);
110
119
  Bend.format(members["Bend"], state);
111
120
  Vibrato.format(members["Vibrato"], state, this);
121
+ this.measuredTopLines = state.topTextLine === topAfter ? topAfter - topBefore : 0;
122
+ this.measuredBottomLines = state.textLine === bottomAfter ? bottomAfter - bottomBefore : 0;
112
123
  this.annotationRightShift = Math.max(0, rightShiftWithAnnotations - state.rightShift);
113
124
  state.rightShift += this.annotationRightShift;
114
125
  this.width = state.leftShift + state.rightShift;
@@ -176,6 +176,11 @@ export class Note extends Tickable {
176
176
  getStave() {
177
177
  return this.stave;
178
178
  }
179
+ clearStave() {
180
+ this.stave = undefined;
181
+ this.preFormatted = false;
182
+ return this;
183
+ }
179
184
  checkStave() {
180
185
  return defined(this.stave, 'NoStave', 'No stave attached to instance.');
181
186
  }
@@ -1,6 +1,6 @@
1
1
  import { Note } from './note.js';
2
2
  import { Stem } from './stem.js';
3
- import { defined, log } from './util.js';
3
+ import { defined, log, RuntimeError } from './util.js';
4
4
  function L(...args) {
5
5
  if (NoteHead.DEBUG)
6
6
  log('VexFlow.NoteHead', args);
@@ -10,7 +10,7 @@ export class NoteHead extends Note {
10
10
  return "NoteHead";
11
11
  }
12
12
  constructor(noteStruct) {
13
- var _a;
13
+ var _a, _b;
14
14
  super(noteStruct);
15
15
  this.customGlyph = false;
16
16
  this.ledger = {
@@ -22,7 +22,10 @@ export class NoteHead extends Note {
22
22
  this.y = noteStruct.y || 0;
23
23
  if (noteStruct.noteType)
24
24
  this.noteType = noteStruct.noteType;
25
- this.displaced = noteStruct.displaced || false;
25
+ this.displacementColumn = (_a = noteStruct.displacementColumn) !== null && _a !== void 0 ? _a : (noteStruct.displaced ? 1 : 0);
26
+ if (!Number.isSafeInteger(this.displacementColumn) || this.displacementColumn < 0)
27
+ throw new RuntimeError('BadArguments', 'Head displacement column must be a nonnegative integer.');
28
+ this.displaced = this.displacementColumn !== 0;
26
29
  this.stemDirection = noteStruct.stemDirection || Stem.UP;
27
30
  this.line = noteStruct.line || 0;
28
31
  this.glyphProps = Note.getGlyphProps(this.duration, this.noteType);
@@ -35,7 +38,7 @@ export class NoteHead extends Note {
35
38
  this.customGlyph = true;
36
39
  this.text = noteStruct.customGlyphCode;
37
40
  }
38
- this.setStyle((_a = noteStruct.style) !== null && _a !== void 0 ? _a : {});
41
+ this.setStyle((_b = noteStruct.style) !== null && _b !== void 0 ? _b : {});
39
42
  this.slashed = noteStruct.slashed || false;
40
43
  this.renderOptions = Object.assign({}, this.renderOptions);
41
44
  }
@@ -54,8 +57,10 @@ export class NoteHead extends Note {
54
57
  }
55
58
  getAbsoluteX() {
56
59
  const x = !this.preFormatted ? this.x : super.getAbsoluteX();
57
- const displacementStemAdjustment = Stem.WIDTH / 2;
58
- return x + (this.displaced ? (this.width - displacementStemAdjustment) * this.stemDirection : 0);
60
+ return x + this.getDisplacementX();
61
+ }
62
+ getDisplacementX() {
63
+ return this.displacementColumn * (this.width - Stem.WIDTH / 2) * this.stemDirection;
59
64
  }
60
65
  setStave(stave) {
61
66
  const line = this.getLine();
@@ -26,12 +26,13 @@ export class Ornament extends Modifier {
26
26
  for (let i = 0; i < ornaments.length; ++i) {
27
27
  const ornament = ornaments[i];
28
28
  const increment = 2;
29
+ ornament.sideOffset = 0;
29
30
  if (ornament.position === ModifierPosition.RIGHT) {
30
- ornament.xShift += rightShift + 2;
31
+ ornament.sideOffset = rightShift + 2;
31
32
  rightShift += ornament.width + Ornament.minPadding;
32
33
  }
33
34
  else if (ornament.position === ModifierPosition.LEFT) {
34
- ornament.xShift -= leftShift + ornament.width + 2;
35
+ ornament.sideOffset = -(leftShift + ornament.width + 2);
35
36
  leftShift += ornament.width + Ornament.minPadding;
36
37
  }
37
38
  else if (ornament.position === ModifierPosition.ABOVE) {
@@ -84,6 +85,7 @@ export class Ornament extends Modifier {
84
85
  }
85
86
  constructor(type) {
86
87
  super();
88
+ this.sideOffset = 0;
87
89
  this.position = ModifierPosition.ABOVE;
88
90
  if (Ornament.ornamentRight.indexOf(type) >= 0) {
89
91
  this.position = ModifierPosition.RIGHT;
@@ -130,15 +132,12 @@ export class Ornament extends Modifier {
130
132
  this.accidentalLower.setText(Tables.accidentalCodes(accid));
131
133
  return this;
132
134
  }
133
- draw() {
134
- const ctx = this.checkContext();
135
+ positionElements() {
135
136
  const note = this.checkAttachedNote();
136
- this.setRendered();
137
137
  const stave = note.checkStave();
138
- const clsAttribute = this.getAttribute('class');
139
- ctx.openGroup('ornament' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
138
+ const parts = [];
140
139
  const start = note.getModifierStartXY(this.position, this.index);
141
- let glyphX = start.x;
140
+ let glyphX = start.x + this.sideOffset;
142
141
  const staffSpace = stave.getSpacingBetweenLines();
143
142
  const initialOffset = getInitialOffset(note, this.position);
144
143
  let glyphY = this.ornamentAlignWithNoteHead ? start.y : 0;
@@ -164,27 +163,48 @@ export class Ornament extends Modifier {
164
163
  else {
165
164
  delayXShift += (stave.getX() + stave.getWidth() - glyphX) * 0.5;
166
165
  }
167
- this.delayXShift = delayXShift;
168
166
  }
169
167
  glyphX += delayXShift;
170
168
  }
171
169
  L('Rendering ornament: ', this.text.charCodeAt(0), glyphX, glyphY);
172
170
  if (this.accidentalLower) {
173
- this.accidentalLower.renderText(ctx, glyphX + this.xShift - this.accidentalLower.getWidth() * 0.5, glyphY + this.yShift - this.accidentalLower.getTextMetrics().actualBoundingBoxDescent);
171
+ parts.push({
172
+ element: this.accidentalLower,
173
+ x: glyphX + this.xShift - this.accidentalLower.getWidth() / 2,
174
+ y: glyphY + this.yShift - this.accidentalLower.getTextMetrics().actualBoundingBoxDescent,
175
+ });
174
176
  glyphY -= this.accidentalLower.getHeight() + this.renderOptions.accidentalLowerPadding;
175
177
  }
176
- if (Ornament.ornamentYShift.indexOf(this.type) >= 0) {
177
- this.yShift += this.getHeight();
178
- }
178
+ const offset = Ornament.ornamentYShift.includes(this.type) ? this.getHeight() : 0;
179
179
  this.x =
180
180
  glyphX -
181
- (this.position === ModifierPosition.ABOVE || this.position === ModifierPosition.BELOW ? this.width * 0.5 : 0);
182
- this.y = glyphY;
183
- this.renderText(ctx, 0, 0);
181
+ (this.position === ModifierPosition.ABOVE || this.position === ModifierPosition.BELOW ? this.width / 2 : 0);
182
+ this.y = glyphY + offset;
183
+ parts.push({ element: this, x: 0, y: 0 });
184
184
  if (this.accidentalUpper) {
185
- glyphY -= this.getHeight() + this.renderOptions.accidentalUpperPadding;
186
- this.accidentalUpper.renderText(ctx, glyphX + this.xShift - this.accidentalUpper.getWidth() * 0.5, glyphY + this.yShift - this.accidentalUpper.getTextMetrics().actualBoundingBoxDescent);
185
+ parts.push({
186
+ element: this.accidentalUpper,
187
+ x: glyphX + this.xShift - this.accidentalUpper.getWidth() / 2,
188
+ y: this.y +
189
+ this.yShift -
190
+ this.getHeight() -
191
+ this.renderOptions.accidentalUpperPadding -
192
+ this.accidentalUpper.getTextMetrics().actualBoundingBoxDescent,
193
+ });
187
194
  }
195
+ return parts;
196
+ }
197
+ getBoundingBox() {
198
+ const boxes = this.positionElements().map(({ element, x, y }) => element.getTextInk(x + element.getX(), y + element.getY()));
199
+ return boxes.reduce((bounds, box) => bounds.mergeWith(box));
200
+ }
201
+ draw() {
202
+ const ctx = this.checkContext();
203
+ this.setRendered();
204
+ const clsAttribute = this.getAttribute('class');
205
+ ctx.openGroup('ornament' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
206
+ for (const { element, x, y } of this.positionElements())
207
+ element.renderText(ctx, x, y);
188
208
  this.drawPointerRect();
189
209
  ctx.closeGroup();
190
210
  }
@@ -48,7 +48,7 @@ export class Parenthesis extends Modifier {
48
48
  }
49
49
  setNote(note) {
50
50
  this.note = note;
51
- this.setFont(note.getFont());
51
+ this.setFont(note.fontInfo);
52
52
  return this;
53
53
  }
54
54
  draw() {