RSVG Custom Fonts

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
daneren2005
Posts: 3
Joined: 2015-07-20T17:30:54-07:00
Authentication code: 1151

RSVG Custom Fonts

Post by daneren2005 »

In several places about bad ImageMagick SVG output, it was recommended to use RSVG instead. I have compiled ImageMagick and Imagick with RSVG successfully, and it does indeed make everything look much better. My problem is I had a little hack to get Google fonts to work with Imagick that now does not appear to be working. I was:

1) downloading the font from google as a tff, saving it to a file under /tmp.
2) Saving a custom types.xml file like:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE typemap [
            <!ELEMENT typemap (type)+>
            <!ELEMENT type (#PCDATA)>
            <!ELEMENT include (#PCDATA)>
            <!ATTLIST type name CDATA #REQUIRED>
            <!ATTLIST type fullname CDATA #IMPLIED>
            <!ATTLIST type family CDATA #IMPLIED>
            <!ATTLIST type foundry CDATA #IMPLIED>
            <!ATTLIST type weight CDATA #IMPLIED>
            <!ATTLIST type style CDATA #IMPLIED>
            <!ATTLIST type stretch CDATA #IMPLIED>
            <!ATTLIST type format CDATA #IMPLIED>
            <!ATTLIST type metrics CDATA #IMPLIED>
            <!ATTLIST type glyphs CDATA #REQUIRED>
            <!ATTLIST type version CDATA #IMPLIED>
            <!ATTLIST include file CDATA #REQUIRED>
            ]>
            <typemap>
                <type
                 format="ttf"
                 name="crafty girls"
                 fullname="crafty girls"
                 family="crafty girls"
                 glyphs="/tmp/crafty girls.ttf"
                 style="normal"
                 stretch="normal"
                 weight="400"/>

            </typemap>
3) Updating system cache:

Code: Select all

exec('fc-cache -v ' . $this->fontDir);
4) Updating Imagick font dir:

Code: Select all

putenv('MAGICK_FONT_PATH=' . $this->fontDir);
This worked great when using Imagick when the renderer was the internal one (XML 2.9 I believe). But now after switching to RSVG it no longer works and it is reverting back to the standard ones. I needed it to be something dynamic like this because I can't edit the system fonts on Heroku. I can't seem to find any decent documentation about RSVG, or anyone else having asked this question before. Do you know how to update RSVG to use custom downloaded fonts similar to this?

Code: Select all

$convert --version
Version: ImageMagick 6.8.9-10 Q16 x86_64 2015-07-20 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr lzma openexr pangocairo png rsvg tiff x xml zlib

$convert -list format | grep SVG
     MSVG  rw+   ImageMagick's own SVG internal renderer
      SVG  rw+   Scalable Vector Graphics (RSVG 2.40.2)
     SVGZ  rw+   Compressed Scalable Vector Graphics (RSVG 2.40.2)
daneren2005
Posts: 3
Joined: 2015-07-20T17:30:54-07:00
Authentication code: 1151

Re: RSVG Custom Fonts

Post by daneren2005 »

I also attempted to embed the fonts directly into the SVG file, but that also does not work:

Code: Select all

$fontStyles = '';
foreach($this->fonts as $font) {
	$fontStyles .= '@font-face {
		font-family: ' . $font['name'] . ';
		src: url("data:application/x-font-ttf;charset=utf-8;base64,' . base64_encode(file_get_contents($font['file'])) . '") format("truetype");
		font-weight: 400;
		font-style: normal;
	}';
}

$style = '<style>' . $fontStyles . '</style>';
$startDefsStr = '<defs>';
$startDefs = strpos($this->svg, $startDefsStr) + strlen($startDefsStr);

$this->svg = substr($this->svg, 0, $startDefs) . $style . substr($this->svg, $startDefs);
This generated a valid SVG file with a working font for the browser, but still no luck with Imagick.
daneren2005
Posts: 3
Joined: 2015-07-20T17:30:54-07:00
Authentication code: 1151

Re: RSVG Custom Fonts

Post by daneren2005 »

For anyone else who comes across this, I ended up figuring out that calling rsvg directly would load fonts from the local home directory. Example: /var/www/.fonts/truetype/google/anton.tff would be automatically loaded. Then you can run exec("rsvg-convert $svgFile > $pngFile");. You have to manually download the fonts from Google, but at least it works. On Heroku I was able to just add rsvg-convert to the application and call that version. It did not need any other libraries to be loaded.
kwik256
Posts: 1
Joined: 2015-10-27T09:21:42-07:00
Authentication code: 1151

Re: RSVG Custom Fonts

Post by kwik256 »

Hello,

I am having the same struggle to which this topic refers.

I saw your last post, the one saying you got RSVG to convert your SVG to a PNG with the proper font, but I do not understand how.

So, I will describe the actions I am taking, and maybe you have ideas about what I might be doing incorrectly.

I am already calling rsvg-convert directly.

I tried including the @font-face style description for my font, and tried a relative and absolute path to the font.

I ran convert -list font and see that ImageMagick has a reference to my font.

The font is located in the proper system folder.

Thoughts?

Thanks for any help.
Post Reply