summaryrefslogtreecommitdiff
path: root/dviware/psprint/vms/fontreade.mod
blob: 223b76cef4d640196b24f8399644aaa99e1ac5a4 (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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
IMPLEMENTATION MODULE FontReader;

(* Author:         Andrew Trevorrow
   Implementation: University of Hamburg Modula-2 under VAX/VMS version 4
   Date Started:   May, 1985

   Description:
   This module can extract font information from PXL, PK or TFM files.
   (GF files are not handled; it is assumed that sites with GF files will
   decide to convert them to PK files sooner or later!)
   Each type of font file is considered to be an array of 8-bit bytes.
   Only one font file is open at any time.  We move to byte positions using
   byteoffset and read bytes and words using GetByte, SignedQuad, etc.

   Revised:
   June, 1985
 - Use Halt(2) instead of HALT.

   August, 1985
 - Use ScreenIO routines when debugging.

   September, 1985
 - Added GetPXLGlyph and associated glyph cacheing code.

   October, 1985
 - Removed above cacheing stuff!  Now use SYS$CRMPSC to map an
   entire PXL file into virtual memory where it is treated as an array of bytes.

   March, 1986
 - Amendments to Modula-2 language required a few minor syntactic changes.

   November, 1986
 - PXLReader has been expanded and is now called FontReader.
   All font-dependent code has been moved out of PSDVI's main module.
   A new font format can be handled just by adding code to FontReader.
 - Reduced page fault cluster size from 36 to 16 because PK files are smaller.
 - Always start fontspec with fontdir; fontarea is really only for TFM files.

   December, 1986
 - GetBitmap now called LoadBitmap and uses Put routines from PSWriter to
   send bitmap and metric info directly to output file.

   November, 1987 (while at The Open University)
 - Added charsperline code to LoadBitmap routines so that output lines
   don't get too long.
 - Added code to handle PostScript fonts (for which only TFM files exist).
   BuildFontSpec now checks for a fontname beginning with "PS-" and sets the
   font's psfont flag.  If TRUE then we must construct a TFM filespec
   (starting with tfmdir or fontarea if fontarealen > 0) and later fill
   the font's pixeltable with character info from that TFM file.
 - Renamed ConvertTFMWidth to FixToDVI.
 - PXL/PKFillPixelTable now call TFMFillPixelTable if the font is
   a PostScript font (and its TFM file could be opened).

   June--August, 1988 (while at Aston University)
 - Now use /psprefix value to check for a PostScript font.
 - Changed BuildFontSpec so that the fontsize substring no longer has to
   be the same length in all font files.

   September--October, 1989 (while at Aston University, 2nd time)
 - Fixed bug reported by Niel Kempson in FixToDVI.
*)

FROM SYSTEM IMPORT
   ADDRESS, ADR, BYTE, WORD;

FROM VMS IMPORT
   SYS$OPEN, SYS$CRMPSC, SYS$DASSGN, SYS$DELTVA;

FROM SECDefinitions IMPORT
   SEC$V_EXPREG;

FROM RMS IMPORT
   FAB, InitFab,
   FOPset, FOPtype,
   FACset, FACtype,
   SHRset, SHRtype;

FROM FileSystem IMPORT
   File, Open, Done, Close;

FROM Options IMPORT
   psprefix, tfmdir, fontdir, dummyfont, mag, resolution, Cap;

FROM DVIReader IMPORT
   (* CONST *)
   maxfontspec, maxTeXchar,
   (* TYPE *)
   fontstring, fontinfo, fontinfoptr,
   pixeltable, pixeltableptr,
   (* VAR *)
   currfont,
   (* PROCEDURE *)
   PixelRound;

FROM PSWriter IMPORT
   Put, PutString, PutInt, PutCard;

FROM TermOut IMPORT
   WriteString, WriteCard, WriteInt, WriteLn, Halt;

CONST
   NULL = 0C;    (* SYSDEP: used to terminate a string if not full *)
   CR   = 15C;   (* used in LoadBitmap *)

TYPE
   (* font files should never have more than MAX(INTEGER) bytes! *)
   fontfile    = ARRAY [0..MAX(INTEGER)] OF BYTE;
   filepointer = POINTER TO fontfile;                     (* never allocated! *)

VAR
   vas : ARRAY [0..1] OF ADDRESS;      (* start and end virtual addresses     *)
   filestart : filepointer;            (* pointer to starting address of file *)
   byteoffset,                         (* byte offset from start of file      *)
   channel : CARDINAL;                 (* fab.STV returned by SYS$OPEN        *)
   gsdnam : ARRAY [0..42] OF CHAR;     (* unused argument in SYS$CRPMSC       *)
   status : CARDINAL;
   fab : FAB;
   psprefixlen,                        (* length of psprefix string           *)
   fontdirlen,                         (* length of fontdir string            *)
   dummyfontlen : CARDINAL;            (* length of dummyfont string          *)
   formatstr : ARRAY [0..2] OF CHAR;   (* PXL or PK                           *)
   formatlen : CARDINAL;               (* 3   or 2                            *)
   xfactor : REAL;                     (* resolution/200 or resolution/1000   *)
   hexdigs : ARRAY [0..15] OF CHAR;    (* 0..9ABCDEF for LoadBitmap           *)
   gpower : ARRAY [0..8] OF BITSET;    (* 0,1,11,111,1111,...,11111111        *)
   CompleteFontSpec :                  (* used by BuildFontSpec               *)
      PROCEDURE (fontinfoptr,
                 CARDINAL,
                 CARDINAL,
                 VAR CARDINAL) : BOOLEAN;

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

PROCEDURE InitFontReader;

(* Assign font-dependent routines to various procedure variables
   according to information present in fontdir and dummyfont:

   - The last character in dummyfont defines the font format (PXL or PK).
     All font files are assumed to be in the same format.
     Note that the Options module has prefixed dummyfont with fontdir.

   - The last two characters in fontdir define the structure of each file spec.
     If ".]" then all font files are assumed to reside in subdirectories of
     fontdir, and the name of each subdirectory is the font size.
     A typical dummyfont value would be TEX_DISK:[TEX.PXL.][1500]CMR10.PXL.
     This is the storage scheme used in K&S's old VMS TeX distributions.
     If not ".]" then all font files are assumed to be in kept in fontdir,
     and the font size is included in the file type.
     A typical dummyfont value would be TEX_DISK:[TEX.PK]CMR10.300PK.
     This type of scheme is used in the latest VMS TeX distributions.

   - The dummyfont must contain a font size substring representing
     an unmagnified font.  Its value, along with /RESOLUTION, is used to
     decide how to calculate the font sizes in all other font files.
*)

VAR
   i,
   fontsizelen,                (* length of n...n in dummyfont *)
   dummyfontsize : CARDINAL;   (* value of n...n in dummyfont *)

BEGIN
hexdigs := "0123456789ABCDEF";                    (* for LoadBitmap *)
dummyfontlen := LEN(dummyfont);
IF dummyfontlen = 0 THEN dummyfontlen := 1 END;   (* last char will be NULL *)
CASE Cap(dummyfont[dummyfontlen-1]) OF            (* last char defines format *)
   'L' : formatstr      := 'pxl';
         formatlen      := 3;
         FillPixelTable := PXLFillPixelTable;
         LoadBitmap     := PXLLoadBitmap;
 | 'K' : formatstr      := 'pk';
         formatlen      := 2;
         FillPixelTable := PKFillPixelTable;
         LoadBitmap     := PKLoadBitmap;
         gpower[0] := {};
         FOR i := 1 TO 8 DO
            gpower[i] := gpower[i-1] + {i-1};     (* used in PKLoadBitmap *)
         END;
ELSE
   WriteString('/DUMMY_FONT value should end with PXL or PK.'); WriteLn;
   Halt(2);
END;
psprefixlen := LEN(psprefix);
fontdirlen := LEN(fontdir);
IF (fontdirlen > 1) AND (fontdir[fontdirlen-2] = '.') AND
                        (fontdir[fontdirlen-1] = ']') THEN
   (* assume font files are kept in subdirectories of fontdir
      and have fontspecs like TEX_DISK:[TEX.subdir.][n...n]CMR10.fmt
   *)
   CompleteFontSpec := OldFontSpec;
   i := fontdirlen + 1;               (* first n after .][ *)
ELSE
   (* assume font files are all kept in fontdir
      and have fontspecs like TEX_DISK:[TEX.subdir]CMR10.n...nfmt
   *)
   CompleteFontSpec := NewFontSpec;
   i := fontdirlen;                   (* first char after ] *)
   WHILE (i < dummyfontlen) AND (dummyfont[i] <> '.') DO
      INC(i);
   END;
   INC(i);                            (* first n after '.' *)
END;
fontsizelen := 0;
dummyfontsize := 0;
WHILE (i < dummyfontlen) AND (dummyfont[i] >= '0') AND (dummyfont[i] <= '9') DO
   INC(fontsizelen);
   dummyfontsize := dummyfontsize * 10 + (ORD(dummyfont[i]) - ORD('0'));
   INC(i);
END;
IF fontsizelen > 0 THEN
   (* The xfactor used to calculate fontsize in BuildFontSpec depends on the
      fontsize in dummyfont (assumed to be an unmagnified font).
   *)
   IF FLOAT(dummyfontsize) / FLOAT(resolution) > 1.0 THEN
      xfactor := FLOAT(resolution) / 200.0;    (* old naming convention *)
   ELSE
      xfactor := FLOAT(resolution) / 1000.0;   (* new naming convention *)
   END;
ELSE
   WriteString('/DUMMY_FONT value does not contain font size!'); WriteLn;
   Halt(2);
END;
END InitFontReader;

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

PROCEDURE BuildFontSpec (fontptr : fontinfoptr; VAR firstn, lastn : CARDINAL);

VAR
   f : File;
   i, j, next, fontsize, tempsize, tempsizelen : CARDINAL;

BEGIN
WITH fontptr^ DO
   (* first check for a PostScript font; following code will set psfont to TRUE
      if psprefixlen = 0 --- ALL fonts will be considered PostScript fonts
   *)
   psfont := TRUE;
   i := 0;
   LOOP
      IF i = psprefixlen THEN EXIT END;
      IF Cap(fontname[i]) <> Cap(psprefix[i]) THEN
         psfont := FALSE;
         EXIT;
      END;
      INC(i);
   END;
   IF psfont THEN
      BuildTFMSpec(fontptr);           (* build TFM file spec *)
      RETURN;
   END;
   i := 0;
   next := fontdirlen;
   REPEAT
      fontspec[i] := fontdir[i];       (* start fontspec with fontdir *)
      INC(i);
   UNTIL (i = next) OR (i > maxfontspec);
   IF next >= maxfontspec THEN
      fontspeclen := maxfontspec;
      RETURN;                          (* fontspec truncated *)
   END;
   fontsize := TRUNC( FLOAT(mag) * (FLOAT(scaledsize) / FLOAT(designsize))
                                 * xfactor + 0.5 );
   IF fontsize = 0 THEN
      INC(fontsize);                   (* allow for subtracting 1 *)
   END;
   tempsize := fontsize;
   i := 1;
   LOOP
      (* Complete rest of fontspec starting at next
         and return the position of first digit for fontsize.
         We have to try fontsize +/- 1 before giving up because
         rounding problems can occur in the above fontsize calculation.
      *)
      j := tempsize;
      tempsizelen := 0;
      WHILE j > 0 DO
         INC(tempsizelen);
         j := j DIV 10;
      END;
      IF NOT CompleteFontSpec(fontptr, next, tempsizelen, firstn) THEN
         RETURN;                       (* fontspec truncated *)
      END;
      lastn := firstn + tempsizelen - 1;
      (* put tempsize into fontspec[firstn..lastn] *)
      FOR j := lastn TO firstn BY -1 DO
         fontspec[j] := CHR(ORD('0') + (tempsize MOD 10));
         tempsize := tempsize DIV 10;
      END;
      IF i > 3 THEN                    (* original fontsize has been restored *)
         RETURN;                       (* could not open fontspec *)
      END;
      Open(f,fontspec,FALSE);          (* SYSDEP: try to open for reading *)
      IF Done() THEN
         Close(f);
         fontexists := TRUE;           (* fontspec exists *)
         RETURN;
      ELSIF i = 1 THEN
         tempsize := fontsize - 1;     (* try fontsize - 1 *)
      ELSIF i = 2 THEN
         tempsize := fontsize + 1;     (* try fontsize + 1 *)
      ELSE
         tempsize := fontsize;         (* restore original fontsize *)
      END;
      INC(i);
   END;
