summaryrefslogtreecommitdiff
path: root/obsolete/systems/knuth/pxl/gftopxl.web
blob: 3570a115d443f8e528e4487262bff5999708bf16 (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
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
% This program is based on an early version of GFtype.
% Although PXL files are now obsolete (since, for example, they can't
% handle fonts with more than 128 characters), this program may be
% needed for awhile to interface with old-fashioned software.

% Version 1.0 was implemented in June 1984 by Arthur Samuel.
% Version 1.1 corrected a bug in the reference point offset (DEK, Sep 84).
% Version 2.0 switched to new GF format          (JDH, Dec 84).
% Version 2.1 switched to a newer GF format        (JDH, Feb 85).

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

\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}

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

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

@* Introduction.
The \.{GFtoPXL} utility program reads binary generic-font (``\.{GF}'')
files that are produced by font compilers such as \MF, and converts them
into PXL Matrix Format (``\.{PXL}'') files. While it is usually possible
to write driver programs for dot matrix outpot devices that accept
\.{GF} input files, there are situations where one may want to use an old
\.{PXLtoDRIVER} program to load recently created \.{GF} information.
\.{GFto PXL} can be used to generate the required \.{PXL} files.  This program,
in its handling of the \.{GF} input to produce an |image_array|,
follows the example set by \.{GFtype}.

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

@d banner=='This is GFtoPXL, Version 2.1' {printed when the program starts}
@d resolution==200.0  {pixelx per inch, standard for pxl files}

