Open testerez opened 2 years ago
Similar problem, mines related to measuring text elements, the widths seem to be short:
The red rectangle is using the coordinates I get back from getBBox()
/ getClientBoundingBox()
. The width gets shorter the longer the text gets e.g.
Similar problem, mines related to measuring text elements, the widths seem to be short:
The red rectangle is using the coordinates I get back from
getBBox()
/getClientBoundingBox()
. The width gets shorter the longer the text gets e.g.
I'm also having the same issue with measuring text elements. My guess is it's using dimensions from another font, default serif maybe. Any solution?
Usually if you register your own font properly it should use that one to measure the dimensions. I would need the font file to test that issue
@testerez @jameshowe @twilson90 In order to investigate this I need the font files.
@Fuzzyma the custom font in the example is https://fonts.google.com/specimen/Mansalva (regular), but you can see it happens even with Arial fonts too.
@jameshowe do you have an example for me with Arial? I guess thats easiest to replicate
This was a while ago, I'm not sure I have the code anymore (presume that's what you are after). I'll have a look around.
Ok so thankfully manage to find it, I've tried to strip it back to the smallest possible demonstrable PoC - I am able to replicate the same issue using Arial font with the following code:
import { createSVGWindow } from 'svgdom';
import { SVG, registerWindow } from '@svgdotjs/svg.js';
import { writeFileSync } from 'fs';
const window = createSVGWindow();
const doc = window.document;
registerWindow(window, doc);
// create svg.js instance
const canvas = SVG(doc.documentElement).size(530, 670);
let curOffset = 30
for (let i = 12; i < 30; i++) {
// render text
const position = canvas.text(`ARIAL FONT @ ${i}px`);
position
.font({
fill: 'black',
family: 'Arial',
size: i
})
.move(30, curOffset)
// render bounding box
const bounds = position.node.getBoundingClientRect()
canvas
.rect(bounds.width, bounds.height)
.move(bounds.x, bounds.y)
.fill('#00000000')
.stroke('red');
// add some padding between each render
curOffset += bounds.height + 5
}
await writeFileSync('output.svg', canvas.svg());
For the packages:
npm i @svgdotjs/svg.js@3.1.2 svgdom@0.1.10
Thanks will look into it once I find some time
I'm trying to calculate the size of an SVG in node but I'm getting incorrect results.
Here is the SVG:
And the node.js code:
Result:
When calculating the bbox the same way in the browser, I'm getting the expected result:
(
width
andheight
are correct here)I've put together an example to compare node and browser results here: https://stackblitz.com/edit/nextjs-ct2xan?file=svgString.js