% $Id: dvi.tex,v 3.4.1.1 1991/08/08 16:03:50 schrod Released schrod $
%------------------------------------------------------------
% taken from DVItype 3.4

%
% definition of DVI format
% LaTeX markup
%


% $Log: dvi.tex,v $
% Revision 3.4.1.1  1991/08/08  16:03:50  schrod
% CHANGES BY DON HOSEK:
%  -- Inserted \subsection's.
%  -- Deleted WEB defines.
%  -- `e.g.' now in italics, to be consistent with the rest of the
%     standard.
%
% CHANGES BY JOACHIM SCHROD:
%  -- Changed \bigbreak between WEB sections to \medbreak.
%  -- Added + signs to length specifications in \cmd tags, to show that
%     the param is signed.
%  -- Make formulas look more `math-like' and less `Pascal-like.'
%  -- DVItype not mentioned any more in the text.
%
% Revision 3.4  1990/10/31  00:00:00  schrod
% updated from DVItype 3.2 to 3.4
%
% Revision 3.2.1.1  90/07/16  00:00:00  schrod
% inserted \endinput
% 
% Revision 3.2  90/07/04  00:00:00  schrod
% extracted from DVItype 3.2
% 




\section{Device-Independent File Format}
\label{dvi-format}

\subsection{Introduction}

The form of \str{DVI} files was designed by {\sc David R. Fuchs} in
1979. Almost any reasonable typesetting device can be driven by a
program that takes \str{DVI} files as input, and a lot of such
\str{DVI}-to-whatever programs have been written. Thus, it is
possible to print the output of document compilers like \TeX\ on many
different kinds of equipment.

A \str{DVI} file is a stream of 8-bit bytes, which may be regarded as
a series of commands in a machine-like language. The first byte of
each command is the operation code, and this code is followed by zero
or more bytes that provide parameters to the command. The parameters
themselves may consist of several consecutive bytes; for example, the
`\id{set\_rule}' command has two parameters, each of which is four
bytes long. Parameters are usually regarded as nonnegative integers;
but four-byte-long parameters, and shorter parameters that denote
distances, can be either positive or negative. Such parameters are
given in two's complement notation. For example, a two-byte-long
distance parameter has a value between $-2^{15}$ and $2^{15}-1$.

A \str{DVI} file consists of a ``preamble,'' followed by a sequence
of one or more ``pages,'' followed by a ``postamble.'' The preamble
is simply a \id{pre} command, with its parameters that define the
dimensions used in the file; this must come first.  Each ``page''
consists of a \id{bop} command, followed by any number of other
commands that tell where characters are to be placed on a physical
page, followed by an \id{eop} command. The pages appear in the order
that they were generated, not in any particular numerical order. If
we ignore \id{nop} commands and \id{fnt\_def} commands (which are
allowed between any two commands in the file), each \id{eop} command
is immediately followed by a \id{bop} command, or by a \id{post}
command; in the latter case, there are no more pages in the file, and
the remaining bytes form the postamble.  Further details about the
postamble will be explained later.

Some parameters in \str{DVI} commands are ``pointers.'' These are
four-byte quantities that give the location number of some other byte
in the file; the first byte is number~0, then comes number~1, and so
on. For example, one of the parameters of a \id{bop} command points
to the previous \id{bop}; this makes it feasible to read the pages in
backwards order, in case the results are being directed to a device
that stacks its output face up. Suppose the preamble of a \str{DVI}
file occupies bytes 0 to 99. Now if the first page occupies bytes 100
to 999, say, and if the second page occupies bytes 1000 to 1999, then
the \id{bop} that starts in byte 1000 points to 100 and the \id{bop}
that starts in byte 2000 points to 1000. (The very first \id{bop},
i.e., the one that starts in byte 100, has a pointer of $-1$.)

\medbreak

The \str{DVI} format is intended to be both compact and easily
interpreted by a machine. Compactness is achieved by making most of
the information implicit instead of explicit. When a
\str{DVI}-reading program reads the commands for a page, it keeps
track of several quantities: (a)~The current font $f$ is an integer;
this value is changed only by \id{fnt} and \id{fnt\_num} commands.
(b)~The current position on the page is given by two numbers called
the horizontal and vertical coordinates, $h$ and $v$. Both
coordinates are zero at the upper left corner of the page; moving to
the right corresponds to increasing the horizontal coordinate, and
moving down corresponds to increasing the vertical coordinate. Thus,
the coordinates are essentially Cartesian, except that vertical
directions are flipped; the Cartesian version of $(h,v)$ would be
$(h,-v)$.  (c)~The current spacing amounts are given by four numbers
$w$, $x$, $y$, and $z$, where $w$ and~$x$ are used for horizontal
spacing and where $y$ and~$z$ are used for vertical spacing.
(d)~There is a stack containing $(h,v,w,x,y,z)$ values; the \str{DVI}
commands \id{push} and \id{pop} are used to change the current level
of operation. Note that the current font~$f$ is not pushed and
popped; the stack contains only information about positioning.

The values of $h$, $v$, $w$, $x$, $y$, and $z$ are signed integers
having up to 32 bits, including the sign. Since they represent
physical distances, there is a small unit of measurement such that
increasing $h$ by~1 means moving a certain tiny distance to the
right. The actual unit of measurement is variable, as explained
below.



\subsection{Summary of {\tt DVI} commands}

Here is a list of all the commands that may appear in a \str{DVI}
file. Each command is specified by its symbolic name ({\it e.g.},
\id{bop}), its opcode byte ({\it e.g.}, 139), and its parameters (if any).
The parameters are followed by a bracketed number telling how many
bytes they occupy; for example, `$p[4]$' means that parameter $p$ is
four bytes long.

