vexflow-native 1.0.1 → 1.2.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 +35 -1
- package/lib/module/base/VexflowRecordingContext.js +69 -7
- package/lib/module/base/VexflowRecordingContext.js.map +1 -1
- package/lib/module/base/VexflowRecordingReplay.js +79 -5
- 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/renderer/ScoreRenderer/ScoreRenderer.js +103 -18
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
- package/lib/module/renderer/measure.js +85 -4
- package/lib/module/renderer/measure.js.map +1 -1
- package/lib/module/renderer/render.js +120 -7
- package/lib/module/renderer/render.js.map +1 -1
- package/lib/module/renderer/scale.js +78 -0
- package/lib/module/renderer/scale.js.map +1 -0
- package/lib/module/renderer/scoreParsing.js +96 -23
- package/lib/module/renderer/scoreParsing.js.map +1 -1
- package/lib/module/renderer/useScoreRecording.js +22 -4
- package/lib/module/renderer/useScoreRecording.js.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts +32 -3
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +10 -2
- package/lib/typescript/src/base/VexflowRecordingReplay.d.ts.map +1 -1
- package/lib/typescript/src/base/VexflowRecordingTypes.d.ts +53 -1
- package/lib/typescript/src/base/VexflowRecordingTypes.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/renderer/ScoreRenderer/ScoreRenderer.d.ts +12 -5
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
- package/lib/typescript/src/renderer/render.d.ts +11 -3
- package/lib/typescript/src/renderer/render.d.ts.map +1 -1
- package/lib/typescript/src/renderer/scale.d.ts +32 -0
- package/lib/typescript/src/renderer/scale.d.ts.map +1 -0
- package/lib/typescript/src/renderer/scoreParsing.d.ts +21 -2
- package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -1
- package/lib/typescript/src/renderer/types.d.ts +76 -1
- package/lib/typescript/src/renderer/types.d.ts.map +1 -1
- package/lib/typescript/src/renderer/useScoreRecording.d.ts +8 -1
- package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
- package/lib/typescript/src/state/types.d.ts +4 -0
- package/lib/typescript/src/state/types.d.ts.map +1 -1
- package/package.json +2 -3
- package/src/base/VexflowRecordingContext.ts +57 -7
- package/src/base/VexflowRecordingReplay.ts +121 -5
- package/src/base/VexflowRecordingTypes.ts +54 -2
- package/src/base/index.ts +1 -0
- package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +146 -25
- package/src/renderer/measure.tsx +117 -5
- package/src/renderer/render.tsx +181 -15
- package/src/renderer/scale.ts +93 -0
- package/src/renderer/scoreParsing.tsx +146 -33
- package/src/renderer/types.ts +80 -1
- package/src/renderer/useScoreRecording.ts +41 -3
- package/src/state/types.ts +13 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
|
-
import { memo, useCallback, useMemo, useState } from 'react';
|
|
2
|
+
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
3
|
import {
|
|
4
4
|
Canvas,
|
|
5
5
|
Group,
|
|
6
6
|
Picture,
|
|
7
7
|
Skia,
|
|
8
|
+
type SkPicture,
|
|
8
9
|
type SkTypefaceFontProvider,
|
|
9
10
|
type Transforms3d,
|
|
10
11
|
useCanvasRef,
|
|
@@ -17,10 +18,13 @@ import type { VexflowRecordingCommand } from '../../base';
|
|
|
17
18
|
import { renderVexflowRecordingCommands } from '../../base/VexflowRecordingReplay';
|
|
18
19
|
import { resolveScoreColorScheme } from '../colorScheme';
|
|
19
20
|
import { insets, renderOptions, spacing } from '../constants';
|
|
21
|
+
import { getRenderScale, toViewSize } from '../scale';
|
|
20
22
|
import type {
|
|
21
23
|
RendererSize,
|
|
22
24
|
RendererType,
|
|
25
|
+
ScoreItemStyleOverrides,
|
|
23
26
|
ScoreOptions,
|
|
27
|
+
ScoreRendererOptions,
|
|
24
28
|
ScoreRendererProps,
|
|
25
29
|
Viewport,
|
|
26
30
|
} from '../types';
|
|
@@ -29,7 +33,7 @@ import { createVisibleViewport } from '../viewport';
|
|
|
29
33
|
import ScoreScrollbar from './ScoreScrollbar';
|
|
30
34
|
import { getMaxScroll, useScoreScroll } from './useScoreScroll';
|
|
31
35
|
|
|
32
|
-
const EMPTY_OPTIONS:
|
|
36
|
+
const EMPTY_OPTIONS: ScoreRendererOptions = {};
|
|
33
37
|
const EMPTY_SIZE: RendererSize = { width: 0, height: 0 };
|
|
34
38
|
|
|
35
39
|
const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
@@ -38,9 +42,11 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
|
38
42
|
defaultFont,
|
|
39
43
|
fontManager,
|
|
40
44
|
colorScheme,
|
|
45
|
+
itemStyleOverrides,
|
|
41
46
|
options: userOptions = EMPTY_OPTIONS,
|
|
42
47
|
scrollEnabled = true,
|
|
43
48
|
showScrollbars = true,
|
|
49
|
+
onItemsLayout,
|
|
44
50
|
}) => {
|
|
45
51
|
const options = useMemo(() => withDefaultOptions(userOptions), [userOptions]);
|
|
46
52
|
const resolvedColorScheme = useMemo(
|
|
@@ -80,7 +86,11 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
|
80
86
|
}),
|
|
81
87
|
[viewportSize.height, viewportSize.width]
|
|
82
88
|
);
|
|
83
|
-
const {
|
|
89
|
+
const {
|
|
90
|
+
commands: recordedCommands,
|
|
91
|
+
layoutPlan,
|
|
92
|
+
itemsLayout,
|
|
93
|
+
} = useScoreRecording({
|
|
84
94
|
defaultFont,
|
|
85
95
|
enabled: hasViewportSize,
|
|
86
96
|
fontManager,
|
|
@@ -90,15 +100,37 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
|
90
100
|
score,
|
|
91
101
|
viewport,
|
|
92
102
|
});
|
|
103
|
+
// Scrolling and overflow checks need the view-space content size, not the
|
|
104
|
+
// content-space size that bounds the recorded picture.
|
|
105
|
+
const scale = getRenderScale(options);
|
|
93
106
|
const contentSize = layoutPlan.contentSize;
|
|
107
|
+
const viewContentSize = useMemo(
|
|
108
|
+
() => toViewSize(contentSize, scale),
|
|
109
|
+
[contentSize, scale]
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
// Deliver geometry once per recording pass. The callback lives in a ref so
|
|
113
|
+
// a new inline callback identity neither re-fires the effect nor loops when
|
|
114
|
+
// the callback sets parent state.
|
|
115
|
+
const onItemsLayoutRef = useRef(onItemsLayout);
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
onItemsLayoutRef.current = onItemsLayout;
|
|
118
|
+
});
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (!hasViewportSize) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
onItemsLayoutRef.current?.(itemsLayout);
|
|
125
|
+
}, [itemsLayout, hasViewportSize]);
|
|
94
126
|
const scrollState = useScoreScroll({
|
|
95
|
-
contentSize,
|
|
127
|
+
contentSize: viewContentSize,
|
|
96
128
|
rendererType: effectiveRendererType,
|
|
97
129
|
scrollEnabled,
|
|
98
130
|
viewportSize,
|
|
99
131
|
});
|
|
100
132
|
const hasScrollableOverflow =
|
|
101
|
-
getMaxScroll(effectiveRendererType, viewportSize,
|
|
133
|
+
getMaxScroll(effectiveRendererType, viewportSize, viewContentSize) > 0;
|
|
102
134
|
|
|
103
135
|
const viewportClip = useMemo(
|
|
104
136
|
() => Skia.XYWHRect(0, 0, viewportSize.width, viewportSize.height),
|
|
@@ -106,7 +138,7 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
|
106
138
|
);
|
|
107
139
|
|
|
108
140
|
const picture = useMemo(() => {
|
|
109
|
-
if (!hasViewportSize) {
|
|
141
|
+
if (!hasViewportSize || itemStyleOverrides) {
|
|
110
142
|
return undefined;
|
|
111
143
|
}
|
|
112
144
|
|
|
@@ -121,20 +153,26 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
|
121
153
|
defaultFont,
|
|
122
154
|
fontManager,
|
|
123
155
|
hasViewportSize,
|
|
156
|
+
itemStyleOverrides,
|
|
124
157
|
recordedCommands,
|
|
125
158
|
]);
|
|
126
159
|
|
|
160
|
+
// Destructure so the worklet below doesn't capture the whole scrollState —
|
|
161
|
+
// the PanGesture inside it is not serializable and would crash the worklet.
|
|
162
|
+
const { scrollOffset } = scrollState;
|
|
127
163
|
const pictureTransform = useDerivedValue(() => {
|
|
128
164
|
return createPictureTransform(
|
|
129
|
-
|
|
165
|
+
scrollOffset.value,
|
|
130
166
|
effectiveRendererType,
|
|
131
167
|
{ width: viewportSize.width, height: viewportSize.height },
|
|
132
|
-
|
|
168
|
+
viewContentSize,
|
|
169
|
+
scale
|
|
133
170
|
);
|
|
134
171
|
}, [
|
|
135
|
-
contentSize,
|
|
136
172
|
effectiveRendererType,
|
|
137
|
-
|
|
173
|
+
scale,
|
|
174
|
+
scrollOffset,
|
|
175
|
+
viewContentSize,
|
|
138
176
|
viewportSize.height,
|
|
139
177
|
viewportSize.width,
|
|
140
178
|
]);
|
|
@@ -149,7 +187,17 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
|
149
187
|
<Canvas style={canvasStyle} ref={canvasRef}>
|
|
150
188
|
<Group clip={viewportClip}>
|
|
151
189
|
<Group transform={pictureTransform}>
|
|
152
|
-
{
|
|
190
|
+
{itemStyleOverrides && hasViewportSize ? (
|
|
191
|
+
<AnimatedScorePicture
|
|
192
|
+
contentSize={contentSize}
|
|
193
|
+
defaultFont={defaultFont}
|
|
194
|
+
fontManager={fontManager}
|
|
195
|
+
itemStyleOverrides={itemStyleOverrides}
|
|
196
|
+
recordedCommands={recordedCommands}
|
|
197
|
+
/>
|
|
198
|
+
) : picture ? (
|
|
199
|
+
<Picture picture={picture} />
|
|
200
|
+
) : null}
|
|
153
201
|
</Group>
|
|
154
202
|
</Group>
|
|
155
203
|
</Canvas>
|
|
@@ -176,31 +224,60 @@ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
|
|
|
176
224
|
|
|
177
225
|
export default memo(ScoreRenderer);
|
|
178
226
|
|
|
227
|
+
function AnimatedScorePicture({
|
|
228
|
+
contentSize,
|
|
229
|
+
defaultFont,
|
|
230
|
+
fontManager,
|
|
231
|
+
itemStyleOverrides,
|
|
232
|
+
recordedCommands,
|
|
233
|
+
}: {
|
|
234
|
+
contentSize: RendererSize;
|
|
235
|
+
defaultFont: string;
|
|
236
|
+
fontManager: SkTypefaceFontProvider;
|
|
237
|
+
itemStyleOverrides: NonNullable<ScoreRendererProps['itemStyleOverrides']>;
|
|
238
|
+
recordedCommands: readonly VexflowRecordingCommand[];
|
|
239
|
+
}) {
|
|
240
|
+
const picture = useDerivedValue<SkPicture>(() => {
|
|
241
|
+
return createScorePictureWorklet({
|
|
242
|
+
contentSize,
|
|
243
|
+
defaultFont,
|
|
244
|
+
fontManager,
|
|
245
|
+
recordedCommands,
|
|
246
|
+
styleOverrides: itemStyleOverrides.value,
|
|
247
|
+
});
|
|
248
|
+
}, [
|
|
249
|
+
contentSize,
|
|
250
|
+
defaultFont,
|
|
251
|
+
fontManager,
|
|
252
|
+
itemStyleOverrides,
|
|
253
|
+
recordedCommands,
|
|
254
|
+
]);
|
|
255
|
+
|
|
256
|
+
return <Picture picture={picture} />;
|
|
257
|
+
}
|
|
258
|
+
|
|
179
259
|
export function createScorePicture({
|
|
180
260
|
contentSize,
|
|
181
261
|
defaultFont,
|
|
182
262
|
fontManager,
|
|
183
263
|
recordedCommands,
|
|
264
|
+
styleOverrides,
|
|
184
265
|
}: {
|
|
185
266
|
contentSize: RendererSize;
|
|
186
267
|
defaultFont: string;
|
|
187
268
|
fontManager: SkTypefaceFontProvider;
|
|
188
269
|
recordedCommands: readonly VexflowRecordingCommand[];
|
|
270
|
+
styleOverrides?: ScoreItemStyleOverrides;
|
|
189
271
|
}) {
|
|
190
272
|
try {
|
|
191
273
|
const start = nowMs();
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
renderVexflowRecordingCommands(
|
|
198
|
-
canvas,
|
|
199
|
-
recordedCommands,
|
|
274
|
+
const picture = createScorePictureWorklet({
|
|
275
|
+
contentSize,
|
|
276
|
+
defaultFont,
|
|
200
277
|
fontManager,
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
278
|
+
recordedCommands,
|
|
279
|
+
styleOverrides,
|
|
280
|
+
});
|
|
204
281
|
|
|
205
282
|
logScorePictureProfile({
|
|
206
283
|
commandCount: recordedCommands.length,
|
|
@@ -215,11 +292,49 @@ export function createScorePicture({
|
|
|
215
292
|
}
|
|
216
293
|
}
|
|
217
294
|
|
|
295
|
+
function createScorePictureWorklet({
|
|
296
|
+
contentSize,
|
|
297
|
+
defaultFont,
|
|
298
|
+
fontManager,
|
|
299
|
+
recordedCommands,
|
|
300
|
+
styleOverrides,
|
|
301
|
+
}: {
|
|
302
|
+
contentSize: RendererSize;
|
|
303
|
+
defaultFont: string;
|
|
304
|
+
fontManager: SkTypefaceFontProvider;
|
|
305
|
+
recordedCommands: readonly VexflowRecordingCommand[];
|
|
306
|
+
styleOverrides?: ScoreItemStyleOverrides;
|
|
307
|
+
}): SkPicture {
|
|
308
|
+
'worklet';
|
|
309
|
+
|
|
310
|
+
const recorder = Skia.PictureRecorder();
|
|
311
|
+
const canvas = recorder.beginRecording(
|
|
312
|
+
Skia.XYWHRect(0, 0, contentSize.width, contentSize.height)
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
renderVexflowRecordingCommands(
|
|
316
|
+
canvas,
|
|
317
|
+
recordedCommands,
|
|
318
|
+
fontManager,
|
|
319
|
+
defaultFont,
|
|
320
|
+
styleOverrides
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
return recorder.finishRecordingAsPicture();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Maps the content-space picture into the view. Skia applies the transform
|
|
328
|
+
* array right-to-left, so the trailing scale entry runs first and the
|
|
329
|
+
* translate entries then shift by the view-space scroll offset; `contentSize`
|
|
330
|
+
* must be the view-space content size.
|
|
331
|
+
*/
|
|
218
332
|
export function createPictureTransform(
|
|
219
333
|
scrollOffset: number,
|
|
220
334
|
rendererType: RendererType,
|
|
221
335
|
viewport: Viewport,
|
|
222
|
-
contentSize: RendererSize
|
|
336
|
+
contentSize: RendererSize,
|
|
337
|
+
scale: number = 1
|
|
223
338
|
): Transforms3d {
|
|
224
339
|
'worklet';
|
|
225
340
|
|
|
@@ -230,7 +345,7 @@ export function createPictureTransform(
|
|
|
230
345
|
contentSize
|
|
231
346
|
);
|
|
232
347
|
|
|
233
|
-
|
|
348
|
+
const transform: Transforms3d = [
|
|
234
349
|
{
|
|
235
350
|
translateX: visibleViewport.x === 0 ? 0 : -visibleViewport.x,
|
|
236
351
|
},
|
|
@@ -238,9 +353,15 @@ export function createPictureTransform(
|
|
|
238
353
|
translateY: visibleViewport.y === 0 ? 0 : -visibleViewport.y,
|
|
239
354
|
},
|
|
240
355
|
];
|
|
356
|
+
|
|
357
|
+
if (scale !== 1) {
|
|
358
|
+
transform.push({ scale });
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return transform;
|
|
241
362
|
}
|
|
242
363
|
|
|
243
|
-
function withDefaultOptions(options:
|
|
364
|
+
function withDefaultOptions(options: ScoreRendererOptions): ScoreOptions {
|
|
244
365
|
return {
|
|
245
366
|
insets: { ...insets, ...(options.insets || {}) },
|
|
246
367
|
spacing: { ...spacing, ...(options.spacing || {}) },
|
package/src/renderer/measure.tsx
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
Articulation as VFArticulation,
|
|
3
|
+
Formatter,
|
|
4
|
+
Stave,
|
|
5
|
+
Voice as VFVoice,
|
|
6
|
+
} from 'vexflow';
|
|
7
|
+
import type { BoundingBox, RenderContext } from 'vexflow';
|
|
3
8
|
import { Platform } from 'react-native';
|
|
4
9
|
|
|
5
10
|
import {
|
|
@@ -10,6 +15,7 @@ import type { Score, Staff } from '../state';
|
|
|
10
15
|
import {
|
|
11
16
|
buildResolvedMeasureStates,
|
|
12
17
|
buildMeasurementGroups,
|
|
18
|
+
indexAttachmentsByOwner,
|
|
13
19
|
makeVFVoice,
|
|
14
20
|
resolveGroupStaves,
|
|
15
21
|
} from './scoreParsing';
|
|
@@ -47,6 +53,7 @@ export function measureScore(
|
|
|
47
53
|
options: ScoreOptions
|
|
48
54
|
): MeasuredScore {
|
|
49
55
|
const groups = buildMeasurementGroups(score);
|
|
56
|
+
const attachmentsByOwner = indexAttachmentsByOwner(score);
|
|
50
57
|
const measures: MeasuredMeasure[] = [];
|
|
51
58
|
const {
|
|
52
59
|
spacing: { minIntrinsicSizeMultiplier },
|
|
@@ -88,6 +95,7 @@ export function measureScore(
|
|
|
88
95
|
|
|
89
96
|
const voiceArtifacts = measure.voices.map((voice) =>
|
|
90
97
|
makeVFVoice(score, resolvedState.meter, resolvedState.clef, voice, {
|
|
98
|
+
attachmentsByOwner,
|
|
91
99
|
resolveClef: (item) =>
|
|
92
100
|
item.targetStaffId
|
|
93
101
|
? resolvedStateByStaffId.get(item.targetStaffId)?.clef ??
|
|
@@ -110,16 +118,20 @@ export function measureScore(
|
|
|
110
118
|
resolvedState,
|
|
111
119
|
showClef:
|
|
112
120
|
measureIndex === 0 || Boolean(measure.leftModifiers?.showClef),
|
|
121
|
+
showMeter: measure.leftModifiers?.showMeter === true,
|
|
113
122
|
voiceArtifacts,
|
|
114
123
|
};
|
|
115
124
|
});
|
|
116
125
|
|
|
117
126
|
try {
|
|
127
|
+
// Clef and time signature sit before the note area, so the intrinsic
|
|
128
|
+
// width must include them or an empty measure with a time signature
|
|
129
|
+
// measures ~0 wide and the signature clips.
|
|
118
130
|
const intrinsicNoteWidth =
|
|
119
|
-
allVoices.length > 0
|
|
131
|
+
(allVoices.length > 0
|
|
120
132
|
? formatter.preCalculateMinTotalWidth(allVoices) *
|
|
121
133
|
Math.max(1, minIntrinsicSizeMultiplier)
|
|
122
|
-
: 0;
|
|
134
|
+
: 0) + measureLeftModifierWidth(staffMeasurementContexts);
|
|
123
135
|
const staffBounds = measureStaffVerticalBounds({
|
|
124
136
|
allVoices,
|
|
125
137
|
intrinsicNoteWidth,
|
|
@@ -154,6 +166,48 @@ export function measureScore(
|
|
|
154
166
|
};
|
|
155
167
|
}
|
|
156
168
|
|
|
169
|
+
// Wide enough that VexFlow never clamps the modifier block on the probe stave.
|
|
170
|
+
const MODIFIER_PROBE_STAVE_WIDTH = 500;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Widest clef/time-signature block of the measure, measured as the note-start
|
|
174
|
+
* delta between a bare probe stave and one carrying the shown modifiers.
|
|
175
|
+
*/
|
|
176
|
+
function measureLeftModifierWidth(
|
|
177
|
+
staffMeasurementContexts: Array<{
|
|
178
|
+
resolvedState: { clef: string; meter: { beats: number; beatUnit: number } };
|
|
179
|
+
showClef: boolean;
|
|
180
|
+
showMeter: boolean;
|
|
181
|
+
}>
|
|
182
|
+
): number {
|
|
183
|
+
return staffMeasurementContexts.reduce(
|
|
184
|
+
(maxWidth, { resolvedState, showClef, showMeter }) => {
|
|
185
|
+
if (!showClef && !showMeter) {
|
|
186
|
+
return maxWidth;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const bareStave = new Stave(0, 0, MODIFIER_PROBE_STAVE_WIDTH);
|
|
190
|
+
const modifiedStave = new Stave(0, 0, MODIFIER_PROBE_STAVE_WIDTH);
|
|
191
|
+
|
|
192
|
+
if (showClef) {
|
|
193
|
+
modifiedStave.addClef(resolvedState.clef);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (showMeter) {
|
|
197
|
+
modifiedStave.addTimeSignature(
|
|
198
|
+
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return Math.max(
|
|
203
|
+
maxWidth,
|
|
204
|
+
modifiedStave.getNoteStartX() - bareStave.getNoteStartX()
|
|
205
|
+
);
|
|
206
|
+
},
|
|
207
|
+
0
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
157
211
|
function measureStaffVerticalBounds({
|
|
158
212
|
allVoices,
|
|
159
213
|
intrinsicNoteWidth,
|
|
@@ -169,18 +223,25 @@ function measureStaffVerticalBounds({
|
|
|
169
223
|
measure: Staff['measures'][number];
|
|
170
224
|
resolvedState: ReturnType<typeof buildResolvedMeasureStates>[number];
|
|
171
225
|
showClef: boolean;
|
|
226
|
+
showMeter: boolean;
|
|
172
227
|
voiceArtifacts: ReturnType<typeof makeVFVoice>[];
|
|
173
228
|
}>;
|
|
174
229
|
}): StaffVerticalBounds[] {
|
|
175
230
|
const width = Math.max(intrinsicNoteWidth, 1);
|
|
176
231
|
const renderedStaves = staffMeasurementContexts.map(
|
|
177
|
-
({ resolvedState, showClef }) => {
|
|
232
|
+
({ resolvedState, showClef, showMeter }) => {
|
|
178
233
|
const stave = new Stave(0, 0, width);
|
|
179
234
|
|
|
180
235
|
if (showClef) {
|
|
181
236
|
stave.addClef(resolvedState.clef);
|
|
182
237
|
}
|
|
183
238
|
|
|
239
|
+
if (showMeter) {
|
|
240
|
+
stave.addTimeSignature(
|
|
241
|
+
`${resolvedState.meter.beats}/${resolvedState.meter.beatUnit}`
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
184
245
|
return stave;
|
|
185
246
|
}
|
|
186
247
|
);
|
|
@@ -245,6 +306,7 @@ function measureStaffVerticalBounds({
|
|
|
245
306
|
staffIndexById.get(
|
|
246
307
|
items[noteIndex]?.targetStaffId ?? ownerStaffId
|
|
247
308
|
) ?? staffIndex;
|
|
309
|
+
mergeArticulationBounds(bounds[ownerStaffIndex], note);
|
|
248
310
|
mergeNoteBounds(bounds[ownerStaffIndex], note);
|
|
249
311
|
});
|
|
250
312
|
|
|
@@ -274,6 +336,56 @@ function measureStaffVerticalBounds({
|
|
|
274
336
|
return bounds;
|
|
275
337
|
}
|
|
276
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 into the staff bounds.
|
|
362
|
+
* VexFlow only positions an articulation inside `draw()`, so each one is
|
|
363
|
+
* drawn against a no-op context first to make its bounding box real.
|
|
364
|
+
*/
|
|
365
|
+
function mergeArticulationBounds(
|
|
366
|
+
bounds: StaffVerticalBounds | undefined,
|
|
367
|
+
note: VFVoiceNote
|
|
368
|
+
) {
|
|
369
|
+
if (!bounds) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
for (const modifier of note.getModifiers()) {
|
|
374
|
+
if (!(modifier instanceof VFArticulation)) {
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
try {
|
|
379
|
+
modifier.setContext(NOOP_RENDER_CONTEXT);
|
|
380
|
+
modifier.draw();
|
|
381
|
+
mergeBoundingBox(bounds, modifier.getBoundingBox());
|
|
382
|
+
} catch {
|
|
383
|
+
// Ghost and spacer notes cannot place modifiers; note bounds remain
|
|
384
|
+
// the fallback.
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
277
389
|
function mergeNoteBounds(
|
|
278
390
|
bounds: StaffVerticalBounds | undefined,
|
|
279
391
|
note: VFVoiceNote
|