@shopify/react-native-skia 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/android/build.gradle +10 -4
  2. package/android/cpp/rnskia-android/OpenGLContext.h +29 -3
  3. package/android/cpp/rnskia-android/OpenGLWindowContext.cpp +9 -2
  4. package/android/cpp/rnskia-android/gl/Context.h +3 -0
  5. package/android/cpp/rnskia-android/gl/Surface.h +1 -1
  6. package/cpp/api/JsiSkiaContext.h +4 -0
  7. package/cpp/rnskia/RNSkView.h +6 -0
  8. package/lib/commonjs/Platform/Platform.js +5 -2
  9. package/lib/commonjs/Platform/Platform.js.map +1 -1
  10. package/lib/commonjs/Platform/Platform.web.js +8 -7
  11. package/lib/commonjs/Platform/Platform.web.js.map +1 -1
  12. package/lib/commonjs/skia/core/Font.d.ts +21 -6
  13. package/lib/commonjs/skia/core/Font.js +24 -4
  14. package/lib/commonjs/skia/core/Font.js.map +1 -1
  15. package/lib/commonjs/skia/types/Data/Data.d.ts +5 -4
  16. package/lib/commonjs/skia/types/Data/Data.js +7 -1
  17. package/lib/commonjs/skia/types/Data/Data.js.map +1 -1
  18. package/lib/commonjs/skia/web/JsiSkFont.d.ts +1 -1
  19. package/lib/commonjs/skia/web/JsiSkFont.js +43 -2
  20. package/lib/commonjs/skia/web/JsiSkFont.js.map +1 -1
  21. package/lib/module/Platform/Platform.js +6 -3
  22. package/lib/module/Platform/Platform.js.map +1 -1
  23. package/lib/module/Platform/Platform.web.js +9 -8
  24. package/lib/module/Platform/Platform.web.js.map +1 -1
  25. package/lib/module/skia/core/Font.d.ts +21 -6
  26. package/lib/module/skia/core/Font.js +26 -5
  27. package/lib/module/skia/core/Font.js.map +1 -1
  28. package/lib/module/skia/types/Data/Data.d.ts +5 -4
  29. package/lib/module/skia/types/Data/Data.js +5 -0
  30. package/lib/module/skia/types/Data/Data.js.map +1 -1
  31. package/lib/module/skia/web/JsiSkFont.d.ts +1 -1
  32. package/lib/module/skia/web/JsiSkFont.js +44 -3
  33. package/lib/module/skia/web/JsiSkFont.js.map +1 -1
  34. package/lib/typescript/lib/commonjs/skia/types/Data/Data.d.ts +1 -0
  35. package/lib/typescript/lib/commonjs/skia/web/JsiSkFont.d.ts +1 -1
  36. package/lib/typescript/lib/module/mock/index.d.ts +1 -0
  37. package/lib/typescript/lib/module/skia/types/Data/Data.d.ts +1 -0
  38. package/lib/typescript/lib/module/skia/web/JsiSkFont.d.ts +1 -1
  39. package/lib/typescript/src/skia/core/Font.d.ts +21 -6
  40. package/lib/typescript/src/skia/types/Data/Data.d.ts +5 -4
  41. package/lib/typescript/src/skia/web/JsiSkFont.d.ts +1 -1
  42. package/package.json +8 -2
  43. package/react-native-skia.podspec +18 -10
  44. package/src/Platform/Platform.ts +6 -7
  45. package/src/Platform/Platform.web.tsx +9 -8
  46. package/src/skia/core/Font.ts +34 -13
  47. package/src/skia/types/Data/Data.ts +14 -4
  48. package/src/skia/web/JsiSkFont.ts +48 -4
@@ -1,5 +1,5 @@
1
1
  import React, { useMemo } from "react";
2
- import { isRNModule } from "../skia/types";
2
+ import { isRNModule, unwrapModule } from "../skia/types";
3
3
  // Layout is observed by the views themselves (see SkiaPictureView.web.tsx):
4
4
  // this shim only reproduces react-native-web's default View styling.
