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.
Files changed (259) hide show
  1. package/README.md +267 -37
  2. package/lib/module/base/FontManager.js +139 -0
  3. package/lib/module/base/FontManager.js.map +1 -0
  4. package/lib/module/base/TextMeasureContext.js +46 -0
  5. package/lib/module/base/TextMeasureContext.js.map +1 -0
  6. package/lib/module/base/VexflowCanvas.js +48 -0
  7. package/lib/module/base/VexflowCanvas.js.map +1 -0
  8. package/lib/module/base/VexflowColorScheme.js +16 -0
  9. package/lib/module/base/VexflowColorScheme.js.map +1 -0
  10. package/lib/module/base/VexflowRecordingContext.js +487 -0
  11. package/lib/module/base/VexflowRecordingContext.js.map +1 -0
  12. package/lib/module/base/VexflowRecordingReplay.js +214 -0
  13. package/lib/module/base/VexflowRecordingReplay.js.map +1 -0
  14. package/lib/module/base/VexflowRecordingTypes.js +4 -0
  15. package/lib/module/base/VexflowRecordingTypes.js.map +1 -0
  16. package/lib/module/base/index.js +9 -0
  17. package/lib/module/base/index.js.map +1 -0
  18. package/lib/module/base/setupVexflowReactNative.js +96 -0
  19. package/lib/module/base/setupVexflowReactNative.js.map +1 -0
  20. package/lib/module/base/types.js +4 -0
  21. package/lib/module/base/types.js.map +1 -0
  22. package/lib/module/base/utils.js +18 -0
  23. package/lib/module/base/utils.js.map +1 -0
  24. package/lib/module/musicxml/MusicXmlParser/InternalTypes.js +4 -0
  25. package/lib/module/musicxml/MusicXmlParser/InternalTypes.js.map +1 -0
  26. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +269 -0
  27. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -0
  28. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +62 -0
  29. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -0
  30. package/lib/module/musicxml/MusicXmlParser/MusicXmlTiming.js +124 -0
  31. package/lib/module/musicxml/MusicXmlParser/MusicXmlTiming.js.map +1 -0
  32. package/lib/module/musicxml/MusicXmlParser/PartParser.js +41 -0
  33. package/lib/module/musicxml/MusicXmlParser/PartParser.js.map +1 -0
  34. package/lib/module/musicxml/MusicXmlParser/Spanners.js +149 -0
  35. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -0
  36. package/lib/module/musicxml/MusicXmlParser/XmlOrder.js +106 -0
  37. package/lib/module/musicxml/MusicXmlParser/XmlOrder.js.map +1 -0
  38. package/lib/module/musicxml/MusicXmlParser/index.js +110 -0
  39. package/lib/module/musicxml/MusicXmlParser/index.js.map +1 -0
  40. package/lib/module/musicxml/MusicXmlParser/mappers.js +312 -0
  41. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -0
  42. package/lib/module/musicxml/MusicXmlParser/types.js +10 -0
  43. package/lib/module/musicxml/MusicXmlParser/types.js.map +1 -0
  44. package/lib/module/musicxml/index.js +4 -0
  45. package/lib/module/musicxml/index.js.map +1 -0
  46. package/lib/module/musicxml/testfiles/lg-8102429.xml +79620 -0
  47. package/lib/module/renderer/Layout/DocumentLayout.js +156 -0
  48. package/lib/module/renderer/Layout/DocumentLayout.js.map +1 -0
  49. package/lib/module/renderer/Layout/LayoutMetrics.js +13 -0
  50. package/lib/module/renderer/Layout/LayoutMetrics.js.map +1 -0
  51. package/lib/module/renderer/Layout/VerticalSpacing.js +69 -0
  52. package/lib/module/renderer/Layout/VerticalSpacing.js.map +1 -0
  53. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +277 -0
  54. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -0
  55. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js +108 -0
  56. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js.map +1 -0
  57. package/lib/module/renderer/ScoreRenderer/index.js +7 -0
  58. package/lib/module/renderer/ScoreRenderer/index.js.map +1 -0
  59. package/lib/module/renderer/ScoreRenderer/types.js +4 -0
  60. package/lib/module/renderer/ScoreRenderer/types.js.map +1 -0
  61. package/lib/module/renderer/ScoreRenderer/useScoreScroll.js +77 -0
  62. package/lib/module/renderer/ScoreRenderer/useScoreScroll.js.map +1 -0
  63. package/lib/module/renderer/colorScheme.js +17 -0
  64. package/lib/module/renderer/colorScheme.js.map +1 -0
  65. package/lib/module/renderer/constants.js +18 -0
  66. package/lib/module/renderer/constants.js.map +1 -0
  67. package/lib/module/renderer/index.js +6 -0
  68. package/lib/module/renderer/index.js.map +1 -0
  69. package/lib/module/renderer/layout.js +153 -0
  70. package/lib/module/renderer/layout.js.map +1 -0
  71. package/lib/module/renderer/measure.js +202 -0
  72. package/lib/module/renderer/measure.js.map +1 -0
  73. package/lib/module/renderer/render.js +184 -0
  74. package/lib/module/renderer/render.js.map +1 -0
  75. package/lib/module/renderer/scoreParsing.js +230 -0
  76. package/lib/module/renderer/scoreParsing.js.map +1 -0
  77. package/lib/module/renderer/types.js +4 -0
  78. package/lib/module/renderer/types.js.map +1 -0
  79. package/lib/module/renderer/useScoreRecording.js +112 -0
  80. package/lib/module/renderer/useScoreRecording.js.map +1 -0
  81. package/lib/module/renderer/viewport.js +26 -0
  82. package/lib/module/renderer/viewport.js.map +1 -0
  83. package/lib/module/shared/Logger.js +67 -0
  84. package/lib/module/shared/Logger.js.map +1 -0
  85. package/lib/module/state/index.js +4 -0
  86. package/lib/module/state/index.js.map +1 -0
  87. package/lib/module/state/types.js +2 -0
  88. package/lib/module/state/types.js.map +1 -0
  89. package/lib/module/types.js +7 -0
  90. package/lib/module/types.js.map +1 -1
  91. package/lib/module/utils.js +42 -77
  92. package/lib/module/utils.js.map +1 -1
  93. package/lib/typescript/src/base/FontManager.d.ts +23 -0
  94. package/lib/typescript/src/base/FontManager.d.ts.map +1 -0
  95. package/lib/typescript/src/base/TextMeasureContext.d.ts +22 -0
  96. package/lib/typescript/src/base/TextMeasureContext.d.ts.map +1 -0
  97. package/lib/typescript/src/base/VexflowCanvas.d.ts +5 -0
  98. package/lib/typescript/src/base/VexflowCanvas.d.ts.map +1 -0
  99. package/lib/typescript/src/base/VexflowColorScheme.d.ts +7 -0
  100. package/lib/typescript/src/base/VexflowColorScheme.d.ts.map +1 -0
  101. package/lib/typescript/src/base/VexflowRecordingContext.d.ts +103 -0
  102. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -0
  103. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +12 -0
  104. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts.map +1 -0
  105. package/lib/typescript/src/base/VexflowRecordingTypes.d.ts +146 -0
  106. package/lib/typescript/src/base/VexflowRecordingTypes.d.ts.map +1 -0
  107. package/lib/typescript/src/base/index.d.ts +7 -0
  108. package/lib/typescript/src/base/index.d.ts.map +1 -0
  109. package/lib/typescript/src/base/setupVexflowReactNative.d.ts +11 -0
  110. package/lib/typescript/src/base/setupVexflowReactNative.d.ts.map +1 -0
  111. package/lib/typescript/src/base/types.d.ts +13 -0
  112. package/lib/typescript/src/base/types.d.ts.map +1 -0
  113. package/lib/typescript/src/base/utils.d.ts +2 -0
  114. package/lib/typescript/src/base/utils.d.ts.map +1 -0
  115. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +30 -0
  116. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -0
  117. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts +4 -0
  118. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -0
  119. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +5 -0
  120. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -0
  121. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlTiming.d.ts +6 -0
  122. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlTiming.d.ts.map +1 -0
  123. package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts +8 -0
  124. package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts.map +1 -0
  125. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +4 -0
  126. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -0
  127. package/lib/typescript/src/musicxml/MusicXmlParser/XmlOrder.d.ts +20 -0
  128. package/lib/typescript/src/musicxml/MusicXmlParser/XmlOrder.d.ts.map +1 -0
  129. package/lib/typescript/src/musicxml/MusicXmlParser/index.d.ts +5 -0
  130. package/lib/typescript/src/musicxml/MusicXmlParser/index.d.ts.map +1 -0
  131. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts +15 -0
  132. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -0
  133. package/lib/typescript/src/musicxml/MusicXmlParser/types.d.ts +13 -0
  134. package/lib/typescript/src/musicxml/MusicXmlParser/types.d.ts.map +1 -0
  135. package/lib/typescript/src/musicxml/index.d.ts +2 -0
  136. package/lib/typescript/src/musicxml/index.d.ts.map +1 -0
  137. package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts +10 -0
  138. package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts.map +1 -0
  139. package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts +6 -0
  140. package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts.map +1 -0
  141. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts +9 -0
  142. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts.map +1 -0
  143. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts +15 -0
  144. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -0
  145. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts +24 -0
  146. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts.map +1 -0
  147. package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts +5 -0
  148. package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts.map +1 -0
  149. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +12 -0
  150. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -0
  151. package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts +13 -0
  152. package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts.map +1 -0
  153. package/lib/typescript/src/renderer/colorScheme.d.ts +9 -0
  154. package/lib/typescript/src/renderer/colorScheme.d.ts.map +1 -0
  155. package/lib/typescript/src/renderer/constants.d.ts +5 -0
  156. package/lib/typescript/src/renderer/constants.d.ts.map +1 -0
  157. package/lib/typescript/src/renderer/index.d.ts +4 -0
  158. package/lib/typescript/src/renderer/index.d.ts.map +1 -0
  159. package/lib/typescript/src/renderer/layout.d.ts +56 -0
  160. package/lib/typescript/src/renderer/layout.d.ts.map +1 -0
  161. package/lib/typescript/src/renderer/measure.d.ts +23 -0
  162. package/lib/typescript/src/renderer/measure.d.ts.map +1 -0
  163. package/lib/typescript/src/renderer/render.d.ts +9 -0
  164. package/lib/typescript/src/renderer/render.d.ts.map +1 -0
  165. package/lib/typescript/src/renderer/scoreParsing.d.ts +76 -0
  166. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -0
  167. package/lib/typescript/src/renderer/types.d.ts +56 -0
  168. package/lib/typescript/src/renderer/types.d.ts.map +1 -0
  169. package/lib/typescript/src/renderer/useScoreRecording.d.ts +21 -0
  170. package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -0
  171. package/lib/typescript/src/renderer/viewport.d.ts +3 -0
  172. package/lib/typescript/src/renderer/viewport.d.ts.map +1 -0
  173. package/lib/typescript/src/shared/Logger.d.ts +18 -0
  174. package/lib/typescript/src/shared/Logger.d.ts.map +1 -0
  175. package/lib/typescript/src/state/index.d.ts +2 -0
  176. package/lib/typescript/src/state/index.d.ts.map +1 -0
  177. package/lib/typescript/src/state/types.d.ts +190 -0
  178. package/lib/typescript/src/state/types.d.ts.map +1 -0
  179. package/lib/typescript/src/types.d.ts +0 -6
  180. package/lib/typescript/src/types.d.ts.map +1 -1
  181. package/lib/typescript/src/utils.d.ts +0 -6
  182. package/lib/typescript/src/utils.d.ts.map +1 -1
  183. package/package.json +41 -14
  184. package/src/base/FontManager.ts +206 -0
  185. package/src/base/TextMeasureContext.ts +59 -0
  186. package/src/base/VexflowCanvas.tsx +60 -0
  187. package/src/base/VexflowColorScheme.ts +32 -0
  188. package/src/base/VexflowRecordingContext.ts +545 -0
  189. package/src/base/VexflowRecordingReplay.ts +327 -0
  190. package/src/base/VexflowRecordingTypes.ts +169 -0
  191. package/src/base/index.ts +7 -0
  192. package/src/base/setupVexflowReactNative.ts +124 -0
  193. package/src/base/types.ts +17 -0
  194. package/src/base/utils.ts +21 -0
  195. package/src/musicxml/MusicXmlParser/InternalTypes.ts +41 -0
  196. package/src/musicxml/MusicXmlParser/MeasureParser.ts +469 -0
  197. package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +84 -0
  198. package/src/musicxml/MusicXmlParser/MusicXmlTiming.ts +153 -0
  199. package/src/musicxml/MusicXmlParser/PartParser.ts +69 -0
  200. package/src/musicxml/MusicXmlParser/Spanners.ts +215 -0
  201. package/src/musicxml/MusicXmlParser/XmlOrder.ts +170 -0
  202. package/src/musicxml/MusicXmlParser/index.ts +144 -0
  203. package/src/musicxml/MusicXmlParser/mappers.ts +307 -0
  204. package/src/musicxml/MusicXmlParser/types.ts +24 -0
  205. package/src/musicxml/index.ts +5 -0
  206. package/src/musicxml/testfiles/lg-8102429.xml +79620 -0
  207. package/src/renderer/Layout/DocumentLayout.ts +240 -0
  208. package/src/renderer/Layout/LayoutMetrics.ts +21 -0
  209. package/src/renderer/Layout/VerticalSpacing.ts +122 -0
  210. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +384 -0
  211. package/src/renderer/ScoreRenderer/ScoreScrollbar.tsx +231 -0
  212. package/src/renderer/ScoreRenderer/index.ts +13 -0
  213. package/src/renderer/ScoreRenderer/types.ts +14 -0
  214. package/src/renderer/ScoreRenderer/useScoreScroll.ts +146 -0
  215. package/src/renderer/colorScheme.ts +28 -0
  216. package/src/renderer/constants.ts +19 -0
  217. package/src/renderer/index.ts +3 -0
  218. package/src/renderer/layout.ts +309 -0
  219. package/src/renderer/measure.tsx +304 -0
  220. package/src/renderer/render.tsx +308 -0
  221. package/src/renderer/scoreParsing.tsx +352 -0
  222. package/src/renderer/types.ts +65 -0
  223. package/src/renderer/useScoreRecording.ts +177 -0
  224. package/src/renderer/viewport.ts +45 -0
  225. package/src/shared/Logger.ts +85 -0
  226. package/src/state/index.ts +1 -0
  227. package/src/state/types.ts +295 -0
  228. package/src/types.ts +6 -6
  229. package/src/utils.tsx +32 -98
  230. package/lib/module/SkiaVexflowContext.js +0 -285
  231. package/lib/module/SkiaVexflowContext.js.map +0 -1
  232. package/lib/module/VexflowCanvas.js +0 -103
  233. package/lib/module/VexflowCanvas.js.map +0 -1
  234. package/lib/module/colorUtils.js +0 -34
  235. package/lib/module/colorUtils.js.map +0 -1
  236. package/lib/module/colorUtils.test.js +0 -37
  237. package/lib/module/colorUtils.test.js.map +0 -1
  238. package/lib/module/constants.js +0 -30
  239. package/lib/module/constants.js.map +0 -1
  240. package/lib/module/index.js +0 -8
  241. package/lib/module/index.js.map +0 -1
  242. package/lib/typescript/src/SkiaVexflowContext.d.ts +0 -101
  243. package/lib/typescript/src/SkiaVexflowContext.d.ts.map +0 -1
  244. package/lib/typescript/src/VexflowCanvas.d.ts +0 -17
  245. package/lib/typescript/src/VexflowCanvas.d.ts.map +0 -1
  246. package/lib/typescript/src/colorUtils.d.ts +0 -2
  247. package/lib/typescript/src/colorUtils.d.ts.map +0 -1
  248. package/lib/typescript/src/colorUtils.test.d.ts +0 -2
  249. package/lib/typescript/src/colorUtils.test.d.ts.map +0 -1
  250. package/lib/typescript/src/constants.d.ts +0 -21
  251. package/lib/typescript/src/constants.d.ts.map +0 -1
  252. package/lib/typescript/src/index.d.ts +0 -7
  253. package/lib/typescript/src/index.d.ts.map +0 -1
  254. package/src/SkiaVexflowContext.ts +0 -462
  255. package/src/VexflowCanvas.tsx +0 -145
  256. package/src/colorUtils.test.ts +0 -52
  257. package/src/colorUtils.ts +0 -55
  258. package/src/constants.tsx +0 -34
  259. package/src/index.ts +0 -8
