vexflow 5.0.0-alpha.3 → 5.0.0-beta.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 (152) hide show
  1. package/README.md +6 -6
  2. package/build/cjs/vexflow-bravura.js +2 -2
  3. package/build/cjs/vexflow-bravura.js.map +1 -1
  4. package/build/cjs/vexflow-core.js +2 -2
  5. package/build/cjs/vexflow-core.js.map +1 -1
  6. package/build/cjs/vexflow-debug-with-tests.js +2066 -1877
  7. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  8. package/build/cjs/vexflow-debug.js +1052 -993
  9. package/build/cjs/vexflow-debug.js.map +1 -1
  10. package/build/cjs/vexflow.js +2 -2
  11. package/build/cjs/vexflow.js.map +1 -1
  12. package/build/esm/src/accidental.js +22 -28
  13. package/build/esm/src/annotation.js +0 -2
  14. package/build/esm/src/barnote.js +3 -3
  15. package/build/esm/src/beam.js +26 -46
  16. package/build/esm/src/bend.js +11 -3
  17. package/build/esm/src/chordsymbol.js +0 -4
  18. package/build/esm/src/clef.js +0 -2
  19. package/build/esm/src/clefnote.js +6 -1
  20. package/build/esm/src/curve.js +8 -5
  21. package/build/esm/src/element.js +94 -118
  22. package/build/esm/src/factory.js +4 -4
  23. package/build/esm/src/flag.js +20 -0
  24. package/build/esm/src/fonts/gonville.js +1 -1
  25. package/build/esm/src/formatter.js +13 -21
  26. package/build/esm/src/fraction.js +15 -22
  27. package/build/esm/src/glyphnote.js +3 -4
  28. package/build/esm/src/gracenote.js +0 -2
  29. package/build/esm/src/gracenotegroup.js +17 -8
  30. package/build/esm/src/index.js +1 -0
  31. package/build/esm/src/keysignature.js +38 -31
  32. package/build/esm/src/keysignote.js +1 -1
  33. package/build/esm/src/metrics.js +58 -7
  34. package/build/esm/src/modifier.js +10 -23
  35. package/build/esm/src/multimeasurerest.js +16 -25
  36. package/build/esm/src/note.js +7 -0
  37. package/build/esm/src/notehead.js +6 -1
  38. package/build/esm/src/ornament.js +0 -2
  39. package/build/esm/src/pedalmarking.js +33 -41
  40. package/build/esm/src/registry.js +5 -19
  41. package/build/esm/src/stave.js +11 -19
  42. package/build/esm/src/stavebarline.js +2 -3
  43. package/build/esm/src/stavehairpin.js +1 -1
  44. package/build/esm/src/staveline.js +0 -4
  45. package/build/esm/src/stavemodifier.js +0 -2
  46. package/build/esm/src/stavenote.js +42 -68
  47. package/build/esm/src/staverepetition.js +3 -2
  48. package/build/esm/src/stavesection.js +13 -15
  49. package/build/esm/src/stavetempo.js +7 -13
  50. package/build/esm/src/stavetext.js +2 -2
  51. package/build/esm/src/stavetie.js +82 -40
  52. package/build/esm/src/stavevolta.js +3 -2
  53. package/build/esm/src/stem.js +0 -4
  54. package/build/esm/src/stemmablenote.js +5 -6
  55. package/build/esm/src/stringnumber.js +0 -2
  56. package/build/esm/src/svgcontext.js +5 -13
  57. package/build/esm/src/tabnote.js +2 -8
  58. package/build/esm/src/textbracket.js +0 -3
  59. package/build/esm/src/textnote.js +0 -2
  60. package/build/esm/src/tickable.js +13 -26
  61. package/build/esm/src/tickcontext.js +20 -2
  62. package/build/esm/src/timesignature.js +2 -4
  63. package/build/esm/src/timesignote.js +2 -0
  64. package/build/esm/src/tuplet.js +37 -30
  65. package/build/esm/src/version.js +3 -3
  66. package/build/esm/src/vexflow.js +6 -2
  67. package/build/esm/src/vibrato.js +6 -1
  68. package/build/esm/tests/accidental_tests.js +30 -17
  69. package/build/esm/tests/annotation_tests.js +12 -12
  70. package/build/esm/tests/articulation_tests.js +13 -13
  71. package/build/esm/tests/auto_beam_formatting_tests.js +33 -33
  72. package/build/esm/tests/beam_tests.js +1 -1
  73. package/build/esm/tests/bend_tests.js +7 -7
  74. package/build/esm/tests/chordsymbol_tests.js +8 -8
  75. package/build/esm/tests/clef_tests.js +5 -1
  76. package/build/esm/tests/curve_tests.js +34 -0
  77. package/build/esm/tests/{bach_tests.js → demo_petzold_tests.js} +7 -7
  78. package/build/esm/tests/dot_tests.js +4 -4
  79. package/build/esm/tests/formatter_tests.js +25 -25
  80. package/build/esm/tests/glyphnote_tests.js +6 -0
  81. package/build/esm/tests/gracenote_tests.js +2 -1
  82. package/build/esm/tests/gracetabnote_tests.js +1 -1
  83. package/build/esm/tests/index.js +1 -1
  84. package/build/esm/tests/key_clef_tests.js +6 -6
  85. package/build/esm/tests/keysignature_tests.js +20 -17
  86. package/build/esm/tests/notehead_tests.js +10 -7
  87. package/build/esm/tests/offscreencanvas_tests.js +1 -1
  88. package/build/esm/tests/ornament_tests.js +8 -8
  89. package/build/esm/tests/pedalmarking_tests.js +27 -0
  90. package/build/esm/tests/percussion_tests.js +6 -3
  91. package/build/esm/tests/renderer_tests.js +1 -1
  92. package/build/esm/tests/rests_tests.js +26 -26
  93. package/build/esm/tests/rhythm_tests.js +15 -15
  94. package/build/esm/tests/stave_tests.js +44 -43
  95. package/build/esm/tests/staveconnector_tests.js +73 -73
  96. package/build/esm/tests/stavehairpin_tests.js +1 -1
  97. package/build/esm/tests/stavemodifier_tests.js +3 -3
  98. package/build/esm/tests/stavenote_tests.js +29 -29
  99. package/build/esm/tests/stavetie_tests.js +29 -0
  100. package/build/esm/tests/stringnumber_tests.js +1 -1
  101. package/build/esm/tests/strokes_tests.js +1 -1
  102. package/build/esm/tests/style_tests.js +1 -1
  103. package/build/esm/tests/tabnote_tests.js +7 -7
  104. package/build/esm/tests/tabslide_tests.js +6 -6
  105. package/build/esm/tests/tabstave_tests.js +2 -1
  106. package/build/esm/tests/tabtie_tests.js +8 -8
  107. package/build/esm/tests/textnote_tests.js +1 -1
  108. package/build/esm/tests/threevoice_tests.js +2 -2
  109. package/build/esm/tests/timesignature_tests.js +23 -13
  110. package/build/esm/tests/vexflow_test_helpers.js +25 -39
  111. package/build/esm/tests/vibrato_tests.js +3 -3
  112. package/build/esm/tests/voice_tests.js +1 -1
  113. package/build/types/src/accidental.d.ts +19 -1
  114. package/build/types/src/annotation.d.ts +1 -1
  115. package/build/types/src/beam.d.ts +12 -1
  116. package/build/types/src/bend.d.ts +6 -0
  117. package/build/types/src/clefnote.d.ts +2 -0
  118. package/build/types/src/curve.d.ts +5 -4
  119. package/build/types/src/element.d.ts +23 -14
  120. package/build/types/src/factory.d.ts +2 -2
  121. package/build/types/src/flag.d.ts +12 -0
  122. package/build/types/src/fonts/gonville.d.ts +1 -1
  123. package/build/types/src/formatter.d.ts +5 -3
  124. package/build/types/src/fraction.d.ts +12 -7
  125. package/build/types/src/frethandfinger.d.ts +1 -1
  126. package/build/types/src/index.d.ts +1 -0
  127. package/build/types/src/keysignature.d.ts +7 -7
  128. package/build/types/src/metrics.d.ts +5 -0
  129. package/build/types/src/modifier.d.ts +3 -2
  130. package/build/types/src/multimeasurerest.d.ts +4 -1
  131. package/build/types/src/note.d.ts +2 -0
  132. package/build/types/src/pedalmarking.d.ts +2 -2
  133. package/build/types/src/registry.d.ts +1 -1
  134. package/build/types/src/stave.d.ts +1 -3
  135. package/build/types/src/stavebarline.d.ts +1 -1
  136. package/build/types/src/staveline.d.ts +1 -1
  137. package/build/types/src/stavemodifier.d.ts +0 -1
  138. package/build/types/src/stavenote.d.ts +2 -1
  139. package/build/types/src/staverepetition.d.ts +1 -1
  140. package/build/types/src/stavesection.d.ts +3 -4
  141. package/build/types/src/stavetempo.d.ts +4 -5
  142. package/build/types/src/stavetext.d.ts +1 -2
  143. package/build/types/src/stavetie.d.ts +39 -2
  144. package/build/types/src/stavevolta.d.ts +1 -2
  145. package/build/types/src/stemmablenote.d.ts +2 -3
  146. package/build/types/src/svgcontext.d.ts +1 -1
  147. package/build/types/src/tickable.d.ts +2 -1
  148. package/build/types/src/tickcontext.d.ts +30 -0
  149. package/build/types/src/tuplet.d.ts +2 -12
  150. package/build/types/src/typeguard.d.ts +1 -0
  151. package/package.json +33 -29
  152. package/tests/flow.html +2 -2
