PROGRAM PSDVI (output);

CONST version = 'This is PSDVI, version 3.0';

(* Author:         Andrew Trevorrow
   Implementation: Pyramid Pascal

   Description:
   This program is run by the psprint script to read a DVI file
   and create an equivalent PostScript file.

   Notes:
 - See AAAREAD.ME for revision history.
 - Debugging code is bracketed by { DEBUG } ... { GUBED }.
   This code will be commented out in the final working version.
 - System-dependent code is indicated by the string "SYSDEP".
 - Uncertain code is indicated by the string "???".
 - Unfinished code is indicated by the string "!!!".
 - Goto 999 is used to emulate a RETURN (from procedure) statement.
 - Goto <= 888 is used to emulate an EXIT (from loop) statement.
 - The above notes are also true for all separately compiled modules.
*)

#include 'globals.h';
#include 'options.h';
#include 'dvireader.h';
#include 'fontreader.h';
#include 'pswriter.h';

CONST
   pagesperline = 10;              (* number of pages displayed per line *)

VAR
   papertop,
   paperleft,
   paperbottom,
   paperright,                     (* these define the edges of the paper *)
   pagecount : INTEGER;            (* count of pages actually output *)
   unusedfont : fontinfoptr;       (* first unused font in sorted fontlist *)

(******************************************************************************)

PROCEDURE Initialize;

BEGIN
(* top left corner of paper is fixed at (-1",-1") *)
papertop    := -resolution;
paperleft   := -resolution;
paperbottom := papertop  + paperht - 1;
paperright  := paperleft + paperwd - 1;
warncount   := 0;
pagecount   := 0;
END; (* Initialize *)

(**********************************************************************)

PROCEDURE ShowUnits;

BEGIN
CASE units OF
   ic : write('in') ;
   cm : write('cm') ;
   mm : write('mm') ;
   pc : write('pc') ;
   pt : write('pt') ;
   bp : write('bp') ;
   px : write('px') ;
END;
END; (* ShowUnits *)

(******************************************************************************)

PROCEDURE ShowDimension (pixels : INTEGER);

(* Show the given pixel dimension in terms of units. *)

LABEL 999;

VAR realdim : REAL;   fracpart : INTEGER;

BEGIN
CASE units OF
   ic : realdim := pixels / resolution ;
   cm : realdim := pixels / resolution * 2.54 ;
   mm : realdim := pixels / resolution * 25.4 ;
   pc : realdim := pixels / resolution * 72.27 / 12.0 ;
   pt : realdim := pixels / resolution * 72.27 ;
   bp : realdim := pixels / resolution * 72.0 ;
   px : BEGIN write(pixels:1, 'px'); goto 999 END;
END;
(* show realdim to an accuracy of 1 decimal place *)
IF ABS(realdim) < 0.05 THEN
   write('0.0')
ELSE BEGIN
   IF realdim < 0.0 THEN BEGIN
      write('-');
      realdim := ABS(realdim);
   END;
   realdim := realdim + 0.05;     (* round up to 1 decimal place *)
   write(TRUNC(realdim):1);       (* whole part *)
   write('.');
   fracpart := TRUNC((realdim - TRUNC(realdim)) * 10.0);
   (* fracpart is now 0..9 *)
   write(fracpart:1);
END;
ShowUnits;
999:
END; (* ShowDimension *)

(******************************************************************************)

PROCEDURE ShowOptions;

(* Show DVI file name and option values. *)

BEGIN
writeln(version); writeln;
writeln('DVI file          = ', DVIname:Len(DVIname));
writeln('PostScript file   = ', PSname:Len(PSname));
writeln('Header file       = ', header:Len(header));
writeln('Resolution        = ', resolution:1, ' pixels per inch');
write  ('Magnification     = ', mag:1);
IF mag <> DVImag THEN
   writeln(' (DVI mag of ', DVImag:1, ' was overridden)')
ELSE
   writeln(' (DVI mag)');
