vexflow 5.2.0 → 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 (114) hide show
  1. package/build/cjs/vexflow-bravura.js +2 -2
  2. package/build/cjs/vexflow-bravura.js.map +1 -1
  3. package/build/cjs/vexflow-core.js +2 -2
  4. package/build/cjs/vexflow-core.js.map +1 -1
  5. package/build/cjs/vexflow-debug-with-tests.js +2759 -1382
  6. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  7. package/build/cjs/vexflow-debug.js +1814 -1227
  8. package/build/cjs/vexflow-debug.js.map +1 -1
  9. package/build/cjs/vexflow.js +2 -2
  10. package/build/cjs/vexflow.js.map +1 -1
  11. package/build/esm/src/accidental.js +10 -3
  12. package/build/esm/src/articulation.js +24 -6
  13. package/build/esm/src/beam.js +158 -28
  14. package/build/esm/src/bend.js +24 -19
  15. package/build/esm/src/curve.js +27 -53
  16. package/build/esm/src/dot.js +13 -4
  17. package/build/esm/src/easyscore/composition.js +40 -1
  18. package/build/esm/src/easyscore/policies.js +34 -6
  19. package/build/esm/src/element.js +4 -0
  20. package/build/esm/src/engravinggeometry.js +150 -0
  21. package/build/esm/src/engravingprofile.js +44 -0
  22. package/build/esm/src/factory.js +15 -7
  23. package/build/esm/src/formatter.js +9 -1
  24. package/build/esm/src/gracenote.js +43 -38
  25. package/build/esm/src/gracenotegroup.js +53 -56
  26. package/build/esm/src/index.js +2 -0
  27. package/build/esm/src/keysignature.js +1 -1
  28. package/build/esm/src/modifiercontext.js +11 -0
  29. package/build/esm/src/notehead.js +11 -6
  30. package/build/esm/src/ornament.js +38 -18
  31. package/build/esm/src/proportionalformatter.js +54 -6
  32. package/build/esm/src/stavenote.js +128 -115
  33. package/build/esm/src/stavetie.js +39 -7
  34. package/build/esm/src/stem.js +26 -11
  35. package/build/esm/src/tickcontext.js +19 -0
  36. package/build/esm/src/timesignature.js +5 -0
  37. package/build/esm/src/tuplet.js +57 -17
  38. package/build/esm/src/version.js +3 -3
  39. package/build/esm/src/vexflow.js +4 -0
  40. package/build/esm/tests/bend_tests.js +14 -1
  41. package/build/esm/tests/curve_tests.js +14 -4
  42. package/build/esm/tests/demo_repertoire_tests.js +27 -0
  43. package/build/esm/tests/demos/drums.js +54 -0
  44. package/build/esm/tests/demos/electric.js +63 -0
  45. package/build/esm/tests/demos/greensleeves.js +90 -0
  46. package/build/esm/tests/demos/joy.js +61 -0
  47. package/build/esm/tests/demos/kesh.js +80 -0
  48. package/build/esm/tests/demos/prelude.js +59 -0
  49. package/build/esm/tests/demos/quartertones.js +50 -0
  50. package/build/esm/tests/demos/saints.js +65 -0
  51. package/build/esm/tests/engraving/score-layout.mjs +75 -0
  52. package/build/esm/tests/engraving/score-persistence.mjs +262 -0
  53. package/build/esm/tests/engraving/studies.mjs +279 -0
  54. package/build/esm/tests/formatter_tests.js +10 -1
  55. package/build/esm/tests/index.js +1 -0
  56. package/build/esm/tests/node/audit-dependencies-tests.mjs +42 -0
  57. package/build/esm/tests/node/easyscore-tests.mjs +71 -0
  58. package/build/esm/tests/node/engraving-layout-tests.mjs +449 -0
  59. package/build/esm/tests/node/local-engraving-tests.mjs +128 -0
  60. package/build/esm/tests/node/proportional-spacing-tests.mjs +64 -0
  61. package/build/esm/tests/node/rendering-tests.mjs +6 -2
  62. package/build/esm/tests/threevoice_tests.js +11 -1
  63. package/build/esm/tests/vexflow_test_helpers.js +19 -10
  64. package/build/esm/tools/audit-dependencies.mjs +61 -0
  65. package/build/esm/tools/score-fixtures.mjs +23 -6
  66. package/build/types/src/accidental.d.ts +4 -0
  67. package/build/types/src/articulation.d.ts +6 -1
  68. package/build/types/src/beam.d.ts +14 -0
  69. package/build/types/src/curve.d.ts +9 -0
  70. package/build/types/src/dot.d.ts +5 -0
  71. package/build/types/src/easyscore/composition.d.ts +19 -0
  72. package/build/types/src/easyscore/policies.d.ts +1 -1
  73. package/build/types/src/element.d.ts +2 -0
  74. package/build/types/src/engravinggeometry.d.ts +31 -0
  75. package/build/types/src/engravingprofile.d.ts +20 -0
  76. package/build/types/src/factory.d.ts +6 -1
  77. package/build/types/src/formatter.d.ts +3 -3
  78. package/build/types/src/gracenote.d.ts +7 -0
  79. package/build/types/src/gracenotegroup.d.ts +7 -2
  80. package/build/types/src/index.d.ts +2 -0
  81. package/build/types/src/modifiercontext.d.ts +4 -0
  82. package/build/types/src/notehead.d.ts +5 -0
  83. package/build/types/src/ornament.d.ts +7 -0
  84. package/build/types/src/proportionalformatter.d.ts +11 -2
  85. package/build/types/src/stavenote.d.ts +20 -2
  86. package/build/types/src/stavetie.d.ts +4 -0
  87. package/build/types/src/stem.d.ts +10 -1
  88. package/build/types/src/tickcontext.d.ts +5 -0
  89. package/build/types/src/timesignature.d.ts +2 -0
  90. package/build/types/src/tuplet.d.ts +5 -48
  91. package/build/types/src/vexflow.d.ts +4 -0
  92. package/changelog/CHANGELOG.md +17 -0
  93. package/package.json +7 -2
  94. package/tests/all.html +277 -0
  95. package/tests/demo_petzold_tests.ts +435 -0
  96. package/tests/demos/drums.ts +55 -0
  97. package/tests/demos/drums.webp +0 -0
  98. package/tests/demos/electric.ts +70 -0
  99. package/tests/demos/electric.webp +0 -0
  100. package/tests/demos/greensleeves.ts +97 -0
  101. package/tests/demos/greensleeves.webp +0 -0
  102. package/tests/demos/joy.ts +65 -0
  103. package/tests/demos/joy.webp +0 -0
  104. package/tests/demos/kesh.ts +86 -0
  105. package/tests/demos/kesh.webp +0 -0
  106. package/tests/demos/prelude.ts +65 -0
  107. package/tests/demos/prelude.webp +0 -0
  108. package/tests/demos/quartertones.ts +51 -0
  109. package/tests/demos/quartertones.webp +0 -0
  110. package/tests/demos/saints.ts +70 -0
  111. package/tests/demos/saints.webp +0 -0
  112. package/tests/flow.html +550 -239
  113. package/tests/fonts.css +493 -0
  114. 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
  }
@@ -242,19 +242,21 @@ export class Articulation extends Modifier {
242
242
  if (!this.articulation) {
243
243
  this.articulation = { code: this.type, betweenLines: false };
244
244
  }
245
+ if (this.betweenLinesOverride !== undefined)
246
+ this.articulation = Object.assign(Object.assign({}, this.articulation), { betweenLines: this.betweenLinesOverride });
245
247
  const code = (this.position === ABOVE ? this.articulation.aboveCode : this.articulation.belowCode) ||
246
248
  this.articulation.code ||
247
249
  Glyphs.null;
248
250
  this.text = code;
249
251
  }
250
252
  setBetweenLines(betweenLines = true) {
251
- this.articulation.betweenLines = betweenLines;
253
+ this.betweenLinesOverride = betweenLines;
254
+ this.articulation = Object.assign(Object.assign({}, this.articulation), { betweenLines });
252
255
  return this;
253
256
  }
254
- draw() {
255
- const ctx = this.checkContext();
257
+ getPlacement() {
256
258
  const note = this.checkAttachedNote();
257
- this.setRendered();
259
+ let centered = false;
258
260
  const index = this.checkIndex();
259
261
  const { position, textLine } = this;
260
262
  const canSitBetweenLines = this.articulation.betweenLines;
@@ -280,10 +282,26 @@ export class Articulation extends Modifier {
280
282
  const distanceFromNote = (note.getYs()[index] - y) / staffSpace;
281
283
  const articLine = distanceFromNote + Number(noteLine);
282
284
  const snappedLine = snapLineToStaff(canSitBetweenLines, articLine, position, offsetDirection);
283
- if (isWithinLines(snappedLine, position))
284
- this.setOrigin(0.5, 0.5);
285
+ centered = isWithinLines(snappedLine, position);
285
286
  y += Math.abs(snappedLine - articLine) * staffSpace * offsetDirection;
286
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();
287
305
  L(`Rendering articulation at (x: ${x}, y: ${y})`);
288
306
  this.x = x;
289
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';
@@ -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,37 +696,58 @@ 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;
622
715
  }
623
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);
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;
750
+ }
624
751
  preFormat() {
625
752
  return this;
626
753
  }
