vexflow-native 1.7.0 → 1.9.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 (129) hide show
  1. package/README.md +33 -348
  2. package/lib/module/base/FontManager.js +0 -0
  3. package/lib/module/base/FontManager.js.map +1 -1
  4. package/lib/module/base/VexflowRecordingContext.js +2 -0
  5. package/lib/module/base/VexflowRecordingContext.js.map +1 -1
  6. package/lib/module/base/index.js +1 -0
  7. package/lib/module/base/index.js.map +1 -1
  8. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +24 -7
  9. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
  10. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +11 -3
  11. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -1
  12. package/lib/module/musicxml/MusicXmlParser/Spanners.js +21 -5
  13. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -1
  14. package/lib/module/musicxml/MusicXmlParser/mappers.js +1 -1
  15. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
  16. package/lib/module/percussion/index.js +8 -0
  17. package/lib/module/percussion/index.js.map +1 -0
  18. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +4 -0
  19. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
  20. package/lib/module/renderer/fixedNoteSpacing.js +56 -0
  21. package/lib/module/renderer/fixedNoteSpacing.js.map +1 -0
  22. package/lib/module/renderer/index.js +1 -0
  23. package/lib/module/renderer/index.js.map +1 -1
  24. package/lib/module/renderer/itemLayout.js +108 -0
  25. package/lib/module/renderer/itemLayout.js.map +1 -0
  26. package/lib/module/renderer/measure.js +31 -102
  27. package/lib/module/renderer/measure.js.map +1 -1
  28. package/lib/module/renderer/measureBounds.js +73 -0
  29. package/lib/module/renderer/measureBounds.js.map +1 -0
  30. package/lib/module/renderer/measureModifiers.js +99 -0
  31. package/lib/module/renderer/measureModifiers.js.map +1 -0
  32. package/lib/module/renderer/noteModifiers.js +212 -0
  33. package/lib/module/renderer/noteModifiers.js.map +1 -0
  34. package/lib/module/renderer/render.js +34 -115
  35. package/lib/module/renderer/render.js.map +1 -1
  36. package/lib/module/renderer/scale.js +16 -2
  37. package/lib/module/renderer/scale.js.map +1 -1
  38. package/lib/module/renderer/scoreParsing.js +37 -188
  39. package/lib/module/renderer/scoreParsing.js.map +1 -1
  40. package/lib/module/renderer/stave.js +37 -0
  41. package/lib/module/renderer/stave.js.map +1 -0
  42. package/lib/module/renderer/useScoreRecording.js +10 -3
  43. package/lib/module/renderer/useScoreRecording.js.map +1 -1
  44. package/lib/module/renderer/vfKeys.js +72 -0
  45. package/lib/module/renderer/vfKeys.js.map +1 -0
  46. package/lib/module/state/index.js +1 -0
  47. package/lib/module/state/index.js.map +1 -1
  48. package/lib/typescript/src/base/FontManager.d.ts +7 -0
  49. package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
  50. package/lib/typescript/src/base/VexflowRecordingContext.d.ts +1 -0
  51. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
  52. package/lib/typescript/src/base/index.d.ts +1 -0
  53. package/lib/typescript/src/base/index.d.ts.map +1 -1
  54. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +4 -2
  55. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
  56. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
  57. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +6 -1
  58. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -1
  59. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +6 -1
  60. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -1
  61. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
  62. package/lib/typescript/src/musicxml/index.d.ts +1 -0
  63. package/lib/typescript/src/musicxml/index.d.ts.map +1 -1
  64. package/lib/typescript/src/percussion/index.d.ts +4 -0
  65. package/lib/typescript/src/percussion/index.d.ts.map +1 -0
  66. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
  67. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts +17 -0
  68. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts.map +1 -0
  69. package/lib/typescript/src/renderer/index.d.ts +2 -0
  70. package/lib/typescript/src/renderer/index.d.ts.map +1 -1
  71. package/lib/typescript/src/renderer/itemLayout.d.ts +23 -0
  72. package/lib/typescript/src/renderer/itemLayout.d.ts.map +1 -0
  73. package/lib/typescript/src/renderer/layout.d.ts +1 -1
  74. package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
  75. package/lib/typescript/src/renderer/measure.d.ts +3 -6
  76. package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
  77. package/lib/typescript/src/renderer/measureBounds.d.ts +13 -0
  78. package/lib/typescript/src/renderer/measureBounds.d.ts.map +1 -0
  79. package/lib/typescript/src/renderer/measureModifiers.d.ts +20 -0
  80. package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -0
  81. package/lib/typescript/src/renderer/noteModifiers.d.ts +29 -0
  82. package/lib/typescript/src/renderer/noteModifiers.d.ts.map +1 -0
  83. package/lib/typescript/src/renderer/render.d.ts +2 -27
  84. package/lib/typescript/src/renderer/render.d.ts.map +1 -1
  85. package/lib/typescript/src/renderer/scale.d.ts.map +1 -1
  86. package/lib/typescript/src/renderer/scoreParsing.d.ts +14 -40
  87. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
  88. package/lib/typescript/src/renderer/stave.d.ts +11 -0
  89. package/lib/typescript/src/renderer/stave.d.ts.map +1 -0
  90. package/lib/typescript/src/renderer/types.d.ts +53 -4
  91. package/lib/typescript/src/renderer/types.d.ts.map +1 -1
  92. package/lib/typescript/src/renderer/useScoreRecording.d.ts +2 -2
  93. package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
  94. package/lib/typescript/src/renderer/vfKeys.d.ts +17 -0
  95. package/lib/typescript/src/renderer/vfKeys.d.ts.map +1 -0
  96. package/lib/typescript/src/state/index.d.ts +1 -0
  97. package/lib/typescript/src/state/index.d.ts.map +1 -1
  98. package/lib/typescript/src/state/types.d.ts +39 -6
  99. package/lib/typescript/src/state/types.d.ts.map +1 -1
  100. package/package.json +15 -5
  101. package/src/base/FontManager.ts +0 -0
  102. package/src/base/VexflowRecordingContext.ts +1 -0
  103. package/src/base/index.ts +1 -0
  104. package/src/musicxml/MusicXmlParser/InternalTypes.ts +5 -2
  105. package/src/musicxml/MusicXmlParser/MeasureParser.ts +36 -7
  106. package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +25 -8
  107. package/src/musicxml/MusicXmlParser/Spanners.ts +27 -4
  108. package/src/musicxml/MusicXmlParser/mappers.ts +7 -0
  109. package/src/musicxml/index.ts +1 -0
  110. package/src/percussion/index.ts +7 -0
  111. package/src/renderer/Layout/VerticalSpacing.ts +1 -1
  112. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +4 -0
  113. package/src/renderer/fixedNoteSpacing.ts +78 -0
  114. package/src/renderer/index.ts +8 -0
  115. package/src/renderer/itemLayout.ts +137 -0
  116. package/src/renderer/layout.ts +1 -1
  117. package/src/renderer/measure.tsx +85 -182
  118. package/src/renderer/measureBounds.ts +109 -0
  119. package/src/renderer/measureModifiers.ts +133 -0
  120. package/src/renderer/noteModifiers.ts +298 -0
  121. package/src/renderer/render.tsx +66 -171
  122. package/src/renderer/scale.ts +21 -1
  123. package/src/renderer/scoreParsing.tsx +78 -262
  124. package/src/renderer/stave.ts +44 -0
  125. package/src/renderer/types.ts +67 -4
  126. package/src/renderer/useScoreRecording.ts +11 -3
  127. package/src/renderer/vfKeys.ts +88 -0
  128. package/src/state/index.ts +1 -0
  129. package/src/state/types.ts +48 -5
