[OS X TeX] Virtual Fonts -- HOWTO

Thomas A.Schmitz thomas.schmitz at uni-bonn.de
Sun Jan 30 06:14:24 EST 2005


If you search the web, there's lots of information on virtual fonts 
(e.g., Knuth's very own ``Virtual Fonts: More Fun for Grand Wizards,'' 
available on CTAN). What I found missing was a step-by-step tutorial 
for non-wizards like myself. I experimented a bit with virtual fonts 
during the last couple of days. It took me a while to understand the 
basics, so I thought that other people might find it useful to hear 
about this and avoid some common mistakes. Most of what I write will be 
trivial to most of you, but maybe someone can make good use of it. (If 
any of the font experts are reading: please correct where needed; feel 
free to include in any websites etc.).

1. Let's start by discussing a few of the features of virtual fonts. 
They can do two immensely useful things. They can remap characters 
within the same font. If you have a font foo with files foo.pfb and 
foo.tfm, you can have a virtual font bar that will be identical to foo 
but print an ``A'' whenever you have a ``B'' in your TeX-file. (This is 
not as absurd as it sounds: some letters have alternative forms, and 
with the help of virtual fonts you can switch between them without 
having to edit your source file.) Or you can take some letters from a 
second font, say bar. This is used quite often to include old-style 
numerals or additional ligatures that are not included in the normal 
font.

