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
@@ -0,0 +1,172 @@
1
+ import { Accidental } from './accidental.js';
2
+ import { measureEngraving } from './engravinggeometry.js';
3
+ import { Fraction } from './fraction.js';
4
+ import { StaveNote } from './stavenote.js';
5
+ import { Tables } from './tables.js';
6
+ import { TabNote } from './tabnote.js';
7
+ import { RuntimeError } from './util.js';
8
+ const signs = {
9
+ '#': 'sharp',
10
+ '##': 'double sharp',
11
+ b: 'flat',
12
+ bb: 'double flat',
13
+ n: 'natural',
14
+ '+': 'quarter sharp',
15
+ d: 'quarter flat',
16
+ o: 'sori',
17
+ k: 'koron',
18
+ };
19
+ const lengths = {
20
+ '1/1': 'whole',
21
+ '1/2': 'half',
22
+ '1/4': 'quarter',
23
+ '1/8': 'eighth',
24
+ '1/16': 'sixteenth',
25
+ '3/4': 'dotted half',
26
+ '3/8': 'dotted quarter',
27
+ '3/16': 'dotted eighth',
28
+ };
29
+ function spokenPitch(key, sign) {
30
+ var _a, _b;
31
+ const [name, octave] = key.split('/');
32
+ const accidental = (_a = sign === null || sign === void 0 ? void 0 : sign.type) !== null && _a !== void 0 ? _a : name.slice(1).toLowerCase();
33
+ return [name[0].toUpperCase(), (_b = signs[accidental]) !== null && _b !== void 0 ? _b : accidental, octave].filter(Boolean).join(' ');
34
+ }
35
+ function describe(note) {
36
+ if (note.isRest())
37
+ return '';
38
+ const marks = note.notation.map((n) => n.label);
39
+ if (note instanceof TabNote) {
40
+ const positions = note.getPositions().map((p) => 'string ' + p.str + ' fret ' + p.fret);
41
+ return [...positions, ...note.notation.filter((n) => n.kind !== 'tuning').map((n) => n.label)].join(', ');
42
+ }
43
+ const unpitched = note.notation.filter((entry) => entry.kind === 'unpitched');
44
+ if (unpitched.some((entry) => entry.index === undefined))
45
+ return marks.join(', ');
46
+ const accidentals = note.getModifiers().filter((m) => m instanceof Accidental);
47
+ return [
48
+ ...note.getKeys().flatMap((key, i) => unpitched.some((entry) => entry.index === i)
49
+ ? []
50
+ : [
51
+ spokenPitch(key, accidentals.find((a) => a.getIndex() === i)),
52
+ ]),
53
+ ...marks,
54
+ ].join(', ');
55
+ }
56
+ function noteRegion(note, diagnostics) {
57
+ if (note instanceof StaveNote) {
58
+ const measured = measureEngraving(note);
59
+ diagnostics.push(...measured.diagnostics);
60
+ return measured.ink;
61
+ }
62
+ const ys = note.getYs();
63
+ const half = note.checkStave().getSpacingBetweenLines() / 2;
64
+ return {
65
+ x: note.getAbsoluteX() - note.getWidth() / 2,
66
+ y: Math.min(...ys) - half,
67
+ w: note.getWidth(),
68
+ h: Math.max(...ys) - Math.min(...ys) + 2 * half,
69
+ };
70
+ }
71
+ export class ScoreNavigation {
72
+ constructor(score, staves, sources = {}) {
73
+ var _a, _b, _c, _d, _e, _f;
74
+ const regions = [], diagnostics = [];
75
+ const events = [];
76
+ const sourceFor = (id) => (Object.prototype.hasOwnProperty.call(sources, id) ? sources[id] : undefined);
77
+ const seen = new Set();
78
+ const layout = score.sheet.getContinuousLayout();
79
+ const muteLabels = new Map();
80
+ for (const fragment of (_a = layout === null || layout === void 0 ? void 0 : layout.fragments) !== null && _a !== void 0 ? _a : []) {
81
+ if (!fragment.strings)
82
+ continue;
83
+ const strings = (fragment.strings.length === 1 ? 'string ' : 'strings ') + fragment.strings.join(', ');
84
+ for (const [id, text] of [
85
+ [fragment.from, 'Begin palm mute on '],
86
+ [fragment.to, 'Last palm-muted attack on '],
87
+ ]) {
88
+ if (id)
89
+ muteLabels.set(id, [...((_b = muteLabels.get(id)) !== null && _b !== void 0 ? _b : []), text + strings]);
90
+ }
91
+ }
92
+ for (const [measure, staffNotes] of score.notes.entries()) {
93
+ if (staffNotes.length !== staves.length)
94
+ throw new RuntimeError('BadArguments', 'Pass the staves in composition order.');
95
+ const ordered = [];
96
+ for (const [index, voices] of staffNotes.entries()) {
97
+ const staff = staves[index];
98
+ for (const [voice, notes] of voices.entries()) {
99
+ const clock = new Fraction(0, 1);
100
+ for (const note of notes) {
101
+ if (note.shouldIgnoreTicks())
102
+ continue;
103
+ const time = clock.clone();
104
+ clock.add(note.getTicks());
105
+ if (!(note instanceof StaveNote || note instanceof TabNote))
106
+ continue;
107
+ const id = note.getAttribute('id');
108
+ if (seen.has(id))
109
+ throw new RuntimeError('DuplicateID', 'Duplicate score event: ' + id);
110
+ seen.add(id);
111
+ const onset = time.clone().divide(Tables.RESOLUTION).simplify().toString();
112
+ const duration = note.getTicks().clone().divide(Tables.RESOLUTION).simplify().toString();
113
+ const length = (_c = lengths[duration]) !== null && _c !== void 0 ? _c : duration + ' of a whole';
114
+ const label = [
115
+ `Measure ${measure + 1}`,
116
+ staff.name || staff.id,
117
+ `voice ${voice + 1}`,
118
+ describe(note),
119
+ length + (note.isRest() ? ' rest' : ' note'),
120
+ ...((_d = muteLabels.get(id)) !== null && _d !== void 0 ? _d : []),
121
+ ]
122
+ .filter(Boolean)
123
+ .join(', ');
124
+ ordered.push({
125
+ time,
126
+ event: {
127
+ id,
128
+ source: sourceFor(id),
129
+ staff: staff.id,
130
+ measure: measure + 1,
131
+ voice: voice + 1,
132
+ onset,
133
+ duration,
134
+ label,
135
+ },
136
+ });
137
+ if (layout === null || layout === void 0 ? void 0 : layout.hidden.has(note))
138
+ continue;
139
+ const box = noteRegion(note, diagnostics);
140
+ if (box)
141
+ regions.push({ id, source: sourceFor(id), kind: 'event', x: box.x, y: box.y, w: box.w, h: box.h });
142
+ }
143
+ }
144
+ }
145
+ ordered.sort((a, b) => (a.time.equals(b.time) ? 0 : a.time.lessThan(b.time) ? -1 : 1));
146
+ events.push(...ordered.map((entry) => entry.event));
147
+ }
148
+ for (const fragment of (_e = layout === null || layout === void 0 ? void 0 : layout.fragments) !== null && _e !== void 0 ? _e : []) {
149
+ if (seen.has(fragment.id))
150
+ throw new RuntimeError('DuplicateID', 'Span shares a score event ID: ' + fragment.id);
151
+ const pad = Math.max(fragment.space, Math.abs((_f = fragment.rise) !== null && _f !== void 0 ? _f : 0));
152
+ regions.push(Object.assign(Object.assign({ id: fragment.id, row: fragment.row }, (fragment.strings && { strings: [...fragment.strings] })), { source: sourceFor(fragment.id), kind: 'span', x: Math.min(fragment.x1, fragment.x2), y: Math.min(fragment.y1, fragment.y2) - pad, w: Math.abs(fragment.x2 - fragment.x1), h: Math.abs(fragment.y2 - fragment.y1) + 2 * pad }));
153
+ }
154
+ this.events = events;
155
+ this.regions = regions;
156
+ this.diagnostics = diagnostics;
157
+ }
158
+ hitTest(x, y) {
159
+ if (!Number.isFinite(x) || !Number.isFinite(y))
160
+ return undefined;
161
+ let best;
162
+ for (const r of this.regions) {
163
+ if (x < r.x || x > r.x + r.w || y < r.y || y > r.y + r.h)
164
+ continue;
165
+ if (!best ||
166
+ (best.kind === 'span' && r.kind === 'event') ||
167
+ (best.kind === r.kind && r.w * r.h < best.w * best.h))
168
+ best = r;
169
+ }
170
+ return best;
171
+ }
172
+ }
@@ -4,6 +4,13 @@ import { StaveNote } from './stavenote.js';
4
4
  import { StemmableNote } from './stemmablenote.js';
