vexflow 5.0.0 → 5.1.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 (127) hide show
  1. package/LICENSE +3 -3
  2. package/README.md +32 -50
  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 +3685 -1386
  8. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  9. package/build/cjs/vexflow-debug.js +2443 -980
  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 +42 -9
  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 +17 -0
  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 +55 -1
  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/textnote.js +3 -3
  56. package/build/esm/src/tickable.js +56 -22
  57. package/build/esm/src/timesignature.js +30 -9
  58. package/build/esm/src/timesignote.js +3 -1
  59. package/build/esm/src/tuning.js +2 -2
  60. package/build/esm/src/tuplet.js +79 -29
  61. package/build/esm/src/util.js +1 -0
  62. package/build/esm/src/version.js +3 -3
  63. package/build/esm/src/vexflow.js +14 -2
  64. package/build/esm/src/voice.js +44 -8
  65. package/build/esm/tests/accidental_tests.js +52 -0
  66. package/build/esm/tests/annotation_tests.js +38 -0
  67. package/build/esm/tests/articulation_tests.js +34 -0
  68. package/build/esm/tests/beam_tests.js +17 -0
  69. package/build/esm/tests/curve_tests.js +81 -0
  70. package/build/esm/tests/font_tests.js +32 -1
  71. package/build/esm/tests/formatter_tests.js +58 -23
  72. package/build/esm/tests/gracenote_tests.js +58 -0
  73. package/build/esm/tests/gracetabnote_tests.js +40 -0
  74. package/build/esm/tests/keysignature_tests.js +14 -3
  75. package/build/esm/tests/node/package-input-tests.mjs +149 -0
  76. package/build/esm/tests/node/render-metrics-tests.mjs +78 -0
  77. package/build/esm/tests/node/rendering-tests.mjs +75 -0
  78. package/build/esm/tests/node/sheet-tests.mjs +202 -0
  79. package/build/esm/tests/node/svg-export-tests.mjs +182 -0
  80. package/build/esm/tests/pedalmarking_tests.js +25 -0
  81. package/build/esm/tests/rests_tests.js +6 -0
  82. package/build/esm/tests/stave_tests.js +6 -3
  83. package/build/esm/tests/stavenote_tests.js +100 -55
  84. package/build/esm/tests/strokes_tests.js +19 -5
  85. package/build/esm/tests/timesignature_tests.js +19 -0
  86. package/build/esm/tests/tuplet_tests.js +273 -12
  87. package/build/esm/tests/unison_tests.js +13 -13
  88. package/build/esm/tests/voice_tests.js +34 -0
  89. package/build/esm/tools/render-metrics.mjs +49 -0
  90. package/build/esm/tools/score-fixtures.mjs +94 -0
  91. package/build/esm/tools/sheet-fixture.mjs +90 -0
  92. package/build/esm/tools/visual-regression.mjs +106 -0
  93. package/build/types/src/annotation.d.ts +17 -1
  94. package/build/types/src/curve.d.ts +24 -7
  95. package/build/types/src/easyscore.d.ts +8 -0
  96. package/build/types/src/element.d.ts +5 -0
  97. package/build/types/src/factory.d.ts +18 -1
  98. package/build/types/src/font.d.ts +2 -0
  99. package/build/types/src/formatter.d.ts +23 -14
  100. package/build/types/src/gracenotegroup.d.ts +5 -0
  101. package/build/types/src/index.d.ts +2 -0
  102. package/build/types/src/metrics.d.ts +2 -0
  103. package/build/types/src/modifiercontext.d.ts +4 -0
  104. package/build/types/src/pedalmarking.d.ts +4 -1
  105. package/build/types/src/registry.d.ts +2 -0
  106. package/build/types/src/rendercontext.d.ts +12 -0
  107. package/build/types/src/sheet.d.ts +49 -0
  108. package/build/types/src/stave.d.ts +4 -4
  109. package/build/types/src/stavebarline.d.ts +10 -0
  110. package/build/types/src/stavenote.d.ts +11 -0
  111. package/build/types/src/strokes.d.ts +4 -0
  112. package/build/types/src/svgcontext.d.ts +5 -0
  113. package/build/types/src/svgexport.d.ts +22 -0
  114. package/build/types/src/system.d.ts +4 -0
  115. package/build/types/src/tables.d.ts +9 -0
  116. package/build/types/src/tabnote.d.ts +4 -0
  117. package/build/types/src/textbracket.d.ts +1 -1
  118. package/build/types/src/textdynamics.d.ts +3 -0
  119. package/build/types/src/tickable.d.ts +27 -10
  120. package/build/types/src/timesignature.d.ts +4 -0
  121. package/build/types/src/tuplet.d.ts +8 -0
  122. package/build/types/src/util.d.ts +1 -0
  123. package/build/types/src/vexflow.d.ts +8 -1
  124. package/build/types/src/voice.d.ts +6 -0
  125. package/changelog/CHANGELOG.md +94 -0
  126. package/package.json +42 -26
  127. package/tests/flow.html +15 -7
