Skip to content

Commit

Permalink
test: add error case: 'the previous segment must be M/L/C'
Browse files Browse the repository at this point in the history
Some special svg paths can raise errors, add a test to ensure it is now fixed..

```
thread '<unnamed>' panicked at 'the previous segment must be M/L/C'

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
```
  • Loading branch information
yisibl committed Feb 8, 2023
1 parent 5c4dcb7 commit ecf46ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ test('Use href to load a JPG image without alpha', async (t) => {
t.is(jimp.diff(r1, r2, 0.01).percent, 0) // 0 means similar, 1 means not similar
})

test("should be no error: thread '<unnamed>' panicked at 'the previous segment must be M/L/C'", async (t) => {
const svg = `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Google News</title><path d="M21.267 21.2a.614.614 0 0 1-.613.613H3.344a.614.614 0 0 1-.612-.613V8.115a.614.614 0 0 1 .613-.613h17.309a.614.614 0 0 1 .613.613V21.2zm-3.032-3.42v-1.195a.08.08 0 0 0-.08-.08h-5.373v1.361h5.373a.082.082 0 0 0 .08-.083v-.003zm.817-2.587v-1.201a.08.08 0 0 0-.079-.082h-6.19v1.362h6.189a.079.079 0 0 0 .08-.078v-.004.003zm-.817-2.588V11.4a.08.08 0 0 0-.08-.08h-5.373v1.361h5.373a.082.082 0 0 0 .08-.079v.004zM8.15 14.045v1.226h1.77c-.145.748-.804 1.292-1.77 1.292a1.976 1.976 0 0 1 0-3.95 1.77 1.77 0 0 1 1.253.49l.934-.932a3.14 3.14 0 0 0-2.187-.853 3.268 3.268 0 1 0 0 6.537c1.89 0 3.133-1.328 3.133-3.197a3.941 3.941 0 0 0-.052-.619l-3.08.006zM2.27 7.654a.616.616 0 0 1 .613-.613h12.154l-1.269-3.49a.595.595 0 0 0-.743-.383L.368 7.775a.594.594 0 0 0-.323.775l2.225 6.112V7.654za.616.616 0 0 1 .613-.613h12.154l-1.269-3.49a.595.595 0 0 0-.743-.383L.368 7.775a.594.594 0 0 0-.323.775l2.225 6.112V7.654zm21.312-.31l-8.803-2.37.751 2.067h5.584a.614.614 0 0 1 .613.613v8.794l2.247-8.366a.592.592 0 0 0-.392-.739zm-4.496-1.675V2.795a.61.61 0 0 0-.611-.608H5.524a.61.61 0 0 0-.616.605v2.837l8.39-3.052a.594.594 0 0 1 .743.39l.544 1.497 4.501 1.205z"/></svg>`
const resvg = new Resvg(svg, {
background: '#fff',
fitTo: {
mode: 'width',
value: 300,
},
font: {
loadSystemFonts: false,
},
})
const pngBuffer = resvg.render().asPng()
const result = await jimp.read(pngBuffer)

t.is(result.getWidth(), 300)
t.is(result.getHeight(), 300)
})

test('svg to RGBA pixels Array', async (t) => {
const svg = `<svg width="10px" height="5px" viewBox="0 0 10 5" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect fill="red" x="0" y="0" width="5" height="5"></rect>
Expand Down
16 changes: 16 additions & 0 deletions __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ test('svg to RGBA pixels Array', async (t) => {
t.is(originPixels.toString(), pixelArray.toString())
})

test("should be no error: RuntimeError { message: 'unreachable', }", async (t) => {
const svg = `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Google News</title><path d="M21.267 21.2a.614.614 0 0 1-.613.613H3.344a.614.614 0 0 1-.612-.613V8.115a.614.614 0 0 1 .613-.613h17.309a.614.614 0 0 1 .613.613V21.2zm-3.032-3.42v-1.195a.08.08 0 0 0-.08-.08h-5.373v1.361h5.373a.082.082 0 0 0 .08-.083v-.003zm.817-2.587v-1.201a.08.08 0 0 0-.079-.082h-6.19v1.362h6.189a.079.079 0 0 0 .08-.078v-.004.003zm-.817-2.588V11.4a.08.08 0 0 0-.08-.08h-5.373v1.361h5.373a.082.082 0 0 0 .08-.079v.004zM8.15 14.045v1.226h1.77c-.145.748-.804 1.292-1.77 1.292a1.976 1.976 0 0 1 0-3.95 1.77 1.77 0 0 1 1.253.49l.934-.932a3.14 3.14 0 0 0-2.187-.853 3.268 3.268 0 1 0 0 6.537c1.89 0 3.133-1.328 3.133-3.197a3.941 3.941 0 0 0-.052-.619l-3.08.006zM2.27 7.654a.616.616 0 0 1 .613-.613h12.154l-1.269-3.49a.595.595 0 0 0-.743-.383L.368 7.775a.594.594 0 0 0-.323.775l2.225 6.112V7.654za.616.616 0 0 1 .613-.613h12.154l-1.269-3.49a.595.595 0 0 0-.743-.383L.368 7.775a.594.594 0 0 0-.323.775l2.225 6.112V7.654zm21.312-.31l-8.803-2.37.751 2.067h5.584a.614.614 0 0 1 .613.613v8.794l2.247-8.366a.592.592 0 0 0-.392-.739zm-4.496-1.675V2.795a.61.61 0 0 0-.611-.608H5.524a.61.61 0 0 0-.616.605v2.837l8.39-3.052a.594.594 0 0 1 .743.39l.544 1.497 4.501 1.205z"/></svg>`
const resvg = new Resvg(svg, {
background: '#fff',
fitTo: {
mode: 'width',
value: 300,
},
})
const pngBuffer = resvg.render().asPng()
const result = await jimp.read(Buffer.from(pngBuffer))

t.is(result.getWidth(), 300)
t.is(result.getHeight(), 300)
})

test('buffer input', async (t) => {
const filePath = '../example/text.svg'
const svg = await fs.readFile(join(__dirname, filePath))
Expand Down

1 comment on commit ecf46ac

@vercel
Copy link

@vercel vercel bot commented on ecf46ac Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

resvg-js – ./

resvg-js-yisibl.vercel.app
resvg-js-git-main-yisibl.vercel.app
resvg-js.vercel.app

Please sign in to comment.