@shopify/react-native-skia 0.1.201 → 0.1.203

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. package/android/CMakeLists.txt +9 -9
  2. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +11 -0
  3. package/cpp/api/JsiSkApi.h +8 -0
  4. package/cpp/api/JsiSkFontMgr.h +54 -0
  5. package/cpp/api/JsiSkFontMgrFactory.h +40 -0
  6. package/cpp/api/JsiSkFontStyle.h +61 -0
  7. package/cpp/api/JsiSkTypeFaceFontProvider.h +80 -0
  8. package/cpp/api/JsiSkTypeFaceFontProviderFactory.h +32 -0
  9. package/cpp/rnskia/RNSkPlatformContext.h +6 -0
  10. package/cpp/rnskia/RNSkView.h +5 -2
  11. package/cpp/skia/include/core/SkColorTable.h +1 -1
  12. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +5 -14
  13. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.mm +7 -56
  14. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +1 -0
  15. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +9 -2
  16. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +31 -0
  17. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +105 -0
  18. package/lib/commonjs/skia/core/Data.d.ts +2 -0
  19. package/lib/commonjs/skia/core/Data.js +29 -1
  20. package/lib/commonjs/skia/core/Data.js.map +1 -1
  21. package/lib/commonjs/skia/core/Font.d.ts +14 -1
  22. package/lib/commonjs/skia/core/Font.js +93 -1
  23. package/lib/commonjs/skia/core/Font.js.map +1 -1
  24. package/lib/commonjs/skia/types/Font/FontMgr.d.ts +8 -0
  25. package/lib/commonjs/skia/types/Font/FontMgr.js +6 -0
  26. package/lib/commonjs/skia/types/Font/FontMgr.js.map +1 -0
  27. package/lib/commonjs/skia/types/Font/FontMgrFactory.d.ts +4 -0
  28. package/lib/commonjs/skia/types/Font/FontMgrFactory.js +6 -0
  29. package/lib/commonjs/skia/types/Font/FontMgrFactory.js.map +1 -0
  30. package/lib/commonjs/skia/types/Font/index.d.ts +2 -0
  31. package/lib/commonjs/skia/types/Font/index.js +26 -0
  32. package/lib/commonjs/skia/types/Font/index.js.map +1 -1
  33. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
  34. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.js +6 -0
  35. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.js.map +1 -0
  36. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
  37. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.js +6 -0
  38. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.js.map +1 -0
  39. package/lib/commonjs/skia/types/Skia.d.ts +4 -1
  40. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  41. package/lib/commonjs/skia/web/Host.d.ts +1 -1
  42. package/lib/commonjs/skia/web/Host.js +0 -2
  43. package/lib/commonjs/skia/web/Host.js.map +1 -1
  44. package/lib/commonjs/skia/web/JsiSkFontMgr.d.ts +10 -0
  45. package/lib/commonjs/skia/web/JsiSkFontMgr.js +34 -0
  46. package/lib/commonjs/skia/web/JsiSkFontMgr.js.map +1 -0
  47. package/lib/commonjs/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
  48. package/lib/commonjs/skia/web/JsiSkFontMgrFactory.js +32 -0
  49. package/lib/commonjs/skia/web/JsiSkFontMgrFactory.js.map +1 -0
  50. package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
  51. package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.js +90 -0
  52. package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.js.map +1 -0
  53. package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
  54. package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.js +25 -0
  55. package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.js.map +1 -0
  56. package/lib/commonjs/skia/web/JsiSkia.js +7 -1
  57. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  58. package/lib/module/skia/core/Data.d.ts +2 -0
  59. package/lib/module/skia/core/Data.js +24 -1
  60. package/lib/module/skia/core/Data.js.map +1 -1
  61. package/lib/module/skia/core/Font.d.ts +14 -1
  62. package/lib/module/skia/core/Font.js +83 -1
  63. package/lib/module/skia/core/Font.js.map +1 -1
  64. package/lib/module/skia/types/Font/FontMgr.d.ts +8 -0
  65. package/lib/module/skia/types/Font/FontMgr.js +2 -0
  66. package/lib/module/skia/types/Font/FontMgr.js.map +1 -0
  67. package/lib/module/skia/types/Font/FontMgrFactory.d.ts +4 -0
  68. package/lib/module/skia/types/Font/FontMgrFactory.js +2 -0
  69. package/lib/module/skia/types/Font/FontMgrFactory.js.map +1 -0
  70. package/lib/module/skia/types/Font/index.d.ts +2 -0
  71. package/lib/module/skia/types/Font/index.js +2 -0
  72. package/lib/module/skia/types/Font/index.js.map +1 -1
  73. package/lib/module/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
  74. package/lib/module/skia/types/Paragraph/TypefaceFontProvider.js +2 -0
  75. package/lib/module/skia/types/Paragraph/TypefaceFontProvider.js.map +1 -0
  76. package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
  77. package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.js +2 -0
  78. package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.js.map +1 -0
  79. package/lib/module/skia/types/Skia.d.ts +4 -1
  80. package/lib/module/skia/types/Skia.js.map +1 -1
  81. package/lib/module/skia/web/Host.d.ts +1 -1
  82. package/lib/module/skia/web/Host.js +0 -2
  83. package/lib/module/skia/web/Host.js.map +1 -1
  84. package/lib/module/skia/web/JsiSkFontMgr.d.ts +10 -0
  85. package/lib/module/skia/web/JsiSkFontMgr.js +24 -0
  86. package/lib/module/skia/web/JsiSkFontMgr.js.map +1 -0
  87. package/lib/module/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
  88. package/lib/module/skia/web/JsiSkFontMgrFactory.js +21 -0
  89. package/lib/module/skia/web/JsiSkFontMgrFactory.js.map +1 -0
  90. package/lib/module/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
  91. package/lib/module/skia/web/JsiSkTypefaceFontProvider.js +80 -0
  92. package/lib/module/skia/web/JsiSkTypefaceFontProvider.js.map +1 -0
  93. package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
  94. package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.js +14 -0
  95. package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.js.map +1 -0
  96. package/lib/module/skia/web/JsiSkia.js +5 -1
  97. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  98. package/lib/typescript/src/skia/core/Data.d.ts +2 -0
  99. package/lib/typescript/src/skia/core/Font.d.ts +14 -1
  100. package/lib/typescript/src/skia/types/Font/FontMgr.d.ts +8 -0
  101. package/lib/typescript/src/skia/types/Font/FontMgrFactory.d.ts +4 -0
  102. package/lib/typescript/src/skia/types/Font/index.d.ts +2 -0
  103. package/lib/typescript/src/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
  104. package/lib/typescript/src/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
  105. package/lib/typescript/src/skia/types/Skia.d.ts +4 -1
  106. package/lib/typescript/src/skia/web/Host.d.ts +1 -1
  107. package/lib/typescript/src/skia/web/JsiSkFontMgr.d.ts +10 -0
  108. package/lib/typescript/src/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
  109. package/lib/typescript/src/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
  110. package/lib/typescript/src/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
  111. package/libs/android/arm64-v8a/libskia.a +0 -0
  112. package/libs/android/arm64-v8a/libskottie.a +0 -0
  113. package/libs/android/arm64-v8a/libskparagraph.a +0 -0
  114. package/libs/android/arm64-v8a/libskshaper.a +0 -0
  115. package/libs/android/arm64-v8a/libskunicode.a +0 -0
  116. package/libs/android/armeabi-v7a/libskia.a +0 -0
  117. package/libs/android/armeabi-v7a/libskottie.a +0 -0
  118. package/libs/android/armeabi-v7a/libskparagraph.a +0 -0
  119. package/libs/android/armeabi-v7a/libskshaper.a +0 -0
  120. package/libs/android/armeabi-v7a/libskunicode.a +0 -0
  121. package/libs/android/x86/libskia.a +0 -0
  122. package/libs/android/x86/libskottie.a +0 -0
  123. package/libs/android/x86/libskparagraph.a +0 -0
  124. package/libs/android/x86/libskshaper.a +0 -0
  125. package/libs/android/x86/libskunicode.a +0 -0
  126. package/libs/android/x86_64/libskia.a +0 -0
  127. package/libs/android/x86_64/libskottie.a +0 -0
  128. package/libs/android/x86_64/libskparagraph.a +0 -0
  129. package/libs/android/x86_64/libskshaper.a +0 -0
  130. package/libs/android/x86_64/libskunicode.a +0 -0
  131. package/libs/ios/libskia.xcframework/Info.plist +5 -5
  132. package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
  133. package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
  134. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
  135. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
  136. package/libs/ios/libskparagraph.xcframework/Info.plist +42 -0
  137. package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e/libskparagraph.a +0 -0
  138. package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e_x86_64-simulator/libskparagraph.a +0 -0
  139. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
  140. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
  141. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
  142. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
  143. package/libs/ios/libskunicode.xcframework/Info.plist +42 -0
  144. package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e/libskunicode.a +0 -0
  145. package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e_x86_64-simulator/libskunicode.a +0 -0
  146. package/libs/ios/libsvg.xcframework/Info.plist +5 -5
  147. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
  148. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
  149. package/package.json +3 -1
  150. package/react-native-skia.podspec +2 -2
  151. package/src/skia/core/Data.ts +31 -1
  152. package/src/skia/core/Font.ts +109 -2
  153. package/src/skia/types/Font/FontMgr.ts +10 -0
  154. package/src/skia/types/Font/FontMgrFactory.ts +5 -0
  155. package/src/skia/types/Font/index.ts +2 -0
  156. package/src/skia/types/Paragraph/TypefaceFontProvider.ts +11 -0
  157. package/src/skia/types/Paragraph/TypefaceFontProviderFactory.ts +5 -0
  158. package/src/skia/types/Skia.ts +4 -2
  159. package/src/skia/web/Host.ts +1 -1
  160. package/src/skia/web/JsiSkFontMgr.ts +26 -0
  161. package/src/skia/web/JsiSkFontMgrFactory.ts +22 -0
  162. package/src/skia/web/JsiSkTypefaceFontProvider.ts +89 -0
  163. package/src/skia/web/JsiSkTypefaceFontProviderFactory.ts +18 -0
  164. package/src/skia/web/JsiSkia.ts +4 -0
  165. package/ios/RNSkia-iOS/SkiaMetalRenderer.h +0 -5
  166. package/ios/RNSkia-iOS/SkiaMetalRenderer.mm +0 -55
