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

svgTextMeasure doesn't consider font-family #300

Closed
jonahdlin opened this issue Jul 23, 2024 · 2 comments
Closed

svgTextMeasure doesn't consider font-family #300

jonahdlin opened this issue Jul 23, 2024 · 2 comments

Comments

@jonahdlin
Copy link

jonahdlin commented Jul 23, 2024

Describe the bug

svgTextMeasure does not consider font-family when measuring text width. Instead it uses a fallback.

svg2pdf.js version: 2.2.4
JSPDF version: 2.5.1

To Reproduce
To reproduce visually with an SVG:

  1. Create a jsPDF doc const doc = new jsPDF()
  2. Add a font to the doc. For mine I used Noto Serif from fontsource CDN. It's likely not relevant since I did verify the font works, but just in case my process for adding fonts is
    a. fetch font using JS fetch
    b. await response.arrayBuffer(), then convert array buffer into binary string
    c. doc.addFileToVFS(${name}-${style}.ttf, btoa(binaryString));
    d. doc.addFont(${name}-${style}.ttf, name, style);
    e. Verify the font does work with a simple SVG to PDF case (<svg><text font-family="Noto Serif">Test</text></svg>
  3. Observe that an SVG like the following doesn't work properly.
<svg>
   <text x="100px" y="100px">
      <tspan font-family="Noto Serif">Testing</tspan>
      <tspan font-family="Noto Serif">many</tspan>
      <tspan font-family="Noto Serif">tspans</tspan>
   </text>
</svg>

The text overlaps incorrectly.
image

You can also reproduce this by just manually overriding the fontFamily used in svgTextMeasure to something nonsensical and observe that when the canvas vs. svg text measuring method decision happens with the test string, the svg text element's bbox().width value is always 760.640625, regardless of whether you use your custom font or not.

With the canvas measuring method the issue is still present, but less so:
image

Expected behavior
The PDF should display the tspans at the correct distance.

Screenshots
Screenshots above

Desktop (please complete the following information):

  • OS: Windows
  • Browser: Chrome
  • Version: 126.0.6478.127
@jonahdlin
Copy link
Author

Found the issue, this isn't a problem with svg2pdf.js. I had an index.css leftover from create-react-app which applied global styles

* {
  font-family:-apple-system, "BlinkMacSystemFont", "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Open Sans", "Helvetica Neue", "blueprint-icons-16", sans-serif;
}

Nothing had fallen back on it till now, so I never noticed it.

@yGuy
Copy link
Member

yGuy commented Jul 24, 2024

Great, thanks!

Well, one could argue that the result should still be consistent, even if the CSS rules make no particular sense. Ideally svg2pdf should have resolved the style rule, applied the font and then rendered the spans properly. However it is hard to automatically resolve styles like this to the effective font being used, so I guess the problem here is that we are either not properly resolving the font or embedding the wrong one in the end. Or is it just a missing font (for the export)? Ideally there should be some kind of warning, then.

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