@@ -0,0 +1,59 @@
1
+ import type { SkFont } from '@shopify/react-native-skia';
2
+ import { Font } from 'vexflow';
3
+ import type VexflowRecordingContext from './VexflowRecordingContext';
4
+
5
+ class TextMeasureContext {
6
+ __workletClass = true;
7
+
8
+ private context: VexflowRecordingContext;
9
+ private currentFont = '';
10
+
11
+ constructor(context: VexflowRecordingContext) {
12
+ this.context = context;
13
+ }
14
+
15
+ measureText(text: string) {
16
+ const currentFont = this.context.getCurrentSkFont();
17
+ const rect = currentFont.measureText(text);
18
+
19
+ const width = getAdvanceWidth(currentFont, text);
20
+ const ascent = Math.max(0, -rect.y);
21
+ const descent = Math.max(0, rect.y + rect.height);
22
+
23
+ return {
24
+ width,
25
+ actualBoundingBoxAscent: ascent,
26
+ actualBoundingBoxDescent: descent,
27
+ fontBoundingBoxAscent: ascent,
28
+ fontBoundingBoxDescent: descent,
29
+ actualBoundingBoxLeft: rect.x,
30
+ actualBoundingBoxRight: rect.x + rect.width,
31
+ };
32
+ }
33
+
34
+ get() {
35
+ return this.currentFont;
36
+ }
37
+
38
+ set(value: string) {
39
+ this.currentFont = value;
40
+ this.context.setFont(Font.fromCSSString(value));
41
+ }
42
+
43
+ get font() {
44
+ return this.get();
45
+ }
46
+
47
+ set font(value: string) {
48
+ this.set(value);
49
+ }
50
+ }
51
+
52
+ export default TextMeasureContext;
53
+
54
+ function getAdvanceWidth(font: SkFont, text: string): number {
55
+ const glyphs = font.getGlyphIDs(text);
56
+ const widths = font.getGlyphWidths(glyphs);
57
+
58
+ return widths.reduce((sum, width) => sum + width, 0);
59
+ }
@@ -0,0 +1,60 @@
1
+ import {
2
+ Canvas,
3
+ Picture,
4
+ Skia,
5
+ useCanvasRef,
6
+ useCanvasSize,
7
+ } from '@shopify/react-native-skia';
8
+ import React, { useMemo } from 'react';
9
+ import { StyleSheet } from 'react-native';
10
+
11
+ import type { VexflowCanvasProps } from './types';
12
+ import VexflowRecordingContext from './VexflowRecordingContext';
13
+ import { renderVexflowRecordingCommands } from './VexflowRecordingReplay';
14
+
15
+ const VexflowCanvas: React.FC<VexflowCanvasProps> = (props) => {
16
+ const {
17
+ onDraw,
18
+ fontManager,
19
+ defaultFont,
20
+ // width: propWidth,
21
+ // height: propHeight,
22
+ // colorScheme = 'light',
23
+ } = props;
24
+
25
+ const canvasRef = useCanvasRef();
26
+ const { size: canvasSize } = useCanvasSize(canvasRef);
27
+
28
+ const picture = useMemo(() => {
29
+ const recorder = Skia.PictureRecorder();
30
+ const canvas = recorder.beginRecording(
31
+ Skia.XYWHRect(0, 0, canvasSize.width, canvasSize.height)
32
+ );
33
+
34
+ const ctx = new VexflowRecordingContext(fontManager, defaultFont);
35
+
36
+ onDraw({ ctx, width: canvasSize.width, height: canvasSize.height });
37
+ renderVexflowRecordingCommands(
38
+ canvas,
39
+ ctx.finish(),
40
+ fontManager,
41
+ defaultFont
42
+ );
43
+
44
+ return recorder.finishRecordingAsPicture();
45
+ }, [onDraw, canvasSize, fontManager, defaultFont]);
46
+
47
+ return (
48
+ <Canvas style={styles.canvas} ref={canvasRef}>
49
+ <Picture picture={picture} />
50
+ </Canvas>
51
+ );
52
+ };
53
+
54
+ export default VexflowCanvas;
55
+
56
+ const styles = StyleSheet.create({
57
+ canvas: {
58
+ flex: 1, // TMP?
59
+ },
60
+ });
@@ -0,0 +1,32 @@
1
+ import { parseStyleToColor } from './utils';
2
+
3
+ export interface VexflowColorScheme {
4
+ foreground?: string;
5
+ background?: string;
6
+ ledgerLine?: string;
7
+ }
8
+
9
+ const FOREGROUND_COLOR_ALIASES = new Set(['black', '#000', '#000000']);
10
+ const LEDGER_LINE_COLOR_ALIASES = new Set(['#444', '#444444']);
11
+
12
+ export function resolveVexflowColor(
13
+ style: string,
14
+ colorScheme?: VexflowColorScheme
15
+ ): string {
16
+ const normalizedStyle = style.trim().toLowerCase();
17
+
18
+ if (
19
+ colorScheme?.foreground &&
20
+ FOREGROUND_COLOR_ALIASES.has(normalizedStyle)
21
+ ) {
22
+ return parseStyleToColor(colorScheme.foreground);
23
+ }
24
+
25
+ if (LEDGER_LINE_COLOR_ALIASES.has(normalizedStyle)) {
26
+ return parseStyleToColor(
27
+ colorScheme?.ledgerLine ?? colorScheme?.foreground ?? style
28
+ );
29
+ }
30
+
31
+ return parseStyleToColor(style);
32
+ }
@@ -0,0 +1,545 @@
1
+ import {
2
+ type SkFont,
3
+ type SkTypefaceFontProvider,
4
+ } from '@shopify/react-native-skia';
5
+ import { Platform } from 'react-native';
6
+ import { Element, RenderContext as VexflowRenderContext } from 'vexflow';
7
+ import type { FontInfo } from 'vexflow';
8
+
9
+ import FontManager from './FontManager';
10
+ import TextMeasureContext from './TextMeasureContext';
11
+ import { installVexflowReactNativeFallbacks } from './setupVexflowReactNative';
12
+ import {
13
+ resolveVexflowColor,
14
+ type VexflowColorScheme,
15
+ } from './VexflowColorScheme';
16
+ import type {
17
+ VexflowRecordingCommand,
18
+ VexflowRecordingFont,
19
+ VexflowRecordingLineCap,
20
+ VexflowRecordingPaint,
21
+ VexflowRecordingPathCommand,
22
+ VexflowRecordingRect,
23
+ } from './VexflowRecordingTypes';
24
+
25
+ const showSilentLogs = false;
26
+
27
+ const WidthScale = 3 / 4;
28
+
29
+ function logUnimplemented(methodName: string, isSilent = false) {
30
+ if (isSilent && !showSilentLogs) {
31
+ return;
32
+ }
33
+
34
+ console.log(
35
+ `VexflowRecordingContext: Method "${methodName}" is not implemented yet.`
36
+ );
37
+ }
38
+
39
+ export default class VexflowRecordingContext implements VexflowRenderContext {
40
+ __workletClass = true;
41
+
42
+ private commands: VexflowRecordingCommand[] = [];
43
+ private colorGroupId?: string;
44
+ private currentPath?: VexflowRecordingPathCommand[];
45
+ private fillPaint: VexflowRecordingPaint = {
46
+ color: '#000000',
47
+ };
48
+ private strokePaint: VexflowRecordingPaint = {
49
+ color: '#000000',
50
+ strokeCap: 'butt',
51
+ strokeWidth: 1.5 * WidthScale,
52
+ };
53
+ private textFont: SkFont;
54
+ private textFontDescriptor: VexflowRecordingFont;
55
+ private fontManager: FontManager;
56
+ private textMeasurementContext: TextMeasureContext;
57
+ private isFinished = false;
58
+ private colorScheme?: VexflowColorScheme;
59
+
60
+ constructor(
61
+ fontProvider: SkTypefaceFontProvider,
62
+ defaultFont: string,
63
+ colorScheme?: VexflowColorScheme
64
+ ) {
65
+ this.colorScheme = colorScheme;
66
+ this.fillPaint.color = resolveVexflowColor('black', colorScheme);
67
+ this.strokePaint.color = resolveVexflowColor('black', colorScheme);
68
+ this.fontManager = new FontManager(fontProvider, defaultFont);
69
+ this.textFontDescriptor = { font: defaultFont };
70
+ this.textFont = this.fontManager.createSkFont(defaultFont);
71
+
72
+ if (Platform.OS === 'ios' || Platform.OS === 'android') {
73
+ installVexflowReactNativeFallbacks();
74
+ this.textMeasurementContext = new TextMeasureContext(this);
75
+
76
+ Element.setTextMeasurementCanvas({
77
+ getContext: (type: string) => {
78
+ if (type === '2d') {
79
+ return this.textMeasurementContext;
80
+ }
81
+
82
+ return null;
83
+ },
84
+ } as unknown as HTMLCanvasElement);
85
+ } else {
86
+ const measurementCanvas = Element.getTextMeasurementCanvas();
87
+ this.textMeasurementContext = measurementCanvas?.getContext(
88
+ '2d' as unknown as 'webgpu'
89
+ ) as unknown as TextMeasureContext;
90
+ }
91
+ }
92
+
93
+ getCurrentSkFont() {
94
+ return this.textFont;
95
+ }
96
+
97
+ getMeasurementContext() {
98
+ return this.textMeasurementContext;
99
+ }
100
+
101
+ finish(): readonly VexflowRecordingCommand[] {
102
+ this.isFinished = true;
103
+ return this.commands.map((command) => cloneCommand(command));
104
+ }
105
+
106
+ clear() {
107
+ this.pushCommand({
108
+ type: 'clear',
109
+ color: 'transparent',
110
+ });
111
+ return this;
112
+ }
113
+
114
+ setFillStyle(style: string | unknown) {
115
+ if (typeof style === 'string') {
116
+ this.fillPaint = {
117
+ ...this.fillPaint,
118
+ color: resolveVexflowColor(style, this.colorScheme),
119
+ };
120
+ return this;
121
+ }
122
+
123
+ logUnimplemented('setFillStyle (gradient or pattern)');
124
+ return this;
125
+ }
126
+
127
+ setBackgroundFillStyle(_style: string) {
128
+ logUnimplemented('setBackgroundFillStyle');
129
+ return this;
130
+ }
131
+
132
+ setStrokeStyle(style: string) {
133
+ this.strokePaint = {
134
+ ...this.strokePaint,
135
+ color: resolveVexflowColor(style, this.colorScheme),
136
+ };
137
+ return this;
138
+ }
139
+
140
+ /**
141
+ * Record a glow/shadow colour (CSS `shadowColor`) into both the fill and the
142
+ * stroke paint — VexFlow's `ElementStyle.shadowColor` is applied as ctx-level
143
+ * state that affects whatever is drawn next, fill or stroke. Replayed in Skia
144
+ * as a drop-shadow image filter (a glow around the ink) and overridable per
145
+ * group at replay.
146
+ */
147
+ setShadowColor(color: string): this {
148
+ const resolved = resolveVexflowColor(color, this.colorScheme);
149
+ this.fillPaint = { ...this.fillPaint, shadowColor: resolved };
150
+ this.strokePaint = { ...this.strokePaint, shadowColor: resolved };
151
+ return this;
152
+ }
153
+
154
+ /**
155
+ * Record a glow/shadow blur radius (CSS `shadowBlur`) into both the fill and
156
+ * the stroke paint. Mapped to a Gaussian sigma at replay; only takes effect
157
+ * when a `shadowColor` is also set.
158
+ */
159
+ setShadowBlur(blur: number): this {
160
+ this.fillPaint = { ...this.fillPaint, shadowBlur: blur };
161
+ this.strokePaint = { ...this.strokePaint, shadowBlur: blur };
162
+ return this;
163
+ }
164
+
165
+ setLineWidth(width: number) {
166
+ this.strokePaint = {
167
+ ...this.strokePaint,
168
+ strokeWidth: width * WidthScale,
169
+ };
170
+ return this;
171
+ }
172
+
173
+ setLineCap(cap: string) {
174
+ this.strokePaint = {
175
+ ...this.strokePaint,
176
+ strokeCap: normalizeLineCap(cap),
177
+ };
178
+ return this;
179
+ }
180
+
181
+ /**
182
+ * Record a dash pattern (CSS `setLineDash`) into the stroke paint — dashing is
183
+ * a stroke-only concept (stems, ledger lines, …). Replayed in Skia as a dash
184
+ * path effect and overridable per group at replay. An empty array clears any
185
+ * recorded dash (CSS semantics: solid line).
186
+ */
187
+ setLineDash(dashPattern: number[]): this {
188
+ this.strokePaint = {
189
+ ...this.strokePaint,
190
+ lineDash: dashPattern.length > 0 ? [...dashPattern] : undefined,
191
+ };
192
+ return this;
193
+ }
194
+
195
+ scale(x: number, y: number) {
196
+ this.pushCommand({ type: 'scale', x, y });
197
+ return this;
198
+ }
199
+
200
+ translate(x: number, y: number) {
201
+ this.pushCommand({ type: 'translate', x, y });
202
+ return this;
203
+ }
204
+
205
+ resize(_width: number, _height: number) {
206
+ logUnimplemented('resize');
207
+ return this;
208
+ }
209
+
210
+ rect(x: number, y: number, width: number, height: number) {
211
+ this.currentPath?.push({
212
+ type: 'addRect',
213
+ rect: makeRect(x, y, width, height),
214
+ });
215
+ return this;
216
+ }
217
+
218
+ fillRect(x: number, y: number, width: number, height: number) {
219
+ this.pushCommand({
220
+ type: 'fillRect',
221
+ rect: makeRect(x, y, width, height),
222
+ paint: clonePaint(this.fillPaint),
223
+ });
224
+ return this;
225
+ }
226
+
227
+ clearRect(x: number, y: number, width: number, height: number) {
228
+ this.pushCommand({
229
+ type: 'clearRect',
230
+ rect: makeRect(x, y, width, height),
231
+ });
232
+ return this;
233
+ }
234
+
235
+ clipRect(x: number, y: number, width: number, height: number) {
236
+ this.pushCommand({
237
+ type: 'clipRect',
238
+ rect: makeRect(x, y, width, height),
239
+ });
240
+ return this;
241
+ }
242
+
243
+ pointerRect(_x: number, _y: number, _width: number, _height: number) {
244
+ logUnimplemented('pointerRect', true);
245
+ return this;
246
+ }
247
+
248
+ beginPath() {
249
+ this.currentPath = [];
250
+ return this;
251
+ }
252
+
253
+ moveTo(x: number, y: number) {
254
+ this.currentPath?.push({ type: 'moveTo', x, y });
255
+ return this;
256
+ }
257
+
258
+ lineTo(x: number, y: number) {
259
+ this.currentPath?.push({ type: 'lineTo', x, y });
260
+ return this;
261
+ }
262
+
263
+ bezierCurveTo(
264
+ cp1x: number,
265
+ cp1y: number,
266
+ cp2x: number,
267
+ cp2y: number,
268
+ x: number,
269
+ y: number
270
+ ) {
271
+ this.currentPath?.push({
272
+ type: 'cubicTo',
273
+ cp1x,
274
+ cp1y,
275
+ cp2x,
276
+ cp2y,
277
+ x,
278
+ y,
279
+ });
280
+ return this;
281
+ }
282
+
283
+ quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
284
+ this.currentPath?.push({ type: 'quadTo', cpx, cpy, x, y });
285
+ return this;
286
+ }
287
+
288
+ arc(
289
+ x: number,
290
+ y: number,
291
+ radius: number,
292
+ startAngle: number,
293
+ endAngle: number,
294
+ anticlockwise: boolean
295
+ ) {
296
+ const startDeg = (startAngle * 180) / Math.PI;
297
+ const endDeg = (endAngle * 180) / Math.PI;
298
+ const sweepDeg = anticlockwise ? startDeg - endDeg : endDeg - startDeg;
299
+
300
+ this.currentPath?.push({
301
+ type: 'addArc',
302
+ rect: makeRect(x - radius, y - radius, radius * 2, radius * 2),
303
+ startDegrees: startDeg,
304
+ sweepDegrees: sweepDeg,
305
+ });
306
+ return this;
307
+ }
308
+
309
+ fill(_attributes?: unknown) {
310
+ this.pushCommand({
311
+ type: 'fillPath',
312
+ path: this.cloneCurrentPath(),
313
+ paint: clonePaint(this.fillPaint),
314
+ });
315
+ return this;
316
+ }
317
+
318
+ stroke() {
319
+ this.pushCommand({
320
+ type: 'strokePath',
321
+ path: this.cloneCurrentPath(),
322
+ paint: clonePaint(this.strokePaint),
323
+ });
324
+ return this;
325
+ }
326
+
327
+ closePath() {
328
+ this.currentPath?.push({ type: 'close' });
329
+ return this;
330
+ }
331
+
332
+ fillText(text: string, x: number, y: number) {
333
+ this.pushCommand({
334
+ type: 'fillText',
335
+ text,
336
+ x,
337
+ y,
338
+ paint: clonePaint(this.fillPaint),
339
+ font: cloneFont(this.textFontDescriptor),
340
+ });
341
+ return this;
342
+ }
343
+
344
+ save(): this {
345
+ this.pushCommand({ type: 'save' });
346
+ return this;
347
+ }
348
+
349
+ restore(): this {
350
+ this.pushCommand({ type: 'restore' });
351
+ return this;
352
+ }
353
+
354
+ openGroup(_cls?: string, _id?: string) {
355
+ logUnimplemented('openGroup', true);
356
+ return this;
357
+ }
358
+
359
+ closeGroup() {
360
+ logUnimplemented('closeGroup', true);
361
+ return this;
362
+ }
363
+
364
+ /**
365
+ * Open a colour group. Every command recorded until `endColorGroup()` is
366
+ * stamped with `id` as its `groupId`, so a replay can recolor the whole group
367
+ * from a `groupId -> color` override map without re-recording. This is a
368
+ * dedicated, explicit grouping — distinct from VexFlow's
369
+ * `openGroup`/`closeGroup` (above, no-ops here), which a real score nests per
370
+ * element. Returns `this` for chaining.
371
+ */
372
+ beginColorGroup(id: string) {
373
+ this.colorGroupId = id;
374
+ return this;
375
+ }
376
+
377
+ endColorGroup() {
378
+ this.colorGroupId = undefined;
379
+ return this;
380
+ }
381
+
382
+ openRotation(_angleDegrees: number, _x: number, _y: number) {
383
+ logUnimplemented('openRotation');
384
+ return this;
385
+ }
386
+
387
+ closeRotation() {
388
+ logUnimplemented('closeRotation');
389
+ return this;
390
+ }
391
+
392
+ add(_child: unknown) {
393
+ logUnimplemented('add');
394
+ return this;
395
+ }
396
+
397
+ measureText(_text: string) {
398
+ logUnimplemented('measureText');
399
+ return { x: 0, y: 0, width: 0, height: 0 };
400
+ }
401
+
402
+ set fillStyle(style: string | unknown) {
403
+ this.setFillStyle(style);
404
+ }
405
+
406
+ get fillStyle() {
407
+ logUnimplemented('get fillStyle');
408
+ return '';
409
+ }
410
+
411
+ set strokeStyle(value: string) {
412
+ this.setStrokeStyle(value);
413
+ }
414
+
415
+ get strokeStyle() {
416
+ logUnimplemented('get strokeStyle');
417
+ return '';
418
+ }
419
+
420
+ setFont(
421
+ font?: string | FontInfo,
422
+ size?: string | number,
423
+ weight?: string | number,
424
+ style?: string
425
+ ) {
426
+ this.textFontDescriptor = { font, size, weight, style };
427
+ this.textFont = this.fontManager.createSkFont(font, size, weight, style);
428
+ return this;
429
+ }
430
+
431
+ getFont() {
432
+ logUnimplemented('getFont');
433
+ return '';
434
+ }
435
+
436
+ set font(value: string) {
437
+ this.setFont(value);
438
+ }
439
+
440
+ get font() {
441
+ logUnimplemented('get font');
442
+ return '';
443
+ }
444
+
445
+ private cloneCurrentPath(): VexflowRecordingPathCommand[] {
446
+ if (!this.currentPath) {
447
+ this.currentPath = [];
448
+ }
449
+
450
+ return this.currentPath.map((command) => clonePathCommand(command));
451
+ }
452
+
453
+ private pushCommand(command: VexflowRecordingCommand) {
454
+ if (this.isFinished) {
455
+ throw new Error(
456
+ 'VexflowRecordingContext: Cannot record commands after finish() has been called.'
457
+ );
458
+ }
459
+
460
+ if (this.colorGroupId != null) {
461
+ command.groupId = this.colorGroupId;
462
+ }
463
+
464
+ this.commands.push(command);
465
+ }
466
+ }
467
+
468
+ function makeRect(
469
+ x: number,
470
+ y: number,
471
+ width: number,
472
+ height: number
473
+ ): VexflowRecordingRect {
474
+ return { x, y, width, height };
475
+ }
476
+
477
+ function normalizeLineCap(cap: string): VexflowRecordingLineCap {
478
+ if (cap === 'round' || cap === 'square') {
479
+ return cap;
480
+ }
481
+
482
+ return 'butt';
483
+ }
484
+
485
+ function clonePaint(paint: VexflowRecordingPaint): VexflowRecordingPaint {
486
+ return {
487
+ ...paint,
488
+ ...(paint.lineDash ? { lineDash: [...paint.lineDash] } : null),
489
+ };
490
+ }
491
+
492
+ function cloneFont(font: VexflowRecordingFont): VexflowRecordingFont {
493
+ return {
494
+ ...font,
495
+ font:
496
+ typeof font.font === 'object' && font.font ? { ...font.font } : font.font,
497
+ };
498
+ }
499
+
500
+ function clonePathCommand(
501
+ command: VexflowRecordingPathCommand
502
+ ): VexflowRecordingPathCommand {
503
+ if (command.type === 'addRect' || command.type === 'addArc') {
504
+ return {
505
+ ...command,
506
+ rect: { ...command.rect },
507
+ };
508
+ }
509
+
510
+ return { ...command };
511
+ }
512
+
513
+ function cloneCommand(
514
+ command: VexflowRecordingCommand
515
+ ): VexflowRecordingCommand {
516
+ switch (command.type) {
517
+ case 'fillRect':
518
+ return {
519
+ ...command,
520
+ rect: { ...command.rect },
521
+ paint: clonePaint(command.paint),
522
+ };
523
+ case 'fillPath':
524
+ case 'strokePath':
525
+ return {
526
+ ...command,
527
+ path: command.path.map((pathCommand) => clonePathCommand(pathCommand)),
528
+ paint: clonePaint(command.paint),
529
+ };
530
+ case 'fillText':
531
+ return {
532
+ ...command,
533
+ paint: clonePaint(command.paint),
534
+ font: cloneFont(command.font),
535
+ };
536
+ case 'clipRect':
537
+ case 'clearRect':
538
+ return {
539
+ ...command,
540
+ rect: { ...command.rect },
541
+ };
542
+ default:
543
+ return { ...command };
544
+ }
545
+ }