Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font weight not rendering as expected #289

Open
camchenry opened this issue Jan 14, 2024 · 1 comment
Open

Font weight not rendering as expected #289

camchenry opened this issue Jan 14, 2024 · 1 comment

Comments

@camchenry
Copy link

Hi, I was trying to render a SVG document which has varied font weights in it. Here is an example script:

const { promises } = require("fs");
const { join } = require("path");
const { Resvg } = require("@resvg/resvg-js");

const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
  <text x="15" y="15" fill="#fff">Normal</text>
  <text x="15" y="30" fill="#fff" font-style="italic">Italic</text>
  <text x="15" y="45" fill="#fff" font-weight="bold">Bold</text>
  <text x="15" y="60" fill="#fff" font-style="italic" font-weight="bold">Bold Italic</text>
</svg>
`;

async function main() {
  const opts = {
    background: "#000",
    fitTo: {
      mode: "width",
      value: 1200,
    },
    font: {
      loadSystemFonts: true, // It will be faster to disable loading system fonts.
    },
  };
  const resvg = new Resvg(svg, opts);
  const pngData = resvg.render();
  const pngBuffer = pngData.asPng();

  console.info("Original SVG Size:", `${resvg.width} x ${resvg.height}`);
  console.info("Output PNG Size  :", `${pngData.width} x ${pngData.height}`);

  await promises.writeFile(join(__dirname, "./text-out.png"), pngBuffer);
}

main();

which I just ran using bun or node example.js.

The output looks like this on my machine:

Image

It resolves to the "Arial" font by default, which I know for certain supports bold font weight. I would expect these to have different font weights.


If I render the example SVG using resvg CLI (version 0.37.0), using this command:

resvg example.svg example.png --width 600 --height 600

This is the output I get:

Image

This is what I originally would have expected resvg-js to render before.

@yisibl
Copy link
Member

yisibl commented Jan 15, 2024

To use the Arial font, you need to add the option: defaultFontFamily: 'Arial'.

resvg-js As of 2.5.0, default_font_family will be empty. This is mainly for Wasm compatibility since system fonts can't be loaded on the Web; fonts can only be loaded on the Web via fontBuffers, and when this option is empty, there are no fonts on the Web.

You can turn on the logLevel: 'debug' option and will see which font is used:

📝 default_font_family = ''
Warning: The default font-family '' not found, set to 'SF Pro'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants