dompdf / php-svg-lib

SVG file parsing / rendering library
GNU Lesser General Public License v3.0
1.39k stars 77 forks source link

Improve support for custom fonts #110

Open bsweeney opened 7 months ago

bsweeney commented 7 months ago

Currently, in order to specify a custom font you have to:

  1. specify the path to the font file (using "/" for directory separators) without the file extension
  2. the font-family must be specified without quotes
  3. specify only a single font
  4. the font must be hosted on the same filesystem as the the current working directory

For example, if the DejaVu Sans font file can be found under fonts/DejaVuSans.ttf then the following must be used to select the font:

<svg width="200" height="200"
  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <style>
    .text {
        font-family: fonts/DejaVuSans;
    }
  </style>
  <text class="text" x="100" y="100">php-svg-lib</text>
</svg>

There are a few reasons for this requirement:

  1. This library has no font manager, meaning that font names are not translated to a font file (as required by the PDF backend). So the font-family value must specify the file.
  2. CSS value lists are generally not supported and so fallback values in the font-family are interpreted as part of the font name (e.g. font-family: DejaVu Sans, sans-serif will cause this library to look for a file named "DejaVu Sans, san-serif.ttf").
  3. CSS strings are not supported, so if the font-family value is quoted then the quotes are interpreted as part of the font name.