vexflow-native 1.8.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 (133) hide show
  1. package/README.md +33 -405
  2. package/lib/module/base/FontManager.js +2 -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/{state/constants.js → percussion/index.js} +3 -3
  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 +30 -106
  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 +33 -116
  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 -199
  39. package/lib/module/renderer/scoreParsing.js.map +1 -1
  40. package/lib/module/renderer/stave.js +19 -3
  41. package/lib/module/renderer/stave.js.map +1 -1
  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 -1
  47. package/lib/module/state/index.js.map +1 -1
  48. package/lib/typescript/src/base/FontManager.d.ts +1 -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 -42
  87. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
  88. package/lib/typescript/src/renderer/stave.d.ts +2 -0
  89. package/lib/typescript/src/renderer/stave.d.ts.map +1 -1
  90. package/lib/typescript/src/renderer/types.d.ts +51 -2
  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 -1
  97. package/lib/typescript/src/state/index.d.ts.map +1 -1
  98. package/lib/typescript/src/state/types.d.ts +23 -5
  99. package/lib/typescript/src/state/types.d.ts.map +1 -1
  100. package/package.json +12 -2
  101. package/src/base/FontManager.ts +1 -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 +84 -186
  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 +59 -169
  122. package/src/renderer/scale.ts +21 -1
  123. package/src/renderer/scoreParsing.tsx +77 -288
  124. package/src/renderer/stave.ts +22 -3
  125. package/src/renderer/types.ts +65 -2
  126. package/src/renderer/useScoreRecording.ts +11 -3
  127. package/src/renderer/vfKeys.ts +88 -0
  128. package/src/state/index.ts +1 -1
  129. package/src/state/types.ts +30 -5
  130. package/lib/module/state/constants.js.map +0 -1
  131. package/lib/typescript/src/state/constants.d.ts +0 -4
  132. package/lib/typescript/src/state/constants.d.ts.map +0 -1
  133. package/src/state/constants.ts +0 -4
