@jimp/wasm-svg 0.0.0-stage → 2.0.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.
- package/LICENSE +21 -0
- package/README.md +142 -2
- package/RESVG-LICENSE +373 -0
- package/dist/browser/detect.d.ts +7 -0
- package/dist/browser/detect.d.ts.map +1 -0
- package/dist/browser/detect.js +56 -0
- package/dist/browser/detect.js.map +1 -0
- package/dist/browser/index.d.ts +43 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +211 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/load-wasm-browser.d.mts.map +1 -0
- package/dist/browser/load-wasm-browser.mjs.map +1 -0
- package/dist/browser/load-wasm.d.ts +6 -0
- package/dist/browser/load-wasm.js +17 -0
- package/dist/browser/package.json +3 -0
- package/dist/browser/resvg.wasm +0 -0
- package/dist/esm/detect.d.ts +7 -0
- package/dist/esm/detect.d.ts.map +1 -0
- package/dist/esm/detect.js +56 -0
- package/dist/esm/detect.js.map +1 -0
- package/dist/esm/index.d.ts +43 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +211 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/load-wasm.d.ts +3 -0
- package/dist/esm/load-wasm.d.ts.map +1 -0
- package/dist/esm/load-wasm.js +9 -0
- package/dist/esm/load-wasm.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +78 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAsB,MAAM,mBAAmB,CAAC;AAExE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,KAAK,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YACX,GAAG,GAAG,MAAM,CAAC;QACf,CAAC;QAED,IAAI,YAAY,EAAE,CAAC;YACjB,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,eAAe,EAAc,EAAE,CAAC;gBACrD,MAAM,KAAK,GAAG,MAAM,YAAY,CAAC,IAAI,CAAC,CAAC;gBAEvC,IAAI,KAAK,EAAE,CAAC;oBACV,GAAG,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;gBAChC,CAAC;YACH,CAAC;QACH,CAAC;QAED,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;QAE3B,IAAI,CAAC;YACH,OAAO;gBACL,IAAI,EAAE,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC;gBACjC,KAAK,EAAE,KAAK,CAAC,KAAK;gBAClB,MAAM,EAAE,KAAK,CAAC,MAAM;aACrB,CAAC;QACJ,CAAC;gBAAS,CAAC;YACT,KAAK,CAAC,IAAI,EAAE,CAAC;QACf,CAAC;IACH,CAAC;YAAS,CAAC;QACT,GAAG,CAAC,IAAI,EAAE,CAAC;IACb,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,GAAG,CAAC,EAAE,IAAI,EAAE,GAAqB,EAAE;IACzD,OAAO;QACL,IAAI,EAAE,eAAe;QACrB,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,KAAK;QACb,MAAM,EAAE,KAAK,IAAI,EAAE;YACjB,MAAM,IAAI,KAAK,CACb,2FAA2F,CAC5F,CAAC;QACJ,CAAC;QACD,MAAM,EAAE,CAAC,IAAI,EAAE,OAAO,GAAqB,EAAE,EAAE,EAAE,CAC/C,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC;KACH,CAAC;AACtC,CAAC","sourcesContent":["import { Buffer } from \"buffer\";\nimport { initWasm, Resvg, ResvgRenderOptions } from \"@resvg/resvg-wasm\";\nimport { Bitmap, Format } from \"@jimp/types\";\nimport { cssColorToHex } from \"@jimp/utils\";\nimport { z } from \"zod\";\n\nimport { isSvg } from \"./detect.js\";\nimport { loadWasm } from \"./load-wasm.js\";\n\n/** Returns the bytes of an image an SVG links to, or nothing to skip it */\nexport type ResolveImage = (\n href: string,\n) => Uint8Array | undefined | Promise<Uint8Array | undefined>;\n\nconst DecodeSvgOptionsSchema = z\n .object({\n /**\n * Width to render at, in pixels. The height keeps the SVG's aspect ratio.\n * With `height` too, the image fits inside both.\n * Defaults to the SVG's own size.\n */\n width: z.number().positive().optional(),\n /**\n * Height to render at, in pixels. The width keeps the SVG's aspect ratio.\n * With `width` too, the image fits inside both.\n * Defaults to the SVG's own size.\n */\n height: z.number().positive().optional(),\n /**\n * Render at a multiple of the SVG's own size, e.g. 2 for twice the\n * width and height. Can't be combined with `width` or `height`.\n * @default 1\n */\n scale: z.number().positive().optional(),\n /**\n * Fill the image with this colour behind the SVG: a CSS colour or a\n * 0xRRGGBBAA number.\n * @default transparent\n */\n background: z.union([z.string(), z.number()]).optional(),\n /**\n * Fonts (TTF, OTF, TTC or WOFF2) to draw `<text>` with. Without any, text isn't\n * drawn: the plugin has no access to system fonts.\n */\n fonts: z.array(z.instanceof(Uint8Array)).optional(),\n /**\n * The family to use for text that doesn't set `font-family`, and for the\n * generic `serif`, `sans-serif` and `monospace` families. Text in a family\n * none of the `fonts` have falls back to a font that has its characters.\n */\n defaultFontFamily: z.string().optional(),\n /**\n * Largest image, in megapixels, that will be rendered. Larger sizes are\n * rejected before any pixel memory is allocated.\n * @default 100\n */\n maxResolutionInMP: z.number().positive().optional(),\n /**\n * Loads the http(s) URLs of `<image>` elements, which are otherwise\n * skipped, by returning the PNG, JPEG or GIF's bytes. Images in `data:`\n * URLs are always drawn.\n *\n * Only use this for SVGs you trust, or check the URL first: an untrusted\n * SVG can make your server request any URL, including internal ones.\n */\n resolveImage: z\n .custom<ResolveImage>((value) => typeof value === \"function\")\n .optional(),\n })\n .strict()\n .refine(\n ({ scale, width, height }) =>\n scale === undefined || (width === undefined && height === undefined),\n \"Pass either scale or width and height, not both\",\n );\n\nexport type DecodeSvgOptions = z.input<typeof DecodeSvgOptionsSchema>;\n\nexport interface SvgWasm {\n /** The compiled `@resvg/resvg-wasm/index_bg.wasm` */\n resvg?: WebAssembly.Module;\n}\n\nexport interface SvgFormatOptions {\n /**\n * A precompiled wasm module for runtimes or bundlers where the plugin\n * can't load resvg's wasm itself.\n * resvg is initialized once, by the first instance that decodes, so pass\n * the same module to every instance.\n */\n wasm?: SvgWasm;\n}\n\nlet resvgReady: Promise<void> | undefined;\n\nfunction initResvgOnce(module?: WebAssembly.Module) {\n resvgReady ??= Promise.resolve(module ?? loadWasm())\n .then((wasm) => initWasm(wasm))\n .catch((error: unknown) => {\n // Another copy of the plugin, or the app, has initialized it already\n if (!(error instanceof Error && error.message.startsWith(\"Already\"))) {\n throw error;\n }\n });\n return resvgReady;\n}\n\n/** resvg renders premultiplied RGBA, which Jimp doesn't use */\nfunction unpremultiply(pixels: Uint8Array) {\n // Already copied out of wasm memory, so it's converted in place\n const data = Buffer.from(pixels.buffer, pixels.byteOffset, pixels.byteLength);\n\n for (let i = 0; i < data.length; i += 4) {\n const alpha = data[i + 3]!;\n\n if (alpha !== 0 && alpha !== 255) {\n for (let c = i; c < i + 3; c++) {\n data[c] = Math.min(255, Math.round((data[c]! * 255) / alpha));\n }\n }\n }\n\n return data;\n}\n\nfunction toHexColor(color: string | number) {\n return `#${(cssColorToHex(color) >>> 0).toString(16).padStart(8, \"0\")}`;\n}\n\n/** How resvg should scale the SVG, when that doesn't depend on its size */\nfunction getFit({\n width,\n height,\n scale,\n}: DecodeSvgOptions): ResvgRenderOptions[\"fitTo\"] {\n if (width !== undefined && height !== undefined) {\n return undefined;\n }\n\n if (width !== undefined) {\n return { mode: \"width\", value: Math.max(1, Math.round(width)) };\n }\n\n if (height !== undefined) {\n return { mode: \"height\", value: Math.max(1, Math.round(height)) };\n }\n\n return scale === undefined\n ? { mode: \"original\" }\n : { mode: \"zoom\", value: scale };\n}\n\n/**\n * The size to render the SVG at, rounded like resvg does, and how resvg\n * should scale the SVG's own size to it\n */\nfunction getRenderSize(\n intrinsicWidth: number,\n intrinsicHeight: number,\n options: DecodeSvgOptions,\n) {\n let { width, height } = options;\n\n // Fit inside both, so whichever is smaller relative to the SVG wins\n if (width !== undefined && height !== undefined) {\n if (width / intrinsicWidth <= height / intrinsicHeight) {\n height = undefined;\n } else {\n width = undefined;\n }\n }\n\n const fitTo = getFit({ ...options, width, height })!;\n\n switch (fitTo.mode) {\n case \"width\":\n return {\n fitTo,\n width: fitTo.value,\n height: Math.round((fitTo.value * intrinsicHeight) / intrinsicWidth),\n };\n case \"height\":\n return {\n fitTo,\n width: Math.round((fitTo.value * intrinsicWidth) / intrinsicHeight),\n height: fitTo.value,\n };\n case \"original\":\n return { fitTo, width: intrinsicWidth, height: intrinsicHeight };\n case \"zoom\": {\n // At least a pixel across the smaller side\n const value = Math.max(\n fitTo.value,\n 1 / Math.min(intrinsicWidth, intrinsicHeight),\n );\n\n return {\n fitTo: { mode: \"zoom\", value } as const,\n width: Math.round(intrinsicWidth * value),\n height: Math.round(intrinsicHeight * value),\n };\n }\n }\n}\n\nasync function decode(\n data: Buffer,\n options: DecodeSvgOptions,\n wasm?: WebAssembly.Module,\n): Promise<Bitmap> {\n const {\n background,\n fonts = [],\n defaultFontFamily,\n maxResolutionInMP = 100,\n resolveImage,\n ...size\n } = DecodeSvgOptionsSchema.parse(options);\n const renderOptions: ResvgRenderOptions = {\n background: background === undefined ? undefined : toHexColor(background),\n // Only the given fonts: the wasm has no access to the system's\n font: { fontBuffers: fonts, defaultFontFamily },\n };\n const fitTo = getFit(size);\n\n await initResvgOnce(wasm);\n\n // Parses the SVG. The pixels are only allocated by render()\n let svg = new Resvg(data, { ...renderOptions, fitTo });\n\n try {\n const target = getRenderSize(svg.width, svg.height, size);\n\n if (target.width * target.height > maxResolutionInMP * 1_000_000) {\n throw new Error(\n `SVG dimensions ${target.width}x${target.height} exceed the maximum resolution of ${maxResolutionInMP} megapixels. Use the width, height or scale option to render it smaller.`,\n );\n }\n\n // The scale depends on the SVG's size, and is a parsing option\n if (JSON.stringify(target.fitTo) !== JSON.stringify(fitTo)) {\n const scaled = new Resvg(data, { ...renderOptions, fitTo: target.fitTo });\n svg.free();\n svg = scaled;\n }\n\n if (resolveImage) {\n for (const href of svg.imagesToResolve() as string[]) {\n const image = await resolveImage(href);\n\n if (image) {\n svg.resolveImage(href, image);\n }\n }\n }\n\n const image = svg.render();\n\n try {\n return {\n data: unpremultiply(image.pixels),\n width: image.width,\n height: image.height,\n };\n } finally {\n image.free();\n }\n } finally {\n svg.free();\n }\n}\n\n/**\n * SVG images (.svg), rendered with resvg. Only reading is supported: writing\n * would need a raster to vector conversion.\n */\nexport default function svg({ wasm }: SvgFormatOptions = {}) {\n return {\n mime: \"image/svg+xml\",\n hasAlpha: true,\n detect: isSvg,\n encode: async () => {\n throw new Error(\n \"Jimp can read SVG but can't write it. Write a raster format like PNG (image/png) instead.\",\n );\n },\n decode: (data, options: DecodeSvgOptions = {}) =>\n decode(data, options, wasm?.resvg),\n } satisfies Format<\"image/svg+xml\">;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-wasm.d.ts","sourceRoot":"","sources":["../../src/load-wasm.ts"],"names":[],"mappings":"AAKA,gDAAgD;AAChD,wBAAsB,QAAQ,gCAK7B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
/** Reads and compiles resvg's wasm from disk */
|
|
5
|
+
export async function loadWasm() {
|
|
6
|
+
const wasm = await readFile(require.resolve("@resvg/resvg-wasm/index_bg.wasm"));
|
|
7
|
+
return WebAssembly.compile(new Uint8Array(wasm));
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=load-wasm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-wasm.js","sourceRoot":"","sources":["../../src/load-wasm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C,gDAAgD;AAChD,MAAM,CAAC,KAAK,UAAU,QAAQ;IAC5B,MAAM,IAAI,GAAG,MAAM,QAAQ,CACzB,OAAO,CAAC,OAAO,CAAC,iCAAiC,CAAC,CACnD,CAAC;IACF,OAAO,WAAW,CAAC,OAAO,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { createRequire } from \"node:module\";\n\nconst require = createRequire(import.meta.url);\n\n/** Reads and compiles resvg's wasm from disk */\nexport async function loadWasm() {\n const wasm = await readFile(\n require.resolve(\"@resvg/resvg-wasm/index_bg.wasm\"),\n );\n return WebAssembly.compile(new Uint8Array(wasm));\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jimp/wasm-svg",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"RESVG-LICENSE"
|
|
7
|
+
],
|
|
8
|
+
"repository": "jimp-dev/jimp",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=22"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"lint": "eslint .",
|
|
14
|
+
"test": "vitest",
|
|
15
|
+
"test:browser": "vitest --config vitest.config.browser.mjs",
|
|
16
|
+
"build": "tshy && node copy-wasm.mjs",
|
|
17
|
+
"dev": "tshy --watch",
|
|
18
|
+
"clean": "rimraf node_modules .tshy .tshy-build dist .turbo"
|
|
19
|
+
},
|
|
20
|
+
"author": "Andrew Lisowski <lisowski54@gmail.com>",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@jimp/config-eslint": "2.0.0",
|
|
24
|
+
"@jimp/config-typescript": "2.0.0",
|
|
25
|
+
"@jimp/config-vitest": "2.0.0",
|
|
26
|
+
"@jimp/core": "2.0.0",
|
|
27
|
+
"@jimp/js-jpeg": "2.0.0",
|
|
28
|
+
"@jimp/js-png": "2.0.0",
|
|
29
|
+
"@jimp/test-utils": "2.0.0",
|
|
30
|
+
"@types/node": "^22.20.5",
|
|
31
|
+
"@vitest/browser": "^5.1.1",
|
|
32
|
+
"eslint": "^10.15.0",
|
|
33
|
+
"tshy": "~4.0.0",
|
|
34
|
+
"typescript": "^7.0.2",
|
|
35
|
+
"vite-plugin-node-polyfills": "^0.28.0",
|
|
36
|
+
"vitest": "^5.1.1"
|
|
37
|
+
},
|
|
38
|
+
"tshy": {
|
|
39
|
+
"exclude": [
|
|
40
|
+
"**/*.test.ts"
|
|
41
|
+
],
|
|
42
|
+
"dialects": [
|
|
43
|
+
"esm"
|
|
44
|
+
],
|
|
45
|
+
"esmDialects": [
|
|
46
|
+
"browser"
|
|
47
|
+
],
|
|
48
|
+
"exports": {
|
|
49
|
+
"./package.json": "./package.json",
|
|
50
|
+
".": "./src/index.ts"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"exports": {
|
|
54
|
+
"./package.json": "./package.json",
|
|
55
|
+
".": {
|
|
56
|
+
"browser": {
|
|
57
|
+
"types": "./dist/browser/index.d.ts",
|
|
58
|
+
"default": "./dist/browser/index.js"
|
|
59
|
+
},
|
|
60
|
+
"import": {
|
|
61
|
+
"types": "./dist/esm/index.d.ts",
|
|
62
|
+
"default": "./dist/esm/index.js"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"type": "module",
|
|
67
|
+
"publishConfig": {
|
|
68
|
+
"access": "public"
|
|
69
|
+
},
|
|
70
|
+
"sideEffects": false,
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@jimp/types": "2.0.0",
|
|
73
|
+
"@jimp/utils": "2.0.0",
|
|
74
|
+
"@resvg/resvg-wasm": "2.6.2",
|
|
75
|
+
"buffer": "^6.0.3",
|
|
76
|
+
"zod": "^4.6.7"
|
|
77
|
+
},
|
|
78
|
+
"module": "./dist/esm/index.js",
|
|
79
|
+
"gitHead": "a0c6199088b0a3f28162a1742c9714b285dd194a"
|
|
80
|
+
}
|