vexflow-native 2.3.1 → 2.5.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 (172) hide show
  1. package/README.md +7 -1
  2. package/lib/module/base/FontManager.js +41 -27
  3. package/lib/module/base/FontManager.js.map +1 -1
  4. package/lib/module/base/VexflowCanvas.js +8 -6
  5. package/lib/module/base/VexflowCanvas.js.map +1 -1
  6. package/lib/module/base/VexflowRecordingClone.js +83 -0
  7. package/lib/module/base/VexflowRecordingClone.js.map +1 -0
  8. package/lib/module/base/VexflowRecordingContext.js +1 -80
  9. package/lib/module/base/VexflowRecordingContext.js.map +1 -1
  10. package/lib/module/base/builtInTextFont.js +21 -0
  11. package/lib/module/base/builtInTextFont.js.map +1 -0
  12. package/lib/module/base/cssFontShorthand.js +34 -0
  13. package/lib/module/base/cssFontShorthand.js.map +1 -0
  14. package/lib/module/base/fonts/OFL.txt +93 -0
  15. package/lib/module/base/fonts/academico.js +12 -0
  16. package/lib/module/base/fonts/academico.js.map +1 -0
  17. package/lib/module/base/pictureRetirement.js +70 -0
  18. package/lib/module/base/pictureRetirement.js.map +1 -0
  19. package/lib/module/base/setupVexflowReactNative.js +1 -36
  20. package/lib/module/base/setupVexflowReactNative.js.map +1 -1
  21. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +22 -119
  22. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -1
  23. package/lib/module/musicxml/MusicXmlParser/MusicXmlNotes.js +129 -0
  24. package/lib/module/musicxml/MusicXmlParser/MusicXmlNotes.js.map +1 -0
  25. package/lib/module/musicxml/MusicXmlParser/PartParser.js +8 -1
  26. package/lib/module/musicxml/MusicXmlParser/PartParser.js.map +1 -1
  27. package/lib/module/musicxml/MusicXmlParser/Spanners.js +8 -5
  28. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -1
  29. package/lib/module/musicxml/MusicXmlParser/mappers.js +83 -3
  30. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -1
  31. package/lib/module/renderer/Layout/DocumentLayout.js +13 -4
  32. package/lib/module/renderer/Layout/DocumentLayout.js.map +1 -1
  33. package/lib/module/renderer/Layout/VerticalSpacing.js +37 -7
  34. package/lib/module/renderer/Layout/VerticalSpacing.js.map +1 -1
  35. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js +80 -46
  36. package/lib/module/renderer/ScoreRenderer/ScoreOverlayPicture.js.map +1 -1
  37. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +10 -22
  38. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -1
  39. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js +18 -8
  40. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js.map +1 -1
  41. package/lib/module/renderer/ScoreRenderer/utils.js +42 -88
  42. package/lib/module/renderer/ScoreRenderer/utils.js.map +1 -1
  43. package/lib/module/renderer/constants.js +1 -1
  44. package/lib/module/renderer/layout.js +11 -4
  45. package/lib/module/renderer/layout.js.map +1 -1
  46. package/lib/module/renderer/lyrics.js +127 -0
  47. package/lib/module/renderer/lyrics.js.map +1 -0
  48. package/lib/module/renderer/measure.js +52 -13
  49. package/lib/module/renderer/measure.js.map +1 -1
  50. package/lib/module/renderer/measureBounds.js +5 -0
  51. package/lib/module/renderer/measureBounds.js.map +1 -1
  52. package/lib/module/renderer/measureModifiers.js +24 -6
  53. package/lib/module/renderer/measureModifiers.js.map +1 -1
  54. package/lib/module/renderer/noteModifiers.js +6 -14
  55. package/lib/module/renderer/noteModifiers.js.map +1 -1
  56. package/lib/module/renderer/{fixedNoteSpacing.js → noteSpacing.js} +46 -10
  57. package/lib/module/renderer/noteSpacing.js.map +1 -0
  58. package/lib/module/renderer/render.js +41 -21
  59. package/lib/module/renderer/render.js.map +1 -1
  60. package/lib/module/renderer/scale.js +8 -1
  61. package/lib/module/renderer/scale.js.map +1 -1
  62. package/lib/module/renderer/tempoMark.js +162 -0
  63. package/lib/module/renderer/tempoMark.js.map +1 -0
  64. package/lib/module/renderer/useScoreRecording.js +2 -1
  65. package/lib/module/renderer/useScoreRecording.js.map +1 -1
  66. package/lib/typescript/src/base/FontManager.d.ts +12 -7
  67. package/lib/typescript/src/base/FontManager.d.ts.map +1 -1
  68. package/lib/typescript/src/base/VexflowCanvas.d.ts.map +1 -1
  69. package/lib/typescript/src/base/VexflowRecordingClone.d.ts +8 -0
  70. package/lib/typescript/src/base/VexflowRecordingClone.d.ts.map +1 -0
  71. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -1
  72. package/lib/typescript/src/base/builtInTextFont.d.ts +6 -0
  73. package/lib/typescript/src/base/builtInTextFont.d.ts.map +1 -0
  74. package/lib/typescript/src/base/cssFontShorthand.d.ts +3 -0
  75. package/lib/typescript/src/base/cssFontShorthand.d.ts.map +1 -0
  76. package/lib/typescript/src/base/fonts/academico.d.ts +5 -0
  77. package/lib/typescript/src/base/fonts/academico.d.ts.map +1 -0
  78. package/lib/typescript/src/base/pictureRetirement.d.ts +26 -0
  79. package/lib/typescript/src/base/pictureRetirement.d.ts.map +1 -0
  80. package/lib/typescript/src/base/setupVexflowReactNative.d.ts +0 -8
  81. package/lib/typescript/src/base/setupVexflowReactNative.d.ts.map +1 -1
  82. package/lib/typescript/src/base/types.d.ts.map +1 -1
  83. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +4 -1
  84. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -1
  85. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -1
  86. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlNotes.d.ts +13 -0
  87. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlNotes.d.ts.map +1 -0
  88. package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts.map +1 -1
  89. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +6 -0
  90. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -1
  91. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts +7 -1
  92. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -1
  93. package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts.map +1 -1
  94. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts +6 -0
  95. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts.map +1 -1
  96. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts +1 -1
  97. package/lib/typescript/src/renderer/ScoreRenderer/ScoreOverlayPicture.d.ts.map +1 -1
  98. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -1
  99. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts +1 -0
  100. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts.map +1 -1
  101. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +14 -11
  102. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -1
  103. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts +8 -27
  104. package/lib/typescript/src/renderer/ScoreRenderer/utils.d.ts.map +1 -1
  105. package/lib/typescript/src/renderer/layout.d.ts +5 -1
  106. package/lib/typescript/src/renderer/layout.d.ts.map +1 -1
  107. package/lib/typescript/src/renderer/lyrics.d.ts +42 -0
  108. package/lib/typescript/src/renderer/lyrics.d.ts.map +1 -0
  109. package/lib/typescript/src/renderer/measure.d.ts +7 -2
  110. package/lib/typescript/src/renderer/measure.d.ts.map +1 -1
  111. package/lib/typescript/src/renderer/measureBounds.d.ts +3 -0
  112. package/lib/typescript/src/renderer/measureBounds.d.ts.map +1 -1
  113. package/lib/typescript/src/renderer/measureModifiers.d.ts +14 -4
  114. package/lib/typescript/src/renderer/measureModifiers.d.ts.map +1 -1
  115. package/lib/typescript/src/renderer/noteModifiers.d.ts +3 -2
  116. package/lib/typescript/src/renderer/noteModifiers.d.ts.map +1 -1
  117. package/lib/typescript/src/renderer/{fixedNoteSpacing.d.ts → noteSpacing.d.ts} +9 -1
  118. package/lib/typescript/src/renderer/noteSpacing.d.ts.map +1 -0
  119. package/lib/typescript/src/renderer/render.d.ts.map +1 -1
  120. package/lib/typescript/src/renderer/scale.d.ts.map +1 -1
  121. package/lib/typescript/src/renderer/tempoMark.d.ts +21 -0
  122. package/lib/typescript/src/renderer/tempoMark.d.ts.map +1 -0
  123. package/lib/typescript/src/renderer/types.d.ts +31 -7
  124. package/lib/typescript/src/renderer/types.d.ts.map +1 -1
  125. package/lib/typescript/src/state/types.d.ts +11 -3
  126. package/lib/typescript/src/state/types.d.ts.map +1 -1
  127. package/package.json +34 -26
  128. package/src/base/FontManager.ts +50 -31
  129. package/src/base/VexflowCanvas.tsx +10 -10
  130. package/src/base/VexflowRecordingClone.ts +89 -0
  131. package/src/base/VexflowRecordingContext.ts +8 -81
  132. package/src/base/builtInTextFont.ts +25 -0
  133. package/src/base/cssFontShorthand.ts +45 -0
  134. package/src/base/fonts/OFL.txt +93 -0
  135. package/src/base/fonts/academico.ts +10 -0
  136. package/src/base/pictureRetirement.ts +74 -0
  137. package/src/base/setupVexflowReactNative.ts +1 -49
  138. package/src/base/types.ts +0 -3
  139. package/src/musicxml/MusicXmlParser/InternalTypes.ts +5 -0
  140. package/src/musicxml/MusicXmlParser/MeasureParser.ts +31 -182
  141. package/src/musicxml/MusicXmlParser/MusicXmlNotes.ts +198 -0
  142. package/src/musicxml/MusicXmlParser/PartParser.ts +10 -3
  143. package/src/musicxml/MusicXmlParser/Spanners.ts +11 -8
  144. package/src/musicxml/MusicXmlParser/mappers.ts +55 -6
  145. package/src/renderer/Layout/DocumentLayout.ts +12 -3
  146. package/src/renderer/Layout/VerticalSpacing.ts +46 -13
  147. package/src/renderer/ScoreRenderer/ScoreOverlayPicture.tsx +127 -66
  148. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +8 -23
  149. package/src/renderer/ScoreRenderer/ScoreScrollbar.tsx +16 -2
  150. package/src/renderer/ScoreRenderer/types.ts +14 -12
  151. package/src/renderer/ScoreRenderer/utils.ts +54 -125
  152. package/src/renderer/constants.ts +1 -1
  153. package/src/renderer/layout.ts +19 -4
  154. package/src/renderer/lyrics.ts +163 -0
  155. package/src/renderer/measure.tsx +70 -12
  156. package/src/renderer/measureBounds.ts +10 -0
  157. package/src/renderer/measureModifiers.ts +27 -7
  158. package/src/renderer/noteModifiers.ts +9 -10
  159. package/src/renderer/{fixedNoteSpacing.ts → noteSpacing.ts} +60 -9
  160. package/src/renderer/render.tsx +63 -27
  161. package/src/renderer/scale.ts +9 -0
  162. package/src/renderer/tempoMark.ts +224 -0
  163. package/src/renderer/types.ts +32 -7
  164. package/src/renderer/useScoreRecording.ts +1 -0
  165. package/src/state/types.ts +11 -3
  166. package/lib/module/renderer/fixedNoteSpacing.js.map +0 -1
  167. package/lib/module/utils.js +0 -44
  168. package/lib/module/utils.js.map +0 -1
  169. package/lib/typescript/src/renderer/fixedNoteSpacing.d.ts.map +0 -1
  170. package/lib/typescript/src/utils.d.ts +0 -1
  171. package/lib/typescript/src/utils.d.ts.map +0 -1
  172. package/src/utils.tsx +0 -42
