vexflow 5.0.0-alpha.4 → 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 (122) 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 +1546 -1393
  7. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  8. package/build/cjs/vexflow-debug.js +600 -549
  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/annotation.js +0 -2
  13. package/build/esm/src/barnote.js +3 -3
  14. package/build/esm/src/beam.js +2 -4
  15. package/build/esm/src/bend.js +11 -3
  16. package/build/esm/src/chordsymbol.js +0 -4
  17. package/build/esm/src/clef.js +0 -2
  18. package/build/esm/src/curve.js +8 -5
  19. package/build/esm/src/element.js +11 -17
  20. package/build/esm/src/factory.js +4 -4
  21. package/build/esm/src/flag.js +20 -0
  22. package/build/esm/src/formatter.js +4 -4
  23. package/build/esm/src/glyphnote.js +0 -2
  24. package/build/esm/src/gracenote.js +0 -2
  25. package/build/esm/src/gracenotegroup.js +17 -8
  26. package/build/esm/src/index.js +1 -0
  27. package/build/esm/src/keysignature.js +38 -31
  28. package/build/esm/src/keysignote.js +1 -1
  29. package/build/esm/src/metrics.js +55 -7
  30. package/build/esm/src/modifier.js +4 -2
  31. package/build/esm/src/notehead.js +6 -1
  32. package/build/esm/src/ornament.js +0 -2
  33. package/build/esm/src/pedalmarking.js +33 -41
  34. package/build/esm/src/stave.js +10 -16
  35. package/build/esm/src/stavebarline.js +2 -3
  36. package/build/esm/src/stavehairpin.js +1 -1
  37. package/build/esm/src/staveline.js +0 -4
  38. package/build/esm/src/stavemodifier.js +0 -2
  39. package/build/esm/src/stavenote.js +16 -28
  40. package/build/esm/src/staverepetition.js +3 -2
  41. package/build/esm/src/stavesection.js +13 -15
  42. package/build/esm/src/stavetempo.js +3 -2
  43. package/build/esm/src/stavetext.js +2 -2
  44. package/build/esm/src/stavetie.js +0 -4
  45. package/build/esm/src/stavevolta.js +3 -2
  46. package/build/esm/src/stem.js +0 -4
  47. package/build/esm/src/stemmablenote.js +5 -6
  48. package/build/esm/src/stringnumber.js +0 -2
  49. package/build/esm/src/svgcontext.js +0 -1
  50. package/build/esm/src/tabnote.js +2 -8
  51. package/build/esm/src/textbracket.js +0 -3
  52. package/build/esm/src/textnote.js +0 -2
  53. package/build/esm/src/timesignature.js +2 -4
  54. package/build/esm/src/timesignote.js +2 -0
  55. package/build/esm/src/tuplet.js +37 -30
  56. package/build/esm/src/version.js +3 -3
  57. package/build/esm/src/vexflow.js +6 -2
  58. package/build/esm/tests/accidental_tests.js +30 -17
  59. package/build/esm/tests/annotation_tests.js +12 -12
  60. package/build/esm/tests/articulation_tests.js +13 -13
  61. package/build/esm/tests/auto_beam_formatting_tests.js +33 -33
  62. package/build/esm/tests/beam_tests.js +1 -1
  63. package/build/esm/tests/bend_tests.js +7 -7
  64. package/build/esm/tests/chordsymbol_tests.js +8 -8
  65. package/build/esm/tests/curve_tests.js +34 -0
  66. package/build/esm/tests/{bach_tests.js → demo_petzold_tests.js} +7 -7
  67. package/build/esm/tests/dot_tests.js +4 -4
  68. package/build/esm/tests/formatter_tests.js +25 -25
  69. package/build/esm/tests/gracenote_tests.js +2 -1
  70. package/build/esm/tests/gracetabnote_tests.js +1 -1
  71. package/build/esm/tests/index.js +1 -1
  72. package/build/esm/tests/key_clef_tests.js +6 -6
  73. package/build/esm/tests/keysignature_tests.js +20 -17
  74. package/build/esm/tests/notehead_tests.js +10 -7
  75. package/build/esm/tests/offscreencanvas_tests.js +1 -1
  76. package/build/esm/tests/ornament_tests.js +8 -8
  77. package/build/esm/tests/pedalmarking_tests.js +27 -0
  78. package/build/esm/tests/percussion_tests.js +6 -3
  79. package/build/esm/tests/renderer_tests.js +1 -1
  80. package/build/esm/tests/rests_tests.js +26 -26
  81. package/build/esm/tests/rhythm_tests.js +15 -15
  82. package/build/esm/tests/stave_tests.js +44 -43
  83. package/build/esm/tests/staveconnector_tests.js +73 -73
  84. package/build/esm/tests/stavehairpin_tests.js +1 -1
  85. package/build/esm/tests/stavemodifier_tests.js +3 -3
  86. package/build/esm/tests/stavenote_tests.js +29 -29
  87. package/build/esm/tests/stringnumber_tests.js +1 -1
  88. package/build/esm/tests/strokes_tests.js +1 -1
  89. package/build/esm/tests/style_tests.js +1 -1
  90. package/build/esm/tests/tabnote_tests.js +7 -7
  91. package/build/esm/tests/tabslide_tests.js +6 -6
  92. package/build/esm/tests/tabstave_tests.js +1 -1
  93. package/build/esm/tests/tabtie_tests.js +8 -8
  94. package/build/esm/tests/textnote_tests.js +1 -1
  95. package/build/esm/tests/threevoice_tests.js +2 -2
  96. package/build/esm/tests/timesignature_tests.js +23 -13
  97. package/build/esm/tests/vibrato_tests.js +3 -3
  98. package/build/esm/tests/voice_tests.js +1 -1
  99. package/build/types/src/bend.d.ts +6 -0
  100. package/build/types/src/curve.d.ts +5 -4
  101. package/build/types/src/element.d.ts +10 -11
  102. package/build/types/src/factory.d.ts +2 -2
  103. package/build/types/src/flag.d.ts +12 -0
  104. package/build/types/src/frethandfinger.d.ts +1 -1
  105. package/build/types/src/index.d.ts +1 -0
  106. package/build/types/src/keysignature.d.ts +7 -7
  107. package/build/types/src/metrics.d.ts +5 -0
  108. package/build/types/src/modifier.d.ts +1 -1
  109. package/build/types/src/pedalmarking.d.ts +2 -2
  110. package/build/types/src/stave.d.ts +1 -3
  111. package/build/types/src/stavebarline.d.ts +1 -1
  112. package/build/types/src/staveline.d.ts +1 -1
  113. package/build/types/src/stavemodifier.d.ts +0 -1
  114. package/build/types/src/staverepetition.d.ts +1 -1
  115. package/build/types/src/stavesection.d.ts +3 -4
  116. package/build/types/src/stavetempo.d.ts +1 -2
  117. package/build/types/src/stavetext.d.ts +1 -2
  118. package/build/types/src/stavevolta.d.ts +1 -2
  119. package/build/types/src/stemmablenote.d.ts +2 -3
  120. package/build/types/src/tuplet.d.ts +2 -12
  121. package/build/types/src/typeguard.d.ts +1 -0
  122. package/package.json +32 -28
