Skip to content

Commit 5a0946c

Browse files
committed
test: new class API
1 parent 2c01385 commit 5a0946c

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

__test__/index.spec.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { join } from 'path'
44
import test from 'ava'
55
import jimp from 'jimp'
66

7-
import { render, renderAsync } from '../index'
7+
import { Resvg, renderAsync } from '../index'
88

99
test('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
146151
test('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

156162
test('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) => {
187194
test('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) =>
202209
test('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) => {
223232
test('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
)

example/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { promises } = require('fs')
22
const { join } = require('path')
33
const { performance } = require('perf_hooks')
44

5-
const { Resvg } = require('../js-binding')
5+
const { Resvg } = require('../index')
66

77
async function main() {
88
const svg = await promises.readFile(join(__dirname, './text.svg'))
@@ -23,15 +23,15 @@ async function main() {
2323
}
2424

2525
const t = performance.now()
26-
const rusty = new Resvg(svg, JSON.stringify(opts))
26+
const rusty = new Resvg(svg, opts)
27+
const pngData = rusty.render()
2728

2829
console.info(rusty.width)
2930
console.info(rusty.height)
30-
console.info(rusty)
31-
// const pngData = rusty.render()
31+
// console.info(rusty)
3232
console.info('✨ Done in', performance.now() - t, 'ms')
3333

34-
// await promises.writeFile(join(__dirname, './text-out.png'), pngData)
34+
await promises.writeFile(join(__dirname, './text-out.png'), pngData)
3535
}
3636

3737
main()

example/text-out.png

50 Bytes
Loading

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ export function renderAsync(
4747
options?: ResvgRenderOptions | null,
4848
signal?: AbortSignal | null,
4949
): Promise<Buffer>
50+
export class Resvg {
51+
constructor(svg: Buffer | string, options?: ResvgRenderOptions | null)
52+
render(): Buffer
53+
get width(): number
54+
get height(): number
55+
}

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { render: _render, renderAsync: _renderAsync, Resvg } = require('./js-binding.js')
1+
const { render: _render, renderAsync: _renderAsync, Resvg: _Resvg } = require('./js-binding.js')
22

33
module.exports.render = function render(svg, options) {
44
if (options) {
@@ -14,4 +14,10 @@ module.exports.renderAsync = function renderAsync(svg, options, signal) {
1414
return _renderAsync(svg, null, signal)
1515
}
1616

17-
module.exports.Resvg = Resvg
17+
module.exports.Resvg = class Resvg extends _Resvg {
18+
constructor(svg, options) {
19+
super(svg, JSON.stringify(options))
20+
}
21+
}
22+
23+
// module.exports.Resvg = _Resvg

0 commit comments

Comments
 (0)