@shopify/react-native-skia 2.8.0 → 2.9.1-next.1
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/build.gradle +10 -4
- package/android/cpp/rnskia-android/OpenGLContext.h +29 -3
- package/android/cpp/rnskia-android/OpenGLWindowContext.cpp +9 -2
- package/android/cpp/rnskia-android/gl/Context.h +3 -0
- package/android/cpp/rnskia-android/gl/Surface.h +1 -1
- package/cpp/api/JsiSkiaContext.h +4 -0
- package/cpp/dawn/include/dawn/dawn_proc.h +53 -0
- package/cpp/dawn/include/dawn/dawn_proc_table.h +330 -0
- package/cpp/dawn/include/dawn/dawn_thread_dispatch_proc.h +47 -0
- package/cpp/dawn/include/dawn/dawn_version.h +41 -0
- package/cpp/dawn/include/dawn/native/D3D11Backend.h +65 -0
- package/cpp/dawn/include/dawn/native/D3D12Backend.h +86 -0
- package/cpp/dawn/include/dawn/native/D3DBackend.h +56 -0
- package/cpp/dawn/include/dawn/native/DawnNative.h +369 -0
- package/cpp/dawn/include/dawn/native/MetalBackend.h +56 -0
- package/cpp/dawn/include/dawn/native/NullBackend.h +39 -0
- package/cpp/dawn/include/dawn/native/OpenGLBackend.h +89 -0
- package/cpp/dawn/include/dawn/native/VulkanBackend.h +183 -0
- package/cpp/dawn/include/dawn/native/WebGPUBackend.h +49 -0
- package/cpp/dawn/include/dawn/native/dawn_native_export.h +49 -0
- package/cpp/dawn/include/dawn/platform/DawnPlatform.h +210 -0
- package/cpp/dawn/include/dawn/platform/dawn_platform_export.h +49 -0
- package/cpp/dawn/include/dawn/replay/Replay.h +99 -0
- package/cpp/dawn/include/dawn/replay/dawn_replay_export.h +49 -0
- package/cpp/dawn/include/dawn/webgpu_cpp_print.h +2844 -0
- package/cpp/dawn/include/tint/tint.h +90 -0
- package/cpp/dawn/include/webgpu/webgpu.h +4987 -0
- package/cpp/dawn/include/webgpu/webgpu_cpp.h +10080 -0
- package/cpp/dawn/include/webgpu/webgpu_cpp_chained_struct.h +57 -0
- package/cpp/dawn/include/webgpu/webgpu_enum_class_bitmasks.h +161 -0
- package/cpp/dawn/include/webgpu/webgpu_glfw.h +88 -0
- package/cpp/rnskia/RNSkView.h +6 -0
- package/cpp/skia/src/gpu/graphite/ContextOptionsPriv.h +45 -0
- package/cpp/skia/src/gpu/graphite/ResourceTypes.h +360 -0
- package/cpp/skia/src/gpu/graphite/TextureProxyView.h +105 -0
- package/lib/commonjs/Platform/Platform.js +5 -2
- package/lib/commonjs/Platform/Platform.js.map +1 -1
- package/lib/commonjs/Platform/Platform.web.js +8 -7
- package/lib/commonjs/Platform/Platform.web.js.map +1 -1
- package/lib/commonjs/skia/core/Font.d.ts +21 -6
- package/lib/commonjs/skia/core/Font.js +24 -4
- package/lib/commonjs/skia/core/Font.js.map +1 -1
- package/lib/commonjs/skia/types/Data/Data.d.ts +5 -4
- package/lib/commonjs/skia/types/Data/Data.js +7 -1
- package/lib/commonjs/skia/types/Data/Data.js.map +1 -1
- package/lib/commonjs/skia/web/JsiSkFont.d.ts +1 -1
- package/lib/commonjs/skia/web/JsiSkFont.js +43 -2
- package/lib/commonjs/skia/web/JsiSkFont.js.map +1 -1
- package/lib/module/Platform/Platform.js +6 -3
- package/lib/module/Platform/Platform.js.map +1 -1
- package/lib/module/Platform/Platform.web.js +9 -8
- package/lib/module/Platform/Platform.web.js.map +1 -1
- package/lib/module/skia/core/Font.d.ts +21 -6
- package/lib/module/skia/core/Font.js +26 -5
- package/lib/module/skia/core/Font.js.map +1 -1
- package/lib/module/skia/types/Data/Data.d.ts +5 -4
- package/lib/module/skia/types/Data/Data.js +5 -0
- package/lib/module/skia/types/Data/Data.js.map +1 -1
- package/lib/module/skia/web/JsiSkFont.d.ts +1 -1
- package/lib/module/skia/web/JsiSkFont.js +44 -3
- package/lib/module/skia/web/JsiSkFont.js.map +1 -1
- package/lib/typescript/lib/commonjs/skia/types/Data/Data.d.ts +1 -0
- package/lib/typescript/lib/commonjs/skia/web/JsiSkFont.d.ts +1 -1
- package/lib/typescript/lib/module/mock/index.d.ts +1 -0
- package/lib/typescript/lib/module/skia/types/Data/Data.d.ts +1 -0
- package/lib/typescript/lib/module/skia/web/JsiSkFont.d.ts +1 -1
- package/lib/typescript/src/skia/core/Font.d.ts +21 -6
- package/lib/typescript/src/skia/types/Data/Data.d.ts +5 -4
- package/lib/typescript/src/skia/web/JsiSkFont.d.ts +1 -1
- package/libs/.graphite +1 -0
- package/package.json +13 -7
- package/react-native-skia.podspec +18 -10
- package/src/Platform/Platform.ts +6 -7
- package/src/Platform/Platform.web.tsx +9 -8
- package/src/skia/core/Font.ts +34 -13
- package/src/skia/types/Data/Data.ts +14 -4
- package/src/skia/web/JsiSkFont.ts +48 -4
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const __esModule: boolean;
|
|
2
2
|
export class JsiSkFont extends _Host.HostObject {
|
|
3
3
|
constructor(CanvasKit: any, ref: any);
|
|
4
|
-
measureText(
|
|
4
|
+
measureText(text: any, paint: any): _JsiSkRect.JsiSkRect;
|
|
5
5
|
getTextWidth(text: any, paint: any): any;
|
|
6
6
|
getMetrics(): {
|
|
7
7
|
ascent: any;
|
|
@@ -195,6 +195,7 @@ export function Mock(CanvasKit: any): {
|
|
|
195
195
|
rrect: (r: any, rx: any, ry: any) => import("../../..").SkRRect;
|
|
196
196
|
processTransform2d: (transforms: any) => any;
|
|
197
197
|
isRNModule: (mod: any) => mod is number;
|
|
198
|
+
unwrapModule: (mod: any) => any;
|
|
198
199
|
ColorSpace: {
|
|
199
200
|
SRGB: string;
|
|
200
201
|
DisplayP3: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export class JsiSkFont extends HostObject {
|
|
2
2
|
constructor(CanvasKit: any, ref: any);
|
|
3
|
-
measureText(
|
|
3
|
+
measureText(text: any, paint: any): JsiSkRect;
|
|
4
4
|
getTextWidth(text: any, paint: any): any;
|
|
5
5
|
getMetrics(): {
|
|
6
6
|
ascent: any;
|
|
@@ -1,18 +1,33 @@
|
|
|
1
|
+
import { FontSlant, FontWeight } from "../types";
|
|
1
2
|
import type { DataModule, DataSourceParam, SkFontMgr } from "../types";
|
|
2
3
|
import type { SkTypefaceFontProvider } from "../types/Paragraph/TypefaceFontProvider";
|
|
3
4
|
/**
|
|
4
5
|
* Returns a Skia Font object
|
|
5
6
|
* */
|
|
6
7
|
export declare const useFont: (font: DataSourceParam, size?: number, onError?: (err: Error) => void) => import("../types").SkFont | null;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* React Native style font slant, as found in the `fontStyle` property of
|
|
10
|
+
* `TextStyle`. The Skia {@link FontSlant} enum is accepted as well.
|
|
11
|
+
*/
|
|
12
|
+
export type RNFontSlant = "normal" | "italic" | "oblique" | FontSlant;
|
|
13
|
+
/**
|
|
14
|
+
* React Native style font weight, as found in the `fontWeight` property of
|
|
15
|
+
* `TextStyle`. Numeric weights such as the {@link FontWeight} enum values
|
|
16
|
+
* used by the Paragraph API are accepted as well.
|
|
17
|
+
*/
|
|
18
|
+
export type RNFontWeight = "normal" | "bold" | "100" | "200" | "300" | "400" | "500" | "600" | "700" | "800" | "900" | FontWeight;
|
|
19
|
+
/**
|
|
20
|
+
* Font style accepted by {@link matchFont}.
|
|
21
|
+
* `fontStyle` and `fontWeight` accept both the React Native string values
|
|
22
|
+
* and the Skia enums ({@link FontSlant} and {@link FontWeight}), so the same
|
|
23
|
+
* values can be shared with the Paragraph API.
|
|
24
|
+
*/
|
|
25
|
+
export interface RNFontStyle {
|
|
10
26
|
fontFamily: string;
|
|
11
27
|
fontSize: number;
|
|
12
|
-
fontStyle:
|
|
13
|
-
fontWeight:
|
|
28
|
+
fontStyle: RNFontSlant;
|
|
29
|
+
fontWeight: RNFontWeight;
|
|
14
30
|
}
|
|
15
31
|
export declare const matchFont: (inputStyle?: Partial<RNFontStyle>, fontMgr?: SkFontMgr) => import("../types").SkFont;
|
|
16
32
|
export declare const listFontFamilies: (fontMgr?: SkFontMgr) => string[];
|
|
17
33
|
export declare const useFonts: (sources: Record<string, DataModule[]>) => SkTypefaceFontProvider | null;
|
|
18
|
-
export {};
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import type { SkJSIInstance } from "../JsiInstance";
|
|
2
2
|
export type SkData = SkJSIInstance<"Data">;
|
|
3
3
|
type RNModule = number;
|
|
4
|
-
type ESModule = {
|
|
5
|
-
__esModule: true;
|
|
6
|
-
default: string;
|
|
7
|
-
};
|
|
8
4
|
type MetroAsset = {
|
|
9
5
|
uri: string;
|
|
10
6
|
width: number;
|
|
11
7
|
height: number;
|
|
12
8
|
};
|
|
9
|
+
type ESModule = {
|
|
10
|
+
__esModule: true;
|
|
11
|
+
default: RNModule | MetroAsset | string;
|
|
12
|
+
};
|
|
13
13
|
export type DataModule = RNModule | ESModule | MetroAsset;
|
|
14
14
|
export type DataSource = DataModule | string | Uint8Array;
|
|
15
15
|
export type DataSourceParam = DataSource | null | undefined;
|
|
16
16
|
export declare const isRNModule: (mod: DataModule) => mod is RNModule;
|
|
17
|
+
export declare const unwrapModule: (mod: DataModule) => RNModule | MetroAsset | string;
|
|
17
18
|
export {};
|
|
@@ -5,7 +5,7 @@ import { JsiSkRect } from "./JsiSkRect";
|
|
|
5
5
|
import { JsiSkTypeface } from "./JsiSkTypeface";
|
|
6
6
|
export declare class JsiSkFont extends HostObject<Font, "Font"> implements SkFont {
|
|
7
7
|
constructor(CanvasKit: CanvasKit, ref: Font);
|
|
8
|
-
measureText(
|
|
8
|
+
measureText(text: string, paint?: SkPaint | undefined): SkRect;
|
|
9
9
|
getTextWidth(text: string, paint?: SkPaint | undefined): number;
|
|
10
10
|
getMetrics(): {
|
|
11
11
|
ascent: number;
|
package/libs/.graphite
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
m150
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"setup-skia-web": "scripts/setup-canvaskit.js"
|
|
9
9
|
},
|
|
10
10
|
"title": "React Native Skia",
|
|
11
|
-
"version": "2.
|
|
11
|
+
"version": "2.9.1-next.1",
|
|
12
12
|
"description": "High-performance React Native Graphics using Skia",
|
|
13
13
|
"main": "lib/module/index.js",
|
|
14
14
|
"react-native": "src/index.ts",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"cpp/**/*.{h,cpp}",
|
|
33
33
|
"apple/**",
|
|
34
34
|
"react-native-skia.podspec",
|
|
35
|
-
"dist/**"
|
|
35
|
+
"dist/**",
|
|
36
|
+
"libs/.graphite"
|
|
36
37
|
],
|
|
37
38
|
"scripts": {
|
|
38
39
|
"lint": "eslint . --ext .ts,.tsx --max-warnings 0 --cache --fix",
|
|
@@ -134,12 +135,16 @@
|
|
|
134
135
|
},
|
|
135
136
|
"dependencies": {
|
|
136
137
|
"canvaskit-wasm": "0.41.0",
|
|
137
|
-
"react-native-skia-android": "150.0.0",
|
|
138
|
-
"react-native-skia-apple-ios": "150.0.0",
|
|
139
|
-
"react-native-skia-apple-macos": "150.0.0",
|
|
140
|
-
"react-native-skia-apple-tvos": "150.0.0",
|
|
138
|
+
"react-native-skia-graphite-android": "150.0.0",
|
|
139
|
+
"react-native-skia-graphite-apple-ios": "150.0.0",
|
|
140
|
+
"react-native-skia-graphite-apple-macos": "150.0.0",
|
|
141
141
|
"react-reconciler": "0.31.0"
|
|
142
142
|
},
|
|
143
|
+
"graphiteDependencies": {
|
|
144
|
+
"react-native-skia-graphite-android": "150.0.0",
|
|
145
|
+
"react-native-skia-graphite-apple-ios": "150.0.0",
|
|
146
|
+
"react-native-skia-graphite-apple-macos": "150.0.0"
|
|
147
|
+
},
|
|
143
148
|
"eslintIgnore": [
|
|
144
149
|
"node_modules/",
|
|
145
150
|
"lib/"
|
|
@@ -176,5 +181,6 @@
|
|
|
176
181
|
}
|
|
177
182
|
]
|
|
178
183
|
]
|
|
179
|
-
}
|
|
184
|
+
},
|
|
185
|
+
"stableVersion": "0.0.0"
|
|
180
186
|
}
|
|
@@ -29,10 +29,8 @@ end
|
|
|
29
29
|
# re-copied and CocoaPods picks up the change. This is best-effort: if `pod install`
|
|
30
30
|
# does not detect the change, a clean reinstall fixes it (acceptable until the upcoming
|
|
31
31
|
# Swift Package Manager migration).
|
|
32
|
-
install_apple_skia_libs = lambda do |base_dir|
|
|
33
|
-
|
|
34
|
-
'macos' => 'react-native-skia-apple-macos',
|
|
35
|
-
'tvos' => 'react-native-skia-apple-tvos' }.each do |platform, pkg_name|
|
|
32
|
+
install_apple_skia_libs = lambda do |base_dir, packages|
|
|
33
|
+
packages.each do |platform, pkg_name|
|
|
36
34
|
pkg_dir = resolve_node_package.call(pkg_name, base_dir)
|
|
37
35
|
next if pkg_dir.nil?
|
|
38
36
|
|
|
@@ -54,9 +52,18 @@ install_apple_skia_libs = lambda do |base_dir|
|
|
|
54
52
|
end
|
|
55
53
|
end
|
|
56
54
|
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
|
|
55
|
+
# The default (Ganesh) build ships its binaries in the react-native-skia-apple-*
|
|
56
|
+
# npm packages, the Graphite build in react-native-skia-graphite-apple-* (no tvOS).
|
|
57
|
+
# During in-repo development install-skia-graphite downloads the binaries directly
|
|
58
|
+
# into libs/ and the graphite packages are absent from node_modules, in which case
|
|
59
|
+
# the copy below is a no-op and the downloaded binaries are used as-is.
|
|
60
|
+
apple_skia_packages = use_graphite ?
|
|
61
|
+
{ 'ios' => 'react-native-skia-graphite-apple-ios',
|
|
62
|
+
'macos' => 'react-native-skia-graphite-apple-macos' } :
|
|
63
|
+
{ 'ios' => 'react-native-skia-apple-ios',
|
|
64
|
+
'macos' => 'react-native-skia-apple-macos',
|
|
65
|
+
'tvos' => 'react-native-skia-apple-tvos' }
|
|
66
|
+
install_apple_skia_libs.call(__dir__, apple_skia_packages)
|
|
60
67
|
|
|
61
68
|
# Set preprocessor definitions based on GRAPHITE flag
|
|
62
69
|
preprocessor_defs = use_graphite ?
|
|
@@ -71,14 +78,15 @@ framework_names = ['libskia', 'libsvg', 'libskshaper', 'libskparagraph',
|
|
|
71
78
|
# Add Dawn library for Graphite builds (contains dawn::native symbols)
|
|
72
79
|
framework_names += ['libdawn_combined'] if use_graphite
|
|
73
80
|
|
|
74
|
-
# Verify that the prebuilt binaries are available (copied in above
|
|
75
|
-
# install-skia-graphite for Graphite builds).
|
|
81
|
+
# Verify that the prebuilt binaries are available (copied in above from the npm
|
|
82
|
+
# packages, or downloaded by install-skia-graphite for in-repo Graphite builds).
|
|
76
83
|
unless Dir.exist?(File.join(__dir__, 'libs', 'ios')) && Dir.exist?(File.join(__dir__, 'libs', 'macos'))
|
|
84
|
+
expected_packages = apple_skia_packages.values.join(', ')
|
|
77
85
|
Pod::UI.warn "#{'-' * 72}"
|
|
78
86
|
Pod::UI.warn "react-native-skia: Skia prebuilt binaries not found in libs/!"
|
|
79
87
|
Pod::UI.warn ""
|
|
80
88
|
Pod::UI.warn "Make sure dependencies are installed (yarn install / npm install) so that"
|
|
81
|
-
Pod::UI.warn "the
|
|
89
|
+
Pod::UI.warn "the #{expected_packages} packages are present, then run `pod install` again."
|
|
82
90
|
Pod::UI.warn "#{'-' * 72}"
|
|
83
91
|
raise "react-native-skia: Skia prebuilt binaries not found. Run `yarn install` then `pod install` to fix this."
|
|
84
92
|
end
|
package/src/Platform/Platform.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from "react-native";
|
|
8
8
|
|
|
9
9
|
import type { DataModule } from "../skia/types";
|
|
10
|
-
import { isRNModule } from "../skia/types";
|
|
10
|
+
import { isRNModule, unwrapModule } from "../skia/types";
|
|
11
11
|
|
|
12
12
|
import type { IPlatform } from "./IPlatform";
|
|
13
13
|
|
|
@@ -15,12 +15,11 @@ export const Platform: IPlatform = {
|
|
|
15
15
|
OS: RNPlatform.OS,
|
|
16
16
|
PixelRatio: PixelRatio.get(),
|
|
17
17
|
resolveAsset: (source: DataModule) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
: source.default;
|
|
18
|
+
const asset = unwrapModule(source);
|
|
19
|
+
if (typeof asset === "string") {
|
|
20
|
+
return asset;
|
|
21
|
+
}
|
|
22
|
+
return isRNModule(asset) ? Image.resolveAssetSource(asset).uri : asset.uri;
|
|
24
23
|
},
|
|
25
24
|
findNodeHandle,
|
|
26
25
|
View,
|
|
@@ -3,7 +3,7 @@ import React, { useMemo } from "react";
|
|
|
3
3
|
import type { ViewComponent, ViewProps } from "react-native";
|
|
4
4
|
|
|
5
5
|
import type { DataModule } from "../skia/types";
|
|
6
|
-
import { isRNModule } from "../skia/types";
|
|
6
|
+
import { isRNModule, unwrapModule } from "../skia/types";
|
|
7
7
|
|
|
8
8
|
import type { IPlatform } from "./IPlatform";
|
|
9
9
|
|
|
@@ -40,12 +40,16 @@ export const Platform: IPlatform = {
|
|
|
40
40
|
OS: "web",
|
|
41
41
|
PixelRatio: typeof window !== "undefined" ? window.devicePixelRatio : 1, // window is not defined on node
|
|
42
42
|
resolveAsset: (source: DataModule) => {
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
const asset = unwrapModule(source);
|
|
44
|
+
if (typeof asset === "string") {
|
|
45
|
+
return asset;
|
|
46
|
+
}
|
|
47
|
+
if (isRNModule(asset)) {
|
|
48
|
+
if (typeof require === "function") {
|
|
45
49
|
const {
|
|
46
50
|
getAssetByID,
|
|
47
51
|
} = require("react-native/Libraries/Image/AssetRegistry");
|
|
48
|
-
const { httpServerLocation, name, type } = getAssetByID(
|
|
52
|
+
const { httpServerLocation, name, type } = getAssetByID(asset);
|
|
49
53
|
const uri = `${httpServerLocation}/${name}.${type}`;
|
|
50
54
|
return uri;
|
|
51
55
|
}
|
|
@@ -53,10 +57,7 @@ export const Platform: IPlatform = {
|
|
|
53
57
|
"Asset source is a number - this is not supported on the web"
|
|
54
58
|
);
|
|
55
59
|
}
|
|
56
|
-
|
|
57
|
-
return source.uri;
|
|
58
|
-
}
|
|
59
|
-
return source.default;
|
|
60
|
+
return asset.uri;
|
|
60
61
|
},
|
|
61
62
|
findNodeHandle: () => {
|
|
62
63
|
throw new Error("findNodeHandle is not supported on the web");
|
package/src/skia/core/Font.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { useEffect, useMemo, useState } from "react";
|
|
3
3
|
|
|
4
4
|
import { Skia } from "../Skia";
|
|
5
|
-
import { FontSlant } from "../types";
|
|
5
|
+
import { FontSlant, FontWeight } from "../types";
|
|
6
6
|
import type { DataModule, DataSourceParam, SkFontMgr } from "../types";
|
|
7
7
|
import { Platform } from "../../Platform";
|
|
8
8
|
import type { SkTypefaceFontProvider } from "../types/Paragraph/TypefaceFontProvider";
|
|
@@ -29,8 +29,18 @@ export const useFont = (
|
|
|
29
29
|
}, [size, typeface]);
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
/**
|
|
33
|
+
* React Native style font slant, as found in the `fontStyle` property of
|
|
34
|
+
* `TextStyle`. The Skia {@link FontSlant} enum is accepted as well.
|
|
35
|
+
*/
|
|
36
|
+
export type RNFontSlant = "normal" | "italic" | "oblique" | FontSlant;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* React Native style font weight, as found in the `fontWeight` property of
|
|
40
|
+
* `TextStyle`. Numeric weights such as the {@link FontWeight} enum values
|
|
41
|
+
* used by the Paragraph API are accepted as well.
|
|
42
|
+
*/
|
|
43
|
+
export type RNFontWeight =
|
|
34
44
|
| "normal"
|
|
35
45
|
| "bold"
|
|
36
46
|
| "100"
|
|
@@ -41,13 +51,20 @@ type Weight =
|
|
|
41
51
|
| "600"
|
|
42
52
|
| "700"
|
|
43
53
|
| "800"
|
|
44
|
-
| "900"
|
|
54
|
+
| "900"
|
|
55
|
+
| FontWeight;
|
|
45
56
|
|
|
46
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Font style accepted by {@link matchFont}.
|
|
59
|
+
* `fontStyle` and `fontWeight` accept both the React Native string values
|
|
60
|
+
* and the Skia enums ({@link FontSlant} and {@link FontWeight}), so the same
|
|
61
|
+
* values can be shared with the Paragraph API.
|
|
62
|
+
*/
|
|
63
|
+
export interface RNFontStyle {
|
|
47
64
|
fontFamily: string;
|
|
48
65
|
fontSize: number;
|
|
49
|
-
fontStyle:
|
|
50
|
-
fontWeight:
|
|
66
|
+
fontStyle: RNFontSlant;
|
|
67
|
+
fontWeight: RNFontWeight;
|
|
51
68
|
}
|
|
52
69
|
|
|
53
70
|
const defaultFontStyle: RNFontStyle = {
|
|
@@ -57,8 +74,10 @@ const defaultFontStyle: RNFontStyle = {
|
|
|
57
74
|
fontWeight: "normal",
|
|
58
75
|
};
|
|
59
76
|
|
|
60
|
-
const slant = (s:
|
|
61
|
-
if (s === "
|
|
77
|
+
const slant = (s: RNFontSlant): FontSlant => {
|
|
78
|
+
if (typeof s === "number") {
|
|
79
|
+
return s;
|
|
80
|
+
} else if (s === "italic") {
|
|
62
81
|
return FontSlant.Italic;
|
|
63
82
|
} else if (s === "oblique") {
|
|
64
83
|
return FontSlant.Oblique;
|
|
@@ -67,14 +86,16 @@ const slant = (s: Slant) => {
|
|
|
67
86
|
}
|
|
68
87
|
};
|
|
69
88
|
|
|
70
|
-
const weight = (fontWeight:
|
|
89
|
+
const weight = (fontWeight: RNFontWeight): FontWeight => {
|
|
71
90
|
switch (fontWeight) {
|
|
72
91
|
case "normal":
|
|
73
|
-
return
|
|
92
|
+
return FontWeight.Normal;
|
|
74
93
|
case "bold":
|
|
75
|
-
return
|
|
94
|
+
return FontWeight.Bold;
|
|
76
95
|
default:
|
|
77
|
-
return
|
|
96
|
+
return typeof fontWeight === "number"
|
|
97
|
+
? fontWeight
|
|
98
|
+
: (parseInt(fontWeight, 10) as FontWeight);
|
|
78
99
|
}
|
|
79
100
|
};
|
|
80
101
|
|
|
@@ -3,15 +3,15 @@ import type { SkJSIInstance } from "../JsiInstance";
|
|
|
3
3
|
export type SkData = SkJSIInstance<"Data">;
|
|
4
4
|
|
|
5
5
|
type RNModule = number;
|
|
6
|
-
type ESModule = {
|
|
7
|
-
__esModule: true;
|
|
8
|
-
default: string;
|
|
9
|
-
};
|
|
10
6
|
type MetroAsset = {
|
|
11
7
|
uri: string;
|
|
12
8
|
width: number;
|
|
13
9
|
height: number;
|
|
14
10
|
};
|
|
11
|
+
type ESModule = {
|
|
12
|
+
__esModule: true;
|
|
13
|
+
default: RNModule | MetroAsset | string;
|
|
14
|
+
};
|
|
15
15
|
|
|
16
16
|
export type DataModule = RNModule | ESModule | MetroAsset;
|
|
17
17
|
export type DataSource = DataModule | string | Uint8Array;
|
|
@@ -19,3 +19,13 @@ export type DataSourceParam = DataSource | null | undefined;
|
|
|
19
19
|
|
|
20
20
|
export const isRNModule = (mod: DataModule): mod is RNModule =>
|
|
21
21
|
typeof mod === "number";
|
|
22
|
+
|
|
23
|
+
// Since Expo SDK 52, on web, require() may return an ES module interop
|
|
24
|
+
// object ({ default: <asset> }) instead of the asset itself.
|
|
25
|
+
// See https://github.com/Shopify/react-native-skia/issues/2784
|
|
26
|
+
export const unwrapModule = (
|
|
27
|
+
mod: DataModule
|
|
28
|
+
): RNModule | MetroAsset | string =>
|
|
29
|
+
typeof mod === "object" && mod !== null && "default" in mod
|
|
30
|
+
? mod.default
|
|
31
|
+
: mod;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CanvasKit, Font } from "canvaskit-wasm";
|
|
1
|
+
import type { CanvasKit, Font, Paint } from "canvaskit-wasm";
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
4
|
FontEdging,
|
|
@@ -10,7 +10,7 @@ import type {
|
|
|
10
10
|
SkTypeface,
|
|
11
11
|
} from "../types";
|
|
12
12
|
|
|
13
|
-
import { HostObject, getEnum
|
|
13
|
+
import { HostObject, getEnum } from "./Host";
|
|
14
14
|
import { JsiSkPaint } from "./JsiSkPaint";
|
|
15
15
|
import { JsiSkPoint } from "./JsiSkPoint";
|
|
16
16
|
import { JsiSkRect } from "./JsiSkRect";
|
|
@@ -21,8 +21,52 @@ export class JsiSkFont extends HostObject<Font, "Font"> implements SkFont {
|
|
|
21
21
|
super(CanvasKit, ref, "Font");
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
measureText(
|
|
25
|
-
|
|
24
|
+
measureText(text: string, paint?: SkPaint | undefined): SkRect {
|
|
25
|
+
// CanvasKit doesn't expose SkFont::measureText directly, so we polyfill
|
|
26
|
+
// it: for each glyph we take its ink bounds (relative to its own origin),
|
|
27
|
+
// offset it by the accumulated advance, and union the results. This
|
|
28
|
+
// matches the bounds computed natively by SkFont::measureText.
|
|
29
|
+
const glyphs = this.ref.getGlyphIDs(text);
|
|
30
|
+
if (glyphs.length === 0) {
|
|
31
|
+
return new JsiSkRect(this.CanvasKit, this.CanvasKit.XYWHRect(0, 0, 0, 0));
|
|
32
|
+
}
|
|
33
|
+
const skPaint = paint ? JsiSkPaint.fromValue<Paint>(paint) : null;
|
|
34
|
+
// Flattened rectangles: 4 floats (left, top, right, bottom) per glyph.
|
|
35
|
+
const bounds = this.ref.getGlyphBounds(glyphs, skPaint);
|
|
36
|
+
const advances = this.ref.getGlyphWidths(glyphs, skPaint);
|
|
37
|
+
let left = 0;
|
|
38
|
+
let top = 0;
|
|
39
|
+
let right = 0;
|
|
40
|
+
let bottom = 0;
|
|
41
|
+
let isEmpty = true;
|
|
42
|
+
let xPos = 0;
|
|
43
|
+
for (let i = 0; i < glyphs.length; i++) {
|
|
44
|
+
const l = bounds[i * 4] + xPos;
|
|
45
|
+
const t = bounds[i * 4 + 1];
|
|
46
|
+
const r = bounds[i * 4 + 2] + xPos;
|
|
47
|
+
const b = bounds[i * 4 + 3];
|
|
48
|
+
xPos += advances[i];
|
|
49
|
+
// Skip empty glyph bounds (e.g. whitespace), like SkRect::join does.
|
|
50
|
+
if (l >= r || t >= b) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (isEmpty) {
|
|
54
|
+
left = l;
|
|
55
|
+
top = t;
|
|
56
|
+
right = r;
|
|
57
|
+
bottom = b;
|
|
58
|
+
isEmpty = false;
|
|
59
|
+
} else {
|
|
60
|
+
left = Math.min(left, l);
|
|
61
|
+
top = Math.min(top, t);
|
|
62
|
+
right = Math.max(right, r);
|
|
63
|
+
bottom = Math.max(bottom, b);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return new JsiSkRect(
|
|
67
|
+
this.CanvasKit,
|
|
68
|
+
this.CanvasKit.LTRBRect(left, top, right, bottom)
|
|
69
|
+
);
|
|
26
70
|
}
|
|
27
71
|
|
|
28
72
|
getTextWidth(text: string, paint?: SkPaint | undefined) {
|