Debian Codegraph @

texk/web2c/omegaware/odvitype.web

raw
1 % odvitype.web: Omega version of file dvitype.web
2 %
3 % This file is part of the Omega project, which
4 % is based in the web2c distribution of TeX.
5 %
6 % Copyright (c) 1994--1998 John Plaice and Yannis Haralambous
7 % applies only to the changes to the original dvitype.web.
8 %
9 % This program by D. E. Knuth is not copyrighted and can be used freely.
10 % Version 1 was completed in September, 1982.
11 % Slight changes were made in October, 1982, for version 0.7 of TeX.
12 % Version 1.1 corrected minor bugs (May, 1983).
13 % Version 2 was released with version 0.999 of TeX (July, 1983).
14 % Version 2.1 corrected a bug when no fonts are present (September, 1983).
15 % Version 2.2 corrected bugs in max_*_so_far and put1 (October, 1983).
16 % Version 2.3 corrected spacing of accents (March, 1984).
17 % Version 2.4 fixed rounding, changed oriental font conventions (April, 1984).
18 % Version 2.5 improved the case of zero pages (May, 1984).
19 % Version 2.6 introduced max_drift (June, 1984).
20 % Version 2.7 had minor editorial changes (August, 1984).
21 % Version 2.8 made default resolution 300/1 (April, 1985).
22 % Version 2.9 changed negative scaling as in TeX version 2.7 (November, 1987).
23 % Version 3 introduced an intermediate level of verbosity (October,1989).
24 % Version 3.1 gave magnification info in final font list (November, 1989).
25 % Version 3.2 doublechecked design size of each font (January, 1990).
26 % Version 3.3 had more robust for loops in print_font, define_font (May, 1990).
27 % Version 3.4 more robustness in presence of bad fonts (September, 1990).
28 % Version 3.5 checked skipped material more strictly (March, 1995).
29
30 % Here is TeX material that gets inserted after \input webmac
31 \def\hang{\hangindent 3em\indent\ignorespaces}
32 \font\ninerm=cmr9
33 \let\mc=\ninerm % medium caps for names like SAIL
34 \def\PASCAL{Pascal}
35
36 \def\(#1){} % 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{ODVI$\,$\lowercase{type}}
40 \def\contentspagenumber{401}
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 ODVItype} processor}
46 \vskip 15pt
47 \centerline{(Version 1.5, March 1997)}
48 \vfill}
49 \def\botofcontents{\vfill
50 \centerline{\hsize 5in\baselineskip9pt
51 \vbox{\ninerm\noindent
52 The preparation of this report
53 was supported in part by the National Science
54 Foundation under grants IST-8201926 and MCS-8300984,
55 and by the System Development Foundation. `\TeX' is a
56 trademark of the American Mathematical Society.}}}
57 \pageno=\contentspagenumber \advance\pageno by 1
58
59 @* Introduction.
60 The \.{DVItype} utility program reads binary device-independent (``\.{DVI}'')
61 files that are produced by document compilers such as \TeX, and converts them
62 into symbolic form. This program has two chief purposes: (1)~It can be used to
63 determine whether a \.{DVI} file is valid or invalid, when diagnosing
64 compiler errors; and (2)~it serves as an example of a program that reads
65 \.{DVI} files correctly, for system programmers who are developing
66 \.{DVI}-related software.
67
68 Goal number (2) needs perhaps a bit more explanation. Programs for
69 typesetting need to be especially careful about how they do arithmetic; if
70 rounding errors accumulate, margins won't be straight, vertical rules
71 won't line up, and so on. But if rounding is done everywhere, even in the
72 midst of words, there will be uneven spacing between the letters, and that
73 looks bad. Human eyes notice differences of a thousandth of an inch in the
74 positioning of lines that are close together; on low resolution devices,
75 where rounding produces effects four times as great as this, the problem
76 is especially critical. Experience has shown that unusual care is needed
77 even on high-resolution equipment; for example, a mistake in the sixth
78 significant hexadecimal place of a constant once led to a difficult-to-find
79 bug in some software for the Alphatype CRS, which has a resolution of 5333
80 pixels per inch (make that 5333.33333333 pixels per inch). The document
81 compilers that generate \.{DVI} files make certain assumptions about the
82 arithmetic that will be used by \.{DVI}-reading software, and if these
83 assumptions are violated the results will be of inferior quality.
84 Therefore the present program is intended as a guide to proper procedure
85 in the critical places where a bit of subtlety is involved.
86
87 The first \.{DVItype} program was designed by David Fuchs in 1979, and it
88 @^Fuchs, David Raymond@>
89 went through several versions on different computers as the format of
90 \.{DVI} files was evolving to its present form. Peter Breitenlohner
91 helped with the latest revisions.
92 @^Breitenlohner, Peter@>
93
94 The |banner| string defined here should be changed whenever \.{DVItype}
95 gets modified.
96
97 @d banner=='This is ODVItype, Version 1.5' {printed when the program starts}
98
99 @ This program is written in standard \PASCAL, except where it is necessary
100 to use extensions; for example, \.{DVItype} must read files whose names
101 are dynamically specified, and that would be impossible in pure \PASCAL.
102 All places where nonstandard constructions are used have been listed in
103 the index under ``system dependencies.''
104 @!@^system dependencies@>
105
106 One of the extensions to standard \PASCAL\ that we shall deal with is the
107 ability to move to a random place in a binary file; another is to
108 determine the length of a binary file. Such extensions are not necessary
109 for reading \.{DVI} files, and they are not important for efficiency
110 reasons either---an infrequently used program like \.{DVItype} does not
111 have to be efficient. But they are included there because of \.{DVItype}'s
112 r\^^Dole as a model of a \.{DVI} reading routine, since other \.{DVI}
113 processors ought to be highly efficient. If \.{DVItype} is being used with
114 \PASCAL s for which random file positioning is not efficiently available,
115 the following definition should be changed from |true| to |false|; in such
116 cases, \.{DVItype} will not include the optional feature that reads the
117 postamble first.
118
119 Another extension is to use a default |case| as in \.{TANGLE}, \.{WEAVE},
120 etc.
121
122 @d random_reading==true {should we skip around in the file?}
123 @d othercases == others: {default for cases not listed explicitly}
124 @d endcases == @+end {follows the default case in an extended |case| statement}
125 @f othercases == else
126 @f endcases == end
127
128 @ The binary input comes from |dvi_file|, and the symbolic output is written
129 on \PASCAL's standard |output| file. The term |print| is used instead of
130 |write| when this program writes on |output|, so that all such output
131 could easily be redirected if desired.
132
133 @d print(#)==write(#)
134 @d print_ln(#)==write_ln(#)
135
136 @p program DVI_type(@!dvi_file,@!output);
137 label @<Labels in the outer block@>@/
138 const @<Constants in the outer block@>@/
139 type @<Types in the outer block@>@/
140 var @<Globals in the outer block@>@/
141 procedure initialize; {this procedure gets things started properly}
142 var i:integer; {loop index for initializations}
143 begin print_ln(banner);@/
144 @<Set initial values@>@/
145 end;
146
147 @ If the program has to stop prematurely, it goes to the
148 `|final_end|'. Another label, |done|, is used when stopping normally.
149
150 @d final_end=9999 {label for the end of it all}
151 @d done=30 {go here when finished with a subtask}
152
153 @<Labels...@>=final_end,done;
154
155 @ The following parameters can be changed at compile time to extend or
156 reduce \.{DVItype}'s capacity.
157
158 @<Constants...@>=
159 @!max_fonts=100; {maximum number of distinct fonts per \.{DVI} file}
160 @!max_widths=100000; {maximum number of different characters among all fonts}
161 @!line_length=79; {bracketed lines of output will be at most this long}
162 @!terminal_line_length=150; {maximum number of characters input in a single
163 line of input from the terminal}
164 @!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth}
165 @!name_size=1000; {total length of all font file names}
166 @!name_length=50; {a file name shouldn't be longer than this}
167
168 @ Here are some macros for common programming idioms.
169
170 @d incr(#) == #:=#+1 {increase a variable by unity}
171 @d decr(#) == #:=#-1 {decrease a variable by unity}
172 @d do_nothing == {empty statement}
173
174 @ If the \.{DVI} file is badly malformed, the whole process must be aborted;
175 \.{DVItype} will give up, after issuing an error message about the symptoms
176 that were noticed.
177
178 Such errors might be discovered inside of subroutines inside of subroutines,
179 so a procedure called |jump_out| has been introduced. This procedure, which
180 simply transfers control to the label |final_end| at the end of the program,
181 contains the only non-local |goto| statement in \.{DVItype}.
182 @^system dependencies@>
183
184 @d abort(#)==begin print(' ',#); jump_out;
185 end
186 @d bad_dvi(#)==abort('Bad DVI file: ',#,'!')
187 @.Bad DVI file@>
188
189 @p procedure jump_out;
190 begin goto final_end;
191 end;
192
193 @* The character set.
194 Like all programs written with the \.{WEB} system, \.{DVItype} can be
195 used with any character set. But it uses ASCII code internally, because
196 the programming for portable input-output is easier when a fixed internal
197 code is used, and because \.{DVI} files use ASCII code for file names
198 and certain other strings.
199
200 The next few sections of \.{DVItype} have therefore been copied from the
201 analogous ones in the \.{WEB} system routines. They have been considerably
202 simplified, since \.{DVItype} need not deal with the controversial
203 ASCII codes less than @'40 or greater than @'176.
204 If such codes appear in the \.{DVI} file,
205 they will be printed as question marks.
206
207 @<Types...@>=
208 @!ASCII_code=" ".."~"; {a subrange of the integers}
209
210 @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
211 character sets were common, so it did not make provision for lower case
212 letters. Nowadays, of course, we need to deal with both upper and lower case
213 alphabets in a convenient way, especially in a program like \.{DVItype}.
214 So we shall assume that the \PASCAL\ system being used for \.{DVItype}
215 has a character set containing at least the standard visible characters
216 of ASCII code (|"!"| through |"~"|).
217
218 Some \PASCAL\ compilers use the original name |char| for the data type
219 associated with the characters in text files, while other \PASCAL s
220 consider |char| to be a 64-element subrange of a larger data type that has
221 some other name. In order to accommodate this difference, we shall use
222 the name |text_char| to stand for the data type of the characters in the
223 output file. We shall also assume that |text_char| consists of
224 the elements |chr(first_text_char)| through |chr(last_text_char)|,
225 inclusive. The following definitions should be adjusted if necessary.
226 @^system dependencies@>
227
228 @d text_char == char {the data type of characters in text files}
229 @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
230 @d last_text_char=127 {ordinal number of the largest element of |text_char|}
231
232 @<Types...@>=
233 @!text_file=packed file of text_char;
234
235 @ The \.{DVItype} processor converts between ASCII code and
236 the user's external character set by means of arrays |xord| and |xchr|
237 that are analogous to \PASCAL's |ord| and |chr| functions.
238
239 @<Globals...@>=
240 @!xord: array [text_char] of ASCII_code;
241 {specifies conversion of input characters}
242 @!xchr: array [0..255] of text_char;
243 {specifies conversion of output characters}
244
245 @ Under our assumption that the visible characters of standard ASCII are
246 all present, the following assignment statements initialize the
247 |xchr| array properly, without needing any system-dependent changes.
248
249 @<Set init...@>=
250 for i:=0 to @'37 do xchr[i]:='?';
251 xchr[@'40]:=' ';
252 xchr[@'41]:='!';
253 xchr[@'42]:='"';
254 xchr[@'43]:='#';
255 xchr[@'44]:='$';
256 xchr[@'45]:='%';
257 xchr[@'46]:='&';
258 xchr[@'47]:='''';@/
259 xchr[@'50]:='(';
260 xchr[@'51]:=')';
261 xchr[@'52]:='*';
262 xchr[@'53]:='+';
263 xchr[@'54]:=',';
264 xchr[@'55]:='-';
265 xchr[@'56]:='.';
266 xchr[@'57]:='/';@/
267 xchr[@'60]:='0';
268 xchr[@'61]:='1';
269 xchr[@'62]:='2';
270 xchr[@'63]:='3';
271 xchr[@'64]:='4';
272 xchr[@'65]:='5';
273 xchr[@'66]:='6';
274 xchr[@'67]:='7';@/
275 xchr[@'70]:='8';
276 xchr[@'71]:='9';
277 xchr[@'72]:=':';
278 xchr[@'73]:=';';
279 xchr[@'74]:='<';
280 xchr[@'75]:='=';
281 xchr[@'76]:='>';
282 xchr[@'77]:='?';@/
283 xchr[@'100]:='@@';
284 xchr[@'101]:='A';
285 xchr[@'102]:='B';
286 xchr[@'103]:='C';
287 xchr[@'104]:='D';
288 xchr[@'105]:='E';
289 xchr[@'106]:='F';
290 xchr[@'107]:='G';@/
291 xchr[@'110]:='H';
292 xchr[@'111]:='I';
293 xchr[@'112]:='J';
294 xchr[@'113]:='K';
295 xchr[@'114]:='L';
296 xchr[@'115]:='M';
297 xchr[@'116]:='N';
298 xchr[@'117]:='O';@/
299 xchr[@'120]:='P';
300 xchr[@'121]:='Q';
301 xchr[@'122]:='R';
302 xchr[@'123]:='S';
303 xchr[@'124]:='T';
304 xchr[@'125]:='U';
305 xchr[@'126]:='V';
306 xchr[@'127]:='W';@/
307 xchr[@'130]:='X';
308 xchr[@'131]:='Y';
309 xchr[@'132]:='Z';
310 xchr[@'133]:='[';
311 xchr[@'134]:='\';
312 xchr[@'135]:=']';
313 xchr[@'136]:='^';
314 xchr[@'137]:='_';@/
315 xchr[@'140]:='`';
316 xchr[@'141]:='a';
317 xchr[@'142]:='b';
318 xchr[@'143]:='c';
319 xchr[@'144]:='d';
320 xchr[@'145]:='e';
321 xchr[@'146]:='f';
322 xchr[@'147]:='g';@/
323 xchr[@'150]:='h';
324 xchr[@'151]:='i';
325 xchr[@'152]:='j';
326 xchr[@'153]:='k';
327 xchr[@'154]:='l';
328 xchr[@'155]:='m';
329 xchr[@'156]:='n';
330 xchr[@'157]:='o';@/
331 xchr[@'160]:='p';
332 xchr[@'161]:='q';
333 xchr[@'162]:='r';
334 xchr[@'163]:='s';
335 xchr[@'164]:='t';
336 xchr[@'165]:='u';
337 xchr[@'166]:='v';
338 xchr[@'167]:='w';@/
339 xchr[@'170]:='x';
340 xchr[@'171]:='y';
341 xchr[@'172]:='z';
342 xchr[@'173]:='{';
343 xchr[@'174]:='|';
344 xchr[@'175]:='}';
345 xchr[@'176]:='~';
346 for i:=@'177 to 255 do xchr[i]:='?';
347
348 @ The following system-independent code makes the |xord| array contain a
349 suitable inverse to the information in |xchr|.
350
351 @<Set init...@>=
352 for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
353 for i:=" " to "~" do xord[xchr[i]]:=i;
354
355 @* Device-independent file format.
356 Before we get into the details of \.{DVItype}, we need to know exactly
357 what \.{DVI} files are. The form of such files was designed by David R.
358 @^Fuchs, David Raymond@>
359 Fuchs in 1979. Almost any reasonable typesetting device can be driven by
360 a program that takes \.{DVI} files as input, and dozens of such
361 \.{DVI}-to-whatever programs have been written. Thus, it is possible to
362 print the output of document compilers like \TeX\ on many different kinds
363 of equipment.
364
365 A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a
366 series of commands in a machine-like language. The first byte of each command
367 is the operation code, and this code is followed by zero or more bytes
368 that provide parameters to the command. The parameters themselves may consist
369 of several consecutive bytes; for example, the `|set_rule|' command has two
370 parameters, each of which is four bytes long. Parameters are usually
371 regarded as nonnegative integers; but four-byte-long parameters,
372 and shorter parameters that denote distances, can be
373 either positive or negative. Such parameters are given in two's complement
374 notation. For example, a two-byte-long distance parameter has a value between
375 $-2^{15}$ and $2^{15}-1$.
376 @.DVI {\rm files}@>
377
378 A \.{DVI} file consists of a ``preamble,'' followed by a sequence of one
379 or more ``pages,'' followed by a ``postamble.'' The preamble is simply a
380 |pre| command, with its parameters that define the dimensions used in the
381 file; this must come first. Each ``page'' consists of a |bop| command,
382 followed by any number of other commands that tell where characters are to
383 be placed on a physical page, followed by an |eop| command. The pages
384 appear in the order that they were generated, not in any particular
385 numerical order. If we ignore |nop| commands and \\{fnt\_def} commands
386 (which are allowed between any two commands in the file), each |eop|
387 command is immediately followed by a |bop| command, or by a |post|
388 command; in the latter case, there are no more pages in the file, and the
389 remaining bytes form the postamble. Further details about the postamble
390 will be explained later.
391
392 Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
393 quantities that give the location number of some other byte in the file;
394 the first byte is number~0, then comes number~1, and so on. For example,
395 one of the parameters of a |bop| command points to the previous |bop|;
396 this makes it feasible to read the pages in backwards order, in case the
397 results are being directed to a device that stacks its output face up.
398 Suppose the preamble of a \.{DVI} file occupies bytes 0 to 99. Now if the
399 first page occupies bytes 100 to 999, say, and if the second
400 page occupies bytes 1000 to 1999, then the |bop| that starts in byte 1000
401 points to 100 and the |bop| that starts in byte 2000 points to 1000. (The
402 very first |bop|, i.e., the one that starts in byte 100, has a pointer of $-1$.)
403
404 @ The \.{DVI} format is intended to be both compact and easily interpreted
405 by a machine. Compactness is achieved by making most of the information
406 implicit instead of explicit. When a \.{DVI}-reading program reads the
407 commands for a page, it keeps track of several quantities: (a)~The current
408 font |f| is an integer; this value is changed only
409 by \\{fnt} and \\{fnt\_num} commands. (b)~The current position on the page
410 is given by two numbers called the horizontal and vertical coordinates,
411 |h| and |v|. Both coordinates are zero at the upper left corner of the page;
412 moving to the right corresponds to increasing the horizontal coordinate, and
413 moving down corresponds to increasing the vertical coordinate. Thus, the
414 coordinates are essentially Cartesian, except that vertical directions are
415 flipped; the Cartesian version of |(h,v)| would be |(h,-v)|. (c)~The
416 current spacing amounts are given by four numbers |w|, |x|, |y|, and |z|,
417 where |w| and~|x| are used for horizontal spacing and where |y| and~|z|
418 are used for vertical spacing. (d)~There is a stack containing
419 |(h,v,w,x,y,z)| values; the \.{DVI} commands |push| and |pop| are used to
420 change the current level of operation. Note that the current font~|f| is
421 not pushed and popped; the stack contains only information about
422 positioning.
423
424 The values of |h|, |v|, |w|, |x|, |y|, and |z| are signed integers having up
425 to 32 bits, including the sign. Since they represent physical distances,
426 there is a small unit of measurement such that increasing |h| by~1 means
427 moving a certain tiny distance to the right. The actual unit of
428 measurement is variable, as explained below.
429
430 @ Here is a list of all the commands that may appear in a \.{DVI} file. Each
431 command is specified by its symbolic name (e.g., |bop|), its opcode byte
432 (e.g., 139), and its parameters (if any). The parameters are followed
433 by a bracketed number telling how many bytes they occupy; for example,
434 `|p[4]|' means that parameter |p| is four bytes long.
435
436 \yskip\hang|set_char_0| 0. Typeset character number~0 from font~|f|
437 such that the reference point of the character is at |(h,v)|. Then
438 increase |h| by the width of that character. Note that a character may
439 have zero or negative width, so one cannot be sure that |h| will advance
440 after this command; but |h| usually does increase.
441
442 \yskip\hang|set_char_1| through |set_char_127| (opcodes 1 to 127).
443 Do the operations of |set_char_0|; but use the character whose number
444 matches the opcode, instead of character~0.
445
446 \yskip\hang|set1| 128 |c[1]|. Same as |set_char_0|, except that character
447 number~|c| is typeset. \TeX82 uses this command for characters in the
448 range |128<=c<256|.
449
450 \yskip\hang|set2| 129 |c[2]|. Same as |set1|, except that |c|~is two
451 bytes long, so it is in the range |0<=c<65536|. \TeX82 never uses this
452 command, which is intended for processors that deal with oriental languages;
453 but \.{DVItype} will allow character codes greater than 255, assuming that
454 they all have the same width as the character whose code is $c \bmod 256$.
455 @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
456
457 \yskip\hang|set3| 130 |c[3]|. Same as |set1|, except that |c|~is three
458 bytes long, so it can be as large as $2^{24}-1$.
459
460 \yskip\hang|set4| 131 |c[4]|. Same as |set1|, except that |c|~is four
461 bytes long, possibly even negative. Imagine that.
462
463 \yskip\hang|set_rule| 132 |a[4]| |b[4]|. Typeset a solid black rectangle
464 of height |a| and width |b|, with its bottom left corner at |(h,v)|. Then
465 set |h:=h+b|. If either |a<=0| or |b<=0|, nothing should be typeset. Note
466 that if |b<0|, the value of |h| will decrease even though nothing else happens.
467 Programs that typeset from \.{DVI} files should be careful to make the rules
468 line up carefully with digitized characters, as explained in connection with
469 the |rule_pixels| subroutine below.
470
471 \yskip\hang|put1| 133 |c[1]|. Typeset character number~|c| from font~|f|
472 such that the reference point of the character is at |(h,v)|. (The `put'
473 commands are exactly like the `set' commands, except that they simply put out a
474 character or a rule without moving the reference point afterwards.)
475
476 \yskip\hang|put2| 134 |c[2]|. Same as |set2|, except that |h| is not changed.
477
478 \yskip\hang|put3| 135 |c[3]|. Same as |set3|, except that |h| is not changed.
479
480 \yskip\hang|put4| 136 |c[4]|. Same as |set4|, except that |h| is not changed.
481
482 \yskip\hang|put_rule| 137 |a[4]| |b[4]|. Same as |set_rule|, except that
483 |h| is not changed.
484
485 \yskip\hang|nop| 138. No operation, do nothing. Any number of |nop|'s
486 may occur between \.{DVI} commands, but a |nop| cannot be inserted between
487 a command and its parameters or between two parameters.
488
489 \yskip\hang|bop| 139 $c_0[4]$ $c_1[4]$ $\ldots$ $c_9[4]$ $p[4]$. Beginning
490 of a page: Set |(h,v,w,x,y,z):=(0,0,0,0,0,0)| and set the stack empty. Set
491 the current font |f| to an undefined value. The ten $c_i$ parameters can
492 be used to identify pages, if a user wants to print only part of a \.{DVI}
493 file; \TeX82 gives them the values of \.{\\count0} $\ldots$ \.{\\count9}
494 at the time \.{\\shipout} was invoked for this page. The parameter |p|
495 points to the previous |bop| command in the file, where the first |bop|
496 has $p=-1$.
497
498 \yskip\hang|eop| 140. End of page: Print what you have read since the
499 previous |bop|. At this point the stack should be empty. (The \.{DVI}-reading
500 programs that drive most output devices will have kept a buffer of the
501 material that appears on the page that has just ended. This material is
502 largely, but not entirely, in order by |v| coordinate and (for fixed |v|) by
503 |h|~coordinate; so it usually needs to be sorted into some order that is
504 appropriate for the device in question. \.{DVItype} does not do such sorting.)
505
506 \yskip\hang|push| 141. Push the current values of |(h,v,w,x,y,z)| onto the
507 top of the stack; do not change any of these values. Note that |f| is
508 not pushed.
509
510 \yskip\hang|pop| 142. Pop the top six values off of the stack and assign
511 them to |(h,v,w,x,y,z)|. The number of pops should never exceed the number
512 of pushes, since it would be highly embarrassing if the stack were empty
513 at the time of a |pop| command.
514
515 \yskip\hang|right1| 143 |b[1]|. Set |h:=h+b|, i.e., move right |b| units.
516 The parameter is a signed number in two's complement notation, |-128<=b<128|;
517 if |b<0|, the reference point actually moves left.
518
519 \yskip\hang|right2| 144 |b[2]|. Same as |right1|, except that |b| is a
520 two-byte quantity in the range |-32768<=b<32768|.
521
522 \yskip\hang|right3| 145 |b[3]|. Same as |right1|, except that |b| is a
523 three-byte quantity in the range |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
524
525 \yskip\hang|right4| 146 |b[4]|. Same as |right1|, except that |b| is a
526 four-byte quantity in the range |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
527
528 \yskip\hang|w0| 147. Set |h:=h+w|; i.e., move right |w| units. With luck,
529 this parameterless command will usually suffice, because the same kind of motion
530 will occur several times in succession; the following commands explain how
531 |w| gets particular values.
532
533 \yskip\hang|w1| 148 |b[1]|. Set |w:=b| and |h:=h+b|. The value of |b| is a
534 signed quantity in two's complement notation, |-128<=b<128|. This command
535 changes the current |w|~spacing and moves right by |b|.
536
537 \yskip\hang|w2| 149 |b[2]|. Same as |w1|, but |b| is a two-byte-long
538 parameter, |-32768<=b<32768|.
539
540 \yskip\hang|w3| 150 |b[3]|. Same as |w1|, but |b| is a three-byte-long
541 parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
542
543 \yskip\hang|w4| 151 |b[4]|. Same as |w1|, but |b| is a four-byte-long
544 parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
545
546 \yskip\hang|x0| 152. Set |h:=h+x|; i.e., move right |x| units. The `|x|'
547 commands are like the `|w|' commands except that they involve |x| instead
548 of |w|.
549
550 \yskip\hang|x1| 153 |b[1]|. Set |x:=b| and |h:=h+b|. The value of |b| is a
551 signed quantity in two's complement notation, |-128<=b<128|. This command
552 changes the current |x|~spacing and moves right by |b|.
553
554 \yskip\hang|x2| 154 |b[2]|. Same as |x1|, but |b| is a two-byte-long
555 parameter, |-32768<=b<32768|.
556
557 \yskip\hang|x3| 155 |b[3]|. Same as |x1|, but |b| is a three-byte-long
558 parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
559
560 \yskip\hang|x4| 156 |b[4]|. Same as |x1|, but |b| is a four-byte-long
561 parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
562
563 \yskip\hang|down1| 157 |a[1]|. Set |v:=v+a|, i.e., move down |a| units.
564 The parameter is a signed number in two's complement notation, |-128<=a<128|;
565 if |a<0|, the reference point actually moves up.
566
567 \yskip\hang|down2| 158 |a[2]|. Same as |down1|, except that |a| is a
568 two-byte quantity in the range |-32768<=a<32768|.
569
570 \yskip\hang|down3| 159 |a[3]|. Same as |down1|, except that |a| is a
571 three-byte quantity in the range |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
572
573 \yskip\hang|down4| 160 |a[4]|. Same as |down1|, except that |a| is a
574 four-byte quantity in the range |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
575
576 \yskip\hang|y0| 161. Set |v:=v+y|; i.e., move down |y| units. With luck,
577 this parameterless command will usually suffice, because the same kind of motion
578 will occur several times in succession; the following commands explain how
579 |y| gets particular values.
580
581 \yskip\hang|y1| 162 |a[1]|. Set |y:=a| and |v:=v+a|. The value of |a| is a
582 signed quantity in two's complement notation, |-128<=a<128|. This command
583 changes the current |y|~spacing and moves down by |a|.
584
585 \yskip\hang|y2| 163 |a[2]|. Same as |y1|, but |a| is a two-byte-long
586 parameter, |-32768<=a<32768|.
587
588 \yskip\hang|y3| 164 |a[3]|. Same as |y1|, but |a| is a three-byte-long
589 parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
590
591 \yskip\hang|y4| 165 |a[4]|. Same as |y1|, but |a| is a four-byte-long
592 parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
593
594 \yskip\hang|z0| 166. Set |v:=v+z|; i.e., move down |z| units. The `|z|' commands
595 are like the `|y|' commands except that they involve |z| instead of |y|.
596
597 \yskip\hang|z1| 167 |a[1]|. Set |z:=a| and |v:=v+a|. The value of |a| is a
598 signed quantity in two's complement notation, |-128<=a<128|. This command
599 changes the current |z|~spacing and moves down by |a|.
600
601 \yskip\hang|z2| 168 |a[2]|. Same as |z1|, but |a| is a two-byte-long
602 parameter, |-32768<=a<32768|.
603
604 \yskip\hang|z3| 169 |a[3]|. Same as |z1|, but |a| is a three-byte-long
605 parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
606
607 \yskip\hang|z4| 170 |a[4]|. Same as |z1|, but |a| is a four-byte-long
608 parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
609
610 \yskip\hang|fnt_num_0| 171. Set |f:=0|. Font 0 must previously have been
611 defined by a \\{fnt\_def} instruction, as explained below.
612
613 \yskip\hang|fnt_num_1| through |fnt_num_63| (opcodes 172 to 234). Set
614 |f:=1|, \dots, |f:=63|, respectively.
615
616 \yskip\hang|fnt1| 235 |k[1]|. Set |f:=k|. \TeX82 uses this command for font
617 numbers in the range |64<=k<256|.
618
619 \yskip\hang|fnt2| 236 |k[2]|. Same as |fnt1|, except that |k|~is two
620 bytes long, so it is in the range |0<=k<65536|. \TeX82 never generates this
621 command, but large font numbers may prove useful for specifications of
622 color or texture, or they may be used for special fonts that have fixed
623 numbers in some external coding scheme.
624
625 \yskip\hang|fnt3| 237 |k[3]|. Same as |fnt1|, except that |k|~is three
626 bytes long, so it can be as large as $2^{24}-1$.
627
628 \yskip\hang|fnt4| 238 |k[4]|. Same as |fnt1|, except that |k|~is four
629 bytes long; this is for the really big font numbers (and for the negative ones).
630
631 \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
632 general; it functions as a $(k+2)$-byte |nop| unless special \.{DVI}-reading
633 programs are being used. \TeX82 generates |xxx1| when a short enough
634 \.{\\special} appears, setting |k| to the number of bytes being sent. It
635 is recommended that |x| be a string having the form of a keyword followed
636 by possible parameters relevant to that keyword.
637
638 \yskip\hang|xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
639
640 \yskip\hang|xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
641
642 \yskip\hang|xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously
643 large. \TeX82 uses |xxx4| when |xxx1| would be incorrect.
644
645 \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
646 Define font |k|, where |0<=k<256|; font definitions will be explained shortly.
647
648 \yskip\hang|fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
649 Define font |k|, where |0<=k<65536|.
650
651 \yskip\hang|fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
652 Define font |k|, where |0<=k<@t$2^{24}$@>|.
653
654 \yskip\hang|fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
655 Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
656
657 \yskip\hang|pre| 247 |i[1]| |num[4]| |den[4]| |mag[4]| |k[1]| |x[k]|.
658 Beginning of the preamble; this must come at the very beginning of the
659 file. Parameters |i|, |num|, |den|, |mag|, |k|, and |x| are explained below.
660
661 \yskip\hang|post| 248. Beginning of the postamble, see below.
662
663 \yskip\hang|post_post| 249. Ending of the postamble, see below.
664
665 \yskip\noindent Commands 250--255 are undefined at the present time.
666
667 @ @d set_char_0=0 {typeset character 0 and move right}
668 @d set1=128 {typeset a character and move right}
669 @d set_rule=132 {typeset a rule and move right}
670 @d put1=133 {typeset a character}
671 @d put_rule=137 {typeset a rule}
672 @d nop=138 {no operation}
673 @d bop=139 {beginning of page}
674 @d eop=140 {ending of page}
675 @d push=141 {save the current positions}
676 @d pop=142 {restore previous positions}
677 @d right1=143 {move right}
678 @d w0=147 {move right by |w|}
679 @d w1=148 {move right and set |w|}
680 @d x0=152 {move right by |x|}
681 @d x1=153 {move right and set |x|}
682 @d down1=157 {move down}
683 @d y0=161 {move down by |y|}
684 @d y1=162 {move down and set |y|}
685 @d z0=166 {move down by |z|}
686 @d z1=167 {move down and set |z|}
687 @d fnt_num_0=171 {set current font to 0}
688 @d fnt1=235 {set current font}
689 @d xxx1=239 {extension to \.{DVI} primitives}
690 @d xxx4=242 {potentially long extension to \.{DVI} primitives}
691 @d fnt_def1=243 {define the meaning of a font number}
692 @d pre=247 {preamble}
693 @d post=248 {postamble beginning}
694 @d post_post=249 {postamble ending}
695 @d undefined_commands==250,251,252,253,254,255
696
697 @ The preamble contains basic information about the file as a whole. As
698 stated above, there are six parameters:
699 $$\hbox{|@!i[1]| |@!num[4]| |@!den[4]| |@!mag[4]| |@!k[1]| |@!x[k]|.}$$
700 The |i| byte identifies \.{DVI} format; currently this byte is always set
701 to~2. (The value |i=3| is currently used for an extended format that
702 allows a mixture of right-to-left and left-to-right typesetting.
703 Some day we will set |i=4|, when \.{DVI} format makes another
704 incompatible change---perhaps in the year 2048.)
705
706 The next two parameters, |num| and |den|, are positive integers that define
707 the units of measurement; they are the numerator and denominator of a
708 fraction by which all dimensions in the \.{DVI} file could be multiplied
709 in order to get lengths in units of $10^{-7}$ meters. (For example, there are
710 exactly 7227 \TeX\ points in 254 centimeters, and \TeX82 works with scaled
711 points where there are $2^{16}$ sp in a point, so \TeX82 sets |num=25400000|
712 and $|den|=7227\cdot2^{16}=473628672$.)
713 @^sp@>
714
715 The |mag| parameter is what \TeX82 calls \.{\\mag}, i.e., 1000 times the
716 desired magnification. The actual fraction by which dimensions are
717 multiplied is therefore $mn/1000d$. Note that if a \TeX\ source document
718 does not call for any `\.{true}' dimensions, and if you change it only by
719 specifying a different \.{\\mag} setting, the \.{DVI} file that \TeX\
720 creates will be completely unchanged except for the value of |mag| in the
721 preamble and postamble. (Fancy \.{DVI}-reading programs allow users to
722 override the |mag|~setting when a \.{DVI} file is being printed.)
723
724 Finally, |k| and |x| allow the \.{DVI} writer to include a comment, which is not
725 interpreted further. The length of comment |x| is |k|, where |0<=k<256|.
726
727 @d id_byte=2 {identifies the kind of \.{DVI} files described here}
728
729 @ Font definitions for a given font number |k| contain further parameters
730 $$\hbox{|c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.}$$
731 The four-byte value |c| is the check sum that \TeX\ (or whatever program
732 generated the \.{DVI} file) found in the \.{TFM} file for this font;
733 |c| should match the check sum of the font found by programs that read
734 this \.{DVI} file.
735 @^check sum@>
736
737 Parameter |s| contains a fixed-point scale factor that is applied to the
738 character widths in font |k|; font dimensions in \.{TFM} files and other
739 font files are relative to this quantity, which is always positive and
740 less than $2^{27}$. It is given in the same units as the other dimensions
741 of the \.{DVI} file. Parameter |d| is similar to |s|; it is the ``design
742 size,'' and (like~|s|) it is given in \.{DVI} units. Thus, font |k| is to be
743 used at $|mag|\cdot s/1000d$ times its normal size.
744
745 The remaining part of a font definition gives the external name of the font,
746 which is an ASCII string of length |a+l|. The number |a| is the length
747 of the ``area'' or directory, and |l| is the length of the font name itself;
748 the standard local system font area is supposed to be used when |a=0|.
749 The |n| field contains the area in its first |a| bytes.
750
751 Font definitions must appear before the first use of a particular font number.
752 Once font |k| is defined, it must not be defined again; however, we
753 shall see below that font definitions appear in the postamble as well as
754 in the pages, so in this sense each font number is defined exactly twice,
755 if at all. Like |nop| commands, font definitions can
756 appear before the first |bop|, or between an |eop| and a |bop|.
757
758 @ The last page in a \.{DVI} file is followed by `|post|'; this command
759 introduces the postamble, which summarizes important facts that \TeX\ has
760 accumulated about the file, making it possible to print subsets of the data
761 with reasonable efficiency. The postamble has the form
762 $$\vbox{\halign{\hbox{#\hfil}\cr
763 |post| |p[4]| |num[4]| |den[4]| |mag[4]| |l[4]| |u[4]| |s[2]| |t[2]|\cr
764 $\langle\,$font definitions$\,\rangle$\cr
765 |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
766 Here |p| is a pointer to the final |bop| in the file. The next three
767 parameters, |num|, |den|, and |mag|, are duplicates of the quantities that
768 appeared in the preamble.
769
770 Parameters |l| and |u| give respectively the height-plus-depth of the tallest
771 page and the width of the widest page, in the same units as other dimensions
772 of the file. These numbers might be used by a \.{DVI}-reading program to
773 position individual ``pages'' on large sheets of film or paper; however,
774 the standard convention for output on normal size paper is to position each
775 page so that the upper left-hand corner is exactly one inch from the left
776 and the top. Experience has shown that it is unwise to design \.{DVI}-to-printer
777 software that attempts cleverly to center the output; a fixed position of
778 the upper left corner is easiest for users to understand and to work with.
779 Therefore |l| and~|u| are often ignored.
780
781 Parameter |s| is the maximum stack depth (i.e., the largest excess of
782 |push| commands over |pop| commands) needed to process this file. Then
783 comes |t|, the total number of pages (|bop| commands) present.
784
785 The postamble continues with font definitions, which are any number of
786 \\{fnt\_def} commands as described above, possibly interspersed with |nop|
787 commands. Each font number that is used in the \.{DVI} file must be defined
788 exactly twice: Once before it is first selected by a \\{fnt} command, and once
789 in the postamble.
790
791 @ The last part of the postamble, following the |post_post| byte that
792 signifies the end of the font definitions, contains |q|, a pointer to the
793 |post| command that started the postamble. An identification byte, |i|,
794 comes next; this currently equals~2, as in the preamble.
795
796 The |i| byte is followed by four or more bytes that are all equal to
797 the decimal number 223 (i.e., @'337 in octal). \TeX\ puts out four to seven of
798 these trailing bytes, until the total length of the file is a multiple of
799 four bytes, since this works out best on machines that pack four bytes per
800 word; but any number of 223's is allowed, as long as there are at least four
801 of them. In effect, 223 is a sort of signature that is added at the very end.
802 @^Fuchs, David Raymond@>
803
804 This curious way to finish off a \.{DVI} file makes it feasible for
805 \.{DVI}-reading programs to find the postamble first, on most computers,
806 even though \TeX\ wants to write the postamble last. Most operating
807 systems permit random access to individual words or bytes of a file, so
808 the \.{DVI} reader can start at the end and skip backwards over the 223's
809 until finding the identification byte. Then it can back up four bytes, read
810 |q|, and move to byte |q| of the file. This byte should, of course,
811 contain the value 248 (|post|); now the postamble can be read, so the
812 \.{DVI} reader discovers all the information needed for typesetting the
813 pages. Note that it is also possible to skip through the \.{DVI} file at
814 reasonably high speed to locate a particular page, if that proves
815 desirable. This saves a lot of time, since \.{DVI} files used in production
816 jobs tend to be large.
817
818 Unfortunately, however, standard \PASCAL\ does not include the ability to
819 @^system dependencies@>
820 access a random position in a file, or even to determine the length of a file.
821 Almost all systems nowadays provide the necessary capabilities, so \.{DVI}
822 format has been designed to work most efficiently with modern operating systems.
823 As noted above, \.{DVItype} will limit itself to the restrictions of standard
824 \PASCAL\ if |random_reading| is defined to be |false|.
825
826 @* Input from binary files.
827 We have seen that a \.{DVI} file is a sequence of 8-bit bytes. The bytes
828 appear physically in what is called a `|packed file of 0..255|'
829 in \PASCAL\ lingo.
830
831 Packing is system dependent, and many \PASCAL\ systems fail to implement
832 such files in a sensible way (at least, from the viewpoint of producing
833 good production software). For example, some systems treat all
834 byte-oriented files as text, looking for end-of-line marks and such
835 things. Therefore some system-dependent code is often needed to deal with
836 binary files, even though most of the program in this section of
837 \.{DVItype} is written in standard \PASCAL.
838 @^system dependencies@>
839
840 One common way to solve the problem is to consider files of |integer|
841 numbers, and to convert an integer in the range $-2^{31}\L x<2^{31}$ to
842 a sequence of four bytes $(a,b,c,d)$ using the following code, which
843 avoids the controversial integer division of negative numbers:
844 $$\vbox{\halign{#\hfil\cr
845 |if x>=0 then a:=x div @'100000000|\cr
846 |else begin x:=(x+@'10000000000)+@'10000000000; a:=x div @'100000000+128;|\cr
847 \quad|end|\cr
848 |x:=x mod @'100000000;|\cr
849 |b:=x div @'200000; x:=x mod @'200000;|\cr
850 |c:=x div @'400; d:=x mod @'400;|\cr}}$$
851 The four bytes are then kept in a buffer and output one by one. (On 36-bit
852 computers, an additional division by 16 is necessary at the beginning.
853 Another way to separate an integer into four bytes is to use/abuse
854 \PASCAL's variant records, storing an integer and retrieving bytes that are
855 packed in the same place; {\sl caveat implementor!\/}) It is also desirable
856 in some cases to read a hundred or so integers at a time, maintaining a
857 larger buffer.
858
859 We shall stick to simple \PASCAL\ in this program, for reasons of clarity,
860 even if such simplicity is sometimes unrealistic.
861
862 @<Types...@>=
863 @!eight_bits=0..255; {unsigned one-byte quantity}
864 @!byte_file=packed file of eight_bits; {files that contain binary data}
865
866 @ The program deals with two binary file variables: |dvi_file| is the main
867 input file that we are translating into symbolic form, and |tfm_file| is
868 the current font metric file from which character-width information is
869 being read.
870
871 @<Glob...@>=
872 @!dvi_file:byte_file; {the stuff we are \.{DVI}typing}
873 @!tfm_file:byte_file; {a font metric file}
874
875 @ To prepare these files for input, we |reset| them. An extension of
876 \PASCAL\ is needed in the case of |tfm_file|, since we want to associate
877 it with external files whose names are specified dynamically (i.e., not
878 known at compile time). The following code assumes that `|reset(f,s)|'
879 does this, when |f| is a file variable and |s| is a string variable that
880 specifies the file name. If |eof(f)| is true immediately after
881 |reset(f,s)| has acted, we assume that no file named |s| is accessible.
882 @^system dependencies@>
883
884 @p procedure open_dvi_file; {prepares to read packed bytes in |dvi_file|}
885 begin reset(dvi_file);
886 cur_loc:=0;
887 end;
888 @#
889 procedure open_tfm_file; {prepares to read packed bytes in |tfm_file|}
890 begin reset(tfm_file,cur_name);
891 end;
892
893 @ If you looked carefully at the preceding code, you probably asked,
894 ``What are |cur_loc| and |cur_name|?'' Good question. They're global
895 variables: |cur_loc| is the number of the byte about to be read next from
896 |dvi_file|, and |cur_name| is a string variable that will be set to the
897 current font metric file name before |open_tfm_file| is called.
898
899 @<Glob...@>=
900 @!cur_loc:integer; {where we are about to look, in |dvi_file|}
901 @!cur_name:packed array[1..name_length] of char; {external name,
902 with no lower case letters}
903
904 @ It turns out to be convenient to read four bytes at a time, when we are
905 inputting from \.{TFM} files. The input goes into global variables
906 |b0|, |b1|, |b2|, and |b3|, with |b0| getting the first byte and |b3|
907 the fourth.
908
909 @<Glob...@>=
910 @!b0,@!b1,@!b2,@!b3: eight_bits; {four bytes input at once}
911
912 @ The |read_tfm_word| procedure sets |b0| through |b3| to the next
913 four bytes in the current \.{TFM} file.
914 @^system dependencies@>
915
916 @p procedure read_tfm_word;
917 begin read(tfm_file,b0); read(tfm_file,b1);
918 read(tfm_file,b2); read(tfm_file,b3);
919 end;
920
921 @ We shall use another set of simple functions to read the next byte or
922 bytes from |dvi_file|. There are seven possibilities, each of which is
923 treated as a separate function in order to minimize the overhead for
924 subroutine calls.
925 @^system dependencies@>
926
927 @p function get_byte:integer; {returns the next byte, unsigned}
928 var b:eight_bits;
929 begin if eof(dvi_file) then get_byte:=0
930 else begin read(dvi_file,b); incr(cur_loc); get_byte:=b;
931 end;
932 end;
933 @#
934 function signed_byte:integer; {returns the next byte, signed}
935 var b:eight_bits;
936 begin read(dvi_file,b); incr(cur_loc);
937 if b<128 then signed_byte:=b @+ else signed_byte:=b-256;
938 end;
939 @#
940 function get_two_bytes:integer; {returns the next two bytes, unsigned}
941 var a,@!b:eight_bits;
942 begin read(dvi_file,a); read(dvi_file,b);
943 cur_loc:=cur_loc+2;
944 get_two_bytes:=a*256+b;
945 end;
946 @#
947 function signed_pair:integer; {returns the next two bytes, signed}
948 var a,@!b:eight_bits;
949 begin read(dvi_file,a); read(dvi_file,b);
950 cur_loc:=cur_loc+2;
951 if a<128 then signed_pair:=a*256+b
952 else signed_pair:=(a-256)*256+b;
953 end;
954 @#
955 function get_three_bytes:integer; {returns the next three bytes, unsigned}
956 var a,@!b,@!c:eight_bits;
957 begin read(dvi_file,a); read(dvi_file,b); read(dvi_file,c);
958 cur_loc:=cur_loc+3;
959 get_three_bytes:=(a*256+b)*256+c;
960 end;
961 @#
962 function signed_trio:integer; {returns the next three bytes, signed}
963 var a,@!b,@!c:eight_bits;
964 begin read(dvi_file,a); read(dvi_file,b); read(dvi_file,c);
965 cur_loc:=cur_loc+3;
966 if a<128 then signed_trio:=(a*256+b)*256+c
967 else signed_trio:=((a-256)*256+b)*256+c;
968 end;
969 @#
970 function signed_quad:integer; {returns the next four bytes, signed}
971 var a,@!b,@!c,@!d:eight_bits;
972 begin read(dvi_file,a); read(dvi_file,b); read(dvi_file,c); read(dvi_file,d);
973 cur_loc:=cur_loc+4;
974 if a<128 then signed_quad:=((a*256+b)*256+c)*256+d
975 else signed_quad:=(((a-256)*256+b)*256+c)*256+d;
976 end;
977
978 @ Finally we come to the routines that are used only if |random_reading| is
979 |true|. The driver program below needs two such routines: |dvi_length| should
980 compute the total number of bytes in |dvi_file|, possibly also
981 causing |eof(dvi_file)| to be true; and |move_to_byte(n)|
982 should position |dvi_file| so that the next |get_byte| will read byte |n|,
983 starting with |n=0| for the first byte in the file.
984 @^system dependencies@>
985
986 Such routines are, of course, highly system dependent. They are implemented
987 here in terms of two assumed system routines called |set_pos| and |cur_pos|.
988 The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is
989 negative or larger than the total number of items in |f|; in the latter
990 case, |set_pos(f,n)| moves to the end of file |f|.
991 The call |cur_pos(f)| gives the total number of items in |f|, if
992 |eof(f)| is true; we use |cur_pos| only in such a situation.
993
994 @p function dvi_length:integer;
995 begin set_pos(dvi_file,-1); dvi_length:=cur_pos(dvi_file);
996 end;
997 @#
998 procedure move_to_byte(n:integer);
999 begin set_pos(dvi_file,n); cur_loc:=n;
1000 end;
1001
1002 @* Reading the font information.
1003 \.{DVI} file format does not include information about character widths, since
1004 that would tend to make the files a lot longer. But a program that reads
1005 a \.{DVI} file is supposed to know the widths of the characters that appear
1006 in \\{set\_char} commands. Therefore \.{DVItype} looks at the font metric
1007 (\.{TFM}) files for the fonts that are involved.
1008 @.TFM {\rm files}@>
1009
1010 The character-width data appears also in other files (e.g., in \.{GF} files
1011 that specify bit patterns for digitized characters);
1012 thus, it is usually possible for \.{DVI} reading programs to get by with
1013 accessing only one file per font. \.{DVItype} has a comparatively easy
1014 task in this regard, since it needs only a few words of information from
1015 each font; other \.{DVI}-to-printer programs may have to go to some pains to
1016 deal with complications that arise when a large number of large font files
1017 all need to be accessed simultaneously.
1018
1019 @ For purposes of this program, we need to know only two things about a
1020 given character |c| in a given font |f|: (1)~Is |c| a legal character
1021 in~|f|? (2)~If so, what is the width of |c|? We also need to know the
1022 symbolic name of each font, so it can be printed out, and we need to know
1023 the approximate size of inter-word spaces in each font.
1024
1025 The answers to these questions appear implicitly in the following data
1026 structures. The current number of known fonts is |nf|. Each known font has
1027 an internal number |f|, where |0<=f<nf|; the external number of this font,
1028 i.e., its font identification number in the \.{DVI} file, is
1029 |font_num[f]|, and the external name of this font is the string that
1030 occupies positions |font_name[f]| through |font_name[f+1]-1| of the array
1031 |names|. The latter array consists of |ASCII_code| characters, and
1032 |font_name[nf]| is its first unoccupied position. A horizontal motion
1033 in the range |-4*font_space[f]<h<font_space[f]|
1034 will be treated as a `kern' that is not
1035 indicated in the printouts that \.{DVItype} produces between brackets. The
1036 legal characters run from |font_bc[f]| to |font_ec[f]|, inclusive; more
1037 precisely, a given character |c| is valid in font |f| if and only if
1038 |font_bc[f]<=c<=font_ec[f]| and |char_width(f)(c)<>invalid_width|.
1039 Finally, |char_width(f)(c)=width[width_base[f]+c]|, and |width_ptr| is the
1040 first unused position of the |width| array.
1041
1042 @d char_width_end(#)==#]
1043 @d char_width(#)==width[width_base[#]+char_width_end
1044 @d invalid_width==@'17777777777
1045 @d invalid_font==max_fonts
1046
1047 @<Glob...@>=
1048 @!font_num:array [0..max_fonts] of integer; {external font numbers}
1049 @!font_name:array [0..max_fonts] of 1..name_size; {starting positions
1050 of external font names}
1051 @!names:array [1..name_size] of ASCII_code; {characters of names}
1052 @!font_check_sum:array [0..max_fonts] of integer; {check sums}
1053 @!font_scaled_size:array [0..max_fonts] of integer; {scale factors}
1054 @!font_design_size:array [0..max_fonts] of integer; {design sizes}
1055 @!font_space:array [0..max_fonts] of integer; {boundary between ``small''
1056 and ``large'' spaces}
1057 @!font_bc:array [0..max_fonts] of integer; {beginning characters in fonts}
1058 @!font_ec:array [0..max_fonts] of integer; {ending characters in fonts}
1059 @!width_base:array [0..max_fonts] of integer; {index into |width| table}
1060 @!width:array [0..max_widths] of integer; {character widths, in \.{DVI} units}
1061 @!nf:0..max_fonts; {the number of known fonts}
1062 @!width_ptr:0..max_widths; {the number of known character widths}
1063
1064 @ @<Set init...@>=
1065 nf:=0; width_ptr:=0; font_name[0]:=1;@/
1066 font_space[invalid_font]:=0; {for |out_space| and |out_vmove|}
1067 font_bc[invalid_font]:=1; font_ec[invalid_font]:=0;
1068
1069 @ It is, of course, a simple matter to print the name of a given font.
1070
1071 @p procedure print_font(@!f:integer); {|f| is an internal font number}
1072 var k:0..name_size; {index into |names|}
1073 begin if f=invalid_font then print('UNDEFINED!')
1074 @.UNDEFINED@>
1075 else begin for k:=font_name[f] to font_name[f+1]-1 do
1076 print(xchr[names[k]]);
1077 end;
1078 end;
1079
1080 @ An auxiliary array |in_width| is used to hold the widths as they are
1081 input. The global variables |tfm_check_sum| and |tfm_design_size| are
1082 set to the check sum and design size that
1083 appear in the current \.{TFM} file.
1084
1085 @<Glob...@>=
1086 @!in_width:array[0..65535] of integer; {\.{TFM} width data in \.{DVI} units}
1087 @!tfm_check_sum:integer; {check sum found in |tfm_file|}
1088 @!tfm_design_size:integer; {design size found in |tfm_file|, in \.{DVI} units}
1089 @!tfm_conv:real; {\.{DVI} units per absolute \.{TFM} unit}
1090
1091 @ Here is a procedure that absorbs the necessary information from a
1092 \.{TFM} file, assuming that the file has just been successfully reset
1093 so that we are ready to read its first byte. (A complete description of
1094 \.{TFM} file format appears in the documentation of \.{TFtoPL} and will
1095 not be repeated here.) The procedure does not check the \.{TFM} file
1096 for validity, nor does it give explicit information about what is
1097 wrong with a \.{TFM} file that proves to be invalid; \.{DVI}-reading
1098 programs need not do this, since \.{TFM} files are almost always valid,
1099 and since the \.{TFtoPL} utility program has been specifically designed
1100 to diagnose \.{TFM} errors. The procedure simply returns |false| if it
1101 detects anything amiss in the \.{TFM} data.
1102
1103 There is a parameter, |z|, which represents the scaling factor being
1104 used to compute the font dimensions; it must be in the range $0<z<2^{27}$.
1105
1106 @p function in_TFM(@!z:integer):boolean; {input \.{TFM} data or return |false|}
1107 label 9997, {go here when the format is bad}
1108 9998, {go here when the information cannot be loaded}
1109 9999; {go here to exit}
1110 var k:integer; {index for loops}
1111 @!lh:integer; {length of the header data, in four-byte words}
1112 @!nw:integer; {number of words in the width table}
1113 @!wp:0..max_widths; {new value of |width_ptr| after successful input}
1114 @!alpha,@!beta:integer; {quantities used in the scaling computation}
1115 @!nco:integer; {offset to get to the characters}
1116 @!extra_words:integer; {no of extra words in |char_info| words}
1117 @!ofm_level:integer; {TFM, OFM level 0 or 1}
1118 @!j,kprime:integer; {extra counters}
1119 begin @<Read past the header data; |goto 9997| if there is a problem@>;
1120 @<Store character-width indices at the end of the |width| table@>;
1121 @<Read and convert the width values, setting up the |in_width| table@>;
1122 @<Move the widths from |in_width| to |width|, and append |pixel_width| values@>;
1123 width_ptr:=wp; in_TFM:=true; goto 9999;
1124 9997: print_ln('---not loaded, font metric file is bad');
1125 @.TFM file is bad@>
1126 9998: in_TFM:=false;
1127 9999: end;
1128
1129 @ @<Read past the header...@>=
1130 read_tfm_word;
1131 if (b0*256+b1)<>0 then begin {TFM file}
1132 ofm_level:=-1;
1133 lh:=b2*256+b3;
1134 read_tfm_word; font_bc[nf]:=b0*256+b1; font_ec[nf]:=b2*256+b3;
1135 if font_ec[nf]<font_bc[nf] then font_bc[nf]:=font_ec[nf]+1;
1136 if width_ptr+font_ec[nf]-font_bc[nf]+1>max_widths then
1137 begin print_ln('---not loaded, ODVItype needs larger width table');
1138 @.ODVItype needs larger...@>
1139 goto 9998;
1140 end;
1141 wp:=width_ptr+font_ec[nf]-font_bc[nf]+1;
1142 read_tfm_word; nw:=b0*256+b1;
1143 if (nw=0)or(nw>256) then goto 9997;
1144 for k:=1 to 3+lh do
1145 begin if eof(tfm_file) then goto 9997;
1146 read_tfm_word;
1147 if k=4 then
1148 if b0<128 then tfm_check_sum:=((b0*256+b1)*256+b2)*256+b3
1149 else tfm_check_sum:=(((b0-256)*256+b1)*256+b2)*256+b3
1150 else if k=5 then
1151 if b0<128 then
1152 tfm_design_size:=round(tfm_conv*(((b0*256+b1)*256+b2)*256+b3))
1153 else goto 9997;
1154 end;
1155 end
1156 else begin
1157 ofm_level:=b2*256+b3;
1158 read_tfm_word;
1159 read_tfm_word; lh:=((b0*256+b1)*256+b2)*256+b3;
1160 read_tfm_word; font_bc[nf]:=((b0*256+b1)*256+b2)*256+b3;
1161 read_tfm_word; font_ec[nf]:=((b0*256+b1)*256+b2)*256+b3;
1162 if font_ec[nf]<font_bc[nf] then font_bc[nf]:=font_ec[nf]+1;
1163 if width_ptr+font_ec[nf]-font_bc[nf]+1>max_widths then
1164 begin print_ln('---not loaded, ODVItype needs larger width table');
1165 @.ODVItype needs larger...@>
1166 goto 9998;
1167 end;
1168 wp:=width_ptr+font_ec[nf]-font_bc[nf]+1;
1169 read_tfm_word; nw:=((b0*256+b1)*256+b2)*256+b3;
1170 if (nw=0)or(nw>65536) then goto 9997;
1171 for k:=1 to 8 do
1172 begin if eof(tfm_file) then goto 9997;
1173 read_tfm_word;
1174 end;
1175 if ofm_level=1 then begin
1176 read_tfm_word; nco:=((b0*256+b1)*256+b2)*256+b3;
1177 read_tfm_word;
1178 read_tfm_word; extra_words:=(((b0*256+b1)*256+b2)*256+b3) div 2;
1179 for k:=1 to 12 do
1180 begin if eof(tfm_file) then goto 9997;
1181 read_tfm_word;
1182 end;
1183 end;
1184 for k:=1 to lh do
1185 begin if eof(tfm_file) then goto 9997;
1186 read_tfm_word;
1187 if k=1 then
1188 if b0<128 then tfm_check_sum:=((b0*256+b1)*256+b2)*256+b3
1189 else tfm_check_sum:=(((b0-256)*256+b1)*256+b2)*256+b3
1190 else if k=2 then
1191 if b0<128 then
1192 tfm_design_size:=round(tfm_conv*(((b0*256+b1)*256+b2)*256+b3))
1193 else goto 9997;
1194 end;
1195 if ofm_level=1 then
1196 for k:=1 to (nco-29-lh) do
1197 read_tfm_word;
1198 end
1199
1200 @ @<Store character-width indices...@>=
1201 case ofm_level of
1202 -1:begin
1203 if wp>0 then for k:=width_ptr to wp-1 do
1204 begin read_tfm_word;
1205 if b0>nw then goto 9997;
1206 width[k]:=b0;
1207 end;
1208 end;
1209 0:begin
1210 if wp>0 then for k:=width_ptr to wp-1 do
1211 begin read_tfm_word;
1212 if (b0*256+b1)>nw then goto 9997;
1213 width[k]:=(b0*256+b1);
1214 read_tfm_word;
1215 end;
1216 end;
1217 1:begin
1218 if wp>0 then begin
1219 k:=width_ptr;
1220 while k<wp do begin
1221 read_tfm_word;
1222 if (b0*256+b1)>nw then goto 9997;
1223 width[k]:=(b0*256+b1);
1224 read_tfm_word; read_tfm_word;
1225 kprime:=k+(b0*256+b1);
1226 for j:=k+1 to kprime do
1227 width[j]:=width[k];
1228 k:=kprime+1;
1229 for j:=1 to extra_words do
1230 read_tfm_word;
1231 end
1232 end
1233 end;
1234 end;
1235
1236
1237 @ The most important part of |in_TFM| is the width computation, which
1238 involves multiplying the relative widths in the \.{TFM} file by the
1239 scaling factor in the \.{DVI} file. This fixed-point multiplication
1240 must be done with precisely the same accuracy by all \.{DVI}-reading programs,
1241 in order to validate the assumptions made by \.{DVI}-writing programs
1242 like \TeX82.
1243
1244 Let us therefore summarize what needs to be done. Each width in a \.{TFM}
1245 file appears as a four-byte quantity called a |fix_word|. A |fix_word|
1246 whose respective bytes are $(a,b,c,d)$ represents the number
1247 $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
1248 b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
1249 -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
1250 (No other choices of $a$ are allowed, since the magnitude of a \.{TFM}
1251 dimension must be less than 16.) We want to multiply this quantity by the
1252 integer~|z|, which is known to be less than $2^{27}$.
1253 If $|z|<2^{23}$, the individual multiplications $b\cdot z$, $c\cdot z$,
1254 $d\cdot z$ cannot overflow; otherwise we will divide |z| by 2, 4, 8, or
1255 16, to obtain a multiplier less than $2^{23}$, and we can compensate for
1256 this later. If |z| has thereby been replaced by $|z|^\prime=|z|/2^e$, let
1257 $\beta=2^{4-e}$; we shall compute
1258 $$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$ if $a=0$,
1259 or the same quantity minus $\alpha=2^{4+e}z^\prime$ if $a=255$.
1260 This calculation must be
1261 done exactly, for the reasons stated above; the following program does the
1262 job in a system-independent way, assuming that arithmetic is exact on
1263 numbers less than $2^{31}$ in magnitude.
1264
1265 @<Read and convert the width values...@>=
1266 @<Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$@>;
1267 for k:=0 to nw-1 do
1268 begin read_tfm_word;
1269 in_width[k]:=(((((b3*z)div@'400)+(b2*z))div@'400)+(b1*z))div beta;
1270 if b0>0 then if b0<255 then goto 9997
1271 else in_width[k]:=in_width[k]-alpha;
1272 end
1273
1274 @ @<Replace |z|...@>=
1275 begin alpha:=16;
1276 while z>=@'40000000 do
1277 begin z:=z div 2; alpha:=alpha+alpha;
1278 end;
1279 beta:=256 div alpha; alpha:=alpha*z;
1280 end
1281
1282 @ A \.{DVI}-reading program usually works with font files instead of
1283 \.{TFM} files, so \.{DVItype} is atypical in that respect. Font files
1284 should, however, contain exactly the same character width data that is
1285 found in the corresponding \.{TFM}s; check sums are used to help
1286 ensure this. In addition, font files usually also contain the widths of
1287 characters in pixels, since the device-independent character widths of
1288 \.{TFM} files are generally not perfect multiples of pixels.
1289
1290 The |pixel_width| array contains this information; when |width[k]| is the
1291 device-independent width of some character in \.{DVI} units, |pixel_width[k]|
1292 is the corresponding width of that character in an actual font.
1293 The macro |char_pixel_width| is set up to be analogous to |char_width|.
1294
1295 @d char_pixel_width(#)==pixel_width[width_base[#]+char_width_end
1296
1297 @<Glob...@>=
1298 @!pixel_width:array[0..max_widths] of integer; {actual character widths,
1299 in pixels}
1300 @!conv:real; {converts \.{DVI} units to pixels}
1301 @!true_conv:real; {converts unmagnified \.{DVI} units to pixels}
1302 @!numerator,@!denominator:integer; {stated conversion ratio}
1303 @!mag:integer; {magnification factor times 1000}
1304
1305 @ The following code computes pixel widths by simply rounding the \.{TFM}
1306 widths to the nearest integer number of pixels, based on the conversion factor
1307 |conv| that converts \.{DVI} units to pixels. However, such a simple
1308 formula will not be valid for all fonts, and it will often give results that
1309 are off by $\pm1$ when a low-resolution font has been carefully
1310 hand-fitted. For example, a font designer often wants to make the letter `m'
1311 a pixel wider or narrower in order to make the font appear more consistent.
1312 \.{DVI}-to-printer programs should therefore input the correct pixel width
1313 information from font files whenever there is a chance that it may differ.
1314 A warning message may also be desirable in the case that at least one character
1315 is found whose pixel width differs from |conv*width| by more than a full pixel.
1316 @^system dependencies@>
1317
1318 @d pixel_round(#)==round(conv*(#))
1319
1320 @<Move the widths from |in_width| to |width|, and append |pixel_width| values@>=
1321 if in_width[0]<>0 then goto 9997; {the first width should be zero}
1322 width_base[nf]:=width_ptr-font_bc[nf];
1323 if wp>0 then for k:=width_ptr to wp-1 do
1324 if width[k]=0 then
1325 begin width[k]:=invalid_width; pixel_width[k]:=0;
1326 end
1327 else begin width[k]:=in_width[width[k]];
1328 pixel_width[k]:=pixel_round(width[k]);
1329 end
1330
1331 @* Optional modes of output.
1332 \.{DVItype} will print different quantities of information based on some
1333 options that the user must specify: The |out_mode| level is set to one of
1334 five values (|errors_only|, |terse|, |mnemonics_only|,
1335 |verbose|, |the_works|), giving
1336 different degrees of output; and the typeout can be confined to a
1337 restricted subset of the pages by specifying the desired starting page and
1338 the maximum number of pages. Furthermore there is an option to specify the
1339 resolution of an assumed discrete output device, so that pixel-oriented
1340 calculations will be shown; and there is an option to override the
1341 magnification factor that is stated in the \.{DVI} file.
1342
1343 The starting page is specified by giving a sequence of 1 to 10 numbers or
1344 asterisks separated by dots. For example, the specification `\.{1.*.-5}'
1345 can be used to refer to a page output by \TeX\ when $\.{\\count0}=1$
1346 and $\.{\\count2}=-5$. (Recall that |bop| commands in a \.{DVI} file
1347 are followed by ten `count' values.) An asterisk matches any number,
1348 so the `\.*' in `\.{1.*.-5}' means that \.{\\count1} is ignored when
1349 specifying the first page. If several pages match the given specification,
1350 \.{DVItype} will begin with the earliest such page in the file. The
1351 default specification `\.*' (which matches all pages) therefore denotes
1352 the page at the beginning of the file.
1353
1354 When \.{DVItype} begins, it engages the user in a brief dialog so that the
1355 options will be specified. This part of \.{DVItype} requires nonstandard
1356 \PASCAL\ constructions to handle the online interaction; so it may be
1357 preferable in some cases to omit the dialog and simply to stick to the
1358 default options (|out_mode=the_works|, starting page `\.*',
1359 |max_pages=1000000|, |resolution=300.0|, |new_mag=0|). On other hand, the
1360 system-dependent routines that are needed are not complicated, so it will
1361 not be terribly difficult to introduce them.
1362 @^system dependencies@>
1363
1364 @d errors_only=0 {value of |out_mode| when minimal printing occurs}
1365 @d terse=1 {value of |out_mode| for abbreviated output}
1366 @d mnemonics_only=2 {value of |out_mode| for medium-quantity output}
1367 @d verbose=3 {value of |out_mode| for detailed tracing}
1368 @d the_works=4 {|verbose|, plus check of postamble if |random_reading|}
1369
1370 @<Glob...@>=
1371 @!out_mode:errors_only..the_works; {controls the amount of output}
1372 @!max_pages:integer; {at most this many |bop..eop| pages will be printed}
1373 @!resolution:real; {pixels per inch}
1374 @!new_mag:integer; {if positive, overrides the postamble's magnification}
1375
1376 @ The starting page specification is recorded in two global arrays called
1377 |start_count| and |start_there|. For example, `\.{1.*.-5}' is represented
1378 by |start_there[0]=true|, |start_count[0]=1|, |start_there[1]=false|,
1379 |start_there[2]=true|, |start_count[2]=-5|.
1380 We also set |start_vals=2|, to indicate that count 2 was the last one
1381 mentioned. The other values of |start_count| and |start_there| are not
1382 important, in this example.
1383
1384 @<Glob...@>=
1385 @!start_count:array[0..9] of integer; {count values to select starting page}
1386 @!start_there:array[0..9] of boolean; {is the |start_count| value relevant?}
1387 @!start_vals:0..9; {the last count considered significant}
1388 @!count:array[0..9] of integer; {the count values on the current page}
1389
1390 @ @<Set init...@>=
1391 out_mode:=the_works; max_pages:=1000000; start_vals:=0; start_there[0]:=false;
1392
1393 @ Here is a simple subroutine that tests if the current page might be the
1394 starting page.
1395
1396 @p function start_match:boolean; {does |count| match the starting spec?}
1397 var k:0..9; {loop index}
1398 @!match:boolean; {does everything match so far?}
1399 begin match:=true;
1400 for k:=0 to start_vals do
1401 if start_there[k]and(start_count[k]<>count[k]) then match:=false;
1402 start_match:=match;
1403 end;
1404
1405 @ The |input_ln| routine waits for the user to type a line at his or her
1406 terminal; then it puts ASCII-code equivalents for the characters on that line
1407 into the |buffer| array. The |term_in| file is used for terminal input,
1408 and |term_out| for terminal output.
1409 @^system dependencies@>
1410
1411 @<Glob...@>=
1412 @!buffer:array[0..terminal_line_length] of ASCII_code;
1413 @!term_in:text_file; {the terminal, considered as an input file}
1414 @!term_out:text_file; {the terminal, considered as an output file}
1415
1416 @ Since the terminal is being used for both input and output, some systems
1417 need a special routine to make sure that the user can see a prompt message
1418 before waiting for input based on that message. (Otherwise the message
1419 may just be sitting in a hidden buffer somewhere, and the user will have
1420 no idea what the program is waiting for.) We shall invoke a system-dependent
1421 subroutine |update_terminal| in order to avoid this problem.
1422 @^system dependencies@>
1423
1424 @d update_terminal == break(term_out) {empty the terminal output buffer}
1425
1426 @ During the dialog, \.{DVItype} will treat the first blank space in a
1427 line as the end of that line. Therefore |input_ln| makes sure that there
1428 is always at least one blank space in |buffer|.
1429 @^system dependencies@>
1430
1431 @p procedure input_ln; {inputs a line from the terminal}
1432 var k:0..terminal_line_length;
1433 begin update_terminal; reset(term_in);
1434 if eoln(term_in) then read_ln(term_in);
1435 k:=0;
1436 while (k<terminal_line_length)and not eoln(term_in) do
1437 begin buffer[k]:=xord[term_in^]; incr(k); get(term_in);
1438 end;
1439 buffer[k]:=" ";
1440 end;
1441
1442 @ The global variable |buf_ptr| is used while scanning each line of input;
1443 it points to the first unread character in |buffer|.
1444
1445 @<Glob...@>=
1446 @!buf_ptr:0..terminal_line_length; {the number of characters read}
1447
1448 @ Here is a routine that scans a (possibly signed) integer and computes
1449 the decimal value. If no decimal integer starts at |buf_ptr|, the
1450 value 0 is returned. The integer should be less than $2^{31}$ in
1451 absolute value.
1452
1453 @p function get_integer:integer;
1454 var x:integer; {accumulates the value}
1455 @!negative:boolean; {should the value be negated?}
1456 begin if buffer[buf_ptr]="-" then
1457 begin negative:=true; incr(buf_ptr);
1458 end
1459 else negative:=false;
1460 x:=0;
1461 while (buffer[buf_ptr]>="0")and(buffer[buf_ptr]<="9") do
1462 begin x:=10*x+buffer[buf_ptr]-"0"; incr(buf_ptr);
1463 end;
1464 if negative then get_integer:=-x @+ else get_integer:=x;
1465 end;
1466
1467 @ The selected options are put into global variables by the |dialog|
1468 procedure, which is called just as \.{DVItype} begins.
1469 @^system dependencies@>
1470
1471 @p procedure dialog;
1472 label 1,2,3,4,5;
1473 var k:integer; {loop variable}
1474 begin rewrite(term_out); {prepare the terminal for output}
1475 write_ln(term_out,banner);
1476 @<Determine the desired |out_mode|@>;
1477 @<Determine the desired |start_count| values@>;
1478 @<Determine the desired |max_pages|@>;
1479 @<Determine the desired |resolution|@>;
1480 @<Determine the desired |new_mag|@>;
1481 @<Print all the selected options@>;
1482 end;
1483
1484 @ @<Determine the desired |out_mode|@>=
1485 1: write(term_out,'Output level (default=4, ? for help): ');
1486 out_mode:=the_works; input_ln;
1487 if buffer[0]<>" " then
1488 if (buffer[0]>="0")and(buffer[0]<="4") then out_mode:=buffer[0]-"0"
1489 else begin write(term_out,'Type 4 for complete listing,');
1490 write(term_out,' 0 for errors and fonts only,');
1491 write_ln(term_out,' 1 or 2 or 3 for something in between.');
1492 goto 1;
1493 end
1494
1495 @ @<Determine the desired |start...@>=
1496 2: write(term_out,'Starting page (default=*): ');
1497 start_vals:=0; start_there[0]:=false;
1498 input_ln; buf_ptr:=0; k:=0;
1499 if buffer[0]<>" " then
1500 repeat if buffer[buf_ptr]="*" then
1501 begin start_there[k]:=false; incr(buf_ptr);
1502 end
1503 else begin start_there[k]:=true; start_count[k]:=get_integer;
1504 end;
1505 if (k<9)and(buffer[buf_ptr]=".") then
1506 begin incr(k); incr(buf_ptr);
1507 end
1508 else if buffer[buf_ptr]=" " then start_vals:=k
1509 else begin write(term_out,'Type, e.g., 1.*.-5 to specify the ');
1510 write_ln(term_out,'first page with \count0=1, \count2=-5.');
1511 goto 2;
1512 end;
1513 until start_vals=k
1514
1515 @ @<Determine the desired |max_pages|@>=
1516 3: write(term_out,'Maximum number of pages (default=1000000): ');
1517 max_pages:=1000000; input_ln; buf_ptr:=0;
1518 if buffer[0]<>" " then
1519 begin max_pages:=get_integer;
1520 if max_pages<=0 then
1521 begin write_ln(term_out,'Please type a positive number.');
1522 goto 3;
1523 end;
1524 end
1525
1526 @ @<Determine the desired |resolution|@>=
1527 4: write(term_out,'Assumed device resolution');
1528 write(term_out,' in pixels per inch (default=300/1): ');
1529 resolution:=300.0; input_ln; buf_ptr:=0;
1530 if buffer[0]<>" " then
1531 begin k:=get_integer;
1532 if (k>0)and(buffer[buf_ptr]="/")and
1533 (buffer[buf_ptr+1]>"0")and(buffer[buf_ptr+1]<="9") then
1534 begin incr(buf_ptr); resolution:=k/get_integer;
1535 end
1536 else begin write(term_out,'Type a ratio of positive integers;');
1537 write_ln(term_out,' (1 pixel per mm would be 254/10).');
1538 goto 4;
1539 end;
1540 end
1541
1542 @ @<Determine the desired |new_mag|@>=
1543 5: write(term_out,'New magnification (default=0 to keep the old one): ');
1544 new_mag:=0; input_ln; buf_ptr:=0;
1545 if buffer[0]<>" " then
1546 if (buffer[0]>="0")and(buffer[0]<="9") then new_mag:=get_integer
1547 else begin write(term_out,'Type a positive integer to override ');
1548 write_ln(term_out,'the magnification in the DVI file.');
1549 goto 5;
1550 end
1551
1552 @ After the dialog is over, we print the options so that the user
1553 can see what \.{DVItype} thought was specified.
1554
1555 @<Print all the selected options@>=
1556 print_ln('Options selected:');
1557 @.Options selected@>
1558 print(' Starting page = ');
1559 for k:=0 to start_vals do
1560 begin if start_there[k] then print(start_count[k]:1)
1561 else print('*');
1562 if k<start_vals then print('.')
1563 else print_ln(' ');
1564 end;
1565 print_ln(' Maximum number of pages = ',max_pages:1);
1566 print(' Output level = ',out_mode:1);
1567 case out_mode of
1568 errors_only: print_ln(' (showing bops, fonts, and error messages only)');
1569 terse: print_ln(' (terse)');
1570 mnemonics_only: print_ln(' (mnemonics)');
1571 verbose: print_ln(' (verbose)');
1572 the_works: if random_reading then print_ln(' (the works)')
1573 else begin out_mode:=verbose;
1574 print_ln(' (the works: same as level 3 in this DVItype)');
1575 end;
1576 end;@/
1577 print_ln(' Resolution = ',resolution:12:8,' pixels per inch');
1578 if new_mag>0 then print_ln(' New magnification factor = ',new_mag/1000:8:3)
1579
1580 @* Defining fonts.
1581 When |out_mode=the_works|, \.{DVItype} reads the postamble first and loads
1582 all of the fonts defined there; then it processes the pages. In this
1583 case, a \\{fnt\_def} command should match a previous definition if and only
1584 if the \\{fnt\_def} being processed is not in the postamble. But if
1585 |out_mode<the_works|, \.{DVItype} reads the pages first and the postamble
1586 last, so the conventions are reversed: a \\{fnt\_def} should match a previous
1587 \\{fnt\_def} if and only if the current one is a part of the postamble.
1588
1589 A global variable |in_postamble| is provided to tell whether we are
1590 processing the postamble or not.
1591
1592 @<Glob...@>=
1593 @!in_postamble:boolean; {are we reading the postamble?}
1594
1595 @ @<Set init...@>=
1596 in_postamble:=false;
1597
1598 @ The following subroutine does the necessary things when a \\{fnt\_def}
1599 command is being processed.
1600
1601 @p procedure define_font(@!e:integer); {|e| is an external font number}
1602 var f:0..max_fonts;
1603 @!p:integer; {length of the area/directory spec}
1604 @!n:integer; {length of the font name proper}
1605 @!c,@!q,@!d,@!m:integer; {check sum, scaled size, design size, magnification}
1606 @!r:0..name_length; {index into |cur_name|}
1607 @!j,@!k:0..name_size; {indices into |names|}
1608 @!mismatch:boolean; {do names disagree?}
1609 begin if nf=max_fonts then abort('DVItype capacity exceeded (max fonts=',
1610 max_fonts:1,')!');
1611 @.DVItype capacity exceeded...@>
1612 font_num[nf]:=e; f:=0;
1613 while font_num[f]<>e do incr(f);
1614 @<Read the font parameters into position for font |nf|, and
1615 print the font name@>;
1616 if ((out_mode=the_works)and in_postamble)or@|
1617 ((out_mode<the_works)and not in_postamble) then
1618 begin if f<nf then print_ln('---this font was already defined!');
1619 @.this font was already defined@>
1620 end
1621 else begin if f=nf then print_ln('---this font wasn''t loaded before!');
1622 @.this font wasn't loaded before@>
1623 end;
1624 if f=nf then @<Load the new font, unless there are problems@>
1625 else @<Check that the current font definition matches the old one@>;
1626 end;
1627
1628 @ @<Check that the current...@>=
1629 begin if font_check_sum[f]<>c then
1630 print_ln('---check sum doesn''t match previous definition!');
1631 @.check sum doesn't match@>
1632 if font_scaled_size[f]<>q then
1633 print_ln('---scaled size doesn''t match previous definition!');
1634 @.scaled size doesn't match@>
1635 if font_design_size[f]<>d then
1636 print_ln('---design size doesn''t match previous definition!');
1637 @.design size doesn't match@>
1638 j:=font_name[f]; k:=font_name[nf];
1639 if font_name[f+1]-j<>font_name[nf+1]-k then mismatch:=true
1640 else begin mismatch:=false;
1641 while j<font_name[f+1] do
1642 begin if names[j]<>names[k] then mismatch:=true;
1643 incr(j); incr(k);
1644 end;
1645 end;
1646 if mismatch then print_ln('---font name doesn''t match previous definition!');
1647 @.font name doesn't match@>
1648 end
1649
1650 @ @<Read the font parameters into position for font |nf|...@>=
1651 c:=signed_quad; font_check_sum[nf]:=c;@/
1652 q:=signed_quad; font_scaled_size[nf]:=q;@/
1653 d:=signed_quad; font_design_size[nf]:=d;@/
1654 if (q<=0) or (d<=0) then m:=1000
1655 else m:=round((1000.0*conv*q)/(true_conv*d));
1656 p:=get_byte; n:=get_byte;
1657 if font_name[nf]+n+p>name_size then
1658 abort('DVItype capacity exceeded (name size=',name_size:1,')!');
1659 @.DVItype capacity exceeded...@>
1660 font_name[nf+1]:=font_name[nf]+n+p;
1661 if showing then print(': ')
1662 {when |showing| is true, the font number has already been printed}
1663 else print('Font ',e:1,': ');
1664 if n+p=0 then print('null font name!')
1665 @.null font name@>
1666 else for k:=font_name[nf] to font_name[nf+1]-1 do names[k]:=get_byte;
1667 print_font(nf);
1668 if not showing then if m<>1000 then print(' scaled ',m:1)
1669 @.scaled@>
1670
1671 @ @<Load the new font, unless there are problems@>=
1672 begin @<Move font name into the |cur_name| string@>;
1673 open_tfm_file;
1674 if eof(tfm_file) then
1675 print('---not loaded, TFM file can''t be opened!')
1676 @.TFM file can\'t be opened@>
1677 else begin if (q<=0)or(q>=@'1000000000) then
1678 print('---not loaded, bad scale (',q:1,')!')
1679 @.bad scale@>
1680 else if (d<=0)or(d>=@'1000000000) then
1681 print('---not loaded, bad design size (',d:1,')!')
1682 @.bad design size@>
1683 else if in_TFM(q) then @<Finish loading the new font info@>;
1684 end;
1685 if out_mode=errors_only then print_ln(' ');
1686 end
1687
1688 @ @<Finish loading...@>=
1689 begin font_space[nf]:=q div 6; {this is a 3-unit ``thin space''}
1690 if (c<>0)and(tfm_check_sum<>0)and(c<>tfm_check_sum) then
1691 begin print_ln('---beware: check sums do not agree!');
1692 @.beware: check sums do not agree@>
1693 @.check sums do not agree@>
1694 print_ln(' (',c:1,' vs. ',tfm_check_sum:1,')');
1695 print(' ');
1696 end;
1697 if abs(tfm_design_size-d)>2 then
1698 begin print_ln('---beware: design sizes do not agree!');
1699 @.beware: design sizes do not agree@>
1700 @.design sizes do not agree@>
1701 print_ln(' (',d:1,' vs. ',tfm_design_size:1,')');
1702 print(' ');
1703 end;
1704 print('---loaded at size ',q:1,' DVI units');
1705 d:=round((100.0*conv*q)/(true_conv*d));
1706 if d<>100 then
1707 begin print_ln(' '); print(' (this font is magnified ',d:1,'%)');
1708 end;
1709 @.this font is magnified@>
1710 incr(nf); {now the new font is officially present}
1711 end
1712
1713 @ If |p=0|, i.e., if no font directory has been specified, \.{DVItype}
1714 is supposed to use the default font directory, which is a
1715 system-dependent place where the standard fonts are kept.
1716 The string variable |default_directory| contains the name of this area.
1717 @^system dependencies@>
1718
1719 @d default_directory_name=='TeXfonts:' {change this to the correct name}
1720 @d default_directory_name_length=9 {change this to the correct length}
1721
1722 @<Glob...@>=
1723 @!default_directory:packed array[1..default_directory_name_length] of char;
1724
1725 @ @<Set init...@>=
1726 default_directory:=default_directory_name;
1727
1728 @ The string |cur_name| is supposed to be set to the external name of the
1729 \.{TFM} file for the current font. This usually means that we need to
1730 prepend the name of the default directory, and
1731 to append the suffix `\.{.TFM}'. Furthermore, we change lower case letters
1732 to upper case, since |cur_name| is a \PASCAL\ string.
1733 @^system dependencies@>
1734
1735 @<Move font name into the |cur_name| string@>=
1736 for k:=1 to name_length do cur_name[k]:=' ';
1737 if p=0 then
1738 begin for k:=1 to default_directory_name_length do
1739 cur_name[k]:=default_directory[k];
1740 r:=default_directory_name_length;
1741 end
1742 else r:=0;
1743 for k:=font_name[nf] to font_name[nf+1]-1 do
1744 begin incr(r);
1745 if r+4>name_length then
1746 abort('ODVItype capacity exceeded (max font name length=',
1747 name_length:1,')!');
1748 @.ODVItype capacity exceeded...@>
1749 if (names[k]>="a")and(names[k]<="z") then
1750 cur_name[r]:=xchr[names[k]-@'40]
1751 else cur_name[r]:=xchr[names[k]];
1752 end;
1753 cur_name[r+1]:='.'; cur_name[r+2]:='T'; cur_name[r+3]:='F'; cur_name[r+4]:='M'
1754
1755 @* Low level output routines.
1756 Simple text in the \.{DVI} file is saved in a buffer until |line_length-2|
1757 characters have accumulated, or until some non-simple \.{DVI} operation
1758 occurs. Then the accumulated text is printed on a line, surrounded by
1759 brackets. The global variable |text_ptr| keeps track of the number of
1760 characters currently in the buffer.
1761
1762 @<Glob...@>=
1763 @!text_ptr:0..line_length; {the number of characters in |text_buf|}
1764 @!text_buf:array[1..line_length] of ASCII_code; {saved characters}
1765
1766 @ @<Set init...@>=
1767 text_ptr:=0;
1768
1769 @ The |flush_text| procedure will empty the buffer if there is something in it.
1770
1771 @p procedure flush_text;
1772 var k:0..line_length; {index into |text_buf|}
1773 begin if text_ptr>0 then
1774 begin if out_mode>errors_only then
1775 begin print('[');
1776 for k:=1 to text_ptr do print(xchr[text_buf[k]]);
1777 print_ln(']');
1778 end;
1779 text_ptr:=0;
1780 end;
1781 end;
1782
1783 @ And the |out_text| procedure puts something in it.
1784
1785 @p procedure out_text(c:ASCII_code);
1786 begin if text_ptr=line_length-2 then flush_text;
1787 incr(text_ptr); text_buf[text_ptr]:=c;
1788 end;
1789
1790 @* Translation to symbolic form.
1791 The main work of \.{DVItype} is accomplished by the |do_page| procedure,
1792 which produces the output for an entire page, assuming that the |bop|
1793 command for that page has already been processed. This procedure is
1794 essentially an interpretive routine that reads and acts on the \.{DVI}
1795 commands.
1796
1797 @ The definition of \.{DVI} files refers to six registers,
1798 $(h,v,w,x,y,z)$, which hold integer values in \.{DVI} units. In practice,
1799 we also need registers |hh| and |vv|, the pixel analogs of $h$ and $v$,
1800 since it is not always true that |hh=pixel_round(h)| or
1801 |vv=pixel_round(v)|.
1802
1803 The stack of $(h,v,w,x,y,z)$ values is represented by eight arrays
1804 called |hstack|, \dots, |zstack|, |hhstack|, and |vvstack|.
1805
1806 @<Glob...@>=
1807 @!h,@!v,@!w,@!x,@!y,@!z,@!hh,@!vv:integer; {current state values}
1808 @!hstack,@!vstack,@!wstack,@!xstack,@!ystack,@!zstack:
1809 array [0..stack_size] of integer; {pushed down values in \.{DVI} units}
1810 @!hhstack,@!vvstack:
1811 array [0..stack_size] of integer; {pushed down values in pixels}
1812
1813 @ Three characteristics of the pages (their |max_v|, |max_h|, and
1814 |max_s|) are specified in the postamble, and a warning message
1815 is printed if these limits are exceeded. Actually |max_v| is set to
1816 the maximum height plus depth of a page, and |max_h| to the maximum width,
1817 for purposes of page layout. Since characters can legally be set outside
1818 of the page boundaries, it is not an error when |max_v| or |max_h| is
1819 exceeded. But |max_s| should not be exceeded.
1820
1821 The postamble also specifies the total number of pages; \.{DVItype}
1822 checks to see if this total is accurate.
1823
1824 @<Glob...@>=
1825 @!max_v:integer; {the value of |abs(v)| should probably not exceed this}
1826 @!max_h:integer; {the value of |abs(h)| should probably not exceed this}
1827 @!max_s:integer; {the stack depth should not exceed this}
1828 @!max_v_so_far,@!max_h_so_far,@!max_s_so_far:integer; {the record high levels}
1829 @!total_pages:integer; {the stated total number of pages}
1830 @!page_count:integer; {the total number of pages seen so far}
1831
1832 @ @<Set init...@>=
1833 max_v:=@'17777777777-99; max_h:=@'17777777777-99; max_s:=stack_size+1;@/
1834 max_v_so_far:=0; max_h_so_far:=0; max_s_so_far:=0; page_count:=0;
1835
1836 @ Before we get into the details of |do_page|, it is convenient to
1837 consider a simpler routine that computes the first parameter of each
1838 opcode.
1839
1840 @d four_cases(#)==#,#+1,#+2,#+3
1841 @d eight_cases(#)==four_cases(#),four_cases(#+4)
1842 @d sixteen_cases(#)==eight_cases(#),eight_cases(#+8)
1843 @d thirty_two_cases(#)==sixteen_cases(#),sixteen_cases(#+16)
1844 @d sixty_four_cases(#)==thirty_two_cases(#),thirty_two_cases(#+32)
1845
1846 @p function first_par(o:eight_bits):integer;
1847 begin case o of
1848 sixty_four_cases(set_char_0),sixty_four_cases(set_char_0+64):
1849 first_par:=o-set_char_0;
1850 set1,put1,fnt1,xxx1,fnt_def1: first_par:=get_byte;
1851 set1+1,put1+1,fnt1+1,xxx1+1,fnt_def1+1: first_par:=get_two_bytes;
1852 set1+2,put1+2,fnt1+2,xxx1+2,fnt_def1+2: first_par:=get_three_bytes;
1853 right1,w1,x1,down1,y1,z1: first_par:=signed_byte;
1854 right1+1,w1+1,x1+1,down1+1,y1+1,z1+1: first_par:=signed_pair;
1855 right1+2,w1+2,x1+2,down1+2,y1+2,z1+2: first_par:=signed_trio;
1856 set1+3,set_rule,put1+3,put_rule,right1+3,w1+3,x1+3,down1+3,y1+3,z1+3,
1857 fnt1+3,xxx1+3,fnt_def1+3: first_par:=signed_quad;
1858 nop,bop,eop,push,pop,pre,post,post_post,undefined_commands: first_par:=0;
1859 w0: first_par:=w;
1860 x0: first_par:=x;
1861 y0: first_par:=y;
1862 z0: first_par:=z;
1863 sixty_four_cases(fnt_num_0): first_par:=o-fnt_num_0;
1864 end;
1865 end;
1866
1867 @ Here is another subroutine that we need: It computes the number of
1868 pixels in the height or width of a rule. Characters and rules will line up
1869 properly if the sizes are computed precisely as specified here. (Since
1870 |conv| is computed with some floating-point roundoff error, in a
1871 machine-dependent way, format designers who are tailoring something for a
1872 particular resolution should not plan their measurements to come out to an
1873 exact integer number of pixels; they should compute things so that the
1874 rule dimensions are a little less than an integer number of pixels, e.g.,
1875 4.99 instead of 5.00.)
1876
1877 @p function rule_pixels(x:integer):integer;
1878 {computes $\lceil|conv|\cdot x\rceil$}
1879 var n:integer;
1880 begin n:=trunc(conv*x);
1881 if n<conv*x then rule_pixels:=n+1 @+ else rule_pixels:=n;
1882 end;
1883
1884 @ Strictly speaking, the |do_page| procedure is really a function with
1885 side effects, not a `\&{procedure}'\thinspace; it returns the value |false|
1886 if \.{DVItype} should be aborted because of some unusual happening. The
1887 subroutine is organized as a typical interpreter, with a multiway branch
1888 on the command code followed by |goto| statements leading to routines that
1889 finish up the activities common to different commands. We will use the
1890 following labels:
1891
1892 @d fin_set=41 {label for commands that set or put a character}
1893 @d fin_rule=42 {label for commands that set or put a rule}
1894 @d move_right=43 {label for commands that change |h|}
1895 @d move_down=44 {label for commands that change |v|}
1896 @d show_state=45 {label for commands that change |s|}
1897 @d change_font=46 {label for commands that change |cur_font|}
1898
1899 @ Some \PASCAL\ compilers severely restrict the length of procedure bodies,
1900 so we shall split |do_page| into two parts, one of which is
1901 called |special_cases|. The different parts communicate with each other
1902 via the global variables mentioned above, together with the following ones:
1903
1904 @<Glob...@>=
1905 @!s:integer; {current stack size}
1906 @!ss:integer; {stack size to print}
1907 @!cur_font:integer; {current internal font number}
1908 @!showing:boolean; {is the current command being translated in full?}
1909
1910 @ Here is the overall setup.
1911
1912 @p @t\4@>@<Declare the function called |special_cases|@>@;
1913 function do_page:boolean;
1914 label fin_set,fin_rule,move_right,show_state,done,9998,9999;
1915 var o:eight_bits; {operation code of the current command}
1916 @!p,@!q:integer; {parameters of the current command}
1917 @!a:integer; {byte number of the current command}
1918 @!hhh:integer; {|h|, rounded to the nearest pixel}
1919 begin cur_font:=invalid_font; {set current font undefined}
1920 s:=0; h:=0; v:=0; w:=0; x:=0; y:=0; z:=0; hh:=0; vv:=0;
1921 {initialize the state variables}
1922 while true do @<Translate the next command in the \.{DVI} file;
1923 |goto 9999| with |do_page=true| if it was |eop|;
1924 |goto 9998| if premature termination is needed@>;
1925 9998: print_ln('!'); do_page:=false;
1926 9999: end;
1927
1928 @ Commands are broken down into ``major'' and ``minor'' categories:
1929 A major command is always shown in full, while a minor one is
1930 put into the buffer in abbreviated form. Minor commands, which
1931 account for the bulk of most \.{DVI} files, involve horizontal spacing
1932 and the typesetting of characters in a line; these are shown in full
1933 only if |out_mode>=verbose|.
1934
1935 @d show(#)==begin flush_text; showing:=true; print(a:1,': ',#);
1936 end
1937 @d major(#)==if out_mode>errors_only then show(#)
1938 @d minor(#)==if out_mode>terse then
1939 begin showing:=true; print(a:1,': ',#);
1940 end
1941 @d error(#)==if not showing then show(#) else print(' ',#)
1942
1943 @<Translate the next command...@>=
1944 begin a:=cur_loc; showing:=false;
1945 o:=get_byte; p:=first_par(o);
1946 if eof(dvi_file) then bad_dvi('the file ended prematurely');
1947 @.the file ended prematurely@>
1948 @<Start translation of command |o| and |goto| the appropriate label to
1949 finish the job@>;
1950 fin_set: @<Finish a command that either sets or puts a character, then
1951 |goto move_right| or |done|@>;
1952 fin_rule: @<Finish a command that either sets or puts a rule, then
1953 |goto move_right| or |done|@>;
1954 move_right: @<Finish a command that sets |h:=h+q|, then |goto done|@>;
1955 show_state: @<Show the values of |ss|, |h|, |v|, |w|, |x|, |y|, |z|,
1956 |hh|, and |vv|; then |goto done|@>;
1957 done: if showing then print_ln(' ');
1958 end
1959
1960 @ The multiway switch in |first_par|, above, was organized by the length
1961 of each command; the one in |do_page| is organized by the semantics.
1962
1963 @<Start translation...@>=
1964 if o<set_char_0+128 then @<Translate a |set_char| command@>
1965 else case o of
1966 four_cases(set1): begin major('set',o-set1+1:1,' ',p:1); goto fin_set;
1967 end;
1968 four_cases(put1): begin major('put',o-put1+1:1,' ',p:1); goto fin_set;
1969 end;
1970 set_rule: begin major('setrule'); goto fin_rule;
1971 end;
1972 put_rule: begin major('putrule'); goto fin_rule;
1973 end;
1974 @t\4@>@<Cases for commands |nop|, |bop|, \dots, |pop|@>@;
1975 @t\4@>@<Cases for horizontal motion@>@;
1976 othercases if special_cases(o,p,a) then goto done@+else goto 9998
1977 endcases
1978
1979 @ @<Declare the function called |special_cases|@>=
1980 function special_cases(@!o:eight_bits;@!p,@!a:integer):boolean;
1981 label change_font,move_down,done,9998;
1982 var q:integer; {parameter of the current command}
1983 @!k:integer; {loop index}
1984 @!bad_char:boolean; {has a non-ASCII character code appeared in this \\{xxx}?}
1985 @!pure:boolean; {is the command error-free?}
1986 @!vvv:integer; {|v|, rounded to the nearest pixel}
1987 begin pure:=true;
1988 case o of
1989 @t\4@>@<Cases for vertical motion@>@;
1990 @t\4@>@<Cases for fonts@>@;
1991 four_cases(xxx1): @<Translate an |xxx| command and |goto done|@>;
1992 pre: begin error('preamble command within a page!'); goto 9998;
1993 end;
1994 @.preamble command within a page@>
1995 post,post_post: begin error('postamble command within a page!'); goto 9998;
1996 @.postamble command within a page@>
1997 end;
1998 othercases begin error('undefined command ',o:1,'!');
1999 goto done;
2000 @.undefined command@>
2001 end
2002 endcases;
2003 move_down: @<Finish a command that sets |v:=v+p|, then |goto done|@>;
2004 change_font: @<Finish a command that changes the current font,
2005 then |goto done|@>;
2006 9998: pure:=false;
2007 done: special_cases:=pure;
2008 end;
2009
2010 @ @<Cases for commands |nop|, |bop|, \dots, |pop|@>=
2011 nop: begin minor('nop'); goto done;
2012 end;
2013 bop: begin error('bop occurred before eop!'); goto 9998;
2014 @.bop occurred before eop@>
2015 end;
2016 eop: begin major('eop');
2017 if s<>0 then error('stack not empty at end of page (level ',
2018 s:1,')!');
2019 @.stack not empty...@>
2020 do_page:=true; print_ln(' '); goto 9999;
2021 end;
2022 push: begin major('push');
2023 if s=max_s_so_far then
2024 begin max_s_so_far:=s+1;
2025 if s=max_s then error('deeper than claimed in postamble!');
2026 @.deeper than claimed...@>
2027 @.push deeper than claimed...@>
2028 if s=stack_size then
2029 begin error('DVItype capacity exceeded (stack size=',
2030 stack_size:1,')'); goto 9998;
2031 end;
2032 end;
2033 hstack[s]:=h; vstack[s]:=v; wstack[s]:=w;
2034 xstack[s]:=x; ystack[s]:=y; zstack[s]:=z;
2035 hhstack[s]:=hh; vvstack[s]:=vv; incr(s); ss:=s-1; goto show_state;
2036 end;
2037 pop: begin major('pop');
2038 if s=0 then error('(illegal at level zero)!')
2039 else begin decr(s); hh:=hhstack[s]; vv:=vvstack[s];
2040 h:=hstack[s]; v:=vstack[s]; w:=wstack[s];
2041 x:=xstack[s]; y:=ystack[s]; z:=zstack[s];
2042 end;
2043 ss:=s; goto show_state;
2044 end;
2045
2046 @ Rounding to the nearest pixel is best done in the manner shown here, so as
2047 to be inoffensive to the eye: When the horizontal motion is small, like a
2048 kern, |hh| changes by rounding the kern; but when the motion is large, |hh|
2049 changes by rounding the true position |h| so that accumulated rounding errors
2050 disappear. We allow a larger space in the negative direction than in
2051 the positive one, because \TeX\ makes comparatively
2052 large backspaces when it positions accents.
2053
2054 @d out_space(#)==if (p>=font_space[cur_font])or(p<=-4*font_space[cur_font]) then
2055 begin out_text(" "); hh:=pixel_round(h+p);
2056 end
2057 else hh:=hh+pixel_round(p);
2058 minor(#,' ',p:1); q:=p; goto move_right
2059
2060 @<Cases for horizontal motion@>=
2061 four_cases(right1):begin out_space('right',o-right1+1:1);
2062 end;
2063 w0,four_cases(w1):begin w:=p; out_space('w',o-w0:1);
2064 end;
2065 x0,four_cases(x1):begin x:=p; out_space('x',o-x0:1);
2066 end;
2067
2068 @ Vertical motion is done similarly, but with the threshold between
2069 ``small'' and ``large'' increased by a factor of five. The idea is to make
2070 fractions like ``$1\over2$'' round consistently, but to absorb accumulated
2071 rounding errors in the baseline-skip moves.
2072
2073 @d out_vmove(#)==if abs(p)>=5*font_space[cur_font] then vv:=pixel_round(v+p)
2074 else vv:=vv+pixel_round(p);
2075 major(#,' ',p:1); goto move_down
2076
2077 @<Cases for vertical motion@>=
2078 four_cases(down1):begin out_vmove('down',o-down1+1:1);
2079 end;
2080 y0,four_cases(y1):begin y:=p; out_vmove('y',o-y0:1);
2081 end;
2082 z0,four_cases(z1):begin z:=p; out_vmove('z',o-z0:1);
2083 end;
2084
2085 @ @<Cases for fonts@>=
2086 sixty_four_cases(fnt_num_0): begin major('fntnum',p:1);
2087 goto change_font;
2088 end;
2089 four_cases(fnt1): begin major('fnt',o-fnt1+1:1,' ',p:1);
2090 goto change_font;
2091 end;
2092 four_cases(fnt_def1): begin major('fntdef',o-fnt_def1+1:1,' ',p:1);
2093 define_font(p); goto done;
2094 end;
2095
2096 @ @<Translate an |xxx| command and |goto done|@>=
2097 begin major('xxx '''); bad_char:=false;
2098 if p<0 then error('string of negative length!');
2099 @.string of negative length@>
2100 for k:=1 to p do
2101 begin q:=get_byte;
2102 if (q<" ")or(q>"~") then bad_char:=true;
2103 if showing then print(xchr[q]);
2104 end;
2105 if showing then print('''');
2106 if bad_char then error('non-ASCII character in xxx command!');
2107 @.non-ASCII character...@>
2108 goto done;
2109 end
2110
2111 @ @<Translate a |set_char|...@>=
2112 begin if (o>" ")and(o<="~") then
2113 begin out_text(p); minor('setchar',p:1);
2114 end
2115 else major('setchar',p:1);
2116 goto fin_set;
2117 end
2118
2119 @ @<Finish a command that either sets or puts a character...@>=
2120 {if p<0 then p:=255-((-1-p) mod 256)
2121 else if p>=256 then p:=p mod 256;} {width computation for oriental fonts}
2122 @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
2123 if (p<font_bc[cur_font])or(p>font_ec[cur_font]) then q:=invalid_width
2124 else q:=char_width(cur_font)(p);
2125 if q=invalid_width then
2126 begin error('character ',p:1,' invalid in font ');
2127 @.character $c$ invalid...@>
2128 print_font(cur_font);
2129 if cur_font<>invalid_font then
2130 print('!'); {the invalid font has `\.!' in its name}
2131 end;
2132 if o>=put1 then goto done;
2133 if q=invalid_width then q:=0
2134 else hh:=hh+char_pixel_width(cur_font)(p);
2135 goto move_right
2136
2137 @ @<Finish a command that either sets or puts a rule...@>=
2138 q:=signed_quad;
2139 if showing then
2140 begin print(' height ',p:1,', width ',q:1);
2141 if out_mode>mnemonics_only then
2142 if (p<=0)or(q<=0) then print(' (invisible)')
2143 else print(' (',rule_pixels(p):1,'x',rule_pixels(q):1,' pixels)');
2144 end;
2145 if o=put_rule then goto done;
2146 if showing then if out_mode>mnemonics_only then print_ln(' ');
2147 hh:=hh+rule_pixels(q); goto move_right
2148
2149 @ A sequence of consecutive rules, or consecutive characters in a fixed-width
2150 font whose width is not an integer number of pixels, can cause |hh| to drift
2151 far away from a correctly rounded value. \.{DVItype} ensures that the
2152 amount of drift will never exceed |max_drift| pixels.
2153
2154 Since \.{DVItype} is intended to diagnose strange errors, it checks
2155 carefully to make sure that |h| and |v| do not get out of range.
2156 Normal \.{DVI}-reading programs need not do this.
2157
2158 @d infinity==@'17777777777 {$\infty$ (approximately)}
2159 @d max_drift=2 {we insist that abs|(hh-pixel_round(h))<=max_drift|}
2160
2161 @<Finish a command that sets |h:=h+q|, then |goto done|@>=
2162 if (h>0)and(q>0) then if h>infinity-q then
2163 begin error('arithmetic overflow! parameter changed from ',
2164 @.arithmetic overflow...@>
2165 q:1,' to ',infinity-h:1);
2166 q:=infinity-h;
2167 end;
2168 if (h<0)and(q<0) then if -h>q+infinity then
2169 begin error('arithmetic overflow! parameter changed from ',
2170 q:1, ' to ',(-h)-infinity:1);
2171 q:=(-h)-infinity;
2172 end;
2173 hhh:=pixel_round(h+q);
2174 if abs(hhh-hh)>max_drift then
2175 if hhh>hh then hh:=hhh-max_drift
2176 else hh:=hhh+max_drift;
2177 if showing then if out_mode>mnemonics_only then
2178 begin print(' h:=',h:1);
2179 if q>=0 then print('+');
2180 print(q:1,'=',h+q:1,', hh:=',hh:1);
2181 end;
2182 h:=h+q;
2183 if abs(h)>max_h_so_far then
2184 begin if abs(h)>max_h+99 then
2185 begin error('warning: |h|>',max_h:1,'!');
2186 @.warning: |h|...@>
2187 max_h:=abs(h);
2188 end;
2189 max_h_so_far:=abs(h);
2190 end;
2191 goto done
2192
2193 @ @<Finish a command that sets |v:=v+p|, then |goto done|@>=
2194 if (v>0)and(p>0) then if v>infinity-p then
2195 begin error('arithmetic overflow! parameter changed from ',
2196 @.arithmetic overflow...@>
2197 p:1,' to ',infinity-v:1);
2198 p:=infinity-v;
2199 end;
2200 if (v<0)and(p<0) then if -v>p+infinity then
2201 begin error('arithmetic overflow! parameter changed from ',
2202 p:1, ' to ',(-v)-infinity:1);
2203 p:=(-v)-infinity;
2204 end;
2205 vvv:=pixel_round(v+p);
2206 if abs(vvv-vv)>max_drift then
2207 if vvv>vv then vv:=vvv-max_drift
2208 else vv:=vvv+max_drift;
2209 if showing then if out_mode>mnemonics_only then
2210 begin print(' v:=',v:1);
2211 if p>=0 then print('+');
2212 print(p:1,'=',v+p:1,', vv:=',vv:1);
2213 end;
2214 v:=v+p;
2215 if abs(v)>max_v_so_far then
2216 begin if abs(v)>max_v+99 then
2217 begin error('warning: |v|>',max_v:1,'!');
2218 @.warning: |v|...@>
2219 max_v:=abs(v);
2220 end;
2221 max_v_so_far:=abs(v);
2222 end;
2223 goto done
2224
2225 @ @<Show the values of |ss|, |h|, |v|, |w|, |x|, |y|, |z|...@>=
2226 if showing then if out_mode>mnemonics_only then
2227 begin print_ln(' ');
2228 print('level ',ss:1,':(h=',h:1,',v=',v:1,
2229 ',w=',w:1,',x=',x:1,',y=',y:1,',z=',z:1,
2230 ',hh=',hh:1,',vv=',vv:1,')');
2231 end;
2232 goto done
2233
2234 @ @<Finish a command that changes the current font...@>=
2235 font_num[nf]:=p; cur_font:=0;
2236 while font_num[cur_font]<>p do incr(cur_font);
2237 if cur_font=nf then
2238 begin cur_font:=invalid_font;
2239 error('invalid font selection: font ',p:1,' was never defined!');
2240 end;
2241 if showing then if out_mode>mnemonics_only then
2242 begin print(' current font is '); print_font(cur_font);
2243 end;
2244 goto done
2245
2246 @* Skipping pages.
2247 A routine that's much simpler than |do_page| is used to pass over
2248 pages that are not being translated. The |skip_pages| subroutine
2249 is assumed to begin just after the preamble has been read, or just
2250 after a |bop| has been processed. It continues until either finding a
2251 |bop| that matches the desired starting page specifications, or until
2252 running into the postamble.
2253
2254 @p @t\4@>@<Declare the procedure called |scan_bop|@>@;
2255 procedure skip_pages(@!bop_seen:boolean);
2256 label 9999; {end of this subroutine}
2257 var p:integer; {a parameter}
2258 @!k:0..255; {command code}
2259 @!down_the_drain:integer; {garbage}
2260 begin showing:=false;
2261 while true do
2262 begin if not bop_seen then
2263 begin scan_bop;
2264 if in_postamble then goto 9999;
2265 if not started then if start_match then
2266 begin started:=true; goto 9999;
2267 end;
2268 end;
2269 @<Skip until finding |eop|@>;
2270 bop_seen:=false;
2271 end;
2272 9999:end;
2273
2274 @ @<Skip until finding |eop|@>=
2275 repeat if eof(dvi_file) then bad_dvi('the file ended prematurely');
2276 @.the file ended prematurely@>
2277 k:=get_byte;
2278 p:=first_par(k);
2279 case k of
2280 set_rule,put_rule: down_the_drain:=signed_quad;
2281 four_cases(fnt_def1): begin define_font(p);
2282 print_ln(' ');
2283 end;
2284 four_cases(xxx1): while p>0 do
2285 begin down_the_drain:=get_byte; decr(p);
2286 end;
2287 bop,pre,post,post_post,undefined_commands:
2288 bad_dvi('illegal command at byte ',cur_loc-1:1);
2289 @.illegal command at byte n@>
2290 othercases do_nothing
2291 endcases;
2292 until k=eop;
2293
2294 @ Global variables called |old_backpointer| and |new_backpointer|
2295 are used to check whether the back pointers are properly set up.
2296 Another one tells whether we have already found the starting page.
2297
2298 @<Glob...@>=
2299 @!old_backpointer:integer; {the previous |bop| command location}
2300 @!new_backpointer:integer; {the current |bop| command location}
2301 @!started:boolean; {has the starting page been found?}
2302
2303 @ @<Set init...@>=
2304 old_backpointer:=-1; started:=false;
2305
2306 @ The |scan_bop| procedure reads \.{DVI} commands following the preamble
2307 or following |eop|, until finding either |bop| or the postamble.
2308
2309 @<Declare the procedure called |scan_bop|@>=
2310 procedure scan_bop;
2311 var k:0..255; {command code}
2312 begin repeat if eof(dvi_file) then bad_dvi('the file ended prematurely');
2313 @.the file ended prematurely@>
2314 k:=get_byte;
2315 if (k>=fnt_def1)and(k<fnt_def1+4) then
2316 begin define_font(first_par(k)); k:=nop;
2317 end;
2318 until k<>nop;
2319 if k=post then in_postamble:=true
2320 else begin if k<>bop then bad_dvi('byte ',cur_loc-1:1,' is not bop');
2321 @.byte n is not bop@>
2322 new_backpointer:=cur_loc-1; incr(page_count);
2323 for k:=0 to 9 do count[k]:=signed_quad;
2324 if signed_quad<>old_backpointer
2325 then print_ln('backpointer in byte ',cur_loc-4:1,
2326 ' should be ',old_backpointer:1,'!');
2327 @.backpointer...should be p@>
2328 old_backpointer:=new_backpointer;
2329 end;
2330 end;
2331
2332 @* Using the backpointers.
2333 The routines in this section of the program are brought into play only
2334 if |random_reading| is |true| (and only if |out_mode=the_works|).
2335 First comes a routine that illustrates how to find the postamble quickly.
2336
2337 @<Find the postamble, working back from the end@>=
2338 n:=dvi_length;
2339 if n<53 then bad_dvi('only ',n:1,' bytes long');
2340 @.only n bytes long@>
2341 m:=n-4;
2342 repeat if m=0 then bad_dvi('all 223s');
2343 @.all 223s@>
2344 move_to_byte(m); k:=get_byte; decr(m);
2345 until k<>223;
2346 if k<>id_byte then bad_dvi('ID byte is ',k:1);
2347 @.ID byte is wrong@>
2348 move_to_byte(m-3); q:=signed_quad;
2349 if (q<0)or(q>m-33) then bad_dvi('post pointer ',q:1,' at byte ',m-3:1);
2350 @.post pointer is wrong@>
2351 move_to_byte(q); k:=get_byte;
2352 if k<>post then bad_dvi('byte ',q:1,' is not post');
2353 @.byte n is not post@>
2354 post_loc:=q; first_backpointer:=signed_quad
2355
2356 @ Note that the last steps of the above code save the locations of the
2357 the |post| byte and the final |bop|. We had better declare these global
2358 variables, together with two more that we will need shortly.
2359
2360 @<Glob...@>=
2361 @!post_loc:integer; {byte location where the postamble begins}
2362 @!first_backpointer:integer; {the pointer following |post|}
2363 @!start_loc:integer; {byte location of the first page to process}
2364 @!after_pre:integer; {byte location immediately following the preamble}
2365
2366 @ The next little routine shows how the backpointers can be followed
2367 to move through a \.{DVI} file in reverse order. Ordinarily a \.{DVI}-reading
2368 program would do this only if it wants to print the pages backwards or
2369 if it wants to find a specified starting page that is not necessarily the
2370 first page in the file; otherwise it would of course be simpler and faster
2371 just to read the whole file from the beginning.
2372
2373 @<Count the pages and move to the starting page@>=
2374 q:=post_loc; p:=first_backpointer; start_loc:=-1;
2375 if p<0 then in_postamble:=true
2376 else begin repeat
2377 {now |q| points to a |post| or |bop| command; |p>=0| is prev pointer}
2378 if p>q-46 then
2379 bad_dvi('page link ',p:1,' after byte ',q:1);
2380 @.page link wrong...@>
2381 q:=p; move_to_byte(q); k:=get_byte;
2382 if k=bop then incr(page_count)
2383 else bad_dvi('byte ',q:1,' is not bop');
2384 @.byte n is not bop@>
2385 for k:=0 to 9 do count[k]:=signed_quad;
2386 p:=signed_quad;
2387 if start_match then
2388 begin start_loc:=q; old_backpointer:=p;
2389 end;
2390 until p<0;
2391 if start_loc<0 then abort('starting page number could not be found!');
2392 @.starting page number...@>
2393 if old_backpointer<0 then start_loc:=after_pre; {we want to check everything}
2394 move_to_byte(start_loc);
2395 end;
2396 if page_count<>total_pages then
2397 print_ln('there are really ',page_count:1,' pages, not ',total_pages:1,'!')
2398 @.there are really n pages@>
2399
2400 @* Reading the postamble.
2401 Now imagine that we are reading the \.{DVI} file and positioned just
2402 four bytes after the |post| command. That, in fact, is the situation,
2403 when the following part of \.{DVItype} is called upon to read, translate,
2404 and check the rest of the postamble.
2405
2406 @p procedure read_postamble;
2407 var k:integer; {loop index}
2408 @!p,@!q,@!m:integer; {general purpose registers}
2409 begin showing:=false; post_loc:=cur_loc-5;
2410 print_ln('Postamble starts at byte ',post_loc:1,'.');
2411 @.Postamble starts at byte n@>
2412 if signed_quad<>numerator then
2413 print_ln('numerator doesn''t match the preamble!');
2414 @.numerator doesn't match@>
2415 if signed_quad<>denominator then
2416 print_ln('denominator doesn''t match the preamble!');
2417 @.denominator doesn't match@>
2418 if signed_quad<>mag then if new_mag=0 then
2419 print_ln('magnification doesn''t match the preamble!');
2420 @.magnification doesn't match@>
2421 max_v:=signed_quad; max_h:=signed_quad;@/
2422 print('maxv=',max_v:1,', maxh=',max_h:1);@/
2423 max_s:=get_two_bytes; total_pages:=get_two_bytes;@/
2424 print_ln(', maxstackdepth=',max_s:1,', totalpages=',total_pages:1);
2425 if out_mode<the_works then
2426 @<Compare the \\{lust} parameters with the accumulated facts@>;
2427 @<Process the font definitions of the postamble@>;
2428 @<Make sure that the end of the file is well-formed@>;
2429 end;
2430
2431 @ No warning is given when |max_h_so_far| exceeds |max_h| by less than~100,
2432 since 100 units is invisibly small; it's approximately the wavelength of
2433 visible light, in the case of \TeX\ output. Rounding errors can be expected
2434 to make |h| and |v| slightly more than |max_h| and |max_v|, every once in
2435 a~while; hence small discrepancies are not cause for alarm.
2436
2437 @<Compare the \\{lust}...@>=
2438 begin if max_v+99<max_v_so_far then
2439 print_ln('warning: observed maxv was ',max_v_so_far:1);
2440 @.warning: observed maxv...@>
2441 @.observed maxv was x@>
2442 if max_h+99<max_h_so_far then
2443 print_ln('warning: observed maxh was ',max_h_so_far:1);
2444 @.warning: observed maxh...@>
2445 @.observed maxh was x@>
2446 if max_s<max_s_so_far then
2447 print_ln('warning: observed maxstackdepth was ',max_s_so_far:1);
2448 @.warning: observed maxstack...@>
2449 @.observed maxstackdepth was x@>
2450 if page_count<>total_pages then
2451 print_ln('there are really ',page_count:1,' pages, not ',total_pages:1,'!');
2452 end
2453 @.there are really n pages@>
2454
2455 @ When we get to the present code, the |post_post| command has
2456 just been read.
2457
2458 @<Make sure that the end of the file is well-formed@>=
2459 q:=signed_quad;
2460 if q<>post_loc then
2461 print_ln('bad postamble pointer in byte ',cur_loc-4:1,'!');
2462 @.bad postamble pointer@>
2463 m:=get_byte;
2464 if m<>id_byte then print_ln('identification in byte ',cur_loc-1:1,
2465 @.identification...should be n@>
2466 ' should be ',id_byte:1,'!');
2467 k:=cur_loc; m:=223;
2468 while (m=223)and not eof(dvi_file) do m:=get_byte;
2469 if not eof(dvi_file) then bad_dvi('signature in byte ',cur_loc-1:1,
2470 @.signature...should be...@>
2471 ' should be 223')
2472 else if cur_loc<k+4 then
2473 print_ln('not enough signature bytes at end of file (',
2474 @.not enough signature bytes...@>
2475 cur_loc-k:1,')');
2476
2477 @ @<Process the font definitions...@>=
2478 repeat k:=get_byte;
2479 if (k>=fnt_def1)and(k<fnt_def1+4) then
2480 begin p:=first_par(k); define_font(p); print_ln(' '); k:=nop;
2481 end;
2482 until k<>nop;
2483 if k<>post_post then
2484 print_ln('byte ',cur_loc-1:1,' is not postpost!')
2485 @.byte n is not postpost@>
2486
2487 @* The main program.
2488 Now we are ready to put it all together. This is where \.{DVItype} starts,
2489 and where it ends.
2490
2491 @p begin initialize; {get all variables initialized}
2492 dialog; {set up all the options}
2493 @<Process the preamble@>;
2494 if out_mode=the_works then {|random_reading=true|}
2495 begin @<Find the postamble, working back from the end@>;
2496 in_postamble:=true; read_postamble; in_postamble:=false;
2497 @<Count the pages and move to the starting page@>;
2498 end;
2499 skip_pages(false);
2500 if not in_postamble then @<Translate up to |max_pages| pages@>;
2501 if out_mode<the_works then
2502 begin if not in_postamble then skip_pages(true);
2503 if signed_quad<>old_backpointer then
2504 print_ln('backpointer in byte ',cur_loc-4:1,
2505 ' should be ',old_backpointer:1,'!');
2506 @.backpointer...should be p@>
2507 read_postamble;
2508 end;
2509 final_end:end.
2510
2511 @ The main program needs a few global variables in order to do its work.
2512
2513 @<Glob...@>=
2514 @!k,@!m,@!n,@!p,@!q:integer; {general purpose registers}
2515
2516 @ A \.{DVI}-reading program that reads the postamble first need not look at the
2517 preamble; but \.{DVItype} looks at the preamble in order to do error
2518 checking, and to display the introductory comment.
2519
2520 @<Process the preamble@>=
2521 open_dvi_file;
2522 p:=get_byte; {fetch the first byte}
2523 if p<>pre then bad_dvi('First byte isn''t start of preamble!');
2524 @.First byte isn't...@>
2525 p:=get_byte; {fetch the identification byte}
2526 if p<>id_byte then
2527 print_ln('identification in byte 1 should be ',id_byte:1,'!');
2528 @.identification...should be n@>
2529 @<Compute the conversion factors@>;
2530 p:=get_byte; {fetch the length of the introductory comment}
2531 print('''');
2532 while p>0 do
2533 begin decr(p); print(xchr[get_byte]);
2534 end;
2535 print_ln('''');
2536 after_pre:=cur_loc
2537
2538 @ The conversion factor |conv| is figured as follows: There are exactly
2539 |n/d| decimicrons per \.{DVI} unit, and 254000 decimicrons per inch,
2540 and |resolution| pixels per inch. Then we have to adjust this
2541 by the stated amount of magnification.
2542
2543 @<Compute the conversion factors@>=
2544 numerator:=signed_quad; denominator:=signed_quad;
2545 if numerator<=0 then bad_dvi('numerator is ',numerator:1);
2546 @.numerator is wrong@>
2547 if denominator<=0 then bad_dvi('denominator is ',denominator:1);
2548 @.denominator is wrong@>
2549 print_ln('numerator/denominator=',numerator:1,'/',denominator:1);
2550 tfm_conv:=(25400000.0/numerator)*(denominator/473628672)/16.0;
2551 conv:=(numerator/254000.0)*(resolution/denominator);
2552 mag:=signed_quad;
2553 if new_mag>0 then mag:=new_mag
2554 else if mag<=0 then bad_dvi('magnification is ',mag:1);
2555 @.magnification is wrong@>
2556 true_conv:=conv; conv:=true_conv*(mag/1000.0);
2557 print_ln('magnification=',mag:1,'; ',conv:16:8,' pixels per DVI unit')
2558
2559 @ The code shown here uses a convention that has proved to be useful:
2560 If the starting page was specified as, e.g., `\.{1.*.-5}', then
2561 all page numbers in the file are displayed by showing the values of
2562 counts 0, 1, and~2, separated by dots. Such numbers can, for example,
2563 be displayed on the console of a printer when it is working on that
2564 page.
2565
2566 @<Translate up to...@>=
2567 begin while max_pages>0 do
2568 begin decr(max_pages);
2569 print_ln(' '); print(cur_loc-45:1,': beginning of page ');
2570 for k:=0 to start_vals do
2571 begin print(count[k]:1);
2572 if k<start_vals then print('.')
2573 else print_ln(' ');
2574 end;
2575 if not do_page then bad_dvi('page ended unexpectedly');
2576 @.page ended unexpectedly@>
2577 scan_bop;
2578 if in_postamble then goto done;
2579 end;
2580 done:end
2581
2582 @* System-dependent changes.
2583 This section should be replaced, if necessary, by changes to the program
2584 that are necessary to make \.{DVItype} work at a particular installation.
2585 It is usually best to design your change file so that all changes to
2586 previous sections preserve the section numbering; then everybody's version
2587 will be consistent with the printed program. More extensive changes,
2588 which introduce new sections, can be inserted here; then only the index
2589 itself will get a new section number.
2590 @^system dependencies@>
2591
2592 @* Index.
2593 Pointers to error messages appear here together with the section numbers
2594 where each ident\-i\-fier is used.