@@ -0,0 +1,42 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>AvailableLibraries</key>
6
+ <array>
7
+ <dict>
8
+ <key>LibraryIdentifier</key>
9
+ <string>ios-arm64_arm64e</string>
10
+ <key>LibraryPath</key>
11
+ <string>libskunicode.a</string>
12
+ <key>SupportedArchitectures</key>
13
+ <array>
14
+ <string>arm64</string>
15
+ <string>arm64e</string>
16
+ </array>
17
+ <key>SupportedPlatform</key>
18
+ <string>ios</string>
19
+ </dict>
20
+ <dict>
21
+ <key>LibraryIdentifier</key>
22
+ <string>ios-arm64_arm64e_x86_64-simulator</string>
23
+ <key>LibraryPath</key>
24
+ <string>libskunicode.a</string>
25
+ <key>SupportedArchitectures</key>
26
+ <array>
27
+ <string>arm64</string>
28
+ <string>arm64e</string>
29
+ <string>x86_64</string>
30
+ </array>
31
+ <key>SupportedPlatform</key>
32
+ <string>ios</string>
33
+ <key>SupportedPlatformVariant</key>
34
+ <string>simulator</string>
35
+ </dict>
36
+ </array>
37
+ <key>CFBundlePackageType</key>
38
+ <string>XFWK</string>
39
+ <key>XCFrameworkFormatVersion</key>
40
+ <string>1.0</string>
41
+ </dict>
42
+ </plist>
@@ -6,32 +6,32 @@
6
6
  <array>