END;
END BuildFontSpec;

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

PROCEDURE BuildTFMSpec (fontptr : fontinfoptr);

(* Build a complete TFM file specification in fontptr^.fontspec.
   This will only be done once per font; fontspeclen will no longer be 0.
   fontptr^.fontexists becomes TRUE if the file can be opened.
*)

VAR f : File;   i, next : CARDINAL;

BEGIN
WITH fontptr^ DO
   i := 0;
   IF fontarealen > 0 THEN
      next := fontarealen;
      REPEAT
         fontspec[i] := fontarea[i];   (* start fontspec with fontarea *)
         INC(i);
      UNTIL (i = next) OR (i > maxfontspec);
   ELSE
      next := LEN(tfmdir);             (* assume > 0 *)
      REPEAT
         fontspec[i] := tfmdir[i];     (* start fontspec with tfmdir *)
         INC(i);
      UNTIL (i = next) OR (i > maxfontspec);
   END;
   IF next >= maxfontspec THEN
      fontspeclen := maxfontspec;
      RETURN;                          (* fontspec truncated *)
   END;
   (* next is current length of fontspec; append fontname.tfm *)
   i := 0;
   WHILE (i < fontnamelen) AND (next < maxfontspec) DO
      fontspec[next] := fontname[i];   (* append fontname *)
      INC(i);
      INC(next);
   END;
   IF next + 4 <= maxfontspec THEN     (* append .tfm *)
      fontspec[next] := '.'; INC(next);
      fontspec[next] := 't'; INC(next);
      fontspec[next] := 'f'; INC(next);
      fontspec[next] := 'm'; INC(next);
   ELSE
      fontspeclen := maxfontspec;
      RETURN;                          (* fontspec truncated *)
   END;
   fontspeclen := next;
   (* SYSDEP: terminate fontspec with NULL *)
   IF fontspeclen < maxfontspec THEN fontspec[fontspeclen] := NULL END;
   Open(f,fontspec,FALSE);             (* SYSDEP: try to open for reading *)
   IF Done() THEN
      Close(f);
      fontexists := TRUE;              (* fontspec exists *)
   END;
END;
END BuildTFMSpec;

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

PROCEDURE OldFontSpec (fontptr : fontinfoptr;
                       next : CARDINAL;
                       fontsizelen : CARDINAL;
                       VAR firstn : CARDINAL) : BOOLEAN;

(* Return TRUE if we can append "[n...n]fontname.fmt" to fontspec.
   Such a scheme is used in old TeX distributions from Kellerman and Smith.
*)

VAR i : CARDINAL;

BEGIN
WITH fontptr^ DO
   firstn := next + 1;                            (* position of 1st n *)
   IF next + fontsizelen + 1 < maxfontspec THEN   (* append [n...n] *)
      fontspec[next] := '[';
      INC(next,fontsizelen + 1);                  (* skip n...n *)
      fontspec[next] := ']';
      INC(next);
   ELSE
      fontspeclen := maxfontspec;
      RETURN FALSE;                               (* fontspec truncated *)
   END;
   i := 0;
   WHILE (i < fontnamelen) AND (next < maxfontspec) DO
      fontspec[next] := fontname[i];              (* append fontname *)
      INC(i);
      INC(next);
   END;
   IF next + formatlen < maxfontspec THEN         (* append .fmt *)
      fontspec[next] := '.'; INC(next);
      i := 0;
      REPEAT
         fontspec[next] := formatstr[i];
         INC(i);
         INC(next);
      UNTIL i = formatlen;
   ELSE
      fontspeclen := maxfontspec;
      RETURN FALSE;                               (* fontspec truncated *)
   END;
   fontspeclen := next;
   (* SYSDEP: terminate fontspec with NULL *)
   IF fontspeclen < maxfontspec THEN fontspec[fontspeclen] := NULL END;
   RETURN TRUE;
END;
END OldFontSpec;

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

PROCEDURE NewFontSpec (fontptr : fontinfoptr;
                       next : CARDINAL;
                       fontsizelen : CARDINAL;
                       VAR firstn : CARDINAL) : BOOLEAN;

