Debian Codegraph @

texk/web2c/dvicopy.web

raw
1 % Uncopyrightable trivial changes, 2018 Karl Berry (karl@@freefriends.org)
2 % Copyright (C) 1990--2014 Peter Breitenlohner (peb@@mppmu.mpg.de)
3 %
4 % This program is free software; you can redistribute it and/or modify
5 % it under the terms of the GNU General Public License as published by
6 % the Free Software Foundation; either version 1, or (at your option)
7 % any later version.
8 %
9 % You should have received a copy of the GNU General Public License
10 % along with this program. If not, see <http://www.gnu.org/licenses/>.
11 %
12 % Version 0.9 was finished May 21, 1990.
13 % Version 1.0 pixel rounding for real devices (August 6, 1990).
14 % Version 1.1 major rearrangements for DVIprint (October 7, 1990).
15 % Version 1.2 fixed some bugs, page selection (February 13, 1991).
16 % Version 1.3 several more changes, command line options,
17 % don't load fonts that are never used (August 25, 1992).
18 % Version 1.4 fixed a typo (March 28, 1995).
19 % Version 1.5 avoided cur_name_length identifier conflict (October 15, 1995).
20 % Version 1.6 minor cleanup: avoid unused or uninitialized variables,
21 % diagnose impossible cases (September 2009).
22 % bug fix (not for Web2C) and some typos (May 2014)
23 % from Udo Wermuth (u.wermuth@@icloud.com).
24 % 17 July 2018, still version 1.6: typos corrected, no code changes.
25 % 02 Sep 2024, still version 1.6: fix sorting order of names-of-sections.
26
27 % Here is TeX material that gets inserted after \input webmac
28 \def\hang{\hangindent 3em\indent\ignorespaces}
29 \font\ninerm=cmr9
30 \let\mc=\ninerm % medium caps for names like SAIL
31 \def\PASCAL{Pascal}
32 \font\logo=manfnt % font used for the METAFONT logo
33 \def\MF{{\logo META}\-{\logo FONT}}
34 \mathchardef\RA="3221 % right arrow
35
36 \def\({} % this is used to make section names sort themselves better
37 \def\9#1{} % this is used for sort keys in the index
38
39 \def\title{DVI\lowercase{copy}} % don't change this line!
40 \def\contentspagenumber{1}
41 \def\topofcontents{\null
42 \titlefalse % include headline on the contents page
43 \def\rheader{\mainfont\hfil \contentspagenumber}
44 \vfill
45 \centerline{\titlefont The {\ttitlefont DVIcopy} processor}
46 \vskip 5pt
47 \centerline{Copyright (C) 1990--2014 Peter Breitenlohner}
48 \centerline{Distributed under terms of GNU General Public License}
49 \vskip 15pt
50 \centerline{(Version 1.6, September 2009)}
51 \vfill}
52 \def\botofcontents{\vfill
53 \centerline{\hsize 5in\baselineskip9pt
54 \vbox{\ninerm\noindent
55 This program was developed at the Max-Planck-Institut f\"ur Physik
56 (Werner-Heisenberg-Institut), Munich, Germany.
57 `\TeX' is a trademark of the American Mathematical Society.
58 `{\logo hijklmnj}\kern1pt' is a trademark of Addison-Wesley
59 Publishing Company.}}}
60 \pageno=\contentspagenumber \advance\pageno by 1
61
62 @* Introduction.
63 The \.{DVIcopy} utility program copies (selected pages of) binary
64 device-independent (``\.{DVI}'') files that are produced by document
65 compilers such as \TeX, and replaces all references to characters from
66 virtual fonts by the typesetting instructions specified for them in
67 binary virtual-font (``\.{VF}'') files.
68 This program has two chief purposes: (1)~It can be used as preprocessor
69 for existing \.{DVI}-related software in cases where this software is
70 unable to handle virtual fonts or (given suitable \.{VF} files) where
71 this software cannot handle fonts with more than 128~characters;
72 and (2)~it serves as an example of a program that reads \.{DVI} and
73 \.{VF} files correctly, for system programmers who are developing
74 \.{DVI}-related software.
75
76 Goal number (1) is important since quite a few existing programs have
77 to be adapted to the extended capabilities of Version~3 of \TeX\ which
78 will require some time. Moreover some existing programs are `as is' and
79 the source code is, unfortunately, not available.
80 Goal number (2) needs perhaps a bit more explanation. Programs for
81 typesetting need to be especially careful about how they do arithmetic; if
82 rounding errors accumulate, margins won't be straight, vertical rules
83 won't line up, and so on (see the documentation of \.{DVItype} for more
84 details). This program is written as if it were a \.{DVI}-driver for a
85 hypothetical typesetting device |out_file|, the output file receiving
86 the copy of the input |dvi_file|. In addition all code related to
87 |out_file| is concentrated in two chapters at the end of this program
88 and quite independent of the rest of the code concerned with the
89 decoding of \.{DVI} and \.{VF} files and with font substitutions. Thus
90 it should be relatively easy to replace the device dependent code of
91 this program by the corresponding code required for a real typesetting
92 device. Having this in mind \.{DVItype}'s pixel rounding algorithms are
93 included as conditional code not used by \.{DVIcopy}.
94
95 The |banner| and |preamble_comment| strings defined here should be
96 changed whenever \.{DVIcopy} gets modified.
97
98 @d banner=='This is DVIcopy, Version 1.6' {printed when the program starts}
99 @d title=='DVIcopy' {the name of this program, used in some messages}
100 @d copyright=='Copyright (C) 1990,2009 Peter Breitenlohner'
101 @#
102 @d preamble_comment=='DVIcopy 1.6 output from '
103 @d comm_length=24 {length of |preamble_comment|}
104 @d from_length=6 {length of its |' from '| part}
105
106 @ This program is written in standard \PASCAL, except where it is necessary
107 to use extensions; for example, \.{DVIcopy} must read files whose names
108 are dynamically specified, and that would be impossible in pure \PASCAL.
109 All places where nonstandard constructions are used have been listed in
110 the index under ``system dependencies.''
111 @!@^system dependencies@>
112
113 One of the extensions to standard \PASCAL\ that we shall deal with is the
114 ability to move to a random place in a binary file; another is to
115 determine the length of a binary file. Such extensions are not necessary
116 for reading \.{DVI} files; since \.{DVIcopy} is (a model for) a
117 production program it should, however, be made as efficient as possible
118 for a particular system. If \.{DVIcopy} is being used with
119 \PASCAL s for which random file positioning is not efficiently available,
120 the following definition should be changed from |true| to |false|; in such
121 cases, \.{DVIcopy} will not include the optional feature that reads the
122 postamble first.
123
124 @d random_reading==true {should we skip around in the file?}
125
126 @ The program begins with a fairly normal header, made up of pieces that
127 @^system dependencies@>
128 will mostly be filled in later. The \.{DVI} input comes from file
129 |dvi_file|, the \.{DVI} output goes to file |out_file|, and messages
130 go to \PASCAL's standard |output| file.
131 The \.{TFM} and \.{VF} files are defined later since their external
132 names are determined dynamically.
133
134 If it is necessary to abort the job because of a fatal error, the program
135 calls the `|jump_out|' procedure, which goes to the label |final_end|.
136
137 @d final_end = 9999 {go here to wrap it up}
138
139 @p @t\4@>@<Compiler directives@>@/
140 program DVI_copy(@!dvi_file,@!out_file,@!output);
141 label final_end;
142 const @<Constants in the outer block@>@/
143 type @<Types in the outer block@>@/
144 var @<Globals in the outer block@>@/
145 @<Error handling procedures@>@/
146 procedure initialize; {this procedure gets things started properly}
147 var @<Local variables for initialization@>@/
148 begin print_ln(banner);@/
149 print_ln(copyright);
150 print_ln('Distributed under terms of GNU General Public License');@/
151 @<Set initial values@>@/
152 end;
153
154 @ The definition of |max_font_type| should be adapted to the number of
155 font types used by the program; the first three values have a fixed
156 meaning: |defined_font=0| indicates that a font has been defined,
157 |loaded_font=1| indicates that the \.{TFM} file has been loaded but the
158 font has not yet been used, and |vf_font_type=2| indicates a virtual
159 font. Font type values |>=real_font=3| indicate real fonts and
160 different font types are used to distinguish various kinds of font files
161 (\.{GF} or \.{PK} or \.{PXL}). \.{DVIcopy} uses |out_font_type=3| for
162 fonts that appear in the output \.{DVI} file.
163 @!@^font types@>
164
165 @d defined_font=0 {this font has been defined}
166 @d loaded_font=1 {this font has been defined and loaded}
167 @d vf_font_type=2 {this font is a virtual font}
168 @d real_font=3 {smallest font type for real fonts}
169 @#
170 @d out_font_type=3 {this font appears in the output file}
171 @d max_font_type=3
172
173 @ The following parameters can be changed at compile time to extend or
174 reduce \.{DVIcopy}'s capacity.
175
176 @d max_select=10 {maximum number of page selection ranges}
177
178 @<Constants...@>=
179 @!max_fonts=100; {maximum number of distinct fonts}
180 @!max_chars=10000; {maximum number of different characters among all fonts}
181 @!max_widths=3000; {maximum number of different characters widths}
182 @!max_packets=5000; {maximum number of different characters packets;
183 must be less than 65536}
184 @!max_bytes=30000; {maximum number of bytes for characters packets}
185 @!max_recursion=10; {\.{VF} files shouldn't recurse beyond this level}
186 @!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth}
187 @!terminal_line_length=150; {maximum number of characters input in a single
188 line of input from the terminal}
189 @!name_length=50; {a file name shouldn't be longer than this}
190
191 @ As mentioned above, \.{DVIcopy} has two chief purposes: (1)~It produces
192 a copy of the input \.{DVI} file with all references to characters from
193 virtual fonts replaced by their expansion as specified in the character
194 packets of \.{VF} files; and (2)~it serves as an example of a program
195 that reads \.{DVI} and \.{VF} files correctly, for system programmers
196 who are developing \.{DVI}-related software.
197
198 In fact, a very large section of code (starting with the second chapter
199 `Introduction (continued)' and ending with the fifteenth chapter
200 `The main program') is used in identical form in \.{DVIcopy} and in
201 \.{DVIprint}, a prototype \.{DVI}-driver. This has been made possible
202 mostly by using several \.{WEB} coding tricks, such as not to make the
203 resulting \PASCAL\ program inefficient in any way.
204
205 Parts of the program that are needed in \.{DVIprint} but not in
206 \.{DVIcopy} are delimited by the code words `$|device|\ldots|ecived|$';
207 these are mostly the pixel rounding algorithms used to convert the
208 \.{DVI} units of a \.{DVI} file to the raster units of a real output
209 device and have been copied more or less verbatim from \.{DVItype}.
210
211 @d device==@{ {change this to `$\\{device}\equiv\null$' when output
212 for a real device is produced}
213 @d ecived==@t@>@} {change this to `$\\{ecived}\equiv\null$' when output
214 for a real device is produced}
215 @f device==begin
216 @f ecived==end
217
218 @* Introduction (continued).
219 On some systems it is necessary to use various integer subrange types
220 in order to make \.{\title} efficient; this is true in particular for
221 frequently used variables such as loop indices. Consider an integer
222 variable |x| with values in the range |0..255|: on most small systems
223 |x| should be a one or two byte integer whereas on most large systems
224 |x| should be a four byte integer.
225 Clearly the author of a program knows best which range of values is
226 required for each variable; thus \.{\title} never uses \PASCAL's |integer|
227 type. All integer variables are declared as one of the integer subrange
228 types defined below as \.{WEB} macros or \PASCAL\ types; these definitions
229 can be used without system-dependent changes, provided the signed 32~bit
230 integers are a subset of the standard type |integer|, and the compiler
231 automatically uses the optimal representation for integer subranges
232 (both conditions need not be satisfied for a particular system).
233 @^system dependencies@>
234
235 The complementary problem of storing large arrays of integer type
236 variables as compactly as possible is addressed differently; here
237 \.{\title} uses a \PASCAL\ |type|~declaration for each kind of array
238 element.
239
240 Note that the primary purpose of these definitions is optimizations, not
241 range checking. All places where optimization for a particular system is
242 highly desirable have been listed in the index under ``optimization.''
243 @!@^optimization@>
244
245 @d int_32 == integer {signed 32~bit integers}
246
247 @<Types...@>=
248 @!int_31 = 0..@"7FFFFFFF; {unsigned 31~bit integer}
249 @!int_24u = 0..@"FFFFFF; {unsigned 24~bit integer}
250 @!int_24 = -@"800000..@"7FFFFF; {signed 24~bit integer}
251 @!int_23 = 0..@"7FFFFF; {unsigned 23~bit integer}
252 @!int_16u = 0..@"FFFF; {unsigned 16~bit integer}
253 @!int_16 = -@"8000..@"7FFF; {signed 16~bit integer}
254 @!int_15 = 0..@"7FFF; {unsigned 15~bit integer}
255 @!int_8u = 0..@"FF; {unsigned 8~bit integer}
256 @!int_8 = -@"80..@"7F; {signed 8~bit integer}
257 @!int_7 = 0..@"7F; {unsigned 7~bit integer}
258
259 @ Some of this code is optional for use when debugging only;
260 such material is enclosed between the delimiters |debug| and $|gubed|$.
261 Other parts, delimited by |stat| and $|tats|$, are optionally included
262 if statistics about \.{\title}'s memory usage are desired.
263
264 @d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
265 @d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
266 @f debug==begin
267 @f gubed==end
268 @#
269 @d stat==@{ {change this to `$\\{stat}\equiv\null$'
270 when gathering usage statistics}
271 @d tats==@t@>@} {change this to `$\\{tats}\equiv\null$'
272 when gathering usage statistics}
273 @f stat==begin
274 @f tats==end
275
276 @ The \PASCAL\ compiler used to develop this program has ``compiler
277 directives'' that can appear in comments whose first character is a dollar sign.
278 In production versions of \.{\title} these directives tell the compiler that
279 @^system dependencies@>
280 it is safe to avoid range checks and to leave out the extra code it inserts
281 for the \PASCAL\ debugger's benefit, although interrupts will occur if
282 there is arithmetic overflow.
283
284 @<Compiler directives@>=
285 @{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
286 @!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
287
288 @ Labels are given symbolic names by the following definitions. We insert
289 the label `|exit|:' just before the `\ignorespaces|end|\unskip' of a
290 procedure in which we have used the `|return|' statement defined below;
291 the label `|restart|' is occasionally used at the very beginning of a
292 procedure; and the label `|reswitch|' is occasionally used just prior to
293 a \&{case} statement in which some cases change the conditions and we wish to
294 branch to the newly applicable case.
295 Loops that are set up with the \&{loop} construction defined below are
296 commonly exited by going to `|done|' or to `|found|' or to `|not_found|',
297 and they are sometimes repeated by going to `|continue|'.
298
299 @d exit=10 {go here to leave a procedure}
300 @d restart=20 {go here to start a procedure again}
301 @d reswitch=21 {go here to start a case statement again}
302 @d continue=22 {go here to resume a loop}
303 @d done=30 {go here to exit a loop}
304 @d found=31 {go here when you've found it}
305 @d not_found=32 {go here when you've found something else}
306
307 @ The term |print| is used instead of |write| when this program writes on
308 |output|, so that all such output could easily be redirected if desired;
309 the term |d_print| is used for conditional output if we are debugging.
310
311 @d print(#)==write(output,#)
312 @d print_ln(#)==write_ln(output,#)
313 @d new_line==write_ln(output) {start new line}
314 @d print_nl(#)== {print information starting on a new line}
315 begin new_line; print(#);
316 end
317 @#
318 @d d_print(#)==@!debug print(#) @; @+ gubed
319 @d d_print_ln(#)==@! debug print_ln(#) @; @+ gubed
320
321 @ Here are some macros for common programming idioms.
322
323 @d incr(#) == #:=#+1 {increase a variable by unity}
324 @d decr(#) == #:=#-1 {decrease a variable by unity}
325 @#
326 @d Incr_Decr_end(#)==#
327 @d Incr(#)==#:=#+Incr_Decr_end {we use |Incr(a)(b)| to increase \dots}
328 @d Decr(#)==#:=#-Incr_Decr_end {\dots\ and |Decr(a)(b)| to decrease
329 variable |a| by |b|; this can be optimized for some compilers}
330 @#
331 @d loop == @+ while true do@+ {repeat over and over until a |goto| happens}
332 @d do_nothing == {empty statement}
333 @d return == goto exit {terminate a procedure call}
334 @f return == nil
335 @f loop == xclause
336
337 @ We assume that |case| statements may include a default case that applies
338 if no matching label is found. Thus, we shall use constructions like
339 @^system dependencies@>
340 $$\vbox{\halign{#\hfil\cr
341 |case x of|\cr
342 1: $\langle\,$code for $x=1\,\rangle$;\cr
343 3: $\langle\,$code for $x=3\,\rangle$;\cr
344 |othercases| $\langle\,$code for |x<>1| and |x<>3|$\,\rangle$\cr
345 |endcases|\cr}}$$
346 since most \PASCAL\ compilers have plugged this hole in the language by
347 incorporating some sort of default mechanism. For example, the compiler
348 used to develop \.{WEB} and \TeX\ allows `|others|:' as a default label,
349 and other \PASCAL s allow syntaxes like `\ignorespaces|else|\unskip' or
350 `\&{otherwise}' or `\\{otherwise}:', etc. The definitions of |othercases|
351 and |endcases| should be changed to agree with local conventions. (Of
352 course, if no default mechanism is available, the |case| statements of
353 this program must be extended by listing all remaining cases.
354 Donald~E. Knuth, the author of the \.{WEB} system program \.{TANGLE},
355 @^Knuth, Donald Ervin@>
356 would have taken the trouble to modify \.{TANGLE} so that such extensions
357 were done automatically, if he had not wanted to encourage \PASCAL\
358 compiler writers to make this important change in \PASCAL, where it belongs.)
359
360 @d othercases == others: {default for cases not listed explicitly}
361 @d endcases == @+end {follows the default case in an extended |case| statement}
362 @f othercases == else
363 @f endcases == end
364
365 @* The character set.
366 Like all programs written with the \.{WEB} system, \.{\title} can be
367 used with any character set. But it uses ASCII code internally, because
368 the programming for portable input-output is easier when a fixed internal
369 code is used, and because \.{DVI} and \.{VF} files use ASCII code for
370 file names and certain other strings.
371
372 The next few sections of \.{\title} have therefore been copied from the
373 analogous ones in the \.{WEB} system routines. They have been considerably
374 simplified, since \.{\title} need not deal with the controversial
375 ASCII codes less than @'40 or greater than @'176.
376 If such codes appear in the \.{DVI} file,
377 they will be printed as question marks.
378
379 @<Types...@>=
380 @!ASCII_code=" ".."~"; {a subrange of the integers}
381
382 @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
383 character sets were common, so it did not make provision for lower case
384 letters. Nowadays, of course, we need to deal with both upper and lower case
385 alphabets in a convenient way, especially in a program like \.{\title}.
386 So we shall assume that the \PASCAL\ system being used for \.{\title}
387 has a character set containing at least the standard visible characters
388 of ASCII code (|"!"| through |"~"|).
389
390 Some \PASCAL\ compilers use the original name |char| for the data type
391 associated with the characters in text files, while other \PASCAL s
392 consider |char| to be a 64-element subrange of a larger data type that has
393 some other name. In order to accommodate this difference, we shall use
394 the name |text_char| to stand for the data type of the characters in the
395 output file. We shall also assume that |text_char| consists of
396 the elements |chr(first_text_char)| through |chr(last_text_char)|,
397 inclusive. The following definitions should be adjusted if necessary.
398 @^system dependencies@>
399
400 @d text_char == char {the data type of characters in text files}
401 @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
402 @d last_text_char=127 {ordinal number of the largest element of |text_char|}
403
404 @<Types...@>=
405 @!text_file=packed file of text_char;
406
407 @ @<Local variables for init...@>=
408 @!i:int_16; {loop index for initializations}
409
410 @ The \.{\title} processor converts between ASCII code and
411 the user's external character set by means of arrays |xord| and |xchr|
412 that are analogous to \PASCAL's |ord| and |chr| functions.
413
414 @<Globals...@>=
415 @!xord: array [text_char] of ASCII_code;
416 {specifies conversion of input characters}
417 @!xchr: array [0..255] of text_char;
418 {specifies conversion of output characters}
419
420 @ Under our assumption that the visible characters of standard ASCII are
421 all present, the following assignment statements initialize the
422 |xchr| array properly, without needing any system-dependent changes.
423
424 @<Set init...@>=
425 for i:=0 to @'37 do xchr[i]:='?';
426 xchr[@'40]:=' ';
427 xchr[@'41]:='!';
428 xchr[@'42]:='"';
429 xchr[@'43]:='#';
430 xchr[@'44]:='$';
431 xchr[@'45]:='%';
432 xchr[@'46]:='&';
433 xchr[@'47]:='''';@/
434 xchr[@'50]:='(';
435 xchr[@'51]:=')';
436 xchr[@'52]:='*';
437 xchr[@'53]:='+';
438 xchr[@'54]:=',';
439 xchr[@'55]:='-';
440 xchr[@'56]:='.';
441 xchr[@'57]:='/';@/
442 xchr[@'60]:='0';
443 xchr[@'61]:='1';
444 xchr[@'62]:='2';
445 xchr[@'63]:='3';
446 xchr[@'64]:='4';
447 xchr[@'65]:='5';
448 xchr[@'66]:='6';
449 xchr[@'67]:='7';@/
450 xchr[@'70]:='8';
451 xchr[@'71]:='9';
452 xchr[@'72]:=':';
453 xchr[@'73]:=';';
454 xchr[@'74]:='<';
455 xchr[@'75]:='=';
456 xchr[@'76]:='>';
457 xchr[@'77]:='?';@/
458 xchr[@'100]:='@@';
459 xchr[@'101]:='A';
460 xchr[@'102]:='B';
461 xchr[@'103]:='C';
462 xchr[@'104]:='D';
463 xchr[@'105]:='E';
464 xchr[@'106]:='F';
465 xchr[@'107]:='G';@/
466 xchr[@'110]:='H';
467 xchr[@'111]:='I';
468 xchr[@'112]:='J';
469 xchr[@'113]:='K';
470 xchr[@'114]:='L';
471 xchr[@'115]:='M';
472 xchr[@'116]:='N';
473 xchr[@'117]:='O';@/
474 xchr[@'120]:='P';
475 xchr[@'121]:='Q';
476 xchr[@'122]:='R';
477 xchr[@'123]:='S';
478 xchr[@'124]:='T';
479 xchr[@'125]:='U';
480 xchr[@'126]:='V';
481 xchr[@'127]:='W';@/
482 xchr[@'130]:='X';
483 xchr[@'131]:='Y';
484 xchr[@'132]:='Z';
485 xchr[@'133]:='[';
486 xchr[@'134]:='\';
487 xchr[@'135]:=']';
488 xchr[@'136]:='^';
489 xchr[@'137]:='_';@/
490 xchr[@'140]:='`';
491 xchr[@'141]:='a';
492 xchr[@'142]:='b';
493 xchr[@'143]:='c';
494 xchr[@'144]:='d';
495 xchr[@'145]:='e';
496 xchr[@'146]:='f';
497 xchr[@'147]:='g';@/
498 xchr[@'150]:='h';
499 xchr[@'151]:='i';
500 xchr[@'152]:='j';
501 xchr[@'153]:='k';
502 xchr[@'154]:='l';
503 xchr[@'155]:='m';
504 xchr[@'156]:='n';
505 xchr[@'157]:='o';@/
506 xchr[@'160]:='p';
507 xchr[@'161]:='q';
508 xchr[@'162]:='r';
509 xchr[@'163]:='s';
510 xchr[@'164]:='t';
511 xchr[@'165]:='u';
512 xchr[@'166]:='v';
513 xchr[@'167]:='w';@/
514 xchr[@'170]:='x';
515 xchr[@'171]:='y';
516 xchr[@'172]:='z';
517 xchr[@'173]:='{';
518 xchr[@'174]:='|';
519 xchr[@'175]:='}';
520 xchr[@'176]:='~';
521 for i:=@'177 to 255 do xchr[i]:='?';
522
523 @ The following system-independent code makes the |xord| array contain a
524 suitable inverse to the information in |xchr|.
525
526 @<Set init...@>=
527 for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
528 for i:=" " to "~" do xord[xchr[i]]:=i;
529
530 @* Reporting errors to the user.
531 The \.{\title} processor does not verify that every single bit read from
532 one of its binary input files is meaningful and consistent; there are
533 other programs, e.g., \.{DVItype}, \.{TFtoPL}, and \.{VFtoPL}, specially
534 designed for that purpose.
535
536 On the other hand, \.{\title} is designed to avoid unpredictable results
537 due to undetected arithmetic overflow, or due to violation of integer
538 subranges or array bounds under {\it all\/} circumstances. Thus a fair
539 amount of checking is done when reading and analyzing the input data,
540 even in cases where such checking reduces the efficiency of the program
541 to some extent.
542
543 @ A global variable called |history| will contain one of four values
544 at the end of every run: |spotless| means that no unusual messages were
545 printed; |harmless_message| means that a message of possible interest
546 was printed but no serious errors were detected; |error_message| means that
547 at least one error was found; |fatal_message| means that the program
548 terminated abnormally. The value of |history| does not influence the
549 behavior of the program; it is simply computed for the convenience
550 of systems that might want to use such information.
551
552 @d spotless=0 {|history| value for normal jobs}
553 @d harmless_message=1 {|history| value when non-serious info was printed}
554 @d error_message=2 {|history| value when an error was noted}
555 @d fatal_message=3 {|history| value when we had to stop prematurely}
556 @#
557 @d mark_harmless==@t@>@+if history=spotless then history:=harmless_message
558 @d mark_error==history:=error_message
559 @d mark_fatal==history:=fatal_message
560
561 @<Glob...@>=@!history:spotless..fatal_message; {how bad was this run?}
562
563 @ @<Set init...@>=history:=spotless;
564
565 @ If an input (\.{DVI}, \.{TFM}, \.{VF}, or other) file is badly malformed,
566 the whole process must be aborted; \.{\title} will give up, after issuing
567 an error message about what caused the error. These messages will, however,
568 in most cases just indicate which input file caused the error. One of the
569 programs \.{DVItype}, \.{TFtoPL}, or \.{VFtoVP} should then be used to
570 diagnose the error in full detail.
571
572 Such errors might be discovered inside of subroutines inside of subroutines,
573 so a procedure called |jump_out| has been introduced. This procedure, which
574 transfers control to the label |final_end| at the end of the program,
575 contains the only non-local |@!goto| statement in \.{\title}.
576 @^system dependencies@>
577 Some \PASCAL\ compilers do not implement non-local |goto| statements. In
578 such cases the |goto final_end| in |jump_out| should simply be replaced
579 by a call on some system procedure that quietly terminates the program.
580 @^system dependencies@>
581
582 @d abort(#)==begin print_ln(' ',#,'.'); jump_out;
583 end
584
585 @<Error handling...@>=
586 @<Basic printing procedures@>@;
587 procedure close_files_and_terminate; forward;
588 @#
589 procedure jump_out;
590 begin mark_fatal; close_files_and_terminate;
591 goto final_end;
592 end;
593
594 @ Sometimes the program's behavior is far different from what it should
595 be, and \.{\title} prints an error message that is really for the
596 \.{\title} maintenance person, not the user. In such cases the program
597 says |confusion(|indication of where we are|)|.
598
599 @<Error handling...@>=
600 procedure confusion(@!p:pckt_pointer);
601 begin print(' !This can''t happen ('); print_packet(p); print_ln(').');
602 @.This can't happen@>
603 jump_out;
604 end;
605
606 @ An overflow stop occurs if \.{\title}'s tables aren't large enough.
607
608 @<Error handling...@>=
609 procedure overflow(@!p:pckt_pointer;@!n:int_16u);
610 begin print(' !Sorry, ',title,' capacity exceeded ['); print_packet(p);
611 @.Sorry, {\title} capacity exceeded@>
612 print_ln('=',n:1,'].');
613 jump_out;
614 end;
615
616 @* Binary data and binary files.
617 A detailed description of the \.{DVI} file format can be found in the
618 documentation of \TeX, \.{DVItype}, or \.{GFtoDVI}; here we just define
619 symbolic names for some of the \.{DVI} command bytes.
620
621 @d set_char_0=0 {typeset character 0 and move right}
622 @d set1=128 {typeset a character and move right}
623 @d set_rule=132 {typeset a rule and move right}
624 @d put1=133 {typeset a character}
625 @d put_rule=137 {typeset a rule}
626 @d nop=138 {no operation}
627 @d bop=139 {beginning of page}
628 @d eop=140 {ending of page}
629 @d push=141 {save the current positions}
630 @d pop=142 {restore previous positions}
631 @d right1=143 {move right}
632 @d w0=147 {move right by |w|}
633 @d w1=148 {move right and set |w|}
634 @d x0=152 {move right by |x|}
635 @d x1=153 {move right and set |x|}
636 @d down1=157 {move down}
637 @d y0=161 {move down by |y|}
638 @d y1=162 {move down and set |y|}
639 @d z0=166 {move down by |z|}
640 @d z1=167 {move down and set |z|}
641 @d fnt_num_0=171 {set current font to 0}
642 @d fnt1=235 {set current font}
643 @d xxx1=239 {extension to \.{DVI} primitives}
644 @d xxx4=242 {potentially long extension to \.{DVI} primitives}
645 @d fnt_def1=243 {define the meaning of a font number}
646 @d pre=247 {preamble}
647 @d post=248 {postamble beginning}
648 @d post_post=249 {postamble ending}
649 @#
650 @d dvi_id=2 {identifies \.{DVI} files}
651 @d dvi_pad=223 {pad bytes at end of \.{DVI} file}
652
653 @ A \.{DVI}, \.{VF}, or \.{TFM} file is a sequence of 8-bit bytes.
654 The bytes appear physically in what is called a `|packed file of 0..255|'
655 in \PASCAL\ lingo. One, two, three, or four consecutive bytes are often
656 interpreted as (signed or unsigned) integers.
657 We might as well define the corresponding data types.
658 @!@^system dependencies@>
659
660 @<Types...@>=
661 @!signed_byte=-@"80..@"7F; {signed one-byte quantity}
662 @!eight_bits=0..@"FF; {unsigned one-byte quantity}
663 @!signed_pair=-@"8000..@"7FFF; {signed two-byte quantity}
664 @!sixteen_bits=0..@"FFFF; {unsigned two-byte quantity}
665 @!signed_trio=-@"800000..@"7FFFFF; {signed three-byte quantity}
666 @!twentyfour_bits=0..@"FFFFFF; {unsigned three-byte quantity}
667 @!signed_quad=int_32; {signed four-byte quantity}
668
669 @ Packing is system dependent, and many \PASCAL\ systems fail to implement
670 such files in a sensible way (at least, from the viewpoint of producing
671 good production software). For example, some systems treat all
672 byte-oriented files as text, looking for end-of-line marks and such
673 things. Therefore some system-dependent code is often needed to deal with
674 binary files, even though most of the program in this section of
675 \.{\title} is written in standard \PASCAL.
676 @^system dependencies@>
677
678 One common way to solve the problem is to consider files of |integer|
679 numbers, and to convert an integer in the range $-2^{31}\L x<2^{31}$ to
680 a sequence of four bytes $(a,b,c,d)$ using the following code, which
681 avoids the controversial integer division of negative numbers:
682 $$\vbox{\halign{#\hfil\cr
683 |if x>=0 then a:=x div @'100000000|\cr
684 |else begin x:=(x+@'10000000000)+@'10000000000; a:=x div @'100000000+128;|\cr
685 \quad|end|\cr
686 |x:=x mod @'100000000;|\cr
687 |b:=x div @'200000; x:=x mod @'200000;|\cr
688 |c:=x div @'400; d:=x mod @'400;|\cr}}$$
689 The four bytes are then kept in a buffer and output one by one. (On 36-bit
690 computers, an additional division by 16 is necessary at the beginning.
691 Another way to separate an integer into four bytes is to use/abuse
692 \PASCAL's variant records, storing an integer and retrieving bytes that are
693 packed in the same place; {\sl caveat implementor!\/}) It is also desirable
694 in some cases to read a hundred or so integers at a time, maintaining a
695 larger buffer.
696
697 @ We shall stick to simple \PASCAL\ in the standard version of this program,
698 for reasons of clarity, even if such simplicity is sometimes unrealistic.
699
700 @<Types...@>=
701 @!byte_file=packed file of eight_bits; {files that contain binary data}
702
703 @ For some operating systems it may be convenient or even necessary to
704 close the input files.
705
706 @d close_in(#)==do_nothing {close an input file}
707
708 @ Character packets extracted from \.{VF} files will be stored in a large
709 array |byte_mem|. Other packets of bytes, e.g., character packets
710 extracted from a \.{GF} or \.{PK} or \.{PXL} file could be stored in the
711 same way. A `|pckt_pointer|' variable, which signifies a packet,
712 is an index into another array |pckt_start|. The actual sequence of bytes
713 in the packet pointed to by |p| appears in positions |pckt_start[p]| to
714 |pckt_start[p+1]-1|, inclusive, in |byte_mem|.
715
716 Packets will also be used to store sequences of |ASCII_code|s; in this
717 respect the |byte_mem| array is very similar to \TeX's string pool and
718 part of the following code has, in fact, been copied more or less
719 verbatim from \TeX.
720
721 In other respects the packets resemble the identifiers used by
722 \.{TANGLE} and \.{WEAVE} (also stored in an array called |byte_mem|)
723 since there is, in general, at most one packet with a given contents;
724 thus part of the code below has been adapted from the corresponding code
725 in these programs.
726
727 Some \PASCAL\ compilers won't pack integers into a single byte unless the
728 integers lie in the range |-128..127|. To accommodate such systems we
729 access the array |byte_mem| only via macros that can easily be redefined.
730 @^system dependencies@>
731
732 @d bi(#) == # {convert from |eight_bits| to |packed_byte|}
733 @d bo(#) == # {convert from |packed_byte| to |eight_bits|}
734
735 @<Types...@>=
736 @!packed_byte = eight_bits; {elements of |byte_mem| array}
737 @!byte_pointer = 0..max_bytes; {an index into |byte_mem|}
738 @!pckt_pointer = 0..max_packets; {an index into |pckt_start|}
739
740 @ The global variable |byte_ptr| points to the first unused location in
741 |byte_mem| and |pckt_ptr| points to the first unused location in
742 |pckt_start|.
743
744 @<Globals...@>=
745 @!byte_mem: packed array [byte_pointer] of packed_byte; {bytes of packets}
746 @!pckt_start: array [pckt_pointer] of byte_pointer;
747 {directory into |byte_mem|}
748 @!byte_ptr: byte_pointer;
749 @!pckt_ptr: pckt_pointer;
750
751 @ Several of the elementary operations with packets are performed using
752 \.{WEB} macros instead of \PASCAL\ procedures, because many of the
753 operations are done quite frequently and we want to avoid the
754 overhead of procedure calls. For example, here is
755 a simple macro that computes the length of a packet.
756 @.WEB@>
757
758 @d pckt_length(#)==(pckt_start[#+1]-pckt_start[#]) {the number of bytes
759 in packet number \#}
760
761 @ Packets are created by appending bytes to |byte_mem|.
762 The |append_byte| macro, defined here, does not check to see if the
763 value of |byte_ptr| has gotten too high; this test is supposed to be
764 made before |append_byte| is used. There is also a |flush_byte|
765 macro, which erases the last byte appended.
766
767 To test if there is room to append |l| more bytes to |byte_mem|,
768 we shall write |pckt_room(l)|, which aborts \.{\title} and gives an
769 apologetic error message if there isn't enough room.
770
771 @d append_byte(#) == {put byte \# at the end of |byte_mem|}
772 begin byte_mem[byte_ptr]:=bi(#); incr(byte_ptr);
773 end
774 @d flush_byte == decr(byte_ptr) {forget the last byte in |byte_mem|}
775 @d pckt_room(#) == {make sure that |byte_mem| hasn't overflowed}
776 if max_bytes-byte_ptr<# then overflow(str_bytes,max_bytes)
777 @#
778 @d append_one(#) ==
779 begin pckt_room(1); append_byte(#);
780 end
781
782 @ The length of the current packet is called |cur_pckt_length|:
783
784 @d cur_pckt_length == (byte_ptr - pckt_start[pckt_ptr])
785
786 @ Once a sequence of bytes has been appended to |byte_mem|, it
787 officially becomes a packet when the |make_packet| function is called.
788 This function returns as its value the identification number of either
789 an existing packet with the same contents or, if no such packet exists,
790 of the new packet. Thus two packets have the same contents if and only
791 if they have the same identification number. In order to locate the
792 packet with a given contents, or to find out that no such packet exists,
793 we need a hash table. The hash table is kept by the method of simple
794 chaining, where the heads of the individual lists appear in the |p_hash|
795 array. If |h| is a hash code, the hash table list starts at |p_hash[h]|
796 and proceeds through |p_link| pointers.
797
798 @d hash_size=353 {should be prime, must be |>256|}
799
800 @<Types...@>=
801 @!hash_code=0..hash_size;
802
803 @ @<Glob...@>=
804 @!p_link:array[pckt_pointer] of pckt_pointer; {hash table}
805 @!p_hash:array[hash_code] of pckt_pointer;
806
807 @ Initially |byte_mem| and all the hash lists are empty; |empty_packet|
808 is the empty packet.
809
810 @d empty_packet=0 {the empty packet}
811 @d invalid_packet==max_packets {used when there is no packet}
812
813 @<Set init...@>=
814 pckt_ptr:=1; byte_ptr:=1;
815 pckt_start[0]:=1; pckt_start[1]:=1;
816 for h:=0 to hash_size-1 do p_hash[h]:=0;
817
818 @ @<Local variables for init...@>=
819 @!h:hash_code; {index into hash-head arrays}
820
821 @ Here now is the |make_packet| function used to create packets (and
822 strings).
823
824 @p function make_packet:pckt_pointer;
825 label found;
826 var i,@!k:byte_pointer; {indices into |byte_mem|}
827 @!h:hash_code; {hash code}
828 @!s,@!l:byte_pointer; {start and length of the given packet}
829 @!p:pckt_pointer; {where the packet is being sought}
830 begin s:=pckt_start[pckt_ptr]; l:=byte_ptr-s; {compute start and length}
831 if l=0 then p:=empty_packet
832 else begin @<Compute the packet hash code |h|@>;
833 @<Compute the packet location |p|@>;
834 if pckt_ptr=max_packets then overflow(str_packets,max_packets);
835 incr(pckt_ptr); pckt_start[pckt_ptr]:=byte_ptr;
836 end;
837 found:make_packet:=p;
838 end;
839
840 @ A simple hash code is used: If the sequence of bytes is
841 $b_1b_2\ldots b_n$, its hash value will be
842 $$(2^{n-1}b_1+2^{n-2}b_2+\cdots+b_n)\,\bmod\,|hash_size|.$$
843
844 @<Compute the packet hash...@>=
845 h:=bo(byte_mem[s]); i:=s+1;
846 while i<byte_ptr do
847 begin h:=(h+h+bo(byte_mem[i])) mod hash_size; incr(i);
848 end
849
850 @ If the packet is new, it will be placed in position |p=pckt_ptr|,
851 otherwise |p| will point to its existing location.
852
853 @<Compute the packet location...@>=
854 p:=p_hash[h];
855 while p<>0 do
856 begin if pckt_length(p)=l then
857 @<Compare packet |p| with current packet, |goto found| if equal@>;
858 p:=p_link[p];
859 end;
860 p:=pckt_ptr; {the current packet is new}
861 p_link[p]:=p_hash[h]; p_hash[h]:=p {insert |p| at beginning of hash list}
862
863 @ @<Compare packet |p|...@>=
864 begin i:=s; k:=pckt_start[p];
865 while (i<byte_ptr)and(byte_mem[i]=byte_mem[k]) do
866 begin incr(i); incr(k);
867 end;
868 if i=byte_ptr then {all bytes agree}
869 begin byte_ptr:=pckt_start[pckt_ptr]; goto found;
870 end;
871 end
872
873 @ Some packets are initialized with predefined strings of |ASCII_code|s;
874 a few macros permit us to do the initialization with a compact program.
875 Since this initialization is done when |byte_mem| is still empty, and
876 since |byte_mem| is supposed to be large enough for all the predefined
877 strings, |pckt_room| is used only if we are debugging.
878
879 @d pid0(#)==#:=make_packet
880 @d pid1(#)==byte_mem[byte_ptr-1]:=bi(#); pid0
881 @d pid2(#)==byte_mem[byte_ptr-2]:=bi(#); pid1
882 @d pid3(#)==byte_mem[byte_ptr-3]:=bi(#); pid2
883 @d pid4(#)==byte_mem[byte_ptr-4]:=bi(#); pid3
884 @d pid5(#)==byte_mem[byte_ptr-5]:=bi(#); pid4
885 @d pid6(#)==byte_mem[byte_ptr-6]:=bi(#); pid5
886 @d pid7(#)==byte_mem[byte_ptr-7]:=bi(#); pid6
887 @d pid8(#)==byte_mem[byte_ptr-8]:=bi(#); pid7
888 @d pid9(#)==byte_mem[byte_ptr-9]:=bi(#); pid8
889 @d pid10(#)==byte_mem[byte_ptr-10]:=bi(#); pid9
890 @#
891 @d pid_init(#)==
892 @!debug pckt_room(#); @+ gubed @;
893 Incr(byte_ptr)(#)
894 @#
895 @d id1==pid_init(1); pid1
896 @d id2==pid_init(2); pid2
897 @d id3==pid_init(3); pid3
898 @d id4==pid_init(4); pid4
899 @d id5==pid_init(5); pid5
900 @d id6==pid_init(6); pid6
901 @d id7==pid_init(7); pid7
902 @d id8==pid_init(8); pid8
903 @d id9==pid_init(9); pid9
904 @d id10==pid_init(10); pid10
905
906 @ Here we initialize some strings used as argument of the |overflow| and
907 |confusion| procedures.
908
909 @<Initialize predefined strings@>=
910 id5("f")("o")("n")("t")("s")(str_fonts);
911 id5("c")("h")("a")("r")("s")(str_chars);
912 id6("w")("i")("d")("t")("h")("s")(str_widths);
913 id7("p")("a")("c")("k")("e")("t")("s")(str_packets);
914 id5("b")("y")("t")("e")("s")(str_bytes);
915 id9("r")("e")("c")("u")("r")("s")("i")("o")("n")(str_recursion);
916 id5("s")("t")("a")("c")("k")(str_stack);
917 id10("n")("a")("m")("e")("l")("e")("n")("g")("t")("h")(str_name_length);
918
919 @ @<Glob...@>=
920 @!str_fonts,@!str_chars,@!str_widths,@!str_packets,@!str_bytes,
921 @!str_recursion,@!str_stack,@!str_name_length:pckt_pointer;
922
923 @ Some packets, e.g., the preamble comments of \.{DVI} and \.{VF} files,
924 are needed only temporarily. In such cases |new_packet| is used to
925 create a packet (which might duplicate an existing packet) and
926 |flush_packet| is used to discard it; the calls to |new_packet| and
927 |flush_packet| must occur in balanced pairs, without any intervening
928 calls to |make_packet|.
929
930 @p function new_packet: pckt_pointer;
931 begin if pckt_ptr=max_packets then overflow(str_packets,max_packets);
932 new_packet:=pckt_ptr; incr(pckt_ptr); pckt_start[pckt_ptr]:=byte_ptr;
933 end;
934 @#
935 procedure flush_packet;
936 begin decr(pckt_ptr); byte_ptr:=pckt_start[pckt_ptr];
937 end;
938
939 @ The |print_packet| procedure prints the contents of a packet; such a
940 packet should, of course, consists of a sequence of |ASCII_code|s.
941
942 @<Basic printing...@>=
943 procedure print_packet(p:pckt_pointer);
944 var k:byte_pointer;
945 begin for k:=pckt_start[p] to pckt_start[p+1]-1 do
946 print(xchr[bo(byte_mem[k])]);
947 end;
948
949 @ When we interpret a packet we will use two (global or local) variables:
950 |cur_loc| will point to the byte to be used next, and |cur_limit| will
951 point to the start of the next packet. The macro |pckt_extract| will be
952 used to extract one byte; it should, however, never be used with
953 |cur_loc>=cur_limit|.
954
955 @d pckt_extract(#) ==
956 @!debug if cur_loc>=cur_limit then confusion(str_packets) @+ else @/
957 gubed @;
958 begin #:=bo(byte_mem[cur_loc]); incr(cur_loc); @+ end
959
960 @<Globals...@>=
961 @!cur_pckt: pckt_pointer; {the current packet}
962 @!cur_loc: byte_pointer; {current location in a packet}
963 @!cur_limit: byte_pointer; {start of next packet}
964
965 @ We will need routines to extract one, two, three, or four bytes from
966 |byte_mem|, from the \.{DVI} file, or from a \.{VF} file and assemble
967 them into (signed or unsigned) integers and these routines should be
968 optimized for efficiency. Here we define \.{WEB} macros to be used for
969 the body of these routines; thus the changes for system dependent
970 optimization have to be applied only once.
971 @^system dependencies@>
972 @^optimization@>
973
974 In addition we demonstrates how these macros can be used to define
975 functions that extract one, two, three, or four bytes from a character
976 packet and assemble them into signed or unsigned integers (assuming that
977 |cur_loc| and |cur_limit| are initialized suitably).
978
979 @d begin_byte(#) ==
980 var a:eight_bits;
981 begin #(a)
982 @d comp_sbyte(#) == if a<128 then #:=a @+ else #:=a-256
983 @d comp_ubyte(#) == #:=a
984 @f begin_byte == begin
985
986 @p function pckt_sbyte:int_8; {returns the next byte, signed}
987 @!begin_byte(pckt_extract); comp_sbyte(pckt_sbyte);
988 end;
989 @#
990 function pckt_ubyte:int_8u; {returns the next byte, unsigned}
991 @!begin_byte(pckt_extract); comp_ubyte(pckt_ubyte);
992 end;
993
994 @ @d begin_pair(#) ==
995 var a,@!b:eight_bits;
996 begin #(a); #(b)
997 @d comp_spair(#) == if a<128 then #:=a*256+b @+ else #:=(a-256)*256+b
998 @d comp_upair(#) == #:=a*256+b
999 @f begin_pair == begin
1000
1001 @p function pckt_spair:int_16; {returns the next two bytes, signed}
1002 @!begin_pair(pckt_extract); comp_spair(pckt_spair);
1003 end;
1004 @#
1005 function pckt_upair:int_16u; {returns the next two bytes, unsigned}
1006 @!begin_pair(pckt_extract); comp_upair(pckt_upair);
1007 end;
1008
1009 @ @d begin_trio(#) ==
1010 var a,@!b,@!c:eight_bits;
1011 begin #(a); #(b); #(c)
1012 @d comp_strio(#) ==
1013 if a<128 then #:=(a*256+b)*256+c @+ else #:=((a-256)*256+b)*256+c
1014 @d comp_utrio(#) == #:=(a*256+b)*256+c
1015 @f begin_trio == begin
1016
1017 @p function pckt_strio:int_24; {returns the next three bytes, signed}
1018 @!begin_trio(pckt_extract); comp_strio(pckt_strio);
1019 end;
1020 @#
1021 function pckt_utrio:int_24u; {returns the next three bytes, unsigned}
1022 @!begin_trio(pckt_extract); comp_utrio(pckt_utrio);
1023 end;
1024
1025 @ @d begin_quad(#) ==
1026 var a,@!b,@!c,@!d:eight_bits;
1027 begin #(a); #(b); #(c); #(d)
1028 @d comp_squad(#) ==
1029 if a<128 then #:=((a*256+b)*256+c)*256+d
1030 else #:=(((a-256)*256+b)*256+c)*256+d
1031 @f begin_quad == begin
1032
1033 @p function pckt_squad:int_32; {returns the next four bytes, signed}
1034 @!begin_quad(pckt_extract); comp_squad(pckt_squad);
1035 end;
1036
1037 @ A similar set of routines is needed for the inverse task of
1038 decomposing a \.{DVI} command into a sequence of bytes to be appended
1039 to |byte_mem| or, in the case of \.{DVIcopy}, to be written to the
1040 output file. Again we define \.{WEB} macros to be used for the body
1041 of these routines; thus the changes for system dependent optimization
1042 have to be applied only once.
1043 @^system dependencies@>
1044 @^optimization@>
1045
1046 First, the |pckt_one| outputs one byte, negative values are represented
1047 in two's complement notation.
1048
1049 @d begin_one == begin
1050 @d comp_one(#) ==
1051 if x<0 then Incr(x)(256);
1052 #(x)
1053 @f begin_one == begin
1054
1055 @p @!device
1056 procedure pckt_one(@!x:int_32); {output one byte}
1057 @!begin_one; pckt_room(1); comp_one(append_byte);
1058 end;
1059 ecived
1060
1061 @ The |pckt_two| outputs two bytes, negative values are represented in
1062 two's complement notation.
1063
1064 @d begin_two == begin
1065 @d comp_two(#) ==
1066 if x<0 then Incr(x)(@"10000);
1067 #(x div @"100); #(x mod @"100)
1068 @f begin_two == begin
1069
1070 @p @!device
1071 procedure pckt_two(@!x:int_32); {output two byte}
1072 @!begin_two; pckt_room(2); comp_two(append_byte);
1073 end;
1074 ecived
1075
1076 @ The |pckt_four| procedure outputs four bytes in two's complement
1077 notation, without risking arithmetic overflow.
1078
1079 @d begin_four == begin
1080 @d comp_four(#) ==
1081 if x>=0 then #(x div @"1000000)
1082 else begin Incr(x)(@"40000000); Incr(x)(@"40000000);
1083 #((x div @"1000000) + 128);
1084 end;
1085 x:=x mod @"1000000; #(x div @"10000);
1086 x:=x mod @"10000; #(x div @"100);
1087 #(x mod @"100)
1088 @f begin_four == begin
1089
1090 @p procedure pckt_four(@!x:int_32); {output four bytes}
1091 @!begin_four; pckt_room(4); comp_four(append_byte);
1092 end;
1093
1094 @ Next, the |pckt_char| procedure outputs a |set_char| or \\{set} command
1095 or, if |upd=false|, a |put| command.
1096
1097 @d begin_char ==
1098 var o:eight_bits; {|set1| or |put1|}
1099 begin
1100 @d comp_char(#) ==
1101 if (not upd)or(res>127)or(ext<>0) then
1102 begin o:=dvi_char_cmd[upd]; {|set1| or |put1|}
1103 if ext<0 then Incr(ext)(@"1000000);
1104 if ext=0 then #(o) @+ else @;
1105 begin if ext<@"100 then #(o+1) @+ else @;
1106 begin if ext<@"10000 then #(o+2) @+ else @;
1107 begin #(o+3); #(ext div @"10000); ext:=ext mod @"10000;
1108 end;
1109 #(ext div @"100); ext:=ext mod @"100;
1110 end;
1111 #(ext);
1112 end;
1113 end;
1114 #(res)
1115 @f begin_char == begin
1116
1117 @p procedure pckt_char(@!upd:boolean;@!ext:int_32;@!res:eight_bits);
1118 {output \\{set} or |put|}
1119 @!begin_char; pckt_room(5); comp_char(append_byte);
1120 end;
1121
1122 @ Then, the |pckt_unsigned| procedure outputs a |fnt| or |xxx|
1123 command with its first parameter (normally unsigned); a |fnt| command
1124 is converted into |fnt_num| whenever this is possible.
1125
1126 @d begin_unsigned == begin
1127 @d comp_unsigned(#) ==
1128 if (x<@"100)and(x>=0) then
1129 if (o=fnt1)and(x<64) then Incr(x)(fnt_num_0) @+ else #(o)
1130 else
1131 begin if (x<@"10000)and(x>=0) then #(o+1) @+ else @;
1132 begin if (x<@"1000000)and(x>=0) then #(o+2) @+ else @;
1133 begin #(o+3);
1134 if x>=0 then #(x div @"1000000)
1135 else begin Incr(x)(@"40000000); Incr(x)(@"40000000);
1136 #((x div @"1000000) + 128);
1137 end;
1138 x:=x mod @"1000000;
1139 end;
1140 #(x div @"10000); x:=x mod @"10000;
1141 end;
1142 #(x div @"100); x:=x mod @"100;
1143 end;
1144 #(x)
1145 @f begin_unsigned == begin
1146
1147 @p procedure pckt_unsigned(@!o:eight_bits;@!x:int_32);
1148 {output |fnt_num|, |fnt|, or |xxx|}
1149 @!begin_unsigned; pckt_room(5); comp_unsigned(append_byte);
1150 end;
1151
1152 @ Finally, the |pckt_signed| procedure outputs a movement (|right|, |w|,
1153 |x|, |down|, |y|, or |z|) command with its (signed) parameter.
1154
1155 @d begin_signed ==
1156 var xx:int_31; {`absolute value' of |x|}
1157 begin
1158 @d comp_signed(#) ==
1159 if x>=0 then xx:=x @+ else xx:=-(x+1);
1160 if xx<@"80 then
1161 begin #(o); @+ if x<0 then Incr(x)(@"100); @+ end
1162 else begin if xx<@"8000 then
1163 begin #(o+1); @+ if x<0 then Incr(x)(@"10000); @+ end
1164 else begin if xx<@"800000 then
1165 begin #(o+2); @+ if x<0 then Incr(x)(@"1000000); @+ end
1166 else begin #(o+3);
1167 if x>=0 then #(x div @"1000000)
1168 else begin x:=@"7FFFFFFF-xx; #((x div @"1000000) + 128); @+ end;
1169 x:=x mod @"1000000;
1170 end;
1171 #(x div @"10000); x:=x mod @"10000;
1172 end;
1173 #(x div @"100); x:=x mod @"100;
1174 end;
1175 #(x)
1176 @f begin_signed == begin
1177
1178 @p procedure pckt_signed(@!o:eight_bits;@!x:int_32);
1179 {output |right|, |w|, |x|, |down|, |y|, or |z|}
1180 @!begin_signed; pckt_room(5); comp_signed(append_byte);
1181 end;
1182
1183 @ The |hex_packet| procedure prints the contents of a packet in
1184 hexadecimal form.
1185
1186 @<Basic printing...@>=
1187 @!debug procedure hex_packet(@!p:pckt_pointer); {prints a packet in hex}
1188 var j,@!k,@!l:byte_pointer; {indices into |byte_mem|}
1189 @!d:int_8u;
1190 begin j:=pckt_start[p]-1; k:=pckt_start[p+1]-1;
1191 print_ln(' packet=',p:1,' start=',j+1:1,' length=',k-j:1);
1192 for l:=j+1 to k do
1193 begin d:=(bo(byte_mem[l])) div 16;
1194 if d<10 then print(xchr[d+"0"]) @+ else print(xchr[d-10+"A"]);
1195 d:=(bo(byte_mem[l])) mod 16;
1196 if d<10 then print(xchr[d+"0"]) @+ else print(xchr[d-10+"A"]);
1197 if (l=k)or(((l-j) mod 16)=0) then new_line
1198 else if ((l-j) mod 4)=0 then print(' ')
1199 else print(' ');
1200 end;
1201 end;
1202 gubed
1203
1204 @* File names.
1205 The structure of file names is different for different systems; therefore
1206 this part of the program will, in most cases, require system dependent
1207 modifications. Here we assume that a file name consists of three parts:
1208 an area or directory specifying where the file can be found, a name
1209 proper and an extension; \.{\title} assumes that these three parts appear
1210 in order stated above but this need not be true in all cases.
1211
1212 The font names extracted from \.{DVI} and \.{VF} files consist of an area
1213 part and a name proper; these are stored as packets consisting of the
1214 length of the area part followed by the area and the name proper.
1215 When we print an external font name we simple print the area and the name
1216 contained in the `file name packet' without delimiter between them.
1217 This may need to be modified for some systems.
1218 @^system dependencies@>
1219
1220 @<Basic printing...@>=
1221 procedure print_font(@!f:font_number);
1222 var p:pckt_pointer; {the font name packet}
1223 @!k:byte_pointer; {index into |byte_mem|}
1224 @!m:int_31; {font magnification}
1225 begin print(' = '); p:=font_name(f);
1226 for k:=pckt_start[p]+1 to pckt_start[p+1]-1 do
1227 print(xchr[bo(byte_mem[k])]);
1228 m:=round((font_scaled(f)/font_design(f))*out_mag);
1229 if m<>1000 then print(' scaled ',m:1);
1230 end;
1231
1232 @ Before a font file can be opened for input we must build a string
1233 with its external name.
1234
1235 @<Glob...@>=
1236 @!cur_name:packed array[1..name_length] of char; {external name,
1237 with no lower case letters}
1238 @!l_cur_name:int_15; {this many characters are actually relevant in
1239 |cur_name|}
1240
1241 @ For \.{TFM} and \.{VF} files we just append the appropriate extension
1242 to the file name packet; in addition a system dependent area part
1243 (usually different for \.{TFM} and \.{VF} files) is prepended if
1244 the file name packet contains no area part.
1245 @^system dependencies@>
1246
1247 @d append_to_name(#)==
1248 if l_cur_name<name_length then
1249 begin incr(l_cur_name); cur_name[l_cur_name]:=#;
1250 end
1251 else overflow(str_name_length,name_length)
1252 @d make_font_name_end(#)==
1253 append_to_name(#[l]); make_name
1254 @d make_font_name(#)==
1255 l_cur_name:=0; for l:=1 to # do make_font_name_end
1256
1257 @ For files with character raster data (e.g., \.{GF} or \.{PK} files) the
1258 extension and\slash or area part will in most cases depend on the
1259 resolution of the output device (corrected for font magnification).
1260 If the special character |res_char| occurs in the extension and\slash or
1261 default area, a character string representing the device resolution will
1262 be substituted.
1263 @^system dependencies@>
1264
1265 @d res_char=='?' {character to be replaced by font resolution}
1266 @d res_ASCII="?" {|xord[res_char]|}
1267 @#
1268 @d append_res_to_name(#)==
1269 begin c:=#;
1270 @!device if c=res_char then
1271 for ll:=n_res_digits downto 1 do append_to_name(res_digits[ll])
1272 else ecived@;@/
1273 append_to_name(c);
1274 end
1275 @d make_font_res_end(#)==
1276 append_res_to_name(#[l]); make_name
1277 @d make_font_res(#)==
1278 make_res; l_cur_name:=0; for l:=1 to # do make_font_res_end
1279
1280 @ @<Glob...@>=
1281 @!device
1282 @!f_res:int_16u; {font resolution}
1283 @!res_digits:array [1..5] of char;
1284 @!n_res_digits:int_7; {number of significant characters in |res_digits|}
1285 ecived
1286
1287 @ The |make_res| procedure creates a sequence of characters representing
1288 to the font resolution |f_res|.
1289
1290 @p @!device procedure make_res;
1291 var r:int_16u;
1292 begin n_res_digits:=0; r:=f_res;
1293 repeat incr(n_res_digits);
1294 res_digits[n_res_digits]:=xchr["0"+(r mod 10)]; r:=r div 10;
1295 until r=0;
1296 end;
1297 ecived
1298
1299 @ The |make_name| procedure used to build the external file name. The
1300 global variable |l_cur_name| contains the length of a default area
1301 which has been copied to |cur_name| before |make_name| is called.
1302 @^system dependencies@>
1303
1304 @p procedure make_name(@!e:pckt_pointer);
1305 var b:eight_bits; {a byte extracted from |byte_mem|}
1306 @!n:pckt_pointer; {file name packet}
1307 @!cur_loc,@!cur_limit:byte_pointer; {indices into |byte_mem|}
1308 @!device
1309 @!ll:int_15; {loop index}
1310 ecived@;@/
1311 @!c:char; {a character to be appended to |cur_name|}
1312 begin n:=font_name(cur_fnt);
1313 cur_loc:=pckt_start[n]; cur_limit:=pckt_start[n+1];
1314 pckt_extract(b); {length of area part}
1315 if b>0 then l_cur_name:=0;
1316 while cur_loc<cur_limit do
1317 begin pckt_extract(b);
1318 if (b>="a")and(b<="z") then Decr(b)(("a"-"A")); {convert to upper case}
1319 append_to_name(xchr[b]);
1320 end;
1321 cur_loc:=pckt_start[e]; cur_limit:=pckt_start[e+1];
1322 while cur_loc<cur_limit do
1323 begin pckt_extract(b); append_res_to_name(xchr[b]);
1324 end;
1325 while l_cur_name<name_length do
1326 begin incr(l_cur_name); cur_name[l_cur_name]:=' ';
1327 end;
1328 end;
1329
1330 @* Font data.
1331 \.{DVI} file format does not include information about character widths, since
1332 that would tend to make the files a lot longer. But a program that reads
1333 a \.{DVI} file is supposed to know the widths of the characters that appear
1334 in \\{set\_char} commands. Therefore \.{\title} looks at the font metric
1335 (\.{TFM}) files for the fonts that are involved.
1336 @.TFM {\rm files}@>
1337
1338 The character-width data appears also in other files (e.g., in \.{VF} files
1339 or in \.{GF} and \.{PK} files that specify bit patterns for digitized
1340 characters); thus, it is usually possible for \.{DVI} reading programs
1341 to get by with accessing only one file per font. For \.{VF} reading
1342 programs there is, however, a problem: (1)~when reading the character
1343 packets from a \.{VF} file the \.{TFM} width for its local fonts should
1344 be known in order to analyze and optimize the packets (e.g., determine
1345 if a packet must indeed be enclosed with |push| and |pop| as implied by
1346 the \.{VF} format); and (2)~ in order to avoid infinite recursion such
1347 programs must not try to read a \.{VF} file for a font before a
1348 character from that font is actually used. Thus \.{\title} reads the
1349 \.{TFM} file whenever a new font is encountered and delays the decision
1350 whether this is a virtual font or not.
1351
1352 @ First of all we need to know for each font~|f| such things as its
1353 external name, design and scaled size, and the approximate size of
1354 inter-word spaces. In addition we need to know the range |bc..ec| of
1355 valid characters for this font, and for each character~|c| in~|f| we
1356 need to know if this character exists and if so what is the width of~|c|.
1357 Depending on the font type of~|f| we may want to know a few other things
1358 about character~|c| in~|f| such as the character packet from a \.{VF}
1359 file or the raster data from a \.{PK} file.
1360 @^font types@>
1361
1362 In \.{\title} we want to be able to handle the full range
1363 |@t$-2^{31}$@><=c<@t$2^{31}$@>| of character codes; each character code
1364 is decomposed into a character residue |0<=res<256| and character
1365 extension |@t$-2^{23}$@><=ext<@t$2^{23}$@>| such that |c=256*ext+res|.
1366 At present \.{VFtoVP}, \.{VPtoVF}, and the standard version of \TeX\ use
1367 only characters in the range |0<=c<256| (i.e., |ext=0|), there are,
1368 however, extensions of \TeX\ which use characters with |ext<>0|.
1369 In any case characters with |ext<>0| will be used rather infrequently
1370 and we want to handle this possibility without too much overhead.
1371
1372 Some of the data for each character~|c| depend only on its residue:
1373 first of all its width and escapement; others, such as \.{VF} packets or
1374 raster data will also depend on its extension. The later will be stored
1375 as packets in |byte_mem|, and the packets for characters with the same
1376 residue but different extension will be chained.
1377
1378 Thus we have to maintain several variables for each character
1379 residue~|bc<=res<=ec| from each font~|f|; we store each type of variable
1380 in a large array such that the array index |font_chars(f)+res| points to
1381 the value for characters with residue |res| from font~|f|.
1382
1383 @ Quite often a particular width value is shared by several characters in
1384 a font or even by characters from different fonts; the later will
1385 probably occur in particular for virtual fonts and the local fonts used
1386 by them. Thus the array |widths| is used to store all different \.{TFM}
1387 width values of all legal characters in all fonts; a variable of type
1388 |width_pointer| is an index into |widths| or is zero if a characters does
1389 not exist.
1390
1391 In order to locate a given width value we use again a hash
1392 table with simple chaining; this time the heads of the individual lists
1393 appear in the |w_hash| array and the lists proceed through |w_link|
1394 pointers.
1395
1396 @<Types...@>=
1397 @!width_pointer=0..max_widths; {an index into |widths|}
1398
1399 @ @<Glob...@>=
1400 @!widths:array[width_pointer] of int_32; {the different width values}
1401 @!w_link:array[width_pointer] of width_pointer; {hash table}
1402 @!w_hash:array[hash_code] of width_pointer;
1403 @!n_widths:width_pointer; {first unoccupied position in |widths|}
1404
1405 @ Initially the |widths| array and all the hash lists are empty, except
1406 for one entry: the width value zero; in addition we set |widths[0]:=0|.
1407
1408 @d invalid_width=0 {width pointer for invalid characters}
1409 @d zero_width=1 {a width pointer to the value zero}
1410
1411 @<Set init...@>=
1412 w_hash[0]:=1; w_link[1]:=0; widths[0]:=0; widths[1]:=0; n_widths:=2;
1413 for h:=1 to hash_size-1 do w_hash[h]:=0;
1414
1415 @ The |make_width| function returns an index into |widths| and, if
1416 necessary, adds a new width value; thus two characters will have the
1417 same |width_pointer| if and only if their widths agree.
1418
1419 @p function make_width(@!w:int_32):width_pointer;
1420 label found;
1421 var h:hash_code; {hash code}
1422 @!p:width_pointer; {where the identifier is being sought}
1423 @!x:int_16; {intermediate value}
1424 begin widths[n_widths]:=w;
1425 @<Compute the width hash code |h|@>;
1426 @<Compute the width location |p|, |goto| found unless the value is new@>;
1427 if n_widths=max_widths then overflow(str_widths,max_widths);
1428 incr(n_widths);
1429 found:make_width:=p;
1430 end;
1431
1432 @ A simple hash code is used: If the width value consists of the four
1433 bytes $b_0b_1b_2b_3$, its hash value will be
1434 $$(8*b_0+4*b_1+2*b_2+b_3)\,\bmod\,|hash_size|.$$
1435
1436 @<Compute the width hash...@>=
1437 if w>=0 then x:=w div @"1000000
1438 else begin w:=w+@"40000000; w:=w+@"40000000; x:=(w div @"1000000)+@"80;
1439 end;
1440 w:=w mod @"1000000; x:=x+x+(w div @"10000);
1441 w:=w mod @"10000; x:=x+x+(w div @"100);
1442 h:=(x+x+(w mod @"100)) mod hash_size
1443
1444 @ If the width is new, it has been placed into position |p=n_widths|,
1445 otherwise |p| will point to its existing location.
1446
1447 @<Compute the width location...@>=
1448 p:=w_hash[h];
1449 while p<>0 do
1450 begin if widths[p]=widths[n_widths] then goto found;
1451 p:=w_link[p];
1452 end;
1453 p:=n_widths; {the current width is new}
1454 w_link[p]:=w_hash[h]; w_hash[h]:=p {insert |p| at beginning of hash list}
1455
1456 @ The |char_widths| array is used to store the |width_pointer|s for all
1457 different characters among all fonts. The |char_packets| array is used
1458 to store the |pckt_pointer|s for all different characters among all
1459 fonts; they can point to character packets from \.{VF} files or, e.g.,
1460 raster packets from \.{PK} files.
1461
1462 @<Types...@>=
1463 @!char_offset=-255..max_chars; {|char_pointer| offset for a font}
1464 @!char_pointer=0..max_chars; {index into |char_widths| or similar arrays}
1465
1466 @ @<Glob...@>=
1467 @!char_widths:array[char_pointer] of width_pointer; {width pointers}
1468 @!char_packets:array[char_pointer] of pckt_pointer; {packet pointers}
1469 @!n_chars:char_pointer; {first unused position in |char_widths|}
1470
1471 @ @<Set init...@>=
1472 n_chars:=0;
1473
1474 @ The current number of known fonts is |nf|; each known font has an
1475 internal number |f|, where |0<=f<nf|. For the moment we need for each
1476 known font: |font_check|, |font_scaled|, |font_design|, |font_name|,
1477 |font_bc|, |font_ec|, |font_chars|, and |font_type|. Here |font_scaled|
1478 and |font_design| are measured in \.{DVI} units and |font_chars| is of
1479 type |char_offset|: the width pointer for character~|c| of the font is
1480 stored in |char_widths[char_offset+c]| (for |font_bc<=c<=font_ec|).
1481 Later on we will need additional information depending on the font type:
1482 \.{VF} or real (\.{GF}, \.{PK}, or \.{PXL}).
1483
1484 @<Types...@>=
1485 @!f_type=defined_font..max_font_type; {type of a font}
1486 @!font_number=0..max_fonts;
1487
1488 @ @<Glob...@>=
1489 @!nf:font_number;
1490
1491 @ These data are stored in several arrays and we use \.{WEB} macros
1492 to access the various fields. Thus it would be simple to store the
1493 data in an array of record structures and adapt the \.{WEB} macros
1494 accordingly.
1495
1496 We will say, e.g., |font_name(f)| for the name field of font~|f|, and
1497 |font_width(f)(c)| for the width pointer of character~|c| in font~|f|
1498 and |font_packet(f)(c)| for its character packet (this character
1499 exists provided |font_bc(f)<=c<=font_ec(f)| and
1500 |font_width(f)(c)<>invalid_width|). The actual width of character~|c| in
1501 font~|f| is stored in |widths[font_width(f)(c)]|.
1502
1503 @d font_check(#)==fnt_check[#] {checksum}
1504 @d font_scaled(#)==fnt_scaled[#] {scaled or `at' size}
1505 @d font_design(#)==fnt_design[#] {design size}
1506 @d font_name(#)==fnt_name[#] {area plus name packet}
1507 @d font_bc(#)==fnt_bc[#] {first character}
1508 @d font_ec(#)==fnt_ec[#] {last character}
1509 @d font_chars(#)==fnt_chars[#] {character info offset}
1510 @d font_type(#)==fnt_type[#] {type of this font}
1511 @d font_font(#)==fnt_font[#] {use depends on |font_type|}
1512 @#
1513 @d font_width_end(#)==#]
1514 @d font_width(#)==char_widths[font_chars(#)+font_width_end
1515 @d font_packet(#)==char_packets[font_chars(#)+font_width_end
1516
1517 @<Glob...@>=
1518 @!fnt_check:array [font_number] of int_32; {checksum}
1519 @!fnt_scaled:array [font_number] of int_31; {scaled size}
1520 @!fnt_design:array [font_number] of int_31; {design size}
1521 @!device @<Declare device dependent font data arrays@>@; @+ ecived @; @/
1522 @!fnt_name:array [font_number] of pckt_pointer; {pointer to area plus
1523 name packet}
1524 @!fnt_bc:array [font_number] of eight_bits; {first character}
1525 @!fnt_ec:array [font_number] of eight_bits; {last character}
1526 @!fnt_chars:array [font_number] of char_offset; {character info offset}
1527 @!fnt_type:array [font_number] of f_type; {type of font}
1528 @!fnt_font:array [font_number] of font_number; {use depends on |font_type|}
1529
1530 @ @d invalid_font==max_fonts {used when there is no valid font}
1531
1532 @<Set init...@>=
1533 @!device @<Initialize device dependent font data@>@; @+ ecived @;@/
1534 nf:=0;
1535
1536 @ A \.{VF}, or \.{GF}, or \.{PK} file may contain information for
1537 several characters with the same residue but with different extension;
1538 all except the first of the corresponding packets in |byte_mem| will
1539 contain a pointer to the previous one and |font_packet(f)(res)|
1540 identifies the last such packet.
1541
1542 A character packet in |byte_mem| starts with a flag byte
1543 $$\hbox{|flag=@"40*ext_flag+@"20*chain_flag+type_flag|}$$
1544 with |0<=ext_flag<=3|, |0<=chain_flag<=1|, |0<=type_flag<=@"1F|,
1545 followed by |ext_flag| bytes with the character extension for this
1546 packet and, if |chain_flag=1|, by a two byte packet pointer to the
1547 previous packet for the same font and character residue. The actual
1548 character packet follows after these header bytes and the
1549 interpretation of the |type_flag| depends on whether this is a \.{VF}
1550 packet or a packet for raster data.
1551
1552 The empty packet is interpreted as a special case of a packet with
1553 |flag=0|.
1554
1555 @d ext_flag=@"40
1556 @d chain_flag=@"20
1557
1558 @<Types...@>=
1559 @!type_flag=0..chain_flag-1; {the range of values for the |type_flag|}
1560
1561 @ The global variable |cur_fnt| is the internal font number of the
1562 currently selected font, or equals |invalid_font| if no font has
1563 been selected; |cur_res| and |cur_ext| are the residue and extension
1564 part of the current character code. The type of a character packet
1565 located by the |find_packet| function defined below is |cur_type|.
1566 While building a character packet for a character, |pckt_ext| and
1567 |pckt_res| are the extension and residue of this character; |pckt_dup|
1568 indicates whether a packet for this extension exists already.
1569
1570 @<Glob...@>=
1571 @!cur_fnt:font_number; {the currently selected font}
1572 @!cur_ext:int_24; {the current character extension}
1573 @!cur_res:int_8u; {the current character residue}
1574 @!cur_type:type_flag; {type of the current character packet}
1575 @!pckt_ext:int_24; {character extension for the current character packet}
1576 @!pckt_res:int_8u; {character residue for the current character packet}
1577 @!pckt_dup:boolean; {is there a previous packet for the same extension?}
1578 @!pckt_prev:pckt_pointer; {a previous packet for the same extension}
1579 @!pckt_m_msg,@!pckt_s_msg,@!pckt_d_msg:int_7; {counts for various character
1580 packet error messages}
1581
1582 @ @<Set init...@>=
1583 cur_fnt:=invalid_font; pckt_m_msg:=0; pckt_s_msg:=0; pckt_d_msg:=0;
1584
1585 @ The |find_packet| functions is used to locate the character packet for
1586 the character with residue~|cur_res| and extension~|cur_ext| from
1587 font~|cur_fnt| and returns |false| if no packet exists for any extension;
1588 otherwise the result is |true| and the global variables |cur_packet|,
1589 |cur_type|, |cur_loc|, and |cur_limit| are initialized. In case none of
1590 the character packets has the correct extension, the last one in the
1591 chain (the one defined first) is used instead and |cur_ext| is changed
1592 accordingly.
1593
1594 @p function find_packet:boolean;
1595 label found,exit;
1596 var p,@!q:pckt_pointer; {current and next packet}
1597 @!f:eight_bits; {a flag byte}
1598 @!e:int_24; {extension for a packet}
1599 begin q:=font_packet(cur_fnt)(cur_res);
1600 if q=invalid_packet then
1601 begin if pckt_m_msg<10 then {stop telling after first 10 times}
1602 begin print_ln('---missing character packet for character ',cur_res:1,
1603 @.missing character packet...@>
1604 ' font ',cur_fnt:1);
1605 incr(pckt_m_msg); mark_error;
1606 if pckt_m_msg=10 then print_ln('---further messages suppressed.');
1607 end;
1608 find_packet:=false; return;
1609 end;
1610 @<Locate a character packet and |goto found| if found@>;
1611 if pckt_s_msg<10 then {stop telling after first 10 times}
1612 begin print_ln('---substituted character packet with extension ',
1613 @.substituted character packet...@>
1614 e:1,' instead of ',cur_ext:1,' for character ',cur_res:1,
1615 ' font ',cur_fnt:1);
1616 incr(pckt_s_msg); mark_error;
1617 if pckt_s_msg=10 then print_ln('---further messages suppressed.');
1618 end;
1619 cur_ext:=e;
1620 found: cur_pckt:=p; cur_type:=f; find_packet:=true;
1621 exit: end;
1622
1623 @ @<Locate a character packet and |goto found| if found@>=
1624 repeat p:=q; q:=invalid_packet;
1625 cur_loc:=pckt_start[p]; cur_limit:=pckt_start[p+1];
1626 if p=empty_packet then
1627 begin e:=0; f:=0;
1628 end
1629 else begin pckt_extract(f);
1630 case (f div ext_flag) of
1631 0: e:=0;
1632 1: e:=pckt_ubyte;
1633 2: e:=pckt_upair;
1634 othercases e:=pckt_strio; {|f div ext_flag = 3|}
1635 endcases;
1636 if (f mod ext_flag)>=chain_flag then q:=pckt_upair;
1637 f:=f mod chain_flag;
1638 end;
1639 if e=cur_ext then goto found;
1640 until q=invalid_packet
1641
1642 @ The |start_packet| procedure is used to create the header bytes of a
1643 character packet for the character with residue~|cur_res| and
1644 extension~|cur_ext| from font~|cur_fnt|; if a previous such packet
1645 exists, we try to build an exact duplicate, i.e., use the chain field of
1646 that previous packet.
1647
1648 @p procedure start_packet(@!t:type_flag);
1649 label found,not_found;
1650 var p,@!q:pckt_pointer; {current and next packet}
1651 @!f:int_8u; {a flag byte}
1652 @!e:int_32; {extension for a packet}
1653 @!cur_loc: byte_pointer; {current location in a packet}
1654 @!cur_limit: byte_pointer; {start of next packet}
1655 begin q:=font_packet(cur_fnt)(cur_res);
1656 if q<>invalid_packet then @<Locate a character packet...@>;
1657 q:=font_packet(cur_fnt)(cur_res); pckt_dup:=false; goto not_found;
1658 found: pckt_dup:=true; pckt_prev:=p;
1659 not_found: pckt_ext:=cur_ext; pckt_res:=cur_res; pckt_room(6);
1660 @!debug if byte_ptr<>pckt_start[pckt_ptr] then confusion(str_packets);
1661 gubed @;@/
1662 if q=invalid_packet then f:=t @+ else f:=t+chain_flag;
1663 e:=cur_ext;
1664 if e<0 then Incr(e)(@"1000000);
1665 if e=0 then append_byte(f) @+ else @;
1666 begin if e<@"100 then append_byte(f+ext_flag) @+ else @;
1667 begin if e<@"10000 then append_byte(f+ext_flag+ext_flag) @+ else @;
1668 begin append_byte(f+ext_flag+ext_flag+ext_flag);
1669 append_byte(e div @"10000); e:=e mod @"10000;
1670 end;
1671 append_byte(e div @"100); e:=e mod @"100;
1672 end;
1673 append_byte(e);
1674 end;
1675 if q<>invalid_packet then
1676 begin append_byte(q div @"100); append_byte(q mod @"100);
1677 end;
1678 end;
1679
1680 @ The |build_packet| procedure is used to finish a character packet.
1681 If a previous packet for the same character extension exists, the new
1682 one is discarded; if the two packets are identical, as it occasionally
1683 occurs for raster files, this is done without an error message.
1684
1685 @p procedure build_packet;
1686 var k,@!l:byte_pointer; {indices into |byte_mem|}
1687 begin if pckt_dup then
1688 begin k:=pckt_start[pckt_prev+1]; l:=pckt_start[pckt_ptr];
1689 if (byte_ptr-l)<>(k-pckt_start[pckt_prev]) then pckt_dup:=false;
1690 while pckt_dup and(byte_ptr>l) do
1691 begin flush_byte; decr(k);
1692 if byte_mem[byte_ptr]<>byte_mem[k] then pckt_dup:=false;
1693 end;
1694 if (not pckt_dup)and(pckt_d_msg<10) then {stop telling after first 10 times}
1695 begin print('---duplicate packet for character ',pckt_res:1);
1696 @.duplicate packet for character...@>
1697 if pckt_ext<>0 then print('.',pckt_ext:1);
1698 print_ln(' font ',cur_fnt:1);
1699 incr(pckt_d_msg); mark_error;
1700 if pckt_d_msg=10 then print_ln('---further messages suppressed.');
1701 end;
1702 byte_ptr:=l;
1703 end
1704 else font_packet(cur_fnt)(pckt_res):=make_packet;
1705 end;
1706
1707 @* Defining fonts.
1708 A detailed description of the \.{TFM} file format can be found in the
1709 documentation of \TeX, \MF, or \.{TFtoPL}. In order to read \.{TFM}
1710 files the program uses the binary file variable |tfm_file|.
1711
1712 @<Glob...@>=
1713 @!tfm_file:byte_file; {a \.{TFM} file}
1714 @!tfm_ext:pckt_pointer; {extension for \.{TFM} files}
1715
1716 @ @<Initialize predefined strings@>=
1717 id4(".")("T")("F")("M")(tfm_ext); {file name extension for \.{TFM} files}
1718
1719 @ If no font directory has been specified, \.{\title} is supposed to use
1720 the default \.{TFM} directory, which is a system-dependent place where
1721 the \.{TFM} files for standard fonts are kept.
1722 The string variable |TFM_default_area| contains the name of this area.
1723 @^system dependencies@>
1724
1725 @d TFM_default_area_name=='TeXfonts:' {change this to the correct name}
1726 @d TFM_default_area_name_length=9 {change this to the correct length}
1727
1728 @<Glob...@>=
1729 @!TFM_default_area:packed array[1..TFM_default_area_name_length] of char;
1730
1731 @ @<Set init...@>=
1732 TFM_default_area:=TFM_default_area_name;
1733
1734 @ If a \.{TFM} file is badly malformed, we say |bad_font|; for a \.{TFM}
1735 file the |bad_tfm| procedure is used to give an error message which
1736 refers the user to \.{TFtoPL} and \.{PLtoTF}, and terminates \.{\title}.
1737
1738 @<Error handling...@>=
1739 procedure bad_tfm;
1740 begin print('Bad TFM file'); print_font(cur_fnt); print_ln('!');
1741 @.Bad TFM file@>
1742 abort('Use TFtoPL/PLtoTF to diagnose and correct the problem');
1743 @.Use TFtoPL/PLtoTF@>
1744 end;
1745 @#
1746 procedure bad_font;
1747 begin new_line;
1748 case font_type(cur_fnt) of
1749 defined_font: confusion(str_fonts);
1750 loaded_font: bad_tfm;
1751 @<Cases for \(|bad_font|@>@;@/
1752 othercases abort('internal error');
1753 endcases;
1754 end;
1755
1756 @ To prepare |tfm_file| for input we |reset| it.
1757
1758 @<TFM: Open |tfm_file|@>=
1759 make_font_name(TFM_default_area_name_length)(TFM_default_area)(tfm_ext);
1760 reset(tfm_file,cur_name);
1761 if eof(tfm_file) then
1762 @^system dependencies@>
1763 abort('---not loaded, TFM file can''t be opened!')
1764 @.TFM file can\'t be opened@>
1765
1766 @ It turns out to be convenient to read four bytes at a time, when we
1767 are inputting from \.{TFM} files. The input goes into global variables
1768 |tfm_b0|, |tfm_b1|, |tfm_b2|, and |tfm_b3|, with |tfm_b0| getting the
1769 first byte and |tfm_b3| the fourth.
1770
1771 @<Glob...@>=
1772 @!tfm_b0,@!tfm_b1,@!tfm_b2,@!tfm_b3: eight_bits; {four bytes input at once}
1773
1774 @ Reading a \.{TFM} file should be done as efficient as possible for a
1775 particular system; on many systems this means that a large number of
1776 bytes from |tfm_file| is read into a buffer and will then be extracted
1777 from that buffer. In order to simplify such system dependent changes
1778 we use the \.{WEB} macro |tfm_byte| to extract the next \.{TFM} byte;
1779 this macro and |eof(tfm_file)| are used only in the |read_tfm_word|
1780 procedure which sets |tfm_b0| through |tfm_b3| to the next four bytes
1781 in the current \.{TFM} file. Here we give simple minded definitions in
1782 terms of standard \PASCAL.
1783 @^system dependencies@>
1784 @^optimization@>
1785
1786 @d tfm_byte(#)==read(tfm_file,#) {read next \.{TFM} byte}
1787
1788 @p procedure read_tfm_word;
1789 begin tfm_byte(tfm_b0); tfm_byte(tfm_b1);
1790 tfm_byte(tfm_b2); tfm_byte(tfm_b3);
1791 if eof(tfm_file) then bad_font;
1792 end;
1793
1794 @ Here are three procedures used to check the consistency of font files:
1795 First, the |check_check_sum| procedure compares two check sum values: a
1796 warning is given if they differ and are both non-zero; if the second
1797 value is not zero it may replace the first one.
1798 Next, the |check_design_size| procedure compares two design size
1799 values: a warning is given if they differ by more than a small amount.
1800 Finally, the |check_width| function compares the character width value
1801 for character |cur_res| read from a \.{VF} or raster file for font
1802 |cur_fnt| with the value previously read from the \.{TFM} file and
1803 returns the width pointer for that value; a warning is given if the two
1804 values differ.
1805
1806 @p procedure check_check_sum(@!c:int_32;@!u:boolean);
1807 {compare |font_check(cur_fnt)| with |c|}
1808 begin if (c<>font_check(cur_fnt))and(c<>0) then
1809 begin
1810 if font_check(cur_fnt)<>0 then
1811 begin new_line; print_ln('---beware: check sums do not agree! (',
1812 @.beware: check sums do not agree@>
1813 @.check sums do not agree@>
1814 c:1,' vs. ',font_check(cur_fnt):1,')');
1815 mark_harmless;
1816 end;
1817 if u then font_check(cur_fnt):=c;
1818 end;
1819 end;
1820 @#
1821 procedure check_design_size(@!d:int_32);
1822 {compare |font_design(cur_fnt)| with |d|}
1823 begin if abs(d-font_design(cur_fnt))>2 then
1824 begin new_line; print_ln('---beware: design sizes do not agree! (',
1825 @.beware: design sizes do not agree@>
1826 @.design sizes do not agree@>
1827 d:1,' vs. ',font_design(cur_fnt):1,')');
1828 mark_error;
1829 end;
1830 end;
1831 @#
1832 function check_width(w:int_32):width_pointer;
1833 {compare |widths[font_width(cur_fnt)(cur_res)]| with |w|}
1834 var wp:width_pointer; {pointer to \.{TFM} width value}
1835 begin if (cur_res>=font_bc(cur_fnt))and(cur_res<=font_ec(cur_fnt)) then
1836 wp:=font_width(cur_fnt)(cur_res)
1837 else wp:=invalid_width;
1838 if wp=invalid_width then
1839 begin print_nl('Bad char ',cur_res:1);
1840 @.Bad char c@>
1841 if cur_ext<>0 then print('.',cur_ext:1);
1842 print(' font ',cur_fnt:1); print_font(cur_fnt);
1843 abort(' (compare TFM file)');
1844 end;
1845 if w<>widths[wp] then
1846 begin new_line; print_ln('---beware: char widths do not agree! (',
1847 @.beware: char widths do not agree@>
1848 @.char widths do not agree@>
1849 w:1,' vs. ',widths[wp]:1,')');
1850 mark_error;
1851 end;
1852 check_width:=wp;
1853 end;
1854
1855 @ The |load_font| procedure reads the \.{TFM} file for a font and puts
1856 the data extracted into position |cur_fnt| of the font data arrays.
1857
1858 @p procedure load_font; {reads a \.{TFM} file}
1859 var l:int_16; {loop index}
1860 @!p:char_pointer; {index into |char_widths|}
1861 @!q:width_pointer; {index into |widths|}
1862 @!bc,@!ec:int_15; {first and last character in this font}
1863 @!lh:int_15; {length of header in four byte words}
1864 @!nw:int_15; {number of words in width table}
1865 @!w:int_32; {a four byte integer}
1866 @<Variables for scaling computation@>@;
1867 begin print('TFM: font ',cur_fnt:1); print_font(cur_fnt);
1868 font_type(cur_fnt):=loaded_font;
1869 @<TFM: Open |tfm_file|@>;
1870 @<TFM: Read past the header data@>;
1871 @<TFM: Store character-width indices@>;
1872 @<TFM: Read and convert the width values@>;
1873 @<TFM: Convert character-width indices to character-width pointers@>;
1874 close_in(tfm_file);
1875 @!device @<Initialize device dependent data for a font@>@; @+ ecived @; @/
1876 d_print(' loaded at ',font_scaled(cur_fnt):1,' DVI units');
1877 print_ln('.');
1878 end;
1879
1880 @ @<Glob...@>=
1881 @!tfm_conv:real; {\.{DVI} units per absolute \.{TFM} unit}
1882
1883 @ We will use the following \.{WEB} macros to construct integers from
1884 two or four of the four bytes read by |read_tfm_word|.
1885 @^system dependencies@>
1886
1887 @d tfm_b01(#)== {|tfm_b0..tfm_b1| as non-negative integer}
1888 if tfm_b0>127 then bad_font
1889 else #:=tfm_b0*256+tfm_b1
1890 @d tfm_b23(#)== {|tfm_b2..tfm_b3| as non-negative integer}
1891 if tfm_b2>127 then bad_font
1892 else #:=tfm_b2*256+tfm_b3
1893 @d tfm_squad(#)== {|tfm_b0..tfm_b3| as signed integer}
1894 if tfm_b0<128 then #:=((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3
1895 else #:=(((tfm_b0-256)*256+tfm_b1)*256+tfm_b2)*256+tfm_b3
1896 @d tfm_uquad== {|tfm_b0..tfm_b3| as unsigned integer}
1897 (((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3)
1898
1899 @<TFM: Read past the header data@>=
1900 read_tfm_word; tfm_b23(lh);
1901 read_tfm_word; tfm_b01(bc); tfm_b23(ec);
1902 if ec<bc then
1903 begin bc:=1; ec:=0;
1904 end
1905 else if ec>255 then bad_font;
1906 read_tfm_word; tfm_b01(nw);
1907 if (nw=0)or(nw>256) then bad_font;
1908 for l:=-2 to lh do
1909 begin read_tfm_word;
1910 if l=1 then
1911 begin tfm_squad(w); check_check_sum(w,true);
1912 end
1913 else if l=2 then
1914 begin if tfm_b0>127 then bad_font;
1915 check_design_size(round(tfm_conv*tfm_uquad));
1916 end;
1917 end
1918
1919 @ The width indices for the characters are stored in positions |n_chars|
1920 through |n_chars-bc+ec| of the |char_widths| array; if characters on
1921 either end of the range |bc..ec| do not exist, they are ignored and the
1922 range is adjusted accordingly.
1923
1924 @<TFM: Store character-width indices@>=
1925 read_tfm_word;
1926 while (tfm_b0=0)and(bc<=ec) do
1927 begin incr(bc); read_tfm_word;
1928 end;
1929 font_bc(cur_fnt):=bc; font_chars(cur_fnt):=n_chars-bc;
1930 if ec>=max_chars-font_chars(cur_fnt) then overflow(str_chars,max_chars);
1931 for l:=bc to ec do
1932 begin char_widths[n_chars]:=tfm_b0; incr(n_chars); read_tfm_word;
1933 end;
1934 while (char_widths[n_chars-1]=0)and(ec>=bc) do
1935 begin decr(n_chars); decr(ec);
1936 end;
1937 font_ec(cur_fnt):=ec
1938
1939 @ The most important part of |load_font| is the width computation, which
1940 involves multiplying the relative widths in the \.{TFM} file by the
1941 scaling factor in the \.{DVI} file. A similar computation is used for
1942 dimensions read from \.{VF} files. This fixed-point multiplication must
1943 be done with precisely the same accuracy by all \.{DVI}-reading programs,
1944 in order to validate the assumptions made by \.{DVI}-writing programs
1945 like \TeX82.
1946
1947 Let us therefore summarize what needs to be done. Each width in a \.{TFM}
1948 file appears as a four-byte quantity called a |fix_word|. A |fix_word|
1949 whose respective bytes are $(a,b,c,d)$ represents the number
1950 $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
1951 b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
1952 -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
1953 (No other choices of $a$ are allowed, since the magnitude of a \.{TFM}
1954 dimension must be less than 16.) We want to multiply this quantity by the
1955 integer~|z|, which is known to be less than $2^{27}$.
1956 If $|z|<2^{23}$, the individual multiplications $b\cdot z$, $c\cdot z$,
1957 $d\cdot z$ cannot overflow; otherwise we will divide |z| by 2, 4, 8, or
1958 16, to obtain a multiplier less than $2^{23}$, and we can compensate for
1959 this later. If |z| has thereby been replaced by $|z|^\prime=|z|/2^e$, let
1960 $\beta=2^{4-e}$; we shall compute
1961 $$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$ if $a=0$,
1962 or the same quantity minus $\alpha=2^{4+e}z^\prime$ if $a=255$.
1963 This calculation must be done exactly, for the reasons stated above; the
1964 following program does the job in a system-independent way, assuming
1965 that arithmetic is exact on numbers less than $2^{31}$ in magnitude. We
1966 use \.{WEB} macros for various versions of this computation.
1967 @^system dependencies@>
1968 @^optimization@>
1969
1970 @d tfm_fix3u== {convert |tfm_b1..tfm_b3| to an unsigned scaled dimension}
1971 (((((tfm_b3*z)div@'400)+(tfm_b2*z))div@'400)+(tfm_b1*z))div beta
1972 @#
1973 @d tfm_fix4(#)== {convert |tfm_b0..tfm_b3| to a scaled dimension}
1974 #:=tfm_fix3u;
1975 if tfm_b0>0 then if tfm_b0=255 then Decr(#)(alpha) else bad_font
1976 @d tfm_fix3(#)== {convert |tfm_b1..tfm_b3| to a scaled dimension}
1977 #:=tfm_fix3u; @+ if tfm_b1>127 then Decr(#)(alpha)
1978 @d tfm_fix2== {convert |tfm_b2..tfm_b3| to a scaled dimension}
1979 if tfm_b2>127 then tfm_b1:=255 else tfm_b1:=0;
1980 tfm_fix3
1981 @d tfm_fix1== {convert |tfm_b3| to a scaled dimension}
1982 if tfm_b3>127 then tfm_b1:=255 else tfm_b1:=0;
1983 tfm_b2:=tfm_b1; tfm_fix3
1984
1985 @<Variables for scaling computation@>=
1986 @!z:int_32; {multiplier}
1987 @!alpha:int_32; {correction for negative values}
1988 @!beta:int_15; {divisor}
1989
1990 @ @<Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$@>=
1991 alpha:=16;
1992 while z>=@'40000000 do
1993 begin z:=z div 2; alpha:=alpha+alpha;
1994 end;
1995 beta:=256 div alpha; alpha:=alpha*z
1996
1997 @ The first width value, which indicates that a character does not exist
1998 and which must vanish, is converted to |invalid_width|; the other width
1999 values are scaled by |font_scaled(cur_fnt)| and converted to width
2000 pointers by |make_width|. The resulting width pointers are stored
2001 temporarily in the |char_widths| array, following the with indices.
2002
2003 @<TFM: Read and convert the width values@>=
2004 if nw-1>max_chars-n_chars then overflow(str_chars,max_chars);
2005 if (tfm_b0<>0)or(tfm_b1<>0)or(tfm_b2<>0)or(tfm_b3<>0) then bad_font
2006 else char_widths[n_chars]:=invalid_width;
2007 z:=font_scaled(cur_fnt);
2008 @<Replace |z|...@>;
2009 for p:=n_chars+1 to n_chars+nw-1 do
2010 begin read_tfm_word; tfm_fix4(w);
2011 char_widths[p]:=make_width(w);
2012 end
2013
2014 @ We simply translate the width indices into width pointers. In addition
2015 we initialize the character packets with the invalid packet.
2016
2017 @<TFM: Convert character-width indices to character-width pointers@>=
2018 for p:=font_chars(cur_fnt)+bc to n_chars-1 do
2019 begin q:=char_widths[n_chars+char_widths[p]]; char_widths[p]:=q;
2020 char_packets[p]:=invalid_packet;
2021 end
2022
2023 @ When processing a font definition we put the data extracted from the
2024 \.{DVI} or \.{VF} file into position |nf| of the font data arrays and
2025 call |define_font| to obtain the internal font number for this font.
2026 The parameter |load| is true if the \.{TFM} file should be loaded.
2027
2028 @p function define_font(@!load:boolean):font_number;
2029 var save_fnt:font_number; {used to save |cur_fnt|}
2030 begin save_fnt:=cur_fnt; {save}
2031 cur_fnt:=0;
2032 while (font_name(cur_fnt)<>font_name(nf))or@|
2033 (font_scaled(cur_fnt)<>font_scaled(nf)) do incr(cur_fnt);
2034 d_print(' => ',cur_fnt:1); print_font(cur_fnt);
2035 if cur_fnt<nf then
2036 begin check_check_sum(font_check(nf),true);
2037 check_design_size(font_design(nf));
2038 @!debug if font_type(cur_fnt)=defined_font then print(' defined')
2039 else print(' loaded');
2040 print(' previously');
2041 gubed@;
2042 end
2043 else begin if nf=max_fonts then overflow(str_fonts,max_fonts);
2044 incr(nf); font_font(cur_fnt):=invalid_font;
2045 font_type(cur_fnt):=defined_font;
2046 d_print(' defined');
2047 end;
2048 print_ln('.');
2049 if load and(font_type(cur_fnt)=defined_font) then load_font;
2050 define_font:=cur_fnt;
2051 cur_fnt:=save_fnt; {restore}
2052 end;
2053
2054 @* Low-level DVI input routines.
2055 The program uses the binary file variable |dvi_file| for its main input
2056 file; |dvi_loc| is the number of the byte about to be read next from
2057 |dvi_file|.
2058
2059 @<Glob...@>=
2060 @!dvi_file:byte_file; {the stuff we are \.{\title}ing}
2061 @!dvi_loc:int_32; {where we are about to look, in |dvi_file|}
2062
2063 @ If the \.{DVI} file is badly malformed, we say |bad_dvi|; this
2064 procedure gives an error message which refers the user to \.{DVItype},
2065 and terminates \.{\title}.
2066
2067 @<Error handling...@>=
2068 procedure bad_dvi;
2069 begin new_line; print_ln('Bad DVI file: loc=',dvi_loc:1,'!');
2070 @.Bad DVI file@>
2071 print(' Use DVItype with output level');
2072 @.Use DVItype@>
2073 if random_reading then print('=4') @+ else print('<4');
2074 abort('to diagnose the problem');
2075 end;
2076
2077 @ To prepare |dvi_file| for input, we |reset| it.
2078
2079 @<Open input file(s)@>=
2080 reset(dvi_file); {prepares to read packed bytes from |dvi_file|}
2081 dvi_loc:=0;
2082
2083 @ Reading the \.{DVI} file should be done as efficient as possible for a
2084 particular system; on many systems this means that a large number of
2085 bytes from |dvi_file| is read into a buffer and will then be extracted
2086 from that buffer. In order to simplify such system dependent changes
2087 we use a pair of \.{WEB} macros: |dvi_byte| extracts the next \.{DVI}
2088 byte and |dvi_eof| is |true| if we have reached the end of the \.{DVI}
2089 file. Here we give simple minded definitions for these macros in terms
2090 of standard \PASCAL.
2091 @^system dependencies@>
2092 @^optimization@>
2093
2094 @d dvi_eof == eof(dvi_file) {has the \.{DVI} file been exhausted?}
2095 @d dvi_byte(#) ==
2096 if dvi_eof then bad_dvi
2097 else read(dvi_file,#) {obtain next \.{DVI} byte}
2098
2099 @ Next we come to the routines that are used only if |random_reading| is
2100 |true|. The driver program below needs two such routines: |dvi_length| should
2101 compute the total number of bytes in |dvi_file|, possibly also
2102 causing |eof(dvi_file)| to be true; and |dvi_move(n)| should position
2103 |dvi_file| so that the next |dvi_byte| will read byte |n|, starting with
2104 |n=0| for the first byte in the file.
2105 @^system dependencies@>
2106
2107 Such routines are, of course, highly system dependent. They are implemented
2108 here in terms of two assumed system routines called |set_pos| and |cur_pos|.
2109 The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is
2110 negative or larger than the total number of items in |f|; in the latter
2111 case, |set_pos(f,n)| moves to the end of file |f|.
2112 The call |cur_pos(f)| gives the total number of items in |f|, if
2113 |eof(f)| is true; we use |cur_pos| only in such a situation.
2114
2115 @p function dvi_length:int_32;
2116 begin set_pos(dvi_file,-1); dvi_length:=cur_pos(dvi_file);
2117 end;
2118 @#
2119 procedure dvi_move(@!n:int_32);
2120 begin set_pos(dvi_file,n); dvi_loc:=n;
2121 end;
2122
2123 @ We need seven simple functions to read the next byte or bytes
2124 from |dvi_file|.
2125
2126 @p function dvi_sbyte:int_8; {returns the next byte, signed}
2127 @!begin_byte(dvi_byte); incr(dvi_loc); comp_sbyte(dvi_sbyte);
2128 end;
2129 @#
2130 function dvi_ubyte:int_8u; {returns the next byte, unsigned}
2131 @!begin_byte(dvi_byte); incr(dvi_loc); comp_ubyte(dvi_ubyte);
2132 end;
2133 @#
2134 function dvi_spair:int_16; {returns the next two bytes, signed}
2135 @!begin_pair(dvi_byte); Incr(dvi_loc)(2); comp_spair(dvi_spair);
2136 end;
2137 @#
2138 function dvi_upair:int_16u; {returns the next two bytes, unsigned}
2139 @!begin_pair(dvi_byte); Incr(dvi_loc)(2); comp_upair(dvi_upair);
2140 end;
2141 @#
2142 function dvi_strio:int_24; {returns the next three bytes, signed}
2143 @!begin_trio(dvi_byte); Incr(dvi_loc)(3); comp_strio(dvi_strio);
2144 end;
2145 @#
2146 function dvi_utrio:int_24u; {returns the next three bytes, unsigned}
2147 @!begin_trio(dvi_byte); Incr(dvi_loc)(3); comp_utrio(dvi_utrio);
2148 end;
2149 @#
2150 function dvi_squad:int_32; {returns the next four bytes, signed}
2151 @!begin_quad(dvi_byte); Incr(dvi_loc)(4); comp_squad(dvi_squad);
2152 end;
2153
2154 @ Three other functions are used in cases where a four byte integer
2155 (which is always signed) must have a non-negative value, a positive
2156 value, or is a pointer which must be either positive or |=-1|.
2157
2158 @p function dvi_uquad:int_31; {result must be non-negative}
2159 var x:int_32;
2160 begin x:=dvi_squad; if x<0 then bad_dvi
2161 else dvi_uquad:=x;
2162 end;
2163 @#
2164 function dvi_pquad:int_31; {result must be positive}
2165 var x:int_32;
2166 begin x:=dvi_squad; if x<=0 then bad_dvi
2167 else dvi_pquad:=x;
2168 end;
2169 @#
2170 function dvi_pointer:int_32; {result must be positive or |=-1|}
2171 var x:int_32;
2172 begin x:=dvi_squad; if (x<=0)and(x<>-1) then bad_dvi
2173 else dvi_pointer:=x;
2174 end;
2175
2176 @ Given the structure of the \.{DVI} commands it is fairly obvious
2177 that their interpretation consists of two steps: First zero to four
2178 bytes are read in order to obtain the value of the first parameter
2179 (e.g., zero bytes for |set_char_0|, four bytes for |set4|); then,
2180 depending on the command class, a specific action is performed (e.g.,
2181 typeset a character but don't move the reference point for |put1..put4|).
2182
2183 The \.{DVItype} program uses large case statements for both steps;
2184 unfortunately some \PASCAL\ compilers fail to implement large case
2185 statements efficiently -- in particular those as the one used in the
2186 |first_par| function of \.{DVItype}. Here we use a pair of look up tables:
2187 |dvi_par| determines how to obtain the value of the first parameter, and
2188 |dvi_cl| determines the command class.
2189
2190 A slight complication arises from the fact that we want to decompose the
2191 character code of each character to be typeset into a residue
2192 |0<=char_res<256| and extension: |char_code=char_res+256*char_ext|;
2193 the \.{TFM} widths as well as the pixel widths for a given resolution
2194 are the same for all characters in a font with the same residue.
2195
2196 @d two_cases(#)==#,#+1
2197 @d three_cases(#)==#,#+1,#+2
2198 @d five_cases(#)==#,#+1,#+2,#+3,#+4
2199
2200 @ First we define the values used as array elements of |dvi_par|; we
2201 distinguish between pure numbers and dimensions because dimensions read
2202 from a \.{VF} file must be scaled.
2203
2204 @d char_par=0 {character for \\{set} and |put|}
2205 @d no_par=1 {no parameter}
2206 @d dim1_par=2 {one-byte signed dimension}
2207 @d num1_par=3 {one-byte unsigned number}
2208 @d dim2_par=4 {two-byte signed dimension}
2209 @d num2_par=5 {two-byte unsigned number}
2210 @d dim3_par=6 {three-byte signed dimension}
2211 @d num3_par=7 {three-byte unsigned number}
2212 @d dim4_par=8 {four-byte signed dimension}
2213 @d num4_par=9 {four-byte signed number}
2214 @d numu_par=10 {four-byte non-negative number}
2215 @d rule_par=11 {dimensions for |set_rule| and |put_rule|}
2216 @d fnt_par=12 {font for |fnt_num| commands}
2217 @d max_par=12 {largest possible value}
2218
2219 @<Types...@>=
2220 @!cmd_par=char_par..max_par;
2221
2222 @ Here we declare the array |dvi_par|.
2223
2224 @<Globals...@>=
2225 @!dvi_par:packed array [eight_bits] of cmd_par;
2226
2227 @ And here we initialize it.
2228
2229 @<Set init...@>=
2230 for i:=0 to put1+3 do dvi_par[i]:=char_par;@/
2231 for i:=nop to 255 do dvi_par[i]:=no_par;@/
2232 dvi_par[set_rule]:=rule_par; dvi_par[put_rule]:=rule_par;@/
2233 dvi_par[right1]:=dim1_par; dvi_par[right1+1]:=dim2_par;
2234 dvi_par[right1+2]:=dim3_par; dvi_par[right1+3]:=dim4_par;@/
2235 for i:=fnt_num_0 to fnt_num_0+63 do dvi_par[i]:=fnt_par;@/
2236 dvi_par[fnt1]:=num1_par; dvi_par[fnt1+1]:=num2_par;
2237 dvi_par[fnt1+2]:=num3_par; dvi_par[fnt1+3]:=num4_par;@/
2238 dvi_par[xxx1]:=num1_par; dvi_par[xxx1+1]:=num2_par;
2239 dvi_par[xxx1+2]:=num3_par; dvi_par[xxx1+3]:=numu_par;@/
2240 for i:=0 to 3 do
2241 begin dvi_par[i+w1]:=dvi_par[i+right1];
2242 dvi_par[i+x1]:=dvi_par[i+right1];
2243 dvi_par[i+down1]:=dvi_par[i+right1];
2244 dvi_par[i+y1]:=dvi_par[i+right1];
2245 dvi_par[i+z1]:=dvi_par[i+right1];
2246 dvi_par[i+fnt_def1]:=dvi_par[i+fnt1];
2247 end;
2248
2249 @ Next we define the values used as array elements of |dvi_cl|;
2250 several \.{DVI} commands (e.g., |nop|, |bop|, |eop|, |pre|, |post|) will
2251 always be treated separately and are therefore assigned to the invalid
2252 class here.
2253
2254 @d char_cl=0
2255 @d rule_cl=char_cl+1
2256 @d xxx_cl=char_cl+2
2257 @d push_cl=3
2258 @d pop_cl=4
2259 @d w0_cl=5
2260 @d x0_cl=w0_cl+1
2261 @d right_cl=w0_cl+2
2262 @d w_cl=w0_cl+3
2263 @d x_cl=w0_cl+4
2264 @d y0_cl=10
2265 @d z0_cl=y0_cl+1
2266 @d down_cl=y0_cl+2
2267 @d y_cl=y0_cl+3
2268 @d z_cl=y0_cl+4
2269 @d fnt_cl=15
2270 @d fnt_def_cl=16
2271 @d invalid_cl=17
2272 @d max_cl=invalid_cl {largest possible value}
2273
2274 @<Types...@>=
2275 @!cmd_cl=char_cl..max_cl;
2276
2277 @ Here we declare the array |dvi_cl|.
2278
2279 @<Globals...@>=
2280 @!dvi_cl:packed array [eight_bits] of cmd_cl;
2281
2282 @ And here we initialize it.
2283
2284 @<Set init...@>=
2285 for i:=set_char_0 to put1+3 do dvi_cl[i]:=char_cl;
2286 dvi_cl[set_rule]:=rule_cl; dvi_cl[put_rule]:=rule_cl;@/
2287 dvi_cl[nop]:=invalid_cl;
2288 dvi_cl[bop]:=invalid_cl; dvi_cl[eop]:=invalid_cl;@/
2289 dvi_cl[push]:=push_cl; dvi_cl[pop]:=pop_cl;@/
2290 dvi_cl[w0]:=w0_cl; dvi_cl[x0]:=x0_cl;@/
2291 dvi_cl[y0]:=y0_cl; dvi_cl[z0]:=z0_cl;@/
2292 for i:=0 to 3 do
2293 begin dvi_cl[i+right1]:=right_cl;
2294 dvi_cl[i+w1]:=w_cl;
2295 dvi_cl[i+x1]:=x_cl;@/
2296 dvi_cl[i+down1]:=down_cl;
2297 dvi_cl[i+y1]:=y_cl;
2298 dvi_cl[i+z1]:=z_cl;@/
2299 dvi_cl[i+xxx1]:=xxx_cl;
2300 dvi_cl[i+fnt_def1]:=fnt_def_cl;
2301 end;
2302 for i:=fnt_num_0 to fnt1+3 do dvi_cl[i]:=fnt_cl;
2303 for i:=pre to 255 do dvi_cl[i]:=invalid_cl;
2304
2305 @ A few small arrays are used to generate \.{DVI} commands.
2306
2307 @<Glob...@>=
2308 @!dvi_char_cmd:array[boolean] of eight_bits; {|put1| and |set1|}
2309 @!dvi_rule_cmd:array[boolean] of eight_bits; {|put_rule| and |set_rule|}
2310 @!dvi_right_cmd:array[right_cl..x_cl] of eight_bits; {|right1|, |w1|, and |x1|}
2311 @!dvi_down_cmd:array[down_cl..z_cl] of eight_bits; {|down1|, |y1|, and |z1|}
2312
2313 @ @<Set init...@>=
2314 dvi_char_cmd[false]:=put1;
2315 dvi_char_cmd[true]:=set1;@/
2316 dvi_rule_cmd[false]:=put_rule;
2317 dvi_rule_cmd[true]:=set_rule;@/
2318 dvi_right_cmd[right_cl]:=right1;
2319 dvi_right_cmd[w_cl]:=w1;
2320 dvi_right_cmd[x_cl]:=x1;@/
2321 dvi_down_cmd[down_cl]:=down1;
2322 dvi_down_cmd[y_cl]:=y1;
2323 dvi_down_cmd[z_cl]:=z1;
2324
2325 @ The global variables |cur_cmd|, |cur_parm|, and |cur_class| are used
2326 for the current \.{DVI} command, its first parameter (if any), and its
2327 command class respectively.
2328
2329 @<Glob...@>=
2330 @!cur_cmd:eight_bits; {current \.{DVI} command byte}
2331 @!cur_parm:int_32; {its first parameter (if any)}
2332 @!cur_class:cmd_cl; {its class}
2333
2334 @ When typesetting a character or rule, the boolean variable |cur_upd|
2335 is |true| for \\{set} commands, |false| for |put| commands.
2336
2337 @<Glob...@>=
2338 @!cur_cp:char_pointer; {|char_widths| index for the current character}
2339 @!cur_wp:width_pointer; {width pointer of the current character}
2340 @!cur_upd:boolean; {is this a \\{set} or |set_rule| command ?}
2341 @!cur_v_dimen:int_32; {a vertical dimension}
2342 @!cur_h_dimen:int_32; {a horizontal dimension}
2343
2344 @ @<Set init...@>=
2345 cur_cp:=0; cur_wp:=invalid_width; {so they can be saved and restored!}
2346
2347 @ The |dvi_first_par| procedure first reads \.{DVI} command bytes into
2348 |cur_cmd| until |cur_cmd<>nop|; then |cur_parm| is set to the value of
2349 the first parameter (if any) and |cur_class| to the command class.
2350
2351 @d set_cur_char(#)== {set up |cur_res|, |cur_ext|, and |cur_upd|}
2352 begin cur_ext:=0;
2353 if cur_cmd<set1 then
2354 begin cur_res:=cur_cmd; cur_upd:=true
2355 end
2356 else begin cur_res:=#; cur_upd:=(cur_cmd<put1);
2357 Decr(cur_cmd)(dvi_char_cmd[cur_upd]);
2358 while cur_cmd>0 do
2359 begin if cur_cmd=3 then if cur_res>127 then cur_ext:=-1;
2360 cur_ext:=cur_ext*256+cur_res; cur_res:=#; decr(cur_cmd);
2361 end;
2362 end;
2363 end
2364
2365 @p procedure dvi_first_par;
2366 begin repeat cur_cmd:=dvi_ubyte;
2367 until cur_cmd<>nop; {skip over |nop|s}
2368 case dvi_par[cur_cmd] of
2369 char_par: set_cur_char(dvi_ubyte);
2370 no_par: do_nothing;
2371 dim1_par: cur_parm:=dvi_sbyte;
2372 num1_par: cur_parm:=dvi_ubyte;
2373 dim2_par: cur_parm:=dvi_spair;
2374 num2_par: cur_parm:=dvi_upair;
2375 dim3_par: cur_parm:=dvi_strio;
2376 num3_par: cur_parm:=dvi_utrio;
2377 two_cases(dim4_par): cur_parm:=dvi_squad; {|dim4_par| and |num4_par|}
2378 numu_par: cur_parm:=dvi_uquad;
2379 rule_par:
2380 begin cur_v_dimen:=dvi_squad; cur_h_dimen:=dvi_squad;
2381 cur_upd:=(cur_cmd=set_rule);
2382 end;
2383 fnt_par:cur_parm:=cur_cmd-fnt_num_0;
2384 othercases abort('internal error');
2385 endcases;
2386 cur_class:=dvi_cl[cur_cmd];
2387 end;
2388
2389 @ The global variable |dvi_nf| is used for the number of different
2390 \.{DVI} fonts defined so far; their external font numbers (as extracted
2391 from the \.{DVI} file) are stored in the array |dvi_e_fnts|, the
2392 corresponding internal font numbers used internally by \.{\title} are
2393 stored in the array |dvi_i_fnts|.
2394
2395 @<Glob...@>=
2396 @!dvi_e_fnts:array[font_number] of int_32; {external font numbers}
2397 @!dvi_i_fnts:array[font_number] of font_number; {corresponding
2398 internal font numbers}
2399 @!dvi_nf:font_number; {number of \.{DVI} fonts defined so far}
2400
2401 @ @<Set ini...@>=
2402 dvi_nf:=0;
2403
2404 @ The |dvi_font| procedure sets |cur_fnt| to the internal font number
2405 corresponding to the external font number |cur_parm| (or aborts the
2406 program if such a font was never defined).
2407
2408 @p procedure dvi_font; {computes |cur_fnt| corresponding to |cur_parm|}
2409 var f:font_number; {where the font is sought}
2410 begin @<DVI: Locate font |cur_parm|@>;
2411 if f=dvi_nf then bad_dvi;
2412 cur_fnt:=dvi_i_fnts[f];
2413 if font_type(cur_fnt)=defined_font then load_font;
2414 end;
2415
2416 @ @<DVI: Locate font |cur_parm|@>=
2417 f:=0; dvi_e_fnts[dvi_nf]:=cur_parm;
2418 while cur_parm<>dvi_e_fnts[f] do incr(f)
2419
2420 @ Finally the |dvi_do_font| procedure is called when one of the commands
2421 |fnt_def1..fnt_def4| and its first parameter have been read from the
2422 \.{DVI} file; the argument indicates whether this should be the second
2423 definition of the font (|true|) or not (|false|).
2424
2425 @p procedure dvi_do_font(@!second:boolean);
2426 var f:font_number; {where the font is sought}
2427 @!k:int_15; {general purpose variable}
2428 begin print('DVI: font ',cur_parm:1);
2429 @<DVI: Locate font |cur_parm|@>;
2430 if (f=dvi_nf)=second then bad_dvi;
2431 font_check(nf):=dvi_squad;
2432 font_scaled(nf):=dvi_pquad;
2433 font_design(nf):=dvi_pquad;
2434 k:=dvi_ubyte; pckt_room(1); append_byte(k);
2435 Incr(k)(dvi_ubyte); pckt_room(k);
2436 while k>0 do begin append_byte(dvi_ubyte); decr(k);
2437 end;
2438 font_name(nf):=make_packet; {the font area plus name}
2439 dvi_i_fnts[dvi_nf]:=define_font(false);
2440 if not second then
2441 begin if dvi_nf=max_fonts then overflow(str_fonts,max_fonts);
2442 incr(dvi_nf);
2443 end
2444 else if dvi_i_fnts[f]<>dvi_i_fnts[dvi_nf] then bad_dvi;
2445 end;
2446
2447 @* Low-level VF input routines.
2448 A detailed description of the \.{VF} file format can be found in the
2449 documentation of \.{VFtoVP}; here we just define symbolic names for
2450 some of the \.{VF} command bytes.
2451
2452 @d long_char=242 {\.{VF} command for general character packet}
2453 @#
2454 @d vf_id=202 {identifies \.{VF} files}
2455
2456 @ The program uses the binary file variable |vf_file| for input from
2457 \.{VF} files; |vf_loc| is the number of the byte about to be read next
2458 from |vf_file|.
2459
2460 @<Glob...@>=
2461 @!vf_file:byte_file; {a \.{VF} file}
2462 @!vf_loc:int_32; {where we are about to look, in |vf_file|}
2463 @!vf_limit:int_32; {value of |vf_loc| at end of a character packet}
2464 @!vf_ext:pckt_pointer; {extension for \.{VF} files}
2465 @!vf_cur_fnt:font_number; {current font number in a \.{VF} file}
2466
2467 @ @<Initialize predefined strings@>=
2468 id3(".")("V")("F")(vf_ext); {file name extension for \.{VF} files}
2469
2470 @ If a \.{VF} file is badly malformed, we say |bad_font|; this procedure
2471 gives an error message which refers the user to \.{VFtoVP} and \.{VPtoVF},
2472 and terminates \.{\title}.
2473
2474 @<Cases for \(|bad_font|@>=
2475 vf_font_type: begin print('Bad VF file'); print_font(cur_fnt);
2476 @.Bad VF file@>
2477 print_ln(' loc=',vf_loc:1);
2478 abort('Use VFtoVP/VPtoVF to diagnose and correct the problem');
2479 @.Use VFtoVP/VPtoVF@>
2480 end;
2481
2482 @ If no font directory has been specified, \.{\title} is supposed to use
2483 the default \.{VF} directory, which is a system-dependent place where
2484 the \.{VF} files for standard fonts are kept.
2485 The string variable |VF_default_area| contains the name of this area.
2486 @^system dependencies@>
2487
2488 @d VF_default_area_name=='TeXvfonts:' {change this to the correct name}
2489 @d VF_default_area_name_length=10 {change this to the correct length}
2490
2491 @<Glob...@>=
2492 @!VF_default_area:packed array[1..VF_default_area_name_length] of char;
2493
2494 @ @<Set init...@>=
2495 VF_default_area:=VF_default_area_name;
2496
2497 @ To prepare |vf_file| for input we |reset| it.
2498
2499 @<VF: Open |vf_file| or |goto not_found|@>=
2500 make_font_name(VF_default_area_name_length)(VF_default_area)(vf_ext);
2501 reset(vf_file,cur_name);
2502 if eof(vf_file) then
2503 @^system dependencies@>
2504 goto not_found;
2505 vf_loc:=0
2506
2507 @ Reading a \.{VF} file should be done as efficient as possible for a
2508 particular system; on many systems this means that a large number of
2509 bytes from |vf_file| is read into a buffer and will then be extracted
2510 from that buffer. In order to simplify such system dependent changes
2511 we use a pair of \.{WEB} macros: |vf_byte| extracts the next \.{VF}
2512 byte and |vf_eof| is |true| if we have reached the end of the \.{VF}
2513 file. Here we give simple minded definitions for these macros in terms
2514 of standard \PASCAL.
2515 @^system dependencies@>
2516 @^optimization@>
2517
2518 @d vf_eof == eof(vf_file) {has the \.{VF} file been exhausted?}
2519 @d vf_byte(#) ==
2520 if vf_eof then bad_font
2521 else read(vf_file,#) {obtain next \.{VF} byte}
2522
2523 @ We need several simple functions to read the next byte or bytes
2524 from |vf_file|.
2525
2526 @p function vf_ubyte:int_8u; {returns the next byte, unsigned}
2527 @!begin_byte(vf_byte); incr(vf_loc); comp_ubyte(vf_ubyte);
2528 end;
2529 @#
2530 function vf_upair:int_16u; {returns the next two bytes, unsigned}
2531 @!begin_pair(vf_byte); Incr(vf_loc)(2); comp_upair(vf_upair);
2532 end;
2533 @#
2534 function vf_strio:int_24; {returns the next three bytes, signed}
2535 @!begin_trio(vf_byte); Incr(vf_loc)(3); comp_strio(vf_strio);
2536 end;
2537 @#
2538 function vf_utrio:int_24u; {returns the next three bytes, unsigned}
2539 @!begin_trio(vf_byte); Incr(vf_loc)(3); comp_utrio(vf_utrio);
2540 end;
2541 @#
2542 function vf_squad:int_32; {returns the next four bytes, signed}
2543 @!begin_quad(vf_byte); Incr(vf_loc)(4); comp_squad(vf_squad);
2544 end;
2545
2546 @ All dimensions in a \.{VF} file, except the design sizes of a virtual
2547 font and its local fonts, are |fix_word|s that must be scaled in exactly
2548 the same way as the character widths from a \.{TFM} file; we can use the
2549 same code, but this time |z|, |alpha|, and |beta| are global variables.
2550
2551 @<Glob...@>=
2552 @<Variables for scaling computation@>@;
2553
2554 @ We need five functions to read the next byte or bytes and convert a
2555 |fix_word| to a scaled dimension.
2556
2557 @p function vf_fix1:int_32; {returns the next byte as scaled value}
2558 var x:int_32; {accumulator}
2559 begin vf_byte(tfm_b3); incr(vf_loc);
2560 tfm_fix1(x); vf_fix1:=x;
2561 end;
2562 @#
2563 function vf_fix2:int_32; {returns the next two bytes as scaled value}
2564 var x:int_32; {accumulator}
2565 begin vf_byte(tfm_b2); vf_byte(tfm_b3); Incr(vf_loc)(2);
2566 tfm_fix2(x); vf_fix2:=x;
2567 end;
2568 @#
2569 function vf_fix3:int_32; {returns the next three bytes as scaled value}
2570 var x:int_32; {accumulator}
2571 begin vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
2572 Incr(vf_loc)(3);@/
2573 tfm_fix3(x); vf_fix3:=x;
2574 end;
2575 @#
2576 function vf_fix3u:int_32; {returns the next three bytes as scaled value}
2577 begin vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
2578 Incr(vf_loc)(3);@/
2579 vf_fix3u:=tfm_fix3u;
2580 end;
2581 @#
2582 function vf_fix4:int_32; {returns the next four bytes as scaled value}
2583 var x:int_32; {accumulator}
2584 begin vf_byte(tfm_b0); vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
2585 Incr(vf_loc)(4);@/
2586 tfm_fix4(x); vf_fix4:=x;
2587 end;
2588
2589 @ Three other functions are used in cases where the result must have a
2590 non-negative value or a positive value.
2591
2592 @p function vf_uquad:int_31; {result must be non-negative}
2593 var x:int_32;
2594 begin x:=vf_squad; if x<0 then bad_font @+ else vf_uquad:=x;
2595 end;
2596 @#
2597 function vf_pquad:int_31; {result must be positive}
2598 var x:int_32;
2599 begin x:=vf_squad; if x<=0 then bad_font @+ else vf_pquad:=x;
2600 end;
2601 @#
2602 function vf_fixp:int_31; {result must be positive}
2603 begin vf_byte(tfm_b0); vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
2604 Incr(vf_loc)(4);@/
2605 if tfm_b0>0 then bad_font;
2606 vf_fixp:=tfm_fix3u;
2607 end;
2608
2609 @ The |vf_first_par| procedure first reads a \.{VF} command byte into
2610 |cur_cmd|; then |cur_parm| is set to the value of the first parameter
2611 (if any) and |cur_class| to the command class.
2612
2613 @d set_cur_wp_end(#)== if cur_wp=invalid_width then #
2614 @d set_cur_wp(#)== {set |cur_wp| to the char's width pointer}
2615 cur_wp:=invalid_width;
2616 if #<>invalid_font then
2617 if (cur_res>=font_bc(#))and(cur_res<=font_ec(#)) then
2618 begin cur_cp:=font_chars(#)+cur_res; cur_wp:=char_widths[cur_cp];
2619 end;
2620 set_cur_wp_end
2621
2622 @p procedure vf_first_par;
2623 begin cur_cmd:=vf_ubyte;
2624 case dvi_par[cur_cmd] of
2625 char_par:
2626 begin set_cur_char(vf_ubyte); set_cur_wp(vf_cur_fnt)(bad_font);
2627 end;
2628 no_par: do_nothing;
2629 dim1_par: cur_parm:=vf_fix1;
2630 num1_par: cur_parm:=vf_ubyte;
2631 dim2_par: cur_parm:=vf_fix2;
2632 num2_par: cur_parm:=vf_upair;
2633 dim3_par: cur_parm:=vf_fix3;
2634 num3_par: cur_parm:=vf_utrio;
2635 dim4_par: cur_parm:=vf_fix4;
2636 num4_par: cur_parm:=vf_squad;
2637 numu_par: cur_parm:=vf_uquad;
2638 rule_par:
2639 begin cur_v_dimen:=vf_fix4; cur_h_dimen:=vf_fix4;
2640 cur_upd:=(cur_cmd=set_rule);
2641 end;
2642 fnt_par:cur_parm:=cur_cmd-fnt_num_0;
2643 othercases abort('internal error');
2644 endcases;
2645 cur_class:=dvi_cl[cur_cmd];
2646 end;
2647
2648 @ For a virtual font we set |font_type(f):=vf_font_type|; in this case
2649 |font_font(f)| is the default font for character packets from virtual
2650 font~|f|.
2651 @^font types@>
2652
2653 The global variable |vf_nf| is used for the number of different local
2654 fonts defined in a \.{VF} file so far; their external font numbers (as
2655 extracted from the \.{VF} file) are stored in the array |vf_e_fnts|, the
2656 corresponding internal font numbers used internally by \.{\title} are
2657 stored in the array |vf_i_fnts|.
2658
2659 @<Glob...@>=
2660 @!vf_e_fnts:array[font_number] of int_32; {external font numbers}
2661 @!vf_i_fnts:array[font_number] of font_number; {corresponding
2662 internal font numbers}
2663 @!vf_nf:font_number; {number of local fonts defined so far}
2664 @!lcl_nf:font_number; {largest |vf_nf| value for any \.{VF} file}
2665
2666 @ @<Set init...@>=
2667 lcl_nf:=0;
2668
2669 @ The |vf_font| procedure sets |vf_cur_fnt| to the internal font number
2670 corresponding to the external font number |cur_parm| (or aborts the
2671 program if such a font was never defined).
2672
2673 @p procedure vf_font; {computes |vf_cur_fnt| corresponding to |cur_parm|}
2674 var f:font_number; {where the font is sought}
2675 begin @<VF: Locate font |cur_parm|@>;
2676 if f=vf_nf then bad_font;
2677 vf_cur_fnt:=vf_i_fnts[f];
2678 end;
2679
2680 @ @<VF: Locate font |cur_parm|@>=
2681 f:=0; vf_e_fnts[vf_nf]:=cur_parm;
2682 while cur_parm<>vf_e_fnts[f] do incr(f)
2683
2684 @ Finally the |vf_do_font| procedure is called when one of the commands
2685 |fnt_def1..fnt_def4| and its first parameter have been read from the
2686 \.{VF} file.
2687
2688 @p procedure vf_do_font;
2689 var f:font_number; {where the font is sought}
2690 @!k:int_15; {general purpose variable}
2691 begin print('VF: font ',cur_parm:1);@/
2692 @<VF: Locate font |cur_parm|@>;
2693 if f<>vf_nf then bad_font;
2694 font_check(nf):=vf_squad;
2695 font_scaled(nf):=vf_fixp;
2696 font_design(nf):=round(tfm_conv*vf_pquad);
2697 k:=vf_ubyte; pckt_room(1); append_byte(k);
2698 Incr(k)(vf_ubyte); pckt_room(k);
2699 while k>0 do begin append_byte(vf_ubyte); decr(k);
2700 end;
2701 font_name(nf):=make_packet; {the font area plus name}
2702 vf_i_fnts[vf_nf]:=define_font(true);
2703 if vf_nf=lcl_nf then
2704 if lcl_nf=max_fonts then overflow(str_fonts,max_fonts)
2705 else incr(lcl_nf);
2706 incr(vf_nf);
2707 end;
2708
2709 @* Reading VF files.
2710 The |do_vf| function attempts to read the \.{VF} file for a font and
2711 returns |false| if the \.{VF} file could not be found; otherwise the
2712 font type is changed to |vf_font_type|.
2713
2714 @p function do_vf:boolean; {read a \.{VF} file}
2715 label reswitch,done,not_found,exit;
2716 var temp_byte:int_8u; {byte for temporary variables}
2717 @!k:byte_pointer; {index into |byte_mem|}
2718 @!l:int_15; {general purpose variable}
2719 @!save_ext:int_24; {used to save |cur_ext|}
2720 @!save_res:int_8u; {used to save |cur_res|}
2721 @!save_cp:width_pointer; {used to save |cur_cp|}
2722 @!save_wp:width_pointer; {used to save |cur_wp|}
2723 @!save_upd:boolean; {used to save |cur_upd|}
2724 @!vf_wp:width_pointer; {width pointer for the current character packet}
2725 @!vf_fnt:font_number; {current font in the current character packet}
2726 @!move_zero:boolean; {|true| if rule 1 is used}
2727 @!last_pop:boolean; {|true| if final |pop| has been manufactured}
2728 begin @<VF: Open |vf_file| or |goto not_found|@>;
2729 save_ext:=cur_ext; save_res:=cur_res; save_cp:=cur_cp; save_wp:=cur_wp;
2730 save_upd:=cur_upd; {save}
2731 font_type(cur_fnt):=vf_font_type;@/
2732 @<VF: Process the preamble@>;@/
2733 @<VF: Process the font definitions@>;@/
2734 while cur_cmd<=long_char do @<VF: Build a character packet@>;
2735 if cur_cmd<>post then bad_font;
2736 @!debug print('VF file for font ',cur_fnt:1); print_font(cur_fnt);
2737 print_ln(' loaded.');
2738 gubed @;@/
2739 close_in(vf_file);
2740 cur_ext:=save_ext; cur_res:=save_res; cur_cp:=save_cp; cur_wp:=save_wp;
2741 cur_upd:=save_upd; {restore}
2742 do_vf:=true; return;
2743 not_found:do_vf:=false;
2744 exit:end;
2745
2746 @ @<VF: Process the preamble@>=
2747 if vf_ubyte<>pre then bad_font;
2748 if vf_ubyte<>vf_id then bad_font;
2749 temp_byte:=vf_ubyte; pckt_room(temp_byte);
2750 for l:=1 to temp_byte do append_byte(vf_ubyte);
2751 print('VF file: '''); print_packet(new_packet); print(''',');
2752 flush_packet;@/
2753 check_check_sum(vf_squad,false);
2754 check_design_size(round(tfm_conv*vf_pquad));@/
2755 z:=font_scaled(cur_fnt);
2756 @<Replace |z|...@>;@/
2757 print_nl(' for font ',cur_fnt:1); print_font(cur_fnt); print_ln('.')
2758
2759 @ @<VF: Process the font definitions@>=
2760 vf_i_fnts[0]:=invalid_font; vf_nf:=0;@/
2761 cur_cmd:=vf_ubyte;
2762 while (cur_cmd>=fnt_def1)and(cur_cmd<=fnt_def1+3) do
2763 begin case cur_cmd-fnt_def1 of
2764 0: cur_parm:=vf_ubyte;
2765 1: cur_parm:=vf_upair;
2766 2: cur_parm:=vf_utrio;
2767 3: cur_parm:=vf_squad;
2768 end; {there are no other cases}
2769 vf_do_font;
2770 cur_cmd:=vf_ubyte;
2771 end;
2772 font_font(cur_fnt):=vf_i_fnts[0]
2773
2774 @ The \.{VF} format specifies that the interpretation of each packet
2775 begins with |w=x=y=z=0|; any |w0|, |x0|, |y0|, or |z0| command using
2776 these initial values will be ignored.
2777
2778 @<Types...@>=
2779 @!vf_state=array[0..1,0..1] of boolean; {state of |w|, |x|, |y|, and |z|}
2780
2781 @ As implied by the \.{VF} format the \.{DVI} commands read from the \.{VF}
2782 file are enclosed by |push| and |pop|; as we read \.{DVI}
2783 commands and append them to |byte_mem|, we perform a set of
2784 transformations in order to simplify the resulting packet: Let |zero| be
2785 any of the commands |put|, |put_rule|, |fnt_num|, |fnt|, or |xxx| which
2786 all leave the current position on the page unchanged, let |move| be any
2787 of the horizontal or vertical movement commands |right1..z4|, and let
2788 |any| be any sequence of commands containing |push| and |pop| in
2789 properly nested pairs; whenever possible we apply one of the following
2790 transformation rules: $$\def\n#1:{\hbox to 3cm{\hfil#1:}}
2791 \leqalignno{
2792 \hbox{|push| |zero|}&\RA\hbox{|zero| |push|}&\n1:\cr
2793 \hbox{|move| |pop|}&\RA\hbox{|pop|}&\n2:\cr
2794 \hbox{|push| |pop|}&\RA{}&\n3:\cr
2795 \hbox{|push| |set_char| |pop|}&\RA\hbox{|put|}&\n4a:\cr
2796 \hbox{|push| \\{set} |pop|}&\RA\hbox{|put|}&\n4b:\cr
2797 \hbox{|push| |set_rule| |pop|}&\RA\hbox{|put_rule|}&\n4c:\cr
2798 \hbox{|push| |push| |any| |pop|}&\RA\hbox{|push| |any| |pop| |push|}&\n5:\cr
2799 \hbox{|push| |any| |pop| |pop|}&\RA\hbox{|any| |pop|}&\n6:\cr
2800 }$$
2801
2802 @ In order to perform these transformations we need a stack which is
2803 indexed by |vf_ptr|, the number of |push| commands without corresponding
2804 |pop| in the packet we are building; the |vf_push_loc| array contains
2805 the locations in |byte_mem| following such |push| commands.
2806 In view of rule~5 consecutive |push| commands are never stored, the
2807 |vf_push_num| array is used to count them.
2808 The |vf_last| array indicates the type of the last non-discardable item:
2809 a character, a rule, or a group enclosed by |push| and |pop|;
2810 the |vf_last_end| array points to the ending locations and, if
2811 |vf_last<>vf_other|, the |vf_last_loc| array points to the starting
2812 locations of these items.
2813
2814 @d vf_set=0 {|vf_set=char_cl|, last item is a |set_char| or \\{set}}
2815 @d vf_rule=1 {|vf_rule=rule_cl|, last item is a |set_rule|}
2816 @d vf_group=2 {last item is a group enclosed by |push| and |pop|}
2817 @d vf_put=3 {last item is a |put|}
2818 @d vf_other=4 {last item (if any) is none of the above}
2819
2820 @<Types...@>=
2821 @!vf_type=vf_set..vf_other;
2822
2823 @ @<Glob...@>=
2824 @!vf_move: array[stack_pointer] of vf_state; {state of |w|, |x|, |y|, and |z|}
2825 @!vf_push_loc: array[stack_pointer] of byte_pointer; {end of a |push|}
2826 @!vf_last_loc: array[stack_pointer] of byte_pointer; {start of an item}
2827 @!vf_last_end: array[stack_pointer] of byte_pointer; {end of an item}
2828 @!vf_push_num: array[stack_pointer] of eight_bits; {|push| count}
2829 @!vf_last: array[stack_pointer] of vf_type; {type of last item}
2830 @!vf_ptr:stack_pointer; {current number of unfinished groups}
2831 @!stack_used:stack_pointer; {largest |vf_ptr| or |stack_ptr| value}
2832
2833 @ We use two small arrays to determine the item type of a character or a
2834 rule.
2835
2836 @<Glob...@>=
2837 @!vf_char_type:array[boolean] of vf_type;
2838 @!vf_rule_type:array[boolean] of vf_type;
2839
2840 @ @<Set init...@>=
2841 vf_move[0][0][0]:=false; vf_move[0][0][1]:=false;
2842 vf_move[0][1][0]:=false; vf_move[0][1][1]:=false;@/
2843 stack_used:=0;@/
2844 vf_char_type[false]:=vf_put; vf_char_type[true]:=vf_set;@/
2845 vf_rule_type[false]:=vf_other; vf_rule_type[true]:=vf_rule;
2846
2847 @ Here we read the first bytes of a character packet from the \.{VF}
2848 file and initialize the packet being built in |byte_mem|; the start of
2849 the whole packet is stored in |vf_push_loc[0]|. When the character
2850 packet is finished, a type is assigned to it: |vf_simple| if the
2851 packet ends with a character of the correct width, or |vf_complex|
2852 otherwise. Moreover, if such a packet for a character with
2853 extension zero consists of just one character with extension zero and
2854 the same residue, and if there is no previous packet, the whole packet
2855 is replaced by the empty packet.
2856
2857 @d vf_simple=0 {the packet ends with a character of the correct width}
2858 @d vf_complex=vf_simple+1 {otherwise}
2859
2860 @<VF: Build a character packet@>=
2861 begin if cur_cmd<long_char then
2862 begin vf_limit:=cur_cmd;
2863 cur_ext:=0; cur_res:=vf_ubyte; vf_wp:=check_width(vf_fix3u);
2864 end
2865 else begin vf_limit:=vf_uquad;
2866 cur_ext:=vf_strio; cur_res:=vf_ubyte; vf_wp:=check_width(vf_fix4);
2867 end;
2868 Incr(vf_limit)(vf_loc);
2869 vf_push_loc[0]:=byte_ptr; vf_last_end[0]:=byte_ptr;
2870 vf_last[0]:=vf_other; vf_ptr:=0;@/
2871 start_packet(vf_complex);
2872 @<VF: Append \.{DVI} commands to the character packet@>;@/
2873 k:=pckt_start[pckt_ptr];
2874 if vf_last[0]=vf_put then if cur_wp=vf_wp then
2875 begin decr(byte_mem[k]); {change |vf_complex| into |vf_simple|}
2876 if (byte_mem[k]=bi(0))and@|(vf_push_loc[0]=vf_last_loc[0])and@|
2877 (cur_ext=0)and@|(cur_res=pckt_res) then byte_ptr:=k;
2878 end;
2879 build_packet;
2880 cur_cmd:=vf_ubyte;
2881 end
2882
2883 @ For every \.{DVI} command read from the \.{VF} file some action is
2884 performed; in addition the initial |push| and the final |pop| are
2885 manufactured here.
2886
2887 @<VF: Append \.{DVI} commands to the character packet@>=
2888 vf_cur_fnt:=font_font(cur_fnt); vf_fnt:=vf_cur_fnt;@/
2889 last_pop:=false; cur_class:=push_cl; {initial |push|}
2890 loop begin
2891 reswitch:case cur_class of
2892 three_cases(char_cl): @<VF: Do a |char|, |rule|, or |xxx|@>;
2893 push_cl: @<VF: Do a |push|@>;
2894 pop_cl: @<VF: Do a |pop|@>;
2895 two_cases(w0_cl):
2896 if vf_move[vf_ptr][0][cur_class-w0_cl] then append_one(cur_cmd);
2897 three_cases(right_cl):
2898 begin pckt_signed(dvi_right_cmd[cur_class],cur_parm);
2899 if cur_class>=w_cl then vf_move[vf_ptr][0][cur_class-w_cl]:=true;
2900 end;
2901 two_cases(y0_cl):
2902 if vf_move[vf_ptr][1][cur_class-y0_cl] then append_one(cur_cmd);
2903 three_cases(down_cl):
2904 begin pckt_signed(dvi_down_cmd[cur_class],cur_parm);
2905 if cur_class>=y_cl then vf_move[vf_ptr][1][cur_class-y_cl]:=true;
2906 end;
2907 fnt_cl: vf_font;
2908 fnt_def_cl: bad_font;
2909 invalid_cl: if cur_cmd<>nop then bad_font;
2910 othercases abort('internal error');
2911 endcases;
2912 if vf_loc<vf_limit then vf_first_par
2913 else if last_pop then goto done
2914 else begin cur_class:=pop_cl; last_pop:=true; {final |pop|}
2915 end;
2916 end;
2917 done:if (vf_ptr<>0)or(vf_loc<>vf_limit) then bad_font
2918
2919 @ For a |push| we either increase |vf_push_num| or start a new level and
2920 append a |push|.
2921
2922 @d incr_stack(#)==
2923 if #=stack_used then
2924 if stack_used=stack_size then overflow(str_stack,stack_size)
2925 else incr(stack_used);
2926 incr(#)
2927
2928 @<VF: Do a |push|@>=
2929 if (vf_ptr>0)and(vf_push_loc[vf_ptr]=byte_ptr) then
2930 begin if vf_push_num[vf_ptr]=255 then overflow(str_stack,255);
2931 incr(vf_push_num[vf_ptr]);
2932 end
2933 else begin incr_stack(vf_ptr);
2934 @<VF: Start a new level@>;
2935 vf_push_num[vf_ptr]:=0;
2936 end
2937
2938 @ @<VF: Start a new level@>=
2939 append_one(push);
2940 vf_move[vf_ptr]:=vf_move[vf_ptr-1];
2941 vf_push_loc[vf_ptr]:=byte_ptr;
2942 vf_last_end[vf_ptr]:=byte_ptr;
2943 vf_last[vf_ptr]:=vf_other
2944
2945 @ When a character, a rule, or an |xxx| is appended, transformation
2946 rule~1 might be applicable.
2947
2948 @<VF: Do a |char|, |rule|, or |xxx|@>=
2949 begin if (vf_ptr=0)or(byte_ptr>vf_push_loc[vf_ptr]) then move_zero:=false
2950 else case cur_class of
2951 char_cl: move_zero:=(not cur_upd)or(vf_cur_fnt<>vf_fnt);
2952 rule_cl: move_zero:=not cur_upd;
2953 xxx_cl: move_zero:=true;
2954 othercases abort('internal error');
2955 endcases;
2956 if move_zero then
2957 begin decr(byte_ptr); decr(vf_ptr);
2958 end;
2959 case cur_class of
2960 char_cl: @<VF: Do a |fnt|, a |char|, or both@>;
2961 rule_cl: @<VF: Do a |rule|@>;
2962 xxx_cl: @<VF: Do an |xxx|@>;
2963 end; {there are no other cases}
2964 vf_last_end[vf_ptr]:=byte_ptr;
2965 if move_zero then
2966 begin incr(vf_ptr); append_one(push); vf_push_loc[vf_ptr]:=byte_ptr;
2967 vf_last_end[vf_ptr]:=byte_ptr;
2968 if cur_class=char_cl then if cur_upd then goto reswitch;
2969 end;
2970 end
2971
2972 @ A special situation arises if transformation rule~1 is applied to a
2973 |fnt_num| of |fnt| command, but not to the |set_char| or \\{set} command
2974 following it; in this case |cur_upd| and |move_zero| are both |true| and
2975 the |set_char| or \\{set} command will be appended later.
2976
2977 @<VF: Do a |fnt|, a |char|, or both@>=
2978 begin if vf_cur_fnt<>vf_fnt then
2979 begin vf_last[vf_ptr]:=vf_other;
2980 pckt_unsigned(fnt1,vf_cur_fnt); vf_fnt:=vf_cur_fnt;
2981 end;
2982 if (not move_zero)or(not cur_upd) then
2983 begin vf_last[vf_ptr]:=vf_char_type[cur_upd];
2984 vf_last_loc[vf_ptr]:=byte_ptr;
2985 pckt_char(cur_upd,cur_ext,cur_res);
2986 end;
2987 end
2988
2989 @ @<VF: Do a |rule|@>=
2990 begin vf_last[vf_ptr]:=vf_rule_type[cur_upd];
2991 vf_last_loc[vf_ptr]:=byte_ptr;
2992 append_one(dvi_rule_cmd[cur_upd]);
2993 pckt_four(cur_v_dimen); pckt_four(cur_h_dimen);
2994 end
2995
2996 @ @<VF: Do an |xxx|@>=
2997 begin vf_last[vf_ptr]:=vf_other;
2998 pckt_unsigned(xxx1,cur_parm); pckt_room(cur_parm);
2999 while cur_parm>0 do
3000 begin append_byte(vf_ubyte); decr(cur_parm);
3001 end;
3002 end
3003
3004 @ Transformation rules 2--6 are triggered by a |pop|, either read from
3005 the \.{VF} file or manufactured at the end of the packet.
3006
3007 @<VF: Do a |pop|@>=
3008 begin if vf_ptr<1 then bad_font;
3009 byte_ptr:=vf_last_end[vf_ptr]; {this is rule 2}
3010 if vf_last[vf_ptr]<=vf_rule then
3011 if vf_last_loc[vf_ptr]=vf_push_loc[vf_ptr] then
3012 @<VF: Prepare for rule 4@>;
3013 if byte_ptr=vf_push_loc[vf_ptr] then @<VF: Apply rule 3 or 4@>
3014 else begin if vf_last[vf_ptr]=vf_group then @<VF: Apply rule 6@>;
3015 append_one(pop); decr(vf_ptr); vf_last[vf_ptr]:=vf_group;
3016 vf_last_loc[vf_ptr]:=vf_push_loc[vf_ptr+1]-1;
3017 vf_last_end[vf_ptr]:=byte_ptr;
3018 if vf_push_num[vf_ptr+1]>0 then @<VF: Apply rule 5@>;
3019 end;
3020 end
3021
3022 @ In order to implement transformation rule~4, we cancel the |set_char|,
3023 \\{set}, or |set_rule|, append a |pop|, and insert a |put| or |put_rule|
3024 with the old parameters.
3025
3026 @<VF: Prepare for rule 4@>=
3027 begin cur_class:=vf_last[vf_ptr]; cur_upd:=false;
3028 byte_ptr:=vf_push_loc[vf_ptr];
3029 end
3030
3031 @ @<VF: Apply rule 3 or 4@>=
3032 begin if vf_push_num[vf_ptr]>0 then
3033 begin decr(vf_push_num[vf_ptr]);
3034 vf_move[vf_ptr]:=vf_move[vf_ptr-1];
3035 end
3036 else begin decr(byte_ptr); decr(vf_ptr);
3037 end;
3038 if cur_class<>pop_cl then goto reswitch; {this is rule 4}
3039 end
3040
3041 @ @<VF: Apply rule 6@>=
3042 begin Decr(byte_ptr)(2);
3043 for k:=vf_last_loc[vf_ptr]+1 to byte_ptr do byte_mem[k-1]:=byte_mem[k];
3044 vf_last[vf_ptr]:=vf_other; vf_last_end[vf_ptr]:=byte_ptr;
3045 end
3046
3047 @ @<VF: Apply rule 5@>=
3048 begin incr(vf_ptr);
3049 @<VF: Start a new level@>;
3050 decr(vf_push_num[vf_ptr]);
3051 end
3052
3053 @ The \.{VF} format specifies that after a character packet invoked by a
3054 |set_char| or \\{set} command, ``|h|~is increased by the \.{TFM} width
3055 (properly scaled)---just as if a simple character had been typeset'';
3056 for |vf_simple| packets this is achieved by changing the final |put|
3057 command into |set_char| or \\{set}, but for |vf_complex| packets an
3058 explicit movement must be done. This poses a problem for programs,
3059 such as \.{DVIcopy}, which write a new \.{DVI} file with all references
3060 to characters from virtual fonts replaced by their character packets:
3061 The \.{DVItype} program specifies that the horizontal movements after a
3062 |set_char| or \\{set} command, after a |set_rule| command, and after one
3063 of the commands |right1..x4|, are all treated differently when \.{DVI}
3064 units are converted to pixels.
3065
3066 Thus we introduce a slight extension of \.{DVItype}'s pixel rounding
3067 algorithm and hope that this extension will become part of the standard
3068 \.{DVItype} program in the near future: If a \.{DVI} file contains a
3069 |set_rule| command for a rule with the negative height |width_dimen|,
3070 then this rule shall be treated in exactly the same way as a fictitious
3071 character whose width is the width of that rule; as value of |width_dimen|
3072 we choose $-2^{31}$, the smallest signed 32-bit integer.
3073
3074 @<Glob...@>=
3075 @!width_dimen:int_32; {vertical dimension of special rules}
3076
3077 @ When initializing |width_dimen| we are careful to avoid arithmetic
3078 overflow.
3079
3080 @<Set init...@>=
3081 width_dimen:=-@"40000000; Decr(width_dimen)(@"40000000);
3082
3083 @* Terminal communication.
3084 When \.{\title} begins, it engages the user in a brief dialog so that
3085 various options may be specified. This part of \.{\title} requires
3086 nonstandard \PASCAL\ constructions to handle the online interaction; so
3087 it may be preferable in some cases to omit the dialog and simply to
3088 stick to the default options. On other hand, the system-dependent
3089 routines that are needed are not complicated, so it will not be terribly
3090 difficult to introduce them; furthermore they are similar to those in
3091 \.{DVItype}.
3092
3093 It may be desirable to (optionally) specify all the options in the
3094 command line and skip the dialog with the user, provided the operating
3095 system permits this. Here we just define the system-independent part of the
3096 code required for this possibility. Since a complete option (a keyword
3097 possibly followed by one or several parameters) may have embedded blanks
3098 it might be necessary to replace these blanks by some other separator,
3099 e.g., by a `\./'. Using, e.g., Unix style options one might then say
3100 $$\.{\title\space-mag/2000 -sel/17.3/5 -sel/47 ...}$$
3101 to override the magnification factor that is stated in the \.{DVI} file,
3102 and to select five pages starting with the page numbered~17.3 as well as
3103 all remaining pages starting with the one numbered~47; alternatively one
3104 might simply say
3105 $$\.{\title\space- ...}$$
3106 to skip the dialog and use the default options.
3107
3108 The system-dependent initialization code should set the |n_opt| variable
3109 to the number of options found in the command line. If |n_opt=0| the
3110 |input_ln| procedure defined below will prompt the user for options. If
3111 |n_opt>0| the |k_opt| variable will be incremented and another piece of
3112 system-dependent code is invoked instead of the dialog; that code should
3113 place the value of command line option number |k_opt| as temporary
3114 string into the |byte-mem| array. This process will be repeated until
3115 |k_opt=n_opt|, indicating that all command line options have been
3116 processed.
3117 @^system dependencies@>
3118
3119 @d opt_separator="/" {acts as blank when scanning (command line) options}
3120
3121 @<Set init...@>=
3122 n_opt:=0; {change this to indicate the presence of command line options}
3123 k_opt:=0; {just in case}
3124
3125 @ The |input_ln| routine waits for the user to type a line at his or her
3126 terminal; then it puts ASCII-code equivalents for the characters on that
3127 line into the |byte_mem| array as a temporary string. \PASCAL's
3128 standard |input| file is used for terminal input, as |output| is used
3129 for terminal output.
3130
3131 Since the terminal is being used for both input and output, some systems
3132 need a special routine to make sure that the user can see a prompt message
3133 before waiting for input based on that message. (Otherwise the message
3134 may just be sitting in a hidden buffer somewhere, and the user will have
3135 no idea what the program is waiting for.) We shall invoke a system-dependent
3136 subroutine |update_terminal| in order to avoid this problem.
3137 @^system dependencies@>
3138
3139 @d update_terminal == break(output) {empty the terminal output buffer}
3140 @#
3141 @d scan_blank(#)== {tests for `blank' when scanning (command line) options}
3142 ((byte_mem[#]=bi(" "))or(byte_mem[#]=bi(opt_separator)))
3143 @d scan_skip== {skip `blanks'}
3144 while scan_blank(scan_ptr)and(scan_ptr<byte_ptr) do incr(scan_ptr)
3145 @d scan_init== {initialize |scan_ptr|}
3146 byte_mem[byte_ptr]:=bi(" "); scan_ptr:=pckt_start[pckt_ptr-1]; scan_skip
3147
3148 @<Action procedures for |dialog|@>=
3149 procedure input_ln; {inputs a line from the terminal}
3150 var k:0..terminal_line_length;
3151 begin if n_opt=0 then
3152 begin print('Enter option: '); update_terminal; reset(input);
3153 if eoln(input) then read_ln(input);
3154 k:=0; pckt_room(terminal_line_length);
3155 while (k<terminal_line_length)and not eoln(input) do
3156 begin append_byte(xord[input^]); incr(k); get(input);
3157 end;
3158 end
3159 else if k_opt<n_opt then
3160 begin incr(k_opt);
3161 {Copy command line option number |k_opt| into |byte_mem| array!}
3162 end;
3163 end;
3164
3165 @ The global variable |scan_ptr| is used while scanning the temporary
3166 packet; it points to the next byte in |byte_mem| to be examined.
3167
3168 @<Glob...@>=
3169 @!n_opt:int_16; {number of options found in command line}
3170 @!k_opt:int_16; {number of command line options processed}
3171 @!scan_ptr:byte_pointer; {pointer to next byte to be examined}
3172 @!sep_char:text_char; {|' '| or |xchr[opt_separator]|}
3173
3174 @ The |scan_keyword| function is used to test for keywords in a character
3175 string stored as temporary packet in |byte_mem|; the result is |true|
3176 (and |scan_ptr| is updated) if the characters starting at position
3177 |scan_ptr| are an abbreviation of a given keyword followed by at least
3178 one blank.
3179
3180 @<Action procedures for |dialog|@>=
3181 function scan_keyword(@!p:pckt_pointer;@!l:int_7):boolean;
3182 var i,@!j,@!k:byte_pointer; {indices into |byte_mem|}
3183 begin i:=pckt_start[p]; j:=pckt_start[p+1]; k:=scan_ptr;
3184 while (i<j)and((byte_mem[k]=byte_mem[i])or(byte_mem[k]=byte_mem[i]-"a"+"A")) do
3185 begin incr(i); incr(k);
3186 end;
3187 if scan_blank(k)and(i-pckt_start[p]>=l) then
3188 begin scan_ptr:=k; scan_skip; scan_keyword:=true;
3189 end
3190 else scan_keyword:=false;
3191 end;
3192
3193 @ Here is a routine that scans a (possibly signed) integer and computes
3194 the decimal value. If no decimal integer starts at |scan_ptr|, the
3195 value~0 is returned. The integer should be less than $2^{31}$ in
3196 absolute value.
3197
3198 @<Action procedures for |dialog|@>=
3199 function scan_int:int_32;
3200 var x:int_32; {accumulates the value}
3201 @!negative:boolean; {should the value be negated?}
3202 begin if byte_mem[scan_ptr]="-" then
3203 begin negative:=true; incr(scan_ptr);
3204 end
3205 else negative:=false;
3206 x:=0;
3207 while (byte_mem[scan_ptr]>="0")and(byte_mem[scan_ptr]<="9") do
3208 begin x:=10*x+byte_mem[scan_ptr]-"0"; incr(scan_ptr);
3209 end;
3210 scan_skip;
3211 if negative then scan_int:=-x @+ else scan_int:=x;
3212 end;
3213
3214 @ The selected options are put into global variables by the |dialog|
3215 procedure, which is called just as \.{\title} begins.
3216 @^system dependencies@>
3217
3218 @p @<Action procedures for |dialog|@>@;
3219 procedure dialog;
3220 label exit;
3221 var p:pckt_pointer; {packet being created}
3222 begin @<Initialize options@>@;
3223 loop begin input_ln; p:=new_packet; scan_init;
3224 if scan_ptr=byte_ptr then
3225 begin flush_packet; return;
3226 end@;@/
3227 @<Cases for options@>@;@/
3228 else begin if n_opt=0 then sep_char:=' '
3229 else sep_char:=xchr[opt_separator];
3230 print_options;
3231 if n_opt>0 then
3232 begin print('Bad command line option: ');
3233 print_packet(p); abort('---run terminated');
3234 end;
3235 end;
3236 flush_packet;
3237 end;
3238 exit:end;
3239
3240 @ The |print_options| procedure might be used in a `Usage message'
3241 displaying the command line syntax.
3242
3243 @<Basic printing...@>=
3244 procedure print_options;
3245 begin print_ln('Valid options are:');
3246 @<Print valid options@>@;
3247 end;
3248
3249 @* Subroutines for typesetting commands.
3250 This is the central part of the whole \.{\title} program:
3251 When a typesetting command from the \.{DVI} file or from a \.{VF} packet
3252 has been decoded, one of the typesetting routines defined below is
3253 invoked to execute the command; apart from the necessary book keeping,
3254 these routines invoke device dependent code defined later.
3255
3256 @p @<Declare typesetting procedures@>
3257
3258 @ These typesetting routines communicate with the rest of the program
3259 through global variables.
3260
3261 @<Glob...@>=
3262 @!type_setting:boolean; {|true| while typesetting a page}
3263
3264 @ @<Set init...@>=
3265 type_setting:=false;
3266
3267 @ The user may select up to |max_select| ranges of consecutive pages to
3268 be processed. Each starting page specification is recorded in two global
3269 arrays called |start_count| and |start_there|. For example, `\.{1.*.-5}'
3270 is represented by |start_there[0]=true|, |start_count[0]=1|,
3271 |start_there[1]=false|, |start_there[2]=true|, |start_count[2]=-5|. We
3272 also set |start_vals=2|, to indicate that count 2 was the last one
3273 mentioned. The other values of |start_count| and |start_there| are not
3274 important, in this example. The number of pages is recorded in
3275 |max_pages|; a non positive value indicates that there is no limit.
3276
3277 @d start_count==select_count[cur_select] {count values to select
3278 starting page}
3279 @d start_there==select_there[cur_select] {is the |start_count| value
3280 relevant?}
3281 @d start_vals==select_vals[cur_select] {the last count considered
3282 significant}
3283 @d max_pages==select_max[cur_select] {at most this many |bop..eop| pages
3284 will be printed}
3285
3286 @<Glob...@>=
3287 @!select_count:array[0..max_select-1,0..9] of int_32;
3288 @!select_there:array[0..max_select-1,0..9] of boolean;
3289 @!select_vals:array[0..max_select-1] of 0..9;
3290 @!select_max:array[0..max_select-1] of int_32;
3291 @!out_mag:int_32; {output magnification}
3292 @!count:array[0..9] of int_32; {the count values on the current page}
3293 @!num_select:0..max_select; {number of page selection ranges specified}
3294 @!cur_select:0..max_select; {current page selection range}
3295 @!selected:boolean; {has starting page been found?}
3296 @!all_done:boolean; {have all selected pages been processed?}
3297 @!str_mag,@!str_select:pckt_pointer;
3298
3299 @ Here is a simple subroutine that tests if the current page might be the
3300 starting page.
3301
3302 @p function start_match:boolean; {does |count| match the starting spec?}
3303 var k:0..9; {loop index}
3304 @!match:boolean; {does everything match so far?}
3305 begin match:=true;
3306 for k:=0 to start_vals do
3307 if start_there[k]and(start_count[k]<>count[k]) then match:=false;
3308 start_match:=match;
3309 end;
3310
3311 @ @<Initialize options@>=
3312 out_mag:=0; cur_select:=0; max_pages:=0; selected:=true;
3313
3314 @ @<Print valid options@>=
3315 print_ln(' mag',sep_char,'<new_mag>');
3316 print_ln(' select',sep_char,'<start_count>',sep_char,
3317 '[<max_pages>] (up to ',max_select:1,' ranges)');
3318
3319 @ @<Action procedures for |dialog|@>=
3320 procedure scan_count; {scan a |start_count| value}
3321 begin if byte_mem[scan_ptr]=bi("*") then
3322 begin start_there[start_vals]:=false; incr(scan_ptr); scan_skip;
3323 end
3324 else begin start_there[start_vals]:=true;
3325 start_count[start_vals]:=scan_int;
3326 if cur_select=0 then selected:=false; {don't start at first page}
3327 end;
3328 end;
3329
3330 @ @<Cases for options@>=
3331 else if scan_keyword(str_mag,3) then out_mag:=scan_int
3332 else if scan_keyword(str_select,3) then
3333 if cur_select=max_select then print_ln('Too many page selections')
3334 else begin start_vals:=0; scan_count;
3335 while (start_vals<9)and(byte_mem[scan_ptr]=bi(".")) do
3336 begin incr(start_vals); incr(scan_ptr); scan_count;
3337 end;
3338 max_pages:=scan_int; incr(cur_select);
3339 end
3340
3341 @ @<Initialize predefined strings@>=
3342 id3("m")("a")("g")(str_mag);
3343 id6("s")("e")("l")("e")("c")("t")(str_select);
3344
3345 @ A stack is used to keep track of the current horizontal and vertical
3346 position, |h| and |v|, and the four registers |w|, |x|, |y|, and |z|;
3347 the register pairs |(w,x)| and |(y,z)| are maintained as arrays.
3348
3349 @<Types...@>=
3350 @!device @<Declare device dependent types@>@; @+ ecived @; @/
3351 @!stack_pointer=0..stack_size;@/
3352 @!stack_index=1..stack_size;@/
3353 @!pair_32=array[0..1] of int_32; {a pair of |int_32| variables}
3354 @!stack_record=record@;@/
3355 @!h_field:int_32; {horizontal position |h|}
3356 @!v_field:int_32; {vertical position |v|}
3357 @!w_x_field:pair_32; {|w| and |x| register for horizontal movements}
3358 @!y_z_field:pair_32; {|y| and |z| register for vertical movements}
3359 @!device @<Device dependent stack record fields@>@; @+ ecived @; @/
3360 end;
3361
3362 @ The current values are kept in |cur_stack|; they are pushed onto and
3363 popped from |stack|. We use \.{WEB} macros to access the current values.
3364
3365 @d cur_h==cur_stack.h_field {the current |@!h| value}
3366 @d cur_v==cur_stack.v_field {the current |@!v| value}
3367 @d cur_w_x==cur_stack.w_x_field {the current |@!w| and |@!x| value}
3368 @d cur_y_z==cur_stack.y_z_field {the current |@!y| and |@!z| value}
3369
3370 @<Glob...@>=
3371 @!stack:array[stack_index] of stack_record; {the pushed values}
3372 @!cur_stack:stack_record; {the current values}
3373 @!zero_stack:stack_record; {initial values}
3374 @!stack_ptr:stack_pointer; {last used position in |stack|}
3375
3376 @ @<Set init...@>=
3377 zero_stack.h_field:=0; zero_stack.v_field:=0;
3378 for i:=0 to 1 do
3379 begin zero_stack.w_x_field[i]:=0; zero_stack.y_z_field[i]:=0;
3380 end;
3381 @!device @<Initialize device dependent stack record fields@>@; @+ ecived @; @/
3382
3383 @ When typesetting for a real device we must convert the current
3384 position from \.{DVI} units to pixels, i.e., |cur_h| and |cur_v| into
3385 |cur_hh| and |cur_vv|. This might be a good place to collect everything
3386 related to the conversion from \.{DVI} units to pixels and in particular
3387 all the pixel rounding algorithms.
3388
3389 @d font_space(#)==fnt_space[#] {boundary between ``small'' and ``large''
3390 spaces}
3391
3392 @<Declare device dependent font data arrays@>=
3393 @!fnt_space:array [font_number] of int_32; {boundary between ``small''
3394 and ``large'' spaces}
3395
3396 @ @<Initialize device dependent font data@>=
3397 font_space(invalid_font):=0;
3398
3399 @ @<Initialize device dependent data for a font@>=
3400 font_space(cur_fnt):=font_scaled(cur_fnt) div 6;
3401 {this is a 3-unit ``thin space''}
3402
3403 @ The |char_pixels| array is used to store the horizontal character
3404 escapements: for \.{PK} or \.{GF} files we use the values given there,
3405 otherwise we must convert the character widths to (horizontal) pixels.
3406 The horizontal escapement of character~|c| in font~|f| is given by
3407 |font_pixel(f)(c)|.
3408
3409 @d font_pixel(#)==char_pixels[font_chars(#)+font_width_end
3410 @#
3411 @d max_pix_value==@"7FFF {largest allowed pixel value; this range may not
3412 suffice for high resolution output devices}
3413
3414 @<Declare device dependent types@>=
3415 @!pix_value=-max_pix_value..max_pix_value; {a pixel coordinate or displacement}
3416
3417 @ @<Glob...@>=
3418 @!device
3419 @!char_pixels:array[char_pointer] of pix_value; {character escapements}
3420 @!h_pixels:pix_value; {a horizontal dimension in pixels}
3421 @!v_pixels:pix_value; {a vertical dimension in pixels}
3422 @!temp_pix:pix_value; {temporary value for pixel rounding}
3423 ecived
3424
3425 @ @d cur_hh==cur_stack.hh_field {the current |@!hh| value}
3426 @d cur_vv==cur_stack.vv_field {the current |@!vv| value}
3427
3428 @<Device dependent stack record fields@>=
3429 @!hh_field:pix_value; {horizontal pixel position |hh|}
3430 @!vv_field:pix_value; {vertical pixel position |vv|}
3431
3432 @ @<Initialize device dependent stack record fields@>=
3433 zero_stack.hh_field:=0; zero_stack.vv_field:=0;
3434
3435 @ For small movements we round the increment in position, for large
3436 movements we round the incremented position. The same applies to rule
3437 dimensions with the only difference that they will always be rounded
3438 towards larger values. For characters we increment the horizontal
3439 position by the escapement values obtained, e.g., from a \.{PK} file or
3440 by the \.{TFM} width converted to pixels.
3441
3442 @d h_pixel_round(#)==round(h_conv*(#))
3443 @d v_pixel_round(#)==round(v_conv*(#))
3444 @^system dependencies@>
3445 @#
3446 @d large_h_space(#)==(#>=font_space(cur_fnt))or(#<=-4*font_space(cur_fnt))
3447 {is this a ``large'' horizontal distance?}
3448 @d large_v_space(#)==(abs(#)>=5*font_space(cur_fnt))
3449 {is this a ``large'' vertical distance?}
3450 @#
3451 @d h_rule_pixels== {converts the rule width |cur_h_dimen| to pixels}
3452 @!device if large_h_space(cur_h_dimen) then
3453 begin h_pixels:=h_pixel_round(cur_h+cur_h_dimen)-cur_hh;
3454 if h_pixels<=0 then if cur_h_dimen>0 then h_pixels:=1;
3455 end
3456 else begin h_pixels:=trunc(h_conv*cur_h_dimen);
3457 if h_pixels<h_conv*cur_h_dimen then incr(h_pixels);
3458 end;
3459 ecived
3460 @#
3461 @d v_rule_pixels== {converts the rule height |cur_v_dimen| to pixels}
3462 @!device if large_v_space(cur_v_dimen) then
3463 begin v_pixels:=cur_vv-v_pixel_round(cur_v-cur_v_dimen);
3464 if v_pixels<=0 then v_pixels:=1; {used only for |cur_v_dimen>0|}
3465 end
3466 else begin v_pixels:=trunc(v_conv*cur_v_dimen);
3467 if v_pixels<v_conv*cur_v_dimen then incr(v_pixels);
3468 end;
3469 ecived
3470
3471 @ A sequence of consecutive rules, or consecutive characters in a
3472 fixed-width font whose width is not an integer number of pixels, can
3473 cause |hh| to drift far away from a correctly rounded value. \.{\title}
3474 ensures that the amount of drift will never exceed |max_h_drift| pixels;
3475 similarly |vv| shall never drift away from the correctly rounded value
3476 by more than |max_v_drift| pixels.
3477
3478 @d h_upd_end(#)== {check for proper horizontal pixel rounding}
3479 begin Incr(cur_hh)(#); temp_pix:=h_pixel_round(cur_h);
3480 if abs(temp_pix-cur_hh)>max_h_drift then
3481 if temp_pix>cur_hh then cur_hh:=temp_pix-max_h_drift
3482 else cur_hh:=temp_pix+max_h_drift;
3483 end @+ ecived
3484 @d h_upd_char(#)==Incr(cur_h)(#)@;
3485 @!device; h_upd_end
3486 @d h_upd_move(#)==Incr(cur_h)(#)@;
3487 @!device; if large_h_space(#) then cur_hh:=h_pixel_round(cur_h)
3488 else h_upd_end
3489 @#
3490 @d v_upd_end(#)== {check for proper vertical pixel rounding}
3491 begin Incr(cur_vv)(#); temp_pix:=v_pixel_round(cur_v);
3492 if abs(temp_pix-cur_vv)>max_v_drift then
3493 if temp_pix>cur_vv then cur_vv:=temp_pix-max_v_drift
3494 else cur_vv:=temp_pix+max_v_drift;
3495 end @+ ecived
3496 @d v_upd_move(#)==Incr(cur_v)(#)@;
3497 @!device; if large_v_space(#) then cur_vv:=v_pixel_round(cur_v)
3498 else v_upd_end
3499
3500 @ The routines defined below use sections named `Declare local variables
3501 (if any) for \dots' or `Declare additional local variables for \dots';
3502 the former may declare variables (including the keyword \&{var}), whereas
3503 the later must at least contain the keyword \&{var}. In general, both may
3504 start with the declaration of labels, constants, and\slash or types.
3505
3506 Let us start with the simple cases:
3507 The |do_pre| procedure is called when the preamble has been read from
3508 the \.{DVI} file; the preamble comment has just been converted into a
3509 temporary packet with the |new_packet| procedure.
3510
3511 @p procedure do_pre;@/
3512 @<OUT: Declare local variables (if any) for |do_pre|@>@;
3513 begin all_done:=false; num_select:=cur_select; cur_select:=0;
3514 if num_select=0 then max_pages:=0;
3515 @!device
3516 h_conv:=(dvi_num/254000.0)*(h_resolution/dvi_den)*(out_mag/1000.0);
3517 v_conv:=(dvi_num/254000.0)*(v_resolution/dvi_den)*(out_mag/1000.0);
3518 ecived @; @/
3519 @<OUT: Process the |pre|@>@;@/
3520 end;
3521
3522 @ The |do_bop| procedure is called when a |bop| has been read. This
3523 routine determines whether a page shall be processed or skipped and sets
3524 the variable |type_setting| accordingly.
3525
3526 @p procedure do_bop;@/
3527 @<OUT: Declare additional local variables for |do_bop|@>@;
3528 @!i,@!j:0..9; {indices into |count|}
3529 begin @<Determine whether this page should be processed or skipped@>;
3530 print('DVI: ');
3531 if type_setting then print('process') @+ else print('skipp');
3532 print('ing page ',count[0]:1); j:=9;
3533 while (j>0)and(count[j]=0) do decr(j);
3534 for i:=1 to j do print('.',count[i]:1);
3535 d_print(' at ',dvi_loc-45:1);
3536 print_ln('.');
3537 if type_setting then
3538 begin stack_ptr:=0; cur_stack:=zero_stack; cur_fnt:=invalid_font;@/
3539 @<OUT: Process a |bop|@>@;@/
3540 end;
3541 end;
3542
3543 @ Note that the device dependent code `OUT: Process a |bop|' may choose
3544 to set |type_setting| to false even if |selected| is true.
3545
3546 @<Determine whether this page...@>=
3547 if not selected then selected:=start_match;
3548 type_setting:=selected
3549
3550 @ The |do_eop| procedure is called in order to process an |eop|; the
3551 stack should be empty.
3552
3553 @p procedure do_eop;@/
3554 @<OUT: Declare local variables (if any) for |do_eop|@>@;
3555 begin if stack_ptr<>0 then bad_dvi;
3556 @<OUT: Process an |eop|@>@;
3557 if max_pages>0 then
3558 begin decr(max_pages);
3559 if max_pages=0 then
3560 begin selected:=false; incr(cur_select);
3561 if cur_select=num_select then all_done:=true;
3562 end;
3563 end;
3564 type_setting:=false;
3565 end;
3566
3567 @ The procedures |do_push| and |do_pop| are called in order to process
3568 |push| and |pop| commands; |do_push| must check for stack overflow,
3569 |do_pop| should never be called when the stack is empty.
3570
3571 @p procedure do_push; {push onto stack}
3572 @<OUT: Declare local variables (if any) for |do_push|@>@;
3573 begin incr_stack(stack_ptr); stack[stack_ptr]:=cur_stack;@/
3574 @<OUT: Process a |push|@>@;
3575 end;
3576 @#
3577 procedure do_pop; {pop from stack}
3578 @<OUT: Declare local variables (if any) for |do_pop|@>@;
3579 begin if stack_ptr=0 then bad_dvi;
3580 cur_stack:=stack[stack_ptr]; decr(stack_ptr);
3581 @<OUT: Process a |pop|@>@;@/
3582 end;
3583
3584 @ The |do_xxx| procedure is called in order to process a special command.
3585 The bytes of the special string have been put into |byte_mem| as the
3586 current string. They are converted to a temporary packet and discarded
3587 again.
3588
3589 @p procedure do_xxx;@/
3590 @<OUT: Declare additional local variables for |do_xxx|@>@;
3591 @!p:pckt_pointer; {temporary packet}
3592 begin p:=new_packet;@/
3593 @<OUT: Process an |xxx|@>@;@/
3594 flush_packet;
3595 end;
3596
3597 @ Next are the movement commands:
3598 The |do_right| procedure is called in order to process the horizontal
3599 movement commands |right|, |w|, and |x|.
3600
3601
3602 @p procedure do_right;@/
3603 @<OUT: Declare local variables (if any) for |do_right|@>@;
3604 begin if cur_class>=w_cl then cur_w_x[cur_class-w_cl]:=cur_parm
3605 else if cur_class<right_cl then cur_parm:=cur_w_x[cur_class-w0_cl];
3606 @<OUT: Process a |right| or |w| or |x|@>@;@/
3607 h_upd_move(cur_parm)(h_pixel_round(cur_parm));
3608 @<OUT: Move right@>@;
3609 end;
3610
3611 @ The |do_down| procedure is called in order to process the vertical
3612 movement commands |down|, |y|, and |z|.
3613
3614 @p procedure do_down;@/
3615 @<OUT: Declare local variables (if any) for |do_down|@>@;
3616 begin if cur_class>=y_cl then cur_y_z[cur_class-y_cl]:=cur_parm
3617 else if cur_class<down_cl then cur_parm:=cur_y_z[cur_class-y0_cl];
3618 @<OUT: Process a |down| or |y| or |z|@>@;@/
3619 v_upd_move(cur_parm)(v_pixel_round(cur_parm));
3620 @<OUT: Move down@>@;
3621 end;
3622
3623 @ The |do_width| procedure, or actually the |do_a_width| macro, is
3624 called in order to increase the current horizontal position |cur_h| by
3625 |cur_h_dimen| in exactly the same way as if a character of width
3626 |cur_h_dimen| had been typeset.
3627
3628 @d do_a_width(#)==
3629 begin @!device h_pixels:=#; @+ ecived @; @+ do_width;
3630 end
3631
3632 @p procedure do_width;@/
3633 @<OUT: Declare local variables (if any) for |do_width|@>@;
3634 begin @<OUT: Typeset a |width|@>@;@/
3635 h_upd_char(cur_h_dimen)(h_pixels);
3636 @<OUT: Move right@>@;
3637 end;
3638
3639 @ Finally we have the commands for the typesetting of rules and characters;
3640 the global variable |cur_upd| is |true| if the horizontal position shall
3641 be updated (\\{set} commands).
3642
3643 The |do_rule| procedure is called in order to typeset a rule.
3644
3645 @p procedure do_rule;@/
3646 @<OUT: Declare additional local variables for |do_rule|@>@;
3647 @!visible:boolean;
3648 begin h_rule_pixels@;
3649 if (cur_h_dimen>0)and(cur_v_dimen>0) then
3650 begin visible:=true; v_rule_pixels@;
3651 @<OUT: Typeset a visible |rule|@>@;
3652 end
3653 else begin visible:=false;
3654 @<OUT: Typeset an invisible |rule|@>@;
3655 end;
3656 if cur_upd then
3657 begin h_upd_move(cur_h_dimen)(h_pixels);
3658 @<OUT: Move right@>@;
3659 end;
3660 end;
3661
3662 @ Last not least the |do_char| procedure is called in order to typeset
3663 character~|cur_res| with extension~|cur_ext| from the real font~|cur_fnt|.
3664
3665 @p procedure do_char;@/
3666 @<OUT: Declare local variables (if any) for |do_char|@>@;
3667 begin @<OUT: Typeset a \(|char|@>@;
3668 if cur_upd then
3669 begin h_upd_char(widths[cur_wp])(char_pixels[cur_cp]);
3670 @<OUT: Move right@>@;
3671 end;
3672 end;
3673
3674 @ If the program terminates abnormally, the following code may be
3675 invoked in the middle of a page.
3676
3677 @<Finish output file(s)@>=
3678 begin if type_setting then @<OUT: Finish incomplete page@>;
3679 @<OUT: Finish output file(s)@>@;
3680 end
3681
3682 @ When the first character of font~|cur_fnt| is about to be typeset, the
3683 |do_font| procedure is called in order to decide whether this is a
3684 virtual font or a real font.
3685
3686 One step in this decision is the attempt to find and read the \.{VF}
3687 file for this font; other attempts to locate a font file may be
3688 performed before and after that, depending on the nature of the output
3689 device and on the structure of the file system at a particular
3690 installation. For a real device we convert the character widths to
3691 (horizontal) pixels.
3692
3693 In any case |do_font| must change |font_type(cur_fnt)| to a value
3694 |>defined_font|; as a last resort one might use the \.{TFM} width data
3695 and draw boxes or leave blank spaces in the output.
3696
3697 @p procedure do_font;@/
3698 label done;@/
3699 @<OUT: Declare additional local variables for |do_font|@>@;
3700 @!p:char_pointer; {index into |char_widths| and |char_pixels|}
3701 begin @!debug if font_type(cur_fnt)=defined_font then confusion(str_fonts);
3702 gubed@;
3703 p:=0; {such that |p| is used}
3704 @!device for p:=font_chars(cur_fnt)+font_bc(cur_fnt)
3705 to font_chars(cur_fnt)+font_ec(cur_fnt) do
3706 char_pixels[p]:=h_pixel_round(widths[char_widths[p]]);
3707 ecived@;
3708 @<OUT: Look for a font file before trying to read the \.{VF} file;
3709 if found |goto done|@>@;@/
3710 if do_vf then goto done; {try to read the \.{VF} file}
3711 @<OUT: Look for a font file after trying to read the \.{VF} file@>@;@/
3712 done:
3713 @!debug if font_type(cur_fnt)<=loaded_font then confusion(str_fonts);
3714 gubed@;
3715 end;
3716
3717 @ Before a character of font~|cur_fnt| is typeset the following piece of
3718 code ensures that the font is ready to be used.
3719
3720 @<Prepare to use font |cur_fnt|@>=
3721 @<OUT: Prepare to use font |cur_fnt|@>@;
3722 if font_type(cur_fnt)<=loaded_font then do_font {|cur_fnt| was not yet used}
3723
3724 @* Interpreting VF packets.
3725 The |pckt_first_par| procedure first reads a \.{DVI} command byte from
3726 the packet into |cur_cmd|; then |cur_parm| is set to the value of the
3727 first parameter (if any) and |cur_class| to the command class.
3728
3729 @p procedure pckt_first_par;
3730 begin cur_cmd:=pckt_ubyte;
3731 case dvi_par[cur_cmd] of
3732 char_par: set_cur_char(pckt_ubyte);
3733 no_par: do_nothing;
3734 dim1_par: cur_parm:=pckt_sbyte;
3735 num1_par: cur_parm:=pckt_ubyte;
3736 dim2_par: cur_parm:=pckt_spair;
3737 num2_par: cur_parm:=pckt_upair;
3738 dim3_par: cur_parm:=pckt_strio;
3739 num3_par: cur_parm:=pckt_utrio;
3740 three_cases(dim4_par): cur_parm:=pckt_squad; {|dim4|, |num4|, or |numu|}
3741 rule_par:
3742 begin cur_v_dimen:=pckt_squad; cur_h_dimen:=pckt_squad;
3743 cur_upd:=(cur_cmd=set_rule);
3744 end;
3745 fnt_par:cur_parm:=cur_cmd-fnt_num_0;
3746 othercases abort('internal error');
3747 endcases;
3748 cur_class:=dvi_cl[cur_cmd];
3749 end;
3750
3751 @ The |do_vf_packet| procedure is called in order to interpret the
3752 character packet for a virtual character. Such a packet may contain the
3753 instruction to typeset a character from the same or an other virtual
3754 font; in such cases |do_vf_packet| calls itself recursively. The
3755 recursion level, i.e., the number of times this has happened, is kept
3756 in the global variable |n_recur| and should not exceed |max_recursion|.
3757 @^recursion@>
3758
3759 @<Types...@>=
3760 @!recur_pointer=0..max_recursion;
3761
3762 @ The \.{\title} processor should detect an infinite recursion caused by
3763 bad \.{VF} files; thus a new recursion level is entered even in cases
3764 where this could be avoided without difficulty.
3765
3766 If the recursion level exceeds the allowed maximum, we want to give
3767 a traceback how this has happened; thus some of the global variables
3768 used in different invocations of |do_vf_packet| are saved in a stack,
3769 others are saved as local variables of |do_vf_packet|.
3770
3771 @<Glob...@>=
3772 @!recur_fnt:array[recur_pointer] of font_number; {this packet's font}
3773 @!recur_ext:array[recur_pointer] of int_24; {this packet's extension}
3774 @!recur_res:array[recur_pointer] of eight_bits; {this packet's residue}
3775 @!recur_pckt:array[recur_pointer] of pckt_pointer; {the packet}
3776 @!recur_loc:array[recur_pointer] of byte_pointer; {next byte of packet}
3777 @!n_recur:recur_pointer; {current recursion level}
3778 @!recur_used:recur_pointer; {highest recursion level used so far}
3779
3780 @ @<Set init...@>=
3781 n_recur:=0; recur_used:=0;
3782
3783 @ Here now is the |do_vf_packet| procedure.
3784
3785 @p procedure do_vf_packet;
3786 label continue,found,done;
3787 var k:recur_pointer; {loop index}
3788 @!f:int_8u; {packet type flag}
3789 @!save_upd:boolean; {used to save |cur_upd|}
3790 @!save_cp:width_pointer; {used to save |cur_cp|}
3791 @!save_wp:width_pointer; {used to save |cur_wp|}
3792 @!save_limit:byte_pointer; {used to save |cur_limit|}
3793 begin @<VF: Save values on entry to |do_vf_packet|@>;@/
3794 @<VF: Interpret the \.{DVI} commands in the packet@>@;@/
3795 if save_upd then
3796 begin cur_h_dimen:=widths[save_wp]; do_a_width(char_pixels[save_cp]);
3797 end;
3798 @<VF: Restore values on exit from |do_vf_packet|@>;@/
3799 end;
3800
3801 @ On entry to |do_vf_packet| several values must be saved.
3802
3803 @<VF: Save values on entry to |do_vf_packet|@>=
3804 save_upd:=cur_upd; save_cp:=cur_cp; save_wp:=cur_wp;@/
3805 recur_fnt[n_recur]:=cur_fnt;
3806 recur_ext[n_recur]:=cur_ext;
3807 recur_res[n_recur]:=cur_res
3808
3809 @ Some of these values must be restored on exit from |do_vf_packet|.
3810
3811 @<VF: Restore values on exit from |do_vf_packet|@>=
3812 cur_fnt:=recur_fnt[n_recur]
3813
3814 @ If |cur_pckt| is the empty packet, we manufacture a |put| command;
3815 otherwise we read and interpret \.{DVI} commands from the packet.
3816
3817 @<VF: Interpret the \.{DVI} commands in the packet@>=
3818 if find_packet then f:=cur_type @+ else goto done;
3819 recur_pckt[n_recur]:=cur_pckt;
3820 save_limit:=cur_limit;
3821 cur_fnt:=font_font(cur_fnt);
3822 if cur_pckt=empty_packet then
3823 begin cur_class:=char_cl; goto found;
3824 end;
3825 if cur_loc>=cur_limit then goto done;
3826 continue: pckt_first_par;
3827 found: case cur_class of
3828 char_cl: @<VF: Typeset a |char|@>;
3829 rule_cl: do_rule;
3830 xxx_cl:
3831 begin pckt_room(cur_parm);
3832 while cur_parm>0 do
3833 begin append_byte(pckt_ubyte); decr(cur_parm);
3834 end;
3835 do_xxx;
3836 end;
3837 push_cl: do_push;
3838 pop_cl: do_pop;
3839 five_cases(w0_cl): do_right; {|right|, |w|, or |x|}
3840 five_cases(y0_cl): do_down; {|down|, |y|, or |z|}
3841 fnt_cl: cur_fnt:=cur_parm;
3842 othercases confusion(str_packets); {font definition or invalid}
3843 endcases;
3844 if cur_loc<cur_limit then goto continue;
3845 done:
3846
3847 @ The final |put| of a simple packet may be changed into |set_char| or
3848 \\{set}.
3849
3850 @<VF: Typeset a |char|@>=
3851 begin @<Prepare to use font |cur_fnt|@>;
3852 cur_cp:=font_chars(cur_fnt)+cur_res; cur_wp:=char_widths[cur_cp];
3853 if (cur_loc=cur_limit)and(f=vf_simple) and save_upd then
3854 begin save_upd:=false; cur_upd:=true;
3855 end;
3856 if font_type(cur_fnt)=vf_font_type then
3857 @<VF: Enter a new recursion level@>
3858 else do_char;
3859 end
3860
3861 @ Before entering a new recursion level we must test for overflow; in
3862 addition a few variables must be saved and restored.
3863 A |set_char| or \\{set} followed by |pop| is changed into |put|.
3864
3865 @<VF: Enter a new recursion level@>=
3866 begin recur_loc[n_recur]:=cur_loc; {save}
3867 if cur_loc<cur_limit then
3868 if byte_mem[cur_loc]=bi(pop) then cur_upd:=false;
3869 if n_recur=recur_used then
3870 if recur_used=max_recursion then
3871 @<VF: Display the recursion traceback and terminate@>
3872 else incr(recur_used);@/
3873 incr(n_recur); do_vf_packet; decr(n_recur); {recurse}
3874 cur_loc:=recur_loc[n_recur]; cur_limit:=save_limit; {restore}
3875 end
3876
3877 @ @<VF: Display the recursion traceback and terminate@>=
3878 begin print_ln(' !Infinite VF recursion?');
3879 @.Infinite VF recursion?@>
3880 for k:=max_recursion downto 0 do
3881 begin print('level=',k:1,' font');
3882 d_print('=',recur_fnt[k]:1);
3883 print_font(recur_fnt[k]);
3884 print(' char=',recur_res[k]:1);
3885 if recur_ext[k]<>0 then print('.',recur_ext[k]:1);
3886 new_line;
3887 @!debug hex_packet(recur_pckt[k]); print_ln('loc=',recur_loc[k]:1);
3888 gubed@;
3889 end;
3890 overflow(str_recursion,max_recursion);
3891 end
3892
3893 @* Interpreting the DVI file.
3894 The |do_dvi| procedure reads the entire \.{DVI} file and initiates
3895 whatever actions may be necessary.
3896
3897 @p procedure do_dvi;
3898 label done,exit;
3899 var temp_byte:int_8u; {byte for temporary variables}
3900 @!temp_int:int_32; {integer for temporary variables}
3901 @!dvi_start:int_32; {starting location}
3902 @!dvi_bop_post:int_32; {location of |bop| or |post|}
3903 @!dvi_back:int_32; {a back pointer}
3904 @!k:int_15; {general purpose variable}
3905 begin @<DVI: Process the preamble@>;
3906 if random_reading then @<DVI: Process the postamble@>;
3907 repeat dvi_first_par;
3908 while cur_class=fnt_def_cl do
3909 begin dvi_do_font(random_reading); dvi_first_par;
3910 end;
3911 if cur_cmd=bop then @<DVI: Process one page@>;
3912 until cur_cmd<>eop;
3913 if cur_cmd<>post then bad_dvi;
3914 exit:end;
3915
3916 @ @<DVI: Process the preamble@>=
3917 if dvi_ubyte<>pre then bad_dvi;
3918 if dvi_ubyte<>dvi_id then bad_dvi;
3919 dvi_num:=dvi_pquad; dvi_den:=dvi_pquad; dvi_mag:=dvi_pquad;
3920 tfm_conv:=(25400000.0/dvi_num)*(dvi_den/473628672)/16.0;
3921 temp_byte:=dvi_ubyte; pckt_room(temp_byte);
3922 for k:=1 to temp_byte do append_byte(dvi_ubyte);
3923 print('DVI file: '''); print_packet(new_packet); print_ln(''',');
3924 print(' num=',dvi_num:1,', den=',dvi_den:1,', mag=',dvi_mag:1);
3925 if out_mag<=0 then out_mag:=dvi_mag @+ else print(' => ',out_mag:1);
3926 print_ln('.');
3927 do_pre; flush_packet
3928
3929 @ @<Glob...@>=
3930 @!dvi_num:int_31; {numerator}
3931 @!dvi_den:int_31; {denominator}
3932 @!dvi_mag:int_31; {magnification}
3933
3934 @ @<DVI: Process the postamble@>=
3935 begin dvi_start:=dvi_loc; {remember start of first page}
3936 @<DVI: Find the postamble@>;
3937 d_print_ln('DVI: postamble at ',dvi_bop_post:1);
3938 dvi_back:=dvi_pointer;
3939 if dvi_num<>dvi_pquad then bad_dvi;
3940 if dvi_den<>dvi_pquad then bad_dvi;
3941 if dvi_mag<>dvi_pquad then bad_dvi;
3942 temp_int:=dvi_squad; temp_int:=dvi_squad;
3943 if stack_size<dvi_upair then overflow(str_stack,stack_size);
3944 temp_int:=dvi_upair;
3945 dvi_first_par;
3946 while cur_class=fnt_def_cl do
3947 begin dvi_do_font(false); dvi_first_par;
3948 end;
3949 if cur_cmd<>post_post then bad_dvi;
3950 if not selected then @<DVI: Find the starting page@>;
3951 dvi_move(dvi_start); {go to first or starting page}
3952 end
3953
3954 @ @<DVI: Find the postamble@>=
3955 temp_int:=dvi_length-5;
3956 repeat if temp_int<49 then bad_dvi;
3957 dvi_move(temp_int); temp_byte:=dvi_ubyte; decr(temp_int);
3958 until temp_byte<>dvi_pad;
3959 if temp_byte<>dvi_id then bad_dvi;
3960 dvi_move(temp_int-4); if dvi_ubyte<>post_post then bad_dvi;
3961 dvi_bop_post:=dvi_pointer;
3962 if (dvi_bop_post<15)or(dvi_bop_post>dvi_loc-34) then bad_dvi;
3963 dvi_move(dvi_bop_post); if dvi_ubyte<>post then bad_dvi
3964
3965 @ @<DVI: Find the starting page@>=
3966 begin dvi_start:=dvi_bop_post; {just in case}
3967 while dvi_back<>-1 do
3968 begin if (dvi_back<15)or(dvi_back>dvi_bop_post-46) then bad_dvi;
3969 dvi_bop_post:=dvi_back; dvi_move(dvi_back);
3970 if dvi_ubyte<>bop then bad_dvi;
3971 for k:=0 to 9 do count[k]:=dvi_squad;
3972 if start_match then dvi_start:=dvi_bop_post;
3973 dvi_back:=dvi_pointer;
3974 end;
3975 end
3976
3977 @ When a |bop| has been read, the \.{DVI} commands for one page are
3978 interpreted until an |eop| is found.
3979
3980 @<DVI: Process one page@>=
3981 begin for k:=0 to 9 do count[k]:=dvi_squad;
3982 temp_int:=dvi_pointer; do_bop;
3983 dvi_first_par;
3984 if type_setting then @<DVI: Process a page; then |goto done|@>
3985 else @<DVI: Skip a page; then |goto done|@>;
3986 done:if cur_cmd<>eop then bad_dvi;
3987 if selected then
3988 begin do_eop;
3989 if all_done then return;
3990 end;
3991 end
3992
3993 @ All \.{DVI} commands are processed, as long as |cur_class<>invalid_cl|;
3994 then we should have found an |eop|.
3995
3996 @<DVI: Process a page; then |goto done|@>=
3997 loop begin
3998 case cur_class of
3999 char_cl: @<DVI: Typeset a |char|@>;
4000 rule_cl:
4001 if cur_upd and(cur_v_dimen=width_dimen) then
4002 do_a_width(h_pixel_round(cur_h_dimen))
4003 else do_rule;
4004 xxx_cl:
4005 begin pckt_room(cur_parm);
4006 while cur_parm>0 do
4007 begin append_byte(dvi_ubyte); decr(cur_parm);
4008 end;
4009 do_xxx;
4010 end;
4011 push_cl: do_push;
4012 pop_cl: do_pop;
4013 five_cases(w0_cl): do_right; {|right|, |w|, or |x|}
4014 five_cases(y0_cl): do_down; {|down|, |y|, or |z|}
4015 fnt_cl: dvi_font;
4016 fnt_def_cl: dvi_do_font(random_reading);
4017 invalid_cl: goto done;
4018 othercases abort('internal error');
4019 endcases;
4020 dvi_first_par; {get the next command}
4021 end
4022
4023 @ While skipping a page all commands other than font definitions are
4024 ignored.
4025
4026 @<DVI: Skip a page; then |goto done|@>=
4027 loop begin
4028 case cur_class of
4029 xxx_cl: while cur_parm>0 do
4030 begin temp_byte:=dvi_ubyte; decr(cur_parm);
4031 end;
4032 fnt_def_cl: dvi_do_font(random_reading);
4033 invalid_cl: goto done;
4034 othercases do_nothing;
4035 endcases;
4036 dvi_first_par; {get the next command}
4037 end
4038
4039 @ @<DVI: Typeset a |char|@>=
4040 begin @<Prepare to use font |cur_fnt|@>;
4041 set_cur_wp(cur_fnt)(bad_dvi);
4042 if font_type(cur_fnt)=vf_font_type then do_vf_packet @+ else do_char;
4043 end
4044
4045 @* The main program.
4046 The code for real devices is still rather incomplete.
4047 Moreover several branches of the program have not been tested because
4048 they are never used with \.{DVI} files made by \TeX\ and \.{VF} files
4049 made by \.{VPtoVF}.
4050
4051 @ At the end of the program the output file(s) have to be finished and
4052 on some systems it may be necessary to close input and\slash or output
4053 files.
4054 @^system dependencies@>
4055
4056 @p procedure close_files_and_terminate;
4057 var k:@!int_15; {general purpose index}
4058 begin close_in(dvi_file);
4059 if history<fatal_message then @<Finish output file(s)@>;
4060 stat @<Print memory usage statistics@>;@+tats@;@/
4061 @<Close output file(s)@>@;
4062 @<Print the job |history|@>;
4063 end;
4064
4065 @ Now we are ready to put it all together.
4066 Here is where \.{\title} starts, and where it ends.
4067 @^system dependencies@>
4068
4069 @p begin initialize; {get all variables initialized}
4070 @<Initialize predefined strings@>@;
4071 dialog; {get options}
4072 @<Open input file(s)@>@;
4073 @<Open output file(s)@>@;
4074 do_dvi; {process the entire \.{DVI} file}
4075 close_files_and_terminate;
4076 final_end:end.
4077
4078 @ @<Print memory usage statistics@>=
4079 print_ln('Memory usage statistics:');
4080 print(dvi_nf:1,' dvi, ',lcl_nf:1,' local, ');
4081 @<Print more font usage statistics@>@;@/
4082 print_ln('and ',nf:1,' internal fonts of ',max_fonts:1);
4083 print_ln(n_widths:1,' widths of ',max_widths:1,' for ',
4084 n_chars:1,' characters of ',max_chars:1);
4085 print_ln(pckt_ptr:1,' byte packets of ',max_packets:1,' with ',
4086 byte_ptr:1,' bytes of ',max_bytes:1);
4087 @<Print more memory usage statistics@>@;@/
4088 print_ln(stack_used:1,' of ',stack_size:1,' stack and ',
4089 recur_used:1,' of ',max_recursion:1,' recursion levels.')
4090
4091 @ Some implementations may wish to pass the |history| value to the
4092 operating system so that it can be used to govern whether or not other
4093 programs are started. Here we simply report the history to the user.
4094 @^system dependencies@>
4095
4096 @<Print the job |history|@>=
4097 case history of
4098 spotless: print_ln('(No errors were found.)');
4099 harmless_message: print_ln('(Did you see the warning message above?)');
4100 error_message: print_ln('(Pardon me, but I think I spotted something wrong.)');
4101 fatal_message: print_ln('(That was a fatal error, my friend.)');
4102 end {there are no other cases}
4103
4104 @* Low-level output routines.
4105 The program uses the binary file variable |out_file| for its main output
4106 file; |out_loc| is the number of the byte about to be written next on
4107 |out_file|.
4108
4109 @<Glob...@>=
4110 @!out_file:byte_file; {the \.{DVI} file we are writing}
4111 @!out_loc:int_32; {where we are about to write, in |out_file|}
4112 @!out_back:int_32; {a back pointer}
4113 @!out_max_v:int_31; {maximum |v| value so far}
4114 @!out_max_h:int_31; {maximum |h| value so far}
4115 @!out_stack:int_16u; {maximum stack depth}
4116 @!out_pages:int_16u; {total number of pages}
4117
4118 @ @<Set ini...@>=
4119 out_loc:=0; out_back:=-1;
4120 out_max_v:=0; out_max_h:=0;
4121 out_stack:=0; out_pages:=0;
4122
4123 @ To prepare |out_file| for output, we |rewrite| it.
4124
4125 @<Open output file(s)@>=
4126 rewrite(out_file); {prepares to write packed bytes to |out_file|}
4127
4128 @ For some operating systems it may be necessary to close |out_file|.
4129
4130 @<Close output file(s)@>=
4131
4132 @ Writing the |out_file| should be done as efficient as possible for a
4133 particular system; on many systems this means that a large number of
4134 bytes will be accumulated in a buffer and is then written from that
4135 buffer to |out_file|. In order to simplify such system dependent changes
4136 we use the \.{WEB} macro |out_byte| to write the next \.{DVI} byte. Here
4137 we give a simple minded definition for this macro in terms of standard
4138 \PASCAL.
4139 @^system dependencies@>
4140 @^optimization@>
4141
4142 @d out_byte(#) == write(out_file,#) {write next \.{DVI} byte}
4143
4144 @ The \.{WEB} macro |out_one| is used to write one byte and to update
4145 |out_loc|.
4146
4147 @d out_one(#) == begin out_byte(#); incr(out_loc); @+ end
4148
4149 @ First the |out_packet| procedure copies a packet to |out_file|.
4150
4151 @<Declare typesetting procedures@>=
4152 procedure out_packet(@!p:pckt_pointer);
4153 var k:byte_pointer; {index into |byte_mem|}
4154 begin Incr(out_loc)(pckt_length(p));
4155 for k:=pckt_start[p] to pckt_start[p+1]-1 do out_byte(bo(byte_mem[k]));
4156 end;
4157
4158 @ Next are the procedures used to write integer numbers or even complete
4159 \.{DVI} commands to |out_file|; they all keep |out_loc| up to date.
4160
4161 The |out_four| procedure outputs four bytes in two's complement notation,
4162 without risking arithmetic overflow.
4163
4164 @<Declare typesetting procedures@>=
4165 procedure out_four(@!x:int_32); {output four bytes}
4166 @!begin_four; comp_four(out_byte); Incr(out_loc)(4);
4167 end;
4168
4169 @ The |out_char| procedure outputs a |set_char| or \\{set} command or, if
4170 |upd=false|, a |put| command.
4171
4172 @<Declare typesetting procedures@>=
4173 procedure out_char(@!upd:boolean;@!ext:int_32;@!res:eight_bits);
4174 {output \\{set} or |put|}
4175 @!begin_char; comp_char(out_one);
4176 end;
4177
4178 @ The |out_unsigned| procedure outputs a |fnt|, |xxx|, or |fnt_def|
4179 command with its first parameter (normally unsigned); a |fnt| command
4180 is converted into |fnt_num| whenever this is possible.
4181
4182 @<Declare typesetting procedures@>=
4183 procedure out_unsigned(@!o:eight_bits;@!x:int_32);
4184 {output |fnt_num|, |fnt|, |xxx|, or |fnt_def|}
4185 @!begin_unsigned; comp_unsigned(out_one);
4186 end;
4187
4188 @ The |out_signed| procedure outputs a movement (|right|, |w|,
4189 |x|, |down|, |y|, or |z|) command with its (signed) parameter.
4190
4191 @<Declare typesetting procedures@>=
4192 procedure out_signed(@!o:eight_bits;@!x:int_32);
4193 {output |right|, |w|, |x|, |down|, |y|, or |z|}
4194 @!begin_signed; comp_signed(out_one);
4195 end;
4196
4197 @ For an output font we set |font_type(f):=out_font_type|; in this case
4198 |font_font(f)| is the font number used for font~|f| in |out_file|.
4199 @^font types@>
4200
4201 The global variable |out_nf| is the number of fonts already used in
4202 |out_file| and the array |out_fnts| contains their internal font numbers;
4203 the current font in |out_file| is called |out_fnt|.
4204
4205 @<Glob...@>=
4206 @!out_fnts:array[font_number] of font_number; {internal font numbers}
4207 @!out_nf:font_number; {number of fonts used in |out_file|}
4208 @!out_fnt:font_number; {internal font number of current output font}
4209
4210 @ @<Set init...@>=
4211 out_nf:=0;
4212
4213 @ @<Print more font usage statistics@>=
4214 print(out_nf:1,' out, ');
4215
4216 @ The |out_fnt_def| procedure outputs a complete font definition
4217 command.
4218
4219 @<Declare typesetting procedures@>=
4220 procedure out_fnt_def(@!f:font_number);
4221 var p:pckt_pointer; {the font name packet}
4222 @!k,@!l:byte_pointer; {indices into |byte_mem|}
4223 @!a:eight_bits; {length of area part}
4224 begin out_unsigned(fnt_def1,font_font(f)); out_four(font_check(f));
4225 out_four(font_scaled(f)); out_four(font_design(f));@/
4226 p:=font_name(f); k:=pckt_start[p]; l:=pckt_start[p+1]-1;
4227 a:=bo(byte_mem[k]);@/
4228 Incr(out_loc)(l-k+2); out_byte(a); out_byte(l-k-a);
4229 while k<l do
4230 begin incr(k); out_byte(bo(byte_mem[k]));
4231 end;
4232 end;
4233
4234 @* Writing the output file.
4235 Here we define the device dependent parts of the typesetting routines
4236 described earlier in this program.
4237
4238 First we define a few quantities required by the device dependent code
4239 for a real output device in order to demonstrate how they might be
4240 defined and in order to be able to compile \.{DVIcopy} with the device
4241 dependent code included.
4242
4243 @d h_resolution==300 {horizontal resolution in pixels per inch (dpi)}
4244 @d v_resolution==300 {vertical resolution in pixels per inch (dpi)}
4245
4246 @d max_h_drift==2 {we insist that |abs(hh-h_pixel_round(h))<=max_h_drift|}
4247 @d max_v_drift==2 {we insist that |abs(vv-v_pixel_round(v))<=max_v_drift|}
4248
4249 @<Glob...@>=
4250 @!device
4251 @!h_conv:real; {converts \.{DVI} units to horizontal pixels}
4252 @!v_conv:real; {converts \.{DVI} units to vertical pixels}
4253 ecived
4254
4255 @ These are the local variables (if any) needed for |do_pre|.
4256
4257 @<OUT: Declare local variables (if any) for |do_pre|@>=
4258 var k:int_15; {general purpose variable}
4259 @!p,@!q,@!r:byte_pointer; {indices into |byte_mem|}
4260 @!comment:packed array[1..comm_length] of char; {preamble comment prefix}
4261
4262 @ And here is the device dependent code for |do_pre|; the \.{DVI} preamble
4263 comment written to |out_file| is similar to the one produced by \.{GFtoPK},
4264 but we want to apply our preamble comment prefix only once.
4265
4266 @<OUT: Process the |pre|@>=
4267 out_one(pre); out_one(dvi_id);
4268 out_four(dvi_num); out_four(dvi_den); out_four(out_mag);@/
4269 p:=pckt_start[pckt_ptr-1]; q:=byte_ptr; {location of old \.{DVI} comment}
4270 comment:=preamble_comment; pckt_room(comm_length);
4271 for k:=1 to comm_length do append_byte(xord[comment[k]]);
4272 while byte_mem[p]=bi(" ") do incr(p); {remove leading blanks}
4273 if p=q then Decr(byte_ptr)(from_length)
4274 else begin k:=0;
4275 while (k<comm_length)and(byte_mem[p+k]=byte_mem[q+k]) do incr(k);
4276 if k=comm_length then Incr(p)(comm_length);
4277 end;
4278 k:=byte_ptr-p; {total length}
4279 if k>255 then
4280 begin k:=255; q:=p+255-comm_length; {at most 255 bytes}
4281 end;
4282 out_one(k); out_packet(new_packet); flush_packet;
4283 for r:=p to q-1 do out_one(bo(byte_mem[r]));
4284
4285 @ These are the additional local variables (if any) needed for |do_bop|;
4286 the variables |@!i| and |@!j| are already declared.
4287
4288 @<OUT: Declare additional local variables for |do_bop|@>=
4289 var
4290
4291 @ And here is the device dependent code for |do_bop|.
4292
4293 @<OUT: Process a |bop|@>=
4294 out_one(bop); incr(out_pages);
4295 for i:=0 to 9 do out_four(count[i]);
4296 out_four(out_back); out_back:=out_loc-45;
4297 out_fnt:=invalid_font;
4298
4299 @ These are the local variables (if any) needed for |do_eop|.
4300
4301 @<OUT: Declare local variables (if any) for |do_eop|@>=
4302
4303 @ And here is the device dependent code for |do_eop|.
4304
4305 @<OUT: Process an |eop|@>=
4306 out_one(eop);
4307
4308 @ These are the local variables (if any) needed for |do_push|.
4309
4310 @<OUT: Declare local variables (if any) for |do_push|@>=
4311
4312 @ And here is the device dependent code for |do_push|.
4313
4314 @<OUT: Process a |push|@>=
4315 if stack_ptr>out_stack then out_stack:=stack_ptr;
4316 out_one(push);
4317
4318 @ These are the local variables (if any) needed for |do_pop|.
4319
4320 @<OUT: Declare local variables (if any) for |do_pop|@>=
4321
4322 @ And here is the device dependent code for |do_pop|.
4323
4324 @<OUT: Process a |pop|@>=
4325 out_one(pop);
4326
4327 @ These are the additional local variables (if any) needed for |do_xxx|;
4328 the variable |@!p|, the pointer to the packet containing the special
4329 string, is already declared.
4330
4331 @<OUT: Declare additional local variables for |do_xxx|@>=
4332 var
4333
4334 @ And here is the device dependent code for |do_xxx|.
4335
4336 @<OUT: Process an |xxx|@>=
4337 out_unsigned(xxx1,pckt_length(p)); out_packet(p);
4338
4339 @ These are the local variables (if any) needed for |do_right|.
4340
4341 @<OUT: Declare local variables (if any) for |do_right|@>=
4342
4343 @ And here is the device dependent code for |do_right|.
4344
4345 @<OUT: Process a |right| or |w| or |x|@>=
4346 if cur_class<right_cl then out_one(cur_cmd) {|w0| or |x0|}
4347 else out_signed(dvi_right_cmd[cur_class],cur_parm); {|right|, |w|, or |x|}
4348
4349 @ Here we update the |out_max_h| value.
4350
4351 @<OUT: Move right@>=
4352 if abs(cur_h)>out_max_h then out_max_h:=abs(cur_h);
4353
4354 @ These are the local variables (if any) needed for |do_down|.
4355
4356 @<OUT: Declare local variables (if any) for |do_down|@>=
4357
4358 @ And here is the device dependent code for |do_down|.
4359
4360 @<OUT: Process a |down| or |y| or |z|@>=
4361 if cur_class<down_cl then out_one(cur_cmd) {|y0| or |z0|}
4362 else out_signed(dvi_down_cmd[cur_class],cur_parm); {|down|, |y|, or |z|}
4363
4364 @ Here we update the |out_max_v| value.
4365
4366 @<OUT: Move down@>=
4367 if abs(cur_v)>out_max_v then out_max_v:=abs(cur_v);
4368
4369 @ These are the local variables (if any) needed for |do_width|.
4370
4371 @<OUT: Declare local variables (if any) for |do_width|@>=
4372
4373 @ And here is the device dependent code for |do_width|.
4374
4375 @<OUT: Typeset a |width|@>=
4376 out_one(set_rule);
4377 out_four(width_dimen); out_four(cur_h_dimen);
4378
4379 @ These are the additional local variables (if any) needed for |do_rule|;
4380 the variable |@!visible| is already declared.
4381
4382 @<OUT: Declare additional local variables for |do_rule|@>=
4383 var
4384
4385 @ And here is the device dependent code for |do_rule|.
4386
4387 @<OUT: Typeset a visible |rule|@>=
4388 out_one(dvi_rule_cmd[cur_upd]);
4389 out_four(cur_v_dimen); out_four(cur_h_dimen);
4390
4391 @ @<OUT: Typeset an invisible |rule|@>=
4392 @<OUT: Typeset a visible |rule|@>
4393
4394 @ These are the additional local variables (if any) needed for |do_font|;
4395 the variable |@!p| is already declared.
4396
4397 @<OUT: Declare additional local variables for |do_font|@>=
4398 var
4399
4400 @ And here is the device dependent code for |do_font|; if the \.{VF} file
4401 for a font could not be found, we simply assume this must be a real font.
4402
4403 @<OUT: Look for a font file before trying to read the \.{VF} file;
4404 if found |goto done|@>=
4405
4406 @ @<OUT: Look for a font file after trying to read the \.{VF} file@>=
4407 if(out_nf>=max_fonts) then overflow(str_fonts,max_fonts);
4408 print('OUT: font ',cur_fnt:1); d_print(' => ',out_nf:1);
4409 print_font(cur_fnt);
4410 d_print(' at ',font_scaled(cur_fnt):1,' DVI units'); print_ln('.');
4411 font_type(cur_fnt):=out_font_type; font_font(cur_fnt):=out_nf;
4412 out_fnts[out_nf]:=cur_fnt; incr(out_nf);
4413 out_fnt_def(cur_fnt);
4414
4415 @ And here is some device dependent code used before each character.
4416
4417 @<OUT: Prepare to use font |cur_fnt|@>=
4418
4419 @ These are the local variables (if any) needed for |do_char|.
4420
4421 @<OUT: Declare local variables (if any) for |do_char|@>=
4422
4423 @ And here is the device dependent code for |do_char|.
4424
4425 @<OUT: Typeset a \(|char|@>=
4426 @!debug if font_type(cur_fnt)<>out_font_type then confusion(str_fonts);
4427 gubed @;
4428 if cur_fnt<>out_fnt then
4429 begin out_unsigned(fnt1,font_font(cur_fnt)); out_fnt:=cur_fnt;
4430 end;
4431 out_char(cur_upd,cur_ext,cur_res);
4432
4433 @ If the program terminates in the middle of a page, we write as many
4434 |pop|s as necessary and one |eop|.
4435
4436 @<OUT: Finish incomplete page@>=
4437 begin while stack_ptr>0 do
4438 begin out_one(pop); decr(stack_ptr);
4439 end;
4440 out_one(eop);
4441 end
4442
4443 @ If the output file has been started, we write the postamble; in
4444 addition we print the number of bytes and pages written to |out_file|.
4445
4446 @<OUT: Finish output file(s)@>=
4447 if out_loc>0 then
4448 begin @<OUT: Write the postamble@>;
4449 k:=7-((out_loc-1) mod 4); {the number of |dvi_pad| bytes}
4450 while k>0 do
4451 begin out_one(dvi_pad); decr(k);
4452 end;
4453 print('OUT file: ',out_loc:1,' bytes, ',out_pages:1,' page');
4454 if out_pages<>1 then print('s');
4455 end
4456 else print('OUT file: no output');
4457 print_ln(' written.');
4458 if out_pages=0 then mark_harmless;
4459
4460 @ Here we simply write the values accumulated during the \.{DVI} output.
4461
4462 @<OUT: Write the postamble@>=
4463 out_one(post); out_four(out_back); out_back:=out_loc-5;@/
4464 out_four(dvi_num); out_four(dvi_den); out_four(out_mag);@/
4465 out_four(out_max_v); out_four(out_max_h);@/
4466 out_one(out_stack div @"100); out_one(out_stack mod @"100);@/
4467 out_one(out_pages div @"100); out_one(out_pages mod @"100);@/
4468 k:=out_nf;
4469 while k>0 do
4470 begin decr(k); out_fnt_def(out_fnts[k]);
4471 end;
4472 out_one(post_post); out_four(out_back);@/
4473 out_one(dvi_id)
4474
4475 @ Here we could print more memory usage statistics; this possibility is,
4476 however, not used for \.{DVIcopy}.
4477
4478 @<Print more memory usage statistics@>=
4479
4480 @* System-dependent changes.
4481 This section should be replaced, if necessary, by changes to the program
4482 that are necessary to make \.{DVIcopy} work at a particular installation.
4483 It is usually best to design your change file so that all changes to
4484 previous sections preserve the section numbering; then everybody's version
4485 will be consistent with the printed program. More extensive changes,
4486 which introduce new sections, can be inserted here; then only the index
4487 itself will get a new section number.
4488 @^system dependencies@>
4489
4490 @* Index.
4491 Pointers to error messages appear here together with the section numbers
4492 where each ident\-i\-fier is used.