% This program interconverts files between TeX and LN03 font file formats.

% Copyright (c) 1984, 1985 by Digital Equipment Corporation.
% UNDER DEVELOPMENT
% Version 0.0, early December 1984, initial functionality
% Version 0.0a, fixes by Scott Robinson to make input more efficient
% Version 0.1, late December, fixed bug: magnification field is no
%     longer ignored in PXL to LN03 conversion.
% Version 2, April-June 1985, a lot of random fixes.
% Version 3, January 1986, tln03 prints width.

% Here is TeX material that gets inserted after \input webmac
\def\hang{\hangindent 3em\indent\ignorespaces}
\font\ninerm=amr9

\def\title{Font File Converter}
\def\contentspagenumber{1}
\def\topofcontents{\null
  \def\titlepage{F} % include headline on the contents page
  \def\rheader{\mainfont\hfil \contentspagenumber}
  \vfill
  \centerline{\titlefont The Font File Converter}
  \vskip 15pt
  \centerline{(Version 3, January 1986)}
  \vfill}
\def\botofcontents{\vfill
  \centerline{\hsize 5in\baselineskip9pt
    \vbox{\ninerm\noindent
       Copyright 1984, 1985, 1986 by Digital Equipment Corporation.}}}
\pageno=\contentspagenumber \advance\pageno by 1

@* Introduction. The purpose of the Font File Converter (FFC) program is to
convert font files from the \TeX\ \.{PXL} format to the DEC Common Font
File Format. The program takes interactive commands from the terminal.
Right now, its conversion capability is quite limited, and the files FFC
produces are essentially usable only with the accompanying LN03 driver for
\TeX. 

A |banner| string is defined to identify the version:

@d banner=='Font File Converter, Version 3'

@ FFC is written in the Web language, but the underlying Pascal is
\hbox{VAX--11} Pascal, and no effort has been made to make anything
portable@^portability@>. In particular, it is assumed that Pascal strings
are ASCII strings. 

We do, however, use the ``standard'' Web name for the default in a
|case| statement:

@f othercases == else
@d othercases == otherwise

@ Detailed instructions for using FFC are given in a separate document. The
LN03 and \.{PXL} file formats are also described separately. 

The LN03 accepts font files in the DEC Common Font File Format. However, it
only reads certain fields of that format. FFC fills enough of these fields
to make the font files it generates acceptable to the LN03, when these
files are first massaged by the accompanying \TeX\ driver software. It
cannot be assumed that FFC fills all fields in the Common Font File Format
correctly. Neither can it be assumed that the files it generates are
acceptable to other software or devices that use the DEC Common Font File
Format. 

@ The overall structure of FFC is given below. 

@p
program FFC (input, output, ln03_file, pxl_file, outfile);
    label @<Labels in the outer block@>@/
    type @<Types in the outer block@>@/
    var @<Globals in the outer block@>@/
    @<Major procedures@>@/
begin
    writeln(banner); writeln;@/
    @<The main program@>@/
end.

@ There are two global labels, |final_end| for use when the program finishes,
and |cmd_prompt| for use when a command comes to an end:

@d final_end = 9999
@d cmd_prompt = 9998

@<Labels ...@>=
    cmd_prompt, final_end;

@ We define a file type |byte_file| for files consisting of 512-byte
blocks. This definition was introduced by Scott Robinson for efficiency
reasons. It is unclear at this point how this Pascal definition restricts
our ability to work with font files of the various types that can exist
in a Vax/VMS file system.

@<Types...@>=
@!eight_bits=0..255;
@!byte_block=packed array [0..511] of eight_bits;
@!byte_file=packed file of byte_block; {files that contain binary data}

@* Command input. The user's command lines are read into a buffer called
|inline|. The variables |ilp| and |istart| are used to point into it,
while |illen| keeps track of its length. The variable |verb| holds an
integer code representing the command verb.

@d the_char == inline[ilp]
@d the_substr == substr(inline,istart,ilp-istart)

@<Globals...@>= 
@!inline: varying[513] of char; 
@!ilp,istart,illen :integer;
@!verb :integer;

@ Each command consists of a verb, a space, and arguments. The command
handling procedure itself parses the arguments from |inline|. The verbs are
converted into integer values by the function |command_verb|. That
function is now coded inefficiently; it should eventually be implemented
using a hash table. 

@d nonesuch = 0 { there is no such command }
@d rln03 = 1  { read LN03 format file }
@d tln03 = 2  { type LN03 character rasters }
@d tln03long = 3  { type a longword from the LN03 file }
@d tln03word = 4  { type a word from the LN03 file }
@d rpxl = 5  { read \.{PXL} format file }
@d tpxl = 6  { type \.{PXL} character rasters }
@d tpxllong = 7  { type a longword from the \.{PXL} file }
@d tpxlword = 8  { type a word from the \.{PXL} file }
@d toln03 = 9  { convert the \.{PXL} file to an LN03 file }
@d wln03 = 10 { write the LN03 buffer out onto a file }
@d toln03x = 11  { convert the \.{PXL} file to an ``extended'' LN03 file }
@d exit = 99  { leave FFC }