(* Return TRUE if we can append "fontname.n...nfmt" to fontspec.
   Such a scheme is used in the latest TeX distributions.
*)

VAR i : CARDINAL;

BEGIN
WITH fontptr^ DO
   i := 0;
   WHILE (i < fontnamelen) AND (next < maxfontspec) DO
      fontspec[next] := fontname[i];                    (* append fontname *)
      INC(i);
      INC(next);
   END;
   firstn := next + 1;                                  (* position of 1st n *)
   IF next + fontsizelen + formatlen < maxfontspec THEN (* append .n...nfmt *)
      fontspec[next] := '.';
      INC(next,fontsizelen + 1);                        (* skip n...n *)
      i := 0;
      REPEAT
         fontspec[next] := formatstr[i];
         INC(i);
         INC(next);
      UNTIL i = formatlen;
   ELSE
      fontspeclen := maxfontspec;
      RETURN FALSE;                                     (* fontspec truncated *)
   END;
   fontspeclen := next;
   (* SYSDEP: terminate fontspec with NULL *)
   IF fontspeclen < maxfontspec THEN fontspec[fontspeclen] := NULL END;
   RETURN TRUE;
END;
END NewFontSpec;

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

PROCEDURE OpenFontFile (VAR fspec : ARRAY OF CHAR) : BOOLEAN;

(* Return TRUE iff given file can be opened and mapped into virtual memory. *)

BEGIN
InitFab(fab);                        (* initialize fab *)
WITH fab DO
   FNA := ADR(fspec);                (* file specification *)
   FNS := BYTE(LEN(fspec));          (* bytes in file name *)
   FAC := FACset{FAC$BRO,FAC$GET};   (* read-only *)
   SHR := SHRset{SHR$GET};           (* share file with other readers *)
   FOP := FOPset{FOP$UFO};           (* need for SYS$CRMPSC *)
   RTV := BYTE(-1);                  (* for more efficient mapping *)
END;
status := SYS$OPEN(ADR(fab),0,0);    (* open the file *)
IF ODD(status) THEN
   channel := fab.STV;               (* channel on which file is open *)
   vas[0] := 0;
   vas[1] := 0;
   status := SYS$CRMPSC              (* map file into virtual address space *)
                 (ADR(vas),          (* starting and ending addresses *)
                  ADR(vas),          (* addresses returned *)
                  0,
                  {SEC$V_EXPREG},    (* pages mapped into 1st available space *)
                  gsdnam,0,0,
                  channel,           (* channel on which file has been opened *)
                  0,0,0,
                  16                 (* page fault cluster size *)
                 );
   IF NOT ODD(status) THEN
      (* DEBUG
         WriteString('SYS$CRMPSC failed in OpenFontFile! status=');
         WriteCard(status); WriteLn; Halt(2);
      GUBED *)
      RETURN FALSE;
   ELSE
      (* The entire file is mapped into virtual memory so we can
         access any byte as an offset from the address in vas[0].
      *)
      filestart := vas[0];
      RETURN TRUE;
   END;
ELSE
   RETURN FALSE;
END;
END OpenFontFile;

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

(* Here are the functions used to get byte/s from fontfile. *)

PROCEDURE GetByte () : CARDINAL;

(* Return the value (unsigned) of the byte at byteoffset in fontfile and
   advance byteoffset for the next GetByte.
*)

VAR b : CARDINAL;

BEGIN
b := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
RETURN b;
END GetByte;

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

PROCEDURE SignedByte () : INTEGER;

(* Return the next byte, possibly signed. *)

VAR b : CARDINAL;

BEGIN
b := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
IF b < 128 THEN
   RETURN b;
ELSE
   RETURN b - 256;
END;
END SignedByte;

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

PROCEDURE GetTwoBytes () : CARDINAL;

(* Return the next 2 bytes, unsigned. *)

VAR a, b : CARDINAL;

BEGIN
a := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
b := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
RETURN a * 256 + b;
END GetTwoBytes;

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

PROCEDURE SignedPair () : INTEGER;

(* Return the next 2 bytes, possibly signed. *)

VAR a, b : CARDINAL;

BEGIN
a := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
b := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
IF a < 128 THEN
   RETURN a * 256 + b;
ELSE
   RETURN (a - 256) * 256 + b;
END;
END SignedPair;

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

PROCEDURE GetThreeBytes () : CARDINAL;

(* Return the next 3 bytes, unsigned. *)

VAR a, b, c : CARDINAL;

BEGIN
a := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
b := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
c := CARDINAL(filestart^[byteoffset]);   INC(byteoffset);
RETURN (a * 256 + b) * 256 + c;
END GetThreeBytes;

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

PROCEDURE SignedQuad () : INTEGER;

(* Return the value (possibly signed) of the 4 bytes at byteoffset and
   advance byteoffset by 4.
*)

VAR overlay : RECORD
                 CASE :BOOLEAN OF
                    TRUE  : i : INTEGER |
                    FALSE : a,b,c,d : BYTE
                 END;
              END;

BEGIN
WITH overlay DO
   (* SYSDEP: on a VAX, d is at least significant end of word *)
   d := filestart^[byteoffset];   INC(byteoffset);
   c := filestart^[byteoffset];   INC(byteoffset);
   b := filestart^[byteoffset];   INC(byteoffset);
   a := filestart^[byteoffset];   INC(byteoffset);
   RETURN i;
END;
END SignedQuad;

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

PROCEDURE CloseFontFile;

(* Close the currently open font file. *)

