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
@@ -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
+ }
@@ -3,7 +3,7 @@ import { Clef } from './clef.js';
3
3
  import { Element } from './element.js';
4
4
  import { KeySignature } from './keysignature.js';
5
5
  import { Metrics } from './metrics.js';
6
- import { Barline, BarlineType } from './stavebarline.js';
6
+ import { Barline, BarlineType, RepeatBracketType } from './stavebarline.js';
7
7
  import { StaveModifierPosition } from './stavemodifier.js';
8
8
  import { Repetition } from './staverepetition.js';
9
9
  import { StaveSection } from './stavesection.js';
@@ -50,7 +50,7 @@ export class Stave extends Element {
50
50
  this.endClef = undefined;
51
51
  this.options = Object.assign({ verticalBarWidth: 10, numLines: 5, leftBar: true, rightBar: true, spacingBetweenLinesPx: Tables.STAVE_LINE_DISTANCE, spaceAboveStaffLn: 4, spaceBelowStaffLn: 4, topTextPosition: 1, bottomTextPosition: 4, lineConfig: [] }, options);
52
52
  this.bounds = { x: this.x, y: this.y, w: this.width, h: 0 };
53
- this.defaultLedgerLineStyle = { strokeStyle: '#444', lineWidth: 2 };
53
+ this.defaultLedgerLineStyle = { lineWidth: 2 };
54
54
  this.resetLines();
55
55
  this.addModifier(new Barline(this.options.leftBar ? BarlineType.SINGLE : BarlineType.NONE));
56
56
  this.addEndModifier(new Barline(this.options.rightBar ? BarlineType.SINGLE : BarlineType.NONE));
@@ -59,7 +59,10 @@ export class Stave extends Element {
59
59
  this.defaultLedgerLineStyle = style;
60
60
  }
61
61
  getDefaultLedgerLineStyle() {
62
- return Object.assign(Object.assign({}, this.getStyle()), this.defaultLedgerLineStyle);
62
+ var _a;
63
+ const themed = ((_a = this.getContext()) === null || _a === void 0 ? void 0 : _a.getDefaultStyle().strokeStyle) !== undefined;
64
+ const fallback = themed ? {} : { strokeStyle: '#444' };
65
+ return Object.assign(Object.assign(Object.assign({}, fallback), (this.styleExplicit ? this.getStyle() : {})), this.defaultLedgerLineStyle);
63
66
  }
64
67
  space(spacing) {
65
68
  return this.options.spacingBetweenLinesPx * spacing;
@@ -233,17 +236,17 @@ export class Stave extends Element {
233
236
  this.addModifier(modifier, StaveModifierPosition.END);
234
237
  return this;
235
238
  }
236
- setBegBarType(type) {
239
+ setBegBarType(type, repeatBracket = RepeatBracketType.NONE) {
237
240
  const { SINGLE, REPEAT_BEGIN, NONE } = BarlineType;
238
241
  if (type === SINGLE || type === REPEAT_BEGIN || type === NONE) {
239
- this.modifiers[0].setType(type);
242
+ this.modifiers[0].setType(type).setRepeatBracket(repeatBracket);
240
243
  this.formatted = false;
241
244
  }
242
245
  return this;
243
246
  }
244
- setEndBarType(type) {
247
+ setEndBarType(type, repeatBracket = RepeatBracketType.NONE) {
245
248
  if (type !== BarlineType.REPEAT_BEGIN) {
246
- this.modifiers[1].setType(type);
249
+ this.modifiers[1].setType(type).setRepeatBracket(repeatBracket);
247
250
  this.formatted = false;
248
251
  }
249
252
  return this;
@@ -451,7 +454,8 @@ export class Stave extends Element {
451
454
  var _a;
452
455
  const ctx = this.checkContext();
453
456
  this.setRendered();
454
- ctx.openGroup('stave', this.getAttribute('id'));
457
+ const clsAttribute = this.getAttribute('class');
458
+ ctx.openGroup('stave' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
455
459
  if (!this.formatted)
456
460
  this.format();
457
461
  const numLines = this.options.numLines;
@@ -469,6 +473,7 @@ export class Stave extends Element {
469
473
  ctx.stroke();
470
474
  }
471
475
  }
476
+ this.drawPointerRect();
472
477
  ctx.closeGroup();
473
478
  for (let i = 0; i < this.modifiers.length; i++) {
474
479
  const modifier = this.modifiers[i];
@@ -10,6 +10,12 @@ export var BarlineType;
10
10
  BarlineType[BarlineType["REPEAT_BOTH"] = 6] = "REPEAT_BOTH";
11
11
  BarlineType[BarlineType["NONE"] = 7] = "NONE";
12
12
  })(BarlineType || (BarlineType = {}));
13
+ export var RepeatBracketType;
14
+ (function (RepeatBracketType) {
15
+ RepeatBracketType[RepeatBracketType["NONE"] = 0] = "NONE";
16
+ RepeatBracketType[RepeatBracketType["STRAIGHT"] = 1] = "STRAIGHT";
17
+ RepeatBracketType[RepeatBracketType["CURVED"] = 2] = "CURVED";
18
+ })(RepeatBracketType || (RepeatBracketType = {}));
13
19
  export class Barline extends StaveModifier {
14
20
  static get CATEGORY() {
15
21
  return "Barline";
@@ -30,6 +36,7 @@ export class Barline extends StaveModifier {
30
36
  }
31
37
  constructor(type) {
32
38
  super();
39
+ this.repeatBrackets = RepeatBracketType.NONE;
33
40
  this.thickness = Tables.STAVE_LINE_THICKNESS;
34
41
  const TYPE = BarlineType;
35
42
  this.widths = {};
@@ -94,6 +101,12 @@ export class Barline extends StaveModifier {
94
101
  this.setPosition(StaveModifierPosition.BEGIN);
95
102
  this.setType(type);
96
103
  }
104
+ setRepeatBracket(val) {
105
+ this.repeatBrackets = val;
106
+ }
107
+ getRepeatBrackets() {
108
+ return this.repeatBrackets;
109
+ }
97
110
  getType() {
98
111
  return this.type;
99
112
  }
@@ -108,7 +121,8 @@ export class Barline extends StaveModifier {
108
121
  const stave = this.checkStave();
109
122
  const ctx = stave.checkContext();
110
123
  this.setRendered();
111
- ctx.openGroup('stavebarline', this.getAttribute('id'));
124
+ const clsAttribute = this.getAttribute('class');
125
+ ctx.openGroup('stavebarline' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
112
126
  switch (this.type) {
113
127
  case BarlineType.SINGLE:
114
128
  this.drawVerticalBar(stave, this.x, false);
@@ -135,6 +149,7 @@ export class Barline extends StaveModifier {
135
149
  default:
136
150
  break;
137
151
  }
152
+ this.drawPointerRect();
138
153
  ctx.closeGroup();
139
154
  }
140
155
  drawVerticalBar(stave, x, doubleBar) {
@@ -153,6 +168,34 @@ export class Barline extends StaveModifier {
153
168
  staveCtx.fillRect(x - 5, topY, 1, botY - topY);
154
169
  staveCtx.fillRect(x - 2, topY, 3, botY - topY);
155
170
  }
171
+ drawStraightBrackets(stave, startX, startY, yDirection, xDirection) {
172
+ const staveCtx = stave.checkContext();
173
+ const rptBracketW = Math.round(1.5 * Tables.STAVE_LINE_DISTANCE) * xDirection;
174
+ const rptBracketH = Math.round(0.6 * Tables.STAVE_LINE_DISTANCE);
175
+ const rptBracketMH = Math.round(rptBracketH / 2);
176
+ const brY = startY + yDirection;
177
+ const brX = xDirection > 0 ? startX - 2 : startX;
178
+ staveCtx.moveTo(brX, brY);
179
+ staveCtx.lineTo(brX, brY + rptBracketH * (-1 * yDirection));
180
+ staveCtx.lineTo(brX + rptBracketW, brY + rptBracketH * yDirection);
181
+ staveCtx.lineTo(brX + rptBracketW, brY + yDirection * (rptBracketH + rptBracketMH));
182
+ staveCtx.lineTo(brX, brY);
183
+ staveCtx.fill();
184
+ }
185
+ drawCurvedBrackets(stave, startX, startY, yDirection, xDirection) {
186
+ const staveCtx = stave.checkContext();
187
+ const rptBracketW = Math.round(1.5 * Tables.STAVE_LINE_DISTANCE) * xDirection;
188
+ const rptBracketH = Math.round(0.6 * Tables.STAVE_LINE_DISTANCE);
189
+ const rptBracketMH = Math.round(rptBracketH / 2);
190
+ const brY = startY + yDirection;
191
+ const brX = xDirection > 0 ? startX - 2 : startX;
192
+ staveCtx.moveTo(brX, brY);
193
+ staveCtx.lineTo(brX, brY + rptBracketH * (-1 * yDirection));
194
+ staveCtx.quadraticCurveTo(brX + rptBracketW / 2, brY + rptBracketH * (-1 * yDirection), brX + rptBracketW, brY + rptBracketH * yDirection);
195
+ staveCtx.lineTo(brX + rptBracketW, brY + yDirection * (rptBracketH + rptBracketMH));
196
+ staveCtx.quadraticCurveTo(brX + rptBracketW / 2, brY, brX, brY);
197
+ staveCtx.fill();
198
+ }
156
199
  drawRepeatBar(stave, x, begin) {
157
200
  const staveCtx = stave.checkContext();
158
201
  const topY = stave.getTopLineTopY();
@@ -161,8 +204,17 @@ export class Barline extends StaveModifier {
161
204
  if (!begin) {
162
205
  xShift = -5;
163
206
  }
207
+ const xBracketDir = begin ? 1 : -1;
164
208
  staveCtx.fillRect(x + xShift, topY, 1, botY - topY);
165
209
  staveCtx.fillRect(x - 2, topY, 3, botY - topY);
210
+ if (this.repeatBrackets === RepeatBracketType.CURVED) {
211
+ this.drawCurvedBrackets(stave, x, topY, -1, xBracketDir);
212
+ this.drawCurvedBrackets(stave, x, botY, 1, xBracketDir);
213
+ }
214
+ else if (this.repeatBrackets === RepeatBracketType.STRAIGHT) {
215
+ this.drawStraightBrackets(stave, x, topY, -1, xBracketDir);
216
+ this.drawStraightBrackets(stave, x, botY, 1, xBracketDir);
217
+ }
166
218
  const dotRadius = 2;
167
219
  if (begin) {
168
220
  xShift += 4;
@@ -104,6 +104,7 @@ export class StaveLine extends Element {
104
104
  ctx.setStrokeStyle(this.renderOptions.color);
105
105
  ctx.setFillStyle(this.renderOptions.color);
106
106
  }
107
+ this.applyStyle(ctx);
107
108
  ctx.beginPath();
108
109
  ctx.moveTo(startX, startY);
109
110
  ctx.lineTo(endX, endY);
@@ -189,6 +190,7 @@ export class StaveLine extends Element {
189
190
  }
190
191
  const color = renderOptions.color;
191
192
  this.applyStyle(ctx, { fillStyle: color, strokeStyle: color });
193
+ this.applyStyle(ctx);
192
194
  this.renderText(ctx, x, y);
193
195
  }
194
196
  }
@@ -14,7 +14,9 @@ function L(...args) {
14
14
  }
15
15
  const isInnerNoteIndex = (note, index) => index === (note.getStemDirection() === Stem.UP ? note.keyProps.length - 1 : 0);
16
16
  function shiftRestVertical(rest, note, dir) {
17
- const delta = dir;
17
+ const clearance = 0.5;
18
+ const distance = dir > 0 ? note.maxLine + clearance - rest.minLine : rest.maxLine + clearance - note.minLine;
19
+ const delta = dir * Math.max(1, Math.ceil(distance));
18
20
  rest.line += delta;
19
21
  rest.maxLine += delta;
20
22
  rest.minLine += delta;
@@ -272,8 +274,6 @@ export class StaveNote extends StemmableNote {
272
274
  else {
273
275
  this.setStemDirection((_c = noteStruct.stemDirection) !== null && _c !== void 0 ? _c : Stem.UP);
274
276
  }
275
- this.reset();
276
- this.buildFlag();
277
277
  }
278
278
  reset() {
279
279
  super.reset();
@@ -304,6 +304,8 @@ export class StaveNote extends StemmableNote {
304
304
  return this;
305
305
  }
306
306
  buildNoteHeads() {
307
+ const oldHeads = new Set(this._noteHeads);
308
+ this.children = this.children.filter((child) => !oldHeads.has(child));
307
309
  this._noteHeads = [];
308
310
  const stemDirection = this.getStemDirection();
309
311
  const keys = this.getKeys();
@@ -407,7 +409,12 @@ export class StaveNote extends StemmableNote {
407
409
  getBoundingBox() {
408
410
  const boundingBox = new BoundingBox(this.getAbsoluteX(), this.ys[0], 0, 0);
409
411
  this._noteHeads.forEach((notehead) => {
410
- boundingBox.mergeWith(notehead.getBoundingBox());
412
+ const displacement = notehead.isDisplaced()
413
+ ? (notehead.getWidth() - Stem.WIDTH / 2) * this.getStemDirection()
414
+ : 0;
415
+ const box = notehead.getBoundingBox();
416
+ box.setX(this.getNoteHeadBeginX() + displacement + notehead.getXShift());
417
+ boundingBox.mergeWith(box);
411
418
  });
412
419
  const { yTop, yBottom } = this.getNoteHeadBounds();
413
420
  if (!this.isRest() && this.hasStem()) {
@@ -417,8 +424,11 @@ export class StaveNote extends StemmableNote {
417
424
  : yBottom - noteStemHeight + this.flag.getTextMetrics().actualBoundingBoxAscent;
418
425
  boundingBox.mergeWith(new BoundingBox(this.getAbsoluteX(), stemY, 0, 0));
419
426
  }
420
- if (this.hasFlag()) {
427
+ if (this.shouldDrawFlag()) {
428
+ const { x, y } = this.getFlagPosition();
421
429
  const bbFlag = this.flag.getBoundingBox();
430
+ bbFlag.setX(x + this.flag.getXShift());
431
+ bbFlag.setY(y + this.flag.getYShift() - this.flag.getTextMetrics().actualBoundingBoxAscent);
422
432
  boundingBox.mergeWith(bbFlag);
423
433
  }
424
434
  for (let i = 0; i < this.modifiers.length; i++) {
@@ -481,7 +491,7 @@ export class StaveNote extends StemmableNote {
481
491
  });
482
492
  this.setYs(ys);
483
493
  if (this.stem) {
484
- const { yTop, yBottom } = this.getNoteHeadBounds();
494
+ const { yTop, yBottom } = this.getStemYBounds();
485
495
  this.stem.setYBounds(yTop, yBottom);
486
496
  }
487
497
  return this;
@@ -497,7 +507,7 @@ export class StaveNote extends StemmableNote {
497
507
  let tieStartX = this.getAbsoluteX();
498
508
  tieStartX += this.getGlyphWidth() + this.xShift + this.rightDisplacedHeadPx;
499
509
  if (this.modifierContext)
500
- tieStartX += this.modifierContext.getRightShift();
510
+ tieStartX += this.modifierContext.getRightShiftForTie();
501
511
  return tieStartX;
502
512
  }
503
513
  getTieLeftX() {
@@ -540,7 +550,6 @@ export class StaveNote extends StemmableNote {
540
550
  }
541
551
  let restShift = 0;
542
552
  switch (this._noteHeads[index].getText()) {
543
- case Glyphs.restDoubleWhole:
544
553
  case Glyphs.restWhole:
545
554
  restShift += 0.5;
546
555
  break;
@@ -555,8 +564,15 @@ export class StaveNote extends StemmableNote {
555
564
  restShift -= 1.5;
556
565
  break;
557
566
  case Glyphs.rest128th:
567
+ case Glyphs.rest256th:
558
568
  restShift -= 2.5;
559
569
  break;
570
+ case Glyphs.rest512th:
571
+ restShift -= 3.5;
572
+ break;
573
+ case Glyphs.rest1024th:
574
+ restShift -= 4.5;
575
+ break;
560
576
  }
561
577
  return {
562
578
  x: this.getAbsoluteX() + x,
@@ -627,6 +643,23 @@ export class StaveNote extends StemmableNote {
627
643
  this.setWidth(width);
628
644
  this.preFormatted = true;
629
645
  }
646
+ getStemYBounds() {
647
+ const anchorKey = this.stemDirection === Stem.UP ? 'up' : 'down';
648
+ let yTop = +Infinity;
649
+ let yBottom = -Infinity;
650
+ this._noteHeads.forEach((nh) => {
651
+ const offset = Tables.noteHeadStemYOffsets[nh.text];
652
+ const anchor = offset ? offset[anchorKey] : 0;
653
+ const y = nh.getY() - anchor * Tables.STAVE_LINE_DISTANCE;
654
+ yTop = Math.min(y, yTop);
655
+ yBottom = Math.max(y, yBottom);
656
+ });
657
+ if (!isFinite(yTop) || !isFinite(yBottom)) {
658
+ const bounds = this.getNoteHeadBounds();
659
+ return { yTop: bounds.yTop, yBottom: bounds.yBottom };
660
+ }
661
+ return { yTop, yBottom };
662
+ }
630
663
  getNoteHeadBounds() {
631
664
  let yTop = +Infinity;
632
665
  let yBottom = -Infinity;
@@ -745,20 +778,22 @@ export class StaveNote extends StemmableNote {
745
778
  const hasNoBeam = this.beam === undefined;
746
779
  return hasStem && hasFlag && hasNoBeam && !this.isRest();
747
780
  }
781
+ getFlagPosition() {
782
+ const { yTop, yBottom } = this.getNoteHeadBounds();
783
+ const noteStemHeight = this.checkStem().getHeight();
784
+ const x = this.getStemX() - Tables.STEM_WIDTH / 2;
785
+ const y = this.getStemDirection() === Stem.DOWN
786
+ ? yTop - noteStemHeight - this.flag.getTextMetrics().actualBoundingBoxDescent
787
+ : yBottom - noteStemHeight + this.flag.getTextMetrics().actualBoundingBoxAscent;
788
+ return { x, y };
789
+ }
748
790
  drawFlag() {
749
791
  const ctx = this.checkContext();
750
792
  if (!ctx) {
751
793
  throw new RuntimeError('NoCanvasContext', "Can't draw without a canvas context.");
752
794
  }
753
795
  if (this.shouldDrawFlag()) {
754
- const { yTop, yBottom } = this.getNoteHeadBounds();
755
- const noteStemHeight = this.stem.getHeight();
756
- const flagX = this.getStemX() - Tables.STEM_WIDTH / 2;
757
- const flagY = this.getStemDirection() === Stem.DOWN
758
- ?
759
- yTop - noteStemHeight - this.flag.getTextMetrics().actualBoundingBoxDescent
760
- :
761
- yBottom - noteStemHeight + this.flag.getTextMetrics().actualBoundingBoxAscent;
796
+ const { x: flagX, y: flagY } = this.getFlagPosition();
762
797
  this.flag.setContext(ctx).setX(flagX).setY(flagY).drawWithStyle();
763
798
  }
764
799
  }
@@ -823,14 +858,14 @@ export class StaveNote extends StemmableNote {
823
858
  this.stem.setNoteHeadXBounds(stemX, stemX);
824
859
  }
825
860
  L('Rendering ', this.isChord() ? 'chord :' : 'note :', this.keys);
826
- ctx.openGroup('stavenote', this.getAttribute('id'));
861
+ const clsAttribute = this.getAttribute('class');
862
+ ctx.openGroup('stavenote' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
827
863
  this.drawLedgerLines();
828
864
  if (shouldRenderStem)
829
865
  this.drawStem();
830
866
  this.drawNoteHeads();
831
867
  this.drawFlag();
832
- const bb = this.getBoundingBox();
833
- ctx.pointerRect(bb.getX(), bb.getY(), bb.getW(), bb.getH());
868
+ this.drawPointerRect();
834
869
  ctx.closeGroup();
835
870
  this.setRendered();
836
871
  }
@@ -74,7 +74,8 @@ export class StaveTie extends Element {
74
74
  const yShift = this.renderOptions.yShift * params.direction;
75
75
  const firstIndexes = this.notes.firstIndexes;
76
76
  const lastIndexes = this.notes.lastIndexes;
77
- ctx.openGroup('stavetie', this.getAttribute('id'));
77
+ const clsAttribute = this.getAttribute('class');
78
+ ctx.openGroup('stavetie' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
78
79
  for (let i = 0; i < firstIndexes.length; ++i) {
79
80
  const cpX = (params.lastX + lastXShift + (params.firstX + firstXShift)) / 2;
80
81
  const firstY = params.firstYs[firstIndexes[i]] + yShift;
@@ -91,6 +92,7 @@ export class StaveTie extends Element {
91
92
  ctx.closePath();
92
93
  ctx.fill();
93
94
  }
95
+ this.drawPointerRect();
94
96
  ctx.closeGroup();
95
97
  }
96
98
  renderTieText(firstX, lastX) {
@@ -120,7 +120,8 @@ export class Stem extends Element {
120
120
  const stemHeight = this.getHeight();
121
121
  L('Rendering stem - ', 'Top Y: ', this.yTop, 'Bottom Y: ', this.yBottom);
122
122
  const stemletYOffset = this.isStemlet ? stemHeight - this.stemletHeight * this.stemDirection : 0;
123
- ctx.openGroup('stem', this.getAttribute('id'));
123
+ const clsAttribute = this.getAttribute('class');
124
+ ctx.openGroup('stem' + (clsAttribute ? ' ' + clsAttribute : ''), this.getAttribute('id'));
124
125
  ctx.beginPath();
125
126
  ctx.setLineWidth(Stem.WIDTH);
126
127
  ctx.moveTo(stemX, stemY - stemletYOffset + yBaseOffset);
@@ -80,6 +80,15 @@ export class StemmableNote extends Note {
80
80
  case '128':
81
81
  length = beamIsUndefined ? 55 : 45;
82
82
  break;
83
+ case '256':
84
+ length = beamIsUndefined ? 60 : 55;
85
+ break;
86
+ case '512':
87
+ length = beamIsUndefined ? 65 : 60;
88
+ break;
89
+ case '1024':
90
+ length = beamIsUndefined ? 70 : 65;
91
+ break;
83
92
  default:
84
93
  break;
85
94
  }
@@ -198,7 +198,6 @@ export class StringNumber extends Modifier {
198
198
  const lastNote = this.lastNote;
199
199
  if (isStemmableNote(lastNote)) {
200
200
  const end = lastNote.getStemX() - note.getX() + 5;
201
- ctx.setStrokeStyle('#000000');
202
201
  ctx.setLineCap('round');
203
202
  ctx.setLineWidth(0.6);
204
203
  if (this.dashed) {
@@ -1,3 +1,4 @@
1
+ import { BoundingBox } from './boundingbox.js';
1
2
  import { Element } from './element.js';
2
3
  import { Glyphs } from './glyphs.js';
3
4
  import { Metrics } from './metrics.js';
@@ -39,6 +40,7 @@ export class Stroke extends Modifier {
39
40
  }
40
41
  constructor(type, options) {
41
42
  super();
43
+ this.boundingBox = new BoundingBox(0, 0, 0, 0);
42
44
  this.options = Object.assign({ allVoices: true }, options);
43
45
  this.allVoices = this.options.allVoices;
44
46
  this.type = type;
@@ -107,6 +109,7 @@ export class Stroke extends Modifier {
107
109
  }
108
110
  if (this.type === Stroke.Type.BRUSH_DOWN || this.type === Stroke.Type.BRUSH_UP) {
109
111
  ctx.fillRect(x + this.xShift, topY, 1, botY - topY);
112
+ this.boundingBox = new BoundingBox(x + this.xShift, topY, 1, botY - topY);
110
113
  }
111
114
  else {
112
115
  const lineGlyph = arrow === Glyphs.arrowheadBlackDown ? Glyphs.wiggleArpeggiatoDown : Glyphs.wiggleArpeggiatoUp;
@@ -123,25 +126,32 @@ export class Stroke extends Modifier {
123
126
  el.renderText(ctx, x + this.xShift, topY - el.getTextMetrics().actualBoundingBoxDescent + el.getHeight() / 2);
124
127
  ctx.closeRotation();
125
128
  textY = topY + el.getWidth() + 5;
129
+ this.boundingBox = new BoundingBox(x + this.xShift - el.getHeight() / 2, topY - el.getTextMetrics().actualBoundingBoxDescent + el.getHeight() / 2, el.getHeight(), el.getWidth());
126
130
  }
127
131
  else {
128
132
  ctx.openRotation(-90, x + this.xShift, botY);
129
133
  el.renderText(ctx, x + this.xShift, botY - el.getTextMetrics().actualBoundingBoxDescent + el.getHeight() / 2);
130
134
  ctx.closeRotation();
131
135
  textY = botY - el.getWidth() - 5;
136
+ this.boundingBox = new BoundingBox(x + this.xShift - el.getHeight() / 2, botY - el.getTextMetrics().actualBoundingBoxDescent + el.getHeight() / 2 - el.getWidth(), el.getHeight(), el.getWidth());
132
137
  }
133
138
  }
134
139
  if (arrowY !== 0) {
135
140
  const el = new Element();
136
141
  el.setText(arrow);
137
142
  el.renderText(ctx, x + this.xShift - el.getWidth() / 2, arrowY);
143
+ this.boundingBox.mergeWith(new BoundingBox(x + this.xShift - el.getWidth() / 2, arrowY - el.getHeight(), el.getWidth(), el.getHeight()));
138
144
  }
139
145
  if (this.type === Stroke.Type.RASGUEADO_DOWN || this.type === Stroke.Type.RASGUEADO_UP) {
140
146
  const el = new Element('Stroke.text');
141
147
  el.setText('R');
142
148
  el.renderText(ctx, x + this.xShift - el.getWidth() / 2, textY + (this.type === Stroke.Type.RASGUEADO_DOWN ? el.getHeight() : 0));
149
+ this.boundingBox.mergeWith(new BoundingBox(x + this.xShift - el.getWidth() / 2, textY + (this.type === Stroke.Type.RASGUEADO_DOWN ? el.getHeight() : 0) - el.getHeight(), el.getWidth(), el.getHeight()));
143
150
  }
144
151
  }
152
+ getBoundingBox() {
153
+ return this.boundingBox;
154
+ }
145
155
  }
146
156
  Stroke.Type = {
147
157
  BRUSH_DOWN: 1,
@@ -2,8 +2,9 @@ import { Element } from './element.js';
2
2
  import { Font, FontStyle, FontWeight } from './font.js';
3
3
  import { Metrics } from './metrics.js';
4
4
  import { RenderContext } from './rendercontext.js';
5
+ import { svgWithFontPaths } from './svgexport.js';
5
6
  import { Tables } from './tables.js';
6
- import { normalizeAngle, prefix, RuntimeError } from './util.js';
7
+ import { normalizeAngle, prefix, RuntimeError, structuredClone } from './util.js';
7
8
  const ATTRIBUTES_TO_IGNORE = {
8
9
  path: {
9
10
  x: true,
@@ -29,6 +30,15 @@ const ATTRIBUTES_TO_IGNORE = {
29
30
  const SVG_NS = 'http://www.w3.org/2000/svg';
30
31
  const TWO_PI = 2 * Math.PI;
31
32
  export class SVGContext extends RenderContext {
33
+ static toSVG(svg, fonts) {
34
+ var _a, _b;
35
+ const output = fonts === undefined ? svg : svgWithFontPaths(svg, fonts);
36
+ const Serializer = (_b = (_a = svg.ownerDocument.defaultView) === null || _a === void 0 ? void 0 : _a.XMLSerializer) !== null && _b !== void 0 ? _b : XMLSerializer;
37
+ return new Serializer().serializeToString(output);
38
+ }
39
+ toSVG(fonts) {
40
+ return SVGContext.toSVG(this.svg, fonts);
41
+ }
32
42
  constructor(element) {
33
43
  super();
34
44
  this.width = 0;
@@ -132,9 +142,9 @@ export class SVGContext extends RenderContext {
132
142
  resize(width, height) {
133
143
  this.width = width;
134
144
  this.height = height;
135
- this.element.style.width = width.toString();
136
- this.svg.style.width = width.toString();
137
- this.svg.style.height = height.toString();
145
+ this.element.style.width = `${width}px`;
146
+ this.svg.style.width = `${width}px`;
147
+ this.svg.style.height = `${height}px`;
138
148
  const attributes = {
139
149
  width,
140
150
  height,