annotate toolboxes/graph_visualisation/share/graphviz/doc/fontfaq.txt @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 Graphviz and fonts.
Daniel@0 2 ===================
Daniel@0 3
Daniel@0 4 Before we launch into the gory details, we would like to explain
Daniel@0 5 why this is a hard problem. The naming and rendering of text fonts
Daniel@0 6 in Graphviz (and other programs) is complicated. There are several reasons:
Daniel@0 7
Daniel@0 8 - Graphviz runs on a wide range of systems: Linux and other Unix
Daniel@0 9 variants, Microsoft Windows, and Mac.
Daniel@0 10 - Graphviz has a wide range of output formats: raster-oriented formats
Daniel@0 11 like PNG and GIF; path-based ones like Postscript, PDF and SVG; some
Daniel@0 12 idiosyncractic legacy formats, like troff PIC and HPGL.
Daniel@0 13 - Often, output will be downloaded and displayed on a computer or other
Daniel@0 14 device, different than the one where the layout was created.
Daniel@0 15 - Graphviz layouts should be identical in size and appearance,
Daniel@0 16 regardless of the output format.
Daniel@0 17 - Graphviz can run on external libraries that help with naming and
Daniel@0 18 rendering text fonts, but they are not required, and stripped-down
Daniel@0 19 Graphviz tools can be built without them. In fact, Graphviz may have
Daniel@0 20 to run on systems with no font files installed.
Daniel@0 21 - There are several major font file formats to be supported.
Daniel@0 22 - Non-Western, international character sets should be supported.
Daniel@0 23 - Graphviz should provide a good set of standard fonts.
Daniel@0 24 - It should be easy to specify standard fonts.
Daniel@0 25 - Users should be able to load their own custom fonts.
Daniel@0 26 - Output should be small to download quickly.
Daniel@0 27 - Output should allow the best rendering possible in a given format.
Daniel@0 28 - Output files should be easy to postprocess, for example, retaining
Daniel@0 29 the objects of the original graph if possible.
Daniel@0 30 - It is very helpful to work around known bugs or missing features
Daniel@0 31 in support libraries and popular external tools.
Daniel@0 32
Daniel@0 33 This is a tall order. Some of the goals conflict. Generally our
Daniel@0 34 approach has been to define defaults that favor convenience and good
Daniel@0 35 looking output, and give the user options to override the defaults.
Daniel@0 36
Daniel@0 37 ===Overview===
Daniel@0 38
Daniel@0 39 In the following, we will assume a ''standard'' version of Graphviz
Daniel@0 40 with the full set of support libraries (fontconfig, gd, Cairo and Pango),
Daniel@0 41 running on a desktop system or server with a standard installation of
Daniel@0 42 font files.
Daniel@0 43
Daniel@0 44 The graphviz layout engines (dot, neato, etc) create layouts with nodes
Daniel@0 45 sized to enclose the text labels. This requires knowing the size of
Daniel@0 46 the text blocks, which in turn requires knowing the metrics of the font
Daniel@0 47 glyphs and their composition into words, taking into account wordspacing,
Daniel@0 48 kerning, hinting, etc. So the overall process is: font specification,
Daniel@0 49 then text layout, followed by Graphviz output (and final rendering on
Daniel@0 50 the target display or device, which may or may not be by a Graphviz tool.)
Daniel@0 51
Daniel@0 52
Daniel@0 53 A font is usually selected by family name ("fontname") and other properties
Daniel@0 54 (see below: "Font selection"). Then fontconfig matches the request
Daniel@0 55 to a system font. [Note: in older versions of Graphviz, fontname was
Daniel@0 56 simply a file name. This required exact file name matching (with a little
Daniel@0 57 bit of helpful name mangling under the hood, e.g. translating Times-Roman
Daniel@0 58 to Times, or Helvetica to Arial on Windows systems (and yes we know
Daniel@0 59 there is a difference). Under fontconfig, fontnames are family names,
Daniel@0 60 which fontconfig matches to the closest font it finds. This always
Daniel@0 61 "succeeds", but unfortunately produces surprising results if fontconfig's
Daniel@0 62 idea of "close" doesn't match yours. This can happen when you specify
Daniel@0 63 a custom (or just nonexistent) font, like Steve-North-Handwriting,
Daniel@0 64 and fontconfig silently falls back to something safe like a typewriter
Daniel@0 65 font.]
Daniel@0 66
Daniel@0 67 Text layout is performed by pango, which accepts text and computes a
Daniel@0 68 layout with metrics that determine node sizes.
Daniel@0 69
Daniel@0 70 Though line drawing is provided by cairo for many output formats (and
Daniel@0 71 likely more in the future), for raster output formats, font rendering
Daniel@0 72 is passed though cairo to freetype. Freetype is also called if gd is
Daniel@0 73 used for drawing. (gd can also be requested explicitly, e.g. dot -Tpng:gd,
Daniel@0 74 or by default when Graphviz is built without cairo). Freetype provides
Daniel@0 75 antialiasing, hinting, kerning, and other low-level font features.
Daniel@0 76
Daniel@0 77 Font metrics are obtained from the fonts installed on the system running
Daniel@0 78 Graphviz. Results are guaranteed when Graphviz outputs raster formats,
Daniel@0 79 because freetype immediately renders the fonts into pixels. On the
Daniel@0 80 other hand, with path-based formats like Postscript (-Tps) and SVG (-Tsvg),
Daniel@0 81 final rendering may be done on a different platform altogether, with
Daniel@0 82 different font files installed. Clearly, Your Milage May Vary. In the
Daniel@0 83 case of Postscript, the driver in Graphviz passes the expected metrics
Daniel@0 84 of the text block down to the renderer, and asks it to make a final stretch
Daniel@0 85 (or squeeze) to force the text to fit the metrics that were in effect at
Daniel@0 86 layout time. In Graphviz SVG, there is only a hope and a prayer that
Daniel@0 87 the SVG rendering program's fonts match the ones fontconfig and freetype
Daniel@0 88 used when Graphviz was run. (More about this later.)
Daniel@0 89
Daniel@0 90 Default fonts and PostScript fonts. ===================================
Daniel@0 91
Daniel@0 92 The default font in graphviz is, and always has been, Times-Roman.
Daniel@0 93
Daniel@0 94 Graphviz has historically supported some ``standard'' Postscript
Daniel@0 95 fonts, initially, Times-Roman, Helvetica, Courier and Symbol.
Daniel@0 96 This list was later enlarged by Adobe to include 35 fonts, which are:
Daniel@0 97 AvantGarde-Book AvantGarde-BookOblique AvantGarde-Demi
Daniel@0 98 AvantGarde-DemiOblique Bookman-Demi Bookman-DemiItalic
Daniel@0 99 Bookman-Light Bookman-LightItalic Courier Courier-Bold
Daniel@0 100 Courier-BoldOblique Courier-Oblique Helvetica
Daniel@0 101 Helvetica-Bold Helvetica-BoldOblique Helvetica-Narrow
Daniel@0 102 Helvetica-Narrow-Bold Helvetica-Narrow-BoldOblique
Daniel@0 103 Helvetica-Narrow-Oblique Helvetica-Oblique NewCenturySchlbk-Bold
Daniel@0 104 NewCenturySchlbk-BoldItalic NewCenturySchlbk-Italic
Daniel@0 105 NewCenturySchlbk-Roman Palatino-Bold Palatino-BoldItalic
Daniel@0 106 Palatino-Italic Palatino-Roman Symbol Times-Bold Times-BoldItalic
Daniel@0 107 Times-Italic Times-Roman ZapfChancery-MediumItalic ZapfDingbats
Daniel@0 108
Daniel@0 109 Unfortunately, fontconfig doesn't recognize PostScript-style font
Daniel@0 110 names directly, so Graphviz makes custom mappings from its list of
Daniel@0 111 PostScipt names into fontconfig family names for use in all cairo
Daniel@0 112 and gd based renderers. In -Tps output, these fonts are used without
Daniel@0 113 name translation.
Daniel@0 114
Daniel@0 115 Font selection. ===============
Daniel@0 116
Daniel@0 117 The fontname attribute in .dot graphs is a fontconfig style specification.
Daniel@0 118 From: http://www.fontconfig.org/fontconfig-user.html
Daniel@0 119
Daniel@0 120 Fontconfig provides a textual representation for patterns that
Daniel@0 121 the library can both accept and generate. The representation is
Daniel@0 122 in three parts, first a family name list, second list of point sizes,
Daniel@0 123 and finally a list of additional properties:
Daniel@0 124
Daniel@0 125 <families>-<point sizes>:<name1>=<values1>:<name2>=<values2>...
Daniel@0 126
Daniel@0 127 Values in a list are separated with commas. The name needn't
Daniel@0 128 include either a family or point size; they can be elided. In
Daniel@0 129 addition, there are symbolic constants that simultaneously
Daniel@0 130 indicate both a name and a value. Here are some examples:
Daniel@0 131
Daniel@0 132 Name Meaning
Daniel@0 133 ----------------------------------------------------------
Daniel@0 134 Times-12 12 point Times Roman
Daniel@0 135 Times-12:bold 12 point Times Bold
Daniel@0 136 Courier:italic Courier Italic in the default size
Daniel@0 137 Monospace:matrix=1 .1 0 The users preferred monospace font
Daniel@0 138 with artificial obliquing
Daniel@0 139
Daniel@0 140 Graphviz currently has a seperate attribute for specififying fontsize.
Daniel@0 141
Daniel@0 142 [ FIXME
Daniel@0 143 We should allow the fontconfig style specification. "Times-20" does
Daniel@0 144 not currently result in a 20pt font.
Daniel@0 145
Daniel@0 146 This is probably because of special treatment of '-' for postscript
Daniel@0 147 font names.
Daniel@0 148 ]
Daniel@0 149
Daniel@0 150 [ FIXME
Daniel@0 151 We seem to have a bug with use of ':' in fontnames, probably because
Daniel@0 152 of special treatment for filenames in Windows.
Daniel@0 153
Daniel@0 154 In fontnames, use <space> instead of ':' to separate values.
Daniel@0 155
Daniel@0 156 -Nfontname="Courier:italic" doesn't produce an italic font in
Daniel@0 157 graphviz-2.16.1, but: -Nfontname="Courier italic" works, but
Daniel@0 158 -Nfontname="Monospace matrix=1 .1 0 1" doesn't.
Daniel@0 159 ]
Daniel@0 160
Daniel@0 161
Daniel@0 162 Font management with fontconfig. ================================
Daniel@0 163
Daniel@0 164 How can I tell what fonts are available?
Daniel@0 165 $ fc-list
Daniel@0 166
Daniel@0 167 How can I tell what fonts dot is using;
Daniel@0 168 $ dot foo.dot -Tpng -o foo.png -v 2>&1 | grep font
Daniel@0 169
Daniel@0 170 How can I add a custom font?
Daniel@0 171 In the current version of Graphviz with fontconfig, Cairo and
Daniel@0 172 Pango, this cannot be done by simply putting a file in the
Daniel@0 173 current directory or setting the DOTFONTPATH path variable.
Daniel@0 174 Your custom font must be explicitly installed by fontconfig tools.
Daniel@0 175
Daniel@0 176 For a single font, e.g., foo.ttf:
Daniel@0 177 $ mkdir -p ~/.fonts
Daniel@0 178 $ cp foo.ttf ~/.fonts/
Daniel@0 179
Daniel@0 180 One can run fc-cache to speed up the use of fontconfig.
Daniel@0 181 $ fc-cache
Daniel@0 182
Daniel@0 183 For Windows users, one can go to the C:\windows\fonts
Daniel@0 184 folder and use File -> Install New Font from the pull-down menus
Daniel@0 185 to install the font.
Daniel@0 186
Daniel@0 187 For a new font directory, e.g., /Library/Fonts, add a new <dir> element
Daniel@0 188
Daniel@0 189 <dir>/Library/Fonts</dir>
Daniel@0 190
Daniel@0 191 to a .conf file. Note that the file must have a correct xml structure
Daniel@0 192 as specified by the fontconfig fonts.dtd. Possible choices for the
Daniel@0 193 .conf file are local.conf in the same directory as the system-wide
Daniel@0 194 fonts.conf file, or .fonts.conf in your home directory.
Daniel@0 195
Daniel@0 196 How can I ... font?
Daniel@0 197 See: http://www.fontconfig.org/fontconfig-user.html
Daniel@0 198
Daniel@0 199 Can I specifiy a font by filename instead of by familyname?
Daniel@0 200 Sorry, the answer is no. {The reason is that for this to
Daniel@0 201 work, Graphviz has to intercept the font lookup before
Daniel@0 202 fontconfig is called, and this can't be done when fonts
Daniel@0 203 are being looked up by Pango.)
Daniel@0 204
Daniel@0 205 Some versions of fontconfig appear to recognize pathnames and
Daniel@0 206 attempt to use that, but this isn't always the case.
Daniel@0 207
Daniel@0 208 How can I be sure that a specific font is selected?
Daniel@0 209 Provide enough specification in the fontname, and test it
Daniel@0 210 with fc-match to ensure that your desired font is selected.
Daniel@0 211 (Note, this will not ensure that the same font is used in -Tps
Daniel@0 212 or -Tsvg renderings where we rely on the fonts available on the
Daniel@0 213 final printer or computer.)
Daniel@0 214
Daniel@0 215 Note the downside, as mentioned previously, is that Graphviz cannot
Daniel@0 216 do much to warn you when fontconfig didn't find a very
Daniel@0 217 good match, because fontconfig just cheerfully falls back
Daniel@0 218 to some standard font. It would be really nice if the
Daniel@0 219 fontconfig developers could provide a metric reflecting the
Daniel@0 220 quality of the font match in their API.
Daniel@0 221
Daniel@0 222 What about SVG fonts?
Daniel@0 223 Graphviz has a native SVG driver that we wrote (which is the
Daniel@0 224 default), and cairo's SVG driver (which you get with -Tsvg:cairo).
Daniel@0 225
Daniel@0 226 Graphviz' native SVG driver generates Windows compliant names
Daniel@0 227 like "Times New Roman" or Arial by default. The names work in a
Daniel@0 228 lot of situations (like Firefox running on Windows), but are
Daniel@0 229 not guaranteed to be portable. If you set -Gfontnames=ps,
Daniel@0 230 you get Postscript names like Times-Roman. If you set -Gfontnames=svg
Daniel@0 231 you are guaranteed to get rock solid standards compliant SVG.
Daniel@0 232 The SVG standard says that the legal generic font names
Daniel@0 233 are Serif, Sans-Serif, and Monospace (plus Cursive and
Daniel@0 234 Fantasy which we don't use in Graphviz). We generate those names.
Daniel@0 235 The bad news is that various downstream renderers and editors
Daniel@0 236 may resolve the generic font names differently, so it's not
Daniel@0 237 quite clear how your SVG will look. Many W3C examples show
Daniel@0 238 how to use CSS (Cascading Style Sheets) to get around this
Daniel@0 239 problem by giving a list of font family names in order of
Daniel@0 240 lookup precedence, but some downstream processors (like the
Daniel@0 241 inkscape editor in Linux) don't implement CSS, so we're up a tree here.
Daniel@0 242
Daniel@0 243 The cairo SVG driver solves this in an effective though brute
Daniel@0 244 force way: it simply encodes embeds the needed fonts as lines and
Daniel@0 245 curves in the target SVG. For small examples, -Tsvg:cairo is
Daniel@0 246 about 10 times bigger than -Tsvg, but maybe it's worth it for
Daniel@0 247 correctness. The other problem is that such SVG is much much
Daniel@0 248 slower to render, no doubt because it bypasses any system
Daniel@0 249 font rendering services, and does it the old fashioned way.
Daniel@0 250
Daniel@0 251 What about Postscript fonts?
Daniel@0 252
Daniel@0 253 say something here. What about non-ASCII like Latin1.
Daniel@0 254 what about loading your own fonts via -L like in the old
Daniel@0 255 days with the weird outline font example.
Daniel@0 256
Daniel@0 257 ==="What if" issues for nonstandard Graphviz builds===
Daniel@0 258 The following only apply if you build your own version of Graphviz
Daniel@0 259 by configuring and compiling the source code to build your own
Daniel@0 260 custom executable. If you don't know what this means, it
Daniel@0 261 definitely does not mean you.
Daniel@0 262
Daniel@0 263 No freetype. ============
Daniel@0 264
Daniel@0 265 When graphviz is built on systems without freetype, then only the gd
Daniel@0 266 renderer will be available for bitmap outputs, and the only available
Daniel@0 267 fonts are a small set of builtin bitmap fonts. The poor quality of
Daniel@0 268 these fonts will be evident, also, "dot ... -v 2>&1 | grep font" will
Daniel@0 269 say that the font is "<internal>". This may actually be desirable
Daniel@0 270 for installing minimal graphviz programs on a server where fonts
Daniel@0 271 may not even be installed.
Daniel@0 272
Daniel@0 273
Daniel@0 274 No fontconfig. ==============
Daniel@0 275
Daniel@0 276 If graphviz is built on systems without fontconfig (e.g. Redhat-7) then
Daniel@0 277 the fontname attribute will be interpreted as a font file name. The
Daniel@0 278 system directories will be searched for this, or the directories can
Daniel@0 279 be specified with the GDFONTPATH environment variable (or DOTFONTPATH
Daniel@0 280 for historical reasons). Graphviz will use gd and freetype to obtain
Daniel@0 281 metrics and render text. No pango/cairo renderers will be available
Daniel@0 282 without fontconfig support.
Daniel@0 283
Daniel@0 284
Daniel@0 285 Disabling fontconfig. =====================
Daniel@0 286
Daniel@0 287 Pango/cairo depends on fontconfig, so to disable fontconfig you also have
Daniel@0 288 to disable pango/cairo. The easiest way to do this temporarily is to
Daniel@0 289 edit /usr/lib/graphviz/config and remove the entire "libpango" block.
Daniel@0 290 [Note that any changes to this file will be lost the next time graphviz
Daniel@0 291 is updated, or "dot -c" is run with installer priviledges.]
Daniel@0 292
Daniel@0 293 With pango disabled, graphviz will use gd which, even if it was built with
Daniel@0 294 fontconfig support, will still allow fontnames to be given as filenames.
Daniel@0 295
Daniel@0 296 You can also disable cairopango at build time with configure script options.
Daniel@0 297
Daniel@0 298
Daniel@0 299 No gd. =====
Daniel@0 300
Daniel@0 301 Cairopango works without gd. We are moving graphviz to the pango/cairo
Daniel@0 302 libraries, but gd still offers some features that are hard to replace,
Daniel@0 303 such as JPEGs, GIFs and paletted color bitmap outputs. However, font support
Daniel@0 304 is fully functional without gd so long as pango, cairo, fontconfig,
Daniel@0 305 freetype are available.
Daniel@0 306
Daniel@0 307 No pango/cairo. ===============
Daniel@0 308
Daniel@0 309 Without pango/cairo, some of the key renderers are only available
Daniel@0 310 with gd, which produces lower quality (but smaller) output.
Daniel@0 311
Daniel@0 312 Looking forward, we expect to depend more on pango for things like:
Daniel@0 313 line wrapping, multiple fonts per label, bidirectional text and
Daniel@0 314 other internationalization features.
Daniel@0 315
Daniel@0 316 No gd and no cairopango =====
Daniel@0 317 This is basically the original Graphviz without any external fonts.
Daniel@0 318 It cannot render any raster formats, so it's mainly good for Postscript.
Daniel@0 319 It relies on a few internal font tables