Skip to content

Commit

Permalink
test: add a bbox test with stroke
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Jan 29, 2024
1 parent c336d70 commit a21b096
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
32 changes: 30 additions & 2 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ test('should render `<use xlink:href>` to an `<defs>` element', async (t) => {
t.is(result.getHeight(), 900)
})

test('should get svg bbox', (t) => {
test('should get svg innerBBox', (t) => {
const svg = `<svg viewBox="-40 0 150 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g fill="red" transform="rotate(-10 50 100) translate(-36 45.5) skewX(40) scale(1 0.5)">
<path id="heart" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z" />
Expand All @@ -524,7 +524,35 @@ test('should get svg bbox', (t) => {
}
})

test('should get svg bbox(rect)', async (t) => {
test('should get svg bbox with stroke', (t) => {
const svg = `<svg viewBox="-40 0 180 260" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g fill="green" transform="rotate(-10 50 100) translate(-36 45.5) skewX(40) scale(1 0.5)">
<path id="heart" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z" />
</g>
<use xlink:href="#heart" fill="none" stroke="red" stroke-width="10" />
</svg>`

const resvg = new Resvg(svg)
const bbox = resvg.getBBox()
t.not(bbox, undefined)
if (bbox) {
t.is(bbox.width, 116.70712280273438)
t.is(bbox.height, 90)
t.is(bbox.x, -21.707120895385742)
t.is(bbox.y, 5)
}

const innerBBox = resvg.innerBBox()
t.not(innerBBox, undefined)
if (innerBBox) {
t.is(innerBBox.width, 125)
t.is(innerBBox.height, 93)
t.is(innerBBox.x, -30)
t.is(innerBBox.y, 5)
}
})

test('should get svg bbox(rect) and cropByBBox', async (t) => {
const svg = `<svg width="300px" height="300px" viewBox="0 0 300 300" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect fill="#5283E8" x="50.4" y="60.8" width="200" height="100"></rect>
</svg>`
Expand Down
30 changes: 29 additions & 1 deletion __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,35 @@ test('should render `<use xlink:href>` to an `<defs>` element', async (t) => {
t.is(result.getHeight(), 900)
})

test('should get svg bbox(rect)', async (t) => {
test('should get svg bbox with stroke', (t) => {
const svg = `<svg viewBox="-40 0 180 260" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g fill="green" transform="rotate(-10 50 100) translate(-36 45.5) skewX(40) scale(1 0.5)">
<path id="heart" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z" />
</g>
<use xlink:href="#heart" fill="none" stroke="red" stroke-width="10" />
</svg>`

const resvg = new Resvg(svg)
const bbox = resvg.getBBox()
t.not(bbox, undefined)
if (bbox) {
t.is(bbox.width, 116.70712280273438)
t.is(bbox.height, 90)
t.is(bbox.x, -21.707120895385742)
t.is(bbox.y, 5)
}

const innerBBox = resvg.innerBBox()
t.not(innerBBox, undefined)
if (innerBBox) {
t.is(innerBBox.width, 125)
t.is(innerBBox.height, 93)
t.is(innerBBox.x, -30)
t.is(innerBBox.y, 5)
}
})

test('should get svg bbox(rect) and cropByBBox', async (t) => {
const svg = `<svg width="300px" height="300px" viewBox="0 0 300 300" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect fill="#5283E8" x="50.4" y="60.8" width="200" height="100"></rect>
</svg>`
Expand Down
Binary file modified example/bbox-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion example/bbox.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified wasm/index_bg.wasm
Binary file not shown.

1 comment on commit a21b096

@vercel
Copy link

@vercel vercel bot commented on a21b096 Jan 29, 2024

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-git-main-yisibl.vercel.app
resvg-js.vercel.app
resvg-js-yisibl.vercel.app

Please sign in to comment.