summaryrefslogtreecommitdiff
path: root/info/virtualfontshowto/virtualfontshowto.txt
blob: 7e4568f0cce53154ea4faa1eee64c8129dc1f082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
                        Virtual Fonts -- a Tutorial

                              Thomas A. Schmitz

                               February 9, 2005



Lots of information on virtual fonts can be found on the web and in books
(e.g., Knuth's very own ``Virtual Fonts: More Fun for Grand Wizards,''
available on CTAN [CTAN.info.virtual-fonts.knuth]). However, there
doesn't seem to be 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 is fairly trivial, but maybe someone can make good use of it.



1. Basic Facts about Virtual Fonts

Virtual fonts 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 foobar that will be identical to foo but print an
``A'' whenever you have a ``B'' in your \TeX-file. This may sound absurd at
first, but there are cases where this is useful. Some fonts offer alternative
forms for letters. With the help of a virtual font, you can remap the
characters and thus switch to these alternative forms without changing your
\TeX-source.  The second use for virtual fonts, however, is much more common:
Given a font foo, you can create a virtual font foobar that will use some
characters from a second font, say bar. This is often used to include old-style
numerals or additional ligatures that are not provided by the normal font.



2. Copying the font-files

So let's begin. We assume that you will be using two postscript-fonts, foo and
bar. Usually, for each of these fonts, you will have two files foo.tfm
(bar.tfm) and foo.pfb (bar.pfb) and nothing else, so we need to create a
vf-file from scratch.  I didn't find it mentioned anywhere that this is not
only possible, but even fairly easy. 

We assume that you will be performing these operations on the command line in a
working directory like /tmp. So the first step would be to copy foo.tfm to this
directory:

cd /tmp

cp PATH_TO_foo.tfm .

(Watch the trailing period, it's necessary!



3. Create a Human-Oriented Property-List

The file foo.tfm is a binary in a format that \TeX can read. If we want to edit
it, we will have to convert it to a human-oriented property list (with
extension .pl). We will be using tools that come with any complete
\TeX-installation. On the command-line, do this:

tftopl foo foo

(yes, that's right: you have to type foo twice)



4. Open the Property-List

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. If you're editing
it in a non-UNIX environment like Windows or Mac OS X, make sure that your
editor is set to use UNIX line-endings (all kinds of nasty things can happen
when UNIX apps like \TeX  encounter non-UNIX 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. Editing the Property-List

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). In a first step, we will
create a virtual font that will remap some characters within foo. So just
before the line starting with (LIGTABLE, add this:

(MAPFONT D 0
   (FONTNAME foo)
   (FONTDSIZE R 10.0)
   )

The FONTDSIZE of foo could be found in the first lines (DESIGNSIZE R 10.0), so
all you have to do is copy this information.



6. Remapping a Character

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)
   )

\TeX only cares about the dimensions of every character and puts an empty box
with these dimensions into the .dvi-file. 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 you want the box for ``B'' to
be of the same size as the box for ``A.'' Hence, 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)
      )
   )

One of the things that can be a bit confusing about these property lists is the
fact that (apart from the numbers and the 26 letters of the alphabet)
characters are referred to by ``octal numbers.'' If you want to know what
character corresponds to what octal number, you can have a look at the tables
created by testing the font (section 9).



7. Saving the File

That's it! You have modified the font description; now you need to generate the
binary files for \TeX to use. The next 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. Generating the Binaries

Back to the command line. 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 is a problem. 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. Installing the New Font

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 ~/texmf or ~/Library/texmf (depending on your local setup as
defined in texmf.cnf). So the files should go here:

cp foobar.tfm ~/texmf/fonts/tfm/

cp foobar.vf ~/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), I would suggest you
test it in a nice quiet little directory on your box. cd to this directory and
then do this:

cd ~

pdfetex testfont

pdfetex will respond to this command like so:

This is pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.3)
 \write18 enabled.
entering extended mode
(/usr/local/teTeX/share/texmf.tetex/tex/plain/base/testfont.tex

Name of the font to test = 

You type 

foobar

pdfetex will respond:

Now type a test command (\help for help):)

*

you type:

\table

pdfetex will open a new line with another asterisk, and you type

\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.

If your \TeX-installation includes \CONTEXT, a macro package for \TeX (see
http://tug.org/pracjourn/2005-1/asknelly/), you can also create a very
nice colorized table. Create a file test.tex with this content:

\starttext

\showfont[foobar]

\stoptext

Then, run this file through \CONTEXT: 

texexec --nonstopmode --pdf test.tex

You'll get a table with all the glyphs; every cell will indicate the decimal,
hexadecimal, and octal value of the glyph (very handy for editing property
lists).



10. Adding a Second Font

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:

rm foobar.vpl

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)
      )
   )

That's 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.  

Now add the information about the second font, just as described in
section 5: 

(MAPFONT D 1
  (FONTNAME bar)
  (FONTDSIZE R 10.0)
  )

If you want to know the DSIZE of bar, just convert bar.tfm into bar.pl, open
this file and look into the first lines (we'll need bar.pl anyway). Or, if you
want to be fancy, you could even do this: tftopl `kpsewhich bar.tfm` | grep
DESIGNSIZE (watch the ``backticks,'' those are single opening quotes!).

(CHARACTER C 0
   (CHARWD R 0.514)
   (CHARHT R 0.628)
   (CHARDP R 0.1)
   (MAP
      (SELECTFONT D 0) 
      (SETCHAR C 0)
      )
   ) 



11. Including Glyphs from a Second Font

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 of 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 done this for all the numerals,
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.



12. Post-Install

After you have edited your virtual font, you can discard the .vpl-file. If you
ever want to edit your font again, you can recreate it by copying both the .vf
and the .tfm into the same directory and running vftovp foobar foobar foobar

Have great fun and feel like a grand wizard!



13. Further Reading

http://www.cl.cam.ac.uk/users/rf/pstex/index.htm

http://homepage.mac.com/bkerstetter/tex/fonttutorial-current.html

http://zoonek.free.fr/LaTeX/Fontes/fontes.html (in French)