BEGIN
status := SYS$DELTVA(ADR(vas),ADR(vas),0);
IF NOT ODD(status) THEN
   (* DEBUG
      WriteString('SYS$DELTVA failed in CloseFontFile! status=');
      WriteCard(status); WriteLn; Halt(2);
   GUBED *)
END;
status := SYS$DASSGN(channel);
IF NOT ODD(status) THEN
   (* DEBUG
      WriteString('SYS$DASSGN failed in CloseFontFile! status=');
      WriteCard(status); WriteLn; Halt(2);
   GUBED *)
END;
END CloseFontFile;

(******************************************************************************)
(* Here are the routines for reading PXL files:                               *)
(******************************************************************************)

PROCEDURE PXLFillPixelTable;

(* Fill the pixeltable for currfont^ using the font directory info
   in the currently open PXL file.
*)

VAR i, b0, b1, b2, b3 : CARDINAL;    (* 4 bytes in TFM width *)

BEGIN
WITH currfont^ DO
   IF psfont AND fontexists THEN
      TFMFillPixelTable;                 (* use TFM file instead *)
      RETURN;
   END;
   (* to find font directory we first move to last byte in PXL file *)
   byteoffset := CARDINAL(ADDRESS(vas[1]) - ADDRESS(filestart));
   (* skip back over any 0 bytes *)
   WHILE (byteoffset > 0) AND (CARDINAL(filestart^[byteoffset]) = 0) DO
      DEC(byteoffset);
   END;
   (* move to byte at start of last non-zero word *)
   WHILE byteoffset MOD 4 <> 0 DO
      DEC(byteoffset);
   END;
   IF SignedQuad() <> 1001 THEN
      WriteLn;
      WriteString('Bad PXL file! id word <> 1001.');
      WriteLn; Halt(2);
   END;
   DEC(byteoffset,4);                    (* SignedQuad will have added 4 *)
   DEC(byteoffset,516 * 4);              (* starting byte of font directory *)
   FOR i := 128 TO maxTeXchar DO
      pixelptr^[i].mapadr := 0;          (* PXL files only have 128 chars *)
      pixelptr^[i].loaded := FALSE;
   END;
   FOR i := 0 TO 127 DO
      WITH pixelptr^[i] DO
         wd     := GetTwoBytes();
         ht     := GetTwoBytes();
         xo     := SignedPair();
         yo     := SignedPair();
         loaded := FALSE;                (* bitmap not yet downloaded *)
         mapadr := SignedQuad();         (* word offset in PXL file *)
         IF (wd = 0) OR (ht = 0) THEN
            mapadr := 0;                 (* in case PXL file is incorrect *)
         END;
         b0     := GetByte();            (* should be 0 or 255 *)
         b1     := GetByte();
         b2     := GetByte();
         b3     := GetByte();
         dwidth := FixToDVI(b0,b1,b2,b3);
         pwidth := PixelRound(dwidth);   (* convert DVI units to pixels *)
      END;
   END;
END;
END PXLFillPixelTable;

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

PROCEDURE FixToDVI (b0, b1, b2, b3 : CARDINAL) : INTEGER;

(* Convert the given fixword (made up of 4 bytes) into DVI units
   using the method recommended in DVITYPE.
*)

VAR alpha, beta, s : CARDINAL;   temp : INTEGER;

