% This program by D. R. Fuchs is not copyrighted and can be used freely.
% Version 0 was implemented in March 1984, before METAFONT itself was complete.
% Version 1.0 incorporated refinements suitable for the TRAP test (August 1984).
% Version 1.1 fixed a trivial bug in the stated initial y value (August 1984).
% Version 1.2 clarified the pixel coordinates (September 1984).
% Version 2.0 changed over to new GF format (December 1984).
% Version 2.1 changed over to newer GF format (February 1985).
% Version 2.2 changed `family' to `extension' (October 1985).

% Here is TeX material that gets inserted after \input webmac
\def\hang{\hangindent 3em\noindent\ignorespaces}
\def\textindent#1{\hangindent2.5em\noindent\hbox to2.5em{\hss#1 }\ignorespaces}
\font\ninerm=cmr9
\let\mc=\ninerm % medium caps for names like SAIL
\font\tenss=cmss10 % for `The METAFONTbook'
\def\PASCAL{Pascal}
\def\ph{\hbox{Pascal-H}}
\font\logo=logo10 % font used for the METAFONT logo
\def\MF{{\logo META}\-{\logo FONT}}
\def\<#1>{$\langle#1\rangle$}
\def\section{\mathhexbox278}
\let\swap=\leftrightarrow
\def\round{\mathop{\rm round}\nolimits}

\def\(#1){} % this is used to make section names sort themselves better
\def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@>

\def\title{GFtype}
\def\contentspagenumber{101}
\def\topofcontents{\null
  \def\titlepage{F} % include headline on the contents page
  \def\rheader{\mainfont\hfil \contentspagenumber}
  \vfill
  \centerline{\titlefont The {\ttitlefont GFtype} processor}
  \vskip 15pt
  \centerline{(Version 2.2, October 1985)}
  \vfill}
\def\botofcontents{\vfill
  \centerline{\hsize 5in\baselineskip9pt
    \vbox{\ninerm\noindent
    The preparation of this report
    was supported in part by the National Science
    Foundation under grants IST-8201926 and MCS-8300984,
    and by the System Development Foundation. `\TeX' is a
    trademark of the American Mathematical Society.}}}
\pageno=\contentspagenumber \advance\pageno by 1

@* Introduction.
The \.{GFtype} utility program reads binary generic-font (``\.{GF}'')
files that are produced by font compilers such as \MF, and converts them
into symbolic form. This program has two chief purposes: (1)~It can be used to
determine whether a \.{GF} file is valid or invalid, when diagnosing
compiler errors; and (2)~it serves as an example of a program that reads
\.{GF} files correctly, for system programmers who are developing
\.{GF}-related software.

The original version of this program was written by David R. Fuchs in
March, 1984. Donald E. Knuth made a few modifications later that year as
\MF\ was taking shape.
@↑Fuchs, David Raymond@>
@↑Knuth, Donald Ervin@>

The |banner| string defined here should be changed whenever \.{GFtype}
gets modified.

@d banner=='This is GFtype, Version 2.2' {printed when the program starts}

@ This program is written in standard \PASCAL, except where it is
necessary to use extensions; for example, one extension is to use a
default |case| as in \.{TANGLE}, \.{WEAVE}, etc.  All places where
nonstandard constructions are used have been listed in the index under
``system dependencies.''
@!@↑system dependencies@>

@d othercases == others: {default for cases not listed explicitly}
@d endcases == @+end {follows the default case in an extended |case| statement}
@f othercases == else
@f endcases == end

@ The binary input comes from |gf←file|, and the symbolic output is written
on \PASCAL's standard |output| file. The term |print| is used instead of
|write| when this program writes on |output|, so that all such output
could easily be redirected if desired.

