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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load all fonts before rendering #660

Merged
merged 1 commit into from
Mar 8, 2024

Conversation

remcohaszing
Copy link
Contributor

馃搼 Summary

Whether or not the used fonts have been loaded, affects the layout of the boxes that Mermaid renders. For consistent results, fonts must be loaded before the diagrams are rendered.

This was taken from mermaid-isomorphic. You may want to leverage that package instead.

Resolves #539

馃搹 Design Decisions

This preloads all fonts in the document.

馃搵 Tasks

Make sure you

  • 馃摉 have read the contribution guidelines
  • 馃捇 have added unit/e2e tests (if appropriate)
  • 馃敄 targeted master branch

Whether or not the used fonts have been loaded, affects the layout of
the boxes that Mermaid renders. For consistent results, fonts must be
loaded before the diagrams are rendered.

Closes mermaid-js#539
@MindaugasLaganeckas
Copy link
Member

Thank you! LGTM :)

Copy link
Member

@aloisklink aloisklink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing! That bug has been bugging me for a while :)

This was taken from mermaid-isomorphic. You may want to leverage that package instead.

Unfortunately, I don't think that would be possible, since that library uses Playwright instead of Puppeteer.

Although, it would be cool if there was some way of making something like a @mermaid-js/mermaid-isomorphic-puppeteer library that shared all of the same code as the mermaid-isomorphic package, with the exception of playwright, but that might get pretty convoluted.

@aloisklink aloisklink merged commit c179bc1 into mermaid-js:master Mar 8, 2024
7 of 8 checks passed
@aloisklink aloisklink added the bug Something isn't working label Mar 8, 2024
@remcohaszing remcohaszing deleted the preload-fonts branch March 8, 2024 05:57
@remcohaszing
Copy link
Contributor Author

Another factor that causes similar issues is the use of a default font that鈥檚 not available everywhere. IIRC it鈥檚 helvetica? To solve that, mermaid-isomorphic defaults to arial,sans-serif.

I don鈥檛 know how much more work it would be to support Puppeteer in mermaid-isomorphic additionally, or if it鈥檚 actually worth it. I don鈥檛 like how often they make semver major releases.

@aloisklink
Copy link
Member

helvetica

Close, it's "trebuchet ms", verdana, arial, sans-serif;. But even Arial is hard to get on Linux, on Debian/Ubuntu, you need to install ttf-mscorefonts-installer .

If I have time, I might make a PR to @mermaid-js to change the official font to something like arial,arimo,"Liberation Sans", since all of those fonts are supposed to have the exact same width/height, so they should look almost identical. And both Arimo/Liberation Sans are available under open-source licenses (they even come with OpenOffice/LibreOffice), so you can use an npm install @fontsource/arimo to add them to your site for every OS!


I don鈥檛 know how much more work it would be to support Puppeteer in mermaid-isomorphic additionally

Probably quite a bit from an infrastructure standpoint, unfortunately :(. There needs to be two different packages with different dependencies/peerDependencies.

I don鈥檛 like how often they make semver major releases.

I know what you mean. I can't wait until https://github.com/servo/servo (or another browser engine) is advanced enough that we no longer need puppeteer/playwright!

@remcohaszing
Copy link
Contributor Author

The test fixtures for mermaid-isomorphic are generated on my Linux laptop and they match pixel perfect with the tests in CI. I don鈥檛 have ttf-mscorefonts-installer installed. The fixtures are only a few millipixels off from testing in Windows or macOS, and it鈥檚 similarly different between those OSes or other browsers. So I鈥檓 quite confident arial is safe to use for this. Depending on a third party font probably less so, as it requires the user to include that font in the place the resulting SVG is viewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using fontawesome (fa) icons clips text or icon from the end
3 participants