vexflow-native 0.1.0 → 0.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
CHANGED
|
@@ -1,26 +1,77 @@
|
|
|
1
1
|
# vexflow-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Render VexFlow music notation in React Native using Skia.
|
|
4
|
+
|
|
5
|
+
## What This Library Provides
|
|
6
|
+
|
|
7
|
+
- `VexflowCanvas`: React component that creates a Skia canvas and gives you a VexFlow-compatible render context.
|
|
8
|
+
- `SkiaVexflowContext`: low-level context implementation (you usually do not need to use it directly).
|
|
4
9
|
|
|
5
10
|
## Installation
|
|
6
11
|
|
|
12
|
+
Install the library and its peer dependencies:
|
|
7
13
|
|
|
8
14
|
```sh
|
|
9
|
-
npm install vexflow-native
|
|
15
|
+
npm install vexflow-native vexflow @shopify/react-native-skia
|
|
10
16
|
```
|
|
11
17
|
|
|
18
|
+
## Basic Usage
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import React, { useCallback } from 'react';
|
|
22
|
+
import { View } from 'react-native';
|
|
23
|
+
import { Formatter, Stave, StaveNote, Voice } from 'vexflow';
|
|
24
|
+
import { VexflowCanvas, type VexflowCanvasDrawArgs } from 'vexflow-native';
|
|
25
|
+
|
|
26
|
+
export default function ScoreExample() {
|
|
27
|
+
const font = require('./assets/fonts/Bravura.otf');
|
|
28
|
+
|
|
29
|
+
const onDraw = useCallback(({ ctx, width }: VexflowCanvasDrawArgs) => {
|
|
30
|
+
const stave = new Stave(10, 40, width - 20);
|
|
31
|
+
stave.addClef('treble').addTimeSignature('4/4');
|
|
32
|
+
stave.setContext(ctx).draw();
|
|
33
|
+
|
|
34
|
+
const notes = [
|
|
35
|
+
new StaveNote({ clef: 'treble', keys: ['c/4'], duration: 'q' }),
|
|
36
|
+
new StaveNote({ clef: 'treble', keys: ['d/4'], duration: 'q' }),
|
|
37
|
+
new StaveNote({ clef: 'treble', keys: ['e/4'], duration: 'q' }),
|
|
38
|
+
new StaveNote({ clef: 'treble', keys: ['f/4'], duration: 'q' }),
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
const voice = new Voice({ num_beats: 4, beat_value: 4 });
|
|
42
|
+
voice.addTickables(notes);
|
|
43
|
+
|
|
44
|
+
new Formatter().joinVoices([voice]).format([voice], width - 80);
|
|
45
|
+
voice.draw(ctx, stave);
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<View style={{ padding: 16 }}>
|
|
50
|
+
<VexflowCanvas
|
|
51
|
+
onDraw={onDraw}
|
|
52
|
+
font={font}
|
|
53
|
+
height={180}
|
|
54
|
+
colorScheme="light"
|
|
55
|
+
/>
|
|
56
|
+
</View>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
12
60
|
|
|
13
|
-
##
|
|
61
|
+
## Component API
|
|
14
62
|
|
|
63
|
+
`VexflowCanvas` props:
|
|
15
64
|
|
|
16
|
-
|
|
17
|
-
|
|
65
|
+
- `onDraw: ({ ctx, width, height }) => void` (required)
|
|
66
|
+
- `font: require('.../Bravura.otf')` (required)
|
|
67
|
+
- `width?: number` (defaults to window width with internal margin)
|
|
68
|
+
- `height?: number` (default: `180`)
|
|
69
|
+
- `colorScheme?: 'light' | 'dark'` (default: `'light'`)
|
|
18
70
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const result = await multiply(3, 7);
|
|
22
|
-
```
|
|
71
|
+
## Notes
|
|
23
72
|
|
|
73
|
+
- `onDraw` is re-run when canvas size, color scheme, or callback identity changes.
|
|
74
|
+
- Keep `onDraw` wrapped in `useCallback` to avoid unnecessary redraws.
|
|
24
75
|
|
|
25
76
|
## Contributing
|
|
26
77
|
|
|
@@ -31,7 +82,3 @@ const result = await multiply(3, 7);
|
|
|
31
82
|
## License
|
|
32
83
|
|
|
33
84
|
MIT
|
|
34
|
-
|
|
35
|
-
---
|
|
36
|
-
|
|
37
|
-
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -9,8 +9,9 @@ import { VEXFLOW_SCORE_COLORS } from "./constants.js";
|
|
|
9
9
|
import { createFont, getAdvanceWidth, parseCssFontShorthand } from "./utils.js";
|
|
10
10
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
11
11
|
const CANVAS_HEIGHT = 180;
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
const fontName = 'vexflowFont';
|
|
13
|
+
function createTextMeasurementCanvas(skiaFont) {
|
|
14
|
+
let currentFont = createFont(12, fontName, skiaFont);
|
|
14
15
|
const measureContext = {
|
|
15
16
|
measureText: text => {
|
|
16
17
|
const rect = currentFont.measureText(text);
|
|
@@ -35,9 +36,9 @@ function createTextMeasurementCanvas(bravuraFont) {
|
|
|
35
36
|
set(value) {
|
|
36
37
|
try {
|
|
37
38
|
const parsed = parseCssFontShorthand(value);
|
|
38
|
-
currentFont = createFont(parsed.sizePx, parsed.family,
|
|
39
|
+
currentFont = createFont(parsed.sizePx, parsed.family, skiaFont);
|
|
39
40
|
} catch {
|
|
40
|
-
currentFont = createFont(12,
|
|
41
|
+
currentFont = createFont(12, fontName, skiaFont);
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
});
|
|
@@ -45,10 +46,10 @@ function createTextMeasurementCanvas(bravuraFont) {
|
|
|
45
46
|
getContext: type => type === '2d' ? measureContext : null
|
|
46
47
|
};
|
|
47
48
|
}
|
|
48
|
-
function createVexflowPicture(width, height, onDraw,
|
|
49
|
+
function createVexflowPicture(width, height, onDraw, skiaFont, scoreColors) {
|
|
49
50
|
const recorder = Skia.PictureRecorder();
|
|
50
51
|
const canvas = recorder.beginRecording(Skia.XYWHRect(0, 0, width, height));
|
|
51
|
-
const ctx = new SkiaVexflowContext(canvas,
|
|
52
|
+
const ctx = new SkiaVexflowContext(canvas, skiaFont, {
|
|
52
53
|
defaultFillStyle: scoreColors.fill,
|
|
53
54
|
defaultStrokeStyle: scoreColors.stroke
|
|
54
55
|
});
|
|
@@ -61,6 +62,7 @@ function createVexflowPicture(width, height, onDraw, bravuraFont, scoreColors) {
|
|
|
61
62
|
}
|
|
62
63
|
export default function VexflowCanvas({
|
|
63
64
|
onDraw,
|
|
65
|
+
font,
|
|
64
66
|
width,
|
|
65
67
|
height = CANVAS_HEIGHT,
|
|
66
68
|
colorScheme = 'light'
|
|
@@ -70,22 +72,22 @@ export default function VexflowCanvas({
|
|
|
70
72
|
} = useWindowDimensions();
|
|
71
73
|
const scoreColors = colorScheme === 'dark' ? VEXFLOW_SCORE_COLORS.dark : VEXFLOW_SCORE_COLORS.light;
|
|
72
74
|
const canvasWidth = width ?? Math.max(300, Math.floor(windowWidth) - 32);
|
|
73
|
-
const
|
|
75
|
+
const skiaFont = useFont(font, 30);
|
|
74
76
|
const pictureInfo = useMemo(() => {
|
|
75
|
-
if (!
|
|
77
|
+
if (!skiaFont) {
|
|
76
78
|
return null;
|
|
77
79
|
}
|
|
78
|
-
Element.setTextMeasurementCanvas(createTextMeasurementCanvas(
|
|
79
|
-
const picture = createVexflowPicture(canvasWidth, height, onDraw,
|
|
80
|
+
Element.setTextMeasurementCanvas(createTextMeasurementCanvas(skiaFont));
|
|
81
|
+
const picture = createVexflowPicture(canvasWidth, height, onDraw, skiaFont, scoreColors);
|
|
80
82
|
return {
|
|
81
83
|
picture,
|
|
82
84
|
width: canvasWidth,
|
|
83
85
|
height
|
|
84
86
|
};
|
|
85
|
-
}, [
|
|
87
|
+
}, [skiaFont, canvasWidth, height, onDraw, scoreColors]);
|
|
86
88
|
if (!pictureInfo) {
|
|
87
89
|
return /*#__PURE__*/_jsx(Text, {
|
|
88
|
-
children: "Loading
|
|
90
|
+
children: "Loading font..."
|
|
89
91
|
});
|
|
90
92
|
}
|
|
91
93
|
return /*#__PURE__*/_jsx(Canvas, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Canvas","Picture","Skia","useFont","useMemo","Element","Text","useWindowDimensions","SkiaVexflowContext","VEXFLOW_SCORE_COLORS","createFont","getAdvanceWidth","parseCssFontShorthand","jsx","_jsx","CANVAS_HEIGHT","createTextMeasurementCanvas","
|
|
1
|
+
{"version":3,"names":["Canvas","Picture","Skia","useFont","useMemo","Element","Text","useWindowDimensions","SkiaVexflowContext","VEXFLOW_SCORE_COLORS","createFont","getAdvanceWidth","parseCssFontShorthand","jsx","_jsx","CANVAS_HEIGHT","fontName","createTextMeasurementCanvas","skiaFont","currentFont","measureContext","measureText","text","rect","width","ascent","Math","max","y","descent","height","actualBoundingBoxAscent","actualBoundingBoxDescent","fontBoundingBoxAscent","fontBoundingBoxDescent","actualBoundingBoxLeft","x","actualBoundingBoxRight","Object","defineProperty","get","set","value","parsed","sizePx","family","getContext","type","createVexflowPicture","onDraw","scoreColors","recorder","PictureRecorder","canvas","beginRecording","XYWHRect","ctx","defaultFillStyle","fill","defaultStrokeStyle","stroke","finishRecordingAsPicture","VexflowCanvas","font","colorScheme","windowWidth","dark","light","canvasWidth","floor","pictureInfo","setTextMeasurementCanvas","picture","children","style"],"sourceRoot":"../../src","sources":["VexflowCanvas.tsx"],"mappings":";;AAAA,SACEA,MAAM,EACNC,OAAO,EACPC,IAAI,EACJC,OAAO,QAGF,4BAA4B;AACnC,SAASC,OAAO,QAAQ,OAAO;AAC/B,SAASC,OAAO,QAAQ,SAAS;AACjC,SAASC,IAAI,EAAEC,mBAAmB,QAAQ,cAAc;AAExD,OAAOC,kBAAkB,MAAM,yBAAsB;AACrD,SAASC,oBAAoB,QAAQ,gBAAa;AAClD,SAASC,UAAU,EAAEC,eAAe,EAAEC,qBAAqB,QAAQ,YAAS;AAAC,SAAAC,GAAA,IAAAC,IAAA;AAE7E,MAAMC,aAAa,GAAG,GAAG;AAgBzB,MAAMC,QAAQ,GAAG,aAAa;AAE9B,SAASC,2BAA2BA,CAACC,QAAuB,EAAE;EAC5D,IAAIC,WAAW,GAAGT,UAAU,CAAC,EAAE,EAAEM,QAAQ,EAAEE,QAAQ,CAAC;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2BAA2B,CAACC,QAAQ,CACtC,CAAC;IAED,MAAMsD,OAAO,GAAGxB,oBAAoB,CAClCoB,WAAW,EACXtC,MAAM,EACNmB,MAAM,EACN/B,QAAQ,EACRgC,WACF,CAAC;IAED,OAAO;MAAEsB,OAAO;MAAEhD,KAAK,EAAE4C,WAAW;MAAEtC;IAAO,CAAC;EAChD,CAAC,EAAE,CAACZ,QAAQ,EAAEkD,WAAW,EAAEtC,MAAM,EAAEmB,MAAM,EAAEC,WAAW,CAAC,CAAC;EAExD,IAAI,CAACoB,WAAW,EAAE;IAChB,oBAAOxD,IAAA,CAACR,IAAI;MAAAmE,QAAA,EAAC;IAAe,CAAM,CAAC;EACrC;EAEA,oBACE3D,IAAA,CAACd,MAAM;IAAC0E,KAAK,EAAE;MAAElD,KAAK,EAAE8C,WAAW,CAAC9C,KAAK;MAAEM,MAAM,EAAEwC,WAAW,CAACxC;IAAO,CAAE;IAAA2C,QAAA,eACtE3D,IAAA,CAACb,OAAO;MAACuE,OAAO,EAAEF,WAAW,CAACE;IAAQ,CAAE;EAAC,CACnC,CAAC;AAEb","ignoreList":[]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useFont } from '@shopify/react-native-skia';
|
|
1
2
|
import SkiaVexflowContext from './SkiaVexflowContext';
|
|
2
3
|
export type VexflowCanvasDrawArgs = {
|
|
3
4
|
ctx: SkiaVexflowContext;
|
|
@@ -6,10 +7,11 @@ export type VexflowCanvasDrawArgs = {
|
|
|
6
7
|
};
|
|
7
8
|
type VexflowCanvasProps = {
|
|
8
9
|
onDraw: (args: VexflowCanvasDrawArgs) => void;
|
|
10
|
+
font: Parameters<typeof useFont>[0];
|
|
9
11
|
width?: number;
|
|
10
12
|
height?: number;
|
|
11
13
|
colorScheme?: 'light' | 'dark';
|
|
12
14
|
};
|
|
13
|
-
export default function VexflowCanvas({ onDraw, width, height, colorScheme, }: VexflowCanvasProps): import("react/jsx-runtime").JSX.Element;
|
|
15
|
+
export default function VexflowCanvas({ onDraw, font, width, height, colorScheme, }: VexflowCanvasProps): import("react/jsx-runtime").JSX.Element;
|
|
14
16
|
export {};
|
|
15
17
|
//# sourceMappingURL=VexflowCanvas.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VexflowCanvas.d.ts","sourceRoot":"","sources":["../../../src/VexflowCanvas.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"VexflowCanvas.d.ts","sourceRoot":"","sources":["../../../src/VexflowCanvas.tsx"],"names":[],"mappings":"AAAA,OAAO,EAIL,OAAO,EAGR,MAAM,4BAA4B,CAAC;AAKpC,OAAO,kBAAkB,MAAM,sBAAsB,CAAC;AAMtD,MAAM,MAAM,qBAAqB,GAAG;IAClC,GAAG,EAAE,kBAAkB,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,KAAK,kBAAkB,GAAG;IACxB,MAAM,EAAE,CAAC,IAAI,EAAE,qBAAqB,KAAK,IAAI,CAAC;IAC9C,IAAI,EAAE,UAAU,CAAC,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;IACpC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;CAChC,CAAC;AAqEF,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,EACpC,MAAM,EACN,IAAI,EACJ,KAAK,EACL,MAAsB,EACtB,WAAqB,GACtB,EAAE,kBAAkB,2CAuCpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vexflow-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "React native backend for rendering music notation using @shopify/react-native-skiaact-native-skia",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
package/src/VexflowCanvas.tsx
CHANGED
|
@@ -24,13 +24,16 @@ export type VexflowCanvasDrawArgs = {
|
|
|
24
24
|
|
|
25
25
|
type VexflowCanvasProps = {
|
|
26
26
|
onDraw: (args: VexflowCanvasDrawArgs) => void;
|
|
27
|
+
font: Parameters<typeof useFont>[0];
|
|
27
28
|
width?: number;
|
|
28
29
|
height?: number;
|
|
29
30
|
colorScheme?: 'light' | 'dark';
|
|
30
31
|
};
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
const fontName = 'vexflowFont';
|
|
34
|
+
|
|
35
|
+
function createTextMeasurementCanvas(skiaFont: SkFont | null) {
|
|
36
|
+
let currentFont = createFont(12, fontName, skiaFont);
|
|
34
37
|
|
|
35
38
|
const measureContext = {
|
|
36
39
|
measureText: (text: string) => {
|
|
@@ -58,9 +61,9 @@ function createTextMeasurementCanvas(bravuraFont: SkFont | null) {
|
|
|
58
61
|
set(value: string) {
|
|
59
62
|
try {
|
|
60
63
|
const parsed = parseCssFontShorthand(value);
|
|
61
|
-
currentFont = createFont(parsed.sizePx, parsed.family,
|
|
64
|
+
currentFont = createFont(parsed.sizePx, parsed.family, skiaFont);
|
|
62
65
|
} catch {
|
|
63
|
-
currentFont = createFont(12,
|
|
66
|
+
currentFont = createFont(12, fontName, skiaFont);
|
|
64
67
|
}
|
|
65
68
|
},
|
|
66
69
|
});
|
|
@@ -75,7 +78,7 @@ function createVexflowPicture(
|
|
|
75
78
|
width: number,
|
|
76
79
|
height: number,
|
|
77
80
|
onDraw: (args: VexflowCanvasDrawArgs) => void,
|
|
78
|
-
|
|
81
|
+
skiaFont: SkFont | null,
|
|
79
82
|
scoreColors: {
|
|
80
83
|
fill: string;
|
|
81
84
|
stroke: string;
|
|
@@ -84,10 +87,11 @@ function createVexflowPicture(
|
|
|
84
87
|
const recorder = Skia.PictureRecorder();
|
|
85
88
|
const canvas = recorder.beginRecording(Skia.XYWHRect(0, 0, width, height));
|
|
86
89
|
|
|
87
|
-
const ctx = new SkiaVexflowContext(canvas,
|
|
90
|
+
const ctx = new SkiaVexflowContext(canvas, skiaFont, {
|
|
88
91
|
defaultFillStyle: scoreColors.fill,
|
|
89
92
|
defaultStrokeStyle: scoreColors.stroke,
|
|
90
93
|
});
|
|
94
|
+
|
|
91
95
|
onDraw({ ctx, width, height });
|
|
92
96
|
|
|
93
97
|
return recorder.finishRecordingAsPicture();
|
|
@@ -95,6 +99,7 @@ function createVexflowPicture(
|
|
|
95
99
|
|
|
96
100
|
export default function VexflowCanvas({
|
|
97
101
|
onDraw,
|
|
102
|
+
font,
|
|
98
103
|
width,
|
|
99
104
|
height = CANVAS_HEIGHT,
|
|
100
105
|
colorScheme = 'light',
|
|
@@ -106,30 +111,30 @@ export default function VexflowCanvas({
|
|
|
106
111
|
: VEXFLOW_SCORE_COLORS.light;
|
|
107
112
|
|
|
108
113
|
const canvasWidth = width ?? Math.max(300, Math.floor(windowWidth) - 32);
|
|
109
|
-
const
|
|
114
|
+
const skiaFont = useFont(font, 30);
|
|
110
115
|
|
|
111
116
|
const pictureInfo = useMemo(() => {
|
|
112
|
-
if (!
|
|
117
|
+
if (!skiaFont) {
|
|
113
118
|
return null;
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
Element.setTextMeasurementCanvas(
|
|
117
|
-
createTextMeasurementCanvas(
|
|
122
|
+
createTextMeasurementCanvas(skiaFont) as unknown as HTMLCanvasElement
|
|
118
123
|
);
|
|
119
124
|
|
|
120
125
|
const picture = createVexflowPicture(
|
|
121
126
|
canvasWidth,
|
|
122
127
|
height,
|
|
123
128
|
onDraw,
|
|
124
|
-
|
|
129
|
+
skiaFont,
|
|
125
130
|
scoreColors
|
|
126
131
|
);
|
|
127
132
|
|
|
128
133
|
return { picture, width: canvasWidth, height };
|
|
129
|
-
}, [
|
|
134
|
+
}, [skiaFont, canvasWidth, height, onDraw, scoreColors]);
|
|
130
135
|
|
|
131
136
|
if (!pictureInfo) {
|
|
132
|
-
return <Text>Loading
|
|
137
|
+
return <Text>Loading font...</Text>;
|
|
133
138
|
}
|
|
134
139
|
|
|
135
140
|
return (
|