\cmd \id{set\_char\_0} 0,.
 Typeset character number~0 from font~$f$ such that the reference
point of the character is at $(h,v)$. Then increase $h$ by the width
of that character. Note that a character may have zero or negative
width, so one cannot be sure that $h$ will advance after this
command; but $h$ usually does increase.

\cmd \id{set\_char\_1} through \id{set\_char\_127} (opcodes 1 to 127),.
 Do the operations of \id{set\_char\_0}; but use the character whose
number matches the opcode, instead of character~0.

\cmd \id{set1} 128, c[1].
 Same as \id{set\_char\_0}, except that character number~$c$ is
typeset. \TeX82 uses this command for characters in the range $128\le
c<256$.

\cmd \id{set2} 129, c[2].
 Same as \id{set1}, except that $c$~is two bytes long, so it is in
the range $0\le c<65536$. \TeX82 never uses this command, which is
intended for processors that deal with oriental languages; but a
\DVI{} processor should allow character codes greater than 255. The
processor may then assume that these characters have the same width
as the character whose respective codes are $c \bmod 256$.

\cmd \id{set3} 130, c[3].
 Same as \id{set1}, except that $c$~is three bytes long, so it can be
as large as $2^{24}-1$.

\cmd \id{set4} 131, c[+4].
 Same as \id{set1}, except that $c$~is four bytes long, possibly even
negative. Imagine that.

\cmd \id{set\_rule} 132, a[+4] b[+4].
 Typeset a solid black rectangle of height $a$ and width $b$, with
its bottom left corner at $(h,v)$. Then set $h\gets h+b$. If either
$a\le 0$ or $b\le 0$, nothing should be typeset. Note that if $b<0$,
the value of $h$ will decrease even though nothing else happens.
Programs that typeset from \str{DVI} files should be careful to make
the rules line up carefully with digitized characters, as explained
in connection with the \id{rule\_pixels} subroutine below.

\cmd \id{put1} 133, c[1].
 Typeset character number~$c$ from font~$f$ such that the reference
point of the character is at $(h,v)$. (The `put' commands are exactly
like the `set' commands, except that they simply put out a character
or a rule without moving the reference point afterwards.)

\cmd \id{put2} 134, c[2].
 Same as \id{set2}, except that $h$ is not changed.

\cmd \id{put3} 135, c[3].
 Same as \id{set3}, except that $h$ is not changed.

\cmd \id{put4} 136, c[+4].
 Same as \id{set4}, except that $h$ is not changed.

\cmd \id{put\_rule} 137, a[+4] b[+4].
 Same as \id{set\_rule}, except that $h$ is not changed.