@@ -6,10 +6,7 @@ import type {
6
6
  SkTypeface,
7
7
  SkTypefaceFontProvider,
8
8
  } from 'react-native-skia';
9
-
10
- // import Logger, { LogCategory } from '../shared/Logger';
11
-
12
- // const Log = Logger.extend(LogCategory.FontManager);
9
+ import { builtInTextTypeface } from './builtInTextFont';
13
10
 
14
11
  interface ResolvedFont {
15
12
  family: string;
@@ -32,7 +29,7 @@ function parseSize(size?: string | number): number {
32
29
  const match = size.trim().match(/^(\d+(?:\.\d+)?)(px|pt|em|%)?$/);
33
30
 
34
31
  if (match) {
35
- const value = parseFloat(match?.[1] || '30');
32
+ const value = parseFloat(match[1] || '30');
36
33
  const unit = match[2] || 'pt';
37
34
 
38
35
  switch (unit) {
@@ -49,7 +46,7 @@ function parseSize(size?: string | number): number {
49
46
  }
50
47
  }
51
48
 
52
- return 30; // Default font size in pixels
49
+ return 30; // Default font size in points
53
50
  }
54
51
 
55
52
  function parseWeight(weight?: string | number): FontWeight {
@@ -93,7 +90,7 @@ export default class FontManager {
93
90
 
94
91
  private fontProvider: SkTypefaceFontProvider;
95
92
  private defaultFontName: string;
96
- private familiesLower: string[];
93
+ private registeredFamilies: Record<string, string> = {};
97
94
  // Instances never cross runtimes; each runtime builds its own, so these
98
95
  // caches are runtime-local.
99
96
  private familyMatchCache: Record<string, string> = {};
@@ -118,11 +115,17 @@ export default class FontManager {
118
115
  );
119
116
  }
120
117
 
121
- this.familiesLower = families.map((family) => family.toLowerCase());
118
+ for (const family of families) {
119
+ const key = family.toLowerCase();
120
+
121
+ if (this.registeredFamilies[key] == null) {
122
+ this.registeredFamilies[key] = family;
123
+ }
124
+ }
122
125
  }
123
126
 
124
- private isFamilyAvailable(family: string): boolean {
125
- return this.familiesLower.includes(family.toLowerCase());
127
+ private registeredFamily(family: string): string | undefined {
128
+ return this.registeredFamilies[family.toLowerCase()];
126
129
  }
127
130
 
128
131
  private getFamiliesFromFont(font: string | FontInfo): string[] {
@@ -151,8 +154,10 @@ export default class FontManager {
151
154
  let matched = this.defaultFontName;
152
155
 
153
156
  for (const family of families) {
154
- if (this.isFamilyAvailable(family)) {
155
- matched = family;
157
+ const registered = this.registeredFamily(family);
158
+
159
+ if (registered != null) {
160
+ matched = registered;
156
161
  break;
157
162
  }
158
163
  }
@@ -161,36 +166,50 @@ export default class FontManager {
161
166
  return matched;
162
167
  }
163
168
 
164
- /** A system typeface for the first requested family the OS knows, walking
165
- * the list in order until a provider family is reached — so a requested
166
- * family the OS knows wins over a later provider family; null when the OS
167
- * knows none of them, or when the provider has the first one. */
168
- private matchSystemTypeface(
169
- font: string | FontInfo | undefined,
170
- style: { weight: FontWeight; slant: FontSlant; width: FontWidth }
171
- ): SkTypeface | null {
172
- if (!font || this.matchFontFamilyName(font) !== this.defaultFontName) {
173
- return null;
174
- }
175
-
169
+ /** The OS font manager, or null where there is none to ask (CanvasKit on
170
+ * the web gives an empty one). */
171
+ private systemFonts(): SkFontMgr | null {
176
172
  if (this.systemFontMgr === undefined) {
177
173
  try {
178
- this.systemFontMgr = Skia.FontMgr.System();
174
+ const fontMgr = Skia.FontMgr.System();
175
+ this.systemFontMgr = fontMgr.countFamilies() > 0 ? fontMgr : null;
179
176
  } catch {
180
177
  this.systemFontMgr = null;
181
178
  }
182
179
  }
183
180
 
184
- if (!this.systemFontMgr) {
181
+ return this.systemFontMgr;
182
+ }
183
+
184
+ /** A typeface from outside the provider, for a request the provider has
185
+ * none of the families of: the system typeface for the first requested
186
+ * family the OS knows, walking the list in order until a provider family is
187
+ * reached — so a requested family the OS knows wins over a later provider
188
+ * family; without system fonts, the built-in text face unless the provider
189
+ * has a requested family; null otherwise. */
190
+ private matchFallbackTypeface(
191
+ font: string | FontInfo | undefined,
192
+ style: { weight: FontWeight; slant: FontSlant; width: FontWidth }
193
+ ): SkTypeface | null {
194
+ if (!font || this.matchFontFamilyName(font) !== this.defaultFontName) {
185
195
  return null;
186
196
  }
187
197
 
188
- for (const family of this.getFamiliesFromFont(font)) {
189
- if (this.isFamilyAvailable(family)) {
198
+ const families = this.getFamiliesFromFont(font);
199
+ const systemFontMgr = this.systemFonts();
200
+
201
+ if (!systemFontMgr) {
202
+ return families.some((family) => this.registeredFamily(family) != null)
203
+ ? null
204
+ : builtInTextTypeface(style.weight);
205
+ }
206
+
207
+ for (const family of families) {
208
+ if (this.registeredFamily(family) != null) {
190
209
  return null;
191
210
  }
192
211
  try {
193
- const typeface = this.systemFontMgr.matchFamilyStyle(family, style);
212
+ const typeface = systemFontMgr.matchFamilyStyle(family, style);
194
213
  if (typeface) {
195
214
  return typeface;
196
215
  }
@@ -247,12 +266,12 @@ export default class FontManager {
247
266
  }
248
267
 
249
268
  const typeface =
250
- this.matchSystemTypeface(font, fontStyle) ??
269
+ this.matchFallbackTypeface(font, fontStyle) ??
251
270
  this.fontProvider.matchFamilyStyle(family, fontStyle);
252
271
 
253
272
  if (!typeface) {
254
273
  throw new Error(
255
- `Failed to create SkFont. No matching typeface found for family "${family}" with weight "${resolvedWeight}" and slant "${slant}".`
274
+ `Failed to create SkFont. No matching typeface found for family "${family}" with weight "${FontWeight[resolvedWeight]}" and slant "${FontSlant[slant]}".`
256
275
  );
257
276
  }
258
277
 
@@ -2,23 +2,21 @@ import { Canvas, Picture, Skia, useCanvasSize } from 'react-native-skia';
2
2
  import React, { useMemo } from 'react';
3
3
  import { StyleSheet } from 'react-native';
4
4
 
5
+ import { usePictureRetirement } from './pictureRetirement';
5
6
  import type { VexflowCanvasProps } from './types';
6
7
  import VexflowRecordingContext from './VexflowRecordingContext';
7
8
  import { renderVexflowRecordingCommands } from './VexflowRecordingReplay';
8
9
 
9
10
  const VexflowCanvas: React.FC<VexflowCanvasProps> = (props) => {
10
- const {
11
- onDraw,
12
- fontManager,
13
- defaultFont,
14
- // width: propWidth,
15
- // height: propHeight,
16
- // colorScheme = 'light',
17
- } = props;
11
+ const { onDraw, fontManager, defaultFont } = props;
18
12
 
19
13
  const { size: canvasSize, onLayout } = useCanvasSize();
20
14
 
21
15
  const picture = useMemo(() => {
16
+ if (canvasSize.width === 0 || canvasSize.height === 0) {
17
+ return null;
18
+ }
19
+
22
20
  const recorder = Skia.PictureRecorder();
23
21
  const canvas = recorder.beginRecording(
24
22
  Skia.XYWHRect(0, 0, canvasSize.width, canvasSize.height)
@@ -37,9 +35,11 @@ const VexflowCanvas: React.FC<VexflowCanvasProps> = (props) => {
37
35
  return recorder.finishRecordingAsPicture();
38
36
  }, [onDraw, canvasSize, fontManager, defaultFont]);
39
37
 
38
+ usePictureRetirement(picture);
39
+
40
40
  return (
41
41
  <Canvas style={styles.canvas} onLayout={onLayout}>
42
- <Picture picture={picture} />
42
+ {picture ? <Picture picture={picture} /> : null}
43
43
  </Canvas>
44
44
  );
45
45
  };
@@ -48,6 +48,6 @@ export default VexflowCanvas;
48
48
 
49
49
  const styles = StyleSheet.create({
50
50
  canvas: {
51
- flex: 1, // TMP?
51
+ flex: 1,
52
52
  },
53
53
  });
@@ -0,0 +1,89 @@
1
+ import type {
2
+ VexflowRecordingCommand,
3
+ VexflowRecordingFont,
4
+ VexflowRecordingLineCap,
5
+ VexflowRecordingPaint,
6
+ VexflowRecordingPathCommand,
7
+ VexflowRecordingRect,
8
+ } from './VexflowRecordingTypes';
9
+
10
+ export function makeRect(
11
+ x: number,
12
+ y: number,
13
+ width: number,
14
+ height: number
15
+ ): VexflowRecordingRect {
16
+ return { x, y, width, height };
17
+ }
18
+
19
+ export function normalizeLineCap(cap: string): VexflowRecordingLineCap {
20
+ if (cap === 'round' || cap === 'square') {
21
+ return cap;
22
+ }
23
+
24
+ return 'butt';
25
+ }
26
+
27
+ export function clonePaint(
28
+ paint: VexflowRecordingPaint
29
+ ): VexflowRecordingPaint {
30
+ return {
31
+ ...paint,
32
+ ...(paint.lineDash ? { lineDash: [...paint.lineDash] } : null),
33
+ };
34
+ }
35
+
36
+ export function cloneFont(font: VexflowRecordingFont): VexflowRecordingFont {
37
+ return {
38
+ ...font,
39
+ font:
40
+ typeof font.font === 'object' && font.font ? { ...font.font } : font.font,
41
+ };
42
+ }
43
+
44
+ export function clonePathCommand(
45
+ command: VexflowRecordingPathCommand
46
+ ): VexflowRecordingPathCommand {
47
+ if (command.type === 'addRect' || command.type === 'addArc') {
48
+ return {
49
+ ...command,
50
+ rect: { ...command.rect },
51
+ };
52
+ }
53
+
54
+ return { ...command };
55
+ }
56
+
57
+ export function cloneCommand(
58
+ command: VexflowRecordingCommand
59
+ ): VexflowRecordingCommand {
60
+ switch (command.type) {
61
+ case 'fillRect':
62
+ return {
63
+ ...command,
64
+ rect: { ...command.rect },
65
+ paint: clonePaint(command.paint),
66
+ };
67
+ case 'fillPath':
68
+ case 'strokePath':
69
+ return {
70
+ ...command,
71
+ path: command.path.map((pathCommand) => clonePathCommand(pathCommand)),
72
+ paint: clonePaint(command.paint),
73
+ };
74
+ case 'fillText':
75
+ return {
76
+ ...command,
77
+ paint: clonePaint(command.paint),
78
+ font: cloneFont(command.font),
79
+ };
80
+ case 'clipRect':
81
+ case 'clearRect':
82
+ return {
83
+ ...command,
84
+ rect: { ...command.rect },
85
+ };
86
+ default:
87
+ return { ...command };
88
+ }
89
+ }
@@ -10,13 +10,19 @@ import {
10
10
  resolveVexflowColor,
11
11
  type VexflowColorScheme,
12
12
  } from './VexflowColorScheme';
13
+ import {
14
+ cloneCommand,
15
+ cloneFont,
16
+ clonePaint,
17
+ clonePathCommand,
18
+ makeRect,
19
+ normalizeLineCap,
20
+ } from './VexflowRecordingClone';
13
21
  import type {
14
22
  VexflowRecordingCommand,
15
23
  VexflowRecordingFont,
16
- VexflowRecordingLineCap,
17
24
  VexflowRecordingPaint,
18
25
  VexflowRecordingPathCommand,
19
- VexflowRecordingRect,
20
26
  } from './VexflowRecordingTypes';
21
27
 
22
28
  const showSilentLogs = false;
@@ -462,82 +468,3 @@ export default class VexflowRecordingContext implements VexflowRenderContext {
462
468
  this.commands.push(command);
463
469
  }
464
470
  }
465
-
466
- function makeRect(
467
- x: number,
468
- y: number,
469
- width: number,
470
- height: number
471
- ): VexflowRecordingRect {
472
- return { x, y, width, height };
473
- }
474
-
475
- function normalizeLineCap(cap: string): VexflowRecordingLineCap {
476
- if (cap === 'round' || cap === 'square') {
477
- return cap;
478
- }
479
-
480
- return 'butt';
481
- }
482
-
483
- function clonePaint(paint: VexflowRecordingPaint): VexflowRecordingPaint {
484
- return {
485
- ...paint,
486
- ...(paint.lineDash ? { lineDash: [...paint.lineDash] } : null),
487
- };
488
- }
489
-
490
- function cloneFont(font: VexflowRecordingFont): VexflowRecordingFont {
491
- return {
492
- ...font,
493
- font:
494
- typeof font.font === 'object' && font.font ? { ...font.font } : font.font,
495
- };
496
- }
497
-
498
- function clonePathCommand(
499
- command: VexflowRecordingPathCommand
500
- ): VexflowRecordingPathCommand {
501
- if (command.type === 'addRect' || command.type === 'addArc') {
502
- return {
503
- ...command,
504
- rect: { ...command.rect },
505
- };
506
- }
507
-
508
- return { ...command };
509
- }
510
-
511
- function cloneCommand(
512
- command: VexflowRecordingCommand
513
- ): VexflowRecordingCommand {
514
- switch (command.type) {
515
- case 'fillRect':
516
- return {
517
- ...command,
518
- rect: { ...command.rect },
519
- paint: clonePaint(command.paint),
520
- };
521
- case 'fillPath':
522
- case 'strokePath':
523
- return {
524
- ...command,
525
- path: command.path.map((pathCommand) => clonePathCommand(pathCommand)),
526
- paint: clonePaint(command.paint),
527
- };
528
- case 'fillText':
529
- return {
530
- ...command,
531
- paint: clonePaint(command.paint),
532
- font: cloneFont(command.font),
533
- };
534
- case 'clipRect':
535
- case 'clearRect':
536
- return {
537
- ...command,
538
- rect: { ...command.rect },
539
- };
540
- default:
541
- return { ...command };
542
- }
543
- }
@@ -0,0 +1,25 @@
1
+ import { FontWeight, Skia } from 'react-native-skia';
2
+ import type { SkTypeface } from 'react-native-skia';
3
+ import { ACADEMICO_WOFF2 } from './fonts/academico';
4
+
5
+ // Deliberately not a worklet: FontManager (a worklet class) captures
6
+ // builtInTextTypeface as a remote-function reference on native, so the font
7
+ // data never enters a worklet closure. Called only without system fonts (the
8
+ // web), where worklets run on the JS thread.
9
+ const decoded: Partial<
10
+ Record<keyof typeof ACADEMICO_WOFF2, SkTypeface | null>
11
+ > = {};
12
+
13
+ /** The built-in Latin text face (Academico; bold from semibold up), decoded
14
+ * on first use once per JS runtime. */
15
+ export function builtInTextTypeface(weight: FontWeight): SkTypeface | null {
16
+ const face = weight >= FontWeight.SemiBold ? 'bold' : 'regular';
17
+
18
+ if (decoded[face] === undefined) {
19
+ decoded[face] = Skia.Typeface.MakeFreeTypeFaceFromData(
20
+ Skia.Data.fromBase64(ACADEMICO_WOFF2[face])
21
+ );
22
+ }
23
+
24
+ return decoded[face];
25
+ }
@@ -0,0 +1,45 @@
1
+ import type { FontInfo } from 'vexflow';
2
+
3
+ const FONT_SIZE_PATTERN =
4
+ /(\d+(?:\.\d+)?(?:px|pt|em|rem|%|in|mm|cm)?)(?:\/[^\s]+)?/i;
5
+ const FONT_STYLE_PATTERN = /\b(italic|oblique|normal)\b/i;
6
+ const FONT_WEIGHT_PATTERN = /\b(bold|bolder|lighter|normal|[1-9]00)\b/i;
7
+
8
+ export function parseCssFontShorthand(
9
+ cssFontShorthand: string
10
+ ): Required<FontInfo> {
11
+ const source = cssFontShorthand.trim();
12
+
13
+ if (source.length === 0) {
14
+ return emptyFontInfo();
15
+ }
16
+
17
+ const sizeMatch = source.match(FONT_SIZE_PATTERN);
18
+
19
+ if (!sizeMatch || sizeMatch.index === undefined) {
20
+ return emptyFontInfo();
21
+ }
22
+
23
+ const prefix = source.slice(0, sizeMatch.index).trim();
24
+ const family = source.slice(sizeMatch.index + sizeMatch[0].length).trim();
25
+ const style =
26
+ prefix.match(FONT_STYLE_PATTERN)?.[1]?.toLowerCase() ?? 'normal';
27
+ const weight =
28
+ prefix.match(FONT_WEIGHT_PATTERN)?.[1]?.toLowerCase() ?? 'normal';
29
+
30
+ return {
31
+ family,
32
+ size: sizeMatch[1] ?? '',
33
+ weight,
34
+ style,
35
+ };
36
+ }
37
+
38
+ function emptyFontInfo(): Required<FontInfo> {
39
+ return {
40
+ family: '',
41
+ size: '',
42
+ weight: '',
43
+ style: '',
44
+ };
45
+ }
@@ -0,0 +1,93 @@
1
+ Copyright © 2022, Steinberg Media Technologies GmbH (http://www.steinberg.net/),
2
+ with Reserved Font Name "Academico".
3
+
4
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
5
+ This license is copied below, and is also available with a FAQ at:
6
+ http://scripts.sil.org/OFL
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION AND CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.