7

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?

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • Does the original SVG render correctly on your machine? Ie. with the correct fonts? – Paul LeBeau Aug 28 '16 at 22:02
  • ImageMagick is a jack-of-all-trades converter. It would not be unexpected if it has issues with one particular format. Have you tried one that specialises in SVGs? For example Apache Batik? https://xmlgraphics.apache.org/batik/tools/rasterizer.html – Paul LeBeau Aug 28 '16 at 22:06
  • The SVG is correct. Though I do appreciate suggestions for other tools, I should note that I am looking for a solution to the problem as specified, i.e. what is specifically required to make ImageMagick work correctly with installed typefaces. (Discussion of other tools would probably be best outside this thread.) – Alex Reynolds Aug 28 '16 at 22:43
  • Hm, isn't there separate ways in css to specify narrow/bold? Like `font-family="Helvetica" font-weight="bold" `? But not sure about the Narrow style there, but maybe one of your calling variants will give correct output when you only modify the line with the bold text? – Stefan Hegny Aug 29 '16 at 09:13
  • Please try running `identify -list font | grep -i helvetica` and pasting the results. – Mark Setchell Aug 30 '16 at 20:55

3 Answers3

4

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.

pranavcode
  • 747
  • 4
  • 12
0

Try editing the ImageMagick fonts file located somewhere like ~/.magick/type.xml

You can force it to use a specific font and recognize it by any name you want. imagick_type_gen.

Here is a link to something that should help what you are trying to do: Make ImageMagick recognize a font

Further reading with additional tool to use with ImageMagick: http://gothick.org.uk/2008/03/14/using-os-x-fonts-in-imagemagick/

Community
  • 1
  • 1
Faisal M
  • 173
  • 2
  • 6
0

You need to clarify where the font's are installed: in the SVG installation, the OS, or embedded in the PDF.

SVG has its own font system, which causes no end of problems. SVG was first made before web fonts were really in use, so SVG made its own font system that was portable. Now you have a font system for SVG fonts only, and they probably aren't converting over to the PDF file.

Your best best bet is to use the @font-face rule, e.g., @font-face { font-family: Helvetica; src: url('/var/www/fonts/Helvetica.ttf'); }. See this (https://graphicdesign.stackexchange.com/questions/5162/how-do-i-embed-google-web-fonts-into-an-svg/5167#5167) for another view.

The alternate is to give up on the fonts completely and have SVG render the path you want. That is, create a PDF with a graphic that is the rendered font. This shows up when using Inkscape to make the other direction of trip, from PDF to SVG. (see Convert PDF to clean SVG?, or https://www.magebay.com/forum/forums/topic/pdf-svg-doesnt-embed-the-fonts/).

Sorry it doesn't boil down to 'add this one line' without knowing a lot more about the particulars, but this should help.

Community
  • 1
  • 1
Charles Merriam
  • 19,908
  • 6
  • 73
  • 83