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
@@ -237,6 +237,7 @@ export class Accidental extends Modifier {
237
237
  tickPosition.add(t.getTicks());
238
238
  });
239
239
  });
240
+ tickPositions.sort((a, b) => a - b);
240
241
  const music = new Music();
241
242
  if (!keySignature)
242
243
  keySignature = 'C';
@@ -4,7 +4,7 @@ import { Modifier, ModifierPosition } from './modifier.js';
4
4
  import { Stem } from './stem.js';
5
5
  import { Tables } from './tables.js';
6
6
  import { isStemmableNote, isTabNote } from './typeguard.js';
7
- import { log } from './util.js';
7
+ import { log, RuntimeError } from './util.js';
8
8
  function L(...args) {
9
9
  if (Annotation.DEBUG)
10
10
  log('VexFlow.Annotation', args);
@@ -140,6 +140,9 @@ export class Annotation extends Modifier {
140
140
  this.verticalJustification = typeof just === 'string' ? Annotation.VerticalJustifyString[just] : just;
141
141
  return this;
142
142
  }
143
+ getVerticalJustification() {
144
+ return this.verticalJustification;
145
+ }
143
146
  getJustification() {
144
147
  return this.horizontalJustification;
145
148
  }
@@ -147,17 +150,18 @@ export class Annotation extends Modifier {
147
150
  this.horizontalJustification = typeof just === 'string' ? Annotation.HorizontalJustifyString[just] : just;
148
151
  return this;
149
152
  }
150
- draw() {
151
- const ctx = this.checkContext();
153
+ setTextBaseline(y) {
154
+ if (y !== undefined && !Number.isFinite(y)) {
155
+ throw new RuntimeError('BadArgument', 'Text baseline must be finite.');
156
+ }
157
+ this.textBaseline = y;
158
+ return this;
159
+ }
160
+ computeX() {
152
161
  const note = this.checkAttachedNote();
153
- const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
154
162
  const start = note.getModifierStartXY(ModifierPosition.ABOVE, this.index);
155
- this.setRendered();
156
- ctx.openGroup('annotation', this.getAttribute('id'));
157
163
  const textWidth = this.getWidth();
158
- const textHeight = Font.convertSizeToPixelValue(this.fontInfo.size);
159
164
  let x;
160
- let y;
161
165
  if (this.horizontalJustification === AnnotationHorizontalJustify.LEFT) {
162
166
  x = start.x;
163
167
  }
@@ -170,6 +174,15 @@ export class Annotation extends Modifier {
170
174
  else {
171
175
  x = note.getStemX() - textWidth / 2;
172
176
  }
177
+ return { x, textWidth };
178
+ }
179
+ computeY() {
180
+ const textHeight = Font.convertSizeToPixelValue(this.fontInfo.size);
181
+ if (this.textBaseline !== undefined)
182
+ return { y: this.textBaseline, textHeight };
183
+ const note = this.checkAttachedNote();
184
+ const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
185
+ let y;
173
186
  let stemExt = {};
174
187
  let spacing = 0;
175
188
  const hasStem = note.hasStem();
@@ -203,10 +216,20 @@ export class Annotation extends Modifier {
203
216
  const extents = note.getStemExtents();
204
217
  y = extents.topY + (extents.baseY - extents.topY) / 2 + textHeight / 2;
205
218
  }
219
+ return { y, textHeight };
220
+ }
221
+ draw() {
222
+ const ctx = this.checkContext();
223
+ const { x } = this.computeX();
224
+ const { y } = this.computeY();
225
+ this.setRendered();
226
+ const clsAttribute = this.getAttribute('class');
227
+ ctx.openGroup('annotation' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
206
228
  L('Rendering annotation: ', this.text, x, y);
207
229
  this.x = x;
208
230
  this.y = y;
209
231
  this.renderText(ctx, 0, 0);
232
+ this.drawPointerRect();
210
233
  ctx.closeGroup();
211
234
  }
212
235
  }
@@ -581,7 +581,7 @@ export class Beam extends Element {
581
581
  }, this);
582
582
  }
583
583
  drawBeamLines(ctx) {
584
- const validBeamDurations = ['4', '8', '16', '32', '64'];
584
+ const validBeamDurations = ['4', '8', '16', '32', '64', '128', '256', '512', '1024'];
585
585
  const firstNote = this.notes[0];
586
586
  let beamY = this.getBeamYToDraw();
587
587
  const firstStemX = firstNote.getStemX();
@@ -634,9 +634,11 @@ export class Beam extends Element {
634
634
  if (!this.postFormatted) {
635
635
  this.postFormat();
636
636
  }
637
- ctx.openGroup('beam', this.getAttribute('id'));
637
+ const clsAttribute = this.getAttribute('class');
638
+ ctx.openGroup('beam' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
638
639
  this.drawStems(ctx);
639
640
  this.drawBeamLines(ctx);
641
+ this.drawPointerRect();
640
642
  ctx.closeGroup();
641
643
  }
642
644
  }
@@ -229,7 +229,8 @@ export class ChordSymbol extends Modifier {
229
229
  const ctx = this.checkContext();
230
230
  const note = this.checkAttachedNote();
231
231
  this.setRendered();
232
- ctx.openGroup('chordsymbol', this.getAttribute('id'));
232
+ const clsAttribute = this.getAttribute('class');
233
+ ctx.openGroup('chordsymbol' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
233
234
  const start = note.getModifierStartXY(Modifier.Position.ABOVE, this.index);
234
235
  ctx.setFont(this.fontInfo);
235
236
  let y;
@@ -273,6 +274,7 @@ export class ChordSymbol extends Modifier {
273
274
  symbol.setY(y);
274
275
  symbol.renderText(ctx, 0, 0);
275
276
  });
277
+ this.drawPointerRect();
276
278
  ctx.closeGroup();
277
279
  }
278
280
  getBoundingBox() {
@@ -109,9 +109,11 @@ export class Clef extends StaveModifier {
109
109
  const stave = this.checkStave();
110
110
  const ctx = stave.checkContext();
111
111
  this.setRendered();
112
- ctx.openGroup('clef', this.getAttribute('id'));
112
+ const clsAttribute = this.getAttribute('class');
113
+ ctx.openGroup('clef' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
113
114
  this.y = stave.getYForLine(this.line);
114
115
  this.renderText(ctx, 0, 0);
116
+ this.drawPointerRect();
115
117
  ctx.closeGroup();
116
118
  }
117
119
  }
@@ -1,5 +1,31 @@
1
+ import { BoundingBox } from './boundingbox.js';
1
2
  import { Element } from './element.js';
2
3
  import { RuntimeError } from './util.js';
4
+ function bezierExtrema(p0, p1, p2, p3) {
5
+ const values = [p0, p3];
6
+ const a = -p0 + 3 * p1 - 3 * p2 + p3;
7
+ const b = 2 * (p0 - 2 * p1 + p2);
8
+ const c = p1 - p0;
9
+ const ts = [];
10
+ if (Math.abs(a) < 1e-12) {
11
+ if (Math.abs(b) > 1e-12)
12
+ ts.push(-c / b);
13
+ }
14
+ else {
15
+ const discriminant = b * b - 4 * a * c;
16
+ if (discriminant >= 0) {
17
+ const root = Math.sqrt(discriminant);
18
+ ts.push((-b + root) / (2 * a), (-b - root) / (2 * a));
19
+ }
20
+ }
21
+ for (const t of ts) {
22
+ if (t > 0 && t < 1) {
23
+ const u = 1 - t;
24
+ values.push(u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t * p3);
25
+ }
26
+ }
27
+ return values;
28
+ }
3
29
  export var CurvePosition;
4
30
  (function (CurvePosition) {
5
31
  CurvePosition[CurvePosition["NEAR_HEAD"] = 1] = "NEAR_HEAD";
@@ -37,9 +63,7 @@ export class Curve extends Element {
37
63
  isPartial() {
38
64
  return !this.from || !this.to;
39
65
  }
40
- renderCurve(params) {
41
- var _a, _b;
42
- const ctx = this.checkContext();
66
+ getBezierPoints(params) {
43
67
  const xShift = this.renderOptions.xShift;
44
68
  const yShift = this.renderOptions.yShift * params.direction;
45
69
  const firstX = params.firstX + xShift;
@@ -51,19 +75,34 @@ export class Curve extends Element {
51
75
  const { x: cp0x, y: cp0y } = cps[0];
52
76
  const { x: cp1x, y: cp1y } = cps[1];
53
77
  const controlPointSpacing = (lastX - firstX) / (cps.length + 2);
78
+ return {
79
+ first: { x: firstX, y: firstY },
80
+ last: { x: lastX, y: lastY },
81
+ cps: [
82
+ { x: firstX + controlPointSpacing + cp0x, y: firstY + cp0y * params.direction },
83
+ { x: lastX - controlPointSpacing + cp1x, y: lastY + cp1y * params.direction },
84
+ ],
85
+ thicknessCps: [
86
+ { x: lastX - controlPointSpacing + cp1x, y: lastY + (cp1y + thickness) * params.direction },
87
+ { x: firstX + controlPointSpacing + cp0x, y: firstY + (cp0y + thickness) * params.direction },
88
+ ],
89
+ };
90
+ }
91
+ renderCurve(params) {
92
+ var _a, _b;
93
+ const ctx = this.checkContext();
94
+ const { first, last, cps, thicknessCps } = this.getBezierPoints(params);
54
95
  ctx.beginPath();
55
- ctx.moveTo(firstX, firstY);
56
- ctx.bezierCurveTo(firstX + controlPointSpacing + cp0x, firstY + cp0y * params.direction, lastX - controlPointSpacing + cp1x, lastY + cp1y * params.direction, lastX, lastY);
96
+ ctx.moveTo(first.x, first.y);
97
+ ctx.bezierCurveTo(cps[0].x, cps[0].y, cps[1].x, cps[1].y, last.x, last.y);
57
98
  if (!((_a = this.style) === null || _a === void 0 ? void 0 : _a.lineDash))
58
- ctx.bezierCurveTo(lastX - controlPointSpacing + cp1x, lastY + (cp1y + thickness) * params.direction, firstX + controlPointSpacing + cp0x, firstY + (cp0y + thickness) * params.direction, firstX, firstY);
99
+ ctx.bezierCurveTo(thicknessCps[0].x, thicknessCps[0].y, thicknessCps[1].x, thicknessCps[1].y, first.x, first.y);
59
100
  ctx.stroke();
60
101
  ctx.closePath();
61
102
  if (!((_b = this.style) === null || _b === void 0 ? void 0 : _b.lineDash))
62
103
  ctx.fill();
63
104
  }
64
- draw() {
65
- this.checkContext();
66
- this.setRendered();
105
+ getRenderParams() {
67
106
  const firstNote = this.from;
68
107
  const lastNote = this.to;
69
108
  let firstX;
@@ -114,13 +153,31 @@ export class Curve extends Element {
114
153
  if (this.renderOptions.openingDirection === 'down') {
115
154
  stemDirection = -1;
116
155
  }
117
- this.renderCurve({
156
+ return {
118
157
  firstX,
119
158
  lastX,
120
159
  firstY,
121
160
  lastY,
122
161
  direction: stemDirection * (this.renderOptions.invert === true ? -1 : 1),
123
- });
162
+ };
163
+ }
164
+ getBoundingBox() {
165
+ var _a;
166
+ const { first, last, cps, thicknessCps } = this.getBezierPoints(this.getRenderParams());
167
+ const xs = bezierExtrema(first.x, cps[0].x, cps[1].x, last.x);
168
+ const ys = bezierExtrema(first.y, cps[0].y, cps[1].y, last.y);
169
+ if (!((_a = this.style) === null || _a === void 0 ? void 0 : _a.lineDash)) {
170
+ xs.push(...bezierExtrema(last.x, thicknessCps[0].x, thicknessCps[1].x, first.x));
171
+ ys.push(...bezierExtrema(last.y, thicknessCps[0].y, thicknessCps[1].y, first.y));
172
+ }
173
+ const minX = Math.min(...xs);
174
+ const minY = Math.min(...ys);
175
+ return new BoundingBox(minX, minY, Math.max(...xs) - minX, Math.max(...ys) - minY);
176
+ }
177
+ draw() {
178
+ this.checkContext();
179
+ this.setRendered();
180
+ this.renderCurve(this.getRenderParams());
124
181
  return true;
125
182
  }
126
183
  }
@@ -196,9 +196,10 @@ export class Builder {
196
196
  this.reset();
197
197
  }
198
198
  reset(options) {
199
+ var _a;
199
200
  this.options = Object.assign({ stem: 'auto', clef: 'treble' }, options);
200
201
  this.elements = { notes: [], accidentals: [] };
201
- this.rollingDuration = '8';
202
+ this.rollingDuration = (_a = options === null || options === void 0 ? void 0 : options.duration) !== null && _a !== void 0 ? _a : '8';
202
203
  this.resetPiece();
203
204
  }
204
205
  getFactory() {
@@ -363,6 +364,44 @@ export class EasyScore {
363
364
  this.parse(line, options);
364
365
  return this.builder.getElements().notes;
365
366
  }
367
+ measures(line, options = {}) {
368
+ const parts = [];
369
+ let start = 0;
370
+ let quote = '';
371
+ for (let i = 0; i < line.length; i++) {
372
+ const char = line[i];
373
+ if (quote) {
374
+ if (char === quote)
375
+ quote = '';
376
+ }
377
+ else if (char === '"' || char === "'") {
378
+ quote = char;
379
+ }
380
+ else if (char === '|') {
381
+ parts.push(line.slice(start, i));
382
+ start = i + 1;
383
+ }
384
+ }
385
+ parts.push(line.slice(start));
386
+ const measures = parts.map((part, index) => {
387
+ let text = part.trim();
388
+ if (index > 0)
389
+ text = text.replace(/^,\s*/, '');
390
+ if (index < parts.length - 1)
391
+ text = text.replace(/\s*,$/, '');
392
+ if (!text)
393
+ throw new RuntimeError('BadArguments', 'Empty EasyScore measure. Use an explicit rest.');
394
+ return text;
395
+ });
396
+ let duration = options.duration;
397
+ return measures.map((text) => {
398
+ const result = this.parse(text, Object.assign(Object.assign({ clef: this.defaults.clef, stem: this.defaults.stem }, options), { duration }));
399
+ if (!result.success)
400
+ throw new RuntimeError('BadArguments', `Invalid EasyScore measure: ${text}`);
401
+ duration = this.builder.rollingDuration;
402
+ return this.builder.getElements().notes;
403
+ });
404
+ }
366
405
  voice(notes, options = {}) {
367
406
  options = Object.assign({ time: this.defaults.time }, options);
368
407
  return this.factory.Voice(options).addTickables(notes);
@@ -30,6 +30,7 @@ export class Element {
30
30
  var _a;
31
31
  this.children = [];
32
32
  this.style = {};
33
+ this.styleExplicit = false;
33
34
  this._text = '';
34
35
  this.metricsValid = false;
35
36
  this._textMetrics = {
@@ -61,6 +62,7 @@ export class Element {
61
62
  this._fontInfo = Metrics.getFontInfo(this.attrs.type);
62
63
  this.style = Metrics.getStyle(this.attrs.type);
63
64
  this.fontScale = Metrics.get(`${this.attrs.type}.fontScale`);
65
+ this.shouldDrawPointerRect = Metrics.get(`${this.attrs.type}.pointerRect`);
64
66
  (_a = Registry.getDefaultRegistry()) === null || _a === void 0 ? void 0 : _a.register(this);
65
67
  }
66
68
  addChild(child) {
@@ -75,10 +77,12 @@ export class Element {
75
77
  }
76
78
  setStyle(style) {
77
79
  this.style = style;
80
+ this.styleExplicit = true;
78
81
  return this;
79
82
  }
80
83
  setGroupStyle(style) {
81
84
  this.style = style;
85
+ this.styleExplicit = true;
82
86
  this.children.forEach((child) => child.setGroupStyle(style));
83
87
  return this;
84
88
  }
@@ -88,26 +92,48 @@ export class Element {
88
92
  applyStyle(context = this.context, style = this.getStyle()) {
89
93
  if (!context)
90
94
  return this;
95
+ if (style === this.style && !this.styleExplicit) {
96
+ const defaults = context.getDefaultStyle();
97
+ style = Object.assign({}, style);
98
+ if (defaults.fillStyle !== undefined)
99
+ delete style.fillStyle;
100
+ if (defaults.strokeStyle !== undefined)
101
+ delete style.strokeStyle;
102
+ }
91
103
  if (style.shadowColor)
92
104
  context.setShadowColor(style.shadowColor);
93
- if (style.shadowBlur)
105
+ if (style.shadowBlur !== undefined)
94
106
  context.setShadowBlur(style.shadowBlur);
95
107
  if (style.fillStyle)
96
108
  context.setFillStyle(style.fillStyle);
97
109
  if (style.strokeStyle)
98
110
  context.setStrokeStyle(style.strokeStyle);
99
- if (style.lineWidth)
111
+ if (style.lineWidth !== undefined)
100
112
  context.setLineWidth(style.lineWidth);
101
- if (style.lineDash)
102
- context.setLineDash(style.lineDash.split(' ').map(Number));
113
+ if (style.lineDash !== undefined)
114
+ context.setLineDash(style.lineDash ? style.lineDash.split(' ').map(Number) : []);
103
115
  return this;
104
116
  }
105
117
  drawWithStyle() {
106
118
  const ctx = this.checkContext();
119
+ const defaults = ctx.getDefaultStyle();
107
120
  ctx.save();
108
- this.applyStyle(ctx);
109
- this.draw();
110
- ctx.restore();
121
+ try {
122
+ if (this.styleExplicit) {
123
+ const inherited = Object.assign({}, defaults);
124
+ if (this.style.fillStyle !== undefined)
125
+ inherited.fillStyle = this.style.fillStyle;
126
+ if (this.style.strokeStyle !== undefined)
127
+ inherited.strokeStyle = this.style.strokeStyle;
128
+ ctx.setDefaultStyle(inherited);
129
+ }
130
+ this.applyStyle(ctx);
131
+ this.draw();
132
+ }
133
+ finally {
134
+ ctx.setDefaultStyle(defaults);
135
+ ctx.restore();
136
+ }
111
137
  return this;
112
138
  }
113
139
  draw() {
@@ -189,6 +215,13 @@ export class Element {
189
215
  getContext() {
190
216
  return this.context;
191
217
  }
218
+ drawPointerRect() {
219
+ var _a;
220
+ if (this.shouldDrawPointerRect) {
221
+ const bb = this.getBoundingBox();
222
+ (_a = this.context) === null || _a === void 0 ? void 0 : _a.pointerRect(bb.getX(), bb.getY(), bb.getW(), bb.getH());
223
+ }
224
+ }
192
225
  setContext(context) {
193
226
  this.context = context;
194
227
  return this;
@@ -383,13 +416,13 @@ export class Element {
383
416
  }
384
417
  setOriginX(x) {
385
418
  const bbox = this.getBoundingBox();
386
- const originX = Math.abs((bbox.getX() - this.xShift) / bbox.getW());
419
+ const originX = Math.abs((bbox.getX() - this.x - this.xShift) / bbox.getW());
387
420
  const xShift = (x - originX) * bbox.getW();
388
421
  this.xShift = -xShift;
389
422
  }
390
423
  setOriginY(y) {
391
424
  const bbox = this.getBoundingBox();
392
- const originY = Math.abs((bbox.getY() - this.yShift) / bbox.getH());
425
+ const originY = Math.abs((bbox.getY() - this.y - this.yShift) / bbox.getH());
393
426
  const yShift = (y - originY) * bbox.getH();
394
427
  this.yShift = -yShift;
395
428
  }
@@ -21,6 +21,7 @@ import { Ornament } from './ornament.js';
21
21
  import { PedalMarking } from './pedalmarking.js';
22
22
  import { Renderer } from './renderer.js';
23
23
  import { RepeatNote } from './repeatnote.js';
24
+ import { Sheet } from './sheet.js';
24
25
  import { Stave } from './stave.js';
25
26
  import { StaveConnector } from './staveconnector.js';
26
27
  import { StaveLine } from './staveline.js';
@@ -49,6 +50,8 @@ export class Factory {
49
50
  return new Factory({ renderer: { elementId, width, height } });
50
51
  }
51
52
  constructor(options = {}) {
53
+ this.retained = false;
54
+ this.style = {};
52
55
  L('New factory: ', options);
53
56
  this.options = {
54
57
  stave: {
@@ -64,6 +67,8 @@ export class Factory {
64
67
  this.setOptions(options);
65
68
  }
66
69
  reset() {
70
+ this.sheets = [];
71
+ this.retained = false;
67
72
  this.renderQ = [];
68
73
  this.systems = [];
69
74
  this.staves = [];
@@ -109,6 +114,15 @@ export class Factory {
109
114
  getVoices() {
110
115
  return this.voices;
111
116
  }
117
+ setStyle(style) {
118
+ this.style = Object.assign({}, style);
119
+ return this;
120
+ }
121
+ Sheet(options = {}) {
122
+ const sheet = new Sheet(Object.assign(Object.assign({}, options), { factory: this }));
123
+ this.sheets.push(sheet);
124
+ return sheet;
125
+ }
112
126
  Stave(params) {
113
127
  const staveSpace = this.options.stave.space;
114
128
  const p = Object.assign({ x: 0, y: 0, width: this.options.renderer.width - staveSpace * 1.0, options: { spacingBetweenLinesPx: staveSpace * 1.0 } }, params);
@@ -447,17 +461,40 @@ export class Factory {
447
461
  group.setContext(this.context);
448
462
  return group;
449
463
  }
450
- draw() {
464
+ draw(options = {}) {
465
+ var _a;
451
466
  const ctx = this.context;
452
- this.systems.forEach((s) => s.setContext(ctx).format());
453
- this.staves.forEach((s) => s.setContext(ctx).drawWithStyle());
454
- this.voices.forEach((v) => v.setContext(ctx).drawWithStyle());
455
- this.renderQ.forEach((e) => {
456
- if (!e.isRendered())
457
- e.setContext(ctx).drawWithStyle();
467
+ this.sheets.forEach((sheet) => sheet.format());
468
+ const managed = new Set(this.sheets.flatMap((sheet) => sheet.getSystems()));
469
+ this.systems.forEach((s) => {
470
+ if (!managed.has(s))
471
+ s.setContext(ctx).format();
458
472
  });
459
- this.systems.forEach((s) => s.setContext(ctx).drawWithStyle());
460
- this.reset();
473
+ const defaults = ctx.getDefaultStyle();
474
+ ctx.save();
475
+ ctx.setDefaultStyle(this.style);
476
+ if (this.style.fillStyle !== undefined)
477
+ ctx.setFillStyle(this.style.fillStyle);
478
+ if (this.style.strokeStyle !== undefined)
479
+ ctx.setStrokeStyle(this.style.strokeStyle);
480
+ if (this.retained)
481
+ this.renderQ.forEach((element) => element.setRendered(false));
482
+ try {
483
+ this.staves.forEach((s) => s.setContext(ctx).drawWithStyle());
484
+ this.voices.forEach((v) => v.setContext(ctx).drawWithStyle());
485
+ this.renderQ.forEach((e) => {
486
+ if (!e.isRendered())
487
+ e.setContext(ctx).drawWithStyle();
488
+ });
489
+ this.systems.forEach((s) => s.setContext(ctx).drawWithStyle());
490
+ }
491
+ finally {
492
+ ctx.setDefaultStyle(defaults);
493
+ ctx.restore();
494
+ }
495
+ this.retained = (_a = options.retain) !== null && _a !== void 0 ? _a : false;
496
+ if (!options.retain)
497
+ this.reset();
461
498
  }
462
499
  }
463
500
  Factory.DEBUG = false;
@@ -11,9 +11,11 @@ export class Flag extends Element {
11
11
  draw() {
12
12
  const ctx = this.checkContext();
13
13
  this.setRendered();
14
- ctx.openGroup('flag', this.getAttribute('id'));
14
+ const clsAttribute = this.getAttribute('class');
15
+ ctx.openGroup('flag' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
15
16
  L("Drawing flag '", this.text, "' at", this.x, this.y);
16
17
  this.renderText(ctx, 0, 0);
18
+ this.drawPointerRect();
17
19
  ctx.closeGroup();
18
20
  }
19
21
  }
@@ -196,6 +196,23 @@ export class Font {
196
196
  return Font.HOST_URL + files[fontName];
197
197
  }
198
198
  }
199
+ Font.TEXT_FONT_NAMES = [
200
+ 'Academico',
201
+ 'Edwin',
202
+ 'Bravura Text',
203
+ 'Finale Ash Text',
204
+ 'Finale Broadway Text',
205
+ 'Finale Jazz Text',
206
+ 'Finale Maestro Text',
207
+ 'Gootville Text',
208
+ 'Leland Text',
209
+ 'MuseJazz Text',
210
+ 'Nepomuk',
211
+ 'Petaluma Script',
212
+ 'Petaluma Text',
213
+ 'Roboto Slab',
214
+ 'Sebastian Text',
215
+ ];
199
216
  Font.scaleToPxFrom = {
200
217
  pt: 4 / 3,
201
218
  px: 1,
@@ -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;