5
5
  const View = ({
@@ -37,8 +37,12 @@ export const Platform = {
37
37
  PixelRatio: typeof window !== "undefined" ? window.devicePixelRatio : 1,
38
38
  // window is not defined on node
39
39
  resolveAsset: source => {
40
- if (isRNModule(source)) {
41
- if (typeof source === "number" && typeof require === "function") {
40
+ const asset = unwrapModule(source);
41
+ if (typeof asset === "string") {
42
+ return asset;
43
+ }
44
+ if (isRNModule(asset)) {
45
+ if (typeof require === "function") {
42
46
  const {
43
47
  getAssetByID
44
48
  } = require("react-native/Libraries/Image/AssetRegistry");
@@ -46,16 +50,13 @@ export const Platform = {
46
50
  httpServerLocation,
47
51
  name,
48
52
  type
49
- } = getAssetByID(source);
53
+ } = getAssetByID(asset);
50
54
  const uri = `${httpServerLocation}/${name}.${type}`;
51
55
  return uri;
52
56
  }
53
57
  throw new Error("Asset source is a number - this is not supported on the web");
54
58
  }
55
- if ("uri" in source) {
56
- return source.uri;
57
- }
58
- return source.default;
59
+ return asset.uri;
59
60
  },
60
61
  findNodeHandle: () => {
61
62
  throw new Error("findNodeHandle is not supported on the web");
@@ -1 +1 @@
1
- {"version":3,"names":["React","useMemo","isRNModule","View","children","style","rawStyle","cssStyles","alignItems","backgroundColor","border","boxSizing","display","flexBasis","flexDirection","flexShrink","listStyle","margin","minHeight","minWidth","padding","position","textDecoration","zIndex","createElement","Platform","OS","PixelRatio","window","devicePixelRatio","resolveAsset","source","require","getAssetByID","httpServerLocation","name","type","uri","Error","default","findNodeHandle"],"sources":["Platform.web.tsx"],"sourcesContent":["import type { CSSProperties } from \"react\";\nimport React, { useMemo } from \"react\";\nimport type { ViewComponent, ViewProps } from \"react-native\";\n\nimport type { DataModule } from \"../skia/types\";\nimport { isRNModule } from \"../skia/types\";\n\nimport type { IPlatform } from \"./IPlatform\";\n\n// Layout is observed by the views themselves (see SkiaPictureView.web.tsx):\n// this shim only reproduces react-native-web's default View styling.\nconst View = (({ children, style: rawStyle }: ViewProps) => {\n const style = useMemo(() => (rawStyle ?? {}) as CSSProperties, [rawStyle]);\n const cssStyles = useMemo(() => {\n return {\n alignItems: \"stretch\" as const,\n backgroundColor: \"transparent\" as const,\n border: \"0 solid black\" as const,\n boxSizing: \"border-box\" as const,\n display: \"flex\" as const,\n flexBasis: \"auto\" as const,\n flexDirection: \"column\" as const,\n flexShrink: 0,\n listStyle: \"none\" as const,\n margin: 0,\n minHeight: 0,\n minWidth: 0,\n padding: 0,\n position: \"relative\" as const,\n textDecoration: \"none\" as const,\n zIndex: 0,\n ...style,\n };\n }, [style]);\n\n return <div style={cssStyles}>{children}</div>;\n}) as unknown as typeof ViewComponent;\n\nexport const Platform: IPlatform = {\n OS: \"web\",\n PixelRatio: typeof window !== \"undefined\" ? window.devicePixelRatio : 1, // window is not defined on node\n resolveAsset: (source: DataModule) => {\n if (isRNModule(source)) {\n if (typeof source === \"number\" && typeof require === \"function\") {\n const {\n getAssetByID,\n } = require(\"react-native/Libraries/Image/AssetRegistry\");\n const { httpServerLocation, name, type } = getAssetByID(source);\n const uri = `${httpServerLocation}/${name}.${type}`;\n return uri;\n }\n throw new Error(\n \"Asset source is a number - this is not supported on the web\"\n );\n }\n if (\"uri\" in source) {\n return source.uri;\n }\n return source.default;\n },\n findNodeHandle: () => {\n throw new Error(\"findNodeHandle is not supported on the web\");\n },\n View,\n};\n"],"mappings":"AACA,OAAOA,KAAK,IAAIC,OAAO,QAAQ,OAAO;AAItC,SAASC,UAAU,QAAQ,eAAe;AAI1C;AACA;AACA,MAAMC,IAAI,GAAIA,CAAC;EAAEC,QAAQ;EAAEC,KAAK,EAAEC;AAAoB,CAAC,KAAK;EAC1D,MAAMD,KAAK,GAAGJ,OAAO,CAAC,MAAOK,QAAQ,aAARA,QAAQ,cAARA,QAAQ,GAAI,CAAC,CAAmB,EAAE,CAACA,QAAQ,CAAC,CAAC;EAC1E,MAAMC,SAAS,GAAGN,OAAO,CAAC,MAAM;IAC9B,OAAO;MACLO,UAAU,EAAE,SAAkB;MAC9BC,eAAe,EAAE,aAAsB;MACvCC,MAAM,EAAE,eAAwB;MAChCC,SAAS,EAAE,YAAqB;MAChCC,OAAO,EAAE,MAAe;MACxBC,SAAS,EAAE,MAAe;MAC1BC,aAAa,EAAE,QAAiB;MAChCC,UAAU,EAAE,CAAC;MACbC,SAAS,EAAE,MAAe;MAC1BC,MAAM,EAAE,CAAC;MACTC,SAAS,EAAE,CAAC;MACZC,QAAQ,EAAE,CAAC;MACXC,OAAO,EAAE,CAAC;MACVC,QAAQ,EAAE,UAAmB;MAC7BC,cAAc,EAAE,MAAe;MAC/BC,MAAM,EAAE,CAAC;MACT,GAAGlB;IACL,CAAC;EACH,CAAC,EAAE,CAACA,KAAK,CAAC,CAAC;EAEX,oBAAOL,KAAA,CAAAwB,aAAA;IAAKnB,KAAK,EAAEE;EAAU,GAAEH,QAAc,CAAC;AAChD,CAAqC;AAErC,OAAO,MAAMqB,QAAmB,GAAG;EACjCC,EAAE,EAAE,KAAK;EACTC,UAAU,EAAE,OAAOC,MAAM,KAAK,WAAW,GAAGA,MAAM,CAACC,gBAAgB,GAAG,CAAC;EAAE;EACzEC,YAAY,EAAGC,MAAkB,IAAK;IACpC,IAAI7B,UAAU,CAAC6B,MAAM,CAAC,EAAE;MACtB,IAAI,OAAOA,MAAM,KAAK,QAAQ,IAAI,OAAOC,OAAO,KAAK,UAAU,EAAE;QAC/D,MAAM;UACJC;QACF,CAAC,GAAGD,OAAO,CAAC,4CAA4C,CAAC;QACzD,MAAM;UAAEE,kBAAkB;UAAEC,IAAI;UAAEC;QAAK,CAAC,GAAGH,YAAY,CAACF,MAAM,CAAC;QAC/D,MAAMM,GAAG,GAAG,GAAGH,kBAAkB,IAAIC,IAAI,IAAIC,IAAI,EAAE;QACnD,OAAOC,GAAG;MACZ;MACA,MAAM,IAAIC,KAAK,CACb,6DACF,CAAC;IACH;IACA,IAAI,KAAK,IAAIP,MAAM,EAAE;MACnB,OAAOA,MAAM,CAACM,GAAG;IACnB;IACA,OAAON,MAAM,CAACQ,OAAO;EACvB,CAAC;EACDC,cAAc,EAAEA,CAAA,KAAM;IACpB,MAAM,IAAIF,KAAK,CAAC,4CAA4C,CAAC;EAC/D,CAAC;EACDnC;AACF,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["React","useMemo","isRNModule","unwrapModule","View","children","style","rawStyle","cssStyles","alignItems","backgroundColor","border","boxSizing","display","flexBasis","flexDirection","flexShrink","listStyle","margin","minHeight","minWidth","padding","position","textDecoration","zIndex","createElement","Platform","OS","PixelRatio","window","devicePixelRatio","resolveAsset","source","asset","require","getAssetByID","httpServerLocation","name","type","uri","Error","findNodeHandle"],"sources":["Platform.web.tsx"],"sourcesContent":["import type { CSSProperties } from \"react\";\nimport React, { useMemo } from \"react\";\nimport type { ViewComponent, ViewProps } from \"react-native\";\n\nimport type { DataModule } from \"../skia/types\";\nimport { isRNModule, unwrapModule } from \"../skia/types\";\n\nimport type { IPlatform } from \"./IPlatform\";\n\n// Layout is observed by the views themselves (see SkiaPictureView.web.tsx):\n// this shim only reproduces react-native-web's default View styling.\nconst View = (({ children, style: rawStyle }: ViewProps) => {\n const style = useMemo(() => (rawStyle ?? {}) as CSSProperties, [rawStyle]);\n const cssStyles = useMemo(() => {\n return {\n alignItems: \"stretch\" as const,\n backgroundColor: \"transparent\" as const,\n border: \"0 solid black\" as const,\n boxSizing: \"border-box\" as const,\n display: \"flex\" as const,\n flexBasis: \"auto\" as const,\n flexDirection: \"column\" as const,\n flexShrink: 0,\n listStyle: \"none\" as const,\n margin: 0,\n minHeight: 0,\n minWidth: 0,\n padding: 0,\n position: \"relative\" as const,\n textDecoration: \"none\" as const,\n zIndex: 0,\n ...style,\n };\n }, [style]);\n\n return <div style={cssStyles}>{children}</div>;\n}) as unknown as typeof ViewComponent;\n\nexport const Platform: IPlatform = {\n OS: \"web\",\n PixelRatio: typeof window !== \"undefined\" ? window.devicePixelRatio : 1, // window is not defined on node\n resolveAsset: (source: DataModule) => {\n const asset = unwrapModule(source);\n if (typeof asset === \"string\") {\n return asset;\n }\n if (isRNModule(asset)) {\n if (typeof require === \"function\") {\n const {\n getAssetByID,\n } = require(\"react-native/Libraries/Image/AssetRegistry\");\n const { httpServerLocation, name, type } = getAssetByID(asset);\n const uri = `${httpServerLocation}/${name}.${type}`;\n return uri;\n }\n throw new Error(\n \"Asset source is a number - this is not supported on the web\"\n );\n }\n return asset.uri;\n },\n findNodeHandle: () => {\n throw new Error(\"findNodeHandle is not supported on the web\");\n },\n View,\n};\n"],"mappings":"AACA,OAAOA,KAAK,IAAIC,OAAO,QAAQ,OAAO;AAItC,SAASC,UAAU,EAAEC,YAAY,QAAQ,eAAe;AAIxD;AACA;AACA,MAAMC,IAAI,GAAIA,CAAC;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F,CAAC,GAAGD,OAAO,CAAC,4CAA4C,CAAC;QACzD,MAAM;UAAEE,kBAAkB;UAAEC,IAAI;UAAEC;QAAK,CAAC,GAAGH,YAAY,CAACF,KAAK,CAAC;QAC9D,MAAMM,GAAG,GAAG,GAAGH,kBAAkB,IAAIC,IAAI,IAAIC,IAAI,EAAE;QACnD,OAAOC,GAAG;MACZ;MACA,MAAM,IAAIC,KAAK,CACb,6DACF,CAAC;IACH;IACA,OAAOP,KAAK,CAACM,GAAG;EAClB,CAAC;EACDE,cAAc,EAAEA,CAAA,KAAM;IACpB,MAAM,IAAID,KAAK,CAAC,4CAA4C,CAAC;EAC/D,CAAC;EACDpC;AACF,CAAC","ignoreList":[]}
@@ -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
- type Slant = "normal" | "italic" | "oblique";
8
- type Weight = "normal" | "bold" | "100" | "200" | "300" | "400" | "500" | "600" | "700" | "800" | "900";
9
- interface RNFontStyle {
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: Slant;
13
- fontWeight: Weight;
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,7 +1,7 @@
1
1
  /*global SkiaApi*/
2
2
  import { useEffect, useMemo, useState } from "react";
3
3
  import { Skia } from "../Skia";
4
- import { FontSlant } from "../types";
4
+ import { FontSlant, FontWeight } from "../types";
5
5
  import { Platform } from "../../Platform";
6
6
  import { useTypeface } from "./Typeface";
7
7
  const defaultFontSize = 14;
@@ -19,6 +19,25 @@ export const useFont = (font, size = defaultFontSize, onError) => {
19
19
  }
20
20
  }, [size, typeface]);
21
21
  };
22
+
23
+ /**
24
+ * React Native style font slant, as found in the `fontStyle` property of
25
+ * `TextStyle`. The Skia {@link FontSlant} enum is accepted as well.
26
+ */
27
+
28
+ /**
29
+ * React Native style font weight, as found in the `fontWeight` property of
30
+ * `TextStyle`. Numeric weights such as the {@link FontWeight} enum values
31
+ * used by the Paragraph API are accepted as well.
32
+ */
33
+
34
+ /**
35
+ * Font style accepted by {@link matchFont}.
36
+ * `fontStyle` and `fontWeight` accept both the React Native string values
37
+ * and the Skia enums ({@link FontSlant} and {@link FontWeight}), so the same
38
+ * values can be shared with the Paragraph API.
39
+ */
40
+
22
41
  const defaultFontStyle = {
23
42
  fontFamily: "System",
24
43
  fontSize: defaultFontSize,
@@ -26,7 +45,9 @@ const defaultFontStyle = {
26
45
  fontWeight: "normal"
27
46
  };
28
47
  const slant = s => {
29
- if (s === "italic") {
48
+ if (typeof s === "number") {
49
+ return s;
50
+ } else if (s === "italic") {
30
51
  return FontSlant.Italic;
31
52
  } else if (s === "oblique") {
32
53
  return FontSlant.Oblique;
@@ -37,11 +58,11 @@ const slant = s => {
37
58
  const weight = fontWeight => {
38
59
  switch (fontWeight) {
39
60
  case "normal":
40
- return 400;
61
+ return FontWeight.Normal;
41
62
  case "bold":
42
- return 700;
63
+ return FontWeight.Bold;
43
64
  default:
44
- return parseInt(fontWeight, 10);
65
+ return typeof fontWeight === "number" ? fontWeight : parseInt(fontWeight, 10);
45
66
  }
46
67
  };
47
68
  export const matchFont = (inputStyle = {}, fontMgr = Skia.FontMgr.System()) => {
@@ -1 +1 @@
1
- {"version":3,"names":["useEffect","useMemo","useState","Skia","FontSlant","Platform","useTypeface","defaultFontSize","useFont","font","size","onError","typeface","Font","defaultFontStyle","fontFamily","fontSize","fontStyle","fontWeight","slant","s","Italic","Oblique","Upright","weight","parseInt","matchFont","inputStyle","fontMgr","FontMgr","System","style","width","matchFamilyStyle","listFontFamilies","families","Set","i","countFamilies","add","getFamilyName","Array","from","loadTypefaces","typefacesToLoad","promises","Object","keys","flatMap","familyName","map","typefaceToLoad","Data","fromURI","resolveAsset","then","data","tf","Typeface","MakeFreeTypeFaceFromData","Error","Promise","all","useFonts","sources","setFontMgr","result","fMgr","TypefaceFontProvider","Make","forEach","registerFont"],"sources":["Font.ts"],"sourcesContent":["/*global SkiaApi*/\nimport { useEffect, useMemo, useState } from \"react\";\n\nimport { Skia } from \"../Skia\";\nimport { FontSlant } from \"../types\";\nimport type { DataModule, DataSourceParam, SkFontMgr } from \"../types\";\nimport { Platform } from \"../../Platform\";\nimport type { SkTypefaceFontProvider } from \"../types/Paragraph/TypefaceFontProvider\";\n\nimport { useTypeface } from \"./Typeface\";\n\nconst defaultFontSize = 14;\n\n/**\n * Returns a Skia Font object\n * */\nexport const useFont = (\n font: DataSourceParam,\n size = defaultFontSize,\n onError?: (err: Error) => void\n) => {\n const typeface = useTypeface(font, onError);\n return useMemo(() => {\n if (typeface) {\n return Skia.Font(typeface, size);\n } else {\n return null;\n }\n }, [size, typeface]);\n};\n\ntype Slant = \"normal\" | \"italic\" | \"oblique\";\ntype Weight =\n | \"normal\"\n | \"bold\"\n | \"100\"\n | \"200\"\n | \"300\"\n | \"400\"\n | \"500\"\n | \"600\"\n | \"700\"\n | \"800\"\n | \"900\";\n\ninterface RNFontStyle {\n fontFamily: string;\n fontSize: number;\n fontStyle: Slant;\n fontWeight: Weight;\n}\n\nconst defaultFontStyle: RNFontStyle = {\n fontFamily: \"System\",\n fontSize: defaultFontSize,\n fontStyle: \"normal\",\n fontWeight: \"normal\",\n};\n\nconst slant = (s: Slant) => {\n if (s === \"italic\") {\n return FontSlant.Italic;\n } else if (s === \"oblique\") {\n return FontSlant.Oblique;\n } else {\n return FontSlant.Upright;\n }\n};\n\nconst weight = (fontWeight: Weight) => {\n switch (fontWeight) {\n case \"normal\":\n return 400;\n case \"bold\":\n return 700;\n default:\n return parseInt(fontWeight, 10);\n }\n};\n\nexport const matchFont = (\n inputStyle: Partial<RNFontStyle> = {},\n fontMgr: SkFontMgr = Skia.FontMgr.System()\n) => {\n const fontStyle = {\n ...defaultFontStyle,\n ...inputStyle,\n };\n const style = {\n weight: weight(fontStyle.fontWeight),\n width: 5,\n slant: slant(fontStyle.fontStyle),\n };\n const typeface = fontMgr.matchFamilyStyle(fontStyle.fontFamily, style);\n return Skia.Font(typeface, fontStyle.fontSize);\n};\n\nexport const listFontFamilies = (\n fontMgr: SkFontMgr = Skia.FontMgr.System()\n) => {\n const families = new Set<string>();\n for (let i = 0; i < fontMgr.countFamilies(); i++) {\n families.add(fontMgr.getFamilyName(i));\n }\n return Array.from(families);\n};\n\nconst loadTypefaces = (typefacesToLoad: Record<string, DataModule[]>) => {\n const promises = Object.keys(typefacesToLoad).flatMap((familyName) => {\n return typefacesToLoad[familyName].map((typefaceToLoad) => {\n return Skia.Data.fromURI(Platform.resolveAsset(typefaceToLoad)).then(\n (data) => {\n const tf = Skia.Typeface.MakeFreeTypeFaceFromData(data);\n if (tf === null) {\n throw new Error(`Couldn't create typeface for ${familyName}`);\n }\n return [familyName, tf] as const;\n }\n );\n });\n });\n return Promise.all(promises);\n};\n\nexport const useFonts = (sources: Record<string, DataModule[]>) => {\n const [fontMgr, setFontMgr] = useState<null | SkTypefaceFontProvider>(null);\n\n useEffect(() => {\n loadTypefaces(sources).then((result) => {\n const fMgr = Skia.TypefaceFontProvider.Make();\n result.forEach(([familyName, typeface]) => {\n fMgr.registerFont(typeface, familyName);\n });\n setFontMgr(fMgr);\n });\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n return fontMgr;\n};\n"],"mappings":"AAAA;AACA,SAASA,SAAS,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,OAAO;AAEpD,SAASC,IAAI,QAAQ,SAAS;AAC9B,SAASC,SAAS,QAAQ,UAAU;AAEpC,SAASC,QAAQ,QAAQ,gBAAgB;AAGzC,SAASC,WAAW,QAAQ,YAAY;AAExC,MAAMC,eAAe,GAAG,EAAE;;AAE1B;AACA;AACA;AACA,OAAO,MAAMC,OAAO,GAAGA,CACrBC,IAAqB,EACrBC,IAAI,GAAGH,eAAe,EACtBI,OAA8B,KAC3B;EACH,MAAMC,QAAQ,GAAGN,WAAW,CAACG,IAAI,EAAEE,OAAO,CAAC;EAC3C,OAAOV,OAAO,CAAC,MAAM;IACnB,IAAIW,QAAQ,EAAE;MACZ,OAAOT,IAAI,CAACU,IAAI,CAACD,QAAQ,EAAEF,IAAI,CAAC;IAClC,CAAC,MAAM;MACL,OAAO,IAAI;IACb;EACF,CAAC,EAAE,CAACA,IAAI,EAAEE,QAAQ,CAAC,CAAC;AACtB,CAAC;AAuBD,MAAME,gBAA6B,GAAG;EACpCC,UAAU,EAAE,QAAQ;EACpBC,QAAQ,EAAET,eAAe;EACzBU,SAAS,EAAE,QAAQ;EACnBC,UAAU,EAAE;AACd,CAAC;AAED,MAAMC,KAAK,GAAIC,CAAQ,IAAK;EAC1B,IAAIA,CA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,EAAEqC,UAAU,CAAC;MACzC,CAAC,CAAC;MACFgB,UAAU,CAACE,IAAI,CAAC;IAClB,CAAC,CAAC;IACF;EACF,CAAC,EAAE,EAAE,CAAC;EACN,OAAOvC,OAAO;AAChB,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["useEffect","useMemo","useState","Skia","FontSlant","FontWeight","Platform","useTypeface","defaultFontSize","useFont","font","size","onError","typeface","Font","defaultFontStyle","fontFamily","fontSize","fontStyle","fontWeight","slant","s","Italic","Oblique","Upright","weight","Normal","Bold","parseInt","matchFont","inputStyle","fontMgr","FontMgr","System","style","width","matchFamilyStyle","listFontFamilies","families","Set","i","countFamilies","add","getFamilyName","Array","from","loadTypefaces","typefacesToLoad","promises","Object","keys","flatMap","familyName","map","typefaceToLoad","Data","fromURI","resolveAsset","then","data","tf","Typeface","MakeFreeTypeFaceFromData","Error","Promise","all","useFonts","sources","setFontMgr","result","fMgr","TypefaceFontProvider","Make","forEach","registerFont"],"sources":["Font.ts"],"sourcesContent":["/*global SkiaApi*/\nimport { useEffect, useMemo, useState } from \"react\";\n\nimport { Skia } from \"../Skia\";\nimport { FontSlant, FontWeight } from \"../types\";\nimport type { DataModule, DataSourceParam, SkFontMgr } from \"../types\";\nimport { Platform } from \"../../Platform\";\nimport type { SkTypefaceFontProvider } from \"../types/Paragraph/TypefaceFontProvider\";\n\nimport { useTypeface } from \"./Typeface\";\n\nconst defaultFontSize = 14;\n\n/**\n * Returns a Skia Font object\n * */\nexport const useFont = (\n font: DataSourceParam,\n size = defaultFontSize,\n onError?: (err: Error) => void\n) => {\n const typeface = useTypeface(font, onError);\n return useMemo(() => {\n if (typeface) {\n return Skia.Font(typeface, size);\n } else {\n return null;\n }\n }, [size, typeface]);\n};\n\n/**\n * React Native style font slant, as found in the `fontStyle` property of\n * `TextStyle`. The Skia {@link FontSlant} enum is accepted as well.\n */\nexport type RNFontSlant = \"normal\" | \"italic\" | \"oblique\" | FontSlant;\n\n/**\n * React Native style font weight, as found in the `fontWeight` property of\n * `TextStyle`. Numeric weights such as the {@link FontWeight} enum values\n * used by the Paragraph API are accepted as well.\n */\nexport type RNFontWeight =\n | \"normal\"\n | \"bold\"\n | \"100\"\n | \"200\"\n | \"300\"\n | \"400\"\n | \"500\"\n | \"600\"\n | \"700\"\n | \"800\"\n | \"900\"\n | FontWeight;\n\n/**\n * Font style accepted by {@link matchFont}.\n * `fontStyle` and `fontWeight` accept both the React Native string values\n * and the Skia enums ({@link FontSlant} and {@link FontWeight}), so the same\n * values can be shared with the Paragraph API.\n */\nexport interface RNFontStyle {\n fontFamily: string;\n fontSize: number;\n fontStyle: RNFontSlant;\n fontWeight: RNFontWeight;\n}\n\nconst defaultFontStyle: RNFontStyle = {\n fontFamily: \"System\",\n fontSize: defaultFontSize,\n fontStyle: \"normal\",\n fontWeight: \"normal\",\n};\n\nconst slant = (s: RNFontSlant): FontSlant => {\n if (typeof s === \"number\") {\n return s;\n } else if (s === \"italic\") {\n return FontSlant.Italic;\n } else if (s === \"oblique\") {\n return FontSlant.Oblique;\n } else {\n return FontSlant.Upright;\n }\n};\n\nconst weight = (fontWeight: RNFontWeight): FontWeight => {\n switch (fontWeight) {\n case \"normal\":\n return FontWeight.Normal;\n case \"bold\":\n return FontWeight.Bold;\n default:\n return typeof fontWeight === \"number\"\n ? fontWeight\n : (parseInt(fontWeight, 10) as FontWeight);\n }\n};\n\nexport const matchFont = (\n inputStyle: Partial<RNFontStyle> = {},\n fontMgr: SkFontMgr = Skia.FontMgr.System()\n) => {\n const fontStyle = {\n ...defaultFontStyle,\n ...inputStyle,\n };\n const style = {\n weight: weight(fontStyle.fontWeight),\n width: 5,\n slant: slant(fontStyle.fontStyle),\n };\n const typeface = fontMgr.matchFamilyStyle(fontStyle.fontFamily, style);\n return Skia.Font(typeface, fontStyle.fontSize);\n};\n\nexport const listFontFamilies = (\n fontMgr: SkFontMgr = Skia.FontMgr.System()\n) => {\n const families = new Set<string>();\n for (let i = 0; i < fontMgr.countFamilies(); i++) {\n families.add(fontMgr.getFamilyName(i));\n }\n return Array.from(families);\n};\n\nconst loadTypefaces = (typefacesToLoad: Record<string, DataModule[]>) => {\n const promises = Object.keys(typefacesToLoad).flatMap((familyName) => {\n return typefacesToLoad[familyName].map((typefaceToLoad) => {\n return Skia.Data.fromURI(Platform.resolveAsset(typefaceToLoad)).then(\n (data) => {\n const tf = Skia.Typeface.MakeFreeTypeFaceFromData(data);\n if (tf === null) {\n throw new Error(`Couldn't create typeface for ${familyName}`);\n }\n return [familyName, tf] as const;\n }\n );\n });\n });\n return Promise.all(promises);\n};\n\nexport const useFonts = (sources: Record<string, DataModule[]>) => {\n const [fontMgr, setFontMgr] = useState<null | SkTypefaceFontProvider>(null);\n\n useEffect(() => {\n loadTypefaces(sources).then((result) => {\n const fMgr = Skia.TypefaceFontProvider.Make();\n result.forEach(([familyName, typeface]) => {\n fMgr.registerFont(typeface, familyName);\n });\n setFontMgr(fMgr);\n });\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n return fontMgr;\n};\n"],"mappings":"AAAA;AACA,SAASA,SAAS,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,OAAO;AAEpD,SAASC,IAAI,QAAQ,SAAS;AAC9B,SAASC,SAAS,EAAEC,UAAU,QAAQ,UAAU;AAEhD,SAASC,QAAQ,QAAQ,gBAAgB;AAGzC,SAASC,WAAW,QAAQ,YAAY;AAExC,MAAMC,eAAe,GAAG,EAAE;;AAE1B;AACA;AACA;AACA,OAAO,MAAMC,OAAO,GAAGA,CACrBC,IAAqB,EACrBC,IAAI,GAAGH,eAAe,EACtBI,OAA8B,KAC3B;EACH,MAAMC,QAAQ,GAAGN,WAAW,CAACG,IAAI,EAAEE,OAAO,CAAC;EAC3C,OAAOX,OAAO,CAAC,MAAM;IACnB,IAAIY,QAAQ,EAAE;MACZ,OAAOV,IAAI,CAACW,IAAI,CAACD,QAAQ,EAAEF,IAAI,CAAC;IAClC,CAAC,MAAM;MACL,OAAO,IAAI;IACb;EACF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J,CAAC,CAAC;EACF,OAAOI,OAAO,CAACC,GAAG,CAACjB,QAAQ,CAAC;AAC9B,CAAC;AAED,OAAO,MAAMkB,QAAQ,GAAIC,OAAqC,IAAK;EACjE,MAAM,CAACpC,OAAO,EAAEqC,UAAU,CAAC,GAAGlE,QAAQ,CAAgC,IAAI,CAAC;EAE3EF,SAAS,CAAC,MAAM;IACd8C,aAAa,CAACqB,OAAO,CAAC,CAACT,IAAI,CAAEW,MAAM,IAAK;MACtC,MAAMC,IAAI,GAAGnE,IAAI,CAACoE,oBAAoB,CAACC,IAAI,CAAC,CAAC;MAC7CH,MAAM,CAACI,OAAO,CAAC,CAAC,CAACrB,UAAU,EAAEvC,QAAQ,CAAC,KAAK;QACzCyD,IAAI,CAACI,YAAY,CAAC7D,QAAQ,EAAEuC,UAAU,CAAC;MACzC,CAAC,CAAC;MACFgB,UAAU,CAACE,IAAI,CAAC;IAClB,CAAC,CAAC;IACF;EACF,CAAC,EAAE,EAAE,CAAC;EACN,OAAOvC,OAAO;AAChB,CAAC","ignoreList":[]}
@@ -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 {};
@@ -1,2 +1,7 @@
1
1
  export const isRNModule = mod => typeof mod === "number";
2
+
3
+ // Since Expo SDK 52, on web, require() may return an ES module interop
4
+ // object ({ default: <asset> }) instead of the asset itself.
5
+ // See https://github.com/Shopify/react-native-skia/issues/2784
6
+ export const unwrapModule = mod => typeof mod === "object" && mod !== null && "default" in mod ? mod.default : mod;
2
7
  //# sourceMappingURL=Data.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["isRNModule","mod"],"sources":["Data.ts"],"sourcesContent":["import type { SkJSIInstance } from \"../JsiInstance\";\n\nexport type SkData = SkJSIInstance<\"Data\">;\n\ntype RNModule = number;\ntype ESModule = {\n __esModule: true;\n default: string;\n};\ntype MetroAsset = {\n uri: string;\n width: number;\n height: number;\n};\n\nexport type DataModule = RNModule | ESModule | MetroAsset;\nexport type DataSource = DataModule | string | Uint8Array;\nexport type DataSourceParam = DataSource | null | undefined;\n\nexport const isRNModule = (mod: DataModule): mod is RNModule =>\n typeof mod === \"number\";\n"],"mappings":"AAmBA,OAAO,MAAMA,UAAU,GAAIC,GAAe,IACxC,OAAOA,GAAG,KAAK,QAAQ","ignoreList":[]}
1
+ {"version":3,"names":["isRNModule","mod","unwrapModule","default"],"sources":["Data.ts"],"sourcesContent":["import type { SkJSIInstance } from \"../JsiInstance\";\n\nexport type SkData = SkJSIInstance<\"Data\">;\n\ntype RNModule = number;\ntype MetroAsset = {\n uri: string;\n width: number;\n height: number;\n};\ntype ESModule = {\n __esModule: true;\n default: RNModule | MetroAsset | string;\n};\n\nexport type DataModule = RNModule | ESModule | MetroAsset;\nexport type DataSource = DataModule | string | Uint8Array;\nexport type DataSourceParam = DataSource | null | undefined;\n\nexport const isRNModule = (mod: DataModule): mod is RNModule =>\n typeof mod === \"number\";\n\n// Since Expo SDK 52, on web, require() may return an ES module interop\n// object ({ default: <asset> }) instead of the asset itself.\n// See https://github.com/Shopify/react-native-skia/issues/2784\nexport const unwrapModule = (\n mod: DataModule\n): RNModule | MetroAsset | string =>\n typeof mod === \"object\" && mod !== null && \"default\" in mod\n ? mod.default\n : mod;\n"],"mappings":"AAmBA,OAAO,MAAMA,UAAU,GAAIC,GAAe,IACxC,OAAOA,GAAG,KAAK,QAAQ;;AAEzB;AACA;AACA;AACA,OAAO,MAAMC,YAAY,GACvBD,GAAe,IAEf,OAAOA,GAAG,KAAK,QAAQ,IAAIA,GAAG,KAAK,IAAI,IAAI,SAAS,IAAIA,GAAG,GACvDA,GAAG,CAACE,OAAO,GACXF,GAAG","ignoreList":[]}
@@ -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(_text: string, _paint?: SkPaint | undefined): SkRect;
8
+ measureText(text: string, paint?: SkPaint | undefined): SkRect;
9
9
  getTextWidth(text: string, paint?: SkPaint | undefined): number;
10
10
  getMetrics(): {
11
11
  ascent: number;
@@ -1,4 +1,4 @@
1
- import { HostObject, getEnum, throwNotImplementedOnRNWeb } from "./Host";
1
+ import { HostObject, getEnum } from "./Host";
2
2
  import { JsiSkPaint } from "./JsiSkPaint";
3
3
  import { JsiSkPoint } from "./JsiSkPoint";
4
4
  import { JsiSkRect } from "./JsiSkRect";
@@ -7,8 +7,49 @@ export class JsiSkFont extends HostObject {
7
7
  constructor(CanvasKit, ref) {
8
8
  super(CanvasKit, ref, "Font");
9
9
  }
10
- measureText(_text, _paint) {
11
- return throwNotImplementedOnRNWeb();
10
+ measureText(text, paint) {
11
+ // CanvasKit doesn't expose SkFont::measureText directly, so we polyfill
12
+ // it: for each glyph we take its ink bounds (relative to its own origin),
13
+ // offset it by the accumulated advance, and union the results. This
14
+ // matches the bounds computed natively by SkFont::measureText.
15
+ const glyphs = this.ref.getGlyphIDs(text);
16
+ if (glyphs.length === 0) {
17
+ return new JsiSkRect(this.CanvasKit, this.CanvasKit.XYWHRect(0, 0, 0, 0));
18
+ }
19
+ const skPaint = paint ? JsiSkPaint.fromValue(paint) : null;
20
+ // Flattened rectangles: 4 floats (left, top, right, bottom) per glyph.
21
+ const bounds = this.ref.getGlyphBounds(glyphs, skPaint);
22
+ const advances = this.ref.getGlyphWidths(glyphs, skPaint);
23
+ let left = 0;
24
+ let top = 0;
25
+ let right = 0;
26
+ let bottom = 0;
27
+ let isEmpty = true;
28
+ let xPos = 0;
29
+ for (let i = 0; i < glyphs.length; i++) {
30
+ const l = bounds[i * 4] + xPos;
31
+ const t = bounds[i * 4 + 1];
32
+ const r = bounds[i * 4 + 2] + xPos;
33
+ const b = bounds[i * 4 + 3];
34
+ xPos += advances[i];
35
+ // Skip empty glyph bounds (e.g. whitespace), like SkRect::join does.
36
+ if (l >= r || t >= b) {
37
+ continue;
38
+ }
39
+ if (isEmpty) {
40
+ left = l;
41
+ top = t;
42
+ right = r;
43
+ bottom = b;
44
+ isEmpty = false;
45
+ } else {
46
+ left = Math.min(left, l);
47
+ top = Math.min(top, t);
48
+ right = Math.max(right, r);
49
+ bottom = Math.max(bottom, b);
50
+ }
51
+ }
52
+ return new JsiSkRect(this.CanvasKit, this.CanvasKit.LTRBRect(left, top, right, bottom));
12
53
  }
13
54
  getTextWidth(text, paint) {
14
55
  const ids = this.getGlyphIDs(text);
@@ -1 +1 @@
1
- {"version":3,"names":["HostObject","getEnum","throwNotImplementedOnRNWeb","JsiSkPaint","JsiSkPoint","JsiSkRect","JsiSkTypeface","JsiSkFont","constructor","CanvasKit","ref","measureText","_text","_paint","getTextWidth","text","paint","ids","getGlyphIDs","widths","getGlyphWidths","reduce","a","b","getMetrics","result","ascent","descent","leading","bounds","undefined","str","numCodePoints","glyphs","fromValue","getGlyphIntercepts","positions","top","bottom","map","p","Array","from","flat","getScaleX","getSize","getSkewX","isEmbolden","getTypeface","tf","setEdging","edging","setEmbeddedBitmaps","embeddedBitmaps","setHinting","hinting","setLinearMetrics","linearMetrics","setScaleX","sx","setSize","points","setSkewX","setEmbolden","embolden","setSubpixel","subpixel","setTypeface","face"],"sources":["JsiSkFont.ts"],"sourcesContent":["import type { CanvasKit, Font } from \"canvaskit-wasm\";\n\nimport type {\n FontEdging,\n FontHinting,\n SkFont,\n SkPaint,\n SkPoint,\n SkRect,\n SkTypeface,\n} from \"../types\";\n\nimport { HostObject, getEnum, throwNotImplementedOnRNWeb } from \"./Host\";\nimport { JsiSkPaint } from \"./JsiSkPaint\";\nimport { JsiSkPoint } from \"./JsiSkPoint\";\nimport { JsiSkRect } from \"./JsiSkRect\";\nimport { JsiSkTypeface } from \"./JsiSkTypeface\";\n\nexport class JsiSkFont extends HostObject<Font, \"Font\"> implements SkFont {\n constructor(CanvasKit: CanvasKit, ref: Font) {\n super(CanvasKit, ref, \"Font\");\n }\n\n measureText(_text: string, _paint?: SkPaint | undefined) {\n return throwNotImplementedOnRNWeb<SkRect>();\n }\n\n getTextWidth(text: string, paint?: SkPaint | undefined) {\n const ids = this.getGlyphIDs(text);\n const widths = this.getGlyphWidths(ids, paint);\n return widths.reduce((a, b) => a + b, 0);\n }\n\n getMetrics() {\n const result = this.ref.getMetrics();\n return {\n ascent: result.ascent,\n descent: result.descent,\n leading: result.leading,\n bounds: result.bounds\n ? new JsiSkRect(this.CanvasKit, result.bounds)\n : undefined,\n };\n }\n\n getGlyphIDs(str: string, numCodePoints?: number) {\n // TODO: Fix return value in the C++ implementation\n return [...this.ref.getGlyphIDs(str, numCodePoints)];\n }\n\n // TODO: Fix return value in the C++ implementation, it return float32\n getGlyphWidths(glyphs: number[], paint?: SkPaint | null) {\n return [\n ...this.ref.getGlyphWidths(\n glyphs,\n paint ? JsiSkPaint.fromValue(paint) : null\n ),\n ];\n }\n\n getGlyphIntercepts(\n glyphs: number[],\n positions: SkPoint[],\n top: number,\n bottom: number\n ) {\n return [\n ...this.ref.getGlyphIntercepts(\n glyphs,\n positions.map((p) => Array.from(JsiSkPoint.fromValue(p))).flat(),\n top,\n bottom\n ),\n ];\n }\n\n getScaleX() {\n return this.ref.getScaleX();\n }\n\n getSize() {\n return this.ref.getSize();\n }\n\n getSkewX() {\n return this.ref.getSkewX();\n }\n\n isEmbolden() {\n return this.ref.isEmbolden();\n }\n\n getTypeface() {\n const tf = this.ref.getTypeface();\n return tf ? new JsiSkTypeface(this.CanvasKit, tf) : null;\n }\n\n setEdging(edging: FontEdging) {\n this.ref.setEdging(getEnum(this.CanvasKit, \"FontEdging\", edging));\n }\n\n setEmbeddedBitmaps(embeddedBitmaps: boolean) {\n this.ref.setEmbeddedBitmaps(embeddedBitmaps);\n }\n\n setHinting(hinting: FontHinting) {\n this.ref.setHinting(getEnum(this.CanvasKit, \"FontHinting\", hinting));\n }\n\n setLinearMetrics(linearMetrics: boolean) {\n this.ref.setLinearMetrics(linearMetrics);\n }\n\n setScaleX(sx: number) {\n this.ref.setScaleX(sx);\n }\n\n setSize(points: number) {\n this.ref.setSize(points);\n }\n\n setSkewX(sx: number) {\n this.ref.setSkewX(sx);\n }\n\n setEmbolden(embolden: boolean) {\n this.ref.setEmbolden(embolden);\n }\n\n setSubpixel(subpixel: boolean) {\n this.ref.setSubpixel(subpixel);\n }\n\n setTypeface(face: SkTypeface | null) {\n this.ref.setTypeface(face ? JsiSkTypeface.fromValue(face) : null);\n }\n}\n"],"mappings":"AAYA,SAASA,UAAU,EAAEC,OAAO,EAAEC,0BAA0B,QAAQ,QAAQ;AACxE,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,SAAS,QAAQ,aAAa;AACvC,SAASC,aAAa,QAAQ,iBAAiB;AAE/C,OAAO,MAAMC,SAAS,SAASP,UAAU,CAAiC;EACxEQ,WAAWA,CAACC,SAAoB,EAAEC,GAAS,EAAE;IAC3C,KAAK,CAACD,SAAS,EAAEC,GAAG,EAAE,MAAM,CAAC;EAC/B;EAEAC,WAAWA,CAACC,KAAa,EAAEC,MAA4B,EAAE;IACvD,OAAOX,0BAA0B,CAAS,CAAC;EAC7C;EAEAY,YAAYA,CAACC,IAAY,EAAEC,KAA2B,EAAE;IACtD,MAAMC,GAAG,GAAG,IAAI,CAACC,WAAW,CAACH,IAAI,CAA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I,CAACnD,GAAG,CAACkD,OAAO,CAACC,MAAM,CAAC;EAC1B;EAEAC,QAAQA,CAACH,EAAU,EAAE;IACnB,IAAI,CAACjD,GAAG,CAACoD,QAAQ,CAACH,EAAE,CAAC;EACvB;EAEAI,WAAWA,CAACC,QAAiB,EAAE;IAC7B,IAAI,CAACtD,GAAG,CAACqD,WAAW,CAACC,QAAQ,CAAC;EAChC;EAEAC,WAAWA,CAACC,QAAiB,EAAE;IAC7B,IAAI,CAACxD,GAAG,CAACuD,WAAW,CAACC,QAAQ,CAAC;EAChC;EAEAC,WAAWA,CAACC,IAAuB,EAAE;IACnC,IAAI,CAAC1D,GAAG,CAACyD,WAAW,CAACC,IAAI,GAAG9D,aAAa,CAAC4B,SAAS,CAACkC,IAAI,CAAC,GAAG,IAAI,CAAC;EACnE;AACF","ignoreList":[]}
1
+ {"version":3,"names":["HostObject","getEnum","JsiSkPaint","JsiSkPoint","JsiSkRect","JsiSkTypeface","JsiSkFont","constructor","CanvasKit","ref","measureText","text","paint","glyphs","getGlyphIDs","length","XYWHRect","skPaint","fromValue","bounds","getGlyphBounds","advances","getGlyphWidths","left","top","right","bottom","isEmpty","xPos","i","l","t","r","b","Math","min","max","LTRBRect","getTextWidth","ids","widths","reduce","a","getMetrics","result","ascent","descent","leading","undefined","str","numCodePoints","getGlyphIntercepts","positions","map","p","Array","from","flat","getScaleX","getSize","getSkewX","isEmbolden","getTypeface","tf","setEdging","edging","setEmbeddedBitmaps","embeddedBitmaps","setHinting","hinting","setLinearMetrics","linearMetrics","setScaleX","sx","setSize","points","setSkewX","setEmbolden","embolden","setSubpixel","subpixel","setTypeface","face"],"sources":["JsiSkFont.ts"],"sourcesContent":["import type { CanvasKit, Font, Paint } from \"canvaskit-wasm\";\n\nimport type {\n FontEdging,\n FontHinting,\n SkFont,\n SkPaint,\n SkPoint,\n SkRect,\n SkTypeface,\n} from \"../types\";\n\nimport { HostObject, getEnum } from \"./Host\";\nimport { JsiSkPaint } from \"./JsiSkPaint\";\nimport { JsiSkPoint } from \"./JsiSkPoint\";\nimport { JsiSkRect } from \"./JsiSkRect\";\nimport { JsiSkTypeface } from \"./JsiSkTypeface\";\n\nexport class JsiSkFont extends HostObject<Font, \"Font\"> implements SkFont {\n constructor(CanvasKit: CanvasKit, ref: Font) {\n super(CanvasKit, ref, \"Font\");\n }\n\n measureText(text: string, paint?: SkPaint | undefined): SkRect {\n // CanvasKit doesn't expose SkFont::measureText directly, so we polyfill\n // it: for each glyph we take its ink bounds (relative to its own origin),\n // offset it by the accumulated advance, and union the results. This\n // matches the bounds computed natively by SkFont::measureText.\n const glyphs = this.ref.getGlyphIDs(text);\n if (glyphs.length === 0) {\n return new JsiSkRect(this.CanvasKit, this.CanvasKit.XYWHRect(0, 0, 0, 0));\n }\n const skPaint = paint ? JsiSkPaint.fromValue<Paint>(paint) : null;\n // Flattened rectangles: 4 floats (left, top, right, bottom) per glyph.\n const bounds = this.ref.getGlyphBounds(glyphs, skPaint);\n const advances = this.ref.getGlyphWidths(glyphs, skPaint);\n let left = 0;\n let top = 0;\n let right = 0;\n let bottom = 0;\n let isEmpty = true;\n let xPos = 0;\n for (let i = 0; i < glyphs.length; i++) {\n const l = bounds[i * 4] + xPos;\n const t = bounds[i * 4 + 1];\n const r = bounds[i * 4 + 2] + xPos;\n const b = bounds[i * 4 + 3];\n xPos += advances[i];\n // Skip empty glyph bounds (e.g. whitespace), like SkRect::join does.\n if (l >= r || t >= b) {\n continue;\n }\n if (isEmpty) {\n left = l;\n top = t;\n right = r;\n bottom = b;\n isEmpty = false;\n } else {\n left = Math.min(left, l);\n top = Math.min(top, t);\n right = Math.max(right, r);\n bottom = Math.max(bottom, b);\n }\n }\n return new JsiSkRect(\n this.CanvasKit,\n this.CanvasKit.LTRBRect(left, top, right, bottom)\n );\n }\n\n getTextWidth(text: string, paint?: SkPaint | undefined) {\n const ids = this.getGlyphIDs(text);\n const widths = this.getGlyphWidths(ids, paint);\n return widths.reduce((a, b) => a + b, 0);\n }\n\n getMetrics() {\n const result = this.ref.getMetrics();\n return {\n ascent: result.ascent,\n descent: result.descent,\n leading: result.leading,\n bounds: result.bounds\n ? new JsiSkRect(this.CanvasKit, result.bounds)\n : undefined,\n };\n }\n\n getGlyphIDs(str: string, numCodePoints?: number) {\n // TODO: Fix return value in the C++ implementation\n return [...this.ref.getGlyphIDs(str, numCodePoints)];\n }\n\n // TODO: Fix return value in the C++ implementation, it return float32\n getGlyphWidths(glyphs: number[], paint?: SkPaint | null) {\n return [\n ...this.ref.getGlyphWidths(\n glyphs,\n paint ? JsiSkPaint.fromValue(paint) : null\n ),\n ];\n }\n\n getGlyphIntercepts(\n glyphs: number[],\n positions: SkPoint[],\n top: number,\n bottom: number\n ) {\n return [\n ...this.ref.getGlyphIntercepts(\n glyphs,\n positions.map((p) => Array.from(JsiSkPoint.fromValue(p))).flat(),\n top,\n bottom\n ),\n ];\n }\n\n getScaleX() {\n return this.ref.getScaleX();\n }\n\n getSize() {\n return this.ref.getSize();\n }\n\n getSkewX() {\n return this.ref.getSkewX();\n }\n\n isEmbolden() {\n return this.ref.isEmbolden();\n }\n\n getTypeface() {\n const tf = this.ref.getTypeface();\n return tf ? new JsiSkTypeface(this.CanvasKit, tf) : null;\n }\n\n setEdging(edging: FontEdging) {\n this.ref.setEdging(getEnum(this.CanvasKit, \"FontEdging\", edging));\n }\n\n setEmbeddedBitmaps(embeddedBitmaps: boolean) {\n this.ref.setEmbeddedBitmaps(embeddedBitmaps);\n }\n\n setHinting(hinting: FontHinting) {\n this.ref.setHinting(getEnum(this.CanvasKit, \"FontHinting\", hinting));\n }\n\n setLinearMetrics(linearMetrics: boolean) {\n this.ref.setLinearMetrics(linearMetrics);\n }\n\n setScaleX(sx: number) {\n this.ref.setScaleX(sx);\n }\n\n setSize(points: number) {\n this.ref.setSize(points);\n }\n\n setSkewX(sx: number) {\n this.ref.setSkewX(sx);\n }\n\n setEmbolden(embolden: boolean) {\n this.ref.setEmbolden(embolden);\n }\n\n setSubpixel(subpixel: boolean) {\n this.ref.setSubpixel(subpixel);\n }\n\n setTypeface(face: SkTypeface | null) {\n this.ref.setTypeface(face ? JsiSkTypeface.fromValue(face) : null);\n }\n}\n"],"mappings":"AAYA,SAASA,UAAU,EAAEC,OAAO,QAAQ,QAAQ;AAC5C,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,SAAS,QAAQ,aAAa;AACvC,SAASC,aAAa,QAAQ,iBAAiB;AAE/C,OAAO,MAAMC,SAAS,SAASN,UAAU,CAAiC;EACxEO,WAAWA,CAACC,SAAoB,EAAEC,GAAS,EAAE;IAC3C,KAAK,CAACD,SAAS,EAAEC,GAAG,EAAE,MAAM,CAAC;EAC/B;EAEAC,WAAWA,CAACC,IAAY,EAAEC,KAA2B,EAAU;IAC7D;IACA;IACA;IACA;IACA,MAAMC,MAAM,GAAG,IAAI,CAACJ,GAAG,CAACK,WAAW,CAACH,IAAI,CAAC;IACzC,IAAIE,MAAM,CAACE,MAAM,KAAK,CAAC,EAAE;MACvB,OAAO,IAAIX,SAAS,CAAC,IAAI,CAACI,SAAS,EAAE,IAAI,CAACA,SAAS,CAACQ,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC3E;IACA,MAAMC,OAAO,GAAGL,KAAK,GAAGV,UAAU,CAACgB,SAAS,CAAQN,KAAK,CAAC,GAAG,IAAI;IACjE;IACA,MAAMO,MAAM,GAAG,IAAI,CAACV,GAAG,CAACW,cAAc,CAACP,MAAM,EAAEI,OAAO,CAAC;IACvD,MAAMI,QAAQ,GAAG,IAAI,CAACZ,GAAG,CAACa,cAAc,CAACT,MAAM,EAAEI,OAAO,CAAC;IACzD,IAAIM,IAAI,GAAG,CAAC;IACZ,IAAIC,GAAG,GAAG,CAAC;IACX,IAAIC,KAAK,GAAG,CAAC;IACb,IAAIC,MAAM,GAAG,CAAC;IACd,IAAIC,OAAO,GAAG,IAAI;IAClB,IA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AAC+D,SAAS,CAACC,EAAE,CAAC;EACxB;EAEAC,OAAOA,CAACC,MAAc,EAAE;IACtB,IAAI,CAAClE,GAAG,CAACiE,OAAO,CAACC,MAAM,CAAC;EAC1B;EAEAC,QAAQA,CAACH,EAAU,EAAE;IACnB,IAAI,CAAChE,GAAG,CAACmE,QAAQ,CAACH,EAAE,CAAC;EACvB;EAEAI,WAAWA,CAACC,QAAiB,EAAE;IAC7B,IAAI,CAACrE,GAAG,CAACoE,WAAW,CAACC,QAAQ,CAAC;EAChC;EAEAC,WAAWA,CAACC,QAAiB,EAAE;IAC7B,IAAI,CAACvE,GAAG,CAACsE,WAAW,CAACC,QAAQ,CAAC;EAChC;EAEAC,WAAWA,CAACC,IAAuB,EAAE;IACnC,IAAI,CAACzE,GAAG,CAACwE,WAAW,CAACC,IAAI,GAAG7E,aAAa,CAACa,SAAS,CAACgE,IAAI,CAAC,GAAG,IAAI,CAAC;EACnE;AACF","ignoreList":[]}
@@ -1,2 +1,3 @@
1
1
  export const __esModule: boolean;
2
2
  export function isRNModule(mod: any): mod is number;
3
+ export function unwrapModule(mod: any): any;
@@ -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(_text: any, _paint: any): jest.Mock<any, any, any>;
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 +1,2 @@
1
1
  export function isRNModule(mod: any): mod is number;
2
+ export function unwrapModule(mod: any): any;
@@ -1,6 +1,6 @@
1
1
  export class JsiSkFont extends HostObject {
2
2
  constructor(CanvasKit: any, ref: any);
3
- measureText(_text: any, _paint: any): jest.Mock<any, any, any>;
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
- type Slant = "normal" | "italic" | "oblique";
8
- type Weight = "normal" | "bold" | "100" | "200" | "300" | "400" | "500" | "600" | "700" | "800" | "900";
9
- interface RNFontStyle {
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: Slant;
13
- fontWeight: Weight;
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(_text: string, _paint?: SkPaint | undefined): SkRect;
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/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.8.0",
11
+ "version": "2.9.0",
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",
@@ -140,6 +141,11 @@
140
141
  "react-native-skia-apple-tvos": "150.0.0",
141
142
  "react-reconciler": "0.31.0"
142
143
  },
144
+ "graphiteDependencies": {
145
+ "react-native-skia-graphite-android": "150.0.0",
146
+ "react-native-skia-graphite-apple-ios": "150.0.0",
147
+ "react-native-skia-graphite-apple-macos": "150.0.0"
148
+ },
143
149
  "eslintIgnore": [
144
150
  "node_modules/",
145
151
  "lib/"
@@ -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
- { 'ios' => 'react-native-skia-apple-ios',
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
- # Graphite downloads its binaries directly into libs/; only the default build needs
58
- # the npm packages copied in.
59
- install_apple_skia_libs.call(__dir__) unless use_graphite
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, or downloaded by
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 react-native-skia-apple-* packages are present, then run `pod install` again."
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
@@ -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
- // eslint-disable-next-line no-nested-ternary
19
- return isRNModule(source)
20
- ? Image.resolveAssetSource(source).uri
21
- : "uri" in source
22
- ? source.uri
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,