writeln('TFM directory     = ', tfmdir:Len(tfmdir));
writeln('PS font prefix    = ', psprefix:Len(psprefix));
writeln('Font directory    = ', fontdir:Len(fontdir));
writeln('Dummy font        = ', dummyfont:Len(dummyfont));
write  ('Horizontal offset = '); ShowDimension(hoffset); writeln;
write  ('Vertical offset   = '); ShowDimension(voffset); writeln;
write  ('Paper width       = '); ShowDimension(paperwd); writeln;
write  ('Paper height      = '); ShowDimension(paperht); writeln;
write  ('Units             = '); ShowUnits;              writeln;
write  ('Reverse           = ');
IF reverse THEN writeln('true') ELSE writeln('false');
write  ('Stats             = ');
IF stats THEN writeln('true') ELSE writeln('false');
write  ('Conserve VM       = ');
IF conserveVM THEN writeln('true') ELSE writeln('false');
write  ('Pages             = ');
IF subrange THEN BEGIN
   IF firstDVIpage = 0 THEN write(firstTeXpage:Len(firstTeXpage))
                       ELSE write(firstDVIpage:1);
   write(':');
   IF finalDVIpage = 0 THEN write(finalTeXpage:Len(finalTeXpage))
                       ELSE write(finalDVIpage:1);
END
ELSE
   write('all pages');
IF increment > 1 THEN write(', but with an increment of ',increment:1);
writeln;
writeln;
END; (* ShowOptions *)

(******************************************************************************)

FUNCTION GetInteger (VAR str  : string;        (* in *)
                     strlen   : INTEGER;       (* in *)
                     VAR pos  : INTEGER;       (* in/out *)
                     VAR n    : INTEGER        (* out *)
                    ) : BOOLEAN;

(* Extract an integer from given str starting at given pos.
   pos is also used to return the position after the integer.
   If no integer is found then set n to 0 and return FALSE (pos will only
   change if leading spaces were skipped).
   If ABS(n) > limit then set n to sign * limit.
   Valid syntax is  +{digit}  or  -{digit}  or  digit{digit}.
   Note that a + or - by itself is valid and sets n to 0.
*)

LABEL 777, 888;

CONST limit = 2147483647;         (* 2^31 - 1 *)
      threshold = limit DIV 10;   (* nearing overflow *)

VAR   absval, last : INTEGER;
      sign : INTEGER;
      inttoobig : BOOLEAN;

BEGIN
WHILE pos < strlen DO BEGIN   (* skip any spaces *)
   IF str[pos] <> ' ' THEN goto 888;
   pos := pos + 1;
END;
888:
absval := 0; sign := 1; last := pos;
inttoobig := FALSE;
IF pos < strlen THEN BEGIN
   IF str[pos] = '-' THEN BEGIN
      sign := -1;
      last := last + 1;
   END
   ELSE IF str[pos] = '+' THEN
      last := last + 1;
   WHILE last < strlen DO BEGIN
      IF (str[last] < '0') OR (str[last] > '9') THEN goto 777;
      IF (absval > threshold) OR ((absval = threshold) AND (str[last] > '7'))
         THEN
         inttoobig := TRUE
      ELSE
         absval := absval * 10 + (ORD(str[last]) - ORD('0'));
      last := last + 1;
   END;
   777:
END;
IF pos = last THEN BEGIN
   n := 0;
   GetInteger := FALSE;
END
ELSE BEGIN
   pos := last;
   IF inttoobig THEN absval := limit;
   n := sign * absval;
   GetInteger := TRUE;
END;
END; (* GetInteger *)

(******************************************************************************)

FUNCTION ParseTeXPage (VAR pagestring : string;
                       VAR newTeXpage : TeXpageinfo) : BOOLEAN;

(* Return TRUE if TeX page specification in pagestring is valid.  If so then
   newTeXpage will contain the appropriate information for CurrMatchesNew.
   The syntax of a TeX page specification is [n{.n}] where n is any integer as
   defined by GetInteger.  Up to 10 integers may be given and are separated by
   periods, even if absent.  Trailing periods may be omitted.  Spaces before
   and after integers and periods are skipped.  The 10 positions correspond to
   the \count0, \count1, ... ,\count9 values that TeX stores with every page.
*)

LABEL 555, 666, 777, 888, 999;

VAR pos, len : INTEGER;

