Skip to content

Commit

Permalink
chore: update wasm and napi-rs bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed May 16, 2022
1 parent f5e457a commit 606337c
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 52 deletions.
13 changes: 6 additions & 7 deletions example/image-url.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { promises } = require('fs')
const { join } = require('path')
// const { performance } = require('perf_hooks')

const fetch = require('node-fetch')

Expand All @@ -14,24 +13,24 @@ async function main() {
},
logLevel: 'off',
}

const resvg = new Resvg(svg, opts)

const resolved = await Promise.all(
resvg.imagesToResolve().map(async (url) => {
console.info('image url', url)
const img = await fetch(url)
const buffer = await img.arrayBuffer()
return {
url,
mime: 'image/png',
buffer: Buffer.from(buffer),
}
}),
)

for (const result of resolved) {
const { url, mime, buffer } = result
resvg.resolveImage(url, mime, buffer)
if (resolved.length > 0) {
for (const result of resolved) {
const { url, buffer } = result
resvg.resolveImage(url, buffer)
}
}

const pngData = resvg.render()
Expand Down
Binary file modified example/url-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions example/url.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export class Resvg {
*/
cropByBBox(bbox: BBox): void

imagesToResolve(): Array<string>
resolveImage(href: string, buffer: Buffer): void

/** Get the SVG width */
get width(): number

Expand Down
2 changes: 2 additions & 0 deletions js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class Resvg {
* the viewbox/size of the svg and do not move the elements for simplicity
*/
cropByBBox(bbox: BBox): void
imagesToResolve(): Array<string>
resolveImage(href: string, buffer: Buffer): void
/** Get the SVG width */
get width(): number
/** Get the SVG height */
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,14 @@ impl Resvg {
}
}

#[wasm_bindgen(js_name = imagesToResolve)]
pub fn images_to_resolve(&self) -> Result<js_sys::Array, js_sys::Error> {
let images = self.images_to_resolve_inner()?;
let result = js_sys::Array::from_iter(images.into_iter().map(|s| JsValue::from(s)));
Ok(result)
}

#[wasm_bindgen(js_name = resolveImage)]
pub fn resolve_image(
&self,
href: String,
Expand Down Expand Up @@ -624,6 +626,7 @@ pub struct AsyncRenderer {
}

#[cfg(not(target_arch = "wasm32"))]
#[napi]
impl Task for AsyncRenderer {
type Output = RenderedImage;
type JsValue = RenderedImage;
Expand Down
19 changes: 2 additions & 17 deletions wasm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ declare class RenderedImage {
}
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
export type ResvgRenderOptions = {
font?: {
loadSystemFonts?: boolean;
fontFiles?: string[];
fontDirs?: string[];
defaultFontFamily?: string;
defaultFontSize?: number;
serifFamily?: string;
sansSerifFamily?: string;
cursiveFamily?: string;
fantasyFamily?: string;
monospaceFamily?: string;
};
dpi?: number;
languages?: string[];
shapeRendering?: 0 // optimizeSpeed
Expand Down Expand Up @@ -76,7 +64,6 @@ export type ResvgRenderOptions = {
right?: number;
bottom?: number;
};
logLevel?: "off" | "error" | "warn" | "info" | "debug" | "trace";
};
/**
* Initialize Wasm module
Expand All @@ -92,11 +79,9 @@ export declare const Resvg: {
innerBBox(): BBox | undefined;
getBBox(): BBox | undefined;
cropByBBox(bbox: BBox): void;
images_to_resolve(): string[];
resolve_image(href: string, buffer: Uint8Array): void;
imagesToResolve(): Array<string>;
resolveImage(href: string, buffer: Uint8Array): void;
readonly height: number;
readonly width: number;
};
};

export {};
31 changes: 22 additions & 9 deletions wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,10 @@
const output = document.getElementById('output')
const svgElement = document.querySelector('#input-svg')
const inputSVG = svgElement.value.trim()
let resvgOpts = {
// fitTo: {
// mode: 'width',
// value: 500,
// }
}

svg2png(inputSVG, resvgOpts)
let resvgOpts = {}
await svg2png(inputSVG, resvgOpts)

function svg2png(svgString, opts, hasCrop) {
async function svg2png(svgString, opts, hasCrop) {
const svg = svgString ? svgString : svgElement.value.trim()

if (!svg) {
Expand All @@ -181,6 +175,25 @@
const resvgJS = new resvg.Resvg(svg, opts)
document.querySelector('#svg-info').textContent = 'Original SVG size: ' + resvgJS.width + ' x ' + resvgJS.height + ' px'

const resolved = await Promise.all(
resvgJS.imagesToResolve().map(async (url) => {
console.log('image url', url)
const img = await fetch(url)
const buffer = await img.arrayBuffer()
// const MIME = img.headers.get('Content-Type')
return {
url,
buffer: new Uint8Array(buffer),
}
}),
)
if (resolved.length > 0) {
for (const result of resolved) {
const { url, buffer } = result
resvgJS.resolveImage(url, buffer)
}
}

const innerBBox = resvgJS.innerBBox()
const bbox = resvgJS.getBBox()
console.log('SVG innerBBox', innerBBox)
Expand Down
29 changes: 17 additions & 12 deletions wasm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
var __reExport = (target, module2, copyDefault, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return to;
return target;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __toCommonJS = /* @__PURE__ */ ((cache) => {
return (module2, temp) => {
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
};
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);

// wasm-binding.ts
var wasm_binding_exports = {};
__export(wasm_binding_exports, {
Resvg: () => Resvg2,
initWasm: () => initWasm
});
module.exports = __toCommonJS(wasm_binding_exports);

// wasm/dist/index.js
var wasm;
Expand Down Expand Up @@ -283,10 +287,10 @@ var Resvg = class {
_assertClass(bbox, BBox);
wasm.resvg_cropByBBox(this.ptr, bbox.ptr);
}
images_to_resolve() {
imagesToResolve() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.resvg_images_to_resolve(retptr, this.ptr);
wasm.resvg_imagesToResolve(retptr, this.ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
Expand All @@ -298,12 +302,12 @@ var Resvg = class {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
resolve_image(href, buffer) {
resolveImage(href, buffer) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(href, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.resvg_resolve_image(retptr, this.ptr, ptr0, len0, addHeapObject(buffer));
wasm.resvg_resolveImage(retptr, this.ptr, ptr0, len0, addHeapObject(buffer));
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
if (r1) {
Expand Down Expand Up @@ -427,3 +431,4 @@ var Resvg2 = class extends Resvg {
super(svg, JSON.stringify(options));
}
};
module.exports = __toCommonJS(wasm_binding_exports);
Loading

0 comments on commit 606337c

Please sign in to comment.