7
7
  <dict>
8
8
  <key>LibraryIdentifier</key>
9
- <string>ios-arm64_arm64e</string>
9
+ <string>ios-arm64_arm64e_x86_64-simulator</string>
10
10
  <key>LibraryPath</key>
11
11
  <string>libsvg.a</string>
12
12
  <key>SupportedArchitectures</key>
13
13
  <array>
14
14
  <string>arm64</string>
15
15
  <string>arm64e</string>
16
+ <string>x86_64</string>
16
17
  </array>
17
18
  <key>SupportedPlatform</key>
18
19
  <string>ios</string>
20
+ <key>SupportedPlatformVariant</key>
21
+ <string>simulator</string>
19
22
  </dict>
20
23
  <dict>
21
24
  <key>LibraryIdentifier</key>
22
- <string>ios-arm64_arm64e_x86_64-simulator</string>
25
+ <string>ios-arm64_arm64e</string>
23
26
  <key>LibraryPath</key>
24
27
  <string>libsvg.a</string>
25
28
  <key>SupportedArchitectures</key>
26
29
  <array>
27
30
  <string>arm64</string>
28
31
  <string>arm64e</string>
29
- <string>x86_64</string>
30
32
  </array>
31
33
  <key>SupportedPlatform</key>
32
34
  <string>ios</string>
