vexflow-native 0.0.2 → 0.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.
Files changed (42) hide show
  1. package/lib/module/SkiaVexflowContext.js +285 -0
  2. package/lib/module/SkiaVexflowContext.js.map +1 -0
  3. package/lib/module/VexflowCanvas.js +101 -0
  4. package/lib/module/VexflowCanvas.js.map +1 -0
  5. package/lib/module/colorUtils.js +34 -0
  6. package/lib/module/colorUtils.js.map +1 -0
  7. package/lib/module/colorUtils.test.js +37 -0
  8. package/lib/module/colorUtils.test.js.map +1 -0
  9. package/lib/module/constants.js +30 -0
  10. package/lib/module/constants.js.map +1 -0
  11. package/lib/module/index.js +5 -3
  12. package/lib/module/index.js.map +1 -1
  13. package/lib/module/types.js +2 -0
  14. package/lib/module/types.js.map +1 -0
  15. package/lib/module/utils.js +79 -0
  16. package/lib/module/utils.js.map +1 -0
  17. package/lib/typescript/src/SkiaVexflowContext.d.ts +101 -0
  18. package/lib/typescript/src/SkiaVexflowContext.d.ts.map +1 -0
  19. package/lib/typescript/src/VexflowCanvas.d.ts +15 -0
  20. package/lib/typescript/src/VexflowCanvas.d.ts.map +1 -0
  21. package/lib/typescript/src/colorUtils.d.ts +2 -0
  22. package/lib/typescript/src/colorUtils.d.ts.map +1 -0
  23. package/lib/typescript/src/colorUtils.test.d.ts +2 -0
  24. package/lib/typescript/src/colorUtils.test.d.ts.map +1 -0
  25. package/lib/typescript/src/constants.d.ts +21 -0
  26. package/lib/typescript/src/constants.d.ts.map +1 -0
  27. package/lib/typescript/src/index.d.ts +6 -1
  28. package/lib/typescript/src/index.d.ts.map +1 -1
  29. package/lib/typescript/src/types.d.ts +7 -0
  30. package/lib/typescript/src/types.d.ts.map +1 -0
  31. package/lib/typescript/src/utils.d.ts +7 -0
  32. package/lib/typescript/src/utils.d.ts.map +1 -0
  33. package/package.json +7 -3
  34. package/src/SkiaVexflowContext.ts +462 -0
  35. package/src/VexflowCanvas.tsx +140 -0
  36. package/src/colorUtils.test.ts +52 -0
  37. package/src/colorUtils.ts +55 -0
  38. package/src/constants.tsx +34 -0
  39. package/src/index.ts +8 -0
  40. package/src/types.ts +7 -0
  41. package/src/utils.tsx +108 -0
  42. package/src/index.tsx +0 -3