BEGIN
s := currfont^.scaledsize;   (* so we don't change scaledsize! *)
alpha := 16 * s;
beta  := 16;
WHILE s >= 40000000B DO      (* 2^23sp = 128pt *)
   s := s DIV 2;
   beta := beta DIV 2;
END;
temp := (((((b3 * s) DIV 400B) + (b2 * s)) DIV 400B) + (b1 * s)) DIV beta;
IF b0 > 0 THEN
   IF b0 = 255 THEN
      RETURN temp - INTEGER(alpha);
   ELSE
      WriteLn;
      WriteString('Bad fixword! 1st byte='); WriteCard(b0);
      WriteLn; Halt(2);
   END;
ELSE
   RETURN temp;
END;
END FixToDVI;

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

PROCEDURE PXLLoadBitmap (fontptr : fontinfoptr; code : CARDINAL);

(* Download bitmap using raster information starting at mapadr
   in currently open PXL file.
*)

VAR b, r, thisbyte, bytesperrow, usedperrow, charsperline : CARDINAL;

BEGIN
WITH fontptr^.pixelptr^[code] DO
bytesperrow := ((wd + 31) DIV 32) * 4;     (* words per row * 4 *)
usedperrow := (wd + 7) DIV 8;              (* not all bytes might be needed *)
charsperline := 0;
PutString('[<');
(* output (ht * usedperrow * 2) hex digits, starting at top row of bitmap *)
byteoffset := mapadr * 4;                  (* mapadr = word offset *)
b := 0;                                    (* byte count for one row *)
r := 0;                                    (* row count *)
LOOP
   INC(charsperline,2);
   IF charsperline >= 72 THEN
      Put(CR);
      charsperline := 0;
   END;
   thisbyte := GetByte();                  (* and increment byteoffset *)
   Put(hexdigs[ thisbyte DIV 16 ]);
   Put(hexdigs[ thisbyte MOD 16 ]);
   INC(b);
   IF b = usedperrow THEN
      INC(r);
      IF r = CARDINAL(ht) THEN EXIT END;
      INC(byteoffset,bytesperrow - b);     (* move to start of next row *)
      b := 0;                              (* reset byte count *)
   END;
END;
Put('>'); Put(CR);
PutCard(usedperrow * 8); Put(' ');
PutInt(ht); Put(' ');
PutInt(xo); Put(' ');
PutInt(yo); Put(' ');                      (* offset of origin from top row *)
PutInt(pwidth);
PutString('] ');
PutCard(code);
PutString(' dc'); Put(CR);
END; (* WITH *)
END PXLLoadBitmap;

(******************************************************************************)
(* Here are the routines for reading PK files:                                *)
(******************************************************************************)

PROCEDURE PKFillPixelTable;

(* Fill the pixeltable for currfont^ using the font directory info
   in the currently open PK file.
*)

CONST
   pkid   =  89;
   pkpost = 245;
   pknoop = 246;
   pkpre  = 247;

VAR
   i, j, flagbyte, flagpos,
   chcode,                      (* assumed to be <= 255 *)
   packetlen, endofpacket,
   b0, b1, b2, b3 : CARDINAL;   (* 4 bytes in TFM width *)

BEGIN
WITH currfont^ DO
   IF psfont AND fontexists THEN
      TFMFillPixelTable;                   (* use TFM file instead *)
      RETURN;
   END;
   byteoffset := 0;                        (* move to first byte *)
   IF GetByte() <> pkpre THEN
      WriteLn;
      WriteString('Bad pre command in ');
      WriteString(fontspec); WriteLn; Halt(2);
   END;
   IF GetByte() <> pkid THEN
      WriteLn;
      WriteString('Bad id byte in ');
      WriteString(fontspec); WriteLn; Halt(2);
   END;
   j := GetByte();                         (* length of comment *)
   INC(byteoffset,j + 16);                 (* skip rest of preamble *)
   FOR i := 0 TO maxTeXchar DO
      WITH pixelptr^[i] DO
         mapadr := 0;                      (* all chars absent initially *)
         loaded := FALSE;                  (* bitmap not yet downloaded *)
      END;
   END;
   LOOP
      flagpos  := byteoffset;              (* remember position of flagbyte *)
      flagbyte := GetByte();
      IF flagbyte < 240 THEN               (* read character definition *)
         flagbyte := flagbyte MOD 8;       (* value of bottom 3 bits *)
         IF flagbyte < 4 THEN              (* short char preamble *)
            packetlen := flagbyte * 256 + GetByte();
            chcode    := GetByte();
            endofpacket := packetlen + byteoffset;
            WITH pixelptr^[chcode] DO
               b1     := GetByte();
               b2     := GetByte();
               b3     := GetByte();
               dwidth := FixToDVI(0,b1,b2,b3);   (* b0 = 0 *)
               pwidth := GetByte();
               wd     := GetByte();
               ht     := GetByte();
               xo     := SignedByte();
               yo     := SignedByte();
            END;
         ELSIF flagbyte < 7 THEN           (* extended short char preamble *)
            packetlen := (flagbyte - 4) * 65536 + GetTwoBytes();
            chcode    := GetByte();
            endofpacket := packetlen + byteoffset;
            WITH pixelptr^[chcode] DO
               b1     := GetByte();
               b2     := GetByte();
               b3     := GetByte();
               dwidth := FixToDVI(0,b1,b2,b3);   (* b0 = 0 *)
               pwidth := GetTwoBytes();
               wd     := GetTwoBytes();
               ht     := GetTwoBytes();
               xo     := SignedPair();
               yo     := SignedPair();
            END;
         ELSE                              (* long char preamble *)
            packetlen := SignedQuad();
            chcode    := SignedQuad();
            endofpacket := packetlen + byteoffset;
            WITH pixelptr^[chcode] DO
               b0     := GetByte();
               b1     := GetByte();
               b2     := GetByte();
               b3     := GetByte();
               dwidth := FixToDVI(b0,b1,b2,b3);
               pwidth := SignedQuad() DIV 65536;   (* dx in pixels *)
               INC(byteoffset,4);                  (* skip dy *)
               wd     := SignedQuad();
               ht     := SignedQuad();
               xo     := SignedQuad();
               yo     := SignedQuad();
            END;
         END;
         WITH pixelptr^[chcode] DO
            IF (wd = 0) OR (ht = 0) THEN
               mapadr := 0;                (* no bitmap *)
            ELSE
               mapadr := flagpos;          (* position of flagbyte *)
            END;
         END;
         byteoffset := endofpacket;        (* skip raster info *)
      ELSE
         CASE flagbyte OF
            240..243 : i := 0;
                       FOR j := 240 TO flagbyte DO i := 256 * i + GetByte() END;
                       INC(byteoffset,i);  (* skip special parameter *)
          | 244      : INC(byteoffset,4);  (* skip numspecial parameter *)
          | pknoop   :                     (* do nothing *)
          | pkpost   : EXIT;               (* no more character definitions *)
         ELSE
            WriteLn;
            WriteString('Bad flag byte in ');
            WriteString(fontspec); WriteLn; Halt(2);
         END;
      END;
   END; (* LOOP; flagbyte = pkpost *)
END;
END PKFillPixelTable;

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

(* Routines to unpack raster info need some global variables: *)

VAR
   turnon : BOOLEAN;       (* is current run black? *)
   dynf,                   (* dynamic packing variable *)
   repeatcount,            (* how many times to repeat the next row *)
   inputbyte,              (* the current input byte *)
   bitweight : CARDINAL;   (* for getting bits or nybbles from inputbyte *)

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

PROCEDURE PKLoadBitmap (fontptr : fontinfoptr; code : CARDINAL);

(* Download bitmap using information from character definition at mapadr
   in currently open PK file.
*)

VAR i, j, flagbyte, bitpos, bytesperrow,
    rowsleft, hbit, count, rp, charsperline : CARDINAL;
    byte : BITSET;
    row : ARRAY [0..400] OF BYTE;      (* SYSDEP: max glyph width = 3200 bits *)

BEGIN
WITH fontptr^.pixelptr^[code] DO
bytesperrow := (wd + 7) DIV 8;         (* bytes in one row *)
byteoffset := mapadr;                  (* mapadr = flagbyte offset in PK file *)
flagbyte := GetByte();                 (* assume < 240 *)
dynf := flagbyte DIV 16;
turnon := (flagbyte MOD 16) >= 8;      (* is 1st pixel black? *)
flagbyte := flagbyte MOD 8;            (* value of bottom 3 bits *)
IF flagbyte < 4 THEN                   (* skip short char preamble *)
   INC(byteoffset,10);
ELSIF flagbyte < 7 THEN                (* skip extended short char preamble *)
   INC(byteoffset,16);
ELSE                                   (* skip long char preamble *)
   INC(byteoffset,36);
END;
charsperline := 0;
PutString('[<');                       (* start of hex string *)
bitweight := 0;                        (* to get 1st inputbyte *)
IF dynf = 14 THEN
   (* raster info is a string of bits in the next (wd * ht + 7) DIV 8 bytes *)
   FOR i := 1 TO CARDINAL(ht) DO
      byte := {};                                    (* set all bits to 0 *)
      bitpos := 7;                                   (* leftmost bit *)
      FOR j := 1 TO CARDINAL(wd) DO
         IF bitweight = 0 THEN
            (* next 2 lines equal inputbyte := GetByte(); *)
            inputbyte := CARDINAL(filestart^[byteoffset]);
            INC(byteoffset);
            bitweight := 8;
         END;
         DEC(bitweight);                             (* 7..0 *)
         IF bitweight IN BITSET(inputbyte) THEN
            INCL(byte,bitpos);                       (* set bit *)
         END;
         IF bitpos > 0 THEN
            DEC(bitpos);                             (* next bit *)
         ELSE
            INC(charsperline,2);
            IF charsperline >= 72 THEN
               Put(CR);
               charsperline := 0;
            END;
            Put(hexdigs[ CARDINAL(byte) DIV 16 ]);   (* high nybble *)
            Put(hexdigs[ CARDINAL(byte) MOD 16 ]);   (* low nybble *)
            byte := {};
            bitpos := 7;
         END;
      END;
      IF bitpos < 7 THEN
         INC(charsperline,2);
         IF charsperline >= 72 THEN
            Put(CR);
            charsperline := 0;
         END;
         Put(hexdigs[ CARDINAL(byte) DIV 16 ]);
         Put(hexdigs[ CARDINAL(byte) MOD 16 ]);
      END;
   END;
ELSE
   (* raster info is encoded as run and repeat counts *)
   rowsleft := ht;
   hbit := wd;
   repeatcount := 0;
   rp := 1;
   bitpos := 8;
   byte := {};
   WHILE rowsleft > 0 DO
      count := PackedNum();
      WHILE count > 0 DO
         IF (count < bitpos) AND (count < hbit) THEN
            IF turnon THEN
               byte := byte + gpower[bitpos] - gpower[bitpos - count];
            END;
            DEC(hbit,count);
            DEC(bitpos,count);
            count := 0;
         ELSIF (count >= hbit) AND (hbit <= bitpos) THEN
            IF turnon THEN
               byte := byte + gpower[bitpos] - gpower[bitpos - hbit];
            END;
            row[rp] := BYTE(byte);
            (* end of current row, so send it repeatcount+1 times *)
            FOR i := 0 TO repeatcount DO
               FOR j := 1 TO bytesperrow DO
                  INC(charsperline,2);
                  IF charsperline >= 72 THEN
                     Put(CR);
                     charsperline := 0;
                  END;
                  Put(hexdigs[ CARDINAL(row[j]) DIV 16 ]);
                  Put(hexdigs[ CARDINAL(row[j]) MOD 16 ]);
               END;
            END;
            DEC(rowsleft,repeatcount + 1);
            repeatcount := 0;
            rp := 1;
            byte := {};
            bitpos := 8;
            DEC(count,hbit);
            hbit := wd;
         ELSE
            IF turnon THEN byte := byte + gpower[bitpos] END;
            row[rp] := BYTE(byte);
            INC(rp);                   (* we assume rp never overflows! *)
            byte := {};
            DEC(count,bitpos);
            DEC(hbit,bitpos);
            bitpos := 8;
         END;
      END;
      turnon := NOT turnon;
   END;
END;
Put('>'); Put(CR);
PutCard(bytesperrow * 8); Put(' ');
PutInt(ht); Put(' ');
PutInt(xo); Put(' ');
PutInt(yo); Put(' ');                  (* offset of origin from top row *)
PutInt(pwidth);
PutString('] ');
PutCard(code);
PutString(' dc'); Put(CR);
END; (* WITH *)
END PKLoadBitmap;

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

PROCEDURE PackedNum () : CARDINAL;

(* Return next run count using algorithm given in section 23 of PKtype.
   A possible side-effect is to set the global repeatcount value used
   to duplicate the current row.
*)

VAR i, j : CARDINAL;

BEGIN
i := GetNyb();
IF i = 0 THEN
   REPEAT j := GetNyb(); INC(i) UNTIL j <> 0;
   WHILE i > 0 DO j := j * 16 + GetNyb(); DEC(i) END;
   RETURN j - 15 + (13 - dynf) * 16 + dynf;
ELSIF i <= dynf THEN
   RETURN i;
ELSIF i < 14 THEN
   RETURN (i - dynf - 1) * 16 + GetNyb() + dynf + 1;
ELSE
   IF i = 14 THEN
      repeatcount := PackedNum();   (* recursive *)
   ELSE
      repeatcount := 1;             (* nybble = 15 *)
   END;
   RETURN PackedNum();              (* recursive *)
END;
END PackedNum;

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

PROCEDURE GetNyb () : CARDINAL;

(* Return next nybble in PK file. *)

BEGIN
IF bitweight = 0 THEN
   (* next 2 lines equal inputbyte := GetByte(); *)
   inputbyte := CARDINAL(filestart^[byteoffset]);
   INC(byteoffset);
   bitweight := 16;           (* for next call of GetNyb *)
   RETURN inputbyte DIV 16;   (* high nybble *)
ELSE
   bitweight := 0;            (* for next call of GetNyb *)
   RETURN inputbyte MOD 16;   (* low nybble *)
END;
END GetNyb;

(******************************************************************************)
(* Here are the declarations and routines for reading TFM files:              *)
(******************************************************************************)

VAR
   lf, lh, bc, ec, nw, nh : INTEGER;
   charinfo    : ARRAY [0..255] OF
                    RECORD
                       wdindex, htindex, dpindex : INTEGER;
                    END;
   charmetrics : ARRAY [0..255] OF
                    RECORD
                                              (* 4 bytes making up fixword *)
                       width, height, depth : ARRAY [0..3] OF INTEGER;
                    END;

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

PROCEDURE TFMFillPixelTable;

(* Fill the pixeltable for currfont^ (a PostScript font)
   using information in the currently open TFM file.
*)

VAR c, dheight, pheight, ddepth, pdepth : INTEGER;

BEGIN
ReadTFMIntegers;                         (* read lf..nh *)
ReadTFMCharInfo;                         (* fill charinfo array *)
ReadTFMCharMetrics;                      (* fill charmetrics array *)
WITH currfont^ DO
   FOR c := 0 TO bc - 1 DO
      pixelptr^[c].mapadr := 0;          (* chars < bc don't exist *)
   END;
   FOR c := ec + 1 TO 255 DO
      pixelptr^[c].mapadr := 0;          (* chars > ec don't exist *)
   END;
   FOR c := bc TO ec DO
      WITH pixelptr^[c] DO
      WITH charmetrics[c] DO
         dwidth  := FixToDVI(width[0],width[1],width[2],width[3]);
         dheight := FixToDVI(height[0],height[1],height[2],height[3]);
         ddepth  := FixToDVI(depth[0],depth[1],depth[2],depth[3]);
         (* convert DVI units to pixels *)
         pwidth  := PixelRound(dwidth);
         pheight := PixelRound(dheight);
         pdepth  := PixelRound(ddepth);
         (* Since we don't have access to bitmap info for a PostScript font
            we will have to use the TFM width/height/depth info to
            approximate wd, ht, xo, yo.
         *)
         wd := pwidth;
         DEC(wd,wd DIV 8);               (* better approximation *)
         ht := pheight + pdepth;
         xo := 0;
         yo := pheight - 1;
         IF (wd = 0) OR (ht = 0) THEN
            mapadr := 0;                 (* char all-white or not in font *)
         ELSE
            mapadr := 1;                 (* anything but 0 *)
         END;
         loaded := FALSE;                (* no bitmap available *)
      END;
      END;
   END;
END;
END TFMFillPixelTable;

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

PROCEDURE ReadTFMIntegers;

(* Read the first 6 16-bit integers in the TFM file.  See TFtoPL section 8. *)

BEGIN
byteoffset := 0;       (* start reading at 1st byte in TFM file *)
lf := GetTwoBytes();
lh := GetTwoBytes();
bc := GetTwoBytes();
ec := GetTwoBytes();
nw := GetTwoBytes();
nh := GetTwoBytes();
END ReadTFMIntegers;

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

PROCEDURE ReadTFMCharInfo;

(* Read the charinfo array.  See TFtoPL section 11. *)

VAR c, i : INTEGER;

BEGIN
byteoffset := 24 + (lh * 4);       (* offset of charinfo array *)
FOR c := bc TO ec DO
   WITH charinfo[c] DO
      wdindex := GetByte() * 4;    (* offset from start of width array *)
      i       := GetByte();        (* 2nd byte contains htindex and dpindex *)
      htindex := (i DIV 16) * 4;   (* offset from start of height array *)
      dpindex := (i MOD 16) * 4;   (* offset from start of depth array *)
      INC(byteoffset,2);           (* skip itindex and remainder bytes *)
   END;
END;
END ReadTFMCharInfo;

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

PROCEDURE ReadTFMCharMetrics;

(* Read the charmetrics array using the indices in charinfo. *)

VAR wdbase, htbase, dpbase, b, c : INTEGER;

BEGIN
wdbase := 24 + lh * 4 + (ec - bc + 1) * 4;   (* offset of width array *)
htbase := wdbase + nw * 4;                   (* offset of height array *)
dpbase := htbase + nh * 4;                   (* offset of depth array *)
FOR c := bc TO ec DO
   WITH charinfo[c] DO
   WITH charmetrics[c] DO
      byteoffset := wdbase + wdindex;
      FOR b := 0 TO 3 DO width[b] := GetByte() END;
      byteoffset := htbase + htindex;
      FOR b := 0 TO 3 DO height[b] := GetByte() END;
      byteoffset := dpbase + dpindex;
      FOR b := 0 TO 3 DO depth[b] := GetByte() END;
   END;
   END;
END;
END ReadTFMCharMetrics;

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

BEGIN
END FontReader.