Debian Codegraph @

texk/web2c/luatexdir/dvi/dvigen.c

raw
1 /*
2
3 dvigen.w
4
5 Copyright 2009-2013 Taco Hoekwater <[email protected]>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26 #undef write_dvi
27
28 /*tex This is the current mode: */
29
30 #define mode cur_list.mode_field
31
32 /*tex
33
34 The most important output produced by a run of \TeX\ is the ``device
35 independent'' (\.{DVI}) file that specifies where characters and rules are to
36 appear on printed pages. The form of these files was designed by David R. Fuchs
37 in 1979. Almost any reasonable typesetting device can be driven by a program that
38 takes \.{DVI} files as input, and dozens of such \.{DVI}-to-whatever programs
39 have been written. Thus, it is possible to print the output of \TeX\ on many
40 different kinds of equipment, using \TeX\ as a device-independent ``front end.''
41
42 A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a series of
43 commands in a machine-like language. The first byte of each command is the
44 operation code, and this code is followed by zero or more bytes that provide
45 parameters to the command. The parameters themselves may consist of several
46 consecutive bytes; for example, the `|set_rule|' command has two parameters, each
47 of which is four bytes long. Parameters are usually regarded as nonnegative
48 integers; but four-byte-long parameters, and shorter parameters that denote
49 distances, can be either positive or negative. Such parameters are given in two's
50 complement notation. For example, a two-byte-long distance parameter has a value
51 between $-2^{15}$ and $2^{15}-1$. As in \.{TFM} files, numbers that occupy more
52 than one byte position appear in BigEndian order.
53
54 A \.{DVI} file consists of a ``preamble,'' followed by a sequence of one or more
55 ``pages,'' followed by a ``postamble.'' The preamble is simply a |pre| command,
56 with its parameters that define the dimensions used in the file; this must come
57 first. Each ``page'' consists of a |bop| command, followed by any number of other
58 commands that tell where characters are to be placed on a physical page, followed
59 by an |eop| command. The pages appear in the order that \TeX\ generated them. If
60 we ignore |nop| commands and \\{fnt\_def} commands (which are allowed between any
61 two commands in the file), each |eop| command is immediately followed by a |bop|
62 command, or by a |post| command; in the latter case, there are no more pages in
63 the file, and the remaining bytes form the postamble. Further details about the
64 postamble will be explained later.
65
66 Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
67 quantities that give the location number of some other byte in the file; the
68 first byte is number~0, then comes number~1, and so on. For example, one of the
69 parameters of a |bop| command points to the previous |bop|; this makes it
70 feasible to read the pages in backwards order, in case the results are being
71 directed to a device that stacks its output face up. Suppose the preamble of a
72 \.{DVI} file occupies bytes 0 to 99. Now if the first page occupies bytes 100 to
73 999, say, and if the second page occupies bytes 1000 to 1999, then the |bop| that
74 starts in byte 1000 points to 100 and the |bop| that starts in byte 2000 points
75 to 1000. (The very first |bop|, i.e., the one starting in byte 100, has a pointer
76 of~$-1$.)
77
78 The \.{DVI} format is intended to be both compact and easily interpreted by a
79 machine. Compactness is achieved by making most of the information implicit
80 instead of explicit. When a \.{DVI}-reading program reads the commands for a
81 page, it keeps track of several quantities: (a)~The current font |f| is an
82 integer; this value is changed only by \\{fnt} and \\{fnt\_num} commands. (b)~The
83 current position on the page is given by two numbers called the horizontal and
84 vertical coordinates, |h| and |v|. Both coordinates are zero at the upper left
85 corner of the page; moving to the right corresponds to increasing the horizontal
86 coordinate, and moving down corresponds to increasing the vertical coordinate.
87 Thus, the coordinates are essentially Cartesian, except that vertical directions
88 are flipped; the Cartesian version of |(h,v)| would be |(h,-v)|. (c)~The current
89 spacing amounts are given by four numbers |w|, |x|, |y|, and |z|, where |w|
90 and~|x| are used for horizontal spacing and where |y| and~|z| are used for
91 vertical spacing. (d)~There is a stack containing |(h,v,w,x,y,z)| values; the
92 \.{DVI} commands |push| and |pop| are used to change the current level of
93 operation. Note that the current font~|f| is not pushed and popped; the stack
94 contains only information about positioning.
95
96 The values of |h|, |v|, |w|, |x|, |y|, and |z| are signed integers having up to
97 32 bits, including the sign. Since they represent physical distances, there is a
98 small unit of measurement such that increasing |h| by~1 means moving a certain
99 tiny distance to the right. The actual unit of measurement is variable, as
100 explained below; \TeX\ sets things up so that its \.{DVI} output is in sp units,
101 i.e., scaled points, in agreement with all the |scaled| dimensions in \TeX's data
102 structures.
103
104 Here is a list of all the commands that may appear in a \.{DVI} file. Each
105 command is specified by its symbolic name (e.g., |bop|), its opcode byte (e.g.,
106 139), and its parameters (if any). The parameters are followed by a bracketed
107 number telling how many bytes they occupy; for example, `|p[4]|' means that
108 parameter |p| is four bytes long.
109
110 \startitemize
111
112 \startitem
113 |set_char_0| 0. Typeset character number~0 from font~|f| such that the
114 reference point of the character is at |(h,v)|. Then increase |h| by the
115 width of that character. Note that a character may have zero or negative
116 width, so one cannot be sure that |h| will advance after this command; but
117 |h| usually does increase.
118 \stopitem
119
120 \startitem
121 \\{set\_char\_1} through \\{set\_char\_127} (opcodes 1 to 127).
122 Do the operations of |set_char_0|; but use the character whose number
123 matches the opcode, instead of character~0.
124 \stopitem
125
126 \startitem
127 |set1| 128 |c[1]|. Same as |set_char_0|, except that character number~|c| is
128 typeset. \TeX82 uses this command for characters in the range |128<=c<256|.
129 \stopitem
130
131 \startitem
132 |set2| 129 |c[2]|. Same as |set1|, except that |c|~is two bytes long, so it
133 is in the range |0<=c<65536|. \TeX82 never uses this command, but it should
134 come in handy for extensions of \TeX\ that deal with oriental languages.
135 \stopitem
136
137 \startitem
138 |set3| 130 |c[3]|. Same as |set1|, except that |c|~is three bytes long, so it
139 can be as large as $2^{24}-1$. Not even the Chinese language has this many
140 characters, but this command might prove useful in some yet unforeseen
141 extension.
142 \stopitem
143
144 \startitem
145 |set4| 131 |c[4]|. Same as |set1|, except that |c|~is four bytes long.
146 Imagine that.
147 \stopitem
148
149 \startitem
150 |set_rule| 132 |a[4]| |b[4]|. Typeset a solid black rectangle of height~|a|
151 and width~|b|, with its bottom left corner at |(h,v)|. Then set |h:=h+b|. If
152 either |a<=0| or |b<=0|, nothing should be typeset. Note that if |b<0|, the
153 value of |h| will decrease even though nothing else happens. See below for
154 details about how to typeset rules so that consistency with \MF\ is
155 guaranteed.
156 \stopitem
157
158 \startitem
159 |put1| 133 |c[1]|. Typeset character number~|c| from font~|f| such that the
160 reference point of the character is at |(h,v)|. (The `put' commands are
161 exactly like the `set' commands, except that they simply put out a character
162 or a rule without moving the reference point afterwards.)
163 \stopitem
164
165 \startitem
166 |put2| 134 |c[2]|. Same as |set2|, except that |h| is not changed.
167 \stopitem
168
169 \startitem
170 |put3| 135 |c[3]|. Same as |set3|, except that |h| is not changed.
171 \stopitem
172
173 \startitem
174 |put4| 136 |c[4]|. Same as |set4|, except that |h| is not changed.
175 \stopitem
176
177 \startitem
178 |put_rule| 137 |a[4]| |b[4]|. Same as |set_rule|, except that |h| is not
179 changed.
180 \stopitem
181
182 \startitem
183 |nop| 138. No operation, do nothing. Any number of |nop|'s may occur between
184 \.{DVI} commands, but a |nop| cannot be inserted between a command and its
185 parameters or between two parameters.
186 \stopitem
187
188 \startitem
189 |bop| 139 $c_0[4]$ $c_1[4]$ $\ldots$ $c_9[4]$ $p[4]$. Beginning of a page:
190 Set |(h,v,w,x,y,z):=(0,0,0,0,0,0)| and set the stack empty. Set the current
191 font |f| to an undefined value. The ten $c_i$ parameters hold the values of
192 \.{\\count0} $\ldots$ \.{\\count9} in \TeX\ at the time \.{\\shipout} was
193 invoked for this page; they can be used to identify pages, if a user wants to
194 print only part of a \.{DVI} file. The parameter |p| points to the previous
195 |bop| in the file; the first |bop| has $p=-1$.
196 \stopitem
197
198 \startitem
199 |eop| 140. End of page: Print what you have read since the previous |bop|. At
200 this point the stack should be empty. (The \.{DVI}-reading programs that
201 drive most output devices will have kept a buffer of the material that
202 appears on the page that has just ended. This material is largely, but not
203 entirely, in order by |v| coordinate and (for fixed |v|) by |h|~coordinate;
204 so it usually needs to be sorted into some order that is appropriate for the
205 device in question.)
206 \stopitem
207
208 \startitem
209 |push| 141. Push the current values of |(h,v,w,x,y,z)| onto the top of the
210 stack; do not change any of these values. Note that |f| is not pushed.
211 \stopitem
212
213 \startitem
214 |pop| 142. Pop the top six values off of the stack and assign them
215 respectively to |(h,v,w,x,y,z)|. The number of pops should never exceed the
216 number of pushes, since it would be highly embarrassing if the stack were
217 empty at the time of a |pop| command.
218
219 \startitem
220 |right1| 143 |b[1]|. Set |h:=h+b|, i.e., move right |b| units. The parameter
221 is a signed number in two's complement notation, |-128<=b<128|; if |b<0|, the
222 reference point moves left.
223 \stopitem
224
225 \startitem
226 |right2| 144 |b[2]|. Same as |right1|, except that |b| is a two-byte quantity
227 in the range |-32768<=b<32768|.
228 \stopitem
229
230 \startitem
231 |right3| 145 |b[3]|. Same as |right1|, except that |b| is a three-byte
232 quantity in the range |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
233 \stopitem
234
235 \startitem
236 |right4| 146 |b[4]|. Same as |right1|, except that |b| is a four-byte
237 quantity in the range |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
238 \stopitem
239
240 \startitem
241 |w0| 147. Set |h:=h+w|; i.e., move right |w| units. With luck, this
242 parameterless command will usually suffice, because the same kind of motion
243 will occur several times in succession; the following commands explain how
244 |w| gets particular values.
245 \stopitem
246
247 \startitem
248 |w1| 148 |b[1]|. Set |w:=b| and |h:=h+b|. The value of |b| is a signed
249 quantity in two's complement notation, |-128<=b<128|. This command changes
250 the current |w|~spacing and moves right by |b|.
251 \stopitem
252
253 \startitem
254 |w2| 149 |b[2]|. Same as |w1|, but |b| is two bytes long, |-32768<=b<32768|.
255 \stopitem
256
257 \startitem
258 |w3| 150 |b[3]|. Same as |w1|, but |b| is three bytes long,
259 |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
260 \stopitem
261
262 \startitem
263 |w4| 151 |b[4]|. Same as |w1|, but |b| is four bytes long,
264 |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
265 \stopitem
266
267 \startitem
268 |x0| 152. Set |h:=h+x|; i.e., move right |x| units. The `|x|' commands are
269 like the `|w|' commands except that they involve |x| instead of |w|.
270 \stopitem
271
272 \startitem
273 |x1| 153 |b[1]|. Set |x:=b| and |h:=h+b|. The value of |b| is a signed
274 quantity in two's complement notation, |-128<=b<128|. This command changes
275 the current |x|~spacing and moves right by |b|.
276 \stopitem
277
278 \startitem
279 |x2| 154 |b[2]|. Same as |x1|, but |b| is two bytes long, |-32768<=b<32768|.
280 \stopitem
281
282 \startitem
283 |x3| 155 |b[3]|. Same as |x1|, but |b| is three bytes long,
284 |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
285 \stopitem
286
287 \startitem
288 |x4| 156 |b[4]|. Same as |x1|, but |b| is four bytes long,
289 |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
290 \stopitem
291
292 \startitem
293 |down1| 157 |a[1]|. Set |v:=v+a|, i.e., move down |a| units. The parameter is
294 a signed number in two's complement notation, |-128<=a<128|; if |a<0|, the
295 reference point moves up.
296 \stopitem
297
298 \startitem
299 |down2| 158 |a[2]|. Same as |down1|, except that |a| is a two-byte quantity
300 in the range |-32768<=a<32768|.
301 \stopitem
302
303 \startitem
304 |down3| 159 |a[3]|. Same as |down1|, except that |a| is a three-byte quantity
305 in the range |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
306 \stopitem
307
308 \startitem
309 |down4| 160 |a[4]|. Same as |down1|, except that |a| is a four-byte quantity
310 in the range |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
311 \stopitem
312
313 \startitem
314 |y0| 161. Set |v:=v+y|; i.e., move down |y| units. With luck, this
315 parameterless command will usually suffice, because the same kind of motion
316 will occur several times in succession; the following commands explain how
317 |y| gets particular values.
318 \stopitem
319
320 \startitem
321 |y1| 162 |a[1]|. Set |y:=a| and |v:=v+a|. The value of |a| is a signed
322 quantity in two's complement notation, |-128<=a<128|. This command changes
323 the current |y|~spacing and moves down by |a|.
324 \stopitem
325
326 \startitem
327 |y2| 163 |a[2]|. Same as |y1|, but |a| is two bytes long, |-32768<=a<32768|.
328 \stopitem
329
330 \startitem
331 |y3| 164 |a[3]|. Same as |y1|, but |a| is three bytes long,
332 |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
333 \stopitem
334
335 \startitem
336 |y4| 165 |a[4]|. Same as |y1|, but |a| is four bytes long,
337 |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
338 \stopitem
339
340 \startitem
341 |z0| 166. Set |v:=v+z|; i.e., move down |z| units. The `|z|' commands are
342 like the `|y|' commands except that they involve |z| instead of |y|.
343 \stopitem
344
345 \startitem
346 |z1| 167 |a[1]|. Set |z:=a| and |v:=v+a|. The value of |a| is a signed
347 quantity in two's complement notation, |-128<=a<128|. This command changes
348 the current |z|~spacing and moves down by |a|.
349 \stopitem
350
351 \startitem
352 |z2| 168 |a[2]|. Same as |z1|, but |a| is two bytes long, |-32768<=a<32768|.
353 \stopitem
354
355 \startitem
356 |z3| 169 |a[3]|. Same as |z1|, but |a| is three bytes long,
357 |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
358 \stopitem
359
360 \startitem
361 |z4| 170 |a[4]|. Same as |z1|, but |a| is four bytes long,
362 |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
363 \stopitem
364
365 \startitem
366 |fnt_num_0| 171. Set |f:=0|. Font 0 must previously have been defined by a
367 \\{fnt\_def} instruction, as explained below.
368 \stopitem
369
370 \startitem
371 \\{fnt\_num\_1} through \\{fnt\_num\_63} (opcodes 172 to 234). Set |f:=1|,
372 \dots, \hbox{|f:=63|}, respectively.
373 \stopitem
374
375 \startitem
376 |fnt1| 235 |k[1]|. Set |f:=k|. \TeX82 uses this command for font numbers in
377 the range |64<=k<256|.
378 \stopitem
379
380 \startitem
381 |fnt2| 236 |k[2]|. Same as |fnt1|, except that |k|~is two bytes long, so it
382 is in the range |0<=k<65536|. \TeX82 never generates this command, but large
383 font numbers may prove useful for specifications of color or texture, or they
384 may be used for special fonts that have fixed numbers in some external coding
385 scheme.
386 \stopitem
387
388 \startitem
389 |fnt3| 237 |k[3]|. Same as |fnt1|, except that |k|~is three bytes long, so it
390 can be as large as $2^{24}-1$.
391 \stopitem
392
393 \startitem
394 |fnt4| 238 |k[4]|. Same as |fnt1|, except that |k|~is four bytes long; this
395 is for the really big font numbers (and for the negative ones).
396 \stopitem
397
398 \startitem
399 |xxx1| 239 |k[1]| |x[k]|. This command is undefined in general; it functions
400 as a $(k+2)$-byte |nop| unless special \.{DVI}-reading programs are being
401 used. \TeX82 generates |xxx1| when a short enough \.{\\special} appears,
402 setting |k| to the number of bytes being sent. It is recommended that |x| be
403 a string having the form of a keyword followed by possible parameters
404 relevant to that keyword.
405 \stopitem
406
407 \startitem
408 |xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
409 \stopitem
410
411 \startitem
412 |xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
413 \stopitem
414
415 \startitem
416 |xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously large.
417 \TeX82 uses |xxx4| when sending a string of length 256 or more.
418 \stopitem
419
420 \startitem
421 |fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|. Define
422 font |k|, where |0<=k<256|; font definitions will be explained shortly.
423 \stopitem
424
425 \startitem
426 |fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|. Define
427 font |k|, where |0<=k<65536|.
428 \stopitem
429
430 \startitem
431 |fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|. Define
432 font |k|, where |0<=k<@t$2^{24}$@>|.
433 \stopitem
434
435 \startitem
436 |fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|. Define
437 font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
438 \stopitem
439
440 \startitem
441 |pre| 247 |i[1]| |num[4]| |den[4]| |mag[4]| |k[1]| |x[k]|. Beginning of the
442 preamble; this must come at the very beginning of the file. Parameters |i|,
443 |num|, |den|, |mag|, |k|, and |x| are explained below.
444 \stopitem
445
446 \startitem
447 |post| 248. Beginning of the postamble, see below.
448 \stopitem
449
450 \startitem
451 |post_post| 249. Ending of the postamble, see below.
452 \stopitem
453
454 \startitem
455 Commands 250--255 are undefined at the present time.
456 \stopitem
457
458 */
459
460 #define set_char_0 0 /* typeset character 0 and move right */
461 #define set1 128 /* typeset a character and move right */
462 #define set_rule 132 /* typeset a rule and move right */
463 #define put1 133 /* typeset a character without moving */
464 #define put_rule 137 /* typeset a rule */
465 #define nop 138 /* no operation */
466 #define bop 139 /* beginning of page */
467 #define eop 140 /* ending of page */
468 #define push 141 /* save the current positions */
469 #define pop 142 /* restore previous positions */
470 #define right1 143 /* move right */
471 #define right4 146 /* move right, 4 bytes */
472 #define w0 147 /* move right by |w| */
473 #define w1 148 /* move right and set |w| */
474 #define x0 152 /* move right by |x| */
475 #define x1 153 /* move right and set |x| */
476 #define down1 157 /* move down */
477 #define down4 160 /* move down, 4 bytes */
478 #define y0 161 /* move down by |y| */
479 #define y1 162 /* move down and set |y| */
480 #define z0 166 /* move down by |z| */
481 #define z1 167 /* move down and set |z| */
482 #define fnt_num_0 171 /* set current font to 0 */
483 #define fnt1 235 /* set current font */
484 #define xxx1 239 /* extension to \.{DVI} primitives */
485 #define xxx4 242 /* potentially long extension to \.{DVI} primitives */
486 #define fnt_def1 243 /* define the meaning of a font number */
487 #define pre 247 /* preamble */
488 #define post 248 /* postamble beginning */
489 #define post_post 249 /* postamble ending */
490
491 /*tex
492
493 The preamble contains basic information about the file as a whole. As stated
494 above, there are six parameters: $$\hbox{|i[1]| |num[4]| |den[4]|
495 |mag[4]| |k[1]| |x[k]|.}$$ The |i| byte identifies \.{DVI} format;
496 currently this byte is always set to~2. (The value |i=3| is currently used for an
497 extended format that allows a mixture of right-to-left and left-to-right
498 typesetting. Some day we will set |i=4|, when \.{DVI} format makes another
499 incompatible change---perhaps in the year 2048.)
500
501 The next two parameters, |num| and |den|, are positive integers that define the
502 units of measurement; they are the numerator and denominator of a fraction by
503 which all dimensions in the \.{DVI} file could be multiplied in order to get
504 lengths in units of $10^{-7}$ meters. Since $\rm 7227{pt} = 254{cm}$, and since
505 \TeX\ works with scaled points where there are $2^{16}$ sp in a point, \TeX\ sets
506 $|num|/|den|=(254\cdot10^5)/(7227\cdot2^{16})=25400000/473628672$.
507
508 The |mag| parameter is what \TeX\ calls \.{\\mag}, i.e., 1000 times the desired
509 magnification. The actual fraction by which dimensions are multiplied is
510 therefore $|mag|\cdot|num|/1000|den|$. Note that if a \TeX\ source document does
511 not call for any `\.{true}' dimensions, and if you change it only by specifying a
512 different \.{\\mag} setting, the \.{DVI} file that \TeX\ creates will be
513 completely unchanged except for the value of |mag| in the preamble and postamble.
514 (Fancy \.{DVI}-reading programs allow users to override the |mag|~setting when a
515 \.{DVI} file is being printed.)
516
517 Finally, |k| and |x| allow the \.{DVI} writer to include a comment, which is not
518 interpreted further. The length of comment |x| is |k|, where |0<=k<256|.
519
520 The next macro identifies the kind of \.{DVI} files described here.
521
522 */
523
524 #define id_byte 2
525
526 /*tex
527
528 Font definitions for a given font number |k| contain further parameters
529 $$\hbox{|c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.}$$ The four-byte value |c|
530 is the check sum that \TeX\ found in the \.{TFM} file for this font; |c| should
531 match the check sum of the font found by programs that read this \.{DVI} file.
532
533 Parameter |s| contains a fixed-point scale factor that is applied to the
534 character widths in font |k|; font dimensions in \.{TFM} files and other font
535 files are relative to this quantity, which is called the ``at size'' elsewhere in
536 this documentation. The value of |s| is always positive and less than $2^{27}$.
537 It is given in the same units as the other \.{DVI} dimensions, i.e., in sp when
538 \TeX82 has made the file. Parameter |d| is similar to |s|; it is the ``design
539 size,'' and (like~|s|) it is given in \.{DVI} units. Thus, font |k| is to be used
540 at $|mag|\cdot s/1000d$ times its normal size.
541
542 The remaining part of a font definition gives the external name of the font,
543 which is an ASCII string of length |a+l|. The number |a| is the length of the
544 ``area'' or directory, and |l| is the length of the font name itself; the
545 standard local system font area is supposed to be used when |a=0|. The |n| field
546 contains the area in its first |a| bytes.
547
548 Font definitions must appear before the first use of a particular font number.
549 Once font |k| is defined, it must not be defined again; however, we
550 shall see below that font definitions appear in the postamble as well as
551 in the pages, so in this sense each font number is defined exactly twice,
552 if at all. Like |nop| commands, font definitions can
553 appear before the first |bop|, or between an |eop| and a |bop|.
554
555 Sometimes it is desirable to make horizontal or vertical rules line up precisely
556 with certain features in characters of a font. It is possible to guarantee the
557 correct matching between \.{DVI} output and the characters generated by \MF\ by
558 adhering to the following principles: (1)~The \MF\ characters should be
559 positioned so that a bottom edge or left edge that is supposed to line up with
560 the bottom or left edge of a rule appears at the reference point, i.e., in row~0
561 and column~0 of the \MF\ raster. This ensures that the position of the rule will
562 not be rounded differently when the pixel size is not a perfect multiple of the
563 units of measurement in the \.{DVI} file. (2)~A typeset rule of height $a>0$ and
564 width $b>0$ should be equivalent to a \MF-generated character having black pixels
565 in precisely those raster positions whose \MF\ coordinates satisfy
566 |0<=x<@t$\alpha$@>b| and |0<=y<@t$\alpha$@>a|, where $\alpha$ is the number of
567 pixels per \.{DVI} unit.
568
569 The last page in a \.{DVI} file is followed by `|post|'; this command introduces
570 the postamble, which summarizes important facts that \TeX\ has accumulated about
571 the file, making it possible to print subsets of the data with reasonable
572 efficiency. The postamble has the form
573
574 $$\vbox{\halign{\hbox{#\hfil}\cr
575 |post| |p[4]| |num[4]| |den[4]| |mag[4]| |l[4]| |u[4]| |s[2]| |t[2]|\cr
576 $\langle\,$font definitions$\,\rangle$\cr
577 |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
578
579 Here |p| is a pointer to the final |bop| in the file. The next three parameters,
580 |num|, |den|, and |mag|, are duplicates of the quantities that appeared in the
581 preamble.
582
583 Parameters |l| and |u| give respectively the height-plus-depth of the tallest
584 page and the width of the widest page, in the same units as other dimensions of
585 the file. These numbers might be used by a \.{DVI}-reading program to position
586 individual ``pages'' on large sheets of film or paper; however, the standard
587 convention for output on normal size paper is to position each page so that the
588 upper left-hand corner is exactly one inch from the left and the top. Experience
589 has shown that it is unwise to design \.{DVI}-to-printer software that attempts
590 cleverly to center the output; a fixed position of the upper left corner is
591 easiest for users to understand and to work with. Therefore |l| and~|u| are often
592 ignored. Parameter |s| is the maximum stack depth (i.e., the largest excess of
593
594 |push| commands over |pop| commands) needed to process this file. Then comes |t|,
595 the total number of pages (|bop| commands) present.
596
597 The postamble continues with font definitions, which are any number of
598 \\{fnt\_def} commands as described above, possibly interspersed with |nop|
599 commands. Each font number that is used in the \.{DVI} file must be defined
600 exactly twice: Once before it is first selected by a \\{fnt} command, and once in
601 the postamble.
602
603 The last part of the postamble, following the |post_post| byte that signifies the
604 end of the font definitions, contains |q|, a pointer to the |post| command that
605 started the postamble. An identification byte, |i|, comes next; this currently
606 equals~2, as in the preamble.
607
608 The |i| byte is followed by four or more bytes that are all equal to the decimal
609 number 223 (i.e., '337 in octal). \TeX\ puts out four to seven of these trailing
610 bytes, until the total length of the file is a multiple of four bytes, since this
611 works out best on machines that pack four bytes per word; but any number of 223's
612 is allowed, as long as there are at least four of them. In effect, 223 is a sort
613 of signature that is added at the very end.
614
615 This curious way to finish off a \.{DVI} file makes it feasible for
616 \.{DVI}-reading programs to find the postamble first, on most computers, even
617 though \TeX\ wants to write the postamble last. Most operating systems permit
618 random access to individual words or bytes of a file, so the \.{DVI} reader can
619 start at the end and skip backwards over the 223's until finding the
620 identification byte. Then it can back up four bytes, read |q|, and move to byte
621 |q| of the file. This byte should, of course, contain the value 248 (|post|); now
622 the postamble can be read, so the \.{DVI} reader can discover all the information
623 needed for typesetting the pages. Note that it is also possible to skip through
624 the \.{DVI} file at reasonably high speed to locate a particular page, if that
625 proves desirable. This saves a lot of time, since \.{DVI} files used in
626 production jobs tend to be large.
627
628 Unfortunately, however, standard \PASCAL\ does not include the ability to access
629 a random position in a file, or even to determine the length of a file. Almost
630 all systems nowadays provide the necessary capabilities, so \.{DVI} format has
631 been designed to work most efficiently with modern operating systems. But if
632 \.{DVI} files have to be processed under the restrictions of standard \PASCAL,
633 one can simply read them from front to back, since the necessary header
634 information is present in the preamble and in the font definitions. (The |l| and
635 |u| and |s| and |t| parameters, which appear only in the postamble, are
636 ``frills'' that are handy but not absolutely necessary.)
637
638 After considering \TeX's eyes and stomach, we come now to the bowels.
639
640 The |ship_out| procedure is given a pointer to a box; its mission is to describe
641 that box in \.{DVI} form, outputting a ``page'' to |dvi_file|. The \.{DVI}
642 coordinates $(h,v)=(0,0)$ should correspond to the upper left corner of the box
643 being shipped.
644
645 Since boxes can be inside of boxes inside of boxes, the main work of |ship_out|
646 is done by two mutually recursive routines, |hlist_out| and |vlist_out|, which
647 traverse the hlists and vlists inside of horizontal and vertical boxes.
648
649 As individual pages are being processed, we need to accumulate information about
650 the entire set of pages, since such statistics must be reported in the postamble.
651 The global variables |total_pages|, |max_v|, |max_h|, |max_push|, and |last_bop|
652 are used to record this information.
653
654 The variable |doing_leaders| is |true| while leaders are being output. The
655 variable |dead_cycles| contains the number of times an output routine has been
656 initiated since the last |ship_out|.
657
658 A few additional global variables are also defined here for use in |vlist_out|
659 and |hlist_out|. They could have been local variables, but that would waste stack
660 space when boxes are deeply nested, since the values of these variables are not
661 needed during recursive calls.
662
663 */
664
665 /* Some global variables are defined in |backend| module. */
666
667 static int max_push = 0; /* deepest nesting of |push| commands encountered so far */
668 static int last_bop = -1; /* location of previous |bop| in the \.{DVI} output */
669 static int oval, ocmd; /* used by |out_cmd| for generating |set|, |fnt| and |fnt_def| commands */
670 pointer g; /* current glue specification */
671
672 /*tex
673
674 The \.{DVI} bytes are output to a buffer instead of being written directly to the
675 output file. This makes it possible to reduce the overhead of subroutine calls,
676 thereby measurably speeding up the computation, since output of \.{DVI} bytes is
677 part of \TeX's inner loop. And it has another advantage as well, since we can
678 change instructions in the buffer in order to make the output more compact. For
679 example, a `|down2|' command can be changed to a `|y2|', thereby making a
680 subsequent `|y0|' command possible, saving two bytes.
681
682 The output buffer is divided into two parts of equal size; the bytes found in
683 |dvi_buf[0..half_buf-1]| constitute the first half, and those in
684 |dvi_buf[half_buf..dvi_buf_size-1]| constitute the second. The global variable
685 |dvi_ptr| points to the position that will receive the next output byte. When
686 |dvi_ptr| reaches |dvi_limit|, which is always equal to one of the two values
687 |half_buf| or |dvi_buf_size|, the half buffer that is about to be invaded next is
688 sent to the output and |dvi_limit| is changed to its other value. Thus, there is
689 always at least a half buffer's worth of information present, except at the very
690 beginning of the job.
691
692 Bytes of the \.{DVI} file are numbered sequentially starting with 0; the next
693 byte to be generated will be number |dvi_offset+dvi_ptr|. A byte is present in
694 the buffer only if its number is |>=dvi_gone|.
695
696 Some systems may find it more efficient to make |dvi_buf| a |packed| array, since
697 output of four bytes at once may be facilitated.
698
699 Initially the buffer is all in one piece; we will output half of it only after it
700 first fills up.
701
702 */
703
704 int dvi_buf_size = 800; /* size of the output buffer; must be a multiple of 8 */
705 eight_bits *dvi_buf; /* buffer for \.{DVI} output */
706 static int half_buf = 0; /* half of |dvi_buf_size| */
707 static int dvi_limit = 0; /* end of the current half buffer */
708 static int dvi_ptr = 0; /* the next available buffer address */
709 static int dvi_offset = 0; /* |dvi_buf_size| times the number of times the output buffer has been fully emptied */
710 static int dvi_gone = 0; /* the number of bytes already output to |dvi_file| */
711
712 /*
713 To put a byte in the buffer without paying the cost of invoking a procedure
714 each time, we use the macro |dvi_out|.
715 */
716
717 #define dvi_out(A) do { \
718 dvi_buf[dvi_ptr++]=(eight_bits)(A); \
719 if (dvi_ptr==dvi_limit) dvi_swap(); \
720 } while (0)
721
722 #define dvi_set(A,B) do { \
723 oval=A; ocmd=set1; out_cmd(); dvi.h += (B); \
724 } while (0)
725
726 #define dvi_put(A) do { \
727 oval=A; ocmd=put1; out_cmd(); \
728 } while (0)
729
730 /*
731 The |vinfo| fields in the entries of the down stack or the right stack
732 have six possible settings: |y_here| or |z_here| mean that the \.{DVI}
733 command refers to |y| or |z|, respectively (or to |w| or |x|, in the
734 case of horizontal motion); |yz_OK| means that the \.{DVI} command is
735 \\{down} (or \\{right}) but can be changed to either |y| or |z| (or
736 to either |w| or |x|); |y_OK| means that it is \\{down} and can be changed
737 to |y| but not |z|; |z_OK| is similar; and |d_fixed| means it must stay
738 \\{down}.
739
740 The four settings |yz_OK|, |y_OK|, |z_OK|, |d_fixed| would not need to
741 be distinguished from each other if we were simply solving the
742 digit-subscripting problem mentioned above. But in \TeX's case there is
743 a complication because of the nested structure of |push| and |pop|
744 commands. Suppose we add parentheses to the digit-subscripting problem,
745 redefining hits so that $\delta_y\ldots \delta_y$ is a hit if all $y$'s between
746 the $\delta$'s are enclosed in properly nested parentheses, and if the
747 parenthesis level of the right-hand $\delta_y$ is deeper than or equal to
748 that of the left-hand one. Thus, `(' and `)' correspond to `|push|'
749 and `|pop|'. Now if we want to assign a subscript to the final 1 in the
750 sequence
751 $$2_y\,7_d\,1_d\,(\,8_z\,2_y\,8_z\,)\,1$$
752 we cannot change the previous $1_d$ to $1_y$, since that would invalidate
753 the $2_y\ldots2_y$ hit. But we can change it to $1_z$, scoring a hit
754 since the intervening $8_z$'s are enclosed in parentheses.
755 */
756
757 typedef enum {
758 y_here = 1, /* |vinfo| when the movement entry points to a |y| command */
759 z_here = 2, /* |vinfo| when the movement entry points to a |z| command */
760 yz_OK = 3, /* |vinfo| corresponding to an unconstrained \\{down} command */
761 y_OK = 4, /* |vinfo| corresponding to a \\{down} that can't become a |z| */
762 z_OK = 5, /* |vinfo| corresponding to a \\{down} that can't become a |y| */
763 d_fixed = 6, /* |vinfo| corresponding to a \\{down} that can't change */
764 } movement_codes;
765
766 /* As we search through the stack, we are in one of three states,
767 |y_seen|, |z_seen|, or |none_seen|, depending on whether we have
768 encountered |y_here| or |z_here| nodes. These states are encoded as
769 multiples of 6, so that they can be added to the |info| fields for quick
770 decision-making. */
771
772 # define none_seen 0 /* no |y_here| or |z_here| nodes have been encountered yet */
773 # define y_seen 6 /* we have seen |y_here| but not |z_here| */
774 # define z_seen 12 /* we have seen |z_here| but not |y_here| */
775
776 void movement(scaled w, eight_bits o);
777
778 /*
779 extern void prune_movements(int l);
780 */
781
782 /*
783 The actual distances by which we want to move might be computed as the
784 sum of several separate movements. For example, there might be several
785 glue nodes in succession, or we might want to move right by the width of
786 some box plus some amount of glue. More importantly, the baselineskip
787 distances are computed in terms of glue together with the depth and
788 height of adjacent boxes, and we want the \.{DVI} file to lump these
789 three quantities together into a single motion.
790
791 Therefore, \TeX\ maintains two pairs of global variables: |dvi.h| and |dvi.v|
792 are the |h| and |v| coordinates corresponding to the commands actually
793 output to the \.{DVI} file, while |cur.h| and |cur.v| are the coordinates
794 corresponding to the current state of the output routines. Coordinate
795 changes will accumulate in |cur.h| and |cur.v| without being reflected
796 in the output, until such a change becomes necessary or desirable; we
797 can call the |movement| procedure whenever we want to make |dvi.h=pos.h|
798 or |dvi.v=pos.v|.
799
800 The current font reflected in the \.{DVI} output is called |dvi_f|;
801 there is no need for a `\\{cur\_f}' variable.
802
803 The depth of nesting of |hlist_out| and |vlist_out| is called |cur_s|;
804 this is essentially the depth of |push| commands in the \.{DVI} output.
805 */
806
807 /*tex A \.{DVI} position in page coordinates, in sync with DVI file: */
808
809 static scaledpos dvi;
810
811 # define synch_h(p) do { \
812 if (p.h != dvi.h) { \
813 movement(p.h - dvi.h, right1); \
814 dvi.h = p.h; \
815 } \
816 } while (0)
817
818 # define synch_v(p) do { \
819 if (p.v != dvi.v) { \
820 movement(dvi.v - p.v, down1); \
821 dvi.v = p.v; \
822 } \
823 } while (0)
824
825 # define synch_dvi_with_pos(p) do {synch_h(p); synch_v(p); } while (0)
826
827 /*tex
828
829 The actual output of |dvi_buf[a..b]| to |dvi_file| is performed by calling
830 |write_dvi(a,b)|. For best results, this procedure should be optimized to run as
831 fast as possible on each particular system, since it is part of \TeX's inner
832 loop. It is safe to assume that |a| and |b+1| will both be multiples of 4 when
833 |write_dvi(a,b)| is called; therefore it is possible on many machines to use
834 efficient methods to pack four bytes per word and to output an array of words
835 with one system call.
836
837 */
838
839 static void write_dvi(int a, int b)
840 {
841 int k;
842 for (k = a; k <= b; k++)
843 fputc(dvi_buf[k], static_pdf->file);
844 }
845
846 /*tex This outputs half of the buffer: */
847
848 static void dvi_swap(void)
849 {
850 if (dvi_limit == dvi_buf_size) {
851 write_dvi(0, half_buf - 1);
852 dvi_limit = half_buf;
853 dvi_offset = dvi_offset + dvi_buf_size;
854 dvi_ptr = 0;
855 } else {
856 write_dvi(half_buf, dvi_buf_size - 1);
857 dvi_limit = dvi_buf_size;
858 }
859 dvi_gone = dvi_gone + half_buf;
860 }
861
862 /*tex
863
864 The |dvi_four| procedure outputs four bytes in two's complement notation, without
865 risking arithmetic overflow.
866
867 */
868
869 static void dvi_four(int x)
870 {
871 if (x >= 0) {
872 dvi_out(x / 0100000000);
873 } else {
874 x = x + 010000000000;
875 x = x + 010000000000;
876 dvi_out((x / 0100000000) + 128);
877 }
878 x = x % 0100000000;
879 dvi_out(x / 0200000);
880 x = x % 0200000;
881 dvi_out(x / 0400);
882 dvi_out(x % 0400);
883 }
884
885 /*tex
886
887 A mild optimization of the output is performed by the |dvi_pop| routine, which
888 issues a |pop| unless it is possible to cancel a `|push| |pop|' pair. The
889 parameter to |dvi_pop| is the byte address following the old |push| that matches
890 the new |pop|.
891
892 */
893
894 static void dvi_push(void)
895 {
896 dvi_out(push);
897 }
898
899 static void dvi_pop(int l)
900 {
901 if ((l == dvi_offset + dvi_ptr) && (dvi_ptr > 0))
902 decr(dvi_ptr);
903 else
904 dvi_out(pop);
905 }
906
907 /*tex
908
909 Here's a procedure that outputs a font definition. $\Omega$ allows more than 256
910 different fonts per job, so the right font definition command must be selected.
911
912 */
913
914 static void out_cmd(void)
915 {
916 if ((oval < 0x100) && (oval >= 0)) {
917 if ((ocmd != set1) || (oval > 127)) {
918 if ((ocmd == fnt1) && (oval < 64))
919 oval += fnt_num_0;
920 else
921 dvi_out(ocmd);
922 }
923 } else {
924 if ((oval < 0x10000) && (oval >= 0)) {
925 dvi_out(ocmd + 1);
926 } else {
927 if ((oval < 0x1000000) && (oval >= 0)) {
928 dvi_out(ocmd + 2);
929 } else {
930 dvi_out(ocmd + 3);
931 if (oval >= 0) {
932 dvi_out(oval / 0x1000000);
933 } else {
934 oval += 0x40000000;
935 oval += 0x40000000;
936 dvi_out((oval / 0x1000000) + 128);
937 oval = oval % 0x1000000;
938 }
939 dvi_out(oval / 0x10000);
940 oval = oval % 0x10000;
941 }
942 dvi_out(oval / 0x10000);
943 oval = oval % 0x10000;
944 }
945 dvi_out(oval / 0x100);
946 oval = oval % 0x100;
947 }
948 dvi_out(oval);
949 }
950
951 static void dvi_font_def(internal_font_number f)
952 {
953 char *fa;
954 oval = f - 1;
955 ocmd = fnt_def1;
956 out_cmd();
957 dvi_out(font_check_0(f));
958 dvi_out(font_check_1(f));
959 dvi_out(font_check_2(f));
960 dvi_out(font_check_3(f));
961 dvi_four(font_size(f));
962 dvi_four(font_dsize(f));
963 dvi_out(0); /* |font_area(f)| is unused */
964 dvi_out(strlen(font_name(f)));
965 /* Output the font name whose internal number is |f| */
966 fa = font_name(f);
967 while (*fa != '\0') {
968 dvi_out(*fa++);
969 }
970 }
971
972 /*tex
973
974 Versions of \TeX\ intended for small computers might well choose to omit the
975 ideas in the next few parts of this program, since it is not really necessary to
976 optimize the \.{DVI} code by making use of the |w0|, |x0|, |y0|, and |z0|
977 commands. Furthermore, the algorithm that we are about to describe does not
978 pretend to give an optimum reduction in the length of the \.{DVI} code; after
979 all, speed is more important than compactness. But the method is surprisingly
980 effective, and it takes comparatively little time.
981
982 We can best understand the basic idea by first considering a simpler problem that
983 has the same essential characteristics. Given a sequence of digits, say
984 $3\,1\,4\,1\,5\,9\,2\,6\,5\,3\,5\,8\,9$, we want to assign subscripts $d$, $y$,
985 or $z$ to each digit so as to maximize the number of ``$y$-hits'' and
986 ``$z$-hits''; a $y$-hit is an instance of two appearances of the same digit with
987 the subscript $y$, where no $y$'s intervene between the two appearances, and a
988 $z$-hit is defined similarly. For example, the sequence above could be decorated
989 with subscripts as follows:
990 $$3_z\,1_y\,4_d\,1_y\,5_y\,9_d\,2_d\,6_d\,5_y\,3_z\,5_y\,8_d\,9_d.$$ There are
991 three $y$-hits ($1_y\ldots1_y$ and $5_y\ldots5_y\ldots5_y$) and one $z$-hit
992 ($3_z\ldots3_z$); there are no $d$-hits, since the two appearances of $9_d$ have
993 $d$'s between them, but we don't count $d$-hits so it doesn't matter how many
994 there are. These subscripts are analogous to the \.{DVI} commands called
995 \\{down}, $y$, and $z$, and the digits are analogous to different amounts of
996 vertical motion; a $y$-hit or $z$-hit corresponds to the opportunity to use the
997 one-byte commands |y0| or |z0| in a \.{DVI} file.
998
999 \TeX's method of assigning subscripts works like this: Append a new digit, say
1000 $\delta$, to the right of the sequence. Now look back through the sequence until
1001 one of the following things happens: (a)~You see $\delta_y$ or $\delta_z$, and
1002 this was the first time you encountered a $y$ or $z$ subscript, respectively.
1003 Then assign $y$ or $z$ to the new $\delta$; you have scored a hit. (b)~You see
1004 $\delta_d$, and no $y$ subscripts have been encountered so far during this
1005 search. Then change the previous $\delta_d$ to $\delta_y$ (this corresponds to
1006 changing a command in the output buffer), and assign $y$ to the new $\delta$;
1007 it's another hit. (c)~You see $\delta_d$, and a $y$ subscript has been seen but
1008 not a $z$. Change the previous $\delta_d$ to $\delta_z$ and assign $z$ to the new
1009 $\delta$. (d)~You encounter both $y$ and $z$ subscripts before encountering a
1010 suitable $\delta$, or you scan all the way to the front of the sequence. Assign
1011 $d$ to the new $\delta$; this assignment may be changed later.
1012
1013 The subscripts $3_z\,1_y\,4_d\ldots\,$ in the example above were, in fact,
1014 produced by this procedure, as the reader can verify. (Go ahead and try it.)
1015
1016 In order to implement such an idea, \TeX\ maintains a stack of pointers to the
1017 \\{down}, $y$, and $z$ commands that have been generated for the current page.
1018 And there is a similar stack for \\{right}, |w|, and |x| commands. These stacks
1019 are called the down stack and right stack, and their top elements are maintained
1020 in the variables |down_ptr| and |right_ptr|.
1021
1022 Each entry in these stacks contains four fields: The |width| field is the amount
1023 of motion down or to the right; the |location| field is the byte number of the
1024 \.{DVI} command in question (including the appropriate |dvi_offset|); the |vlink|
1025 field points to the next item below this one on the stack; and the |vinfo| field
1026 encodes the options for possible change in the \.{DVI} command.
1027
1028 */
1029
1030 /*tex The \.{DVI} byte number for a movement command: */
1031
1032 #define location(A) varmem[(A)+1].cint
1033
1034 /*tex The heads of the down and right stacks: */
1035
1036 static halfword down_ptr = null;
1037 static halfword right_ptr = null;
1038
1039 /*tex
1040
1041 Here is a subroutine that produces a \.{DVI} command for some specified downward
1042 or rightward motion. It has two parameters: |w| is the amount of motion, and |o|
1043 is either |down1| or |right1|. We use the fact that the command codes have
1044 convenient arithmetic properties: |y1-down1=w1-right1| and |z1-down1=x1-right1|.
1045
1046 */
1047
1048 void movement(scaled w, eight_bits o)
1049 {
1050 small_number mstate; /* have we seen a |y| or |z|? */
1051 halfword p, q; /* current and top nodes on the stack */
1052 int k; /* index into |dvi_buf|, modulo |dvi_buf_size| */
1053 /*tex something todo? */
1054 if (false) {
1055 /*tex new node for the top of the stack */
1056 q = new_node(movement_node, 0);
1057 width(q) = w;
1058 location(q) = dvi_offset + dvi_ptr;
1059 if (o == down1) {
1060 vlink(q) = down_ptr;
1061 down_ptr = q;
1062 } else {
1063 vlink(q) = right_ptr;
1064 right_ptr = q;
1065 }
1066 /*tex
1067
1068 Look at the other stack entries until deciding what sort of \.{DVI}
1069 command to generate; |goto found| if node |p| is a ``hit''.
1070 */
1071 p = vlink(q);
1072 mstate = none_seen;
1073 while (p != null) {
1074 if (width(p) == w) {
1075 /*
1076 Consider a node with matching width;|goto found| if it's a
1077 hit. We might find a valid hit in a |y| or |z| byte that is
1078 already gone from the buffer. But we can't change bytes that
1079 are gone forever; ``the moving finger writes, $\ldots\,\,$.''
1080 */
1081 switch (mstate + vinfo(p)) {
1082 case none_seen + yz_OK:
1083 case none_seen + y_OK:
1084 case z_seen + yz_OK:
1085 case z_seen + y_OK:
1086 if (location(p) < dvi_gone) {
1087 goto NOT_FOUND;
1088 } else {
1089 /* Change buffered instruction to |y| or |w| and |goto found| */
1090 k = location(p) - dvi_offset;
1091 if (k < 0)
1092 k = k + dvi_buf_size;
1093 dvi_buf[k] = (eight_bits) (dvi_buf[k] + y1 - down1);
1094 vinfo(p) = y_here;
1095 goto FOUND;
1096 }
1097 break;
1098 case none_seen + z_OK:
1099 case y_seen + yz_OK:
1100 case y_seen + z_OK:
1101 if (location(p) < dvi_gone) {
1102 goto NOT_FOUND;
1103 } else {
1104 /* Change buffered instruction to |z| or |x| and |goto found| */
1105 k = location(p) - dvi_offset;
1106 if (k < 0)
1107 k = k + dvi_buf_size;
1108 dvi_buf[k] = (eight_bits) (dvi_buf[k] + z1 - down1);
1109 vinfo(p) = z_here;
1110 goto FOUND;
1111 }
1112 break;
1113 case none_seen + y_here:
1114 case none_seen + z_here:
1115 case y_seen + z_here:
1116 case z_seen + y_here:
1117 goto FOUND;
1118 break;
1119 default:
1120 break;
1121 }
1122 } else {
1123 switch (mstate + vinfo(p)) {
1124 case none_seen + y_here:
1125 mstate = y_seen;
1126 break;
1127 case none_seen + z_here:
1128 mstate = z_seen;
1129 break;
1130 case y_seen + z_here:
1131 case z_seen + y_here:
1132 goto NOT_FOUND;
1133 break;
1134 default:
1135 break;
1136 }
1137 }
1138 p = vlink(p);
1139 }
1140 }
1141 NOT_FOUND:
1142 /*tex
1143 Generate a |down| or |right| command for |w| and |return|:
1144 */
1145 if (abs(w) >= 040000000) {
1146 /*tex |down4| or |right4| */
1147 dvi_out(o + 3);
1148 dvi_four(w);
1149 return;
1150 }
1151 if (abs(w) >= 0100000) {
1152 /*tex |down3| or |right3| */
1153 dvi_out(o + 2);
1154 if (w < 0)
1155 w = w + 0100000000;
1156 dvi_out(w / 0200000);
1157 w = w % 0200000;
1158 goto TWO;
1159 }
1160 if (abs(w) >= 0200) {
1161 /*tex |down2| or |right2| */
1162 dvi_out(o + 1);
1163 if (w < 0)
1164 w = w + 0200000;
1165 goto TWO;
1166 }
1167 /*tex |down1| or |right1| */
1168 dvi_out(o);
1169 if (w < 0)
1170 w = w + 0400;
1171 goto ONE;
1172 TWO:
1173 dvi_out(w / 0400);
1174 ONE:
1175 dvi_out(w % 0400);
1176 return;
1177 FOUND:
1178 /*tex
1179
1180 Generate a |y0| or |z0| command in order to reuse a previous appearance
1181 of~|w|.
1182
1183 The program below removes movement nodes that are introduced after a
1184 |push|, before it outputs the corresponding |pop|.
1185
1186 When the |movement| procedure gets to the label |found|, the value of
1187 |vinfo(p)| will be either |y_here| or |z_here|. If it is, say, |y_here|,
1188 the procedure generates a |y0| command (or a |w0| command), and marks all
1189 |vinfo| fields between |q| and |p| so that |y| is not OK in that range.
1190
1191 */
1192 vinfo(q) = vinfo(p);
1193 if (vinfo(q) == y_here) {
1194 /*tex |y0| or |w0| */
1195 dvi_out(o + y0 - down1);
1196 while (vlink(q) != p) {
1197 q = vlink(q);
1198 switch (vinfo(q)) {
1199 case yz_OK:
1200 vinfo(q) = z_OK;
1201 break;
1202 case y_OK:
1203 vinfo(q) = d_fixed;
1204 break;
1205 default:
1206 break;
1207 }
1208 }
1209 } else {
1210 /*tex |z0| or |x0| */
1211 dvi_out(o + z0 - down1);
1212 while (vlink(q) != p) {
1213 q = vlink(q);
1214 switch (vinfo(q)) {
1215 case yz_OK:
1216 vinfo(q) = y_OK;
1217 break;
1218 case z_OK:
1219 vinfo(q) = d_fixed;
1220 break;
1221 default:
1222 break;
1223 }
1224 }
1225 }
1226 }
1227
1228 /*tex
1229
1230 In case you are wondering when all the movement nodes are removed from \TeX's
1231 memory, the answer is that they are recycled just before |hlist_out| and
1232 |vlist_out| finish outputting a box. This restores the down and right stacks to
1233 the state they were in before the box was output, except that some |vinfo|'s may
1234 have become more restrictive.
1235
1236 Here we delete movement nodes with |location>=l|:
1237
1238 */
1239
1240 static void prune_movements(int l)
1241 {
1242 pointer p;
1243 while (down_ptr != null) {
1244 if (location(down_ptr) < l)
1245 break;
1246 p = down_ptr;
1247 down_ptr = vlink(p);
1248 flush_node(p);
1249 }
1250 while (right_ptr != null) {
1251 if (location(right_ptr) < l)
1252 return;
1253 p = right_ptr;
1254 right_ptr = vlink(p);
1255 flush_node(p);
1256 }
1257 }
1258
1259 /*tex
1260
1261 When |hlist_out| is called, its duty is to output the box represented by the
1262 |hlist_node| pointed to by |temp_ptr|. The reference point of that box has
1263 coordinates |(cur.h,cur.v)|.
1264
1265 Similarly, when |vlist_out| is called, its duty is to output the box represented
1266 by the |vlist_node| pointed to by |temp_ptr|. The reference point of that box has
1267 coordinates |(cur.h,cur.v)|.
1268
1269 The recursive procedures |hlist_out| and |vlist_out| each have a local variable
1270 |save_dvi| to hold the value of |dvi| just before entering a new level of
1271 recursion. In effect, the value of |save_dvi| on \TeX's run-time stack
1272 corresponds to the values of |h| and |v| that a \.{DVI}-reading program will push
1273 onto its coordinate stack.
1274
1275 */
1276
1277 void dvi_place_rule(PDF pdf, halfword q, scaledpos size,int callback_id)
1278 {
1279 (void)callback_id;/*tex to be consistent with pdf_place_rule */
1280 synch_dvi_with_pos(pdf->posstruct->pos);
1281 if ((subtype(q) >= box_rule) && (subtype(q) <= user_rule)) {
1282 /*tex place nothing, only take space */
1283 if (textdir_is_L(pdf->posstruct->dir))
1284 dvi.h += size.h;
1285 } else {
1286 /*tex normal_rule or >= 100 being a leader rule */
1287 if (textdir_is_L(pdf->posstruct->dir)) {
1288 /*tex movement optimization for |dir_*L*| */
1289 dvi_out(set_rule);
1290 dvi.h += size.h;
1291 } else
1292 dvi_out(put_rule);
1293 }
1294 dvi_four(size.v);
1295 dvi_four(size.h);
1296 }
1297
1298 void dvi_place_glyph(PDF pdf, internal_font_number f, int c, int ex)
1299 {
1300 scaled_whd ci;
1301 synch_dvi_with_pos(pdf->posstruct->pos);
1302 if (f != pdf->f_cur) {
1303 /*tex Change font |f_cur| to |f| */
1304 if (!font_used(f)) {
1305 dvi_font_def(f);
1306 set_font_used(f, true);
1307 }
1308 oval = f - 1;
1309 ocmd = fnt1;
1310 out_cmd();
1311 pdf->f_cur = f;
1312 }
1313 if (textdir_is_L(pdf->posstruct->dir)) {
1314 ci = get_charinfo_whd(f, c);
1315 /*tex movement optimization for |dir_*L*| */
1316 dvi_set(c, ci.wd);
1317 } else {
1318 dvi_put(c);
1319 }
1320 }
1321
1322 void dvi_special(PDF pdf, halfword p)
1323 {
1324 /*tex holds print |selector| */
1325 int old_setting;
1326 /*tex index into |cur_string| */
1327 unsigned k;
1328 halfword h;
1329 synch_dvi_with_pos(pdf->posstruct->pos);
1330 old_setting = selector;
1331 if (subtype(p) == late_special_node ) {
1332 expand_macros_in_tokenlist(special_tokens(p));
1333 h = token_link(def_ref);
1334 } else {
1335 h = token_link(special_tokens(p));
1336 }
1337 selector = new_string;
1338 show_token_list(h, null, -1);
1339 selector = old_setting;
1340 if (subtype(p) == late_special_node ) {
1341 flush_list(def_ref);
1342 }
1343 if (cur_length < 256) {
1344 dvi_out(xxx1);
1345 dvi_out(cur_length);
1346 } else {
1347 dvi_out(xxx4);
1348 dvi_four((int) cur_length);
1349 }
1350 for (k = 0; k < cur_length; k++) {
1351 dvi_out(cur_string[k]);
1352 }
1353 /*tex erase the string */
1354 cur_length = 0;
1355 }
1356
1357 /*tex
1358
1359 Here's an example of how these conventions are used. Whenever it is time to ship
1360 out a box of stuff, we shall use the macro |ensure_dvi_open|.
1361
1362 */
1363
1364 void dvi_write_header(PDF pdf)
1365 {
1366 unsigned l;
1367 /*tex index into |str_pool| */
1368 unsigned s;
1369 /*tex saved |selector| setting */
1370 int old_setting;
1371 if (half_buf == 0) {
1372 half_buf = dvi_buf_size / 2;
1373 dvi_limit = dvi_buf_size;
1374 }
1375 dvi_out(pre);
1376 /*tex output the preamble */
1377 dvi_out(id_byte);
1378 dvi_four(25400000);
1379 /*tex conversion ratio for sp */
1380 dvi_four(473628672);
1381 prepare_mag();
1382 /*tex magnification factor is frozen */
1383 dvi_four(mag_par);
1384 if (output_comment) {
1385 l = (unsigned) strlen(output_comment);
1386 dvi_out(l);
1387 for (s = 0; s < l; s++) {
1388 dvi_out(output_comment[s]);
1389 }
1390 } else {
1391 /*tex the default code is unchanged */
1392 old_setting = selector;
1393 selector = new_string;
1394 tprint(" LuaTeX output ");
1395 print_int(year_par);
1396 print_char('.');
1397 print_two(month_par);
1398 print_char('.');
1399 print_two(day_par);
1400 print_char(':');
1401 print_two(time_par / 60);
1402 print_two(time_par % 60);
1403 selector = old_setting;
1404 dvi_out(cur_length);
1405 for (s = 0; s < cur_length; s++)
1406 dvi_out(cur_string[s]);
1407 cur_length = 0;
1408 }
1409 }
1410
1411 void dvi_begin_page(PDF pdf)
1412 {
1413 int k;
1414 /*tex location of the current |bop| */
1415 int page_loc;
1416 ensure_output_state(pdf, ST_HEADER_WRITTEN);
1417 /*tex Initialize variables as |ship_out| begins */
1418 page_loc = dvi_offset + dvi_ptr;
1419 dvi_out(bop);
1420 for (k = 0; k <= 9; k++)
1421 dvi_four(count(k));
1422 dvi_four(last_bop);
1423 last_bop = page_loc;
1424 }
1425
1426 void dvi_end_page(PDF pdf)
1427 {
1428 (void) pdf;
1429 if (check_dvi_total_pages && total_pages>=65536){
1430 print_err(" ==> Fatal error occurred: total_pages>=65536, bad output DVI file produced!");
1431 jump_out();
1432 }
1433 dvi_out(eop);
1434 }
1435
1436 /*tex
1437
1438 At the end of the program, we must finish things off by writing the post\-amble.
1439 If |total_pages=0|, the \.{DVI} file was never opened. If |total_pages>=65536|,
1440 the \.{DVI} file will lie, and if the option |check_dvi_total_pages| is true, the program abort.
1441 And if |max_push>=65536|, the user deserves whatever
1442 chaos might ensue.
1443
1444 */
1445
1446 void dvi_open_file(PDF pdf) {
1447 ensure_output_file_open(pdf, ".dvi");
1448 }
1449
1450 void dvi_finish_file(PDF pdf, int fatal_error)
1451 {
1452 int k;
1453 int callback_id = callback_defined(stop_run_callback);
1454 if (fatal_error) {
1455 print_err(" ==> Fatal error occurred, bad output DVI file produced!");
1456 }
1457 while (cur_s > -1) {
1458 if (cur_s > 0) {
1459 dvi_out(pop);
1460 } else {
1461 dvi_out(eop);
1462 incr(total_pages);
1463 }
1464 decr(cur_s);
1465 }
1466 if (total_pages == 0) {
1467 if (callback_id == 0) {
1468 tprint_nl("No pages of output.");
1469 print_ln();
1470 } else if (callback_id > 0) {
1471 run_callback(callback_id, "->");
1472 }
1473 } else {
1474 /*tex beginning of the postamble */
1475 dvi_out(post);
1476 dvi_four(last_bop);
1477 last_bop = dvi_offset + dvi_ptr - 5;
1478 /*tex |post| location */
1479 dvi_four(25400000);
1480 /*tex conversion ratio for sp */
1481 dvi_four(473628672);
1482 prepare_mag();
1483 /*tex magnification factor */
1484 dvi_four(mag_par);
1485 dvi_four(max_v);
1486 dvi_four(max_h);
1487 dvi_out(max_push / 256);
1488 dvi_out(max_push % 256);
1489 dvi_out((total_pages / 256) % 256);
1490 dvi_out(total_pages % 256);
1491 /*tex Output the font definitions for all fonts that were used */
1492 k = max_font_id();
1493 while (k > 0) {
1494 if (font_used(k)) {
1495 dvi_font_def(k);
1496 }
1497 decr(k);
1498 }
1499 dvi_out(post_post);
1500 dvi_four(last_bop);
1501 dvi_out(id_byte);
1502 /*tex the number of 223's */
1503 #ifndef IPC
1504 k = 4 + ((dvi_buf_size - dvi_ptr) % 4);
1505 #else
1506 k = 7 - ((3 + dvi_offset + dvi_ptr) % 4);
1507 #endif
1508 while (k > 0) {
1509 dvi_out(223);
1510 decr(k);
1511 }
1512 /*tex
1513 Here is how we clean out the buffer when \TeX\ is all through;
1514 |dvi_ptr| will be a multiple of~4.
1515 */
1516 if (dvi_limit == half_buf)
1517 write_dvi(half_buf, dvi_buf_size - 1);
1518 if (dvi_ptr > 0)
1519 write_dvi(0, dvi_ptr - 1);
1520 if (callback_id == 0) {
1521 tprint_nl("Output written on ");
1522 tprint(pdf->file_name);
1523 tprint(" (");
1524 print_int(total_pages);
1525 tprint(" page");
1526 if (total_pages != 1)
1527 print_char('s');
1528 tprint(", ");
1529 print_int(dvi_offset + dvi_ptr);
1530 tprint(" bytes).");
1531 } else if (callback_id > 0) {
1532 run_callback(callback_id, "->");
1533 }
1534 close_file(pdf->file);
1535 }
1536 }
1537
1538 void dvi_push_list(PDF pdf, scaledpos *saved_pos, int *saved_loc)
1539 {
1540 if (cur_s > max_push) {
1541 max_push = cur_s;
1542 }
1543 if (cur_s > 0) {
1544 dvi_push();
1545 *saved_pos = dvi;
1546 }
1547 *saved_loc = dvi_offset + dvi_ptr;
1548 }
1549
1550 void dvi_pop_list(PDF pdf, scaledpos *saved_pos, int *saved_loc)
1551 {
1552 prune_movements(*saved_loc);
1553 if (cur_s > 0) {
1554 dvi_pop(*saved_loc);
1555 dvi = *saved_pos;
1556 }
1557 }
1558
1559 void dvi_set_reference_point(PDF pdf, posstructure *refpoint)
1560 {
1561 refpoint->pos.h = one_true_inch;
1562 refpoint->pos.v = pdf->page_size.v - one_true_inch;
1563 dvi = refpoint->pos;
1564 }
1565
1566 int dvi_get_status_ptr(PDF pdf)
1567 {
1568 return dvi_ptr;
1569 }
1570
1571 int dvi_get_status_gone(PDF pdf)
1572 {
1573 return dvi_gone;
1574 }