Home / Alt manpages / printafm(1)

  • printafm(1)
  • User command
  • linux

Extract PostScript Font Metrics with printafm

You will finish with an Adobe Font Metrics (AFM) file generated from an installed PostScript font, plus a couple of checks that show whether the file is complete. The examples use printafm from Ghostscript 10.02.1, installed here as package version 10.02.1~dfsg1-0ubuntu7.9.

Allow about ten minutes. You need a shell, the ghostscript package, and an installed Type 1 PostScript font. This is a read-only inspection workflow apart from creating the output file, so it normally needs no elevated privileges.

1. Check the installed command

Confirm which script will run and record the Ghostscript version. These are ordinary read-only commands:

$ command -v printafm
/usr/bin/printafm
$ dpkg-query -W -f='${Package} ${Version}\n' ghostscript
ghostscript 10.02.1~dfsg1-0ubuntu7.9
$ gs --version
10.02.1

The exact package suffix is distribution-specific. The useful part for this guide is the Ghostscript release: the local printafm(1) manpage was revised for version 10.02.1. The wrapper invokes Ghostscript in quiet, non-display mode and sends the AFM text to standard output.

Checkpoint

If command -v printafm prints nothing, install or enable Ghostscript through your normal package-management process before continuing. Do not copy a script from another machine just to make the command appear.

2. Find a font name that Ghostscript can use

printafm takes one argument, called fontname in the synopsis. It does not take a directory and it does not choose a default font. Start by locating a local Type 1 font file:

$ find /usr/share/fonts -type f \( -iname '*.pfb' -o -iname '*.pfa' \) | head -n 5
/usr/share/fonts/X11/Type1/NimbusSansNarrow-Bold.pfb
/usr/share/fonts/X11/Type1/c0611bt_.pfb
/usr/share/fonts/X11/Type1/URWBookman-Light.pfb
/usr/share/fonts/X11/Type1/D050000L.pfb
/usr/share/fonts/X11/Type1/URWBookman-DemiItalic.pfb

For the first example, use the font's PostScript name, NimbusSansNarrow-Bold, rather than the .pfb pathname. Ghostscript's font lookup resolves that name on this installation. Font names are case-sensitive in practice, and the name in a filename is not always the internal PostScript name, so do not guess when a lookup fails.

If the search returns no files, your machine may have no Type 1 fonts in that directory. Check the font package and its installed locations. A TrueType or OpenType font is not automatically a suitable input for this command just because another program can render it.

3. Generate an AFM file without overwriting an old one

AFM output is written to standard output. Redirect it to a new pathname so that the command's diagnostic stream remains visible in the terminal:

$ printafm NimbusSansNarrow-Bold > NimbusSansNarrow-Bold.afm
$ printf 'exit status: %s\n' "$?"
exit status: 0

The redirection operator creates the file before the program starts and truncates an existing file with the same name. That is the main destructive trap in this small workflow. If the destination already contains useful metrics, choose a new name first, or make a backup before running the command:

$ cp --preserve=all NimbusSansNarrow-Bold.afm NimbusSansNarrow-Bold.afm.bak
$ printafm NimbusSansNarrow-Bold > NimbusSansNarrow-Bold.afm.new
$ mv NimbusSansNarrow-Bold.afm.new NimbusSansNarrow-Bold.afm

The last command replaces the old file only after generation succeeds. If generation fails, remove the incomplete .new file and keep the original. Removing the backup later is irreversible, so do that only after checking the replacement.

Checkpoint

The command should produce no normal terminal output because the AFM text went to the file. A zero exit status means Ghostscript completed; it is still worth checking the file content.

4. Verify the AFM structure

An AFM file is text. Inspect its opening records and its closing marker:

$ sed -n '1,16p' NimbusSansNarrow-Bold.afm
StartFontMetrics 2.0
FontName NimbusSansNarrow-Bold
ItalicAngle 0.0
Weight Bold
Notice (URW)++,Copyright 2014 by (URW)++ Design & Development
IsFixedPitch false
Version 1.00
Copyright Copyright (URW)++,Copyright 2014 by (URW)++ Design & Development
UnderlineThickness 50
FamilyName Nimbus Sans Narrow
FullName Nimbus Sans Narrow Bold
UnderlinePosition -100
FontBBox -144 -307 1000 1094
StartCharMetrics 854
C 32 ; WX 228 ; N space ; B 228 0 228 0 ;
$ tail -n 3 NimbusSansNarrow-Bold.afm
EndCharMetrics
EndFontMetrics

The exact metadata and character count depend on the selected font. Check the markers rather than comparing every line with this example:

$ grep -E '^(StartFontMetrics|FontName|StartCharMetrics|EndCharMetrics|EndFontMetrics)' NimbusSansNarrow-Bold.afm
StartFontMetrics 2.0
FontName NimbusSansNarrow-Bold
StartCharMetrics 854
EndCharMetrics
EndFontMetrics

If the file is empty or lacks EndFontMetrics, treat the run as failed even if a later script accepts the partial file. Keep the source font installed and rerun with its correct PostScript name.

5. Diagnose lookup and argument errors

Running the command without its required argument prints its usage instead of selecting a default font:

$ printafm
Usage: printafm fontname

A guessed name can produce a Ghostscript error and a non-zero status. Capture output in a temporary file if you need to preserve the diagnostic while testing a name:

$ printafm NOT_A_REAL_FONT > /tmp/printafm-test.afm 2> /tmp/printafm-test.err
$ status=$?
$ printf 'exit status: %s\n' "$status"
exit status: 1
$ sed -n '1,8p' /tmp/printafm-test.err

The wording can vary with the Ghostscript build. The useful evidence is a non-zero status and an error naming the unresolved font or PostScript problem. Do not solve a font lookup failure with sudo: elevated privileges do not invent a font name, and running the command as root can leave the generated file owned by root.

When you have finished examining the temporary diagnostic, remove those two test files if they contain nothing you need. They are not part of the AFM workflow. No service restart, font-cache rebuild or system configuration change is required to generate metrics from a font that Ghostscript can already resolve.

Done means

  • printafm and its Ghostscript package version were checked.
  • You supplied one real PostScript font name instead of relying on a default.
  • The AFM text was redirected to a deliberately chosen output path.
  • The output contains matching StartFontMetrics and EndFontMetrics markers.
  • An existing metrics file was protected from truncation, or can be restored from its backup.
  • No elevated privilege or persistent system change was needed.