@@ -628,10 +755,13 @@ export class Beam extends Element {
628
755
  if (this.postFormatted)
629
756
  return;
630
757
  if (isTabNote(this.notes[0]) || this.renderOptions.flatBeams) {
758
+ const before = this.renderOptions.flatBeamOffset;
631
759
  this.calculateFlatSlope();
760
+ this.flatOffset = { before, after: this.renderOptions.flatBeamOffset };
632
761
  }
633
762
  else {
634
763
  this.calculateSlope();
764
+ this.clearOtherVoiceHeads();
635
765
  }
636
766
  this.applyStemExtensions();
637
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
  }
@@ -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();
@@ -82,7 +82,7 @@ export class Dot extends Modifier {
82
82
  }
83
83
  }
84
84
  if (note.isRest()) {
85
- dot.dotShiftY += -halfShiftY;
85
+ dot.dotShiftY = dot.restShiftY - halfShiftY;
86
86
  }
87
87
  else {
88
88
  dot.dotShiftY = -halfShiftY;
@@ -99,6 +99,7 @@ export class Dot extends Modifier {
99
99
  }
100
100
  constructor() {
101
101
  super();
102
+ this.restShiftY = 0;
102
103
  this.position = Modifier.Position.RIGHT;
103
104
  this.setText(Glyphs.augmentationDot);
104
105
  this.dotShiftY = 0;
@@ -109,13 +110,12 @@ export class Dot extends Modifier {
109
110
  return this;
110
111
  }
111
112
  setDotShiftY(y) {
113
+ this.restShiftY = y;
112
114
  this.dotShiftY = y;
113
115
  return this;
114
116
  }
115
- draw() {
116
- const ctx = this.checkContext();
117
+ positionGlyph() {
117
118
  const note = this.checkAttachedNote();
118
- this.setRendered();
119
119
  const stave = note.checkStave();
120
120
  const lineSpace = stave.getSpacingBetweenLines();
121
121
  const start = note.getModifierStartXY(this.position, this.index, { forceFlagRight: true });
@@ -124,6 +124,15 @@ export class Dot extends Modifier {
124
124
  }
125
125
  this.x = start.x;
126
126
  this.y = start.y + this.dotShiftY * lineSpace;
127
+ }
128
+ getInkBounds() {
129
+ this.positionGlyph();
130
+ return this.getTextInk(this.x, this.y);
131
+ }
132
+ draw() {
133
+ const ctx = this.checkContext();
134
+ this.positionGlyph();
135
+ this.setRendered();
127
136
  this.renderText(ctx, 0, 0);
128
137
  }
129
138
  }
@@ -1,11 +1,43 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
1
12
  import { Factory } from '../factory.js';
2
13
  import { Fraction } from '../fraction.js';
14
+ import { Modifier } from '../modifier.js';
3
15
  import { Note } from '../note.js';
4
16
  import { StemmableNote } from '../stemmablenote.js';
5
17
  import { Tickable } from '../tickable.js';
6
18
  import { RuntimeError } from '../util.js';
7
19
  import { Voice } from '../voice.js';
8
20
  import { applyCompositionAccidentals } from './policies.js';
21
+ function buildGraceGroup(score, { side, notes, slur, beam }) {
22
+ if (!['before', 'after'].includes(side) || !notes.length)
23
+ invalid('Expected a nonempty before or after grace group.');
24
+ const children = notes.map((_a) => {
25
+ var { accidentals } = _a, spec = __rest(_a, ["accidentals"]);
26
+ const child = score.make.graceNote(spec);
27
+ for (const { index, type, cautionary } of accidentals !== null && accidentals !== void 0 ? accidentals : []) {
28
+ const mark = score.make.accidental({ type });
29
+ if (cautionary)
30
+ mark.setAsCautionary();
31
+ score.attach(child, mark, index);
32
+ }
33
+ return child;
34
+ });
35
+ const group = score.make.graceNoteGroup({ notes: children, slur });
36
+ group.setPosition(side === 'after' ? Modifier.Position.RIGHT : Modifier.Position.LEFT);
37
+ if (beam)
38
+ group.beamNotes();
39
+ return group;
40
+ }
9
41
  function invalid(message) {
10
42
  throw new RuntimeError('BadComposition', message);
11
43
  }
@@ -63,6 +95,9 @@ function buildVoice(score, spec, initial, display) {
63
95
  const note = 'tab' in item
64
96
  ? score.make.tabNote(item.tab, item.drawStem)
65
97
  : score.make.note('note' in item ? item.note : item);
98
+ if ('grace' in item && item.grace) {
99
+ score.attach(note, buildGraceGroup(score, item.grace), 0);
100
+ }
66
101
  if ('id' in item && item.id !== undefined)
67
102
  note.setAttribute('id', item.id);
68
103
  if ('stave' in item && item.stave)
@@ -203,6 +238,10 @@ export function compose(score, spec) {
203
238
  invalid('All staves need the same number of measures.');
204
239
  if (spec.accidentals && !['literal', 'key-aware'].includes(spec.accidentals))
205
240
  invalid('Unknown accidental policy.');
241
+ if (spec.graceAccidentals && !['explicit', 'ordered'].includes(spec.graceAccidentals))
242
+ invalid('Unknown grace accidental policy.');
243
+ if (spec.graceAccidentals === 'ordered' && spec.accidentals !== 'key-aware')
244
+ invalid('Ordered grace accidentals require key-aware accidentals.');
206
245
  if (spec.signatures && !['changes', 'every-measure'].includes(spec.signatures))
207
246
  invalid('Unknown signature policy.');
208
247
  if (spec.crossMeasureSpans && !['reject', 'manual'].includes(spec.crossMeasureSpans))
@@ -272,7 +311,7 @@ export function compose(score, spec) {
272
311
  });
273
312
  if (spec.accidentals === 'key-aware') {
274
313
  const generated = new Set(policies.flatMap((policy) => [...policy.generated]));
275
- policies.forEach((policy, staffIndex) => applyCompositionAccidentals(score, voices.flat(), policy.key, policy.changes, generated, staves[staffIndex]));
314
+ policies.forEach((policy, staffIndex) => applyCompositionAccidentals(score, voices.flat(), policy.key, policy.changes, generated, staves[staffIndex], spec.graceAccidentals === 'ordered'));
276
315
  }
277
316
  result.staves.push(staves);
278
317
  result.voices.push(voices);