php-svg icon indicating copy to clipboard operation
php-svg copied to clipboard

How to set fonts for rasterization on existing documents?

Open JukkaHau opened this issue 3 years ago • 7 comments

when rasterizing svg, all the texts are missing in the png file. is it implemented at all to rasterize texts ?

sorry, was just missing a font. You can delete this issue. works with this, ->setFont(new SVGFont('arial', './arial.ttf'))

JukkaHau avatar Aug 16 '20 10:08 JukkaHau

Dear Developers and Users, I have the similar problem and I couldn't figure how to use setFont() from outside.

How can I inject my font name (and location on server) into $params['font_path']?

My texts have font-family attribute in original SVG $svg source:

$image = \SVG\SVG::fromString($svg);
$doc = $image->getDocument();
$rasterImage = $image->toRasterImage($doc->getWidth(), $doc->getHeight());
ob_start();
imagepng($rasterImage);
$string =  ob_get_clean(); // raw image string for base64 encoding

Do you have any suggestions? Thank you for reading!

r3sist avatar Aug 26 '20 13:08 r3sist

@r3sist Did you find a solution to this? Is it at all possible to use custom fonts with this library?

notflip avatar Nov 12 '20 06:11 notflip

@notflip No, sorry. I switched to Imagick with inkscape engine for rasterization process. Inkscape is important for SVG font rendering. It also handles rotated texts.

$image = new Imagick();
$image->setFont(__DIR__ . '/Sans-Serif.ttf');
$image->readImageBlob($svgString);
$image->setImageFormat('png');
$image->getImageBlob()

r3sist avatar Nov 12 '20 07:11 r3sist

I was using Imagick as well for rendering the image! Did not know about the setFont() method, with this, do you now have a working png generator with fonts? Did you have to any special settings to Imagick? Thanks!

notflip avatar Nov 12 '20 07:11 notflip

Yes I have a working project. Comparison example - SVG left

As far as I know, Imagick will use automatically Inkscape if it's installed.

In my project notes I found (for Ubuntua 20.04):

Imagick readImageBlob() needs:

apt-get install ghostscript

apt-get install libgraphicsmagick1-dev libmagickcore-dev libmagickcore-6.q16-2 libmagickcore-6.q16-2-extr

Replace built-in SVG processor with inkscape:

apt-get install inkscape

r3sist avatar Nov 12 '20 07:11 r3sist

Thanks! This is of great help, the only thing I don't get is if you define the font with setFont in Imagick, do you still need to set the font using this library's methods for doing that, for example

$text = (new SVGText('Hello World', '50%', '50%'))
    ->setFont(new SVGFont('Montserrat-ExtraBoldItalic', $fontPath));

notflip avatar Nov 12 '20 07:11 notflip

I think you should test it. I have font attrinbutes in raw SVG, so I guess yes. However I use only generic CSS font types like Sans-Serif (font-family="Sans-Serif" in SVG source) and I have a .ttf file called like this (see above comment). (Disclamier: I don't always use this lib for generating SVG)

r3sist avatar Nov 12 '20 08:11 r3sist

I've implemented a new system for registering fonts statically on the SVG class. The font to use for rendering is then automatically chosen as the best match. See PR #191 and please let me know what you think!

meyfa avatar Dec 28 '22 18:12 meyfa