@@ -153,7 +153,6 @@ export class Annotation extends Modifier {
153
153
  const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
154
154
  const start = note.getModifierStartXY(ModifierPosition.ABOVE, this.index);
155
155
  this.setRendered();
156
- this.applyStyle();
157
156
  ctx.openGroup('annotation', this.getAttribute('id'));
158
157
  const textWidth = this.getWidth();
159
158
  const textHeight = Font.convertSizeToPixelValue(this.fontInfo.size);
@@ -209,7 +208,6 @@ export class Annotation extends Modifier {
209
208
  this.y = y;
210
209
  this.renderText(ctx, 0, 0);
211
210
  ctx.closeGroup();
212
- this.restoreStyle();
213
211
  }
214
212
  }
215
213
  Annotation.DEBUG = false;
@@ -45,11 +45,11 @@ export class BarNote extends Note {
45
45
  draw() {
46
46
  const ctx = this.checkContext();
47
47
  L('Rendering bar line at: ', this.getAbsoluteX());
48
- this.applyStyle(ctx);
49
48
  const barline = new Barline(this.type);
50
49
  barline.setX(this.getAbsoluteX());
51
- barline.draw(this.checkStave());
52
- this.restoreStyle(ctx);
50
+ barline.setStave(this.checkStave());
51
+ barline.setContext(ctx);
52
+ barline.drawWithStyle();
53
53
  this.setRendered();
54
54
  }
55
55
  }