\cmd \id{nop} 138,.
 No operation, do nothing. Any number of \id{nop}'s may occur between
\str{DVI} commands, but a \id{nop} cannot be inserted between a
command and its parameters or between two parameters.

\cmd \id{bop} 139, c_0[+4] c_1[+4] \ldots c_9[+4] p[+4].
 Beginning of a page: Set $(h,v,w,x,y,z)\gets (0,0,0,0,0,0)$ and set
the stack empty. Set the current font $f$ to an undefined value. The
ten $c_i$ parameters can be used to identify pages, if a user wants
to print only part of a \str{DVI} file; \TeX82 gives them the values
of \str{\\count0} $\ldots$ \str{\\count9} at the time \str{\\shipout}
was invoked for this page. The parameter $p$ points to the previous
\id{bop} command in the file, where the first \id{bop} has $p=-1$.

\cmd \id{eop} 140,.
 End of page: Print what you have read since the previous \id{bop}.
At this point the stack should be empty.

\cmd \id{push} 141,.
 Push the current values of $(h,v,w,x,y,z)$ onto the top of the
stack; do not change any of these values. Note that $f$ is not
pushed.

\cmd \id{pop} 142,.
 Pop the top six values off of the stack and assign them to
$(h,v,w,x,y,z)$. The number of pops should never exceed the number of
pushes, since it would be highly embarrassing if the stack were empty
at the time of a \id{pop} command.

\cmd \id{right1} 143, b[+1].
 Set $h\gets h+b$, i.e., move right $b$ units. The parameter is a
signed number in two's complement notation, $-128\le b<128$; if
$b<0$, the reference point actually moves left.

\cmd \id{right2} 144, b[+2].
 Same as \id{right1}, except that $b$ is a two-byte quantity in the
range $-32768\le b<32768$.

\cmd \id{right3} 145, b[+3].
 Same as \id{right1}, except that $b$ is a three-byte quantity in the
range $-2^{23}\le b<2^{23}$.

\cmd \id{right4} 146, b[+4].
 Same as \id{right1}, except that $b$ is a four-byte quantity in the
range $-2^{31}\le b<2^{31}$.

\cmd \id{w0} 147,.
 Set $h\gets h+w$; i.e., move right $w$ units. With luck, this
parameterless command will usually suffice, because the same kind of
motion will occur several times in succession; the following commands
explain how $w$ gets particular values.

\cmd \id{w1} 148, b[+1].
 Set $w\gets b$ and $h\gets h+b$. The value of $b$ is a signed
quantity in two's complement notation, $-128\le b<128$. This command
changes the current $w$~spacing and moves right by $b$.

\cmd \id{w2} 149, b[+2].
 Same as \id{w1}, but $b$ is a two-byte-long parameter, $-32768\le
b<32768$.

\cmd \id{w3} 150, b[+3].
 Same as \id{w1}, but $b$ is a three-byte-long parameter, $-2^{23}\le
b<2^{23}$.

\cmd \id{w4} 151, b[+4].
 Same as \id{w1}, but $b$ is a four-byte-long parameter, $-2^{31}\le
b<2^{31}$.

\cmd \id{x0} 152,.
 Set $h\gets h+x$; i.e., move right $x$ units. The `$x$' commands are
like the `$w$' commands except that they involve $x$ instead of $w$.

\cmd \id{x1} 153, b[+1].
 Set $x\gets b$ and $h\gets h+b$. The value of $b$ is a signed
quantity in two's complement notation, $-128\le b<128$. This command
changes the current $x$~spacing and moves right by $b$.

\cmd \id{x2} 154, b[+2].
 Same as \id{x1}, but $b$ is a two-byte-long parameter, $-32768\le
b<32768$.

\cmd \id{x3} 155, b[+3].
 Same as \id{x1}, but $b$ is a three-byte-long parameter, $-2^{23}\le
b<2^{23}$.

\cmd \id{x4} 156, b[+4].
 Same as \id{x1}, but $b$ is a four-byte-long parameter, $-2^{31}\le
b<2^{31}$.

\cmd \id{down1} 157, a[+1].
 Set $v\gets v+a$, i.e., move down $a$ units. The parameter is a
signed number in two's complement notation, $-128\le a<128$; if
$a<0$, the reference point actually moves up.