5
5
  import { RuntimeError } from './util.js';
6
6
  export class Sheet {
7
+ setContinuousLayout(layout) {
8
+ this.continuous = layout;
9
+ return this;
10
+ }
11
+ getContinuousLayout() {
12
+ return this.continuous;
13
+ }
7
14
  constructor(options) {
8
15
  this.systems = [];
9
16
  this.rows = [];
@@ -38,6 +45,10 @@ export class Sheet {
38
45
  return last ? last.y + last.height : this.options.y;
39
46
  }
40
47
  format() {
48
+ if (this.continuous) {
49
+ this.rows = this.continuous.format(this.options);
50
+ return this;
51
+ }
41
52
  const widths = this.measureSystems();
42
53
  this.rows = [];
43
54
  let y = this.options.y;
@@ -74,8 +85,9 @@ export class Sheet {
74
85
  return groups;
75
86
  }
76
87
  verticalBounds(system) {
77
- let top = 0;
78
- let bottom = system.getBoundingBox().getH();
88
+ const envelope = system.getBoundingBox();
89
+ let top = Math.min(0, envelope.y);
90
+ let bottom = envelope.y + envelope.h;
79
91
  for (const voice of system.getVoices()) {
80
92
  for (const note of voice.getTickables()) {
81
93
  if (note instanceof StaveNote || note instanceof ClefNote) {
@@ -15,21 +15,27 @@ import { TimeSignature } from './timesignature.js';
15
15
  import { isBarline } from './typeguard.js';
16
16
  import { RuntimeError } from './util.js';
17
17
  const SORT_ORDER_BEG_MODIFIERS = {
18
- [Barline.CATEGORY]: 0,
19
- [Clef.CATEGORY]: 1,
20
- [KeySignature.CATEGORY]: 2,
21
- [TimeSignature.CATEGORY]: 3,
18
+ ["Barline"]: 0,
19
+ ["Clef"]: 1,
20
+ ["KeySignature"]: 2,
21
+ ["TimeSignature"]: 3,
22
22
  };
23
23
  const SORT_ORDER_END_MODIFIERS = {
24
- [TimeSignature.CATEGORY]: 0,
25
- [KeySignature.CATEGORY]: 1,
26
- [Barline.CATEGORY]: 2,
27
- [Clef.CATEGORY]: 3,
24
+ ["TimeSignature"]: 0,
25
+ ["KeySignature"]: 1,
26
+ ["Barline"]: 2,
27
+ ["Clef"]: 3,
28
28
  };
29
29
  export class Stave extends Element {
30
30
  static get CATEGORY() {
31
31
  return "Stave";
32
32
  }
33
+ setBoundaryModifiers(modifiers) {
34
+ this.boundaryModifiers = modifiers;
35
+ for (const mark of modifiers)
36
+ mark.setStave(this);
37
+ this.formatted = false;
38
+ }
33
39
  static get defaultPadding() {
34
40
  return Metrics.get('Stave.padding') + Metrics.get('Stave.endPaddingMax');
35
41
  }
@@ -39,6 +45,7 @@ export class Stave extends Element {
39
45
  constructor(x, y, width, options) {
40
46
  var _a;
41
47
  super();
48
+ this.boundaryModifiers = [];
42
49
  this.x = x;
43
50
  this.y = y;
44
51
  this.width = width;
@@ -49,7 +56,7 @@ export class Stave extends Element {
49
56
  this.measure = 0;
50
57
  this.clef = (_a = options === null || options === void 0 ? void 0 : options.clef) !== null && _a !== void 0 ? _a : 'treble';
51
58
  this.endClef = undefined;
52
- this.options = Object.assign({ clef: 'treble', verticalBarWidth: 10, numLines: 5, leftBar: true, rightBar: true, spacingBetweenLinesPx: Tables.STAVE_LINE_DISTANCE, spaceAboveStaffLn: 4, spaceBelowStaffLn: 4, topTextPosition: 1, bottomTextPosition: 4, lineConfig: [] }, options);
59
+ this.options = Object.assign({ clef: 'treble', barlineSpan: null, verticalBarWidth: 10, numLines: 5, leftBar: true, rightBar: true, spacingBetweenLinesPx: Tables.STAVE_LINE_DISTANCE, spaceAboveStaffLn: 4, spaceBelowStaffLn: 4, topTextPosition: 1, bottomTextPosition: 4, lineConfig: [] }, options);
53
60
  this.bounds = { x: this.x, y: this.y, w: this.width, h: 0 };
54
61
  this.defaultLedgerLineStyle = { lineWidth: 2 };
55
62
  this.resetLines();
@@ -106,6 +113,15 @@ export class Stave extends Element {
106
113
  this.resetLines();
107
114
  return this;
108
115
  }
116
+ getBarlineBounds() {
117
+ var _a;
118
+ const span = this.options.barlineSpan;
119
+ if (!span)
120
+ return { topY: this.getTopLineTopY(), botY: this.getBottomLineBottomY() };
121
+ if (!Number.isFinite(span.top) || !Number.isFinite(span.bottom) || span.top >= span.bottom)
122
+ throw new RuntimeError('StaveConfigError', 'Barline endpoints must increase from top to bottom.');
123
+ return { topY: this.getYForLine(span.top), botY: this.getYForLine(span.bottom) + ((_a = this.getStyle().lineWidth) !== null && _a !== void 0 ? _a : 1) };
124
+ }
109
125
  getTopLineTopY() {
110
126
  return this.getYForLine(0);
111
127
  }
@@ -285,7 +301,8 @@ export class Stave extends Element {
285
301
  return this;
286
302
  }
287
303
  getEndClef() {
288
- return this.endClef;
304
+ const courtesy = this.boundaryModifiers.find((m) => m instanceof Clef && m.getPosition() === StaveModifierPosition.END);
305
+ return courtesy instanceof Clef ? courtesy.getType() : this.endClef;
289
306
  }
290
307
  setKeySignature(keySpec, cancelKeySpec, position) {
291
308
  if (position === undefined) {
@@ -355,19 +372,20 @@ export class Stave extends Element {
355
372
  return this;
356
373
  }
357
374
  getModifiers(position, category) {
375
+ const modifiers = this.boundaryModifiers.length ? this.modifiers.concat(this.boundaryModifiers) : this.modifiers;
358
376
  const noPosition = position === undefined;
359
377
  const noCategory = category === undefined;
360
378
  if (noPosition && noCategory) {
361
- return this.modifiers;
379
+ return modifiers;
362
380
  }
363
381
  else if (noPosition) {
364
- return this.modifiers.filter((m) => category === m.getCategory());
382
+ return modifiers.filter((m) => category === m.getCategory());
365
383
  }
366
384
  else if (noCategory) {
367
- return this.modifiers.filter((m) => position === m.getPosition());
385
+ return modifiers.filter((m) => position === m.getPosition());
368
386
  }
369
387
  else {
370
- return this.modifiers.filter((m) => position === m.getPosition() && category === m.getCategory());
388
+ return modifiers.filter((m) => position === m.getPosition() && category === m.getCategory());
371
389
  }
372
390
  }
373
391
  sortByCategory(items, order) {
@@ -482,8 +500,7 @@ export class Stave extends Element {
482
500
  }
483
501
  this.drawPointerRect();
484
502
  ctx.closeGroup();
485
- for (let i = 0; i < this.modifiers.length; i++) {
486
- const modifier = this.modifiers[i];
503
+ for (const modifier of this.getModifiers()) {
487
504
  modifier.setContext(ctx);
488
505
  modifier.setStave(this);
489
506
  modifier.drawWithStyle();
@@ -154,8 +154,7 @@ export class Barline extends StaveModifier {
154
154
  }
155
155
  drawVerticalBar(stave, x, doubleBar) {
156
156
  const staveCtx = stave.checkContext();
157
- const topY = stave.getTopLineTopY();
158
- const botY = stave.getBottomLineBottomY();
157
+ const { topY, botY } = stave.getBarlineBounds();
159
158
  if (doubleBar) {
160
159
  staveCtx.fillRect(x - 3, topY, 1, botY - topY);
161
160
  }
@@ -163,8 +162,7 @@ export class Barline extends StaveModifier {
163
162
  }
164
163
  drawVerticalEndBar(stave, x) {
165
164
  const staveCtx = stave.checkContext();
166
- const topY = stave.getTopLineTopY();
167
- const botY = stave.getBottomLineBottomY();
165
+ const { topY, botY } = stave.getBarlineBounds();
168
166
  staveCtx.fillRect(x - 5, topY, 1, botY - topY);
169
167
  staveCtx.fillRect(x - 2, topY, 3, botY - topY);
170
168
  }
@@ -198,8 +196,7 @@ export class Barline extends StaveModifier {
198
196
  }
199
197
  drawRepeatBar(stave, x, begin) {
200
198
  const staveCtx = stave.checkContext();
201
- const topY = stave.getTopLineTopY();
202
- const botY = stave.getBottomLineBottomY();
199
+ const { topY, botY } = stave.getBarlineBounds();
203
200
  let xShift = 3;
204
201
  if (!begin) {
205
202
  xShift = -5;
@@ -223,7 +220,8 @@ export class Barline extends StaveModifier {
223
220
  xShift -= 4;
224
221
  }
225
222
  const dotX = x + xShift + dotRadius / 2;
226
- let yOffset = (stave.getNumLines() - 1) * stave.getSpacingBetweenLines();
223
+ const span = stave.options.barlineSpan;
224
+ let yOffset = (span ? span.bottom - span.top : stave.getNumLines() - 1) * stave.getSpacingBetweenLines();
227
225
  yOffset = yOffset / 2 - stave.getSpacingBetweenLines() / 2;
228
226
  let dotY = topY + yOffset + dotRadius / 2;
229
227
  staveCtx.beginPath();
@@ -113,6 +113,7 @@ export class StaveConnector extends Element {
113
113
  break;
114
114
  }
115
115
  case StaveConnector.type.BRACKET:
116
+ topX += this.xShift;
116
117
  topY -= 6;
117
118
  botY += 6;
118
119
  attachmentHeight = botY - topY;
@@ -1,3 +1,4 @@
1
+ import { BoundingBox } from './boundingbox.js';
1
2
  import { Element } from './element.js';
2
3
  import { Modifier } from './modifier.js';
3
4
  import { RuntimeError } from './util.js';
@@ -32,6 +33,8 @@ export class StaveHairpin extends Element {
32
33
  }
33
34
  constructor(notes, type) {
34
35
  super();
36
+ this.layoutLeft = 0;
37
+ this.layoutRight = 0;
35
38
  this.setNotes(notes);
36
39
  this.hairpin = type;
37
40
  this.position = Modifier.Position.BELOW;
@@ -63,27 +66,62 @@ export class StaveHairpin extends Element {
63
66
  this.lastNote = notes.lastNote;
64
67
  return this;
65
68
  }
69
+ getNotes() {
70
+ return [this.firstNote, this.lastNote];
71
+ }
72
+ setLayoutBaseline(y, left = 0, right = 0) {
73
+ this.layoutBaseline = y;
74
+ this.layoutLeft = left;
75
+ this.layoutRight = right;
76
+ }
77
+ parameters() {
78
+ const first = this.firstNote, last = this.lastNote;
79
+ if (!first || !last)
80
+ throw new RuntimeError('NoNote', 'Notes required to draw');
81
+ return {
82
+ firstX: first.getModifierStartXY(this.position, 0).x,
83
+ lastX: last.getModifierStartXY(this.position, 0).x,
84
+ firstY: first.checkStave().getY() + first.checkStave().getHeight(),
85
+ lastY: last.checkStave().getY() + last.checkStave().getHeight(),
86
+ staffHeight: first.checkStave().getHeight(),
87
+ };
88
+ }
89
+ edges(params) {
90
+ const { yShift, height, leftShiftPx, rightShiftPx } = this.renderOptions;
91
+ const above = this.position === Modifier.Position.ABOVE;
92
+ const top = this.layoutBaseline === undefined
93
+ ? params.firstY + (above ? 10 - params.staffHeight - yShift : 20 + yShift)
94
+ : this.layoutBaseline - height / 2 + (above ? -yShift : yShift);
95
+ return {
96
+ top,
97
+ left: params.firstX + leftShiftPx + this.layoutLeft,
98
+ right: params.lastX + rightShiftPx - this.layoutRight,
99
+ height,
100
+ };
101
+ }
102
+ getLayoutEdges() {
103
+ return this.edges(this.parameters());
104
+ }
105
+ getBoundingBox() {
106
+ var _a;
107
+ const { top, left, right, height } = this.getLayoutEdges();
108
+ const pad = ((_a = this.getStyle().lineWidth) !== null && _a !== void 0 ? _a : 1) / 2;
109
+ return new BoundingBox(Math.min(left, right) - pad, Math.min(top, top + height) - pad, Math.abs(right - left) + 2 * pad, Math.abs(height) + 2 * pad);
110
+ }
66
111
  renderHairpin(params) {
67
112
  const ctx = this.checkContext();
68
- let dis = this.renderOptions.yShift + 20;
69
- let yShift = params.firstY;
70
- if (this.position === Modifier.Position.ABOVE) {
71
- dis = -dis + 30;
72
- yShift = params.firstY - params.staffHeight;
73
- }
74
- const leftShiftPx = this.renderOptions.leftShiftPx;
75
- const rightShiftPx = this.renderOptions.rightShiftPx;
113
+ const { top, left, right, height } = this.edges(params);
76
114
  ctx.beginPath();
77
115
  switch (this.hairpin) {
78
116
  case StaveHairpin.type.CRESC:
79
- ctx.moveTo(params.lastX + rightShiftPx, yShift + dis);
80
- ctx.lineTo(params.firstX + leftShiftPx, yShift + this.renderOptions.height / 2 + dis);
81
- ctx.lineTo(params.lastX + rightShiftPx, yShift + this.renderOptions.height + dis);
117
+ ctx.moveTo(right, top);
118
+ ctx.lineTo(left, top + height / 2);
119
+ ctx.lineTo(right, top + height);
82
120
  break;
83
121
  case StaveHairpin.type.DECRESC:
84
- ctx.moveTo(params.firstX + leftShiftPx, yShift + dis);
85
- ctx.lineTo(params.lastX + rightShiftPx, yShift + this.renderOptions.height / 2 + dis);
86
- ctx.lineTo(params.firstX + leftShiftPx, yShift + this.renderOptions.height + dis);
122
+ ctx.moveTo(left, top);
123
+ ctx.lineTo(right, top + height / 2);
124
+ ctx.lineTo(left, top + height);
87
125
  break;
88
126
  default:
89
127
  break;
@@ -94,19 +132,7 @@ export class StaveHairpin extends Element {
94
132
  draw() {
95
133
  this.checkContext();
96
134
  this.setRendered();
97
- const firstNote = this.firstNote;
98
- const lastNote = this.lastNote;
99
- if (!firstNote || !lastNote)
100
- throw new RuntimeError('NoNote', 'Notes required to draw');
101
- const start = firstNote.getModifierStartXY(this.position, 0);
102
- const end = lastNote.getModifierStartXY(this.position, 0);
103
- this.renderHairpin({
104
- firstX: start.x,
105
- lastX: end.x,
106
- firstY: firstNote.checkStave().getY() + firstNote.checkStave().getHeight(),
107
- lastY: lastNote.checkStave().getY() + lastNote.checkStave().getHeight(),
108
- staffHeight: firstNote.checkStave().getHeight(),
109
- });
135
+ this.renderHairpin(this.parameters());
110
136
  }
111
137
  }
112
138
  StaveHairpin.type = {