@<Major procedures@>=
function command_verb :integer;
    var i :integer;
    begin

    @<Pick the verb out in |inline| and lowercase it@>@/
    if index(the_substr,'rln03') = 1 then command_verb := rln03
    else if index(the_substr,'tln03long') = 1 then command_verb := tln03long
    else if index(the_substr,'tln03word') = 1 then command_verb := tln03word
    else if index(the_substr,'tln03') = 1 then command_verb := tln03
    else if index(the_substr,'rpxl') = 1 then command_verb := rpxl
    else if index(the_substr,'tpxllong') = 1 then command_verb := tpxllong
    else if index(the_substr,'tpxlword') = 1 then command_verb := tpxlword
    else if index(the_substr,'tpxl') = 1 then command_verb := tpxl
    else if index(the_substr,'toln03x') = 1 then command_verb := toln03x
    else if index(the_substr,'toln03') = 1 then command_verb := toln03
    else if index(the_substr,'wln03') = 1 then command_verb := wln03
    else if index(the_substr,'exit') = 1 then command_verb := exit
    else command_verb := nonesuch
    end;

@ To pick out the command verb, we frequently use two parsing macros,
|skipb| and |skipnb|, which skip over blanks and non-blanks respectively in
the |inline| string, advancing the index variable |ilp| as they go. 

@d skipb == while (ilp <= illen) and (the_char = ' ') do ilp := ilp+1
@d skipnb == while (ilp <= illen) and (the_char <> ' ') do ilp := ilp+1

@<Pick the verb...@>=
skipb; istart := ilp; skipnb;@/
for i := istart to ilp-1 do begin
    if (inline[i] >= 'A') and (inline[i] <= 'Z') then 
    inline[i] := chr(ord(inline[i])+ord('a')-ord('A'))
end;

@ The main loop of the program is a loop of reading and executing commands:

