vexflow 5.1.2 → 5.3.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 (162) hide show
  1. package/README.md +7 -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 +5325 -1730
  7. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  8. package/build/cjs/vexflow-debug.js +4228 -1545
  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 +13 -3
  13. package/build/esm/src/articulation.js +51 -20
  14. package/build/esm/src/beam.js +170 -30
  15. package/build/esm/src/bend.js +24 -19
  16. package/build/esm/src/clef.js +3 -0
  17. package/build/esm/src/clefnote.js +5 -1
  18. package/build/esm/src/curve.js +27 -53
  19. package/build/esm/src/dot.js +14 -5
  20. package/build/esm/src/easyscore/composition.js +336 -0
  21. package/build/esm/src/easyscore/elementindex.js +81 -0
  22. package/build/esm/src/easyscore/elements.js +324 -0
  23. package/build/esm/src/easyscore/policies.js +97 -0
  24. package/build/esm/src/easyscore/rhythm.js +42 -0
  25. package/build/esm/src/easyscore.js +448 -26
  26. package/build/esm/src/element.js +17 -2
  27. package/build/esm/src/engravinggeometry.js +150 -0
  28. package/build/esm/src/engravingprofile.js +44 -0
  29. package/build/esm/src/factory.js +268 -64
  30. package/build/esm/src/formatter.js +40 -4
  31. package/build/esm/src/gracenote.js +43 -38
  32. package/build/esm/src/gracenotegroup.js +53 -56
  33. package/build/esm/src/index.js +2 -0
  34. package/build/esm/src/keysignature.js +10 -2
  35. package/build/esm/src/keysignote.js +2 -2
  36. package/build/esm/src/modifiercontext.js +11 -0
  37. package/build/esm/src/note.js +5 -0
  38. package/build/esm/src/notehead.js +11 -6
  39. package/build/esm/src/ornament.js +38 -18
  40. package/build/esm/src/parenthesis.js +1 -1
  41. package/build/esm/src/parser.js +6 -1
  42. package/build/esm/src/proportionalformatter.js +234 -0
  43. package/build/esm/src/registry.js +27 -0
  44. package/build/esm/src/sheet.js +20 -3
  45. package/build/esm/src/stave.js +9 -2
  46. package/build/esm/src/stavenote.js +140 -127
  47. package/build/esm/src/stavetie.js +39 -7
  48. package/build/esm/src/stem.js +26 -11
  49. package/build/esm/src/stemmablenote.js +6 -1
  50. package/build/esm/src/tickable.js +5 -0
  51. package/build/esm/src/tickcontext.js +19 -0
  52. package/build/esm/src/timesignature.js +5 -0
  53. package/build/esm/src/tuplet.js +60 -19
  54. package/build/esm/src/version.js +3 -3
  55. package/build/esm/src/vexflow.js +6 -1
  56. package/build/esm/tests/beam_tests.js +35 -0
  57. package/build/esm/tests/bend_tests.js +14 -1
  58. package/build/esm/tests/curve_tests.js +14 -4
  59. package/build/esm/tests/demo_repertoire_tests.js +27 -0
  60. package/build/esm/tests/demos/drums.js +54 -0
  61. package/build/esm/tests/demos/electric.js +63 -0
  62. package/build/esm/tests/demos/greensleeves.js +90 -0
  63. package/build/esm/tests/demos/joy.js +61 -0
  64. package/build/esm/tests/demos/kesh.js +80 -0
  65. package/build/esm/tests/demos/prelude.js +59 -0
  66. package/build/esm/tests/demos/quartertones.js +50 -0
  67. package/build/esm/tests/demos/saints.js +65 -0
  68. package/build/esm/tests/easyscore_tests.js +40 -0
  69. package/build/esm/tests/easyscore_types.js +65 -0
  70. package/build/esm/tests/engraving/score-layout.mjs +75 -0
  71. package/build/esm/tests/engraving/score-persistence.mjs +262 -0
  72. package/build/esm/tests/engraving/studies.mjs +279 -0
  73. package/build/esm/tests/formatter_tests.js +51 -1
  74. package/build/esm/tests/index.js +1 -0
  75. package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
  76. package/build/esm/tests/node/easyscore-rendering-tests.mjs +332 -0
  77. package/build/esm/tests/node/easyscore-tests.mjs +1024 -0
  78. package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
  79. package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
  80. package/build/esm/tests/node/proportional-spacing-tests.mjs +278 -0
  81. package/build/esm/tests/node/rendering-tests.mjs +6 -2
  82. package/build/esm/tests/threevoice_tests.js +11 -1
  83. package/build/esm/tests/vexflow_test_helpers.js +19 -10
  84. package/build/esm/tools/audit-dependencies.mjs +61 -0
  85. package/build/esm/tools/easyscore-api-audit.mjs +58 -0
  86. package/build/esm/tools/easyscore-composition.mjs +62 -0
  87. package/build/esm/tools/easyscore-example.mjs +16 -0
  88. package/build/esm/tools/easyscore-families.mjs +61 -0
  89. package/build/esm/tools/easyscore-references-example.mjs +26 -0
  90. package/build/esm/tools/easyscore-rhythm.mjs +58 -0
  91. package/build/esm/tools/easyscore-structure.mjs +88 -0
  92. package/build/esm/tools/explorer/catalog/entries.mjs +6 -0
  93. package/build/esm/tools/explorer/catalog/expression.mjs +68 -0
  94. package/build/esm/tools/explorer/catalog/helpers.mjs +29 -0
  95. package/build/esm/tools/explorer/catalog/layout.mjs +109 -0
  96. package/build/esm/tools/explorer/catalog/notes.mjs +61 -0
  97. package/build/esm/tools/explorer/catalog/staves.mjs +85 -0
  98. package/build/esm/tools/explorer/catalog/strings.mjs +25 -0
  99. package/build/esm/tools/explorer/catalog.mjs +14 -0
  100. package/build/esm/tools/score-fixtures.mjs +23 -6
  101. package/build/types/src/accidental.d.ts +6 -0
  102. package/build/types/src/articulation.d.ts +12 -1
  103. package/build/types/src/beam.d.ts +16 -0
  104. package/build/types/src/clef.d.ts +2 -0
  105. package/build/types/src/curve.d.ts +9 -0
  106. package/build/types/src/dot.d.ts +5 -0
  107. package/build/types/src/easyscore/composition.d.ts +126 -0
  108. package/build/types/src/easyscore/elementindex.d.ts +30 -0
  109. package/build/types/src/easyscore/elements.d.ts +156 -0
  110. package/build/types/src/easyscore/policies.d.ts +12 -0
  111. package/build/types/src/easyscore/rhythm.d.ts +9 -0
  112. package/build/types/src/easyscore.d.ts +153 -11
  113. package/build/types/src/element.d.ts +7 -1
  114. package/build/types/src/engravinggeometry.d.ts +31 -0
  115. package/build/types/src/engravingprofile.d.ts +20 -0
  116. package/build/types/src/factory.d.ts +97 -9
  117. package/build/types/src/formatter.d.ts +7 -1
  118. package/build/types/src/gracenote.d.ts +7 -0
  119. package/build/types/src/gracenotegroup.d.ts +7 -2
  120. package/build/types/src/index.d.ts +2 -0
  121. package/build/types/src/keysignature.d.ts +3 -0
  122. package/build/types/src/keysignote.d.ts +1 -1
  123. package/build/types/src/modifiercontext.d.ts +4 -0
  124. package/build/types/src/note.d.ts +2 -0
  125. package/build/types/src/notehead.d.ts +5 -0
  126. package/build/types/src/ornament.d.ts +7 -0
  127. package/build/types/src/parser.d.ts +5 -0
  128. package/build/types/src/proportionalformatter.d.ts +39 -0
  129. package/build/types/src/registry.d.ts +4 -0
  130. package/build/types/src/stave.d.ts +2 -0
  131. package/build/types/src/stavenote.d.ts +21 -3
  132. package/build/types/src/stavetie.d.ts +4 -0
  133. package/build/types/src/stem.d.ts +10 -1
  134. package/build/types/src/stemmablenote.d.ts +6 -1
  135. package/build/types/src/tickable.d.ts +2 -0
  136. package/build/types/src/tickcontext.d.ts +5 -0
  137. package/build/types/src/timesignature.d.ts +2 -0
  138. package/build/types/src/tuplet.d.ts +5 -48
  139. package/build/types/src/vexflow.d.ts +6 -1
  140. package/changelog/CHANGELOG.md +52 -0
  141. package/package.json +13 -3
  142. package/tests/all.html +277 -0
  143. package/tests/demo_petzold_tests.ts +435 -0
  144. package/tests/demos/drums.ts +55 -0
  145. package/tests/demos/drums.webp +0 -0
  146. package/tests/demos/electric.ts +70 -0
  147. package/tests/demos/electric.webp +0 -0
  148. package/tests/demos/greensleeves.ts +97 -0
  149. package/tests/demos/greensleeves.webp +0 -0
  150. package/tests/demos/joy.ts +65 -0
  151. package/tests/demos/joy.webp +0 -0
  152. package/tests/demos/kesh.ts +86 -0
  153. package/tests/demos/kesh.webp +0 -0
  154. package/tests/demos/prelude.ts +65 -0
  155. package/tests/demos/prelude.webp +0 -0
  156. package/tests/demos/quartertones.ts +51 -0
  157. package/tests/demos/quartertones.webp +0 -0
  158. package/tests/demos/saints.ts +70 -0
  159. package/tests/demos/saints.webp +0 -0
  160. package/tests/flow.html +550 -239
  161. package/tests/fonts.css +493 -0
  162. package/tests/minuet.webp +0 -0
