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