summaryrefslogtreecommitdiff
path: root/dviware/psprint/unix/psdvi.p
blob: ad3e365cc7ec5a1000850b1a342b8871c48f65e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
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 *)