Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting SVG to PDF with ImageMagick with reliable font selection?

I have a simple test SVG that uses two installed typefaces (Helvetica-Narrow and Helvetica-Bold):

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="50">
  <text x="0" y="24" fill="blue" font-family="Helvetica-Bold" font-size="24px">Bold</text>
  <text x="0" y="48" fill="blue" font-family="Helvetica-Narrow" font-size="24px">Narrow</text>
</svg>

If I convert this to a PDF file using ImageMagick (ImageMagick 7.0.2-0 Q16 x86_64 running on CentOS Linux 7 (Core)), then the result does not use the installed fonts.

For example:

$ convert -density 600 test.svg test.pdf

Yields:

default font applied

It looks like ImageMagick defaults to using the normal weight of Helvetica, which does not match either of the font families specified in the input SVG.

Next, I try specifying the path to one of the typefaces specified in the input SVG. This is a path to the Helvetica-Bold typeface, as determined from running convert -list font.

$ convert -density 600 -font /net/module/sw/ghostscript-fonts/5.50-32/n019004l.pfb test-helvetica-mix.svg test-helvetica-mix-bold.pdf

bold font specified

The first <text> element is correct — it uses Helvetica-Bold. The second <text> element is incorrect - it also uses Helvetica-Bold, but should really be using Helvetica-Narrow.

Still, I'm getting closer with this approach, so I try adding the path to the second typeface used in the input SVG:

$ convert -density 600 -font /net/module/sw/ghostscript-fonts/5.50-32/n019004l.pfb -font /net/module/sw/ghostscript-fonts/5.50-32/n019043l.pfb test-helvetica-mix.svg test-helvetica-mix-both.pdf

both font paths added

ImageMagick uses the Helvetica-Narrow typeface for both elements, which is incorrect for the same reason.

Is there a way to convince ImageMagick to use the correct typefaces specified in the <text> elements in the input SVG?

like image 242
Alex Reynolds Avatar asked Aug 25 '16 20:08

Alex Reynolds


1 Answers

Looking at what convert -list font says

Font: Helvetica-Bold
  family: Helvetica
  style: Normal
  stretch: Normal
  weight: 700
  glyphs: /usr/share/fonts/type1/gsfonts/n019004l.pfb
Font: Helvetica-Narrow
  family: Helvetica Narrow
  style: Normal
  stretch: Condensed
  weight: 400
  glyphs: /usr/share/fonts/type1/gsfonts/n019043l.pfb

Font family for Helvetica-Bold and Helvetica-Narrow is Helvetica and Helvetica Narrow respectively. You can achieve the intended effect (boldness/narrowness) by using additional attributes on <text> like:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="50">
  <text x="0" y="24" fill="blue" font-family="Helvetica" font-size="24px" font-weight="700">Bold</text>
  <text x="0" y="48" fill="blue" font-family="Helvetica Narrow" font-size="24px" font-stretch="Condensed">Narrow</text>
</svg>

I researched to see if we can directly use the type name (Helvetica-Bold, Helvetica-Narrow), but to no avail, this is the only solution I could find, which is respected by ImageMagick's convert tool. Hope it helps.

like image 132
pranavcode Avatar answered Sep 30 '22 10:09

pranavcode