@@ -0,0 +1,298 @@
1
+ import {
2
+ Accidental as VFAccidental,
3
+ Annotation as VFAnnotation,
4
+ AnnotationVerticalJustify,
5
+ Articulation as VFArticulation,
6
+ Dot,
7
+ GraceNote as VFGraceNote,
8
+ GraceNoteGroup,
9
+ ModifierPosition,
10
+ Parenthesis,
11
+ StaveNote,
12
+ Stem,
13
+ } from 'vexflow';
14
+ import type { StemmableNote } from 'vexflow';
15
+
16
+ import type {
17
+ Articulation,
18
+ Clef,
19
+ Direction,
20
+ DurationValue,
21
+ Dynamic,
22
+ GraceNoteAttachment,
23
+ LyricAttachment,
24
+ NoteAttachment,
25
+ Pitch,
26
+ } from '../state';
27
+ import { durationToVF, pitchToVFKey } from './vfKeys';
28
+
29
+ /**
30
+ * Maps library articulation names to VexFlow articulation codes.
31
+ */
32
+ export const ARTICULATION_TO_VF_CODE: Record<Articulation, string> = {
33
+ staccato: 'a.',
34
+ staccatissimo: 'av',
35
+ tenuto: 'a-',
36
+ accent: 'a>',
37
+ marcato: 'a^',
38
+ fermata: 'a@',
39
+ open: 'ah',
40
+ stopped: 'a+',
41
+ };
42
+
43
+ /** SMuFL dynamics glyphs, drawn with the music font. */
44
+ const DYNAMIC_GLYPHS: Record<Dynamic, string> = {
45
+ ppp: '',
46
+ pp: '',
47
+ p: '',
48
+ mp: '',
49
+ mf: '',
50
+ f: '',
51
+ ff: '',
52
+ fff: '',
53
+ fp: '',
54
+ sf: '',
55
+ sfp: '',
56
+ sfz: '',
57
+ rf: '',
58
+ rfz: '',
59
+ fz: '',
60
+ n: '',
61
+ };
62
+
63
+ const DYNAMIC_FONT_SIZE = 30;
64
+
65
+ const ANNOTATION_FONT = {
66
+ family: 'Arial, Helvetica, sans-serif',
67
+ size: 10,
68
+ weight: 'bold',
69
+ };
70
+
71
+ const LYRIC_FONT = {
72
+ family: ANNOTATION_FONT.family,
73
+ size: 10,
74
+ weight: 'normal',
75
+ };
76
+
77
+ const DIRECTION_FONT = {
78
+ family: ANNOTATION_FONT.family,
79
+ size: 11,
80
+ weight: 'bold',
81
+ };
82
+
83
+ /** `GhostNote`s have no glyphs, so their element box is the origin. */
84
+ export function hasNoteHeads(note: StemmableNote): note is StaveNote {
85
+ return 'getNoteHeadBeginX' in note;
86
+ }
87
+
88
+ /**
89
+ * Applies the requested number of augmentation dots to a note.
90
+ */
91
+ export function applyDots(note: StaveNote, dots?: 0 | 1 | 2 | 3) {
92
+ for (let i = 0; i < (dots ?? 0); i++) {
93
+ Dot.buildAndAttach([note], { all: true });
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Adds pitch accidentals to the matching keys in a VexFlow note.
99
+ */
100
+ function addPitchAccidentals(note: StaveNote, pitches: readonly Pitch[]) {
101
+ pitches.forEach((pitch, index) => {
102
+ if (!pitch.accidental) {
103
+ return;
104
+ }
105
+
106
+ const type =
107
+ pitch.accidental === 'quarter-flat'
108
+ ? 'db'
109
+ : pitch.accidental === 'quarter-sharp'
110
+ ? 'd#'
111
+ : pitch.accidental;
112
+
113
+ note.addModifier(new VFAccidental(type), index);
114
+ });
115
+ }
116
+
117
+ /**
118
+ * Wraps each parenthesized pitch of a note in parentheses.
119
+ */
120
+ function applyPitchParentheses(note: StaveNote, pitches: readonly Pitch[]) {
121
+ pitches.forEach((pitch, index) => {
122
+ if (!pitch.parenthesized) {
123
+ return;
124
+ }
125
+
126
+ note.addModifier(new Parenthesis(ModifierPosition.LEFT), index);
127
+ note.addModifier(new Parenthesis(ModifierPosition.RIGHT), index);
128
+ });
129
+ }
130
+
131
+ export function decorateStaveNote(
132
+ note: StaveNote,
133
+ pitches: readonly Pitch[],
134
+ duration: DurationValue
135
+ ) {
136
+ addPitchAccidentals(note, pitches);
137
+ applyPitchParentheses(note, pitches);
138
+ applyDots(note, duration.dots);
139
+ }
140
+
141
+ /** Every attachment a rest can carry: grace notes need a pitched owner. */
142
+ export function restAttachments(
143
+ attachments: NoteAttachment[] | undefined
144
+ ): NoteAttachment[] | undefined {
145
+ return attachments?.filter((attachment) => attachment.type !== 'grace');
146
+ }
147
+
148
+ function verticalJustification(placement: 'above' | 'below') {
149
+ return placement === 'above'
150
+ ? AnnotationVerticalJustify.TOP
151
+ : AnnotationVerticalJustify.BOTTOM;
152
+ }
153
+
154
+ function byVerse(left: LyricAttachment, right: LyricAttachment) {
155
+ return (left.verse ?? -Infinity) - (right.verse ?? -Infinity);
156
+ }
157
+
158
+ /**
159
+ * Attaches the owner's marks to a VexFlow note in a fixed order regardless of
160
+ * the attachments' order: articulations, annotations, dynamics, lyrics (by
161
+ * verse), grace notes. Same-side marks stack outward in that order.
162
+ */
163
+ export function applyNoteModifiers(
164
+ note: StaveNote,
165
+ clef: Clef,
166
+ attachments: NoteAttachment[] | undefined
167
+ ) {
168
+ if (!attachments) {
169
+ return;
170
+ }
171
+
172
+ for (const attachment of attachments) {
173
+ if (attachment.type === 'articulation') {
174
+ const articulation = new VFArticulation(
175
+ ARTICULATION_TO_VF_CODE[attachment.articulation]
176
+ );
177
+
178
+ if (attachment.placement === 'below') {
179
+ articulation.setPosition(ModifierPosition.BELOW);
180
+ }
181
+
182
+ note.addModifier(articulation, 0);
183
+ }
184
+ }
185
+
186
+ for (const attachment of attachments) {
187
+ if (attachment.type === 'annotation') {
188
+ const annotation = new VFAnnotation(attachment.text);
189
+
190
+ annotation.setFont(ANNOTATION_FONT);
191
+ annotation.setVerticalJustification(
192
+ verticalJustification(attachment.placement ?? 'below')
193
+ );
194
+ note.addModifier(annotation, 0);
195
+ }
196
+ }
197
+
198
+ for (const attachment of attachments) {
199
+ if (attachment.type === 'dynamic') {
200
+ const dynamic = new VFAnnotation(DYNAMIC_GLYPHS[attachment.dynamic]);
201
+
202
+ dynamic.setFontSize(DYNAMIC_FONT_SIZE);
203
+ dynamic.setVerticalJustification(
204
+ verticalJustification(attachment.placement ?? 'below')
205
+ );
206
+ note.addModifier(dynamic, 0);
207
+ }
208
+ }
209
+
210
+ attachments
211
+ .filter(
212
+ (attachment): attachment is LyricAttachment => attachment.type === 'lyric'
213
+ )
214
+ .sort(byVerse)
215
+ .forEach((attachment) => {
216
+ const lyric = new VFAnnotation(attachment.text);
217
+
218
+ lyric.setFont(LYRIC_FONT);
219
+ lyric.setVerticalJustification(AnnotationVerticalJustify.BOTTOM);
220
+ note.addModifier(lyric, 0);
221
+ });
222
+
223
+ for (const attachment of attachments) {
224
+ if (attachment.type === 'grace') {
225
+ applyGraceNoteGroup(note, clef, attachment);
226
+ }
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Grace notes stem up unless the owner was built stem-down (auto stems that
232
+ * flip later in `Beam.generateBeams` are not followed); two or more are
233
+ * beamed together, with the acciaccatura slash on the first stem only.
234
+ */
235
+ function applyGraceNoteGroup(
236
+ note: StaveNote,
237
+ clef: Clef,
238
+ attachment: GraceNoteAttachment
239
+ ) {
240
+ if (attachment.notes.length === 0) {
241
+ return;
242
+ }
243
+
244
+ const stemDirection =
245
+ note.getStemDirection() === Stem.DOWN ? Stem.DOWN : Stem.UP;
246
+ const graceNotes = attachment.notes.map((graceNote, index) => {
247
+ const vfGraceNote = new VFGraceNote({
248
+ clef,
249
+ keys: [pitchToVFKey(graceNote.pitch)],
250
+ duration: durationToVF(graceNote.duration),
251
+ slash: index === 0 && attachment.slash === true,
252
+ stemDirection,
253
+ });
254
+ decorateStaveNote(vfGraceNote, [graceNote.pitch], graceNote.duration);
255
+ return vfGraceNote;
256
+ });
257
+ const group = new GraceNoteGroup(graceNotes);
258
+
259
+ if (graceNotes.length > 1) {
260
+ group.beamNotes();
261
+ }
262
+
263
+ note.addModifier(group, 0);
264
+ }
265
+
266
+ /**
267
+ * Draws each text direction of a measure on the voice's first drawn item,
268
+ * after that item's own marks; a voice of spacers draws none. Tempo
269
+ * directions are not drawn.
270
+ */
271
+ export function applyMeasureDirections(
272
+ notes: readonly StemmableNote[],
273
+ directions: readonly Direction[] | undefined
274
+ ) {
275
+ if (!directions?.length) {
276
+ return;
277
+ }
278
+
279
+ const anchor = notes.find(hasNoteHeads);
280
+
281
+ if (!anchor) {
282
+ return;
283
+ }
284
+
285
+ for (const direction of directions) {
286
+ if (direction.type !== 'text') {
287
+ continue;
288
+ }
289
+
290
+ const annotation = new VFAnnotation(direction.text);
291
+
292
+ annotation.setFont(DIRECTION_FONT);
293
+ annotation.setVerticalJustification(
294
+ verticalJustification(direction.placement ?? 'above')
295
+ );
296
+ anchor.addModifier(annotation, 0);
297
+ }
298
+ }
@@ -1,4 +1,5 @@
1
- import { Formatter, Stave, StaveConnector, Voice as VFVoice } from 'vexflow';
1
+ import { Formatter, Stave, StaveConnector } from 'vexflow';
2
+ import type { Voice as VFVoice } from 'vexflow';
2
3
  import type { StaveConnectorType } from 'vexflow';
3
4
 
4
5
  import type { VexflowRecordingContext } from '../base';
@@ -14,11 +15,15 @@ import type {
14
15
  ScoreLayoutPlan,
15
16
  } from './layout';
16
17
  import {
17
- indexAttachmentsByOwner,
18
- makeVFVoice,
19
- noteheadWidth,
20
- } from './scoreParsing';
21
- import type { ScoreItemsLayout, ScoreOptions } from './types';
18
+ applyMeasureModifiers,
19
+ resolveMeasureModifiers,
20
+ } from './measureModifiers';
21
+ import { indexAttachmentsByOwner, makeVFVoice } from './scoreParsing';
22
+ import { applyFixedNoteSpacing } from './fixedNoteSpacing';
23
+ import { itemLayoutOf } from './itemLayout';
24
+ import { hasNoteHeads } from './noteModifiers';
25
+ import { applyStaffLines, visibleLineYs } from './stave';
26
+ import type { ScoreItemHooks, ScoreItemsLayout, ScoreOptions } from './types';
22
27
  import type { VFVoiceNote } from './scoreParsing';
23
28
 
24
29
  /**
@@ -29,7 +34,8 @@ export function renderScore(
29
34
  ctx: VexflowRecordingContext,
30
35
  score: Score,
31
36
  layoutPlan: ScoreLayoutPlan,
32
- options: ScoreOptions
37
+ options: ScoreOptions,
38
+ hooks: ScoreItemHooks = {}
33
39
  ): ScoreItemsLayout {
34
40
  const groupsById = new Map(
35
41
  layoutPlan.groups.map((group) => [group.groupId, group])
@@ -66,6 +72,7 @@ export function renderScore(
66
72
  isFirstMeasureInSystem: measureIndex === 0,
67
73
  isLastMeasureInSystem: measureIndex === measurePlans.length - 1,
68
74
  fixedNoteSpacing: options.render.fixedNoteSpacing,
75
+ ...hooks,
69
76
  },
70
77
  itemsLayout
71
78
  );
@@ -90,88 +97,12 @@ type StaffRenderArtifacts = {
90
97
  vfVoices: VFVoice[];
91
98
  };
92
99
 
93
- interface RenderMeasureOptions {
100
+ interface RenderMeasureOptions extends ScoreItemHooks {
94
101
  isFirstMeasureInSystem: boolean;
95
102
  isLastMeasureInSystem: boolean;
96
103
  fixedNoteSpacing: boolean;
97
104
  }
98
105
 
99
- /**
100
- * Re-positions every tick context at its time-proportional x inside the note
101
- * area, overriding the formatter's duration-weighted spacing. Runs after
102
- * `formatToStave` (the same post-format `setX` mechanism the formatter's own
103
- * tuning uses), so beams, tuplets and modifiers pick the final positions up
104
- * at draw time. The lattice follows VexFlow's own convention for its first
105
- * tick: it starts at that context's left-modifier width (`totalLeftPx`), so a
106
- * grace group or accidental on beat 1 hangs inside the note area instead of
107
- * over the clef. The proportional span is clamped only by that inset and the
108
- * LAST context's width — with a spacer voice on the lattice the latter is
109
- * constant, so the mapping stays a pure function of time and of what hangs
110
- * left of beat 1.
111
- */
112
- export function applyFixedNoteSpacing(
113
- formatter: Formatter,
114
- voices: VFVoice[],
115
- stave: Stave
116
- ) {
117
- const contexts = formatter.getTickContexts();
118
- const lastTickKey = contexts?.list[contexts.list.length - 1];
119
-
120
- if (
121
- !contexts ||
122
- voices.length === 0 ||
123
- lastTickKey === undefined ||
124
- lastTickKey <= 0
125
- ) {
126
- return;
127
- }
128
-
129
- const totalTicks = voices[0]?.getTotalTicks().value() ?? 0;
130
-
131
- if (!(totalTicks > 0)) {
132
- return;
133
- }
134
-
135
- // Tick context keys are ticks × the formatter's resolution multiplier (the
136
- // lcm that keeps tuplet ticks integral: 3 with triplets, 5 with
137
- // quintuplets…), while getTotalTicks() is in plain ticks. Compare in plain
138
- // ticks, or every position scales by the multiplier and the overflow
139
- // correction below crushes the last tick flush against the note end.
140
- const multiplier = contexts.resolutionMultiplier || 1;
141
- const lastTick = lastTickKey / multiplier;
142
-
143
- // Tick context x is relative to noteStartX + the stave's left note padding
144
- // (see Tickable.getAbsoluteX), so the usable span ends at noteEndX. The
145
- // padding is derived from Stave statics because the `Metrics` module is not
146
- // re-exported by every vexflow build.
147
- const stavePadding = Stave.defaultPadding - Stave.rightPadding;
148
- const noteAreaWidth =
149
- stave.getNoteEndX() - stave.getNoteStartX() - stavePadding;
150
- const firstLeft = contexts.map[contexts.list[0]!]!.getMetrics().totalLeftPx;
151
- let span = noteAreaWidth - firstLeft;
152
-
153
- const lastContext = contexts.map[lastTickKey]!;
154
- const lastOverflow =
155
- firstLeft +
156
- (lastTick / totalTicks) * span +
157
- lastContext.getWidth() -
158
- noteAreaWidth;
159
-
160
- if (lastOverflow > 0) {
161
- span -= (lastOverflow * totalTicks) / lastTick;
162
- }
163
-
164
- if (!(span > 0)) {
165
- return;
166
- }
167
-
168
- for (const tickKey of contexts.list) {
169
- contexts.map[tickKey]!.setX(
170
- firstLeft + (tickKey / multiplier / totalTicks) * span
171
- );
172
- }
173
- }
174
-
175
106
  function renderMeasure(
176
107
  ctx: VexflowRecordingContext,
177
108
  score: Score,
@@ -199,9 +130,18 @@ function renderMeasure(
199
130
  return { vfVoices: [], voiceArtifacts: [], beams: [], tuplets: [] };
200
131
  }
201
132
 
202
- const voiceArtifacts = measure.voices.map((voice) => ({
133
+ const voiceArtifacts = measure.voices.map((voice, voiceIndex) => ({
203
134
  ...makeVFVoice(score, resolvedState.meter, resolvedState.clef, voice, {
204
135
  attachmentsByOwner,
136
+ staff,
137
+ directions: voiceIndex === 0 ? measure.directions : undefined,
138
+ itemContext: options.decorateItem
139
+ ? {
140
+ decorateItem: options.decorateItem,
141
+ staff,
142
+ measureIndex: measurePlan.measureIndex,
143
+ }
144
+ : undefined,
205
145
  resolveClef: (item) =>
206
146
  item.targetStaffId
207
147
  ? resolvedStateByStaffId.get(item.targetStaffId)?.clef ??
@@ -236,22 +176,20 @@ function renderMeasure(
236
176
  const measure = staff.measures[measurePlan.measureIndex]!;
237
177
  const resolvedState =
238
178
  group.resolvedStatesByStaff[staffIndex]![measurePlan.measureIndex]!;
239
- const stave = new Stave(
240
- measurePlan.x,
241
- measurePlan.y + (measurePlan.staffYOffsets[staffIndex] ?? 0),
242
- measurePlan.width
179
+ const stave = applyStaffLines(
180
+ new Stave(
181
+ measurePlan.x,
182
+ measurePlan.y + (measurePlan.staffYOffsets[staffIndex] ?? 0),
183
+ measurePlan.width
184
+ ),
185
+ staff.lines
243
186
  );
244
187
 
245
- if (measurePlan.measureIndex === 0 || measure.leftModifiers?.showClef) {
246
- stave.addClef(resolvedState.clef);
247
- }
248
-
249
- if (measure.leftModifiers?.showMeter === true) {
250
- stave.addTimeSignature(
251
- `${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
252
- );
253
- }
254
-
188
+ applyMeasureModifiers(
189
+ stave,
190
+ resolveMeasureModifiers(measure, measurePlan.measureIndex),
191
+ resolvedState
192
+ );
255
193
  stave.setContext(ctx).draw();
256
194
  return stave;
257
195
  });
@@ -290,24 +228,25 @@ function renderMeasure(
290
228
  }
291
229
  }
292
230
 
293
- collectMeasureItemsLayout(
294
- itemsLayout,
295
- measurePlan,
296
- group.staves,
297
- renderedStaves,
298
- staffRenderArtifacts
299
- );
300
-
301
231
  staffRenderArtifacts.forEach(
302
232
  ({ vfVoices, voiceArtifacts, beams, tuplets }) => {
303
233
  vfVoices.forEach((voice) => voice.setRendered());
304
234
  voiceArtifacts.forEach(({ items, notes }) => {
305
- drawVoiceItems(ctx, items, notes);
235
+ drawVoiceItems(
236
+ ctx,
237
+ items,
238
+ notes,
239
+ measurePlan.measureIndex,
240
+ itemsLayout,
241
+ options.onDrawItem
242
+ );
306
243
  });
307
244
  beams.forEach((beam) => beam.setContext(ctx).draw());
308
245
  tuplets.forEach((tuplet) => tuplet.setContext(ctx).draw());
309
246
  }
310
247
  );
248
+
249
+ collectMeasureLayout(itemsLayout, measurePlan, group.staves, renderedStaves);
311
250
  }
312
251
 
313
252
  /**
@@ -321,39 +260,15 @@ function getFormatReferenceStave(renderedStaves: Stave[]): Stave {
321
260
  }
322
261
 
323
262
  /**
324
- * Records the formatted geometry of one measure; must run after
325
- * `formatToStave` because note positions are only final then. Emits one
326
- * measure entry per rendered stave, since note bounds differ per stave.
263
+ * Emits one measure entry per rendered stave, since note bounds differ per
264
+ * stave.
327
265
  */
328
- function collectMeasureItemsLayout(
266
+ function collectMeasureLayout(
329
267
  itemsLayout: ScoreItemsLayout,
330
268
  measurePlan: MeasureLayoutPlan,
331
269
  staves: GroupLayoutContext['staves'],
332
- renderedStaves: Stave[],
333
- staffRenderArtifacts: StaffRenderArtifacts[]
270
+ renderedStaves: Stave[]
334
271
  ) {
335
- staffRenderArtifacts.forEach(({ voiceArtifacts }) => {
336
- voiceArtifacts.forEach(({ items, notes }) => {
337
- items.forEach((item, index) => {
338
- const note = notes[index];
339
-
340
- if (!note) {
341
- return;
342
- }
343
-
344
- const x = note.getAbsoluteX();
345
- const width = note.getWidth();
346
-
347
- itemsLayout.items[item.id] = {
348
- x,
349
- width,
350
- headCenterX: resolveItemHeadCenterX(note, x, width),
351
- measureIndex: measurePlan.measureIndex,
352
- };
353
- });
354
- });
355
- });
356
-
357
272
  staves.forEach((staff, staffIndex) => {
358
273
  const stave = renderedStaves[staffIndex];
359
274
 
@@ -374,50 +289,22 @@ function collectMeasureItemsLayout(
374
289
  height: measurePlan.height,
375
290
  staveLineTopY: stave.getTopLineTopY(),
376
291
  staveLineBottomY: stave.getBottomLineBottomY(),
292
+ visibleLineYs: visibleLineYs(stave, staff.lines),
377
293
  });
378
294
  });
379
295
  }
380
296
 
381
- /** Detected structurally because `GhostNote`s lack these getters. */
382
- type NoteHeadSpan = {
383
- getNoteHeadBeginX?: () => number;
384
- getNoteHeadEndX?: () => number;
385
- };
386
-
387
297
  /**
388
- * Center of a formatted note's visual notehead span. When the getters are
389
- * missing (GhostNotes) the fallback is the center of a NOTIONAL notehead at
390
- * the block's left edge, capped by the block width — so a `spacer` rest
391
- * anchors where a real note's head would sit on the same tick, and external
392
- * UI aligned to it does not hop when the note appears. Zero-width `hidden`
393
- * rests keep anchoring at their tick x.
298
+ * Draws each item and records its formatted geometry; the layout entry is
299
+ * taken after `draw()` because modifiers only take their position there.
394
300
  */
395
- export function resolveItemHeadCenterX(
396
- note: VFVoiceNote,
397
- x: number,
398
- width: number
399
- ): number {
400
- const { getNoteHeadBeginX, getNoteHeadEndX } = note as NoteHeadSpan;
401
-
402
- if (
403
- typeof getNoteHeadBeginX === 'function' &&
404
- typeof getNoteHeadEndX === 'function'
405
- ) {
406
- const center =
407
- (getNoteHeadBeginX.call(note) + getNoteHeadEndX.call(note)) / 2;
408
-
409
- if (Number.isFinite(center) && center > x && center <= x + width) {
410
- return center;
411
- }
412
- }
413
-
414
- return x + Math.min(width, noteheadWidth()) / 2;
415
- }
416
-
417
301
  function drawVoiceItems(
418
302
  ctx: VexflowRecordingContext,
419
303
  items: VoiceItem[],
420
- notes: VFVoiceNote[]
304
+ notes: VFVoiceNote[],
305
+ measureIndex: number,
306
+ itemsLayout: ScoreItemsLayout,
307
+ onDrawItem: ScoreItemHooks['onDrawItem']
421
308
  ) {
422
309
  items.forEach((item, index) => {
423
310
  const note = notes[index];
@@ -430,6 +317,14 @@ function drawVoiceItems(
430
317
 
431
318
  try {
432
319
  note.setContext(ctx).drawWithStyle();
320
+
321
+ const layout = itemLayoutOf(note, measureIndex);
322
+
323
+ itemsLayout.items[item.id] = layout;
324
+
325
+ if (onDrawItem && hasNoteHeads(note)) {
326
+ onDrawItem(ctx, item, note, layout);
327
+ }
433
328
  } finally {
434
329
  ctx.endColorGroup();
435
330
  }
@@ -1,4 +1,9 @@
1
- import type { RendererRect, RendererSize, ScoreItemsLayout } from './types';
1
+ import type {
2
+ RendererRect,
3
+ RendererSize,
4
+ ScoreItemBounds,
5
+ ScoreItemsLayout,
6
+ } from './types';
2
7
 
3
8
  /**
4
9
  * Scale rule for `options.render.scale`: layout runs in content space against
@@ -76,6 +81,9 @@ export function scaleItemsLayoutToViewSpace(
76
81
  x: item.x * scale,
77
82
  width: item.width * scale,
78
83
  headCenterX: item.headCenterX * scale,
84
+ ...(item.modifierBounds
85
+ ? { modifierBounds: scaleBounds(item.modifierBounds, scale) }
86
+ : {}),
79
87
  };
80
88
  }
81
89
 
@@ -91,7 +99,19 @@ export function scaleItemsLayoutToViewSpace(
91
99
  height: measure.height * scale,
92
100
  staveLineTopY: measure.staveLineTopY * scale,
93
101
  staveLineBottomY: measure.staveLineBottomY * scale,
102
+ ...(measure.visibleLineYs
103
+ ? { visibleLineYs: measure.visibleLineYs.map((y) => y * scale) }
104
+ : {}),
94
105
  })),
95
106
  contentSize: toViewSize(itemsLayout.contentSize, scale),
96
107
  };
97
108
  }
109
+
110
+ function scaleBounds(bounds: ScoreItemBounds, scale: number): ScoreItemBounds {
111
+ return {
112
+ left: bounds.left * scale,
113
+ right: bounds.right * scale,
114
+ top: bounds.top * scale,
115
+ bottom: bounds.bottom * scale,
116
+ };
117
+ }