BEGIN
WITH newTeXpage DO BEGIN
   pos := 0;
   IF pagestring[pos] <> '[' THEN BEGIN
      writeln('[ expected!');
      ParseTeXPage := FALSE;
      goto 999;
   END;
   lastvalue := 0;
   len := 0;
   WHILE len < maxstring DO
      IF pagestring[len] = ' ' THEN goto 888 ELSE len := len + 1;
   888:
   WHILE TRUE DO BEGIN
      pos := pos + 1;
      present[lastvalue] := GetInteger(pagestring, len, pos, value[lastvalue]);
      (* pos now at len, space, period, non-digit or ']' *)
      WHILE pos < len DO BEGIN
         IF pagestring[pos] <> ' ' THEN goto 777;
         pos := pos + 1;              (* skip any spaces *)
      END;
      777:
      IF pos = len THEN BEGIN         (* check this first! *)
         writeln('] expected!');
         ParseTeXPage := FALSE;
         goto 999;
      END;
      IF pagestring[pos] = ']' THEN   (* end of TeX page specification *)
         goto 666;
      IF lastvalue < 9 THEN
         lastvalue := lastvalue + 1
      ELSE BEGIN
         writeln('] expected after 10 integers!');
         ParseTeXPage := FALSE;
         goto 999;
      END;
      IF pagestring[pos] <> '.' THEN BEGIN
         writeln('Period, integer or ] expected!');
         ParseTeXPage := FALSE;
         goto 999;
      END;
   END;
   666:
   WHILE lastvalue > 0 DO BEGIN
      IF present[lastvalue] THEN goto 555;
      lastvalue := lastvalue - 1;
   END;
   555:
END;
ParseTeXPage := TRUE;
999:
END; (* ParseTeXPage *)

(******************************************************************************)

PROCEDURE CheckPageRange;

(* If user requested a page subrange then we make sure it is valid. *)

LABEL 777, 888;

VAR newTeXpage : TeXpageinfo;

BEGIN
IF NOT subrange THEN BEGIN    (* translate all pages *)
   firstDVIpage := 1;
   finalDVIpage := totalpages;
END
ELSE BEGIN
   IF firstDVIpage = 0 THEN   (* parse and locate firstTeXpage *)
      IF ParseTeXPage(firstTeXpage,newTeXpage) THEN BEGIN
         MoveToDVIPage(1);
         (* go forwards until newTeXpage matches currTeXpage *)
         WHILE TRUE DO BEGIN
            IF CurrMatchesNew(newTeXpage) THEN BEGIN
               firstDVIpage := currDVIpage;
               goto 888;
            END
            ELSE IF currDVIpage = totalpages THEN BEGIN
               writeln('First TeX page does not exist!');
               exit(1);
            END
            ELSE
               MoveToDVIPage(currDVIpage + 1);
         END;
         888:
      END
      ELSE BEGIN
         writeln('Error in first TeX page!');
         exit(1);
      END;
   IF finalDVIpage = 0 THEN   (* parse and locate finalTeXpage *)
      IF ParseTeXPage(finalTeXpage,newTeXpage) THEN BEGIN
         MoveToDVIPage(totalpages);
         (* go backwards until newTeXpage matches currTeXpage *)
         WHILE TRUE DO BEGIN
            IF CurrMatchesNew(newTeXpage) THEN BEGIN
               finalDVIpage := currDVIpage;
               goto 777;
            END
            ELSE IF currDVIpage = 1 THEN BEGIN
               writeln('Final TeX page does not exist!');
               exit(1);
            END
            ELSE
               MoveToDVIPage(currDVIpage - 1);
         END;
         777:
      END
      ELSE BEGIN
         writeln('Error in final TeX page!');
         exit(1);
      END;
   IF firstDVIpage > finalDVIpage THEN BEGIN
      writeln('First page > final page!');
      exit(1);
   END
   ELSE IF firstDVIpage > totalpages THEN BEGIN
      writeln('First page > total number of pages!');
      exit(1);
   END;
   (* allow user to give a final page > totalpages *)
   IF finalDVIpage > totalpages THEN finalDVIpage := totalpages;
END;
END; (* CheckPageRange *)

(******************************************************************************)

PROCEDURE ShowTeXPage;

(* Show current TeX page counter(s). *)

LABEL 888;

VAR i, lastnonzero : INTEGER;