@@ -1,17 +1,8 @@
1
1
  import {
2
- Accidental as VFAccidental,
3
- Annotation as VFAnnotation,
4
- AnnotationVerticalJustify,
5
- Articulation as VFArticulation,
6
2
  Beam,
7
- Dot,
8
3
  Element,
9
4
  Fraction as VFFraction,
10
5
  GhostNote,
11
- GraceNote as VFGraceNote,
12
- GraceNoteGroup,
13
- ModifierPosition,
14
- Parenthesis,
15
6
  StaveNote,
16
7
  Stem,
17
8
  Tuplet,
@@ -20,19 +11,14 @@ import {
20
11
  import type { StemmableNote } from 'vexflow';
21
12
 
22
13
  import type {
23
- Articulation,
24
14
  Clef,
15
+ Direction,
25
16
  KeySignature,
26
- DurationValue,
27
- GraceNoteAttachment,
28
17
  Meter,
29
18
  NoteAttachment,
30
- Notehead,
31
- Pitch,
32
19
  Score,
33
20
  Staff,
34
21
  StaffGroup,
35
- StaffLines,
36
22
  StemDirection,
37
23
  Tempo,
38
24
  TupletGroup,
@@ -40,6 +26,15 @@ import type {
40
26
  VoiceItem,
41
27
  VoiceTimingMode,
42
28
  } from '../state';
29
+ import {
30
+ applyDots,
31
+ applyMeasureDirections,
32
+ applyNoteModifiers,
33
+ decorateStaveNote,
34
+ restAttachments,
35
+ } from './noteModifiers';
36
+ import { durationToVF, pitchToVFKey, restKeyForStaffLine } from './vfKeys';
37
+ import type { DecorateItem } from './types';
43
38
 
44
39
  export type StaffGroupLookup = {
45
40
  groupId: string;
@@ -63,37 +58,22 @@ export interface MakeVFVoiceOptions {
63
58
  * rebuilding it on every call.
64
59
  */
65
60
  attachmentsByOwner?: Map<string, NoteAttachment[]>;
66
- /** `Staff.lines`; on a one-line staff auto stems point up. */
67
- staffLines?: StaffLines;
61
+ /** Owning staff; on a one-line staff auto stems point up. */
62
+ staff?: Staff;
63
+ /** Measure directions drawn on this voice's first drawn item. */
64
+ directions?: Direction[];
65
+ itemContext?: {
66
+ decorateItem: DecorateItem;
67
+ staff: Staff;
68
+ measureIndex: number;
69
+ };
68
70
  }
69
71
 
70
- /**
71
- * Maps library notehead names to VexFlow key glyph codes.
72
- */
73
- export const NOTEHEAD_TO_VF_CODE: Record<Notehead, string> = {
74
- 'x': 'x',
75
- 'circle-x': 'cx',
76
- 'diamond': 'h',
77
- 'circle': 'ci',
78
- 'square': 'sq',
79
- 'triangle': 'tu',
80
- 'triangle-down': 'td',
81
- 'slash': 'sf',
82
- };
83
-
84
- /**
85
- * Maps library articulation names to VexFlow articulation codes.
86
- */
87
- export const ARTICULATION_TO_VF_CODE: Record<Articulation, string> = {
88
- staccato: 'a.',
89
- staccatissimo: 'av',
90
- tenuto: 'a-',
91
- accent: 'a>',
92
- marcato: 'a^',
93
- fermata: 'a@',
94
- open: 'ah',
95
- stopped: 'a+',
96
- };
72
+ type ItemDecorator = (
73
+ item: VoiceItem,
74
+ note: StaveNote,
75
+ attachments: readonly NoteAttachment[]
76
+ ) => void;
97
77
 
98
78
  /**
99
79
  * Maps the library stem direction to the numeric VexFlow value.
@@ -110,213 +90,6 @@ export function toVFStemDirection(dir?: StemDirection): number | undefined {
110
90
  return undefined;
111
91
  }
112
92
 
113
- /**
114
- * Converts an internal pitch into the VexFlow key string format.
115
- */
116
- export function pitchToVFKey(pitch: Pitch): string {
117
- const accidental =
118
- pitch.accidental === 'quarter-flat'
119
- ? 'db'
120
- : pitch.accidental === 'quarter-sharp'
121
- ? 'd#'
122
- : pitch.accidental ?? '';
123
-
124
- const key = `${pitch.step.toLowerCase()}${accidental}/${pitch.octave}`;
125
-
126
- if (pitch.notehead) {
127
- return `${key}/${NOTEHEAD_TO_VF_CODE[pitch.notehead]}`;
128
- }
129
-
130
- return key;
131
- }
132
-
133
- /**
134
- * Converts a duration into the VexFlow duration token, including rests.
135
- */
136
- export function durationToVF(duration: DurationValue, isRest = false): string {
137
- const length =
138
- duration.length === 'long' || duration.length === 'breve'
139
- ? '1/2'
140
- : duration.length;
141
-
142
- // Dots drive VexFlow's tick math, so rests need them in the token too or
143
- // strict voices come up short.
144
- const dots = 'd'.repeat(duration.dots ?? 0);
145
-
146
- return `${length}${dots}${isRest ? 'r' : ''}`;
147
- }
148
-
149
- /**
150
- * Applies the requested number of augmentation dots to a note.
151
- */
152
- export function applyDots(note: StaveNote, dots?: 0 | 1 | 2 | 3) {
153
- for (let i = 0; i < (dots ?? 0); i++) {
154
- Dot.buildAndAttach([note], { all: true });
155
- }
156
- }
157
-
158
- /**
159
- * Adds pitch accidentals to the matching keys in a VexFlow note.
160
- */
161
- export function addPitchAccidentals(
162
- note: StaveNote,
163
- pitches: readonly Pitch[]
164
- ) {
165
- pitches.forEach((pitch, index) => {
166
- if (!pitch.accidental) {
167
- return;
168
- }
169
-
170
- const type =
171
- pitch.accidental === 'quarter-flat'
172
- ? 'db'
173
- : pitch.accidental === 'quarter-sharp'
174
- ? 'd#'
175
- : pitch.accidental;
176
-
177
- note.addModifier(new VFAccidental(type), index);
178
- });
179
- }
180
-
181
- /**
182
- * Wraps each ghost pitch of a note in parentheses.
183
- */
184
- export function applyGhostParentheses(
185
- note: StaveNote,
186
- pitches: readonly Pitch[]
187
- ) {
188
- pitches.forEach((pitch, index) => {
189
- if (!pitch.ghost) {
190
- return;
191
- }
192
-
193
- note.addModifier(new Parenthesis(ModifierPosition.LEFT), index);
194
- note.addModifier(new Parenthesis(ModifierPosition.RIGHT), index);
195
- });
196
- }
197
-
198
- function hasAccentAttachment(attachments: NoteAttachment[] | undefined) {
199
- return (
200
- attachments?.some(
201
- (attachment) =>
202
- attachment.type === 'articulation' &&
203
- attachment.articulation === 'accent'
204
- ) ?? false
205
- );
206
- }
207
-
208
- /**
209
- * Draws one accent above a note whose pitches carry the `accent` flag, unless
210
- * the owner already has an accent attachment.
211
- */
212
- export function applyPitchAccent(
213
- note: StaveNote,
214
- pitches: readonly Pitch[],
215
- attachments?: NoteAttachment[]
216
- ) {
217
- if (!pitches.some((pitch) => pitch.accent)) {
218
- return;
219
- }
220
-
221
- if (hasAccentAttachment(attachments)) {
222
- return;
223
- }
224
-
225
- note.addModifier(new VFArticulation(ARTICULATION_TO_VF_CODE.accent), 0);
226
- }
227
-
228
- const ANNOTATION_FONT = {
229
- family: 'Arial, Helvetica, sans-serif',
230
- size: 10,
231
- weight: 'bold',
232
- };
233
-
234
- /**
235
- * Attaches the owner's articulation, annotation and grace-note modifiers to
236
- * a VexFlow note; dynamics and lyrics are drawn elsewhere.
237
- */
238
- export function applyNoteModifiers(
239
- note: StaveNote,
240
- clef: Clef,
241
- attachments: NoteAttachment[] | undefined
242
- ) {
243
- if (!attachments) {
244
- return;
245
- }
246
-
247
- for (const attachment of attachments) {
248
- if (attachment.type === 'articulation') {
249
- const articulation = new VFArticulation(
250
- ARTICULATION_TO_VF_CODE[attachment.articulation]
251
- );
252
-
253
- if (attachment.placement === 'below') {
254
- articulation.setPosition(ModifierPosition.BELOW);
255
- }
256
-
257
- note.addModifier(articulation, 0);
258
- } else if (attachment.type === 'annotation') {
259
- const annotation = new VFAnnotation(attachment.text);
260
-
261
- annotation.setFont(ANNOTATION_FONT);
262
- annotation.setVerticalJustification(
263
- attachment.placement === 'above'
264
- ? AnnotationVerticalJustify.TOP
265
- : AnnotationVerticalJustify.BOTTOM
266
- );
267
- note.addModifier(annotation, 0);
268
- } else if (attachment.type === 'grace') {
269
- applyGraceNoteGroup(note, clef, attachment);
270
- }
271
- }
272
- }
273
-
274
- /**
275
- * Grace notes stem up unless the owner was built stem-down (auto stems that
276
- * flip later in `Beam.generateBeams` are not followed); two or more are
277
- * beamed together, with the acciaccatura slash on the first stem only.
278
- */
279
- function applyGraceNoteGroup(
280
- note: StaveNote,
281
- clef: Clef,
282
- attachment: GraceNoteAttachment
283
- ) {
284
- if (attachment.notes.length === 0) {
285
- return;
286
- }
287
-
288
- const stemDirection =
289
- note.getStemDirection() === Stem.DOWN ? Stem.DOWN : Stem.UP;
290
- const graceNotes = attachment.notes.map((graceNote, index) => {
291
- const vfGraceNote = new VFGraceNote({
292
- clef,
293
- keys: [pitchToVFKey(graceNote.pitch)],
294
- duration: durationToVF(graceNote.duration),
295
- slash: index === 0 && attachment.slash === true,
296
- stemDirection,
297
- });
298
- decorateStaveNote(vfGraceNote, [graceNote.pitch], graceNote.duration);
299
- return vfGraceNote;
300
- });
301
- const group = new GraceNoteGroup(graceNotes);
302
-
303
- if (graceNotes.length > 1) {
304
- group.beamNotes();
305
- }
306
-
307
- note.addModifier(group, 0);
308
- }
309
-
310
- function decorateStaveNote(
311
- note: StaveNote,
312
- pitches: readonly Pitch[],
313
- duration: DurationValue
314
- ) {
315
- addPitchAccidentals(note, pitches);
316
- applyGhostParentheses(note, pitches);
317
- applyDots(note, duration.dots);
318
- }
319
-
320
93
  export function indexAttachmentsByOwner(
321
94
  score: Score
322
95
  ): Map<string, NoteAttachment[]> {
@@ -362,53 +135,55 @@ function spacerTickWidth(): number {
362
135
  export function voiceItemToStaveNote(
363
136
  item: VoiceItem,
364
137
  clef: Clef,
365
- attachments?: NoteAttachment[]
138
+ attachments?: NoteAttachment[],
139
+ decorate?: ItemDecorator
366
140
  ): VFVoiceNote {
367
- if (item.type === 'rest') {
368
- if (item.kind === 'hidden' || item.kind === 'spacer') {
369
- const note = new GhostNote(durationToVF(item.duration));
370
- if (item.kind === 'spacer') {
371
- note.setWidth(spacerTickWidth());
372
- }
373
- return note;
141
+ if (
142
+ item.type === 'rest' &&
143
+ (item.kind === 'hidden' || item.kind === 'spacer')
144
+ ) {
145
+ const note = new GhostNote(durationToVF(item.duration));
146
+
147
+ if (item.kind === 'spacer') {
148
+ note.setWidth(spacerTickWidth());
374
149
  }
375
150
 
376
- const note = new StaveNote({
377
- clef,
378
- keys: ['b/4'],
379
- duration: durationToVF(item.duration, true),
380
- });
381
- applyDots(note, item.duration.dots);
382
- applyNoteModifiers(
383
- note,
384
- clef,
385
- attachments?.filter((attachment) => attachment.type === 'annotation')
386
- );
387
151
  return note;
388
152
  }
389
153
 
390
- if (item.type === 'note') {
154
+ const note = buildStaveNote(item, clef);
155
+ const applied =
156
+ (item.type === 'rest' ? restAttachments(attachments) : attachments) ?? [];
157
+
158
+ applyNoteModifiers(note, clef, applied);
159
+ decorate?.(item, note, applied);
160
+
161
+ return note;
162
+ }
163
+
164
+ function buildStaveNote(item: VoiceItem, clef: Clef): StaveNote {
165
+ if (item.type === 'rest') {
391
166
  const note = new StaveNote({
392
167
  clef,
393
- keys: [pitchToVFKey(item.pitch)],
394
- duration: durationToVF(item.duration),
395
- stemDirection: toVFStemDirection(item.stemDirection),
168
+ keys: [restKeyForStaffLine(clef, item.staffLine)],
169
+ duration: durationToVF(item.duration, true),
396
170
  });
397
- decorateStaveNote(note, [item.pitch], item.duration);
398
- applyPitchAccent(note, [item.pitch], attachments);
399
- applyNoteModifiers(note, clef, attachments);
171
+
172
+ applyDots(note, item.duration.dots);
173
+
400
174
  return note;
401
175
  }
402
176
 
177
+ const pitches = item.type === 'note' ? [item.pitch] : item.pitches;
403
178
  const note = new StaveNote({
404
179
  clef,
405
- keys: item.pitches.map(pitchToVFKey),
180
+ keys: pitches.map(pitchToVFKey),
406
181
  duration: durationToVF(item.duration),
407
182
  stemDirection: toVFStemDirection(item.stemDirection),
408
183
  });
409
- decorateStaveNote(note, item.pitches, item.duration);
410
- applyPitchAccent(note, item.pitches, attachments);
411
- applyNoteModifiers(note, clef, attachments);
184
+
185
+ decorateStaveNote(note, pitches, item.duration);
186
+
412
187
  return note;
413
188
  }
414
189
 
@@ -504,13 +279,27 @@ export function makeVFVoice(
504
279
  } {
505
280
  const attachmentsByOwner =
506
281
  options.attachmentsByOwner ?? indexAttachmentsByOwner(score);
507
- const notes = voice.items.map((item) =>
508
- voiceItemToStaveNote(
282
+ const { itemContext } = options;
283
+ const notes = voice.items.map((item) => {
284
+ const itemClef = options.resolveClef?.(item) ?? clef;
285
+ const decorate: ItemDecorator | undefined = itemContext
286
+ ? (decoratedItem, note, attachments) =>
287
+ itemContext.decorateItem(decoratedItem, note, {
288
+ clef: itemClef,
289
+ staff: itemContext.staff,
290
+ measureIndex: itemContext.measureIndex,
291
+ attachments,
292
+ })
293
+ : undefined;
294
+
295
+ return voiceItemToStaveNote(
509
296
  item,
510
- options.resolveClef?.(item) ?? clef,
511
- attachmentsByOwner.get(item.id)
512
- )
513
- );
297
+ itemClef,
298
+ attachmentsByOwner.get(item.id),
299
+ decorate
300
+ );
301
+ });
302
+ applyMeasureDirections(notes, options.directions);
514
303
 
515
304
  const noteByItemId = new Map<string, VFVoiceNote>();
516
305
  voice.items.forEach((item, index) =>
@@ -558,7 +347,7 @@ export function makeVFVoice(
558
347
 
559
348
  const groups = beamGroupsToVF(meter);
560
349
  const maintainStemDirections =
561
- options.staffLines === 1 || voice.items.some(hasExplicitStemDirection);
350
+ options.staff?.lines === 1 || voice.items.some(hasExplicitStemDirection);
562
351
  const beamOptions =
563
352
  groups || maintainStemDirections
564
353
  ? {
@@ -2,6 +2,19 @@ import type { Stave } from 'vexflow';
2
2
 
3
3
  import type { StaffLines } from '../state';
4
4
 
5
+ /**
6
+ * Indices (0 = top) of the lines a `lines`-line staff draws on five-line
7
+ * geometry: the middle line plus `lines - 1` around it.
8
+ */
9
+ function visibleLineIndices(lineCount: number, lines?: StaffLines): number[] {
10
+ const middleLine = (lineCount - 1) / 2;
11
+ const reach = ((lines ?? lineCount) - 1) / 2;
12
+
13
+ return Array.from({ length: lineCount }, (_, index) => index).filter(
14
+ (index) => Math.abs(index - middleLine) <= reach
15
+ );
16
+ }
17
+
5
18
  /**
6
19
  * Hides the outer lines of a 5-line stave so `lines` stay visible around the
7
20
  * middle one. Must run after construction: the constructor resets the line
@@ -14,12 +27,18 @@ export function applyStaffLines(stave: Stave, lines?: StaffLines): Stave {
14
27
  return stave;
15
28
  }
16
29
 
17
- const middleLine = (lineCount - 1) / 2;
18
- const reach = (lines - 1) / 2;
30
+ const visible = new Set(visibleLineIndices(lineCount, lines));
19
31
 
20
32
  return stave.setConfigForLines(
21
33
  Array.from({ length: lineCount }, (_, index) => ({
22
- visible: Math.abs(index - middleLine) <= reach,
34
+ visible: visible.has(index),
23
35
  }))
24
36
  );
25
37
  }
38
+
39
+ /** Stroke centre y of every drawn line of a rendered stave, top to bottom. */
40
+ export function visibleLineYs(stave: Stave, lines?: StaffLines): number[] {
41
+ return visibleLineIndices(stave.getNumLines(), lines).map((index) =>
42
+ stave.getYForLine(index)
43
+ );
44
+ }
@@ -1,8 +1,9 @@
1
1
  import type { SkTypefaceFontProvider } from '@shopify/react-native-skia';
2
2
  import type { SharedValue } from 'react-native-reanimated';
3
+ import type { RenderContext, StaveNote } from 'vexflow';
3
4
 
4
5
  import type { VexflowStyleOverride } from '../base';
5
- import type { Score } from '../state';
6
+ import type { Clef, NoteAttachment, Score, Staff, VoiceItem } from '../state';
6
7
  import type { ScoreColorScheme } from './colorScheme';
7
8
 
8
9
  export type RendererType = 'infiniteScore' | 'document' | 'documentEven';
@@ -47,7 +48,48 @@ export interface ScoreScrollGeometry {
47
48
  maxScroll: number;
48
49
  }
49
50
 
50
- export interface ScoreRendererProps {
51
+ /** Where a voice item sits when its VexFlow note is built. */
52
+ export interface ScoreItemContext {
53
+ clef: Clef;
54
+ staff: Staff;
55
+ measureIndex: number;
56
+ /** For rests, the rest-eligible subset (everything but grace). */
57
+ attachments: readonly NoteAttachment[];
58
+ }
59
+
60
+ /**
61
+ * Called once per drawable item (not hidden/spacer rests) in BOTH the
62
+ * measurement and the drawing pass, after the item's own attachments are
63
+ * applied, before measure directions and before formatting, so added
64
+ * modifiers count in width and vertical bounds. Must be deterministic in its
65
+ * inputs; `note` is rebuilt on every pass.
66
+ */
67
+ export type DecorateItem = (
68
+ item: VoiceItem,
69
+ note: StaveNote,
70
+ context: ScoreItemContext
71
+ ) => void;
72
+
73
+ /**
74
+ * Called in the drawing pass only, right after the note and its modifiers
75
+ * are drawn, inside the item's color group. `layout` is the item's geometry
76
+ * in content space — the space `ctx` draws in (VexFlow units before
77
+ * `render.scale`); `onItemsLayout` receives the same entry scaled to view
78
+ * space.
79
+ */
80
+ export type DrawItem = (
81
+ ctx: RenderContext,
82
+ item: VoiceItem,
83
+ note: StaveNote,
84
+ layout: Readonly<ScoreItemLayout>
85
+ ) => void;
86
+
87
+ export interface ScoreItemHooks {
88
+ decorateItem?: DecorateItem;
89
+ onDrawItem?: DrawItem;
90
+ }
91
+
92
+ export interface ScoreRendererProps extends ScoreItemHooks {
51
93
  score: Score;
52
94
  defaultFont: string;
53
95
  fontManager: SkTypefaceFontProvider;
@@ -158,6 +200,19 @@ export interface ScoreItemLayout {
158
200
  */
159
201
  headCenterX: number;
160
202
  measureIndex: number;
203
+ /**
204
+ * Union box of the item's drawn modifiers (articulations, annotations,
205
+ * dynamics, lyrics, grace notes, parentheses, accidentals, dots), in the
206
+ * same space as `x`. undefined when the item has no modifiers.
207
+ */
208
+ modifierBounds?: ScoreItemBounds;
209
+ }
210
+
211
+ export interface ScoreItemBounds {
212
+ left: number;
213
+ right: number;
214
+ top: number;
215
+ bottom: number;
161
216
  }
162
217
 
163
218
  /**
@@ -186,6 +241,14 @@ export interface ScoreMeasureItemsLayout {
186
241
  staveLineTopY: number;
187
242
  /** Bottom edge of this stave's bottom staff line (hidden lines included). */
188
243
  staveLineBottomY: number;
244
+ /**
245
+ * Stroke centre y of each drawn line, top to bottom (5, 3 or 1 entries).
246
+ * On a five-line staff `visibleLineYs[0] === staveLineTopY` and the last
247
+ * entry is `staveLineBottomY - lineWidth`; with hidden lines
248
+ * `staveLineTopY`/`staveLineBottomY` still span the five-line geometry, so
249
+ * read the drawn line from here. Always emitted by the renderer.
250
+ */
251
+ visibleLineYs?: number[];
189
252
  }
190
253
 
191
254
  /**
@@ -21,6 +21,7 @@ import type { ResolvedScoreColorScheme } from './colorScheme';
21
21
  import type {
22
22
  RendererRect,
23
23
  RendererType,
24
+ ScoreItemHooks,
24
25
  ScoreItemsLayout,
25
26
  ScoreOptions,
26
27
  } from './types';
@@ -43,15 +44,17 @@ export interface ScoreRecording {
43
44
  }
44
45
 
45
46
  export function useScoreRecording({
47
+ decorateItem,
46
48
  defaultFont,
47
49
  enabled = true,
48
50
  fontManager,
49
51
  colorScheme,
52
+ onDrawItem,
50
53
  options,
51
54
  rendererType,
52
55
  score,
53
56
  viewport,
54
- }: {
57
+ }: ScoreItemHooks & {
55
58
  defaultFont: string;
56
59
  enabled?: boolean;
57
60
  fontManager: SkTypefaceFontProvider;
@@ -82,7 +85,7 @@ export function useScoreRecording({
82
85
  defaultFont,
83
86
  colorScheme
84
87
  );
85
- const measuredScore = measureScore(score, options);
88
+ const measuredScore = measureScore(score, options, { decorateItem });
86
89
  const measureMs = nowMs() - measureStart;
87
90
 
88
91
  const layoutStart = nowMs();
@@ -97,7 +100,10 @@ export function useScoreRecording({
97
100
 
98
101
  const renderStart = nowMs();
99
102
  const itemsLayout = scaleItemsLayoutToViewSpace(
100
- renderScore(ctx, score, layoutPlan, options),
103
+ renderScore(ctx, score, layoutPlan, options, {
104
+ decorateItem,
105
+ onDrawItem,
106
+ }),
101
107
  scale
102
108
  );
103
109
  const renderMs = nowMs() - renderStart;
@@ -130,9 +136,11 @@ export function useScoreRecording({
130
136
  };
131
137
  }, [
132
138
  colorScheme,
139
+ decorateItem,
133
140
  defaultFont,
134
141
  enabled,
135
142
  fontManager,
143
+ onDrawItem,
136
144
  options,
137
145
  rendererType,
138
146
  score,