skanaar / nomnoml

The sassy UML diagram renderer
https://www.nomnoml.com
MIT License
2.66k stars 208 forks source link

Improve SVG measureText #91

Closed ronkok closed 5 years ago

ronkok commented 5 years ago

This PR aims to improve SVG node sizing when possible, by improving the accuracy of the SVG measureText method. The function renderSvg(code, canvas) now has an optional parameter canvas.

The pre-existing measureText heuristic is still in place and it is the default method. measureText will instead use a CanvasRenderingContext2D to measure the text in a particular node only if three conditions are met:

  1. renderSvg is called from a browser, not from node.js.
  2. A canvas element is passed in the second, optional argument to renderSvg().
  3. The node uses one of the following fonts: Helvetica, Arial, Times, or Times New Roman.

Helvetica and Arial are font-metric identical. Ditto Times and Times New Roman. They can be safely measured without worry that a user-agent will not have a matching system font.

ronkok commented 5 years ago

This wraps up my nomnoml work. Thank you for creating a tool that is so useful and easy to use. I hope you can soon place a Download SVG button onto the nomnoml.com page.

I'll stand by for your review.

ronkok commented 5 years ago

Cool. Thank you.