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.
- package/README.md +71 -403
- package/lib/module/base/FontManager.js +5 -5
- package/lib/module/base/FontManager.js.map +1 -1
- package/lib/module/base/VexflowCanvas.js +1 -1
- package/lib/module/base/VexflowCanvas.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/VexflowRecordingReplay.js +1 -1
- package/lib/module/base/VexflowRecordingReplay.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/{state/constants.js → percussion/index.js} +3 -3
- package/lib/module/percussion/index.js.map +1 -0
- package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js +4 -3
- package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js.map +1 -1
- package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js +1 -1
- package/lib/module/renderer/ScoreRenderer/ScorePlayhead.js.map +1 -1
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +5 -1
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
- package/lib/module/renderer/ScoreRenderer/utils.js +1 -1
- package/lib/module/renderer/ScoreRenderer/utils.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 +30 -106
- 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 +33 -116
- 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 -199
- package/lib/module/renderer/scoreParsing.js.map +1 -1
- package/lib/module/renderer/stave.js +19 -3
- package/lib/module/renderer/stave.js.map +1 -1
- 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 -1
- package/lib/module/state/index.js.map +1 -1
- package/lib/typescript/src/base/FontManager.d.ts +3 -2
- package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts +2 -1
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +1 -1
- package/lib/typescript/src/base/VexflowRecordingReplay.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/base/types.d.ts +1 -1
- package/lib/typescript/src/base/types.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/ScoreOverlayPicture.d.ts +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts.map +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/ScorePlayhead.d.ts.map +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts +1 -1
- package/lib/typescript/src/renderer/ScoreRenderer/utils.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 -42
- package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
- package/lib/typescript/src/renderer/stave.d.ts +2 -0
- package/lib/typescript/src/renderer/stave.d.ts.map +1 -1
- package/lib/typescript/src/renderer/types.d.ts +52 -3
- package/lib/typescript/src/renderer/types.d.ts.map +1 -1
- package/lib/typescript/src/renderer/useScoreRecording.d.ts +3 -3
- 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 -1
- package/lib/typescript/src/state/index.d.ts.map +1 -1
- package/lib/typescript/src/state/types.d.ts +23 -5
- package/lib/typescript/src/state/types.d.ts.map +1 -1
- package/package.json +31 -17
- package/src/base/FontManager.ts +5 -11
- package/src/base/TextMeasureContext.ts +1 -1
- package/src/base/VexflowCanvas.tsx +1 -1
- package/src/base/VexflowRecordingContext.ts +2 -4
- package/src/base/VexflowRecordingReplay.ts +1 -1
- package/src/base/index.ts +1 -0
- package/src/base/types.ts +1 -1
- 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/ScoreOverlayPicture.tsx +4 -7
- package/src/renderer/ScoreRenderer/ScorePlayhead.tsx +1 -1
- package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +5 -7
- package/src/renderer/ScoreRenderer/types.ts +1 -4
- package/src/renderer/ScoreRenderer/utils.ts +1 -1
- 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 +84 -186
- 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 +59 -169
- package/src/renderer/scale.ts +21 -1
- package/src/renderer/scoreParsing.tsx +77 -288
- package/src/renderer/stave.ts +22 -3
- package/src/renderer/types.ts +66 -3
- package/src/renderer/useScoreRecording.ts +12 -4
- package/src/renderer/vfKeys.ts +88 -0
- package/src/state/index.ts +1 -1
- package/src/state/types.ts +30 -5
- package/lib/module/musicxml/testfiles/lg-8102429.xml +0 -79620
- package/lib/module/state/constants.js.map +0 -1
- package/lib/typescript/src/state/constants.d.ts +0 -4
- package/lib/typescript/src/state/constants.d.ts.map +0 -1
- package/src/musicxml/testfiles/lg-8102429.xml +0 -79620
- package/src/state/constants.ts +0 -4
package/src/renderer/measure.tsx
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Annotation as VFAnnotation,
|
|
3
|
-
Articulation as VFArticulation,
|
|
4
|
-
Formatter,
|
|
5
|
-
GraceNoteGroup,
|
|
6
|
-
Stave,
|
|
7
|
-
Voice as VFVoice,
|
|
8
|
-
} from 'vexflow';
|
|
9
|
-
import type { BoundingBox, RenderContext } from 'vexflow';
|
|
1
|
+
import { Formatter, Stave, Voice as VFVoice } from 'vexflow';
|
|
10
2
|
import { Platform } from 'react-native';
|
|
11
3
|
|
|
12
4
|
import {
|
|
@@ -21,19 +13,21 @@ import {
|
|
|
21
13
|
makeVFVoice,
|
|
22
14
|
resolveGroupStaves,
|
|
23
15
|
} from './scoreParsing';
|
|
16
|
+
import {
|
|
17
|
+
applyMeasureModifiers,
|
|
18
|
+
resolveMeasureModifiers,
|
|
19
|
+
} from './measureModifiers';
|
|
20
|
+
import { mergeNoteBounds, mergeY } from './measureBounds';
|
|
24
21
|
import { applyStaffLines } from './stave';
|
|
25
|
-
import type { ScoreOptions } from './types';
|
|
26
|
-
import type {
|
|
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';
|
|
27
26
|
import {
|
|
28
27
|
VEXFLOW_STAVE_BOTTOM_LINE_OFFSET,
|
|
29
28
|
VEXFLOW_STAVE_TOP_LINE_OFFSET,
|
|
30
29
|
} from './Layout/LayoutMetrics';
|
|
31
30
|
|
|
32
|
-
export interface StaffVerticalBounds {
|
|
33
|
-
top: number;
|
|
34
|
-
bottom: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
31
|
export interface MeasuredMeasure {
|
|
38
32
|
groupId: string;
|
|
39
33
|
measureIndex: number;
|
|
@@ -48,12 +42,23 @@ export interface MeasuredScore {
|
|
|
48
42
|
maxIntrinsicNoteWidth: number;
|
|
49
43
|
}
|
|
50
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
|
+
|
|
51
55
|
/**
|
|
52
56
|
* Measures each staff group by building VexFlow voices and estimating note width.
|
|
53
57
|
*/
|
|
54
58
|
export function measureScore(
|
|
55
59
|
score: Score,
|
|
56
|
-
options: ScoreOptions
|
|
60
|
+
options: ScoreOptions,
|
|
61
|
+
{ decorateItem }: Pick<ScoreItemHooks, 'decorateItem'> = {}
|
|
57
62
|
): MeasuredScore {
|
|
58
63
|
const groups = buildMeasurementGroups(score);
|
|
59
64
|
const attachmentsByOwner = indexAttachmentsByOwner(score);
|
|
@@ -91,38 +96,44 @@ export function measureScore(
|
|
|
91
96
|
resolvedStatesByStaff[staffIndex]?.[measureIndex],
|
|
92
97
|
])
|
|
93
98
|
);
|
|
94
|
-
const staffMeasurementContexts = staves.map(
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
+
);
|
|
126
137
|
|
|
127
138
|
try {
|
|
128
139
|
// Clef and time signature sit before the note area, so the intrinsic
|
|
@@ -171,34 +182,31 @@ export function measureScore(
|
|
|
171
182
|
const MODIFIER_PROBE_STAVE_WIDTH = 500;
|
|
172
183
|
|
|
173
184
|
/**
|
|
174
|
-
* Widest
|
|
175
|
-
*
|
|
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.
|
|
176
187
|
*/
|
|
177
188
|
function measureLeftModifierWidth(
|
|
178
|
-
staffMeasurementContexts:
|
|
179
|
-
resolvedState: { clef: string; meter: { beats: number; beatUnit: number } };
|
|
180
|
-
showClef: boolean;
|
|
181
|
-
showMeter: boolean;
|
|
182
|
-
}>
|
|
189
|
+
staffMeasurementContexts: StaffMeasurementContext[]
|
|
183
190
|
): number {
|
|
184
191
|
return staffMeasurementContexts.reduce(
|
|
185
|
-
(maxWidth, { resolvedState,
|
|
186
|
-
|
|
192
|
+
(maxWidth, { resolvedState, modifiers }) => {
|
|
193
|
+
const { showClef, showMeter, showKeySignature, startBarline } = modifiers;
|
|
194
|
+
|
|
195
|
+
if (
|
|
196
|
+
!showClef &&
|
|
197
|
+
!showMeter &&
|
|
198
|
+
!showKeySignature &&
|
|
199
|
+
startBarline === 'single'
|
|
200
|
+
) {
|
|
187
201
|
return maxWidth;
|
|
188
202
|
}
|
|
189
203
|
|
|
190
204
|
const bareStave = new Stave(0, 0, MODIFIER_PROBE_STAVE_WIDTH);
|
|
191
|
-
const modifiedStave =
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (showMeter) {
|
|
198
|
-
modifiedStave.addTimeSignature(
|
|
199
|
-
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
200
|
-
);
|
|
201
|
-
}
|
|
205
|
+
const modifiedStave = applyMeasureModifiers(
|
|
206
|
+
new Stave(0, 0, MODIFIER_PROBE_STAVE_WIDTH),
|
|
207
|
+
modifiers,
|
|
208
|
+
resolvedState
|
|
209
|
+
);
|
|
202
210
|
|
|
203
211
|
return Math.max(
|
|
204
212
|
maxWidth,
|
|
@@ -218,34 +226,16 @@ function measureStaffVerticalBounds({
|
|
|
218
226
|
allVoices: VFVoice[];
|
|
219
227
|
intrinsicNoteWidth: number;
|
|
220
228
|
staves: Staff[];
|
|
221
|
-
staffMeasurementContexts:
|
|
222
|
-
ownerStaffId: string;
|
|
223
|
-
staffIndex: number;
|
|
224
|
-
staffLines: Staff['lines'];
|
|
225
|
-
measure: Staff['measures'][number];
|
|
226
|
-
resolvedState: ReturnType<typeof buildResolvedMeasureStates>[number];
|
|
227
|
-
showClef: boolean;
|
|
228
|
-
showMeter: boolean;
|
|
229
|
-
voiceArtifacts: ReturnType<typeof makeVFVoice>[];
|
|
230
|
-
}>;
|
|
229
|
+
staffMeasurementContexts: StaffMeasurementContext[];
|
|
231
230
|
}): StaffVerticalBounds[] {
|
|
232
231
|
const width = Math.max(intrinsicNoteWidth, 1);
|
|
233
232
|
const renderedStaves = staffMeasurementContexts.map(
|
|
234
|
-
({ resolvedState,
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
if (showMeter) {
|
|
242
|
-
stave.addTimeSignature(
|
|
243
|
-
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return stave;
|
|
248
|
-
}
|
|
233
|
+
({ resolvedState, modifiers, staff }) =>
|
|
234
|
+
applyMeasureModifiers(
|
|
235
|
+
applyStaffLines(new Stave(0, 0, width), staff.lines),
|
|
236
|
+
modifiers,
|
|
237
|
+
resolvedState
|
|
238
|
+
)
|
|
249
239
|
);
|
|
250
240
|
const staffIndexById = new Map(
|
|
251
241
|
staves.map((staff, staffIndex) => [staff.id, staffIndex])
|
|
@@ -306,7 +296,6 @@ function measureStaffVerticalBounds({
|
|
|
306
296
|
staffIndexById.get(
|
|
307
297
|
items[noteIndex]?.targetStaffId ?? ownerStaffId
|
|
308
298
|
) ?? staffIndex;
|
|
309
|
-
mergeModifierBounds(bounds[ownerStaffIndex], note);
|
|
310
299
|
mergeNoteBounds(bounds[ownerStaffIndex], note);
|
|
311
300
|
});
|
|
312
301
|
|
|
@@ -335,94 +324,3 @@ function measureStaffVerticalBounds({
|
|
|
335
324
|
|
|
336
325
|
return bounds;
|
|
337
326
|
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Render context that absorbs every method call without drawing anything, so
|
|
341
|
-
* modifiers can run their `draw()` placement math during measurement.
|
|
342
|
-
*/
|
|
343
|
-
function createNoopRenderContext(): RenderContext {
|
|
344
|
-
const memo: Record<PropertyKey, unknown> = {};
|
|
345
|
-
const proxy: object = new Proxy(memo, {
|
|
346
|
-
get: (target, property) => {
|
|
347
|
-
if (!(property in target)) {
|
|
348
|
-
target[property] = () => proxy;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
return target[property];
|
|
352
|
-
},
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
return proxy as RenderContext;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const NOOP_RENDER_CONTEXT = createNoopRenderContext();
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* Merges the drawn extents of a note's articulations, annotations and
|
|
362
|
-
* grace-note groups into the staff bounds. VexFlow only positions these
|
|
363
|
-
* modifiers inside `draw()`, so each one is drawn against a no-op context
|
|
364
|
-
* first to make its bounding boxes real.
|
|
365
|
-
*/
|
|
366
|
-
function mergeModifierBounds(
|
|
367
|
-
bounds: StaffVerticalBounds | undefined,
|
|
368
|
-
note: VFVoiceNote
|
|
369
|
-
) {
|
|
370
|
-
if (!bounds) {
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
for (const modifier of note.getModifiers()) {
|
|
375
|
-
const isArticulation = modifier instanceof VFArticulation;
|
|
376
|
-
const isAnnotation = modifier instanceof VFAnnotation;
|
|
377
|
-
const isGraceNoteGroup = modifier instanceof GraceNoteGroup;
|
|
378
|
-
|
|
379
|
-
if (!isArticulation && !isAnnotation && !isGraceNoteGroup) {
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
try {
|
|
384
|
-
modifier.setContext(NOOP_RENDER_CONTEXT);
|
|
385
|
-
modifier.draw();
|
|
386
|
-
|
|
387
|
-
if (isGraceNoteGroup) {
|
|
388
|
-
modifier.getGraceNotes().forEach((graceNote) => {
|
|
389
|
-
mergeBoundingBox(bounds, graceNote.getBoundingBox());
|
|
390
|
-
});
|
|
391
|
-
} else {
|
|
392
|
-
mergeBoundingBox(bounds, modifier.getBoundingBox());
|
|
393
|
-
}
|
|
394
|
-
} catch {
|
|
395
|
-
// Ghost and spacer notes cannot place modifiers; note bounds remain
|
|
396
|
-
// the fallback.
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
function mergeNoteBounds(
|
|
402
|
-
bounds: StaffVerticalBounds | undefined,
|
|
403
|
-
note: VFVoiceNote
|
|
404
|
-
) {
|
|
405
|
-
if (!bounds) {
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
try {
|
|
410
|
-
mergeBoundingBox(bounds, note.getBoundingBox());
|
|
411
|
-
} catch {
|
|
412
|
-
// Ghost notes and spacers may not expose useful boxes.
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
function mergeBoundingBox(bounds: StaffVerticalBounds, box: BoundingBox) {
|
|
417
|
-
mergeY(bounds, box.getY());
|
|
418
|
-
mergeY(bounds, box.getY() + box.getH());
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
function mergeY(bounds: StaffVerticalBounds | undefined, y: number) {
|
|
422
|
-
if (!bounds || !Number.isFinite(y)) {
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
bounds.top = Math.min(bounds.top, y);
|
|
427
|
-
bounds.bottom = Math.max(bounds.bottom, y);
|
|
428
|
-
}
|
|
@@ -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
|
+
}
|