11import { promises as fs } from 'fs'
22import { join } from 'path'
3- import b from 'benny'
43
5- import { render } from '../index'
4+ import { createCanvas , Image } from '@napi-rs/canvas'
5+ import b from 'benny'
66import sharp from 'sharp'
77import svg2img from 'svg2img'
8- import { createCanvas , Image } from '@napi-rs/canvas'
8+
9+ import { render } from '../index'
910
1011async function run ( ) {
1112 const svg1 = await fs . readFile ( join ( __dirname , '../example/text.svg' ) )
@@ -15,7 +16,7 @@ async function run() {
1516 await b . suite (
1617 'resize width' ,
1718 b . add ( 'resvg-js(Rust)' , ( ) => {
18- render ( svg1 . toString ( 'utf-8' ) , {
19+ render ( svg1 , {
1920 background : '#eeebe6' ,
2021 fitTo : {
2122 mode : 'width' ,
@@ -33,7 +34,7 @@ async function run() {
3334 } ) ,
3435
3536 // test from https://github.com/Brooooooklyn/canvas/blob/main/example/resize-svg.js
36- b . add ( 'skr-canvas(Rust)' , async ( ) => {
37+ b . add ( 'skr-canvas(Rust)' , ( ) => {
3738 const image = new Image ( )
3839 image . src = svg1
3940
@@ -50,11 +51,11 @@ async function run() {
5051
5152 // fill the canvas with the image
5253 ctx . drawImage ( image , 0 , 0 )
53- await canvas . encode ( ' png')
54+ canvas . toBuffer ( 'image/ png')
5455 } ) ,
5556
5657 b . add ( 'svg2img(canvg and node-canvas)' , ( ) => {
57- svg2img ( svg1 , { width : 1200 , height : 623 } , function ( error , buffer ) { } )
58+ svg2img ( svg1 . toString ( 'utf8' ) , { width : 1200 , height : 623 } , function ( _error , _buffer ) { } )
5859 } ) ,
5960
6061 b . cycle ( ) ,
@@ -64,7 +65,7 @@ async function run() {
6465 await b . suite (
6566 'resize icon width' ,
6667 b . add ( 'resvg-js(Rust)' , ( ) => {
67- render ( icon . toString ( 'utf-8' ) , {
68+ render ( icon , {
6869 fitTo : {
6970 mode : 'width' ,
7071 value : 386 ,
@@ -77,7 +78,7 @@ async function run() {
7778 } ) ,
7879
7980 b . add ( 'sharp' , async ( ) => {
80- await sharp ( '__test__/ icon-alarm.svg' , {
81+ await sharp ( icon , {
8182 // https://github.com/lovell/sharp/issues/1421#issuecomment-514446234
8283 density : ( 72 * 386 ) / 24 , // 72 * width / actual width
8384 } )
@@ -86,7 +87,7 @@ async function run() {
8687 } ) ,
8788
8889 // test from https://github.com/Brooooooklyn/canvas/blob/main/example/resize-svg.js
89- b . add ( 'skr-canvas(Rust)' , async ( ) => {
90+ b . add ( 'skr-canvas(Rust)' , ( ) => {
9091 const image = new Image ( )
9192 image . src = icon
9293
@@ -103,11 +104,11 @@ async function run() {
103104
104105 // fill the canvas with the image
105106 ctx . drawImage ( image , 0 , 0 )
106- await canvas . encode ( ' png')
107+ canvas . toBuffer ( 'image/ png')
107108 } ) ,
108109
109110 b . add ( 'svg2img(canvg and node-canvas)' , ( ) => {
110- svg2img ( icon , { width : 386 , height : 386 } , function ( error , buffer ) { } )
111+ svg2img ( icon . toString ( 'utf8' ) , { width : 386 , height : 386 } , function ( _error , _buffer ) { } )
111112 } ) ,
112113
113114 b . cycle ( ) ,
@@ -117,7 +118,7 @@ async function run() {
117118 await b . suite (
118119 'default options and no text' ,
119120 b . add ( 'resvg-js(Rust)' , ( ) => {
120- render ( tiger . toString ( 'utf-8' ) , {
121+ render ( tiger , {
121122 font : {
122123 loadSystemFonts : false ,
123124 } ,
@@ -126,10 +127,10 @@ async function run() {
126127 } ) ,
127128
128129 b . add ( 'sharp' , async ( ) => {
129- await sharp ( '__test__/ tiger.svg' ) . toBuffer ( )
130+ await sharp ( tiger ) . toBuffer ( )
130131 } ) ,
131132
132- b . add ( 'skr-canvas(Rust)' , async ( ) => {
133+ b . add ( 'skr-canvas(Rust)' , ( ) => {
133134 const image = new Image ( )
134135 image . src = tiger
135136
@@ -146,16 +147,18 @@ async function run() {
146147
147148 // fill the canvas with the image
148149 ctx . drawImage ( image , 0 , 0 )
149- await canvas . encode ( ' png')
150+ canvas . toBuffer ( 'image/ png')
150151 } ) ,
151152
152153 b . add ( 'svg2img(canvg and node-canvas)' , ( ) => {
153- svg2img ( tiger , { width : 900 , height : 900 } , function ( error , buffer ) { } )
154+ svg2img ( tiger . toString ( 'utf8' ) , { width : 900 , height : 900 } , function ( _error , _buffer ) { } )
154155 } ) ,
155156
156157 b . cycle ( ) ,
157158 b . complete ( ) ,
158159 )
159160}
160161
161- run ( )
162+ run ( ) . catch ( ( e ) => {
163+ console . error ( e )
164+ } )
0 commit comments