-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fafff4c
commit 279afb9
Showing
9 changed files
with
126 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2465,9 +2465,7 @@ | |
|
||
"@shopify/react-native-skia@link:../package": | ||
version "0.0.0" | ||
dependencies: | ||
canvaskit-wasm "0.39.1" | ||
react-reconciler "0.27.0" | ||
uid "" | ||
|
||
"@sideway/address@^4.1.5": | ||
version "4.1.5" | ||
|
@@ -9120,6 +9118,14 @@ [email protected]: | |
iconv-lite "0.4.24" | ||
unpipe "1.0.0" | ||
|
||
raw-loader@^4.0.2: | ||
version "4.0.2" | ||
resolved "https://registry.yarnpkg.com/raw-loader/-/raw-loader-4.0.2.tgz#1aac6b7d1ad1501e66efdac1522c73e59a584eb6" | ||
integrity sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA== | ||
dependencies: | ||
loader-utils "^2.0.0" | ||
schema-utils "^3.0.0" | ||
|
||
rc@~1.2.7: | ||
version "1.2.8" | ||
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed" | ||
|
@@ -10134,16 +10140,7 @@ string-natural-compare@^3.0.1: | |
resolved "https://registry.yarnpkg.com/string-natural-compare/-/string-natural-compare-3.0.1.tgz#7a42d58474454963759e8e8b7ae63d71c1e7fdf4" | ||
integrity sha512-n3sPwynL1nwKi3WJ6AIsClwBMa0zTi54fn2oLU6ndfTSIO05xaznjSf15PcBZU6FNWbmN5Q6cxT4V5hGvB4taw== | ||
|
||
"string-width-cjs@npm:string-width@^4.2.0": | ||
version "4.2.3" | ||
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" | ||
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== | ||
dependencies: | ||
emoji-regex "^8.0.0" | ||
is-fullwidth-code-point "^3.0.0" | ||
strip-ansi "^6.0.1" | ||
|
||
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: | ||
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: | ||
version "4.2.3" | ||
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" | ||
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== | ||
|
@@ -10221,7 +10218,7 @@ string_decoder@~1.1.1: | |
dependencies: | ||
safe-buffer "~5.1.0" | ||
|
||
"strip-ansi-cjs@npm:strip-ansi@^6.0.1": | ||
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: | ||
version "6.0.1" | ||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" | ||
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== | ||
|
@@ -10235,13 +10232,6 @@ strip-ansi@^5.0.0, strip-ansi@^5.2.0: | |
dependencies: | ||
ansi-regex "^4.1.0" | ||
|
||
strip-ansi@^6.0.0, strip-ansi@^6.0.1: | ||
version "6.0.1" | ||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" | ||
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== | ||
dependencies: | ||
ansi-regex "^5.0.1" | ||
|
||
strip-ansi@^7.0.1: | ||
version "7.1.0" | ||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" | ||
|
@@ -11199,7 +11189,7 @@ wonka@^6.3.2: | |
resolved "https://registry.yarnpkg.com/wonka/-/wonka-6.3.4.tgz#76eb9316e3d67d7febf4945202b5bdb2db534594" | ||
integrity sha512-CjpbqNtBGNAeyNS/9W6q3kSkKE52+FjIj7AkFlLr11s/VWGUu6a2CdYSdGxocIhIVjaW/zchesBQUKPVU69Cqg== | ||
|
||
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": | ||
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: | ||
version "7.0.0" | ||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" | ||
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== | ||
|
@@ -11217,15 +11207,6 @@ wrap-ansi@^6.2.0: | |
string-width "^4.1.0" | ||
strip-ansi "^6.0.0" | ||
|
||
wrap-ansi@^7.0.0: | ||
version "7.0.0" | ||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" | ||
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== | ||
dependencies: | ||
ansi-styles "^4.0.0" | ||
string-width "^4.1.0" | ||
strip-ansi "^6.0.0" | ||
|
||
wrap-ansi@^8.1.0: | ||
version "8.1.0" | ||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Skia } from "../Skia"; | ||
import type { DataSourceParam } from "../types"; | ||
|
||
export const useSVG = ( | ||
source: DataSourceParam, | ||
onError?: (err: Error) => void | ||
) => { | ||
if (source === null || source === undefined) { | ||
throw new Error(`Invalid svg data source. Got: ${source}`); | ||
} | ||
if ( | ||
typeof source !== "object" || | ||
source instanceof Uint8Array || | ||
typeof source.default !== "string" | ||
) { | ||
throw new Error( | ||
`Invalid svg data source. Make sure that the source resolves to a string. Got: ${JSON.stringify( | ||
source, | ||
null, | ||
2 | ||
)}` | ||
); | ||
} | ||
const svg = Skia.SVG.MakeFromString(source.default); | ||
if (svg === null && onError !== undefined) { | ||
onError(new Error("Failed to create SVG from source.")); | ||
} | ||
return svg; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { CanvasKit } from "canvaskit-wasm"; | ||
|
||
import type { SkSVG } from "../types"; | ||
|
||
import { HostObject } from "./Host"; | ||
|
||
export class JsiSkSVG | ||
extends HostObject<HTMLImageElement, "SVG"> | ||
implements SkSVG | ||
{ | ||
constructor(CanvasKit: CanvasKit, ref: HTMLImageElement) { | ||
super(CanvasKit, ref, "SVG"); | ||
} | ||
|
||
width(): number { | ||
return this.ref.width; | ||
} | ||
height(): number { | ||
return this.ref.height; | ||
} | ||
|
||
dispose = () => { | ||
if (this.ref.parentNode) { | ||
this.ref.parentNode.removeChild(this.ref); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,61 @@ | ||
import type { CanvasKit } from "canvaskit-wasm"; | ||
|
||
import type { SkData, SkSVG } from "../types"; | ||
import type { SkSVG } from "../types"; | ||
import type { SVGFactory } from "../types/SVG/SVGFactory"; | ||
|
||
import { Host, NotImplementedOnRNWeb } from "./Host"; | ||
import { Host } from "./Host"; | ||
import type { JsiSkData } from "./JsiSkData"; | ||
import { JsiSkSVG } from "./JsiSkSVG"; | ||
|
||
export class JsiSkSVGFactory extends Host implements SVGFactory { | ||
constructor(CanvasKit: CanvasKit) { | ||
super(CanvasKit); | ||
} | ||
|
||
MakeFromData(_data: SkData): SkSVG | null { | ||
throw new NotImplementedOnRNWeb(); | ||
MakeFromData(data: JsiSkData): SkSVG | null { | ||
const decoder = new TextDecoder("utf-8"); | ||
const str = decoder.decode(data.ref); | ||
return this.MakeFromString(str); | ||
} | ||
|
||
MakeFromString(_str: string): SkSVG | null { | ||
throw new NotImplementedOnRNWeb(); | ||
MakeFromString(str: string): SkSVG | null { | ||
const parser = new DOMParser(); | ||
const svgDoc = parser.parseFromString(str, "image/svg+xml"); | ||
const svgElement = svgDoc.documentElement; | ||
|
||
const attrWidth = svgElement.getAttribute("width"); | ||
const attrHeight = svgElement.getAttribute("height"); | ||
let width = attrWidth ? parseFloat(attrWidth) : null; | ||
let height = attrHeight ? parseFloat(attrHeight) : null; | ||
|
||
const svgDataUrl = | ||
"data:image/svg+xml;charset=utf-8," + encodeURIComponent(str); | ||
// Create a new HTMLImageElement | ||
const img = new Image(); | ||
img.src = svgDataUrl; | ||
|
||
// Optionally set styles or attributes on the image | ||
img.style.display = "none"; | ||
img.alt = "SVG Image"; | ||
if (!width || !height) { | ||
const viewBox = svgElement.getAttribute("viewBox"); | ||
if (viewBox) { | ||
const viewBoxValues = viewBox.split(" "); | ||
if (viewBoxValues.length === 4) { | ||
width = width || parseFloat(viewBoxValues[2]); | ||
height = height || parseFloat(viewBoxValues[3]); | ||
} | ||
} | ||
} | ||
if (width && height) { | ||
img.width = width; | ||
img.height = height; | ||
} | ||
|
||
img.onerror = (e) => { | ||
console.error("SVG failed to load", e); | ||
}; | ||
document.body.appendChild(img); | ||
return new JsiSkSVG(this.CanvasKit, img); | ||
} | ||
} |