@@ -316,15 +316,25 @@ export class Accidental extends Modifier {
316
316
  this.reset();
317
317
  return this;
318
318
  }
319
- draw() {
319
+ isCautionary() {
320
+ return this.cautionary;
321
+ }
322
+ positionGlyph() {
320
323
  const { type, position, index } = this;
321
- const ctx = this.checkContext();
322
324
  const note = this.checkAttachedNote();
323
- this.setRendered();
324
325
  const start = note.getModifierStartXY(position, index);
325
326
  this.x = start.x - this.width;
326
327
  this.y = start.y;
327
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();
328
338
  this.renderText(ctx, 0, 0);
329
339
  }
330
340
  }
@@ -9,6 +9,7 @@ function L(...args) {
9
9
  log('VexFlow.Articulation', args);
10
10
  }
11
11
  const { ABOVE, BELOW } = Modifier.Position;
12
+ const easyScoreGlyphs = new Set(Object.values(Glyphs));
12
13
  function roundToNearestHalf(mathFn, value) {
13
14
  return mathFn(value / 0.5) * 0.5;
14
15
  }
@@ -190,24 +191,36 @@ export class Articulation extends Modifier {
190
191
  state.rightShift += overlap / 2;
191
192
  return true;
192
193
  }
193
- static easyScoreHook({ articulations }, note, builder) {
194
- if (!articulations)
195
- return;
196
- const articNameToCode = {
194
+ static parseEasyScore(articulations) {
195
+ const aliases = {
197
196
  staccato: 'a.',
198
197
  tenuto: 'a-',
199
198
  accent: 'a>',
199
+ fermata: 'a@',
200
+ marcato: 'a^',
201
+ staccatissimo: 'av',
200
202
  };
201
- articulations
202
- .split(',')
203
- .map((articString) => articString.trim().split('.'))
204
- .map(([name, position]) => {
205
- const artic = { type: articNameToCode[name] };
206
- if (position)
207
- artic.position = Modifier.PositionString[position];
208
- return builder.getFactory().Articulation(artic);
209
- })
210
- .map((artic) => note.addModifier(artic, 0));
203
+ const singleCode = articulations.trim().replace(/\.(above|below|left|right)$/, '');
204
+ const items = Tables.articulationCodes(singleCode) ? [articulations] : articulations.split(',');
205
+ return items.map((value) => {
206
+ var _a;
207
+ const match = /^(.*)\.(above|below|left|right)$/.exec(value.trim());
208
+ const name = match ? match[1] : value.trim();
209
+ const type = Object.hasOwnProperty.call(aliases, name) ? aliases[name] : name;
210
+ const glyph = Object.hasOwnProperty.call(Glyphs, type) ? Glyphs[type] : type;
211
+ if (!Object.hasOwnProperty.call((_a = Tables.articulationCodes(type)) !== null && _a !== void 0 ? _a : {}, 'betweenLines') &&
212
+ !easyScoreGlyphs.has(glyph)) {
213
+ throw new RuntimeError('BadArguments', `Unknown EasyScore articulation: ${name}`);
214
+ }
215
+ return { type: glyph, position: match ? Modifier.PositionString[match[2]] : undefined };
216
+ });
217
+ }
218
+ static easyScoreHook({ articulations }, note, builder) {
219
+ if (!articulations)
220
+ return;
221
+ Articulation.parseEasyScore(articulations).forEach((params) => {
222
+ note.addModifier(builder.getFactory().Articulation(params), 0);
223
+ });
211
224
  }
212
225
  constructor(type) {
213
226
  var _a;
@@ -229,19 +242,21 @@ export class Articulation extends Modifier {
229
242
  if (!this.articulation) {
230
243
  this.articulation = { code: this.type, betweenLines: false };
231
244
  }
245
+ if (this.betweenLinesOverride !== undefined)
246
+ this.articulation = Object.assign(Object.assign({}, this.articulation), { betweenLines: this.betweenLinesOverride });
232
247
  const code = (this.position === ABOVE ? this.articulation.aboveCode : this.articulation.belowCode) ||
233
248
  this.articulation.code ||
234
249
  Glyphs.null;
235
250
  this.text = code;
236
251
  }
237
252
  setBetweenLines(betweenLines = true) {
238
- this.articulation.betweenLines = betweenLines;
253
+ this.betweenLinesOverride = betweenLines;
254
+ this.articulation = Object.assign(Object.assign({}, this.articulation), { betweenLines });
239
255
  return this;
240
256
  }
241
- draw() {
242
- const ctx = this.checkContext();
257
+ getPlacement() {
243
258
  const note = this.checkAttachedNote();
244
- this.setRendered();
259
+ let centered = false;
245
260
  const index = this.checkIndex();
246
261
  const { position, textLine } = this;
247
262
  const canSitBetweenLines = this.articulation.betweenLines;
@@ -267,10 +282,26 @@ export class Articulation extends Modifier {
267
282
  const distanceFromNote = (note.getYs()[index] - y) / staffSpace;
268
283
  const articLine = distanceFromNote + Number(noteLine);
269
284
  const snappedLine = snapLineToStaff(canSitBetweenLines, articLine, position, offsetDirection);
270
- if (isWithinLines(snappedLine, position))
271
- this.setOrigin(0.5, 0.5);
285
+ centered = isWithinLines(snappedLine, position);
272
286
  y += Math.abs(snappedLine - articLine) * staffSpace * offsetDirection;
273
287
  }
288
+ return { x, y, centered };
289
+ }
290
+ getInkBounds() {
291
+ const { x, y, centered } = this.getPlacement();
292
+ const box = this.getTextInk(x, y);
293
+ if (centered) {
294
+ box.x += -this.width / 2 - this.xShift;
295
+ box.y = y - this.height / 2;
296
+ }
297
+ return box;
298
+ }
299
+ draw() {
300
+ const ctx = this.checkContext();
301
+ const { x, y, centered } = this.getPlacement();
302
+ if (centered)
303
+ this.setOrigin(0.5, 0.5);
304
+ this.setRendered();
274
305
  L(`Rendering articulation at (x: ${x}, y: ${y})`);
275
306
  this.x = x;
276
307
  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';
@@ -195,6 +196,16 @@ export class Beam extends Element {
195
196
  }
196
197
  function formatStems() {
197
198
  noteGroups.forEach((group) => {
199
+ if (config.stemPolicy === 'preserve')
200
+ return;
201
+ if (config.stemPolicy === 'explicit') {
202
+ const explicit = new Set(group.filter((note) => !note.isRest() && !note.isAutoStem()).map((note) => note.getStemDirection()));
203
+ if (explicit.size > 1)
204
+ return;
205
+ const direction = explicit.size ? [...explicit][0] : calculateStemDirection(group);
206
+ group.filter((note) => note.isAutoStem()).forEach((note) => note.setStemDirection(direction, true));
207
+ return;
208
+ }
198
209
  let stemDirection;
199
210
  if (config.maintainStemDirections) {
200
211
  const note = findFirstNote(group);
@@ -222,7 +233,7 @@ export class Beam extends Element {
222
233
  }
223
234
  function applyStemDirection(group, direction) {
224
235
  group.forEach((note) => {
225
- note.setStemDirection(direction);
236
+ note.setStemDirection(direction, config.stemDirection === undefined && note.isAutoStem());
226
237
  });
227
238
  }
228
239
  function getTuplets() {
@@ -278,6 +289,7 @@ export class Beam extends Element {
278
289
  super();
279
290
  this.slope = 0;
280
291
  this.yShift = 0;
292
+ this.fittedStems = [];
281
293
  this.forcedPartialDirections = {};
282
294
  if (!notes || notes.length === 0) {
283
295
  throw new RuntimeError('BadArguments', 'No notes provided for beam.');
@@ -303,7 +315,7 @@ export class Beam extends Element {
303
315
  for (i = 0; i < notes.length; ++i) {
304
316
  note = notes[i];
305
317
  if (autoStem) {
306
- note.setStemDirection(stemDirection);
318
+ note.setStemDirection(stemDirection, true);
307
319
  this._stemDirection = stemDirection;
308
320
  }
309
321
  note.setBeam(this);
@@ -324,6 +336,22 @@ export class Beam extends Element {
324
336
  flatBeams: false,
325
337
  minFlatBeamOffset: 15,
326
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
+ }
327
355
  }
328
356
  getNotes() {
329
357
  return this.notes;
@@ -357,7 +385,10 @@ export class Beam extends Element {
357
385
  let minCost = Number.MAX_VALUE;
358
386
  let bestSlope = 0;
359
387
  let yShift = 0;
388
+ const followContour = this.renderOptions.contourSlope && minSlope <= 0 && maxSlope >= 0;
360
389
  for (let slope = minSlope; slope <= maxSlope; slope += increment) {
390
+ if (followContour && Math.abs(slope) > 1e-8 && slope * initialSlope <= 0)
391
+ continue;
361
392
  let totalStemExtension = 0;
362
393
  let yShiftTemp = 0;
363
394
  for (let i = 1; i < notes.length; ++i) {
@@ -384,9 +415,92 @@ export class Beam extends Element {
384
415
  yShift = yShiftTemp;
385
416
  }
386
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
+ }
387
429
  this.slope = bestSlope;
388
430
  this.yShift = yShift;
389
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
+ }
390
504
  calculateFlatSlope() {
391
505
  const { notes, renderOptions: { beamWidth, minFlatBeamOffset, flatBeamOffset }, } = this;
392
506
  const stemDirection = this._stemDirection;
@@ -460,7 +574,9 @@ export class Beam extends Element {
460
574
  const beamCount = note.getGlyphProps().beamCount;
461
575
  crossStemExtension = (1 + (beamCount - 1) * 1.5) * this.renderOptions.beamWidth;
462
576
  }
463
- stem.setExtension(preBeamExtension + beamExtension + crossStemExtension);
577
+ const after = preBeamExtension + beamExtension + crossStemExtension;
578
+ this.fittedStems.push({ stem, before: preBeamExtension, after });
579
+ stem.setExtension(after);
464
580
  stem.adjustHeightForBeam();
465
581
  if (note.isRest() && showStemlets) {
466
582
  const totalBeamWidth = (beamCount - 1) * beamWidth * 1.5 + beamWidth;
@@ -580,36 +696,57 @@ export class Beam extends Element {
580
696
  }
581
697
  }, this);
582
698
  }
583
- drawBeamLines(ctx) {
584
- const validBeamDurations = ['4', '8', '16', '32', '64', '128', '256', '512', '1024'];
585
- const firstNote = this.notes[0];
586
- let beamY = this.getBeamYToDraw();
587
- const firstStemX = firstNote.getStemX();
588
- const beamThickness = this.renderOptions.beamWidth * this._stemDirection;
589
- for (let i = 0; i < validBeamDurations.length; ++i) {
590
- const duration = validBeamDurations[i];
591
- const beamLines = this.getBeamLines(duration);
592
- for (let j = 0; j < beamLines.length; ++j) {
593
- const beamLine = beamLines[j];
594
- const startBeamX = beamLine.start;
595
- const startBeamY = this.getSlopeY(startBeamX, firstStemX, beamY, this.slope);
596
- const lastBeamX = beamLine.end;
597
- if (lastBeamX) {
598
- const lastBeamY = this.getSlopeY(lastBeamX, firstStemX, beamY, this.slope);
599
- ctx.beginPath();
600
- ctx.moveTo(startBeamX, startBeamY);
601
- ctx.lineTo(startBeamX, startBeamY + beamThickness);
602
- ctx.lineTo(lastBeamX + 1, lastBeamY + beamThickness);
603
- ctx.lineTo(lastBeamX + 1, lastBeamY);
604
- ctx.closePath();
605
- ctx.fill();
606
- }
607
- 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)
608
705
  throw new RuntimeError('NoLastBeamX', 'lastBeamX undefined.');
609
- }
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
+ };
610
713
  }
611
- beamY += beamThickness * 1.5;
714
+ beamY += thickness * 1.5;
715
+ }
716
+ }
717
+ getBoundingBox() {
718
+ this.postFormat();
719
+ let box;
720
+ for (const { x1, x2, y1, y2, thickness } of this.getStrips()) {
721
+ 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));
722
+ box = box ? box.mergeWith(strip) : strip;
723
+ }
724
+ return box !== null && box !== void 0 ? box : new BoundingBox(this.notes[0].getStemX(), this.getBeamYToDraw(), 0, 0);
725
+ }
726
+ drawBeamLines(ctx) {
727
+ for (const { x1, x2, y1, y2, thickness } of this.getStrips()) {
728
+ ctx.beginPath();
729
+ ctx.moveTo(x1, y1);
730
+ ctx.lineTo(x1, y1 + thickness);
731
+ ctx.lineTo(x2, y2 + thickness);
732
+ ctx.lineTo(x2, y2);
733
+ ctx.closePath();
734
+ ctx.fill();
735
+ }
736
+ }
737
+ resetLayout() {
738
+ if (!this.postFormatted)
739
+ return;
740
+ for (const { stem, before, after } of this.fittedStems) {
741
+ if (stem.getExtension() === after)
742
+ stem.setExtension(before);
612
743
  }
744
+ if (this.flatOffset && this.renderOptions.flatBeamOffset === this.flatOffset.after) {
745
+ this.renderOptions.flatBeamOffset = this.flatOffset.before;
746
+ }
747
+ this.fittedStems.length = 0;
748
+ this.flatOffset = undefined;
749
+ this.postFormatted = false;
613
750
  }
614
751
  preFormat() {
615
752
  return this;
@@ -618,10 +755,13 @@ export class Beam extends Element {
618
755
  if (this.postFormatted)
619
756
  return;
620
757
  if (isTabNote(this.notes[0]) || this.renderOptions.flatBeams) {
758
+ const before = this.renderOptions.flatBeamOffset;
621
759
  this.calculateFlatSlope();
760
+ this.flatOffset = { before, after: this.renderOptions.flatBeamOffset };
622
761
  }
623
762
  else {
624
763
  this.calculateSlope();
764
+ this.clearOtherVoiceHeads();
625
765
  }
626
766
  this.applyStemExtensions();
627
767
  this.postFormatted = true;
@@ -14,23 +14,31 @@ export class Bend extends Modifier {
14
14
  return 1;
15
15
  }
16
16
  static format(bends, state) {
17
+ var _a;
17
18
  if (!bends || bends.length === 0)
18
19
  return false;
19
- let lastWidth = 0;
20
- for (let i = 0; i < bends.length; ++i) {
21
- const bend = bends[i];
20
+ const chords = new Map();
21
+ for (const bend of bends) {
22
22
  const note = bend.checkAttachedNote();
23
+ const chord = (_a = chords.get(note)) !== null && _a !== void 0 ? _a : [];
24
+ chord.push(bend);
25
+ chords.set(note, chord);
26
+ if (isTabNote(note))
27
+ state.topTextLine = Math.max(state.topTextLine, note.leastString() - 1);
28
+ }
29
+ let width = 0;
30
+ for (const [note, chord] of chords) {
23
31
  if (isTabNote(note)) {
24
- const stringPos = note.leastString() - 1;
25
- if (state.topTextLine < stringPos) {
26
- state.topTextLine = stringPos;
27
- }
32
+ const positions = note.getPositions();
33
+ chord.sort((a, b) => positions[a.checkIndex()].str - positions[b.checkIndex()].str);
34
+ }
35
+ for (const bend of chord) {
36
+ bend.setXShift(width);
37
+ width = bend.getWidth();
38
+ bend.setTextLine(state.topTextLine);
28
39
  }
29
- bend.setXShift(lastWidth);
30
- lastWidth = bend.getWidth();
31
- bend.setTextLine(state.topTextLine);
32
40
  }
33
- state.rightShift += lastWidth;
41
+ state.rightShift += width;
34
42
  state.topTextLine += 1;
35
43
  return true;
36
44
  }
@@ -90,7 +98,7 @@ export class Bend extends Modifier {
90
98
  return this;
91
99
  }
92
100
  draw() {
93
- var _a;
101
+ var _a, _b;
94
102
  const ctx = this.checkContext();
95
103
  const note = this.checkAttachedNote();
96
104
  this.setRendered();
@@ -143,11 +151,8 @@ export class Bend extends Modifier {
143
151
  }
144
152
  for (let i = 0; i < this.phrase.length; ++i) {
145
153
  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);
154
+ const drawWidth = ((_b = bend.drawWidth) !== null && _b !== void 0 ? _b : 0) + (i === 0 ? xShift : 0);
155
+ lastDrawnWidth = drawWidth + lastBendDrawWidth - (i === 1 ? xShift : 0);
151
156
  if (bend.type === Bend.UP) {
152
157
  if (lastBend && lastBend.type === Bend.UP) {
153
158
  renderArrowHead(start.x, bendHeight, +1);
@@ -163,13 +168,13 @@ export class Bend extends Modifier {
163
168
  renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
164
169
  }
165
170
  if (!lastBend) {
166
- lastDrawnWidth = bend.drawWidth;
171
+ lastDrawnWidth = drawWidth;
167
172
  renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
168
173
  }
169
174
  }
170
175
  renderText(start.x + lastDrawnWidth, bend.text);
171
176
  lastBend = bend;
172
- lastBendDrawWidth = bend.drawWidth;
177
+ lastBendDrawWidth = drawWidth;
173
178
  lastBend.x = start.x;
174
179
  start.x += lastDrawnWidth;
175
180
  }
@@ -73,6 +73,9 @@ export class Clef extends StaveModifier {
73
73
  this.setType(type, size, annotation);
74
74
  L('Creating clef:', type);
75
75
  }
76
+ getType() {
77
+ return this.type;
78
+ }
76
79
  setType(type, size = 'default', annotation) {
77
80
  this.type = type;
78
81
  this.code = Clef.types[type].code;
@@ -30,6 +30,10 @@ export class ClefNote extends Note {
30
30
  this.clef.renderText(ctx, 0, 0);
31
31
  }
32
32
  getBoundingBox() {
33
- return this.clef.getBoundingBox();
33
+ const box = this.clef.getBoundingBox();
34
+ const stave = this.getStave();
35
+ if (stave)
36
+ box.setY(stave.getYForLine(this.clef.line) + this.clef.getYShift() - this.clef.getTextMetrics().actualBoundingBoxAscent);
37
+ return box;
34
38
  }
35
39
  }
@@ -46,7 +46,7 @@ export class Curve extends Element {
46
46
  }
47
47
  constructor(from, to, options) {
48
48
  super();
49
- this.renderOptions = Object.assign({ thickness: 2, xShift: 0, yShift: 10, position: CurvePosition.NEAR_HEAD, positionEnd: CurvePosition.NEAR_HEAD, invert: false, cps: [
49
+ this.renderOptions = Object.assign({ thickness: 2, xShift: 0, yShift: 10, position: 'auto', positionEnd: 'auto', invert: false, cps: [
50
50
  { x: 0, y: 10 },
51
51
  { x: 0, y: 10 },
52
52
  ], openingDirection: 'auto' }, options);
@@ -105,60 +105,30 @@ export class Curve extends Element {
105
105
  getRenderParams() {
106
106
  const firstNote = this.from;
107
107
  const lastNote = this.to;
108
- let firstX;
109
- let lastX;
110
- let firstY;
111
- let lastY;
112
- let stemDirection = 0;
113
- let metric = 'baseY';
114
- let endMetric = 'baseY';
115
- function getPosition(position) {
116
- return typeof position === 'string' ? Curve.PositionString[position] : position;
117
- }
118
- const position = getPosition(this.renderOptions.position);
119
- const positionEnd = getPosition(this.renderOptions.positionEnd);
120
- if (position === CurvePosition.NEAR_TOP) {
121
- metric = 'topY';
122
- endMetric = 'topY';
123
- }
124
- if (positionEnd === CurvePosition.NEAR_HEAD) {
125
- endMetric = 'baseY';
126
- }
127
- else if (positionEnd === CurvePosition.NEAR_TOP) {
128
- endMetric = 'topY';
129
- }
130
- if (firstNote) {
131
- firstX = firstNote.getTieRightX();
132
- stemDirection = firstNote.getStemDirection();
133
- firstY = firstNote.getStemExtents()[metric];
134
- }
135
- else {
136
- const stave = lastNote.checkStave();
137
- firstX = stave.getTieStartX();
138
- firstY = lastNote.getStemExtents()[metric];
139
- }
140
- if (lastNote) {
141
- lastX = lastNote.getTieLeftX();
142
- stemDirection = lastNote.getStemDirection();
143
- lastY = lastNote.getStemExtents()[endMetric];
144
- }
145
- else {
146
- const stave = firstNote.checkStave();
147
- lastX = stave.getTieEndX();
148
- lastY = firstNote.getStemExtents()[endMetric];
149
- }
150
- if (this.renderOptions.openingDirection === 'up') {
151
- stemDirection = 1;
152
- }
153
- if (this.renderOptions.openingDirection === 'down') {
154
- stemDirection = -1;
108
+ const start = firstNote !== null && firstNote !== void 0 ? firstNote : lastNote;
109
+ const end = lastNote !== null && lastNote !== void 0 ? lastNote : firstNote;
110
+ const { openingDirection, invert } = this.renderOptions;
111
+ let direction = end.getStemDirection();
112
+ if (openingDirection === 'up')
113
+ direction = 1;
114
+ if (openingDirection === 'down')
115
+ direction = -1;
116
+ if (invert)
117
+ direction *= -1;
118
+ function endpointY(note, position) {
119
+ const extents = note.getStemExtents();
120
+ const onStemSide = direction === -note.getStemDirection();
121
+ const useTip = position === CurvePosition.NEAR_TOP ||
122
+ position === 'nearTop' ||
123
+ (position === 'auto' && openingDirection !== 'auto' && onStemSide);
124
+ return useTip ? extents.topY : extents.baseY;
155
125
  }
156
126
  return {
157
- firstX,
158
- lastX,
159
- firstY,
160
- lastY,
161
- direction: stemDirection * (this.renderOptions.invert === true ? -1 : 1),
127
+ firstX: firstNote ? firstNote.getTieRightX() : end.checkStave().getTieStartX(),
128
+ lastX: lastNote ? lastNote.getTieLeftX() : start.checkStave().getTieEndX(),
129
+ firstY: endpointY(start, this.renderOptions.position),
130
+ lastY: endpointY(end, this.renderOptions.positionEnd),
131
+ direction,
162
132
  };
163
133
  }
164
134
  getBoundingBox() {
@@ -174,6 +144,10 @@ export class Curve extends Element {
174
144
  const minY = Math.min(...ys);
175
145
  return new BoundingBox(minX, minY, Math.max(...xs) - minX, Math.max(...ys) - minY);
176
146
  }
147
+ getAnchors() {
148
+ const { first, last } = this.getBezierPoints(this.getRenderParams());
149
+ return { start: first, end: last };
150
+ }
177
151
  draw() {
178
152
  this.checkContext();
179
153
  this.setRendered();