-
Notifications
You must be signed in to change notification settings - Fork 448
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
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); | ||
} | ||
} |