diff --git a/Cargo.toml b/Cargo.toml index f72b746a..c9e66bea 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ js-sys = "0.3.60" resvg = { version = "0.25.0", default-features = false, features = [ "filter", "dump-svg", + "raster-images", ] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] @@ -41,6 +42,7 @@ napi-derive = "2.9.1" resvg = { version = "0.25.0", default-features = false, features = [ "filter", "dump-svg", + "raster-images", "text", ] } diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 9054c413..8a5e089d 100755 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -3,11 +3,54 @@ import { join } from 'path' import test from 'ava' import jimp from 'jimp-compact' +import fetch from 'node-fetch' import { Resvg, renderAsync } from '../index' import { jimpToRgbaPixels } from './helper' +test('Use href to load a JPG image without alpha', async (t) => { + const imgUrl = 'http://tva2.sinaimg.cn/crop.0.0.250.250.80/534b48acjw8ehw72edguyj206y06yq32.jpg' + const svg = ` + +` + const resvg = new Resvg(svg, { + font: { + loadSystemFonts: false, + }, + }) + 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, + buffer: Buffer.from(buffer), + } + }), + ) + if (resolved.length > 0) { + for (const result of resolved) { + const { url, buffer } = result + resvg.resolveImage(url, buffer) + } + } + const pngData = resvg.render() + const resvgPngBuffer = pngData.asPng() + const result1 = await jimp.read(resvgPngBuffer) + + const jimpImg = await fetch(imgUrl) + const jimpBuffer = await jimpImg.arrayBuffer() + const result2 = await jimp.read(jimpBuffer) + + const r1 = new jimp({ data: result1.bitmap.data, width: pngData.width, height: pngData.height }) + const r2 = new jimp({ data: result2.bitmap.data, width: result2.bitmap.width, height: result2.bitmap.height }) + + t.is(result1.hasAlpha(), false) + t.is(jimp.diff(r1, r2, 0.01).percent, 0) // 0 means similar, 1 means not similar +}) + test('svg to RGBA pixels Array', async (t) => { const svg = ` @@ -270,7 +313,7 @@ test('should render HEXA color format', async (t) => { const r1 = new jimp({ data: HEXABuffer.bitmap.data, width: 200, height: 200 }) const r2 = new jimp({ data: RGBABuffer.bitmap.data, width: 200, height: 200 }) - const diff = await jimp.diff(r1, r2, 0.01) + const diff = jimp.diff(r1, r2, 0.01) t.is(diff.percent, 0) // 0 means similar, 1 means not similar }) diff --git a/__test__/wasm.spec.ts b/__test__/wasm.spec.ts index c9d92967..ae24020b 100755 --- a/__test__/wasm.spec.ts +++ b/__test__/wasm.spec.ts @@ -4,6 +4,7 @@ import { join } from 'path' import test from 'ava' import jimp from 'jimp-compact' +import fetch from 'node-fetch' import { Resvg, initWasm } from '../wasm' @@ -14,6 +15,44 @@ test.before(async () => { await initWasm(fs.readFile(join(__dirname, '../wasm/index_bg.wasm'))) }) +test('Use href to load a JPG image without alpha', async (t) => { + const imgUrl = 'http://tva2.sinaimg.cn/crop.0.0.250.250.80/534b48acjw8ehw72edguyj206y06yq32.jpg' + const svg = ` + +` + const resvg = new Resvg(svg) + 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, + buffer: Buffer.from(buffer), + } + }), + ) + if (resolved.length > 0) { + for (const result of resolved) { + const { url, buffer } = result + resvg.resolveImage(url, buffer) + } + } + const pngData = resvg.render() + const resvgPngBuffer = Buffer.from(pngData.asPng()) + const result1 = await jimp.read(resvgPngBuffer) + + const jimpImg = await fetch(imgUrl) + const jimpBuffer = Buffer.from(await jimpImg.arrayBuffer()) + const result2 = await jimp.read(jimpBuffer) + + const r1 = new jimp({ data: result1.bitmap.data, width: pngData.width, height: pngData.height }) + const r2 = new jimp({ data: result2.bitmap.data, width: result2.bitmap.width, height: result2.bitmap.height }) + + t.is(result1.hasAlpha(), false) + t.is(jimp.diff(r1, r2, 0.01).percent, 0) // 0 means similar, 1 means not similar +}) + test('svg to RGBA pixels Array', async (t) => { const svg = ` @@ -183,7 +222,7 @@ test('should render HEXA color format', async (t) => { const r1 = new jimp({ data: HEXABuffer.bitmap.data, width: 200, height: 200 }) const r2 = new jimp({ data: RGBABuffer.bitmap.data, width: 200, height: 200 }) - const diff = await jimp.diff(r1, r2, 0.01) + const diff = jimp.diff(r1, r2, 0.01) t.is(diff.percent, 0) // 0 means similar, 1 means not similar }) diff --git a/wasm/index_bg.wasm b/wasm/index_bg.wasm index 8abebbad..1e7f25ce 100644 Binary files a/wasm/index_bg.wasm and b/wasm/index_bg.wasm differ