Problems with graphviz font selection

20 Mar 2014

When tweeting about my recent Ansible post I mentioned that graphviz selection problems were the cause of some delay. @magneticnorth responded that it was dismaying given the size of the underlying font handling libraries. So I thought I’d give a more detailed breakdown.

First, let’s give it some context.

The inventory image was generated using graphviz’s dot from a dot source file.

Font selection in dot labels can be done a number of ways, but the resolution of the fonts (i.e. what system fonts to use to render the fonts requested in the dotfile) is where things go wrong. Here’s an example dot file:

digraph dummy { 
a -> b -> c -> d -> e -> f -> g -> h -> i;
a [fontname="Times New Roman, Bold"]
b [fontname="Times New Roman Bold"]
c [fontname="Times, Bold"]
d [fontname="Times Bold"]
e [fontname="Times-Roman, Bold"]
f [fontname="Times-Roman Bold"]
g [fontname="Times-Roman-Bold"]
h [fontname="Times-Bold"]
i [fontname="Times-New-Roman-Bold"]
}

And the results of the dot conversion (on a Fedora 20 machine with fairly standard fonts - it didn’t go better on a different Fedora 20 machine with the MS core fonts installed):

fontname: "Times New Roman, Bold" resolved to: (PangoCairoFcFont) "Liberation Serif, Bold" /usr/share/fonts/liberation/LiberationSerif-Bold.ttf
fontname: "Times New Roman Bold" resolved to: (PangoCairoFcFont) "DejaVu Sans, Bold" /usr/share/fonts/dejavu/DejaVuSans-Bold.ttf
fontname: "Times, Bold" resolved to: (PangoCairoFcFont) "Nimbus Roman No9 L, Medium" /usr/share/fonts/default/Type1/n021004l.pfb
fontname: "Times Bold" resolved to: (PangoCairoFcFont) "Nimbus Roman No9 L, Medium" /usr/share/fonts/default/Type1/n021004l.pfb
fontname: "Times-Roman, Bold" resolved to: (PangoCairoFcFont) "DejaVu Sans, Bold" /usr/share/fonts/dejavu/DejaVuSans-Bold.ttf
fontname: "Times-Roman Bold" resolved to: (PangoCairoFcFont) "DejaVu Sans, Bold" /usr/share/fonts/dejavu/DejaVuSans-Bold.ttf
fontname: "Times-Roman-Bold" resolved to: (PangoCairoFcFont) "DejaVu Sans, Book" /usr/share/fonts/dejavu/DejaVuSans.ttf
fontname: "Times-Bold" resolved to: (ps:pango Nimbus Roman No9 L, ) (PangoCairoFcFont) "Nimbus Roman No9 L, Regular" /usr/share/fonts/default/Type1/n021003l.pfb
fontname: "Times-New-Roman-Bold" resolved to: (PangoCairoFcFont) "DejaVu Sans, Book" /usr/share/fonts/dejavu/DejaVuSans.ttf

There’s very little consistency or reason to why certain fonts resolve to DejaVu Sans and others resolve to non Bold versions. Only “Times New Roman, Bold” resolves to anything reasonable. And that was thanks to a handy hint in a graphviz bug report!

To be clear, I know it’s the underlying libraries that are most likely at fault rather than graphviz, but there is little adequate documentation of how best to specify font selections (lots of FIXME notes in the graphviz font FAQ). The FAQ did at least tell me how to know what fonts were being chosen, even if not why.