@d print(#)==write(#)
@d print←ln(#)==write←ln(#)
@d print←nl==write←ln

@p program GF←type(@!gf←file,@!output);
label @<Labels in the outer block@>@/
const @<Constants in the outer block@>@/
type @<Types in the outer block@>@/
var @<Globals in the outer block@>@/
procedure initialize; {this procedure gets things started properly}
  var i:integer; {loop index for initializations}
  begin print←ln(banner);@/
  @<Set initial values@>@/
  end;

@ If the program has to stop prematurely, it goes to the
`|final←end|'.

@d final←end=9999 {label for the end of it all}

@<Labels...@>=final←end;

@ The following parameters can be changed at compile time to extend or
reduce \.{GFtype}'s capacity.

The total number of bits in the main |image←array| will be
$$\hbox{$|max←row|+1\;\times\;|max←col|+1$}.$$
(\MF's full pixel range is rarely implemented, because it would require
8~megabytes of memory.)

@<Constants...@>=
@!terminal←line←length=150; {maximum number of characters input in a single
  line of input from the terminal}
@!line←length=79; {\\{xxx} strings will not produce lines longer than this}
@!max←row=79; {vertical extent of pixel image array}
@!max←col=79; {horizontal extent of pixel image array}

@ Here are some macros for common programming idioms.

@d incr(#) == #:=#+1 {increase a variable by unity}
@d decr(#) == #:=#-1 {decrease a variable by unity}
@d negate(#) == #:=-# {change the sign of a variable}

@ If the \.{GF} file is badly malformed, the whole process must be aborted;
\.{GFtype} will give up, after issuing an error message about the symptoms
that were noticed.

Such errors might be discovered inside of subroutines inside of subroutines,
so a procedure called |jump←out| has been introduced. This procedure, which
simply transfers control to the label |final←end| at the end of the program,
contains the only non-local |goto| statement in \.{GFtype}.
@↑system dependencies@>

@d abort(#)==begin print(' ',#); jump←out;
    end
@d bad←gf(#)==abort('Bad GF file: ',#,'!')
@.Bad GF file@>

@p procedure jump←out;
begin goto final←end;
end;

@* The character set.
Like all programs written with the  \.{WEB} system, \.{GFtype} can be
used with any character set. But it uses ASCII code internally, because
the programming for portable input-output is easier when a fixed internal
code is used.

The next few sections of \.{GFtype} have therefore been copied from the
analogous ones in the \.{WEB} system routines. They have been considerably
simplified, since \.{GFtype} need not deal with the controversial
ASCII codes less than @'40. If such codes appear in the \.{GF} file,
they will be printed as question marks.

@<Types...@>=
@!ASCII←code=" ".."~"; {a subrange of the integers}

@ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
character sets were common, so it did not make provision for lower case
letters. Nowadays, of course, we need to deal with both upper and lower case
alphabets in a convenient way, especially in a program like \.{GFtype}.
So we shall assume that the \PASCAL\ system being used for \.{GFtype}
has a character set containing at least the standard visible characters
of ASCII code (|"!"| through |"~"|).

Some \PASCAL\ compilers use the original name |char| for the data type
associated with the characters in text files, while other \PASCAL s
consider |char| to be a 64-element subrange of a larger data type that has
some other name.  In order to accommodate this difference, we shall use
the name |text←char| to stand for the data type of the characters in the
output file.  We shall also assume that |text←char| consists of
the elements |chr(first←text←char)| through |chr(last←text←char)|,
inclusive. The following definitions should be adjusted if necessary.
@↑system dependencies@>

@d text←char == char {the data type of characters in text files}
@d first←text←char=0 {ordinal number of the smallest element of |text←char|}
@d last←text←char=127 {ordinal number of the largest element of |text←char|}

@<Types...@>=
@!text←file=packed file of text←char;

@ The \.{GFtype} processor converts between ASCII code and
the user's external character set by means of arrays |xord| and |xchr|
that are analogous to \PASCAL's |ord| and |chr| functions.

@<Globals...@>=
@!xord: array [text←char] of ASCII←code;
  {specifies conversion of input characters}
@!xchr: array [0..255] of text←char;
  {specifies conversion of output characters}

@ Under our assumption that the visible characters of standard ASCII are
all present, the following assignment statements initialize the
|xchr| array properly, without needing any system-dependent changes.

@<Set init...@>=
for i:=0 to @'37 do xchr[i]:='?';
xchr[@'40]:=' ';
xchr[@'41]:='!';
xchr[@'42]:='"';
xchr[@'43]:='#';
xchr[@'44]:='$';
xchr[@'45]:='%';
xchr[@'46]:='&';
xchr[@'47]:='''';@/
xchr[@'50]:='(';
xchr[@'51]:=')';
xchr[@'52]:='*';
xchr[@'53]:='+';
xchr[@'54]:=',';
xchr[@'55]:='-';
xchr[@'56]:='.';
xchr[@'57]:='/';@/
xchr[@'60]:='0';
xchr[@'61]:='1';
xchr[@'62]:='2';
xchr[@'63]:='3';
xchr[@'64]:='4';
xchr[@'65]:='5';
xchr[@'66]:='6';
xchr[@'67]:='7';@/
xchr[@'70]:='8';
xchr[@'71]:='9';
xchr[@'72]:=':';
xchr[@'73]:=';';
xchr[@'74]:='<';
xchr[@'75]:='=';
xchr[@'76]:='>';
xchr[@'77]:='?';@/
xchr[@'100]:='@@';
xchr[@'101]:='A';
xchr[@'102]:='B';
xchr[@'103]:='C';
xchr[@'104]:='D';
xchr[@'105]:='E';
xchr[@'106]:='F';
xchr[@'107]:='G';@/
xchr[@'110]:='H';
xchr[@'111]:='I';
xchr[@'112]:='J';
xchr[@'113]:='K';
xchr[@'114]:='L';
xchr[@'115]:='M';
xchr[@'116]:='N';
xchr[@'117]:='O';@/
xchr[@'120]:='P';
xchr[@'121]:='Q';
xchr[@'122]:='R';
xchr[@'123]:='S';
xchr[@'124]:='T';
xchr[@'125]:='U';
xchr[@'126]:='V';
xchr[@'127]:='W';@/
xchr[@'130]:='X';
xchr[@'131]:='Y';
xchr[@'132]:='Z';
xchr[@'133]:='[';
xchr[@'134]:='\';
xchr[@'135]:=']';
xchr[@'136]:='↑';
xchr[@'137]:='←';@/
xchr[@'140]:='`';
xchr[@'141]:='a';
xchr[@'142]:='b';
xchr[@'143]:='c';
xchr[@'144]:='d';
xchr[@'145]:='e';
xchr[@'146]:='f';
xchr[@'147]:='g';@/
xchr[@'150]:='h';
xchr[@'151]:='i';
xchr[@'152]:='j';
xchr[@'153]:='k';
xchr[@'154]:='l';
xchr[@'155]:='m';
xchr[@'156]:='n';
xchr[@'157]:='o';@/
xchr[@'160]:='p';
xchr[@'161]:='q';
xchr[@'162]:='r';
xchr[@'163]:='s';
xchr[@'164]:='t';
xchr[@'165]:='u';
xchr[@'166]:='v';
xchr[@'167]:='w';@/
xchr[@'170]:='x';
xchr[@'171]:='y';
xchr[@'172]:='z';
xchr[@'173]:='{';
xchr[@'174]:='|';
xchr[@'175]:='}';
xchr[@'176]:='~';
for i:=@'177 to 255 do xchr[i]:='?';

@ The following system-independent code makes the |xord| array contain a
suitable inverse to the information in |xchr|.

@<Set init...@>=
for i:=first←text←char to last←text←char do xord[chr(i)]:=@'40;
for i:=" " to "~" do xord[xchr[i]]:=i;

@* Generic font file format.
The most important output produced by a typical run of \MF\ is the
``generic font'' (\.{GF}) file that specifies the bit patterns of the
characters that have been drawn. The term {\sl generic\/} indicates that
this file format doesn't match the conventions of any name-brand manufacturer;
but it is easy to convert \.{GF} files to the special format required by
almost all digital phototypesetting equipment. There's a strong analogy
between the \.{DVI} files written by \TeX\ and the \.{GF} files written
by \MF; and, in fact, the file formats have a lot in common.
It is therefore not surprising that \.{GFtype} is identical in many
respects to the \.{DVItype} program.

A \.{GF} file is a stream of 8-bit bytes that 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
`|boc|' (beginning of character) command has six parameters, each of
which is four bytes long. Parameters are usually regarded as nonnegative
integers; but four-byte-long parameters can be either positive or
negative, hence they range in value from $-2↑{31}$ to $2↑{31}-1$.
As in \.{TFM} files, numbers that occupy
more than one byte position appear in BigEndian order,
and negative numbers appear in two's complement notation.

A \.{GF} file consists of a ``preamble,'' followed by a sequence of one or
more ``characters,'' followed by a ``postamble.'' The preamble is simply a
|pre| command, with its parameters that introduce the file; this must come
first.  Each ``character'' consists of a |boc| command, followed by any
number of other commands that specify ``black'' pixels,
followed by an |eoc| command. The characters appear in the order that \MF\
generated them. If we ignore no-op commands (which are allowed between any
two commands in the file), each |eoc| command is immediately followed by a
|boc| command, or by a |post| command; in the latter case, there are no
more characters in the file, and the remaining bytes form the postamble.
Further details about the postamble will be explained later.

Some parameters in \.{GF} commands are ``pointers.'' These are four-byte
quantities that give the location number of some other byte in the file;
the first file byte is number~0, then comes number~1, and so on.

@ The \.{GF} format is intended to be both compact and easily interpreted
by a machine. Compactness is achieved by making most of the information
relative instead of absolute. When a \.{GF}-reading program reads the
commands for a character, it keeps track of two quantities: (a)~the current
column number,~|m|; and (b)~the current row number,~|n|.  These are 32-bit
signed integers, although most actual font formats produced from \.{GF}
files will need to curtail this vast range because of practical
limitations. (\MF\ output will never allow $\vert m\vert$ or $\vert
n\vert$ to get extremely large, but the \.{GF} format tries to be more general.)

How do \.{GF}'s row and column numbers correspond to the conventions
of \TeX\ and \MF? Well, the ``reference point'' of a character, in \TeX's
view, is considered to be at the lower left corner of the pixel in row~0
and column~0. This point is the intersection of the baseline with the left
edge of the type; it corresponds to location $(0,0)$ in \MF\ programs.
Thus the pixel in \.{GF} row~0 and column~0 is \MF's unit square, comprising the
region of the plane whose coordinates both lie between 0 and~1. The
pixel in \.{GF} row~|n| and column~|m| consists of the points whose \MF\
coordinates |(x,y)| satisfy |m<=x<=m+1| and |n<=y<=n+1|.  Negative values of
|m| and~|x| correspond to columns of pixels {\sl left\/} of the reference
point; negative values of |n| and~|y| correspond to rows of pixels {\sl
below\/} the baseline.

Besides |m| and |n|, there's also a third aspect of the current
state, namely the @!|paint←switch|, which is always either \\{black} or
\\{white}. Each \\{paint} command advances |m| by a specified amount~|d|,
and blackens the intervening pixels if |paint←switch=black|; then
the |paint←switch| changes to the opposite state. \.{GF}'s commands are
designed so that |m| will never decrease within a row, and |n| will never
increase within a character; hence there is no way to whiten a pixel that
has been blackened.

@ Here is a list of all the commands that may appear in a \.{GF} file. Each
command is specified by its symbolic name (e.g., |boc|), its opcode byte
(e.g., 67), and its parameters (if any). The parameters are followed
by a bracketed number telling how many bytes they occupy; for example,
`|d[2]|' means that parameter |d| is two bytes long.

\yskip\hang|paint←0| 0. This is a \\{paint} command with |d=0|; it does
nothing but change the |paint←switch| from \\{black} to \\{white} or vice~versa.

\yskip\hang\\{paint\←1} through \\{paint\←63} (opcodes 1 to 63).
These are \\{paint} commands with |d=1| to~63, defined as follows: If
|paint←switch=black|, blacken |d|~pixels of the current row~|n|,
in columns |m| through |m+d-1| inclusive. Then, in any case,
complement the |paint←switch| and advance |m| by~|d|.

\yskip\hang|paint1| 64 |d[1]|. This is a \\{paint} command with a specified
value of~|d|; \MF\ uses it to paint when |64<=d<256|.

\yskip\hang|@!paint2| 65 |d[2]|. Same as |paint1|, but |d|~can be as high
as~65535.

\yskip\hang|@!paint3| 66 |d[3]|. Same as |paint1|, but |d|~can be as high
as $2↑{24}-1$. \MF\ never needs this command, and it is hard to imagine
anybody making practical use of it; surely a more compact encoding will be
desirable when characters can be this large. But the command is there,
anyway, just in case.

\yskip\hang|boc| 67 |c[4]| |p[4]| |min←m[4]| |max←m[4]| |min←n[4]|
|max←n[4]|. Beginning of a character:  Here |c| is the character code, and
|p| points to the previous character beginning (if any) for characters having
this code number modulo 256.  (The pointer |p| is |-1| if there was no
prior character with an equivalent code.) The values of registers |m| and |n|
defined by the instructions that follow for this character must
satisfy |min←m<=m<=max←m| and |min←n<=n<=max←n|.  (The values of |max←m| and
|min←n| need not be the tightest bounds possible.)  When a \.{GF}-reading
program sees a |boc|, it can use |min←m|, |max←m|, |min←n|, and |max←n| to
initialize the bounds of an array. Then it sets |m:=min←m|, |n:=max←n|, and
|paint←switch:=white|.

\yskip\hang|boc1| 68 |c[1]| |@!del←m[1]| |max←m[1]| |@!del←n[1]| |max←n[1]|.
Same as |boc|, but |p| is assumed to be~$-1$; also |del←m=max←m-min←m|
and |del←n=max←n-min←n| are given instead of |min←m| and |min←n|.
The one-byte parameters must be between 0 and 255, inclusive.
\ (This abbreviated |boc| saves 19~bytes per character, in common cases.)

\yskip\hang|eoc| 69. End of character: All pixels blackened so far
constitute the pattern for this character. In particular, a completely
blank character might have |eoc| immediately following |boc|.

\yskip\hang|skip0| 70. Decrease |n| by 1 and set |m:=min←m|,
|paint←switch:=white|. \ (This finishes one row and begins another,
ready to whiten the leftmost pixel in the new row.)

\yskip\hang|skip1| 71 |d[1]|. Decrease |n| by |d+1|, set |m:=min←m|, and set
|paint←switch:=white|. This is a way to produce |d| all-white rows.

\yskip\hang|@!skip2| 72 |d[2]|. Same as |skip1|, but |d| can be as large
as 65535.

\yskip\hang|@!skip3| 73 |d[3]|. Same as |skip1|, but |d| can be as large
as $2↑{24}-1$. \MF\ obviously never needs this command.

\yskip\hang|new←row←0| 74. Decrease |n| by 1 and set |m:=min←m|,
|paint←switch:=black|. \ (This finishes one row and begins another,
ready to {\sl blacken\/} the leftmost pixel in the new row.)

\yskip\hang|@!new←row←1| through |@!new←row←164| (opcodes 75 to 238). Same as
|new←row←0|, but with |m:=min←m+1| through |min←m+164|, respectively.

\yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
general; it functions as a $(k+2)$-byte |no←op| unless special \.{GF}-reading
programs are being used. \MF\ generates \\{xxx} commands when encountering
a \&{special} string; this occurs in the \.{GF} file only between
characters, after the preamble, and before the postamble. However,
\\{xxx} commands might appear anywhere in \.{GF} files generated by other
processors. It is recommended that |x| be a string having the form of a
keyword followed by possible parameters relevant to that keyword.

\yskip\hang|@!xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.

\yskip\hang|xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2↑{24}$@>|.
\MF\ uses this when sending a \&{special} string whose length exceeds~255.

\yskip\hang|@!xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be
ridiculously large; |k| mustn't be negative.

\yskip\hang|yyy| 243 |y[4]|. This command is undefined in general;
it functions as a 5-byte |no←op| unless special \.{GF}-reading programs
are being used. \MF\ puts |scaled| numbers into |yyy|'s, as a
result of \&{numspecial} commands; the intent is to provide numeric
parameters to \\{xxx} commands that immediately precede.

\yskip\hang|no←op| 244. No operation, do nothing. Any number of |no←op|'s
may occur between \.{GF} commands, but a |no←op| cannot be inserted between
a command and its parameters or between two parameters.

\yskip\hang|char←loc| 245 |c[1]| |dx[4]| |dy[4]| |w[4]| |p[4]|.
This command will appear only in the postamble, which will be explained shortly.

\yskip\hang|@!char←loc0| 246 |c[1]| |@!dm[1]| |w[4]| |p[4]|.
Same as |char←loc|, except that |dy| is assumed to be zero, and the value
of~|dx| is taken to be |65536*dm|, where |0<=dm<256|.

\yskip\hang|pre| 247 |i[1]| |k[1]| |x[k]|.
Beginning of the preamble; this must come at the very beginning of the
file. Parameter |i| is an identifying number for \.{GF} format, currently
131. The other information is merely commentary; it is not given
special interpretation like \\{xxx} commands are. (Note that \\{xxx}
commands may immediately follow the preamble, before the first |boc|.)

\yskip\hang|post| 248. Beginning of the postamble, see below.

\yskip\hang|post←post| 249. Ending of the postamble, see below.

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

@d gf←id←byte=131 {identifies the kind of \.{GF} files described here}

@ Here are the opcodes that \.{GFtype} actually refers to.

@d paint←0=0 {beginning of the \\{paint} commands}
@d paint1=64 {move right a given number of columns, then
  black${}\swap{}$white}
@d boc=67 {beginning of a character}
@d boc1=68 {abbreviated |boc|}
@d eoc=69 {end of a character}
@d skip0=70 {skip no blank rows}
@d skip1=71 {skip over blank rows}
@d new←row←0=74 {move down one row and then right}
@d xxx1=239 {for \&{special} strings}
@d yyy=243 {for \&{numspecial} numbers}
@d no←op=244 {no operation}
@d char←loc=245 {character locators in the postamble}
@d pre=247 {preamble}
@d post=248 {postamble beginning}
@d post←post=249 {postamble ending}
@d undefined←commands==250,251,252,253,254,255

@ The last character in a \.{GF} file is followed by `|post|'; this command
introduces the postamble, which summarizes important facts that \MF\ has
accumulated. The postamble has the form
$$\vbox{\halign{\hbox{#\hfil}\cr
  |post| |p[4]| |@!ds[4]| |@!cs[4]| |@!hppp[4]| |@!vppp[4]|
   |min←m[4]| |max←m[4]| |min←n[4]| |max←n[4]|\cr
  $\langle\,$character locators$\,\rangle$\cr
  |post←post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
Here |p| is a pointer to the byte following the final |eoc| in the file
(or to the byte following the preamble, if there are no characters);
it can be used to locate the beginning of \\{xxx} commands
that might have preceded the postamble. The |ds| and |cs| parameters
@↑design size@> @↑check sum@>
give the design size and check sum, respectively, which are exactly the
values put into the header of any \.{TFM} file that shares information with this
\.{GF} file. Parameters |hppp| and |vppp| are the ratios of
pixels per point, horizontally and vertically, expressed as |scaled| integers
(i.e., multiplied by $2↑{16}$); they can be used to correlate the font
with specific device resolutions, magnifications, and ``at sizes.''  Then
come |min←m|, |max←m|, |min←n|, and |max←n|, which bound the values that
registers |m| and~|n| assume in all characters in this \.{GF} file.
(These bounds need not be the best possible; |max←m| and |min←n| may, on the
other hand, be tighter than the similar bounds in |boc| commands. For
example, some character may have |min←n=-100| in its |boc|, but it might
turn out that |n| never gets lower than |-50| in any character; then
|min←n| can have any value |<=-50|. If there are no characters in the file,
it's possible to have |min←m>max←m| and/or |min←n>max←n|.)

@ Character locators are introduced by |char←loc| commands,
which specify a character residue~|c|, character escapements (|dx,dy|),
a character width~|w|, and a pointer~|p|
to the beginning of that character. (If two or more characters have the
same code~|c| modulo 256, only the last will be indicated; the others can be
located by following backpointers. Characters whose codes differ by a
multiple of 256 are assumed to share the same font metric information,
hence the \.{TFM} file contains only residues of character codes modulo~256.
This convention is intended for oriental languages, when there are many
character shapes but few distinct widths.)
@↑oriental characters@>@↑Chinese characters@>@↑Japanese characters@>

The character escapements (|dx,dy|) are the values of \MF's \&{chardx}
and \&{chardy} parameters; they are in units of |scaled| pixels;
i.e., |dx| is in horizontal pixel units times $2↑{16}$, and |dy| is in
vertical pixel units times $2↑{16}$.  This is the intended amount of
displacement after typesetting the character; for \.{DVI} files, |dy|
should be zero, but other document file formats allow nonzero vertical
escapement.

The character width~|w| duplicates the information in the \.{TFM} file; it
is $2↑{24}$ times the ratio of the true width to the font's design size.

The backpointer |p| points to the character's |boc|, or to the first of
a sequence of consecutive \\{xxx} or |yyy| or |no←op| commands that
immediately precede the |boc|, if such commands exist; such ``special''
commands essentially belong to the characters, while the special commands
after the final character belong to the postamble (i.e., to the font
as a whole). This convention about |p| applies also to the backpointers
in |boc| commands, even though it wasn't explained in the description
of~|boc|. @↑backpointers@>

Pointer |p| might be |-1| if the character exists in the \.{TFM} file
but not in the \.{GF} file. This unusual situation can arise in \MF\ output
if the user had |proofing<0| when the character was being shipped out,
but then made |proofing>=0| in order to get a \.{GF} file.

@ The last part of the postamble, following the |post←post| byte that
signifies the end of the character locators, contains |q|, a pointer to the
|post| command that started the postamble.  An identification byte, |i|,
comes next; this currently equals~131, 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). \MF\ 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.
@↑Fuchs, David Raymond@>

This curious way to finish off a \.{GF} file makes it feasible for
\.{GF}-reading programs to find the postamble first, on most computers,
even though \MF\ wants to write the postamble last. Most operating
systems permit random access to individual words or bytes of a file, so
the \.{GF} 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
\.{GF} reader can discover all the information needed for individual characters.

Unfortunately, however, standard \PASCAL\ does not include the ability to
@↑system dependencies@>
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 \.{GF}
format has been designed to work most efficiently with modern operating systems.
But if \.{GF} files have to be processed under the restrictions of standard
\PASCAL, one can simply read them from front to back. This will
be adequate for most applications. However, the postamble-first approach
would facilitate a program that merges two \.{GF} files, replacing data
from one that is overridden by corresponding data in the other.

@* Input from binary files.
We have seen that a \.{GF} file is a sequence of 8-bit bytes. The bytes
appear physically in what is called a `|packed file of 0..255|'
in \PASCAL\ lingo.

Packing is system dependent, and many \PASCAL\ systems fail to implement
such files in a sensible way (at least, from the viewpoint of producing
good production software).  For example, some systems treat all
byte-oriented files as text, looking for end-of-line marks and such
things. Therefore some system-dependent code is often needed to deal with
binary files, even though most of the program in this section of
\.{GFtype} is written in standard \PASCAL.
@↑system dependencies@>

We shall stick to simple \PASCAL\ in this program, for reasons of clarity,
even if such simplicity is sometimes unrealistic.

@<Types...@>=
@!eight←bits=0..255; {unsigned one-byte quantity}
@!byte←file=packed file of eight←bits; {files that contain binary data}

@ The program deals with one binary file variable: |gf←file| is the main
input file that we are translating into symbolic form.

@<Glob...@>=
@!gf←file:byte←file; {the stuff we are \.{GF}typing}

@ To prepare this file for input, we |reset| it.

@p procedure open←gf←file; {prepares to read packed bytes in |gf←file|}
begin reset(gf←file);
cur←loc:=0;
end;

@ If you looked carefully at the preceding code, you probably asked,
``What is |cur←loc|?'' Good question. It's a global variable that holds
the number of the byte about to be read next from |gf←file|.

@<Glob...@>=
@!cur←loc:integer; {where we are about to look, in |gf←file|}

@ We shall use a set of simple functions to read the next byte or
bytes from |gf←file|. There are four possibilities, each of which is
treated as a separate function in order to minimize the overhead for
subroutine calls.
@↑system dependencies@>

@p function get←byte:integer; {returns the next byte, unsigned}
var b:eight←bits;
begin if eof(gf←file) then get←byte:=0
else  begin read(gf←file,b); incr(cur←loc); get←byte:=b;
  end;
end;
@#
function get←two←bytes:integer; {returns the next two bytes, unsigned}
var a,@!b:eight←bits;
begin read(gf←file,a); read(gf←file,b);
cur←loc:=cur←loc+2;
get←two←bytes:=a*256+b;
end;
@#
function get←three←bytes:integer; {returns the next three bytes, unsigned}
var a,@!b,@!c:eight←bits;
begin read(gf←file,a); read(gf←file,b); read(gf←file,c);
cur←loc:=cur←loc+3;
get←three←bytes:=(a*256+b)*256+c;
end;
@#
function signed←quad:integer; {returns the next four bytes, signed}
var a,@!b,@!c,@!d:eight←bits;
begin read(gf←file,a); read(gf←file,b); read(gf←file,c); read(gf←file,d);
cur←loc:=cur←loc+4;
if a<128 then signed←quad:=((a*256+b)*256+c)*256+d
else signed←quad:=(((a-256)*256+b)*256+c)*256+d;
end;

@* Optional modes of output.
\.{GFtype} will print different quantities of information based on some
options that the user must specify: We set |wants←mnemonics| if the
user wants to see a mnemonic dump of the \.{GF} file; and we set
|wants←pixels| if the user wants to see a pixel image of each
character.

When \.{GFtype} begins, it engages the user in a brief dialog so that the
options will be specified. This part of \.{GFtype} requires nonstandard
\PASCAL\ constructions to handle the online interaction; so it may be
preferable in some cases to omit the dialog and simply to stick to the
default options (|wants←mnemonics=wants←pixels=true|). On other hand, the
system-dependent routines that are needed are not complicated, so it will
not be terribly difficult to introduce them.
@↑system dependencies@>

@<Glob...@>=
@!wants←mnemonics: boolean; {controls mnemonic output}
@!wants←pixels: boolean; {controls pixel output}

@ @<Set init...@>=
wants←mnemonics:=true; wants←pixels:=true;

@ The |input←ln| routine waits for the user to type a line at his or her
terminal; then it puts ASCII-code equivalents for the characters on that line
into the |buffer| array. The |term←in| file is used for terminal input,
and |term←out| for terminal output.
@↑system dependencies@>

@<Glob...@>=
@!buffer:array[0..terminal←line←length] of ASCII←code;
@!term←in:text←file; {the terminal, considered as an input file}
@!term←out:text←file; {the terminal, considered as an output file}

@ Since the terminal is being used for both input and output, some systems
need a special routine to make sure that the user can see a prompt message
before waiting for input based on that message. (Otherwise the message
may just be sitting in a hidden buffer somewhere, and the user will have
no idea what the program is waiting for.) We shall invoke a system-dependent
subroutine |update←terminal| in order to avoid this problem.
@↑system dependencies@>

@d update←terminal == break(term←out) {empty the terminal output buffer}

@ During the dialog, extensions of \.{GFtype} might treat the first blank
space in a line as the end of that line. Therefore |input←ln| makes sure
that there is always at least one blank space in |buffer|.

(This routine is more complex than the present implementation needs, but
it has been copied from \.{DVItype} so that system-dependent changes that
worked before will work again.)
@↑system dependencies@>

@p procedure input←ln; {inputs a line from the terminal}
var k:0..terminal←line←length;
begin update←terminal; reset(term←in);
if eoln(term←in) then read←ln(term←in);
k:=0;
while (k<terminal←line←length)and not eoln(term←in) do
  begin buffer[k]:=xord[term←in↑]; incr(k); get(term←in);
  end;
buffer[k]:=" ";
end;

@ This is humdrum.

@p function lower←casify(@!c:ASCII←code):ASCII←code;
begin
if (c>="A") and (c<="Z") then lower←casify:=c+"a"-"A"
else lower←casify:=c;
end;

@ The selected options are put into global variables by the |dialog|
procedure, which is called just as \.{GFtype} begins.
@↑system dependencies@>

@p procedure dialog;
label 1,2;
begin rewrite(term←out); {prepare the terminal for output}
write←ln(term←out,banner);@/
@<Determine whether the user |wants←mnemonics|@>;
@<Determine whether the user |wants←pixels|@>;
@<Print all the selected options@>;
end;

@ @<Determine whether the user |wants←mnemonics|@>=
1: write(term←out,'Mnemonic output? (default=yes, ? for help): ');
@.Mnemonic output?@>
input←ln;
buffer[0]:=lower←casify(buffer[0]);
if buffer[0]<>"?" then
  wants←mnemonics:=(buffer[0]="y")or(buffer[0]="1")or(buffer[0]="t")
    or(buffer[0]=" ")
else  begin write(term←out,'Type Y for complete listing,');
  write←ln(term←out,' N for errors/images only.');
  goto 1;
  end

@ @<Determine whether the user |wants←pixels|@>=
2: write(term←out,'Pixel output? (default=yes, ? for help): ');
@.Pixel output?@>
input←ln;
buffer[0]:=lower←casify(buffer[0]);
if buffer[0]<>"?" then
  wants←pixels:=(buffer[0]="y")or(buffer[0]="1")or(buffer[0]="t")
    or(buffer[0]=" ")
else  begin write(term←out,'Type Y to list characters pictorially');
  write←ln(term←out,' with *''s, N to omit this option.');
  goto 2;
  end

@ After the dialog is over, we print the options so that the user
can see what \.{GFtype} thought was specified.

@<Print all the selected options@>=
print('Options selected: Mnemonic output = ');
@.Options selected@>
if wants←mnemonics then print('true')@+else print('false');
print('; pixel output = ');
if wants←pixels then print('true')@+else print('false');
print←ln('.')

@* The image array.
The definition of \.{GF} files refers to two registers,
|m| and~|n|, which hold integer column and row numbers. We actually
keep the values $m'=m-|min←m|$ and $n'=|max←n|-n$ instead, so that
our internal image array always has |m,n>=0|. We also
need to remember |paint←switch|, whose value is either |black|
or |white|.

@<Glob...@>=
@!m,@!n:integer; {current state values, modified by |min←m| and |max←n|}
@!paint←switch: pixel;

@ We'll need a big array of pixels to hold the character image.  Each
pixel should be represented as a single bit in order to save space.
Some systems may prefer the following definitions, while others
may do better using the |boolean| type and boolean constants.
@↑system dependencies@>

@d white=0 {could also be |false|}
@d black=1 {could also be |true|}

@<Types...@>=
@!pixel=white..black; {could also be |boolean|}

@ In order to allow different systems to change the |image| array easily from
row-major order to column-major order (or vice versa), or to transpose it top
and bottom or left and right, we declare and access it as follows.
@↑system dependencies@>

@d image==image←array[m,n]

@<Glob...@>=
@!image←array: packed array [0..max←col,0..max←row] of pixel;

@ A |boc| command has parameters |min←m|, |max←m|, |min←n|, and |max←n|
that define a rectangular subarray in which the pixels of the current
character must lie. The program here computes limits on \.{GFtype}'s
modified |m| and |n| variables, and clears the resulting subarray
to all |white|.

(There may be a faster way to clear a subarray on particular systems,
using nonstandard extensions of \PASCAL.)
@↑system dependencies@>

@<Clear the image@>=
begin max←subcol:=max←m←stated-min←m←stated-1;
if max←subcol>max←col then max←subcol:=max←col;
max←subrow:=max←n←stated-min←n←stated;
if max←subrow>max←row then max←subrow:=max←row;
n:=0;
while n<=max←subrow do
  begin m:=0;
  while m<=max←subcol do
    begin image:=white; incr(m);
    end;
  incr(n);
  end;
end

@ @<Glob...@>=
@!max←subrow,@!max←subcol:integer; {size of current subarray of interest}

@ As we paint the pixels of a character, we will record its actual
boundaries in variables |max←m←observed| and |max←n←observed|.
Then the following routine will be called on to output the image,
using blanks for |white| and asterisks for |black|. Blanks are
emitted only when they are followed by nonblanks, in order to conserve
space in the output. Further compaction could be achieved on many
systems by using tab marks.
@↑system dependencies@>

An integer variable |b| will be declared for use in counting blanks.

@<Print the image@>=
begin @<Compare the subarray boundaries with the observed boundaries@>;
if max←subcol>=0 then {there was at least one \\{paint} command}
  @<Print asterisk patterns for rows 0 to |max←subrow|@>
else print←ln('(The character is entirely blank.)');
end

@ @<Glob...@>=
@!min←m←stated, @!max←m←stated, @!min←n←stated, @!max←n←stated: integer;
  {bounds stated in the \.{GF} file}
@!max←m←observed,@!max←n←observed: integer;
  {bounds on $(m',n')$ actually observed when painting}
@!min←m←overall, @!max←m←overall, @!min←n←overall, @!max←n←overall: integer;
  {bounds observed in the entire file so far}

@ If the given character is substantially smaller than the |boc|
command predicted, we don't want to bother to output rows and columns
that are all blank.

@<Compare the subarray boundaries with the observed boundaries@>=
if (max←m←observed>max←col)or(max←n←observed>max←row) then
  print←ln('(The character is too large to be displayed in full.)');
@.The character is too large...@>
if max←subcol>max←m←observed then max←subcol:=max←m←observed;
if max←subrow>max←n←observed then max←subrow:=max←n←observed;

@ @<Print asterisk patterns...@>=
begin print←ln('.<--This pixel''s lower left corner is at (',
  min←m←stated:1,',',max←n←stated+1:1,') in METAFONT coordinates');
@.This pixel's lower...@>
n:=0;
while n<=max←subrow do
  begin m:=0; b:=0;
  while m<=max←subcol do
    begin if image=white then incr(b)
    else  begin while b>0 do
        begin print(' '); decr(b);
        end;
      print('*');
      end;
    incr(m);
    end;
  print←nl; incr(n);
  end;
print←ln('.<--This pixel''s upper left corner is at (',
  min←m←stated:1,',',max←n←stated-max←subrow:1,
  ') in METAFONT coordinates');
@.This pixel's upper@>
end

@* Translation to symbolic form.
The main work of \.{GFtype} is accomplished by the |do←char| procedure,
which produces the output for an entire character, assuming that the |boc|
command for that page has already been processed. This procedure is
essentially an interpretive routine that reads and acts on the \.{GF}
commands.

@ We steal the following routine from \MF.

@d unity == @'200000 {$2↑{16}$, represents 1.00000}

@p procedure print←scaled(@!s:integer);
  {prints a scaled number, rounded to five digits}
var @!delta:integer; {amount of allowable inaccuracy}
begin if s<0 then
  begin print('-'); negate(s); {print the sign, if negative}
  end;
print(s div unity:1); {print the integer part}
s:=10*(s mod unity)+5;
if s<>5 then
  begin delta:=10; print('.');
  repeat if delta>unity then
    s:=s+@'100000-(delta div 2); {round the final digit}
  print(chr(ord('0')+(s div unity))); s:=10*(s mod unity); delta:=delta*10;
  until s<=delta;
  end;
end;

@ Let's keep track of how many characters are in the font, and the
locations of where each one occurred in the file.

@<Glob...@>=
@!total←chars:integer; {the total number of characters seen so far}
@!char←ptr: array[0..255] of integer; {correct character location pointer}
@!gf←prev←ptr: integer; {|char←ptr| for next character}
@!character←code: integer; {current character number}

@ @<Set init...@>=
for i:=0 to 255 do char←ptr[i]:=-1; {mark characters as not being in the file}
total←chars:=0;

@ Before we get into the details of |do←char|, it is convenient to
consider a simpler routine that computes the first parameter of each
opcode.

@d four←cases(#)==#,#+1,#+2,#+3
@d eight←cases(#)==four←cases(#),four←cases(#+4)
@d sixteen←cases(#)==eight←cases(#),eight←cases(#+8)
@d thirty←two←cases(#)==sixteen←cases(#),sixteen←cases(#+16)
@d thirty←seven←cases(#)==thirty←two←cases(#),four←cases(#+32),#+36
@d sixty←four←cases(#)==thirty←two←cases(#),thirty←two←cases(#+32)

@p function first←par(o:eight←bits):integer;
begin case o of
sixty←four←cases(paint←0): first←par:=o-paint←0;
paint1,skip1,char←loc,char←loc+1,xxx1: first←par:=get←byte;
paint1+1,skip1+1,xxx1+1: first←par:=get←two←bytes;
paint1+2,skip1+2,xxx1+2: first←par:=get←three←bytes;
xxx1+3,yyy: first←par:=signed←quad;
boc,boc1,eoc,skip0,no←op,pre,post,post←post,undefined←commands: first←par:=0;
sixty←four←cases(new←row←0), sixty←four←cases(new←row←0+64),
  thirty←seven←cases(new←row←0+128): first←par:=o-new←row←0;
end;
end;

@ Strictly speaking, the |do←char| procedure is really a function with
side effects, not a `\&{procedure}'\thinspace; it returns the value |false|
if \.{GFtype} should be aborted because of some unusual happening. The
subroutine is organized as a typical interpreter, with a multiway branch
on the command code.

@p function do←char:boolean;
label 9998,9999;
var o:eight←bits; {operation code of the current command}
@!p,@!q:integer; {parameters of the current command}
@!aok:boolean; {the value to return}
begin {we've already scanned the |boc|}
aok:=true;
while true do @<Translate the next command in the \.{GF} file;
    |goto 9999| if it was |eoc|;
    |goto 9998| if premature termination is needed@>;
9998: print←ln('!'); aok:=false;
9999: do←char:=aok;
end;

@ @d show←label(#)==print(a:1,': ',#)
@d show←mnemonic(#)==if wants←mnemonics then begin print←nl; show←label(#); end
@d error(#)==begin show←label('! ',#); print←nl; end
@d nl←error(#)==begin print←nl; show←label('! ',#); print←nl; end
@d start←op==a:=cur←loc; o:=get←byte; p:=first←par(o);
  if eof(gf←file) then bad←gf('the file ended prematurely')
@.the file ended prematurely@>

@<Translate the next command...@>=
begin start←op;
@<Start translation of command |o| and |goto| the appropriate label to
  finish the job@>;
end

@ The multiway switch in |first←par|, above, was organized by the length
of each command; the one in |do←char| is organized by the semantics.

@<Start translation...@>=
if o<=paint1+3 then @<Translate a sequence of |paint| commands,
  until reaching a non-|paint|@>;
case o of
four←cases(skip0): @<Translate a |skip| command@>;
sixty←four←cases(new←row←0), sixty←four←cases(new←row←0+64),
 thirty←seven←cases(new←row←0+128):
  @<Translate a |new←row| command@>;
@t\4@>@<Cases for commands |no←op|, |pre|, |post|, |post←post|, |boc|,
  and |eoc|@>@;
four←cases(xxx1): @<Translate an |xxx| command@>;
yyy: @<Translate a |yyy| command@>;
othercases error('undefined command ',o:1,'!')
@.undefined command@>
endcases

@ @<Cases for commands |no←op|...@>=
no←op: show←mnemonic('no op');
pre: begin error('preamble command within a character!'); goto 9998;
  end;
@.preamble command within...@>
post,post←post: begin error('postamble command within a character!');
@.postamble command within...@>
  goto 9998;
  end;
boc,boc1: begin error('boc occurred before eoc!'); goto 9998;
@.boc occurred before eoc@>
  end;
eoc: begin show←mnemonic('eoc');
  print←nl; goto 9999;
  end;

@ @<Translate an |xxx| command@>=
begin show←mnemonic('xxx '''); bad←char:=false; b:=16;
if p<0 then nl←error('string of negative length!');
@.string of negative length@>
while p>0 do
  begin q:=get←byte;
  if (q<" ")or(q>"~") then bad←char:=true;
  if wants←mnemonics then
    begin print(xchr[q]);
    if b<line←length then incr(b)
    else  begin print←nl; b:=2;
      end;
    end;
  decr(p);
  end;
if wants←mnemonics then print('''');
if bad←char then nl←error('non-ASCII character in xxx command!');
@.non-ASCII character...@>
end

@ @<Glob...@>=
@!bad←char:boolean; {has a non-ASCII character code appeared in this \\{xxx}?}

@ @<Translate a |yyy| command@>=
begin show←mnemonic('yyy ',p:1,' (');
if wants←mnemonics then
  begin print←scaled(p); print(')');
  end;
end

@ The bulk of a \.{GF} file generally consists of |paint| commands,
so we collect them together and print them in an abbreviated format
on one line.

@<Translate a sequence of |paint| commands...@>=
begin if wants←mnemonics then print(' paint ');
repeat @<Paint the next |p| pixels@>;
start←op;
until o>paint1+3;
end

@ @<Paint the next...@>=
if wants←mnemonics then
  if paint←switch=white then print('(',p:1,')')@+else print(p:1);
m:=m+p;
if m>max←m←observed then max←m←observed:=m-1;
if wants←pixels then
  @<Paint pixels |m-p| through |m-1| in row |n| of the subarray@>;
paint←switch:=white+black-paint←switch
  {could also be |paint←switch:=not paint←switch|}

@ We use the fact that the subarray has been initialized to all |white|.

@<Paint pixels |m-p|...@>=
if paint←switch=black then if n<=max←subrow then
  begin l:=m-p; r:=m-1;
  if r>max←subcol then r:=max←subcol;
  m:=l;
  while m<=r do
    begin image:=black; incr(m);
    end;
  m:=l+p;
  end

@ @<Translate a |new←row| command@>=
begin show←mnemonic('newrow ',p:1);
incr(n); m:=p; paint←switch:=black;
if wants←mnemonics then print(' (n=',max←n←stated-n:1,')');
end

@ @<Translate a |skip| command@>=
begin show←mnemonic('skip',(o-skip1+1)mod 4:1,' ',p:1);
n:=n+p+1; m:=0; paint←switch:=white;
if wants←mnemonics then print(' (n=',max←n←stated-n:1,')');
end

@* Reading the postamble.
Now imagine that we are reading the \.{GF} file and positioned just
after the |post| command. That, in fact, is the situation,
when the following part of \.{GFtype} is called upon to read, translate,
and check the rest of the postamble.

@p procedure read←postamble;
var k:integer; {loop index}
@!p,@!q,@!m,@!u,@!v,@!w,@!c:integer; {general purpose registers}
begin post←loc:=cur←loc-1;
print('Postamble starts at byte ',post←loc:1);
@.Postamble starts at byte n@>
if post←loc=gf←prev←ptr then print←ln('.')
else print←ln(', after special info at byte ',gf←prev←ptr:1,'.');
p:=signed←quad;
if p<>gf←prev←ptr then
  error('backpointer in byte ',cur←loc-4:1,
    ' should be ',gf←prev←ptr:1,' not ',p:1,'!');
@.backpointer...should be p@>
design←size:=signed←quad; check←sum:=signed←quad;@/
print('design size = ',design←size:1,' (');
print←scaled(design←size div 16); print←ln('pt)');
print←ln('check sum = ',check←sum:1);@/
hppp:=signed←quad; vppp:=signed←quad;@/
print('hppp = ',hppp:1,' ('); print←scaled(hppp); print←ln(')');
print('vppp = ',vppp:1,' ('); print←scaled(vppp); print←ln(')');
pix←ratio:=(design←size/1048576)*(hppp/1048576);
min←m←stated:=signed←quad; max←m←stated:=signed←quad;
min←n←stated:=signed←quad; max←n←stated:=signed←quad;@/
print←ln('min m = ',min←m←stated:1,', max m = ',max←m←stated:1);@/
if min←m←stated>min←m←overall then
  error('min m should be <=',min←m←overall:1,'!');
if max←m←stated<max←m←overall then
  error('max m should be >=',max←m←overall:1,'!');
print←ln('min n = ',min←n←stated:1,', max n = ',max←n←stated:1);@/
if min←n←stated>min←n←overall then
  error('min n should be <=',min←n←overall:1,'!');
if max←n←stated<max←n←overall then
  error('max n should be >=',max←n←overall:1,'!');
@<Process the character locations in the postamble@>;
@<Make sure that the end of the file is well-formed@>;
end;

@ @<Glob...@>=
@!design←size,@!check←sum: integer; {\.{TFM}-oriented parameters}
@!hppp, @!vppp: integer; {magnification-oriented parameters}
@!post←loc: integer; {location of the |post| command}
@!pix←ratio: real; {multiply by this to convert \.{TFM} width to scaled pixels}

@ @<Set init...@>=
min←m←overall:=max←int; max←m←overall:=-max←int;
min←n←overall:=max←int; max←n←overall:=-max←int;

@ When we get to the present code, the |post←post| command has
just been read.

@<Make sure that the end of the file is well-formed@>=
if k<>post←post then
  error('should be postpost!');
@.should be postpost@>
q:=signed←quad;
if q<>post←loc then
  error('postamble pointer should be ',post←loc:1,' not ',q:1);
@.postamble pointer should be...@>
m:=get←byte;
if m<>gf←id←byte then error('identification byte should be ',gf←id←byte:1,
  ', not ',m:1);
@.identification byte should be n@>
k:=cur←loc; m:=223;
while (m=223)and not eof(gf←file) do m:=get←byte;
if not eof(gf←file) then bad←gf('signature in byte ',cur←loc-1:1,
@.signature...should be...@>
    ' should be 223')
else if cur←loc<k+4 then
  error('not enough signature bytes at end of file!');
@.not enough signature bytes...@>

@ @<Process the character locations...@>=
repeat a:=cur←loc; k:=get←byte;
if (k=char←loc)or(k=char←loc+1) then
  begin c:=first←par(k);
  if k=char←loc then
    begin u:=signed←quad; v:=signed←quad;
    end
  else  begin u:=get←byte*unity; v:=0;
    end;
  w:=signed←quad; p:=signed←quad;
  print('Character ',c:1,': dx ',u:1,' (');
  print←scaled(u);
  if v<>0 then
    begin print('), dy ',v:1,' ('); print←scaled(v);
    end;
  print('), width ',w:1,' (');
  w:=round(w*pix←ratio);
  print←scaled(w);
  print←ln('), loc ',p:1);
  if p<>char←ptr[c] then
    error('character location should be ',char←ptr[c]:1,'!');
@.character location should be...@>
  k:=no←op;
  end;
until k<>no←op;

@* The main program.
Now we are ready to put it all together. This is where \.{GFtype} starts,
and where it ends.

@p begin initialize; {get all variables initialized}
dialog; {set up all the options}
@<Process the preamble@>;
@<Translate all the characters@>;
print←nl;
read←postamble;
print('The file had ',total←chars:1,' character');
if total←chars<>1 then print('s');
print(' altogether.');
@.The file had n characters...@>
final←end:end.

@ The main program needs a few global variables in order to do its work.

@<Glob...@>=
@!a:integer; {byte number of the current command}
@!b,@!c,@!l,@!o,@!p,@!q,@!r:integer; {general purpose registers}

@ \.{GFtype} looks at the preamble in order to do error checking, and to
display the introductory comment.

@<Process the preamble@>=
open←gf←file;
o:=get←byte; {fetch the first byte}
if o<>pre then bad←gf('First byte isn''t start of preamble!');
@.First byte isn't...@>
o:=get←byte; {fetch the identification byte}
if o<>gf←id←byte then
  bad←gf('identification byte should be ',gf←id←byte:1,
  ' not ',o:1);
@.identification byte should be n@>
o:=get←byte; {fetch the length of the introductory comment}
print('''');
while o>0 do
  begin decr(o); print(xchr[get←byte]);
  end;
print←ln('''');

@ @<Translate all...@>=
repeat gf←prev←ptr:=cur←loc;
  @<Pass |no←op|, |xxx| and |yyy| commands@>;
  if o<>post then
    begin if o<>boc then if o<>boc1 then
      bad←gf('byte ',cur←loc-1:1,' is not boc (',o:1,')');
@.byte n is not boc@>
    print←nl; print(cur←loc-1:1,': beginning of char ');
    @<Pass a |boc| command@>;
    if not do←char then bad←gf('char ended unexpectedly');
@.char ended unexpectedly@>
    max←n←observed:=n;
    if wants←pixels then @<Print the image@>;
    @<Pass an |eoc| command@>;
    end;
until o=post;

@ @<Pass |no←op|, |xxx| and |yyy| commands@>=
repeat start←op;
  if o=yyy then
    begin @<Translate a |yyy|...@>; o:=no←op;
    end
  else if (o>=xxx1) and (o<=xxx1+3) then
    begin @<Translate an |xxx|...@>; o:=no←op;
    end
  else if o=no←op then show←mnemonic('no op');
until o<>no←op;

@ @<Pass a |boc|...@>=
a:=cur←loc-1; incr(total←chars);
if o=boc then
  begin character←code:=signed←quad;
  p:=signed←quad;
  c:=character←code mod 256;
  if c<0 then c:=c+256;
  min←m←stated:=signed←quad; max←m←stated:=signed←quad;
  min←n←stated:=signed←quad; max←n←stated:=signed←quad;
  end
else  begin character←code:=get←byte; p:=-1; c:=character←code;
  q:=get←byte; max←m←stated:=get←byte; min←m←stated:=max←m←stated-q;
  q:=get←byte; max←n←stated:=get←byte; min←n←stated:=max←n←stated-q;
  end;
print(c:1);
if character←code<>c then
  print(' with extension ',(character←code-c) div 256 : 1);
if wants←mnemonics then
  print←ln(': ',min←m←stated:1,'<=m<=',max←m←stated:1,' ',
   min←n←stated:1,'<=n<=',max←n←stated:1);
max←m←observed:=-1;
if char←ptr[c]<>p then
  error('previous character pointer should be ',char←ptr[c]:1,
    ', not ',p:1,'!')
@.previous character pointer...@>
else if p>0 then if wants←mnemonics then
  print←ln('(previous character with the same code started at byte ',
    p:1,')');
char←ptr[c]:=gf←prev←ptr;
if wants←mnemonics then print('(initially n=',max←n←stated:1,')');
if wants←pixels then @<Clear the image@>;
m:=0; n:=0; paint←switch:=white;

@ @<Pass an |eoc|...@>=
max←m←observed:=min←m←stated+max←m←observed+1;
n:=max←n←stated-max←n←observed; {now |n| is the minimum |n| observed}
if min←m←stated<min←m←overall then min←m←overall:=min←m←stated;
if max←m←observed>max←m←overall then max←m←overall:=max←m←observed;
if n<min←n←overall then min←n←overall:=n;
if max←n←stated>max←n←overall then max←n←overall:=max←n←stated;
if max←m←observed>max←m←stated then
  print←ln('The previous character should have had max m >= ',
    max←m←observed:1);
if n<min←n←stated then
  print←ln('The previous character should have had min n <= ',n:1)

@* System-dependent changes.
This section should be replaced, if necessary, by changes to the program
that are necessary to make \.{GFtype} work at a particular installation.
It is usually best to design your change file so that all changes to
previous sections preserve the section numbering; then everybody's version
will be consistent with the printed program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@↑system dependencies@>

@* Index.
Pointers to error messages appear here together with the section numbers
where each ident\-i\-fier is used.