@d cmd_error(#) == begin writeln(#); goto cmd_prompt end

@<The main program@>=
cmd_prompt:
    writeln;
    write('FFC>');
    readln(inline);@/
    if length(inline) = 513 then cmd_error('Command line too long');@/
    inline := inline+' ';
    ilp := 1;
    illen := length(inline);@/
    verb := command_verb;@/
    if verb = nonesuch then cmd_error('No such command')@/
    else @<Execute the command@>;@/
    goto cmd_prompt;
final_end:

@* Reading LN03 format files. We read the entire LN03 file into a single
large buffer: 

@d ln03_blocks = 512
@d ln03_buf_size == ((ln03_blocks*512)-1)
@d ln03_buf == l_buf.c

@<Globals...@>=
@!ln03_file: byte_file;
@!ln03_count,ln03_len,i: integer;
@!l_buf: packed record
	 case boolean of
		false: (c: packed array[0..ln03_buf_size] of char);
		true: (b: packed array [0..ln03_blocks-1] of byte_block);
	 end;

@ The file name is obtained from |inline|:

@<Execute...@>=
if verb = rln03 then begin
    skipb;
    istart := ilp;
    skipnb;@/
    open(ln03_file,the_substr,@=readonly@>,@=error:=continue@>);
    if status(ln03_file) > 0 then cmd_error('Couldn''t open file');
    reset(ln03_file);
    @<Read the LN03 file@>
end    

@ The LN03 file is read into |inline|, line by line, and copied into the
|ln03_buf|: 

@<Read the LN03 file@>=
ln03_count:=0;
while (not eof(ln03_file)) and (ln03_count < ln03_blocks) do
 begin
 read(ln03_file,l_buf.b[ln03_count]);
 ln03_count:=ln03_count+1;
 end;
ln03_len:=ln03_count*512;
close(ln03_file);
@<Check LN03 file@>

@ It would be desirable to check@^checking@> the LN03 file just read in, to
warn the user about anomalies. For the moment, though, we just look at the
opening two longwords. There are lots of other things we would like to do,
for example: check that all character locators and string descriptors point
into the right ranges, check that character definitions and strings in the
pool are in order, check that every character definition has the ``flag
flag'' set, check that character definitions are aligned and do not
overlap. 

@d ln03_long(#) == (ord(ln03_buf[#]) + (256*(ord(ln03_buf[(#)+1]) 
    			+ 256*(ord(ln03_buf[(#)+2])
    			+ 256*ord(ln03_buf[(#)+3])))))

@d ln03_word(#) == (ord(ln03_buf[#]) + 256*ord(ln03_buf[(#)+1]))

@<Check LN03 file@>=
if ln03_len < 16 then writeln('LN03 file too short');
if (ln03_buf[4] <> 'F') or (ln03_buf[5] <> 'O') or (ln03_buf[6] <> 'N') or
    (ln03_buf[7] <> 'T') then writeln('Second longword not FONT')

@* Reading PXL format files. The \.{PXL} format is simple. Again we read
the entire file into a single large buffer: 

@d pxl_blocks = 512
@d pxl_buf_size == ((pxl_blocks*512)-1)
@d pxl_buf == p_buf.c

@<Globals...@>=
@!pxl_file: byte_file;
@!pxl_count,pxl_len: integer;
@!p_buf: packed record
	 case boolean of
		false: (c: packed array[0..pxl_buf_size] of char);
		true: (b: packed array [0..pxl_blocks-1] of byte_block);
	 end;

@ The file name is obtained from |inline|:

@<Execute...@>=
else if verb = rpxl then begin
    skipb;
    istart := ilp;
    skipnb;@/
    open(pxl_file,the_substr,@=readonly@>,@=error:=continue@>);
    if status(pxl_file) > 0 then cmd_error('Couldn''t open file');
    reset(pxl_file);
    @<Read the \.{PXL} file@>
end    

@ The \.{PXL} file is read into |inline|, line by line, and copied into the
|pxl_buf|: 

@<Read the \.{PXL} file@>=
pxl_count:=0;
while (not eof(pxl_file)) and (pxl_count < pxl_blocks) do
 begin
 read(pxl_file,p_buf.b[pxl_count]);
 pxl_count:=pxl_count+1;
 end;
pxl_len:=pxl_count*512;
close(pxl_file);
@<Check \.{PXL} file@>

@ As was the case for the LN03 format file, we would also like to
check@^checks@> the \.{PXL} file just read in, to warn the user about
anomalies. 

On VMS systems, \.{PXL} files are usually padded with extra longwords at
the end so their length is a multiple of 512. Thus, we search backwards
from the end of the file, looking for the ID longword (value 1001), which
marks the real end of the file. We only search through the last 512 bytes,
however. 

The definitions of |pxl_long| and |pxl_word| take into account the fact
that the bytes in \.{PXL} file longwords are reversed with respect to the
normal VAX ordering. 

@d pxl_long(#) == (ord(pxl_buf[(#)+3]) + (256*(ord(pxl_buf[(#)+2]) 
    			+ 256*(ord(pxl_buf[(#)+1])
    			+ 256*ord(pxl_buf[#])))))

@d pxl_word(#) == (ord(pxl_buf[(#)+1]) + 256*ord(pxl_buf[#]))

@<Check \.{PXL} file@>=
if not (pxl_len mod 4 = 0) then writeln('PXL file length not multiple of 4');
if pxl_long(0) <> 1001 then writeln('Initial PXL format id wrong');
i := pxl_len-4;
while (i >= pxl_len-512) do begin
    if pxl_long(i) = 1001 then begin
    	pxl_len := i+4;
    	i := -1
    end else i := i-4
end;
if pxl_len < 16 then writeln('PXL file too short');
if pxl_long(pxl_len-4) <> 1001 then writeln('Final PXL format id wrong');

@* Typing the character rasters. When FFC gets a raster typing command, it
may read a character number from |inline| using the function |getfixnum|. 

@<Major...@>=
    function getfixnum :integer;
	label 1;
    	var x,x1 :integer;
    	    negative :boolean;

    begin
	x1 := 0;
	negative := false;
    	skipb;
	if ilp > illen then goto 1;
	if the_char = '-' then begin
	    negative := true;
	    ilp := ilp+1
	end;

	if (ilp > illen) or (the_char < '0') or (the_char > '9')
	then goto 1;

	x1 := 0;
	while  (the_char >= '0') and (the_char <= '9') do begin
	    x1 := 10*x1 + ord(the_char) - ord('0');
	    ilp := ilp+1;
	    if ilp > illen then goto 1;
	end;
	
    1:
	x := x1;
	if negative then x := -x;
	getfixnum := x

    end;    

@ The rasters corresponding to a character are stored in the area of the
LN03 file called the character definitions area. This area is pointed to by
entries in the character directory. The location of the character directory
is specified in the font file header. We define symbolic names for all the
offsets in the font file header: 

@d nob_offset = 0
@d font_offset = nob_offset+4
@d version_offset = font_offset+4
@d id_offset = version_offset+4
@d id_string_offset = id_offset + 8
@d revision_offset = id_string_offset + 64
@d date_offset = revision_offset + 4
@d attributes_offset = date_offset + 12
@d parameters_offset = attributes_offset + 8
@d chardir_offset = parameters_offset + 8
@d segment_list_offset = chardir_offset + 8
@d future_info_offset = segment_list_offset + 8
@d string_pool_offset = future_info_offset + 8
@d kern_info_offset = string_pool_offset + 8
@d chardefs_offset = kern_info_offset + 8
@d charcount_offset = chardefs_offset + 8
@d organization_flags_offset = charcount_offset + 32
@d size_of_char_params_offset = organization_flags_offset + 4
@d raster_expansion_info_offset = size_of_char_params_offset + 4

@ With these definitions, we figure out which character the user wants us
to display, determine where its locator is in the character directory, and
display the rasters. 

@<Execute...@>=
else if verb = tln03 then begin
    skipb;
    if ilp > illen then cmd_error('You have to say what character to type');
    if (the_char = '''') and (ilp < illen) then i := ord(inline[ilp+1])
    else i := getfixnum;
    if (i < ln03_long(charcount_offset)) or 
    	(i > ln03_long(charcount_offset+4)) 
    then cmd_error('No such character');
    i := 4*(i-ln03_long(charcount_offset))+ln03_long(chardir_offset+4);
    @<Find start of rasters for character@>;
    @<Actually type the rasters@>
end

@ The variable |def_start| is the location in the file where the character
definition begins. The rasters themselves begin a few bytes down from
there, at |ras_start|. The array |visible_byte| is used to hold the visible
form of a byte from the raster.

@<Global...@>=
@!def_start, ras_start: integer;
@!j,k,l,m,n: integer;  { scratch variables }
@!visible_byte: packed array[1..8] of char;

@ The character definition we are seeking is pointed to by a character
locator@^character locators@> in the character directory portion of the
LN03 file. 

In this version of FFC, we only support locators that point directly into
the file. The LN03 format also allows ``indirect'' locators that point to
other files, or to another locator. These are distinguished by a high bit 1
in the locator longword, whence the test |ln03_buf[i+3] >= chr(@"80)|
below. 

@<Find start...@>=
    if ln03_buf[i+3] >= chr(@"80) 
    then cmd_error('Indirect locators not supported');
    def_start := ord(ln03_buf[i])+256*ln03_word(i+1);
    if def_start = 0 then cmd_error('Locator is zero');
    ras_start := def_start+ln03_long(size_of_char_params_offset);
    writeln(' ma = ',ras_start:1);
    if ras_start > ln03_buf_size 
    then cmd_error('Character outside piece of file read')    	

@ The LN03 format allows the raster to be stored in a number of ways. The
rasters may be placed in one of eight different ``orientations,''
corresponding to repeated $90$ degree orthogonal rotation and mirroring.
When we type out the rasters, we just show things the way they are in the
file. All we need to know is if the orientation is landscape or portrait,
which can be tested by determining of the orientation value is odd or even.

In addition to the different orientations, the format supports run length
encoding@^run length encoding@>. The current version of FFC does not,
however. We therefore require that the ``Type1'' field in the rasters equal
|@"81|, which indicates no run length encoding: 

@<Actually type...@>=
    writeln('width ',((ln03_long(def_start+4))/24.0):5:1,' pixels');
    if ord(ln03_buf[ras_start+1]) <> @"81 
    then cmd_error('Run length encoding not supported');
    if odd(ord(ln03_buf[ras_start])) then begin { landscape }
    	writeln('landscape');
	i := ln03_word(ras_start+4);    
    	j := ln03_word(ras_start+6)    	
    end else begin { portrait }
    	writeln('portrait');
	i := ln03_word(ras_start+6);    
    	j := ln03_word(ras_start+4)
    end;
    writeln(i:1,' rows ',j:1,' columns');
    
    k := i div 8;
    if i <> 8*k then k := k+1;
    for l := 0 to j-1 do begin
    	for m := 0 to k-1 do begin
    	    binrep(ord(ln03_buf[ras_start+8+k*l+m]));
    	    write(visible_byte)
    	end;
    	writeln
    end

@ The |binrep| procedure used in the preceding section writes the visible
representation of a bitmap byte into the array |visible_byte|. The visible
representation is binary, padded out to a width of 8, using a `\.{B}' to
represent 1 and a period `\.{.}' to represent 0. 

@<Major...@>=
procedure binrep (v: integer);
    var cnt, rem, quo: integer;
begin
    visible_byte := '........';
    quo := v;
    for cnt := 1 to 8 do begin
    	rem := quo mod 2;
    	quo := quo div 2;
    	if rem <> 0 then visible_byte[cnt] := 'B'
    end;
end;

@ The code up to now has been concerned with typing rasters from LN03
files. \.{PXL} files are similar but simpler. The orientation is always
portrait. The directory information is always located at a fixed position
with respect to the end of the file. 

@<Execute...@>=
else if verb = tpxl then begin
    skipb;
    if ilp > illen then cmd_error('You have to say what character to type');
    if (the_char = '''') and (ilp < illen) then i := ord(inline[ilp+1])
    else i := getfixnum;
    if (i < 0) or (i > 127) then cmd_error('No such character'); 
    def_start := pxl_len - 4*517 + 16*i;
    ras_start := 4*pxl_long(def_start+8);
    writeln(' rasters at ',ras_start:1);
    if ras_start > pxl_len then cmd_error('Rasters outside file');
    i := pxl_word(def_start);    
    j := pxl_word(def_start+2);
    writeln(i:1,' columns ',j:1,' rows');
    k := i div 32;
    if i <> 32*k then k := k+1;
    for l := 0 to j-1 do begin
    	for m := 0 to k-1 do begin
    	    rev_binrep(ord(pxl_buf[ras_start+4*k*l+4*m]));
    	    write(visible_byte);
    	    rev_binrep(ord(pxl_buf[ras_start+4*k*l+4*m+1]));
    	    write(visible_byte);
    	    rev_binrep(ord(pxl_buf[ras_start+4*k*l+4*m+2]));
    	    write(visible_byte);
    	    rev_binrep(ord(pxl_buf[ras_start+4*k*l+4*m+3]));
    	    write(visible_byte)
    	end;
    	writeln
    end
end

@ A different |binrep| procedure is wanted here, one that puts the
bits into |visible_byte| in opposite order:

@<Major...@>=
procedure rev_binrep (v: integer);
    var cnt, rem, quo: integer;
begin
    visible_byte := '........';
    quo := v;
    for cnt := 1 to 8 do begin
    	rem := quo mod 2;
    	quo := quo div 2;
    	if rem <> 0 then visible_byte[9-cnt] := 'B'
    end;
end;


@* Typing raw data from the file. For preliminary debugging purposes, it is
useful to have the ability to type raw data from a font file. This is the
purpose of the following simple code: 

@<Execute...@>=
else if verb = tln03long then begin
    i := getfixnum;
    if (i >= 0) and (i < ln03_len) then writeln(ln03_long(i):1)
    else writeln('Location not in file')
end
else if verb = tln03word then begin
    i := getfixnum;
    if (i >= 0) and (i < ln03_len) then writeln(ln03_word(i))
    else writeln('Location not in file')
end
else if verb = tpxllong then begin
    i := getfixnum;
    if (i >= 0) and (i < pxl_len) then writeln(pxl_long(i):1)
    else writeln('Location not in file')
end
else if verb = tpxlword then begin
    i := getfixnum;
    if (i >= 0) and (i < pxl_len) then writeln(pxl_word(i))
    else writeln('Location not in file')
end

@* Conversion to LN03 form. When converting a font from \.{PXL} to LN03
form, in addition to converting the character rasters, we have to fill
in a number of font parameter slots. These slots are located in the
first three areas of the font file. We already defined offset constants
into the first area, the font file header. Here are some offsets into the
other two areas, the ``font attributes'' and the ``font parameters.''

@d flags_offset = raster_expansion_info_offset + 48
@d strings_offset = flags_offset + 4
@d type_size_offset = strings_offset + 48
@d foundry_offset = type_size_offset + 36
@d param_flags_offset = foundry_offset + 16
@d lining_offset = param_flags_offset + 4
@d subsup_offset = lining_offset + 32
@d hspace_offset = subsup_offset + 16
@d vspace_offset = hspace_offset + 32
@d charxdir_offset = vspace_offset + 40

@ When we are asked to convert to LN03 form, we first clear the |ln03_buf|,
then we convert the rasters, and finally we fill the parameters. For the
moment, to comply with the LN03 format's restriction to 94 characters, we
only convert characters 33 through 126 of the \.{PXL} file. The rest are
discarded. If the command is \.{toln03x}, however, the entire 128
characters are copied, violating the LN03 format standard. 

@<Execute...@>=
else if (verb = toln03) or (verb = toln03x) then begin
    for i := 0 to ln03_buf_size do ln03_buf[i] := chr(0);
    if verb = toln03 then begin first_char := 33; last_char := 126 end
    else begin first_char := 0; last_char := 127 end;
    num_chars := last_char-first_char+1;    	
    @<Convert \.{PXL} rasters to LN03 form@>;
    @<Fill LN03 parameters@>
end

@ We have to know where to put the rasters as we convert them, which means
we have to know where the ``character definitions'' area begins in the LN03
file. That area comes after the character directory (4 bytes per
character), the table of font segments (4 bytes, to indicate there are
no font segments), the future info section (empty), the string pool,
and the kerning info (empty). 

The string pool has to contain the character set designator (7 bytes), type
family ID (7 bytes), type family name, Font ID (16 bytes), type category,
foundry, font designer, and segment names. For the moment, converted files
have a 16-byte type family name and empty type category, foundry, font
designer and segment names. The total length is 46 bytes, rounded up to 48
bytes. 

To fill some slots in the LN03 format, we have to add up the sizes of the
rasters in three different ways---portrait, landscape and
``mixed''---whence the variables |psize|, |lsize| and |msize|. 

@<Globals...@>= 
@!charxdef_offset,string_pool_size,stringx_pool_offset: integer;
@!psize,lsize,msize: integer; 
@!first_char,last_char,num_chars: integer;
@!all_blank: boolean;
@!zchar,tfm_width,x_offset,y_offset: integer;
@!dsize,mag: real;

@ The |charxdef_offset| can be computed from the information above. (We
could have defined this offset as a constant, but want to leave ourselves a
little more flexibility.) 

@<Convert \.{PXL} rasters...@>=
    string_pool_size := 48;
    stringx_pool_offset := charxdir_offset+4*num_chars+4;
    charxdef_offset := stringx_pool_offset+string_pool_size;
    ln03_len := charxdef_offset;
    psize := 0; lsize := 0; msize := 0;
    for zchar := first_char to last_char do 
    	begin @<Convert \.{PXL} for |zchar|@> end;

@ When converting for a single character, we first fill in the ``flag
flag'' of the character, which the format requires us to set. Then we copy
the nominal width, left bearing and raster baseline. 

The LN03 format demands that these dimensions be converted to
``centipoints,'' where a ``centipoint@^centipoint (unit of measure)@>'' is
defined to be one 7200$^{\rm th}$ of an inch. The nominal width is stored
in the \.{PXL} file as a fraction of the magnification times design size.
The magnification, multiplied by five times the dots per inch (in our case
1500), is stored in the fourth-from-last longword of the \.{PXL} file. The
design size is stored in the third-from-last longword in units of $2^{-20}$
``points,'' a ``point'' being defined as one 72.27$^{\rm th}$ of an inch. 

The left bearing and raster baseline are stored in pixel units in the
\.{PXL} file and in centipoints in the LN03 file, so the conversion
factor is $7200/300 = 24$. 

@d two_to_the_20th == (@"100000)

@<Convert \.{PXL} for ...@>=
    ln03_buf[ln03_len+3] := chr(@"80);
    def_start := pxl_len - 4*517 + 16*zchar;
    ras_start := 4*pxl_long(def_start+8);
    if ras_start > pxl_len then cmd_error('Rasters outside file for ',
    	zchar:1);
    tfm_width := pxl_long(def_start+12);
    dsize := pxl_long(pxl_len-12);
    mag := pxl_long(pxl_len-16);
    dsize := (dsize/two_to_the_20th)*(mag/1500.0);
    x_offset := signed_pxl_word(def_start+4);
    y_offset := signed_pxl_word(def_start+6);
    set_ln03_long(ln03_len+4,round((dsize*7200.0*tfm_width)/
	(two_to_the_20th*72.27)));
    set_ln03_long(ln03_len+8,-24*x_offset);
    set_ln03_long(ln03_len+12,-24*y_offset);

@ Even though we're not trying to make this program portable, we do
the word and longword computations by hand, instead of using overlays.

@<Major...@>=
    function signed_pxl_word (index: integer) :integer;
    begin
    	if ord(pxl_buf[index]) >= @"80 then 
	    signed_pxl_word := pxl_word(index) - @"10000
	else signed_pxl_word := pxl_word(index)
    end;

    procedure set_ln03_long (index, value: integer);
    var negative: boolean;
	a,b,c,d,carry: integer;
    begin
	if value < 0 then negative := true else negative := false;
	if negative then value := -value;
	a := value mod 256;
	b := (value div 256) mod 256;
	c := (value div (256*256)) mod 256;
	d := value div (256*256*256);
	if negative then begin
	    carry := 0;
	    a := 256-a;
	    if a = 256 then begin a := 0; carry := 1 end;
	    b := 255+carry-b;
	    if b = 256 then b := 0 else carry := 0;
	    c := 255+carry-c;
	    if c = 256 then c := 0 else carry := 0;
	    d := 255+carry-d;
	    if d = 256 then d := 0 
	end;
	ln03_buf[index] := chr(a);
	ln03_buf[index+1] := chr(b);
	ln03_buf[index+2] := chr(c);
	ln03_buf[index+3] := chr(d)
    end;

@ The rasters are always placed in portrait into the LN03 file, with no use
of run-length encoding, so the ``orient'' field of the raster format is 0,
and the ``Type1'' field is @"81. We copy the rasters byte by byte from the
\.{PXL} buffer, reversing each byte. At the end we store the raster address
in the character directory and increment the |pxl_len| variable, making
sure that all character definitions fall on even-byte boundaries. We also
increment the size variables. 

If the rasters for a character are completely blank, an ``undocumented
feature'' of the LN03 seems to cause things to go wrong. Thus, in that
case, we set height and width to 1, and put in a blank byte. 

@<Convert \.{PXL} for...@>=
    ln03_buf[ln03_len+17] := chr(@"81);
    i := pxl_word(def_start);
    j := pxl_word(def_start+2);
    all_blank := (i = 0) and (j = 0);
    if all_blank then begin i := 1; j := 1 end;
    ln03_buf[ln03_len+20] := chr(j mod 256);
    ln03_buf[ln03_len+21] := chr(j div 256);
    ln03_buf[ln03_len+22] := chr(i mod 256);
    ln03_buf[ln03_len+23] := chr(i div 256);
    k := i div 32;
    if i <> 32*k then k := k+1;
    n := i div 8;
    if i <> 8*n then n := n+1;
    if not all_blank then
    for l := 0 to j-1 do 
    	for m := 0 to n-1 do 
    	    ln03_buf[ln03_len+24+n*l+m] :=
    		reverse_byte(pxl_buf[ras_start+4*k*l+m]);
    set_ln03_long(charxdir_offset+4*(zchar-first_char),ln03_len);
    ln03_len := ln03_len+24+j*n;
    if odd(ln03_len) then ln03_len := ln03_len+1;@/
    { now update the size counts }
    psize := psize+j*n;
    k := j div 8;
    if j <> 8*k then k := k+1;
    lsize := lsize+i*k;
    if i*k > j*n then msize := msize+i*k else msize := msize+j*n;

@ It is necessary to use a function |reverse_byte| to reverse the bits in
going from the \.{PXL} to the LN03 raster format. It would be faster to do
this with a 256-byte string. 

@<Major...@>=
    function reverse_byte (u: char) : char;
    var cnt,rv :integer;
    begin
    	binrep(ord(u));
    	rv := 0;
    	for cnt := 1 to 8 do
	    if visible_byte[cnt] = 'B' then rv := 1 + 2*rv
	    else rv := 2*rv;
	reverse_byte := chr(rv)
    end;

@* Filling in the LN03 parameters. Once the rasters are transferred from
\.{PXL} to LN03 format, we have to fill in the endless LN03 parameters,
using the offsets defined above. We will only fill parameters that are not
left at zero, since we already initialized the |ln03_buf| to zero above. We
start off with the header and trailer, placing |'FONT'| and the |ln03_len|
in the opening and closing longwords. 

@<Fill LN03 parameters@>=
    if ln03_len mod 4 <> 0 then ln03_len := ln03_len + (4 - (ln03_len mod 4));
    ln03_len := ln03_len+8; { for trailer }
    set_ln03_long(0,ln03_len);
    set_ln03_long(ln03_len-8,ln03_len);
    ln03_buf[4] := 'F';
    ln03_buf[ln03_len-4] := 'F';
    ln03_buf[5] := 'O';
    ln03_buf[ln03_len-3] := 'O';
    ln03_buf[6] := 'N';
    ln03_buf[ln03_len-2] := 'N';
    ln03_buf[7] := 'T';
    ln03_buf[ln03_len-1] := 'T';

@ There follow the version number of the format (always 1), the font file
ID (always the value indicated below), the revision number of the font
(always 0), and the date and time record (always 2:00 {\eightrm P.M.},
September 11, 1973). Eventually the user will be able to set the font file
ID with a command, and the time will come from the timestamp on the \.{PXL}
file. 

@<Fill LN03 parameters@>=
    ln03_buf[version_offset] := chr(1);
    ln03_buf[id_offset] := chr(31);
    ln03_buf[id_offset+4] := chr(id_offset+8);
    font_id_string := 'U000000002SK00GG0001UZZZZ02F000';
    for i := 1 to 31 do ln03_buf[id_string_offset+i-1] :=
    	font_id_string[i];
    ln03_buf[date_offset] := chr(1973 mod 256);
    ln03_buf[date_offset+1] := chr(1973 div 256);
    ln03_buf[date_offset+2] := chr(9);
    ln03_buf[date_offset+4] := chr(11);
    ln03_buf[date_offset+6] := chr(14);

@ Oops, we forgot to declare the |font_id_string|:

@<Globals...@>=
@!font_id_string: packed array [1..31] of char;

@ After the date and time come the various pointers into the file. 

@<Fill LN03 parameters@>=
    set_ln03_long(attributes_offset,param_flags_offset-flags_offset);
    set_ln03_long(attributes_offset+4,flags_offset);
    set_ln03_long(parameters_offset,charxdir_offset-param_flags_offset);
    set_ln03_long(parameters_offset+4,param_flags_offset);
    set_ln03_long(chardir_offset,4*num_chars);
    set_ln03_long(chardir_offset+4,charxdir_offset);
    set_ln03_long(segment_list_offset,4);
    set_ln03_long(segment_list_offset+4,charxdir_offset+4*num_chars);
    set_ln03_long(future_info_offset+4,charxdir_offset+4*num_chars+4);
    set_ln03_long(string_pool_offset,string_pool_size);
    set_ln03_long(string_pool_offset+4,charxdir_offset+4*num_chars+4);
    set_ln03_long(kern_info_offset+4,charxdir_offset+4*num_chars+4+
    	string_pool_size);
    set_ln03_long(chardefs_offset,ln03_len-8-charxdef_offset);
    set_ln03_long(chardefs_offset+4,charxdef_offset);

@ Next come the eight longwords of character count information, the
``organization'' flags, the size of character parameters, and the raster
expansion information. 

@<Fill LN03 parameters@>=
    set_ln03_long(charcount_offset,first_char); { first character }
    set_ln03_long(charcount_offset+4,last_char); { last character }
    set_ln03_long(charcount_offset+28,32); { space encoding }
    set_ln03_long(charcount_offset+32,@"A8); { ``organization'' flags }
    set_ln03_long(charcount_offset+36,16); { size of character params. }
    set_ln03_long(raster_expansion_info_offset,num_chars); { in file locator count }
    set_ln03_long(raster_expansion_info_offset+8,num_chars); { number of character defs. }
    set_ln03_long(raster_expansion_info_offset+16,num_chars); { number of rasters }
    set_ln03_long(raster_expansion_info_offset+24,psize); 
    set_ln03_long(raster_expansion_info_offset+28,lsize); 
    set_ln03_long(raster_expansion_info_offset+32,msize); 

@ Next come the font attributes. These include a number of strings that
have to be placed in the string pool. Of the flags, we set the Roman one
only. The character set designator is always set to |'0B'|, tab, |'ZZZZ'|.
Again, eventually the user will have commands to set these. 

@<Fill LN03 parameters@>=
    set_ln03_long(flags_offset,2);	{ flags }
    set_ln03_long(flags_offset+4,7);    { length of character set designator }
    set_ln03_long(flags_offset+8,stringx_pool_offset);
    ln03_buf[stringx_pool_offset] := '0';
    ln03_buf[stringx_pool_offset+1] := 'B';
    ln03_buf[stringx_pool_offset+2] := chr(9);
    ln03_buf[stringx_pool_offset+3] := 'Z';
    ln03_buf[stringx_pool_offset+4] := 'Z';
    ln03_buf[stringx_pool_offset+5] := 'Z';
    ln03_buf[stringx_pool_offset+6] := 'Z';
    set_ln03_long(flags_offset+12,7);	{ length of type family ID }
    set_ln03_long(flags_offset+16,stringx_pool_offset+7);
    for i := 1 to 7 do
    	ln03_buf[stringx_pool_offset+7+i-1] := font_id_string[i];
    set_ln03_long(flags_offset+20,16);  { length of type family name }
    set_ln03_long(flags_offset+24,stringx_pool_offset+7+7);
    for i := 1 to 16 do
    	ln03_buf[stringx_pool_offset+7+7+i-1] := ' ';
    set_ln03_long(flags_offset+28,16);  { length of font id }
    set_ln03_long(flags_offset+32,stringx_pool_offset+7+7+16);
    for i := 1 to 16 do
    	ln03_buf[stringx_pool_offset+7+7+16+i-1] := font_id_string[i];

@ The type size is given in the \.{PXL} file in units of $2^{-20}$ points;
we have to translate it to points. The average character width is
arbitrarily set to half the point size (which means we multiply it by 50 to
convert it to centipoints). 

@<Fill LN03 parameters@>=
    i := pxl_long(pxl_len-12);
    points := i div two_to_the_20th;
    k := (10000*i mod two_to_the_20th) div two_to_the_20th;
    ln03_buf[type_size_offset] := chr(points mod 256);
    ln03_buf[type_size_offset+1] := chr(points div 256);
    ln03_buf[type_size_offset+2] := chr(k mod 256);
    ln03_buf[type_size_offset+3] := chr(k div 256);
    if k > 4999 then points := points+1;
    set_ln03_long(type_size_offset+4,50*points); 

@ We need to declare |points|, by the way, which stores the pointsize of
the font as an integer.

@<Globals...@>=
@!points: integer;

@ Next come the resolution, weight, horizontal proportion, horizontal
proportion fraction, aspect ratio and character up vector. 

@<Fill LN03 parameters@>=
    ln03_buf[type_size_offset+10] := chr(24); { hocus-pocus means 300 dpi }
    ln03_buf[type_size_offset+12] := chr(16); { weight is ``regular'' }
    ln03_buf[type_size_offset+16] := chr(16); { horizontal proportion is regular}
    ln03_buf[type_size_offset+20] := chr(1);
    ln03_buf[type_size_offset+22] := chr(1);@/
    ln03_buf[type_size_offset+24] := chr(1);  { aspect ratio is 1/1}
    ln03_buf[type_size_offset+26] := chr(1);
    ln03_buf[type_size_offset+30] := chr(1); { character up vector }

@ The third and last step of the filling process handles the font
parameters, which are mostly supposed to be in ``centipoints.'' Eventually
some of these will be obtained from the \.{TFM} file, but for the moment we
wing it, guessing them all from the point size |points|. 

The flag longword that begins the font parameter section can conveniently
be left empty. The slant is not guessed, but always set to zero. As you can
see, the values of the horizontal and vertical spacing parameters are
fairly random, but should not lead to impossibly poor output. 

@<Fill LN03 parameters@>=
    set_ln03_long(lining_offset,12*points); { underline offset }
    set_ln03_long(lining_offset+4,8*points); { underline thickness }
    set_ln03_long(lining_offset+8,-25*points); { strike through offset }
    set_ln03_long(lining_offset+12,8*points); { strike through thickness }
    set_ln03_long(lining_offset+16,-60*points); { overline offset }
    set_ln03_long(lining_offset+20,8*points); { overline thickness }
    ln03_buf[lining_offset+26] := chr(1);   { slant (always 0) }
    ln03_buf[lining_offset+30] := chr(points*12 mod 256); { shadow vector }
    ln03_buf[lining_offset+31] := chr(points*12 div 256);
    set_ln03_long(subsup_offset,-36*points);
    set_ln03_long(subsup_offset+8,16*points);
    set_ln03_long(hspace_offset,24*points);  { center line }
    set_ln03_long(hspace_offset+4,20*points); { minimum space width }
    set_ln03_long(hspace_offset+8,80*points);  { maximum space width }
    set_ln03_long(hspace_offset+12,25*points); { width of space }
    set_ln03_long(hspace_offset+16,100*points); { width of em }
    set_ln03_long(hspace_offset+20,50*points); { width of en }
    set_ln03_long(hspace_offset+24,10*points); { width of thinspace }
    set_ln03_long(hspace_offset+28,35*points); { width of digit }
    set_ln03_long(vspace_offset,-64*points); { top line }
    set_ln03_long(vspace_offset+4,-50*points); { floating accent line }
    set_ln03_long(vspace_offset+8,-35*points); { half line }
    set_ln03_long(vspace_offset+12,100*points); { total vertical size }
    set_ln03_long(vspace_offset+16,-65*points); { above baseline }
    set_ln03_long(vspace_offset+20,35*points); { below baseline }
    set_ln03_long(vspace_offset+24,65*points); { cap H height }
    set_ln03_long(vspace_offset+28,35*points); { small x height }
    set_ln03_long(vspace_offset+32,10*points); { white space above tallest }
    set_ln03_long(vspace_offset+36,10*points); { white space below deepest }

@* Writing the LN03 file. It seems to be conventional to store LN03 format
font files as 512-byte fixed length record VAX files. For this reason, we
declare |outfile| to be this type of file: 

@<Globals...@>=
@!outfile: byte_file;

@ The writing simply consists of putting chunks of the |ln03_buf| out
into |outfile|.

@<Execute...@>=
else if verb = wln03 then begin
    skipb;
    if ilp > illen then cmd_error('You must specify a file to write into');
    istart := ilp; skipnb;
    open(outfile,the_substr,@=error:=continue@>);
    if status(outfile) <> 0 then cmd_error('couldn''t open',the_substr);
    rewrite(outfile);
    i := ln03_len div 512;
    if ln03_len <> i*512 then i := i+1;
    for j := 0 to i-1 do
        write(outfile,l_buf.b[j]);
    close(outfile)
end    

@* Leaving the program. If the user types an |exit| command, we leave:

@<Execute...@>=
else if verb = exit then goto final_end

@* Index. This is the standard Web index to all identifiers.
