vexflow 5.2.0 → 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 (114) hide show
  1. package/build/cjs/vexflow-bravura.js +2 -2
  2. package/build/cjs/vexflow-bravura.js.map +1 -1
  3. package/build/cjs/vexflow-core.js +2 -2
  4. package/build/cjs/vexflow-core.js.map +1 -1
  5. package/build/cjs/vexflow-debug-with-tests.js +2759 -1382
  6. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  7. package/build/cjs/vexflow-debug.js +1814 -1227
  8. package/build/cjs/vexflow-debug.js.map +1 -1
  9. package/build/cjs/vexflow.js +2 -2
  10. package/build/cjs/vexflow.js.map +1 -1
  11. package/build/esm/src/accidental.js +10 -3
  12. package/build/esm/src/articulation.js +24 -6
  13. package/build/esm/src/beam.js +158 -28
  14. package/build/esm/src/bend.js +24 -19
  15. package/build/esm/src/curve.js +27 -53
  16. package/build/esm/src/dot.js +13 -4
  17. package/build/esm/src/easyscore/composition.js +40 -1
  18. package/build/esm/src/easyscore/policies.js +34 -6
  19. package/build/esm/src/element.js +4 -0
  20. package/build/esm/src/engravinggeometry.js +150 -0
  21. package/build/esm/src/engravingprofile.js +44 -0
  22. package/build/esm/src/factory.js +15 -7
  23. package/build/esm/src/formatter.js +9 -1
  24. package/build/esm/src/gracenote.js +43 -38
  25. package/build/esm/src/gracenotegroup.js +53 -56
  26. package/build/esm/src/index.js +2 -0
  27. package/build/esm/src/keysignature.js +1 -1
  28. package/build/esm/src/modifiercontext.js +11 -0
  29. package/build/esm/src/notehead.js +11 -6
  30. package/build/esm/src/ornament.js +38 -18
  31. package/build/esm/src/proportionalformatter.js +54 -6
  32. package/build/esm/src/stavenote.js +128 -115
  33. package/build/esm/src/stavetie.js +39 -7
  34. package/build/esm/src/stem.js +26 -11
  35. package/build/esm/src/tickcontext.js +19 -0
  36. package/build/esm/src/timesignature.js +5 -0
  37. package/build/esm/src/tuplet.js +57 -17
  38. package/build/esm/src/version.js +3 -3
  39. package/build/esm/src/vexflow.js +4 -0
  40. package/build/esm/tests/bend_tests.js +14 -1
  41. package/build/esm/tests/curve_tests.js +14 -4
  42. package/build/esm/tests/demo_repertoire_tests.js +27 -0
  43. package/build/esm/tests/demos/drums.js +54 -0
  44. package/build/esm/tests/demos/electric.js +63 -0
  45. package/build/esm/tests/demos/greensleeves.js +90 -0
  46. package/build/esm/tests/demos/joy.js +61 -0
  47. package/build/esm/tests/demos/kesh.js +80 -0
  48. package/build/esm/tests/demos/prelude.js +59 -0
  49. package/build/esm/tests/demos/quartertones.js +50 -0
  50. package/build/esm/tests/demos/saints.js +65 -0
  51. package/build/esm/tests/engraving/score-layout.mjs +75 -0
  52. package/build/esm/tests/engraving/score-persistence.mjs +262 -0
  53. package/build/esm/tests/engraving/studies.mjs +279 -0
  54. package/build/esm/tests/formatter_tests.js +10 -1
  55. package/build/esm/tests/index.js +1 -0
  56. package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
  57. package/build/esm/tests/node/easyscore-tests.mjs +71 -0
  58. package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
  59. package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
  60. package/build/esm/tests/node/proportional-spacing-tests.mjs +64 -0
  61. package/build/esm/tests/node/rendering-tests.mjs +6 -2
  62. package/build/esm/tests/threevoice_tests.js +11 -1
  63. package/build/esm/tests/vexflow_test_helpers.js +19 -10
  64. package/build/esm/tools/audit-dependencies.mjs +61 -0
  65. package/build/esm/tools/score-fixtures.mjs +23 -6
  66. package/build/types/src/accidental.d.ts +4 -0
  67. package/build/types/src/articulation.d.ts +6 -1
  68. package/build/types/src/beam.d.ts +14 -0
  69. package/build/types/src/curve.d.ts +9 -0
  70. package/build/types/src/dot.d.ts +5 -0
  71. package/build/types/src/easyscore/composition.d.ts +19 -0
  72. package/build/types/src/easyscore/policies.d.ts +1 -1
  73. package/build/types/src/element.d.ts +2 -0
  74. package/build/types/src/engravinggeometry.d.ts +31 -0
  75. package/build/types/src/engravingprofile.d.ts +20 -0
  76. package/build/types/src/factory.d.ts +6 -1
  77. package/build/types/src/formatter.d.ts +3 -3
  78. package/build/types/src/gracenote.d.ts +7 -0
  79. package/build/types/src/gracenotegroup.d.ts +7 -2
  80. package/build/types/src/index.d.ts +2 -0
  81. package/build/types/src/modifiercontext.d.ts +4 -0
  82. package/build/types/src/notehead.d.ts +5 -0
  83. package/build/types/src/ornament.d.ts +7 -0
  84. package/build/types/src/proportionalformatter.d.ts +11 -2
  85. package/build/types/src/stavenote.d.ts +20 -2
  86. package/build/types/src/stavetie.d.ts +4 -0
  87. package/build/types/src/stem.d.ts +10 -1
  88. package/build/types/src/tickcontext.d.ts +5 -0
  89. package/build/types/src/timesignature.d.ts +2 -0
  90. package/build/types/src/tuplet.d.ts +5 -48
  91. package/build/types/src/vexflow.d.ts +4 -0
  92. package/changelog/CHANGELOG.md +17 -0
  93. package/package.json +7 -2
  94. package/tests/all.html +277 -0
  95. package/tests/demo_petzold_tests.ts +435 -0
  96. package/tests/demos/drums.ts +55 -0
  97. package/tests/demos/drums.webp +0 -0
  98. package/tests/demos/electric.ts +70 -0
  99. package/tests/demos/electric.webp +0 -0
  100. package/tests/demos/greensleeves.ts +97 -0
  101. package/tests/demos/greensleeves.webp +0 -0
  102. package/tests/demos/joy.ts +65 -0
  103. package/tests/demos/joy.webp +0 -0
  104. package/tests/demos/kesh.ts +86 -0
  105. package/tests/demos/kesh.webp +0 -0
  106. package/tests/demos/prelude.ts +65 -0
  107. package/tests/demos/prelude.webp +0 -0
  108. package/tests/demos/quartertones.ts +51 -0
  109. package/tests/demos/quartertones.webp +0 -0
  110. package/tests/demos/saints.ts +70 -0
  111. package/tests/demos/saints.webp +0 -0
  112. package/tests/flow.html +550 -239
  113. package/tests/fonts.css +493 -0
  114. package/tests/minuet.webp +0 -0
