vexflow-native 0.2.0 → 1.1.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 +267 -37
- package/lib/module/base/FontManager.js +139 -0
- package/lib/module/base/FontManager.js.map +1 -0
- package/lib/module/base/TextMeasureContext.js +46 -0
- package/lib/module/base/TextMeasureContext.js.map +1 -0
- package/lib/module/base/VexflowCanvas.js +48 -0
- package/lib/module/base/VexflowCanvas.js.map +1 -0
- package/lib/module/base/VexflowColorScheme.js +16 -0
- package/lib/module/base/VexflowColorScheme.js.map +1 -0
- package/lib/module/base/VexflowRecordingContext.js +487 -0
- package/lib/module/base/VexflowRecordingContext.js.map +1 -0
- package/lib/module/base/VexflowRecordingReplay.js +214 -0
- package/lib/module/base/VexflowRecordingReplay.js.map +1 -0
- package/lib/module/base/VexflowRecordingTypes.js +4 -0
- package/lib/module/base/VexflowRecordingTypes.js.map +1 -0
- package/lib/module/base/index.js +9 -0
- package/lib/module/base/index.js.map +1 -0
- package/lib/module/base/setupVexflowReactNative.js +96 -0
- package/lib/module/base/setupVexflowReactNative.js.map +1 -0
- package/lib/module/base/types.js +4 -0
- package/lib/module/base/types.js.map +1 -0
- package/lib/module/base/utils.js +18 -0
- package/lib/module/base/utils.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/InternalTypes.js +4 -0
- package/lib/module/musicxml/MusicXmlParser/InternalTypes.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +269 -0
- package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +62 -0
- package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/MusicXmlTiming.js +124 -0
- package/lib/module/musicxml/MusicXmlParser/MusicXmlTiming.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/PartParser.js +41 -0
- package/lib/module/musicxml/MusicXmlParser/PartParser.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/Spanners.js +149 -0
- package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/XmlOrder.js +106 -0
- package/lib/module/musicxml/MusicXmlParser/XmlOrder.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/index.js +110 -0
- package/lib/module/musicxml/MusicXmlParser/index.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/mappers.js +312 -0
- package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -0
- package/lib/module/musicxml/MusicXmlParser/types.js +10 -0
- package/lib/module/musicxml/MusicXmlParser/types.js.map +1 -0
- package/lib/module/musicxml/index.js +4 -0
- package/lib/module/musicxml/index.js.map +1 -0
- package/lib/module/musicxml/testfiles/lg-8102429.xml +79620 -0
- package/lib/module/renderer/Layout/DocumentLayout.js +156 -0
- package/lib/module/renderer/Layout/DocumentLayout.js.map +1 -0
- package/lib/module/renderer/Layout/LayoutMetrics.js +13 -0
- package/lib/module/renderer/Layout/LayoutMetrics.js.map +1 -0
- package/lib/module/renderer/Layout/VerticalSpacing.js +69 -0
- package/lib/module/renderer/Layout/VerticalSpacing.js.map +1 -0
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +277 -0
- package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -0
- package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js +108 -0
- package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js.map +1 -0
- package/lib/module/renderer/ScoreRenderer/index.js +7 -0
- package/lib/module/renderer/ScoreRenderer/index.js.map +1 -0
- package/lib/module/renderer/ScoreRenderer/types.js +4 -0
- package/lib/module/renderer/ScoreRenderer/types.js.map +1 -0
- package/lib/module/renderer/ScoreRenderer/useScoreScroll.js +77 -0
- package/lib/module/renderer/ScoreRenderer/useScoreScroll.js.map +1 -0
- package/lib/module/renderer/colorScheme.js +17 -0
- package/lib/module/renderer/colorScheme.js.map +1 -0
- package/lib/module/renderer/constants.js +18 -0
- package/lib/module/renderer/constants.js.map +1 -0
- package/lib/module/renderer/index.js +6 -0
- package/lib/module/renderer/index.js.map +1 -0
- package/lib/module/renderer/layout.js +153 -0
- package/lib/module/renderer/layout.js.map +1 -0
- package/lib/module/renderer/measure.js +202 -0
- package/lib/module/renderer/measure.js.map +1 -0
- package/lib/module/renderer/render.js +184 -0
- package/lib/module/renderer/render.js.map +1 -0
- package/lib/module/renderer/scoreParsing.js +230 -0
- package/lib/module/renderer/scoreParsing.js.map +1 -0
- package/lib/module/renderer/types.js +4 -0
- package/lib/module/renderer/types.js.map +1 -0
- package/lib/module/renderer/useScoreRecording.js +112 -0
- package/lib/module/renderer/useScoreRecording.js.map +1 -0
- package/lib/module/renderer/viewport.js +26 -0
- package/lib/module/renderer/viewport.js.map +1 -0
- package/lib/module/shared/Logger.js +67 -0
- package/lib/module/shared/Logger.js.map +1 -0
- package/lib/module/state/index.js +4 -0
- package/lib/module/state/index.js.map +1 -0
- package/lib/module/state/types.js +2 -0
- package/lib/module/state/types.js.map +1 -0
- package/lib/module/types.js +7 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils.js +42 -77
- package/lib/module/utils.js.map +1 -1
- package/lib/typescript/src/base/FontManager.d.ts +23 -0
- package/lib/typescript/src/base/FontManager.d.ts.map +1 -0
- package/lib/typescript/src/base/TextMeasureContext.d.ts +22 -0
- package/lib/typescript/src/base/TextMeasureContext.d.ts.map +1 -0
- package/lib/typescript/src/base/VexflowCanvas.d.ts +5 -0
- package/lib/typescript/src/base/VexflowCanvas.d.ts.map +1 -0
- package/lib/typescript/src/base/VexflowColorScheme.d.ts +7 -0
- package/lib/typescript/src/base/VexflowColorScheme.d.ts.map +1 -0
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts +103 -0
- package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -0
- package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +12 -0
- package/lib/typescript/src/base/VexflowRecordingReplay.d.ts.map +1 -0
- package/lib/typescript/src/base/VexflowRecordingTypes.d.ts +146 -0
- package/lib/typescript/src/base/VexflowRecordingTypes.d.ts.map +1 -0
- package/lib/typescript/src/base/index.d.ts +7 -0
- package/lib/typescript/src/base/index.d.ts.map +1 -0
- package/lib/typescript/src/base/setupVexflowReactNative.d.ts +11 -0
- package/lib/typescript/src/base/setupVexflowReactNative.d.ts.map +1 -0
- package/lib/typescript/src/base/types.d.ts +13 -0
- package/lib/typescript/src/base/types.d.ts.map +1 -0
- package/lib/typescript/src/base/utils.d.ts +2 -0
- package/lib/typescript/src/base/utils.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +30 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts +4 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +5 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlTiming.d.ts +6 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlTiming.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts +8 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +4 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/XmlOrder.d.ts +20 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/XmlOrder.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/index.d.ts +5 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/index.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts +15 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/types.d.ts +13 -0
- package/lib/typescript/src/musicxml/MusicXmlParser/types.d.ts.map +1 -0
- package/lib/typescript/src/musicxml/index.d.ts +2 -0
- package/lib/typescript/src/musicxml/index.d.ts.map +1 -0
- package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts +10 -0
- package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts.map +1 -0
- package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts +6 -0
- package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts.map +1 -0
- package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts +9 -0
- package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts.map +1 -0
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts +15 -0
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -0
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts +24 -0
- package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts.map +1 -0
- package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts +5 -0
- package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts.map +1 -0
- package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +12 -0
- package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -0
- package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts +13 -0
- package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts.map +1 -0
- package/lib/typescript/src/renderer/colorScheme.d.ts +9 -0
- package/lib/typescript/src/renderer/colorScheme.d.ts.map +1 -0
- package/lib/typescript/src/renderer/constants.d.ts +5 -0
- package/lib/typescript/src/renderer/constants.d.ts.map +1 -0
- package/lib/typescript/src/renderer/index.d.ts +4 -0
- package/lib/typescript/src/renderer/index.d.ts.map +1 -0
- package/lib/typescript/src/renderer/layout.d.ts +56 -0
- package/lib/typescript/src/renderer/layout.d.ts.map +1 -0
- package/lib/typescript/src/renderer/measure.d.ts +23 -0
- package/lib/typescript/src/renderer/measure.d.ts.map +1 -0
- package/lib/typescript/src/renderer/render.d.ts +9 -0
- package/lib/typescript/src/renderer/render.d.ts.map +1 -0
- package/lib/typescript/src/renderer/scoreParsing.d.ts +76 -0
- package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -0
- package/lib/typescript/src/renderer/types.d.ts +56 -0
- package/lib/typescript/src/renderer/types.d.ts.map +1 -0
- package/lib/typescript/src/renderer/useScoreRecording.d.ts +21 -0
- package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -0
- package/lib/typescript/src/renderer/viewport.d.ts +3 -0
- package/lib/typescript/src/renderer/viewport.d.ts.map +1 -0
- package/lib/typescript/src/shared/Logger.d.ts +18 -0
- package/lib/typescript/src/shared/Logger.d.ts.map +1 -0
- package/lib/typescript/src/state/index.d.ts +2 -0
- package/lib/typescript/src/state/index.d.ts.map +1 -0
- package/lib/typescript/src/state/types.d.ts +190 -0
- package/lib/typescript/src/state/types.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +0 -6
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/utils.d.ts +0 -6
- package/lib/typescript/src/utils.d.ts.map +1 -1
- package/package.json +41 -14
- package/src/base/FontManager.ts +206 -0
- package/src/base/TextMeasureContext.ts +59 -0
- package/src/base/VexflowCanvas.tsx +60 -0
- package/src/base/VexflowColorScheme.ts +32 -0
- package/src/base/VexflowRecordingContext.ts +545 -0
- package/src/base/VexflowRecordingReplay.ts +327 -0
- package/src/base/VexflowRecordingTypes.ts +169 -0
- package/src/base/index.ts +7 -0
- package/src/base/setupVexflowReactNative.ts +124 -0
- package/src/base/types.ts +17 -0
- package/src/base/utils.ts +21 -0
- package/src/musicxml/MusicXmlParser/InternalTypes.ts +41 -0
- package/src/musicxml/MusicXmlParser/MeasureParser.ts +469 -0
- package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +84 -0
- package/src/musicxml/MusicXmlParser/MusicXmlTiming.ts +153 -0
- package/src/musicxml/MusicXmlParser/PartParser.ts +69 -0
- package/src/musicxml/MusicXmlParser/Spanners.ts +215 -0
- package/src/musicxml/MusicXmlParser/XmlOrder.ts +170 -0
- package/src/musicxml/MusicXmlParser/index.ts +144 -0
- package/src/musicxml/MusicXmlParser/mappers.ts +307 -0
- package/src/musicxml/MusicXmlParser/types.ts +24 -0
- package/src/musicxml/index.ts +5 -0
- package/src/musicxml/testfiles/lg-8102429.xml +79620 -0
- package/src/renderer/Layout/DocumentLayout.ts +240 -0
- package/src/renderer/Layout/LayoutMetrics.ts +21 -0
- package/src/renderer/Layout/VerticalSpacing.ts +122 -0
- package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +384 -0
- package/src/renderer/ScoreRenderer/ScoreScrollbar.tsx +231 -0
- package/src/renderer/ScoreRenderer/index.ts +13 -0
- package/src/renderer/ScoreRenderer/types.ts +14 -0
- package/src/renderer/ScoreRenderer/useScoreScroll.ts +146 -0
- package/src/renderer/colorScheme.ts +28 -0
- package/src/renderer/constants.ts +19 -0
- package/src/renderer/index.ts +3 -0
- package/src/renderer/layout.ts +309 -0
- package/src/renderer/measure.tsx +304 -0
- package/src/renderer/render.tsx +308 -0
- package/src/renderer/scoreParsing.tsx +352 -0
- package/src/renderer/types.ts +65 -0
- package/src/renderer/useScoreRecording.ts +177 -0
- package/src/renderer/viewport.ts +45 -0
- package/src/shared/Logger.ts +85 -0
- package/src/state/index.ts +1 -0
- package/src/state/types.ts +295 -0
- package/src/types.ts +6 -6
- package/src/utils.tsx +32 -98
- package/lib/module/SkiaVexflowContext.js +0 -285
- package/lib/module/SkiaVexflowContext.js.map +0 -1
- package/lib/module/VexflowCanvas.js +0 -103
- package/lib/module/VexflowCanvas.js.map +0 -1
- package/lib/module/colorUtils.js +0 -34
- package/lib/module/colorUtils.js.map +0 -1
- package/lib/module/colorUtils.test.js +0 -37
- package/lib/module/colorUtils.test.js.map +0 -1
- package/lib/module/constants.js +0 -30
- package/lib/module/constants.js.map +0 -1
- package/lib/module/index.js +0 -8
- package/lib/module/index.js.map +0 -1
- package/lib/typescript/src/SkiaVexflowContext.d.ts +0 -101
- package/lib/typescript/src/SkiaVexflowContext.d.ts.map +0 -1
- package/lib/typescript/src/VexflowCanvas.d.ts +0 -17
- package/lib/typescript/src/VexflowCanvas.d.ts.map +0 -1
- package/lib/typescript/src/colorUtils.d.ts +0 -2
- package/lib/typescript/src/colorUtils.d.ts.map +0 -1
- package/lib/typescript/src/colorUtils.test.d.ts +0 -2
- package/lib/typescript/src/colorUtils.test.d.ts.map +0 -1
- package/lib/typescript/src/constants.d.ts +0 -21
- package/lib/typescript/src/constants.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -7
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/src/SkiaVexflowContext.ts +0 -462
- package/src/VexflowCanvas.tsx +0 -145
- package/src/colorUtils.test.ts +0 -52
- package/src/colorUtils.ts +0 -55
- package/src/constants.tsx +0 -34
- package/src/index.ts +0 -8
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Accidental as VFAccidental,
|
|
3
|
+
Beam,
|
|
4
|
+
Dot,
|
|
5
|
+
Fraction as VFFraction,
|
|
6
|
+
GhostNote,
|
|
7
|
+
StaveNote,
|
|
8
|
+
Stem,
|
|
9
|
+
Tuplet,
|
|
10
|
+
Voice as VFVoice,
|
|
11
|
+
} from 'vexflow';
|
|
12
|
+
import type { StemmableNote } from 'vexflow';
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
Clef,
|
|
16
|
+
KeySignature,
|
|
17
|
+
DurationValue,
|
|
18
|
+
Meter,
|
|
19
|
+
Pitch,
|
|
20
|
+
Score,
|
|
21
|
+
Staff,
|
|
22
|
+
StaffGroup,
|
|
23
|
+
StemDirection,
|
|
24
|
+
Tempo,
|
|
25
|
+
TupletGroup,
|
|
26
|
+
Voice,
|
|
27
|
+
VoiceItem,
|
|
28
|
+
VoiceTimingMode,
|
|
29
|
+
} from '../state';
|
|
30
|
+
|
|
31
|
+
export type StaffGroupLookup = {
|
|
32
|
+
groupId: string;
|
|
33
|
+
staffGroup?: StaffGroup;
|
|
34
|
+
staffIds: string[];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export interface ResolvedMeasureState {
|
|
38
|
+
clef: Clef;
|
|
39
|
+
meter: Meter;
|
|
40
|
+
keySignature?: KeySignature;
|
|
41
|
+
tempo?: Tempo;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type VFVoiceNote = StemmableNote;
|
|
45
|
+
|
|
46
|
+
export interface MakeVFVoiceOptions {
|
|
47
|
+
resolveClef?: (item: VoiceItem) => Clef;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Maps the library stem direction to the numeric VexFlow value.
|
|
52
|
+
*/
|
|
53
|
+
export function toVFStemDirection(dir?: StemDirection): number | undefined {
|
|
54
|
+
if (dir === 'up') {
|
|
55
|
+
return Stem.UP;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (dir === 'down') {
|
|
59
|
+
return Stem.DOWN;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Converts an internal pitch into the VexFlow key string format.
|
|
67
|
+
*/
|
|
68
|
+
export function pitchToVFKey(pitch: Pitch): string {
|
|
69
|
+
const accidental =
|
|
70
|
+
pitch.accidental === 'quarter-flat'
|
|
71
|
+
? 'db'
|
|
72
|
+
: pitch.accidental === 'quarter-sharp'
|
|
73
|
+
? 'd#'
|
|
74
|
+
: pitch.accidental ?? '';
|
|
75
|
+
|
|
76
|
+
return `${pitch.step.toLowerCase()}${accidental}/${pitch.octave}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Converts a duration into the VexFlow duration token, including rests.
|
|
81
|
+
*/
|
|
82
|
+
export function durationToVF(duration: DurationValue, isRest = false): string {
|
|
83
|
+
const length =
|
|
84
|
+
duration.length === 'long' || duration.length === 'breve'
|
|
85
|
+
? '1/2'
|
|
86
|
+
: duration.length;
|
|
87
|
+
|
|
88
|
+
if (isRest) {
|
|
89
|
+
return `${length}r`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (duration.dots) {
|
|
93
|
+
return `${length}${'d'.repeat(duration.dots)}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return length;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Applies the requested number of augmentation dots to a note.
|
|
101
|
+
*/
|
|
102
|
+
export function applyDots(note: StaveNote, dots?: 0 | 1 | 2 | 3) {
|
|
103
|
+
for (let i = 0; i < (dots ?? 0); i++) {
|
|
104
|
+
Dot.buildAndAttach([note], { all: true });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Adds pitch accidentals to the matching keys in a VexFlow note.
|
|
110
|
+
*/
|
|
111
|
+
export function addPitchAccidentals(note: StaveNote, pitches: Pitch[]) {
|
|
112
|
+
pitches.forEach((pitch, index) => {
|
|
113
|
+
if (!pitch.accidental) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const type =
|
|
118
|
+
pitch.accidental === 'quarter-flat'
|
|
119
|
+
? 'db'
|
|
120
|
+
: pitch.accidental === 'quarter-sharp'
|
|
121
|
+
? 'd#'
|
|
122
|
+
: pitch.accidental;
|
|
123
|
+
|
|
124
|
+
note.addModifier(new VFAccidental(type), index);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Builds a VexFlow stave note from a score voice item and clef.
|
|
130
|
+
*/
|
|
131
|
+
export function voiceItemToStaveNote(item: VoiceItem, clef: Clef): VFVoiceNote {
|
|
132
|
+
if (item.type === 'rest') {
|
|
133
|
+
if (item.kind === 'hidden' || item.kind === 'spacer') {
|
|
134
|
+
return new GhostNote(durationToVF(item.duration));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const note = new StaveNote({
|
|
138
|
+
clef,
|
|
139
|
+
keys: ['b/4'],
|
|
140
|
+
duration: durationToVF(item.duration, true),
|
|
141
|
+
});
|
|
142
|
+
applyDots(note, item.duration.dots);
|
|
143
|
+
return note;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (item.type === 'note') {
|
|
147
|
+
const note = new StaveNote({
|
|
148
|
+
clef,
|
|
149
|
+
keys: [pitchToVFKey(item.pitch)],
|
|
150
|
+
duration: durationToVF(item.duration),
|
|
151
|
+
stemDirection: toVFStemDirection(item.stemDirection),
|
|
152
|
+
});
|
|
153
|
+
addPitchAccidentals(note, [item.pitch]);
|
|
154
|
+
applyDots(note, item.duration.dots);
|
|
155
|
+
return note;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const note = new StaveNote({
|
|
159
|
+
clef,
|
|
160
|
+
keys: item.pitches.map(pitchToVFKey),
|
|
161
|
+
duration: durationToVF(item.duration),
|
|
162
|
+
stemDirection: toVFStemDirection(item.stemDirection),
|
|
163
|
+
});
|
|
164
|
+
addPitchAccidentals(note, item.pitches);
|
|
165
|
+
applyDots(note, item.duration.dots);
|
|
166
|
+
return note;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Translates beam group fractions into the VexFlow representation.
|
|
171
|
+
*/
|
|
172
|
+
export function beamGroupsToVF(meter?: Meter): VFFraction[] | undefined {
|
|
173
|
+
if (!meter?.beamGroups?.length) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return meter.beamGroups.map((group) => new VFFraction(group.num, group.den));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function hasExplicitStemDirection(item: VoiceItem): boolean {
|
|
181
|
+
return (
|
|
182
|
+
item.type !== 'rest' &&
|
|
183
|
+
(item.stemDirection === 'up' || item.stemDirection === 'down')
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Finds the tuplet groups that belong to a given voice.
|
|
189
|
+
*/
|
|
190
|
+
export function findTupletsForVoice(score: Score, voice: Voice): TupletGroup[] {
|
|
191
|
+
return (score.tuplets ?? []).filter((tuplet) => tuplet.voiceId === voice.id);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Maps the internal timing mode to the matching VexFlow voice mode.
|
|
196
|
+
*/
|
|
197
|
+
export function modeToVF(mode: VoiceTimingMode): number {
|
|
198
|
+
switch (mode) {
|
|
199
|
+
case 'soft':
|
|
200
|
+
return VFVoice.Mode.SOFT;
|
|
201
|
+
case 'free':
|
|
202
|
+
return VFVoice.Mode.FULL;
|
|
203
|
+
case 'strict':
|
|
204
|
+
default:
|
|
205
|
+
return VFVoice.Mode.STRICT;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Resolves each measure state by carrying forward prior staff state.
|
|
211
|
+
*/
|
|
212
|
+
export function buildResolvedMeasureStates(
|
|
213
|
+
score: Score,
|
|
214
|
+
staff: Staff
|
|
215
|
+
): ResolvedMeasureState[] {
|
|
216
|
+
let previousState: ResolvedMeasureState | undefined;
|
|
217
|
+
|
|
218
|
+
return staff.measures.map((measure) => {
|
|
219
|
+
const resolvedState: ResolvedMeasureState = {
|
|
220
|
+
clef: measure.state?.clef ?? previousState?.clef ?? staff.defaultClef,
|
|
221
|
+
meter:
|
|
222
|
+
measure.state?.meter ?? previousState?.meter ?? score.defaults.meter,
|
|
223
|
+
keySignature:
|
|
224
|
+
measure.state?.keySignature ??
|
|
225
|
+
previousState?.keySignature ??
|
|
226
|
+
score.defaults.keySignature,
|
|
227
|
+
tempo:
|
|
228
|
+
measure.state?.tempo ?? previousState?.tempo ?? score.defaults.tempo,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
previousState = resolvedState;
|
|
232
|
+
return resolvedState;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Builds the VexFlow voice, notes, beams, and tuplets for one score voice.
|
|
238
|
+
*/
|
|
239
|
+
export function makeVFVoice(
|
|
240
|
+
score: Score,
|
|
241
|
+
meter: Meter,
|
|
242
|
+
clef: Clef,
|
|
243
|
+
voice: Voice,
|
|
244
|
+
options: MakeVFVoiceOptions = {}
|
|
245
|
+
): {
|
|
246
|
+
vfVoice: VFVoice;
|
|
247
|
+
notes: VFVoiceNote[];
|
|
248
|
+
beams: Beam[];
|
|
249
|
+
tuplets: Tuplet[];
|
|
250
|
+
} {
|
|
251
|
+
const notes = voice.items.map((item) =>
|
|
252
|
+
voiceItemToStaveNote(item, options.resolveClef?.(item) ?? clef)
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
const noteByItemId = new Map<string, VFVoiceNote>();
|
|
256
|
+
voice.items.forEach((item, index) =>
|
|
257
|
+
noteByItemId.set(item.id, notes[index]!)
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
const vfVoice = new VFVoice({
|
|
261
|
+
numBeats: meter.beats,
|
|
262
|
+
beatValue: meter.beatUnit,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
if (voice.timingMode) {
|
|
266
|
+
vfVoice.setMode(modeToVF(voice.timingMode));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
try {
|
|
270
|
+
vfVoice.addTickables(notes);
|
|
271
|
+
} catch (error) {
|
|
272
|
+
throw new Error(
|
|
273
|
+
`Error adding tickables to voice ${voice.id}: ${(error as Error).message}`
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const groups = beamGroupsToVF(meter);
|
|
278
|
+
const maintainStemDirections = voice.items.some(hasExplicitStemDirection);
|
|
279
|
+
const beamOptions =
|
|
280
|
+
groups || maintainStemDirections
|
|
281
|
+
? {
|
|
282
|
+
...(groups ? { groups } : {}),
|
|
283
|
+
...(maintainStemDirections ? { maintainStemDirections } : {}),
|
|
284
|
+
}
|
|
285
|
+
: undefined;
|
|
286
|
+
const beams = Beam.generateBeams(notes, beamOptions);
|
|
287
|
+
|
|
288
|
+
const tuplets = findTupletsForVoice(score, voice)
|
|
289
|
+
.map((group) => {
|
|
290
|
+
const tupletNotes = group.itemIds
|
|
291
|
+
.map((id) => noteByItemId.get(id))
|
|
292
|
+
.filter((note): note is StaveNote => Boolean(note));
|
|
293
|
+
|
|
294
|
+
if (tupletNotes.length < 2) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return new Tuplet(tupletNotes, {
|
|
299
|
+
numNotes: group.ratio.num,
|
|
300
|
+
notesOccupied: group.ratio.den,
|
|
301
|
+
bracketed: group.bracketed,
|
|
302
|
+
location:
|
|
303
|
+
group.placement === 'below'
|
|
304
|
+
? Tuplet.LOCATION_BOTTOM
|
|
305
|
+
: Tuplet.LOCATION_TOP,
|
|
306
|
+
});
|
|
307
|
+
})
|
|
308
|
+
.filter((tuplet): tuplet is Tuplet => Boolean(tuplet));
|
|
309
|
+
|
|
310
|
+
return { vfVoice, notes, beams, tuplets };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Resolves explicit staff groups and standalone staves into render groups.
|
|
315
|
+
*/
|
|
316
|
+
export function buildMeasurementGroups(score: Score): StaffGroupLookup[] {
|
|
317
|
+
const explicitGroups = (score.staffGroups ?? []).map((group) => ({
|
|
318
|
+
groupId: group.id,
|
|
319
|
+
staffGroup: group,
|
|
320
|
+
staffIds: [...(group.staffIds ?? [])].sort((staffIdA, staffIdB) => {
|
|
321
|
+
const staffA = score.staves.find((staff) => staff.id === staffIdA);
|
|
322
|
+
const staffB = score.staves.find((staff) => staff.id === staffIdB);
|
|
323
|
+
return (staffA?.order ?? 0) - (staffB?.order ?? 0);
|
|
324
|
+
}),
|
|
325
|
+
}));
|
|
326
|
+
|
|
327
|
+
const groupedStaffIds = new Set(
|
|
328
|
+
explicitGroups.flatMap((group) => group.staffIds)
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
const singles = score.staves
|
|
332
|
+
.filter((staff) => !groupedStaffIds.has(staff.id))
|
|
333
|
+
.sort((staffA, staffB) => staffA.order - staffB.order)
|
|
334
|
+
.map((staff) => ({
|
|
335
|
+
groupId: `staff:${staff.id}`,
|
|
336
|
+
staffIds: [staff.id],
|
|
337
|
+
}));
|
|
338
|
+
|
|
339
|
+
return [...explicitGroups, ...singles];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Resolves a staff group lookup into the existing ordered score staves.
|
|
344
|
+
*/
|
|
345
|
+
export function resolveGroupStaves(
|
|
346
|
+
score: Score,
|
|
347
|
+
group: StaffGroupLookup
|
|
348
|
+
): Staff[] {
|
|
349
|
+
return group.staffIds
|
|
350
|
+
.map((id) => score.staves.find((staff) => staff.id === id))
|
|
351
|
+
.filter((staff): staff is Staff => Boolean(staff));
|
|
352
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { SkTypefaceFontProvider } from '@shopify/react-native-skia';
|
|
2
|
+
import type { SharedValue } from 'react-native-reanimated';
|
|
3
|
+
|
|
4
|
+
import type { VexflowStyleOverride } from '../base';
|
|
5
|
+
import type { Score } from '../state';
|
|
6
|
+
import type { ScoreColorScheme } from './colorScheme';
|
|
7
|
+
|
|
8
|
+
export type RendererType = 'infiniteScore' | 'document' | 'documentEven';
|
|
9
|
+
export type ScoreItemStyleOverride = VexflowStyleOverride;
|
|
10
|
+
export type ScoreItemStyleOverrides = Record<string, ScoreItemStyleOverride>;
|
|
11
|
+
|
|
12
|
+
export interface ScoreRendererProps {
|
|
13
|
+
score: Score;
|
|
14
|
+
defaultFont: string;
|
|
15
|
+
fontManager: SkTypefaceFontProvider;
|
|
16
|
+
colorScheme?: ScoreColorScheme;
|
|
17
|
+
itemStyleOverrides?: SharedValue<ScoreItemStyleOverrides>;
|
|
18
|
+
rendererType?: RendererType;
|
|
19
|
+
options?: Partial<ScoreOptions>;
|
|
20
|
+
scrollEnabled?: boolean;
|
|
21
|
+
showScrollbars?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Viewport {
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ScoreInsets {
|
|
30
|
+
top: number;
|
|
31
|
+
right: number;
|
|
32
|
+
bottom: number;
|
|
33
|
+
left: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ScoreSpacing {
|
|
37
|
+
staffGap: number;
|
|
38
|
+
minIntrinsicSizeMultiplier: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface RenderOptions {
|
|
42
|
+
pixelRatio: number;
|
|
43
|
+
scale: number;
|
|
44
|
+
debug: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ScoreOptions {
|
|
48
|
+
spacing: ScoreSpacing;
|
|
49
|
+
insets: ScoreInsets;
|
|
50
|
+
render: RenderOptions;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface RendererPoint {
|
|
54
|
+
x: number;
|
|
55
|
+
y: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface RendererSize {
|
|
59
|
+
width: number;
|
|
60
|
+
height: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface RendererRect extends RendererPoint, RendererSize {}
|
|
64
|
+
|
|
65
|
+
export interface VisibleViewport extends RendererRect {}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import type { SkTypefaceFontProvider } from '@shopify/react-native-skia';
|
|
3
|
+
|
|
4
|
+
import type { VexflowRecordingCommand } from '../base';
|
|
5
|
+
import VexflowRecordingContext from '../base/VexflowRecordingContext';
|
|
6
|
+
import type { Score } from '../state';
|
|
7
|
+
import { layoutScore, type ScoreLayoutPlan } from './layout';
|
|
8
|
+
import { measureScore } from './measure';
|
|
9
|
+
import { renderScore } from './render';
|
|
10
|
+
import type { ResolvedScoreColorScheme } from './colorScheme';
|
|
11
|
+
import type { RendererRect, RendererType, ScoreOptions } from './types';
|
|
12
|
+
|
|
13
|
+
export interface ScoreRecording {
|
|
14
|
+
commands: readonly VexflowRecordingCommand[];
|
|
15
|
+
layoutPlan: ScoreLayoutPlan;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function useScoreRecording({
|
|
19
|
+
defaultFont,
|
|
20
|
+
enabled = true,
|
|
21
|
+
fontManager,
|
|
22
|
+
colorScheme,
|
|
23
|
+
options,
|
|
24
|
+
rendererType,
|
|
25
|
+
score,
|
|
26
|
+
viewport,
|
|
27
|
+
}: {
|
|
28
|
+
defaultFont: string;
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
fontManager: SkTypefaceFontProvider;
|
|
31
|
+
colorScheme: ResolvedScoreColorScheme;
|
|
32
|
+
options: ScoreOptions;
|
|
33
|
+
rendererType: RendererType;
|
|
34
|
+
score: Score;
|
|
35
|
+
viewport: RendererRect;
|
|
36
|
+
}): ScoreRecording {
|
|
37
|
+
return useMemo(() => {
|
|
38
|
+
if (!enabled) {
|
|
39
|
+
return {
|
|
40
|
+
commands: [],
|
|
41
|
+
layoutPlan: createEmptyLayoutPlan(rendererType, viewport),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const measureStart = nowMs();
|
|
46
|
+
const ctx = new VexflowRecordingContext(
|
|
47
|
+
fontManager,
|
|
48
|
+
defaultFont,
|
|
49
|
+
colorScheme
|
|
50
|
+
);
|
|
51
|
+
const measuredScore = measureScore(score, options);
|
|
52
|
+
const measureMs = nowMs() - measureStart;
|
|
53
|
+
|
|
54
|
+
const layoutStart = nowMs();
|
|
55
|
+
const layoutPlan = layoutScore(
|
|
56
|
+
score,
|
|
57
|
+
measuredScore,
|
|
58
|
+
options,
|
|
59
|
+
rendererType,
|
|
60
|
+
viewport
|
|
61
|
+
);
|
|
62
|
+
const layoutMs = nowMs() - layoutStart;
|
|
63
|
+
|
|
64
|
+
const renderStart = nowMs();
|
|
65
|
+
renderScore(ctx, score, layoutPlan, options);
|
|
66
|
+
const renderMs = nowMs() - renderStart;
|
|
67
|
+
|
|
68
|
+
const finishStart = nowMs();
|
|
69
|
+
const commands = ctx.finish();
|
|
70
|
+
const finishMs = nowMs() - finishStart;
|
|
71
|
+
|
|
72
|
+
logScoreRecordingProfile({
|
|
73
|
+
commandCount: commands.length,
|
|
74
|
+
contentSize: layoutPlan.contentSize,
|
|
75
|
+
finishMs,
|
|
76
|
+
layoutMs,
|
|
77
|
+
measureCount: layoutPlan.measures.length,
|
|
78
|
+
measureMs,
|
|
79
|
+
renderMs,
|
|
80
|
+
rendererType,
|
|
81
|
+
scoreId: score.id,
|
|
82
|
+
systemCount: layoutPlan.systems.length,
|
|
83
|
+
viewport,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
commands,
|
|
88
|
+
layoutPlan,
|
|
89
|
+
};
|
|
90
|
+
}, [
|
|
91
|
+
colorScheme,
|
|
92
|
+
defaultFont,
|
|
93
|
+
enabled,
|
|
94
|
+
fontManager,
|
|
95
|
+
options,
|
|
96
|
+
rendererType,
|
|
97
|
+
score,
|
|
98
|
+
viewport,
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function createEmptyLayoutPlan(
|
|
103
|
+
rendererType: RendererType,
|
|
104
|
+
viewport: RendererRect
|
|
105
|
+
): ScoreLayoutPlan {
|
|
106
|
+
return {
|
|
107
|
+
rendererType,
|
|
108
|
+
contentSize: {
|
|
109
|
+
width: viewport.width,
|
|
110
|
+
height: viewport.height,
|
|
111
|
+
},
|
|
112
|
+
systems: [],
|
|
113
|
+
measures: [],
|
|
114
|
+
groups: [],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function logScoreRecordingProfile({
|
|
119
|
+
commandCount,
|
|
120
|
+
contentSize,
|
|
121
|
+
finishMs,
|
|
122
|
+
layoutMs,
|
|
123
|
+
measureCount,
|
|
124
|
+
measureMs,
|
|
125
|
+
renderMs,
|
|
126
|
+
rendererType,
|
|
127
|
+
scoreId,
|
|
128
|
+
systemCount,
|
|
129
|
+
viewport,
|
|
130
|
+
}: {
|
|
131
|
+
commandCount: number;
|
|
132
|
+
contentSize: { height: number; width: number };
|
|
133
|
+
finishMs: number;
|
|
134
|
+
layoutMs: number;
|
|
135
|
+
measureCount: number;
|
|
136
|
+
measureMs: number;
|
|
137
|
+
renderMs: number;
|
|
138
|
+
rendererType: RendererType;
|
|
139
|
+
scoreId: string;
|
|
140
|
+
systemCount: number;
|
|
141
|
+
viewport: RendererRect;
|
|
142
|
+
}) {
|
|
143
|
+
if (!isDevBuild()) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
console.info('[ScoreRenderer] recording profile', {
|
|
148
|
+
scoreId,
|
|
149
|
+
rendererType,
|
|
150
|
+
viewport,
|
|
151
|
+
contentSize,
|
|
152
|
+
measureCount,
|
|
153
|
+
systemCount,
|
|
154
|
+
commandCount,
|
|
155
|
+
measureMs: roundMs(measureMs),
|
|
156
|
+
layoutMs: roundMs(layoutMs),
|
|
157
|
+
renderMs: roundMs(renderMs),
|
|
158
|
+
finishMs: roundMs(finishMs),
|
|
159
|
+
totalMs: roundMs(measureMs + layoutMs + renderMs + finishMs),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function nowMs(): number {
|
|
164
|
+
return globalThis.performance?.now?.() ?? Date.now();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function roundMs(value: number): number {
|
|
168
|
+
return Math.round(value * 10) / 10;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isDevBuild(): boolean {
|
|
172
|
+
if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'test') {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return typeof __DEV__ === 'undefined' ? false : __DEV__;
|
|
177
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RendererSize,
|
|
3
|
+
RendererType,
|
|
4
|
+
Viewport,
|
|
5
|
+
VisibleViewport,
|
|
6
|
+
} from './types';
|
|
7
|
+
|
|
8
|
+
function clampOffset(
|
|
9
|
+
offset: number,
|
|
10
|
+
contentExtent: number,
|
|
11
|
+
viewportExtent: number
|
|
12
|
+
): number {
|
|
13
|
+
'worklet';
|
|
14
|
+
|
|
15
|
+
const maxOffset = Math.max(0, contentExtent - viewportExtent);
|
|
16
|
+
|
|
17
|
+
return Math.min(Math.max(offset, 0), maxOffset);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createVisibleViewport(
|
|
21
|
+
scrollOffset: number,
|
|
22
|
+
rendererType: RendererType,
|
|
23
|
+
viewport: Viewport,
|
|
24
|
+
contentSize: RendererSize
|
|
25
|
+
): VisibleViewport {
|
|
26
|
+
'worklet';
|
|
27
|
+
|
|
28
|
+
const offset =
|
|
29
|
+
rendererType === 'infiniteScore'
|
|
30
|
+
? {
|
|
31
|
+
x: clampOffset(scrollOffset, contentSize.width, viewport.width),
|
|
32
|
+
y: 0,
|
|
33
|
+
}
|
|
34
|
+
: {
|
|
35
|
+
x: 0,
|
|
36
|
+
y: clampOffset(scrollOffset, contentSize.height, viewport.height),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
x: offset.x,
|
|
41
|
+
y: offset.y,
|
|
42
|
+
width: viewport.width,
|
|
43
|
+
height: viewport.height,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export enum LogCategory {
|
|
4
|
+
FontManager = 'FontManager',
|
|
5
|
+
VexflowRecordingContext = 'VexflowRecordingContext',
|
|
6
|
+
VexflowCanvas = 'VexflowCanvas',
|
|
7
|
+
Default = 'Default',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const disabledCategories: Set<LogCategory> = new Set([
|
|
11
|
+
LogCategory.FontManager,
|
|
12
|
+
// LogCategory.VexflowRecordingContext,
|
|
13
|
+
// LogCategory.VexflowCanvas,
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
const categoryColors: Record<LogCategory, string> = Platform.select({
|
|
17
|
+
web: {
|
|
18
|
+
[LogCategory.FontManager]: '#1d56b3',
|
|
19
|
+
[LogCategory.VexflowRecordingContext]: 'green',
|
|
20
|
+
[LogCategory.VexflowCanvas]: 'cyan',
|
|
21
|
+
[LogCategory.Default]: 'black',
|
|
22
|
+
},
|
|
23
|
+
default: {
|
|
24
|
+
[LogCategory.FontManager]: '\x1b[34m', // Blue
|
|
25
|
+
[LogCategory.VexflowRecordingContext]: '\x1b[32m', // Green
|
|
26
|
+
[LogCategory.VexflowCanvas]: '\x1b[36m', // Cyan
|
|
27
|
+
[LogCategory.Default]: '\x1b[0m', // Reset
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
class Logger {
|
|
32
|
+
private logCategory: string;
|
|
33
|
+
|
|
34
|
+
constructor(category: LogCategory = LogCategory.Default) {
|
|
35
|
+
this.logCategory = category;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
extend(logCategory: LogCategory): Logger {
|
|
39
|
+
return new Logger(logCategory);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
formatMessage(...params: unknown[]) {
|
|
43
|
+
if (Platform.OS === 'web') {
|
|
44
|
+
const color = categoryColors[this.logCategory as LogCategory] || 'black';
|
|
45
|
+
|
|
46
|
+
return [
|
|
47
|
+
`%c${this.logCategory}`,
|
|
48
|
+
`background-color: ${color}; font-weight: bold;`,
|
|
49
|
+
...params,
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const colorCode =
|
|
54
|
+
categoryColors[this.logCategory as LogCategory] ||
|
|
55
|
+
categoryColors[LogCategory.Default];
|
|
56
|
+
|
|
57
|
+
return [`${colorCode}${this.logCategory}`, ...params, '\x1b[0m'];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
log(...params: unknown[]) {
|
|
61
|
+
if (disabledCategories.has(this.logCategory as LogCategory)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log(...this.formatMessage(...params));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
warn(...params: unknown[]) {
|
|
69
|
+
if (disabledCategories.has(this.logCategory as LogCategory)) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
console.warn(...this.formatMessage(...params));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
error(...params: unknown[]) {
|
|
77
|
+
if (disabledCategories.has(this.logCategory as LogCategory)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
console.error(...this.formatMessage(...params));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default new Logger();
|