# Obscure But Nifty #1: bm2font

Here I’m going to talk about tools and tricks that are perhaps a bit obscure and outdated, but nifty. Interesting solutions remain interesting even if newer solutions arrive. Solutions that were valuable when resources were more highly constrained may not be useful today, but can teach us something about solving problems and working with limited resources. Or they might just be interesting. bm2font is an old utility that converts bitmap images to LaTeX fonts so they can be included in documents without loading packages.

Why?  Because it is nifty.  Here is the recipe:

(1) Grabbed a couple of images off my blog (http://wp.me/a3fsue-iB and http://wp.me/p3fsue-nZ).  One a png one a jpg.  Used convert (ImageMagick) to turn them into a suitable format for bm2fontgif looks good.

$convert.exe TheBigHuntcover-1.jpg TheBigHuntcover-1.gif$ convert.exe twothingscar.png twothingscar.gif

(2) bm2font on the colour image:

$bm2font -h600 -v600 TheBigHuntcover-1.gif This is BitMapTOfont, version 3.0 of january 94, via p2c Converting Bitmap Files to TeX-Fonts Decoding GIF file Image is loaded sequentially. maximum width of characters is 480 pixel maximum height of characters is 480 pixel Halftone picture required Analysing and optimizing the picture Best result may be with parameter -b1 !? Dithering the picture Packing font thebigha.600pk .. Font thebigha.600pk at 600 dots per inch written Packing font thebighb.600pk .. Font thebighb.600pk at 600 dots per inch written Packing font thebighc.600pk .. Font thebighc.600pk at 600 dots per inch written Packing font thebighd.600pk .. Font thebighd.600pk at 600 dots per inch written Packing font thebighe.600pk .... Font thebighe.600pk at 600 dots per inch written Generating tfmfile thebigha.tfm Generating tfmfile thebighb.tfm Generating tfmfile thebighc.tfm Generating tfmfile thebighd.tfm Generating tfmfile thebighe.tfm File thebigh.tex written input thebigh.tex into your document and set the picture with \setthebigh (3) bm2font on the greyscale image (-wn makes white white not light grey) $ bm2font -wn -h600 -v600 twothingscar.gif
This is BitMapTOfont, version 3.0 of january 94, via p2c
Converting Bitmap Files to TeX-Fonts
Decoding GIF file
maximum width of characters is 480 pixel
maximum height of characters is 480 pixel
Halftone picture required
Analysing and optimizing the picture
Dithering the picture
Packing font twothina.600pk
........
Font twothina.600pk at 600 dots per inch written
Packing font twothinb.600pk
........
Font twothinb.600pk at 600 dots per inch written
Generating tfmfile twothina.tfm
Generating tfmfile twothinb.tfm
File twothin.tex written
input twothin.tex into your document and set the picture with \settwothin

(4) Create a small .tex file, test.tex:

\documentclass{article}

\newcommand{\pkg}[1]{\textsf{#1}}
\newcommand{\prog}[1]{\texttt{#1}}

\listfiles

\pagestyle{empty}

\begin{document}

The following pictures were inserted into
package, not even \pkg{graphics}\ or \pkg{graphicx}.
\prog{bm2font}\footnote{http://www.ctan.org/tex-archive/graphics/bm2font/}
was used to convert bitmaps to pixel fonts,
which \LaTeX\ (and \TeX) can include natively.

\input{twothin}
\input{thebigh}

\begin{center}
First, the colour, but mostly greys, picture.

\setthebigh

\vspace{2\baselineskip}

Secondly, the monochrome image.

\settwothin
\end{center}

\end{document}               % End of document.

(5) Now, the bm2font commands created several files, one .tex file for each image, plus enough fonts to contain enough glyphs (each glyph being an array of black and white dots) to reconstruct the images.  These fonts are each described by a tfm file and a packed pixel font file, pk.  So a directory listing looks like this:

$ls thebigh* twothin* thebigh.tex thebighc.600pk thebighe.tfm twothinb.tfm thebigha.600pk thebighc.tfm twothin.tex twothingscar.gif thebigha.tfm thebighd.600pk twothina.600pk twothingscar.png thebighb.600pk thebighd.tfm twothina.tfm thebighb.tfm thebighe.600pk twothinb.600pk The .tex file is the TeX code needed to tell (La)TeX about the font — what files it is in, where to put the glyphs, how big the box is over all. The pk files contain the glyphs and the tfm files contain the metrics that let (La)TeX work with them. For example $ cat thebigh.tex
\newbox\thebighbox
\newdimen\thebighwd
\font\thebigha=thebigha at 57.816truept
\font\thebighb=thebighb at 57.816truept
\font\thebighc=thebighc at 57.816truept
\font\thebighd=thebighd at 57.816truept
\font\thebighe=thebighe at 57.816truept
\setbox\thebighbox=\vbox{\hbox{%
\thebigha\char0\char1\thebighb\char0}}
\thebighwd=\wd\thebighbox
\setbox\thebighbox=\hbox{\vbox{\hsize=\thebighwd
\parskip=0pt\offinterlineskip\parindent0pt
\hbox{\thebigha\char0\char1\thebighb\char0}
\hbox{\thebighb\char1\thebighc\char0\char1}
\hbox{\thebighd\char0\char1\thebighe\char0}
\hbox{\thebighe\char1\char2\char3}}}
\ifx\parbox\undefined
\def\setthebigh{\box\thebighbox}
\else
\def\setthebigh{\parbox{\wd\thebighbox}{\box\thebighbox}}
\fi

(6)  So then:

$latex test Output of \listfiles shows that it reads in *File List* article.cls 2007/10/19 v1.4h Standard LaTeX document class size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option) twothin.tex thebigh.tex *********** (7) Convert to PostScript. We can see that it reads in the pk files which contain the actual images. If your default resolution is not 600 dpi, you might need dvips -D 600 test to force it to use the ‘600pk‘ files. Or change the flags in the call to bm2font. $ dvips test
' TeX output 2015.06.04:1211' -> test.ps
<./thebigha.600pk><./thebighb.600pk><./thebighc.600pk><./thebighd.600pk>
<./thebighe.600pk><./twothina.600pk><./twothinb.600pk>...


(8) If you like, you can run ps2pdf at this stage.

ps2pdf -dEmbedAllFonts=true -dCompatabilityLevel=1.3 test.ps

The flags are important because the fonts here are non-standard, custom ones.  I can then get a png using Adobe Acrobat, say, or some other conversion tool. The image used in this post was generated from the PostScript output of dvips, thus:

pstopnm -dpi 600 test.ps

which made a massive pnm file that was opened in Imagej, cropped and scaled to 600 pixels wide.

(9) Some comments:  These pictures tend to print better than they look on screen.  Since they are made of glyphs, anything that can modify glyphs can be used (\scalebox, \rotatebox, color commands, and so on, for example). Note that bm2font is old.  Does not work directly with png, for example.  Also, since it produces pk and tfm files, but of course not METAFONT input, the dvi viewer/convertor cannot automatically generate new fonts at different resolutions, so if you want to change from say 600 dpi to 1200 dpi, a new bm2font run has to be done, preferably after deleting the old outputs. It has a lot of switches to play with to modify the conversion to improve it, but can take quite a bit of trial and error.  You’ll note I have used pretty much default settings.  Lastly, it is essentially for greyscale/monochrome images.