@shopify/react-native-skia 0.1.202 → 0.1.203

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) 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/skia/include/core/SkColorTable.h +1 -1
  11. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +1 -0
  12. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +7 -0
  13. package/lib/commonjs/skia/core/Data.d.ts +2 -0
  14. package/lib/commonjs/skia/core/Data.js +29 -1
  15. package/lib/commonjs/skia/core/Data.js.map +1 -1
  16. package/lib/commonjs/skia/core/Font.d.ts +14 -1
  17. package/lib/commonjs/skia/core/Font.js +93 -1
  18. package/lib/commonjs/skia/core/Font.js.map +1 -1
  19. package/lib/commonjs/skia/types/Font/FontMgr.d.ts +8 -0
  20. package/lib/commonjs/skia/types/Font/FontMgr.js +6 -0
  21. package/lib/commonjs/skia/types/Font/FontMgr.js.map +1 -0
  22. package/lib/commonjs/skia/types/Font/FontMgrFactory.d.ts +4 -0
  23. package/lib/commonjs/skia/types/Font/FontMgrFactory.js +6 -0
  24. package/lib/commonjs/skia/types/Font/FontMgrFactory.js.map +1 -0
  25. package/lib/commonjs/skia/types/Font/index.d.ts +2 -0
  26. package/lib/commonjs/skia/types/Font/index.js +26 -0
  27. package/lib/commonjs/skia/types/Font/index.js.map +1 -1
  28. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
  29. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.js +6 -0
  30. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.js.map +1 -0
  31. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
  32. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.js +6 -0
  33. package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.js.map +1 -0
  34. package/lib/commonjs/skia/types/Skia.d.ts +4 -1
  35. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  36. package/lib/commonjs/skia/web/Host.d.ts +1 -1
  37. package/lib/commonjs/skia/web/Host.js +0 -2
  38. package/lib/commonjs/skia/web/Host.js.map +1 -1
  39. package/lib/commonjs/skia/web/JsiSkFontMgr.d.ts +10 -0
  40. package/lib/commonjs/skia/web/JsiSkFontMgr.js +34 -0
  41. package/lib/commonjs/skia/web/JsiSkFontMgr.js.map +1 -0
  42. package/lib/commonjs/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
  43. package/lib/commonjs/skia/web/JsiSkFontMgrFactory.js +32 -0
  44. package/lib/commonjs/skia/web/JsiSkFontMgrFactory.js.map +1 -0
  45. package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
  46. package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.js +90 -0
  47. package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.js.map +1 -0
  48. package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
  49. package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.js +25 -0
  50. package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.js.map +1 -0
  51. package/lib/commonjs/skia/web/JsiSkia.js +7 -1
  52. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  53. package/lib/module/skia/core/Data.d.ts +2 -0
  54. package/lib/module/skia/core/Data.js +24 -1
  55. package/lib/module/skia/core/Data.js.map +1 -1
  56. package/lib/module/skia/core/Font.d.ts +14 -1
  57. package/lib/module/skia/core/Font.js +83 -1
  58. package/lib/module/skia/core/Font.js.map +1 -1
  59. package/lib/module/skia/types/Font/FontMgr.d.ts +8 -0
  60. package/lib/module/skia/types/Font/FontMgr.js +2 -0
  61. package/lib/module/skia/types/Font/FontMgr.js.map +1 -0
  62. package/lib/module/skia/types/Font/FontMgrFactory.d.ts +4 -0
  63. package/lib/module/skia/types/Font/FontMgrFactory.js +2 -0
  64. package/lib/module/skia/types/Font/FontMgrFactory.js.map +1 -0
  65. package/lib/module/skia/types/Font/index.d.ts +2 -0
  66. package/lib/module/skia/types/Font/index.js +2 -0
  67. package/lib/module/skia/types/Font/index.js.map +1 -1
  68. package/lib/module/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
  69. package/lib/module/skia/types/Paragraph/TypefaceFontProvider.js +2 -0
  70. package/lib/module/skia/types/Paragraph/TypefaceFontProvider.js.map +1 -0
  71. package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
  72. package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.js +2 -0
  73. package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.js.map +1 -0
  74. package/lib/module/skia/types/Skia.d.ts +4 -1
  75. package/lib/module/skia/types/Skia.js.map +1 -1
  76. package/lib/module/skia/web/Host.d.ts +1 -1
  77. package/lib/module/skia/web/Host.js +0 -2
  78. package/lib/module/skia/web/Host.js.map +1 -1
  79. package/lib/module/skia/web/JsiSkFontMgr.d.ts +10 -0
  80. package/lib/module/skia/web/JsiSkFontMgr.js +24 -0
  81. package/lib/module/skia/web/JsiSkFontMgr.js.map +1 -0
  82. package/lib/module/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
  83. package/lib/module/skia/web/JsiSkFontMgrFactory.js +21 -0
  84. package/lib/module/skia/web/JsiSkFontMgrFactory.js.map +1 -0
  85. package/lib/module/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
  86. package/lib/module/skia/web/JsiSkTypefaceFontProvider.js +80 -0
  87. package/lib/module/skia/web/JsiSkTypefaceFontProvider.js.map +1 -0
  88. package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
  89. package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.js +14 -0
  90. package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.js.map +1 -0
  91. package/lib/module/skia/web/JsiSkia.js +5 -1
  92. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  93. package/lib/typescript/src/skia/core/Data.d.ts +2 -0
  94. package/lib/typescript/src/skia/core/Font.d.ts +14 -1
  95. package/lib/typescript/src/skia/types/Font/FontMgr.d.ts +8 -0
  96. package/lib/typescript/src/skia/types/Font/FontMgrFactory.d.ts +4 -0
  97. package/lib/typescript/src/skia/types/Font/index.d.ts +2 -0
  98. package/lib/typescript/src/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
  99. package/lib/typescript/src/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
  100. package/lib/typescript/src/skia/types/Skia.d.ts +4 -1
  101. package/lib/typescript/src/skia/web/Host.d.ts +1 -1
  102. package/lib/typescript/src/skia/web/JsiSkFontMgr.d.ts +10 -0
  103. package/lib/typescript/src/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
  104. package/lib/typescript/src/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
  105. package/lib/typescript/src/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
  106. package/libs/android/arm64-v8a/libskia.a +0 -0
  107. package/libs/android/arm64-v8a/libskottie.a +0 -0
  108. package/libs/android/arm64-v8a/libskparagraph.a +0 -0
  109. package/libs/android/arm64-v8a/libskshaper.a +0 -0
  110. package/libs/android/arm64-v8a/libskunicode.a +0 -0
  111. package/libs/android/armeabi-v7a/libskia.a +0 -0
  112. package/libs/android/armeabi-v7a/libskottie.a +0 -0
  113. package/libs/android/armeabi-v7a/libskparagraph.a +0 -0
  114. package/libs/android/armeabi-v7a/libskshaper.a +0 -0
  115. package/libs/android/armeabi-v7a/libskunicode.a +0 -0
  116. package/libs/android/x86/libskia.a +0 -0
  117. package/libs/android/x86/libskottie.a +0 -0
  118. package/libs/android/x86/libskparagraph.a +0 -0
  119. package/libs/android/x86/libskshaper.a +0 -0
  120. package/libs/android/x86/libskunicode.a +0 -0
  121. package/libs/android/x86_64/libskia.a +0 -0
  122. package/libs/android/x86_64/libskottie.a +0 -0
  123. package/libs/android/x86_64/libskparagraph.a +0 -0
  124. package/libs/android/x86_64/libskshaper.a +0 -0
  125. package/libs/android/x86_64/libskunicode.a +0 -0
  126. package/libs/ios/libskia.xcframework/Info.plist +5 -5
  127. package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
  128. package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
  129. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
  130. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
  131. package/libs/ios/libskparagraph.xcframework/Info.plist +42 -0
  132. package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e/libskparagraph.a +0 -0
  133. package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e_x86_64-simulator/libskparagraph.a +0 -0
  134. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
  135. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
  136. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
  137. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
  138. package/libs/ios/libskunicode.xcframework/Info.plist +42 -0
  139. package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e/libskunicode.a +0 -0
  140. package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e_x86_64-simulator/libskunicode.a +0 -0
  141. package/libs/ios/libsvg.xcframework/Info.plist +5 -5
  142. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
  143. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
  144. package/package.json +3 -1
  145. package/react-native-skia.podspec +2 -2
  146. package/src/skia/core/Data.ts +31 -1
  147. package/src/skia/core/Font.ts +109 -2
  148. package/src/skia/types/Font/FontMgr.ts +10 -0
  149. package/src/skia/types/Font/FontMgrFactory.ts +5 -0
  150. package/src/skia/types/Font/index.ts +2 -0
  151. package/src/skia/types/Paragraph/TypefaceFontProvider.ts +11 -0
  152. package/src/skia/types/Paragraph/TypefaceFontProviderFactory.ts +5 -0
  153. package/src/skia/types/Skia.ts +4 -2
  154. package/src/skia/web/Host.ts +1 -1
  155. package/src/skia/web/JsiSkFontMgr.ts +26 -0
  156. package/src/skia/web/JsiSkFontMgrFactory.ts +22 -0
  157. package/src/skia/web/JsiSkTypefaceFontProvider.ts +89 -0
  158. package/src/skia/web/JsiSkTypefaceFontProviderFactory.ts +18 -0
  159. package/src/skia/web/JsiSkia.ts +4 -0
@@ -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
  });