vexflow-native 1.8.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 -405
- package/lib/module/base/FontManager.js +2 -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/{state/constants.js → percussion/index.js} +3 -3
- 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 +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 +1 -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 -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 +51 -2
- 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 -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 +12 -2
- package/src/base/FontManager.ts +1 -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 +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 +65 -2
- package/src/renderer/useScoreRecording.ts +11 -3
- package/src/renderer/vfKeys.ts +88 -0
- package/src/state/index.ts +1 -1
- package/src/state/types.ts +30 -5
- 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/state/constants.ts +0 -4
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Parenthesis } from 'vexflow';
|
|
2
|
+
import type { BoundingBox, GraceNoteGroup, Modifier } from 'vexflow';
|
|
3
|
+
|
|
4
|
+
import { noteheadWidth } from './scoreParsing';
|
|
5
|
+
import type { VFVoiceNote } from './scoreParsing';
|
|
6
|
+
import type { ScoreItemBounds, ScoreItemLayout } from './types';
|
|
7
|
+
|
|
8
|
+
function hasGraceNotes(
|
|
9
|
+
modifier: Modifier
|
|
10
|
+
): modifier is Modifier & Pick<GraceNoteGroup, 'getGraceNotes'> {
|
|
11
|
+
return 'getGraceNotes' in modifier;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Parenthesis draws at its anchor offset without moving itself, so its box
|
|
16
|
+
* is re-anchored where `Parenthesis.draw()` places it.
|
|
17
|
+
*/
|
|
18
|
+
function modifierBoundingBox(note: VFVoiceNote, modifier: Modifier) {
|
|
19
|
+
const box = modifier.getBoundingBox();
|
|
20
|
+
|
|
21
|
+
if (modifier.getCategory() !== Parenthesis.CATEGORY) {
|
|
22
|
+
return box;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const start = note.getModifierStartXY(
|
|
26
|
+
modifier.getPosition(),
|
|
27
|
+
modifier.checkIndex(),
|
|
28
|
+
{ forceFlagRight: true }
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return box.move(start.x, start.y);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Union box of a drawn note's modifiers; must run after `draw()` because
|
|
36
|
+
* modifiers take their position from it. undefined without modifiers or when
|
|
37
|
+
* none could be measured.
|
|
38
|
+
*/
|
|
39
|
+
export function computeModifierBounds(
|
|
40
|
+
note: VFVoiceNote
|
|
41
|
+
): ScoreItemBounds | undefined {
|
|
42
|
+
let bounds: ScoreItemBounds | undefined;
|
|
43
|
+
|
|
44
|
+
for (const modifier of note.getModifiers()) {
|
|
45
|
+
if (hasGraceNotes(modifier)) {
|
|
46
|
+
for (const graceNote of modifier.getGraceNotes()) {
|
|
47
|
+
bounds = mergeBox(bounds, graceNote.getBoundingBox());
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
bounds = mergeBox(bounds, modifierBoundingBox(note, modifier));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return bounds;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function mergeBox(
|
|
58
|
+
bounds: ScoreItemBounds | undefined,
|
|
59
|
+
box: BoundingBox
|
|
60
|
+
): ScoreItemBounds | undefined {
|
|
61
|
+
const left = box.getX();
|
|
62
|
+
const top = box.getY();
|
|
63
|
+
const right = left + box.getW();
|
|
64
|
+
const bottom = top + box.getH();
|
|
65
|
+
|
|
66
|
+
if (![left, top, right, bottom].every(Number.isFinite)) {
|
|
67
|
+
return bounds;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!bounds) {
|
|
71
|
+
return { left, right, top, bottom };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
left: Math.min(bounds.left, left),
|
|
76
|
+
right: Math.max(bounds.right, right),
|
|
77
|
+
top: Math.min(bounds.top, top),
|
|
78
|
+
bottom: Math.max(bounds.bottom, bottom),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Detected structurally because `GhostNote`s lack these getters. */
|
|
83
|
+
type NoteHeadSpan = {
|
|
84
|
+
getNoteHeadBeginX?: () => number;
|
|
85
|
+
getNoteHeadEndX?: () => number;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Center of a formatted note's visual notehead span. When the getters are
|
|
90
|
+
* missing (GhostNotes) the fallback is the center of a NOTIONAL notehead at
|
|
91
|
+
* the block's left edge, capped by the block width — so a `spacer` rest
|
|
92
|
+
* anchors where a real note's head would sit on the same tick, and external
|
|
93
|
+
* UI aligned to it does not hop when the note appears. Zero-width `hidden`
|
|
94
|
+
* rests keep anchoring at their tick x.
|
|
95
|
+
*/
|
|
96
|
+
export function resolveItemHeadCenterX(
|
|
97
|
+
note: VFVoiceNote,
|
|
98
|
+
x: number,
|
|
99
|
+
width: number
|
|
100
|
+
): number {
|
|
101
|
+
const { getNoteHeadBeginX, getNoteHeadEndX } = note as NoteHeadSpan;
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
typeof getNoteHeadBeginX === 'function' &&
|
|
105
|
+
typeof getNoteHeadEndX === 'function'
|
|
106
|
+
) {
|
|
107
|
+
const center =
|
|
108
|
+
(getNoteHeadBeginX.call(note) + getNoteHeadEndX.call(note)) / 2;
|
|
109
|
+
|
|
110
|
+
if (Number.isFinite(center) && center > x && center <= x + width) {
|
|
111
|
+
return center;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return x + Math.min(width, noteheadWidth()) / 2;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Geometry of a drawn item; must run after `draw()` because modifiers only
|
|
120
|
+
* take their position there.
|
|
121
|
+
*/
|
|
122
|
+
export function itemLayoutOf(
|
|
123
|
+
note: VFVoiceNote,
|
|
124
|
+
measureIndex: number
|
|
125
|
+
): ScoreItemLayout {
|
|
126
|
+
const x = note.getAbsoluteX();
|
|
127
|
+
const width = note.getWidth();
|
|
128
|
+
const modifierBounds = computeModifierBounds(note);
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
x,
|
|
132
|
+
width,
|
|
133
|
+
headCenterX: resolveItemHeadCenterX(note, x, width),
|
|
134
|
+
measureIndex,
|
|
135
|
+
...(modifierBounds ? { modifierBounds } : {}),
|
|
136
|
+
};
|
|
137
|
+
}
|
package/src/renderer/layout.ts
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
type ResolvedMeasureState,
|
|
18
18
|
} from './scoreParsing';
|
|
19
19
|
import type { MeasuredScore } from './measure';
|
|
20
|
-
import type { StaffVerticalBounds } from './
|
|
20
|
+
import type { StaffVerticalBounds } from './measureBounds';
|
|
21
21
|
import type {
|
|
22
22
|
RendererRect,
|
|
23
23
|
RendererSize,
|
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
|
+
}
|