@@ -576,7 +576,7 @@ export class Beam extends Element {
576
576
  if (stem) {
577
577
  const stemX = note.getStemX();
578
578
  stem.setNoteHeadXBounds(stemX, stemX);
579
- stem.setContext(ctx).draw();
579
+ stem.setContext(ctx).drawWithStyle();
580
580
  }
581
581
  }, this);
582
582
  }
@@ -634,11 +634,9 @@ export class Beam extends Element {
634
634
  if (!this.postFormatted) {
635
635
  this.postFormat();
636
636
  }
637
- this.drawStems(ctx);
638
- this.applyStyle();
639
637
  ctx.openGroup('beam', this.getAttribute('id'));
638
+ this.drawStems(ctx);
640
639
  this.drawBeamLines(ctx);
641
640
  ctx.closeGroup();
642
- this.restoreStyle();
643
641
  }
644
642
  }
@@ -1,4 +1,5 @@
1
1
  import { Element } from './element.js';
2
+ import { Metrics } from './metrics.js';
2
3
  import { Modifier } from './modifier.js';
3
4
  import { isTabNote } from './typeguard.js';
4
5
  import { RuntimeError } from './util.js';
@@ -33,15 +34,22 @@ export class Bend extends Modifier {
33
34
  state.topTextLine += 1;
34
35
  return true;
35
36
  }
37
+ setStyleLine(style) {
38
+ this.styleLine = style;
39
+ return this;
40
+ }
41
+ getStyleLine() {
42
+ return this.styleLine;
43
+ }
36
44
  constructor(phrase) {
37
45
  super();
46
+ this.styleLine = Metrics.getStyle('Bend.line');
38
47
  this.xShift = 0;
39
48
  this.tap = '';
40
49
  this.renderOptions = {
41
50
  bendWidth: 8,
42
51
  releaseWidth: 8,
43
52
  };
44
- this.setStyle({ lineWidth: 1.0, strokeStyle: 'black', fillStyle: 'black' });
45
53
  this.phrase = phrase;
46
54
  this.updateWidth();
47
55
  }