33
- <key>SupportedPlatformVariant</key>
34
- <string>simulator</string>
35
35
  </dict>
36
36
  </array>
37
37
  <key>CFBundlePackageType</key>
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "setup-skia-web": "./scripts/setup-canvaskit.js"
8
8
  },
9
9
  "title": "React Native Skia",
10
- "version": "0.1.201",
10
+ "version": "0.1.203",
11
11
  "description": "High-performance React Native Graphics using Skia",
12
12
  "main": "lib/module/index.js",
13
13
  "files": [
@@ -33,6 +33,8 @@
33
33
  "libs/ios/libsvg.xcframework",
34
34
  "libs/ios/libskottie.xcframework",
35
35
  "libs/ios/libsksg.xcframework",
36
+ "libs/ios/libskparagraph.xcframework",
37
+ "libs/ios/libskunicode.xcframework",
36
38
  "react-native-skia.podspec",
37
39
  "scripts/setup-canvaskit.js",
38
40
  "dist/**"
@@ -34,8 +34,8 @@ Pod::Spec.new do |s|
34
34
  'libs/ios/libskia.xcframework',
35
35
  'libs/ios/libsvg.xcframework',
36
36
  'libs/ios/libskshaper.xcframework',
37
- #'libs/ios/libskparagraph.xcframework',
38
- #'libs/ios/libskunicode.xcframework',
37
+ 'libs/ios/libskparagraph.xcframework',
38
+ 'libs/ios/libskunicode.xcframework',
39
39
  ]
40
40
 
41
41
  # All iOS cpp/h files
@@ -18,7 +18,7 @@ const factoryWrapper = <T>(
18
18
  }
19
19
  };
20
20
 
21
- const loadData = <T>(
21
+ export const loadData = <T>(
22
22
  source: DataSourceParam,
23
23
  factory: (data: SkData) => T | null,
24
24
  onError?: (err: Error) => void
@@ -37,6 +37,7 @@ const loadData = <T>(
37
37
  );
38
38
  }
39
39
  };
40
+
40
41
  const useLoading = <T extends SkJSIInstance<string>>(
41
42
  source: DataSourceParam,
42
43
  loader: () => Promise<T | null>
@@ -61,6 +62,35 @@ const useLoading = <T extends SkJSIInstance<string>>(
61
62
  return data;
62
63
  };
63
64
 
65
+ export const useCollectionLoading = <T extends SkJSIInstance<string>>(
66
+ source: DataSourceParam[],
67
+ loader: () => Promise<(T | null)[]>
68
+ ) => {
69
+ const mounted = useRef(false);
70
+ const [data, setData] = useState<T[] | null>(null);
71
+ const dataRef = useRef<T[] | null>(null);
72
+
73
+ useEffect(() => {
74
+ mounted.current = true;
75
+ loader().then((result) => {
76
+ const value = result.filter((r) => r !== null) as T[];
77
+ if (mounted.current) {
78
+ setData(value);
79
+ dataRef.current = value;
80
+ }
81
+ });
82
+
83
+ return () => {
84
+ dataRef.current?.forEach((instance) => instance?.dispose());
85
+ mounted.current = false;
86
+ };
87
+
88
+ // eslint-disable-next-line react-hooks/exhaustive-deps
89
+ }, [source]);
90
+
91
+ return data;
92
+ };
93
+
64
94
  export const useRawData = <T extends SkJSIInstance<string>>(
65
95
  source: DataSourceParam,
66
96
  factory: (data: SkData) => T | null,
@@ -1,8 +1,11 @@
1
1
  /*global SkiaApi*/
2
- import { useMemo } from "react";
2
+ import { useEffect, useMemo, useState } from "react";
3
3
 
4
4
  import { Skia } from "../Skia";
5
- import type { DataSourceParam } from "../types";
5
+ import { FontSlant } from "../types";
6
+ import type { DataModule, DataSourceParam, SkFontMgr } from "../types";
7
+ import { Platform } from "../../Platform";
8
+ import type { SkTypefaceFontProvider } from "../types/Paragraph/TypefaceFontProvider";
6
9
 
7
10
  import { useTypeface } from "./Typeface";
8
11
 
@@ -25,3 +28,107 @@ export const useFont = (
25
28
  }
26
29
  }, [size, typeface]);
