vexflow 5.2.0 → 5.4.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 (189) hide show
  1. package/README.md +2 -0
  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 +6502 -1708
  7. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  8. package/build/cjs/vexflow-debug.js +5622 -1618
  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 +10 -3
  13. package/build/esm/src/alternatingtremolo.js +68 -0
  14. package/build/esm/src/annotation.js +19 -3
  15. package/build/esm/src/articulation.js +28 -6
  16. package/build/esm/src/beam.js +171 -28
  17. package/build/esm/src/bend.js +79 -23
  18. package/build/esm/src/clef.js +3 -0
  19. package/build/esm/src/continuouslayout.js +681 -0
  20. package/build/esm/src/continuousspan.js +159 -0
  21. package/build/esm/src/curve.js +48 -55
  22. package/build/esm/src/dot.js +13 -4
  23. package/build/esm/src/easyscore/composition.js +52 -2
  24. package/build/esm/src/easyscore/policies.js +39 -8
  25. package/build/esm/src/element.js +4 -0
  26. package/build/esm/src/engravinggeometry.js +157 -0
  27. package/build/esm/src/engravingprofile.js +44 -0
  28. package/build/esm/src/factory.js +31 -11
  29. package/build/esm/src/font.js +9 -1
  30. package/build/esm/src/formatter.js +9 -1
  31. package/build/esm/src/fretdiagram.js +77 -0
  32. package/build/esm/src/gracenote.js +43 -38
  33. package/build/esm/src/gracenotegroup.js +53 -56
  34. package/build/esm/src/guitargesture.js +33 -0
  35. package/build/esm/src/guitarmodifiers.js +26 -0
  36. package/build/esm/src/index.js +10 -0
  37. package/build/esm/src/instrumentprofile.js +123 -0
  38. package/build/esm/src/keysignature.js +24 -2
  39. package/build/esm/src/modifiercontext.js +11 -0
  40. package/build/esm/src/notationspelling.js +73 -0
  41. package/build/esm/src/note.js +3 -0
  42. package/build/esm/src/notehead.js +11 -6
  43. package/build/esm/src/ornament.js +43 -18
  44. package/build/esm/src/pagebreaks.js +58 -0
  45. package/build/esm/src/pedalmarking.js +39 -17
  46. package/build/esm/src/phraselayout.js +503 -0
  47. package/build/esm/src/phraseribbon.js +28 -0
  48. package/build/esm/src/proportionalformatter.js +54 -6
  49. package/build/esm/src/publication.js +402 -0
  50. package/build/esm/src/publicationview.js +114 -0
  51. package/build/esm/src/scorenavigation.js +172 -0
  52. package/build/esm/src/sheet.js +14 -2
  53. package/build/esm/src/stave.js +33 -16
  54. package/build/esm/src/stavebarline.js +5 -7
  55. package/build/esm/src/staveconnector.js +1 -0
  56. package/build/esm/src/stavehairpin.js +53 -27
  57. package/build/esm/src/stavenote.js +134 -116
  58. package/build/esm/src/stavetie.js +66 -9
  59. package/build/esm/src/stem.js +26 -11
  60. package/build/esm/src/strokes.js +4 -0
  61. package/build/esm/src/system.js +19 -4
  62. package/build/esm/src/systembreaks.js +42 -0
  63. package/build/esm/src/tabnote.js +6 -1
  64. package/build/esm/src/textbracket.js +24 -5
  65. package/build/esm/src/tickcontext.js +19 -0
  66. package/build/esm/src/timesignature.js +20 -6
  67. package/build/esm/src/tuplet.js +58 -17
  68. package/build/esm/src/version.js +3 -3
  69. package/build/esm/src/vexflow.js +20 -0
  70. package/build/esm/src/voice.js +7 -3
  71. package/build/esm/tests/bend_tests.js +14 -1
  72. package/build/esm/tests/curve_tests.js +14 -4
  73. package/build/esm/tests/demo_repertoire_tests.js +27 -0
  74. package/build/esm/tests/demos/drums.js +54 -0
  75. package/build/esm/tests/demos/electric.js +63 -0
  76. package/build/esm/tests/demos/greensleeves.js +90 -0
  77. package/build/esm/tests/demos/joy.js +61 -0
  78. package/build/esm/tests/demos/kesh.js +80 -0
  79. package/build/esm/tests/demos/prelude.js +59 -0
  80. package/build/esm/tests/demos/quartertones.js +50 -0
  81. package/build/esm/tests/demos/saints.js +65 -0
  82. package/build/esm/tests/engraving/continuous-studies.mjs +140 -0
  83. package/build/esm/tests/engraving/instrument-studies.mjs +273 -0
  84. package/build/esm/tests/engraving/phrase-studies.mjs +63 -0
  85. package/build/esm/tests/engraving/publication-studies.mjs +87 -0
  86. package/build/esm/tests/engraving/score-layout.mjs +75 -0
  87. package/build/esm/tests/engraving/score-persistence.mjs +262 -0
  88. package/build/esm/tests/engraving/specialist-studies.mjs +347 -0
  89. package/build/esm/tests/engraving/studies.mjs +367 -0
  90. package/build/esm/tests/formatter_tests.js +10 -1
  91. package/build/esm/tests/index.js +1 -0
  92. package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
  93. package/build/esm/tests/node/continuous-score-tests.mjs +282 -0
  94. package/build/esm/tests/node/easyscore-tests.mjs +71 -0
  95. package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
  96. package/build/esm/tests/node/instrument-profile-tests.mjs +224 -0
  97. package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
  98. package/build/esm/tests/node/phrase-layout-tests.mjs +281 -0
  99. package/build/esm/tests/node/proportional-spacing-tests.mjs +64 -0
  100. package/build/esm/tests/node/publication-tests.mjs +219 -0
  101. package/build/esm/tests/node/rendering-tests.mjs +6 -2
  102. package/build/esm/tests/threevoice_tests.js +11 -1
  103. package/build/esm/tests/vexflow_test_helpers.js +19 -10
  104. package/build/esm/tools/audit-dependencies.mjs +61 -0
  105. package/build/esm/tools/easyscore-structure.mjs +1 -1
  106. package/build/esm/tools/explorer/catalog/entries.mjs +2 -1
  107. package/build/esm/tools/explorer/catalog/expression.mjs +14 -9
  108. package/build/esm/tools/explorer/catalog/guitar.mjs +238 -0
  109. package/build/esm/tools/explorer/catalog/layout.mjs +8 -6
  110. package/build/esm/tools/explorer/catalog/notes.mjs +16 -14
  111. package/build/esm/tools/explorer/catalog/staves.mjs +12 -6
  112. package/build/esm/tools/explorer/catalog/strings.mjs +10 -7
  113. package/build/esm/tools/score-fixtures.mjs +23 -6
  114. package/build/types/src/accidental.d.ts +4 -0
  115. package/build/types/src/alternatingtremolo.d.ts +13 -0
  116. package/build/types/src/annotation.d.ts +9 -0
  117. package/build/types/src/articulation.d.ts +9 -1
  118. package/build/types/src/beam.d.ts +16 -0
  119. package/build/types/src/bend.d.ts +8 -0
  120. package/build/types/src/clef.d.ts +3 -0
  121. package/build/types/src/continuouslayout.d.ts +103 -0
  122. package/build/types/src/continuousspan.d.ts +60 -0
  123. package/build/types/src/curve.d.ts +20 -0
  124. package/build/types/src/dot.d.ts +5 -0
  125. package/build/types/src/easyscore/composition.d.ts +27 -0
  126. package/build/types/src/easyscore/policies.d.ts +1 -1
  127. package/build/types/src/element.d.ts +2 -0
  128. package/build/types/src/engravinggeometry.d.ts +31 -0
  129. package/build/types/src/engravingprofile.d.ts +20 -0
  130. package/build/types/src/factory.d.ts +6 -1
  131. package/build/types/src/formatter.d.ts +3 -3
  132. package/build/types/src/fretdiagram.d.ts +23 -0
  133. package/build/types/src/gracenote.d.ts +7 -0
  134. package/build/types/src/gracenotegroup.d.ts +7 -2
  135. package/build/types/src/guitargesture.d.ts +26 -0
  136. package/build/types/src/guitarmodifiers.d.ts +3 -0
  137. package/build/types/src/index.d.ts +12 -0
  138. package/build/types/src/instrumentprofile.d.ts +124 -0
  139. package/build/types/src/keysignature.d.ts +9 -0
  140. package/build/types/src/modifiercontext.d.ts +4 -0
  141. package/build/types/src/notationspelling.d.ts +25 -0
  142. package/build/types/src/note.d.ts +15 -0
  143. package/build/types/src/notehead.d.ts +5 -0
  144. package/build/types/src/ornament.d.ts +10 -1
  145. package/build/types/src/pagebreaks.d.ts +26 -0
  146. package/build/types/src/pedalmarking.d.ts +9 -0
  147. package/build/types/src/phraselayout.d.ts +87 -0
  148. package/build/types/src/phraseribbon.d.ts +7 -0
  149. package/build/types/src/proportionalformatter.d.ts +11 -2
  150. package/build/types/src/publication.d.ts +61 -0
  151. package/build/types/src/publicationview.d.ts +27 -0
  152. package/build/types/src/scorenavigation.d.ts +33 -0
  153. package/build/types/src/sheet.d.ts +5 -0
  154. package/build/types/src/stave.d.ts +13 -0
  155. package/build/types/src/stavehairpin.d.ts +18 -0
  156. package/build/types/src/stavenote.d.ts +20 -2
  157. package/build/types/src/stavetie.d.ts +9 -0
  158. package/build/types/src/stem.d.ts +10 -1
  159. package/build/types/src/system.d.ts +8 -1
  160. package/build/types/src/systembreaks.d.ts +9 -0
  161. package/build/types/src/textbracket.d.ts +7 -0
  162. package/build/types/src/tickcontext.d.ts +5 -0
  163. package/build/types/src/timesignature.d.ts +6 -0
  164. package/build/types/src/tuplet.d.ts +5 -48
  165. package/build/types/src/vexflow.d.ts +20 -0
  166. package/build/types/src/voice.d.ts +2 -0
  167. package/changelog/CHANGELOG.md +25 -0
  168. package/package.json +15 -3
  169. package/tests/all.html +277 -0
  170. package/tests/demo_petzold_tests.ts +435 -0
  171. package/tests/demos/drums.ts +55 -0
  172. package/tests/demos/drums.webp +0 -0
  173. package/tests/demos/electric.ts +70 -0
  174. package/tests/demos/electric.webp +0 -0
  175. package/tests/demos/greensleeves.ts +97 -0
  176. package/tests/demos/greensleeves.webp +0 -0
  177. package/tests/demos/joy.ts +65 -0
  178. package/tests/demos/joy.webp +0 -0
  179. package/tests/demos/kesh.ts +86 -0
  180. package/tests/demos/kesh.webp +0 -0
  181. package/tests/demos/prelude.ts +65 -0
  182. package/tests/demos/prelude.webp +0 -0
  183. package/tests/demos/quartertones.ts +51 -0
  184. package/tests/demos/quartertones.webp +0 -0
  185. package/tests/demos/saints.ts +70 -0
  186. package/tests/demos/saints.webp +0 -0
  187. package/tests/flow.html +550 -239
  188. package/tests/fonts.css +493 -0
  189. package/tests/minuet.webp +0 -0
