Debian Codegraph @

texk/web2c/omegaware/ovf2ovp.web

raw
1 %
2 % This file is part of the Omega project, which
3 % is based in the web2c distribution of TeX.
4 %
5 % Copyright (c) 1994--2000 John Plaice and Yannis Haralambous
6 % applies only to the changes to the original vftovp.web.
7 %
8 % This program by D. E. Knuth is not copyrighted and can be used freely.
9 % Version 1 was implemented in December 1989.
10 % Version 1.1 fixed problems of strict Pascal (April 1990).
11 % Version 1.2 fixed various bugs found by Peter Breitenlohner (September 1990).
12 % Version 1.3 made out_as_fix tell the truth in overflow cases (Dec 2002).
13 % Version 1.4 reports out-of-range characters (Breitenlohner, Jan 2014),
14 % and tests nl>lig_size not 4*lig_size (C. M. Connelly, Melissa O'Neill).
15
16 % Version 1.0 of OVF2OVP (Month Year).
17 % Version 1.11 (February 2000).
18 % Version 1.12 (September 2009) various bug fixes by Peter Breitenlohner.
19 % Version 1.13 (January 2014) more bug fixes.
20
21 % Here is TeX material that gets inserted after \input webmac
22 \def\hang{\hangindent 3em\indent\ignorespaces}
23 \font\ninerm=cmr9
24 \let\mc=\ninerm % medium caps for names like SAIL
25 \def\PASCAL{Pascal}
26
27 \def\(#1){} % this is used to make section names sort themselves better
28 \def\9#1{} % this is used for sort keys in the index
29
30 \def\title{OVF2OVP}
31 \def\contentspagenumber{101}
32 \def\topofcontents{\null
33 \titlefalse % include headline on the contents page
34 \def\rheader{\mainfont\hfil \contentspagenumber}
35 \vfill
36 \centerline{\titlefont The {\ttitlefont OVF2OVP} processor}
37 \vskip 15pt
38 \centerline{(Version 1.13, January 2014)}
39 \vfill}
40 \def\botofcontents{\vfill
41 \centerline{\hsize 5in\baselineskip9pt
42 \vbox{\ninerm\noindent
43 The preparation of this program
44 was supported in part by the National Science
45 Foundation and by the System Development Foundation. `\TeX' is a
46 trademark of the American Mathematical Society.}}}
47 \pageno=\contentspagenumber \advance\pageno by 1
48
49 @* Introduction.
50 The \.{VFtoVP} utility program converts a virtual font (``\.{VF}'') file
51 and its associated \TeX\ font metric (``\.{TFM}'')
52 file into an equivalent virtual-property-list (``\.{VPL}'') file. It also
53 makes a thorough check of the given files, using algorithms that are
54 essentially the same as those used by
55 \.{DVI} device drivers and by \TeX. Thus if \TeX\ or a \.{DVI} driver
56 complains that a \.{TFM} or \.{VF}
57 file is ``bad,'' this program will pinpoint the source or sources of
58 badness. A \.{VPL} file output by this program can be edited with
59 a normal text editor, and the result can be converted back to \.{VF} and \.{TFM}
60 format using the companion program \.{VPtoVF}.
61
62 \indent\.{VFtoVP} is an extended version of the program \.{TFtoPL}, which
63 is part of the standard \TeX ware library.
64 The idea of a virtual font was inspired by the work of David R. Fuchs
65 @^Fuchs, David Raymond@>
66 who designed a similar set of conventions in 1984 while developing a
67 device driver for ArborText, Inc. He wrote a somewhat similar program
68 called \.{AMFtoXPL}.
69
70 The |banner| string defined here should be changed whenever \.{VFtoVP}
71 gets modified.
72
73 @d banner=='This is OVF2OVP, Version 1.13' {printed when the program starts}
74
75 @ This program is written entirely in standard \PASCAL, except that
76 it occasionally has lower case letters in strings that are output.
77 Such letters can be converted to upper case if necessary. The input is read
78 from |vf_file| and |tfm_file|; the output is written on |vpl_file|.
79 Error messages and
80 other remarks are written on the |output| file, which the user may
81 choose to assign to the terminal if the system permits it.
82 @^system dependencies@>
83
84 The term |print| is used instead of |write| when this program writes on
85 the |output| file, so that all such output can be easily deflected.
86
87 @d print(#)==write(#)
88 @d print_ln(#)==write_ln(#)
89
90 @p program OVF2OVP(@!vf_file,@!tfm_file,@!vpl_file,@!output);
91 label @<Labels in the outer block@>@/
92 const @<Constants in the outer block@>@/
93 type @<Types in the outer block@>@/
94 var @<Globals in the outer block@>@/
95 procedure initialize; {this procedure gets things started properly}
96 var @!k:integer; {all-purpose index for initialization}
97 begin print_ln(banner);@/
98 @<Set initial values@>@/
99 end;
100
101 @ If the program has to stop prematurely, it goes to the
102 `|final_end|'.
103
104 @d final_end=9999 {label for the end of it all}
105
106 @<Labels...@>=final_end;
107
108 @ The following parameters can be changed at compile time to extend or
109 reduce \.{VFtoVP}'s capacity.
110
111 @d char_max=@"FFFF
112 @d xchar_max=char_max+1
113 @d xxchar_max=xchar_max+1
114 @d xxxchar_max=xxchar_max+1
115
116 @<Constants...@>=
117 @!tfm_size=2000000; {maximum length of |tfm| data, in bytes}
118 @!vf_size=600000; {maximum length of |vf| data, in bytes}
119 @!max_fonts=300; {maximum number of local fonts in the |vf| file}
120 @!vc_size=150000; {maximum number of characters from local fonts}
121 @!lig_size=800000; {maximum length of |lig_kern| program, in words}
122 @!hash_size=130003; {preferably a prime number, a bit larger than the number
123 of character pairs in lig/kern steps}
124 @!hash_mult=16007; {another prime}
125 @!max_char=char_max; {the largest character number in a font}
126 @!xmax_char=xchar_max; {|max_char|+1}
127 @!xxmax_char=xxchar_max;{|max_char|+2}
128 @!xmax_label=80001;{must be greater than |max_lig_steps|}
129 @!name_length=50; {a file name shouldn't be longer than this}
130 @!max_stack=50; {maximum depth of \.{DVI} stack in character packets}
131
132 @ Here are some macros for common programming idioms.
133
134 @d incr(#) == #:=#+1 {increase a variable by unity}
135 @d decr(#) == #:=#-1 {decrease a variable by unity}
136 @d do_nothing == {empty statement}
137 @d exit=10 {go here to leave a procedure}
138 @d not_found=45 {go here when you've found nothing}
139 @d return==goto exit {terminate a procedure call}
140 @f return==nil
141
142 @<Types...@>=
143 @!byte=0..255; {unsigned eight-bit quantity}
144
145 @* Virtual fonts. The idea behind \.{VF} files is that a general
146 interface mechanism is needed to switch between the myriad font
147 layouts provided by different suppliers of typesetting equipment.
148 Without such a mechanism, people must go to great lengths writing
149 inscrutable macros whenever they want to use typesetting conventions
150 based on one font layout in connection with actual fonts that have
151 another layout. This puts an extra burden on the typesetting system,
152 interfering with the other things it needs to do (like kerning,
153 hyphenation, and ligature formation).
154
155 These difficulties go away when we have a ``virtual font,''
156 i.e., a font that exists in a logical sense but not a physical sense.
157 A typesetting system like \TeX\ can do its job without knowing where the
158 actual characters come from; a device driver can then do its job by
159 letting a \.{VF} file tell what actual characters correspond to the
160 characters \TeX\ imagined were present. The actual characters
161 can be shifted and/or magnified and/or combined with other characters
162 from many different fonts. A virtual font can even make use of characters
163 from virtual fonts, including itself.
164
165 Virtual fonts also allow convenient character substitutions for proofreading
166 purposes, when fonts designed for one output device are unavailable on another.
167
168 @ A \.{VF} file is organized as a stream of 8-bit bytes, using conventions
169 borrowed from \.{DVI} and \.{PK} files. Thus, a device driver that knows
170 about \.{DVI} and \.{PK} format will already
171 contain most of the mechanisms necessary to process \.{VF} files.
172 We shall assume that \.{DVI} format is understood; the conventions in the
173 \.{DVI} documentation (see, for example, {\sl \TeX: The Program}, part 31)
174 are adopted here to define \.{VF} format.
175
176 A preamble
177 appears at the beginning, followed by a sequence of character definitions,
178 followed by a postamble. More precisely, the first byte of every \.{VF} file
179 must be the first byte of the following ``preamble command'':
180
181 \yskip\hang|pre| 247 |i[1]| |k[1]| |x[k]| |cs[4]| |ds[4]|.
182 Here |i| is the identification byte of \.{VF}, currently 202. The string
183 |x| is merely a comment, usually indicating the source of the \.{VF} file.
184 Parameters |cs| and |ds| are respectively the check sum and the design size
185 of the virtual font; they should match the first two words in the header of
186 the \.{TFM} file, as described below.
187
188 \yskip
189 After the |pre| command, the preamble continues with font definitions;
190 every font needed to specify ``actual'' characters in later
191 \\{set\_char} commands is defined here. The font definitions are
192 exactly the same in \.{VF} files as they are in \.{DVI} files, except
193 that the scaled size |s| is relative and the design size |d| is absolute:
194
195 \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
196 Define font |k|, where |0<=k<256|.
197
198 \yskip\hang|@!fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
199 Define font |k|, where |0<=k<65536|.
200
201 \yskip\hang|@!fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
202 Define font |k|, where |0<=k<@t$2^{24}$@>|.
203
204 \yskip\hang|@!fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
205 Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
206
207 \yskip\noindent
208 These font numbers |k| are ``local''; they have no relation to font numbers
209 defined in the \.{DVI} file that uses this virtual font. The dimension~|s|,
210 which represents the scaled size of the local font being defined,
211 is a |fix_word| relative to the design size of the virtual font.
212 Thus if the local font is to be used at the same size
213 as the design size of the virtual font itself, |s| will be the
214 integer value $2^{20}$. The value of |s| must be positive and less than
215 $2^{24}$ (thus less than 16 when considered as a |fix_word|).
216 The dimension~|d| is a |fix_word| in units of printer's points; hence it
217 is identical to the design size found in the corresponding \.{TFM} file.
218
219 @d id_byte=202
220
221 @<Glob...@>=
222 @!vf_file:packed file of byte;
223
224 @ The preamble is followed by zero or more character packets, where each
225 character packet begins with a byte that is $<243$. Character packets have
226 two formats, one long and one short:
227
228 \yskip\hang|long_char| 242 |pl[4]| |cc[4]| |tfm[4]| |dvi[pl]|. This long form
229 specifies a virtual character in the general case.
230
231 \yskip\hang|short_char0..short_char241|
232 |pl[1]| |cc[1]| |tfm[3]| |dvi[pl]|. This short form specifies a
233 virtual character in the common case
234 when |0<=pl<242| and |0<=cc<256| and $0\le|tfm|<2^{24}$.
235
236 \yskip\noindent
237 Here |pl| denotes the packet length following the |tfm| value; |cc| is
238 the character code; and |tfm| is the character width copied from the
239 \.{TFM} file for this virtual font. There should be at most one character
240 packet having any given |cc| code.
241
242 The |dvi| bytes are a sequence of complete \.{DVI} commands, properly
243 nested with respect to |push| and |pop|. All \.{DVI} operations are
244 permitted except |bop|, |eop|, and commands with opcodes |>=243|.
245 Font selection commands (|fnt_num0| through |fnt4|) must refer to fonts
246 defined in the preamble.
247
248 Dimensions that appear in the \.{DVI} instructions are analogous to
249 |fix_word| quantities; i.e., they are integer multiples of $2^{-20}$ times
250 the design size of the virtual font. For example, if the virtual font
251 has design size $10\,$pt, the \.{DVI} command to move down $5\,$pt
252 would be a \\{down} instruction with parameter $2^{19}$. The virtual font
253 itself might be used at a different size, say $12\,$pt; then that
254 \\{down} instruction would move down $6\,$pt instead. Each dimension
255 must be less than $2^{24}$ in absolute value.
256
257 Device drivers processing \.{VF} files treat the sequences of |dvi| bytes
258 as subroutines or macros, implicitly enclosing them with |push| and |pop|.
259 Each subroutine begins with |w=x=y=z=0|, and with current font~|f| the
260 number of the first-defined in the preamble (undefined if there's no
261 such font). After the |dvi| commands have been
262 performed, the |h| and~|v| position registers of \.{DVI} format and the
263 current font~|f| are restored to their former values;
264 then, if the subroutine has been invoked by a \\{set\_char} or \\{set}
265 command, |h|~is increased by the \.{TFM} width
266 (properly scaled)---just as if a simple character had been typeset.
267
268 @d long_char=242 {\.{VF} command for general character packet}
269 @d set_char_0=0 {\.{DVI} command to typeset character 0 and move right}
270 @d set1=128 {typeset a character and move right}
271 @d set_rule=132 {typeset a rule and move right}
272 @d put1=133 {typeset a character}
273 @d put_rule=137 {typeset a rule}
274 @d nop=138 {no operation}
275 @d push=141 {save the current positions}
276 @d pop=142 {restore previous positions}
277 @d right1=143 {move right}
278 @d w0=147 {move right by |w|}
279 @d w1=148 {move right and set |w|}
280 @d x0=152 {move right by |x|}
281 @d x1=153 {move right and set |x|}
282 @d down1=157 {move down}
283 @d y0=161 {move down by |y|}
284 @d y1=162 {move down and set |y|}
285 @d z0=166 {move down by |z|}
286 @d z1=167 {move down and set |z|}
287 @d fnt_num_0=171 {set current font to 0}
288 @d fnt1=235 {set current font}
289 @d xxx1=239 {extension to \.{DVI} primitives}
290 @d xxx4=242 {potentially long extension to \.{DVI} primitives}
291 @d fnt_def1=243 {define the meaning of a font number}
292 @d pre=247 {preamble}
293 @d post=248 {postamble beginning}
294 @d improper_DVI_for_VF==139,140,243,244,245,246,247,248,249,250,251,252,
295 253,254,255
296
297 @ The character packets are followed by a trivial postamble, consisting of
298 one or more bytes all equal to |post| (248). The total number of bytes
299 in the file should be a multiple of~4.
300
301 @* Font metric data.
302 The idea behind \.{TFM} files is that typesetting routines like \TeX\
303 need a compact way to store the relevant information about several
304 dozen fonts, and computer centers need a compact way to store the
305 relevant information about several hundred fonts. \.{TFM} files are
306 compact, and most of the information they contain is highly relevant,
307 so they provide a solution to the problem.
308
309 The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
310 Since the number of bytes is always a multiple of 4, we could
311 also regard the file as a sequence of 32-bit words; but \TeX\ uses the
312 byte interpretation, and so does \.{VFtoVP}. Note that the bytes
313 are considered to be unsigned numbers.
314
315 @<Glob...@>=
316 @!tfm_file:packed file of byte;
317
318 @ On some systems you may have to do something special to read a
319 packed file of bytes. For example, the following code didn't work
320 when it was first tried at Stanford, because packed files have to be
321 opened with a special switch setting on the \PASCAL\ that was used.
322 @^system dependencies@>
323
324 @<Set init...@>=
325 reset(tfm_file); reset(vf_file);
326
327 @ The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
328 integers that give the lengths of the various subsequent portions
329 of the file. These twelve integers are, in order:
330 $$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr
331 |@!lf|&length of the entire file, in words;\cr
332 |@!lh|&length of the header data, in words;\cr
333 |@!bc|&smallest character code in the font;\cr
334 |@!ec|&largest character code in the font;\cr
335 |@!nw|&number of words in the width table;\cr
336 |@!nh|&number of words in the height table;\cr
337 |@!nd|&number of words in the depth table;\cr
338 |@!ni|&number of words in the italic correction table;\cr
339 |@!nl|&number of words in the lig/kern table;\cr
340 |@!nk|&number of words in the kern table;\cr
341 |@!ne|&number of words in the extensible character table;\cr
342 |@!np|&number of font parameter words.\cr}}$$
343 They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
344 |ne<=256|, and
345 $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
346 Note that a font may contain as many as 256 characters (if |bc=0| and |ec=255|),
347 and as few as 0 characters (if |bc=ec+1|).
348
349 Incidentally, when two or more 8-bit bytes are combined to form an integer of
350 16 or more bits, the most significant bytes appear first in the file.
351 This is called BigEndian order.
352
353 @<Glob...@>=
354 @!ofm_level,
355 @!nco,@!ncw,@!npc,@!nki,@!nwi,@!nkf,@!nwf,@!nkr,@!nwr,@!nkg,@!nwg,@!nkp,@!nwp,
356 @!nkm,@!nwm,@!real_lf,@!nlw,@!neew,
357 @!lf,@!lh,@!bc,@!ec,@!nw,@!nh,@!nd,@!ni,@!nl,@!nk,@!ne,@!np,@!font_dir:integer;
358 {subfile sizes}
359 @!ofm_on:boolean;
360
361 @ @<Set init...@>=
362 ofm_on:=false; ofm_level:=-1; lf:=0; lh:=0;
363 nco:=0; ncw:=0; npc:=0; bc:=0; ec:=0; nw:=0; nh:=0; nd:=0; ni:=0;
364 nl:=0; nk:=0; ne:=0; np:=0;
365 nki:=0; nwi:=0; nkf:=0; nwf:=0;
366 nkm:=0; nwm:=0; real_lf:=0;
367 nkr:=0; nwr:=0; nkg:=0; nwg:=0;
368 nkp:=0; nwp:=0; font_dir:=0;
369
370 @ The rest of the \.{TFM} file may be regarded as a sequence of ten data
371 arrays having the informal specification
372 $$\def\arr$[#1]#2${\&{array} $[#1]$ \&{of} #2}
373 \vbox{\halign{\hfil\\{#}&$\,:\,$\arr#\hfil\cr
374 header&|[0..lh-1]stuff|\cr
375 char\_info&|[bc..ec]char_info_word|\cr
376 width&|[0..nw-1]fix_word|\cr
377 height&|[0..nh-1]fix_word|\cr
378 depth&|[0..nd-1]fix_word|\cr
379 italic&|[0..ni-1]fix_word|\cr
380 lig\_kern&|[0..nl-1]lig_kern_command|\cr
381 kern&|[0..nk-1]fix_word|\cr
382 exten&|[0..ne-1]extensible_recipe|\cr
383 param&|[1..np]fix_word|\cr}}$$
384 The most important data type used here is a |@!fix_word|, which is
385 a 32-bit representation of a binary fraction. A |fix_word| is a signed
386 quantity, with the two's complement of the entire word used to represent
387 negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
388 binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
389 the smallest is $-2048$. We will see below, however, that all but one of
390 the |fix_word| values will lie between $-16$ and $+16$.
391
392 @ The first data array is a block of header information, which contains
393 general facts about the font. The header must contain at least two words,
394 and for \.{TFM} files to be used with Xerox printing software it must
395 contain at least 18 words, allocated as described below. When different
396 kinds of devices need to be interfaced, it may be necessary to add further
397 words to the header block.
398
399 \yskip\hang|header[0]| is a 32-bit check sum that \TeX\ will copy into the
400 \.{DVI} output file whenever it uses the font. Later on when the \.{DVI}
401 file is printed, possibly on another computer, the actual font that gets
402 used is supposed to have a check sum that agrees with the one in the
403 \.{TFM} file used by \TeX. In this way, users will be warned about
404 potential incompatibilities. (However, if the check sum is zero in either
405 the font file or the \.{TFM} file, no check is made.) The actual relation
406 between this check sum and the rest of the \.{TFM} file is not important;
407 the check sum is simply an identification number with the property that
408 incompatible fonts almost always have distinct check sums.
409 @^check sum@>
410
411 \yskip\hang|header[1]| is a |fix_word| containing the design size of the
412 font, in units of \TeX\ points (7227 \TeX\ points = 254 cm). This number
413 must be at least 1.0; it is fairly arbitrary, but usually the design size
414 is 10.0 for a ``10 point'' font, i.e., a font that was designed to look
415 best at a 10-point size, whatever that really means. When a \TeX\ user
416 asks for a font `\.{at} $\delta$ \.{pt}', the effect is to override the
417 design size and replace it by $\delta$, and to multiply the $x$ and~$y$
418 coordinates of the points in the font image by a factor of $\delta$
419 divided by the design size. {\sl All other dimensions in the\/\ \.{TFM}
420 file are |fix_word|\kern-1pt\ numbers in design-size units.} Thus, for example,
421 the value of |param[6]|, one \.{em} or \.{\\quad}, is often the |fix_word|
422 value $2^{20}=1.0$, since many fonts have a design size equal to one em.
423 The other dimensions must be less than 16 design-size units in absolute
424 value; thus, |header[1]| and |param[1]| are the only |fix_word| entries in
425 the whole \.{TFM} file whose first byte might be something besides 0 or
426 255. @^design size@>
427
428 \yskip\hang|header[2..11]|, if present, contains 40 bytes that identify
429 the character coding scheme. The first byte, which must be between 0 and
430 39, is the number of subsequent ASCII bytes actually relevant in this
431 string, which is intended to specify what character-code-to-symbol
432 convention is present in the font. Examples are \.{ASCII} for standard
433 ASCII, \.{TeX text} for fonts like \.{cmr10} and \.{cmti9}, \.{TeX math
434 extension} for \.{cmex10}, \.{XEROX text} for Xerox fonts, \.{GRAPHIC} for
435 special-purpose non-alphabetic fonts, \.{UNSPECIFIED} for the default case
436 when there is no information. Parentheses should not appear in this name.
437 (Such a string is said to be in {\mc BCPL} format.)
438 @^coding scheme@>
439
440 \yskip\hang|header[12..16]|, if present, contains 20 bytes that name the
441 font family (e.g., \.{CMR} or \.{HELVETICA}), in {\mc BCPL} format.
442 This field is also known as the ``font identifier.''
443 @^family name@>
444 @^font identifier@>
445
446 \yskip\hang|header[17]|, if present, contains a first byte called the
447 |seven_bit_safe_flag|, then two bytes that are ignored, and a fourth byte
448 called the |face|. If the value of the fourth byte is less than 18, it has
449 the following interpretation as a ``weight, slope, and expansion'': Add 0
450 or 2 or 4 (for medium or bold or light) to 0 or 1 (for roman or italic) to
451 0 or 6 or 12 (for regular or condensed or extended). For example, 13 is
452 0+1+12, so it represents medium italic extended. A three-letter code
453 (e.g., \.{MIE}) can be used for such |face| data.
454
455 \yskip\hang|header[18..@twhatever@>]| might also be present; the individual
456 words are simply called |header[18]|, |header[19]|, etc., at the moment.
457
458 @ Next comes the |char_info| array, which contains one |char_info_word|
459 per character. Each |char_info_word| contains six fields packed into
460 four bytes as follows.
461
462 \yskip\hang first byte: |width_index| (8 bits)\par
463 \hang second byte: |height_index| (4 bits) times 16, plus |depth_index|
464 (4~bits)\par
465 \hang third byte: |italic_index| (6 bits) times 4, plus |tag|
466 (2~bits)\par
467 \hang fourth byte: |remainder| (8 bits)\par
468 \yskip\noindent
469 The actual width of a character is |width[width_index]|, in design-size
470 units; this is a device for compressing information, since many characters
471 have the same width. Since it is quite common for many characters
472 to have the same height, depth, or italic correction, the \.{TFM} format
473 imposes a limit of 16 different heights, 16 different depths, and
474 64 different italic corrections.
475
476 Incidentally, the relation |width[0]=height[0]=depth[0]=italic[0]=0|
477 should always hold, so that an index of zero implies a value of zero.
478 The |width_index| should never be zero unless the character does
479 not exist in the font, since a character is valid if and only if it lies
480 between |bc| and |ec| and has a nonzero |width_index|.
481
482 @ The |tag| field in a |char_info_word| has four values that explain how to
483 interpret the |remainder| field.
484
485 \yskip\hang|tag=0| (|no_tag|) means that |remainder| is unused.\par
486 \hang|tag=1| (|lig_tag|) means that this character has a ligature/kerning
487 program starting at |lig_kern[remainder]|.\par
488 \hang|tag=2| (|list_tag|) means that this character is part of a chain of
489 characters of ascending sizes, and not the largest in the chain. The
490 |remainder| field gives the character code of the next larger character.\par
491 \hang|tag=3| (|ext_tag|) means that this character code represents an
492 extensible character, i.e., a character that is built up of smaller pieces
493 so that it can be made arbitrarily large. The pieces are specified in
494 |exten[remainder]|.\par
495
496 @d no_tag=0 {vanilla character}
497 @d lig_tag=1 {character has a ligature/kerning program}
498 @d list_tag=2 {character has a successor in a charlist}
499 @d ext_tag=3 {character is extensible}
500
501 @ The |lig_kern| array contains instructions in a simple programming language
502 that explains what to do for special letter pairs. Each word is a
503 |lig_kern_command| of four bytes.
504
505 \yskip\hang first byte: |skip_byte|, indicates that this is the final program
506 step if the byte is 128 or more, otherwise the next step is obtained by
507 skipping this number of intervening steps.\par
508 \hang second byte: |next_char|, ``if |next_char| follows the current character,
509 then perform the operation and stop, otherwise continue.''\par
510 \hang third byte: |op_byte|, indicates a ligature step if less than~128,
511 a kern step otherwise.\par
512 \hang fourth byte: |remainder|.\par
513 \yskip\noindent
514 In a kern step, an
515 additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
516 between the current character and |next_char|. This amount is
517 often negative, so that the characters are brought closer together
518 by kerning; but it might be positive.
519
520 There are eight kinds of ligature steps, having |op_byte| codes $4a+2b+c$ where
521 $0\le a\le b+c$ and $0\le b,c\le1$. The character whose code is
522 |remainder| is inserted between the current character and |next_char|;
523 then the current character is deleted if $b=0$, and |next_char| is
524 deleted if $c=0$; then we pass over $a$~characters to reach the next
525 current character (which may have a ligature/kerning program of its own).
526
527 Notice that if $a=0$ and $b=1$, the current character is unchanged; if
528 $a=b$ and $c=1$, the current character is changed but the next character is
529 unchanged. \.{VFtoVP} will check to see that infinite loops are avoided.
530
531 If the very first instruction of the |lig_kern| array has |skip_byte=255|,
532 the |next_char| byte is the so-called right boundary character of this font;
533 the value of |next_char| need not lie between |bc| and~|ec|.
534 If the very last instruction of the |lig_kern| array has |skip_byte=255|,
535 there is a special ligature/kerning program for a left boundary character,
536 beginning at location |256*op_byte+remainder|.
537 The interpretation is that \TeX\ puts implicit boundary characters
538 before and after each consecutive string of characters from the same font.
539 These implicit characters do not appear in the output, but they can affect
540 ligatures and kerning.
541
542 If the very first instruction of a character's |lig_kern| program has
543 |skip_byte>128|, the program actually begins in location
544 |256*op_byte+remainder|. This feature allows access to large |lig_kern|
545 arrays, because the first instruction must otherwise
546 appear in a location |<=255|.
547
548 Any instruction with |skip_byte>128| in the |lig_kern| array must have
549 |256*op_byte+remainder<nl|. If such an instruction is encountered during
550 normal program execution, it denotes an unconditional halt; no ligature
551 command is performed.
552
553 @d stop_flag=128 {value indicating `\.{STOP}' in a lig/kern program}
554 @d kern_flag=128 {op code for a kern step}
555
556 @ Extensible characters are specified by an |extensible_recipe|,
557 which consists of four bytes called |top|, |mid|,
558 |bot|, and |rep| (in this order). These bytes are the character codes
559 of individual pieces used to build up a large symbol.
560 If |top|, |mid|, or |bot| are zero,
561 they are not present in the built-up result. For example, an extensible
562 vertical line is like an extensible bracket, except that the top and
563 bottom pieces are missing.
564
565
566 @ The final portion of a \.{TFM} file is the |param| array, which is another
567 sequence of |fix_word| values.
568
569 \yskip\hang|param[1]=@!slant| is the amount of italic slant, which is used
570 to help position accents. For example, |slant=.25| means that when you go
571 up one unit, you also go .25 units to the right. The |slant| is a pure
572 number; it's the only |fix_word| other than the design size itself that is
573 not scaled by the design size.
574
575 \hang|param[2]=space| is the normal spacing between words in text.
576 Note that character |" "| in the font need not have anything to do with
577 blank spaces.
578
579 \hang|param[3]=space_stretch| is the amount of glue stretching between words.
580
581 \hang|param[4]=space_shrink| is the amount of glue shrinking between words.
582
583 \hang|param[5]=x_height| is the height of letters for which accents don't
584 have to be raised or lowered.
585
586 \hang|param[6]=quad| is the size of one em in the font.
587
588 \hang|param[7]=extra_space| is the amount added to |param[2]| at the
589 ends of sentences.
590
591 When the character coding scheme is \.{TeX math symbols}, the font is
592 supposed to have 15 additional parameters called |num1|, |num2|, |num3|,
593 |denom1|, |denom2|, |sup1|, |sup2|, |sup3|, |sub1|, |sub2|, |supdrop|,
594 |subdrop|, |delim1|, |delim2|, and |axis_height|, respectively. When the
595 character coding scheme is \.{TeX math extension}, the font is supposed to
596 have six additional parameters called |default_rule_thickness| and
597 |big_op_spacing1| through |big_op_spacing5|.
598
599 @ So that is what \.{TFM} files hold. The next question is, ``What about
600 \.{VPL} files?'' A complete answer to that question appears in the
601 documentation of the companion program, \.{VPtoVF}, so it will not
602 be repeated here. Suffice it to say that a \.{VPL} file is an ordinary
603 \PASCAL\ text file, and that the output of \.{VFtoVP} uses only a
604 subset of the possible constructions that might appear in a \.{VPL} file.
605 Furthermore, hardly anybody really wants to look at the formal
606 definition of \.{VPL} format, because it is almost self-explanatory when
607 you see an example or two.
608
609 @<Glob...@>=
610 @!vpl_file:text;
611
612 @ @<Set init...@>=
613 rewrite(vpl_file);
614
615 @* Unpacking the TFM file.
616 The first thing \.{VFtoVP} does is read the entire |tfm_file| into an array of
617 bytes, |tfm[0..(4*lf-1)]|.
618
619 @<Types...@>=
620 @!index=0..tfm_size; {address of a byte in |tfm|}
621 @!char_type=0..char_max;
622 @!xchar_type=0..xchar_max;
623 @!xxchar_type=0..xxchar_max;
624 @!xxxchar_type=0..xxxchar_max;
625
626 @ @<Glob...@>=
627 @!tfm:array [-1000..tfm_size] of byte; {the input data all goes here}
628 {the negative addresses avoid range checks for invalid characters}
629 @!top_char,@!top_width,@!top_height,@!top_depth,@!top_italic:integer;
630 @!start_ptr,@!check_sum,@!design_size,@!scheme,@!family,@!random_word:integer;
631 @!header_length,@!char_ptr,@!copies,@!j:integer;
632
633 @ The input may, of course, be all screwed up and not a \.{TFM} file
634 at all. So we begin cautiously.
635
636 @d abort(#)==begin print_ln(#);
637 print_ln('Sorry, but I can''t go on; are you sure this is a OFM?');
638 goto final_end;
639 end
640
641 @<Read the whole font metric file@>=
642 read(tfm_file,tfm[0]);
643 if tfm[0]>127 then abort('The first byte of the input file exceeds 127!');
644 @.The first byte...@>
645 if eof(tfm_file) then abort('The input file is only one byte long!');
646 @.The input...one byte long@>
647 read(tfm_file,tfm[1]); lf:=tfm[0]*@'400+tfm[1];
648 if lf=0 then
649 begin for i:=2 to 7 do
650 begin if eof(tfm_file) then
651 abort('The input file is too short to designate its length!');
652 read(tfm_file, tfm[i]);
653 end;
654 ofm_on := true; ofm_level := tfm[2]*@"100+tfm[3];
655 if ofm_level>1 then
656 abort('OFMLEVEL ',ofm_level:1,' not supported, must be 0 or 1!');
657 @.OFMLEVEL...must be 0 or 1@>
658 if tfm[4]>127 then abort('The fifth byte of the input file exceeds 127!');
659 @.The fifth byte...@>
660 lf := tfm[4]*@"1000000 + tfm[5]*@"10000 + tfm[6]*@"100 + tfm[7];
661 end
662 else begin ofm_on := false;
663 end;
664 case ofm_level of
665 -1: begin start_ptr:=2; check_sum:=24; end;
666 0: begin start_ptr:=8; check_sum:=56; end;
667 1: begin start_ptr:=8; check_sum:=116; end;
668 end;
669 design_size:=check_sum+4;
670 scheme:=design_size+4;
671 family:=scheme+40;
672 random_word:=family+20;
673 if lf=0 then
674 abort('The file claims to have length zero, but that''s impossible!');
675 @.The file claims...@>
676 if 4*lf-1>tfm_size then abort('The file is bigger than I can handle!');
677 @.The file is bigger...@>
678 for tfm_ptr:=start_ptr to 4*lf-1 do
679 begin if eof(tfm_file) then
680 abort('The file has fewer bytes than it claims!');
681 @.The file has fewer bytes...@>
682 read(tfm_file,tfm[tfm_ptr]);
683 end;
684 if not eof(tfm_file) then
685 begin print_ln('There''s some extra junk at the end of the OFM file,');
686 @.There's some extra junk...@>
687 print_ln('but I''ll proceed as if it weren''t there.');
688 end
689
690 @ After the file has been read successfully, we look at the subfile sizes
691 to see if they check out.
692
693 @d eval_two_bytes(#)==begin if tfm[tfm_ptr]>127 then
694 abort('One of the subfile sizes is negative!');
695 @.One of the subfile sizes...@>
696 #:=tfm[tfm_ptr]*@'400+tfm[tfm_ptr+1];
697 tfm_ptr:=tfm_ptr+2;
698 end
699 @d eval_four_bytes(#)==begin if tfm[tfm_ptr]>127 then
700 abort('One of the subfile sizes is negative!');
701 @.One of the subfile sizes...@>
702 #:=tfm[tfm_ptr]*@"1000000+tfm[tfm_ptr+1]*@"10000+
703 tfm[tfm_ptr+2]*@"100+tfm[tfm_ptr+3];
704 tfm_ptr:=tfm_ptr+4;
705 end
706
707 @<Set subfile sizes |lh|, |bc|, \dots, |np|@>=
708 begin
709 if not ofm_on then begin
710 tfm_ptr:=2;
711 eval_two_bytes(lh);
712 eval_two_bytes(bc);
713 eval_two_bytes(ec);
714 eval_two_bytes(nw);
715 eval_two_bytes(nh);
716 eval_two_bytes(nd);
717 eval_two_bytes(ni);
718 eval_two_bytes(nl);
719 eval_two_bytes(nk);
720 eval_two_bytes(ne);
721 eval_two_bytes(np);
722 ncw:=(ec-bc+1);
723 nlw:=nl;
724 neew:=ne;
725 header_length:=6;
726 top_char:=255;
727 top_width:=255;
728 top_height:=15;
729 top_depth:=15;
730 top_italic:=63;
731 end
732 else begin
733 tfm_ptr:=8;
734 eval_four_bytes(lh);
735 eval_four_bytes(bc);
736 eval_four_bytes(ec);
737 eval_four_bytes(nw);
738 eval_four_bytes(nh);
739 eval_four_bytes(nd);
740 eval_four_bytes(ni);
741 eval_four_bytes(nl);
742 eval_four_bytes(nk);
743 eval_four_bytes(ne);
744 eval_four_bytes(np);
745 eval_four_bytes(font_dir);
746 nlw:=2*nl;
747 neew:=2*ne;
748 top_char:=char_max;
749 top_width:=char_max;
750 top_height:=255;
751 top_depth:=255;
752 top_italic:=255;
753 if ofm_level=0 then begin
754 header_length:=14;
755 ncw:=2*(ec-bc+1);
756 end
757 else begin
758 header_length:=29;
759 eval_four_bytes(nco);
760 eval_four_bytes(ncw);
761 eval_four_bytes(npc);
762 eval_four_bytes(nki); {Kinds of font ivalues}
763 eval_four_bytes(nwi); {Words of font ivalues}
764 eval_four_bytes(nkf); {Kinds of font fvalues}
765 eval_four_bytes(nwf); {Words of font fvalues}
766 eval_four_bytes(nkm); {Kinds of font mvalues}
767 eval_four_bytes(nwm); {Words of font mvalues}
768 eval_four_bytes(nkr); {Kinds of font rules}
769 eval_four_bytes(nwr); {Words of font rules}
770 eval_four_bytes(nkg); {Kinds of font glues}
771 eval_four_bytes(nwg); {Words of font glues}
772 eval_four_bytes(nkp); {Kinds of font penalties}
773 eval_four_bytes(nwp); {Words of font penalties}
774 end;
775 end;
776 if lf<>(header_length+lh+ncw+nw+nh+nd+ni+nlw+nk+neew+np+
777 nki+nwi+nkf+nwf+nkm+nwm+nkr+nwr+nkg+nwg+nkp+nwp) then
778 abort('Subfile sizes don''t add up to the stated total!');
779 @.Subfile sizes don't add up...@>
780 if lh<2 then abort('The header length is only ',lh:1,'!');
781 @.The header length...@>
782 if nl>lig_size then
783 abort('The lig/kern program is longer than I can handle!');
784 @.The lig/kern program...@>
785 if (bc>ec+1)or(ec>top_char) then abort('The character code range ',
786 @.The character code range...@>
787 bc:1,'..',ec:1,' is illegal!');
788 if ec>max_char then
789 abort('Character ',ec:1,'is too large. Ask a wizard to enlarge me.');
790 if (nw=0)or(nh=0)or(nd=0)or(ni=0) then
791 abort('Incomplete subfiles for character dimensions!');
792 @.Incomplete subfiles...@>
793 if ne>(top_char+1) then abort('There are ',ne:1,' extensible recipes!');
794 @.There are ... recipes@>
795 end
796
797 @ Once the input data successfully passes these basic checks,
798 \.{VFtoVP} believes that it is a \.{TFM} file, and the conversion
799 to \.{VPL} format will take place. Access to the various subfiles
800 is facilitated by computing the following base addresses. For example,
801 the |char_info| for character |c| in a \.{TFM} file will start in location
802 |4*(char_base+c)| of the |tfm| array.
803
804 @<Globals...@>=
805 @!ivalues_start,@!fvalues_start,@!mvalues_start,
806 @!rules_start,@!glues_start,@!penalties_start:
807 integer;
808 @!ivalues_base,@!fvalues_base,@!mvalues_base,
809 @!rules_base,@!glues_base,@!penalties_base:
810 integer;
811 @!char_base,@!width_base,@!height_base,@!depth_base,@!italic_base: integer;
812 @!lig_kern_base,@!kern_base,@!exten_base,@!param_base:integer;
813 {base addresses for the subfiles}
814 @!char_start:array [0..max_char] of integer;
815 @!bytes_per_entry:integer;
816
817 @ @<Compute the base addresses@>=
818 begin
819 ivalues_start:=header_length+lh;
820 fvalues_start:=ivalues_start+nki;
821 mvalues_start:=fvalues_start+nkf;
822 rules_start:=mvalues_start+nkm;
823 glues_start:=rules_start+nkr;
824 penalties_start:=glues_start+nkg;
825 ivalues_base:=penalties_start+nkp;
826 fvalues_base:=ivalues_base+nwi;
827 mvalues_base:=fvalues_base+nwf;
828 rules_base:=mvalues_base+nwm;
829 glues_base:=rules_base+nwr;
830 penalties_base:=glues_base+nwg;
831 char_base:=penalties_base+nwp;
832 bytes_per_entry:=(12 + 2*npc) div 4 * 4;
833 if not ofm_on then begin
834 for i:=bc to ec do begin
835 char_start[i]:=4*char_base+4*(i-bc);
836 end;
837 end
838 else if ofm_level=0 then begin
839 for i:=bc to ec do begin
840 char_start[i]:=4*char_base+8*(i-bc);
841 end;
842 end
843 else begin
844 char_ptr:=4*char_base;
845 i:=bc;
846 while i<=ec do begin
847 copies:=1+256*tfm[char_ptr+8]+tfm[char_ptr+9];
848 for j:=1 to copies do begin
849 char_start[i]:=char_ptr;
850 i:=i+1;
851 end;
852 char_ptr:=char_ptr + bytes_per_entry;
853 end;
854 if char_ptr<>(4*(char_base+ncw)) then
855 abort('Length of char info table does not correspond to specification');
856 end;
857 width_base:=char_base+ncw;
858 height_base:=width_base+nw;
859 depth_base:=height_base+nh;
860 italic_base:=depth_base+nd;
861 lig_kern_base:=italic_base+ni;
862 kern_base:=lig_kern_base+nlw;
863 exten_base:=kern_base+nk;
864 param_base:=exten_base+neew-1;
865 end
866
867 @ Of course we want to define macros that suppress the detail of how the
868 font information is actually encoded. Each word will be referred to by
869 the |tfm| index of its first byte. For example, if |c| is a character
870 code between |bc| and |ec|, then |tfm[char_info(c)]| will be the
871 first byte of its |char_info|, i.e., the |width_index|; furthermore
872 |width(c)| will point to the |fix_word| for |c|'s width.
873
874 @d char_info(#)==char_start[#]
875 @d nonexistent(#)==((#<bc)or(#>ec)or(width_index(#)=0))
876 @d width(#)==4*(width_base+width_index(#))
877 @d height(#)==4*(height_base+height_index(#))
878 @d depth(#)==4*(depth_base+depth_index(#))
879 @d italic(#)==4*(italic_base+italic_index(#))
880 @d kern(#)==4*(kern_base+#) {here \#\ is an index, not a character}
881 @d param(#)==4*(param_base+#) {likewise}
882
883 @p function width_index(c:char_type):integer;
884 begin if not ofm_on then
885 width_index:=tfm[char_info(c)]
886 else
887 width_index:=256*tfm[char_info(c)]+tfm[char_info(c)+1];
888 end;
889
890 function height_index(c:char_type):integer;
891 begin if not ofm_on then
892 height_index:=tfm[char_info(c)+1] div 16
893 else
894 height_index:=tfm[char_info(c)+2];
895 end;
896
897 function depth_index(c:char_type):integer;
898 begin if not ofm_on then
899 depth_index:=tfm[char_info(c)+1] mod 16
900 else
901 depth_index:=tfm[char_info(c)+3];
902 end;
903
904 function italic_index(c:char_type):integer;
905 begin if not ofm_on then
906 italic_index:=tfm[char_info(c)+2] div 4
907 else
908 italic_index:=tfm[char_info(c)+4];
909 end;
910
911 function tag(c:char_type):integer;
912 begin if not ofm_on then
913 tag:=tfm[char_info(c)+2] mod 4
914 else
915 tag:=tfm[char_info(c)+5] mod 4;
916 end;
917
918 procedure set_no_tag(c:char_type);
919 begin if not ofm_on then
920 tfm[char_info(c)+2] := (tfm[char_info(c)+2] div 64)*64 + no_tag
921 else
922 tfm[char_info(c)+5] := (tfm[char_info(c)+5] div 64)*64 + no_tag;
923 end;
924
925 procedure check_unused(c:char_type);
926 var @!x:integer;
927 begin if ofm_level=1 then x:=tfm[char_info(c)+5] div 8
928 else x:=tfm[char_info(c)+5] div 4;
929 if x<>0 then begin
930 if ofm_level=1 then x:=tfm[char_info(c)+5] mod 8
931 else x:=tfm[char_info(c)+5] mod 4;
932 tfm[char_info(c)+5]:=x;
933 perfect:=false; if chars_on_line>0 then print_ln(' ');
934 chars_on_line:=0; print_ln('Ignoring non-zero unused char info bits');
935 @.Ignoring non-zero unused...@>
936 end;
937 end;
938
939 function ctag(c:char_type):boolean;
940 begin if not (ofm_level=1) then
941 ctag:=false
942 else
943 ctag:=tfm[char_info(c)+5] div 4 mod 2;
944 end;
945
946 procedure set_no_ctag(c:char_type);
947 begin if not (ofm_level=1) then
948 tfm[char_info(c)+5] :=
949 tfm[char_info(c)+5] div 8 * 8 + tfm[char_info(c)+5] mod 4;
950 end;
951
952 function no_repeats(c:char_type):integer;
953 begin if ofm_level<=0 then
954 no_repeats:=0
955 else
956 no_repeats:=256*tfm[char_info(c)+8]+tfm[char_info(c)+9];
957 end;
958
959 function char_param(c:char_type; i:integer):integer;
960 begin
961 char_param:=256*tfm[char_info(c)+2*i+10]+tfm[char_info(c)+2*i+11];
962 end;
963
964 function rremainder(c:char_type):integer;
965 begin if not ofm_on then
966 rremainder:=tfm[char_info(c)+3]
967 else
968 rremainder:=256*tfm[char_info(c)+6]+tfm[char_info(c)+7];
969 end;
970
971 function lig_step(c:char_type):integer;
972 begin if not ofm_on then
973 lig_step:=4*(lig_kern_base+c)
974 else
975 lig_step:=4*(lig_kern_base+2*c);
976 end;
977
978 function exten(c:char_type):integer;
979 begin if not ofm_on then
980 exten:=4*(exten_base+rremainder(c))
981 else
982 exten:=4*(exten_base+2*rremainder(c));
983 end;
984
985 function l_skip_byte(c:integer):integer;
986 begin if not ofm_on then
987 l_skip_byte:=tfm[c]
988 else
989 l_skip_byte:=256*tfm[c]+tfm[c+1];
990 end;
991
992 procedure set_l_skip_byte(c:integer; newc:integer);
993 begin if not ofm_on then
994 tfm[c]:=newc
995 else begin
996 tfm[c]:=newc div 256;
997 tfm[c+1]:=newc mod 256
998 end
999 end;
1000
1001 function l_next_char(c:integer):integer;
1002 begin if not ofm_on then
1003 l_next_char:=tfm[c+1]
1004 else
1005 l_next_char:=256*tfm[c+2]+tfm[c+3];
1006 end;
1007
1008 procedure set_l_next_char(c:integer; newc:char_type);
1009 begin if not ofm_on then
1010 tfm[c+1]:=newc
1011 else begin
1012 tfm[c+2]:=newc div 256;
1013 tfm[c+3]:=newc mod 256
1014 end
1015 end;
1016
1017 function l_op_byte(c:integer):integer;
1018 begin if not ofm_on then
1019 l_op_byte:=tfm[c+2]
1020 else
1021 l_op_byte:=256*tfm[c+4]+tfm[c+5];
1022 end;
1023
1024 procedure set_l_op_byte(c:integer; newc:integer);
1025 begin if not ofm_on then
1026 tfm[c+2]:=newc
1027 else begin
1028 tfm[c+2]:=newc div 256;
1029 tfm[c+3]:=newc mod 256
1030 end
1031 end;
1032
1033 function l_remainder(c:integer):integer;
1034 begin if not ofm_on then
1035 l_remainder:=tfm[c+3]
1036 else
1037 l_remainder:=256*tfm[c+6]+tfm[c+7];
1038 end;
1039
1040 procedure set_l_remainder(c:integer; newc:char_type);
1041 begin if not ofm_on then
1042 tfm[c+3]:=newc
1043 else begin
1044 tfm[c+6]:=newc div 256;
1045 tfm[c+7]:=newc mod 256
1046 end
1047 end;
1048
1049 @ One of the things we would like to do is take cognizance of fonts whose
1050 character coding scheme is \.{TeX math symbols} or \.{TeX math extension};
1051 we will set the |font_type| variable to one of the three choices
1052 |vanilla|, |mathsy|, or |mathex|.
1053
1054 @d vanilla=0 {not a special scheme}
1055 @d mathsy=1 {\.{TeX math symbols} scheme}
1056 @d mathex=2 {\.{TeX math extension} scheme}
1057
1058 @<Glob...@>=
1059 @!font_type:vanilla..mathex; {is this font special?}
1060
1061 @* Unpacking the VF file.
1062 Once the \.{TFM} file has been brought into memory, \.{VFtoVP} completes
1063 the input phase by reading the \.{VF} information into another array of bytes.
1064 In this case we don't store all the data; we check the redundant bytes
1065 for consistency with their \.{TFM} counterparts, and we partially decode
1066 the packets.
1067
1068 @<Glob...@>=
1069 @!vf:array[0..vf_size] of byte; {the \.{VF} input data goes here}
1070 @!vc:array[0..vc_size] of char_type; {the valid characters from local fonts}
1071 @!font_number:array[0..max_fonts] of integer; {local font numbers}
1072 @!font_start:array[0..max_fonts] of 0..vf_size; {font info}
1073 @!font_chars:array[0..max_fonts] of 0..vc_size; {font info}
1074 @!font_ptr:0..max_fonts; {number of local fonts}
1075 @!packet_start,@!packet_end:array[char_type] of 0..vf_size;
1076 {character packet boundaries}
1077 @!packet_found:boolean; {at least one packet has appeared}
1078 @!temp_byte:byte;@+@!reg_count:integer; {registers for simple calculations}
1079 @!real_dsize:real; {the design size, converted to floating point}
1080 @!pl:integer; {packet length}
1081 @!vf_ptr:0..vf_size; {first unused location in |vf|}
1082 @!vc_ptr:0..vc_size; {first unused location in |vc|}
1083 @!vf_count:integer; {number of bytes read from |vf_file|}
1084
1085 @ Again we cautiously verify that we've been given decent data.
1086
1087 @d read_vf(#)==read(vf_file,#)
1088 @d vf_abort(#)==
1089 begin print_ln(#);
1090 print_ln('Sorry, but I can''t go on; are you sure this is a OVF?');
1091 goto final_end;
1092 end
1093
1094 @<Read the whole \.{VF} file@>=
1095 read_vf(temp_byte);
1096 if temp_byte<>pre then vf_abort('The first byte isn''t `pre''!');
1097 @.The first byte...@>
1098 @<Read the preamble command@>;
1099 @<Read and store the font definitions and character packets@>;
1100 @<Read and verify the postamble@>
1101
1102 @ @d vf_store(#)==@t@>@;@/
1103 if vf_ptr+#>=vf_size then vf_abort('The file is bigger than I can handle!');
1104 @.The file is bigger...@>
1105 for k:=vf_ptr to vf_ptr+#-1 do
1106 begin if eof(vf_file) then vf_abort('The file ended prematurely!');
1107 @.The file ended prematurely@>
1108 read_vf(vf[k]);
1109 end;
1110 vf_count:=vf_count+#; vf_ptr:=vf_ptr+#
1111
1112 @<Read the preamble command@>=
1113 if eof(vf_file) then vf_abort('The input file is only one byte long!');
1114 @.The input...one byte long@>
1115 read_vf(temp_byte);
1116 if temp_byte<>id_byte then vf_abort('Wrong OVF version number in second byte!');
1117 @.Wrong OVF version...@>
1118 if eof(vf_file) then vf_abort('The input file is only two bytes long!');
1119 read_vf(temp_byte); {read the length of introductory comment}
1120 vf_count:=11; vf_ptr:=0; vf_store(temp_byte);
1121 for k:=0 to vf_ptr-1 do print(xchr[vf[k]]);
1122 print_ln(' '); reg_count:=0;
1123 for k:=0 to 7 do
1124 begin if eof(vf_file) then vf_abort('The file ended prematurely!');
1125 @.The file ended prematurely@>
1126 read_vf(temp_byte);
1127 if temp_byte=tfm[check_sum+k] then incr(reg_count);
1128 end;
1129 real_dsize:=(((tfm[design_size]*256+tfm[design_size+1])*256+tfm[design_size+2])
1130 *256+tfm[design_size+3])/@'4000000;
1131 if reg_count<>8 then
1132 begin print_ln('Check sum and/or design size mismatch.');
1133 @.Check sum...mismatch@>
1134 print_ln('Data from OFM file will be assumed correct.');
1135 end
1136
1137 @ @<Read and store the font definitions and character packets@>=
1138 for k:=0 to char_max do packet_start[k]:=vf_size;
1139 font_ptr:=0; packet_found:=false; font_start[0]:=vf_ptr;
1140 vc_ptr:=0; font_chars[0]:=vc_ptr;
1141 repeat if eof(vf_file) then
1142 begin print_ln('File ended without a postamble!'); temp_byte:=post;
1143 @.File ended without a postamble@>
1144 end
1145 else begin read_vf(temp_byte); incr(vf_count);
1146 if temp_byte<>post then
1147 if temp_byte>long_char then @<Read and store a font definition@>
1148 else @<Read and store a character packet@>;
1149 end;
1150 until temp_byte=post
1151
1152 @ @<Read and verify the postamble@>=
1153 while (temp_byte=post)and not eof(vf_file) do
1154 begin read_vf(temp_byte); incr(vf_count);
1155 end;
1156 if not eof(vf_file) then
1157 begin print_ln('There''s some extra junk at the end of the OVF file.');
1158 @.There's some extra junk...@>
1159 print_ln('I''ll proceed as if it weren''t there.');
1160 end;
1161 if vf_count mod 4 <> 0 then
1162 print_ln('VF data not a multiple of 4 bytes')
1163 @.VF data not a multiple of 4 bytes@>
1164
1165 @ @<Read and store a font definition@>=
1166 begin if packet_found or(temp_byte>=pre) then
1167 vf_abort('Illegal byte ',temp_byte:1,' at beginning of character packet!');
1168 @.Illegal byte...@>
1169 font_number[font_ptr]:=vf_read(temp_byte-fnt_def1+1);
1170 if font_ptr=max_fonts then vf_abort('I can''t handle that many fonts!');
1171 @.I can't handle that many fonts@>
1172 vf_store(14); {|c[4]| |s[4]| |d[4]| |a[1]| |l[1]|}
1173 if vf[vf_ptr-10]>0 then {|s| is negative or exceeds $2^{24}-1$}
1174 vf_abort('Mapped font size is too big!');
1175 @.Mapped font size...big@>
1176 a:=vf[vf_ptr-2]; l:=vf[vf_ptr-1]; vf_store(a+l); {|n[a+l]|}
1177 @<Print the name of the local font@>;
1178 @<Read the local font's \.{TFM} file and record the characters it contains@>;
1179 incr(font_ptr); font_start[font_ptr]:=vf_ptr; font_chars[font_ptr]:=vc_ptr;
1180 end
1181
1182 @ The font area may need to be separated from the font name on some systems.
1183 Here we simply reproduce the font area and font name (with no space
1184 or punctuation between them).
1185 @^system dependencies@>
1186
1187 @<Print the name...@>=
1188 print('MAPFONT ',font_ptr:1,': ');
1189 for k:=font_start[font_ptr]+14 to vf_ptr-1 do print(xchr[vf[k]]);
1190 k:=font_start[font_ptr]+5;
1191 print_ln(' at ',(((vf[k]*256+vf[k+1])*256+vf[k+2])/@'4000000)*real_dsize:2:2,
1192 'pt')
1193
1194 @ Now we must read in another \.{TFM} file. But this time we needn't be so
1195 careful, because we merely want to discover which characters are present.
1196 The next few sections of the program are copied pretty much verbatim from
1197 \.{DVItype}, so that system-dependent modifications can be copied from existing
1198 software.
1199
1200 It turns out to be convenient to read four bytes at a time, when we are
1201 inputting from the local \.{TFM} files. The input goes into global variables
1202 |b0|, |b1|, |b2|, and |b3|, with |b0| getting the first byte and |b3|
1203 the fourth.
1204
1205 @<Glob...@>=
1206 @!a:integer; {length of the area/directory spec}
1207 @!l:integer; {length of the font name proper}
1208 @!cur_name:packed array[1..name_length] of char; {external name,
1209 with no lower case letters}
1210 @!b0,@!b1,@!b2,@!b3: byte; {four bytes input at once}
1211 @!font_lh:integer; {header length of current local font}
1212 @!font_bc,@!font_ec:integer; {character range of current local font}
1213 @!font_ofm_level,@!font_extra_words:integer;
1214 @!font_kprime,@!font_j,@!font_wd:integer;
1215
1216 @ The |read_tfm_word| procedure sets |b0| through |b3| to the next
1217 four bytes in the current \.{TFM} file.
1218 @^system dependencies@>
1219
1220 @d read_tfm(#)==if eof(tfm_file) then #:=0@+else read(tfm_file,#)
1221
1222 @p procedure read_tfm_word;
1223 begin read_tfm(b0); read_tfm(b1);
1224 read_tfm(b2); read_tfm(b3);
1225 end;
1226
1227 @ We use the |vc| array to store a list of all valid characters in the
1228 local font, beginning at location |font_chars[f]|.
1229 In \.{VFtoVP} the |vf| array is used for that purpose, but that is not
1230 possible for \.{OVF2OVP} with characters in the range |0..char_max|.
1231
1232 @<Read the local font's \.{TFM} file...@>=
1233 @<Move font name into the |cur_name| string@>;
1234 reset(tfm_file,cur_name);
1235 @^system dependencies@>
1236 if eof(tfm_file) then
1237 print_ln('---not loaded, font metric file can''t be opened!')
1238 @.font metric file can\'t be opened@>
1239 else begin font_bc:=0; font_ec:=xchar_max; {will cause error if not modified soon}
1240 @<Read past the header@>;
1241 if font_bc<=font_ec then
1242 if font_ec>char_max then print_ln('---not loaded, bad font metric file!')
1243 @.bad font metric file@>
1244 else @<Read |char_info| entries@>;
1245 if eof(tfm_file) then
1246 print_ln('---trouble is brewing, font metric file ended too soon!');
1247 @.trouble is brewing...@>
1248 end;
1249 incr(vc_ptr) {leave space for character search later}
1250
1251 @ @<Read past the header@>=
1252 read_tfm_word;
1253 if (b0*256+b1)<>0 then begin {TFM file}
1254 font_ofm_level:=-1;
1255 font_lh:=b2*256+b3;
1256 read_tfm_word; font_bc:=b0*256+b1; font_ec:=b2*256+b3;
1257 if font_ec<font_bc then font_bc:=font_ec+1;
1258 read_tfm_word;
1259 for k:=1 to 3+font_lh do begin
1260 read_tfm_word;
1261 if k=4 then @<Check the check sum@>;
1262 if k=5 then @<Check the design size@>;
1263 end;
1264 end
1265 else begin
1266 font_ofm_level:=b2*256+b3;
1267 read_tfm_word;
1268 read_tfm_word; font_lh:=((b0*256+b1)*256+b2)*256+b3;
1269 read_tfm_word; font_bc:=((b0*256+b1)*256+b2)*256+b3;
1270 read_tfm_word; font_ec:=((b0*256+b1)*256+b2)*256+b3;
1271 if font_ec<font_bc then font_bc:=font_ec+1;
1272 read_tfm_word;
1273 for k:=1 to 8 do begin
1274 read_tfm_word;
1275 end;
1276 if font_ofm_level=1 then begin
1277 read_tfm_word; font_lh:=((b0*256+b1)*256+b2)*256+b3-29;
1278 read_tfm_word; read_tfm_word;
1279 font_extra_words:=(((b0*256+b1)*256+b2)*256+b3) div 2;
1280 for k:=1 to 12 do begin
1281 read_tfm_word;
1282 end;
1283 end;
1284 for k:=1 to font_lh do begin
1285 read_tfm_word;
1286 if k=1 then @<Check the check sum@>;
1287 if k=2 then @<Check the design size@>;
1288 end;
1289 end
1290
1291 @ @<Read |char_info| entries@>=
1292 case font_ofm_level of
1293 -1: begin
1294 for k:=font_bc to font_ec do begin
1295 read_tfm_word;
1296 if b0>0 then {character |k| exists in the font}
1297 begin vc[vc_ptr]:=k; incr(vc_ptr);
1298 if vc_ptr=vc_size then vf_abort('I''m out of OVF memory!');
1299 end;
1300 end;
1301 end;
1302 0: begin
1303 for k:=font_bc to font_ec do
1304 begin read_tfm_word;
1305 if (b0*256+b1)>0 then {character |k| exists in the font}
1306 begin vc[vc_ptr]:=k; incr(vc_ptr);
1307 if vc_ptr=vc_size then vf_abort('I''m out of OVF memory!');
1308 end;
1309 read_tfm_word;
1310 end;
1311 end;
1312 1: begin
1313 k:=font_bc;
1314 while k<=font_ec do
1315 begin read_tfm_word; font_wd:=b0*256+b1;
1316 read_tfm_word; read_tfm_word;
1317 font_kprime:=k+(b0*256+b1);
1318 if font_wd>0 then {character |k| exists in the font}
1319 for font_j:=k to font_kprime do
1320 begin vc[vc_ptr]:=font_j; incr(vc_ptr);
1321 if vc_ptr=vc_size then vf_abort('I''m out of OVF memory!');
1322 end;
1323 k:=font_kprime+1;
1324 for font_j:=1 to font_extra_words do
1325 read_tfm_word;
1326 end
1327 end;
1328 end;
1329
1330 @ @<Check the check sum@>=
1331 if b0+b1+b2+b3>0 then
1332 if(b0<>vf[font_start[font_ptr]])or@|
1333 (b1<>vf[font_start[font_ptr]+1])or@|
1334 (b2<>vf[font_start[font_ptr]+2])or@|
1335 (b3<>vf[font_start[font_ptr]+3]) then
1336 begin print_ln('Check sum in OVF file being replaced by font metric check sum');
1337 @.Check sum...replaced...@>
1338 vf[font_start[font_ptr]]:=b0;
1339 vf[font_start[font_ptr]+1]:=b1;
1340 vf[font_start[font_ptr]+2]:=b2;
1341 vf[font_start[font_ptr]+3]:=b3;
1342 end
1343
1344 @ @<Check the design size@>=
1345 if(b0<>vf[font_start[font_ptr]+8])or@|
1346 (b1<>vf[font_start[font_ptr]+9])or@|
1347 (b2<>vf[font_start[font_ptr]+10])or@|
1348 (b3<>vf[font_start[font_ptr]+11]) then
1349 begin print_ln('Design size in OVF file being replaced by font metric design size');
1350 @.Design size...replaced...@>
1351 vf[font_start[font_ptr]+8]:=b0;
1352 vf[font_start[font_ptr]+9]:=b1;
1353 vf[font_start[font_ptr]+10]:=b2;
1354 vf[font_start[font_ptr]+11]:=b3;
1355 end
1356
1357 @ If no font directory has been specified, \.{DVI}-reading software
1358 is supposed to use the default font directory, which is a
1359 system-dependent place where the standard fonts are kept.
1360 The string variable |default_directory| contains the name of this area.
1361 @^system dependencies@>
1362
1363 @d default_directory_name=='TeXfonts:' {change this to the correct name}
1364 @d default_directory_name_length=9 {change this to the correct length}
1365
1366 @<Glob...@>=
1367 @!default_directory:packed array[1..default_directory_name_length] of char;
1368
1369 @ @<Set init...@>=
1370 default_directory:=default_directory_name;
1371
1372 @ The string |cur_name| is supposed to be set to the external name of the
1373 \.{TFM} file for the current font. This usually means that we need to
1374 prepend the name of the default directory, and
1375 to append the suffix `\.{.TFM}'. Furthermore, we change lower case letters
1376 to upper case, since |cur_name| is a \PASCAL\ string.
1377 @^system dependencies@>
1378
1379 @<Move font name into the |cur_name| string@>=
1380 for k:=1 to name_length do cur_name[k]:=' ';
1381 if a=0 then
1382 begin for k:=1 to default_directory_name_length do
1383 cur_name[k]:=default_directory[k];
1384 r:=default_directory_name_length;
1385 end
1386 else r:=0;
1387 for k:=font_start[font_ptr]+14 to vf_ptr-1 do
1388 begin incr(r);
1389 if r+4>name_length then vf_abort('Font name too long for me!');
1390 @.Font name too long for me@>
1391 if (vf[k]>="a")and(vf[k]<="z") then
1392 cur_name[r]:=xchr[vf[k]-@'40]
1393 else cur_name[r]:=xchr[vf[k]];
1394 end;
1395 cur_name[r+1]:='.'; cur_name[r+2]:='T'; cur_name[r+3]:='F'; cur_name[r+4]:='M'
1396
1397
1398 @ It's convenient to have a subroutine
1399 that reads a |k|-byte number from |vf_file|.
1400
1401 @d get_vf(#)==if eof(vf_file) then #:=0 @+else read_vf(#)
1402
1403 @p function vf_read(@!k:integer):integer; {actually |1<=k<=4|}
1404 var @!b:byte; {input byte}
1405 @!a:integer; {accumulator}
1406 begin vf_count:=vf_count+k; get_vf(b); a:=b;
1407 if k=4 then if b>=128 then a:=a-256; {4-byte numbers are signed}
1408 while k>1 do
1409 begin get_vf(b);
1410 a:=256*a+b; decr(k);
1411 end;
1412 vf_read:=a;
1413 end;
1414
1415 @ The \.{VF} format supports arbitrary 4-byte character codes,
1416 but \.{VPL} format presently does not.
1417 But \.{OVP} does.
1418
1419 @<Read and store a character packet@>=
1420 begin if temp_byte=long_char then
1421 begin pl:=vf_read(4); c:=vf_read(4); reg_count:=vf_read(4);
1422 {|pl[4]| |cc[4]| |tfm[4]|}
1423 end
1424 else begin pl:=temp_byte; c:=vf_read(1); reg_count:=vf_read(3);
1425 {|pl[1]| |cc[1]| |tfm[3]|}
1426 end;
1427 if nonexistent(c) then vf_abort('Character ',c:1,' does not exist!');
1428 @.Character c does not exist@>
1429 if packet_start[c]<vf_size then
1430 print_ln('Discarding earlier packet for character ',c:1);
1431 @.Discarding earlier packet...@>
1432 if reg_count<>tfm_width(c) then
1433 print_ln('Incorrect OFM width for character ',c:1,' in OVF file');
1434 @.Incorrect OFM width...@>
1435 if pl<0 then vf_abort('Negative packet length!');
1436 @.Negative packet length@>
1437 packet_start[c]:=vf_ptr; vf_store(pl); packet_end[c]:=vf_ptr-1;
1438 packet_found:=true;
1439 end
1440
1441 @ The preceding code requires a simple subroutine that evaluates \.{TFM} data.
1442
1443 @p function tfm_width(@!c:integer):integer;
1444 var @!a:integer; {accumulator}
1445 @!k:index; {index into |tfm|}
1446 begin k:=width(c); {we assume that character |c| exists}
1447 a:=tfm[k];
1448 if a>=128 then a:=a-256;
1449 tfm_width:=((256*a+tfm[k+1])*256+tfm[k+2])*256+tfm[k+3];
1450 end;
1451
1452 @* Basic output subroutines.
1453 Let us now define some procedures that will reduce the rest of \.{VFtoVP}'s
1454 work to a triviality.
1455
1456 First of all, it is convenient to have an abbreviation for output to the
1457 \.{VPL} file:
1458
1459 @d out(#)==write(vpl_file,#)
1460
1461 @ In order to stick to standard \PASCAL, we use an |xchr| array to do
1462 appropriate conversion of ASCII codes. Three other little strings are
1463 used to produce |face| codes like \.{MIE}.
1464
1465 @<Glob...@>=
1466 @!ASCII_04,@!ASCII_10,@!ASCII_14: packed array [1..32] of char;
1467 {strings for output in the user's external character set}
1468 @!xchr:packed array [0..255] of char;
1469 @!MBL_string,@!RI_string,@!RCE_string:packed array [1..3] of char;
1470 {handy string constants for |face| codes}
1471 @!HEX: packed array [1..16] of char;
1472
1473 @ @<Set init...@>=
1474 ASCII_04:=' !"#$%&''()*+,-./0123456789:;<=>?';@/
1475 ASCII_10:='@@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';@/
1476 ASCII_14:='`abcdefghijklmnopqrstuvwxyz{|}~?';@/
1477 for k:=0 to 255 do xchr[k]:='?';
1478 for k:=0 to @'37 do
1479 begin xchr[k+@'40]:=ASCII_04[k+1];
1480 xchr[k+@'100]:=ASCII_10[k+1];
1481 xchr[k+@'140]:=ASCII_14[k+1];
1482 end;
1483 MBL_string:='MBL'; RI_string:='RI '; RCE_string:='RCE';
1484 HEX:='0123456789ABCDEF';@/
1485
1486 @ The array |dig| will hold a sequence of digits to be output.
1487
1488 @<Glob...@>=
1489 @!dig:array[0..11] of integer;
1490
1491 @ Here, in fact, are two procedures that output |dig[j-1]|$\,\ldots\,$|dig[0]|,
1492 given $j>0$.
1493
1494 @p procedure out_digs(j:integer); {outputs |j| digits}
1495 begin repeat decr(j); out(HEX[1+dig[j]]);
1496 until j=0;
1497 end;
1498 @#
1499 procedure print_digs(j:integer); {prints |j| digits}
1500 begin repeat decr(j); print(HEX[1+dig[j]]);
1501 until j=0;
1502 end;
1503
1504 @ The |print_number| procedure indicates how |print_digs| can be used.
1505 This procedure can print in octal, decimal or hex notation.
1506
1507 @d print_hex(#)==print_number(#,16)
1508 @d print_octal(#)==print_number(#,8)
1509 @d print_decimal(#)==print_number(#,10)
1510
1511 @p procedure print_number(c:integer; form:integer); {prints value of |c|}
1512 var j:0..32; {index into |dig|}
1513 begin
1514 j:=0;
1515 if (c<0) then begin
1516 print_ln('Internal error: print_number (negative value)');
1517 c:=0;
1518 end;
1519 if form=8 then
1520 print('''') {an apostrophe indicates the octal notation}
1521 else if form=16 then
1522 print('"') { a double apostrophe indicates the hexadecimal notation}
1523 else if form<>10 then begin
1524 print_ln('Internal error: print_number (form)');
1525 form:=16;
1526 end;
1527 while (c>0) or (j=0) do begin
1528 dig[j]:=c mod form; c:=c div form;
1529 j:=j+1;
1530 end;
1531 print_digs(j);
1532 end;
1533
1534 @ A \.{VPL} file has nested parentheses, and we want to format the output
1535 so that its structure is clear. The |level| variable keeps track of the
1536 depth of nesting.
1537
1538 @<Glob...@>=
1539 @!level:0..5;
1540
1541 @ @<Set init...@>=
1542 level:=0;
1543
1544 @ Three simple procedures suffice to produce the desired structure in the
1545 output.
1546
1547 @p procedure out_ln; {finishes one line, indents the next}
1548 var l:0..5;
1549 begin write_ln(vpl_file);
1550 for l:=1 to level do out(' ');
1551 end;
1552 @#
1553 procedure left; {outputs a left parenthesis}
1554 begin incr(level); out('(');
1555 end;
1556 @#
1557 procedure right; {outputs a right parenthesis and finishes a line}
1558 begin decr(level); out(')'); out_ln;
1559 end;
1560
1561 @ The value associated with a property can be output in a variety of
1562 ways. For example, we might want to output a {\mc BCPL} string that
1563 begins in |tfm[k]|:
1564
1565 @p procedure out_BCPL(@!k:index); {outputs a string, preceded by a blank space}
1566 var l:0..39; {the number of bytes remaining}
1567 begin out(' '); l:=tfm[k];
1568 while l>0 do
1569 begin incr(k); decr(l); out(xchr[tfm[k]]);
1570 end;
1571 end;
1572
1573 @ The property value might also be a sequence of |l| bytes, beginning
1574 in |tfm[k]|, that we would like to output in hex notation.
1575 The following procedure assumes that |l<=4|, but larger values of |l|
1576 could be handled easily by enlarging the |dig| array and increasing
1577 the upper bounds on |b| and |j|.
1578
1579 @d out_octal_number(#)==out_number(#,8)
1580 @d out_decimal_number(#)==out_number(#,10)
1581 @d out_hex_number(#)==out_number(#,16)
1582 @d out_dec(#)==out_decimal_number(#)
1583 @d out_hex_char(#)==out_hex_number(#)
1584
1585 @p procedure out_number(c:integer; form:integer); {outputs value of |c|}
1586 var j:0..32; {index into |dig|}
1587 begin
1588 j:=0;
1589 if (c<0) then begin
1590 print_ln('Internal error: print_number (negative value)');
1591 c:=0;
1592 end;
1593 if form=8 then
1594 out(' O ')
1595 else if form=10 then
1596 out(' D ')
1597 else if form=16 then
1598 out(' H ')
1599 else begin
1600 print_ln('Internal error: print_number (form)');
1601 form:=16;
1602 out(' H ')
1603 end;
1604 while (c>0) or (j=0) do begin
1605 dig[j]:=c mod form; c:=c div form;
1606 j:=j+1;
1607 end;
1608 out_digs(j);
1609 end;
1610 @#
1611 procedure out_hex(@!k,@!l:index);
1612 {outputs |l| bytes in hex}
1613 var a:0..@"7FFFFFFF; {accumulator for bits not yet output}
1614 @!b:0..32; {the number of significant bits in |a|}
1615 @!j:0..11; {the number of digits of output}
1616 begin
1617 out(' H ');
1618 a:=0; b:=0; j:=0;
1619 while l>0 do @<Reduce \(1)|l| by one, preserving the invariants@>;
1620 while (a>0)or(j=0) do begin
1621 dig[j]:=a mod 16; a:=a div 16; incr(j);
1622 end;
1623 out_digs(j);
1624 end;
1625
1626 @ @<Reduce \(1)|l|...@>=
1627 begin decr(l);
1628 if tfm[k+l]<>0 then begin
1629 while b>2 do begin
1630 dig[j]:=a mod 16; a:=a div 16; b:=b-4; incr(j);
1631 end;
1632 case b of
1633 0: a:=tfm[k+l];
1634 1:a:=a+2*tfm[k+l];
1635 2:a:=a+4*tfm[k+l];
1636 3:a:=a+8*tfm[k+l];
1637 end;
1638 end;
1639 b:=b+8;
1640 end
1641
1642 @ The property value may be a character, which is output in hex
1643 unless it is a letter or a digit. This procedure is the only place
1644 where a lowercase letter will be output to the \.{PL} file.
1645 @^system dependencies@>
1646
1647 @p procedure out_char(@!c:integer); {outputs a character}
1648 begin if font_type>vanilla then
1649 out_hex_char(c)
1650 else if (c>="0")and(c<="9") then
1651 out(' C ',c-"0":1)
1652 else if (c>="A")and(c<="Z") then
1653 out(' C ',ASCII_10[c-"A"+2])
1654 else if (c>="a")and(c<="z") then
1655 out(' C ',ASCII_14[c-"a"+2])
1656 else out_hex_char(c);
1657 end;
1658
1659 @ The property value might be a ``face'' byte, which is output in the
1660 curious code mentioned earlier, provided that it is less than 18.
1661
1662 @p procedure out_face(@!k:index); {outputs a |face|}
1663 var s:0..1; {the slope}
1664 @!b:0..8; {the weight and expansion}
1665 begin if tfm[k]>=18 then out_hex(k,1)
1666 else begin out(' F '); {specify face-code format}
1667 s:=tfm[k] mod 2; b:=tfm[k] div 2;
1668 out(MBL_string[1+(b mod 3)]);
1669 out(RI_string[1+s]);
1670 out(RCE_string[1+(b div 3)]);
1671 end;
1672 end;
1673
1674 @ And finally, the value might be a |fix_word|, which is output in
1675 decimal notation with just enough decimal places for \.{VPtoVF}
1676 to recover every bit of the given |fix_word|.
1677
1678 All of the numbers involved in the intermediate calculations of
1679 this procedure will be nonnegative and less than $10\cdot2^{24}$.
1680
1681 @p procedure out_fix(@!k:index); {outputs a |fix_word|}
1682 var a:0..@'7777; {accumulator for the integer part}
1683 @!f:integer; {accumulator for the fraction part}
1684 @!j:0..12; {index into |dig|}
1685 @!delta:integer; {amount if allowable inaccuracy}
1686 begin out(' R '); {specify real format}
1687 a:=(tfm[k]*16)+(tfm[k+1] div 16);
1688 f:=((tfm[k+1] mod 16)*@'400+tfm[k+2])*@'400+tfm[k+3];
1689 if a>@'3777 then @<Reduce \(2)negative to positive@>;
1690 @<Output the integer part, |a|, in decimal notation@>;
1691 @<Output the fraction part, $|f|/2^{20}$, in decimal notation@>;
1692 end;
1693
1694 @ The following code outputs at least one digit even if |a=0|.
1695
1696 @<Output the integer...@>=
1697 begin j:=0;
1698 repeat dig[j]:=a mod 10; a:=a div 10; incr(j);
1699 until a=0;
1700 out_digs(j);
1701 end
1702
1703 @ And the following code outputs at least one digit to the right
1704 of the decimal point.
1705
1706 @<Output the fraction...@>=
1707 begin out('.'); f:=10*f+5; delta:=10;
1708 repeat if delta>@'4000000 then f:=f+@'2000000-(delta div 2);
1709 out(f div @'4000000:1); f:=10*(f mod @'4000000); delta:=delta*10;
1710 until f<=delta;
1711 end;
1712
1713 @ @<Reduce \(2)negative to positive@>=
1714 begin out('-'); a:=@'10000-a;
1715 if f>0 then
1716 begin f:=@'4000000-f; decr(a);
1717 end;
1718 end
1719
1720 @* Outputting the TFM info.
1721 \TeX\ checks the information of a \.{TFM} file for validity as the
1722 file is being read in, so that no further checks will be needed when
1723 typesetting is going on. And when it finds something wrong, it justs
1724 calls the file ``bad,'' without identifying the nature of the problem,
1725 since \.{TFM} files are supposed to be good almost all of the time.
1726
1727 Of course, a bad file shows up every now and again, and that's where
1728 \.{VFtoVP} comes in. This program wants to catch at least as many errors as
1729 \TeX\ does, and to give informative error messages besides.
1730 All of the errors are corrected, so that the \.{VPL} output will
1731 be correct (unless, of course, the \.{TFM} file was so loused up
1732 that no attempt is being made to fathom it).
1733
1734 @ Just before each character is processed, its code is printed in hex
1735 notation. Up to eight such codes appear on a line; so we have a variable
1736 to keep track of how many are currently there. We also keep track of
1737 whether or not any errors have had to be corrected.
1738
1739 @<Glob...@>=
1740 @!chars_on_line:0..8; {the number of characters printed on the current line}
1741 @!perfect:boolean; {was the file free of errors?}
1742
1743 @ @<Set init...@>=
1744 chars_on_line:=0;@/
1745 perfect:=true; {innocent until proved guilty}
1746
1747 @ Error messages are given with the help of the |bad| and |range_error|
1748 and |bad_char| macros:
1749
1750 @d bad(#)==begin perfect:=false; if chars_on_line>0 then print_ln(' ');
1751 chars_on_line:=0; print_ln('Bad OFM file: ',#);
1752 end
1753 @.Bad OFM file@>
1754 @d range_error(#)==begin perfect:=false; print_ln(' ');
1755 print(#,' index for character ');
1756 print_hex(c); print_ln(' is too large;');
1757 print_ln('so I reset it to zero.');
1758 end
1759 @d bad_char_tail(#)==print_hex(#); print_ln('.');
1760 end
1761 @d bad_char(#)==begin perfect:=false; if chars_on_line>0 then print_ln(' ');
1762 chars_on_line:=0; print('Bad OFM file: ',#,' nonexistent character ');
1763 bad_char_tail
1764 @d correct_bad_char_tail(#)==#(k,0)
1765 end
1766 @d correct_bad_char_middle(#)==print_hex(#(k)); print_ln('.');
1767 correct_bad_char_tail
1768 @d correct_bad_char(#)== begin perfect:=false;
1769 if chars_on_line>0 then print_ln(' ');
1770 chars_on_line:=0; print('Bad OFM file: ',#,' nonexistent character ');
1771 correct_bad_char_middle
1772
1773 @<Glob...@>=
1774 @!i:integer; {an index to words of a subfile}
1775 @!c:xchar_type; {a random character}
1776 @!d:0..3; {byte number in a word}
1777 @!k:index; {a random index}
1778 @!r:0..max_char; {a random two-byte value}
1779 @!count:0..127; {for when we need to enumerate a small set}
1780
1781 @ There are a lot of simple things to do, and they have to be done one
1782 at a time, so we might as well get down to business. The first things
1783 that \.{VFtoVP} will put into the \.{VPL} file appear in the header part.
1784
1785 @<Do the header@>=
1786 begin
1787 case ofm_level of
1788 0: begin out('(OFMLEVEL H 0)'); out_ln; end;
1789 1: begin out('(OFMLEVEL H 1)'); out_ln; end;
1790 end;
1791 if ofm_on then
1792 begin left;
1793 if font_dir<=7 then out('FONTDIR')
1794 else out('NFONTDIR');
1795 case font_dir mod 8 of
1796 0: out(' TL');
1797 1: out(' LT');
1798 2: out(' TR');
1799 3: out(' LB');
1800 4: out(' BL');
1801 5: out(' RT');
1802 6: out(' BR');
1803 7: out(' RB');
1804 end;
1805 right
1806 end;
1807 font_type:=vanilla;
1808 if lh>=12 then begin
1809 @<Set the true |font_type|@>;
1810 if lh>=17 then begin
1811 @<Output the family name@>;
1812 if lh>=18 then @<Output the rest of the header@>;
1813 end;
1814 @<Output the character coding scheme@>;
1815 end;
1816 @<Output the design size@>;
1817 @<Output the check sum@>;
1818 @<Output the |seven_bit_safe_flag|@>;
1819 end
1820
1821 @ @<Output the check sum@>=
1822 left; out('CHECKSUM'); out_hex(check_sum,4);
1823 right
1824
1825 @ Incorrect design sizes are changed to 10 points.
1826
1827 @d bad_design(#)==begin bad('Design size ',#,'!');
1828 @.Design size wrong@>
1829 print_ln('I''ve set it to 10 points.');
1830 out(' D 10');
1831 end
1832
1833 @ @<Output the design size@>=
1834 left; out('DESIGNSIZE');
1835 if tfm[design_size]>127 then bad_design('negative')
1836 else if (tfm[design_size]=0)and(tfm[design_size+1]<16) then
1837 bad_design('too small')
1838 else out_fix(design_size);
1839 right;
1840 out('(COMMENT DESIGNSIZE IS IN POINTS)'); out_ln;
1841 out('(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)'); out_ln
1842 @.DESIGNSIZE IS IN POINTS@>
1843
1844 @ Since we have to check two different {\mc BCPL} strings for validity,
1845 we might as well write a subroutine to make the check.
1846
1847 @p procedure check_BCPL(@!k,@!l:index); {checks a string of length |<l|}
1848 var j:index; {runs through the string}
1849 @!c:integer; {character being checked}
1850 begin if tfm[k]>=l then
1851 begin bad('String is too long; I''ve shortened it drastically.');
1852 @.String is too long...@>
1853 tfm[k]:=1;
1854 end;
1855 for j:=k+1 to k+tfm[k] do
1856 begin c:=tfm[j];
1857 if (c="(")or(c=")") then
1858 begin bad('Parenthesis in string has been changed to slash.');
1859 @.Parenthesis...changed to slash@>
1860 tfm[j]:="/";
1861 end
1862 else if (c<" ")or(c>"~") then
1863 begin bad('Nonstandard ASCII code has been blotted out.');
1864 @.Nonstandard ASCII code...@>
1865 tfm[j]:="?";
1866 end
1867 else if (c>="a")and(c<="z") then tfm[j]:=c+"A"-"a"; {upper-casify letters}
1868 end;
1869 end;
1870
1871 @ The |font_type| starts out |vanilla|; possibly we need to reset it.
1872
1873 @<Set the true |font_type|@>=
1874 begin check_BCPL(scheme,40);
1875 if (tfm[scheme]>=11)and@|(tfm[scheme+1]="T")and@|
1876 (tfm[scheme+2]="E")and@|(tfm[scheme+3]="X")and@|
1877 (tfm[scheme+4]=" ")and@|(tfm[scheme+5]="M")and@|
1878 (tfm[scheme+6]="A")and@|(tfm[scheme+7]="T")and@|
1879 (tfm[scheme+8]="H")and@|(tfm[scheme+9]=" ") then
1880 begin if (tfm[scheme+10]="S")and(tfm[scheme+11]="Y") then font_type:=mathsy
1881 else if (tfm[scheme+10]="E")and(tfm[scheme+11]="X") then font_type:=mathex;
1882 end;
1883 end
1884
1885 @ @<Output the character coding scheme@>=
1886 left; out('CODINGSCHEME');
1887 out_BCPL(scheme);
1888 right
1889
1890 @ @<Output the family name@>=
1891 left; out('FAMILY');
1892 check_BCPL(family,20);
1893 out_BCPL(family);
1894 right
1895
1896 @ @<Output the rest of the header@>=
1897 begin left; out('FACE'); out_face(random_word+3); right;
1898 for i:=18 to lh-1 do
1899 begin left; out('HEADER D ',i:1);
1900 out_hex(check_sum+4*i,@,4); right;
1901 end;
1902 end
1903
1904 @ This program does not check to see if the |seven_bit_safe_flag| has the
1905 correct setting, i.e., if it really reflects the seven-bit-safety of
1906 the \.{TFM} file; the stated value is merely put into the \.{VPL} file.
1907 The \.{VPtoVF} program will store a correct value and give a warning
1908 message if a file falsely claims to be safe.
1909
1910 \.{OFM} files are assumed to be seven-bit-unsafe.
1911
1912 @<Output the |seven_bit_safe_flag|@>=
1913 if ofm_on then
1914 begin left; out('SEVENBITSAFEFLAG FALSE'); right;
1915 end
1916 else if (lh>17) and (tfm[random_word]>127) then
1917 begin left; out('SEVENBITSAFEFLAG TRUE'); right;
1918 end
1919
1920 @ The next thing to take care of is the list of parameters.
1921
1922 @<Do the parameters@>=
1923 if np>0 then
1924 begin left; out('FONTDIMEN'); out_ln;
1925 for i:=1 to np do @<Check and output the $i$th parameter@>;
1926 right;
1927 end;
1928 @<Check to see if |np| is complete for this font type@>;
1929
1930 @ @<Check to see if |np|...@>=
1931 if (font_type=mathsy)and(np<>22) then
1932 print_ln('Unusual number of fontdimen parameters for a math symbols font (',
1933 @.Unusual number of fontdimen...@>
1934 np:1,' not 22).')
1935 else if (font_type=mathex)and(np<>13) then
1936 print_ln('Unusual number of fontdimen parameters for an extension font (',
1937 np:1,' not 13).')
1938
1939 @ All |fix_word| values except the design size and the first parameter
1940 will be checked to make sure that they are less than 16.0 in magnitude,
1941 using the |check_fix| macro:
1942
1943 @d check_fix_tail(#)==bad(#,' ',i:1,' is too big;');
1944 print_ln('I have set it to zero.');
1945 end
1946 @d check_fix(#)==if (tfm[#]>0)and(tfm[#]<255) then
1947 begin tfm[#]:=0; tfm[(#)+1]:=0; tfm[(#)+2]:=0; tfm[(#)+3]:=0;
1948 check_fix_tail
1949
1950 @<Check and output the $i$th parameter@>=
1951 begin left;
1952 if i=1 then out('SLANT') {this parameter is not checked}
1953 else begin check_fix(param(i))('Parameter');@/
1954 @.Parameter n is too big@>
1955 @<Output the name of parameter $i$@>;
1956 end;
1957 out_fix(param(i)); right;
1958 end
1959
1960 @ @<Output the name...@>=
1961 if i<=7 then case i of
1962 2:out('SPACE');@+3:out('STRETCH');@+4:out('SHRINK');
1963 5:out('XHEIGHT');@+6:out('QUAD');@+7:out('EXTRASPACE')@+end
1964 else if (i<=22)and(font_type=mathsy) then case i of
1965 8:out('NUM1');@+9:out('NUM2');@+10:out('NUM3');
1966 11:out('DENOM1');@+12:out('DENOM2');
1967 13:out('SUP1');@+14:out('SUP2');@+15:out('SUP3');
1968 16:out('SUB1');@+17:out('SUB2');
1969 18:out('SUPDROP');@+19:out('SUBDROP');
1970 20:out('DELIM1');@+21:out('DELIM2');
1971 22:out('AXISHEIGHT')@+end
1972 else if (i<=13)and(font_type=mathex) then
1973 if i=8 then out('DEFAULTRULETHICKNESS')
1974 else out('BIGOPSPACING',i-8:1)
1975 else out('PARAMETER D ',i:1)
1976
1977 @ @<Glob...@>=
1978 @!start_counter,@!base_counter,@!number_entries:integer;
1979 @!value:integer;
1980
1981 @ @<Do the ivalue parameters@>=
1982 if nki>0 then begin
1983 start_counter:=ivalues_start*4;
1984 base_counter:=ivalues_base*4;
1985 for i:=0 to nki-1 do @<Check and output the $i$th ivalue table@>;
1986 end;
1987
1988 @ @<Check and output the $i$th ivalue table@>=
1989 begin
1990 left; out('FONTIVALUE'); out_hex_number(i); out_ln;
1991 number_entries:=256*tfm[start_counter+2]+tfm[start_counter+3];
1992 for j:=0 to number_entries-1 do begin
1993 left; out('IVALUE'); out_hex_number(j); out_ln;
1994 value:=256*tfm[base_counter+2]+tfm[base_counter+3];
1995 left; out('IVALUEVAL'); out_hex_number(value); right;
1996 right;
1997 base_counter:=base_counter+4;
1998 end;
1999 right;
2000 start_counter:=start_counter+4;
2001 end;
2002
2003 @ @<Do the fvalue parameters@>=
2004 if nkf>0 then begin
2005 start_counter:=fvalues_start*4;
2006 base_counter:=fvalues_base*4;
2007 for i:=0 to nkf-1 do @<Check and output the $i$th fvalue table@>;
2008 end;
2009
2010 @ @<Check and output the $i$th fvalue table@>=
2011 begin
2012 left; out('FONTFVALUE'); out_hex_number(i); out_ln;
2013 number_entries:=256*tfm[start_counter+2]+tfm[start_counter+3];
2014 for j:=0 to number_entries-1 do begin
2015 left; out('FVALUE'); out_hex_number(j); out_ln;
2016 left; out('FVALUEVAL'); out_fix(base_counter); right;
2017 right;
2018 base_counter:=base_counter+4;
2019 end;
2020 right;
2021 start_counter:=start_counter+4;
2022 end;
2023
2024 @ @<Do the mvalue parameters@>=
2025 if nkm>0 then begin
2026 start_counter:=mvalues_start*4;
2027 base_counter:=mvalues_base*4;
2028 for i:=0 to nkm-1 do @<Check and output the $i$th mvalue table@>;
2029 end;
2030
2031 @ @<Check and output the $i$th mvalue table@>=
2032 begin
2033 left; out('FONTMVALUE'); out_hex_number(i); out_ln;
2034 number_entries:=256*tfm[start_counter+2]+tfm[start_counter+3];
2035 for j:=0 to number_entries-1 do begin
2036 left; out('MVALUE'); out_hex_number(j); out_ln;
2037 left; out('MVALUEVAL'); out_fix(base_counter); right;
2038 right;
2039 base_counter:=base_counter+4;
2040 end;
2041 right;
2042 start_counter:=start_counter+4;
2043 end;
2044
2045 @ @<Do the rule parameters@>=
2046 if nkr>0 then
2047 begin start_counter:=rules_start*4;
2048 base_counter:=rules_base*4;
2049 for i:=0 to nkr-1 do @<Check and output the $i$th rule table@>;
2050 end
2051
2052 @ @<Check and output the $i$th rule table@>=
2053 begin left; out('FONTRULE'); out_hex_number(i); out_ln;
2054 number_entries:=256*tfm[start_counter+2]+tfm[start_counter+3];
2055 for j:=0 to number_entries-1 do
2056 begin left; out('RULE'); out_hex_number(j); out_ln;
2057 left; out('RULEWD'); out_fix(base_counter); right;
2058 left; out('RULEHT'); out_fix(base_counter+4); right;
2059 left; out('RULEDP'); out_fix(base_counter+8); right;
2060 right;
2061 base_counter:=base_counter+12;
2062 end;
2063 right;
2064 start_counter:=start_counter+4;
2065 end;
2066
2067 @ @<Do the glue parameters@>=
2068 if nkg>0 then
2069 begin start_counter:=glues_start*4;
2070 base_counter:=glues_base*4;
2071 for i:=0 to nkg-1 do @<Check and output the $i$th glue table@>;
2072 end;
2073
2074 @ @<Glob...@>=
2075 @!glue_subtype,@!glue_argument_kind,@!glue_stretch_order,
2076 @!glue_shrink_order,@!glue_argument:integer;
2077
2078 @ @<Check and output the $i$th glue table@>=
2079 begin left; out('FONTGLUE'); out_hex_number(i); out_ln;
2080 number_entries:=256*tfm[start_counter+2]+tfm[start_counter+3];
2081 for j:=0 to number_entries-1 do
2082 begin left; out('GLUE'); out_hex_number(j); out_ln;
2083 glue_subtype:=tfm[base_counter] div 16;
2084 glue_argument_kind:=tfm[base_counter] mod 16;
2085 glue_stretch_order:=tfm[base_counter+1] div 16;
2086 glue_shrink_order:=tfm[base_counter+1] mod 16;
2087 glue_argument:=tfm[base_counter+2]*256+tfm[base_counter+3];
2088 left;
2089 out('GLUETYPE');
2090 case glue_subtype of
2091 0: out(' H 0');
2092 1: out(' H 1');
2093 2: out(' H 2');
2094 3: out(' H 3');
2095 end;
2096 right;
2097 case glue_argument_kind of
2098 1: begin
2099 left; out('GLUERULE'); out_hex_number(glue_argument); right;
2100 end;
2101 2: begin
2102 left; out('GLUECHAR'); out_hex_number(glue_argument); right;
2103 end;
2104 end;
2105 left;
2106 out('GLUESTRETCHORDER');
2107 case glue_stretch_order of
2108 0: out(' H 0');
2109 1: out(' H 1');
2110 2: out(' H 2');
2111 3: out(' H 3');
2112 4: out(' H 4');
2113 end;
2114 right;
2115 left;
2116 out('GLUESHRINKORDER');
2117 case glue_shrink_order of
2118 0: out(' H 0');
2119 1: out(' H 1');
2120 2: out(' H 2');
2121 3: out(' H 3');
2122 4: out(' H 4');
2123 end;
2124 right;
2125 left; out('GLUEWD'); out_fix(base_counter+4); right;
2126 left; out('GLUESTRETCH'); out_fix(base_counter+8); right;
2127 left; out('GLUESHRINK'); out_fix(base_counter+12); right;
2128 right;
2129 base_counter:=base_counter+16;
2130 end;
2131 right;
2132 start_counter:=start_counter+4;
2133 end;
2134
2135 @ @<Do the penalty parameters@>=
2136 if nkp>0 then
2137 begin start_counter:=penalties_start*4;
2138 base_counter:=penalties_base*4;
2139 for i:=0 to nkp-1 do @<Check and output the $i$th penalty table@>;
2140 end;
2141
2142 @ @<Check and output the $i$th penalty table@>=
2143 begin
2144 left; out('FONTPENALTY'); out_hex_number(i); out_ln;
2145 number_entries:=256*tfm[start_counter+2]+tfm[start_counter+3];
2146 for j:=0 to number_entries-1 do begin
2147 left; out('PENALTY'); out_hex_number(j); out_ln;
2148 value:=256*tfm[base_counter+2]+tfm[base_counter+3];
2149 left; out('PENALTYVAL'); out_hex_number(value); right;
2150 right;
2151 base_counter:=base_counter+4;
2152 end;
2153 right;
2154 start_counter:=start_counter+4;
2155 end;
2156
2157 @ We need to check the range of all the remaining |fix_word| values,
2158 and to make sure that |width[0]=0|, etc.
2159
2160 @d nonzero_fix(#)==(tfm[#]>0)or(tfm[#+1]>0)or(tfm[#+2]>0)or(tfm[#+3]>0)
2161
2162 @<Check the |fix_word| entries@>=
2163 if nonzero_fix(4*width_base) then bad('width[0] should be zero.');
2164 @.should be zero@>
2165 if nonzero_fix(4*height_base) then bad('height[0] should be zero.');
2166 if nonzero_fix(4*depth_base) then bad('depth[0] should be zero.');
2167 if nonzero_fix(4*italic_base) then bad('italic[0] should be zero.');
2168 for i:=0 to nw-1 do check_fix(4*(width_base+i))('Width');
2169 @.Width n is too big@>
2170 for i:=0 to nh-1 do check_fix(4*(height_base+i))('Height');
2171 @.Height n is too big@>
2172 for i:=0 to nd-1 do check_fix(4*(depth_base+i))('Depth');
2173 @.Depth n is too big@>
2174 for i:=0 to ni-1 do check_fix(4*(italic_base+i))('Italic correction');
2175 @.Italic correction n is too big@>
2176 if nk>0 then for i:=0 to nk-1 do check_fix(kern(i))('Kern');
2177 @.Kern n is too big@>
2178
2179 @ The ligature/kerning program comes next. Before we can put it out in
2180 \.{VPL} format, we need to make a table of ``labels'' that will be inserted
2181 into the program. For each character |c| whose |tag| is |lig_tag| and
2182 whose starting address is |r|, we will store the pair |(c,r)| in the
2183 |label_table| array. If there's a boundary-char program starting at~|r|,
2184 we also store the pair |(256,r)|.
2185 This array is sorted by its second components, using the
2186 simple method of straight insertion.
2187
2188 @<Glob...@>=
2189 @!label_table:array[xxxchar_type] of record
2190 @!cc:xchar_type;@!rr:0..lig_size;ischar:boolean;end;
2191 @!label_ptr:xxchar_type; {the largest entry in |label_table|}
2192 @!sort_ptr:xxchar_type; {index into |label_table|}
2193 @!boundary_char:xchar_type; {boundary character, or |xmax_char| if none}
2194 @!bchar_label:0..xmax_label; {beginning of boundary character program}
2195
2196 @ @<Set init...@>=
2197 boundary_char:=xmax_char; bchar_label:=xmax_label;@/
2198 label_ptr:=0; label_table[0].rr:=0; {a sentinel appears at the bottom}
2199
2200 @ We'll also identify and remove inaccessible program steps, using the
2201 |activity| array.
2202
2203 @d unreachable=0 {a program step not known to be reachable}
2204 @d pass_through=1 {a program step passed through on initialization}
2205 @d accessible=2 {a program step that can be relevant}
2206
2207 @<Glob...@>=
2208 @!activity:array[0..lig_size] of unreachable..accessible;
2209 @!ai,@!acti:0..lig_size; {indices into |activity|}
2210
2211 @ @<Do the ligatures and kerns@>=
2212 if nl>0 then
2213 begin for ai:=0 to (nl-1) do activity[ai]:=unreachable;
2214 @<Check for a boundary char@>;
2215 end;
2216 @<Build the label table@>;
2217 if nl>0 then
2218 begin left; out('LIGTABLE'); out_ln;@/
2219 @<Compute the |activity| array@>;
2220 @<Output and correct the ligature/kern program@>;
2221 right;
2222 @<Check for ligature cycles@>;
2223 end
2224
2225 @ We build the label table even when |nl=0|, because this catches errors
2226 that would not otherwise be detected.
2227
2228 @<Build...@>=
2229 for c:=bc to ec do
2230 if (tag(c)=lig_tag) or (ctag(c)) then
2231 begin r:=rremainder(c);
2232 if (l_skip_byte(lig_step(r)) div 256)=0 then begin
2233 if r<nl then begin
2234 if l_skip_byte(lig_step(r))>stop_flag then begin
2235 r:=256*l_op_byte(lig_step(r))+l_remainder(lig_step(r));
2236 if r<nl then
2237 if activity[rremainder(c)]=unreachable then
2238 activity[rremainder(c)]:=pass_through;
2239 end;
2240 end;
2241 end;
2242 if r>=nl then
2243 begin perfect:=false; print_ln(' ');
2244 print('Ligature/kern starting index for character '); print_hex(c);
2245 print_ln(' is too large;'); print_ln('so I removed it.'); set_no_tag(c);
2246 set_no_ctag(c);
2247 @.Ligature/kern starting index...@>
2248 end
2249 else @<Insert |(c,r)| into |label_table|@>;
2250 end;
2251 label_table[label_ptr+1].rr:=lig_size; {put ``infinite'' sentinel at the end}
2252
2253 @ @<Insert |(c,r)|...@>=
2254 begin sort_ptr:=label_ptr; {there's a hole at position |sort_ptr+1|}
2255 while label_table[sort_ptr].rr>r do
2256 begin label_table[sort_ptr+1]:=label_table[sort_ptr];
2257 decr(sort_ptr); {move the hole}
2258 end;
2259 label_table[sort_ptr+1].ischar:=not ctag(c);
2260 if ctag(c) then
2261 label_table[sort_ptr+1].cc:=char_param(c,0)
2262 else
2263 label_table[sort_ptr+1].cc:=c;
2264 label_table[sort_ptr+1].rr:=r; {fill the hole}
2265 incr(label_ptr); activity[r]:=accessible;
2266 end
2267
2268 @ @<Check for a bound...@>=
2269 if l_skip_byte(lig_step(0))=255 then
2270 begin left; out('BOUNDARYCHAR');
2271 boundary_char:=l_next_char(lig_step(0)); out_char(boundary_char); right;
2272 activity[0]:=pass_through;
2273 end;
2274 if l_skip_byte(lig_step(nl-1))=255 then
2275 begin r:=256*l_op_byte(lig_step(nl-1))+l_remainder(lig_step(nl-1));
2276 if r>=nl then
2277 begin perfect:=false; print_ln(' ');
2278 print('Ligature/kern starting index for boundarychar is too large;');
2279 print_ln('so I removed it.');
2280 @.Ligature/kern starting index...@>
2281 end
2282 else begin label_ptr:=1; label_table[1].cc:=xmax_char; label_table[1].rr:=r;
2283 bchar_label:=r; activity[r]:=accessible;
2284 end;
2285 activity[nl-1]:=pass_through;
2286 end
2287
2288 @ @<Compute the |activity| array@>=
2289 for ai:=0 to (nl-1) do
2290 if (l_skip_byte(lig_step(ai)) div 256)=1 then
2291 activity[ai]:=accessible
2292 else if activity[ai]=accessible then begin
2293 r:=l_skip_byte(lig_step(ai));
2294 if r<stop_flag then begin
2295 r:=r+ai+1;
2296 if r>=nl then begin
2297 bad('Ligature/kern step ',ai:1,' skips too far;');
2298 @.Lig...skips too far@>
2299 print_ln('I made it stop.'); set_l_skip_byte(lig_step(ai),stop_flag);
2300 end
2301 else activity[r]:=accessible;
2302 end;
2303 end
2304
2305 @ We ignore |pass_through| items, which don't need to be mentioned in
2306 the \.{VPL} file.
2307
2308 @<Output and correct the ligature...@>=
2309 sort_ptr:=1; {point to the next label that will be needed}
2310 for acti:=0 to nl-1 do if activity[acti]<>pass_through then
2311 begin i:=acti; @<Take care of commenting out unreachable steps@>;
2312 @<Output any labels for step $i$@>;
2313 @<Output step $i$ of the ligature/kern program@>;
2314 end;
2315 if level=2 then right {the final step was unreachable}
2316
2317 @ @<Globals...@>=
2318 @!output_clabels:array[0..256] of boolean;
2319 @!clabel_runner:integer;
2320 @!cprime:integer;
2321
2322 @ @<Set init...@>=
2323 for clabel_runner:=0 to 256 do
2324 output_clabels[clabel_runner]:=false;
2325
2326 @ @<Output any labels...@>=
2327 while i=label_table[sort_ptr].rr do begin
2328 if label_table[sort_ptr].cc<>xmax_char and not label_table[sort_ptr].ischar then begin
2329 if not output_clabels[label_table[sort_ptr].cc] then begin
2330 output_clabels[label_table[sort_ptr].cc]:=true;
2331 left;
2332 out('CLABEL');
2333 out_char(label_table[sort_ptr].cc);
2334 right;
2335 end
2336 end
2337 else begin
2338 left;
2339 out('LABEL');
2340 if label_table[sort_ptr].cc=xmax_char then out(' BOUNDARYCHAR')
2341 else out_char(label_table[sort_ptr].cc);
2342 right;
2343 end;
2344 incr(sort_ptr);
2345 end
2346
2347 @ @<Take care of commenting out...@>=
2348 if activity[i]=unreachable then
2349 begin if level=1 then
2350 begin left; out('COMMENT THIS PART OF THE PROGRAM IS NEVER USED!'); out_ln;
2351 end
2352 end
2353 else if level=2 then right
2354
2355 @ @<Output step $i$...@>=
2356 begin k:=lig_step(i);
2357 if (l_skip_byte(k) div 256)=1 then begin
2358 case l_op_byte(k) of
2359 17: begin
2360 left; out('CPEN');
2361 out_hex_number(l_next_char(k));
2362 out_hex_number(l_remainder(k));
2363 right;
2364 end;
2365 18: begin
2366 left; out('CGLUE');
2367 out_hex_number(l_next_char(k));
2368 out_hex_number(l_remainder(k));
2369 right;
2370 end;
2371 19: begin
2372 left; out('CPENGLUE');
2373 out_hex_number(l_next_char(k));
2374 out_hex_number(l_remainder(k) div 256);
2375 out_hex_number(l_remainder(k) mod 256);
2376 right;
2377 end;
2378 20: begin
2379 left; out('CKRN');
2380 out_hex_number(l_next_char(k));
2381 r:=l_remainder(k);
2382 if r>=nk then begin
2383 bad('Kern index too large.');
2384 @.Kern index too large@>
2385 out(' R 0.0');
2386 end
2387 else out_fix(kern(r));
2388 right;
2389 end;
2390 end;
2391 end
2392 else if l_skip_byte(k)>stop_flag then begin
2393 if (256*l_op_byte(k)+l_remainder(k))>=nl then
2394 bad('Ligature unconditional stop command address is too big.');
2395 @.Ligature unconditional stop...@>
2396 end
2397 else if l_op_byte(k)>=kern_flag then @<Output a kern step@>
2398 else @<Output a ligature step@>;
2399 if (l_skip_byte(k) mod 256)>0 then
2400 if level=1 then @<Output either \.{SKIP} or \.{STOP}@>;
2401 end
2402
2403 @ The \.{SKIP} command is a bit tricky, because we will be omitting all
2404 inaccessible commands.
2405
2406 @<Output either...@>=
2407 begin if (l_skip_byte(k) mod 256)>=stop_flag then out('(STOP)')
2408 else begin count:=0;
2409 for ai:=i+1 to (i+(l_skip_byte(k) mod 256)) do
2410 if activity[ai]=accessible then incr(count);
2411 out('(SKIP D ',count:1,')'); {possibly $count=0$, so who cares}
2412 end;
2413 out_ln;
2414 end
2415
2416 @ @<Output a kern step@>=
2417 begin if nonexistent(l_next_char(k)) then
2418 if l_next_char(k)<>boundary_char then
2419 correct_bad_char('Kern step for')(l_next_char)(set_l_next_char);
2420 @.Kern step for nonexistent...@>
2421 left; out('KRN'); out_char(l_next_char(k));
2422 r:=256*(l_op_byte(k)-kern_flag)+l_remainder(k);
2423 if r>=nk then
2424 begin bad('Kern index too large.');
2425 @.Kern index too large@>
2426 out(' R 0.0');
2427 end
2428 else out_fix(kern(r));
2429 right;
2430 end
2431
2432 @ @<Output a ligature step@>=
2433 begin if nonexistent(l_next_char(k)) then
2434 if l_next_char(k)<>boundary_char then
2435 correct_bad_char('Ligature step for')(l_next_char)(set_l_next_char);
2436 @.Ligature step for nonexistent...@>
2437 if nonexistent(l_remainder(k)) then
2438 correct_bad_char('Ligature step produces the')(l_remainder)(set_l_remainder);
2439 @.Ligature step produces...@>
2440 left; r:=l_op_byte(k);
2441 if (r=4)or((r>7)and(r<>11)) then
2442 begin print_ln('Ligature step with nonstandard code changed to LIG');
2443 r:=0; set_l_op_byte(k,0);
2444 end;
2445 if r mod 4>1 then out('/');
2446 out('LIG');
2447 if odd(r) then out('/');
2448 while r>3 do
2449 begin out('>'); r:=r-4;
2450 end;
2451 out_char(l_next_char(k)); out_char(l_remainder(k)); right;
2452 end
2453
2454 @ The last thing on \.{VFtoVP}'s agenda is to go through the
2455 list of |char_info| and spew out the information about each individual
2456 character.
2457
2458 @<Do the characters@>=
2459 sort_ptr:=0; {this will suppress `\.{STOP}' lines in ligature comments}
2460 c:=bc;
2461 while (c<=ec) do
2462 begin if width_index(c)>0 then
2463 for cprime:=c to (c+no_repeats(c)) do
2464 begin if chars_on_line=8 then
2465 begin print_ln(' '); chars_on_line:=1;
2466 end
2467 else begin if chars_on_line>0 then print(' ');
2468 incr(chars_on_line);
2469 end;
2470 print_hex(cprime); {progress report}
2471 left; out('CHARACTER'); out_char(cprime); out_ln;
2472 if ofm_on then check_unused(c);
2473 @<Output the character's width@>;
2474 if height_index(c)>0 then @<Output the character's height@>;
2475 if depth_index(c)>0 then @<Output the character's depth@>;
2476 if italic_index(c)>0 then @<Output the italic correction@>;
2477 case tag(c) of
2478 no_tag: do_nothing;
2479 lig_tag: @<Output the applicable part of the ligature/kern
2480 program as a comment@>;
2481 list_tag: @<Output the character link unless there is a problem@>;
2482 ext_tag: @<Output an extensible character recipe@>;
2483 end; {there are no other cases}
2484 for i:=0 to npc-1 do begin
2485 if char_param(c,i)<>0 then begin
2486 left;
2487 if i<nki then begin
2488 out('CHARIVALUE'); out_hex_number(i);
2489 end
2490 else if i<(nki+nkf) then begin
2491 out('CHARFVALUE'); out_hex_number(i-nki);
2492 end
2493 else if i<(nki+nkf+nkm) then begin
2494 out('CHARMVALUE'); out_hex_number(i-nki-nkf);
2495 end
2496 else if i<(nki+nkf+nkm+nkr) then begin
2497 out('CHARRULE'); out_hex_number(i-nki-nkf-nkm);
2498 end
2499 else if i<(nki+nkf+nkm+nkr+nkg) then begin
2500 out('CHARGLUE'); out_hex_number(i-nki-nkf-nkm-nkr);
2501 end
2502 else if i<(nki+nkf+nkm+nkr+nkg+nkp) then begin
2503 out('CHARPENALTY'); out_hex_number(i-nki-nkf-nkm-nkr-nkg);
2504 end;
2505 out_hex_number(char_param(c,i));
2506 right;
2507 end;
2508 end;
2509 if not do_map(cprime) then goto final_end;
2510 right;
2511 end;
2512 c:=c+1+no_repeats(c);
2513 end
2514
2515 @ @<Output the character's width@>=
2516 if width_index(c)>=nw then range_error('Width')
2517 @.Width index for char...@>
2518 else begin left; out('CHARWD'); out_fix(width(c)); right;
2519 end
2520
2521 @ @<Output the character's height@>=
2522 if height_index(c)>=nh then range_error('Height')
2523 @.Height index for char...@>
2524 else begin left; out('CHARHT'); out_fix(height(c)); right;
2525 end
2526
2527 @ @<Output the character's depth@>=
2528 if depth_index(c)>=nd then range_error('Depth')
2529 @.Depth index for char@>
2530 else begin left; out('CHARDP'); out_fix(depth(c)); right;
2531 end
2532
2533 @ @<Output the italic correction@>=
2534 if italic_index(c)>=ni then range_error('Italic correction')
2535 @.Italic correction index for char...@>
2536 else begin left; out('CHARIC'); out_fix(italic(c)); right;
2537 end
2538
2539 @ @<Output the applicable part of the ligature...@>=
2540 begin left; out('COMMENT'); out_ln;@/
2541 i:=rremainder(c); r:=lig_step(i);
2542 if (l_skip_byte(r) mod 256)>stop_flag then i:=256*l_op_byte(r)+l_remainder(r);
2543 repeat @<Output step...@>;
2544 if (l_skip_byte(k) mod 256)>=stop_flag then i:=nl
2545 else i:=i+1+(l_skip_byte(k) mod 256);
2546 until i>=nl;
2547 right;
2548 end
2549
2550 @ We want to make sure that there is no cycle of characters linked together
2551 by |list_tag| entries, since such a cycle would get \TeX\ into an endless
2552 loop. If such a cycle exists, the routine here detects it when processing
2553 the largest character code in the cycle.
2554
2555 @<Output the character link unless there is a problem@>=
2556 begin r:=rremainder(c);
2557 if nonexistent(r) then
2558 begin bad_char('Character list link to')(r); set_no_tag(c);
2559 @.Character list link...@>
2560 end
2561 else begin while (r<c)and(tag(r)=list_tag) do r:=rremainder(r);
2562 if r=c then
2563 begin bad('Cycle in a character list!');
2564 @.Cycle in a character list@>
2565 print('Character '); print_hex(c);
2566 print_ln(' now ends the list.');
2567 set_no_tag(c);
2568 end
2569 else begin left; out('NEXTLARGER'); out_char(rremainder(c));
2570 right;
2571 end;
2572 end;
2573 end
2574
2575 @ @<Output an extensible character recipe@>=
2576 if rremainder(c)>=ne then
2577 begin range_error('Extensible'); set_no_tag(c);
2578 @.Extensible index for char@>
2579 end
2580 else begin left; out('VARCHAR'); out_ln;
2581 @<Output the extensible pieces that exist@>;
2582 right;
2583 end
2584
2585 @ @<Glob...@>=
2586 @!exten_char:integer;
2587
2588 @ @<Output the extensible pieces that...@>=
2589 for d:=0 to 3 do begin
2590 if not ofm_on then begin
2591 k:=exten(c)+d;
2592 exten_char:=tfm[k];
2593 end
2594 else begin
2595 k:=exten(c)+2*d;
2596 exten_char:=256*tfm[k]+tfm[k+1];
2597 end;
2598 if (d=3)or(exten_char>0) then begin
2599 left;
2600 case d of
2601 0:out('TOP');@+1:out('MID');@+2:out('BOT');@+3:out('REP')@+end;
2602 if nonexistent(exten_char) then out_char(c)
2603 else out_char(exten_char);
2604 right;
2605 end
2606 end
2607
2608 @ Some of the extensible recipes may not actually be used, but \TeX\ will
2609 complain about them anyway if they refer to nonexistent characters.
2610 Therefore \.{VFtoVP} must check them too.
2611
2612 @<Check the extensible recipes@>=
2613 if ne>0 then
2614 for c:=0 to ne-1 do
2615 for d:=0 to 3 do begin
2616 if not ofm_on then begin
2617 k:=4*(exten_base+c)+d;
2618 exten_char:=tfm[k];
2619 end
2620 else begin
2621 k:=4*(exten_base+c)+2*d;
2622 exten_char:=256*tfm[k]+tfm[k+1];
2623 end;
2624 if (exten_char>0)or(d=3) then begin
2625 if nonexistent(exten_char) then begin
2626 bad_char('Extensible recipe involves the')(exten_char);
2627 @.Extensible recipe involves...@>
2628 if d<3 then begin
2629 if not ofm_on then begin
2630 tfm[k]:=0;
2631 end
2632 else begin
2633 tfm[k]:=0;
2634 tfm[k+1]:=0;
2635 end;
2636 end;
2637 end;
2638 end;
2639 end
2640
2641 @* Checking for ligature loops.
2642 We have programmed almost everything but the most interesting calculation of
2643 all, which has been saved for last as a special treat. \TeX's extended ligature
2644 mechanism allows unwary users to specify sequences of ligature replacements
2645 that never terminate. For example, the pair of commands
2646 $$\.{(/LIG $x$ $y$) (/LIG $y$ $x$)}$$
2647 alternately replaces character $x$ by character $y$ and vice versa. A similar
2648 loop occurs if \.{(LIG/ $z$ $y$)} occurs in the program for $x$ and
2649 \.{(LIG/ $z$ $x$)} occurs in the program for $y$.
2650
2651 More complicated loops are also possible. For example, suppose the ligature
2652 programs for $x$ and $y$ are
2653 $$\vcenter{\halign{#\hfil\cr
2654 \.{(LABEL $x$)(/LIG/ $z$ $w$)(/LIG/> $w$ $y$)} \dots,\cr
2655 \.{(LABEL $y$)(LIG $w$ $x$)} \dots;\cr}}$$
2656 then the adjacent characters $xz$ change to $xwz$, $xywz$, $xxz$, $xxwz$,
2657 \dots, ad infinitum.
2658
2659 @ To detect such loops, \.{VFtoVP} attempts to evaluate the function
2660 $f(x,y)$ for all character pairs $x$ and~$y$, where $f$ is defined as
2661 follows: If the current character is $x$ and the next character is
2662 $y$, we say the ``cursor'' is between $x$ and $y$; when the cursor
2663 first moves past $y$, the character immediately to its left is
2664 $f(x,y)$. This function is defined if and only if no infinite loop is
2665 generated when the cursor is between $x$ and~$y$.
2666
2667 The function $f(x,y)$ can be defined recursively. It turns out that all pairs
2668 $(x,y)$ belong to one of five classes. The simplest class has $f(x,y)=y$; this
2669 happens if there's no ligature between $x$ and $y$, or in the cases
2670 \.{LIG/>} and \.{/LIG/>>}. Another simple class arises when there's a
2671 \.{LIG} or \.{/LIG>} between $x$ and~$y$, generating the character~$z$;
2672 then $f(x,y)=z$. Otherwise we always have $f(x,y)$ equal to
2673 either $f(x,z)$ or $f(z,y)$ or $f(f(x,z),y)$, where $z$ is the inserted
2674 ligature character.
2675
2676 The first two of these classes can be merged; we can also consider
2677 $(x,y)$ to belong to the simple class when $f(x,y)$ has been evaluated.
2678 For technical reasons we allow $x$ to be 256 (for the boundary character
2679 at the left) or 257 (in cases when an error has been detected).
2680
2681 For each pair $(x,y)$ having a ligature program step, we store
2682 $(x,y)$ in a hash table from which the values $z$ and $class$ can be read.
2683
2684 @d simple=0 {$f(x,y)=z$}
2685 @d left_z=1 {$f(x,y)=f(z,y)$}
2686 @d right_z=2 {$f(x,y)=f(x,z)$}
2687 @d both_z=3 {$f(x,y)=f(f(x,z),y)$}
2688 @d pending=4 {$f(x,y)$ is being evaluated}
2689
2690 @<Glob...@>=
2691 @!hash:array[0..hash_size] of integer64;
2692 @!class:array[0..hash_size] of simple..pending;
2693 @!lig_z:array[0..hash_size] of xxchar_type;
2694 @!hash_ptr:0..hash_size; {the number of nonzero entries in |hash|}
2695 @!hash_list:array[0..hash_size] of 0..hash_size;
2696 {list of those nonzero entries}
2697 @!h,@!hh:0..hash_size; {indices into the hash table}
2698 @!x_lig_cycle,@!y_lig_cycle:xchar_type; {problematic ligature pair}
2699
2700 @ @<Check for ligature cycles@>=
2701 hash_ptr:=0; y_lig_cycle:=xmax_char;
2702 for hh:=0 to hash_size do hash[hh]:=0; {clear the hash table}
2703 for c:=bc to ec do if tag(c)=lig_tag then
2704 begin i:=rremainder(c);
2705 if (l_skip_byte(lig_step(i)) mod 256)>stop_flag then
2706 i:=256*l_op_byte(lig_step(i))+l_remainder(lig_step(i));
2707 @<Enter data for character $c$ starting at location |i| in the hash table@>;
2708 end;
2709 if bchar_label<nl then
2710 begin c:=xmax_char; i:=bchar_label;
2711 @<Enter data for character $c$ starting at location |i| in the hash table@>;
2712 end;
2713 if hash_ptr=hash_size then
2714 begin print_ln('Sorry, I haven''t room for so many ligature/kern pairs!');
2715 @.Sorry, I haven't room...@>
2716 goto final_end;
2717 end;
2718 for hh:=1 to hash_ptr do
2719 begin r:=hash_list[hh];
2720 if class[r]>simple then {make sure $f$ is defined}
2721 r:=f(r,(hash[r]-1)div xmax_char,(hash[r]-1)mod xmax_char);
2722 end;
2723 if y_lig_cycle<xmax_char then
2724 begin print('Infinite ligature loop starting with ');
2725 @.Infinite ligature loop...@>
2726 if x_lig_cycle=xmax_char then print('boundary')@+else print_hex(x_lig_cycle);
2727 print(' and '); print_hex(y_lig_cycle); print_ln('!');
2728 out('(INFINITE LIGATURE LOOP MUST BE BROKEN!)'); out_ln; perfect:=false;;
2729 end
2730
2731 @ @<Enter data for character $c$...@>=
2732 repeat hash_input; k:=l_skip_byte(lig_step(i));
2733 if k>=stop_flag then i:=nl
2734 else i:=i+1+k;
2735 until i>=nl
2736
2737 @ We use an ``ordered hash table'' with linear probing, because such a table
2738 is efficient when the lookup of a random key tends to be unsuccessful.
2739
2740 @p procedure hash_input; {enter data for character |c| and command |i|}
2741 label exit;
2742 var @!cc:simple..both_z; {class of data being entered}
2743 @!zz:char_type; {function value or ligature character being entered}
2744 @!y:char_type; {the character after the cursor}
2745 @!key:integer64; {value to be stored in |hash|}
2746 @!t64:integer64; {temporary register for swapping}
2747 @!t:integer; {temporary register for swapping}
2748 begin if hash_ptr=hash_size then goto exit;
2749 @<Compute the command parameters |y|, |cc|, and |zz|@>;
2750 key:=int64cast(xmax_char)*c+y+1; h:=(hash_mult*key) mod hash_size;
2751 while hash[h]>0 do
2752 begin if hash[h]<=key then
2753 begin if hash[h]=key then goto exit; {unused ligature command}
2754 t64:=hash[h]; hash[h]:=key; key:=t64; {do ordered-hash-table insertion}
2755 t:=class[h]; class[h]:=cc; cc:=t; {namely, do a swap}
2756 t:=lig_z[h]; lig_z[h]:=zz; zz:=t;
2757 end;
2758 if h>0 then decr(h)@+else h:=hash_size;
2759 end;
2760 hash[h]:=key; class[h]:=cc; lig_z[h]:=zz;
2761 incr(hash_ptr); hash_list[hash_ptr]:=h;
2762 exit:end;
2763
2764 @ We must store kern commands as well as ligature commands, because the former
2765 might make the latter inapplicable.
2766
2767 @<Compute the command param...@>=
2768 k:=lig_step(i); y:=l_next_char(k); t:=l_op_byte(k);
2769 cc:=simple; zz:=l_remainder(k);
2770 if t>=kern_flag then zz:=y
2771 else begin case t of
2772 0,6:do_nothing; {\.{LIG},\.{/LIG>}}
2773 5,11:zz:=y; {\.{LIG/>}, \.{/LIG/>>}}
2774 1,7:cc:=left_z; {\.{LIG/}, \.{/LIG/>}}
2775 2:cc:=right_z; {\.{/LIG}}
2776 3:cc:=both_z; {\.{/LIG/}}
2777 end; {there are no other cases}
2778 end
2779
2780 @ Evaluation of $f(x,y)$ is handled by two mutually recursive procedures.
2781 Kind of a neat algorithm, generalizing a depth-first search.
2782
2783 @p function f(@!h,@!x,@!y:index):index; forward;@t\2@>
2784 {compute $f$ for arguments known to be in |hash[h]|}
2785 function eval(@!x,@!y:index):index; {compute $f(x,y)$ with hashtable lookup}
2786 var @!key:integer64; {value sought in hash table}
2787 begin key:=int64cast(xmax_char)*x+y+1; h:=(hash_mult*key) mod hash_size;
2788 while hash[h]>key do
2789 if h>0 then decr(h)@+else h:=hash_size;
2790 if hash[h]<key then eval:=y {not in ordered hash table}
2791 else eval:=f(h,x,y);
2792 end;
2793
2794 @ Pascal's beastly convention for |forward| declarations prevents us from
2795 saying |function f(h,x,y:index):index| here.
2796
2797 @p function f;
2798 begin case class[h] of
2799 simple: do_nothing;
2800 left_z: begin class[h]:=pending; lig_z[h]:=eval(lig_z[h],y); class[h]:=simple;
2801 end;
2802 right_z: begin class[h]:=pending; lig_z[h]:=eval(x,lig_z[h]); class[h]:=simple;
2803 end;
2804 both_z: begin class[h]:=pending; lig_z[h]:=eval(eval(x,lig_z[h]),y);
2805 class[h]:=simple;
2806 end;
2807 pending: begin x_lig_cycle:=x; y_lig_cycle:=y; lig_z[h]:=xxmax_char; class[h]:=simple;
2808 end; {the value |xxmax_char| will break all cycles, since it's not in |hash|}
2809 end; {there are no other cases}
2810 f:=lig_z[h];
2811 end;
2812
2813 @* Outputting the VF info.
2814 The routines we've used for output from the |tfm| array have counterparts
2815 for output from |vf|. One difference is that the string outputs from |vf|
2816 need to be checked for balanced parentheses. The |string_balance| routine
2817 tests the string of length~|l| that starts at location~|k|.
2818
2819 @p function string_balance(@!k,@!l:integer):boolean;
2820 label not_found,exit;
2821 var @!j,@!bal:integer;
2822 begin if l>0 then if vf[k]=" " then goto not_found;
2823 {a leading blank is considered unbalanced}
2824 bal:=0;
2825 for j:=k to k+l-1 do
2826 begin if (vf[j]<" ")or(vf[j]>=127) then goto not_found;
2827 if vf[j]="(" then incr(bal)
2828 else if vf[j]=")" then
2829 if bal=0 then goto not_found else decr(bal);
2830 end;
2831 if bal>0 then goto not_found;
2832 string_balance:=true; return;
2833 not_found:string_balance:=false;
2834 exit:end;
2835
2836 @ @d bad_vf(#)==begin perfect:=false; if chars_on_line>0 then print_ln(' ');
2837 chars_on_line:=0; print_ln('Bad OVF file: ',#);
2838 end
2839 @.Bad OVF file@>
2840
2841 @<Do the virtual font title@>=
2842 if string_balance(0,font_start[0]) then
2843 begin left; out('VTITLE ');
2844 for k:=0 to font_start[0]-1 do out(xchr[vf[k]]);
2845 right;
2846 end
2847 else bad_vf('Title is not a balanced ASCII string')
2848 @.Title is not balanced@>
2849
2850 @ We can re-use some code by moving |fix_word| data to |tfm|, using the
2851 fact that the design size has already been output.
2852
2853 @p procedure out_as_fix(@!x:integer);
2854 var @!k:1..3;
2855 begin if abs(x)>=@'100000000 then
2856 begin bad_vf('Oversize dimension has been reset to zero.');
2857 @.Oversize dimension...@>
2858 x:=0;
2859 end;
2860 if x>=0 then tfm[design_size]:=0
2861 else begin tfm[design_size]:=255; x:=x+@'100000000;
2862 end;
2863 for k:=3 downto 1 do
2864 begin tfm[design_size+k]:=x mod 256; x:=x div 256;
2865 end;
2866 out_fix(design_size);
2867 end;
2868
2869 @ @<Do the local fonts@>=
2870 for f:=0 to font_ptr-1 do
2871 begin left; out('MAPFONT D ',f:1); out_ln;
2872 @<Output the font area and name@>;
2873 for k:=0 to 11 do tfm[k]:=vf[font_start[f]+k];
2874 if tfm[0]+tfm[1]+tfm[2]+tfm[3]>0 then
2875 begin left; out('FONTCHECKSUM'); out_hex(0,4); right;
2876 end;
2877 left; out('FONTAT'); out_fix(4); right;
2878 left; out('FONTDSIZE'); out_fix(8); right; right;
2879 end
2880
2881 @ @<Output the font area and name@>=
2882 a:=vf[font_start[f]+12]; l:=vf[font_start[f]+13];
2883 if a>0 then
2884 if not string_balance(font_start[f]+14,a) then
2885 bad_vf('Improper font area will be ignored')
2886 @.Improper font area@>
2887 else begin left; out('FONTAREA ');
2888 for k:=font_start[f]+14 to font_start[f]+a+13 do out(xchr[vf[k]]);
2889 right;
2890 end;
2891 if (l=0)or not string_balance(font_start[f]+14+a,l) then
2892 bad_vf('Improper font name will be ignored')
2893 @.Improper font name@>
2894 else begin left; out('FONTNAME ');
2895 for k:=font_start[f]+14+a to font_start[f]+a+l+13 do out(xchr[vf[k]]);
2896 right;
2897 end
2898
2899 @ Now we get to the interesting part of \.{VF} output, where \.{DVI}
2900 commands are translated into symbolic form. The \.{VPL} language is a subset
2901 of \.{DVI}, so we sometimes need to output semantic equivalents of
2902 the commands instead of producing a literal translation. This causes a
2903 small but tolerable loss of efficiency. We need to simulate the stack
2904 used by \.{DVI}-reading software.
2905
2906 @<Glob...@>=
2907 @!top:0..max_stack; {\.{DVI} stack pointer}
2908 @!wstack,@!xstack,@!ystack,@!zstack:array[0..max_stack] of integer;
2909 {stacked values of \.{DVI} registers |w|, |x|, |y|, |z|}
2910 @!vf_limit:0..vf_size; {the current packet ends here}
2911 @!o:byte; {the current opcode}
2912
2913 @ @<Do the packet for character |c|@>=
2914 if packet_start[c]=vf_size then
2915 bad_vf('Missing packet for character ',c:1)
2916 @.Missing packet@>
2917 else begin left; out('MAP'); out_ln;
2918 top:=0; wstack[0]:=0; xstack[0]:=0; ystack[0]:=0; zstack[0]:=0;
2919 vf_ptr:=packet_start[c]; vf_limit:=packet_end[c]+1; f:=0;
2920 while vf_ptr<vf_limit do
2921 begin o:=vf[vf_ptr]; incr(vf_ptr);
2922 case o of
2923 @<Cases of \.{DVI} instructions that can appear in character packets@>@;
2924 improper_DVI_for_VF: bad_vf('Illegal DVI code ',o:1,' will be ignored');
2925 end; {there are no other cases}
2926 end;
2927 if top>0 then
2928 begin bad_vf('More pushes than pops!');
2929 @.More pushes than pops@>
2930 repeat out('(POP)'); decr(top);@+until top=0;
2931 end;
2932 right;
2933 end
2934
2935 @ A procedure called |get_bytes| helps fetch the parameters of \.{DVI} commands.
2936
2937 @p function get_bytes(@!k:integer;@!signed:boolean):integer;
2938 var @!a:integer; {accumulator}
2939 begin if vf_ptr+k>vf_limit then
2940 begin bad_vf('Packet ended prematurely'); k:=vf_limit-vf_ptr;
2941 end;
2942 a:=vf[vf_ptr];
2943 if (k=4) or signed then
2944 if a>=128 then a:=a-256;
2945 incr(vf_ptr);
2946 while k>1 do
2947 begin a:=a*256+vf[vf_ptr]; incr(vf_ptr); decr(k);
2948 end;
2949 get_bytes:=a;
2950 end;
2951
2952 @ Let's look at the simplest cases first, in order to get some experience.
2953
2954 @d four_cases(#)==#,#+1,#+2,#+3
2955 @d eight_cases(#)==four_cases(#),four_cases(#+4)
2956 @d sixteen_cases(#)==eight_cases(#),eight_cases(#+8)
2957 @d thirty_two_cases(#)==sixteen_cases(#),sixteen_cases(#+16)
2958 @d sixty_four_cases(#)==thirty_two_cases(#),thirty_two_cases(#+32)
2959
2960 @<Cases...@>=
2961 nop:do_nothing;
2962 push:begin if top=max_stack then
2963 begin print_ln('Stack overflow!'); goto final_end;
2964 @.Stack overflow@>
2965 end;
2966 incr(top); wstack[top]:=wstack[top-1]; xstack[top]:=xstack[top-1];
2967 ystack[top]:=ystack[top-1]; zstack[top]:=zstack[top-1]; out('(PUSH)');
2968 out_ln;
2969 end;
2970 pop:if top=0 then bad_vf('More pops than pushes!')
2971 @.More pops than pushes@>
2972 else begin decr(top); out('(POP)'); out_ln;
2973 end;
2974 set_rule,put_rule:begin if o=put_rule then out('(PUSH)');
2975 left; out('SETRULE'); out_as_fix(get_bytes(4,true));
2976 out_as_fix(get_bytes(4,true));
2977 if o=put_rule then out(')(POP');
2978 right;
2979 end;
2980
2981 @ Horizontal and vertical motions become \.{RIGHT} and \.{DOWN} in \.{VPL}
2982 lingo.
2983
2984 @<Cases...@>=
2985 four_cases(right1):begin out('(MOVERIGHT');
2986 out_as_fix(get_bytes(o-right1+1,true));
2987 out(')'); out_ln;@+end;
2988 w0,four_cases(w1):begin if o<>w0 then wstack[top]:=get_bytes(o-w1+1,true);
2989 out('(MOVERIGHT'); out_as_fix(wstack[top]); out(')'); out_ln;@+end;
2990 x0,four_cases(x1):begin if o<>x0 then xstack[top]:=get_bytes(o-x1+1,true);
2991 out('(MOVERIGHT'); out_as_fix(xstack[top]); out(')'); out_ln;@+end;
2992 four_cases(down1):begin out('(MOVEDOWN'); out_as_fix(get_bytes(o-down1+1,true));
2993 out(')'); out_ln;@+end;
2994 y0,four_cases(y1):begin if o<>y0 then ystack[top]:=get_bytes(o-y1+1,true);
2995 out('(MOVEDOWN'); out_as_fix(ystack[top]); out(')'); out_ln;@+end;
2996 z0,four_cases(z1):begin if o<>z0 then zstack[top]:=get_bytes(o-z1+1,true);
2997 out('(MOVEDOWN'); out_as_fix(zstack[top]); out(')'); out_ln;@+end;
2998
2999 @ Variable |f| always refers to the current font. If |f=font_ptr|, it's
3000 a font that hasn't been defined (so its characters will be ignored).
3001
3002 @<Cases...@>=
3003 sixty_four_cases(fnt_num_0),four_cases(fnt1):begin f:=0;
3004 if o>=fnt1 then font_number[font_ptr]:=get_bytes(o-fnt1+1,false)
3005 else font_number[font_ptr]:=o-fnt_num_0;
3006 while font_number[f]<>font_number[font_ptr] do incr(f);
3007 if f=font_ptr then bad_vf('Undeclared font selected')
3008 @.Undeclared font selected@>
3009 else begin out('(SELECTFONT D ',f:1,')'); out_ln;
3010 end;
3011 end;
3012
3013 @ Before we typeset a character we make sure that it exists.
3014
3015 @<Cases...@>=
3016 sixty_four_cases(set_char_0),sixty_four_cases(set_char_0+64),
3017 four_cases(set1),four_cases(put1):begin if o>=set1 then
3018 if o>=put1 then c:=get_bytes(o-put1+1,false)
3019 else c:=get_bytes(o-set1+1,false)
3020 else c:=o;
3021 if (c<0)or(c>char_max) then
3022 bad_vf('Character ',c:1,' is out of range and will be ignored')
3023 else if f=font_ptr then
3024 bad_vf('Character ',c:1,' in undeclared font will be ignored')
3025 @.Character...will be ignored@>
3026 else begin vc[font_chars[f+1]-1]:=c; {store |c| in the ``hole'' we left}
3027 k:=font_chars[f];@+while vc[k]<>c do incr(k);
3028 if k=font_chars[f+1]-1 then
3029 bad_vf('Character ',c:1,' in font ',f:1,' will be ignored')
3030 else begin if o>=put1 then out('(PUSH)');
3031 left; out('SETCHAR'); out_char(c);
3032 if o>=put1 then out(')(POP');
3033 right;
3034 end;
3035 end;
3036 end;
3037
3038 @ The ``special'' commands are the only ones remaining to be dealt with.
3039 We use a hexadecimal
3040 output in the general case, if a simple string would be inadequate.
3041
3042 @d vf_out_hex(#)==begin a:=#;
3043 if a<10 then out(a:1)
3044 else out(xchr[a-10+"A"]);
3045 end
3046
3047 @<Cases...@>=
3048 four_cases(xxx1):begin k:=get_bytes(o-xxx1+1,false);
3049 if k<0 then bad_vf('String of negative length!')
3050 else begin left;
3051 if k+vf_ptr>vf_limit then
3052 begin bad_vf('Special command truncated to packet length');
3053 k:=vf_limit-vf_ptr;
3054 end;
3055 if (k>64)or not string_balance(vf_ptr,k) then
3056 begin out('SPECIALHEX ');
3057 while k>0 do
3058 begin if k mod 32=0 then out_ln
3059 else if k mod 4=0 then out(' ');
3060 vf_out_hex(vf[vf_ptr] div 16); vf_out_hex(vf[vf_ptr] mod 16);
3061 incr(vf_ptr); decr(k);
3062 end;
3063 end
3064 else begin out('SPECIAL ');
3065 while k>0 do
3066 begin out(xchr[vf[vf_ptr]]); incr(vf_ptr); decr(k);
3067 end;
3068 end;
3069 right;
3070 end;
3071 end;
3072
3073 @* The main program.
3074 The routines sketched out so far need to be packaged into separate procedures,
3075 on some systems, since some \PASCAL\ compilers place a strict limit on the
3076 size of a routine. The packaging is done here in an attempt to avoid some
3077 system-dependent changes.
3078
3079 First come the |vf_input| and |organize| procedures, which read the input data
3080 and get ready for subsequent events. If something goes wrong, the routines
3081 return |false|.
3082
3083 @p function vf_input:boolean;
3084 label final_end, exit;
3085 var vf_ptr:0..vf_size; {an index into |vf|}
3086 @!k:integer; {all-purpose index}
3087 @!c:integer; {character code}
3088 begin @<Read the whole \.{VF} file@>;
3089 vf_input:=true; return;
3090 final_end: vf_input:=false;
3091 exit: end;
3092 @#
3093 function organize:boolean;
3094 label final_end, exit;
3095 var tfm_ptr:index; {an index into |tfm|}
3096 begin @<Read the whole font metric file@>;
3097 @<Set subfile sizes |lh|, |bc|, \dots, |np|@>;
3098 @<Compute the base addresses@>;
3099 organize:=vf_input; return;
3100 final_end: organize:=false;
3101 exit: end;
3102
3103 @ Next we do the simple things.
3104
3105 @p procedure do_simple_things;
3106 var i:0..@'77777; {an index to words of a subfile}
3107 @!f:0..vf_size; {local font number}
3108 @!k:integer; {all-purpose index}
3109 begin @<Do the virtual font title@>;
3110 @<Do the header@>;
3111 @<Do the parameters@>;
3112 @<Do the ivalue parameters@>;@/
3113 @<Do the fvalue parameters@>;@/
3114 @<Do the mvalue parameters@>;@/
3115 @<Do the rule parameters@>;@/
3116 @<Do the glue parameters@>;@/
3117 @<Do the penalty parameters@>;@/
3118 @<Do the local fonts@>;
3119 @<Check the |fix_word| entries@>;
3120 end;
3121
3122 @ And then there's a routine for individual characters.
3123
3124 @p function do_map(@!c:integer):boolean;
3125 label final_end,exit;
3126 var @!k:integer;
3127 @!f:0..vf_size; {current font number}
3128 begin @<Do the packet for character |c|@>;
3129 do_map:=true; return;
3130 final_end: do_map:=false;
3131 exit:end;
3132 @#
3133 function do_characters:boolean;
3134 label final_end, exit;
3135 var @!c:integer; {character being done}
3136 @!k:index; {a random index}
3137 @!ai:0..lig_size; {index into |activity|}
3138 begin @<Do the characters@>;@/
3139 do_characters:=true; return;
3140 final_end: do_characters:=false;
3141 exit:end;
3142
3143 @ Here is where \.{VFtoVP} begins and ends.
3144 @p begin initialize;@/
3145 if not organize then goto final_end;
3146 do_simple_things;@/
3147 @<Do the ligatures and kerns@>;
3148 @<Check the extensible recipes@>;
3149 if not do_characters then goto final_end;
3150 print_ln('.');@/
3151 if level<>0 then print_ln('This program isn''t working!');
3152 @.This program isn't working@>
3153 if not perfect then
3154 begin out('(COMMENT THE OFM AND/OR OVF FILE WAS BAD, ');
3155 out('SO THE DATA HAS BEEN CHANGED!)');
3156 write_ln(vpl_file);
3157 end;
3158 @.THE OFM AND/OR OVF FILE WAS BAD...@>
3159 final_end:end.
3160
3161 @* System-dependent changes.
3162 This section should be replaced, if necessary, by changes to the program
3163 that are necessary to make \.{VFtoVP} work at a particular installation.
3164 It is usually best to design your change file so that all changes to
3165 previous sections preserve the section numbering; then everybody's version
3166 will be consistent with the printed program. More extensive changes,
3167 which introduce new sections, can be inserted here; then only the index
3168 itself will get a new section number.
3169 @^system dependencies@>
3170
3171 @* Index.
3172 Pointers to error messages appear here together with the section numbers
3173 where each ident\-i\-fier is used.