@@ -86,7 +94,6 @@ export class Bend extends Modifier {
86
94
  const ctx = this.checkContext();
87
95
  const note = this.checkAttachedNote();
88
96
  this.setRendered();
89
- this.applyStyle();
90
97
  const start = note.getModifierStartXY(Modifier.Position.RIGHT, this.index);
91
98
  start.x += 3;
92
99
  start.y += 0.5;
@@ -99,12 +106,14 @@ export class Bend extends Modifier {
99
106
  const renderBend = (x, y, width, height) => {
100
107
  const cpX = x + width;
101
108
  const cpY = y;
109
+ this.applyStyle(ctx, this.styleLine);
102
110
  ctx.beginPath();
103
111
  ctx.moveTo(x, y);
104
112
  ctx.quadraticCurveTo(cpX, cpY, x + width, height);
105
113
  ctx.stroke();
106
114
  };
107
115
  const renderRelease = (x, y, width, height) => {
116
+ this.applyStyle(ctx, this.styleLine);
108
117
  ctx.beginPath();
109
118
  ctx.moveTo(x, height);
110
119
  ctx.quadraticCurveTo(x + width, height, x + width, y);
@@ -173,6 +182,5 @@ export class Bend extends Modifier {
173
182
  else if (lastBend.type === Bend.DOWN) {
174
183
  renderArrowHead(lastBend.x + lastDrawnWidth, start.y, -1);
175
184
  }
176
- this.restoreStyle();
177
185
  }
178
186
  }
@@ -229,8 +229,6 @@ export class ChordSymbol extends Modifier {
229
229
  const ctx = this.checkContext();
230
230
  const note = this.checkAttachedNote();
231
231
  this.setRendered();
232
- ctx.save();
233
- this.applyStyle();
234
232
  ctx.openGroup('chordsymbol', this.getAttribute('id'));
235
233
  const start = note.getModifierStartXY(Modifier.Position.ABOVE, this.index);
236
234
  ctx.setFont(this.fontInfo);
@@ -276,8 +274,6 @@ export class ChordSymbol extends Modifier {
276
274
  symbol.renderText(ctx, 0, 0);
277
275
  });
278
276
  ctx.closeGroup();
279
- this.restoreStyle();
280
- ctx.restore();
281
277
  }
282
278
  getBoundingBox() {
283
279
  const boundingBox = this.symbolBlocks[0].getBoundingBox();
@@ -109,12 +109,10 @@ export class Clef extends StaveModifier {
109
109
  const stave = this.checkStave();
110
110
  const ctx = stave.checkContext();
111
111
  this.setRendered();
112
- this.applyStyle(ctx);
113
112
  ctx.openGroup('clef', this.getAttribute('id'));
114
113
  this.y = stave.getYForLine(this.line);
115
114
  this.renderText(ctx, 0, 0);
116
115
  ctx.closeGroup();
117
- this.restoreStyle(ctx);
118
116
  }
119
117
  }
120
118
  Clef.DEBUG = false;
@@ -23,9 +23,8 @@ export class Curve extends Element {
23
23
  this.renderOptions = Object.assign({ thickness: 2, xShift: 0, yShift: 10, position: CurvePosition.NEAR_HEAD, positionEnd: CurvePosition.NEAR_HEAD, invert: false, cps: [
24
24
  { x: 0, y: 10 },
25
25
  { x: 0, y: 10 },
26
- ] }, options);
27
- this.from = from;
28
- this.to = to;
26
+ ], openingDirection: 'auto' }, options);
27
+ this.setNotes(from, to);
29
28
  }