BEGIN
write('[');
lastnonzero := 9;
WHILE lastnonzero > 0 DO BEGIN
   IF currTeXpage[lastnonzero] <> 0 THEN goto 888;
   lastnonzero := lastnonzero - 1;   (* find last counter with non-zero value *)
END;
888:
(* always show \count0 but don't show trailing 0 counters *)
FOR i := 0 TO lastnonzero DO BEGIN
   write(currTeXpage[i]:1);
   IF i <> lastnonzero THEN write('.');
END;
write(']');
END; (* ShowTeXPage *)

(******************************************************************************)

PROCEDURE PageOffPaper;

(* One or more page edges do not fall within the paper edges.
   We show user just how bad the problem is.
*)

BEGIN
warncount := warncount + 1;
writeln;
write('Page off paper (paper is ');
ShowDimension(paperwd); write(' wide by ');
ShowDimension(paperht); write(' high)');
writeln;
IF minhp < paperleft THEN BEGIN
   write('Beyond left edge by ');
   ShowDimension(paperleft - minhp);
   writeln;
END;
IF maxhp > paperright THEN BEGIN
   write('Beyond right edge by ');
   ShowDimension(maxhp - paperright);
   writeln;
END;
IF minvp < papertop THEN BEGIN
   write('Above top edge by ');
   ShowDimension(papertop - minvp);
   writeln;
END;
IF maxvp > paperbottom THEN BEGIN
   write('Below bottom edge by ');
   ShowDimension(maxvp - paperbottom);
   writeln;
END;
END; (* PageOffPaper *)

(******************************************************************************)

PROCEDURE OpenFont (thisfontinfo : fontinfoptr);

BEGIN
WITH thisfontinfo^ DO
   IF OpenFontFile(fontspec) THEN
      { DEBUG
      BEGIN
      writeln;
      writeln('Loading characters for ', fontspec:fontspeclen);
      END
      GUBED }
   ELSE BEGIN
      (* this should never happen since we avoid loading dummy font chars *)
      writeln;
      writeln('Bug in OpenFont!  Could not open: ', fontspec:fontspeclen);
      exit(1);
   END;
END; (* OpenFont *)

(******************************************************************************)

PROCEDURE LoadFonts;

(* For each bitmapped font that is used (and exists) on the current page,
   go thru charlist and call LoadBitmap for each character that hasn't yet
   been downloaded.  BeginBitmapFont will only be called if necessary.
*)

VAR
   thisfontinfo : fontinfoptr;    (* current font info in fontlist *)
   thischarinfo : charinfoptr;    (* current char info in charlist *)
   thischar     : INTEGER;        (* current index into current chartable *)
   fontopen     : BOOLEAN;        (* is thisfontinfo^.fontspec open? *)

BEGIN
thisfontinfo := fontlist;
WHILE thisfontinfo <> unusedfont DO
(* SortFonts makes sure we only consider used fonts *)
WITH thisfontinfo^ DO BEGIN
   (* do nothing if resident PostScript font or bitmapped font doesn't exist *)
   IF (NOT psfont) AND fontexists THEN BEGIN
      fontopen := FALSE;              (* avoid opening font unnecessarily *)
      thischarinfo := charlist;
      WHILE thischarinfo <> NIL DO    (* process unloaded chars in chartable *)
         WITH thischarinfo^ DO BEGIN
            thischar := 0;
            WHILE thischar < charcount DO BEGIN
               WITH chartable[thischar] DO
               WITH pixelptr^[code] DO
                  IF (NOT loaded) AND (mapadr > 0) THEN BEGIN (* load bitmap *)
                     IF NOT fontopen THEN BEGIN
                        OpenFont(thisfontinfo);
                        BeginBitmapFont(fontid);
                        fontopen := TRUE;                 (* only open once *)
                     END;
                     LoadBitmap(thisfontinfo,code);
                     loaded := TRUE;                      (* only load once *)
                  END;
               thischar := thischar + 1;
            END;
            thischarinfo := nextchar;
         END;
      IF fontopen THEN CloseFontFile;
   END;
   thisfontinfo := nextfont;
END;
END; (* LoadFonts *)

(******************************************************************************)

PROCEDURE DoSpecials;

(* Call OutputSpecial for each \special command on the current page.
   (The speciallist is built during InterpretPage.)
*)

VAR temp : specialinfoptr;

BEGIN
temp := speciallist;
WHILE temp <> NIL DO
   WITH temp^ DO BEGIN
      (* The \special bytes are treated as a file name, possibly followed by
         a space and additional PostScript text.
         PSWriter will read this file and copy it verbatim to the output file.
         The optional text is prefixed to the file as a separate line.
      *)
      IF NOT OutputSpecial(special,hp,vp) THEN BEGIN
         warncount := warncount + 1;
         writeln;
         writeln('Couldn''t open \special file: ', special:Len(special));
      END
      ELSE IF stats THEN BEGIN
         writeln;
         write('\special command at (');
         ShowDimension(hp); write(',');
         ShowDimension(vp); write('): ');
         write(special:Len(special)); writeln;
      END;
      temp := nextspecial;
   END;
END; (* DoSpecials *)

(******************************************************************************)

PROCEDURE DoFonts;

(* For each font that is used (and exists) on the current page,
   call the appropriate sequence of PSWriter routines depending on
   the conserveVM flag and whether the font is bitmapped or resident.
*)

VAR
   thisfontinfo : fontinfoptr;    (* current font info in fontlist *)
   thischarinfo : charinfoptr;    (* current char info in charlist *)
   thischar     : INTEGER;        (* current index into current chartable *)

BEGIN
thisfontinfo := fontlist;
WHILE thisfontinfo <> unusedfont DO
(* SortFonts makes sure we only consider used fonts! *)
WITH thisfontinfo^ DO BEGIN
   IF fontexists THEN BEGIN                       (* won't be dummy font info *)
      IF psfont THEN
         BeginPostScriptFont(fontname,scaledsize,mag)
      ELSE BEGIN
         IF conserveVM THEN SaveVM(fontid);
         BeginBitmapFont(fontid);
      END;

      IF conserveVM AND (NOT psfont) THEN BEGIN   (* download bitmaps *)
         OpenFont(thisfontinfo);
         thischarinfo := charlist;
         WHILE thischarinfo <> NIL DO             (* process unique chars *)
            WITH thischarinfo^ DO BEGIN
               thischar := 0;
               WHILE thischar < charcount DO BEGIN
                  WITH chartable[thischar] DO
                  WITH pixelptr^[code] DO
                     IF (NOT loaded) AND (mapadr > 0) THEN BEGIN
                        LoadBitmap(thisfontinfo,code);
                        loaded := TRUE;                  (* but only once *)
                     END;
                  thischar := thischar + 1;
               END;
               thischarinfo := nextchar;
            END;
         CloseFontFile;
         (* reset loaded flags to FALSE for next page *)
         FOR thischar := 0 TO maxTeXchar DO
            pixelptr^[thischar].loaded := FALSE;
      END;

      IF psfont THEN BEGIN
         thischarinfo := charlist;
         WHILE thischarinfo <> NIL DO
            WITH thischarinfo^ DO BEGIN
               thischar := 0;
               WHILE thischar < charcount DO BEGIN
                  WITH chartable[thischar] DO
                  WITH pixelptr^[code] DO
                     IF mapadr > 0 THEN               (* char exists *)
                        SetPostScriptChar(CHR(code),
                                          hp,vp,      (* reference point *)
                                          pwidth);    (* advance width *)
                  thischar := thischar + 1;
               END;
               thischarinfo := nextchar;
            END;
      END
      ELSE BEGIN
         thischarinfo := charlist;
         WHILE thischarinfo <> NIL DO
            WITH thischarinfo^ DO BEGIN
               thischar := 0;
               WHILE thischar < charcount DO BEGIN
                  WITH chartable[thischar] DO
                  WITH pixelptr^[code] DO
                     IF mapadr > 0 THEN               (* char exists *)
                        SetBitmapChar(CHR(code),
                                      hp,vp,          (* reference point *)
                                      pwidth);        (* advance width *)
                  thischar := thischar + 1;
               END;
               thischarinfo := nextchar;
            END;
      END;
      EndFont;

      IF conserveVM AND (NOT psfont) THEN RestoreVM;
   END;
   thisfontinfo := nextfont;
END;
END; (* DoFonts *)

(******************************************************************************)

PROCEDURE DoRules;

(* Call SetRule for each rule on the current page. *)

VAR thisrule : INTEGER;   thisruleinfo : ruleinfoptr;

BEGIN
thisruleinfo := rulelist;
WHILE thisruleinfo <> NIL DO
   WITH thisruleinfo^ DO BEGIN
      thisrule := 0;
      WHILE thisrule < rulecount DO BEGIN
         WITH ruletable[thisrule] DO SetRule(wd,ht,hp,vp);
         thisrule := thisrule + 1;
      END;
      thisruleinfo := nextrule;
   END;
END; (* DoRules *)

(******************************************************************************)

PROCEDURE DoPage;

(* Interpret the current DVI page and fill in DVIReader's data structures.
   PSWriter routines are called at appropriate times to output
   the PostScript description of the current page.
*)

BEGIN
pagecount := pagecount + 1;
write(currDVIpage:1);           (* show the current DVI page *)
write('/'); ShowTeXPage;        (* and TeX page *)
flush(output);                  (* SYSDEP: show page immediately *)
IF pagecount MOD pagesperline = 0 THEN writeln ELSE write(' ');

BeginPage(currDVIpage);
InterpretPage;                  (* PixelTableRoutine calls NewBitmapFont *)
IF pageempty THEN BEGIN
   OutputPage(currDVIpage);     (* must be called even if no chars/rules *)
   DoSpecials;
END
ELSE BEGIN
   (* check that the page edges are within the paper edges *)
   IF (minhp < paperleft)  OR (minvp < papertop) OR
      (maxhp > paperright) OR (maxvp > paperbottom) THEN
      PageOffPaper;
   (* Sort fonts in order of increasing totalchars and
      return pointer to first unused font (for LoadFonts and DoFonts).
   *)
   SortFonts(unusedfont);
   IF NOT conserveVM THEN LoadFonts;
   OutputPage(currDVIpage);
   DoSpecials;
   DoFonts;
   DoRules;
END;
EndPage(currDVIpage);           (* final PostScript for current page *)
END; (* DoPage *)

(******************************************************************************)

PROCEDURE ShowPtSize (scaledsize : INTEGER);

(* Show given font size (in DVI units) in terms of (possibly magnified) pts. *)

VAR realdim : REAL;   fracpart : INTEGER;

BEGIN
write(' at ');
realdim := (scaledsize / 16#10000) * (mag / 1000.0);
(* show realdim to an accuracy of 1 decimal place *)
IF ABS(realdim) < 0.05 THEN
   write('0')
ELSE BEGIN
   IF realdim < 0.0 THEN BEGIN
      write('-');
      realdim := ABS(realdim);
   END;
   realdim := realdim + 0.05;     (* round up to 1 decimal place *)
   write(TRUNC(realdim):1);       (* whole part *)
   fracpart := TRUNC((realdim - TRUNC(realdim)) * 10.0);   (* 0..9 *)
   IF fracpart > 0 THEN
      write('.', fracpart:1);
END;
write('pt');
END; (* ShowPtSize *)

(******************************************************************************)

PROCEDURE ShowPageStats;

(* Show rule/font/character statistics for current page. *)

VAR fontcount : INTEGER;   thisfontinfo : fontinfoptr;

BEGIN
writeln;
writeln('Total rules on current page = ', totalrules:1);
writeln('Fonts on current page:');
fontcount := 0;
thisfontinfo := fontlist;
WHILE thisfontinfo <> NIL DO
   WITH thisfontinfo^ DO BEGIN
      IF fontused THEN BEGIN
         write(fontspec:fontspeclen);
         IF psfont THEN ShowPtSize(scaledsize);
         IF NOT fontexists THEN write('   DOES NOT EXIST!');
         fontcount := fontcount + 1;
         writeln('   total chars = ', totalchars:1);
      END;
      thisfontinfo := nextfont;
   END;
writeln('Total fonts on current page = ', fontcount:1);
writeln;
END; (* ShowPageStats *)

(******************************************************************************)

PROCEDURE ShowFinalStats;

(* Show some overall statistics. *)

VAR fontsused, c, loadcount, loadtotal, bitmapbytes : INTEGER;
    thisfontinfo : fontinfoptr;

BEGIN
writeln('Summary');
writeln('=======');
writeln;
writeln('Total pages output = ', pagecount:1);
writeln('Total pages in DVI file = ', totalpages:1);
writeln('Total fonts in DVI file = ', totalfonts:1);
(* go thru fontlist showing info for EVERY font *)
fontsused := 0;
loadtotal := 0;
bitmapbytes := 0;
thisfontinfo := fontlist;
WHILE thisfontinfo <> NIL DO
   WITH thisfontinfo^ DO BEGIN
      IF fontspeclen > 0 THEN BEGIN
         write(fontspec:fontspeclen);
         IF psfont THEN ShowPtSize(scaledsize);
         IF fontexists THEN BEGIN
            fontsused := fontsused + 1;
            IF (NOT conserveVM) AND (NOT psfont) THEN BEGIN
               loadcount := 0;
               FOR c := 0 TO maxTeXchar DO
                  WITH pixelptr^[c] DO
                     IF loaded AND (mapadr > 0) THEN BEGIN
                        loadcount := loadcount + 1;
                        bitmapbytes := bitmapbytes + (ht * ((wd + 7) DIV 8));
                     END;
               write('   loaded chars = ', loadcount:1);
               loadtotal := loadtotal + loadcount;
            END;
         END
         ELSE
            write('   DOES NOT EXIST!');
      END
      ELSE BEGIN
         write(fontname:fontnamelen, ' scaled ');
         write(TRUNC(mag * (scaledsize / designsize) + 0.5):1);
         write(' not used');
      END;
      writeln;
      thisfontinfo := nextfont;
   END;
writeln('Total fonts actually used = ', fontsused:1);
IF NOT conserveVM THEN BEGIN
   writeln('Total characters loaded = ', loadtotal:1);
   writeln('Hex digits in loaded bitmaps = 2 * ', bitmapbytes:1);
END;
END; (* ShowFinalStats *)

(******************************************************************************)

PROCEDURE TopLevel;

LABEL 888;

BEGIN
InitOptions;                                (* init DVIname and options *)
InitDVIReader;
InitFontReader;
OpenDVIFile(DVIname);                       (* and read DVImag etc. *)
IF mag = 0 THEN mag := DVImag;              (* use DVImag *)
SetConversionFactor(resolution,mag);        (* for DVIReader *)
Initialize;
IF stats THEN ShowOptions;
CheckPageRange;                             (* set firstDVIpage, finalDVIpage *)
IF NOT OpenOutput(PSname) THEN BEGIN
   writeln('Couldn''t open output file: ', PSname:Len(PSname));
   exit(1);
END;
IF header[0] <> ' ' THEN                    (* output header file first *)
   IF NOT OutputHeader(header) THEN BEGIN
      writeln('Couldn''t open header file: ', header:Len(header));
      exit(1);
   END;
IF increment > 1 THEN                       (* finalDVIpage may need reducing *)
   WHILE (finalDVIpage - firstDVIpage) MOD increment > 0 DO
      finalDVIpage := finalDVIpage - 1;
IF reverse THEN BEGIN
   MoveToDVIPage(finalDVIpage);             (* start with finalDVIpage *)
   finalDVIpage := firstDVIpage;            (* and end with firstDVIpage *)
END
ELSE
   MoveToDVIPage(firstDVIpage);             (* start with firstDVIpage *)
WHILE TRUE DO BEGIN
   DoPage;                                  (* do at least one page *)
   IF stats THEN ShowPageStats;
   IF currDVIpage = finalDVIpage THEN goto 888;
   IF reverse THEN
      MoveToDVIPage(currDVIpage - increment)
   ELSE
      MoveToDVIPage(currDVIpage + increment);
END;
888:
IF pagecount MOD pagesperline > 0 THEN writeln;
IF stats THEN ShowFinalStats;
CloseDVIFile;
CloseOutput;
IF warncount > 0 THEN
   exit(2)
ELSE
   exit(0);
END; (* TopLevel *)

(******************************************************************************)

BEGIN
TopLevel;
END. (* PSDVI *)
