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.
- package/README.md +33 -348
- package/lib/module/base/FontManager.js +0 -0
- package/lib/module/base/FontManager.js.map +1 -1
- package/lib/module/base/VexflowRecordingContext.js +2 -0
- package/lib/module/base/VexflowRecordingContext.js.map +1 -1
- package/lib/module/base/index.js +1 -0
- package/lib/module/base/index.js.map +1 -1
- package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +24 -7
- package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
- package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +11 -3
- package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -1
- package/lib/module/musicxml/MusicXmlParser/Spanners.js +21 -5
- package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -1
- package/lib/module/musicxml/MusicXmlParser/mappers.js +1 -1
- package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
- package/lib/module/percussion/index.js +8 -0
- package/lib/module/percussion/index.js.map +1 -0
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +4 -0
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
- package/lib/module/renderer/fixedNoteSpacing.js +56 -0
- package/lib/module/renderer/fixedNoteSpacing.js.map +1 -0
- package/lib/module/renderer/index.js +1 -0
- package/lib/module/renderer/index.js.map +1 -1
- package/lib/module/renderer/itemLayout.js +108 -0
- package/lib/module/renderer/itemLayout.js.map +1 -0
- package/lib/module/renderer/measure.js +31 -102
- package/lib/module/renderer/measure.js.map +1 -1
- package/lib/module/renderer/measureBounds.js +73 -0
- package/lib/module/renderer/measureBounds.js.map +1 -0
- package/lib/module/renderer/measureModifiers.js +99 -0
- package/lib/module/renderer/measureModifiers.js.map +1 -0
- package/lib/module/renderer/noteModifiers.js +212 -0
- package/lib/module/renderer/noteModifiers.js.map +1 -0
- package/lib/module/renderer/render.js +34 -115
- package/lib/module/renderer/render.js.map +1 -1
- package/lib/module/renderer/scale.js +16 -2
- package/lib/module/renderer/scale.js.map +1 -1
- package/lib/module/renderer/scoreParsing.js +37 -188
- package/lib/module/renderer/scoreParsing.js.map +1 -1
- package/lib/module/renderer/stave.js +37 -0
- package/lib/module/renderer/stave.js.map +1 -0
- package/lib/module/renderer/useScoreRecording.js +10 -3
- package/lib/module/renderer/useScoreRecording.js.map +1 -1
- package/lib/module/renderer/vfKeys.js +72 -0
- package/lib/module/renderer/vfKeys.js.map +1 -0
- package/lib/module/state/index.js +1 -0
- package/lib/module/state/index.js.map +1 -1
- package/lib/typescript/src/base/FontManager.d.ts +7 -0
- package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts +1 -0
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
- package/lib/typescript/src/base/index.d.ts +1 -0
- package/lib/typescript/src/base/index.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +4 -2
- package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +6 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +6 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
- package/lib/typescript/src/musicxml/index.d.ts +1 -0
- package/lib/typescript/src/musicxml/index.d.ts.map +1 -1
- package/lib/typescript/src/percussion/index.d.ts +4 -0
- package/lib/typescript/src/percussion/index.d.ts.map +1 -0
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
- package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts +17 -0
- package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts.map +1 -0
- package/lib/typescript/src/renderer/index.d.ts +2 -0
- package/lib/typescript/src/renderer/index.d.ts.map +1 -1
- package/lib/typescript/src/renderer/itemLayout.d.ts +23 -0
- package/lib/typescript/src/renderer/itemLayout.d.ts.map +1 -0
- package/lib/typescript/src/renderer/layout.d.ts +1 -1
- package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measure.d.ts +3 -6
- package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measureBounds.d.ts +13 -0
- package/lib/typescript/src/renderer/measureBounds.d.ts.map +1 -0
- package/lib/typescript/src/renderer/measureModifiers.d.ts +20 -0
- package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -0
- package/lib/typescript/src/renderer/noteModifiers.d.ts +29 -0
- package/lib/typescript/src/renderer/noteModifiers.d.ts.map +1 -0
- package/lib/typescript/src/renderer/render.d.ts +2 -27
- package/lib/typescript/src/renderer/render.d.ts.map +1 -1
- package/lib/typescript/src/renderer/scale.d.ts.map +1 -1
- package/lib/typescript/src/renderer/scoreParsing.d.ts +14 -40
- package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
- package/lib/typescript/src/renderer/stave.d.ts +11 -0
- package/lib/typescript/src/renderer/stave.d.ts.map +1 -0
- package/lib/typescript/src/renderer/types.d.ts +53 -4
- package/lib/typescript/src/renderer/types.d.ts.map +1 -1
- package/lib/typescript/src/renderer/useScoreRecording.d.ts +2 -2
- package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
- package/lib/typescript/src/renderer/vfKeys.d.ts +17 -0
- package/lib/typescript/src/renderer/vfKeys.d.ts.map +1 -0
- package/lib/typescript/src/state/index.d.ts +1 -0
- package/lib/typescript/src/state/index.d.ts.map +1 -1
- package/lib/typescript/src/state/types.d.ts +39 -6
- package/lib/typescript/src/state/types.d.ts.map +1 -1
- package/package.json +15 -5
- package/src/base/FontManager.ts +0 -0
- package/src/base/VexflowRecordingContext.ts +1 -0
- package/src/base/index.ts +1 -0
- package/src/musicxml/MusicXmlParser/InternalTypes.ts +5 -2
- package/src/musicxml/MusicXmlParser/MeasureParser.ts +36 -7
- package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +25 -8
- package/src/musicxml/MusicXmlParser/Spanners.ts +27 -4
- package/src/musicxml/MusicXmlParser/mappers.ts +7 -0
- package/src/musicxml/index.ts +1 -0
- package/src/percussion/index.ts +7 -0
- package/src/renderer/Layout/VerticalSpacing.ts +1 -1
- package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +4 -0
- package/src/renderer/fixedNoteSpacing.ts +78 -0
- package/src/renderer/index.ts +8 -0
- package/src/renderer/itemLayout.ts +137 -0
- package/src/renderer/layout.ts +1 -1
- package/src/renderer/measure.tsx +85 -182
- package/src/renderer/measureBounds.ts +109 -0
- package/src/renderer/measureModifiers.ts +133 -0
- package/src/renderer/noteModifiers.ts +298 -0
- package/src/renderer/render.tsx +66 -171
- package/src/renderer/scale.ts +21 -1
- package/src/renderer/scoreParsing.tsx +78 -262
- package/src/renderer/stave.ts +44 -0
- package/src/renderer/types.ts +67 -4
- package/src/renderer/useScoreRecording.ts +11 -3
- package/src/renderer/vfKeys.ts +88 -0
- package/src/state/index.ts +1 -0
- package/src/state/types.ts +48 -5
package/src/renderer/measure.tsx
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Articulation as VFArticulation,
|
|
3
|
-
Formatter,
|
|
4
|
-
GraceNoteGroup,
|
|
5
|
-
Stave,
|
|
6
|
-
Voice as VFVoice,
|
|
7
|
-
} from 'vexflow';
|
|
8
|
-
import type { BoundingBox, RenderContext } from 'vexflow';
|
|
1
|
+
import { Formatter, Stave, Voice as VFVoice } from 'vexflow';
|
|
9
2
|
import { Platform } from 'react-native';
|
|
10
3
|
|
|
11
4
|
import {
|
|
@@ -20,18 +13,21 @@ import {
|
|
|
20
13
|
makeVFVoice,
|
|
21
14
|
resolveGroupStaves,
|
|
22
15
|
} from './scoreParsing';
|
|
23
|
-
import
|
|
24
|
-
|
|
16
|
+
import {
|
|
17
|
+
applyMeasureModifiers,
|
|
18
|
+
resolveMeasureModifiers,
|
|
19
|
+
} from './measureModifiers';
|
|
20
|
+
import { mergeNoteBounds, mergeY } from './measureBounds';
|
|
21
|
+
import { applyStaffLines } from './stave';
|
|
22
|
+
import type { ScoreItemHooks, ScoreOptions } from './types';
|
|
23
|
+
import type { StaffVerticalBounds } from './measureBounds';
|
|
24
|
+
import type { ResolvedMeasureModifiers } from './measureModifiers';
|
|
25
|
+
import type { ResolvedMeasureState } from './scoreParsing';
|
|
25
26
|
import {
|
|
26
27
|
VEXFLOW_STAVE_BOTTOM_LINE_OFFSET,
|
|
27
28
|
VEXFLOW_STAVE_TOP_LINE_OFFSET,
|
|
28
29
|
} from './Layout/LayoutMetrics';
|
|
29
30
|
|
|
30
|
-
export interface StaffVerticalBounds {
|
|
31
|
-
top: number;
|
|
32
|
-
bottom: number;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
31
|
export interface MeasuredMeasure {
|
|
36
32
|
groupId: string;
|
|
37
33
|
measureIndex: number;
|
|
@@ -46,12 +42,23 @@ export interface MeasuredScore {
|
|
|
46
42
|
maxIntrinsicNoteWidth: number;
|
|
47
43
|
}
|
|
48
44
|
|
|
45
|
+
interface StaffMeasurementContext {
|
|
46
|
+
ownerStaffId: string;
|
|
47
|
+
staffIndex: number;
|
|
48
|
+
staff: Staff;
|
|
49
|
+
measure: Staff['measures'][number];
|
|
50
|
+
resolvedState: ResolvedMeasureState;
|
|
51
|
+
modifiers: ResolvedMeasureModifiers;
|
|
52
|
+
voiceArtifacts: ReturnType<typeof makeVFVoice>[];
|
|
53
|
+
}
|
|
54
|
+
|
|
49
55
|
/**
|
|
50
56
|
* Measures each staff group by building VexFlow voices and estimating note width.
|
|
51
57
|
*/
|
|
52
58
|
export function measureScore(
|
|
53
59
|
score: Score,
|
|
54
|
-
options: ScoreOptions
|
|
60
|
+
options: ScoreOptions,
|
|
61
|
+
{ decorateItem }: Pick<ScoreItemHooks, 'decorateItem'> = {}
|
|
55
62
|
): MeasuredScore {
|
|
56
63
|
const groups = buildMeasurementGroups(score);
|
|
57
64
|
const attachmentsByOwner = indexAttachmentsByOwner(score);
|
|
@@ -89,37 +96,44 @@ export function measureScore(
|
|
|
89
96
|
resolvedStatesByStaff[staffIndex]?.[measureIndex],
|
|
90
97
|
])
|
|
91
98
|
);
|
|
92
|
-
const staffMeasurementContexts = staves.map(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
99
|
+
const staffMeasurementContexts = staves.map(
|
|
100
|
+
(staff, staffIndex): StaffMeasurementContext => {
|
|
101
|
+
const measure = staff.measures[measureIndex]!;
|
|
102
|
+
const resolvedState =
|
|
103
|
+
resolvedStatesByStaff[staffIndex]![measureIndex]!;
|
|
104
|
+
measureNumbers.push(measure.number);
|
|
105
|
+
|
|
106
|
+
const voiceArtifacts = measure.voices.map((voice, voiceIndex) =>
|
|
107
|
+
makeVFVoice(score, resolvedState.meter, resolvedState.clef, voice, {
|
|
108
|
+
attachmentsByOwner,
|
|
109
|
+
staff,
|
|
110
|
+
directions: voiceIndex === 0 ? measure.directions : undefined,
|
|
111
|
+
itemContext: decorateItem
|
|
112
|
+
? { decorateItem, staff, measureIndex }
|
|
113
|
+
: undefined,
|
|
114
|
+
resolveClef: (item) =>
|
|
115
|
+
item.targetStaffId
|
|
116
|
+
? resolvedStateByStaffId.get(item.targetStaffId)?.clef ??
|
|
117
|
+
resolvedState.clef
|
|
118
|
+
: resolvedState.clef,
|
|
119
|
+
})
|
|
120
|
+
);
|
|
121
|
+
const vfVoices = voiceArtifacts.map(({ vfVoice }) => vfVoice);
|
|
122
|
+
|
|
123
|
+
formatter.joinVoices(vfVoices);
|
|
124
|
+
allVoices.push(...vfVoices);
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
ownerStaffId: staff.id,
|
|
128
|
+
staffIndex,
|
|
129
|
+
staff,
|
|
130
|
+
measure,
|
|
131
|
+
resolvedState,
|
|
132
|
+
modifiers: resolveMeasureModifiers(measure, measureIndex),
|
|
133
|
+
voiceArtifacts,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
);
|
|
123
137
|
|
|
124
138
|
try {
|
|
125
139
|
// Clef and time signature sit before the note area, so the intrinsic
|
|
@@ -168,34 +182,31 @@ export function measureScore(
|
|
|
168
182
|
const MODIFIER_PROBE_STAVE_WIDTH = 500;
|
|
169
183
|
|
|
170
184
|
/**
|
|
171
|
-
* Widest
|
|
172
|
-
*
|
|
185
|
+
* Widest left-modifier block of the measure, measured as the note-start delta
|
|
186
|
+
* between a bare probe stave and one carrying the resolved modifiers.
|
|
173
187
|
*/
|
|
174
188
|
function measureLeftModifierWidth(
|
|
175
|
-
staffMeasurementContexts:
|
|
176
|
-
resolvedState: { clef: string; meter: { beats: number; beatUnit: number } };
|
|
177
|
-
showClef: boolean;
|
|
178
|
-
showMeter: boolean;
|
|
179
|
-
}>
|
|
189
|
+
staffMeasurementContexts: StaffMeasurementContext[]
|
|
180
190
|
): number {
|
|
181
191
|
return staffMeasurementContexts.reduce(
|
|
182
|
-
(maxWidth, { resolvedState,
|
|
183
|
-
|
|
192
|
+
(maxWidth, { resolvedState, modifiers }) => {
|
|
193
|
+
const { showClef, showMeter, showKeySignature, startBarline } = modifiers;
|
|
194
|
+
|
|
195
|
+
if (
|
|
196
|
+
!showClef &&
|
|
197
|
+
!showMeter &&
|
|
198
|
+
!showKeySignature &&
|
|
199
|
+
startBarline === 'single'
|
|
200
|
+
) {
|
|
184
201
|
return maxWidth;
|
|
185
202
|
}
|
|
186
203
|
|
|
187
204
|
const bareStave = new Stave(0, 0, MODIFIER_PROBE_STAVE_WIDTH);
|
|
188
|
-
const modifiedStave =
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (showMeter) {
|
|
195
|
-
modifiedStave.addTimeSignature(
|
|
196
|
-
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
197
|
-
);
|
|
198
|
-
}
|
|
205
|
+
const modifiedStave = applyMeasureModifiers(
|
|
206
|
+
new Stave(0, 0, MODIFIER_PROBE_STAVE_WIDTH),
|
|
207
|
+
modifiers,
|
|
208
|
+
resolvedState
|
|
209
|
+
);
|
|
199
210
|
|
|
200
211
|
return Math.max(
|
|
201
212
|
maxWidth,
|
|
@@ -215,33 +226,16 @@ function measureStaffVerticalBounds({
|
|
|
215
226
|
allVoices: VFVoice[];
|
|
216
227
|
intrinsicNoteWidth: number;
|
|
217
228
|
staves: Staff[];
|
|
218
|
-
staffMeasurementContexts:
|
|
219
|
-
ownerStaffId: string;
|
|
220
|
-
staffIndex: number;
|
|
221
|
-
measure: Staff['measures'][number];
|
|
222
|
-
resolvedState: ReturnType<typeof buildResolvedMeasureStates>[number];
|
|
223
|
-
showClef: boolean;
|
|
224
|
-
showMeter: boolean;
|
|
225
|
-
voiceArtifacts: ReturnType<typeof makeVFVoice>[];
|
|
226
|
-
}>;
|
|
229
|
+
staffMeasurementContexts: StaffMeasurementContext[];
|
|
227
230
|
}): StaffVerticalBounds[] {
|
|
228
231
|
const width = Math.max(intrinsicNoteWidth, 1);
|
|
229
232
|
const renderedStaves = staffMeasurementContexts.map(
|
|
230
|
-
({ resolvedState,
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (showMeter) {
|
|
238
|
-
stave.addTimeSignature(
|
|
239
|
-
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return stave;
|
|
244
|
-
}
|
|
233
|
+
({ resolvedState, modifiers, staff }) =>
|
|
234
|
+
applyMeasureModifiers(
|
|
235
|
+
applyStaffLines(new Stave(0, 0, width), staff.lines),
|
|
236
|
+
modifiers,
|
|
237
|
+
resolvedState
|
|
238
|
+
)
|
|
245
239
|
);
|
|
246
240
|
const staffIndexById = new Map(
|
|
247
241
|
staves.map((staff, staffIndex) => [staff.id, staffIndex])
|
|
@@ -302,7 +296,6 @@ function measureStaffVerticalBounds({
|
|
|
302
296
|
staffIndexById.get(
|
|
303
297
|
items[noteIndex]?.targetStaffId ?? ownerStaffId
|
|
304
298
|
) ?? staffIndex;
|
|
305
|
-
mergeModifierBounds(bounds[ownerStaffIndex], note);
|
|
306
299
|
mergeNoteBounds(bounds[ownerStaffIndex], note);
|
|
307
300
|
});
|
|
308
301
|
|
|
@@ -331,93 +324,3 @@ function measureStaffVerticalBounds({
|
|
|
331
324
|
|
|
332
325
|
return bounds;
|
|
333
326
|
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Render context that absorbs every method call without drawing anything, so
|
|
337
|
-
* modifiers can run their `draw()` placement math during measurement.
|
|
338
|
-
*/
|
|
339
|
-
function createNoopRenderContext(): RenderContext {
|
|
340
|
-
const memo: Record<PropertyKey, unknown> = {};
|
|
341
|
-
const proxy: object = new Proxy(memo, {
|
|
342
|
-
get: (target, property) => {
|
|
343
|
-
if (!(property in target)) {
|
|
344
|
-
target[property] = () => proxy;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
return target[property];
|
|
348
|
-
},
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
return proxy as RenderContext;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const NOOP_RENDER_CONTEXT = createNoopRenderContext();
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* Merges the drawn extents of a note's articulations and grace-note groups
|
|
358
|
-
* into the staff bounds. VexFlow only positions these modifiers inside
|
|
359
|
-
* `draw()`, so each one is drawn against a no-op context first to make its
|
|
360
|
-
* bounding boxes real.
|
|
361
|
-
*/
|
|
362
|
-
function mergeModifierBounds(
|
|
363
|
-
bounds: StaffVerticalBounds | undefined,
|
|
364
|
-
note: VFVoiceNote
|
|
365
|
-
) {
|
|
366
|
-
if (!bounds) {
|
|
367
|
-
return;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
for (const modifier of note.getModifiers()) {
|
|
371
|
-
const isArticulation = modifier instanceof VFArticulation;
|
|
372
|
-
const isGraceNoteGroup = modifier instanceof GraceNoteGroup;
|
|
373
|
-
|
|
374
|
-
if (!isArticulation && !isGraceNoteGroup) {
|
|
375
|
-
continue;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
try {
|
|
379
|
-
modifier.setContext(NOOP_RENDER_CONTEXT);
|
|
380
|
-
modifier.draw();
|
|
381
|
-
|
|
382
|
-
if (isGraceNoteGroup) {
|
|
383
|
-
modifier.getGraceNotes().forEach((graceNote) => {
|
|
384
|
-
mergeBoundingBox(bounds, graceNote.getBoundingBox());
|
|
385
|
-
});
|
|
386
|
-
} else {
|
|
387
|
-
mergeBoundingBox(bounds, modifier.getBoundingBox());
|
|
388
|
-
}
|
|
389
|
-
} catch {
|
|
390
|
-
// Ghost and spacer notes cannot place modifiers; note bounds remain
|
|
391
|
-
// the fallback.
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
function mergeNoteBounds(
|
|
397
|
-
bounds: StaffVerticalBounds | undefined,
|
|
398
|
-
note: VFVoiceNote
|
|
399
|
-
) {
|
|
400
|
-
if (!bounds) {
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
try {
|
|
405
|
-
mergeBoundingBox(bounds, note.getBoundingBox());
|
|
406
|
-
} catch {
|
|
407
|
-
// Ghost notes and spacers may not expose useful boxes.
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
function mergeBoundingBox(bounds: StaffVerticalBounds, box: BoundingBox) {
|
|
412
|
-
mergeY(bounds, box.getY());
|
|
413
|
-
mergeY(bounds, box.getY() + box.getH());
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
function mergeY(bounds: StaffVerticalBounds | undefined, y: number) {
|
|
417
|
-
if (!bounds || !Number.isFinite(y)) {
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
bounds.top = Math.min(bounds.top, y);
|
|
422
|
-
bounds.bottom = Math.max(bounds.bottom, y);
|
|
423
|
-
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BoundingBox,
|
|
3
|
+
GraceNoteGroup,
|
|
4
|
+
Modifier,
|
|
5
|
+
RenderContext,
|
|
6
|
+
} from 'vexflow';
|
|
7
|
+
|
|
8
|
+
import { hasNoteHeads } from './noteModifiers';
|
|
9
|
+
import type { VFVoiceNote } from './scoreParsing';
|
|
10
|
+
|
|
11
|
+
export interface StaffVerticalBounds {
|
|
12
|
+
top: number;
|
|
13
|
+
bottom: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Render context that absorbs every method call without drawing anything, so
|
|
18
|
+
* notes and modifiers can run their `draw()` placement math during
|
|
19
|
+
* measurement.
|
|
20
|
+
*/
|
|
21
|
+
function createNoopRenderContext(): RenderContext {
|
|
22
|
+
const memo: Record<PropertyKey, unknown> = {};
|
|
23
|
+
const proxy: object = new Proxy(memo, {
|
|
24
|
+
get: (target, property) => {
|
|
25
|
+
if (!(property in target)) {
|
|
26
|
+
target[property] = () => proxy;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return target[property];
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return proxy as RenderContext;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const NOOP_RENDER_CONTEXT = createNoopRenderContext();
|
|
37
|
+
|
|
38
|
+
function hasGraceNotes(
|
|
39
|
+
modifier: Modifier
|
|
40
|
+
): modifier is Modifier & Pick<GraceNoteGroup, 'getGraceNotes'> {
|
|
41
|
+
return 'getGraceNotes' in modifier;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Mutates the note (draws it against a no-op context and anchors unplaced
|
|
46
|
+
* modifiers); safe only because measurement voices are discarded and
|
|
47
|
+
* rendering builds fresh ones.
|
|
48
|
+
*/
|
|
49
|
+
export function mergeNoteBounds(
|
|
50
|
+
bounds: StaffVerticalBounds | undefined,
|
|
51
|
+
note: VFVoiceNote
|
|
52
|
+
) {
|
|
53
|
+
if (!bounds) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const modifiers = note.getModifiers();
|
|
58
|
+
const origins = modifiers.map((modifier) => [
|
|
59
|
+
modifier.getX(),
|
|
60
|
+
modifier.getY(),
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
note.setContext(NOOP_RENDER_CONTEXT).draw();
|
|
65
|
+
} catch {
|
|
66
|
+
// Modifiers the draw did not reach are anchored below instead.
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
modifiers.forEach((modifier, index) => {
|
|
70
|
+
try {
|
|
71
|
+
const [x, y] = origins[index]!;
|
|
72
|
+
|
|
73
|
+
if (modifier.getX() === x && modifier.getY() === y) {
|
|
74
|
+
const start = note.getModifierStartXY(
|
|
75
|
+
modifier.getPosition(),
|
|
76
|
+
modifier.checkIndex()
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
modifier.setX(start.x).setY(start.y);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (hasGraceNotes(modifier)) {
|
|
83
|
+
modifier.getGraceNotes().forEach((graceNote) => {
|
|
84
|
+
mergeBoundingBox(bounds, graceNote.getBoundingBox());
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
// Ghost and spacer notes cannot anchor modifiers.
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (hasNoteHeads(note)) {
|
|
93
|
+
mergeBoundingBox(bounds, note.getBoundingBox());
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function mergeBoundingBox(bounds: StaffVerticalBounds, box: BoundingBox) {
|
|
98
|
+
mergeY(bounds, box.getY());
|
|
99
|
+
mergeY(bounds, box.getY() + box.getH());
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function mergeY(bounds: StaffVerticalBounds | undefined, y: number) {
|
|
103
|
+
if (!bounds || !Number.isFinite(y)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
bounds.top = Math.min(bounds.top, y);
|
|
108
|
+
bounds.bottom = Math.max(bounds.bottom, y);
|
|
109
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { BarlineType, type Stave, StaveModifierPosition } from 'vexflow';
|
|
2
|
+
|
|
3
|
+
import type { Barline, KeyMode, KeySignature, Measure, Step } from '../state';
|
|
4
|
+
import type { ResolvedMeasureState } from './scoreParsing';
|
|
5
|
+
|
|
6
|
+
export interface ResolvedMeasureModifiers {
|
|
7
|
+
showClef: boolean;
|
|
8
|
+
showMeter: boolean;
|
|
9
|
+
showKeySignature: boolean;
|
|
10
|
+
startBarline: Barline;
|
|
11
|
+
endBarline: Barline;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Fifths of the natural tonic; `#` adds 7, `b` subtracts 7. */
|
|
15
|
+
const STEP_FIFTHS: Record<Step, number> = {
|
|
16
|
+
C: 0,
|
|
17
|
+
D: 2,
|
|
18
|
+
E: 4,
|
|
19
|
+
F: -1,
|
|
20
|
+
G: 1,
|
|
21
|
+
A: 3,
|
|
22
|
+
B: 5,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Fifths offset from the parallel major with the same tonic. */
|
|
26
|
+
const MODE_FIFTHS: Record<KeyMode, number> = {
|
|
27
|
+
major: 0,
|
|
28
|
+
mixolydian: -1,
|
|
29
|
+
dorian: -2,
|
|
30
|
+
minor: -3,
|
|
31
|
+
phrygian: -4,
|
|
32
|
+
lydian: 1,
|
|
33
|
+
locrian: -5,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** VexFlow major key specs indexed by fifths + 7. */
|
|
37
|
+
const MAJOR_KEY_SPECS = [
|
|
38
|
+
'Cb',
|
|
39
|
+
'Gb',
|
|
40
|
+
'Db',
|
|
41
|
+
'Ab',
|
|
42
|
+
'Eb',
|
|
43
|
+
'Bb',
|
|
44
|
+
'F',
|
|
45
|
+
'C',
|
|
46
|
+
'G',
|
|
47
|
+
'D',
|
|
48
|
+
'A',
|
|
49
|
+
'E',
|
|
50
|
+
'B',
|
|
51
|
+
'F#',
|
|
52
|
+
'C#',
|
|
53
|
+
] as const;
|
|
54
|
+
|
|
55
|
+
const START_BARLINE_TYPES: Partial<Record<Barline, BarlineType>> = {
|
|
56
|
+
'repeat-begin': BarlineType.REPEAT_BEGIN,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const END_BARLINE_TYPES: Record<Barline, BarlineType> = {
|
|
60
|
+
'single': BarlineType.SINGLE,
|
|
61
|
+
'double': BarlineType.DOUBLE,
|
|
62
|
+
'end': BarlineType.END,
|
|
63
|
+
'final': BarlineType.END,
|
|
64
|
+
'repeat-begin': BarlineType.SINGLE,
|
|
65
|
+
'repeat-end': BarlineType.REPEAT_END,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Resolves the tri-state measure modifiers: `true` shows, `false` hides,
|
|
70
|
+
* `undefined` falls back to the default (clef on the first measure only,
|
|
71
|
+
* meter and key signature hidden, single barlines).
|
|
72
|
+
*/
|
|
73
|
+
export function resolveMeasureModifiers(
|
|
74
|
+
measure: Pick<Measure, 'leftModifiers' | 'rightModifiers'>,
|
|
75
|
+
measureIndex: number
|
|
76
|
+
): ResolvedMeasureModifiers {
|
|
77
|
+
const { leftModifiers, rightModifiers } = measure;
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
showClef: leftModifiers?.showClef ?? measureIndex === 0,
|
|
81
|
+
showMeter: leftModifiers?.showMeter ?? false,
|
|
82
|
+
showKeySignature: leftModifiers?.showKeySignature ?? false,
|
|
83
|
+
startBarline: leftModifiers?.startBarline ?? 'single',
|
|
84
|
+
endBarline: rightModifiers?.endBarline ?? 'single',
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function applyMeasureModifiers(
|
|
89
|
+
stave: Stave,
|
|
90
|
+
modifiers: ResolvedMeasureModifiers,
|
|
91
|
+
resolvedState: ResolvedMeasureState
|
|
92
|
+
): Stave {
|
|
93
|
+
const { clef, keySignature, meter } = resolvedState;
|
|
94
|
+
|
|
95
|
+
if (modifiers.showClef) {
|
|
96
|
+
stave.addClef(clef);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (modifiers.showKeySignature && keySignature && clef !== 'percussion') {
|
|
100
|
+
stave.addKeySignature(keySignatureToVFSpec(keySignature));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (modifiers.showMeter) {
|
|
104
|
+
stave.addTimeSignature(`${meter.beats}/${meter.beatUnit}`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
stave.setBegBarType(
|
|
108
|
+
START_BARLINE_TYPES[modifiers.startBarline] ?? BarlineType.SINGLE
|
|
109
|
+
);
|
|
110
|
+
stave.setEndBarType(END_BARLINE_TYPES[modifiers.endBarline]);
|
|
111
|
+
|
|
112
|
+
// Stave.format pads a lone REPEAT_BEGIN by 0, so its dots would overlap the first note.
|
|
113
|
+
if (
|
|
114
|
+
modifiers.startBarline === 'repeat-begin' &&
|
|
115
|
+
stave.getModifiers(StaveModifierPosition.BEGIN).length === 1
|
|
116
|
+
) {
|
|
117
|
+
const { xMax, paddingRight } = stave.getModifiers()[0]!.getLayoutMetrics()!;
|
|
118
|
+
|
|
119
|
+
stave.setNoteStartX(stave.getX() + xMax + paddingRight);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return stave;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Major-key VexFlow spec for the key's accidentals, respelt enharmonically beyond 7 sharps/flats. */
|
|
126
|
+
export function keySignatureToVFSpec(keySignature: KeySignature): string {
|
|
127
|
+
const { tonic, accidental, mode = 'major' } = keySignature;
|
|
128
|
+
const accidentalFifths = accidental === '#' ? 7 : accidental === 'b' ? -7 : 0;
|
|
129
|
+
const fifths = STEP_FIFTHS[tonic] + accidentalFifths + MODE_FIFTHS[mode];
|
|
130
|
+
const wrapped = fifths > 7 ? fifths - 12 : fifths < -7 ? fifths + 12 : fifths;
|
|
131
|
+
|
|
132
|
+
return MAJOR_KEY_SPECS[wrapped + 7]!;
|
|
133
|
+
}
|