Using OS X fonts in ImageMagick

Posted by on Mar 14, 2008 in Geekery, Graphics, tips | 13 Comments

Introduction

This is a short explanation of how I got my OS X fonts working in ImageMagick. I won’t guarantee that it’s the best way — leave me a comment if you know better — but it’s what I did.

This worked for me on OS X Leopard 10.5.2, with ImageMagick 6.3.3. I used MacPorts to grab one bit of software I needed, but you can get away without it, I reckon.

This explanation is intended for the more Unix-geeky Mac user, but frankly, the kind of person who wants to draw graphics using ImageMagick will naturally lean towards Unix geekhood, won’t they?

The Problem

My problem started when I wanted to use my favourite Mac font, American Typewriter, with ImageMagick. I tried something like this:

convert -background lightblue -fill blue -font AmericanTypewriter \

	-pointsize 72 label:"No luck" output.png

…and ended up with this:

convert: unable to read font `AmericanTypewriter’.

…and this:

output.png

The fundamental problem is that ImageMagick doesn’t understand the OS X font format, dfont. Dfont files are resource files, effectively directories, which wrap up different varieties of font in a single bundle.

You can check what fonts ImageMagick can see with an IM command like:

identify -list Type

On my system the output was a pretty paltry list.

So, helped by a hint from Peter’s Projects, here’s what I did.

The Solution

Installing Fondu

First I found out where my fonts were, by using OS X’s standard Font Book application. If you right-click on a font in Font Book, you can choose “Reveal In Finder” and it’ll show you where the font file is. Unsurprisingly, American Typewriter and the rest of my fonts were in /Libarary/Fonts.

Then I installed fondu. Fondu extracts the fonts from .dfont files, effectively sucking out all the standard font goodness from these “pay no attention to the man behind the curtain” bundles.

I installed my fondu using MacPorts, which was as easy as typing:

sudo port install fondu

If you’re lucky, you’ll already be a MacPorts user, as that may well be how you got ImageMagick in the first place. On the other hand, it looks like there’s a pre-built package containing fondu available from the fondu site on SourceForge, so it shouldn’t be too much trouble either way.

Using Fondu

Then I figured I’d put my fonts in a nice, MacPorts-friendly location, carefully not putting them in the /Library/Fonts folder. As root, I created /usr/local/share/fonts to hold my extracted fonts, then let fondu work its magic:

matt-gibsons-computer:~ root# cd /usr/local/share/fonts/
matt-gibsons-computer:fonts root# fondu /Library/Fonts/*.dfont

I got prompted a couple of times whether I wanted to overwrite some files — I figure that some of the extracted fonts had a name clash — but it didn’t seem to be anything I cared about, so I just overwrote them. At some point I may figure out what I lost, but it was a small percentage of the total fonts, and it hasn’t done me any harm so far.

This left me with a load of shiny new extracted fonts, including Amercian Typewriter:

matt-gibsons-computer:fonts root# ls *American*
AmericanTypewriter.ttf
AmericanTypewriterBold.ttf
AmericanTypewriterCondensed.ttf
AmericanTypewriterCondensedBold.ttf
AmericanTypewriterCondensedLight.ttf
AmericanTypewriterLight.ttf

 

Getting imagick_type_gen

Then I grabbed the imagick_type_gen script from ImageMagick. This generates an ImageMagick font configuration file automagically, by searching your system for fonts that ImageMagick can use.

The locate Database

One problem, though: imagick_type_gen uses the locate database to do its searching. The locate database isn’t live — it’s only updated every now and again (on Leopard it’s updated weekly, for example.)

Rather than waiting a week for the database to be updated so that imagick_type_gen could find the fonts I’d just extracted, I updated the database manually, using the scheduled script that normally kicks it off:

matt-gibsons-computer:~ root# /etc/periodic/weekly/310.locate

That can take a while, as it effectively does a find across your whole disk. I went for a coffee.

Running imagick_type_gen

Okay, so now imagick_type_gen will actually find some fonts. Try running it; it won’t hurt. You don’t have to be root at this point, or at any point from here on, in fact.

matt-gibsons-computer:~ matt$ imagick_type_gen.pl
<?xml version="1.0"?>
<typemap>
Doing TTF fonts
AScore - AScore - AScore
  <type
     format="ttf"
     name="AScore"
     ...

As you can see, this generates an XML file, which is what ImageMagick uses to figure out what fonts you’ve got. ImageMagick has a system (i.e. all-users) version of this file in its config directory — on my installation this is /usr/local/lib/ImageMagick-6.3.3/config/type.xml — but you don’t have to overwrite that one if you don’t want to. Personally I took the coward’s way out and just installed a user version of the file.

The user version of the type.xml file goes in $HOME/.magick/type.xml, so (having shoved imagick_type_gen into /usr/local/bin and made it executable, on the grounds it may come in handy in the future) I created my type.xml file:

matt-gibsons-computer:~ matt$ mkdir ~/.magick
matt-gibsons-computer:~ matt$ imagick_type_gen > ~/.magick/type.xml

 

Testing

Okay, so I’ve got my fonts extracted, and I have a configuration file that ImageMagick should be able to use to find them. The first thing I did was have another go at listing my available fonts, to see if American Typewriter was among them:

matt-gibsons-computer:~ matt$ identify -list Type | grep American
AmericanTypewriter               American Typewriter ...
AmericanTypewriterB              American Typewriter ...
AmericanTypewriterC              American Typewriter ...
AmericanTypewriterCB             American Typewriter ...
AmericanTypewriterCL             American Typewriter ...
AmericanTypewriterL              American Typewriter ...
matt-gibsons-computer:~ matt$

Yay! So, now it was time to have another try at generating my image:

matt-gibsons-computer:~ matt$ convert -background \
	lightblue -fill blue -font "AmericanTypewriter" \
	-pointsize 72 label:'Hurrah!' doyoufeellucky.png
doyoufeellucky.png

And there it was, in all its glory.

Conclusion

As with a lot of things ImageMagick related, I’m left with the feeling that there’s probably an easier way of doing this. But if there is, I couldn’t find instructions for it on the web, which is of course the first place I looked. Hopefully this has helped you out, but if you know of a better way of doing this, like I said, please drop me a line :)

« Google Errors | New Null Blog Entry »

13 Comments

  1. Claus Pfisterer
    September 11, 2008

    Hi Matt

    That’s a great tutorial. I’ve been using ImageMagick since a few years. But nowadays i installed a newer version of IM with the help of MacPort. And I found your tutorial really usefull. I also missed ghostscript.

    Thanks a lot

  2. Alma
    December 30, 2008

    if you do this with graphicsmagick, type.xml should be named type.mgk…
    additionally you will have to export the path to the config-folder in your .bashrc like so: export MAGICK_CONFIGURE_PATH=/Users/myuname/.magick

    cheers

  3. Aral Balkan - Links for 2009-01-16
    January 16, 2009

    […] Using OS X fonts in ImageMagick « Gothick.org.uk Fondu extracts the fonts from .dfont files, effectively sucking out all the standard font goodness from these “pay no attention to the man behind the curtain” bundles. […]

  4. Daniel Brauer
    June 19, 2009

    Great tutorial! The only mistake I encountered was the fondu invocation, which refers to the misspelled “Libarary”

  5. gothick
    June 19, 2009

    @Daniel — Oops! Thanks for pointing that out; I’ve fixed the original now!

  6. mini
    March 4, 2010

    Hi,
    Iam a windows user switching to mac and having a tough time getting to run imagemagick
    I followed your tutorial ..iam not getting my fonts yet. i did sudo for fondu , then tried to run the perlscript..like this.

    perl imagick_type_gen.pl

    and iam getting this error.

    The locate database (/var/db/locate.database) does not exist.
    To create the database, run the following command:

    sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

    i run this and still the fonts are not recognised by imagemagicks.
    Please help

  7. Matt
    March 4, 2010

    Hi mini,

    Running the launchctl script will only set up the rebuild of the locate database to be run as a job once a week. If you want to build the locate database right now, try:

    sudo /etc/periodic/weekly/310.locate

    …hopefully that’ll still work in Snow Leopard, which I guess is the OS you’re using? This article was written for Leopard, so some things may have moved around a little.

    Also, if that command works, it will still take take some time to complete. The “locate” database is basically a cache of all the filenames on your entire hard drive, so it takes time to build…

  8. Henry
    November 10, 2010

    On Snow Leopard, after running:

    sudo launchctl load –w /System/Library/LaunchDaemons/com.apple.locate.plist

    run:

    sudo /usr/libexec/locate.updatedb

    to create the database.

  9. Andrea
    July 27, 2011

    Excellent tutorial! I followed and now when I type ‘convert -list font’ I can see the list of fonts. However, I still get a weird message:

    “delegate library support not built-in `/Library/Fonts/Arial.ttf’ (Freetype).”

    Do any of you have any idea how to fix that?

  10. Vic
    August 13, 2011

    You are my hero.

    To add to what Henry said, I ran into an error when running

    sudo /usr/libexec/locate.updatedb

    while in a sym-linked directory. To get around this, I did

    cd /

    beforehand.

  11. Martin
    October 30, 2012

    This seems like it’s out of date. When I ran identify -list Type, I got:

    Bilevel
    ColorSeparation
    ColorSeparationAlpha
    ColorSeparationMatte
    Grayscale
    GrayscaleAlpha
    GrayscaleMatte
    Optimize
    Palette
    PaletteBilevelAlpha
    PaletteBilevelMatte
    PaletteAlpha
    PaletteMatte
    TrueColorAlpha
    TrueColorMatte
    TrueColor

    which is a list of image types, not a list of fonts. If I run identify -list font, I see over 400 fonts.

    The trick is that American Typewriter has a space in it. It is accessible as -font American-Typewriter

  12. Anthony Durity
    February 26, 2013

    The command for listing where the fonts are located and what the fonts are named is:

    convert -list font

    On my Mac (installed with homebrew) imagemagick seems to do the right thing, nothing extra needed.