php-svg-lib
php-svg-lib copied to clipboard
Setting font family to built-in "Helvetica" does not work
Because of the logic in SurfaceCpdf->setFont(), neither setting font to "Helvetica" or "helvetica" will work. That's because "helvetica" key is not present in the "map" array and the upper-case "Helvetica" will be automatically lower-cased. A simple fix would be to add "helvetica" => "Helvetica" to "map" array.
Hello, can you make a pull request for this please ?
Honestly, I am not sure if the "simple" fix is the correct fix. Is it intended behavior to only support fonts listed in the "$map" array? Shouldn't it be possible for users to be able to select any custom font?
Beyond addressing the reported bug I would note that php-svg-lib by itself has very limited font support out of the box. This limitation applies to the core PDF fonts (Helvetica, Times, etc) as well. The primary issue is that metrics files are not provided for any fonts, which means that as a standalone library there's not much php-svg-lib can do with regard to font selection or placement. Basically, php-svg-lib in stand-alone use without any additional configuration always falls back to the default Cpdf font.
With a little bit of work both core fonts and non-core fonts can be used. The first thing to do is acquire font metrics for the core PDF fonts, and font files and metrics for any non-core fonts. You can get a starter set from the Dompdf project: https://github.com/dompdf/dompdf/tree/master/lib/fonts.
Some guidelines on how to actually use fonts:
- To utilize the generic sans/sans-serif/monospace family names you have to put the core font files in the same directory as the executing PHP script.
- When referencing an actual font in your SVG you have to specify the path to the font file relative to the executing PHP script, and without the file extension. Do not use the "friendly" font name. So, for example, if you have the files for the DejaVu Sans font DejaVuSans.ttf, specify the font as (for example)
fonts/DejaVuSans
instead ofDejaVu Sans
. - Also note that CSS parsing is somewhat limited. You should not include quotes around the font and no fallbacks should be specified.
For example, let's say I have the following structure:
project | - php-svg-lib | - fonts where the project directory contains my executing PHP script and the font metrics for the core fonts, and the fonts directory contains all my font files. With this structure the following SVG document will render with the appropriate font:
<svg viewBox="0 0 240 80" xmlns="http://www.w3.org/2000/svg">
<style>
.small { font-style: italic; font-size: 13; font-family: fonts/DejaVuSans; }
.heavy { font-style: bold; font-size: 30; font-family: sans-serif; }
.Rrrrr { font-style: italic; font-size: 40; font-family: Times-Roman; fill: red; }
</style>
<text x="20" y="35" class="small">My</text>
<text x="40" y="35" class="heavy">cat</text>
<text x="55" y="55" class="small">is</text>
<text x="65" y="55" class="Rrrrr">Grumpy!</text>
</svg>
You'll want to clear your font cache once you've correctly configured your installation. Otherwise any existing cached metrics containing incorrect information will cause incorrect font rendering.
Note: this discussion is all related to using php-svg-lib with Cpdf. Font handling functionality hasn't yet been built out for the PDFLib surface implementation.