svgdotjs / svgdom

Straightforward DOM implementation to make SVG.js run headless on Node.js
MIT License
269 stars 53 forks source link

bbox() returns zeros for text elems? #104

Open matehegedus opened 1 year ago

matehegedus commented 1 year ago

Hi,

I have the following problem, when I am using bbox()-es for some calculations, in my node.js environment it returns only zeros (although in web it works well)

this is my minimal reproduction:

 config.setFontDir("src/fonts/Roboto");
  config.setFontFamilyMappings({
    Roboto: "Roboto-Black.ttf",
    "sans-serif": "Roboto-Black.ttf",
    "Roboto-Bold": "Roboto-Bold.ttf",
  });
  config.preloadFonts();
  const window = createSVGWindow();
  const { document } = window;
  registerWindow(window, document);
  const draw = SVG(document.documentElement);

  draw.attr({
    width: 200,
    height: 200,
  });

  const group = draw.group().id(`txtGroup-example`);
  const textELem = group
    .text("label")
    .fill("red")
    .attr({
      x: 50,
      y: 50,
      "text-anchor": "middle",
      "dominant-baseline": "auto",
      transform: `rotate(${90},${50},${50})`,
      "font-size": `${16}px`,
      "font-family": `Roboto`,
    });

  const bbox = textELem.bbox();
  console.log("group :>> ", bbox); //every element inside bbox will be zero

Am I doing something wrong ?

Thanks

matehegedus commented 1 year ago

My bad, I was using 'font-size: 16px', although it should be only '16'. This caused the issue in node.js

Fuzzyma commented 1 year ago

Glad you figured it out. That would have taken me ages to realize :D