@ Some of the code below is intended to be used only when debugging this
program.
Such code will not normally be compiled; it is
delimited by the codewords `$|debug|\ldots|gubed|$', with apologies
to people who wish to preserve the purity of English.  For even
more drastic and voluminous output, we use |eebug|.
@^debugging@>

@d debug==@{
@d gubed==@t@>@}
@d eebug==@{ {change this to `$\\{eebug}\equiv\null$' when really debugging}
@d gubee==@t@>@} {change this to `$\\{gubee}\equiv\null$' when really debugging}
@f debug==begin
@f gubed==end
@f eebug==begin
@f gubee==end

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

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

@ The binary input comes from |gf_file|, and the output font is written
on |pxl_file|.  Status reporting and error messages appear
on \PASCAL's standard |output| file. The term |print| is used instead of
|write| when this program writes on |output|, so that all such output
could easily be redirected if desired.

@d print(#)==write(#)
@d print_ln(#)==write_ln(#)
@d print_nl==write_ln

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

@ If the program has to stop prematurely, it goes to the
`|final_end|'.

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

@<Labels...@>=final_end;

@ The following parameters can be changed at compile time to extend or
reduce |GF_to_PXL|'s capacity.

@<Constants...@>=
@!line_length=79; {bracketed lines of output will be at most this long}
@!terminal_line_length=150; {maximum number of characters input in a single
  line of input from the terminal}
@!max_glyph_no=127; {maximum glyph number in font, later change to 255}
@!top_pixel=700; {boundary of pixel image of glyph}
@!bot_pixel=-250;
@!left_pixel=-250;
@!right_pixel=750;

@ Here are some macros for common programming idioms.

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

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

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

@d abort(#)==begin print(' ',#); jump_out;
    end
@d bad_gf(#)==abort('Bad GF file: ',#,'!')
@.Bad GF file@>

@p procedure jump_out;
begin goto final_end;
end;

@ We steal the following routine from \MF.

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

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

@* Optional modes of output.
The following is left in the program to simplify the addition of some
input and output routines should these be needed later.

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

@<Glob...@>=
@!buffer:array[0..terminal_line_length] of ASCII_code;
@!term_in:text_file; {the terminal, considered as an input file}
@!term_out:text_file; {the terminal, considered as an output file}

@ Humdrum.
@p function lower_casify(@!c:ASCII_code):ASCII_code;
begin
if (c>="A") and (c<="Z") then lower_casify:=c+"a"-"A"
else lower_casify:=c;
end;

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

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

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

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

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

@d text_char == char {the data type of characters in text files}
@d first_text_char=0 {ordinal number of the smallest element of |text_char|}
@d last_text_char=127 {ordinal number of the largest element of |text_char|}

@<Types...@>=
@!text_file=packed file of text_char;

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

@<Globals...@>=
@!xord: array [text_char] of ASCII_code;
  {specifies conversion of input characters}
@!xchr: array [0..255] of text_char;
  {specifies conversion of output characters}

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

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

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

@<Set init...@>=
for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
for i:=" " to "~" do xord[xchr[i]]:=i;

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

A \.{GF} file is a stream of 8-bit bytes that may be
regarded as a series of commands in a machine-like language. The first
byte of each command is the operation code, and this code is followed by
zero or more bytes that provide parameters to the command. The parameters
themselves may consist of several consecutive bytes; for example, the
`|boc|' (beginning of character) command has six parameters, each of
which is four bytes long. Parameters are usually regarded as nonnegative
integers; but four-byte-long parameters can be either positive or
negative, hence they range in value from $-2^{31}$ to $2^{31}-1$.
As in \.{TFM} files, numbers that occupy
more than one byte position appear in BigEndian order,
and negative numbers appear in two's complement notation.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

\yskip\hang|@!new_row_1| through |@!new_row_164| (opcodes 75 to 238). Same as
|new_row_0|, but with |m:=min_m+1| through |min_m+164|, respectively.

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

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

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

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

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

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

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

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

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

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

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

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

@d gf_id_byte=131 {identifies the kind of \.{GF} files described here}

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

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

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

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

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

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

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

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

@ The last part of the postamble, following the |post_post| byte that
signifies the end of the character locators, contains |q|, a pointer to the
|post| command that started the postamble.  An identification byte, |i|,
comes next; this currently equals~131, as in the preamble.

The |i| byte is followed by four or more bytes that are all equal to
the decimal number 223 (i.e., @'337 in octal). \MF\ puts out four to seven of
these trailing bytes, until the total length of the file is a multiple of
four bytes, since this works out best on machines that pack four bytes per
word; but any number of 223's is allowed, as long as there are at least four
of them. In effect, 223 is a sort of signature that is added at the very end.
@^Fuchs, David Raymond@>

This curious way to finish off a \.{GF} file makes it feasible for
\.{GF}-reading programs to find the postamble first, on most computers,
even though \MF\ wants to write the postamble last. Most operating
systems permit random access to individual words or bytes of a file, so
the \.{GF} reader can start at the end and skip backwards over the 223's
until finding the identification byte. Then it can back up four bytes, read
|q|, and move to byte |q| of the file. This byte should, of course,
contain the value 248 (|post|); now the postamble can be read, so the
\.{GF} reader can discover all the information needed for individual characters.

Unfortunately, however, standard \PASCAL\ does not include the ability to
@^system dependencies@>
access a random position in a file, or even to determine the length of a file.
Almost all systems nowadays provide the necessary capabilities, so \.{GF}
format has been designed to work most efficiently with modern operating systems.
Indeed, \.{GFtoPXL} insists on being able to read the postamble first, because
it needs the device width information found there in order to encode characters.

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

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

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

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

@ The program deals with two binary file variables: |gf_file| is the
input file that we are translating into \.{PXL} format, to be written
on |pxl_file|.

@<Glob...@>=
@!gf_file:byte_file; {the stuff we are \.{GFtoPXL}ing}
@!pxl_file:byte_file; {the stuff we have \.{GFtoPXL}ed}

@ To prepare the |gf_file| for input, we |reset| it.

@p procedure open_gf_file; {prepares to read packed bytes in |gf_file|}
begin reset(gf_file);
cur_loc:=0;
end;

@ To prepare the |pxl_file| for output, we |rewrite| it.

@p procedure open_pxl_file; {prepares to write packed bytes in |pxl_file|}
begin rewrite(pxl_file);
end;

@ If you looked carefully at the preceding code, you probably asked,
``What is |cur_loc|?'' Good question. It's a global variable that holds
the number of the byte about to be read next from |gf_file|.  Likewise,
|pxl_byte| holds the number of the byte about to be written next
into |pxl_file|.

@<Glob...@>=
@!cur_loc:integer; {where we are about to look, in |gf_file|}
@!pxl_byte_no:integer; {where we are about to write, in |pxl_file|}

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

@p function get_byte:integer; {returns the next byte, unsigned}
var b:eight_bits;
begin if eof(gf_file) then get_byte:=0
else  begin read(gf_file,b); incr(cur_loc); get_byte:=b;
  end;
end;
@#
function signed_byte:integer; {returns the next byte, signed}
var b:eight_bits;
begin read(gf_file,b); incr(cur_loc);
if b<128 then signed_byte:=b @+ else signed_byte:=b-256;
end;
@#
function get_two_bytes:integer; {returns the next two bytes, unsigned}
var a,@!b:eight_bits;
begin read(gf_file,a); read(gf_file,b);
cur_loc:=cur_loc+2;
get_two_bytes:=a*256+b;
end;
@#
function signed_pair:integer; {returns the next two bytes, signed}
var a,@!b:eight_bits;
begin read(gf_file,a); read(gf_file,b);
cur_loc:=cur_loc+2;
if a<128 then signed_pair:=a*256+b
else signed_pair:=(a-256)*256+b;
end;
@#
function get_three_bytes:integer; {returns the next three bytes, unsigned}
var a,@!b,@!c:eight_bits;
begin read(gf_file,a); read(gf_file,b); read(gf_file,c);
cur_loc:=cur_loc+3;
get_three_bytes:=(a*256+b)*256+c;
end;
@#
function signed_trio:integer; {returns the next three bytes, signed}
var a,@!b,@!c:eight_bits;
begin read(gf_file,a); read(gf_file,b); read(gf_file,c);
cur_loc:=cur_loc+3;
if a<128 then signed_trio:=(a*256+b)*256+c
else signed_trio:=((a-256)*256+b)*256+c;
end;
@#
function signed_quad:integer; {returns the next four bytes, signed}
var a,@!b,@!c,@!d:eight_bits;
begin read(gf_file,a); read(gf_file,b); read(gf_file,c); read(gf_file,d);
cur_loc:=cur_loc+4;
if a<128 then signed_quad:=((a*256+b)*256+c)*256+d
else signed_quad:=(((a-256)*256+b)*256+c)*256+d;
end;

@ Most info in the |pxl_file| comes in words, but we have to write it
as bytes and halfwords occasionally.

@d pxl_byte(#)==begin write(pxl_file,#); incr(pxl_byte_no); end

@p procedure pxl_halfword(@!w:integer);
begin
if w<0 then w:=w+@"10000;
pxl_byte(w div @"100);
pxl_byte(w mod @"100);
end;
@#
procedure pxl_word(@!w:integer);
begin
if w>=0 then pxl_byte(w div @"1000000)
else begin
  w:=w+@"40000000;
  w:=w+@"40000000;
  pxl_byte((w div @"1000000) + 128);
  end;
pxl_byte((w div @"10000) mod @"100);
pxl_byte((w div @"100) mod @"100);
pxl_byte(w mod @"100);
end;

@ Finally we come to the routines that are used for random access of the
|gf_file|. The driver program below needs two such routines: |gf_length| should
compute the total number of bytes in |gf_file|, possibly also
causing |eof(gf_file)| to be true; and |move_to_byte(n)|
should position |gf_file| so that the next |get_byte| will read byte |n|,
starting with |n=0| for the first byte in the file.
@^system dependencies@>

Such routines are, of course, highly system dependent. They are implemented
here in terms of two assumed system routines called |set_pos| and |cur_pos|.
The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is
negative or larger than the total number of items in |f|; in the latter
case, |set_pos(f,n)| moves to the end of file |f|.
The call |cur_pos(f)| gives the total number of items in |f|, if
|eof(f)| is true; we use |cur_pos| only in such a situation.

@p function gf_length:integer;
begin set_pos(gf_file,-1); gf_length:=cur_pos(gf_file);
end;
@#
procedure move_to_byte(n:integer);
begin set_pos(gf_file,n); cur_loc:=n;
end;

@* Translation to raster form.
The main work of \.{GFtoPXL} is accomplished by the |do_char| procedure,
which produces the output for an entire character, assuming that the |boc|
command for that character has already been processed. This procedure
works in two parts, the first of which is
essentially an interpretive routine that reads and acts on the \.{GF}
commands by writing into an array of pixels, and the second is a
routine which encapsules this information into the form needed for
the Raster Information portion of the \.{PXL} file.


@ The definition of \.{GF} files refers to two registers,
$(m,n)$, which hold integer row and column numbers.  We also
need to remember |paint_switch|, who's value is either |black|
or |white|.

@<Types...@>=
@!x_coord=left_pixel..right_pixel;
@!y_coord=bot_pixel..top_pixel;
{BUG FIXME ??? off by one}

@ @<Glob...@>=
@!m: x_coord;
@!n: y_coord; {current state values}
@!paint_switch: pixel;

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

@d white=0 {could also be |false|}
@d black=1 {could also be |true|}
@d complement(#)==if #=black then #:=white@+else #:=black
  {could also be |paint_switch:=not paint_switch|}

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

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

@d image==image_array[n,m]

@<Glob...@>=
@!image_array: packed array [y_coord,x_coord] of pixel;

@ Maybe there's a faster way to do this on your system.  Note that the
only part of the |image_array| that we clear is the part that the current
character may use.  Thus, the rest of this program may not look outside
the area delimited by |[y,x]=[min_n..max_n,min_m..max_m]| and expect to
see anything but junk.
@^system dependencies@>

@<Clear the image@>=
begin
n:=min_n;
while n<=max_n do begin
  m:=min_m;
  while m<=max_m do begin
    image:=white;
    incr(m);
    end;
  incr(n);
  end;
end

@ @<Print the image@>=
begin
n:=max_n;
while (n>=min_n)and(n>=max_n-80) do begin
  m:=min_m;
  while (m<=max_m) and (m<=min_m+78) do begin
    if image=white then print(' ')@+else print('*');
    incr(m);
    end;
  print_ln(' ');
  decr(n);
  end;
end

@ Let's keep track of how many characters are in the font.

@<Glob...@>=
@!char_code: integer; {current character number}
@!total_chars:integer; {the total number of characters seen so far}

@ @<Set init...@>=
total_chars:=0;

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

@d three_cases(#)==#,#+1,#+2
@d four_cases(#)==#,#+1,#+2,#+3
@d eight_cases(#)==four_cases(#),four_cases(#+4)
@d nine_cases(#)==eight_cases(#),#+8
@d sixteen_cases(#)==eight_cases(#),eight_cases(#+8)
@d eighteen_cases(#)==nine_cases(#),nine_cases(#+9)
@d thirty_two_cases(#)==sixteen_cases(#),sixteen_cases(#+16)
@d sixty_four_cases(#)==thirty_two_cases(#),thirty_two_cases(#+32)
@d eighty_two_cases(#)==sixty_four_cases(#),eighteen_cases(#+64)
@d one_hundred_sixty_five_cases(#)==
  #,eighty_two_cases(#+1),eighty_two_cases(#+83)

@p function first_par(o:eight_bits):integer;
begin case o of
sixty_four_cases(paint_0): first_par:=o-paint_0;
paint1,skip1,char_loc,char_loc0,xxx1: first_par:=get_byte;
paint1+1,skip1+1,xxx1+1: first_par:=get_two_bytes;
paint1+2,skip1+2,xxx1+2: first_par:=get_three_bytes;
xxx1+3,yyy: first_par:=signed_quad;
skip0,nop,boc,boc1,eoc,pre,post,post_post,undefined_commands: first_par:=0;
one_hundred_sixty_five_cases(new_row_0): first_par:=o-new_row_0;
end;
end;

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

@p function do_char:boolean;
label 1,2,3,4,9997,9998,9999;
var o:eight_bits; {operation code of the current command}
@!p,@!q:integer; {parameters of the current command}
i,j: integer; {used to index in |select byte type|}
b: eight_bits; {holding byte for pxl bits}
begin {we've already scanned the |boc|}
do_char:=true;
while true do @<Translate the next command in the \.{GF} file;
    |goto 9999| if it was |eoc|;
    |goto 9998| if premature termination is needed@>;
9998: print_ln('!'); do_char:=false; goto 9997;
9999:
@<Remove white rows and columns@>;
@<Save directory info.@>
@<Encode the glyph@>;
9997: end;

@ This is the main command loop.

@d error(#)==begin print(a:1,': ! ',#); print_nl; end

@<Translate the next command...@>=
begin a:=cur_loc;
o:=get_byte; p:=first_par(o);
if eof(gf_file) then bad_gf('the file ended prematurely');
@.the file ended prematurely@>
@<Translate command |o|@>;
end

@ The multiway switch in |first_par|, above, was organized by the length
of each command; the one in |do_char| is organized by the semantics.

@<Translate command...@>=
if o<=paint1+2 then @<Translate a |paint| command@>
else if (new_row_0<=o) and (o<=max_new_row) then
  @<Translate a |new_row| command@>
else case o of
  four_cases(skip0): @<Translate a |skip| command@>;
  @t\4@>@<Cases for commands |nop|, |pre|, |post|, |post_post|, |boc|,
    and |eoc|@>@;
  four_cases(xxx1): @<Translate an |xxx| command@>;
  yyy: @<Translate a |yyy| command@>;
  othercases error('undefined command ',o:1,'!')
@.undefined command@>
  endcases

@ @<Cases for commands |nop|...@>=
nop: do_nothing;
pre: begin error('preamble command within a character!'); goto 9998;
  end;
@.preamble command within a character@>
post,post_post: begin error('postamble command within a character!'); goto 9998;
@.postamble command within a character@>
  end;
boc,boc1: begin error('boc occurred before eoc'); goto 9998;
@.boc occurred before eoc@>
  end;
eoc: goto 9999;

@ @<Translate an |xxx| command@>=
begin bad_char:=false;
while p>0 do begin
  q:=get_byte;
  if (q<" ")or(q>"~") then bad_char:=true;
  decr(p);
  end;
if bad_char then error('non-ASCII character in xxx command!');
@.non-ASCII character...@>
end

@ @<Translate a |yyy| command@>=
do_nothing

@ @<Translate a |paint| command@>=
begin
if m+p>right_pixel then
  begin
  if m<=right_pixel then {give error message only once}
    error('character extends too far to the right for me');
@.character extends...@>
  m:=m+p;
  end
else while p>0 do begin
  image:=paint_switch;
  incr(m);
  decr(p);
  end;
complement(paint_switch);
end

@ @<Translate a |new_row| command@>=
begin
decr(n);
m:=min_m+p;
paint_switch:=black;
end

@ @<Translate a |skip| command@>=
begin
n:=n-(p+1);
m:=min_m;
paint_switch:=white;
end

@ Note that \.{GF} format does not guarantee that |max_m| and |max_n| are
as small as they might be (they're sure to be big enough, though).  Likewise,
|min_m| and |min_n| might be smaller than they might be.  Here we set the
bounds as tightly as possible.

@d no_black==(min_n>max_n)

@<Remove white rows and columns@>=
@!debug
print_nl;
print_ln('before: minm=',min_m:1,' maxm=',max_m:1,
  ' minn=',min_n:1,' maxn=',max_n:1);
gubed@/
n:=min_n;
while n<=max_n do begin
  m:=min_m;
  while m<=max_m do begin
    if image=black then goto 1;
    incr(m);
    end;
  incr(n);
  min_n:=n; {blank row at bottom now gone}
  end;
1:
if no_black then begin
  min_m:=max_m+1;
  goto 4; {why bother?}
  end;
n:=max_n;
while n>min_n do begin
  m:=min_m;
  while m<=max_m do begin
    if image=black then goto 2;
    incr(m);
    end;
  decr(n);
  max_n:=n; {blank row at top now gone}
  end;
2:
m:=min_m;
while m<max_m do begin
  n:=min_n;
  while n<=max_n do begin
    if image=black then goto 3;
    incr(n);
    end;
  incr(m);
  min_m:=m; {blank column at left now gone}
  end;
3:
m:=max_m;
while m>min_m do begin
  n:=min_n;
  while n<=max_n do begin
    if image=black then goto 4;
    incr(n);
    end;
  decr(m);
  max_m:=m; {blank column at right now gone}
  end;
4:
cols:=max_m-min_m+1;
rows:=max_n-min_n+1;
num_bytes:=4*((cols+31) div 32);
@!debug
print_ln('after : minm=',min_m:1,' maxm=',max_m:1,
  ' minn=',min_n:1,' maxn=',max_n:1);
print_ln('cols=',cols:1,' rows=',rows:1);
gubed

@ While we were at it, we also snuck in a computation of how many
rows and columns are in the bounding box and the number of bytes
needed to report each row of pixels.

@<Glob...@>=
@!cols,@!rows: integer; {columns and rows of pixels}
@!num_bytes: integer; {needed bytes per row}

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

@p procedure read_postamble;
var k:integer; {loop index}
@!p,@!q,@!m,@!c:integer; {general purpose registers}
begin post_loc:=cur_loc-1;
print_ln('Postamble starts at byte ',post_loc:1,'.');
@.Postamble starts at byte n@>
p:=signed_quad;
design_size:=signed_quad; check_sum:=signed_quad;@/
print('design size = ',design_size:1,' (');
print_scaled(design_size div 16); print_ln(')');
print_ln('check sum = ',check_sum:1);@/
hppp:=signed_quad; vppp:=signed_quad;@/
print('hppp = ',hppp:1,' ('); print_scaled(hppp); print_ln(')');
print('vppp = ',vppp:1,' ('); print_scaled(vppp); print_ln(')');
magnification:=hppp/(65536.0*resolution/72.27);
pxl_mag:=round(1000*magnification);
print_ln('mag = ',pxl_mag:1);
min_m:=signed_quad; max_m:=signed_quad;
min_n:=signed_quad; max_n:=signed_quad;@/
print_ln('min m = ',min_m:1,', max m = ',max_m:1);@/
print_ln('min n = ',min_n:1,', max n = ',max_n:1);@/
@<Process the character locations in the postamble@>;
@<Make sure that the end of the file is well-formed@>;
end;

@ Here is the main information we glean from the postamble.

@<Glob...@>=
@!design_size: integer;
@!min_m, @!max_m, @!min_n, @!max_n: integer;
@!hppp, @!vppp: integer;
@!check_sum: integer;
@!post_loc: integer;
@!magnification: real;
@!tfm_width: array [0..max_glyph_no] of integer;
@!device_width: array [0..max_glyph_no] of integer;

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

@<Make sure that the end of the file is well-formed@>=
if k<>post_post then
  error('should be postpost!');
@.should be postpost@>
q:=signed_quad;
if q<>post_loc then
  error('postamble pointer should be ',post_loc:1,' not ',q:1);
@.postamble pointer should be...@>
m:=get_byte;
if m<>gf_id_byte then error('identification byte should be ',gf_id_byte:1);
@.identification byte should be n@>
k:=cur_loc; m:=223;
while (m=223)and not eof(gf_file) do m:=get_byte;
if not eof(gf_file) then bad_gf('signature in byte ',cur_loc-1:1,
@.signature...should be...@>
    ' should be 223')
else if cur_loc<k+4 then
  error('not enough signature bytes at end of file');
@.not enough signature bytes...@>

@ @<Process the character locations...@>=
repeat k:=get_byte;
if (k=char_loc) or (k=char_loc0) then begin
  c:=first_par(k);
  if c>max_glyph_no then abort('Character number too large');
  if k=char_loc0 then device_width[c]:=get_byte
  else begin device_width[c]:=round(signed_quad/unity);
    m := signed_quad;
    end;
  tfm_width[c]:=signed_quad;
  p:=signed_quad;
  k:=nop;
  end;
until k<>nop;

@ This routines is brought into play in order to read the postamble first.

@p procedure find_postamble;
var q,@!k: integer;
begin
post_loc:=gf_length-4;
repeat if post_loc=0 then bad_gf('all 223s');
@.all 223s@>
move_to_byte(post_loc); k:=get_byte; decr(post_loc);
until k<>223;
if k<>gf_id_byte then bad_gf('ID byte is ',k:1);
@.ID byte is wrong@>
move_to_byte(post_loc-3); q:=signed_quad;
if (q<0)or(q>post_loc-3) then
  bad_gf('post pointer ',q:1,' at byte ',post_loc-3:1);
@.post pointer is wrong@>
move_to_byte(q); k:=get_byte;
if k<>post then bad_gf('byte ',q:1,' is not post');
@.byte n is not post@>
end;

@* PXL Matrix Format.
A \.{PXL} file is an expanded raster description of a single font at a
particular resolution and contains essentially the same information as
that contained in a \.{GF} file.  \.{PXL} files are used by many existing
device-driver programs for dot matrix devices. By convention, \.{PXL} files
are for 200 pixels per inch. \.{GFtoPXL} will report the magnification
over the design point size that will occur if the \.{PXL} file is
used on a 200 pixel per inch output device.

All words in of \.{PXL} files are in 32-bit format, with the four lower
bits zero on 36-bit machines. The raster information is contained in a
sequence of binary words which record white pixels as zeros and black
pixels as ones.

The first word of the \.{PXL} file and the last word contain the \.{PXL
ID} which is currently equal to 1001(decimal).

This first word is followed by a sequence of raster information words
where each line of pixels in the glyphs is represented by one or more
words of binary information. The number of words used to represent each
row of pixels for any particular glyph is fixed and it is set by the value
of |max_m-min_m+1| for that particular glyph. Each white pixel is represented
by a zero and each black pixel is represented by a one in the corresponding bit
positions (the first 32 only of each word on 36-bit machines).
 The unused bit positions
toward the end of each set of words for each row of pixels are filled with
zeros. It sould be noted that this representation is more wasteful of
space than it needs to be, but it may possibly simplify the
subsequent use of the information by a device-driver program.

The Font Directory follows, occupying a fixed position with respect to the
end of the file (in words 517 through 6 from this end), and assigns 4
words for each of the potential 128 different glyphs that could be
contained in this particular font in the order of their assending ascii
values (not in the order that the glyphs appear in the raster section,
which may be entirely arbitrary). This means that the first four words are
for the ascii zero glyph.  All four words reserved for any missing glyphs
are set to zero.

The first word of each glyph's directory information contains the Pixel
Width in the left half-word (the leftmost 16 bits) and the Pixel Height in
the right half-word (the next 16 bits). These dimensions are those of the
smallest bounding-box, measured in pixels, and they have nothing
necessarily to do with the width and height figures that appear in the
\.{TFM} file.  The \.{TFM} width, measured in \.{FIXes}, where 1 \.{FIX}
is $1/(2^{20}$ times the design size, is listed in the fourth word of the
glyph's directory information.

The second word of the glyph's directory information contains the offset
of the glyph's reference point from its upper-left-hand corner of the
bounding box, measured in pixels, with the X-Offset in the left half-word
and the Y-Offset in the right half-word.  These numbers may be negative,
and two's complement representation is used.  Remember that the positive x
direction means `rightward' and positive y is `downward' on the page.

The third word of a glyph's directory information contains the number of the
word in this \.{PXL} file where the Raster Description for this particular
glyph begins, measured from the first word which is numbered zero.

As mentioned earlier, the fourth word of directory information for each
glyph contains the \.{TFM} width.

The final five words in the \.{PXL} file contain information relation to
the entire file.

  The first of these five words is a checksum which should
match the checksum contained in the \.{TFM} file that \TeX\ used in
reference to this font, although, if this checksum is zero, no validity
checking will be done.

The second of these five words is an integer that is 1000 times the
magnification factor at which this font was produced.

The third word contains the design sige of the font measured in \.{FIXes}
($2^{-20}$ unmagnified points).

The fourth word contains a pointer to the first word of the font directory.

The fifth and last word of the entire file contains a duplicate of the
\.{PXL} ID as contained in the first word of the file.

@d pxl_id=1001 {current version of \.{PXL} format}

@ @<Glob...@>=
@!glyph_ptr: array [0..max_glyph_no] of integer;
@!glyph_cols: array [0..max_glyph_no] of integer;
@!glyph_rows: array [0..max_glyph_no] of integer;
@!cols_offset: array [0..max_glyph_no] of integer;
@!rows_offset: array [0..max_glyph_no] of integer;
@!bc,@!ec:integer;
@!pxl_dir_ptr:integer;
@!pxl_mag: integer;

@ @<Set init...@>=
char_code:=0;
while char_code<=max_glyph_no do
begin
glyph_cols[char_code]:=0;
glyph_rows[char_code]:=0;
cols_offset[char_code]:=0;
rows_offset[char_code]:=0;
glyph_ptr[char_code]:=0; {marks nonexistent character}
incr(char_code);
end;
bc:=max_glyph_no+1; ec:=-1;

@ The |glyph_ptr| is saved in Encode the glyph.

@<Save directory info.@>=
glyph_cols[char_code]:=cols;
glyph_rows[char_code]:=rows;
cols_offset[char_code]:=-min_m;
rows_offset[char_code]:=max_n;

@ @<Start up the |pxl_file|@>=
pxl_byte_no:=0;
pxl_word(pxl_id);
@!debug
print_ln('Start of PXL info');
gubed

@ When we get to this section we have all of the esential information
needed to write the |4*max_glyph_no| words of the font directory and the
last five words of the |pxl_file|.

@<Finish off the |pxl_file|@>=
if (pxl_byte_no mod 4)<>0 then abort('This can''t happen: alignment');
pxl_dir_ptr:=pxl_byte_no div 4;
for char_code:=0 to max_glyph_no do begin
pxl_halfword(glyph_cols[char_code]);
pxl_halfword(glyph_rows[char_code]);
pxl_halfword(cols_offset[char_code]);
pxl_halfword(rows_offset[char_code]);
pxl_word(glyph_ptr[char_code]); {ptr to glyph raster info}
pxl_word(tfm_width[char_code]);
end;
pxl_word(check_sum);
pxl_word(pxl_mag);
pxl_word(design_size);
pxl_word(pxl_dir_ptr);
pxl_word(pxl_id);
@!debug
print_ln('End of PXL info');
gubed

@ The following procedure writes out the |pxl| raster, and updates
|pxl_byte_no|.

@<Encode the glyph@>=
if glyph_ptr[char_code]<>0 then error('Duplicate glyph');
glyph_ptr[char_code]:=pxl_byte_no div 4;
@!eebug
print_nl; @<Print the image@>;
gubee@/
n:=max_n;
while (n>=min_n) do
begin
m:=min_m;
for j:=1 to num_bytes do
begin
if m<(max_m-7) then @<Get full byte@> else
if (m>max_m) then pxl_byte(0)
else @<Get mixed byte@>;
end;
if (pxl_byte_no mod 4)<>0 then error('Mod 4 error in |encode_glyph|');
decr(n)
end;

@ @<Get full byte@>=
  begin
    b:=image; incr(m);
    for i:=2 to 8 do
      begin
  b:=b*2+image;
  incr(m);
      end;
    pxl_byte(b);
  end

@ @<Get mixed byte@>=
begin
b:=image; incr(m);
i:=2;
while m<=max_m do
  begin
  b:=b*2+image;
  incr(m);
  incr(i);
  end;
for i:=i to 8 do b:=b*2;
pxl_byte(b);
end


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

@p begin initialize; {get all variables initialized}
open_gf_file;
find_postamble; read_postamble;
open_gf_file;
@<Process the preamble@>;
open_pxl_file;
@<Start up the |pxl_file|@>
@<Translate all the characters@>;
print_nl;
@<Finish off the |pxl_file|@>
print('Font had ',total_chars:1,' character');
if total_chars<>1 then print('s');
print(' altogether');
final_end:end.

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

@<Glob...@>=
@!a:integer; {byte number of the current command}
@!o,@!p,@!q:integer; {general purpose registers}
@!bad_char:boolean; {has a non-ASCII character code appeared in this \\{xxx}?}

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

@<Process the preamble@>=
o:=get_byte; {fetch the first byte}
if o<>pre then bad_gf('First byte isn''t start of preamble!');
@.First byte isn't...@>
o:=get_byte; {fetch the identification byte}
if o<>gf_id_byte then
  error('identification byte should be ',gf_id_byte:1,
  ' not ',o:1,'!');
@.identification byte should be n@>
o:=get_byte; {fetch the length of the introductory comment}
print('''');
while o>0 do
  begin decr(o); print(xchr[get_byte]);
  end;
print_ln('''');

@ @<Translate all...@>=
repeat
  @<Pass |nop|, |xxx| and |yyy| commands@>;
  if o<>post then begin
    if (o<>boc)and(o<>boc1)
    then bad_gf('byte ',cur_loc-1:1,' is not boc (',o:1,')');
@.byte n is not boc@>
    @<Pass a |boc| command@>;
    if not do_char then bad_gf('char ended unexpectedly');
@.char ended unexpectedly@>
    print(']');
    incr(total_chars);
    if (total_chars mod 10)=0 then print_nl;
    end
until o=post;

@ @<Pass |nop|, |xxx| and |yyy| commands@>=
repeat
  a:=cur_loc;
  o:=get_byte; p:=first_par(o);
  if eof(gf_file) then bad_gf('the file ended prematurely');
@.the file ended prematurely@>
  if o=yyy then begin @<Translate a |yyy|...@>; o:=nop; end
  else if (o>=xxx1) and (o<=xxx1+3) then begin
    @<Translate an |xxx|...@>; o:=nop;
    end;
until o<>nop;

@ @<Pass a |boc|...@>=
a:=cur_loc;
if o=boc1 then
  begin char_code:=get_byte;
  q:=get_byte; max_m:=get_byte; min_m:=max_m-q;
  q:=get_byte; max_n:=get_byte; min_n:=max_n-q;
  end
else   begin char_code:=get_two_bytes; char_code:=get_two_bytes;
  p:=signed_quad; {ignore backpointer}
  min_m:=signed_quad; max_m:=signed_quad;
  min_n:=signed_quad; max_n:=signed_quad;
  end;
print('[',char_code:1);
if char_code>max_glyph_no then abort('Character number too large');
if char_code>ec then ec:=char_code;
if char_code<bc then bc:=char_code;
if min_m<left_pixel then abort('Pixels beyond left limit');
if max_m>right_pixel then abort('Pixels beyond right limit');
if min_n<bot_pixel then abort('Pixels beyond bottom limit');
if max_n>top_pixel then abort('Pixels beyond top limit');
@.Pixels beyond...limit@>
@<Clear the image@>;
n:=max_n;
m:=min_m;
paint_switch:=white;

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

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