@@ -66,10 +66,10 @@ export class Formatter {
66
66
  static SimpleFormat(notes, x = 0, { paddingBetween = 10 } = {}) {
67
67
  notes.reduce((accumulator, note) => {
68
68
  note.addToModifierContext(new ModifierContext());
69
- const tick = new TickContext().addTickable(note).preFormat();
70
- const metrics = tick.getMetrics();
71
- tick.setX(accumulator + metrics.totalLeftPx);
72
- return accumulator + tick.getWidth() + metrics.totalRightPx + paddingBetween;
69
+ const tickContext = new TickContext().addTickable(note).preFormat();
70
+ const metrics = tickContext.getMetrics();
71
+ tickContext.setX(accumulator + metrics.totalLeftPx);
72
+ return accumulator + tickContext.getWidth() + metrics.totalRightPx + paddingBetween;
73
73
  }, x);
74
74
  }
75
75
  static plotDebugging(ctx, formatter, xPos, y1, y2, options) {
@@ -111,8 +111,8 @@ export class Formatter {
111
111
  new Formatter()
112
112
  .joinVoices([voice])
113
113
  .formatToStave([voice], stave, { alignRests: options.alignRests, stave });
114
- voice.setStave(stave).draw(ctx, stave);
115
- beams.forEach((beam) => beam.setContext(ctx).draw());
114
+ voice.setContext(ctx).setStave(stave).drawWithStyle();
115
+ beams.forEach((beam) => beam.setContext(ctx).drawWithStyle());
116
116
  return voice.getBoundingBox();
117
117
  }
118
118
  static FormatAndDrawTab(ctx, tabstave, stave, tabnotes, notes, autoBeam, params) {
@@ -135,8 +135,8 @@ export class Formatter {
135
135
  .formatToStave([notevoice, tabvoice], stave, { alignRests: opts.alignRests });
136
136
  notevoice.draw(ctx, stave);
137
137
  tabvoice.draw(ctx, tabstave);
138
- beams.forEach((beam) => beam.setContext(ctx).draw());
139
- new StaveConnector(stave, tabstave).setContext(ctx).draw();
138
+ beams.forEach((beam) => beam.setContext(ctx).drawWithStyle());
139
+ new StaveConnector(stave, tabstave).setContext(ctx).drawWithStyle();
140
140
  }
141
141
  static AlignRestsToNotes(tickables, alignAllNotes, alignTuplets) {
142
142
  tickables.forEach((currTickable, index) => {
@@ -492,11 +492,12 @@ export class Formatter {
492
492
  function updateStats(duration, space) {
493
493
  const stats = durationStats[duration];
494
494
  if (stats === undefined) {
495
- durationStats[duration] = { mean: space, count: 1 };
495
+ durationStats[duration] = { mean: space, count: 1, total: space };
496
496
  }
497
497
  else {
498
498
  stats.count += 1;
499
- stats.mean = (stats.mean + space) / 2;
499
+ stats.total += space;
500
+ stats.mean = stats.total / stats.count;
500
501
  }
501
502
  }
502
503
  this.voices.forEach((voice) => {
@@ -545,23 +546,14 @@ export class Formatter {
545
546
  return 0;
546
547
  }
547
548
  const alpha = (_a = options === null || options === void 0 ? void 0 : options.alpha) !== null && _a !== void 0 ? _a : 0.5;
548
- function move(current, shift, prev, next) {
549
- current.setX(current.getX() + shift);
550
- current.getFormatterMetrics().freedom.left += shift;
551
- current.getFormatterMetrics().freedom.right -= shift;
552
- if (prev)
553
- prev.getFormatterMetrics().freedom.right += shift;
554
- if (next)
555
- next.getFormatterMetrics().freedom.left -= shift;
556
- }
557
549
  let shift = 0;
558
550
  this.totalShift = 0;
559
551
  contexts.list.forEach((tick, index, list) => {
560
552
  const context = contexts.map[tick];
561
553
  const prevContext = index > 0 ? contexts.map[list[index - 1]] : undefined;
562
554
  const nextContext = index < list.length - 1 ? contexts.map[list[index + 1]] : undefined;
563
- move(context, shift, prevContext, nextContext);
564
- const cost = -sumArray(context.getTickables().map((t) => t.getFormatterMetrics().space.deviation));
555
+ context.move(shift, prevContext, nextContext);
556
+ const cost = -context.getDeviationCost();
565
557
  if (cost > 0) {
566
558
  shift = -Math.min(context.getFormatterMetrics().freedom.right, Math.abs(cost));
567
559
  }
@@ -1,9 +1,3 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var _a, _Fraction_fractionA, _Fraction_fractionB;
7
1
  import { RuntimeError } from './util.js';
8
2
  export class Fraction {
9
3
  static get CATEGORY() {
@@ -22,7 +16,7 @@ export class Fraction {
22
16
  return a;
23
17
  }
24
18
  static LCM(a, b) {
25
- return (a * b) / _a.GCD(a, b);
19
+ return (a * b) / Fraction.GCD(a, b);
26
20
  }
27
21
  static LCMM(args) {
28
22
  if (args.length === 0) {
@@ -32,10 +26,10 @@ export class Fraction {
32
26
  return args[0];
33
27
  }
34
28
  else if (args.length === 2) {
35
- return _a.LCM(args[0], args[1]);
29
+ return Fraction.LCM(args[0], args[1]);
36
30
  }
37
31
  else {
38
- return _a.LCM(args.shift(), _a.LCMM(args));
32
+ return Fraction.LCM(args.shift(), Fraction.LCMM(args));
39
33
  }
40
34
  }
41
35
  constructor(numerator, denominator) {
@@ -54,7 +48,7 @@ export class Fraction {
54
48
  simplify() {
55
49
  let u = this.numerator;
56
50
  let d = this.denominator;
57
- const gcd = _a.GCD(u, d);
51
+ const gcd = Fraction.GCD(u, d);
58
52
  u /= gcd;
59
53
  d /= gcd;
60
54
  if (d < 0) {
@@ -65,7 +59,7 @@ export class Fraction {
65
59
  }
66
60
  add(param1 = 0, param2 = 1) {
67
61
  const [otherNumerator, otherDenominator] = getNumeratorAndDenominator(param1, param2);
68
- const lcm = _a.LCM(this.denominator, otherDenominator);
62
+ const lcm = Fraction.LCM(this.denominator, otherDenominator);
69
63
  const a = lcm / this.denominator;
70
64
  const b = lcm / otherDenominator;
71
65
  const u = this.numerator * a + otherNumerator * b;
@@ -73,7 +67,7 @@ export class Fraction {
73
67
  }
74
68
  subtract(param1 = 0, param2 = 1) {
75
69
  const [otherNumerator, otherDenominator] = getNumeratorAndDenominator(param1, param2);
76
- const lcm = _a.LCM(this.denominator, otherDenominator);
70
+ const lcm = Fraction.LCM(this.denominator, otherDenominator);
77
71
  const a = lcm / this.denominator;
78
72
  const b = lcm / otherDenominator;
79
73
  const u = this.numerator * a - otherNumerator * b;
@@ -88,17 +82,17 @@ export class Fraction {
88
82
  return this.set(this.numerator * otherDenominator, this.denominator * otherNumerator);
89
83
  }
90
84
  equals(compare) {
91
- const a = __classPrivateFieldGet(_a, _a, "f", _Fraction_fractionA).copy(compare).simplify();
92
- const b = __classPrivateFieldGet(_a, _a, "f", _Fraction_fractionB).copy(this).simplify();
85
+ const a = Fraction.fractionA.copy(compare).simplify();
86
+ const b = Fraction.fractionB.copy(this).simplify();
93
87
  return a.numerator === b.numerator && a.denominator === b.denominator;
94
88
  }
95
89
  greaterThan(compare) {
96
- const a = __classPrivateFieldGet(_a, _a, "f", _Fraction_fractionA).copy(this);
90
+ const a = Fraction.fractionA.copy(this);
97
91
  a.subtract(compare);
98
92
  return a.numerator > 0;
99
93
  }
100
94
  greaterThanEquals(compare) {
101
- const a = __classPrivateFieldGet(_a, _a, "f", _Fraction_fractionA).copy(this);
95
+ const a = Fraction.fractionA.copy(this);
102
96
  a.subtract(compare);
103
97
  return a.numerator >= 0;
104
98
  }
@@ -109,7 +103,7 @@ export class Fraction {
109
103
  return !this.greaterThan(compare);
110
104
  }
111
105
  clone() {
112
- return new _a(this.numerator, this.denominator);
106
+ return new Fraction(this.numerator, this.denominator);
113
107
  }
114
108
  copy(other) {
115
109
  if (typeof other === 'number') {
@@ -134,12 +128,12 @@ export class Fraction {
134
128
  return `${this.numerator}/${this.denominator}`;
135
129
  }
136
130
  toSimplifiedString() {
137
- return __classPrivateFieldGet(_a, _a, "f", _Fraction_fractionA).copy(this).simplify().toString();
131
+ return Fraction.fractionA.copy(this).simplify().toString();
138
132
  }
139
133
  toMixedString() {
140
134
  let s = '';
141
135
  const q = this.quotient();
142
- const f = __classPrivateFieldGet(_a, _a, "f", _Fraction_fractionA).copy(this);
136
+ const f = Fraction.fractionA.copy(this);
143
137
  if (q < 0) {
144
138
  f.makeAbs();
145
139
  }
@@ -164,9 +158,8 @@ export class Fraction {
164
158
  return this.set(n, d);
165
159
  }
166
160
  }
167
- _a = Fraction;
168
- _Fraction_fractionA = { value: new _a() };
169
- _Fraction_fractionB = { value: new _a() };
161
+ Fraction.fractionA = new Fraction();
162
+ Fraction.fractionB = new Fraction();
170
163
  function getNumeratorAndDenominator(n, d = 1) {
171
164
  if (typeof n === 'number') {
172
165
  return [n, d];
@@ -32,12 +32,11 @@ export class GlyphNote extends Note {
32
32
  const stave = this.checkStave();
33
33
  const ctx = stave.checkContext();
34
34
  this.setRendered();
35
- this.applyStyle(ctx);
36
35
  ctx.openGroup('glyphNote', this.getAttribute('id'));
37
- const x = this.isCenterAligned() ? this.getAbsoluteX() - this.getWidth() / 2 : this.getAbsoluteX();
38
- this.renderText(ctx, x, stave.getYForLine(this.options.line));
36
+ this.x = this.isCenterAligned() ? this.getAbsoluteX() - this.getWidth() / 2 : this.getAbsoluteX();
37
+ this.y = stave.getYForLine(this.options.line);
38
+ this.renderText(ctx, 0, 0);
39
39
  this.drawModifiers();
40
40
  ctx.closeGroup();
41
- this.restoreStyle(ctx);
42
41
  }
43
42
  }
@@ -66,14 +66,12 @@ export class GraceNote extends StaveNote {
66
66
  }
67
67
  }
68
68
  const ctx = this.checkContext();
69
- ctx.save();
70
69
  ctx.setLineWidth(1 * scale);
71
70
  ctx.beginPath();
72
71
  ctx.moveTo(slashBBox.x1, slashBBox.y1);
73
72
  ctx.lineTo(slashBBox.x2, slashBBox.y2);
74
73
  ctx.closePath();
75
74
  ctx.stroke();
76
- ctx.restore();
77
75
  }
78
76
  }
79
77
  calcBeamedNotesSlashBBox(slashStemOffset, slashBeamOffset, protrusions) {
@@ -23,7 +23,7 @@ export class GraceNoteGroup extends Modifier {
23
23
  return false;
24
24
  const groupList = [];
25
25
  let prevNote = null;
26
- let shiftL = 0;
26
+ let shift = 0;
27
27
  for (let i = 0; i < gracenoteGroups.length; ++i) {
28
28
  const gracenoteGroup = gracenoteGroups[i];
29
29
  const note = gracenoteGroup.getNote();
@@ -31,16 +31,22 @@ export class GraceNoteGroup extends Modifier {
31
31
  const spacing = isStavenote ? groupSpacingStave : groupSpacingTab;
32
32
  if (isStavenote && note !== prevNote) {
33
33
  for (let n = 0; n < note.keys.length; ++n) {
34
- shiftL = Math.max(note.getLeftDisplacedHeadPx(), shiftL);
34
+ shift = Math.max(note.getLeftDisplacedHeadPx(), shift);
35
35
  }
36
36
  prevNote = note;
37
37
  }
38
- groupList.push({ shift: shiftL, gracenoteGroup, spacing });
38
+ groupList.push({ shift: shift, gracenoteGroup, spacing });
39
39
  }
40
40
  let groupShift = groupList[0].shift;
41
41
  let formatWidth;
42
+ let right = false;
43
+ let left = false;
42
44
  for (let i = 0; i < groupList.length; ++i) {
43
45
  const gracenoteGroup = groupList[i].gracenoteGroup;
46
+ if (gracenoteGroup.position === Modifier.Position.RIGHT)
47
+ right = true;
48
+ else
49
+ left = true;
44
50
  gracenoteGroup.preFormat();
45
51
  formatWidth = gracenoteGroup.getWidth() + groupList[i].spacing;
46
52
  groupShift = Math.max(formatWidth, groupShift);
@@ -50,7 +56,10 @@ export class GraceNoteGroup extends Modifier {
50
56
  formatWidth = gracenoteGroup.getWidth() + groupList[i].spacing;
51
57
  gracenoteGroup.setSpacingFromNextModifier(groupShift - Math.min(formatWidth, groupShift) + StaveNote.minNoteheadPadding);
52
58
  }
53
- state.leftShift += groupShift;
59
+ if (right)
60
+ state.rightShift += groupShift;
61
+ if (left)
62
+ state.leftShift += groupShift;
54
63
  return true;
55
64
  }
56
65
  constructor(graceNotes, showSlur) {
@@ -108,9 +117,9 @@ export class GraceNoteGroup extends Modifier {
108
117
  const note = this.checkAttachedNote();
109
118
  this.setRendered();
110
119
  L('Drawing grace note group for:', note);
111
- this.alignSubNotesWithNote(this.getGraceNotes(), note);
112
- this.graceNotes.forEach((graceNote) => graceNote.setContext(ctx).draw());
113
- this.beams.forEach((beam) => beam.setContext(ctx).draw());
120
+ this.alignSubNotesWithNote(this.getGraceNotes(), note, this.position);
121
+ this.graceNotes.forEach((graceNote) => graceNote.setContext(ctx).drawWithStyle());
122
+ this.beams.forEach((beam) => beam.setContext(ctx).drawWithStyle());
114
123
  if (this.showSlur) {
115
124
  const isStavenote = isStaveNote(note);
116
125
  const TieClass = isStavenote ? StaveTie : TabTie;
@@ -122,7 +131,7 @@ export class GraceNoteGroup extends Modifier {
122
131
  });
123
132
  this.slur.renderOptions.cp2 = 12;
124
133
  this.slur.renderOptions.yShift = (isStavenote ? 7 : 5) + this.renderOptions.slurYShift;
125
- this.slur.setContext(ctx).draw();
134
+ this.slur.setContext(ctx).drawWithStyle();
126
135
  }
127
136
  }
128
137
  }
@@ -16,6 +16,7 @@ export * from './easyscore.js';
16
16
  export * from './element.js';
17
17
  export * from './factory.js';
18
18
  export * from './font.js';
19
+ export * from './flag.js';
19
20
  export * from './formatter.js';
20
21
  export * from './fraction.js';
21
22
  export * from './frethandfinger.js';
@@ -1,4 +1,6 @@
1
+ import { BoundingBox } from './boundingbox.js';
1
2
  import { Element } from './element.js';
3
+ import { Glyphs } from './glyphs.js';
2
4
  import { StaveModifier, StaveModifierPosition } from './stavemodifier.js';
3
5
  import { Tables } from './tables.js';
4
6
  import { defined } from './util.js';
@@ -11,20 +13,25 @@ export class KeySignature extends StaveModifier {
11
13
  this.accList = [];
12
14
  this.setKeySig(keySpec, cancelKeySpec, alterKeySpec);
13
15
  this.setPosition(StaveModifierPosition.BEGIN);
14
- this.glyphs = [];
16
+ this.children = [];
15
17
  this.paddingForced = false;
16
18
  }
17
19
  convertToGlyph(acc, nextAcc, stave) {
18
20
  const code = Tables.accidentalCodes(acc.type);
19
21
  const glyph = new Element("KeySignature");
20
22
  glyph.setText(code);
21
- const extraWidth = 1;
23
+ let extraWidth = 1;
22
24
  glyph.setYShift(stave.getYForLine(acc.line));
23
- if (this.glyphs.length > 0) {
24
- const prevGlyph = this.glyphs[this.glyphs.length - 1];
25
+ if (this.children.length > 0) {
26
+ const prevGlyph = this.children[this.children.length - 1];
27
+ const isNatural = (el) => el.getText() === Glyphs.accidentalNatural;
28
+ const yShiftDiff = (el1, el2) => Math.abs(el2.getYShift() - el1.getYShift());
29
+ if ((isNatural(prevGlyph) || isNatural(glyph)) && yShiftDiff(prevGlyph, glyph) < 10) {
30
+ extraWidth = 2;
31
+ }
25
32
  glyph.setXShift(prevGlyph.getXShift() + prevGlyph.getWidth() + extraWidth);
26
33
  }
27
- this.glyphs.push(glyph);
34
+ this.children.push(glyph);
28
35
  this.width += glyph.getWidth() + extraWidth;
29
36
  }
30
37
  cancelKey(spec) {
@@ -58,6 +65,26 @@ export class KeySignature extends StaveModifier {
58
65
  stave.addModifier(this);
59
66
  return this;
60
67
  }
68
+ getBoundingBox() {
69
+ if (!this.formatted)
70
+ this.format();
71
+ return super.getBoundingBox();
72
+ }
73
+ calculateDimensions() {
74
+ let boundingBox;
75
+ if (this.children.length > 0) {
76
+ boundingBox = this.children[0].getBoundingBox();
77
+ }
78
+ else {
79
+ boundingBox = new BoundingBox(this.x + this.xShift, this.y + this.yShift, 0, 0);
80
+ }
81
+ this.children.forEach((glyph) => {
82
+ boundingBox.mergeWith(glyph.getBoundingBox());
83
+ });
84
+ this.width = boundingBox.getW();
85
+ this.height = boundingBox.getH();
86
+ this.y = boundingBox.getY();
87
+ }
61
88
  convertAccLines(clef, type, accList = this.accList) {
62
89
  let offset = 0.0;
63
90
  let customLines;
@@ -112,11 +139,11 @@ export class KeySignature extends StaveModifier {
112
139
  getPadding(index) {
113
140
  if (!this.formatted)
114
141
  this.format();
115
- return this.glyphs.length === 0 || (!this.paddingForced && index < 2) ? 0 : this.padding;
142
+ return this.children.length === 0 || (!this.paddingForced && index < 2) ? 0 : this.padding;
116
143
  }
117
144
  getWidth() {
118
145
  if (!this.formatted)
119
- this.calculateWidth();
146
+ this.format();
120
147
  return this.width;
121
148
  }
122
149
  setKeySig(keySpec, cancelKeySpec, alterKeySpec) {
@@ -142,7 +169,7 @@ export class KeySignature extends StaveModifier {
142
169
  format() {
143
170
  const stave = this.checkStave();
144
171
  this.width = 0;
145
- this.glyphs = [];
172
+ this.children = [];
146
173
  this.accList = Tables.keySignature(defined(this.keySpec));
147
174
  const accList = this.accList;
148
175
  const firstAccidentalType = accList.length > 0 ? accList[0].type : undefined;
@@ -163,40 +190,20 @@ export class KeySignature extends StaveModifier {
163
190
  this.convertToGlyph(this.accList[i], this.accList[i + 1], stave);
164
191
  }
165
192
  }
193
+ this.calculateDimensions();
166
194
  this.formatted = true;
167
195
  }
168
- calculateWidth() {
169
- this.width = 0;
170
- this.accList = Tables.keySignature(defined(this.keySpec));
171
- if (this.cancelKeySpec) {
172
- this.convertToCancelAccList(this.cancelKeySpec);
173
- }
174
- if (this.alterKeySpec) {
175
- this.convertToAlterAccList(this.alterKeySpec);
176
- }
177
- if (this.accList.length > 0) {
178
- for (let i = 0; i < this.accList.length; ++i) {
179
- const code = Tables.accidentalCodes(this.accList[i].type);
180
- const glyph = new Element("KeySignature");
181
- glyph.setText(code);
182
- const extraWidth = 1;
183
- this.width += glyph.getWidth() + extraWidth;
184
- }
185
- }
186
- }
187
196
  draw() {
188
197
  const stave = this.checkStave();
189
198
  const ctx = stave.checkContext();
190
199
  if (!this.formatted)
191
200
  this.format();
192
201
  this.setRendered();
193
- this.applyStyle(ctx);
194
202
  ctx.openGroup('keysignature', this.getAttribute('id'));
195
- for (let i = 0; i < this.glyphs.length; i++) {
196
- const glyph = this.glyphs[i];
203
+ for (let i = 0; i < this.children.length; i++) {
204
+ const glyph = this.children[i];
197
205
  glyph.renderText(ctx, this.x, 0);
198
206
  }
199
207
  ctx.closeGroup();
200
- this.restoreStyle(ctx);
201
208
  }
202
209
  }
@@ -23,6 +23,6 @@ export class KeySigNote extends Note {
23
23
  this.setRendered();
24
24
  this.keySignature.setX(this.getAbsoluteX());
25
25
  this.keySignature.setContext(ctx);
26
- this.keySignature.draw();
26
+ this.keySignature.drawWithStyle();
27
27
  }
28
28
  }
@@ -1,11 +1,41 @@
1
1
  export class Metrics {
2
+ static clear(key) {
3
+ if (key) {
4
+ this.cacheFont.delete(key);
5
+ this.cacheStyle.delete(key);
6
+ }
7
+ else {
8
+ this.cacheFont.clear();
9
+ this.cacheStyle.clear();
10
+ }
11
+ }
2
12
  static getFontInfo(key) {
3
- return {
4
- family: Metrics.get(`${key}.fontFamily`),
5
- size: Metrics.get(`${key}.fontSize`) * Metrics.get(`${key}.fontScale`),
6
- weight: Metrics.get(`${key}.fontWeight`),
7
- style: Metrics.get(`${key}.fontStyle`),
8
- };
13
+ let font = this.cacheFont.get(key);
14
+ if (!font) {
15
+ font = {
16
+ family: Metrics.get(`${key}.fontFamily`),
17
+ size: Metrics.get(`${key}.fontSize`) * Metrics.get(`${key}.fontScale`),
18
+ weight: Metrics.get(`${key}.fontWeight`),
19
+ style: Metrics.get(`${key}.fontStyle`),
20
+ };
21
+ this.cacheFont.set(key, font);
22
+ }
23
+ return structuredClone(font);
24
+ }
25
+ static getStyle(key) {
26
+ let style = this.cacheStyle.get(key);
27
+ if (!style) {
28
+ style = {
29
+ fillStyle: Metrics.get(`${key}.fillStyle`),
30
+ strokeStyle: Metrics.get(`${key}.strokeStyle`),
31
+ lineWidth: Metrics.get(`${key}.lineWidth`),
32
+ lineDash: Metrics.get(`${key}.lineDash`),
33
+ shadowBlur: Metrics.get(`${key}.shadowBlur`),
34
+ shadowColor: Metrics.get(`${key}.shadowColor`),
35
+ };
36
+ this.cacheStyle.set(key, style);
37
+ }
38
+ return structuredClone(style);
9
39
  }
10
40
  static get(key, defaultValue) {
11
41
  var _a;
@@ -26,6 +56,8 @@ export class Metrics {
26
56
  return retVal;
27
57
  }
28
58
  }
59
+ Metrics.cacheStyle = new Map();
60
+ Metrics.cacheFont = new Map();
29
61
  export const MetricsDefaults = {
30
62
  fontFamily: 'Bravura,Academico',
31
63
  fontSize: 30,
@@ -37,7 +69,7 @@ export const MetricsDefaults = {
37
69
  fontSize: 20,
38
70
  },
39
71
  grace: {
40
- fontSize: 25,
72
+ fontSize: 20,
41
73
  },
42
74
  noteheadAccidentalPadding: 1,
43
75
  leftPadding: 2,
@@ -48,6 +80,10 @@ export const MetricsDefaults = {
48
80
  },
49
81
  Bend: {
50
82
  fontSize: 10,
83
+ line: {
84
+ strokeStyle: '#777777',
85
+ lineWidth: 1,
86
+ },
51
87
  },
52
88
  ChordSymbol: {
53
89
  fontSize: 12,
@@ -91,6 +127,7 @@ export const MetricsDefaults = {
91
127
  },
92
128
  },
93
129
  Stave: {
130
+ strokeStyle: '#999999',
94
131
  fontSize: 8,
95
132
  padding: 12,
96
133
  endPaddingMax: 10,
@@ -108,6 +145,9 @@ export const MetricsDefaults = {
108
145
  StaveSection: {
109
146
  fontSize: 10,
110
147
  fontWeight: 'bold',
148
+ lineWidth: 2,
149
+ padding: 2,
150
+ strokeStyle: 'black',
111
151
  },
112
152
  StaveTempo: {
113
153
  fontSize: 14,
@@ -124,6 +164,9 @@ export const MetricsDefaults = {
124
164
  StaveTie: {
125
165
  fontSize: 10,
126
166
  },
167
+ Stem: {
168
+ strokeStyle: 'black',
169
+ },
127
170
  StringNumber: {
128
171
  fontSize: 10,
129
172
  fontWeight: 'bold',
@@ -149,6 +192,10 @@ export const MetricsDefaults = {
149
192
  fontStyle: 'italic',
150
193
  fontWeight: 'bold',
151
194
  },
195
+ TabStave: {
196
+ strokeStyle: '#999999',
197
+ fontSize: 8,
198
+ },
152
199
  TabTie: {
153
200
  fontSize: 10,
154
201
  },
@@ -164,6 +211,10 @@ export const MetricsDefaults = {
164
211
  Tremolo: {
165
212
  spacing: 7,
166
213
  },
214
+ Tuplet: {
215
+ yOffset: 0,
216
+ textYOffset: 2,
217
+ },
167
218
  Volta: {
168
219
  fontSize: 9,
169
220
  fontWeight: 'bold',
@@ -1,15 +1,3 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _Modifier_spacingFromNextModifier, _Modifier_modifierContext;
13
1
  import { Element } from './element.js';
14
2
  import { defined, RuntimeError } from './util.js';
15
3
  export var ModifierPosition;
@@ -38,12 +26,10 @@ export class Modifier extends Element {
38
26
  }
39
27
  constructor() {
40
28
  super();
41
- _Modifier_spacingFromNextModifier.set(this, void 0);
42
- _Modifier_modifierContext.set(this, void 0);
43
29
  this.width = 0;
44
30
  this.textLine = 0;
45
31
  this.position = Modifier.Position.LEFT;
46
- __classPrivateFieldSet(this, _Modifier_spacingFromNextModifier, 0, "f");
32
+ this.spacingFromNextModifier = 0;
47
33
  }
48
34
  reset() {
49
35
  }
@@ -70,13 +56,13 @@ export class Modifier extends Element {
70
56
  return this;
71
57
  }
72
58
  getModifierContext() {
73
- return __classPrivateFieldGet(this, _Modifier_modifierContext, "f");
59
+ return this.modifierContext;
74
60
  }
75
61
  checkModifierContext() {
76
- return defined(__classPrivateFieldGet(this, _Modifier_modifierContext, "f"), 'NoModifierContext', 'Modifier Context Required');
62
+ return defined(this.modifierContext, 'NoModifierContext', 'Modifier Context Required');
77
63
  }
78
64
  setModifierContext(c) {
79
- __classPrivateFieldSet(this, _Modifier_modifierContext, c, "f");
65
+ this.modifierContext = c;
80
66
  return this;
81
67
  }
82
68
  getPosition() {
@@ -96,10 +82,10 @@ export class Modifier extends Element {
96
82
  return this;
97
83
  }
98
84
  setSpacingFromNextModifier(x) {
99
- __classPrivateFieldSet(this, _Modifier_spacingFromNextModifier, x, "f");
85
+ this.spacingFromNextModifier = x;
100
86
  }
101
87
  getSpacingFromNextModifier() {
102
- return __classPrivateFieldGet(this, _Modifier_spacingFromNextModifier, "f");
88
+ return this.spacingFromNextModifier;
103
89
  }
104
90
  setXShift(x) {
105
91
  this.xShift = 0;
@@ -118,11 +104,13 @@ export class Modifier extends Element {
118
104
  this.checkContext();
119
105
  throw new RuntimeError('NotImplemented', 'draw() not implemented for this modifier.');
120
106
  }
121
- alignSubNotesWithNote(subNotes, note) {
107
+ alignSubNotesWithNote(subNotes, note, position = Modifier.Position.LEFT) {
122
108
  const tickContext = note.getTickContext();
123
109
  const metrics = tickContext.getMetrics();
124
110
  const stave = note.getStave();
125
- const subNoteXOffset = tickContext.getX() - metrics.modLeftPx - metrics.modRightPx + this.getSpacingFromNextModifier();
111
+ const subNoteXOffset = position === Modifier.Position.RIGHT
112
+ ? tickContext.getX() + this.getSpacingFromNextModifier() * subNotes.length + 10
113
+ : tickContext.getX() - metrics.modLeftPx - metrics.modRightPx + this.getSpacingFromNextModifier();
126
114
  subNotes.forEach((subNote) => {
127
115
  const subTickContext = subNote.getTickContext();
128
116
  if (stave)
@@ -131,4 +119,3 @@ export class Modifier extends Element {
131
119
  });
132
120
  }
133
121
  }
134
- _Modifier_spacingFromNextModifier = new WeakMap(), _Modifier_modifierContext = new WeakMap();