@@ -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,3 +1,4 @@
1
+ import { structuredClone } from './util.js';
1
2
  export class Metrics {
2
3
  static clear(key) {
3
4
  if (key) {
@@ -13,7 +14,7 @@ export class Metrics {
13
14
  let font = this.cacheFont.get(key);
14
15
  if (!font) {
15
16
  font = {
16
- family: Metrics.get(`${key}.fontFamily`),
17
+ family: Metrics.getFontFamily(key),
17
18
  size: Metrics.get(`${key}.fontSize`) * Metrics.get(`${key}.fontScale`),
18
19
  weight: Metrics.get(`${key}.fontWeight`),
19
20
  style: Metrics.get(`${key}.fontStyle`),
@@ -22,6 +23,20 @@ export class Metrics {
22
23
  }
23
24
  return structuredClone(font);
24
25
  }
26
+ static getFontFamily(key) {
27
+ var _a;
28
+ if (!Metrics.get(`${key}.isText`, false))
29
+ return Metrics.get(`${key}.fontFamily`);
30
+ let family = MetricsDefaults.textFontFamily;
31
+ let metrics = MetricsDefaults;
32
+ for (const part of key.split('.')) {
33
+ metrics = metrics === null || metrics === void 0 ? void 0 : metrics[part];
34
+ if (!metrics)
35
+ break;
36
+ family = (_a = metrics.fontFamily) !== null && _a !== void 0 ? _a : family;
37
+ }
38
+ return family;
39
+ }
25
40
  static getStyle(key) {
26
41
  let style = this.cacheStyle.get(key);
27
42
  if (!style) {
@@ -59,7 +74,9 @@ export class Metrics {
59
74
  Metrics.cacheStyle = new Map();
60
75
  Metrics.cacheFont = new Map();
61
76
  export const MetricsDefaults = {
77
+ pointerRect: false,
62
78
  fontFamily: 'Bravura,Academico',
79
+ textFontFamily: 'Academico,Bravura',
63
80
  fontSize: 30,
64
81
  fontScale: 1.0,
65
82
  fontWeight: 'normal',
@@ -76,9 +93,11 @@ export const MetricsDefaults = {
76
93
  accidentalSpacing: 3,
77
94
  },
78
95
  Annotation: {
96
+ isText: true,
79
97
  fontSize: 10,
80
98
  },
81
99
  Bend: {
100
+ isText: true,
82
101
  fontSize: 10,
83
102
  line: {
84
103
  strokeStyle: '#777777',
@@ -86,6 +105,7 @@ export const MetricsDefaults = {
86
105
  },
87
106
  },
88
107
  ChordSymbol: {
108
+ isText: true,
89
109
  fontSize: 12,
90
110
  spacing: 0.05,
91
111
  subscriptOffset: 0.2,
@@ -93,6 +113,7 @@ export const MetricsDefaults = {
93
113
  superSubRatio: 0.6,
94
114
  },
95
115
  FretHandFinger: {
116
+ isText: true,
96
117
  fontSize: 9,
97
118
  fontWeight: 'bold',
98
119
  },
@@ -107,12 +128,14 @@ export const MetricsDefaults = {
107
128
  },
108
129
  PedalMarking: {
109
130
  text: {
131
+ isText: true,
110
132
  fontSize: 12,
111
133
  fontStyle: 'italic',
112
134
  },
113
135
  },
114
136
  Repetition: {
115
137
  text: {
138
+ isText: true,
116
139
  fontSize: 12,
117
140
  fontWeight: 'bold',
118
141
  offsetX: 12,
@@ -127,6 +150,7 @@ export const MetricsDefaults = {
127
150
  },
128
151
  },
129
152
  Stave: {
153
+ isText: true,
130
154
  strokeStyle: '#999999',
131
155
  fontSize: 8,
132
156
  padding: 12,
@@ -136,13 +160,19 @@ export const MetricsDefaults = {
136
160
  },
137
161
  StaveConnector: {
138
162
  text: {
163
+ isText: true,
139
164
  fontSize: 16,
140
165
  },
141
166
  },
142
167
  StaveLine: {
168
+ isText: true,
143
169
  fontSize: 10,
144
170
  },
171
+ StaveNote: {
172
+ pointerRect: true,
173
+ },
145
174
  StaveSection: {
175
+ isText: true,
146
176
  fontSize: 10,
147
177
  fontWeight: 'bold',
148
178
  lineWidth: 2,
@@ -150,8 +180,10 @@ export const MetricsDefaults = {
150
180
  strokeStyle: 'black',
151
181
  },
152
182
  StaveTempo: {
183
+ isText: true,
153
184
  fontSize: 14,
154
185
  glyph: {
186
+ isText: false,
155
187
  fontSize: 25,
156
188
  },
157
189
  name: {
@@ -159,15 +191,18 @@ export const MetricsDefaults = {
159
191
  },
160
192
  },
161
193
  StaveText: {
194
+ isText: true,
162
195
  fontSize: 16,
163
196
  },
164
197
  StaveTie: {
198
+ isText: true,
165
199
  fontSize: 10,
166
200
  },
167
201
  Stem: {
168
202
  strokeStyle: 'black',
169
203
  },
170
204
  StringNumber: {
205
+ isText: true,
171
206
  fontSize: 10,
172
207
  fontWeight: 'bold',
173
208
  verticalPadding: 8,
@@ -177,6 +212,7 @@ export const MetricsDefaults = {
177
212
  },
178
213
  Stroke: {
179
214
  text: {
215
+ isText: true,
180
216
  fontSize: 10,
181
217
  fontStyle: 'italic',
182
218
  fontWeight: 'bold',
@@ -184,27 +220,33 @@ export const MetricsDefaults = {
184
220
  },
185
221
  TabNote: {
186
222
  text: {
223
+ isText: true,
187
224
  fontSize: 9,
188
225
  },
189
226
  },
190
227
  TabSlide: {
228
+ isText: true,
191
229
  fontSize: 10,
192
230
  fontStyle: 'italic',
193
231
  fontWeight: 'bold',
194
232
  },
195
233
  TabStave: {
234
+ isText: true,
196
235
  strokeStyle: '#999999',
197
236
  fontSize: 8,
198
237
  },
199
238
  TabTie: {
239
+ isText: true,
200
240
  fontSize: 10,
201
241
  },
202
242
  TextBracket: {
243
+ isText: true,
203
244
  fontSize: 15,
204
245
  fontStyle: 'italic',
205
246
  },
206
247
  TextNote: {
207
248
  text: {
249
+ isText: true,
208
250
  fontSize: 12,
209
251
  },
210
252
  },
@@ -212,10 +254,22 @@ export const MetricsDefaults = {
212
254
  spacing: 7,
213
255
  },
214
256
  Tuplet: {
257
+ pointerRect: true,
215
258
  yOffset: 0,
216
259
  textYOffset: 2,
260
+ bracket: {
261
+ padding: 5,
262
+ lineWidth: 1,
263
+ legLength: 10,
264
+ },
265
+ suffix: {
266
+ fontScale: 1 / 2,
267
+ extraSpacing: 1,
268
+ suffixOffsetY: -3,
269
+ },
217
270
  },
218
271
  Volta: {
272
+ isText: true,
219
273
  fontSize: 9,
220
274
  fontWeight: 'bold',
221
275
  },
@@ -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
  }
@@ -0,0 +1,117 @@
1
+ import { RuntimeError } from './util.js';
2
+ export class Sheet {
3
+ constructor(options) {
4
+ this.systems = [];
5
+ this.rows = [];
6
+ this.options = Object.assign({ width: 500, x: 10, y: 10, rowGap: 40, measureGap: 0, measurePadding: 20, justify: true }, options);
7
+ this.setWidth(this.options.width);
8
+ for (const key of ['x', 'y', 'rowGap', 'measureGap', 'measurePadding']) {
9
+ if (!Number.isFinite(this.options[key]) || this.options[key] < 0) {
10
+ throw new RuntimeError('BadArgument', `Sheet ${key} must be a nonnegative finite number.`);
11
+ }
12
+ }
13
+ }
14
+ addSystem(options = {}) {
15
+ const system = this.options.factory.System(Object.assign(Object.assign({}, options), { autoWidth: true, noJustification: false }));
16
+ this.systems.push(system);
17
+ return system;
18
+ }
19
+ setWidth(width) {
20
+ if (!Number.isFinite(width) || width <= 0) {
21
+ throw new RuntimeError('BadArgument', 'Sheet width must be a positive finite number.');
22
+ }
23
+ this.options.width = width;
24
+ return this;
25
+ }
26
+ getSystems() {
27
+ return this.systems.slice();
28
+ }
29
+ getRows() {
30
+ return this.rows.map((row) => (Object.assign(Object.assign({}, row), { systems: row.systems.slice() })));
31
+ }
32
+ getHeight() {
33
+ const last = this.rows[this.rows.length - 1];
34
+ return last ? last.y + last.height : this.options.y;
35
+ }
36
+ format() {
37
+ const widths = this.measureSystems();
38
+ this.rows = [];
39
+ let y = this.options.y;
40
+ for (const indices of this.packRows(widths)) {
41
+ const row = this.formatRow(indices, widths, y);
42
+ this.rows.push(row);
43
+ y += row.height + this.options.rowGap;
44
+ }
45
+ return this;
46
+ }
47
+ measureSystems() {
48
+ return this.systems.map((system) => {
49
+ system.setX(0).setY(0).setWidth();
50
+ system.format();
51
+ return system.getWidth() + this.options.measurePadding;
52
+ });
53
+ }
54
+ packRows(widths) {
55
+ const groups = [];
56
+ let group = [];
57
+ let used = 0;
58
+ widths.forEach((width, index) => {
59
+ const gap = group.length ? this.options.measureGap : 0;
60
+ if (group.length && used + gap + width > this.options.width) {
61
+ groups.push(group);
62
+ group = [];
63
+ used = 0;
64
+ }
65
+ used += (group.length ? this.options.measureGap : 0) + width;
66
+ group.push(index);
67
+ });
68
+ if (group.length)
69
+ groups.push(group);
70
+ return groups;
71
+ }
72
+ verticalBounds(system) {
73
+ let top = 0;
74
+ let bottom = system.getBoundingBox().getH();
75
+ for (const voice of system.getVoices()) {
76
+ for (const note of voice.getTickables()) {
77
+ const box = note.getBoundingBox();
78
+ top = Math.min(top, box.getY());
79
+ bottom = Math.max(bottom, box.getY() + box.getH());
80
+ }
81
+ }
82
+ return { top, bottom };
83
+ }
84
+ formatRow(indices, widths, y) {
85
+ const minimum = indices.reduce((sum, index) => sum + widths[index], 0) + this.options.measureGap * (indices.length - 1);
86
+ const extra = this.options.justify ? Math.max(0, this.options.width - minimum) / indices.length : 0;
87
+ let x = this.options.x;
88
+ let top = 0;
89
+ let bottom = 0;
90
+ const systems = indices.map((index) => this.systems[index]);
91
+ indices.forEach((index) => {
92
+ const system = this.systems[index];
93
+ system
94
+ .setX(x)
95
+ .setY(0)
96
+ .setWidth(widths[index] + extra);
97
+ system.format();
98
+ const bounds = this.verticalBounds(system);
99
+ top = Math.min(top, bounds.top);
100
+ bottom = Math.max(bottom, bounds.bottom);
101
+ x += system.getWidth() + this.options.measureGap;
102
+ });
103
+ for (const system of systems) {
104
+ system.setY(y - top);
105
+ system.format();
106
+ }
107
+ const width = x - this.options.x - this.options.measureGap;
108
+ return {
109
+ systems,
110
+ x: this.options.x,
111
+ y,
112
+ width,
113
+ height: bottom - top,
114
+ overflow: Math.max(0, width - this.options.width),
115
+ };
116
+ }
117
+ }