vexflow-native 1.8.0 → 2.0.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 (165) hide show
  1. package/README.md +71 -403
  2. package/lib/module/base/FontManager.js +5 -5
  3. package/lib/module/base/FontManager.js.map +1 -1
  4. package/lib/module/base/VexflowCanvas.js +1 -1
  5. package/lib/module/base/VexflowCanvas.js.map +1 -1
  6. package/lib/module/base/VexflowRecordingContext.js +2 -0
  7. package/lib/module/base/VexflowRecordingContext.js.map +1 -1
  8. package/lib/module/base/VexflowRecordingReplay.js +1 -1
  9. package/lib/module/base/VexflowRecordingReplay.js.map +1 -1
  10. package/lib/module/base/index.js +1 -0
  11. package/lib/module/base/index.js.map +1 -1
  12. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +24 -7
  13. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
  14. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +11 -3
  15. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -1
  16. package/lib/module/musicxml/MusicXmlParser/Spanners.js +21 -5
  17. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -1
  18. package/lib/module/musicxml/MusicXmlParser/mappers.js +1 -1
  19. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
  20. package/lib/module/{state/constants.js → percussion/index.js} +3 -3
  21. package/lib/module/percussion/index.js.map +1 -0
  22. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js +4 -3
  23. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js.map +1 -1
  24. package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js +1 -1
  25. package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js.map +1 -1
  26. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +5 -1
  27. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
  28. package/lib/module/renderer/ScoreRenderer/utils.js +1 -1
  29. package/lib/module/renderer/ScoreRenderer/utils.js.map +1 -1
  30. package/lib/module/renderer/fixedNoteSpacing.js +56 -0
  31. package/lib/module/renderer/fixedNoteSpacing.js.map +1 -0
  32. package/lib/module/renderer/index.js +1 -0
  33. package/lib/module/renderer/index.js.map +1 -1
  34. package/lib/module/renderer/itemLayout.js +108 -0
  35. package/lib/module/renderer/itemLayout.js.map +1 -0
  36. package/lib/module/renderer/measure.js +30 -106
  37. package/lib/module/renderer/measure.js.map +1 -1
  38. package/lib/module/renderer/measureBounds.js +73 -0
  39. package/lib/module/renderer/measureBounds.js.map +1 -0
  40. package/lib/module/renderer/measureModifiers.js +99 -0
  41. package/lib/module/renderer/measureModifiers.js.map +1 -0
  42. package/lib/module/renderer/noteModifiers.js +212 -0
  43. package/lib/module/renderer/noteModifiers.js.map +1 -0
  44. package/lib/module/renderer/render.js +33 -116
  45. package/lib/module/renderer/render.js.map +1 -1
  46. package/lib/module/renderer/scale.js +16 -2
  47. package/lib/module/renderer/scale.js.map +1 -1
  48. package/lib/module/renderer/scoreParsing.js +37 -199
  49. package/lib/module/renderer/scoreParsing.js.map +1 -1
  50. package/lib/module/renderer/stave.js +19 -3
  51. package/lib/module/renderer/stave.js.map +1 -1
  52. package/lib/module/renderer/useScoreRecording.js +10 -3
  53. package/lib/module/renderer/useScoreRecording.js.map +1 -1
  54. package/lib/module/renderer/vfKeys.js +72 -0
  55. package/lib/module/renderer/vfKeys.js.map +1 -0
  56. package/lib/module/state/index.js +1 -1
  57. package/lib/module/state/index.js.map +1 -1
  58. package/lib/typescript/src/base/FontManager.d.ts +3 -2
  59. package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
  60. package/lib/typescript/src/base/VexflowRecordingContext.d.ts +2 -1
  61. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
  62. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +1 -1
  63. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts.map +1 -1
  64. package/lib/typescript/src/base/index.d.ts +1 -0
  65. package/lib/typescript/src/base/index.d.ts.map +1 -1
  66. package/lib/typescript/src/base/types.d.ts +1 -1
  67. package/lib/typescript/src/base/types.d.ts.map +1 -1
  68. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +4 -2
  69. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
  70. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
  71. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +6 -1
  72. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -1
  73. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +6 -1
  74. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -1
  75. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
  76. package/lib/typescript/src/musicxml/index.d.ts +1 -0
  77. package/lib/typescript/src/musicxml/index.d.ts.map +1 -1
  78. package/lib/typescript/src/percussion/index.d.ts +4 -0
  79. package/lib/typescript/src/percussion/index.d.ts.map +1 -0
  80. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts +1 -1
  81. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts.map +1 -1
  82. package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts +1 -1
  83. package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts.map +1 -1
  84. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
  85. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +1 -1
  86. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -1
  87. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts +1 -1
  88. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts.map +1 -1
  89. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts +17 -0
  90. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts.map +1 -0
  91. package/lib/typescript/src/renderer/index.d.ts +2 -0
  92. package/lib/typescript/src/renderer/index.d.ts.map +1 -1
  93. package/lib/typescript/src/renderer/itemLayout.d.ts +23 -0
  94. package/lib/typescript/src/renderer/itemLayout.d.ts.map +1 -0
  95. package/lib/typescript/src/renderer/layout.d.ts +1 -1
  96. package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
  97. package/lib/typescript/src/renderer/measure.d.ts +3 -6
  98. package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
  99. package/lib/typescript/src/renderer/measureBounds.d.ts +13 -0
  100. package/lib/typescript/src/renderer/measureBounds.d.ts.map +1 -0
  101. package/lib/typescript/src/renderer/measureModifiers.d.ts +20 -0
  102. package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -0
  103. package/lib/typescript/src/renderer/noteModifiers.d.ts +29 -0
  104. package/lib/typescript/src/renderer/noteModifiers.d.ts.map +1 -0
  105. package/lib/typescript/src/renderer/render.d.ts +2 -27
  106. package/lib/typescript/src/renderer/render.d.ts.map +1 -1
  107. package/lib/typescript/src/renderer/scale.d.ts.map +1 -1
  108. package/lib/typescript/src/renderer/scoreParsing.d.ts +14 -42
  109. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
  110. package/lib/typescript/src/renderer/stave.d.ts +2 -0
  111. package/lib/typescript/src/renderer/stave.d.ts.map +1 -1
  112. package/lib/typescript/src/renderer/types.d.ts +52 -3
  113. package/lib/typescript/src/renderer/types.d.ts.map +1 -1
  114. package/lib/typescript/src/renderer/useScoreRecording.d.ts +3 -3
  115. package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
  116. package/lib/typescript/src/renderer/vfKeys.d.ts +17 -0
  117. package/lib/typescript/src/renderer/vfKeys.d.ts.map +1 -0
  118. package/lib/typescript/src/state/index.d.ts +1 -1
  119. package/lib/typescript/src/state/index.d.ts.map +1 -1
  120. package/lib/typescript/src/state/types.d.ts +23 -5
  121. package/lib/typescript/src/state/types.d.ts.map +1 -1
  122. package/package.json +31 -17
  123. package/src/base/FontManager.ts +5 -11
  124. package/src/base/TextMeasureContext.ts +1 -1
  125. package/src/base/VexflowCanvas.tsx +1 -1
  126. package/src/base/VexflowRecordingContext.ts +2 -4
  127. package/src/base/VexflowRecordingReplay.ts +1 -1
  128. package/src/base/index.ts +1 -0
  129. package/src/base/types.ts +1 -1
  130. package/src/musicxml/MusicXmlParser/InternalTypes.ts +5 -2
  131. package/src/musicxml/MusicXmlParser/MeasureParser.ts +36 -7
  132. package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +25 -8
  133. package/src/musicxml/MusicXmlParser/Spanners.ts +27 -4
  134. package/src/musicxml/MusicXmlParser/mappers.ts +7 -0
  135. package/src/musicxml/index.ts +1 -0
  136. package/src/percussion/index.ts +7 -0
  137. package/src/renderer/Layout/VerticalSpacing.ts +1 -1
  138. package/src/renderer/ScoreRenderer/ScoreOverlayPicture.tsx +4 -7
  139. package/src/renderer/ScoreRenderer/ScorePlayhead.tsx +1 -1
  140. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +5 -7
  141. package/src/renderer/ScoreRenderer/types.ts +1 -4
  142. package/src/renderer/ScoreRenderer/utils.ts +1 -1
  143. package/src/renderer/fixedNoteSpacing.ts +78 -0
  144. package/src/renderer/index.ts +8 -0
  145. package/src/renderer/itemLayout.ts +137 -0
  146. package/src/renderer/layout.ts +1 -1
  147. package/src/renderer/measure.tsx +84 -186
  148. package/src/renderer/measureBounds.ts +109 -0
  149. package/src/renderer/measureModifiers.ts +133 -0
  150. package/src/renderer/noteModifiers.ts +298 -0
  151. package/src/renderer/render.tsx +59 -169
  152. package/src/renderer/scale.ts +21 -1
  153. package/src/renderer/scoreParsing.tsx +77 -288
  154. package/src/renderer/stave.ts +22 -3
  155. package/src/renderer/types.ts +66 -3
  156. package/src/renderer/useScoreRecording.ts +12 -4
  157. package/src/renderer/vfKeys.ts +88 -0
  158. package/src/state/index.ts +1 -1
  159. package/src/state/types.ts +30 -5
  160. package/lib/module/musicxml/testfiles/lg-8102429.xml +0 -79620
  161. package/lib/module/state/constants.js.map +0 -1
  162. package/lib/typescript/src/state/constants.d.ts +0 -4
  163. package/lib/typescript/src/state/constants.d.ts.map +0 -1
  164. package/src/musicxml/testfiles/lg-8102429.xml +0 -79620
  165. package/src/state/constants.ts +0 -4