2. So let's begin. Usually, for any given font, you will have the two 
files foo.tfm and foo.pfb (if it's a postscript font) and nothing else, 
so we need to create a vf-file from scratch. This is far easier than it 
sounds, and I didn't find it mentioned anywhere. Open the terminal 
(yes, you will have to spend some time in this dark and scary place) 
and copy foo.tfm to the directory where you will be doing your magic:
cp PATH_TO_foo.tfm ./

3. We will have to convert the binary tfm to a human-oriented property 
list. Do this:
tftopl foo foo
(yes, that's right: write foo twice)
All the tools and programs mentioned here come with a normal TeX 
installation.

4. You now have a new file foo.pl which contains all the information 
about the font that TeX needs. Open it in your favorite text editor; 
make certain that it is set to use UNIX line-endings (all kinds of 
nasty things can happen when UNIX apps like TeX encounter mac 
line-endings). The first couple of lines will read like this:
(FAMILY TEX-FOO)
(FACE F MRR)
(CODINGSCHEME FONTSPECIFIC + TEX TEXT)
(DESIGNSIZE R 10.0)
(COMMENT DESIGNSIZE IS IN POINTS)
(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)
(FONTDIMEN
    (SLANT R 0.0)
    (SPACE R 0.252)
    (STRETCH R 0.2)
    (SHRINK R 0.1)
    (XHEIGHT R 0.459)
    (QUAD R 1.0)
    (EXTRASPACE R 0.111)
    )
(LIGTABLE
etc.
If there is a line (CHECKSUM O ...), delete it (it will be regenerated 
later).

5. In order to generate a virtual font, you need to modify this file. 
First, you will have to tell TeX which fonts this virtual font will be 
referring to. Let's say they're foo.tfm and bar.tfm (needless to say, 
both have to be installed and functional in your TeX-installation). 
Just before the line starting with (LIGTABLE, add this:
(MAPFONT D 0
    (FONTNAME foo)
    (FONTDSIZE R 10.0)
    )
(MAPFONT D 1
    (FONTNAME bar)
    (FONTDSIZE R 10.0)
    )
The FONTDSIZE of foo could be found in the first lines (DESIGNSIZE R 
10.0), so you only need to copy this information. If you want to know 
the DSIZE of bar, just convert bar.tfm into bar.pl and open this file. 
For the time being, let's say you want to use just one font and remap 
characters within this font, so you will add only the section referring 
to MAPFONT D 0; we'll take care of bar later.

6. If you scroll down in this file, the LIGTABLE (containing 
information about ligatures and kerning) will end with two lines
(STOP)
)
After this, the section with information about all the defined 
characters in the font will follow, probably starting something like 
this:
(CHARACTER O 0
    (CHARWD R 0.674)
    (CHARHT R 0.726)
    )
As you know, TeX cares only about the dimensions of every character and 
puts an empty box with this dimension into the .dvi. It is only when 
this .dvi is interpreted by a postscript- or pdf-driver that the actual 
character (the ``glyph'') is put into this box. So let us assume you 
want to have a virtual font that will always print ``A'' when you have 
``B'' in your source. Scroll further down until you reach the section 
with the capital letters:
(CHARACTER C A
    (CHARWD R 0.747)
    (CHARHT R 0.747)
    )
(CHARACTER C B
    (CHARWD R 0.739)
    (CHARHT R 0.726)
    )
TeX will be using the box described here, so the first thing you need 
to do is copy the dimensions of ``A'' into ``B.'' So the section should 
look like this:
(CHARACTER C A
    (CHARWD R 0.747)
    (CHARHT R 0.747)
    )
(CHARACTER C B
    (CHARWD R 0.747)
    (CHARHT R 0.747)
    )
Next (and this is the magic of virtual fonts) you will tell TeX that it 
should remap ``B'' to ``A.'' Just before the closing parenthesis of 
CHARACTER B, you will insert a new section. Now ``B'' will look like 
this:
(CHARACTER C B
    (CHARWD R 0.747)
    (CHARHT R 0.747)
    (MAP
       (SETCHAR C A)
       )
    )

7. That's it! You have modified the font description; now you need to 
generate the binary files for TeX to use. The nest step is extremely 
important: SAVE THE FILE TO A DIFFERENT NAME. So in our case, let's say 
we call the new virtual font foobar (the name doesn't matter; the 
extension has to be .vpl); so save to foobar.vpl

8. Back to the Terminal. We now run a little program that will convert 
your foobar.vpl into two new files: foobar.tfm and foobar.vf
vptovf foobar.vpl
This will not only do the conversion, it will also check whether you 
.vpl-file is in good order. It is very picky about the right 
indentation level and parentheses, and it will tell you exactly in 
which line there are problems. So if you get errors, just go back and 
edit foobar.vpl again. vptovf may also tell you that it had to ``round 
some units,'' that's OK.

9. So now you should have foobar.vf and foobar.tfm. Copy both files 
into the right places. I would suggest you create your own texmf-branch 
in your home directory, under ~/Library. So the files should go here:
cp foobar.tfm ~/Library/texmf/fonts/tfm/
cp foobar.vf ~/Library/texmf/fonts/vf
(you will have to create these directories if they don't exist yet). 
Since this virtual font will only be seen by TeX, you don't need to 
fiddle with any map file; for the postscript- or pdf-driver, only font 
foo will exist, which was already functional. Before you embark on a 
long journey with this new virtual font (say your 1200-page thesis that 
is due in two weeks), it would be a brilliant idea to test it in a nice 
quiet little directory on your box, let's assume /tmp. Do this:
cd /tmp
pdfetex testfont
This will present you with a new prompt to give the name of the font, 
so you type
foobar
again a new prompt asking you for a command
\table
and then
\end
If all goes well, a file testfont.pdf will be created, with a table 
showing that font foobar does not have a letter ``B,'' but twice the 
letter ``A''---which is just what we wanted.

10. Now let's assume you want to include further stuff, like old-style 
numerals or ligatures from font bar. We go back to our working 
directory and delete the old file foobar.vpl. Don't worry, we'll create 
it again:
vftovp foobar foobar foobar
The reason we do this is that vftovp will automagically include one 
important piece of information: every character description will now 
look like this:
(CHARACTER C A
    (CHARWD R 0.747)
    (CHARHT R 0.747)
    (MAP
       (SETCHAR C A)
       )
    )
This is already not bad, but if we want to mix glyphs from two fonts, 
we will have to say which font to use at every instance. So in your 
editor, you should perform a ``find and replace'' that will find every 
instance of (MAP and replace it with
(MAP
    (SELECTFONT D 0)
In emacs, it is possible to include the line break in the replace 
pattern, I don't know if (and how) other editors can handle it.
Moreover, as described in section 5, add the information about the 
second font:
(MAPFONT D 1
    (FONTNAME bar)
    (FONTDSIZE R 10.0)
    )

11. Now look for the section containing the numerals. It should start 
like this:
(CHARACTER C 0
    (CHARWD R 0.514)
    (CHARHT R 0.628)
    (CHARDP R 0.1)
    (MAP
       (SELECTFONT D 0)
       (SETCHAR C 0)
       )
    )
Again, the first thing you should do is copy the dimensions about 
CHARACTER C 0 from bar.pl to foo.pl. Then replace (SELECTFONT D 0) with 
(SELECTFONT D 1), and everything should work: After you have generated 
the tfm/vf pair (section 8) and copied these files to the right 
directories (section 9), TeX will look at the tfm/vf pair, and will 
take the numerals from font bar, everything else from font foo. Again, 
test to see if this works as expected.

Hope this can help. I'm not a font wizzard myself, so I probably won't 
be able to help with further questions.

Best

Thomas

--------------------- Info ---------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
           & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>





More information about the MacOSX-TeX mailing list