27
30
  };
31
+
32
+ type Slant = "normal" | "italic" | "oblique";
33
+ type Weight =
34
+ | "normal"
35
+ | "bold"
36
+ | "100"
37
+ | "200"
38
+ | "300"
39
+ | "400"
40
+ | "500"
41
+ | "600"
42
+ | "700"
43
+ | "800"
44
+ | "900";
45
+
46
+ interface RNFontStyle {
47
+ fontFamily: string;
48
+ fontSize: number;
49
+ fontStyle: Slant;
50
+ fontWeight: Weight;
51
+ }
52
+
53
+ const defaultFontStyle: RNFontStyle = {
54
+ fontFamily: "System",
55
+ fontSize: 14,
56
+ fontStyle: "normal",
57
+ fontWeight: "normal",
58
+ };
59
+
60
+ const slant = (s: Slant) => {
61
+ if (s === "italic") {
62
+ return FontSlant.Italic;
63
+ } else if (s === "oblique") {
64
+ return FontSlant.Oblique;
65
+ } else {
66
+ return FontSlant.Upright;
67
+ }
68
+ };
69
+
70
+ const weight = (fontWeight: Weight) => {
71
+ switch (fontWeight) {
72
+ case "normal":
73
+ return 400;
74
+ case "bold":
75
+ return 700;
76
+ default:
77
+ return parseInt(fontWeight, 10);
78
+ }
79
+ };
80
+
81
+ export const matchFont = (
82
+ inputStyle: Partial<RNFontStyle> = {},
83
+ fontMgr: SkFontMgr = Skia.FontMgr.System()
84
+ ) => {
85
+ const fontStyle = {
86
+ ...defaultFontStyle,
87
+ ...inputStyle,
88
+ };
89
+ const style = {
90
+ weight: weight(fontStyle.fontWeight),
91
+ width: 5,
92
+ slant: slant(fontStyle.fontStyle),
93
+ };
94
+ const typeface = fontMgr.matchFamilyStyle(fontStyle.fontFamily, style);
95
+ return Skia.Font(typeface, fontStyle.fontSize);
96
+ };
97
+
98
+ export const listFontFamilies = (fontMgr: SkFontMgr = Skia.FontMgr.System()) =>
99
+ new Array(fontMgr.countFamilies())
100
+ .fill(0)
101
+ .map((_, i) => fontMgr.getFamilyName(i));
102
+
103
+ const loadTypefaces = (typefacesToLoad: Record<string, DataModule[]>) => {
104
+ const promises = Object.keys(typefacesToLoad).flatMap((familyName) => {
105
+ return typefacesToLoad[familyName].map((typefaceToLoad) => {
106
+ return Skia.Data.fromURI(Platform.resolveAsset(typefaceToLoad)).then(
107
+ (data) => {
108
+ const tf = Skia.Typeface.MakeFreeTypeFaceFromData(data);
109
+ if (tf === null) {
110
+ throw new Error(`Couldn't create typeface for ${familyName}`);
111
+ }
112
+ return [familyName, tf] as const;
113
+ }
114
+ );
115
+ });
116
+ });
117
+ return Promise.all(promises);
118
+ };
119
+
120
+ export const useFonts = (sources: Record<string, DataModule[]>) => {
121
+ const [fontMgr, setFontMgr] = useState<null | SkTypefaceFontProvider>(null);
122
+
123
+ useEffect(() => {
124
+ loadTypefaces(sources).then((result) => {
125
+ const fMgr = Skia.TypefaceFontProvider.Make();
126
+ result.forEach(([familyName, typeface]) => {
127
+ fMgr.registerFont(typeface, familyName);
128
+ });
129
+ setFontMgr(fMgr);
130
+ });
131
+ // eslint-disable-next-line react-hooks/exhaustive-deps
132
+ }, []);
133
+ return fontMgr;
134
+ };
@@ -0,0 +1,10 @@
1
+ import type { SkJSIInstance } from "../JsiInstance";
2
+ import type { SkTypeface } from "../Typeface";
3
+
4
+ import type { FontStyle } from "./Font";
5
+
6
+ export interface SkFontMgr extends SkJSIInstance<"FontMgr"> {
7
+ countFamilies(): number;
8
+ getFamilyName(index: number): string;
9
+ matchFamilyStyle(name: string, style: FontStyle): SkTypeface;
10
+ }
@@ -0,0 +1,5 @@
1
+ import type { SkFontMgr } from "./FontMgr";
2
+
3
+ export interface FontMgrFactory {
4
+ System(): SkFontMgr;
5
+ }
@@ -1 +1,3 @@
1
1
  export * from "./Font";