\cmd \id{down2} 158, a[+2].
 Same as \id{down1}, except that $a$ is a two-byte quantity in the
range $-32768\le a<32768$.

\cmd \id{down3} 159, a[+3].
 Same as \id{down1}, except that $a$ is a three-byte quantity in the
range $-2^{23}\le a<2^{23}$.

\cmd \id{down4} 160, a[+4].
 Same as \id{down1}, except that $a$ is a four-byte quantity in the
range $-2^{31}\le a<2^{31}$.

\cmd \id{y0} 161,.
 Set $v\gets v+y$; i.e., move down $y$ units. With luck, this
parameterless command will usually suffice, because the same kind of
motion will occur several times in succession; the following commands
explain how $y$ gets particular values.

\cmd \id{y1} 162, a[+1].
 Set $y\gets a$ and $v\gets v+a$. The value of $a$ is a signed
quantity in two's complement notation, $-128\le a<128$. This command
changes the current $y$~spacing and moves down by $a$.

\cmd \id{y2} 163, a[+2].
 Same as \id{y1}, but $a$ is a two-byte-long parameter, $-32768\le
a<32768$.

\cmd \id{y3} 164, a[+3].
 Same as \id{y1}, but $a$ is a three-byte-long parameter, $-2^{23}\le
a<2^{23}$.

\cmd \id{y4} 165, a[+4].
 Same as \id{y1}, but $a$ is a four-byte-long parameter, $-2^{31}\le
a<2^{31}$.

\cmd \id{z0} 166,.
 Set $v\gets v+z$; i.e., move down $z$ units. The `$z$' commands are
like the `$y$' commands except that they involve $z$ instead of $y$.

\cmd \id{z1} 167, a[+1].
 Set $z\gets a$ and $v\gets v+a$. The value of $a$ is a signed
quantity in two's complement notation, $-128\le a<128$. This command
changes the current $z$~spacing and moves down by $a$.

\cmd \id{z2} 168, a[+2].
 Same as \id{z1}, but $a$ is a two-byte-long parameter, $-32768\le
a<32768$.

\cmd \id{z3} 169, a[+3].
 Same as \id{z1}, but $a$ is a three-byte-long parameter, $-2^{23}\le
a<2^{23}$.

\cmd \id{z4} 170, a[+4].
 Same as \id{z1}, but $a$ is a four-byte-long parameter, $-2^{31}\le
a<2^{31}$.

\cmd \id{fnt\_num\_0} 171,.
 Set $f\gets 0$. Font 0 must previously have been defined by a
\id{fnt\_def} instruction, as explained below.

\cmd \id{fnt\_num\_1} through \id{fnt\_num\_63} (opcodes 172 to 234),.
 Set $f\gets 1$, \dots, $f\gets 63$, respectively.

\cmd \id{fnt1} 235, k[1].
 Set $f\gets k$. \TeX82 uses this command for font numbers in the
range $64\le k<256$.

\cmd \id{fnt2} 236, k[2].
 Same as \id{fnt1}, except that $k$~is two bytes long, so it is in
the range $0\le k<65536$. \TeX82 never generates this command, but
large font numbers may prove useful for specifications of color or
texture, or they may be used for special fonts that have fixed
numbers in some external coding scheme.

\cmd \id{fnt3} 237, k[3].
 Same as \id{fnt1}, except that $k$~is three bytes long, so it can be
as large as $2^{24}-1$.

\cmd \id{fnt4} 238, k[+4].
 Same as \id{fnt1}, except that $k$~is four bytes long; this is for
the really big font numbers (and for the negative ones).

\cmd \id{xxx1} 239, k[1] x[k].
 This command is undefined in general; it functions as a $(k+2)$-byte
\id{nop} unless special \str{DVI}-reading programs are being used.
\TeX82 generates \id{xxx1} when a short enough \str{\\special}
appears, setting $k$ to the number of bytes being sent. It is
recommended that $x$ be a string having the form of a keyword
followed by possible parameters relevant to that keyword.

\cmd \id{xxx2} 240, k[2] x[k].
 Like \id{xxx1}, but $0\le k<65536$.

