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
@@ -9,39 +9,6 @@ import { TickContext } from './tickcontext.js';
9
9
  import { isNote, isStaveNote } from './typeguard.js';
10
10
  import { defined, log, midLine, RuntimeError, sumArray } from './util.js';
11
11
  import { Voice } from './voice.js';
12
- function createContexts(voices, makeContext, addToContext) {
13
- if (voices.length === 0)
14
- return {
15
- map: {},
16
- array: [],
17
- list: [],
18
- resolutionMultiplier: 0,
19
- };
20
- const tickToContextMap = {};
21
- const tickList = [];
22
- const contexts = [];
23
- const resolutionMultiplier = Formatter.getResolutionMultiplier(voices);
24
- voices.forEach((voice, voiceIndex) => {
25
- const ticksUsed = new Fraction(0, resolutionMultiplier);
26
- voice.getTickables().forEach((tickable) => {
27
- const integerTicks = ticksUsed.numerator;
28
- if (!tickToContextMap[integerTicks]) {
29
- const newContext = makeContext({ tickID: integerTicks });
30
- contexts.push(newContext);
31
- tickToContextMap[integerTicks] = newContext;
32
- tickList.push(integerTicks);
33
- }
34
- addToContext(tickable, tickToContextMap[integerTicks], voiceIndex);
35
- ticksUsed.add(tickable.getTicks());
36
- });
37
- });
38
- return {
39
- map: tickToContextMap,
40
- array: contexts,
41
- list: tickList.sort((a, b) => a - b),
42
- resolutionMultiplier,
43
- };
44
- }
45
12
  function L(...args) {
46
13
  if (Formatter.DEBUG)
47
14
  log('VexFlow.Formatter', args);
@@ -138,7 +105,7 @@ export class Formatter {
138
105
  beams.forEach((beam) => beam.setContext(ctx).drawWithStyle());
139
106
  new StaveConnector(stave, tabstave).setContext(ctx).drawWithStyle();
140
107
  }
141
- static AlignRestsToNotes(tickables, alignAllNotes, alignTuplets) {
108
+ static AlignRestsToNotes(tickables, alignAllNotes, alignTuplets = false) {
142
109
  tickables.forEach((currTickable, index) => {
143
110
  if (isStaveNote(currTickable) && currTickable.isRest()) {
144
111
  if (currTickable.getTuplet() && !alignTuplets) {
@@ -292,8 +259,39 @@ export class Formatter {
292
259
  });
293
260
  }
294
261
  createTickContexts(voices) {
295
- const fn = (tickable, context, voiceIndex) => context.addTickable(tickable, voiceIndex);
296
- const contexts = createContexts(voices, (tick) => new TickContext(tick), fn);
262
+ if (voices.length === 0) {
263
+ return {
264
+ map: {},
265
+ array: [],
266
+ list: [],
267
+ resolutionMultiplier: 0,
268
+ };
269
+ }
270
+ const tickToContextMap = {};
271
+ const tickList = [];
272
+ const tickContexts = [];
273
+ const resolutionMultiplier = Formatter.getResolutionMultiplier(voices);
274
+ voices.forEach((voice, voiceIndex) => {
275
+ const ticksUsed = new Fraction(0, resolutionMultiplier);
276
+ voice.getTickables().forEach((tickable) => {
277
+ const integerTicks = ticksUsed.numerator;
278
+ if (!tickToContextMap[integerTicks]) {
279
+ const newContext = new TickContext({ tickID: integerTicks });
280
+ tickContexts.push(newContext);
281
+ tickToContextMap[integerTicks] = newContext;
282
+ tickList.push(integerTicks);
283
+ }
284
+ const tickContext = tickToContextMap[integerTicks];
285
+ tickContext.addTickable(tickable, voiceIndex);
286
+ ticksUsed.add(tickable.getTicks());
287
+ });
288
+ });
289
+ const contexts = {
290
+ map: tickToContextMap,
291
+ array: tickContexts,
292
+ list: tickList.sort((a, b) => a - b),
293
+ resolutionMultiplier,
294
+ };
297
295
  this.tickContexts = contexts;
298
296
  const contextArray = this.tickContexts.array;
299
297
  contextArray.forEach((context) => {
@@ -4,7 +4,7 @@ export class Fraction {
4
4
  return "Fraction";
5
5
  }
6
6
  static GCD(a, b) {
7
- if (typeof a !== 'number' || Number.isNaN(a) || typeof b !== 'number' || Number.isNaN(b)) {
7
+ if (!Number.isFinite(a) || !Number.isFinite(b)) {
8
8
  throw new RuntimeError('BadArgument', `Invalid numbers: ${a}, ${b}`);
9
9
  }
10
10
  let t;
@@ -38,6 +38,9 @@ export class Fraction {
38
38
  this.set(numerator, denominator);
39
39
  }
40
40
  set(numerator = 1, denominator = 1) {
41
+ if (!Number.isFinite(numerator) || !Number.isFinite(denominator) || denominator === 0) {
42
+ throw new RuntimeError('BadArgument', `Invalid fraction: ${numerator}/${denominator}`);
43
+ }
41
44
  this.numerator = numerator;
42
45
  this.denominator = denominator;
43
46
  return this;
@@ -32,11 +32,13 @@ export class GlyphNote extends Note {
32
32
  const stave = this.checkStave();
33
33
  const ctx = stave.checkContext();
34
34
  this.setRendered();
35
- ctx.openGroup('glyphNote', this.getAttribute('id'));
35
+ const clsAttribute = this.getAttribute('class');
36
+ ctx.openGroup('glyphNote' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
36
37
  this.x = this.isCenterAligned() ? this.getAbsoluteX() - this.getWidth() / 2 : this.getAbsoluteX();
37
38
  this.y = stave.getYForLine(this.options.line);
38
39
  this.renderText(ctx, 0, 0);
39
40
  this.drawModifiers();
41
+ this.drawPointerRect();
40
42
  ctx.closeGroup();
41
43
  }
42
44
  }
@@ -112,12 +112,29 @@ export class GraceNoteGroup extends Modifier {
112
112
  getGraceNotes() {
113
113
  return this.graceNotes;
114
114
  }
115
+ positionGraceNotes() {
116
+ const note = this.checkAttachedNote();
117
+ note.checkStave();
118
+ this.preFormat();
119
+ this.alignSubNotesWithNote(this.getGraceNotes(), note, this.position);
120
+ this.beams.forEach((beam) => beam.postFormat());
121
+ }
122
+ getBoundingBox() {
123
+ if (this.graceNotes.length === 0)
124
+ return super.getBoundingBox();
125
+ this.positionGraceNotes();
126
+ const boundingBox = this.graceNotes[0].getBoundingBox();
127
+ for (let i = 1; i < this.graceNotes.length; i++) {
128
+ boundingBox.mergeWith(this.graceNotes[i].getBoundingBox());
129
+ }
130
+ return boundingBox;
131
+ }
115
132
  draw() {
116
133
  const ctx = this.checkContext();
117
134
  const note = this.checkAttachedNote();
118
135
  this.setRendered();
119
136
  L('Drawing grace note group for:', note);
120
- this.alignSubNotesWithNote(this.getGraceNotes(), note, this.position);
137
+ this.positionGraceNotes();
121
138
  this.graceNotes.forEach((graceNote) => graceNote.setContext(ctx).drawWithStyle());
122
139
  this.beams.forEach((beam) => beam.setContext(ctx).drawWithStyle());
123
140
  if (this.showSlur) {
@@ -44,6 +44,7 @@ export * from './registry.js';
44
44
  export * from './rendercontext.js';
45
45
  export * from './renderer.js';
46
46
  export * from './repeatnote.js';
47
+ export * from './sheet.js';
47
48
  export * from './stave.js';
48
49
  export * from './stavebarline.js';
49
50
  export * from './staveconnector.js';
@@ -62,6 +63,7 @@ export * from './stemmablenote.js';
62
63
  export * from './stringnumber.js';
63
64
  export * from './strokes.js';
64
65
  export * from './svgcontext.js';
66
+ export * from './svgexport.js';
65
67
  export * from './system.js';
66
68
  export * from './tabnote.js';
67
69
  export * from './tabslide.js';
@@ -208,11 +208,13 @@ export class KeySignature extends StaveModifier {
208
208
  if (!this.formatted)
209
209
  this.format();
210
210
  this.setRendered();
211
- ctx.openGroup('keysignature', this.getAttribute('id'));
211
+ const clsAttribute = this.getAttribute('class');
212
+ ctx.openGroup('keysignature' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
212
213
  for (let i = 0; i < this.children.length; i++) {
213
214
  const glyph = this.children[i];
214
215
  glyph.renderText(ctx, this.x, 0);
215
216
  }
217
+ this.drawPointerRect();
216
218
  ctx.closeGroup();
217
219
  }
218
220
  }
@@ -1,5 +1,7 @@
1
+ import { TextMetricsCache } from './textmetricscache.js';
1
2
  export class Metrics {
2
3
  static clear(key) {
4
+ TextMetricsCache.clear();
3
5
  if (key) {
4
6
  this.cacheFont.delete(key);
5
7
  this.cacheStyle.delete(key);
@@ -13,14 +15,28 @@ export class Metrics {
13
15
  let font = this.cacheFont.get(key);
14
16
  if (!font) {
15
17
  font = {
16
- family: Metrics.get(`${key}.fontFamily`),
18
+ family: Metrics.getFontFamily(key),
17
19
  size: Metrics.get(`${key}.fontSize`) * Metrics.get(`${key}.fontScale`),
18
20
  weight: Metrics.get(`${key}.fontWeight`),
19
21
  style: Metrics.get(`${key}.fontStyle`),
20
22
  };
21
23
  this.cacheFont.set(key, font);
22
24
  }
23
- return structuredClone(font);
25
+ return Object.assign({}, font);
26
+ }
27
+ static getFontFamily(key) {
28
+ var _a;
29
+ if (!Metrics.get(`${key}.isText`, false))
30
+ return Metrics.get(`${key}.fontFamily`);
31
+ let family = MetricsDefaults.textFontFamily;
32
+ let metrics = MetricsDefaults;
33
+ for (const part of key.split('.')) {
34
+ metrics = metrics === null || metrics === void 0 ? void 0 : metrics[part];
35
+ if (!metrics)
36
+ break;
37
+ family = (_a = metrics.fontFamily) !== null && _a !== void 0 ? _a : family;
38
+ }
39
+ return family;
24
40
  }
25
41
  static getStyle(key) {
26
42
  let style = this.cacheStyle.get(key);
@@ -35,7 +51,7 @@ export class Metrics {
35
51
  };
36
52
  this.cacheStyle.set(key, style);
37
53
  }
38
- return structuredClone(style);
54
+ return Object.assign({}, style);
39
55
  }
40
56
  static get(key, defaultValue) {
41
57
  var _a;
@@ -59,7 +75,9 @@ export class Metrics {
59
75
  Metrics.cacheStyle = new Map();
60
76
  Metrics.cacheFont = new Map();
61
77
  export const MetricsDefaults = {
78
+ pointerRect: false,
62
79
  fontFamily: 'Bravura,Academico',
80
+ textFontFamily: 'Academico,Bravura',
63
81
  fontSize: 30,
64
82
  fontScale: 1.0,
65
83
  fontWeight: 'normal',
@@ -76,9 +94,11 @@ export const MetricsDefaults = {
76
94
  accidentalSpacing: 3,
77
95
  },
78
96
  Annotation: {
97
+ isText: true,
79
98
  fontSize: 10,
80
99
  },
81
100
  Bend: {
101
+ isText: true,
82
102
  fontSize: 10,
83
103
  line: {
84
104
  strokeStyle: '#777777',
@@ -86,6 +106,7 @@ export const MetricsDefaults = {
86
106
  },
87
107
  },
88
108
  ChordSymbol: {
109
+ isText: true,
89
110
  fontSize: 12,
90
111
  spacing: 0.05,
91
112
  subscriptOffset: 0.2,
@@ -93,6 +114,7 @@ export const MetricsDefaults = {
93
114
  superSubRatio: 0.6,
94
115
  },
95
116
  FretHandFinger: {
117
+ isText: true,
96
118
  fontSize: 9,
97
119
  fontWeight: 'bold',
98
120
  },
@@ -107,12 +129,14 @@ export const MetricsDefaults = {
107
129
  },
108
130
  PedalMarking: {
109
131
  text: {
132
+ isText: true,
110
133
  fontSize: 12,
111
134
  fontStyle: 'italic',
112
135
  },
113
136
  },
114
137
  Repetition: {
115
138
  text: {
139
+ isText: true,
116
140
  fontSize: 12,
117
141
  fontWeight: 'bold',
118
142
  offsetX: 12,
@@ -127,6 +151,7 @@ export const MetricsDefaults = {
127
151
  },
128
152
  },
129
153
  Stave: {
154
+ isText: true,
130
155
  strokeStyle: '#999999',
131
156
  fontSize: 8,
132
157
  padding: 12,
@@ -136,13 +161,19 @@ export const MetricsDefaults = {
136
161
  },
137
162
  StaveConnector: {
138
163
  text: {
164
+ isText: true,
139
165
  fontSize: 16,
140
166
  },
141
167
  },
142
168
  StaveLine: {
169
+ isText: true,
143
170
  fontSize: 10,
144
171
  },
172
+ StaveNote: {
173
+ pointerRect: true,
174
+ },
145
175
  StaveSection: {
176
+ isText: true,
146
177
  fontSize: 10,
147
178
  fontWeight: 'bold',
148
179
  lineWidth: 2,
@@ -150,8 +181,10 @@ export const MetricsDefaults = {
150
181
  strokeStyle: 'black',
151
182
  },
152
183
  StaveTempo: {
184
+ isText: true,
153
185
  fontSize: 14,
154
186
  glyph: {
187
+ isText: false,
155
188
  fontSize: 25,
156
189
  },
157
190
  name: {
@@ -159,15 +192,18 @@ export const MetricsDefaults = {
159
192
  },
160
193
  },
161
194
  StaveText: {
195
+ isText: true,
162
196
  fontSize: 16,
163
197
  },
164
198
  StaveTie: {
199
+ isText: true,
165
200
  fontSize: 10,
166
201
  },
167
202
  Stem: {
168
203
  strokeStyle: 'black',
169
204
  },
170
205
  StringNumber: {
206
+ isText: true,
171
207
  fontSize: 10,
172
208
  fontWeight: 'bold',
173
209
  verticalPadding: 8,
@@ -177,6 +213,7 @@ export const MetricsDefaults = {
177
213
  },
178
214
  Stroke: {
179
215
  text: {
216
+ isText: true,
180
217
  fontSize: 10,
181
218
  fontStyle: 'italic',
182
219
  fontWeight: 'bold',
@@ -184,27 +221,33 @@ export const MetricsDefaults = {
184
221
  },
185
222
  TabNote: {
186
223
  text: {
224
+ isText: true,
187
225
  fontSize: 9,
188
226
  },
189
227
  },
190
228
  TabSlide: {
229
+ isText: true,
191
230
  fontSize: 10,
192
231
  fontStyle: 'italic',
193
232
  fontWeight: 'bold',
194
233
  },
195
234
  TabStave: {
235
+ isText: true,
196
236
  strokeStyle: '#999999',
197
237
  fontSize: 8,
198
238
  },
199
239
  TabTie: {
240
+ isText: true,
200
241
  fontSize: 10,
201
242
  },
202
243
  TextBracket: {
244
+ isText: true,
203
245
  fontSize: 15,
204
246
  fontStyle: 'italic',
205
247
  },
206
248
  TextNote: {
207
249
  text: {
250
+ isText: true,
208
251
  fontSize: 12,
209
252
  },
210
253
  },
@@ -212,10 +255,22 @@ export const MetricsDefaults = {
212
255
  spacing: 7,
213
256
  },
214
257
  Tuplet: {
258
+ pointerRect: true,
215
259
  yOffset: 0,
216
260
  textYOffset: 2,
261
+ bracket: {
262
+ padding: 5,
263
+ lineWidth: 1,
264
+ legLength: 10,
265
+ },
266
+ suffix: {
267
+ fontScale: 1 / 2,
268
+ extraSpacing: 1,
269
+ suffixOffsetY: -3,
270
+ },
217
271
  },
218
272
  Volta: {
273
+ isText: true,
219
274
  fontSize: 9,
220
275
  fontWeight: 'bold',
221
276
  },
@@ -32,6 +32,7 @@ export class ModifierContext {
32
32
  this.formatted = false;
33
33
  this.width = 0;
34
34
  this.spacing = 0;
35
+ this.annotationRightShift = 0;
35
36
  }
36
37
  addModifier(member) {
37
38
  L('addModifier is deprecated, use addMember instead.');
@@ -64,6 +65,9 @@ export class ModifierContext {
64
65
  getRightShift() {
65
66
  return this.state.rightShift;
66
67
  }
68
+ getRightShiftForTie() {
69
+ return this.state.rightShift - this.annotationRightShift;
70
+ }
67
71
  getState() {
68
72
  return this.state;
69
73
  }
@@ -77,6 +81,7 @@ export class ModifierContext {
77
81
  };
78
82
  }
79
83
  preFormat() {
84
+ var _a;
80
85
  if (this.preFormatted)
81
86
  return;
82
87
  L('Preformatting ModifierContext');
@@ -93,10 +98,19 @@ export class ModifierContext {
93
98
  StringNumber.format(members["StringNumber"], state);
94
99
  Articulation.format(members["Articulation"], state);
95
100
  Ornament.format(members["Ornament"], state);
96
- Annotation.format(members["Annotation"], state);
101
+ const annotations = (_a = members["Annotation"]) !== null && _a !== void 0 ? _a : [];
102
+ const isOutsideNote = (annotation) => annotation.getVerticalJustification() === Annotation.VerticalJustify.TOP ||
103
+ annotation.getVerticalJustification() === Annotation.VerticalJustify.BOTTOM;
104
+ Annotation.format(annotations.filter((annotation) => !isOutsideNote(annotation)), state);
105
+ const rightShiftBeforeAnnotations = state.rightShift;
106
+ Annotation.format(annotations.filter(isOutsideNote), state);
107
+ const rightShiftWithAnnotations = state.rightShift;
108
+ state.rightShift = rightShiftBeforeAnnotations;
97
109
  ChordSymbol.format(members["ChordSymbol"], state);
98
110
  Bend.format(members["Bend"], state);
99
111
  Vibrato.format(members["Vibrato"], state, this);
112
+ this.annotationRightShift = Math.max(0, rightShiftWithAnnotations - state.rightShift);
113
+ state.rightShift += this.annotationRightShift;
100
114
  this.width = state.leftShift + state.rightShift;
101
115
  this.preFormatted = true;
102
116
  }
@@ -62,10 +62,10 @@ export class Note extends Tickable {
62
62
  ctx.restore();
63
63
  }
64
64
  static parseDuration(durationString) {
65
- if (!durationString) {
65
+ if (!durationString || durationString.length > 64) {
66
66
  return undefined;
67
67
  }
68
- const regexp = /(\d*\/?\d+|[a-z])(d*)([nrhms]|$)/;
68
+ const regexp = /^(\d+(?:\/\d+)?|[a-z])(d*)([nrhms]?)$/;
69
69
  const result = regexp.exec(durationString);
70
70
  if (!result) {
71
71
  return undefined;
@@ -76,11 +76,13 @@ export class NoteHead extends Note {
76
76
  var _a;
77
77
  const ctx = this.checkContext();
78
78
  this.setRendered();
79
- ctx.openGroup('notehead', this.getAttribute('id'));
79
+ const clsAttribute = this.getAttribute('class');
80
+ ctx.openGroup('notehead' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
80
81
  L("Drawing note head '", this.noteType, this.duration, "' at", this.x, this.y);
81
82
  this.x = this.getAbsoluteX();
82
83
  this.renderText(ctx, 0, 0);
83
84
  (_a = this.parent) === null || _a === void 0 ? void 0 : _a.drawModifiers(this);
85
+ this.drawPointerRect();
84
86
  ctx.closeGroup();
85
87
  }
86
88
  }
@@ -135,7 +135,8 @@ export class Ornament extends Modifier {
135
135
  const note = this.checkAttachedNote();
136
136
  this.setRendered();
137
137
  const stave = note.checkStave();
138
- ctx.openGroup('ornament', this.getAttribute('id'));
138
+ const clsAttribute = this.getAttribute('class');
139
+ ctx.openGroup('ornament' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
139
140
  const start = note.getModifierStartXY(this.position, this.index);
140
141
  let glyphX = start.x;
141
142
  const staffSpace = stave.getSpacingBetweenLines();
@@ -184,6 +185,7 @@ export class Ornament extends Modifier {
184
185
  glyphY -= this.getHeight() + this.renderOptions.accidentalUpperPadding;
185
186
  this.accidentalUpper.renderText(ctx, glyphX + this.xShift - this.accidentalUpper.getWidth() * 0.5, glyphY + this.yShift - this.accidentalUpper.getTextMetrics().actualBoundingBoxDescent);
186
187
  }
188
+ this.drawPointerRect();
187
189
  ctx.closeGroup();
188
190
  }
189
191
  }
@@ -1,6 +1,8 @@
1
+ import { Annotation } from './annotation.js';
1
2
  import { Element } from './element.js';
2
3
  import { Glyphs } from './glyphs.js';
3
4
  import { Metrics } from './metrics.js';
5
+ import { isAnnotation, isStaveNote } from './typeguard.js';
4
6
  import { log, RuntimeError } from './util.js';
5
7
  function L(...args) {
6
8
  if (PedalMarking.DEBUG)
@@ -37,7 +39,6 @@ export class PedalMarking extends Element {
37
39
  bracketHeight: 10,
38
40
  textMarginRight: 6,
39
41
  bracketLineWidth: 1,
40
- color: 'black',
41
42
  };
42
43
  }
43
44
  setType(type) {
@@ -57,17 +58,49 @@ export class PedalMarking extends Element {
57
58
  this.line = line;
58
59
  return this;
59
60
  }
61
+ getYForStave(stave) {
62
+ let y = stave.getYForBottomText(this.line + 3);
63
+ const events = this.notes.filter((note) => note.getStave() === stave);
64
+ if (!events.length)
65
+ return y;
66
+ const startX = Math.min(...events.map((note) => note.getAbsoluteX()));
67
+ const endX = Math.max(...events.map((note) => note.getAbsoluteX()));
68
+ const voices = new Set(events.map((note) => note.getVoice()));
69
+ const text = new Element('PedalMarking').setFont(this.fontInfo);
70
+ const textHeight = Math.max(text.setText(this.depressText).getTextMetrics().actualBoundingBoxAscent, text.setText(this.releaseText).getTextMetrics().actualBoundingBoxAscent);
71
+ const height = this.type === PedalMarking.type.BRACKET
72
+ ? this.renderOptions.bracketHeight
73
+ : Math.max(textHeight, this.type === PedalMarking.type.MIXED ? this.renderOptions.bracketHeight : 0);
74
+ for (const voice of voices) {
75
+ for (const note of voice.getTickables()) {
76
+ if (!isStaveNote(note) || note.getStave() !== stave)
77
+ continue;
78
+ if (note.getAbsoluteX() < startX || note.getAbsoluteX() > endX)
79
+ continue;
80
+ for (const modifier of note.getModifiers()) {
81
+ if (!isAnnotation(modifier) || modifier.getVerticalJustification() !== Annotation.VerticalJustify.BOTTOM)
82
+ continue;
83
+ const bottom = modifier.computeY().y + modifier.getYShift() + modifier.getTextMetrics().actualBoundingBoxDescent;
84
+ y = Math.max(y, bottom + 4 + height);
85
+ }
86
+ }
87
+ }
88
+ return y;
89
+ }
60
90
  drawBracketed() {
61
91
  const ctx = this.checkContext();
62
92
  let isPedalDepressed = false;
63
93
  let prevX;
64
94
  let prevY;
65
95
  let textWidth = 0;
96
+ const baselines = new Map(this.notes.map((note) => [note.checkStave(), 0]));
97
+ for (const stave of baselines.keys())
98
+ baselines.set(stave, this.getYForStave(stave));
66
99
  this.notes.forEach((note, index, notes) => {
67
100
  var _a, _b, _c, _d;
68
101
  isPedalDepressed = !isPedalDepressed;
69
102
  const x = note.getAbsoluteX();
70
- const y = note.checkStave().getYForBottomText(this.line + 3);
103
+ const y = baselines.get(note.checkStave());
71
104
  if (x < prevX) {
72
105
  throw new RuntimeError('InvalidConfiguration', 'The notes provided must be in order of ascending x positions');
73
106
  }
@@ -112,12 +145,15 @@ export class PedalMarking extends Element {
112
145
  const ctx = this.checkContext();
113
146
  let isPedalDepressed = false;
114
147
  let textWidth = 0;
148
+ const baselines = new Map(this.notes.map((note) => [note.checkStave(), 0]));
149
+ for (const stave of baselines.keys())
150
+ baselines.set(stave, this.getYForStave(stave));
115
151
  this.notes.forEach((note) => {
116
152
  var _a, _b, _c, _d;
117
153
  isPedalDepressed = !isPedalDepressed;
118
154
  const stave = note.checkStave();
119
155
  const x = note.getAbsoluteX();
120
- const y = stave.getYForBottomText(this.line + 3);
156
+ const y = baselines.get(stave);
121
157
  if (isPedalDepressed) {
122
158
  textWidth = ctx.measureText(this.depressText).width;
123
159
  ctx.fillText(this.depressText, x, y);
@@ -138,8 +174,11 @@ export class PedalMarking extends Element {
138
174
  draw() {
139
175
  const ctx = this.checkContext();
140
176
  this.setRendered();
141
- ctx.setStrokeStyle(this.renderOptions.color);
142
- ctx.setFillStyle(this.renderOptions.color);
177
+ if (this.renderOptions.color !== undefined) {
178
+ ctx.setStrokeStyle(this.renderOptions.color);
179
+ ctx.setFillStyle(this.renderOptions.color);
180
+ }
181
+ this.applyStyle(ctx);
143
182
  ctx.setFont(this.font);
144
183
  L('Rendering Pedal Marking');
145
184
  if (this.type === PedalMarking.type.BRACKET || this.type === PedalMarking.type.MIXED) {
@@ -1,9 +1,12 @@
1
1
  import { RuntimeError } from './util.js';
2
2
  class Index {
3
3
  constructor() {
4
- this.id = {};
5
- this.type = {};
6
- this.class = {};
4
+ this.id = Object.create(null);
5
+ this.type = Object.create(null);
6
+ this.class = Object.create(null);
7
+ }
8
+ static isValidName(name) {
9
+ return name === 'id' || name === 'type' || name === 'class';
7
10
  }
8
11
  }
9
12
  export class Registry {
@@ -24,13 +27,19 @@ export class Registry {
24
27
  return this;
25
28
  }
26
29
  setIndexValue(name, value, id, elem) {
30
+ if (!Index.isValidName(name)) {
31
+ throw new RuntimeError('BadArgument', `Unknown registry index: ${name}`);
32
+ }
27
33
  const index = this.index;
28
34
  if (!index[name][value]) {
29
- index[name][value] = {};
35
+ index[name][value] = Object.create(null);
30
36
  }
31
37
  index[name][value][id] = elem;
32
38
  }
33
39
  updateIndex({ id, name, value, oldValue }) {
40
+ if (!Index.isValidName(name)) {
41
+ throw new RuntimeError('BadArgument', `Unknown registry index: ${name}`);
42
+ }
34
43
  const elem = this.getElementById(id);
35
44
  if (oldValue !== undefined && this.index[name][oldValue]) {
36
45
  delete this.index[name][oldValue][id];
@@ -55,6 +64,8 @@ export class Registry {
55
64
  return (_b = (_a = this.index.id) === null || _a === void 0 ? void 0 : _a[id]) === null || _b === void 0 ? void 0 : _b[id];
56
65
  }
57
66
  getElementsByAttribute(attribute, value) {
67
+ if (!Index.isValidName(attribute))
68
+ return [];
58
69
  const indexAttr = this.index[attribute];
59
70
  if (indexAttr) {
60
71
  const indexAttrVal = indexAttr[value];
@@ -72,8 +83,7 @@ export class Registry {
72
83
  return this.getElementsByAttribute('class', className);
73
84
  }
74
85
  onUpdate(info) {
75
- const allowedNames = ['id', 'type', 'class'];
76
- if (allowedNames.includes(info.name)) {
86
+ if (Index.isValidName(info.name)) {
77
87
  this.updateIndex(info);
78
88
  }
79
89
  return this;
@@ -1,4 +1,14 @@
1
1
  export class RenderContext {
2
+ constructor() {
3
+ this.defaultStyle = {};
4
+ }
5
+ setDefaultStyle(style) {
6
+ this.defaultStyle = Object.assign({}, style);
7
+ return this;
8
+ }
9
+ getDefaultStyle() {
10
+ return Object.assign({}, this.defaultStyle);
11
+ }
2
12
  static get CATEGORY() {
3
13
  return "RenderContext";
4
14
  }