vexflow 5.0.0 → 5.1.1

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 (130) hide show
  1. package/LICENSE +3 -3
  2. package/README.md +46 -53
  3. package/build/cjs/vexflow-bravura.js +2 -2
  4. package/build/cjs/vexflow-bravura.js.map +1 -1
  5. package/build/cjs/vexflow-core.js +2 -2
  6. package/build/cjs/vexflow-core.js.map +1 -1
  7. package/build/cjs/vexflow-debug-with-tests.js +3825 -1396
  8. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  9. package/build/cjs/vexflow-debug.js +2577 -984
  10. package/build/cjs/vexflow-debug.js.map +1 -1
  11. package/build/cjs/vexflow.js +2 -2
  12. package/build/cjs/vexflow.js.map +1 -1
  13. package/build/esm/src/accidental.js +1 -0
  14. package/build/esm/src/annotation.js +31 -8
  15. package/build/esm/src/beam.js +4 -2
  16. package/build/esm/src/chordsymbol.js +3 -1
  17. package/build/esm/src/clef.js +3 -1
  18. package/build/esm/src/curve.js +68 -11
  19. package/build/esm/src/easyscore.js +40 -1
  20. package/build/esm/src/element.js +49 -11
  21. package/build/esm/src/factory.js +46 -9
  22. package/build/esm/src/flag.js +3 -1
  23. package/build/esm/src/font.js +20 -1
  24. package/build/esm/src/formatter.js +34 -36
  25. package/build/esm/src/fraction.js +4 -1
  26. package/build/esm/src/glyphnote.js +3 -1
  27. package/build/esm/src/gracenotegroup.js +18 -1
  28. package/build/esm/src/index.js +2 -0
  29. package/build/esm/src/keysignature.js +3 -1
  30. package/build/esm/src/metrics.js +58 -3
  31. package/build/esm/src/modifiercontext.js +15 -1
  32. package/build/esm/src/note.js +2 -2
  33. package/build/esm/src/notehead.js +3 -1
  34. package/build/esm/src/ornament.js +3 -1
  35. package/build/esm/src/pedalmarking.js +44 -5
  36. package/build/esm/src/registry.js +16 -6
  37. package/build/esm/src/rendercontext.js +10 -0
  38. package/build/esm/src/sheet.js +117 -0
  39. package/build/esm/src/stave.js +13 -8
  40. package/build/esm/src/stavebarline.js +53 -1
  41. package/build/esm/src/staveline.js +2 -0
  42. package/build/esm/src/stavenote.js +54 -19
  43. package/build/esm/src/stavetie.js +3 -1
  44. package/build/esm/src/stem.js +2 -1
  45. package/build/esm/src/stemmablenote.js +9 -0
  46. package/build/esm/src/stringnumber.js +0 -1
  47. package/build/esm/src/strokes.js +10 -0
  48. package/build/esm/src/svgcontext.js +14 -4
  49. package/build/esm/src/svgexport.js +68 -0
  50. package/build/esm/src/system.js +14 -0
  51. package/build/esm/src/tables.js +69 -37
  52. package/build/esm/src/tabnote.js +20 -1
  53. package/build/esm/src/textbracket.js +5 -4
  54. package/build/esm/src/textdynamics.js +5 -0
  55. package/build/esm/src/textmetricscache.js +93 -0
  56. package/build/esm/src/textnote.js +3 -3
  57. package/build/esm/src/tickable.js +56 -22
  58. package/build/esm/src/timesignature.js +30 -9
  59. package/build/esm/src/timesignote.js +3 -1
  60. package/build/esm/src/tuning.js +2 -2
  61. package/build/esm/src/tuplet.js +79 -29
  62. package/build/esm/src/util.js +1 -0
  63. package/build/esm/src/version.js +3 -3
  64. package/build/esm/src/vexflow.js +14 -2
  65. package/build/esm/src/voice.js +44 -8
  66. package/build/esm/tests/accidental_tests.js +52 -0
  67. package/build/esm/tests/annotation_tests.js +38 -0
  68. package/build/esm/tests/articulation_tests.js +34 -0
  69. package/build/esm/tests/beam_tests.js +17 -0
  70. package/build/esm/tests/curve_tests.js +81 -0
  71. package/build/esm/tests/font_tests.js +32 -1
  72. package/build/esm/tests/formatter_tests.js +58 -23
  73. package/build/esm/tests/gracenote_tests.js +58 -0
  74. package/build/esm/tests/gracetabnote_tests.js +40 -0
  75. package/build/esm/tests/keysignature_tests.js +14 -3
  76. package/build/esm/tests/node/metrics-tests.mjs +278 -0
  77. package/build/esm/tests/node/package-input-tests.mjs +149 -0
  78. package/build/esm/tests/node/render-metrics-tests.mjs +78 -0
  79. package/build/esm/tests/node/rendering-tests.mjs +75 -0
  80. package/build/esm/tests/node/sheet-tests.mjs +202 -0
  81. package/build/esm/tests/node/svg-export-tests.mjs +182 -0
  82. package/build/esm/tests/pedalmarking_tests.js +25 -0
  83. package/build/esm/tests/rests_tests.js +6 -0
  84. package/build/esm/tests/stave_tests.js +6 -3
  85. package/build/esm/tests/stavenote_tests.js +100 -55
  86. package/build/esm/tests/strokes_tests.js +19 -5
  87. package/build/esm/tests/timesignature_tests.js +19 -0
  88. package/build/esm/tests/tuplet_tests.js +273 -12
  89. package/build/esm/tests/unison_tests.js +13 -13
  90. package/build/esm/tests/voice_tests.js +34 -0
  91. package/build/esm/tools/render-metrics.mjs +49 -0
  92. package/build/esm/tools/score-fixtures.mjs +94 -0
  93. package/build/esm/tools/sheet-fixture.mjs +90 -0
  94. package/build/esm/tools/visual-regression.mjs +106 -0
  95. package/build/types/src/annotation.d.ts +17 -1
  96. package/build/types/src/curve.d.ts +24 -7
  97. package/build/types/src/easyscore.d.ts +8 -0
  98. package/build/types/src/element.d.ts +7 -0
  99. package/build/types/src/factory.d.ts +18 -1
  100. package/build/types/src/font.d.ts +2 -0
  101. package/build/types/src/formatter.d.ts +23 -14
  102. package/build/types/src/gracenotegroup.d.ts +5 -0
  103. package/build/types/src/index.d.ts +2 -0
  104. package/build/types/src/metrics.d.ts +2 -0
  105. package/build/types/src/modifiercontext.d.ts +4 -0
  106. package/build/types/src/pedalmarking.d.ts +4 -1
  107. package/build/types/src/registry.d.ts +2 -0
  108. package/build/types/src/rendercontext.d.ts +12 -0
  109. package/build/types/src/sheet.d.ts +49 -0
  110. package/build/types/src/stave.d.ts +4 -4
  111. package/build/types/src/stavebarline.d.ts +10 -0
  112. package/build/types/src/stavenote.d.ts +11 -0
  113. package/build/types/src/strokes.d.ts +4 -0
  114. package/build/types/src/svgcontext.d.ts +5 -0
  115. package/build/types/src/svgexport.d.ts +22 -0
  116. package/build/types/src/system.d.ts +4 -0
  117. package/build/types/src/tables.d.ts +9 -0
  118. package/build/types/src/tabnote.d.ts +4 -0
  119. package/build/types/src/textbracket.d.ts +1 -1
  120. package/build/types/src/textdynamics.d.ts +3 -0
  121. package/build/types/src/textmetricscache.d.ts +21 -0
  122. package/build/types/src/tickable.d.ts +27 -10
  123. package/build/types/src/timesignature.d.ts +4 -0
  124. package/build/types/src/tuplet.d.ts +8 -0
  125. package/build/types/src/util.d.ts +1 -0
  126. package/build/types/src/vexflow.d.ts +8 -1
  127. package/build/types/src/voice.d.ts +6 -0
  128. package/changelog/CHANGELOG.md +109 -0
  129. package/package.json +44 -26
  130. package/tests/flow.html +15 -7