\cmd \id{xxx3} 241, k[3] x[k].
 Like \id{xxx1}, but $0\le k<2^{24}$.

\cmd \id{xxx4} 242, k[4] x[k].
 Like \id{xxx1}, but $k$ can be ridiculously large. \TeX82 uses
\id{xxx4} when \id{xxx1} would be incorrect.

\cmd \id{fnt\_def1} 243, k[1] c[4] s[4] d[4] a[1] l[1] n[a+l].
 Define font $k$, where $0\le k<256$; font definitions will be
explained shortly.

\cmd \id{fnt\_def2} 244, k[2] c[4] s[4] d[4] a[1] l[1] n[a+l].
 Define font $k$, where $0\le k<65536$.

\cmd \id{fnt\_def3} 245, k[3] c[4] s[4] d[4] a[1] l[1] n[a+l].
 Define font $k$, where $0\le k<2^{24}$.

\cmd \id{fnt\_def4} 246, k[+4] c[4] s[4] d[4] a[1] l[1] n[a+l].
 Define font $k$, where $-2^{31}\le k<2^{31}$.

\cmd \id{pre} 247, i[1] \id{num}[4] \id{den}[4] \id{mag}[4] k[1] x[k].
 Beginning of the preamble; this must come at the very beginning of
the file. Parameters $i$, \id{num}, \id{den}, \id{mag}, $k$, and $x$
are explained below.

\cmd \id{post} 248,.
 Beginning of the postamble, see below.

\cmd \id{post\_post} 249,.
 Ending of the postamble, see below.

\smallskip

\noindent Commands 250--255 are undefined at the present time.



\subsection{The preamble}

The preamble contains basic information about the file as a whole. As
stated above, there are six parameters:
 $$
   i[1]\ \id{num}[4]\ \id{den}[4]\ \id{mag}[4]\ k[1]\ x[k].
 $$
 The $i$ byte identifies \str{DVI} format; currently this byte is
always set to~2. (The value $i=3$ is currently used for an extended
format that allows a mixture of right-to-left and left-to-right
typesetting. Some day we will set $i=4$, when \str{DVI} format makes
another incompatible change---perhaps in the year 2048.)

The next two parameters, \id{num} and \id{den}, are positive integers
that define the units of measurement; they are the numerator and
denominator of a fraction by which all dimensions in the \str{DVI}
file could be multiplied in order to get lengths in units of
$10^{-7}$ meters. (For example, there are exactly 7227 \TeX\ points
in 254 centimeters, and \TeX82 works with scaled points where there
are $2^{16}$ sp in a point, so \TeX82 sets $\id{num}=25400000$ and
$\id{den}= 7227 \cdot 2^{16} =473628672$.)