30
29
  setNotes(from, to) {
31
30
  if (!from && !to) {
@@ -41,7 +40,6 @@ export class Curve extends Element {
41
40
  renderCurve(params) {
42
41
  var _a, _b;
43
42
  const ctx = this.checkContext();
44
- this.applyStyle();
45
43
  const xShift = this.renderOptions.xShift;
46
44
  const yShift = this.renderOptions.yShift * params.direction;
47
45
  const firstX = params.firstX + xShift;
@@ -62,7 +60,6 @@ export class Curve extends Element {
62
60
  ctx.closePath();
63
61
  if (!((_b = this.style) === null || _b === void 0 ? void 0 : _b.lineDash))
64
62
  ctx.fill();
65
- this.restoreStyle();
66
63
  }
67
64
  draw() {
68
65
  this.checkContext();
@@ -111,6 +108,12 @@ export class Curve extends Element {
111
108
  lastX = stave.getTieEndX();
112
109
  lastY = firstNote.getStemExtents()[endMetric];
113
110
  }
111
+ if (this.renderOptions.openingDirection === 'up') {
112
+ stemDirection = 1;
113
+ }
114
+ if (this.renderOptions.openingDirection === 'down') {
115
+ stemDirection = -1;
116
+ }
114
117
  this.renderCurve({
115
118
  firstX,
116
119
  lastX,
@@ -29,6 +29,7 @@ export class Element {
29
29
  constructor(category) {
30
30
  var _a;
31
31
  this.children = [];
32
+ this.style = {};
32
33
  this._text = '';
33
34
  this.metricsValid = false;
34
35
  this._textMetrics = {
@@ -58,10 +59,14 @@ export class Element {
58
59
  };
59
60
  this.rendered = false;
60
61
  this._fontInfo = Metrics.getFontInfo(this.attrs.type);
62
+ this.style = Metrics.getStyle(this.attrs.type);
61
63
  this.fontScale = Metrics.get(`${this.attrs.type}.fontScale`);
62
64
  (_a = Registry.getDefaultRegistry()) === null || _a === void 0 ? void 0 : _a.register(this);
63
65
  }
64
- addChildElement(child) {
66
+ addChild(child) {
67
+ if (child.parent)
68
+ throw new RuntimeError('Element', 'Parent already defined');
69
+ child.parent = this;
65
70
  this.children.push(child);
66
71
  return this;
67
72
  }
@@ -81,11 +86,8 @@ export class Element {
81
86
  return this.style;
82
87
  }
83
88
  applyStyle(context = this.context, style = this.getStyle()) {
84
- if (!style)
85
- return this;
86
89
  if (!context)
87
90
  return this;
88
- context.save();
89
91
  if (style.shadowColor)
90
92
  context.setShadowColor(style.shadowColor);
91
93
  if (style.shadowBlur)
@@ -100,19 +102,13 @@ export class Element {
100
102
  context.setLineDash(style.lineDash.split(' ').map(Number));
101
103
  return this;
102
104
  }
103
- restoreStyle(context = this.context, style = this.getStyle()) {
104
- if (!style)
105
- return this;
106
- if (!context)
107
- return this;
108
- context.restore();
109
- return this;
110
- }
111
105
  drawWithStyle() {
112
- this.checkContext();
113
- this.applyStyle();
106
+ const ctx = this.checkContext();
107
+ ctx.save();
108
+ this.applyStyle(ctx);
114
109
  this.draw();
115
- this.restoreStyle();
110
+ ctx.restore();
111
+ return this;
116
112
  }
117
113
  draw() {
118
114
  throw new RuntimeError('Element', 'Draw not defined');
@@ -333,14 +329,12 @@ export class Element {
333
329
  return this._text;
334
330
  }
335
331
  renderText(ctx, xPos, yPos) {
336
- ctx.save();
337
332
  ctx.setFont(this._fontInfo);
338
333
  ctx.fillText(this._text, xPos + this.x + this.xShift, yPos + this.y + this.yShift);
339
334
  this.children.forEach((child) => {
340
335
  ctx.setFont(child.fontInfo);
341
336
  ctx.fillText(child.text, xPos + child.x + child.xShift, yPos + child.y + child.yShift);
342
337
  });
343
- ctx.restore();
344
338
  }
345
339
  measureText() {
346
340
  var _a;
@@ -450,13 +450,13 @@ export class Factory {
450
450
  draw() {
451
451
  const ctx = this.context;
452
452
  this.systems.forEach((s) => s.setContext(ctx).format());
453
- this.staves.forEach((s) => s.setContext(ctx).draw());
454
- this.voices.forEach((v) => v.setContext(ctx).draw());
453
+ this.staves.forEach((s) => s.setContext(ctx).drawWithStyle());
454
+ this.voices.forEach((v) => v.setContext(ctx).drawWithStyle());
455
455
  this.renderQ.forEach((e) => {
456
456
  if (!e.isRendered())
457
- e.setContext(ctx).draw();
457
+ e.setContext(ctx).drawWithStyle();
458
458
  });
459
- this.systems.forEach((s) => s.setContext(ctx).draw());
459
+ this.systems.forEach((s) => s.setContext(ctx).drawWithStyle());
460
460
  this.reset();
461
461
  }
462
462
  }
@@ -0,0 +1,20 @@
1
+ import { Element } from './element.js';
2
+ import { log } from './util.js';
3
+ function L(...args) {
4
+ if (Flag.DEBUG)
5
+ log('VexFlow.Flag', args);
6
+ }
7
+ export class Flag extends Element {
8
+ static get CATEGORY() {
9
+ return "Flag";
10
+ }
11
+ draw() {
12
+ const ctx = this.checkContext();
13
+ this.setRendered();
14
+ ctx.openGroup('flag', this.getAttribute('id'));
15
+ L("Drawing flag '", this.text, "' at", this.x, this.y);
16
+ this.renderText(ctx, 0, 0);
17
+ ctx.closeGroup();
18
+ }
19
+ }
20
+ Flag.DEBUG = false;
@@ -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) => {
@@ -32,13 +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
36
  this.x = this.isCenterAligned() ? this.getAbsoluteX() - this.getWidth() / 2 : this.getAbsoluteX();
38
37
  this.y = stave.getYForLine(this.options.line);
39
38
  this.renderText(ctx, 0, 0);
40
39
  this.drawModifiers();
41
40
  ctx.closeGroup();
42
- this.restoreStyle(ctx);
43
41
  }
44
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
  }