Section 583: Device-independent file format
The most important output produced by a run of is the “device independent” (DVI
) file that specifies where characters and rules are to appear on printed pages.
The form of these files was designed by David R. Fuchs in 1979.
Almost any reasonable typesetting device can be driven by a program that takes DVI
files as input, and dozens of such DVI
-to-whatever programs have been written.
Thus, it is possible to print the output of on many different kinds of equipment, using as a device-independent front end“.
A 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 ‘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 and .
As in TFM
files, numbers that occupy more than one byte position appear in BigEndian order.
A DVI
file consists of a “preamble”, followed by a sequence of one or more “pages”, followed by a “postamble”. The preamble is simply a PRE command, with its parameters that define the dimensions used in the file; this must come first.
Each “page” consists of a BOP command, followed by any number of other commands that tell where characters are to be placed on a physical page, followed by an EOP command.
The pages appear in the order that generated them.
If we ignore NOP commands and FNT_DEF commands (which are allowed between any two commands in the file), each EOP command is immediately followed by a BOP command, or by a 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 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 BOP command points to the previous 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 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 BOP that starts in byte 1000 points to 100 and the BOP that starts in byte 2000 points to 1000.
(The very first BOP, i.e., the one starting in byte 100, has a pointer of −1.)
Section 584
The 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 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 FNT and 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 DVI
commands PUSH and 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; sets things up so that its DVI
output is in sp units, i.e., scaled points, in agreement with all the scaled dimensions in ’s data structures.
Section 585
Here is a list of all the commands that may appear in a DVI
file.
Each command is specified by its symbolic name (e.g., BOP), its opcode byte (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.
-
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. -
SET_CHAR_1 through SET_CHAR_127 (opcodes 1 to 127).
Do the operations of SET_CHAR_0; but use the character whose number matches the opcode, instead of character 0. -
SET1 128 c[1].
Same as SET_CHAR_0, except that character number c is typeset. uses this command for characters in the range 128 c < 256. -
SET2 129 c[2].
Same as SET1, except that c is two bytes long, so it is in the range 0 c 65536. never uses this command, but it should come in handy for extensions of that deal with oriental languages. -
SET3 130 c[3].
Same as SET1, except that c is three bytes long, so it can be as large as . Not even the Chinese language has this many characters, but this command might prove useful in some yet unforeseen extension. -
SET4 131 c[4].
Same as SET1, except that c is four bytes long. Imagine that. -
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 ← h + b. If either a 0 or b 0, nothing should be typeset. Note that if b 0, the value of h will decrease even though nothing else happens. See below for details about how to typeset rules so that consistency withMETAFONT
is guaranteed. -
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.) -
PUT2 134 c[2].
Same as SET2, except that h is not changed. -
PUT3 135 c[3].
Same as SET3, except that h is not changed. -
PUT4 136 c[4].
Same as SET4, except that h is not changed. -
PUT_RULE 137 a[4] b[4].
Same as SET_RULE, except that h is not changed. -
NOP 138.
No operation, do nothing. Any number of NOP’s may occur betweenDVI
commands, but a NOP cannot be inserted between a command and its parameters or between two parameters. -
BOP 139 [4] [4] [4] p[4].
Beginning of a page: Set (h, v, w, x, y, z) ← (0, 0, 0, 0, 0, 0) and set the stack empty. Set the current font f to an undefined value. The ten parameters hold the values of\count0
\count9
in at the time\shipout
was invoked for this page; they can be used to identify pages, if a user wants to print only part of aDVI
file. The parameter p points to the previous BOP in the file; the first BOP has p = −1. -
EOP 140.
End of page: Print what you have read since the previous BOP. At this point the stack should be empty. (TheDVI
-reading programs that drive most output devices will have kept a buffer of the material that appears on the page that has just ended. This material is largely, but not entirely, in order by v coordinate and (for fixed v) by h coordinate; so it usually needs to be sorted into some order that is appropriate for the device in question.) -
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. -
POP 142.
Pop the top six values off of the stack and assign them respectively 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 POP command. -
RIGHT1 143 b[1].
Set h ← h + b, i.e., move right b units. The parameter is a signed number in two’s complement notation, −128 b 128; if b 0, the reference point moves left. -
RIGHT2 144 b[2].
Same as RIGHT1, except that b is a two-byte quantity in the range −32768 b 32768. -
RIGHT3 145 b[3].
Same as RIGHT1, except that b is a three-byte quantity in the range . -
RIGHT4 146 b[4].
Same as RIGHT1, except that b is a four-byte quantity in the range . -
W0 147.
Set h ← 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. -
W1 148 b[1].
Set w ← b and h ← h + b. The value of b is a signed quantity in two’s complement notation, −128 b 128. This command changes the current w spacing and moves right by b. -
W2 149 b[2].
Same as W1, but b is two bytes long, −32768 b 32768. -
W3 150 b[3].
Same as W1, but b is three bytes long, . -
W4 151 b[4].
Same as W1, but b is four bytes long, . -
X0 152.
Set h ← h + x; i.e., move right x units. The ‘X’ commands are like the ‘W’ commands except that they involve x instead of w. -
X1 153 b[1].
Set x ← b and h ← h + b. The value of b is a signed quantity in two’s complement notation, −128 b 128. This command changes the current x spacing and moves right by b. -
X2 154 b[2].
Same as X1, but b is two bytes long, −32768 b 32768. -
X3 155 b[3].
Same as X1, but b is three bytes long, . -
X4 156 b[4].
Same as X1, but b is four bytes long, . -
DOWN1 157 a[1].
Set v ← v + a, i.e., move down a units. The parameter is a signed number in two’s complement notation, −128 a 128; if a 0, the reference point moves up. -
DOWN2 158 a[2].
Same as DOWN1, except that a is a two-byte quantity in the range −32768 a 32768. -
DOWN3 159 a[3].
Same as DOWN1, except that a is a three-byte quantity in the range . -
DOWN4 160 a[4].
Same as DOWN1, except that a is a four-byte quantity in the range . -
Y0 161.
Set v ← 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. -
Y1 162 a[1].
Set y ← a and v ← v + a. The value of a is a signed quantity in two’s complement notation, −128 a 128. This command changes the current y spacing and moves down by a. -
Y2 163 a[2].
Same as Y1, but a is two bytes long, −32768 a 32768. -
Y3 164 a[3].
Same as Y1, but a is three bytes long, . -
Y4 165 a[4].
Same as Y1, but a is four bytes long, . -
Z0 166.
Set v ← v + z; i.e., move down z units. The ‘Z’ commands are like the ‘Z’ commands except that they involve z instead of y. -
Z1 167 a[1].
Set z ← a and v ← v + a. The value of a is a signed quantity in two’s complement notation, −128 a 128. This command changes the current z spacing and moves down by a. -
Z2 168 a[2].
Same as Z1, but a is two bytes long, −32768 a 32768. -
Z3 169 a[3].
Same as Z1, but a is three bytes long, . -
Z4 170 a[4].
Same as Z1, but a is four bytes long, . -
FNT_NUM_0 171.
Set f ← 0. Font 0 must previously have been defined by a FNT_DEF instruction, as explained below. -
FNT_NUM_1 through FNT_NUM_63 (opcodes 172 to 234).
Set f ← 1, , f ← 63, respectively. -
FNT1 235 k[1].
Set f ← k. uses this command for font numbers in the range 64 k 256. -
FNT2 236 k[2].
Same as FNT1, except that k is two bytes long, so it is in the range 0 k 65536. 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. -
FNT3 237 k[3].
Same as FNT1, except that k is three bytes long, so it can be as large as . -
FNT4 238 k[4].
Same as FNT1, except that k is four bytes long; this is for the really big font numbers (and for the negative ones). -
XXX1 239 k[1] x[k].
This command is undefined in general; it functions as a (k + 2)-byte NOP unless specialDVI
-reading programs are being used. generates XXX1 when a short enough\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. -
XXX2 240 k[2] x[k].
Like XXX1, but 0 k 65536. -
XXX3 241 k[3] x[k].
Like XXX1, but 0 k . -
XXX4 242 k[4] x[k].
Like XXX1, but k can be ridiculously large. uses XXX4 when sending a string of length 256 or more. -
FNT_DEF1 243 k[1] c[4] s[4] d[4] a[1] l[1] n[a + l].
Define font k, where 0 k 256; font definitions will be explained shortly. -
FNT_DEF2 244 k[2] c[4] s[4] d[4] a[1] l[1] n[a + l].
Define font k, where 0 k 65536. -
FNT_DEF3 245 k[3] c[4] s[4] d[4] a[1] l[1] n[a + l].
Define font k, where . -
FNT_DEF4 246 k[4] c[4] s[4] d[4] a[1] l[1] n[a + l].
Define font k, where . -
PRE 247 i[1] num[4] den[4] mag[4] k[1] x[k].
Beginning of the preamble; this must come at the very beginning of the file. Parameters i, num, den, mag, k, and x are explained below. -
POST 248.
Beginning of the postamble, see below. -
POST_POST 249.
Ending of the postamble, see below.
Commands 250–255 are undefined at the present time.
Section 586
#define SET_CHAR_0 0 // typeset character 0 and move right
#define SET1 128 // typeset a character and move right
#define SET_RULE 132 // typeset a rule and move right
#define PUT_RULE 137 // typeset a rule
#define NOP 138 // no operation
#define BOP 139 // beginning of page
#define EOP 140 // ending of page
#define PUSH 141 // save the current positions
#define POP 142 // restore previous positions
#define RIGHT1 143 // move right
#define W0 147 // move right by |w|
#define W1 148 // move right and set |w|
#define X0 152 // move right by |x|
#define X1 153 // move right and set |x|
#define DOWN1 157 // move down
#define Y0 161 // move down by |y|
#define Y1 162 // move down and set |y|
#define Z0 166 // move down by |z|
#define Z1 167 // move down and set |z|
#define FNT_NUM_0 171 // set current font to 0
#define FNT1 235 // set current font
#define XXX1 239 // extension to `DVI` primitives
#define XXX4 242 // potentially long extension to `DVI` primitives
#define FNT_DEF1 243 // define the meaning of a font number
#define PRE 247 // preamble
#define POST 248 // postamble beginning
#define POST_POST 249 // postamble ending
Section 587
The preamble contains basic information about the file as a whole. As stated above, there are six parameters:
i[1] num[4] den[4] mag[4] k[1] x[k].
The i byte identifies 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 DVI
format makes another incompatible change—perhaps in the year 2048.)
The next two parameters, num and 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 DVI
file could be multiplied in order to get lengths in units of meters.
Since 7227 pt = 254 cm, and since works with scaled points where there are sp in a point, sets numden = .
The mag parameter is what calls \mag
, i.e., 1000 times the desired magnification.
The actual fraction by which dimensions are multiplied is therefore magnum1000den.
Note that if a source document does not call for any ‘true
’ dimensions, and if you change it only by specifying a different \mag
setting, the DVI
file that creates will be completely unchanged except for the value of mag in the preamble and postamble.
(Fancy DVI
-reading programs allow users to override the mag setting when a DVI
file is being printed.)
Finally, k and x allow the DVI
writer to include a comment, which is not interpreted further.
The length of comment x is k, where 0 k 256.
#define ID_BYTE 2 // identifies the kind of `DVI` files described here
Section 588
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 found in the TFM
file for this font;
c should match the check sum of the font found by programs that read this DVI
file.
Parameter s contains a fixed-point scale factor that is applied to the character widths in font k;
font dimensions in TFM
files and other font files are relative to this quantity, which is called the “at size” elsewhere in this documentation.
The value of s is always positive and less than .
It is given in the same units as the other DVI
dimensions, i.e., in sp when has made the
file.
Parameter d is similar to s; it is the “design size,” and (like s) it is given in DVI
units.
Thus, font k is to be used at mag s1000d 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 NOP commands, font definitions can appear before the first BOP, or between an EOP and a BOP.
Section 589
Sometimes it is desirable to make horizontal or vertical rules line up precisely with certain features in characters of a font.
It is possible to guarantee the correct matching between DVI
output and the characters generated by METAFONT
by adhering to the following principles:
(1) The METAFONT
characters should be positioned so that a bottom edge or left edge that is supposed to line up with the bottom or left edge of a rule appears at the reference point, i.e., in row 0 and column 0 of the METAFONT
raster.
This ensures that the position of the rule will not be rounded differently when the pixel size is not a perfect multiple of the units of measurement in the DVI
file.
(2) A typeset rule of height and width b > 0 should be equivalent to a METAFONT
-generated character having black pixels in precisely those raster positions whose METAFONT
coordinates satisfy and , where is the number of pixels per DVI
unit.
Section 590
The last page in a DVI
file is followed by ‘POST’;
this command introduces the postamble, which summarizes important facts that has accumulated about the file, making it possible to print subsets of the data with reasonable efficiency.
The postamble has the form
post p[4] num[4] den[4] mag[4] l[4] u[4] s[2] t[2]
⟨ font definitions ⟩
post_post q[4] i[1] 223’s[ 4]
Here p is a pointer to the final BOP in the file. The next three parameters, num, den, and 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 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 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 PUSH commands over POP commands) needed to process this file. Then comes t, the total number of pages (BOP commands) present.
The postamble continues with font definitions, which are any number of FNT_DEF commands as described above, possibly interspersed with NOP commands.
Each font number that is used in the DVI
file must be defined exactly twice:
Once before it is first selected by a FNT command, and once in the postamble.
Section 591
The last part of the postamble, following the POST_POST byte that signifies the end of the font definitions, contains q, a pointer to the 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., ’337 in octal). 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 DVI
file makes it feasible for DVI
-reading programs to find the postamble first, on most computers, even though wants to write the postamble last.
Most operating systems permit random access to individual words or bytes of a file, so the 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 (post); now the postamble can be read, so the
DVI
reader can discover all the information needed for typesetting the pages.
Note that it is also possible to skip through the DVI
file at reasonably high speed to locate a particular page, if that proves desirable.
This saves a lot of time, since DVI
files used in production jobs tend to be large.
Unfortunately, however, standard Pascal does not include the ability to access a random position in a file, or even to determine the length of a file.
Almost all systems nowadays provide the necessary capabilities, so DVI
format has been designed to work most efficiently with modern operating systems.
But if DVI
files have to be processed under the restrictions of standard PASCAL, one can simply read them from front to back, since the necessary header information is present in the preamble and in the font definitions.
(The l and u and s and t parameters, which appear only in the postamble, are “frills” that are handy but not absolutely necessary.)