beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / dvi / dvigen.w
blob0a3ca08ea14c95bedc7b18a1da2ed1ec9b108942
1 % dvigen.w
3 % Copyright 2009-2013 Taco Hoekwater <taco@@luatex.org>
5 % This file is part of LuaTeX.
7 % LuaTeX is free software; you can redistribute it and/or modify it under
8 % the terms of the GNU General Public License as published by the Free
9 % Software Foundation; either version 2 of the License, or (at your
10 % option) any later version.
12 % LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 % ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 % FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 % License for more details.
17 % You should have received a copy of the GNU General Public License along
18 % with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
20 \def\MF{MetaFont}
21 \def\MP{MetaPost}
22 \def\PASCAL{Pascal}
23 \def\[#1]{#1}
24 \pdfoutput=1
25 \pdfmapline{cmtex10 < cmtex10.pfb}
26 \pdfmapfile{pdftex.map}
28 \title{: generation of DVI output}
30 @ Initial identification of this file, and the needed headers.
32 #include "ptexlib.h"
34 @ Here is the start of the actual C file.
36 #undef write_dvi
38 /* todo: move macros to api */
40 #define mode cur_list.mode_field /* current mode */
42 #define mag int_par(mag_code)
43 #define tracing_output int_par(tracing_output_code)
44 #define tracing_stats int_par(tracing_stats_code)
45 #define tracing_online int_par(tracing_online_code)
46 #define page_width dimen_par(page_width_code)
47 #define page_height dimen_par(page_height_code)
48 #define page_left_offset dimen_par(page_left_offset_code)
49 #define page_right_offset dimen_par(page_right_offset_code)
50 #define page_top_offset dimen_par(page_top_offset_code)
51 #define page_bottom_offset dimen_par(page_bottom_offset_code)
52 #define h_offset dimen_par(h_offset_code)
53 #define v_offset dimen_par(v_offset_code)
55 #define count(A) eqtb[count_base+(A)].cint
57 @ The most important output produced by a run of \TeX\ is the ``device
58 independent'' (\.{DVI}) file that specifies where characters and rules
59 are to appear on printed pages. The form of these files was designed by
60 David R. Fuchs in 1979. Almost any reasonable typesetting device can be
61 @^Fuchs, David Raymond@>
62 @:DVI_files}{\.{DVI} files@>
63 driven by a program that takes \.{DVI} files as input, and dozens of such
64 \.{DVI}-to-whatever programs have been written. Thus, it is possible to
65 print the output of \TeX\ on many different kinds of equipment, using \TeX\
66 as a device-independent ``front end.''
68 A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a
69 series of commands in a machine-like language. The first byte of each command
70 is the operation code, and this code is followed by zero or more bytes
71 that provide parameters to the command. The parameters themselves may consist
72 of several consecutive bytes; for example, the `|set_rule|' command has two
73 parameters, each of which is four bytes long. Parameters are usually
74 regarded as nonnegative integers; but four-byte-long parameters,
75 and shorter parameters that denote distances, can be
76 either positive or negative. Such parameters are given in two's complement
77 notation. For example, a two-byte-long distance parameter has a value between
78 $-2^{15}$ and $2^{15}-1$. As in \.{TFM} files, numbers that occupy
79 more than one byte position appear in BigEndian order.
81 A \.{DVI} file consists of a ``preamble,'' followed by a sequence of one
82 or more ``pages,'' followed by a ``postamble.'' The preamble is simply a
83 |pre| command, with its parameters that define the dimensions used in the
84 file; this must come first. Each ``page'' consists of a |bop| command,
85 followed by any number of other commands that tell where characters are to
86 be placed on a physical page, followed by an |eop| command. The pages
87 appear in the order that \TeX\ generated them. If we ignore |nop| commands
88 and \\{fnt\_def} commands (which are allowed between any two commands in
89 the file), each |eop| command is immediately followed by a |bop| command,
90 or by a |post| command; in the latter case, there are no more pages in the
91 file, and the remaining bytes form the postamble. Further details about
92 the postamble will be explained later.
94 Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
95 quantities that give the location number of some other byte in the file;
96 the first byte is number~0, then comes number~1, and so on. For example,
97 one of the parameters of a |bop| command points to the previous |bop|;
98 this makes it feasible to read the pages in backwards order, in case the
99 results are being directed to a device that stacks its output face up.
100 Suppose the preamble of a \.{DVI} file occupies bytes 0 to 99. Now if the
101 first page occupies bytes 100 to 999, say, and if the second
102 page occupies bytes 1000 to 1999, then the |bop| that starts in byte 1000
103 points to 100 and the |bop| that starts in byte 2000 points to 1000. (The
104 very first |bop|, i.e., the one starting in byte 100, has a pointer of~$-1$.)
106 @ The \.{DVI} format is intended to be both compact and easily interpreted
107 by a machine. Compactness is achieved by making most of the information
108 implicit instead of explicit. When a \.{DVI}-reading program reads the
109 commands for a page, it keeps track of several quantities: (a)~The current
110 font |f| is an integer; this value is changed only
111 by \\{fnt} and \\{fnt\_num} commands. (b)~The current position on the page
112 is given by two numbers called the horizontal and vertical coordinates,
113 |h| and |v|. Both coordinates are zero at the upper left corner of the page;
114 moving to the right corresponds to increasing the horizontal coordinate, and
115 moving down corresponds to increasing the vertical coordinate. Thus, the
116 coordinates are essentially Cartesian, except that vertical directions are
117 flipped; the Cartesian version of |(h,v)| would be |(h,-v)|. (c)~The
118 current spacing amounts are given by four numbers |w|, |x|, |y|, and |z|,
119 where |w| and~|x| are used for horizontal spacing and where |y| and~|z|
120 are used for vertical spacing. (d)~There is a stack containing
121 |(h,v,w,x,y,z)| values; the \.{DVI} commands |push| and |pop| are used to
122 change the current level of operation. Note that the current font~|f| is
123 not pushed and popped; the stack contains only information about
124 positioning.
126 The values of |h|, |v|, |w|, |x|, |y|, and |z| are signed integers having up
127 to 32 bits, including the sign. Since they represent physical distances,
128 there is a small unit of measurement such that increasing |h| by~1 means
129 moving a certain tiny distance to the right. The actual unit of
130 measurement is variable, as explained below; \TeX\ sets things up so that
131 its \.{DVI} output is in sp units, i.e., scaled points, in agreement with
132 all the |scaled| dimensions in \TeX's data structures.
134 @ Here is a list of all the commands that may appear in a \.{DVI} file. Each
135 command is specified by its symbolic name (e.g., |bop|), its opcode byte
136 (e.g., 139), and its parameters (if any). The parameters are followed
137 by a bracketed number telling how many bytes they occupy; for example,
138 `|p[4]|' means that parameter |p| is four bytes long.
140 \yskip\hang|set_char_0| 0. Typeset character number~0 from font~|f|
141 such that the reference point of the character is at |(h,v)|. Then
142 increase |h| by the width of that character. Note that a character may
143 have zero or negative width, so one cannot be sure that |h| will advance
144 after this command; but |h| usually does increase.
146 \yskip\hang\\{set\_char\_1} through \\{set\_char\_127} (opcodes 1 to 127).
147 Do the operations of |set_char_0|; but use the character whose number
148 matches the opcode, instead of character~0.
150 \yskip\hang|set1| 128 |c[1]|. Same as |set_char_0|, except that character
151 number~|c| is typeset. \TeX82 uses this command for characters in the
152 range |128<=c<256|.
154 \yskip\hang|@!set2| 129 |c[2]|. Same as |set1|, except that |c|~is two
155 bytes long, so it is in the range |0<=c<65536|. \TeX82 never uses this
156 command, but it should come in handy for extensions of \TeX\ that deal
157 with oriental languages.
158 @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
160 \yskip\hang|@!set3| 130 |c[3]|. Same as |set1|, except that |c|~is three
161 bytes long, so it can be as large as $2^{24}-1$. Not even the Chinese
162 language has this many characters, but this command might prove useful
163 in some yet unforeseen extension.
165 \yskip\hang|@!set4| 131 |c[4]|. Same as |set1|, except that |c|~is four
166 bytes long. Imagine that.
168 \yskip\hang|set_rule| 132 |a[4]| |b[4]|. Typeset a solid black rectangle
169 of height~|a| and width~|b|, with its bottom left corner at |(h,v)|. Then
170 set |h:=h+b|. If either |a<=0| or |b<=0|, nothing should be typeset. Note
171 that if |b<0|, the value of |h| will decrease even though nothing else happens.
172 See below for details about how to typeset rules so that consistency with
173 \MF\ is guaranteed.
175 \yskip\hang|@!put1| 133 |c[1]|. Typeset character number~|c| from font~|f|
176 such that the reference point of the character is at |(h,v)|. (The `put'
177 commands are exactly like the `set' commands, except that they simply put out a
178 character or a rule without moving the reference point afterwards.)
180 \yskip\hang|@!put2| 134 |c[2]|. Same as |set2|, except that |h| is not changed.
182 \yskip\hang|@!put3| 135 |c[3]|. Same as |set3|, except that |h| is not changed.
184 \yskip\hang|@!put4| 136 |c[4]|. Same as |set4|, except that |h| is not changed.
186 \yskip\hang|put_rule| 137 |a[4]| |b[4]|. Same as |set_rule|, except that
187 |h| is not changed.
189 \yskip\hang|nop| 138. No operation, do nothing. Any number of |nop|'s
190 may occur between \.{DVI} commands, but a |nop| cannot be inserted between
191 a command and its parameters or between two parameters.
193 \yskip\hang|bop| 139 $c_0[4]$ $c_1[4]$ $\ldots$ $c_9[4]$ $p[4]$. Beginning
194 of a page: Set |(h,v,w,x,y,z):=(0,0,0,0,0,0)| and set the stack empty. Set
195 the current font |f| to an undefined value. The ten $c_i$ parameters hold
196 the values of \.{\\count0} $\ldots$ \.{\\count9} in \TeX\ at the time
197 \.{\\shipout} was invoked for this page; they can be used to identify
198 pages, if a user wants to print only part of a \.{DVI} file. The parameter
199 |p| points to the previous |bop| in the file; the first
200 |bop| has $p=-1$.
202 \yskip\hang|eop| 140. End of page: Print what you have read since the
203 previous |bop|. At this point the stack should be empty. (The \.{DVI}-reading
204 programs that drive most output devices will have kept a buffer of the
205 material that appears on the page that has just ended. This material is
206 largely, but not entirely, in order by |v| coordinate and (for fixed |v|) by
207 |h|~coordinate; so it usually needs to be sorted into some order that is
208 appropriate for the device in question.)
210 \yskip\hang|push| 141. Push the current values of |(h,v,w,x,y,z)| onto the
211 top of the stack; do not change any of these values. Note that |f| is
212 not pushed.
214 \yskip\hang|pop| 142. Pop the top six values off of the stack and assign
215 them respectively to |(h,v,w,x,y,z)|. The number of pops should never
216 exceed the number of pushes, since it would be highly embarrassing if the
217 stack were empty at the time of a |pop| command.
219 \yskip\hang|right1| 143 |b[1]|. Set |h:=h+b|, i.e., move right |b| units.
220 The parameter is a signed number in two's complement notation, |-128<=b<128|;
221 if |b<0|, the reference point moves left.
223 \yskip\hang|right2| 144 |b[2]|. Same as |right1|, except that |b| is a
224 two-byte quantity in the range |-32768<=b<32768|.
226 \yskip\hang|right3| 145 |b[3]|. Same as |right1|, except that |b| is a
227 three-byte quantity in the range |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
229 \yskip\hang|right4| 146 |b[4]|. Same as |right1|, except that |b| is a
230 four-byte quantity in the range |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
232 \yskip\hang|w0| 147. Set |h:=h+w|; i.e., move right |w| units. With luck,
233 this parameterless command will usually suffice, because the same kind of motion
234 will occur several times in succession; the following commands explain how
235 |w| gets particular values.
237 \yskip\hang|w1| 148 |b[1]|. Set |w:=b| and |h:=h+b|. The value of |b| is a
238 signed quantity in two's complement notation, |-128<=b<128|. This command
239 changes the current |w|~spacing and moves right by |b|.
241 \yskip\hang|@!w2| 149 |b[2]|. Same as |w1|, but |b| is two bytes long,
242 |-32768<=b<32768|.
244 \yskip\hang|@!w3| 150 |b[3]|. Same as |w1|, but |b| is three bytes long,
245 |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
247 \yskip\hang|@!w4| 151 |b[4]|. Same as |w1|, but |b| is four bytes long,
248 |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
250 \yskip\hang|x0| 152. Set |h:=h+x|; i.e., move right |x| units. The `|x|'
251 commands are like the `|w|' commands except that they involve |x| instead
252 of |w|.
254 \yskip\hang|x1| 153 |b[1]|. Set |x:=b| and |h:=h+b|. The value of |b| is a
255 signed quantity in two's complement notation, |-128<=b<128|. This command
256 changes the current |x|~spacing and moves right by |b|.
258 \yskip\hang|@!x2| 154 |b[2]|. Same as |x1|, but |b| is two bytes long,
259 |-32768<=b<32768|.
261 \yskip\hang|@!x3| 155 |b[3]|. Same as |x1|, but |b| is three bytes long,
262 |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
264 \yskip\hang|@!x4| 156 |b[4]|. Same as |x1|, but |b| is four bytes long,
265 |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
267 \yskip\hang|down1| 157 |a[1]|. Set |v:=v+a|, i.e., move down |a| units.
268 The parameter is a signed number in two's complement notation, |-128<=a<128|;
269 if |a<0|, the reference point moves up.
271 \yskip\hang|@!down2| 158 |a[2]|. Same as |down1|, except that |a| is a
272 two-byte quantity in the range |-32768<=a<32768|.
274 \yskip\hang|@!down3| 159 |a[3]|. Same as |down1|, except that |a| is a
275 three-byte quantity in the range |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
277 \yskip\hang|@!down4| 160 |a[4]|. Same as |down1|, except that |a| is a
278 four-byte quantity in the range |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
280 \yskip\hang|y0| 161. Set |v:=v+y|; i.e., move down |y| units. With luck,
281 this parameterless command will usually suffice, because the same kind of motion
282 will occur several times in succession; the following commands explain how
283 |y| gets particular values.
285 \yskip\hang|y1| 162 |a[1]|. Set |y:=a| and |v:=v+a|. The value of |a| is a
286 signed quantity in two's complement notation, |-128<=a<128|. This command
287 changes the current |y|~spacing and moves down by |a|.
289 \yskip\hang|@!y2| 163 |a[2]|. Same as |y1|, but |a| is two bytes long,
290 |-32768<=a<32768|.
292 \yskip\hang|@!y3| 164 |a[3]|. Same as |y1|, but |a| is three bytes long,
293 |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
295 \yskip\hang|@!y4| 165 |a[4]|. Same as |y1|, but |a| is four bytes long,
296 |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
298 \yskip\hang|z0| 166. Set |v:=v+z|; i.e., move down |z| units. The `|z|' commands
299 are like the `|y|' commands except that they involve |z| instead of |y|.
301 \yskip\hang|z1| 167 |a[1]|. Set |z:=a| and |v:=v+a|. The value of |a| is a
302 signed quantity in two's complement notation, |-128<=a<128|. This command
303 changes the current |z|~spacing and moves down by |a|.
305 \yskip\hang|@!z2| 168 |a[2]|. Same as |z1|, but |a| is two bytes long,
306 |-32768<=a<32768|.
308 \yskip\hang|@!z3| 169 |a[3]|. Same as |z1|, but |a| is three bytes long,
309 |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
311 \yskip\hang|@!z4| 170 |a[4]|. Same as |z1|, but |a| is four bytes long,
312 |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
314 \yskip\hang|fnt_num_0| 171. Set |f:=0|. Font 0 must previously have been
315 defined by a \\{fnt\_def} instruction, as explained below.
317 \yskip\hang\\{fnt\_num\_1} through \\{fnt\_num\_63} (opcodes 172 to 234). Set
318 |f:=1|, \dots, \hbox{|f:=63|}, respectively.
320 \yskip\hang|fnt1| 235 |k[1]|. Set |f:=k|. \TeX82 uses this command for font
321 numbers in the range |64<=k<256|.
323 \yskip\hang|@!fnt2| 236 |k[2]|. Same as |fnt1|, except that |k|~is two
324 bytes long, so it is in the range |0<=k<65536|. \TeX82 never generates this
325 command, but large font numbers may prove useful for specifications of
326 color or texture, or they may be used for special fonts that have fixed
327 numbers in some external coding scheme.
329 \yskip\hang|@!fnt3| 237 |k[3]|. Same as |fnt1|, except that |k|~is three
330 bytes long, so it can be as large as $2^{24}-1$.
332 \yskip\hang|@!fnt4| 238 |k[4]|. Same as |fnt1|, except that |k|~is four
333 bytes long; this is for the really big font numbers (and for the negative ones).
335 \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
336 general; it functions as a $(k+2)$-byte |nop| unless special \.{DVI}-reading
337 programs are being used. \TeX82 generates |xxx1| when a short enough
338 \.{\\special} appears, setting |k| to the number of bytes being sent. It
339 is recommended that |x| be a string having the form of a keyword followed
340 by possible parameters relevant to that keyword.
342 \yskip\hang|@!xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
344 \yskip\hang|@!xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
346 \yskip\hang|xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously
347 large. \TeX82 uses |xxx4| when sending a string of length 256 or more.
349 \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
350 Define font |k|, where |0<=k<256|; font definitions will be explained shortly.
352 \yskip\hang|@!fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
353 Define font |k|, where |0<=k<65536|.
355 \yskip\hang|@!fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
356 Define font |k|, where |0<=k<@t$2^{24}$@>|.
358 \yskip\hang|@!fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
359 Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
361 \yskip\hang|pre| 247 |i[1]| |num[4]| |den[4]| |mag[4]| |k[1]| |x[k]|.
362 Beginning of the preamble; this must come at the very beginning of the
363 file. Parameters |i|, |num|, |den|, |mag|, |k|, and |x| are explained below.
365 \yskip\hang|post| 248. Beginning of the postamble, see below.
367 \yskip\hang|post_post| 249. Ending of the postamble, see below.
369 \yskip\noindent Commands 250--255 are undefined at the present time.
372 #define set_char_0 0 /* typeset character 0 and move right */
373 #define set1 128 /* typeset a character and move right */
374 #define set_rule 132 /* typeset a rule and move right */
375 #define put1 133 /* typeset a character without moving */
376 #define put_rule 137 /* typeset a rule */
377 #define nop 138 /* no operation */
378 #define bop 139 /* beginning of page */
379 #define eop 140 /* ending of page */
380 #define push 141 /* save the current positions */
381 #define pop 142 /* restore previous positions */
382 #define right1 143 /* move right */
383 #define right4 146 /* move right, 4 bytes */
384 #define w0 147 /* move right by |w| */
385 #define w1 148 /* move right and set |w| */
386 #define x0 152 /* move right by |x| */
387 #define x1 153 /* move right and set |x| */
388 #define down1 157 /* move down */
389 #define down4 160 /* move down, 4 bytes */
390 #define y0 161 /* move down by |y| */
391 #define y1 162 /* move down and set |y| */
392 #define z0 166 /* move down by |z| */
393 #define z1 167 /* move down and set |z| */
394 #define fnt_num_0 171 /* set current font to 0 */
395 #define fnt1 235 /* set current font */
396 #define xxx1 239 /* extension to \.{DVI} primitives */
397 #define xxx4 242 /* potentially long extension to \.{DVI} primitives */
398 #define fnt_def1 243 /* define the meaning of a font number */
399 #define pre 247 /* preamble */
400 #define post 248 /* postamble beginning */
401 #define post_post 249 /* postamble ending */
403 @ The preamble contains basic information about the file as a whole. As
404 stated above, there are six parameters:
405 $$\hbox{|@!i[1]| |@!num[4]| |@!den[4]| |@!mag[4]| |@!k[1]| |@!x[k]|.}$$
406 The |i| byte identifies \.{DVI} format; currently this byte is always set
407 to~2. (The value |i=3| is currently used for an extended format that
408 allows a mixture of right-to-left and left-to-right typesetting.
409 Some day we will set |i=4|, when \.{DVI} format makes another
410 incompatible change---perhaps in the year 2048.)
412 The next two parameters, |num| and |den|, are positive integers that define
413 the units of measurement; they are the numerator and denominator of a
414 fraction by which all dimensions in the \.{DVI} file could be multiplied
415 in order to get lengths in units of $10^{-7}$ meters. Since $\rm 7227{pt} =
416 254{cm}$, and since \TeX\ works with scaled points where there are $2^{16}$
417 sp in a point, \TeX\ sets
418 $|num|/|den|=(254\cdot10^5)/(7227\cdot2^{16})=25400000/473628672$.
419 @^sp@>
421 The |mag| parameter is what \TeX\ calls \.{\\mag}, i.e., 1000 times the
422 desired magnification. The actual fraction by which dimensions are
423 multiplied is therefore $|mag|\cdot|num|/1000|den|$. Note that if a \TeX\
424 source document does not call for any `\.{true}' dimensions, and if you
425 change it only by specifying a different \.{\\mag} setting, the \.{DVI}
426 file that \TeX\ creates will be completely unchanged except for the value
427 of |mag| in the preamble and postamble. (Fancy \.{DVI}-reading programs allow
428 users to override the |mag|~setting when a \.{DVI} file is being printed.)
430 Finally, |k| and |x| allow the \.{DVI} writer to include a comment, which is not
431 interpreted further. The length of comment |x| is |k|, where |0<=k<256|.
435 #define id_byte 2 /* identifies the kind of \.{DVI} files described here */
437 @ Font definitions for a given font number |k| contain further parameters
438 $$\hbox{|c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.}$$
439 The four-byte value |c| is the check sum that \TeX\ found in the \.{TFM}
440 file for this font; |c| should match the check sum of the font found by
441 programs that read this \.{DVI} file.
442 @^check sum@>
444 Parameter |s| contains a fixed-point scale factor that is applied to
445 the character widths in font |k|; font dimensions in \.{TFM} files and
446 other font files are relative to this quantity, which is called the
447 ``at size'' elsewhere in this documentation. The value of |s| is
448 always positive and less than $2^{27}$. It is given in the same units
449 as the other \.{DVI} dimensions, i.e., in sp when \TeX82 has made the
450 file. Parameter |d| is similar to |s|; it is the ``design size,'' and
451 (like~|s|) it is given in \.{DVI} units. Thus, font |k| is to be used
452 at $|mag|\cdot s/1000d$ times its normal size.
454 The remaining part of a font definition gives the external name of the font,
455 which is an ASCII string of length |a+l|. The number |a| is the length
456 of the ``area'' or directory, and |l| is the length of the font name itself;
457 the standard local system font area is supposed to be used when |a=0|.
458 The |n| field contains the area in its first |a| bytes.
460 Font definitions must appear before the first use of a particular font number.
461 Once font |k| is defined, it must not be defined again; however, we
462 shall see below that font definitions appear in the postamble as well as
463 in the pages, so in this sense each font number is defined exactly twice,
464 if at all. Like |nop| commands, font definitions can
465 appear before the first |bop|, or between an |eop| and a |bop|.
467 @ Sometimes it is desirable to make horizontal or vertical rules line up
468 precisely with certain features in characters of a font. It is possible to
469 guarantee the correct matching between \.{DVI} output and the characters
470 generated by \MF\ by adhering to the following principles: (1)~The \MF\
471 characters should be positioned so that a bottom edge or left edge that is
472 supposed to line up with the bottom or left edge of a rule appears at the
473 reference point, i.e., in row~0 and column~0 of the \MF\ raster. This
474 ensures that the position of the rule will not be rounded differently when
475 the pixel size is not a perfect multiple of the units of measurement in
476 the \.{DVI} file. (2)~A typeset rule of height $a>0$ and width $b>0$
477 should be equivalent to a \MF-generated character having black pixels in
478 precisely those raster positions whose \MF\ coordinates satisfy
479 |0<=x<@t$\alpha$@>b| and |0<=y<@t$\alpha$@>a|, where $\alpha$ is the number
480 of pixels per \.{DVI} unit.
481 @:METAFONT}{\MF@>
482 @^alignment of rules with characters@>
483 @^rules aligning with characters@>
485 @ The last page in a \.{DVI} file is followed by `|post|'; this command
486 introduces the postamble, which summarizes important facts that \TeX\ has
487 accumulated about the file, making it possible to print subsets of the data
488 with reasonable efficiency. The postamble has the form
489 $$\vbox{\halign{\hbox{#\hfil}\cr
490 |post| |p[4]| |num[4]| |den[4]| |mag[4]| |l[4]| |u[4]| |s[2]| |t[2]|\cr
491 $\langle\,$font definitions$\,\rangle$\cr
492 |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
493 Here |p| is a pointer to the final |bop| in the file. The next three
494 parameters, |num|, |den|, and |mag|, are duplicates of the quantities that
495 appeared in the preamble.
497 Parameters |l| and |u| give respectively the height-plus-depth of the tallest
498 page and the width of the widest page, in the same units as other dimensions
499 of the file. These numbers might be used by a \.{DVI}-reading program to
500 position individual ``pages'' on large sheets of film or paper; however,
501 the standard convention for output on normal size paper is to position each
502 page so that the upper left-hand corner is exactly one inch from the left
503 and the top. Experience has shown that it is unwise to design \.{DVI}-to-printer
504 software that attempts cleverly to center the output; a fixed position of
505 the upper left corner is easiest for users to understand and to work with.
506 Therefore |l| and~|u| are often ignored.
508 Parameter |s| is the maximum stack depth (i.e., the largest excess of
509 |push| commands over |pop| commands) needed to process this file. Then
510 comes |t|, the total number of pages (|bop| commands) present.
512 The postamble continues with font definitions, which are any number of
513 \\{fnt\_def} commands as described above, possibly interspersed with |nop|
514 commands. Each font number that is used in the \.{DVI} file must be defined
515 exactly twice: Once before it is first selected by a \\{fnt} command, and once
516 in the postamble.
518 @ The last part of the postamble, following the |post_post| byte that
519 signifies the end of the font definitions, contains |q|, a pointer to the
520 |post| command that started the postamble. An identification byte, |i|,
521 comes next; this currently equals~2, as in the preamble.
523 The |i| byte is followed by four or more bytes that are all equal to
524 the decimal number 223 (i.e., '337 in octal). \TeX\ puts out four to seven of
525 these trailing bytes, until the total length of the file is a multiple of
526 four bytes, since this works out best on machines that pack four bytes per
527 word; but any number of 223's is allowed, as long as there are at least four
528 of them. In effect, 223 is a sort of signature that is added at the very end.
529 @^Fuchs, David Raymond@>
531 This curious way to finish off a \.{DVI} file makes it feasible for
532 \.{DVI}-reading programs to find the postamble first, on most computers,
533 even though \TeX\ wants to write the postamble last. Most operating
534 systems permit random access to individual words or bytes of a file, so
535 the \.{DVI} reader can start at the end and skip backwards over the 223's
536 until finding the identification byte. Then it can back up four bytes, read
537 |q|, and move to byte |q| of the file. This byte should, of course,
538 contain the value 248 (|post|); now the postamble can be read, so the
539 \.{DVI} reader can discover all the information needed for typesetting the
540 pages. Note that it is also possible to skip through the \.{DVI} file at
541 reasonably high speed to locate a particular page, if that proves
542 desirable. This saves a lot of time, since \.{DVI} files used in production
543 jobs tend to be large.
545 Unfortunately, however, standard \PASCAL\ does not include the ability to
546 @^system dependencies@>
547 access a random position in a file, or even to determine the length of a file.
548 Almost all systems nowadays provide the necessary capabilities, so \.{DVI}
549 format has been designed to work most efficiently with modern operating systems.
550 But if \.{DVI} files have to be processed under the restrictions of standard
551 \PASCAL, one can simply read them from front to back, since the necessary
552 header information is present in the preamble and in the font definitions.
553 (The |l| and |u| and |s| and |t| parameters, which appear only in the
554 postamble, are ``frills'' that are handy but not absolutely necessary.)
557 @* \[32] Shipping pages out.
558 After considering \TeX's eyes and stomach, we come now to the bowels.
559 @^bowels@>
561 The |ship_out| procedure is given a pointer to a box; its mission is
562 to describe that box in \.{DVI} form, outputting a ``page'' to |dvi_file|.
563 The \.{DVI} coordinates $(h,v)=(0,0)$ should correspond to the upper left
564 corner of the box being shipped.
566 Since boxes can be inside of boxes inside of boxes, the main work of
567 |ship_out| is done by two mutually recursive routines, |hlist_out|
568 and |vlist_out|, which traverse the hlists and vlists inside of horizontal
569 and vertical boxes.
571 As individual pages are being processed, we need to accumulate
572 information about the entire set of pages, since such statistics must be
573 reported in the postamble. The global variables |total_pages|, |max_v|,
574 |max_h|, |max_push|, and |last_bop| are used to record this information.
576 The variable |doing_leaders| is |true| while leaders are being output.
577 The variable |dead_cycles| contains the number of times an output routine
578 has been initiated since the last |ship_out|.
580 A few additional global variables are also defined here for use in
581 |vlist_out| and |hlist_out|. They could have been local variables, but
582 that would waste stack space when boxes are deeply nested, since the
583 values of these variables are not needed during recursive calls.
584 @^recursion@>
587 int total_pages = 0; /* the number of pages that have been shipped out */
588 scaled max_v = 0; /* maximum height-plus-depth of pages shipped so far */
589 scaled max_h = 0; /* maximum width of pages shipped so far */
590 int max_push = 0; /* deepest nesting of |push| commands encountered so far */
591 int last_bop = -1; /* location of previous |bop| in the \.{DVI} output */
592 int dead_cycles = 0; /* recent outputs that didn't ship anything out */
593 boolean doing_leaders = false; /* are we inside a leader box? */
594 int oval, ocmd; /* used by |out_cmd| for generating |set|, |fnt| and |fnt_def| commands */
595 pointer g; /* current glue specification */
596 int lq, lr; /* quantities used in calculations for leaders */
597 int cur_s = -1; /* current depth of output box nesting, initially $-1$ */
599 @ The \.{DVI} bytes are output to a buffer instead of being written directly
600 to the output file. This makes it possible to reduce the overhead of
601 subroutine calls, thereby measurably speeding up the computation, since
602 output of \.{DVI} bytes is part of \TeX's inner loop. And it has another
603 advantage as well, since we can change instructions in the buffer in order to
604 make the output more compact. For example, a `|down2|' command can be
605 changed to a `|y2|', thereby making a subsequent `|y0|' command possible,
606 saving two bytes.
608 The output buffer is divided into two parts of equal size; the bytes found
609 in |dvi_buf[0..half_buf-1]| constitute the first half, and those in
610 |dvi_buf[half_buf..dvi_buf_size-1]| constitute the second. The global
611 variable |dvi_ptr| points to the position that will receive the next
612 output byte. When |dvi_ptr| reaches |dvi_limit|, which is always equal
613 to one of the two values |half_buf| or |dvi_buf_size|, the half buffer that
614 is about to be invaded next is sent to the output and |dvi_limit| is
615 changed to its other value. Thus, there is always at least a half buffer's
616 worth of information present, except at the very beginning of the job.
618 Bytes of the \.{DVI} file are numbered sequentially starting with 0;
619 the next byte to be generated will be number |dvi_offset+dvi_ptr|.
620 A byte is present in the buffer only if its number is |>=dvi_gone|.
622 Some systems may find it more efficient to make |dvi_buf| a |packed|
623 array, since output of four bytes at once may be facilitated.
624 @^system dependencies@>
627 @ Initially the buffer is all in one piece; we will output half of it only
628 after it first fills up.
631 int dvi_buf_size = 800; /* size of the output buffer; must be a multiple of 8 */
632 eight_bits *dvi_buf; /* buffer for \.{DVI} output */
633 dvi_index half_buf = 0; /* half of |dvi_buf_size| */
634 dvi_index dvi_limit = 0; /* end of the current half buffer */
635 dvi_index dvi_ptr = 0; /* the next available buffer address */
636 int dvi_offset = 0; /* |dvi_buf_size| times the number of times the output buffer has been fully emptied */
637 int dvi_gone = 0; /* the number of bytes already output to |dvi_file| */
639 @ The actual output of |dvi_buf[a..b]| to |dvi_file| is performed by calling
640 |write_dvi(a,b)|. For best results, this procedure should be optimized to
641 run as fast as possible on each particular system, since it is part of
642 \TeX's inner loop. It is safe to assume that |a| and |b+1| will both be
643 multiples of 4 when |write_dvi(a,b)| is called; therefore it is possible on
644 many machines to use efficient methods to pack four bytes per word and to
645 output an array of words with one system call.
646 @^system dependencies@>
647 @^inner loop@>
648 @^defecation@>
651 static void write_dvi(dvi_index a, dvi_index b)
653 dvi_index k;
654 for (k = a; k <= b; k++)
655 fputc(dvi_buf[k], static_pdf->file);
658 /* outputs half of the buffer */
659 void dvi_swap(void)
661 if (dvi_limit == dvi_buf_size) {
662 write_dvi(0, half_buf - 1);
663 dvi_limit = half_buf;
664 dvi_offset = dvi_offset + dvi_buf_size;
665 dvi_ptr = 0;
666 } else {
667 write_dvi(half_buf, dvi_buf_size - 1);
668 dvi_limit = dvi_buf_size;
670 dvi_gone = dvi_gone + half_buf;
673 @ The |dvi_four| procedure outputs four bytes in two's complement notation,
674 without risking arithmetic overflow.
677 void dvi_four(int x)
679 if (x >= 0) {
680 dvi_out(x / 0100000000);
681 } else {
682 x = x + 010000000000;
683 x = x + 010000000000;
684 dvi_out((x / 0100000000) + 128);
686 x = x % 0100000000;
687 dvi_out(x / 0200000);
688 x = x % 0200000;
689 dvi_out(x / 0400);
690 dvi_out(x % 0400);
694 A mild optimization of the output is performed by the |dvi_pop|
695 routine, which issues a |pop| unless it is possible to cancel a
696 `|push| |pop|' pair. The parameter to |dvi_pop| is the byte address
697 following the old |push| that matches the new |pop|.
701 void dvi_push(void)
703 dvi_out(push);
706 void dvi_pop(int l)
708 if ((l == dvi_offset + dvi_ptr) && (dvi_ptr > 0))
709 decr(dvi_ptr);
710 else
711 dvi_out(pop);
714 @ Here's a procedure that outputs a font definition. $\Omega$ allows
715 more than 256 different fonts per job, so the right font definition
716 command must be selected.
719 void out_cmd(void)
721 if ((oval < 0x100) && (oval >= 0)) {
722 if ((ocmd != set1) || (oval > 127)) {
723 if ((ocmd == fnt1) && (oval < 64))
724 oval += fnt_num_0;
725 else
726 dvi_out(ocmd);
728 } else {
729 if ((oval < 0x10000) && (oval >= 0)) {
730 dvi_out(ocmd + 1);
731 } else {
732 if ((oval < 0x1000000) && (oval >= 0)) {
733 dvi_out(ocmd + 2);
734 } else {
735 dvi_out(ocmd + 3);
736 if (oval >= 0) {
737 dvi_out(oval / 0x1000000);
738 } else {
739 oval += 0x40000000;
740 oval += 0x40000000;
741 dvi_out((oval / 0x1000000) + 128);
742 oval = oval % 0x1000000;
744 dvi_out(oval / 0x10000);
745 oval = oval % 0x10000;
747 dvi_out(oval / 0x10000);
748 oval = oval % 0x10000;
750 dvi_out(oval / 0x100);
751 oval = oval % 0x100;
753 dvi_out(oval);
756 void dvi_font_def(internal_font_number f)
758 char *fa;
759 oval = f - 1;
760 ocmd = fnt_def1;
761 out_cmd();
762 dvi_out(font_check_0(f));
763 dvi_out(font_check_1(f));
764 dvi_out(font_check_2(f));
765 dvi_out(font_check_3(f));
766 dvi_four(font_size(f));
767 dvi_four(font_dsize(f));
768 dvi_out(0); /* |font_area(f)| is unused */
769 dvi_out(strlen(font_name(f)));
770 /* Output the font name whose internal number is |f| */
771 fa = font_name(f);
772 while (*fa != '\0') {
773 dvi_out(*fa++);
777 @ Versions of \TeX\ intended for small computers might well choose to omit
778 the ideas in the next few parts of this program, since it is not really
779 necessary to optimize the \.{DVI} code by making use of the |w0|, |x0|,
780 |y0|, and |z0| commands. Furthermore, the algorithm that we are about to
781 describe does not pretend to give an optimum reduction in the length
782 of the \.{DVI} code; after all, speed is more important than compactness.
783 But the method is surprisingly effective, and it takes comparatively little
784 time.
786 We can best understand the basic idea by first considering a simpler problem
787 that has the same essential characteristics. Given a sequence of digits,
788 say $3\,1\,4\,1\,5\,9\,2\,6\,5\,3\,5\,8\,9$, we want to assign subscripts
789 $d$, $y$, or $z$ to each digit so as to maximize the number of ``$y$-hits''
790 and ``$z$-hits''; a $y$-hit is an instance of two appearances of the same
791 digit with the subscript $y$, where no $y$'s intervene between the two
792 appearances, and a $z$-hit is defined similarly. For example, the sequence
793 above could be decorated with subscripts as follows:
794 $$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.$$
795 There are three $y$-hits ($1_y\ldots1_y$ and $5_y\ldots5_y\ldots5_y$) and
796 one $z$-hit ($3_z\ldots3_z$); there are no $d$-hits, since the two appearances
797 of $9_d$ have $d$'s between them, but we don't count $d$-hits so it doesn't
798 matter how many there are. These subscripts are analogous to the \.{DVI}
799 commands called \\{down}, $y$, and $z$, and the digits are analogous to
800 different amounts of vertical motion; a $y$-hit or $z$-hit corresponds to
801 the opportunity to use the one-byte commands |y0| or |z0| in a \.{DVI} file.
803 \TeX's method of assigning subscripts works like this: Append a new digit,
804 say $\delta$, to the right of the sequence. Now look back through the
805 sequence until one of the following things happens: (a)~You see
806 $\delta_y$ or $\delta_z$, and this was the first time you encountered a
807 $y$ or $z$ subscript, respectively. Then assign $y$ or $z$ to the new
808 $\delta$; you have scored a hit. (b)~You see $\delta_d$, and no $y$
809 subscripts have been encountered so far during this search. Then change
810 the previous $\delta_d$ to $\delta_y$ (this corresponds to changing a
811 command in the output buffer), and assign $y$ to the new $\delta$; it's
812 another hit. (c)~You see $\delta_d$, and a $y$ subscript has been seen
813 but not a $z$. Change the previous $\delta_d$ to $\delta_z$ and assign
814 $z$ to the new $\delta$. (d)~You encounter both $y$ and $z$ subscripts
815 before encountering a suitable $\delta$, or you scan all the way to the
816 front of the sequence. Assign $d$ to the new $\delta$; this assignment may
817 be changed later.
819 The subscripts $3_z\,1_y\,4_d\ldots\,$ in the example above were, in fact,
820 produced by this procedure, as the reader can verify. (Go ahead and try it.)
822 @ In order to implement such an idea, \TeX\ maintains a stack of pointers
823 to the \\{down}, $y$, and $z$ commands that have been generated for the
824 current page. And there is a similar stack for \\{right}, |w|, and |x|
825 commands. These stacks are called the down stack and right stack, and their
826 top elements are maintained in the variables |down_ptr| and |right_ptr|.
828 Each entry in these stacks contains four fields: The |width| field is
829 the amount of motion down or to the right; the |location| field is the
830 byte number of the \.{DVI} command in question (including the appropriate
831 |dvi_offset|); the |vlink| field points to the next item below this one
832 on the stack; and the |vinfo| field encodes the options for possible change
833 in the \.{DVI} command.
836 #define location(A) varmem[(A)+1].cint /* \.{DVI} byte number for a movement command */
838 halfword down_ptr = null, right_ptr = null; /* heads of the down and right stacks */
840 @ Here is a subroutine that produces a \.{DVI} command for some specified
841 downward or rightward motion. It has two parameters: |w| is the amount
842 of motion, and |o| is either |down1| or |right1|. We use the fact that
843 the command codes have convenient arithmetic properties: |y1-down1=w1-right1|
844 and |z1-down1=x1-right1|.
847 void movement(scaled w, eight_bits o)
849 small_number mstate; /* have we seen a |y| or |z|? */
850 halfword p, q; /* current and top nodes on the stack */
851 int k; /* index into |dvi_buf|, modulo |dvi_buf_size| */
852 if (false) { /* TODO: HUH? */
853 q = new_node(movement_node, 0); /* new node for the top of the stack */
854 width(q) = w;
855 location(q) = dvi_offset + dvi_ptr;
856 if (o == down1) {
857 vlink(q) = down_ptr;
858 down_ptr = q;
859 } else {
860 vlink(q) = right_ptr;
861 right_ptr = q;
863 /* Look at the other stack entries until deciding what sort of \.{DVI} command
864 to generate; |goto found| if node |p| is a ``hit'' */
865 p = vlink(q);
866 mstate = none_seen;
867 while (p != null) {
868 if (width(p) == w) {
869 /* Consider a node with matching width;|goto found| if it's a hit */
870 /* We might find a valid hit in a |y| or |z| byte that is already gone
871 from the buffer. But we can't change bytes that are gone forever; ``the
872 moving finger writes, $\ldots\,\,$.'' */
874 switch (mstate + vinfo(p)) {
875 case none_seen + yz_OK:
876 case none_seen + y_OK:
877 case z_seen + yz_OK:
878 case z_seen + y_OK:
879 if (location(p) < dvi_gone) {
880 goto NOT_FOUND;
881 } else {
882 /* Change buffered instruction to |y| or |w| and |goto found| */
883 k = location(p) - dvi_offset;
884 if (k < 0)
885 k = k + dvi_buf_size;
886 dvi_buf[k] = (eight_bits) (dvi_buf[k] + y1 - down1);
887 vinfo(p) = y_here;
888 goto FOUND;
890 break;
891 case none_seen + z_OK:
892 case y_seen + yz_OK:
893 case y_seen + z_OK:
894 if (location(p) < dvi_gone) {
895 goto NOT_FOUND;
896 } else {
897 /* Change buffered instruction to |z| or |x| and |goto found| */
898 k = location(p) - dvi_offset;
899 if (k < 0)
900 k = k + dvi_buf_size;
901 dvi_buf[k] = (eight_bits) (dvi_buf[k] + z1 - down1);
902 vinfo(p) = z_here;
903 goto FOUND;
905 break;
906 case none_seen + y_here:
907 case none_seen + z_here:
908 case y_seen + z_here:
909 case z_seen + y_here:
910 goto FOUND;
911 break;
912 default:
913 break;
915 } else {
916 switch (mstate + vinfo(p)) {
917 case none_seen + y_here:
918 mstate = y_seen;
919 break;
920 case none_seen + z_here:
921 mstate = z_seen;
922 break;
923 case y_seen + z_here:
924 case z_seen + y_here:
925 goto NOT_FOUND;
926 break;
927 default:
928 break;
931 p = vlink(p);
934 NOT_FOUND:
935 /* Generate a |down| or |right| command for |w| and |return| */
936 if (abs(w) >= 040000000) {
937 dvi_out(o + 3); /* |down4| or |right4| */
938 dvi_four(w);
939 return;
941 if (abs(w) >= 0100000) {
942 dvi_out(o + 2); /* |down3| or |right3| */
943 if (w < 0)
944 w = w + 0100000000;
945 dvi_out(w / 0200000);
946 w = w % 0200000;
947 goto TWO;
949 if (abs(w) >= 0200) {
950 dvi_out(o + 1); /* |down2| or |right2| */
951 if (w < 0)
952 w = w + 0200000;
953 goto TWO;
955 dvi_out(o); /* |down1| or |right1| */
956 if (w < 0)
957 w = w + 0400;
958 goto ONE;
959 TWO:
960 dvi_out(w / 0400);
961 ONE:
962 dvi_out(w % 0400);
963 return;
964 FOUND:
965 /* Generate a |y0| or |z0| command in order to reuse a previous appearance of~|w| */
966 /* The program below removes movement nodes that are introduced after a |push|,
967 before it outputs the corresponding |pop|. */
969 When the |movement| procedure gets to the label |found|, the value of
970 |vinfo(p)| will be either |y_here| or |z_here|. If it is, say, |y_here|,
971 the procedure generates a |y0| command (or a |w0| command), and marks
972 all |vinfo| fields between |q| and |p| so that |y| is not OK in that range.
974 vinfo(q) = vinfo(p);
975 if (vinfo(q) == y_here) {
976 dvi_out(o + y0 - down1); /* |y0| or |w0| */
977 while (vlink(q) != p) {
978 q = vlink(q);
979 switch (vinfo(q)) {
980 case yz_OK:
981 vinfo(q) = z_OK;
982 break;
983 case y_OK:
984 vinfo(q) = d_fixed;
985 break;
986 default:
987 break;
990 } else {
991 dvi_out(o + z0 - down1); /* |z0| or |x0| */
992 while (vlink(q) != p) {
993 q = vlink(q);
994 switch (vinfo(q)) {
995 case yz_OK:
996 vinfo(q) = y_OK;
997 break;
998 case z_OK:
999 vinfo(q) = d_fixed;
1000 break;
1001 default:
1002 break;
1008 @ In case you are wondering when all the movement nodes are removed from
1009 \TeX's memory, the answer is that they are recycled just before
1010 |hlist_out| and |vlist_out| finish outputting a box. This restores the
1011 down and right stacks to the state they were in before the box was output,
1012 except that some |vinfo|'s may have become more restrictive.
1016 /* delete movement nodes with |location>=l| */
1017 void prune_movements(int l)
1019 pointer p; /* node being deleted */
1020 while (down_ptr != null) {
1021 if (location(down_ptr) < l)
1022 break;
1023 p = down_ptr;
1024 down_ptr = vlink(p);
1025 flush_node(p);
1027 while (right_ptr != null) {
1028 if (location(right_ptr) < l)
1029 return;
1030 p = right_ptr;
1031 right_ptr = vlink(p);
1032 flush_node(p);
1036 scaledpos dvi; /* a \.{DVI} position in page coordinates, in sync with DVI file */
1038 @ When |hlist_out| is called, its duty is to output the box represented
1039 by the |hlist_node| pointed to by |temp_ptr|. The reference point of that
1040 box has coordinates |(cur.h,cur.v)|.
1042 Similarly, when |vlist_out| is called, its duty is to output the box represented
1043 by the |vlist_node| pointed to by |temp_ptr|. The reference point of that
1044 box has coordinates |(cur.h,cur.v)|.
1045 @^recursion@>
1047 @ The recursive procedures |hlist_out| and |vlist_out| each have a local variable
1048 |save_dvi| to hold the value of |dvi| just before
1049 entering a new level of recursion. In effect, the value of |save_dvi|
1050 on \TeX's run-time stack corresponds to the values of |h| and |v|
1051 that a \.{DVI}-reading program will push onto its coordinate stack.
1054 void dvi_place_rule(PDF pdf, halfword q, scaledpos size)
1056 synch_dvi_with_pos(pdf->posstruct->pos);
1057 if ((subtype(q) >= box_rule) && (subtype(q) <= user_rule)) {
1058 /* place nothing, only take space */
1059 if (textdir_is_L(pdf->posstruct->dir))
1060 dvi.h += size.h;
1061 } else {
1062 /* normal_rule or >= 100 being a leader rule */
1063 if (textdir_is_L(pdf->posstruct->dir)) {
1064 dvi_out(set_rule); /* movement optimization for |dir_*L*| */
1065 dvi.h += size.h;
1066 } else
1067 dvi_out(put_rule);
1069 dvi_four(size.v);
1070 dvi_four(size.h);
1073 void dvi_place_glyph(PDF pdf, internal_font_number f, int c, int ex)
1075 /* TODO: do something on ex, select font (if possible) */
1076 scaled_whd ci;
1077 synch_dvi_with_pos(pdf->posstruct->pos);
1078 if (f != pdf->f_cur) {
1079 /* Change font |f_cur| to |f| */
1080 if (!font_used(f)) {
1081 dvi_font_def(f);
1082 set_font_used(f, true);
1084 oval = f - 1;
1085 ocmd = fnt1;
1086 out_cmd();
1087 pdf->f_cur = f;
1089 if (textdir_is_L(pdf->posstruct->dir)) {
1090 ci = get_charinfo_whd(f, c);
1091 dvi_set(c, ci.wd); /* movement optimization for |dir_*L*| */
1092 } else
1093 dvi_put(c);
1096 void dvi_special(PDF pdf, halfword p)
1098 int old_setting; /* holds print |selector| */
1099 unsigned k; /* index into |cur_string| */
1100 synch_dvi_with_pos(pdf->posstruct->pos);
1101 old_setting = selector;
1102 selector = new_string;
1103 show_token_list(token_link(write_tokens(p)), null, -1);
1104 selector = old_setting;
1105 if (cur_length < 256) {
1106 dvi_out(xxx1);
1107 dvi_out(cur_length);
1108 } else {
1109 dvi_out(xxx4);
1110 dvi_four((int) cur_length);
1112 for (k = 0; k < cur_length; k++)
1113 dvi_out(cur_string[k]);
1114 cur_length = 0; /* erase the string */
1117 @ Here's an example of how these conventions are used. Whenever it is time to
1118 ship out a box of stuff, we shall use the macro |ensure_dvi_open|.
1121 void ensure_dvi_header_written(PDF pdf)
1123 unsigned l;
1124 unsigned s; /* index into |str_pool| */
1125 int old_setting; /* saved |selector| setting */
1126 assert(output_mode_used == OMODE_DVI);
1127 assert(pdf->o_state == ST_FILE_OPEN);
1129 if (half_buf == 0) {
1130 half_buf = dvi_buf_size / 2;
1131 dvi_limit = dvi_buf_size;
1134 dvi_out(pre);
1135 dvi_out(id_byte); /* output the preamble */
1136 dvi_four(25400000);
1137 dvi_four(473628672); /* conversion ratio for sp */
1138 prepare_mag();
1139 dvi_four(mag); /* magnification factor is frozen */
1140 if (output_comment) {
1141 l = (unsigned) strlen(output_comment);
1142 dvi_out(l);
1143 for (s = 0; s < l; s++)
1144 dvi_out(output_comment[s]);
1145 } else { /* the default code is unchanged */
1146 old_setting = selector;
1147 selector = new_string;
1148 tprint(" LuaTeX output ");
1149 print_int(int_par(year_code));
1150 print_char('.');
1151 print_two(int_par(month_code));
1152 print_char('.');
1153 print_two(int_par(day_code));
1154 print_char(':');
1155 print_two(int_par(time_code) / 60);
1156 print_two(int_par(time_code) % 60);
1157 selector = old_setting;
1158 dvi_out(cur_length);
1159 for (s = 0; s < cur_length; s++)
1160 dvi_out(cur_string[s]);
1161 cur_length = 0;
1165 void dvi_begin_page(PDF pdf)
1167 int k;
1168 int page_loc; /* location of the current |bop| */
1169 ensure_output_state(pdf, ST_HEADER_WRITTEN);
1170 /* Initialize variables as |ship_out| begins */
1171 page_loc = dvi_offset + dvi_ptr;
1172 dvi_out(bop);
1173 for (k = 0; k <= 9; k++)
1174 dvi_four(count(k));
1175 dvi_four(last_bop);
1176 last_bop = page_loc;
1179 void dvi_end_page(PDF pdf)
1181 (void) pdf;
1182 dvi_out(eop);
1184 #ifdef IPC
1185 if (ipcon > 0) {
1186 if (dvi_limit == half_buf) {
1187 write_dvi(half_buf, dvi_buf_size - 1);
1188 fflush(static_pdf->file);
1189 dvi_gone = dvi_gone + half_buf;
1191 if (dvi_ptr > 0) {
1192 write_dvi(0, dvi_ptr - 1);
1193 fflush(static_pdf->file);
1194 dvi_offset = dvi_offset + dvi_ptr;
1195 dvi_gone = dvi_gone + dvi_ptr;
1197 dvi_ptr = 0;
1198 dvi_limit = dvi_buf_size;
1199 ipcpage(dvi_gone);
1201 #endif /* IPC */
1204 @ At the end of the program, we must finish things off by writing the
1205 post\-amble. If |total_pages=0|, the \.{DVI} file was never opened.
1206 If |total_pages>=65536|, the \.{DVI} file will lie. And if
1207 |max_push>=65536|, the user deserves whatever chaos might ensue.
1210 void finish_dvi_file(PDF pdf, int version, int revision)
1212 int k;
1213 int callback_id = callback_defined(stop_run_callback);
1214 (void) version;
1215 (void) revision;
1216 while (cur_s > -1) {
1217 if (cur_s > 0) {
1218 dvi_out(pop);
1219 } else {
1220 dvi_out(eop);
1221 incr(total_pages);
1223 decr(cur_s);
1225 if (total_pages == 0) {
1226 if (callback_id == 0) {
1227 tprint_nl("No pages of output.");
1228 print_ln();
1229 } else if (callback_id > 0) {
1230 run_callback(callback_id, "->");
1232 } else {
1233 dvi_out(post); /* beginning of the postamble */
1234 dvi_four(last_bop);
1235 last_bop = dvi_offset + dvi_ptr - 5; /* |post| location */
1236 dvi_four(25400000);
1237 dvi_four(473628672); /* conversion ratio for sp */
1238 prepare_mag();
1239 dvi_four(mag); /* magnification factor */
1240 dvi_four(max_v);
1241 dvi_four(max_h);
1242 dvi_out(max_push / 256);
1243 dvi_out(max_push % 256);
1244 dvi_out((total_pages / 256) % 256);
1245 dvi_out(total_pages % 256);
1246 /* Output the font definitions for all fonts that were used */
1247 k = max_font_id();
1248 while (k > 0) {
1249 if (font_used(k)) {
1250 dvi_font_def(k);
1252 decr(k);
1255 dvi_out(post_post);
1256 dvi_four(last_bop);
1257 dvi_out(id_byte);
1258 #ifndef IPC
1259 k = 4 + ((dvi_buf_size - dvi_ptr) % 4); /* the number of 223's */
1260 #else
1261 k = 7 - ((3 + dvi_offset + dvi_ptr) % 4); /* the number of 223's */
1262 #endif
1264 while (k > 0) {
1265 dvi_out(223);
1266 decr(k);
1268 /* Empty the last bytes out of |dvi_buf| */
1269 /* Here is how we clean out the buffer when \TeX\ is all through; |dvi_ptr|
1270 will be a multiple of~4. */
1271 if (dvi_limit == half_buf)
1272 write_dvi(half_buf, dvi_buf_size - 1);
1273 if (dvi_ptr > 0)
1274 write_dvi(0, dvi_ptr - 1);
1276 if (callback_id == 0) {
1277 tprint_nl("Output written on ");
1278 tprint(pdf->file_name);
1279 tprint(" (");
1280 print_int(total_pages);
1281 tprint(" page");
1282 if (total_pages != 1)
1283 print_char('s');
1284 tprint(", ");
1285 print_int(dvi_offset + dvi_ptr);
1286 tprint(" bytes).");
1287 } else if (callback_id > 0) {
1288 run_callback(callback_id, "->");
1290 close_file(pdf->file);