@@ -319,15 +319,22 @@ export class Accidental extends Modifier {
319
319
  isCautionary() {
320
320
  return this.cautionary;
321
321
  }
322
- draw() {
322
+ positionGlyph() {
323
323
  const { type, position, index } = this;
324
- const ctx = this.checkContext();
325
324
  const note = this.checkAttachedNote();
326
- this.setRendered();
327
325
  const start = note.getModifierStartXY(position, index);
328
326
  this.x = start.x - this.width;
329
327
  this.y = start.y;
330
328
  L('Rendering: ', type, start.x, start.y);
329
+ }
330
+ getInkBounds() {
331
+ this.positionGlyph();
332
+ return this.getTextInk(this.x, this.y);
333
+ }
334
+ draw() {
335
+ const ctx = this.checkContext();
336
+ this.positionGlyph();
337
+ this.setRendered();
331
338
  this.renderText(ctx, 0, 0);
332
339
  }
333
340
  }
@@ -0,0 +1,68 @@
1
+ import { Element } from './element.js';
2
+ import { Fraction } from './fraction.js';
3
+ import { Tables } from './tables.js';
4
+ import { RuntimeError } from './util.js';
5
+ export class AlternatingTremolo extends Element {
6
+ constructor(first, last, strokes) {
7
+ super();
8
+ this.first = first;
9
+ this.last = last;
10
+ this.strokes = strokes;
11
+ if (!Number.isInteger(strokes) || strokes < 1 || strokes > 3)
12
+ throw new RuntimeError('BadArguments', 'Expected one to three tremolo strokes.');
13
+ }
14
+ static endpoint(note, strokes) {
15
+ var _a;
16
+ const denominator = Number(Tables.sanitizeDuration(note.duration));
17
+ if (![2, 4].includes(denominator) ||
18
+ note.dots ||
19
+ note.durationOverride ||
20
+ note.type === 'r' ||
21
+ !Number.isInteger(strokes) ||
22
+ strokes < 1 ||
23
+ strokes > 3)
24
+ throw new RuntimeError('BadArguments', 'Expected an undotted half or quarter tremolo endpoint.');
25
+ return Object.assign(Object.assign({}, note), { durationOverride: new Fraction(1, denominator * 2), notation: [
26
+ ...((_a = note.notation) !== null && _a !== void 0 ? _a : []),
27
+ {
28
+ kind: 'tremolo',
29
+ id: 'alternating-' + strokes,
30
+ label: 'Alternating tremolo, ' + strokes + (strokes === 1 ? ' stroke' : ' strokes'),
31
+ },
32
+ ] });
33
+ }
34
+ draw() {
35
+ const { first, last, strokes } = this;
36
+ const voice = first.getVoice();
37
+ const notes = voice.getTickables();
38
+ if (first.checkStave() !== last.checkStave() ||
39
+ last.getVoice() !== voice ||
40
+ notes[notes.indexOf(first) + 1] !== last ||
41
+ first.getDuration() !== last.getDuration() ||
42
+ first.getStemDirection() !== last.getStemDirection() ||
43
+ ![first, last].every((n) => n.getIntrinsicTicks() === Tables.durationToTicks(n.getDuration()) / 2 &&
44
+ n.notation.some((entry) => entry.kind === 'tremolo' && entry.id === 'alternating-' + strokes)))
45
+ throw new RuntimeError('BadArguments', 'Expected adjacent matching tremolo endpoints with parallel stems on one staff.');
46
+ const ctx = this.checkContext();
47
+ const spacing = first.checkStave().getSpacingBetweenLines();
48
+ const x1 = first.getStemX(), x2 = last.getStemX();
49
+ const inset = Math.min(spacing * 0.4, (x2 - x1) / 5);
50
+ const direction = first.getStemDirection();
51
+ const y1 = first.getStemExtents().topY, y2 = last.getStemExtents().topY;
52
+ const slopeInset = ((y2 - y1) * inset) / (x2 - x1);
53
+ ctx.openGroup('alternating-tremolo', this.getAttribute('id'));
54
+ for (let i = 0; i < strokes; i++) {
55
+ const offset = direction * spacing * (0.2 + i * 0.68);
56
+ const thickness = direction * spacing * 0.42;
57
+ ctx.beginPath();
58
+ ctx.moveTo(x1 + inset, y1 + slopeInset + offset);
59
+ ctx.lineTo(x2 - inset, y2 - slopeInset + offset);
60
+ ctx.lineTo(x2 - inset, y2 - slopeInset + offset + thickness);
61
+ ctx.lineTo(x1 + inset, y1 + slopeInset + offset + thickness);
62
+ ctx.closePath();
63
+ ctx.fill();
64
+ }
65
+ ctx.closeGroup();
66
+ this.setRendered();
67
+ }
68
+ }
@@ -43,7 +43,7 @@ export class Annotation extends Modifier {
43
43
  let verticalSpaceNeeded = textLines;
44
44
  const note = annotation.checkAttachedNote();
45
45
  const glyphWidth = note.getGlyphWidth();
46
- const textWidth = annotation.getWidth();
46
+ const textWidth = annotation.getWidth() + 2 * annotation.connectorPadding;
47
47
  if (annotation.horizontalJustification === AnnotationHorizontalJustify.RIGHT) {
48
48
  maxLeftGlyphWidth = Math.max(glyphWidth, maxLeftGlyphWidth);
49
49
  leftWidth = Math.max(leftWidth, textWidth) + Annotation.minAnnotationPadding;
@@ -132,6 +132,8 @@ export class Annotation extends Modifier {
132
132
  }
133
133
  constructor(text) {
134
134
  super();
135
+ this.phraseManaged = false;
136
+ this.connectorPadding = 0;
135
137
  this.text = text;
136
138
  this.horizontalJustification = AnnotationHorizontalJustify.CENTER;
137
139
  this.verticalJustification = AnnotationVerticalJustify.TOP;
@@ -150,6 +152,18 @@ export class Annotation extends Modifier {
150
152
  this.horizontalJustification = typeof just === 'string' ? Annotation.HorizontalJustifyString[just] : just;
151
153
  return this;
152
154
  }
155
+ setConnectorPadding(px) {
156
+ if (!Number.isFinite(px) || px < 0)
157
+ throw new RangeError('Expected nonnegative connector padding.');
158
+ this.connectorPadding = px;
159
+ return this;
160
+ }
161
+ getTextBaseline() {
162
+ return this.textBaseline;
163
+ }
164
+ setLayoutBaseline(y) {
165
+ this.layoutBaseline = y;
166
+ }
153
167
  setTextBaseline(y) {
154
168
  if (y !== undefined && !Number.isFinite(y)) {
155
169
  throw new RuntimeError('BadArgument', 'Text baseline must be finite.');
@@ -177,9 +191,11 @@ export class Annotation extends Modifier {
177
191
  return { x, textWidth };
178
192
  }
179
193
  computeY() {
194
+ var _a;
180
195
  const textHeight = Font.convertSizeToPixelValue(this.fontInfo.size);
181
- if (this.textBaseline !== undefined)
182
- return { y: this.textBaseline, textHeight };
196
+ const baseline = (_a = this.textBaseline) !== null && _a !== void 0 ? _a : this.layoutBaseline;
197
+ if (baseline !== undefined)
198
+ return { y: baseline, textHeight };
183
199
  const note = this.checkAttachedNote();
184
200
  const stemDirection = note.hasStem() ? note.getStemDirection() : Stem.UP;
185
201
  let y;
@@ -226,6 +226,7 @@ export class Articulation extends Modifier {
226
226
  var _a;
227
227
  super();
228
228
  this.heightShift = 0;
229
+ this.layoutShiftY = 0;
229
230
  this.type = type;
230
231
  this.position = ABOVE;
231
232
  if (!Tables.articulationCodes(this.type)) {
@@ -242,19 +243,24 @@ export class Articulation extends Modifier {
242
243
  if (!this.articulation) {
243
244
  this.articulation = { code: this.type, betweenLines: false };
244
245
  }
246
+ if (this.betweenLinesOverride !== undefined)
247
+ this.articulation = Object.assign(Object.assign({}, this.articulation), { betweenLines: this.betweenLinesOverride });
245
248
  const code = (this.position === ABOVE ? this.articulation.aboveCode : this.articulation.belowCode) ||
246
249
  this.articulation.code ||
247
250
  Glyphs.null;
248
251
  this.text = code;
249
252
  }
250
253
  setBetweenLines(betweenLines = true) {
251
- this.articulation.betweenLines = betweenLines;
254
+ this.betweenLinesOverride = betweenLines;
255
+ this.articulation = Object.assign(Object.assign({}, this.articulation), { betweenLines });
252
256
  return this;
253
257
  }
254
- draw() {
255
- const ctx = this.checkContext();
258
+ setLayoutShiftY(y) {
259
+ this.layoutShiftY = y;
260
+ }
261
+ getPlacement() {
256
262
  const note = this.checkAttachedNote();
257
- this.setRendered();
263
+ let centered = false;
258
264
  const index = this.checkIndex();
259
265
  const { position, textLine } = this;
260
266
  const canSitBetweenLines = this.articulation.betweenLines;
@@ -280,10 +286,26 @@ export class Articulation extends Modifier {
280
286
  const distanceFromNote = (note.getYs()[index] - y) / staffSpace;
281
287
  const articLine = distanceFromNote + Number(noteLine);
282
288
  const snappedLine = snapLineToStaff(canSitBetweenLines, articLine, position, offsetDirection);
283
- if (isWithinLines(snappedLine, position))
284
- this.setOrigin(0.5, 0.5);
289
+ centered = isWithinLines(snappedLine, position);
285
290
  y += Math.abs(snappedLine - articLine) * staffSpace * offsetDirection;
286
291
  }
292
+ return { x, y: y + this.layoutShiftY, centered };
293
+ }
294
+ getInkBounds() {
295
+ const { x, y, centered } = this.getPlacement();
296
+ const box = this.getTextInk(x, y);
297
+ if (centered) {
298
+ box.x += -this.width / 2 - this.xShift;
299
+ box.y = y - this.height / 2;
300
+ }
301
+ return box;
302
+ }
303
+ draw() {
304
+ const ctx = this.checkContext();
305
+ const { x, y, centered } = this.getPlacement();
306
+ if (centered)
307
+ this.setOrigin(0.5, 0.5);
308
+ this.setRendered();
287
309
  L(`Rendering articulation at (x: ${x}, y: ${y})`);
288
310
  this.x = x;
289
311
  this.y = y;
@@ -1,3 +1,4 @@
1
+ import { BoundingBox } from './boundingbox.js';
1
2
  import { Element } from './element.js';
2
3
  import { Fraction } from './fraction.js';
3
4
  import { Stem } from './stem.js';
@@ -288,6 +289,7 @@ export class Beam extends Element {
288
289
  super();
289
290
  this.slope = 0;
290
291
  this.yShift = 0;
292
+ this.fittedStems = [];
291
293
  this.forcedPartialDirections = {};
292
294
  if (!notes || notes.length === 0) {
293
295
  throw new RuntimeError('BadArguments', 'No notes provided for beam.');
@@ -334,6 +336,22 @@ export class Beam extends Element {
334
336
  flatBeams: false,
335
337
  minFlatBeamOffset: 15,
336
338
  };
339
+ const first = notes[0];
340
+ const profile = isStaveNote(first) ? first.engraving : undefined;
341
+ if (profile) {
342
+ let explicitWidth;
343
+ Object.defineProperty(this.renderOptions, 'beamWidth', {
344
+ enumerable: true,
345
+ configurable: true,
346
+ get: () => {
347
+ var _a, _b;
348
+ return explicitWidth !== null && explicitWidth !== void 0 ? explicitWidth : profile.beamThickness * ((_b = (_a = first.getStave()) === null || _a === void 0 ? void 0 : _a.getSpacingBetweenLines()) !== null && _b !== void 0 ? _b : Tables.STAVE_LINE_DISTANCE);
349
+ },
350
+ set: (px) => {
351
+ explicitWidth = px;
352
+ },
353
+ });
354
+ }
337
355
  }
338
356
  getNotes() {
339
357
  return this.notes;
@@ -367,7 +385,10 @@ export class Beam extends Element {
367
385
  let minCost = Number.MAX_VALUE;
368
386
  let bestSlope = 0;
369
387
  let yShift = 0;
388
+ const followContour = this.renderOptions.contourSlope && minSlope <= 0 && maxSlope >= 0;
370
389
  for (let slope = minSlope; slope <= maxSlope; slope += increment) {
390
+ if (followContour && Math.abs(slope) > 1e-8 && slope * initialSlope <= 0)
391
+ continue;
371
392
  let totalStemExtension = 0;
372
393
  let yShiftTemp = 0;
373
394
  for (let i = 1; i < notes.length; ++i) {
@@ -394,9 +415,92 @@ export class Beam extends Element {
394
415
  yShift = yShiftTemp;
395
416
  }
396
417
  }
418
+ if (this.renderOptions.contourSlope && Math.abs(bestSlope) < 1e-8) {
419
+ const stave = firstNote.checkStave();
420
+ const top = stave.getYForLine(0);
421
+ const edge = firstNote.getStemExtents().topY + yShift;
422
+ const step = stave.getSpacingBetweenLines() / 2;
423
+ if (edge >= top && edge <= stave.getYForLine(stave.getNumLines() - 1)) {
424
+ const slots = (edge - top) / step;
425
+ const snapped = stemDirection === Stem.UP ? Math.floor(slots) : Math.ceil(slots);
426
+ yShift += top + snapped * step - edge;
427
+ }
428
+ }
397
429
  this.slope = bestSlope;
398
430
  this.yShift = yShift;
399
431
  }
432
+ clearOtherVoiceHeads() {
433
+ var _a, _b;
434
+ const first = this.notes[0];
435
+ const stave = first.getStave();
436
+ const direction = this._stemDirection;
437
+ if (!stave || this.notes.some((n) => n.getStave() !== stave || n.getStemDirection() !== direction))
438
+ return;
439
+ const firstContext = first.checkTickContext();
440
+ const lastContext = this.notes[this.notes.length - 1].checkTickContext();
441
+ const contexts = firstContext.tContexts.length
442
+ ? [...firstContext.through(lastContext)]
443
+ : this.notes.map((n) => n.checkTickContext());
444
+ const peers = contexts
445
+ .flatMap((c) => c.getTickables())
446
+ .filter(isStaveNote)
447
+ .filter((n) => n.getBeam() !== this && n.getStave() === stave && !n.isRest());
448
+ if (!peers.length)
449
+ return;
450
+ const firstX = first.getStemX();
451
+ const space = stave.getSpacingBetweenLines();
452
+ const gap = space * (isStaveNote(first) ? ((_b = (_a = first.engraving) === null || _a === void 0 ? void 0 : _a.clearance) !== null && _b !== void 0 ? _b : 0.25) : 0.25);
453
+ const width = this.renderOptions.beamWidth;
454
+ const origin = first.getStemExtents().topY + this.yShift;
455
+ const tip = (x) => (origin + (x - firstX) * this.slope) * -direction;
456
+ let required = 0;
457
+ let available = Infinity;
458
+ for (const note of this.notes) {
459
+ const depth = width * (1 + 1.5 * (note.getGlyphProps().beamCount - 1));
460
+ const inner = tip(note.getStemX()) - depth;
461
+ if (!isStaveNote(note))
462
+ return;
463
+ for (const head of note.noteHeads) {
464
+ const bounds = head.getBoundingBox();
465
+ const edge = direction === Stem.UP ? bounds.getY() : bounds.getY() + bounds.getH();
466
+ available = Math.min(available, inner - edge * -direction - gap);
467
+ }
468
+ }
469
+ const strips = Array.from(this.getStrips(origin));
470
+ for (const peer of peers) {
471
+ for (const box of peer.getHeadInk()) {
472
+ let outer = -Infinity;
473
+ let inner = Infinity;
474
+ for (const strip of strips) {
475
+ const left = Math.max(Math.min(strip.x1, strip.x2), box.x);
476
+ const right = Math.min(Math.max(strip.x1, strip.x2), box.x + box.w);
477
+ if (right <= left)
478
+ continue;
479
+ const offset = (strip.y1 - origin - (strip.x1 - firstX) * this.slope) * -direction;
480
+ outer = Math.max(outer, Math.max(tip(left), tip(right)) + offset);
481
+ inner = Math.min(inner, Math.min(tip(left), tip(right)) + offset - width);
482
+ }
483
+ if (outer === -Infinity)
484
+ continue;
485
+ const a = box.y * -direction;
486
+ const b = (box.y + box.h) * -direction;
487
+ const near = Math.min(a, b);
488
+ const far = Math.max(a, b);
489
+ if (far <= inner)
490
+ available = Math.min(available, inner - far - gap);
491
+ else if (near <= outer && far >= inner)
492
+ required = Math.max(required, outer - near + gap);
493
+ }
494
+ }
495
+ if (required <= 0)
496
+ return;
497
+ if (required <= available) {
498
+ this.yShift += direction * required;
499
+ }
500
+ else if (required - available < 2 * gap) {
501
+ this.yShift += direction * ((required + available) / 2);
502
+ }
503
+ }
400
504
  calculateFlatSlope() {
401
505
  const { notes, renderOptions: { beamWidth, minFlatBeamOffset, flatBeamOffset }, } = this;
402
506
  const stemDirection = this._stemDirection;
@@ -470,7 +574,9 @@ export class Beam extends Element {
470
574
  const beamCount = note.getGlyphProps().beamCount;
471
575
  crossStemExtension = (1 + (beamCount - 1) * 1.5) * this.renderOptions.beamWidth;
472
576
  }
473
- stem.setExtension(preBeamExtension + beamExtension + crossStemExtension);
577
+ const after = preBeamExtension + beamExtension + crossStemExtension;
578
+ this.fittedStems.push({ stem, before: preBeamExtension, after });
579
+ stem.setExtension(after);
474
580
  stem.adjustHeightForBeam();
475
581
  if (note.isRest() && showStemlets) {
476
582
  const totalBeamWidth = (beamCount - 1) * beamWidth * 1.5 + beamWidth;
@@ -590,36 +696,70 @@ export class Beam extends Element {
590
696
  }
591
697
  }, this);
592
698
  }
593
- drawBeamLines(ctx) {
594
- const validBeamDurations = ['4', '8', '16', '32', '64', '128', '256', '512', '1024'];
595
- const firstNote = this.notes[0];
596
- let beamY = this.getBeamYToDraw();
597
- const firstStemX = firstNote.getStemX();
598
- const beamThickness = this.renderOptions.beamWidth * this._stemDirection;
599
- for (let i = 0; i < validBeamDurations.length; ++i) {
600
- const duration = validBeamDurations[i];
601
- const beamLines = this.getBeamLines(duration);
602
- for (let j = 0; j < beamLines.length; ++j) {
603
- const beamLine = beamLines[j];
604
- const startBeamX = beamLine.start;
605
- const startBeamY = this.getSlopeY(startBeamX, firstStemX, beamY, this.slope);
606
- const lastBeamX = beamLine.end;
607
- if (lastBeamX) {
608
- const lastBeamY = this.getSlopeY(lastBeamX, firstStemX, beamY, this.slope);
609
- ctx.beginPath();
610
- ctx.moveTo(startBeamX, startBeamY);
611
- ctx.lineTo(startBeamX, startBeamY + beamThickness);
612
- ctx.lineTo(lastBeamX + 1, lastBeamY + beamThickness);
613
- ctx.lineTo(lastBeamX + 1, lastBeamY);
614
- ctx.closePath();
615
- ctx.fill();
616
- }
617
- else {
699
+ *getStrips(beamY = this.getBeamYToDraw()) {
700
+ const firstX = this.notes[0].getStemX();
701
+ const thickness = this.renderOptions.beamWidth * this._stemDirection;
702
+ for (const duration of ['4', '8', '16', '32', '64', '128', '256', '512', '1024']) {
703
+ for (const { start, end } of this.getBeamLines(duration)) {
704
+ if (end === undefined)
618
705
  throw new RuntimeError('NoLastBeamX', 'lastBeamX undefined.');
619
- }
706
+ yield {
707
+ x1: start,
708
+ x2: end + 1,
709
+ y1: this.getSlopeY(start, firstX, beamY, this.slope),
710
+ y2: this.getSlopeY(end, firstX, beamY, this.slope),
711
+ thickness,
712
+ };
620
713
  }
621
- beamY += beamThickness * 1.5;
714
+ beamY += thickness * 1.5;
715
+ }
716
+ }
717
+ getStripBounds() {
718
+ this.postFormat();
719
+ const parts = [];
720
+ for (const { x1, x2, y1, y2, thickness } of this.getStrips()) {
721
+ const count = Math.min(64, Math.max(1, Math.ceil(Math.abs(y2 - y1) / 2)));
722
+ for (let i = 0; i < count; i++) {
723
+ const x = x1 + ((x2 - x1) * i) / count, y = y1 + ((y2 - y1) * i) / count;
724
+ const dx = (x2 - x1) / count, dy = (y2 - y1) / count;
725
+ parts.push(new BoundingBox(x + Math.min(0, dx), y + Math.min(0, dy) + Math.min(0, thickness), Math.abs(dx), Math.abs(dy) + Math.abs(thickness)));
726
+ }
727
+ }
728
+ return parts;
729
+ }
730
+ getBoundingBox() {
731
+ this.postFormat();
732
+ let box;
733
+ for (const { x1, x2, y1, y2, thickness } of this.getStrips()) {
734
+ const strip = new BoundingBox(Math.min(x1, x2), Math.min(y1, y2) + Math.min(0, thickness), Math.abs(x2 - x1), Math.abs(y2 - y1) + Math.abs(thickness));
735
+ box = box ? box.mergeWith(strip) : strip;
736
+ }
737
+ return box !== null && box !== void 0 ? box : new BoundingBox(this.notes[0].getStemX(), this.getBeamYToDraw(), 0, 0);
738
+ }
739
+ drawBeamLines(ctx) {
740
+ for (const { x1, x2, y1, y2, thickness } of this.getStrips()) {
741
+ ctx.beginPath();
742
+ ctx.moveTo(x1, y1);
743
+ ctx.lineTo(x1, y1 + thickness);
744
+ ctx.lineTo(x2, y2 + thickness);
745
+ ctx.lineTo(x2, y2);
746
+ ctx.closePath();
747
+ ctx.fill();
748
+ }
749
+ }
750
+ resetLayout() {
751
+ if (!this.postFormatted)
752
+ return;
753
+ for (const { stem, before, after } of this.fittedStems) {
754
+ if (stem.getExtension() === after)
755
+ stem.setExtension(before);
622
756
  }
757
+ if (this.flatOffset && this.renderOptions.flatBeamOffset === this.flatOffset.after) {
758
+ this.renderOptions.flatBeamOffset = this.flatOffset.before;
759
+ }
760
+ this.fittedStems.length = 0;
761
+ this.flatOffset = undefined;
762
+ this.postFormatted = false;
623
763
  }
624
764
  preFormat() {
625
765
  return this;
@@ -628,10 +768,13 @@ export class Beam extends Element {
628
768
  if (this.postFormatted)
629
769
  return;
630
770
  if (isTabNote(this.notes[0]) || this.renderOptions.flatBeams) {
771
+ const before = this.renderOptions.flatBeamOffset;
631
772
  this.calculateFlatSlope();
773
+ this.flatOffset = { before, after: this.renderOptions.flatBeamOffset };
632
774
  }
633
775
  else {
634
776
  this.calculateSlope();
777
+ this.clearOtherVoiceHeads();
635
778
  }
636
779
  this.applyStemExtensions();
637
780
  this.postFormatted = true;
@@ -1,4 +1,5 @@
1
1
  import { Element } from './element.js';
2
+ import { bendAmount, copyBend } from './guitargesture.js';
2
3
  import { Metrics } from './metrics.js';
3
4
  import { Modifier } from './modifier.js';
4
5
  import { isTabNote } from './typeguard.js';
@@ -14,26 +15,41 @@ export class Bend extends Modifier {
14
15
  return 1;
15
16
  }
16
17
  static format(bends, state) {
18
+ var _a;
17
19
  if (!bends || bends.length === 0)
18
20
  return false;
19
- let lastWidth = 0;
20
- for (let i = 0; i < bends.length; ++i) {
21
- const bend = bends[i];
21
+ const chords = new Map();
22
+ for (const bend of bends) {
22
23
  const note = bend.checkAttachedNote();
24
+ const chord = (_a = chords.get(note)) !== null && _a !== void 0 ? _a : [];
25
+ chord.push(bend);
26
+ chords.set(note, chord);
27
+ if (isTabNote(note))
28
+ state.topTextLine = Math.max(state.topTextLine, note.leastString() - 1);
29
+ }
30
+ let width = 0;
31
+ for (const [note, chord] of chords) {
23
32
  if (isTabNote(note)) {
24
- const stringPos = note.leastString() - 1;
25
- if (state.topTextLine < stringPos) {
26
- state.topTextLine = stringPos;
27
- }
33
+ const positions = note.getPositions();
34
+ chord.sort((a, b) => positions[a.checkIndex()].str - positions[b.checkIndex()].str);
35
+ }
36
+ for (const bend of chord) {
37
+ bend.setXShift(width);
38
+ width = bend.getWidth();
39
+ bend.setTextLine(state.topTextLine);
28
40
  }
29
- bend.setXShift(lastWidth);
30
- lastWidth = bend.getWidth();
31
- bend.setTextLine(state.topTextLine);
32
41
  }
33
- state.rightShift += lastWidth;
42
+ state.rightShift += width;
34
43
  state.topTextLine += 1;
35
44
  return true;
36
45
  }
46
+ static timed(spec) {
47
+ const timing = copyBend(spec);
48
+ const bend = new Bend([{ type: Bend.UP, text: bendAmount(timing) }]);
49
+ bend.timing = timing;
50
+ bend.updateWidth();
51
+ return bend;
52
+ }
37
53
  setStyleLine(style) {
38
54
  this.styleLine = style;
39
55
  return this;
@@ -73,6 +89,10 @@ export class Bend extends Modifier {
73
89
  element.setText(text);
74
90
  return element.getWidth();
75
91
  };
92
+ if (this.timing) {
93
+ this.setWidth(this.xShift + Math.max(48, measureText(bendAmount(this.timing)) + 18));
94
+ return this;
95
+ }
76
96
  let totalWidth = 0;
77
97
  for (let i = 0; i < this.phrase.length; ++i) {
78
98
  const bend = this.phrase[i];
@@ -90,11 +110,14 @@ export class Bend extends Modifier {
90
110
  return this;
91
111
  }
92
112
  draw() {
93
- var _a;
113
+ var _a, _b, _c;
94
114
  const ctx = this.checkContext();
95
115
  const note = this.checkAttachedNote();
116
+ const index = this.checkIndex();
117
+ if (this.timing && (!isTabNote(note) || ((_a = note.getPositions()[index]) === null || _a === void 0 ? void 0 : _a.str) !== this.timing.string))
118
+ throw new RuntimeError('BadArguments', 'Attach the timed bend to its authored TAB string.');
96
119
  this.setRendered();
97
- const start = note.getModifierStartXY(Modifier.Position.RIGHT, this.index);
120
+ const start = note.getModifierStartXY(Modifier.Position.RIGHT, index);
98
121
  start.x += 3;
99
122
  start.y += 0.5;
100
123
  const xShift = this.xShift;
@@ -134,28 +157,59 @@ export class Bend extends Modifier {
134
157
  const renderX = x - ctx.measureText(text).width / 2;
135
158
  ctx.fillText(text, renderX, annotationY);
136
159
  };
160
+ if (this.timing) {
161
+ const { rise, hold, release } = this.timing;
162
+ const left = start.x + xShift;
163
+ const extent = this.getWidth() - xShift;
164
+ const phaseX = ([n, d]) => left + (extent * n) / d;
165
+ const peak = phaseX(rise);
166
+ renderBend(left, start.y, peak - left, bendHeight);
167
+ renderArrowHead(peak, bendHeight, 1);
168
+ renderText(peak, bendAmount(this.timing));
169
+ const held = hold ? phaseX(hold) : peak;
170
+ if (hold) {
171
+ ctx.beginPath();
172
+ ctx.moveTo(peak, bendHeight);
173
+ ctx.lineTo(held, bendHeight);
174
+ ctx.stroke();
175
+ }
176
+ if (release) {
177
+ const end = phaseX(release);
178
+ renderRelease(held, start.y, end - held, bendHeight);
179
+ renderArrowHead(end, start.y, -1);
180
+ }
181
+ return;
182
+ }
137
183
  let lastBend = undefined;
138
184
  let lastBendDrawWidth = 0;
139
185
  let lastDrawnWidth = 0;
140
- if ((_a = this.tap) === null || _a === void 0 ? void 0 : _a.length) {
141
- const tapStart = note.getModifierStartXY(Modifier.Position.CENTER, this.index);
186
+ if ((_b = this.tap) === null || _b === void 0 ? void 0 : _b.length) {
187
+ const tapStart = note.getModifierStartXY(Modifier.Position.CENTER, index);
142
188
  renderText(tapStart.x, this.tap);
143
189
  }
144
190
  for (let i = 0; i < this.phrase.length; ++i) {
145
191
  const bend = this.phrase[i];
146
- if (!bend.drawWidth)
147
- bend.drawWidth = 0;
148
- if (i === 0)
149
- bend.drawWidth += xShift;
150
- lastDrawnWidth = bend.drawWidth + lastBendDrawWidth - (i === 1 ? xShift : 0);
192
+ const drawWidth = ((_c = bend.drawWidth) !== null && _c !== void 0 ? _c : 0) + (i === 0 ? xShift : 0);
193
+ lastDrawnWidth = drawWidth + lastBendDrawWidth - (i === 1 ? xShift : 0);
151
194
  if (bend.type === Bend.UP) {
152
195
  if (lastBend && lastBend.type === Bend.UP) {
153
196
  renderArrowHead(start.x, bendHeight, +1);
154
197
  }
155
198
  renderBend(start.x, start.y, lastDrawnWidth, bendHeight);
156
199
  }
200
+ if (bend.type === Bend.PREBEND || bend.type === Bend.HOLD) {
201
+ this.applyStyle(ctx, this.styleLine);
202
+ ctx.beginPath();
203
+ ctx.moveTo(start.x, bend.type === Bend.PREBEND ? start.y : bendHeight);
204
+ if (bend.type === Bend.PREBEND)
205
+ ctx.lineTo(start.x, bendHeight);
206
+ ctx.lineTo(start.x + lastDrawnWidth, bendHeight);
207
+ ctx.stroke();
208
+ if (bend.type === Bend.PREBEND)
209
+ renderArrowHead(start.x, bendHeight, +1);
210
+ }
157
211
  if (bend.type === Bend.DOWN) {
158
- if (lastBend && lastBend.type === Bend.UP) {
212
+ if (lastBend && [Bend.UP, Bend.PREBEND, Bend.HOLD].includes(lastBend.type)) {
159
213
  renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
160
214
  }
161
215
  if (lastBend && lastBend.type === Bend.DOWN) {
@@ -163,13 +217,13 @@ export class Bend extends Modifier {
163
217
  renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
164
218
  }
165
219
  if (!lastBend) {
166
- lastDrawnWidth = bend.drawWidth;
220
+ lastDrawnWidth = drawWidth;
167
221
  renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
168
222
  }
169
223
  }
170
224
  renderText(start.x + lastDrawnWidth, bend.text);
171
225
  lastBend = bend;
172
- lastBendDrawWidth = bend.drawWidth;
226
+ lastBendDrawWidth = drawWidth;
173
227
  lastBend.x = start.x;
174
228
  start.x += lastDrawnWidth;
175
229
  }
@@ -184,3 +238,5 @@ export class Bend extends Modifier {
184
238
  }
185
239
  }
186
240
  }
241
+ Bend.PREBEND = 2;
242
+ Bend.HOLD = 3;
@@ -108,6 +108,9 @@ export class Clef extends StaveModifier {
108
108
  this.stave = stave;
109
109
  return this;
110
110
  }
111
+ getInkBounds() {
112
+ return this.getTextInk(this.getX(), this.checkStave().getYForLine(this.line));
113
+ }
111
114
  draw() {
112
115
  const stave = this.checkStave();
113
116
  const ctx = stave.checkContext();