2
+ export * from "./FontMgr";
3
+ export * from "./FontMgrFactory";
@@ -0,0 +1,11 @@
1
+ import type { SkFontMgr } from "../Font";
2
+ import type { SkTypeface } from "../Typeface";
3
+
4
+ export interface SkTypefaceFontProvider extends SkFontMgr {
5
+ /**
6
+ * Registers a given typeface with the given family name.
7
+ * @param typeface - Typeface.
8
+ * @param family
9
+ */
10
+ registerFont(typeface: SkTypeface, familyName: string): void;
11
+ }
@@ -0,0 +1,5 @@
1
+ import type { SkTypefaceFontProvider } from "./TypefaceFontProvider";
2
+
3
+ export interface TypefaceFontProviderFactory {
4
+ Make(): SkTypefaceFontProvider;
5
+ }
@@ -1,7 +1,7 @@
1
1
  import type { ImageFilterFactory } from "./ImageFilter";
2
2
  import type { PathFactory } from "./Path";
3
3
  import type { ColorFilterFactory } from "./ColorFilter";
4
- import type { SkFont } from "./Font";
4
+ import type { SkFont, FontMgrFactory } from "./Font";
5
5
  import type { SkTypeface, TypefaceFactory } from "./Typeface";
6
6
  import type { ImageFactory } from "./Image";
7
7
  import type { MaskFilterFactory } from "./MaskFilter";
@@ -27,7 +27,7 @@ import type { SkPath } from "./Path/Path";
27
27
  import type { SkContourMeasureIter } from "./ContourMeasure";
28
28
  import type { PictureFactory, SkPictureRecorder } from "./Picture";
29
29
  import type { Color, SkColor } from "./Color";
30
-
30
+ import type { TypefaceFontProviderFactory } from "./Paragraph/TypefaceFontProviderFactory";
31
31
  /**
32
32
  * Declares the interface for the native Skia API
33
33
  */
@@ -51,6 +51,8 @@ export interface Skia {
51
51
  ColorFilter: ColorFilterFactory;
52
52
  Font: (typeface?: SkTypeface, size?: number) => SkFont;
53
53
  Typeface: TypefaceFactory;
54
+ TypefaceFontProvider: TypefaceFontProviderFactory;
55
+ FontMgr: FontMgrFactory;
54
56
  MaskFilter: MaskFilterFactory;
55
57
  RuntimeEffect: RuntimeEffectFactory;
56
58
  ImageFilter: ImageFilterFactory;
@@ -29,7 +29,7 @@ export abstract class BaseHostObject<T, N extends string>
29
29
  this.__typename__ = typename;
30
30
  }
31
31
 
32
- abstract dispose: () => void;
32
+ abstract dispose(): void;
33
33
  }
34
34
 
35
35
  export abstract class HostObject<T, N extends string> extends BaseHostObject<
