diff --git a/__test__/tiger.svg b/__test__/tiger.svg index 679edec2..3394bf65 100644 --- a/__test__/tiger.svg +++ b/__test__/tiger.svg @@ -1,725 +1,1154 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/benchmark/bench.ts b/benchmark/bench.ts index 70b786d1..d9c58616 100755 --- a/benchmark/bench.ts +++ b/benchmark/bench.ts @@ -1 +1,109 @@ -// TODO +import { promises as fs } from 'fs' +import { join } from 'path' +import b from 'benny' + +import { render } from '../index' +import sharp from 'sharp' +import svg2img from 'svg2img' +import { createCanvas, Image } from '@napi-rs/canvas' + +async function run() { + const svg1 = await fs.readFile(join(__dirname, '../example/text.svg')) + const tiger = await fs.readFile(join(__dirname, '../__test__/tiger.svg')) + + await b.suite( + 'resize width', + b.add('resvg-js(Rust)', () => { + render(svg1.toString('utf-8'), { + background: '#eeebe6', + fitTo: { + mode: 'width', + value: 1200, + }, + font: { + loadSystemFonts: false, + }, + logLevel: 'off', + }) + }), + + b.add('sharp', async () => { + await sharp('example/text.svg').resize(1200).toBuffer() + }), + + // test from https://github.com/Brooooooklyn/canvas/blob/main/example/resize-svg.js + b.add('skr-canvas(Rust)', async () => { + const image = new Image() + image.src = svg1 + + const w = 1200 + const h = 623 + + // resize SVG + image.width = w + image.height = h + + // create a canvas of the same size as the image + const canvas = createCanvas(w, h) + const ctx = canvas.getContext('2d') + + // fill the canvas with the image + ctx.drawImage(image, 0, 0) + await canvas.encode('png') + }), + + b.add('svg2img(canvg and node-canvas)', () => { + svg2img(svg1, { width: 1200 }, function (error, buffer) {}) + }), + + b.cycle(), + b.complete(), + ) + + await b.suite( + 'default options and no text', + b.add('resvg-js(Rust)', () => { + render(tiger.toString('utf-8'), { + font: { + loadSystemFonts: false, + fontFiles: ['../example/SourceHanSerifCN-Light-subset.ttf'], + defaultFontFamily: 'Source Han Serif CN Light', + }, + logLevel: 'off', + }) + }), + + b.add('sharp', async () => { + await sharp('__test__/tiger.svg').toBuffer() + }), + + b.add('skr-canvas(Rust)', async () => { + const image = new Image() + image.src = tiger + + const w = 1200 + const h = 623 + + // resize SVG + image.width = w + image.height = h + + // create a canvas of the same size as the image + const canvas = createCanvas(w, h) + const ctx = canvas.getContext('2d') + + // fill the canvas with the image + ctx.drawImage(image, 0, 0) + await canvas.encode('png') + }), + + b.add('svg2img(canvg and node-canvas)', () => { + svg2img(tiger, function (error, buffer) {}) + }), + + b.cycle(), + b.complete(), + ) +} + +run() diff --git a/example/index.js b/example/index.js index a1dac71a..15e54654 100644 --- a/example/index.js +++ b/example/index.js @@ -3,6 +3,7 @@ const { join } = require('path') const { performance } = require('perf_hooks') const { render } = require('../index') +const svg2img = require('svg2img') async function main() { const svg = await fs.readFile(join(__dirname, './text.svg')) @@ -28,6 +29,11 @@ async function main() { console.log('✨ Done in', t1 - t0, 'ms') await fs.writeFile(join(__dirname, './text-out.png'), pngData) + + svg2img(svg, { width: 1200, preserveAspectRatio: true }, function (error, buffer) { + fs.writeFile(join(__dirname, './text.png'), buffer) + }) } +// sharp2() main() diff --git a/example/text.svg b/example/text.svg index ed910275..2bd7c63f 100644 --- a/example/text.svg +++ b/example/text.svg @@ -1,408 +1,701 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 竹外桃花三两枝 - 春江水暖鸭先知 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 竹外桃花三两枝 + 春江水暖鸭先知 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file + + diff --git a/package.json b/package.json index b446ff9f..f10277eb 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,9 @@ "format": "run-p format:md format:json format:yaml format:source format:rs", "format:md": "prettier --parser markdown --write './**/*.md'", "format:json": "prettier --parser json --write './**/*.json'", + "format:svg": "prettier --parser html --write './**/*.svg'", "format:rs": "cargo fmt", - "format:source": "prettier --config ./package.json --write './**/*.{js,ts}'", + "format:source": "prettier --config ./package.json --write './**/*.{js,ts,mjs}'", "format:yaml": "prettier --parser yaml --write './**/*.{yml,yaml}'", "lint": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}'", "lint:fix": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}' --fix", @@ -48,6 +49,7 @@ "version": "napi version" }, "devDependencies": { + "@napi-rs/canvas": "^0.1.13", "@napi-rs/cli": "^1.3.3", "@swc-node/register": "^1.3.6", "@types/node": "^14.17.22", @@ -66,6 +68,8 @@ "lint-staged": "^11.2.3", "npm-run-all": "^4.1.5", "prettier": "^2.4.1", + "sharp": "^0.29.2", + "svg2img": "^0.9.4", "typescript": "^4.4.4" }, "dependencies": { @@ -75,7 +79,8 @@ "*.@(js|ts|tsx)": ["prettier --write", "eslint -c .eslintrc.yml --fix"], "*.@(yml|yaml)": ["prettier --parser yaml --write"], "*.md": ["prettier --parser markdown --write"], - "*.json": ["prettier --parser json --write"] + "*.json": ["prettier --parser json --write"], + "*.svg": ["prettier --parser html --write"] }, "ava": { "require": ["@swc-node/register"],