@@ -0,0 +1,34 @@
1
+ import { type SkFont, StrokeCap, StrokeJoin } from '@shopify/react-native-skia';
2
+ import { type LineCap, type LineJoin } from './types';
3
+
4
+ export const PT_TO_PX = 4 / 3;
5
+
6
+ export const VEXFLOW_SCORE_COLORS = {
7
+ light: {
8
+ fill: '#111827',
9
+ stroke: '#111827',
10
+ },
11
+ dark: {
12
+ fill: '#F9FAFB',
13
+ stroke: '#F9FAFB',
14
+ },
15
+ } as const;
16
+
17
+ export const DEFAULT_FILL_STYLE = VEXFLOW_SCORE_COLORS.light.fill;
18
+ export const DEFAULT_STROKE_STYLE = VEXFLOW_SCORE_COLORS.light.stroke;
19
+ export const DEFAULT_LINE_WIDTH = 1;
20
+ export const DEFAULT_FONT = '12px Bravura';
21
+
22
+ export const LINE_CAP_MAP: Record<LineCap, StrokeCap> = {
23
+ butt: StrokeCap.Butt,
24
+ round: StrokeCap.Round,
25
+ square: StrokeCap.Square,
26
+ };
27
+
28
+ export const LINE_JOIN_MAP: Record<LineJoin, StrokeJoin> = {
29
+ miter: StrokeJoin.Miter,
30
+ round: StrokeJoin.Round,
31
+ bevel: StrokeJoin.Bevel,
32
+ };
33
+
34
+ export const fontCache = new Map<string, SkFont>();
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './constants';
2
+ export * from './types';
3
+ export * from './utils';
4
+
5
+ export { default as SkiaVexflowContext } from './SkiaVexflowContext';
6
+ export { default as VexflowCanvas } from './VexflowCanvas';
7
+
8
+ export type { VexflowCanvasDrawArgs } from './VexflowCanvas';
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export type LineCap = 'butt' | 'round' | 'square';
2
+ export type LineJoin = 'miter' | 'round' | 'bevel';
3
+
4
+ export type ParsedCssFont = {
5
+ sizePx: number;
6
+ family?: string;
7
+ };
package/src/utils.tsx ADDED
@@ -0,0 +1,108 @@
1
+ import { Skia, type SkFont } from '@shopify/react-native-skia';
2
+
3
+ import { fontCache, PT_TO_PX } from './constants';
4
+ import { type ParsedCssFont } from './types';
5
+
6
+ export function getAdvanceWidth(font: SkFont, text: string): number {
7
+ const glyphs = font.getGlyphIDs(text);
8
+ const widths = font.getGlyphWidths(glyphs);
9
+ return widths.reduce((sum, width) => sum + width, 0);
10
+ }
11
+
12
+ export function toPxFontSize(size: number | string | undefined): number {
13
+ if (size === undefined || size === null) return 12;
14
+
15
+ if (typeof size === 'number' && Number.isFinite(size)) {
16
+ return size * PT_TO_PX;
17
+ }
18
+
19
+ if (typeof size === 'string') {
20
+ const match = size.trim().match(/^(\d+(?:\.\d+)?)(px|pt)?$/i);
21
+
22
+ if (match) {
23
+ const value = Number.parseFloat(match[1] || '12');
24
+ const unit = match[2]?.toLowerCase();
25
+ if (!unit || unit === 'px') return value;
26
+ return value * PT_TO_PX;
27
+ }
28
+ }
29
+
30
+ return 12;
31
+ }
32
+
33
+ export function parseCssFontShorthand(cssFont: string): ParsedCssFont {
34
+ const sizeMatch = cssFont.match(/(\d+(?:\.\d+)?(?:px|pt))/i);
35
+
36
+ if (!sizeMatch) {
37
+ return { sizePx: 12 };
38
+ }
39
+
40
+ const sizePx = toPxFontSize(sizeMatch[1]);
41
+
42
+ const family = cssFont
43
+ .slice((sizeMatch.index ?? 0) + (sizeMatch[1]?.length || 0))
44
+ .trim();
45
+
46
+ return {
47
+ sizePx,
48
+ family: family || undefined,
49
+ };
50
+ }
51
+
52
+ export function createFont(
53
+ size: number,
54
+ familyName?: string,
55
+ bravuraFont?: SkFont | null
56
+ ): SkFont {
57
+ const fontSize = Number.isFinite(size) && size > 0 ? size : 12;
58
+ const family = familyName?.trim();
59
+
60
+ if (family?.toLowerCase().includes('bravura') && bravuraFont) {
61
+ const cacheKey = `Bravura-${fontSize}`;
62
+ const cached = fontCache.get(cacheKey);
63
+ if (cached) return cached;
64
+
65
+ const typeface = bravuraFont.getTypeface();
66
+ if (typeface) {
67
+ const font = Skia.Font(typeface, fontSize);
68
+ fontCache.set(cacheKey, font);
69
+ return font;
70
+ }
71
+ }
72
+
73
+ const fontMgr = Skia.FontMgr.System();
74
+ const requestedFamilies = family
75
+ ? family
76
+ .replace(/["']/g, '')
77
+ .split(',')
78
+ .map((part) => part.trim())
79
+ .filter(Boolean)
80
+ : [];
81
+
82
+ for (const requestedFamily of requestedFamilies) {
83
+ const typeface = fontMgr.matchFamilyStyle(requestedFamily, {
84
+ weight: 400,
85
+ width: 5,
86
+ slant: 0,
87
+ });
88
+
89
+ if (typeface) {
90
+ return Skia.Font(typeface, fontSize);
91
+ }
92
+ }
93
+
94
+ for (let i = 0; i < fontMgr.countFamilies(); i++) {
95
+ const systemFamily = fontMgr.getFamilyName(i);
96
+ const typeface = fontMgr.matchFamilyStyle(systemFamily, {
97
+ weight: 400,
98
+ width: 5,
99
+ slant: 0,
100
+ });
101
+
102
+ if (typeface) {
103
+ return Skia.Font(typeface, fontSize);
104
+ }
105
+ }
106
+
107
+ throw new Error('Unable to create a Skia font.');
108
+ }
package/src/index.tsx DELETED
@@ -1,3 +0,0 @@
1
- export function multiply(a: number, b: number): number {
2
- return a * b;
3
- }