@@ -0,0 +1,26 @@
1
+ import type { CanvasKit, FontMgr } from "canvaskit-wasm";
2
+
3
+ import type { FontStyle, SkFontMgr, SkTypeface } from "../types";
4
+
5
+ import { HostObject, NotImplementedOnRNWeb } from "./Host";
6
+
7
+ export class JsiSkFontMgr
8
+ extends HostObject<FontMgr, "FontMgr">
9
+ implements SkFontMgr
10
+ {
11
+ constructor(CanvasKit: CanvasKit, ref: FontMgr) {
12
+ super(CanvasKit, ref, "FontMgr");
13
+ }
14
+ dispose() {
15
+ this.ref.delete();
16
+ }
17
+ countFamilies() {
18
+ return this.ref.countFamilies();
19
+ }
20
+ getFamilyName(index: number) {
21
+ return this.ref.getFamilyName(index);
22
+ }
23
+ matchFamilyStyle(_familyName: string, _fontStyle: FontStyle): SkTypeface {
24
+ throw new NotImplementedOnRNWeb();
25
+ }
26
+ }
@@ -0,0 +1,22 @@
1
+ import type { CanvasKit } from "canvaskit-wasm";
2
+
3
+ import type { FontMgrFactory } from "../types";
4
+
5
+ import { Host } from "./Host";
6
+ import { JsiSkFontMgr } from "./JsiSkFontMgr";
7
+
8
+ export class JsiSkFontMgrFactory extends Host implements FontMgrFactory {
9
+ constructor(CanvasKit: CanvasKit) {
10
+ super(CanvasKit);
11
+ }
12
+
13
+ System() {
14
+ const fontMgr = this.CanvasKit.TypefaceFontProvider.Make();
15
+ if (!fontMgr) {
16
+ throw new Error("Couldn't create system font manager");
17
+ }
18
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
19
+ // @ts-expect-error
20
+ return new JsiSkFontMgr(this.CanvasKit, fontMgr);
21
+ }
22
+ }
@@ -0,0 +1,89 @@
1
+ import type { CanvasKit, TypefaceFontProvider } from "canvaskit-wasm";
2
+
3
+ import type { SkTypefaceFontProvider } from "../types/Paragraph/TypefaceFontProvider";
4
+ import type { FontStyle, SkTypeface } from "../types";
5
+
6
+ import { HostObject, NotImplementedOnRNWeb } from "./Host";
7
+
8
+ export class JsiSkTypefaceFontProvider
9
+ extends HostObject<TypefaceFontProvider, "FontMgr">
10
+ implements SkTypefaceFontProvider
11
+ {
12
+ private allocatedPointers: number[] = [];
13
+
14
+ constructor(CanvasKit: CanvasKit, ref: TypefaceFontProvider) {
15
+ super(CanvasKit, ref, "FontMgr");
16
+ }
17
+
18
+ matchFamilyStyle(_name: string, _style: FontStyle): SkTypeface {
19
+ throw new NotImplementedOnRNWeb();
20
+ }
21
+ countFamilies() {
22
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
23
+ // @ts-expect-error
24
+ return this.ref.countFamilies();
25
+ }
26
+ getFamilyName(index: number) {
27
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
28
+ // @ts-expect-error
29
+ return this.ref.getFamilyName(index);
30
+ }
31
+ registerFont(typeface: SkTypeface, familyName: string) {
32
+ const strLen = lengthBytesUTF8(familyName) + 1;
33
+
34
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
35
+ // @ts-expect-error
36
+ const strPtr = this.CanvasKit._malloc(strLen);
37
+ stringToUTF8(this.CanvasKit, familyName, strPtr, strLen);
38
+
39
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
40
+ // @ts-expect-error
41
+ this.ref._registerFont(typeface.ref, strPtr);
42
+ }
43
+
44
+ dispose() {
45
+ for (const ptr of this.allocatedPointers) {
46
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
47
+ // @ts-expect-error
48
+ this.CanvasKit._free(ptr);
49
+ }
50
+
51
+ this.ref.delete();
52
+ }
53
+ }
54
+
55
+ const lengthBytesUTF8 = (str: string) => {
56
+ // TextEncoder will give us the byte length in UTF8 form
57
+ const encoder = new TextEncoder();
58
+ const utf8 = encoder.encode(str);
59
+ return utf8.length;
60
+ };
61
+
62
+ const stringToUTF8 = (
63
+ CanvasKit: CanvasKit,
64
+ str: string,
65
+ outPtr: number,
66
+ maxBytesToWrite: number
67
+ ) => {
68
+ // TextEncoder will give us the byte array in UTF8 form
69
+ const encoder = new TextEncoder();
70
+ const utf8 = encoder.encode(str);
71
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
72
+ // @ts-expect-error
73
+ const heap: Int8Array = CanvasKit.HEAPU8;
74
+
75
+ // Check if there's enough space
76
+ if (utf8.length > maxBytesToWrite) {
77
+ throw new Error("Not enough space to write UTF8 encoded string");
78
+ }
79
+
80
+ // Copy the bytes
81
+ for (let i = 0; i < utf8.length; i++) {
82
+ heap[outPtr + i] = utf8[i];
83
+ }
84
+
85
+ // Null terminate
86
+ if (utf8.length < maxBytesToWrite) {
87
+ heap[outPtr + utf8.length] = 0;
88
+ }
89
+ };
@@ -0,0 +1,18 @@
1
+ import type { CanvasKit } from "canvaskit-wasm";
2
+
3
+ import { Host } from "./Host";
4
+ import { JsiSkTypefaceFontProvider } from "./JsiSkTypefaceFontProvider";
5
+
6
+ export class JsiSkTypefaceFontProviderFactory
7
+ extends Host
8
+ implements JsiSkTypefaceFontProviderFactory
9
+ {
10
+ constructor(CanvasKit: CanvasKit) {
11
+ super(CanvasKit);
12
+ }
13
+
14
+ Make() {
15
+ const tf = this.CanvasKit.TypefaceFontProvider.Make();
16
+ return new JsiSkTypefaceFontProvider(this.CanvasKit, tf);
17
+ }
18
+ }
@@ -37,6 +37,8 @@ import { JsiSkFont } from "./JsiSkFont";
37
37
  import { MakeVertices } from "./JsiSkVerticesFactory";