@@ -4,6 +4,7 @@ import { Accidental } from '../src/accidental.js';
4
4
  import { Bend } from '../src/bend.js';
5
5
  import { CanvasContext } from '../src/canvascontext.js';
6
6
  import { Font, FontStyle, FontWeight } from '../src/font.js';
7
+ import { Metrics, MetricsDefaults } from '../src/metrics.js';
7
8
  import { PedalMarking } from '../src/pedalmarking.js';
8
9
  import { StaveNote } from '../src/stavenote.js';
9
10
  import { TextBracket } from '../src/textbracket.js';
@@ -12,6 +13,7 @@ import { Voice } from '../src/voice.js';
12
13
  const FontTests = {
13
14
  Start() {
14
15
  QUnit.module('Font');
16
+ QUnit.test('textFontDefaults', textFontDefaults);
15
17
  QUnit.test('setFont', setFont);
16
18
  QUnit.test('Parsing', fontParsing);
17
19
  QUnit.test('Sizes', fontSizes);
@@ -49,7 +51,7 @@ function fontParsing(assert) {
49
51
  { type: Bend.DOWN, text: '' },
50
52
  ]);
51
53
  const bFont = b.fontInfo;
52
- assert.equal(bFont === null || bFont === void 0 ? void 0 : bFont.family, 'Bravura,Academico');
54
+ assert.equal(bFont === null || bFont === void 0 ? void 0 : bFont.family, 'Academico,Bravura');
53
55
  assert.equal(bFont === null || bFont === void 0 ? void 0 : bFont.size, 10);
54
56
  assert.equal(bFont === null || bFont === void 0 ? void 0 : bFont.weight, FontWeight.NORMAL);
55
57
  assert.equal(bFont === null || bFont === void 0 ? void 0 : bFont.style, FontStyle.NORMAL);
@@ -132,3 +134,32 @@ function setMusicFontToPetaluma(options) {
132
134
  }
133
135
  VexFlowTests.register(FontTests);
134
136
  export { FontTests };
137
+ function textFontDefaults(assert) {
138
+ const fonts = VexFlow.getFonts();
139
+ const previous = MetricsDefaults.Annotation.fontFamily;
140
+ try {
141
+ VexFlow.setFonts('Bravura', 'Academico');
142
+ const annotation = new VexFlow.Annotation('Sing with gentle voices');
143
+ const ctx = document.createElement('canvas').getContext('2d');
144
+ ctx.font = Font.toCSSString(annotation.fontInfo);
145
+ const actual = ctx.measureText(annotation.getText()).width;
146
+ ctx.font = Font.toCSSString(Object.assign(Object.assign({}, annotation.fontInfo), { family: 'Academico' }));
147
+ assert.equal(actual, ctx.measureText(annotation.getText()).width);
148
+ assert.equal(new Accidental('#').fontInfo.family, 'Bravura,Academico');
149
+ assert.equal(annotation.setFont('Georgia', 12).fontInfo.family, 'Georgia');
150
+ VexFlow.setTextFonts('Courier New');
151
+ assert.ok(new VexFlow.Annotation('Custom').fontInfo.family.startsWith('Courier New'));
152
+ VexFlow.setFonts('Petaluma', 'Bravura', 'Leland');
153
+ assert.equal(new Accidental('#').fontInfo.family, 'Petaluma,Bravura,Leland');
154
+ MetricsDefaults.Annotation.fontFamily = 'Georgia';
155
+ Metrics.clear();
156
+ assert.equal(new VexFlow.Annotation('Class override').fontInfo.family, 'Georgia');
157
+ }
158
+ finally {
159
+ if (previous === undefined)
160
+ delete MetricsDefaults.Annotation.fontFamily;
161
+ else
162
+ MetricsDefaults.Annotation.fontFamily = previous;
163
+ VexFlow.setFonts(...fonts);
164
+ }
165
+ }
@@ -23,9 +23,32 @@ import { StringNumber } from '../src/stringnumber.js';
23
23
  import { Tuplet } from '../src/tuplet.js';
24
24
  import { Voice } from '../src/voice.js';
25
25
  import { MockTickable } from './mocks.js';
26
+ const QUARTER_NOTE_DURATION = VexFlow.RESOLUTION / 4;
27
+ function createTickable(beat) {
28
+ return new MockTickable().setTicks(beat);
29
+ }
30
+ function getTestVoices() {
31
+ const BEAT = VexFlow.RESOLUTION / 4;
32
+ const tickables1 = [
33
+ createTickable(BEAT).setWidth(10),
34
+ createTickable(BEAT * 2).setWidth(20),
35
+ createTickable(BEAT).setWidth(30),
36
+ ];
37
+ const tickables2 = [
38
+ createTickable(BEAT * 2).setWidth(10),
39
+ createTickable(BEAT).setWidth(20),
40
+ createTickable(BEAT).setWidth(30),
41
+ ];
42
+ const voice1 = new Voice(VexFlow.TIME4_4);
43
+ const voice2 = new Voice(VexFlow.TIME4_4);
44
+ voice1.addTickables(tickables1);
45
+ voice2.addTickables(tickables2);
46
+ return [voice1, voice2];
47
+ }
26
48
  const FormatterTests = {
27
49
  Start() {
28
50
  QUnit.module('Formatter');
51
+ QUnit.test('getResolutionMultiplier', getResolutionMultiplier);
29
52
  QUnit.test('TickContext Building', buildTickContexts);
30
53
  const run = VexFlowTests.runTests;
31
54
  run('Penultimate Note Padding', penultimateNote);
@@ -55,25 +78,37 @@ function getGlyphWidth(glyph) {
55
78
  el.setText(glyph);
56
79
  return el.getWidth();
57
80
  }
58
- function buildTickContexts(assert) {
59
- function createTickable(beat) {
60
- return new MockTickable().setTicks(beat);
81
+ function getResolutionMultiplier(assert) {
82
+ const [voice1, voice2] = getTestVoices();
83
+ assert.equal(voice1.getResolutionMultiplier(), voice2.getResolutionMultiplier(), 'Voices with same smallest duration have the same resolution multiplier');
84
+ assert.equal(Formatter.getResolutionMultiplier([voice1, voice2]), voice1.getResolutionMultiplier(), 'LCM of voice1, voice2 is voice1');
85
+ const TIME1_4 = {
86
+ numBeats: 1,
87
+ beatValue: 4,
88
+ resolution: VexFlow.RESOLUTION,
89
+ };
90
+ const tickables3 = [];
91
+ for (let i = 0; i < 3; i++) {
92
+ tickables3.push(new StaveNote({ keys: ['f/4'], duration: '8' }).setWidth(15));
61
93
  }
62
- const BEAT = (1 * VexFlow.RESOLUTION) / 4;
63
- const tickables1 = [
64
- createTickable(BEAT).setWidth(10),
65
- createTickable(BEAT * 2).setWidth(20),
66
- createTickable(BEAT).setWidth(30),
67
- ];
68
- const tickables2 = [
69
- createTickable(BEAT * 2).setWidth(10),
70
- createTickable(BEAT).setWidth(20),
71
- createTickable(BEAT).setWidth(30),
72
- ];
73
- const voice1 = new Voice(VexFlow.TIME4_4);
74
- const voice2 = new Voice(VexFlow.TIME4_4);
75
- voice1.addTickables(tickables1);
76
- voice2.addTickables(tickables2);
94
+ new Tuplet(tickables3, { numNotes: 3, notesOccupied: 2 });
95
+ const voice3 = new Voice(TIME1_4);
96
+ voice3.addTickables(tickables3);
97
+ const tickables4 = [];
98
+ for (let i = 0; i < 5; i++) {
99
+ tickables4.push(new StaveNote({ keys: ['f/4'], duration: '16' }).setWidth(9));
100
+ }
101
+ new Tuplet(tickables4, { numNotes: 5, notesOccupied: 4 });
102
+ const voice4 = new Voice(TIME1_4);
103
+ voice4.addTickables(tickables4);
104
+ assert.notEqual(voice3.getResolutionMultiplier(), voice4.getResolutionMultiplier());
105
+ assert.notEqual(Formatter.getResolutionMultiplier([voice3, voice4]), voice3.getResolutionMultiplier(), 'LCM of RM of voices 3 and 4 is neither RM(3) nor RM(4)');
106
+ assert.notEqual(Formatter.getResolutionMultiplier([voice3, voice4]), voice4.getResolutionMultiplier(), 'LCM of RM of voices 3 and 4 is neither RM(3) nor RM(4)');
107
+ }
108
+ function buildTickContexts(assert) {
109
+ const [voice1, voice2] = getTestVoices();
110
+ const tickables1 = voice1.getTickables();
111
+ const tickables2 = voice2.getTickables();
77
112
  const formatter = new Formatter();
78
113
  const tContexts = formatter.createTickContexts([voice1, voice2]);
79
114
  assert.equal(tContexts.list.length, 4, 'Voices should have four tick contexts');
@@ -105,10 +140,10 @@ function rightJustify(options) {
105
140
  stave.drawWithStyle();
106
141
  voice.draw(f.getContext(), stave);
107
142
  };
108
- renderTest({ numBeats: 4, beatValue: 4, resolution: 4 * 4096 }, 3, '4', '2', 10, 300);
109
- renderTest({ numBeats: 4, beatValue: 4, resolution: 4 * 4096 }, 1, 'w', 'w', 310, 300);
110
- renderTest({ numBeats: 3, beatValue: 4, resolution: 4 * 4096 }, 3, '4', '4', 610, 300);
111
- renderTest({ numBeats: 3, beatValue: 4, resolution: 4 * 4096 }, 6, '8', '8', 910, 300);
143
+ renderTest({ numBeats: 4, beatValue: 4, resolution: 4 * QUARTER_NOTE_DURATION }, 3, '4', '2', 10, 300);
144
+ renderTest({ numBeats: 4, beatValue: 4, resolution: 4 * QUARTER_NOTE_DURATION }, 1, 'w', 'w', 310, 300);
145
+ renderTest({ numBeats: 3, beatValue: 4, resolution: 4 * QUARTER_NOTE_DURATION }, 3, '4', '4', 610, 300);
146
+ renderTest({ numBeats: 3, beatValue: 4, resolution: 4 * QUARTER_NOTE_DURATION }, 6, '8', '8', 910, 300);
112
147
  options.assert.ok(true);
113
148
  }
114
149
  function penultimateNote(options) {
@@ -700,7 +735,7 @@ function annotations(options) {
700
735
  });
701
736
  let notesToBeam = [];
702
737
  notes.forEach((note) => {
703
- if (note.getIntrinsicTicks() < 4096) {
738
+ if (note.getIntrinsicTicks() < QUARTER_NOTE_DURATION) {
704
739
  notesToBeam.push(note);
705
740
  if (notesToBeam.length >= beamGroup) {
706
741
  beams.push(new Beam(notesToBeam));
@@ -1,3 +1,4 @@
1
+ import { VexFlow } from '../src/vexflow.js';
1
2
  import { VexFlowTests } from './vexflow_test_helpers.js';
2
3
  import { ModifierPosition } from '../src.js';
3
4
  import { Accidental } from '../src/accidental.js';
@@ -5,9 +6,13 @@ import { Annotation } from '../src/annotation.js';
5
6
  import { Articulation } from '../src/articulation.js';
6
7
  import { Dot } from '../src/dot.js';
7
8
  import { Formatter } from '../src/formatter.js';
9
+ import { GraceNote } from '../src/gracenote.js';
10
+ import { GraceNoteGroup } from '../src/gracenotegroup.js';
11
+ import { StaveNote } from '../src/stavenote.js';
8
12
  const GraceNoteTests = {
9
13
  Start() {
10
14
  QUnit.module('Grace Notes');
15
+ QUnit.test('boundsBeforeDrawing', boundsBeforeDrawing);
11
16
  const run = VexFlowTests.runTests;
12
17
  run('Grace Note Basic', basic);
13
18
  run('With Articulation and Annotation on Parent Note', graceNoteModifiers);
@@ -35,8 +40,32 @@ const GraceNoteTests = {
35
40
  run('Grace Note Slash with Beams', slashWithBeams);
36
41
  run('Grace Notes Multiple Voices', multipleVoices);
37
42
  run('Grace Notes Multiple Voices Multiple Draws', multipleVoicesMultipleDraws);
43
+ run('Grace Note Group Bounding Box', boundingBox);
38
44
  },
39
45
  };
46
+ function boundingBox(options) {
47
+ const f = VexFlowTests.makeFactory(options, 300, 130);
48
+ const stave = f.Stave({ x: 10, y: 10, width: 250 });
49
+ const graceNotes = [
50
+ { keys: ['e/4'], duration: '16' },
51
+ { keys: ['f/4'], duration: '16' },
52
+ { keys: ['g/5'], duration: '16' },
53
+ ].map(f.GraceNote.bind(f));
54
+ const group = f.GraceNoteGroup({ notes: graceNotes });
55
+ group.beamNotes();
56
+ const note = f.StaveNote({ keys: ['b/4'], duration: 'q' }).addModifier(group, 0);
57
+ const voice = f.Voice().setStrict(false).addTickables([note]);
58
+ f.Formatter().joinVoices([voice]).formatToStave([voice], stave);
59
+ f.draw();
60
+ VexFlowTests.drawBoundingBox(f.getContext(), group);
61
+ const bb = group.getBoundingBox();
62
+ options.assert.ok(bb.getX() > 0 && bb.getY() > 0, 'Group box is not at the origin');
63
+ options.assert.ok(bb.getX() + bb.getW() <= note.getAbsoluteX(), 'Group box ends left of the main note');
64
+ options.assert.ok(bb.getY() < stave.getBottomLineBottomY() && bb.getY() + bb.getH() > stave.getTopLineTopY(), 'Group box overlaps the stave vertically');
65
+ options.assert.ok(bb.getY() > stave.getTopLineTopY() - 40, 'Group box is near the stave');
66
+ options.assert.ok(note.getBoundingBox().getX() >= bb.getX() - 10, 'Main note box is not stretched to the origin');
67
+ options.assert.ok(new GraceNoteGroup([]).getBoundingBox() !== undefined, 'Empty group has a box');
68
+ }
40
69
  function basic(options) {
41
70
  const f = VexFlowTests.makeFactory(options, 700, 130);
42
71
  const stave = f.Stave({ x: 10, y: 10, width: 650 });
@@ -412,3 +441,32 @@ function multipleVoicesMultipleDraws(options) {
412
441
  }
413
442
  VexFlowTests.register(GraceNoteTests);
414
443
  export { GraceNoteTests };
444
+ function boundsBeforeDrawing(assert) {
445
+ for (const beamed of [false, true]) {
446
+ const ctx = new VexFlow.SVGContext(document.createElement('div'));
447
+ const stave = new VexFlow.Stave(80, 75, 550).setContext(ctx);
448
+ const grace = [
449
+ new GraceNote({ keys: ['d/4', 'e/4'], duration: '8', stemDirection: beamed ? -1 : 1 }),
450
+ new GraceNote({ keys: ['g/4'], duration: '8', stemDirection: beamed ? -1 : 1 }),
451
+ ];
452
+ const group = new GraceNoteGroup(grace);
453
+ if (beamed)
454
+ group.beamNotes();
455
+ const parent = new StaveNote({ keys: ['c/5'], duration: 'q' }).setStave(stave).addModifier(group, 0);
456
+ const dynamics = new VexFlow.TextDynamics({ text: 'sfz', duration: 'q', line: 8 }).setStave(stave);
457
+ const voice = new VexFlow.Voice('2/4').addTickables([parent, dynamics]);
458
+ new Formatter().joinVoices([voice]).formatToStave([voice], stave);
459
+ const before = group.getBoundingBox(), dynamicsBefore = dynamics.getBoundingBox();
460
+ assert.deepEqual(group.getBoundingBox(), before);
461
+ assert.notOk(grace.some((note) => note.isRendered()));
462
+ voice.draw(ctx, stave);
463
+ assert.deepEqual(group.getBoundingBox(), before);
464
+ assert.deepEqual(dynamics.getBoundingBox(), dynamicsBefore);
465
+ assert.ok(before.x > stave.getX() && before.w > 0);
466
+ assert.equal(dynamicsBefore.x, dynamics.getAbsoluteX());
467
+ assert.equal(dynamicsBefore.y, stave.getYForLine(5) - dynamics.getTextMetrics().actualBoundingBoxAscent);
468
+ parent.setStave(new VexFlow.Stave(140, 100, 600).setContext(ctx));
469
+ const moved = group.getBoundingBox();
470
+ assert.ok(moved.x > before.x && moved.y > before.y);
471
+ }
472
+ }
@@ -1,14 +1,17 @@
1
1
  import { VexFlow } from '../src/vexflow.js';
2
2
  import { VexFlowTests } from './vexflow_test_helpers.js';
3
+ import { Font } from '../src/font.js';
3
4
  import { Formatter } from '../src/formatter.js';
4
5
  import { GraceNoteGroup } from '../src/gracenotegroup.js';
5
6
  import { GraceTabNote } from '../src/gracetabnote.js';
7
+ import { Metrics, MetricsDefaults } from '../src/metrics.js';
6
8
  import { TabNote } from '../src/tabnote.js';
7
9
  import { TabStave } from '../src/tabstave.js';
8
10
  import { Voice } from '../src/voice.js';
9
11
  const GraceTabNoteTests = {
10
12
  Start() {
11
13
  QUnit.module('Grace Tab Notes');
14
+ QUnit.test('fretSizing', fretSizing);
12
15
  const run = VexFlowTests.runTests;
13
16
  run('Grace Tab Note Simple', simple);
14
17
  run('Grace Tab Note Slurred', slurred);
@@ -77,3 +80,40 @@ function slurred(options, contextBuilder) {
77
80
  }
78
81
  VexFlowTests.register(GraceTabNoteTests);
79
82
  export { GraceTabNoteTests };
83
+ function fretSizing(assert) {
84
+ class InspectableGraceTabNote extends GraceTabNote {
85
+ getFrets() {
86
+ return this.fretElement;
87
+ }
88
+ }
89
+ const scale = MetricsDefaults.GraceTabNote.fontScale;
90
+ try {
91
+ const note = new InspectableGraceTabNote({
92
+ positions: [
93
+ { str: 2, fret: 12 },
94
+ { str: 4, fret: 3 },
95
+ ],
96
+ duration: '8',
97
+ });
98
+ for (const size of [4, 6, '12px', 14]) {
99
+ note.setFontSize(size).preFormat();
100
+ for (const fret of note.getFrets()) {
101
+ assert.equal(Font.convertSizeToPointValue(fret.fontInfo.size), Font.convertSizeToPointValue(size));
102
+ const metrics = fret.getTextMetrics();
103
+ assert.equal(fret.getYShift(), (metrics.actualBoundingBoxAscent - metrics.actualBoundingBoxDescent) / 2);
104
+ }
105
+ }
106
+ note.setFontSize(7).setGhost(true);
107
+ assert.equal(Font.convertSizeToPointValue(note.getFrets()[0].fontInfo.size), 7);
108
+ note.setGhost(false).setFontSize();
109
+ assert.equal(Font.convertSizeToPointValue(note.getFrets()[0].fontInfo.size), 6);
110
+ MetricsDefaults.GraceTabNote.fontScale = 1 / 3;
111
+ Metrics.clear('GraceTabNote');
112
+ const small = new InspectableGraceTabNote({ positions: [{ str: 2, fret: 12 }], duration: '8' });
113
+ assert.equal(Font.convertSizeToPointValue(small.getFrets()[0].fontInfo.size), 3);
114
+ }
115
+ finally {
116
+ MetricsDefaults.GraceTabNote.fontScale = scale;
117
+ Metrics.clear('GraceTabNote');
118
+ }
119
+ }
@@ -28,20 +28,25 @@ const fontWidths = () => {
28
28
  return { sharpWidth, flatWidth, naturalWidth, clefWidth };
29
29
  };
30
30
  function parser(assert) {
31
- assert.expect(11);
31
+ assert.expect(16);
32
32
  function catchError(spec) {
33
33
  assert.throws(() => VexFlow.keySignature(spec), /BadKeySignature/);
34
34
  }
35
35
  catchError('asdf');
36
36
  catchError('D!');
37
37
  catchError('E#');
38
- catchError('D#');
39
38
  catchError('#');
40
39
  catchError('b');
41
40
  catchError('Kb');
42
41
  catchError('Fb');
43
- catchError('Dbm');
44
42
  catchError('B#m');
43
+ catchError('flats_15');
44
+ catchError('sharps_16');
45
+ catchError('flat_1');
46
+ catchError('sharps_-1');
47
+ catchError('sharp_0');
48
+ catchError('sharps_B');
49
+ catchError('flats_c');
45
50
  VexFlow.keySignature('B');
46
51
  VexFlow.keySignature('C');
47
52
  VexFlow.keySignature('Fm');
@@ -49,6 +54,12 @@ function parser(assert) {
49
54
  VexFlow.keySignature('Abm');
50
55
  VexFlow.keySignature('F#');
51
56
  VexFlow.keySignature('G#m');
57
+ VexFlow.keySignature('flats_8');
58
+ VexFlow.keySignature('sharps_14');
59
+ VexFlow.keySignature('flats_10');
60
+ VexFlow.keySignature('sharps_8');
61
+ VexFlow.keySignature('flats_0');
62
+ VexFlow.keySignature('sharps_0');
52
63
  assert.ok(true, 'all pass');
53
64
  }
54
65
  function majorKeys(options, contextBuilder) {
@@ -0,0 +1,278 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import assert from 'node:assert/strict';
11
+ import test from 'node:test';
12
+ const { Element, Font, Metrics, MetricsDefaults } = await import(new URL('../../build/esm/entry/vexflow-core.js', import.meta.url));
13
+ function canvas() {
14
+ let calls = 0;
15
+ const context = {
16
+ font: '',
17
+ direction: 'ltr',
18
+ textAlign: 'start',
19
+ textBaseline: 'alphabetic',
20
+ measureText(text) {
21
+ calls++;
22
+ return Object.create({
23
+ width: text.length * 5,
24
+ actualBoundingBoxLeft: 1,
25
+ actualBoundingBoxRight: 9,
26
+ actualBoundingBoxAscent: 8,
27
+ actualBoundingBoxDescent: 2,
28
+ fontBoundingBoxAscent: 11,
29
+ fontBoundingBoxDescent: 4,
30
+ emHeightAscent: 10,
31
+ emHeightDescent: 3,
32
+ alphabeticBaseline: 0,
33
+ hangingBaseline: 7,
34
+ ideographicBaseline: -3,
35
+ });
36
+ },
37
+ };
38
+ return { getContext: () => context, context, calls: () => calls };
39
+ }
40
+ test('font and style records preserve caller isolation without deep cloning', () => {
41
+ for (const [get, key] of [
42
+ [Metrics.getFontInfo.bind(Metrics), 'StaveNote'],
43
+ [Metrics.getStyle.bind(Metrics), 'StaveNote'],
44
+ ]) {
45
+ const original = get(key);
46
+ const edited = get(key);
47
+ assert.notEqual(edited, original);
48
+ for (const field of Object.keys(edited))
49
+ edited[field] = 'changed';
50
+ assert.deepEqual(get(key), original);
51
+ }
52
+ const first = new Element();
53
+ const second = new Element();
54
+ first.fontInfo.size = 99;
55
+ first.getStyle().lineDash = '2,4';
56
+ assert.notEqual(second.fontInfo.size, 99);
57
+ assert.notEqual(second.getStyle().lineDash, '2,4');
58
+ });
59
+ test('elements and width lookups share native measurements but not mutable results', () => {
60
+ const measured = canvas();
61
+ Element.setTextMeasurementCanvas(measured);
62
+ const first = new Element().setText('abc');
63
+ const metrics = first.measureText();
64
+ assert.equal(metrics.width, 15);
65
+ assert.equal(first.getHeight(), 10);
66
+ assert.equal(metrics.fontBoundingBoxAscent, 11);
67
+ assert.equal(metrics.ideographicBaseline, -3);
68
+ metrics.width = 999;
69
+ metrics.actualBoundingBoxAscent = 100;
70
+ const second = new Element().setText('abc');
71
+ assert.equal(second.getWidth(), 15);
72
+ assert.equal(second.getHeight(), 10);
73
+ assert.equal(Element.measureWidth('abc', 'Element'), 15);
74
+ assert.equal(first.measureText().width, 15);
75
+ assert.equal(measured.calls(), 1);
76
+ });
77
+ test('text, font, shaping, alignment, and baseline settings are distinct cache keys', () => {
78
+ const measured = canvas();
79
+ Element.setTextMeasurementCanvas(measured);
80
+ const el = new Element().setText('one');
81
+ el.measureText();
82
+ el.setText('two').measureText();
83
+ el.setFontSize(22).measureText();
84
+ assert.equal(measured.calls(), 3);
85
+ for (const [name, value] of Object.entries({
86
+ direction: 'rtl',
87
+ textAlign: 'right',
88
+ textBaseline: 'top',
89
+ fontKerning: 'none',
90
+ fontStretch: 'expanded',
91
+ fontVariantCaps: 'small-caps',
92
+ letterSpacing: '1px',
93
+ wordSpacing: '2px',
94
+ textRendering: 'optimizeLegibility',
95
+ lang: 'ja',
96
+ })) {
97
+ const before = measured.calls();
98
+ measured.context[name] = value;
99
+ el.measureText();
100
+ el.measureText();
101
+ assert.equal(measured.calls(), before + 1, name);
102
+ }
103
+ });
104
+ test('canvas replacement and either form of Metrics.clear invalidate shared measurements', () => {
105
+ const first = canvas();
106
+ Element.setTextMeasurementCanvas(first);
107
+ Element.measureWidth('same');
108
+ Element.measureWidth('same');
109
+ assert.equal(first.calls(), 1);
110
+ for (const key of [undefined, 'Element']) {
111
+ Metrics.clear(key);
112
+ Element.measureWidth('same');
113
+ }
114
+ assert.equal(first.calls(), 3);
115
+ Element.setTextMeasurementCanvas(first);
116
+ Element.measureWidth('same');
117
+ assert.equal(first.calls(), 4, 'reinstalling a reconfigured canvas also clears the cache');
118
+ const second = canvas();
119
+ Element.setTextMeasurementCanvas(second);
120
+ Element.measureWidth('same');
121
+ assert.equal(second.calls(), 1);
122
+ Element.clearTextMeasurementCache();
123
+ Element.measureWidth('same');
124
+ assert.equal(second.calls(), 2, 'external font registration can invalidate measurements explicitly');
125
+ });
126
+ test('inherited canvas direction and language participate in measurement keys', () => {
127
+ const measured = canvas();
128
+ let direction = 'ltr';
129
+ let language = 'en';
130
+ Object.assign(measured, {
131
+ ownerDocument: {
132
+ defaultView: { getComputedStyle: () => ({ direction }) },
133
+ documentElement: { lang: 'en' },
134
+ },
135
+ closest: () => ({ getAttribute: () => language }),
136
+ isConnected: true,
137
+ });
138
+ Object.assign(measured.context, { canvas: measured, direction: 'inherit', lang: 'inherit' });
139
+ Element.setTextMeasurementCanvas(measured);
140
+ Element.measureWidth('text');
141
+ Element.measureWidth('text');
142
+ assert.equal(measured.calls(), 1);
143
+ direction = 'rtl';
144
+ Element.measureWidth('text');
145
+ language = 'ja';
146
+ Element.measureWidth('text');
147
+ assert.equal(measured.calls(), 3);
148
+ });
149
+ test('category font/style caches refresh after edits to defaults', () => {
150
+ const key = 'MeasurementTest';
151
+ try {
152
+ MetricsDefaults[key] = { fontSize: 12, fillStyle: '#123456' };
153
+ Metrics.clear(key);
154
+ assert.equal(Metrics.getFontInfo(key).size, 12);
155
+ assert.equal(Metrics.getStyle(key).fillStyle, '#123456');
156
+ MetricsDefaults[key].fontSize = 24;
157
+ MetricsDefaults[key].fillStyle = '#abcdef';
158
+ Metrics.clear(key);
159
+ assert.equal(Metrics.getFontInfo(key).size, 24);
160
+ assert.equal(Metrics.getStyle(key).fillStyle, '#abcdef');
161
+ }
162
+ finally {
163
+ delete MetricsDefaults[key];
164
+ Metrics.clear(key);
165
+ }
166
+ });
167
+ test('cache capacity is bounded, hot entries survive, and very long keys are not retained', () => {
168
+ const measured = canvas();
169
+ Element.setTextMeasurementCanvas(measured);
170
+ Element.measureWidth('old');
171
+ for (let i = 0; i < 2200; i++) {
172
+ Element.measureWidth('hot');
173
+ Element.measureWidth(`entry ${i}`);
174
+ }
175
+ const before = measured.calls();
176
+ Element.measureWidth('hot');
177
+ assert.equal(measured.calls(), before);
178
+ Element.measureWidth('old');
179
+ assert.equal(measured.calls(), before + 1);
180
+ Element.measureWidth('x'.repeat(5000));
181
+ Element.measureWidth('x'.repeat(5000));
182
+ assert.equal(measured.calls(), before + 3);
183
+ });
184
+ class Fonts extends EventTarget {
185
+ constructor() {
186
+ super(...arguments);
187
+ this.status = 'loaded';
188
+ this.size = 0;
189
+ }
190
+ add() {
191
+ this.size++;
192
+ }
193
+ }
194
+ test('document and worker font loading, errors, and synchronous set changes invalidate the cache', () => {
195
+ const oldDocument = globalThis.document;
196
+ const oldFonts = globalThis.fonts;
197
+ try {
198
+ for (const location of ['document', 'worker']) {
199
+ const fonts = new Fonts();
200
+ if (location === 'document')
201
+ globalThis.document = { fonts };
202
+ else {
203
+ delete globalThis.document;
204
+ globalThis.fonts = fonts;
205
+ }
206
+ const measured = canvas();
207
+ Element.setTextMeasurementCanvas(measured);
208
+ Element.measureWidth('glyph');
209
+ fonts.status = 'loading';
210
+ Element.measureWidth('glyph');
211
+ Element.measureWidth('glyph');
212
+ assert.equal(measured.calls(), 3, 'fallback metrics are not shared while loading');
213
+ fonts.status = 'loaded';
214
+ Element.measureWidth('glyph');
215
+ Element.measureWidth('glyph');
216
+ assert.equal(measured.calls(), 4);
217
+ for (const type of ['loading', 'loadingdone', 'loadingerror']) {
218
+ fonts.dispatchEvent(new Event(type));
219
+ Element.measureWidth('glyph');
220
+ }
221
+ assert.equal(measured.calls(), 7);
222
+ fonts.size++;
223
+ Element.measureWidth('glyph');
224
+ fonts.size--;
225
+ Element.measureWidth('glyph');
226
+ assert.equal(measured.calls(), 9, 'loaded faces can be added/deleted without loading events');
227
+ }
228
+ }
229
+ finally {
230
+ if (oldDocument === undefined)
231
+ delete globalThis.document;
232
+ else
233
+ globalThis.document = oldDocument;
234
+ if (oldFonts === undefined)
235
+ delete globalThis.fonts;
236
+ else
237
+ globalThis.fonts = oldFonts;
238
+ Metrics.clear();
239
+ }
240
+ });
241
+ test('Font.load clears measurements when a font finishes loading or fails', () => __awaiter(void 0, void 0, void 0, function* () {
242
+ const oldFace = globalThis.FontFace;
243
+ let settle;
244
+ globalThis.FontFace = class {
245
+ load() {
246
+ return new Promise((resolve, reject) => {
247
+ settle = { resolve, reject };
248
+ });
249
+ }
250
+ };
251
+ try {
252
+ for (const fails of [false, true]) {
253
+ const measured = canvas();
254
+ Element.setTextMeasurementCanvas(measured);
255
+ Element.measureWidth('glyph');
256
+ const loading = Font.load('Test', 'data:font/otf;base64,AA==');
257
+ Element.measureWidth('glyph');
258
+ assert.equal(measured.calls(), 2, 'load start clears old results');
259
+ if (fails) {
260
+ settle.reject(new Error('font failed'));
261
+ yield assert.rejects(loading, /font failed/);
262
+ }
263
+ else {
264
+ settle.resolve({ family: 'Test' });
265
+ yield loading;
266
+ }
267
+ Element.measureWidth('glyph');
268
+ assert.equal(measured.calls(), 3, 'settlement clears fallback results');
269
+ }
270
+ }
271
+ finally {
272
+ if (oldFace === undefined)
273
+ delete globalThis.FontFace;
274
+ else
275
+ globalThis.FontFace = oldFace;
276
+ Metrics.clear();
277
+ }
278
+ }));