vexflow-native 2.0.3 → 2.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 +1 -1
- package/lib/module/renderer/Layout/DocumentLayout.js +47 -36
- package/lib/module/renderer/Layout/DocumentLayout.js.map +1 -1
- package/lib/module/renderer/Layout/LayoutMetrics.js +7 -1
- package/lib/module/renderer/Layout/LayoutMetrics.js.map +1 -1
- package/lib/module/renderer/Layout/VerticalSpacing.js +31 -2
- package/lib/module/renderer/Layout/VerticalSpacing.js.map +1 -1
- package/lib/module/renderer/constants.js +5 -1
- package/lib/module/renderer/constants.js.map +1 -1
- package/lib/module/renderer/engraveScore.js +202 -0
- package/lib/module/renderer/engraveScore.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/layout.js +4 -1
- package/lib/module/renderer/layout.js.map +1 -1
- package/lib/module/renderer/measure.js +12 -3
- package/lib/module/renderer/measure.js.map +1 -1
- package/lib/module/renderer/measureModifiers.js +5 -4
- package/lib/module/renderer/measureModifiers.js.map +1 -1
- package/lib/module/renderer/render.js +2 -1
- package/lib/module/renderer/render.js.map +1 -1
- package/lib/module/renderer/systemExtents.js +104 -0
- package/lib/module/renderer/systemExtents.js.map +1 -0
- package/lib/module/renderer/useScoreRecording.js +19 -109
- package/lib/module/renderer/useScoreRecording.js.map +1 -1
- package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts.map +1 -1
- package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts +2 -2
- package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts.map +1 -1
- package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts +7 -0
- package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts.map +1 -1
- package/lib/typescript/src/renderer/constants.d.ts.map +1 -1
- package/lib/typescript/src/renderer/engraveScore.d.ts +101 -0
- package/lib/typescript/src/renderer/engraveScore.d.ts.map +1 -0
- package/lib/typescript/src/renderer/index.d.ts +1 -0
- package/lib/typescript/src/renderer/index.d.ts.map +1 -1
- package/lib/typescript/src/renderer/layout.d.ts +1 -0
- package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measure.d.ts +5 -0
- package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
- package/lib/typescript/src/renderer/measureModifiers.d.ts +4 -3
- package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -1
- package/lib/typescript/src/renderer/render.d.ts.map +1 -1
- package/lib/typescript/src/renderer/systemExtents.d.ts +15 -0
- package/lib/typescript/src/renderer/systemExtents.d.ts.map +1 -0
- package/lib/typescript/src/renderer/types.d.ts +32 -0
- package/lib/typescript/src/renderer/types.d.ts.map +1 -1
- package/lib/typescript/src/renderer/useScoreRecording.d.ts +3 -20
- package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/renderer/Layout/DocumentLayout.ts +94 -44
- package/src/renderer/Layout/LayoutMetrics.ts +9 -2
- package/src/renderer/Layout/VerticalSpacing.ts +77 -2
- package/src/renderer/constants.ts +4 -0
- package/src/renderer/engraveScore.ts +338 -0
- package/src/renderer/index.ts +6 -0
- package/src/renderer/layout.ts +5 -2
- package/src/renderer/measure.tsx +27 -3
- package/src/renderer/measureModifiers.ts +7 -4
- package/src/renderer/render.tsx +7 -1
- package/src/renderer/systemExtents.ts +134 -0
- package/src/renderer/types.ts +32 -0
- package/src/renderer/useScoreRecording.ts +27 -176
|
@@ -10,6 +10,10 @@ export const insets: ScoreInsets = {
|
|
|
10
10
|
export const spacing: ScoreSpacing = {
|
|
11
11
|
staffGap: 96,
|
|
12
12
|
minIntrinsicSizeMultiplier: 2,
|
|
13
|
+
uniformSystemPitch: false,
|
|
14
|
+
firstSystemIndent: 0,
|
|
15
|
+
lastSystemFill: 'justify',
|
|
16
|
+
clefOnEachSystem: false,
|
|
13
17
|
};
|
|
14
18
|
|
|
15
19
|
export const renderOptions: RenderOptions = {
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import type { SkTypefaceFontProvider } from 'react-native-skia';
|
|
2
|
+
|
|
3
|
+
import type { VexflowRecordingCommand } from '../base';
|
|
4
|
+
import FontManager from '../base/FontManager';
|
|
5
|
+
import VexflowRecordingContext from '../base/VexflowRecordingContext';
|
|
6
|
+
import {
|
|
7
|
+
buildVexflowGroupIndex,
|
|
8
|
+
type VexflowRecordingGroupIndex,
|
|
9
|
+
} from '../base/VexflowRecordingIndex';
|
|
10
|
+
import type { Score } from '../state';
|
|
11
|
+
import { isVexflowNativeDebugEnabled } from '../shared/debug';
|
|
12
|
+
import {
|
|
13
|
+
resolveScoreColorScheme,
|
|
14
|
+
type ResolvedScoreColorScheme,
|
|
15
|
+
type ScoreColorScheme,
|
|
16
|
+
} from './colorScheme';
|
|
17
|
+
import { layoutScore, type ScoreLayoutPlan } from './layout';
|
|
18
|
+
import { measureScore, type MeasuredScore } from './measure';
|
|
19
|
+
import { renderScore } from './render';
|
|
20
|
+
import {
|
|
21
|
+
createContentViewport,
|
|
22
|
+
getRenderScale,
|
|
23
|
+
scaleItemsLayoutToViewSpace,
|
|
24
|
+
} from './scale';
|
|
25
|
+
import { withDefaultOptions } from './ScoreRenderer/utils';
|
|
26
|
+
import { coverDrawnExtents } from './systemExtents';
|
|
27
|
+
import type {
|
|
28
|
+
RendererRect,
|
|
29
|
+
RendererSize,
|
|
30
|
+
RendererType,
|
|
31
|
+
ScoreItemHooks,
|
|
32
|
+
ScoreItemsLayout,
|
|
33
|
+
ScoreOptions,
|
|
34
|
+
ScoreRendererOptions,
|
|
35
|
+
} from './types';
|
|
36
|
+
|
|
37
|
+
export interface EngraveScoreParams extends ScoreItemHooks {
|
|
38
|
+
score: Score;
|
|
39
|
+
defaultFont: string;
|
|
40
|
+
fontManager: SkTypefaceFontProvider;
|
|
41
|
+
colorScheme?: ScoreColorScheme;
|
|
42
|
+
options?: ScoreRendererOptions;
|
|
43
|
+
/** Default `document`. */
|
|
44
|
+
rendererType?: RendererType;
|
|
45
|
+
/** View-space width the score is laid out against. */
|
|
46
|
+
width: number;
|
|
47
|
+
/**
|
|
48
|
+
* View-space height. Only `infiniteScore`, which centers on it, and a
|
|
49
|
+
* score without measures read it. Default 0.
|
|
50
|
+
*/
|
|
51
|
+
height?: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** One system of an engraving, in content space. */
|
|
55
|
+
export interface ScoreSystemLayout {
|
|
56
|
+
groupId: string;
|
|
57
|
+
/** Index within the staff group. */
|
|
58
|
+
systemIndex: number;
|
|
59
|
+
x: number;
|
|
60
|
+
width: number;
|
|
61
|
+
/**
|
|
62
|
+
* `y`/`height` cover the system's layout band and everything drawn for it
|
|
63
|
+
* (clefs, ledger lines, stems, modifiers, text), so clipping the commands
|
|
64
|
+
* to this band cuts nothing off.
|
|
65
|
+
*/
|
|
66
|
+
y: number;
|
|
67
|
+
height: number;
|
|
68
|
+
measureIndices: number[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface ScoreEngraving {
|
|
72
|
+
/**
|
|
73
|
+
* Display list in content space (VexFlow units before `render.scale`);
|
|
74
|
+
* replay it with `renderVexflowRecordingCommands`.
|
|
75
|
+
*/
|
|
76
|
+
commands: readonly VexflowRecordingCommand[];
|
|
77
|
+
/** Content space; multiply by `render.scale` for view space. */
|
|
78
|
+
contentSize: RendererSize;
|
|
79
|
+
/** Top to bottom per staff group. */
|
|
80
|
+
systems: ScoreSystemLayout[];
|
|
81
|
+
/** View space, the value `ScoreRenderer` passes to `onItemsLayout`. */
|
|
82
|
+
itemsLayout: ScoreItemsLayout;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Engraves a score to a display list without mounting a view, for print,
|
|
87
|
+
* export or custom drawing. Same pipeline and output as `ScoreRenderer`.
|
|
88
|
+
*/
|
|
89
|
+
export function engraveScore({
|
|
90
|
+
score,
|
|
91
|
+
defaultFont,
|
|
92
|
+
fontManager,
|
|
93
|
+
colorScheme,
|
|
94
|
+
options: userOptions = {},
|
|
95
|
+
rendererType = 'document',
|
|
96
|
+
width,
|
|
97
|
+
height = 0,
|
|
98
|
+
decorateItem,
|
|
99
|
+
onDrawItem,
|
|
100
|
+
}: EngraveScoreParams): ScoreEngraving {
|
|
101
|
+
const options = withDefaultOptions(userOptions);
|
|
102
|
+
const { commands, layoutPlan, itemsLayout } = recordScore({
|
|
103
|
+
measurement: measureScoreRecording({
|
|
104
|
+
score,
|
|
105
|
+
options,
|
|
106
|
+
fontManager,
|
|
107
|
+
defaultFont,
|
|
108
|
+
decorateItem,
|
|
109
|
+
}),
|
|
110
|
+
score,
|
|
111
|
+
options,
|
|
112
|
+
rendererType,
|
|
113
|
+
viewport: { x: 0, y: 0, width, height },
|
|
114
|
+
fontManager,
|
|
115
|
+
defaultFont,
|
|
116
|
+
colorScheme: resolveScoreColorScheme(colorScheme),
|
|
117
|
+
decorateItem,
|
|
118
|
+
onDrawItem,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
commands,
|
|
123
|
+
contentSize: layoutPlan.contentSize,
|
|
124
|
+
systems: coverDrawnExtents(
|
|
125
|
+
layoutPlan.systems.map(
|
|
126
|
+
(system): ScoreSystemLayout => ({
|
|
127
|
+
groupId: system.groupId,
|
|
128
|
+
systemIndex: system.systemIndex,
|
|
129
|
+
x: system.x,
|
|
130
|
+
width: system.width,
|
|
131
|
+
y: system.y,
|
|
132
|
+
height: system.height,
|
|
133
|
+
measureIndices: system.measureIndices,
|
|
134
|
+
})
|
|
135
|
+
),
|
|
136
|
+
commands,
|
|
137
|
+
new FontManager(fontManager, defaultFont)
|
|
138
|
+
),
|
|
139
|
+
itemsLayout,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface ScoreMeasurement {
|
|
144
|
+
measuredScore: MeasuredScore;
|
|
145
|
+
measureMs: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ScoreRecording {
|
|
149
|
+
/** Recorded draw commands, content-space coordinates. */
|
|
150
|
+
commands: readonly VexflowRecordingCommand[];
|
|
151
|
+
/**
|
|
152
|
+
* `commands` bucketed by `groupId` so a style-override replay can redraw
|
|
153
|
+
* just the overridden items instead of the whole recording.
|
|
154
|
+
*/
|
|
155
|
+
groupIndex: VexflowRecordingGroupIndex;
|
|
156
|
+
/**
|
|
157
|
+
* Layout plan in content space; multiply its `contentSize` by the render
|
|
158
|
+
* scale for the view-space size that drives scrolling.
|
|
159
|
+
*/
|
|
160
|
+
layoutPlan: ScoreLayoutPlan;
|
|
161
|
+
/** Emitted geometry, already converted to view space. */
|
|
162
|
+
itemsLayout: ScoreItemsLayout;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The measuring pass. It does not depend on the viewport and is the most
|
|
167
|
+
* expensive pass, so callers that re-layout cache it apart from
|
|
168
|
+
* {@link recordScore}.
|
|
169
|
+
*/
|
|
170
|
+
export function measureScoreRecording({
|
|
171
|
+
score,
|
|
172
|
+
options,
|
|
173
|
+
fontManager,
|
|
174
|
+
defaultFont,
|
|
175
|
+
decorateItem,
|
|
176
|
+
}: Pick<ScoreItemHooks, 'decorateItem'> & {
|
|
177
|
+
score: Score;
|
|
178
|
+
options: ScoreOptions;
|
|
179
|
+
fontManager: SkTypefaceFontProvider;
|
|
180
|
+
defaultFont: string;
|
|
181
|
+
}): ScoreMeasurement {
|
|
182
|
+
const measureStart = nowMs();
|
|
183
|
+
// On native, constructing a context installs it as VexFlow's text
|
|
184
|
+
// measurement canvas, so text is measured with this font setup.
|
|
185
|
+
// eslint-disable-next-line no-new
|
|
186
|
+
new VexflowRecordingContext(fontManager, defaultFont);
|
|
187
|
+
const measuredScore = measureScore(score, options, { decorateItem });
|
|
188
|
+
|
|
189
|
+
return { measuredScore, measureMs: nowMs() - measureStart };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Layout and render passes; `viewport` is view space. */
|
|
193
|
+
export function recordScore({
|
|
194
|
+
measurement,
|
|
195
|
+
score,
|
|
196
|
+
options,
|
|
197
|
+
rendererType,
|
|
198
|
+
viewport,
|
|
199
|
+
fontManager,
|
|
200
|
+
defaultFont,
|
|
201
|
+
colorScheme,
|
|
202
|
+
decorateItem,
|
|
203
|
+
onDrawItem,
|
|
204
|
+
}: ScoreItemHooks & {
|
|
205
|
+
measurement: ScoreMeasurement;
|
|
206
|
+
score: Score;
|
|
207
|
+
options: ScoreOptions;
|
|
208
|
+
rendererType: RendererType;
|
|
209
|
+
viewport: RendererRect;
|
|
210
|
+
fontManager: SkTypefaceFontProvider;
|
|
211
|
+
defaultFont: string;
|
|
212
|
+
colorScheme: ResolvedScoreColorScheme;
|
|
213
|
+
}): ScoreRecording {
|
|
214
|
+
// Layout and render run in content space against the virtual viewport;
|
|
215
|
+
// see src/renderer/scale.ts.
|
|
216
|
+
const scale = getRenderScale(options);
|
|
217
|
+
const contentViewport = createContentViewport(viewport, scale);
|
|
218
|
+
const ctx = new VexflowRecordingContext(
|
|
219
|
+
fontManager,
|
|
220
|
+
defaultFont,
|
|
221
|
+
colorScheme
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const layoutStart = nowMs();
|
|
225
|
+
const layoutPlan = layoutScore(
|
|
226
|
+
score,
|
|
227
|
+
measurement.measuredScore,
|
|
228
|
+
options,
|
|
229
|
+
rendererType,
|
|
230
|
+
contentViewport
|
|
231
|
+
);
|
|
232
|
+
const layoutMs = nowMs() - layoutStart;
|
|
233
|
+
|
|
234
|
+
const renderStart = nowMs();
|
|
235
|
+
const itemsLayout = scaleItemsLayoutToViewSpace(
|
|
236
|
+
renderScore(ctx, score, layoutPlan, options, {
|
|
237
|
+
decorateItem,
|
|
238
|
+
onDrawItem,
|
|
239
|
+
}),
|
|
240
|
+
scale
|
|
241
|
+
);
|
|
242
|
+
const renderMs = nowMs() - renderStart;
|
|
243
|
+
|
|
244
|
+
const finishStart = nowMs();
|
|
245
|
+
const commands = ctx.finish();
|
|
246
|
+
const groupIndex = buildVexflowGroupIndex(commands);
|
|
247
|
+
const finishMs = nowMs() - finishStart;
|
|
248
|
+
|
|
249
|
+
logScoreRecordingProfile({
|
|
250
|
+
commandCount: commands.length,
|
|
251
|
+
groupCount: Object.keys(groupIndex).length,
|
|
252
|
+
contentSize: layoutPlan.contentSize,
|
|
253
|
+
finishMs,
|
|
254
|
+
layoutMs,
|
|
255
|
+
measureCount: layoutPlan.measures.length,
|
|
256
|
+
measurement,
|
|
257
|
+
renderMs,
|
|
258
|
+
rendererType,
|
|
259
|
+
scoreId: score.id,
|
|
260
|
+
systemCount: layoutPlan.systems.length,
|
|
261
|
+
viewport,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
commands,
|
|
266
|
+
groupIndex,
|
|
267
|
+
layoutPlan,
|
|
268
|
+
itemsLayout,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Measurements already counted by a profile line. A cached measurement is
|
|
274
|
+
* reported once, by the first recording built on it, so summed profiles
|
|
275
|
+
* count each measure pass once.
|
|
276
|
+
*/
|
|
277
|
+
const profiledMeasurements = new WeakSet<ScoreMeasurement>();
|
|
278
|
+
|
|
279
|
+
function logScoreRecordingProfile({
|
|
280
|
+
commandCount,
|
|
281
|
+
groupCount,
|
|
282
|
+
contentSize,
|
|
283
|
+
finishMs,
|
|
284
|
+
layoutMs,
|
|
285
|
+
measureCount,
|
|
286
|
+
measurement,
|
|
287
|
+
renderMs,
|
|
288
|
+
rendererType,
|
|
289
|
+
scoreId,
|
|
290
|
+
systemCount,
|
|
291
|
+
viewport,
|
|
292
|
+
}: {
|
|
293
|
+
commandCount: number;
|
|
294
|
+
groupCount: number;
|
|
295
|
+
contentSize: { height: number; width: number };
|
|
296
|
+
finishMs: number;
|
|
297
|
+
layoutMs: number;
|
|
298
|
+
measureCount: number;
|
|
299
|
+
measurement: ScoreMeasurement;
|
|
300
|
+
renderMs: number;
|
|
301
|
+
rendererType: RendererType;
|
|
302
|
+
scoreId: string;
|
|
303
|
+
systemCount: number;
|
|
304
|
+
viewport: RendererRect;
|
|
305
|
+
}) {
|
|
306
|
+
if (!isVexflowNativeDebugEnabled()) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const measureMs = profiledMeasurements.has(measurement)
|
|
311
|
+
? 0
|
|
312
|
+
: measurement.measureMs;
|
|
313
|
+
profiledMeasurements.add(measurement);
|
|
314
|
+
|
|
315
|
+
console.info('[ScoreRenderer] recording profile', {
|
|
316
|
+
scoreId,
|
|
317
|
+
rendererType,
|
|
318
|
+
viewport,
|
|
319
|
+
contentSize,
|
|
320
|
+
measureCount,
|
|
321
|
+
systemCount,
|
|
322
|
+
commandCount,
|
|
323
|
+
groupCount,
|
|
324
|
+
measureMs: roundMs(measureMs),
|
|
325
|
+
layoutMs: roundMs(layoutMs),
|
|
326
|
+
renderMs: roundMs(renderMs),
|
|
327
|
+
finishMs: roundMs(finishMs),
|
|
328
|
+
totalMs: roundMs(measureMs + layoutMs + renderMs + finishMs),
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function nowMs(): number {
|
|
333
|
+
return globalThis.performance?.now?.() ?? Date.now();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function roundMs(value: number): number {
|
|
337
|
+
return Math.round(value * 10) / 10;
|
|
338
|
+
}
|
package/src/renderer/index.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export { default as ScoreRenderer } from './ScoreRenderer';
|
|
2
|
+
export {
|
|
3
|
+
engraveScore,
|
|
4
|
+
type EngraveScoreParams,
|
|
5
|
+
type ScoreEngraving,
|
|
6
|
+
type ScoreSystemLayout,
|
|
7
|
+
} from './engraveScore';
|
|
2
8
|
export * from './colorScheme';
|
|
3
9
|
export * from './types';
|
|
4
10
|
export {
|
package/src/renderer/layout.ts
CHANGED
|
@@ -29,6 +29,7 @@ export interface MeasuredGroupMeasure {
|
|
|
29
29
|
groupId: string;
|
|
30
30
|
measureIndex: number;
|
|
31
31
|
intrinsicWidth: number;
|
|
32
|
+
systemStartClefWidth?: number;
|
|
32
33
|
measureNumbers: number[];
|
|
33
34
|
staffBounds: StaffVerticalBounds[];
|
|
34
35
|
}
|
|
@@ -180,6 +181,9 @@ function buildGroupLayoutContext(
|
|
|
180
181
|
groupId: measure.groupId,
|
|
181
182
|
measureIndex: measure.measureIndex,
|
|
182
183
|
intrinsicWidth: measure.intrinsicNoteWidth,
|
|
184
|
+
...(measure.systemStartClefWidth
|
|
185
|
+
? { systemStartClefWidth: measure.systemStartClefWidth }
|
|
186
|
+
: {}),
|
|
183
187
|
measureNumbers: measure.measureNumbers,
|
|
184
188
|
staffBounds: measure.staffBounds,
|
|
185
189
|
})),
|
|
@@ -243,8 +247,7 @@ function layoutInfiniteScoreGroup(
|
|
|
243
247
|
groupId: group.groupId,
|
|
244
248
|
systems,
|
|
245
249
|
measures,
|
|
246
|
-
nextY:
|
|
247
|
-
origin.y + verticalLayout.height + getSystemGap(options.spacing.staffGap),
|
|
250
|
+
nextY: origin.y + verticalLayout.height + getSystemGap(options.spacing),
|
|
248
251
|
};
|
|
249
252
|
}
|
|
250
253
|
|
package/src/renderer/measure.tsx
CHANGED
|
@@ -35,6 +35,11 @@ export interface MeasuredMeasure {
|
|
|
35
35
|
staffIds: string[];
|
|
36
36
|
staffBounds: StaffVerticalBounds[];
|
|
37
37
|
intrinsicNoteWidth: number;
|
|
38
|
+
/**
|
|
39
|
+
* Extra width the measure needs when it starts a system and
|
|
40
|
+
* `spacing.clefOnEachSystem` adds its clef; absent = 0.
|
|
41
|
+
*/
|
|
42
|
+
systemStartClefWidth?: number;
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
export interface MeasuredScore {
|
|
@@ -64,7 +69,7 @@ export function measureScore(
|
|
|
64
69
|
const attachmentsByOwner = indexAttachmentsByOwner(score);
|
|
65
70
|
const measures: MeasuredMeasure[] = [];
|
|
66
71
|
const {
|
|
67
|
-
spacing: { minIntrinsicSizeMultiplier },
|
|
72
|
+
spacing: { minIntrinsicSizeMultiplier, clefOnEachSystem },
|
|
68
73
|
} = options;
|
|
69
74
|
|
|
70
75
|
if (Platform.OS === 'ios' || Platform.OS === 'android') {
|
|
@@ -139,11 +144,26 @@ export function measureScore(
|
|
|
139
144
|
// Clef and time signature sit before the note area, so the intrinsic
|
|
140
145
|
// width must include them or an empty measure with a time signature
|
|
141
146
|
// measures ~0 wide and the signature clips.
|
|
147
|
+
const leftModifierWidth = measureLeftModifierWidth(
|
|
148
|
+
staffMeasurementContexts
|
|
149
|
+
);
|
|
142
150
|
const intrinsicNoteWidth =
|
|
143
151
|
(allVoices.length > 0
|
|
144
152
|
? formatter.preCalculateMinTotalWidth(allVoices) *
|
|
145
153
|
Math.max(1, minIntrinsicSizeMultiplier)
|
|
146
|
-
: 0) +
|
|
154
|
+
: 0) + leftModifierWidth;
|
|
155
|
+
const systemStartClefWidth =
|
|
156
|
+
clefOnEachSystem &&
|
|
157
|
+
measureLeftModifierWidth(
|
|
158
|
+
staffMeasurementContexts.map((context) => ({
|
|
159
|
+
...context,
|
|
160
|
+
modifiers: resolveMeasureModifiers(
|
|
161
|
+
context.measure,
|
|
162
|
+
measureIndex,
|
|
163
|
+
true
|
|
164
|
+
),
|
|
165
|
+
}))
|
|
166
|
+
) - leftModifierWidth;
|
|
147
167
|
const staffBounds = measureStaffVerticalBounds({
|
|
148
168
|
allVoices,
|
|
149
169
|
intrinsicNoteWidth,
|
|
@@ -158,6 +178,7 @@ export function measureScore(
|
|
|
158
178
|
staffIds: group.staffIds,
|
|
159
179
|
staffBounds,
|
|
160
180
|
intrinsicNoteWidth,
|
|
181
|
+
...(systemStartClefWidth ? { systemStartClefWidth } : {}),
|
|
161
182
|
});
|
|
162
183
|
} catch (error) {
|
|
163
184
|
throw new Error(
|
|
@@ -186,7 +207,10 @@ const MODIFIER_PROBE_STAVE_WIDTH = 500;
|
|
|
186
207
|
* between a bare probe stave and one carrying the resolved modifiers.
|
|
187
208
|
*/
|
|
188
209
|
function measureLeftModifierWidth(
|
|
189
|
-
staffMeasurementContexts:
|
|
210
|
+
staffMeasurementContexts: Pick<
|
|
211
|
+
StaffMeasurementContext,
|
|
212
|
+
'resolvedState' | 'modifiers'
|
|
213
|
+
>[]
|
|
190
214
|
): number {
|
|
191
215
|
return staffMeasurementContexts.reduce(
|
|
192
216
|
(maxWidth, { resolvedState, modifiers }) => {
|
|
@@ -67,17 +67,20 @@ const END_BARLINE_TYPES: Record<Barline, BarlineType> = {
|
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Resolves the tri-state measure modifiers: `true` shows, `false` hides,
|
|
70
|
-
* `undefined` falls back to the default (clef on the first measure
|
|
71
|
-
* meter and key signature hidden,
|
|
70
|
+
* `undefined` falls back to the default (clef on the first measure, and on
|
|
71
|
+
* a measure that `startsSystemWithClef`; meter and key signature hidden,
|
|
72
|
+
* single barlines).
|
|
72
73
|
*/
|
|
73
74
|
export function resolveMeasureModifiers(
|
|
74
75
|
measure: Pick<Measure, 'leftModifiers' | 'rightModifiers'>,
|
|
75
|
-
measureIndex: number
|
|
76
|
+
measureIndex: number,
|
|
77
|
+
startsSystemWithClef = false
|
|
76
78
|
): ResolvedMeasureModifiers {
|
|
77
79
|
const { leftModifiers, rightModifiers } = measure;
|
|
78
80
|
|
|
79
81
|
return {
|
|
80
|
-
showClef:
|
|
82
|
+
showClef:
|
|
83
|
+
leftModifiers?.showClef ?? (measureIndex === 0 || startsSystemWithClef),
|
|
81
84
|
showMeter: leftModifiers?.showMeter ?? false,
|
|
82
85
|
showKeySignature: leftModifiers?.showKeySignature ?? false,
|
|
83
86
|
startBarline: leftModifiers?.startBarline ?? 'single',
|
package/src/renderer/render.tsx
CHANGED
|
@@ -72,6 +72,7 @@ export function renderScore(
|
|
|
72
72
|
isFirstMeasureInSystem: measureIndex === 0,
|
|
73
73
|
isLastMeasureInSystem: measureIndex === measurePlans.length - 1,
|
|
74
74
|
fixedNoteSpacing: options.render.fixedNoteSpacing,
|
|
75
|
+
clefOnEachSystem: options.spacing.clefOnEachSystem,
|
|
75
76
|
...hooks,
|
|
76
77
|
},
|
|
77
78
|
itemsLayout
|
|
@@ -101,6 +102,7 @@ interface RenderMeasureOptions extends ScoreItemHooks {
|
|
|
101
102
|
isFirstMeasureInSystem: boolean;
|
|
102
103
|
isLastMeasureInSystem: boolean;
|
|
103
104
|
fixedNoteSpacing: boolean;
|
|
105
|
+
clefOnEachSystem: boolean;
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
function renderMeasure(
|
|
@@ -187,7 +189,11 @@ function renderMeasure(
|
|
|
187
189
|
|
|
188
190
|
applyMeasureModifiers(
|
|
189
191
|
stave,
|
|
190
|
-
resolveMeasureModifiers(
|
|
192
|
+
resolveMeasureModifiers(
|
|
193
|
+
measure,
|
|
194
|
+
measurePlan.measureIndex,
|
|
195
|
+
options.clefOnEachSystem && options.isFirstMeasureInSystem
|
|
196
|
+
),
|
|
191
197
|
resolvedState
|
|
192
198
|
);
|
|
193
199
|
stave.setContext(ctx).draw();
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { SkFont } from 'react-native-skia';
|
|
2
|
+
|
|
3
|
+
import type { VexflowRecordingCommand } from '../base';
|
|
4
|
+
import type FontManager from '../base/FontManager';
|
|
5
|
+
import type { VexflowRecordingPathCommand } from '../base/VexflowRecordingTypes';
|
|
6
|
+
|
|
7
|
+
interface VerticalExtent {
|
|
8
|
+
top: number;
|
|
9
|
+
bottom: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface VerticalBand {
|
|
13
|
+
y: number;
|
|
14
|
+
height: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Grows each band to cover everything the recording draws for it. A command
|
|
19
|
+
* belongs to the band its vertical centre is in, or else the nearest one.
|
|
20
|
+
* Curve control points count as drawn, so a band can come out slightly taller
|
|
21
|
+
* than its ink, never shorter.
|
|
22
|
+
*/
|
|
23
|
+
export function coverDrawnExtents<Band extends VerticalBand>(
|
|
24
|
+
bands: readonly Band[],
|
|
25
|
+
commands: readonly VexflowRecordingCommand[],
|
|
26
|
+
fontManager: FontManager
|
|
27
|
+
): Band[] {
|
|
28
|
+
const extents = bands.map(
|
|
29
|
+
(band): VerticalExtent => ({ top: band.y, bottom: band.y + band.height })
|
|
30
|
+
);
|
|
31
|
+
const textExtents = new Map<SkFont, Map<string, VerticalExtent>>();
|
|
32
|
+
|
|
33
|
+
for (const command of commands) {
|
|
34
|
+
const drawn = commandExtent(command, fontManager, textExtents);
|
|
35
|
+
const extent = drawn && nearestExtent(extents, bands, drawn);
|
|
36
|
+
|
|
37
|
+
if (drawn && extent) {
|
|
38
|
+
extent.top = Math.min(extent.top, drawn.top);
|
|
39
|
+
extent.bottom = Math.max(extent.bottom, drawn.bottom);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return bands.map((band, index) => ({
|
|
44
|
+
...band,
|
|
45
|
+
y: extents[index]!.top,
|
|
46
|
+
height: extents[index]!.bottom - extents[index]!.top,
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function nearestExtent(
|
|
51
|
+
extents: VerticalExtent[],
|
|
52
|
+
bands: readonly VerticalBand[],
|
|
53
|
+
drawn: VerticalExtent
|
|
54
|
+
): VerticalExtent | undefined {
|
|
55
|
+
const centre = (drawn.top + drawn.bottom) / 2;
|
|
56
|
+
let nearest: VerticalExtent | undefined;
|
|
57
|
+
let nearestDistance = Infinity;
|
|
58
|
+
|
|
59
|
+
bands.forEach((band, index) => {
|
|
60
|
+
const distance = Math.max(band.y - centre, centre - band.y - band.height);
|
|
61
|
+
|
|
62
|
+
if (distance < nearestDistance) {
|
|
63
|
+
nearest = extents[index];
|
|
64
|
+
nearestDistance = distance;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return nearest;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function commandExtent(
|
|
72
|
+
command: VexflowRecordingCommand,
|
|
73
|
+
fontManager: FontManager,
|
|
74
|
+
textExtents: Map<SkFont, Map<string, VerticalExtent>>
|
|
75
|
+
): VerticalExtent | undefined {
|
|
76
|
+
switch (command.type) {
|
|
77
|
+
case 'fillRect':
|
|
78
|
+
return {
|
|
79
|
+
top: command.rect.y,
|
|
80
|
+
bottom: command.rect.y + command.rect.height,
|
|
81
|
+
};
|
|
82
|
+
case 'fillPath':
|
|
83
|
+
return pathExtent(command.path, 0);
|
|
84
|
+
case 'strokePath':
|
|
85
|
+
return pathExtent(command.path, (command.paint.strokeWidth ?? 0) / 2);
|
|
86
|
+
case 'fillText': {
|
|
87
|
+
const { font, size, weight, style } = command.font;
|
|
88
|
+
const skFont = fontManager.createSkFont(font, size, weight, style);
|
|
89
|
+
const byText = textExtents.get(skFont) ?? new Map();
|
|
90
|
+
let glyphs = byText.get(command.text);
|
|
91
|
+
|
|
92
|
+
if (!glyphs) {
|
|
93
|
+
const rect = skFont.measureText(command.text);
|
|
94
|
+
|
|
95
|
+
glyphs = { top: rect.y, bottom: rect.y + rect.height };
|
|
96
|
+
byText.set(command.text, glyphs);
|
|
97
|
+
textExtents.set(skFont, byText);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
top: command.y + glyphs.top,
|
|
102
|
+
bottom: command.y + glyphs.bottom,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function pathExtent(
|
|
111
|
+
path: VexflowRecordingPathCommand[],
|
|
112
|
+
outset: number
|
|
113
|
+
): VerticalExtent | undefined {
|
|
114
|
+
const ys = path.flatMap((segment) => {
|
|
115
|
+
switch (segment.type) {
|
|
116
|
+
case 'moveTo':
|
|
117
|
+
case 'lineTo':
|
|
118
|
+
return [segment.y];
|
|
119
|
+
case 'quadTo':
|
|
120
|
+
return [segment.cpy, segment.y];
|
|
121
|
+
case 'cubicTo':
|
|
122
|
+
return [segment.cp1y, segment.cp2y, segment.y];
|
|
123
|
+
case 'addRect':
|
|
124
|
+
case 'addArc':
|
|
125
|
+
return [segment.rect.y, segment.rect.y + segment.rect.height];
|
|
126
|
+
default:
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return ys.length > 0
|
|
132
|
+
? { top: Math.min(...ys) - outset, bottom: Math.max(...ys) + outset }
|
|
133
|
+
: undefined;
|
|
134
|
+
}
|
package/src/renderer/types.ts
CHANGED
|
@@ -145,6 +145,38 @@ export interface ScoreInsets {
|
|
|
145
145
|
export interface ScoreSpacing {
|
|
146
146
|
staffGap: number;
|
|
147
147
|
minIntrinsicSizeMultiplier: number;
|
|
148
|
+
/**
|
|
149
|
+
* `documentEven` only: place every system of a staff group at one uniform
|
|
150
|
+
* pitch. All systems share the tallest system's vertical extent, so staves
|
|
151
|
+
* sit on a fixed grid instead of each system advancing by its own height —
|
|
152
|
+
* rows of two renderers laid out side by side stay aligned. Default false.
|
|
153
|
+
*/
|
|
154
|
+
uniformSystemPitch: boolean;
|
|
155
|
+
/**
|
|
156
|
+
* `document` only: indent of the first system of each staff group, in
|
|
157
|
+
* content units (before `render.scale`). That system starts later and is
|
|
158
|
+
* narrower by the same amount. Default 0.
|
|
159
|
+
*/
|
|
160
|
+
firstSystemIndent: number;
|
|
161
|
+
/**
|
|
162
|
+
* Vertical gap between systems in content units, clamped to >= 0. With
|
|
163
|
+
* `uniformSystemPitch` the pitch is the shared system height plus this gap.
|
|
164
|
+
* Undefined or non-finite = the built-in `max(82, staffGap / 8)`.
|
|
165
|
+
*/
|
|
166
|
+
systemGap?: number;
|
|
167
|
+
/**
|
|
168
|
+
* `document` only: `justify` stretches the last system of each staff group
|
|
169
|
+
* to the full width like every other system; `natural` keeps its intrinsic
|
|
170
|
+
* width (a one-system score counts as its own last system). `documentEven`
|
|
171
|
+
* never stretches a partial last system. Default `justify`.
|
|
172
|
+
*/
|
|
173
|
+
lastSystemFill: 'justify' | 'natural';
|
|
174
|
+
/**
|
|
175
|
+
* Draw the clef on the first measure of every system instead of the first
|
|
176
|
+
* measure of the score only; line breaking reserves its width. A measure
|
|
177
|
+
* with `leftModifiers.showClef: false` still hides it. Default false.
|
|
178
|
+
*/
|
|
179
|
+
clefOnEachSystem: boolean;
|
|
148
180
|
}
|
|
149
181
|
|
|
150
182
|
export interface RenderOptions {
|