@shopify/react-native-skia 0.1.202 → 0.1.203
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.
- package/android/CMakeLists.txt +9 -9
- package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +11 -0
- package/cpp/api/JsiSkApi.h +8 -0
- package/cpp/api/JsiSkFontMgr.h +54 -0
- package/cpp/api/JsiSkFontMgrFactory.h +40 -0
- package/cpp/api/JsiSkFontStyle.h +61 -0
- package/cpp/api/JsiSkTypeFaceFontProvider.h +80 -0
- package/cpp/api/JsiSkTypeFaceFontProviderFactory.h +32 -0
- package/cpp/rnskia/RNSkPlatformContext.h +6 -0
- package/cpp/skia/include/core/SkColorTable.h +1 -1
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +1 -0
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +7 -0
- package/lib/commonjs/skia/core/Data.d.ts +2 -0
- package/lib/commonjs/skia/core/Data.js +29 -1
- package/lib/commonjs/skia/core/Data.js.map +1 -1
- package/lib/commonjs/skia/core/Font.d.ts +14 -1
- package/lib/commonjs/skia/core/Font.js +93 -1
- package/lib/commonjs/skia/core/Font.js.map +1 -1
- package/lib/commonjs/skia/types/Font/FontMgr.d.ts +8 -0
- package/lib/commonjs/skia/types/Font/FontMgr.js +6 -0
- package/lib/commonjs/skia/types/Font/FontMgr.js.map +1 -0
- package/lib/commonjs/skia/types/Font/FontMgrFactory.d.ts +4 -0
- package/lib/commonjs/skia/types/Font/FontMgrFactory.js +6 -0
- package/lib/commonjs/skia/types/Font/FontMgrFactory.js.map +1 -0
- package/lib/commonjs/skia/types/Font/index.d.ts +2 -0
- package/lib/commonjs/skia/types/Font/index.js +26 -0
- package/lib/commonjs/skia/types/Font/index.js.map +1 -1
- package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
- package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.js +6 -0
- package/lib/commonjs/skia/types/Paragraph/TypefaceFontProvider.js.map +1 -0
- package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
- package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.js +6 -0
- package/lib/commonjs/skia/types/Paragraph/TypefaceFontProviderFactory.js.map +1 -0
- package/lib/commonjs/skia/types/Skia.d.ts +4 -1
- package/lib/commonjs/skia/types/Skia.js.map +1 -1
- package/lib/commonjs/skia/web/Host.d.ts +1 -1
- package/lib/commonjs/skia/web/Host.js +0 -2
- package/lib/commonjs/skia/web/Host.js.map +1 -1
- package/lib/commonjs/skia/web/JsiSkFontMgr.d.ts +10 -0
- package/lib/commonjs/skia/web/JsiSkFontMgr.js +34 -0
- package/lib/commonjs/skia/web/JsiSkFontMgr.js.map +1 -0
- package/lib/commonjs/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
- package/lib/commonjs/skia/web/JsiSkFontMgrFactory.js +32 -0
- package/lib/commonjs/skia/web/JsiSkFontMgrFactory.js.map +1 -0
- package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
- package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.js +90 -0
- package/lib/commonjs/skia/web/JsiSkTypefaceFontProvider.js.map +1 -0
- package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
- package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.js +25 -0
- package/lib/commonjs/skia/web/JsiSkTypefaceFontProviderFactory.js.map +1 -0
- package/lib/commonjs/skia/web/JsiSkia.js +7 -1
- package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
- package/lib/module/skia/core/Data.d.ts +2 -0
- package/lib/module/skia/core/Data.js +24 -1
- package/lib/module/skia/core/Data.js.map +1 -1
- package/lib/module/skia/core/Font.d.ts +14 -1
- package/lib/module/skia/core/Font.js +83 -1
- package/lib/module/skia/core/Font.js.map +1 -1
- package/lib/module/skia/types/Font/FontMgr.d.ts +8 -0
- package/lib/module/skia/types/Font/FontMgr.js +2 -0
- package/lib/module/skia/types/Font/FontMgr.js.map +1 -0
- package/lib/module/skia/types/Font/FontMgrFactory.d.ts +4 -0
- package/lib/module/skia/types/Font/FontMgrFactory.js +2 -0
- package/lib/module/skia/types/Font/FontMgrFactory.js.map +1 -0
- package/lib/module/skia/types/Font/index.d.ts +2 -0
- package/lib/module/skia/types/Font/index.js +2 -0
- package/lib/module/skia/types/Font/index.js.map +1 -1
- package/lib/module/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
- package/lib/module/skia/types/Paragraph/TypefaceFontProvider.js +2 -0
- package/lib/module/skia/types/Paragraph/TypefaceFontProvider.js.map +1 -0
- package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
- package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.js +2 -0
- package/lib/module/skia/types/Paragraph/TypefaceFontProviderFactory.js.map +1 -0
- package/lib/module/skia/types/Skia.d.ts +4 -1
- package/lib/module/skia/types/Skia.js.map +1 -1
- package/lib/module/skia/web/Host.d.ts +1 -1
- package/lib/module/skia/web/Host.js +0 -2
- package/lib/module/skia/web/Host.js.map +1 -1
- package/lib/module/skia/web/JsiSkFontMgr.d.ts +10 -0
- package/lib/module/skia/web/JsiSkFontMgr.js +24 -0
- package/lib/module/skia/web/JsiSkFontMgr.js.map +1 -0
- package/lib/module/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
- package/lib/module/skia/web/JsiSkFontMgrFactory.js +21 -0
- package/lib/module/skia/web/JsiSkFontMgrFactory.js.map +1 -0
- package/lib/module/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
- package/lib/module/skia/web/JsiSkTypefaceFontProvider.js +80 -0
- package/lib/module/skia/web/JsiSkTypefaceFontProvider.js.map +1 -0
- package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
- package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.js +14 -0
- package/lib/module/skia/web/JsiSkTypefaceFontProviderFactory.js.map +1 -0
- package/lib/module/skia/web/JsiSkia.js +5 -1
- package/lib/module/skia/web/JsiSkia.js.map +1 -1
- package/lib/typescript/src/skia/core/Data.d.ts +2 -0
- package/lib/typescript/src/skia/core/Font.d.ts +14 -1
- package/lib/typescript/src/skia/types/Font/FontMgr.d.ts +8 -0
- package/lib/typescript/src/skia/types/Font/FontMgrFactory.d.ts +4 -0
- package/lib/typescript/src/skia/types/Font/index.d.ts +2 -0
- package/lib/typescript/src/skia/types/Paragraph/TypefaceFontProvider.d.ts +10 -0
- package/lib/typescript/src/skia/types/Paragraph/TypefaceFontProviderFactory.d.ts +4 -0
- package/lib/typescript/src/skia/types/Skia.d.ts +4 -1
- package/lib/typescript/src/skia/web/Host.d.ts +1 -1
- package/lib/typescript/src/skia/web/JsiSkFontMgr.d.ts +10 -0
- package/lib/typescript/src/skia/web/JsiSkFontMgrFactory.d.ts +8 -0
- package/lib/typescript/src/skia/web/JsiSkTypefaceFontProvider.d.ts +13 -0
- package/lib/typescript/src/skia/web/JsiSkTypefaceFontProviderFactory.d.ts +7 -0
- package/libs/android/arm64-v8a/libskia.a +0 -0
- package/libs/android/arm64-v8a/libskottie.a +0 -0
- package/libs/android/arm64-v8a/libskparagraph.a +0 -0
- package/libs/android/arm64-v8a/libskshaper.a +0 -0
- package/libs/android/arm64-v8a/libskunicode.a +0 -0
- package/libs/android/armeabi-v7a/libskia.a +0 -0
- package/libs/android/armeabi-v7a/libskottie.a +0 -0
- package/libs/android/armeabi-v7a/libskparagraph.a +0 -0
- package/libs/android/armeabi-v7a/libskshaper.a +0 -0
- package/libs/android/armeabi-v7a/libskunicode.a +0 -0
- package/libs/android/x86/libskia.a +0 -0
- package/libs/android/x86/libskottie.a +0 -0
- package/libs/android/x86/libskparagraph.a +0 -0
- package/libs/android/x86/libskshaper.a +0 -0
- package/libs/android/x86/libskunicode.a +0 -0
- package/libs/android/x86_64/libskia.a +0 -0
- package/libs/android/x86_64/libskottie.a +0 -0
- package/libs/android/x86_64/libskparagraph.a +0 -0
- package/libs/android/x86_64/libskshaper.a +0 -0
- package/libs/android/x86_64/libskunicode.a +0 -0
- package/libs/ios/libskia.xcframework/Info.plist +5 -5
- package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
- package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
- package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
- package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
- package/libs/ios/libskparagraph.xcframework/Info.plist +42 -0
- package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e/libskparagraph.a +0 -0
- package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e_x86_64-simulator/libskparagraph.a +0 -0
- package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
- package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
- package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
- package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
- package/libs/ios/libskunicode.xcframework/Info.plist +42 -0
- package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e/libskunicode.a +0 -0
- package/libs/ios/libskunicode.xcframework/ios-arm64_arm64e_x86_64-simulator/libskunicode.a +0 -0
- package/libs/ios/libsvg.xcframework/Info.plist +5 -5
- package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
- package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
- package/package.json +3 -1
- package/react-native-skia.podspec +2 -2
- package/src/skia/core/Data.ts +31 -1
- package/src/skia/core/Font.ts +109 -2
- package/src/skia/types/Font/FontMgr.ts +10 -0
- package/src/skia/types/Font/FontMgrFactory.ts +5 -0
- package/src/skia/types/Font/index.ts +2 -0
- package/src/skia/types/Paragraph/TypefaceFontProvider.ts +11 -0
- package/src/skia/types/Paragraph/TypefaceFontProviderFactory.ts +5 -0
- package/src/skia/types/Skia.ts +4 -2
- package/src/skia/web/Host.ts +1 -1
- package/src/skia/web/JsiSkFontMgr.ts +26 -0
- package/src/skia/web/JsiSkFontMgrFactory.ts +22 -0
- package/src/skia/web/JsiSkTypefaceFontProvider.ts +89 -0
- package/src/skia/web/JsiSkTypefaceFontProviderFactory.ts +18 -0
- package/src/skia/web/JsiSkia.ts +4 -0
package/src/skia/core/Font.ts
CHANGED
|
@@ -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
|
|
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,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
|
+
}
|
package/src/skia/types/Skia.ts
CHANGED
|
@@ -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;
|
package/src/skia/web/Host.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/skia/web/JsiSkia.ts
CHANGED
|
@@ -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
|
});
|