The \id{mag} parameter is what \TeX82 calls \str{\\mag}, i.e., 1000
times the desired magnification. The actual fraction by which
dimensions are multiplied is therefore $mn/1000d$. Note that if a
\TeX\ source document does not call for any `\str{true}' dimensions,
and if you change it only by specifying a different \str{\\mag}
setting, the \str{DVI} file that \TeX\ creates will be completely
unchanged except for the value of \id{mag} in the preamble and
postamble. (Fancy \str{DVI}-reading programs allow users to override
the \id{mag}~setting when a \str{DVI} file is being printed.)

Finally, $k$ and $x$ allow the \str{DVI} writer to include a comment,
which is not interpreted further. The length of comment $x$ is $k$,
where $0\le k<256$.



\subsection{Font definitions}

Font definitions for a given font number $k$ contain further
parameters
 $$
   c[4]\ s[4]\ d[4]\ a[1]\ l[1]\ n[a+l].
 $$
 The four-byte value $c$ is the check sum that \TeX\ (or whatever
program generated the \str{DVI} file) found in the \str{TFM} file for
this font; $c$ should match the check sum of the font found by
programs that read this \str{DVI} file.

Parameter $s$ contains a fixed-point scale factor that is applied to
the character widths in font $k$; font dimensions in \str{TFM} files
and other font files are relative to this quantity, which is always
positive and less than $2^{27}$. It is given in the same units as the
other dimensions of the \str{DVI} file.  Parameter $d$ is similar to
$s$; it is the ``design size,'' and (like~$s$) it is given in
\str{DVI} units. Thus, font $k$ is to be used at
$\id{mag} \cdot s/1000d$ times its normal size.

The remaining part of a font definition gives the external name of
the font, which is an ASCII string of length $a+l$. The number $a$ is
the length of the ``area'' or directory, and $l$ is the length of the
font name itself; the standard local system font area is supposed to
be used when $a=0$. The $n$ field contains the area in its first $a$
bytes.

Font definitions must appear before the first use of a particular
font number. Once font $k$ is defined, it must not be defined again;
however, we shall see below that font definitions appear in the
postamble as well as in the pages, so in this sense each font number
is defined exactly twice, if at all. Like \id{nop} commands, 
font definitions can appear before the first
\id{bop}, or between an \id{eop} and a \id{bop}.



\subsection{The postamble}

The last page in a \str{DVI} file is followed by `\id{post}'; this
command introduces the postamble, which summarizes important facts
that \TeX\ has accumulated about the file, making it possible to
print subsets of the data with reasonable efficiency. The postamble
has the form
%
 \begin{center}
 \begin{tabular}{l}
   $\id{post}\ p[4]\ \id{num}[4]\ \id{den}[4]\ \id{mag}[4]\ 
                                                   l[4]\ u[4]\ s[2]\ t[2]$\\
   $\langle\,$font definitions$\,\rangle$\cr
   $\id{post\_post}\ q[4]\ i[1]\ \hbox{223's}[\ge 4]$\\
 \end{tabular}
 \end{center}
%
 Here $p$ is a pointer to the final \id{bop} in the file. The next
three parameters, \id{num}, \id{den}, and \id{mag}, are duplicates of
the quantities that appeared in the preamble.

Parameters $l$ and $u$ give respectively the height-plus-depth of the
tallest page and the width of the widest page, in the same units as
other dimensions of the file. These numbers might be used by a
\str{DVI}-reading program to position individual ``pages'' on large
sheets of film or paper; however, the standard convention for output
on normal size paper is to position each page so that the upper
left-hand corner is exactly one inch from the left and the top.
Experience has shown that it is unwise to design \str{DVI}-to-printer
software that attempts cleverly to center the output; a fixed
position of the upper left corner is easiest for users to understand
and to work with. Therefore $l$ and~$u$ are often ignored.

Parameter $s$ is the maximum stack depth (i.e., the largest excess of
\id{push} commands over \id{pop} commands) needed to process this
file. Then comes $t$, the total number of pages (\id{bop} commands)
present.

The postamble continues with font definitions, which are any number
of \id{fnt\_def} commands as described above, possibly interspersed
with \id{nop} commands. Each font number that is used in the
\str{DVI} file must be defined exactly twice: Once before it is first
selected by a \id{fnt} command, and once in the postamble.

\medbreak

The last part of the postamble, following the \id{post\_post} byte
that signifies the end of the font definitions, contains $q$, a
pointer to the \id{post} command that started the postamble.  An
identification byte, $i$, comes next; this currently equals~2, as in
the preamble.

The $i$ byte is followed by four or more bytes that are all equal to
the decimal number 223 (i.e., \O{337} in octal). \TeX\ puts out four
to seven of these trailing bytes, until the total length of the file
is a multiple of four bytes, since this works out best on machines
that pack four bytes per word; but any number of 223's is allowed, as
long as there are at least four of them. In effect, 223 is a sort of
signature that is added at the very end.

This curious way to finish off a \str{DVI} file makes it feasible for
\str{DVI}-reading programs to find the postamble first, on most
computers, even though \TeX\ wants to write the postamble last. Most
operating systems permit random access to individual words or bytes
of a file, so the \str{DVI} reader can start at the end and skip
backwards over the 223's until finding the identification byte. Then
it can back up four bytes, read $q$, and move to byte $q$ of the
file. This byte should, of course, contain the value 248 (\id{post});
now the postamble can be read, so the \str{DVI} reader discovers all
the information needed for typesetting the pages. Note that it is
also possible to skip through the \str{DVI} file at reasonably high
speed to locate a particular page, if that proves desirable. This
saves a lot of time, since \str{DVI} files used in production jobs
tend to be large.




\endinput