38
38
  import { JsiSkPath } from "./JsiSkPath";
39
39
  import { JsiSkTypeface } from "./JsiSkTypeface";
40
+ import { JsiSkTypefaceFontProviderFactory } from "./JsiSkTypefaceFontProviderFactory";
41
+ import { JsiSkFontMgrFactory } from "./JsiSkFontMgrFactory";
40
42
 
41
43
  export const JsiSkApi = (CanvasKit: CanvasKit): Skia => ({
42
44
  Point: (x: number, y: number) =>
@@ -102,4 +104,6 @@ export const JsiSkApi = (CanvasKit: CanvasKit): Skia => ({
102
104
  return new JsiSkRect(CanvasKit, CanvasKit.XYWHRect(x, y, width, height));
103
105
  },
104
106
  Surface: new JsiSkSurfaceFactory(CanvasKit),
107
+ TypefaceFontProvider: new JsiSkTypefaceFontProviderFactory(CanvasKit),
108
+ FontMgr: new JsiSkFontMgrFactory(CanvasKit),
105
109
  });
@@ -1,5 +0,0 @@
1
- #pragma once
2
-
3
- #include "SkSurface.h"
4
-
5
- sk_sp<SkSurface> MakeOffscreenMetalSurface(int width, int height);
@@ -1,55 +0,0 @@
1
- #pragma clang diagnostic push
2
- #pragma clang diagnostic ignored "-Wdocumentation"
3
-
4
- #import "SkCanvas.h"
5
- #import "SkColorSpace.h"
6
- #import "SkSurface.h"
7
-
8
- #import <include/gpu/GrBackendSurface.h>
9
- #import <include/gpu/GrDirectContext.h>
10
- #import <include/gpu/ganesh/SkSurfaceGanesh.h>
11
-
12
- #pragma clang diagnostic pop
13
-
14
- #import <MetalKit/MetalKit.h>
15
-
16
- struct OffscreenRenderContext {
17
- id<MTLDevice> device;
18
- id<MTLCommandQueue> commandQueue;
19
- sk_sp<GrDirectContext> skiaContext;
20
- id<MTLTexture> texture;
21
-
22
- OffscreenRenderContext(int width, int height) {
23
- device = MTLCreateSystemDefaultDevice();
24
- commandQueue =
25
- id<MTLCommandQueue>(CFRetain((GrMTLHandle)[device newCommandQueue]));
26
- skiaContext = GrDirectContext::MakeMetal((__bridge void *)device,
27
- (__bridge void *)commandQueue);
28
- // Create a Metal texture descriptor
29
- MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor
30
- texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
31
- width:width
32
- height:height
33
- mipmapped:NO];
34
- textureDescriptor.usage =
35
- MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
36
- texture = [device newTextureWithDescriptor:textureDescriptor];
37
- }
38
- };
39
-
40
- sk_sp<SkSurface> MakeOffscreenMetalSurface(int width, int height) {
41
- auto ctx = new OffscreenRenderContext(width, height);
42
-
43
- // Create a GrBackendTexture from the Metal texture
44
- GrMtlTextureInfo info;
45
- info.fTexture.retain((__bridge void *)ctx->texture);
46
- GrBackendTexture backendTexture(width, height, GrMipMapped::kNo, info);
47
-
48
- // Create a SkSurface from the GrBackendTexture
49
- auto surface = SkSurfaces::WrapBackendTexture(
50
- ctx->skiaContext.get(), backendTexture, kTopLeft_GrSurfaceOrigin, 0,
51
- kBGRA_8888_SkColorType, nullptr, nullptr,
52
- [](void *addr) { delete (OffscreenRenderContext *)addr; }, ctx);
53
-
54
- return surface;
55
- }