@@ -1,9 +1,32 @@
1
1
  import { Accidental } from '../accidental.js';
2
2
  import { Fraction } from '../fraction.js';
3
+ import { GraceNoteGroup } from '../gracenotegroup.js';
4
+ import { Modifier } from '../modifier.js';
3
5
  import { Music } from '../music.js';
4
6
  import { StaveNote } from '../stavenote.js';
5
- export function applyCompositionAccidentals(score, voices, key, changes, generated, stave) {
6
- var _a, _b;
7
+ function orderedAttacks(hosts, includeGrace) {
8
+ const attacks = new Map([[0, hosts.filter((note) => !note.isRest())]]);
9
+ if (includeGrace)
10
+ for (const host of hosts) {
11
+ for (const group of host.getModifiers()) {
12
+ if (!(group instanceof GraceNoteGroup))
13
+ continue;
14
+ const notes = group.getGraceNotes();
15
+ notes.forEach((note, i) => {
16
+ var _a;
17
+ if (!(note instanceof StaveNote) || note.isRest())
18
+ return;
19
+ const offset = group.getPosition() === Modifier.Position.RIGHT ? i + 1 : i - notes.length;
20
+ const simultaneous = (_a = attacks.get(offset)) !== null && _a !== void 0 ? _a : [];
21
+ simultaneous.push(note);
22
+ attacks.set(offset, simultaneous);
23
+ });
24
+ }
25
+ }
26
+ return [...attacks.entries()].sort(([a], [b]) => a - b).map(([, notes]) => notes);
27
+ }
28
+ export function applyCompositionAccidentals(score, voices, key, changes, generated, stave, includeGrace = false) {
29
+ var _a;
7
30
  const music = new Music();
8
31
  let scale = music.createScaleMap(key);
9
32
  const state = new Map();
@@ -15,7 +38,7 @@ export function applyCompositionAccidentals(score, voices, key, changes, generat
15
38
  continue;
16
39
  const id = at.clone().simplify().toString();
17
40
  const group = (_a = positions.get(id)) !== null && _a !== void 0 ? _a : { at: at.clone(), notes: [] };
18
- if (note instanceof StaveNote && !note.isRest() && note.getStave() === stave)
41
+ if (note instanceof StaveNote && note.getStave() === stave)
19
42
  group.notes.push(note);
20
43
  positions.set(id, group);
21
44
  at.add(note.getTicks());
@@ -28,18 +51,23 @@ export function applyCompositionAccidentals(score, voices, key, changes, generat
28
51
  scale = music.createScaleMap(changes[changeIndex++].key);
29
52
  state.clear();
30
53
  }
54
+ for (const notes of orderedAttacks(group.notes, includeGrace))
55
+ applyAttack(notes);
56
+ }
57
+ function applyAttack(notes) {
58
+ var _a;
31
59
  const simultaneous = new Map();
32
- for (const note of group.notes)
60
+ for (const note of notes)
33
61
  for (const written of note.getKeys()) {
34
62
  const [name, octave] = written.split('/');
35
63
  const parts = music.getNoteParts(name);
36
64
  const id = `${parts.root}/${octave}`;
37
- const values = (_b = simultaneous.get(id)) !== null && _b !== void 0 ? _b : new Set();
65
+ const values = (_a = simultaneous.get(id)) !== null && _a !== void 0 ? _a : new Set();
38
66
  values.add(parts.accidental || 'n');
39
67
  simultaneous.set(id, values);
40
68
  }
41
69
  const updates = new Map();
42
- for (const note of group.notes) {
70
+ for (const note of notes) {
43
71
  note.getKeys().forEach((written, index) => {
44
72
  var _a, _b, _c, _d;
45
73
  const [name, octave] = written.split('/');
@@ -225,6 +225,10 @@ export class Element {
225
225
  (_c = this.idListeners) === null || _c === void 0 ? void 0 : _c.forEach((listener) => listener.changed());
226
226
  return this;
227
227
  }
228
+ getTextInk(x, y) {
229
+ const m = this.textMetrics;
230
+ return new BoundingBox(x + this.xShift - m.actualBoundingBoxLeft, y + this.yShift - m.actualBoundingBoxAscent, m.actualBoundingBoxLeft + m.actualBoundingBoxRight, m.actualBoundingBoxAscent + m.actualBoundingBoxDescent);
231
+ }
228
232
  getBoundingBox() {
229
233
  return new BoundingBox(this.x + this.xShift, this.y + this.yShift - this.textMetrics.actualBoundingBoxAscent, this.width, this.height);
230
234
  }
@@ -0,0 +1,150 @@
1
+ import { Accidental } from './accidental.js';
2
+ import { Annotation } from './annotation.js';
3
+ import { Articulation } from './articulation.js';
4
+ import { Beam } from './beam.js';
5
+ import { BoundingBox } from './boundingbox.js';
6
+ import { Curve } from './curve.js';
7
+ import { Dot } from './dot.js';
8
+ import { GraceNote } from './gracenote.js';
9
+ import { GraceNoteGroup } from './gracenotegroup.js';
10
+ import { Ornament } from './ornament.js';
11
+ import { StaveNote } from './stavenote.js';
12
+ import { StaveTie } from './stavetie.js';
13
+ import { Stem } from './stem.js';
14
+ import { Tuplet } from './tuplet.js';
15
+ import { RuntimeError } from './util.js';
16
+ function inflate(box, amount) {
17
+ return new BoundingBox(box.x - amount, box.y - amount, box.w + 2 * amount, box.h + 2 * amount);
18
+ }
19
+ function rectangle(x1, y1, x2, y2) {
20
+ return new BoundingBox(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
21
+ }
22
+ function noteShape(note) {
23
+ var _a, _b, _c;
24
+ if (note.renderOptions.draw === false)
25
+ return { parts: [], anchors: {} };
26
+ (_a = note.getBeam()) === null || _a === void 0 ? void 0 : _a.postFormat();
27
+ const parts = note.getHeadInk();
28
+ const anchors = {};
29
+ parts.forEach((box, i) => {
30
+ anchors[`head${i}`] = { x: box.x + box.w / 2, y: note.getYs()[i] };
31
+ });
32
+ const segment = note.getStemSegment();
33
+ if (segment) {
34
+ const { x, baseY, tipY } = segment;
35
+ parts.push(new BoundingBox(x - Stem.WIDTH / 2, Math.min(baseY, tipY), Stem.WIDTH, Math.abs(tipY - baseY)));
36
+ anchors.stemBase = { x, y: baseY };
37
+ anchors.stemTip = { x, y: tipY };
38
+ }
39
+ const flag = note.getFlagInk();
40
+ if (flag)
41
+ parts.push(flag);
42
+ const lineWidth = (_c = (_b = note.getLedgerLineStyle().lineWidth) !== null && _b !== void 0 ? _b : note.checkStave().getDefaultLedgerLineStyle().lineWidth) !== null && _c !== void 0 ? _c : 1;
43
+ for (const { x, y, width } of note.getLedgerLines())
44
+ parts.push(new BoundingBox(x, y - lineWidth / 2, width, lineWidth));
45
+ if (note instanceof GraceNote) {
46
+ const slash = note.getSlash();
47
+ if (slash)
48
+ parts.push(inflate(rectangle(slash.x1, slash.y1, slash.x2, slash.y2), note.getFontScale() / 2));
49
+ }
50
+ return { parts, anchors };
51
+ }
52
+ export function measureEngraving(element, options = {}) {
53
+ var _a;
54
+ const diagnostics = [];
55
+ const report = (owner, code) => {
56
+ diagnostics.push({
57
+ code,
58
+ phase: 'measure',
59
+ severity: code === 'LayoutOverflow' ? 'warning' : 'error',
60
+ sourceId: owner.getAttribute('id'),
61
+ });
62
+ };
63
+ function collect(owner) {
64
+ var _a, _b;
65
+ const shape = { parts: [], anchors: {} };
66
+ const children = (elements) => {
67
+ for (const child of elements)
68
+ shape.parts.push(...collect(child).parts);
69
+ };
70
+ try {
71
+ const kind = Object.getPrototypeOf(owner).constructor;
72
+ const custom = (_a = options.extension) === null || _a === void 0 ? void 0 : _a.call(options, owner);
73
+ if (custom)
74
+ Object.assign(shape, custom);
75
+ else if (kind === StaveNote || kind === GraceNote) {
76
+ const note = owner;
77
+ Object.assign(shape, noteShape(note));
78
+ if (note.renderOptions.draw !== false)
79
+ children(note.getModifiers());
80
+ }
81
+ else if (kind === GraceNoteGroup)
82
+ children(owner.getLayoutElements());
83
+ else if (kind === Articulation) {
84
+ const box = owner.getInkBounds();
85
+ shape.parts.push(box);
86
+ shape.anchors.center = { x: box.x + box.w / 2, y: box.y + box.h / 2 };
87
+ }
88
+ else if (kind === Accidental || kind === Dot) {
89
+ shape.parts.push(owner.getInkBounds());
90
+ }
91
+ else if (kind === Tuplet || kind === Ornament) {
92
+ shape.parts.push(owner.getBoundingBox());
93
+ }
94
+ else if (kind === Beam) {
95
+ const beam = owner;
96
+ shape.parts.push(beam.getBoundingBox());
97
+ for (const [i, note] of beam.getNotes().entries())
98
+ shape.anchors[`stem${i}`] = { x: note.getStemX(), y: note.getStemExtents().topY };
99
+ }
100
+ else if (kind === Curve) {
101
+ const curve = owner;
102
+ shape.parts.push(inflate(curve.getBoundingBox(), ((_b = curve.getStyle().lineWidth) !== null && _b !== void 0 ? _b : 1) / 2));
103
+ shape.anchors = curve.getAnchors();
104
+ }
105
+ else if (kind === Annotation) {
106
+ const mark = owner;
107
+ const { x } = mark.computeX();
108
+ const { y } = mark.computeY();
109
+ shape.parts.push(mark.getTextInk(x, y));
110
+ shape.anchors.baseline = { x: x + mark.getXShift(), y: y + mark.getYShift() };
111
+ }
112
+ else if (kind === StaveTie && !owner.text)
113
+ shape.parts.push(owner.getRibbonBounds());
114
+ else
115
+ report(owner, 'UnsupportedGeometry');
116
+ const finiteBox = (b) => [b.x, b.y, b.w, b.h].every(Number.isFinite) && b.w >= 0 && b.h >= 0;
117
+ if (!shape.parts.every(finiteBox) ||
118
+ !Object.values(shape.anchors).every((p) => Number.isFinite(p.x) && Number.isFinite(p.y))) {
119
+ report(owner, 'InvalidGeometry');
120
+ return { parts: [], anchors: {} };
121
+ }
122
+ }
123
+ catch (error) {
124
+ if (!(error instanceof RuntimeError))
125
+ throw error;
126
+ report(owner, 'UnformattedGeometry');
127
+ }
128
+ return shape;
129
+ }
130
+ const shape = collect(element);
131
+ const padding = (_a = options.paddingPx) !== null && _a !== void 0 ? _a : 0;
132
+ if (!Number.isFinite(padding) || padding < 0)
133
+ throw new RangeError('Expected finite nonnegative layout padding.');
134
+ let ink;
135
+ for (const part of shape.parts) {
136
+ const box = new BoundingBox(part.x, part.y, part.w, part.h);
137
+ ink = ink ? ink.mergeWith(box) : box;
138
+ }
139
+ const layout = ink && inflate(ink, padding);
140
+ if (ink && element instanceof GraceNoteGroup) {
141
+ shape.anchors.left = { x: ink.x, y: element.checkAttachedNote().getYs()[element.checkIndex()] };
142
+ shape.anchors.right = { x: ink.x + ink.w, y: shape.anchors.left.y };
143
+ }
144
+ const v = options.viewport;
145
+ if (v &&
146
+ layout &&
147
+ (layout.x < v.x || layout.y < v.y || layout.x + layout.w > v.x + v.w || layout.y + layout.h > v.y + v.h))
148
+ report(element, 'LayoutOverflow');
149
+ return Object.assign(Object.assign({}, shape), { ink, layout, diagnostics });
150
+ }
@@ -0,0 +1,44 @@
1
+ import { Glyphs } from './glyphs.js';
2
+ function record(value) {
3
+ return value !== null && typeof value === 'object' ? value : {};
4
+ }
5
+ function distance(value, fallback, allowZero = false) {
6
+ return typeof value === 'number' && Number.isFinite(value) && (allowZero ? value >= 0 : value > 0) && value <= 16
7
+ ? value
8
+ : fallback;
9
+ }
10
+ function readAnchor(value) {
11
+ if (!Array.isArray(value) || value.length !== 2 || !value.every((v) => Number.isFinite(v) && Math.abs(v) <= 16))
12
+ return;
13
+ return Object.freeze([value[0], value[1]]);
14
+ }
15
+ export class EngravingProfile {
16
+ constructor(options) {
17
+ var _a, _b;
18
+ if (!options.musicFont.trim())
19
+ throw new RangeError('Expected a music font family.');
20
+ this.musicFont = options.musicFont;
21
+ const supplied = record(options.metadata);
22
+ const metadata = supplied.fontName === this.musicFont ? supplied : {};
23
+ this.beamThickness = (_a = options.beamThickness) !== null && _a !== void 0 ? _a : distance(record(metadata.engravingDefaults).beamThickness, 0.5);
24
+ this.clearance = (_b = options.clearance) !== null && _b !== void 0 ? _b : 0.25;
25
+ if (!Number.isFinite(distance(this.beamThickness, NaN)) || !Number.isFinite(distance(this.clearance, NaN, true)))
26
+ throw new RangeError('Invalid engraving distance in staff spaces.');
27
+ const anchors = {};
28
+ for (const [name, fields] of Object.entries(record(metadata.glyphsWithAnchors))) {
29
+ if (!Object.prototype.hasOwnProperty.call(Glyphs, name))
30
+ continue;
31
+ const glyph = Glyphs[name];
32
+ anchors[glyph] = Object.freeze({
33
+ up: readAnchor(record(fields).stemUpSE),
34
+ down: readAnchor(record(fields).stemDownNW),
35
+ });
36
+ }
37
+ this.anchors = Object.freeze(anchors);
38
+ Object.freeze(this);
39
+ }
40
+ stemAnchor(glyph, direction) {
41
+ var _a;
42
+ return (_a = this.anchors[glyph]) === null || _a === void 0 ? void 0 : _a[direction];
43
+ }
44
+ }
@@ -244,14 +244,22 @@ export class Factory {
244
244
  Bend(...args) {
245
245
  return this.adopt(new Bend(...args).setContext(this.context), 'modifier');
246
246
  }
247
+ musicGlyph(element) {
248
+ if (this.options.engraving)
249
+ element.setFont(Object.assign(Object.assign({}, element.fontInfo), { family: this.options.engraving.musicFont }));
250
+ return element;
251
+ }
247
252
  Clef(...args) {
248
- return this.adopt(new Clef(...args).setContext(this.context), 'staveModifier');
253
+ return this.adopt(this.musicGlyph(new Clef(...args)).setContext(this.context), 'staveModifier');
249
254
  }
250
255
  KeySignature(...args) {
251
- return this.adopt(new KeySignature(...args).setContext(this.context), 'staveModifier');
256
+ return this.adopt(this.musicGlyph(new KeySignature(...args)).setContext(this.context), 'staveModifier');
252
257
  }
253
258
  TimeSignature(...args) {
254
- return this.adopt(new TimeSignature(...args).setContext(this.context), 'staveModifier');
259
+ const signature = new TimeSignature(...args);
260
+ if (this.options.engraving)
261
+ signature.setMusicFont(this.options.engraving.musicFont);
262
+ return this.adopt(signature.setContext(this.context), 'staveModifier');
255
263
  }
256
264
  Barline(...args) {
257
265
  return this.adopt(new Barline(...args).setContext(this.context), 'staveModifier');
@@ -281,7 +289,7 @@ export class Factory {
281
289
  return this.own(new GraceTabNote(options).setContext(this.context));
282
290
  }
283
291
  StaveNote(noteStruct) {
284
- const note = new StaveNote(noteStruct);
292
+ const note = new StaveNote(Object.assign({ engraving: this.options.engraving }, noteStruct));
285
293
  if (this.stave)
286
294
  note.setStave(this.stave);
287
295
  note.setContext(this.context);
@@ -363,7 +371,7 @@ export class Factory {
363
371
  return this.own(note);
364
372
  }
365
373
  GraceNote(noteStruct) {
366
- const note = new GraceNote(noteStruct);
374
+ const note = new GraceNote(Object.assign({ engraving: this.options.engraving }, noteStruct));
367
375
  if (this.stave)
368
376
  note.setStave(this.stave);
369
377
  note.setContext(this.context);
@@ -373,7 +381,7 @@ export class Factory {
373
381
  return this.own(new GraceNoteGroup(params.notes, params.slur).setContext(this.context));
374
382
  }
375
383
  Accidental(params) {
376
- const accid = new Accidental(params.type);
384
+ const accid = this.musicGlyph(new Accidental(params.type));
377
385
  accid.setContext(this.context);
378
386
  return this.own(accid);
379
387
  }
@@ -405,7 +413,7 @@ export class Factory {
405
413
  }
406
414
  Articulation(params) {
407
415
  var _a;
408
- const articulation = new Articulation((_a = params === null || params === void 0 ? void 0 : params.type) !== null && _a !== void 0 ? _a : 'a.');
416
+ const articulation = this.musicGlyph(new Articulation((_a = params === null || params === void 0 ? void 0 : params.type) !== null && _a !== void 0 ? _a : 'a.'));
409
417
  if ((params === null || params === void 0 ? void 0 : params.position) !== undefined)
410
418
  articulation.setPosition(params.position);
411
419
  if ((params === null || params === void 0 ? void 0 : params.betweenLines) !== undefined)
@@ -140,7 +140,7 @@ export class Formatter {
140
140
  }
141
141
  constructor(options) {
142
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) && options.spacing.mode !== 'proportional') {
143
+ if ((options === null || options === void 0 ? void 0 : options.spacing) && !['proportional', 'optical'].includes(options.spacing.mode)) {
144
144
  throw new RuntimeError('BadArgument', 'Unknown formatter spacing mode.');
145
145
  }
146
146
  this.justifyWidth = 0;
@@ -310,6 +310,7 @@ export class Formatter {
310
310
  list: tickList.sort((a, b) => a - b),
311
311
  resolutionMultiplier,
312
312
  };
313
+ TickContext.linkTimeline(contexts.list.map((tick) => contexts.map[tick]));
313
314
  this.tickContexts = contexts;
314
315
  const contextArray = this.tickContexts.array;
315
316
  contextArray.forEach((context) => {
@@ -611,8 +612,15 @@ export class Formatter {
611
612
  return this;
612
613
  }
613
614
  format(voices, justifyWidth, options) {
615
+ var _a;
614
616
  const opts = Object.assign({ alignRests: false }, options);
615
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
+ }
616
624
  const softmaxFactor = this.formatterOptions.softmaxFactor;
617
625
  if (softmaxFactor) {
618
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) {