@@ -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,12 +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 { applyStaffLines } from './stave';
22
- 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';
23
27
  import type { VFVoiceNote } from './scoreParsing';
24
28
 
25
29
  /**
@@ -30,7 +34,8 @@ export function renderScore(
30
34
  ctx: VexflowRecordingContext,
31
35
  score: Score,
32
36
  layoutPlan: ScoreLayoutPlan,
33
- options: ScoreOptions
37
+ options: ScoreOptions,
38
+ hooks: ScoreItemHooks = {}
34
39
  ): ScoreItemsLayout {
35
40
  const groupsById = new Map(
36
41
  layoutPlan.groups.map((group) => [group.groupId, group])
@@ -67,6 +72,7 @@ export function renderScore(
67
72
  isFirstMeasureInSystem: measureIndex === 0,
68
73
  isLastMeasureInSystem: measureIndex === measurePlans.length - 1,
69
74
  fixedNoteSpacing: options.render.fixedNoteSpacing,
75
+ ...hooks,
70
76
  },
71
77
  itemsLayout
72
78
  );
@@ -91,88 +97,12 @@ type StaffRenderArtifacts = {
91
97
  vfVoices: VFVoice[];
92
98
  };
93
99
 
94
- interface RenderMeasureOptions {
100
+ interface RenderMeasureOptions extends ScoreItemHooks {
95
101
  isFirstMeasureInSystem: boolean;
96
102
  isLastMeasureInSystem: boolean;
97
103
  fixedNoteSpacing: boolean;
98
104
  }
99
105
 
100
- /**
101
- * Re-positions every tick context at its time-proportional x inside the note
102
- * area, overriding the formatter's duration-weighted spacing. Runs after
103
- * `formatToStave` (the same post-format `setX` mechanism the formatter's own
104
- * tuning uses), so beams, tuplets and modifiers pick the final positions up
105
- * at draw time. The lattice follows VexFlow's own convention for its first
106
- * tick: it starts at that context's left-modifier width (`totalLeftPx`), so a
107
- * grace group or accidental on beat 1 hangs inside the note area instead of
108
- * over the clef. The proportional span is clamped only by that inset and the
109
- * LAST context's width — with a spacer voice on the lattice the latter is
110
- * constant, so the mapping stays a pure function of time and of what hangs
111
- * left of beat 1.
112
- */
113
- export function applyFixedNoteSpacing(
114
- formatter: Formatter,
115
- voices: VFVoice[],
116
- stave: Stave
117
- ) {
118
- const contexts = formatter.getTickContexts();
119
- const lastTickKey = contexts?.list[contexts.list.length - 1];
120
-
121
- if (
122
- !contexts ||
123
- voices.length === 0 ||
124
- lastTickKey === undefined ||
125
- lastTickKey <= 0
126
- ) {
127
- return;
128
- }
129
-
130
- const totalTicks = voices[0]?.getTotalTicks().value() ?? 0;
131
-
132
- if (!(totalTicks > 0)) {
133
- return;
134
- }
135
-
136
- // Tick context keys are ticks × the formatter's resolution multiplier (the
137
- // lcm that keeps tuplet ticks integral: 3 with triplets, 5 with
138
- // quintuplets…), while getTotalTicks() is in plain ticks. Compare in plain
139
- // ticks, or every position scales by the multiplier and the overflow
140
- // correction below crushes the last tick flush against the note end.
141
- const multiplier = contexts.resolutionMultiplier || 1;
142
- const lastTick = lastTickKey / multiplier;
143
-
144
- // Tick context x is relative to noteStartX + the stave's left note padding
145
- // (see Tickable.getAbsoluteX), so the usable span ends at noteEndX. The
146
- // padding is derived from Stave statics because the `Metrics` module is not
147
- // re-exported by every vexflow build.
148
- const stavePadding = Stave.defaultPadding - Stave.rightPadding;
149
- const noteAreaWidth =
150
- stave.getNoteEndX() - stave.getNoteStartX() - stavePadding;
151
- const firstLeft = contexts.map[contexts.list[0]!]!.getMetrics().totalLeftPx;
152
- let span = noteAreaWidth - firstLeft;
153
-
154
- const lastContext = contexts.map[lastTickKey]!;
155
- const lastOverflow =
156
- firstLeft +
157
- (lastTick / totalTicks) * span +
158
- lastContext.getWidth() -
159
- noteAreaWidth;
160
-
161
- if (lastOverflow > 0) {
162
- span -= (lastOverflow * totalTicks) / lastTick;
163
- }
164
-
165
- if (!(span > 0)) {
166
- return;
167
- }
168
-
169
- for (const tickKey of contexts.list) {
170
- contexts.map[tickKey]!.setX(
171
- firstLeft + (tickKey / multiplier / totalTicks) * span
172
- );
173
- }
174
- }
175
-
176
106
  function renderMeasure(
177
107
  ctx: VexflowRecordingContext,
178
108
  score: Score,
@@ -200,10 +130,18 @@ function renderMeasure(
200
130
  return { vfVoices: [], voiceArtifacts: [], beams: [], tuplets: [] };
201
131
  }
202
132
 
203
- const voiceArtifacts = measure.voices.map((voice) => ({
133
+ const voiceArtifacts = measure.voices.map((voice, voiceIndex) => ({
204
134
  ...makeVFVoice(score, resolvedState.meter, resolvedState.clef, voice, {
205
135
  attachmentsByOwner,
206
- staffLines: staff.lines,
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,
207
145
  resolveClef: (item) =>
208
146
  item.targetStaffId
209
147
  ? resolvedStateByStaffId.get(item.targetStaffId)?.clef ??
@@ -247,16 +185,11 @@ function renderMeasure(
247
185
  staff.lines
248
186
  );
249
187
 
250
- if (measure.leftModifiers?.showClef ?? measurePlan.measureIndex === 0) {
251
- stave.addClef(resolvedState.clef);
252
- }
253
-
254
- if (measure.leftModifiers?.showMeter === true) {
255
- stave.addTimeSignature(
256
- `${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
257
- );
258
- }
259
-
188
+ applyMeasureModifiers(
189
+ stave,
190
+ resolveMeasureModifiers(measure, measurePlan.measureIndex),
191
+ resolvedState
192
+ );
260
193
  stave.setContext(ctx).draw();
261
194
  return stave;
262
195
  });
@@ -295,24 +228,25 @@ function renderMeasure(
295
228
  }
296
229
  }
297
230
 
298
- collectMeasureItemsLayout(
299
- itemsLayout,
300
- measurePlan,
301
- group.staves,
302
- renderedStaves,
303
- staffRenderArtifacts
304
- );
305
-
306
231
  staffRenderArtifacts.forEach(
307
232
  ({ vfVoices, voiceArtifacts, beams, tuplets }) => {
308
233
  vfVoices.forEach((voice) => voice.setRendered());
309
234
  voiceArtifacts.forEach(({ items, notes }) => {
310
- drawVoiceItems(ctx, items, notes);
235
+ drawVoiceItems(
236
+ ctx,
237
+ items,
238
+ notes,
239
+ measurePlan.measureIndex,
240
+ itemsLayout,
241
+ options.onDrawItem
242
+ );
311
243
  });
312
244
  beams.forEach((beam) => beam.setContext(ctx).draw());
313
245
  tuplets.forEach((tuplet) => tuplet.setContext(ctx).draw());
314
246
  }
315
247
  );
248
+
249
+ collectMeasureLayout(itemsLayout, measurePlan, group.staves, renderedStaves);
316
250
  }
317
251
 
318
252
  /**
@@ -326,39 +260,15 @@ function getFormatReferenceStave(renderedStaves: Stave[]): Stave {
326
260
  }
327
261
 
328
262
  /**
329
- * Records the formatted geometry of one measure; must run after
330
- * `formatToStave` because note positions are only final then. Emits one
331
- * 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.
332
265
  */
333
- function collectMeasureItemsLayout(
266
+ function collectMeasureLayout(
334
267
  itemsLayout: ScoreItemsLayout,
335
268
  measurePlan: MeasureLayoutPlan,
336
269
  staves: GroupLayoutContext['staves'],
337
- renderedStaves: Stave[],
338
- staffRenderArtifacts: StaffRenderArtifacts[]
270
+ renderedStaves: Stave[]
339
271
  ) {
340
- staffRenderArtifacts.forEach(({ voiceArtifacts }) => {
341
- voiceArtifacts.forEach(({ items, notes }) => {
342
- items.forEach((item, index) => {
343
- const note = notes[index];
344
-
345
- if (!note) {
346
- return;
347
- }
348
-
349
- const x = note.getAbsoluteX();
350
- const width = note.getWidth();
351
-
352
- itemsLayout.items[item.id] = {
353
- x,
354
- width,
355
- headCenterX: resolveItemHeadCenterX(note, x, width),
356
- measureIndex: measurePlan.measureIndex,
357
- };
358
- });
359
- });
360
- });
361
-
362
272
  staves.forEach((staff, staffIndex) => {
363
273
  const stave = renderedStaves[staffIndex];
364
274
 
@@ -379,50 +289,22 @@ function collectMeasureItemsLayout(
379
289
  height: measurePlan.height,
380
290
  staveLineTopY: stave.getTopLineTopY(),
381
291
  staveLineBottomY: stave.getBottomLineBottomY(),
292
+ visibleLineYs: visibleLineYs(stave, staff.lines),
382
293
  });
383
294
  });
384
295
  }
385
296
 
386
- /** Detected structurally because `GhostNote`s lack these getters. */
387
- type NoteHeadSpan = {
388
- getNoteHeadBeginX?: () => number;
389
- getNoteHeadEndX?: () => number;
390
- };
391
-
392
297
  /**
393
- * Center of a formatted note's visual notehead span. When the getters are
394
- * missing (GhostNotes) the fallback is the center of a NOTIONAL notehead at
395
- * the block's left edge, capped by the block width — so a `spacer` rest
396
- * anchors where a real note's head would sit on the same tick, and external
397
- * UI aligned to it does not hop when the note appears. Zero-width `hidden`
398
- * 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.
399
300
  */
400
- export function resolveItemHeadCenterX(
401
- note: VFVoiceNote,
402
- x: number,
403
- width: number
404
- ): number {
405
- const { getNoteHeadBeginX, getNoteHeadEndX } = note as NoteHeadSpan;
406
-
407
- if (
408
- typeof getNoteHeadBeginX === 'function' &&
409
- typeof getNoteHeadEndX === 'function'
410
- ) {
411
- const center =
412
- (getNoteHeadBeginX.call(note) + getNoteHeadEndX.call(note)) / 2;
413
-
414
- if (Number.isFinite(center) && center > x && center <= x + width) {
415
- return center;
416
- }
417
- }
418
-
419
- return x + Math.min(width, noteheadWidth()) / 2;
420
- }
421
-
422
301
  function drawVoiceItems(
423
302
  ctx: VexflowRecordingContext,
424
303
  items: VoiceItem[],
425
- notes: VFVoiceNote[]
304
+ notes: VFVoiceNote[],
305
+ measureIndex: number,
306
+ itemsLayout: ScoreItemsLayout,
307
+ onDrawItem: ScoreItemHooks['onDrawItem']
426
308
  ) {
427
309
  items.forEach((item, index) => {
428
310
  const note = notes[index];
@@ -435,6 +317,14 @@ function drawVoiceItems(
435
317
 
436
318
  try {
437
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
+ }
438
328
  } finally {
439
329
  ctx.endColorGroup();
440
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
+ }