@@ -4,19 +4,19 @@ import { join } from 'path'
44import test from 'ava'
55import jimp from 'jimp'
66
7- import { render , renderAsync } from '../index'
7+ import { Resvg , renderAsync } from '../index'
88
99test ( 'fit to width' , async ( t ) => {
1010 const filePath = '../example/text.svg'
1111 const svg = await fs . readFile ( join ( __dirname , filePath ) )
12- const svgString = svg . toString ( 'utf-8' )
13- const pngData = render ( svgString , {
12+ const resvg = new Resvg ( svg , {
1413 background : '#eeebe6' ,
1514 fitTo : {
1615 mode : 'width' ,
1716 value : 1200 ,
1817 } ,
1918 } )
19+ const pngData = resvg . render ( )
2020 const result = await jimp . read ( pngData )
2121
2222 t . is ( result . getWidth ( ) , 1200 )
@@ -27,9 +27,10 @@ test('Set the background with alpha by rgba().', async (t) => {
2727 const filePath = './tiger.svg'
2828 const svg = await fs . readFile ( join ( __dirname , filePath ) )
2929 const svgString = svg . toString ( 'utf-8' )
30- const pngData = render ( svgString , {
30+ const resvg = new Resvg ( svgString , {
3131 background : 'rgba(0, 0, 0, 0.6)' ,
3232 } )
33+ const pngData = resvg . render ( )
3334 const result = await jimp . read ( pngData )
3435
3536 t . is ( result . hasAlpha ( ) , true )
@@ -39,9 +40,10 @@ test('Set the background with alpha by rgb().', async (t) => {
3940 const filePath = './tiger.svg'
4041 const svg = await fs . readFile ( join ( __dirname , filePath ) )
4142 const svgString = svg . toString ( 'utf-8' )
42- const pngData = render ( svgString , {
43+ const resvg = new Resvg ( svgString , {
4344 background : 'rgb(255, 0, 0, .832)' ,
4445 } )
46+ const pngData = resvg . render ( )
4547 const result = await jimp . read ( pngData )
4648
4749 t . is ( result . hasAlpha ( ) , true )
@@ -51,9 +53,10 @@ test('Set the background without alpha by hsla()', async (t) => {
5153 const filePath = './tiger.svg'
5254 const svg = await fs . readFile ( join ( __dirname , filePath ) )
5355 const svgString = svg . toString ( 'utf-8' )
54- const pngData = render ( svgString , {
56+ const resvg = new Resvg ( svgString , {
5557 background : 'hsla(255, 80%, 50%, 1)' ,
5658 } )
59+ const pngData = resvg . render ( )
5760 const result = await jimp . read ( pngData )
5861
5962 t . is ( result . hasAlpha ( ) , false )
@@ -63,13 +66,14 @@ test('Load custom font', async (t) => {
6366 const filePath = '../example/text.svg'
6467 const svg = await fs . readFile ( join ( __dirname , filePath ) )
6568 const svgString = svg . toString ( 'utf-8' )
66- const pngData = render ( svgString , {
69+ const resvg = new Resvg ( svgString , {
6770 font : {
6871 fontFiles : [ './example/SourceHanSerifCN-Light-subset.ttf' ] , // Load custom fonts.
6972 loadSystemFonts : false , // It will be faster to disable loading system fonts.
7073 defaultFontFamily : 'Source Han Serif CN Light' ,
7174 } ,
7275 } )
76+ const pngData = resvg . render ( )
7377 const result = await jimp . read ( pngData )
7478
7579 t . is ( result . getWidth ( ) , 1324 )
@@ -86,13 +90,13 @@ test('Async rendering', async (t) => {
8690 defaultFontFamily : 'Source Han Serif CN Light' ,
8791 } ,
8892 }
89- const syncRenderingResult = render ( svg , params )
93+ const syncRenderingResult = new Resvg ( svg , params )
9094 const asyncRenderingResult = await renderAsync ( svg , params )
91- t . is ( syncRenderingResult . length , asyncRenderingResult . length )
95+ t . is ( syncRenderingResult . render ( ) . length , asyncRenderingResult . length )
9296 // Do not run this assert in non-x64 environment.
9397 // It's too slow
9498 if ( process . arch === 'x64' ) {
95- t . deepEqual ( syncRenderingResult , asyncRenderingResult )
99+ t . deepEqual ( syncRenderingResult , syncRenderingResult )
96100 }
97101} )
98102
@@ -126,14 +130,15 @@ test('should generate a 80x80 png and opaque', async (t) => {
126130 const svg = `<svg width="200px" height="200px" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
127131 <rect fill="green" x="0" y="0" width="100" height="100"></rect>
128132 </svg>`
129- const pngData = render ( svg , {
133+ const resvg = new Resvg ( svg , {
130134 crop : {
131135 left : 20 ,
132136 top : 20 ,
133137 right : 100 ,
134138 bottom : 100 ,
135139 } ,
136140 } )
141+ const pngData = resvg . render ( )
137142 const result = await jimp . read ( pngData )
138143
139144 t . is ( result . getWidth ( ) , 100 - 20 )
@@ -145,7 +150,8 @@ test('should generate a 80x80 png and opaque', async (t) => {
145150// https://github.com/RazrFalcon/resvg/commit/5998e9b8411ff3f0171515371938ee1940be17c3
146151test ( 'should generate a 100x100 transparent png' , async ( t ) => {
147152 const svg = `<svg xmlns="http://www.w3.org/2000/svg"></svg>`
148- const pngData = render ( svg )
153+ const resvg = new Resvg ( svg )
154+ const pngData = resvg . render ( )
149155 const result = await jimp . read ( pngData )
150156
151157 t . is ( result . hasAlpha ( ) , true )
@@ -155,9 +161,10 @@ test('should generate a 100x100 transparent png', async (t) => {
155161
156162test ( 'should generate a 128x128 png' , async ( t ) => {
157163 const svg = `<svg width="128" height="128" xmlns="http://www.w3.org/2000/svg"></svg>`
158- const pngData = render ( svg , {
164+ const resvg = new Resvg ( svg , {
159165 background : 'green' ,
160166 } )
167+ const pngData = resvg . render ( )
161168 const result = await jimp . read ( pngData )
162169
163170 t . is ( result . hasAlpha ( ) , false )
@@ -187,7 +194,7 @@ test('should render `<use xlink:href>` to an `<svg>` element', async (t) => {
187194test ( 'should throw because invalid SVG attribute (width attribute is 0)' , ( t ) => {
188195 const error = t . throws (
189196 ( ) => {
190- render ( `
197+ new Resvg ( `
191198 <svg width="0" height="100px" viewBox="0 0 200 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
192199 <rect fill="#FCA6A6" x="0" y="0" width="200" height="100"></rect>
193200 </svg>
@@ -202,7 +209,7 @@ test('should throw because invalid SVG attribute (width attribute is 0)', (t) =>
202209test ( 'should throw because invalid options (width 0)' , ( t ) => {
203210 const error = t . throws (
204211 ( ) => {
205- render (
212+ const resvg = new Resvg (
206213 `<svg width="200px" height="100px" viewBox="0 0 200 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
207214 <rect fill="#FCA6A6" x="0" y="0" width="200" height="100"></rect>
208215 </svg>` ,
@@ -213,6 +220,8 @@ test('should throw because invalid options (width 0)', (t) => {
213220 } ,
214221 } ,
215222 )
223+
224+ resvg . render ( )
216225 } ,
217226 { instanceOf : Error } ,
218227 )
@@ -223,12 +232,14 @@ test('should throw because invalid options (width 0)', (t) => {
223232test ( 'should throw because invalid options (zoom 0)' , ( t ) => {
224233 const error = t . throws (
225234 ( ) => {
226- render ( '<svg width="128" height="128" xmlns="http://www.w3.org/2000/svg"></svg>' , {
235+ const resvg = new Resvg ( '<svg width="128" height="128" xmlns="http://www.w3.org/2000/svg"></svg>' , {
227236 fitTo : {
228237 mode : 'zoom' ,
229238 value : 0 ,
230239 } ,
231240 } )
241+
242+ resvg . render ( )
232243 } ,
233244 { instanceOf : Error } ,
234245 )
@@ -243,7 +254,7 @@ test('should throw because missing namespace', (t) => {
243254 16"></svg>`
244255 const error = t . throws (
245256 ( ) => {
246- render ( svg )
257+ new Resvg ( svg )
247258 } ,
248259 { instanceOf : Error } ,
249260 )
0 commit comments