Convert a PK Font to PBM Files with pktopbm
You will turn a TeX packed (PK) font file into one PBM bitmap per character, with predictable names and dimensions you can inspect with other Netpbm tools. This guide uses the installed Netpbm 11.5.2 command. Allow about fifteen minutes if you already have a PK file and know which character range it contains.
The route
Jump straight to the step you need, or tick off Done means at the end.
Prerequisites are a readable PK font, the netpbm package, and a writable output directory. The conversion itself normally needs no elevated privileges. Do not use sudo just because the input font came from a system directory: copy it to a working directory or grant your normal account read access instead.
1. Check the installed command
Confirm the executable and package version before building a script around it. This is a read-only checkpoint:
$ command -v pktopbm
/usr/bin/pktopbm
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ pktopbm --version
pktopbm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pktopbm: Built from source dated 2024-03-31 09:09:47
pktopbm: Built by Debian
pktopbm: Use 'man pktopbm' for help.
The version output also includes build details, so the wording can differ on another distribution. The useful checkpoint is that the command resolves and identifies the Netpbm library. The local manual is dated 15 September 2023 and documents the command used here.
2. Prepare a separate output directory
pktopbm writes PBM output files. It does not edit the PK input, but shell redirection and output names can still overwrite files if you choose an existing destination. Start in a new directory:
$ mkdir -p ~/tmp/pktopbm-output
$ cd ~/tmp/pktopbm-output
$ cp --preserve=all /path/to/font.pk ./font.pk
$ test -r font.pk && echo 'input is readable'
input is readable
Replace /path/to/font.pk with the real file. Copying is optional, but it gives you a working copy to retain if you later remove generated bitmaps. The command accepts - as a filename for standard input or standard output, but a named file is easier to check while learning the workflow.
Safety boundary
Do not place the output glob in a directory containing valuable PBM files unless you have checked the names pktopbm will use. If you need to abandon this preparation, remove only the copied input and empty directory you created, not the original font.
3. Convert a font using its recorded bitmap sizes
Run the input PK file followed by one or more PBM output filenames:
$ pktopbm font.pk glyph-001.pbm glyph-002.pbm
The first output file is associated with the first character selected from the font, the second with the next character, and so on. The default width and height come from each bitmap in the font. The command reads the packed font and writes the requested PBM files; it does not resize every glyph to a common canvas by default.
Check the files immediately:
$ ls -lh glyph-001.pbm glyph-002.pbm
$ file glyph-001.pbm glyph-002.pbm
glyph-001.pbm: Netpbm image data, bitmap, 8 x 12
glyph-002.pbm: Netpbm image data, bitmap, 7 x 12
Your dimensions and file wording will depend on the font. A successful command with no progress message is normal. Check the exit status in a script with $?, and keep the original PK file until the PBM images have been inspected.
4. Select the character number for the first output
Use -character followed by a numeric code point when the first output filename should start at a particular character. For example, this asks for output beginning at character 65:
$ pktopbm -character 65 font.pk glyph-065.pbm glyph-066.pbm
$ file glyph-065.pbm glyph-066.pbm
glyph-065.pbm: Netpbm image data, bitmap, 8 x 12
glyph-066.pbm: Netpbm image data, bitmap, 8 x 12
There is no output for code points lower than the supplied starting number. The option is useful when you have a selected range of filenames, but it does not make missing characters appear. Use an exact number from the font's encoding or your conversion plan, not a guessed letter label.
The synopsis in some installed documentation shows the short spelling -c, while the options section names -character. The current binary accepts -c as well, but the long spelling is clearer in new scripts:
$ pktopbm -c 65 font.pk glyph-065.pbm
Do not confuse this option with a request to change the glyph bitmap. It only changes which character the first output file represents.
5. Put glyphs on a common canvas
Use -x for a fixed width, -y for a fixed height, or both. These values apply to every bitmap produced by that invocation:
$ pktopbm -x 16 -y 16 -character 65 font.pk glyph-065.pbm glyph-066.pbm
$ file glyph-065.pbm glyph-066.pbm
glyph-065.pbm: Netpbm image data, bitmap, 16 x 16
glyph-066.pbm: Netpbm image data, bitmap, 16 x 16
When either dimension is specified, pktopbm uses the horizontal and vertical offsets stored in the PK file to relocate the bitmap. The base point is placed in the lower-left corner. If the requested canvas is too small, content at the top or right is truncated. That is a data-loss risk for the generated PBM files, so choose dimensions from the font metrics rather than using 16 by 16 blindly.
For a fixed width but font-defined height, use only -x. For a fixed height but font-defined width, use only -y. Verify the resulting dimensions with file; a file existing on disk does not prove that the canvas was large enough.
6. Diagnose a failed conversion
A missing input is reported before any useful conversion can happen:
$ pktopbm /path/to/missing.pk glyph.pbm
pktopbm: Unable to open file '/path/to/missing.pk' for reading. fopen() returns errno 2 (No such file or directory)
Check the path and permissions without changing anything:
$ ls -l /path/to/font.pk
$ test -r /path/to/font.pk && echo readable
An invalid or non-PK input produces a bad-file diagnostic such as bad PK file (pre command missing). Check that the file is the packed font itself, not a TeX font metric file, a compressed archive, or a file whose name merely ends in .pk. The filename suffix does not validate the contents.
If the image dimensions are surprising, first rerun without -x or -y to see the font's recorded sizes. If you need a common canvas, add one dimension at a time and check the output. If glyphs are visibly clipped, choose a larger canvas and regenerate into a new directory. The existing PBM files are not repaired in place by a later command.
Done means
pktopbmis the expected Netpbm 11.5.2 executable and the PK input is readable.- Each requested output file is a non-empty PBM image confirmed with
file. - The first output character is controlled with an explicit
-charactervalue when needed. - Fixed
-xand-ydimensions were chosen deliberately and checked for clipping. - The original PK font remains available, and generated files were written to a reviewed destination.