Add fonts to your OpenStreetMap server

rweait's picture

Road sign with a custom font

OpenStreetMap offers many choices and great power. We know that with great power, there must also come great responsibility.[1] And so it is with fonts. Your choice is likely to be a font from your corporate brand guide or a font mandated by regulation. It would be irresponsible to use a decorative font like Olde English on our maps so for this demo we'll add a suitable font to the system and show how to bring it in to play on the map.

This article is one of a series that involve customizing OpenStreetMap. Please use comments below to request additional OpenStreetMap topics or to offer feedback.

Where are we?

Which fonts come with OpenStreetMap? That is determined by the fonts installed in Mapnik when we compile it. Mapnik does the rendering, so if Mapnik doesn't know about a font, neither does OpenStreetMap.

Find out which fonts are installed in Mapnik. [you type the text marked like this the rest is prompting and responses from Python.]
python
>>> from mapnik import *
>>> for face in FontEngine.face_names(): print face
... [Enter]
DejaVu Sans Bold
DejaVu Sans Bold Oblique
DejaVu Sans Book
DejaVu Sans Condensed
DejaVu Sans Condensed Bold
DejaVu Sans Condensed Bold Oblique
DejaVu Sans Condensed Oblique
DejaVu Sans ExtraLight
DejaVu Sans Mono Bold
DejaVu Sans Mono Bold Oblique
DejaVu Sans Mono Book
DejaVu Sans Mono Oblique
DejaVu Sans Oblique
DejaVu Serif Bold
DejaVu Serif Bold Italic
DejaVu Serif Book
DejaVu Serif Condensed
DejaVu Serif Condensed Bold
DejaVu Serif Condensed Bold Italic
DejaVu Serif Italic
>>> Ctrl-d

So twenty DejaVu faces are installed. I'll guess that your corporate brand guide doesn't include DejaVu in the list of acceptable fonts.

For this demonstration let's add a font with a great tradition in maps and a noble history in company newsletters, Comic Sans.

I presume that you already have the truetype files somewhere and that you have moved them to your home directory on the map server. From there the fonts must be placed in the directory structure so that Mapnik can find them.
cd ~
mkdir -p src/mapnik/fonts/comic/ttf
mv comic.ttf comicbd.ttf src/mapnik/fonts/comic/ttf/

This will put the .ttf files in the correct directory for installation.

Now we rebuild Mapnik so that it registers the fonts.

Configure mapnik
python scons/scons.py PYTHON=/usr/bin/python PGSQL_INCLUDES=/usr/include/postgresql PGSQL_LIBS=/usr/lib/postgresql BOOST_LIBS=/usr/lib

Configure will respond with many lines ending with

scons: Building targets ...
scons: `.' is up to date.
scons: done building targets.

Install mapnik
sudo python scons/scons.py install PYTHON=/usr/bin/python PGSQL_INCLUDES=/usr/include/postgresql PGSQL_LIBS=/usr/lib/postgresql BOOST_LIBS=/usr/lib

Install will respond with many lines ending with these

scons: Building targets ...
Install file: "fonts/comic-fonts-ttf/ttf/comicbd.ttf" as "/usr/local/lib/mapnik/fonts/comicbd.ttf"
Install file: "fonts/comic-fonts-ttf/ttf/comic.ttf" as "/usr/local/lib/mapnik/fonts/comic.ttf"
scons: done building targets.

Which gives us the good news that Comic Sans and Comic Sans Bold were installed. Yay!

Let's confirm that Mapnik has registered the new fonts as we did above.
python
>>> from mapnik import *
>>> for face in FontEngine.face_names(): print face
... [Enter]
Comic Sans MS Bold
Comic Sans MS Regular
DejaVu Sans Bold
DejaVu Sans Bold Oblique
DejaVu Sans Book
DejaVu Sans Condensed
DejaVu Sans Condensed Bold
DejaVu Sans Condensed Bold Oblique
DejaVu Sans Condensed Oblique
DejaVu Sans ExtraLight
DejaVu Sans Mono Bold
DejaVu Sans Mono Bold Oblique
DejaVu Sans Mono Book
DejaVu Sans Mono Oblique
DejaVu Sans Oblique
DejaVu Serif Bold
DejaVu Serif Bold Italic
DejaVu Serif Book
DejaVu Serif Condensed
DejaVu Serif Condensed Bold
DejaVu Serif Condensed Bold Italic
DejaVu Serif Italic
>>> Ctrl-d

And we see that the first two face names listed are our new friends. Make a note of the name of your new fonts as reported here. You'll need to invoke them with this name in the osm-template.xml file. ~/mapnik/osm-template.xml manages the style of the maps you render. So it is here that we will change the font.

For this demonstration we'll take the very ordinary Highway 401and cheer it up a bit by changing the highway shield text to Comic Sans Bold. Edit ~/mapnik/osm-template.xml and find the rule that includes motorway_shield3.png Only one line needs to be changed.
Here is the original line
<ShieldSymbolizer name="ref" face_name="DejaVu Sans Bold" size="11" fill="#809bc0" placement="line" file= "%SYMBOLS_DIR%/motorway_shield3.png" type="png" width="31" height="17" min_distance="120"/>

And here it is with our changes marked.
<ShieldSymbolizer name="ref" face_name="Comic Sans MS Bold" size="13" fill="#809bc0" placement="line" file= "%SYMBOLS_DIR%/motorway_shield3.png" type="png" width="31" height="17" min_distance="120"/>
Again, remember to use the face name as reported by python.

Save the changes then re-render as usual.

Let's compare the before and after, shall we? First the original
shield3 with default DejaVu Sans Bold text
and the customized render.
shield3 with wonderful Comic Sans Bold text
And everyone will agree that Comic Sans will make your commute along this section of the 401 just a little bit more joyful.

And that is the key to adding custom fonts for your OpenStreetMap tile server.

References and Credits

Thanks to the helpful folks on #osm and #mapnik and especially springmeyer.

Map images and data © 2009 CCBYSA OpenStreetMap and contributors.

Resources

This article from the archives was originally published on Fri, 03/20/2009 - 01:23

[1] Uncle Ben, or Tom H. One or the other said it.

Comments

highway sign and other icons

Thanks for the nice tutorial!
It might be remarkable that it is also very easy to exchange the symbols for the highway (the blue rectangle), since in this example it's almost too tight.
Those are png's for each length: 1-digit, 2-digit, 3-digit etc. It is also possible to use svg graphics which scale nicely.
I'm too lazy to explain this here in detail, but whoever managed this tutorial of fonts should easily find the icons within the mapnik style tree and exchange them.

dejavu?

Richard, nice tutorial, but haven't you already posted this same text time ago? Feels like having a déjàvu ;-)

At the end of the article see

At the end of the article see:
"This article from the archives was originally published on Fri, 03/20/2009 - 01:23"

This is similar for lots of these articles which are getting re added.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.