Debian Codegraph @

texk/web2c/pktogf.web

raw
1 % This program is not copyrighted and can be used freely.
2 % PKtoGF.web
3 %
4 % PKtoGF creates a generic font file from a packed pixel file.
5 %
6 % Preliminary 0.0 version: January, 1988
7 % Fixed bug to include specials in character (1.0): January 1988
8 % Cleaned up description (bitweight errors) no version change: July 1990
9 % Fixed bug with empty character setting min_n to 1 (1.1): 19 October 1990
10 % A few typos, no version change: 22 April 2020, 27 October 2020
11 % Sorting order fixed, no version change: 02 September 2024
12 \def\versiondate{22 April 2020}
13 %
14 \font\ninerm=cmr9
15 \let\mc=\ninerm % medium caps for names like PASCAL
16 \font\logo=logo10 % font used for the METAFONT logo
17 \def\MF{{\logo META}\-{\logo FONT}}
18 \def\PASCAL{{\mc Pascal}}
19 \def\tamu{Texas A\char38 M}
20 \def\({} % this is used to make section names sort themselves better
21 \def\9#1{} % this is used for sort keys in the index
22 \def\title{PKtoGF}
23 \def\contentspagenumber{1}
24 \def\topofcontents{\null
25 \titlefalse % include headline on the contents page
26 \def\rheader{\mainfont\hfil \contentspagenumber}
27 \vfill
28 \centerline{\titlefont The {\ttitlefont PKtoGF} processor}
29 \vskip 15pt
30 \centerline{(Version 1.1, \versiondate)}
31 \vfill}
32 \def\botofcontents{\vfill
33 \centerline{\hsize 5in\baselineskip9pt
34 \vbox{\ninerm\noindent
35 The preparation of this report
36 was supported in part by the National Science
37 Foundation under grants IST-8201926 and MCS-8300984,
38 and by the System Development Foundation. `\TeX' is a
39 trademark of the American Mathematical Society.}}}
40 \pageno=\contentspagenumber \advance\pageno by 1
41
42 @* Introduction.
43 This program takes a packed, or \.{PK} file, and converts it into the
44 standard \.{GF} format. The resulting \.{GF} file is standard in
45 every way, and is essentially identical to the \.{GF} file from which
46 the \.{PK} file was produced in the first place. Note that, however,
47 \.{GF} to \.{PK} to \.{GF} is not an exact identity transformation, as
48 the new \.{GF} file will have a different preamble string and the actual
49 minimum bounding box will be used, instead of a possibly larger bounding
50 box in the original \.{GF} file.
51
52 @ The |banner| string defined here should be changed whenever \.{PKtoGF}
53 gets modified. You should update the preamble comment as well.
54
55 @d banner=='This is PKtoGF, Version 1.1'
56 {printed when the program starts}
57 @d preamble_comment=='PKtoGF 1.1 output'
58 @d comm_length==17
59
60 @ This program is written in standard \PASCAL, except where it is necessary
61 to use extensions; for example, \.{PKtoGF} must read files whose names
62 are dynamically specified, and that would be impossible in pure \PASCAL.
63
64 @d othercases == others: {default for cases not listed explicitly}
65 @d endcases == @+end {follows the default case in an extended |case| statement}
66 @f othercases == else
67 @f endcases == end
68
69 @ Both the input and output come from binary files. On line interaction
70 is handled through \PASCAL's standard |input| and |output| files.
71
72 @d print_ln(#)==write_ln(output,#)
73 @d print(#)==write(output,#)
74
75 @p program PKtoGF(input, output);
76 label @<Labels in the outer block@>@/
77 const @<Constants in the outer block@>@/
78 type @<Types in the outer block@>@/
79 var @<Globals in the outer block@>@/
80 procedure initialize; {this procedure gets things started properly}
81 var i:integer; {loop index for initializations}
82 begin print_ln(banner);@/
83 @<Set initial values@>@/
84 end;
85
86 @ If the program has to stop prematurely, it goes to the
87 `|final_end|'.
88
89 @d final_end=9999 {label for the end of it all}
90
91 @<Labels...@>=final_end;
92
93 @ These constants determine the maximum length of a file name and the length
94 of the terminal line, as well as the maximum number of run counts allowed
95 per line of the \.{GF} file. (We need this to implement repeat counts.)
96 @^system dependancies@>
97
98 @<Constants...@>=
99 @!name_length=80; {maximum length of a file name}
100 @!terminal_line_length=132; {maximum length of an input line}
101 @!max_counts=400; {maximum number of run counts in a raster line}
102
103 @ Here are some macros for common programming idioms.
104
105 @d incr(#) == #:=#+1 {increase a variable by unity}
106 @d decr(#) == #:=#-1 {decrease a variable by unity}
107 @d do_nothing == {empty statement}
108
109 @ It is possible that a malformed packed file (heaven forbid!) or some other
110 error might be detected by this program. Such errors might occur in a deeply
111 nested procedure, so the procedure called |jump_out| has been added to transfer
112 to the very end of the program with an error message.
113
114 @d abort(#)==begin print_ln(' ',#); jump_out; end
115
116 @p procedure jump_out;
117 begin goto final_end;
118 end;
119
120 @* The character set.
121 Like all programs written with the \.{WEB} system, \.{PKtoGF} can be
122 used with any character set. But it uses ASCII code internally, because
123 the programming for portable input-output is easier when a fixed internal
124 code is used.
125
126 The next few sections of \.{PKtoGF} have therefore been copied from the
127 analogous ones in the \.{WEB} system routines. They have been considerably
128 simplified, since \.{PKtoGF} need not deal with the controversial
129 ASCII codes less than @'40.
130
131 @<Types...@>=
132 @!ASCII_code=" ".."~"; {a subrange of the integers}
133
134 @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
135 character sets were common, so it did not make provision for lower case
136 letters. Nowadays, of course, we need to deal with both upper and lower case
137 alphabets in a convenient way, especially in a program like \.{GFtoPK}.
138 So we shall assume that the \PASCAL\ system being used for \.{GFtoPK}
139 has a character set containing at least the standard visible characters
140 of ASCII code (|"!"| through |"~"|).
141
142 Some \PASCAL\ compilers use the original name |char| for the data type
143 associated with the characters in text files, while other \PASCAL s
144 consider |char| to be a 64-element subrange of a larger data type that has
145 some other name. In order to accommodate this difference, we shall use
146 the name |text_char| to stand for the data type of the characters in the
147 output file. We shall also assume that |text_char| consists of
148 the elements |chr(first_text_char)| through |chr(last_text_char)|,
149 inclusive. The following definitions should be adjusted if necessary.
150 @^system dependencies@>
151
152 @d text_char == char {the data type of characters in text files}
153 @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
154 @d last_text_char=127 {ordinal number of the largest element of |text_char|}
155
156 @<Types...@>=
157 @!text_file=packed file of text_char;
158
159 @ The \.{GFtoPK} processor converts between ASCII code and
160 the user's external character set by means of arrays |xord| and |xchr|
161 that are analogous to \PASCAL's |ord| and |chr| functions.
162
163 @<Globals...@>=
164 @!xord: array [text_char] of ASCII_code;
165 {specifies conversion of input characters}
166 @!xchr: array [0..255] of text_char;
167 {specifies conversion of output characters}
168
169 @ Under our assumption that the visible characters of standard ASCII are
170 all present, the following assignment statements initialize the
171 |xchr| array properly, without needing any system-dependent changes.
172
173 @<Set init...@>=
174 for i:=0 to @'37 do xchr[i]:='?';
175 xchr[@'40]:=' ';
176 xchr[@'41]:='!';
177 xchr[@'42]:='"';
178 xchr[@'43]:='#';
179 xchr[@'44]:='$';
180 xchr[@'45]:='%';
181 xchr[@'46]:='&';
182 xchr[@'47]:='''';@/
183 xchr[@'50]:='(';
184 xchr[@'51]:=')';
185 xchr[@'52]:='*';
186 xchr[@'53]:='+';
187 xchr[@'54]:=',';
188 xchr[@'55]:='-';
189 xchr[@'56]:='.';
190 xchr[@'57]:='/';@/
191 xchr[@'60]:='0';
192 xchr[@'61]:='1';
193 xchr[@'62]:='2';
194 xchr[@'63]:='3';
195 xchr[@'64]:='4';
196 xchr[@'65]:='5';
197 xchr[@'66]:='6';
198 xchr[@'67]:='7';@/
199 xchr[@'70]:='8';
200 xchr[@'71]:='9';
201 xchr[@'72]:=':';
202 xchr[@'73]:=';';
203 xchr[@'74]:='<';
204 xchr[@'75]:='=';
205 xchr[@'76]:='>';
206 xchr[@'77]:='?';@/
207 xchr[@'100]:='@@';
208 xchr[@'101]:='A';
209 xchr[@'102]:='B';
210 xchr[@'103]:='C';
211 xchr[@'104]:='D';
212 xchr[@'105]:='E';
213 xchr[@'106]:='F';
214 xchr[@'107]:='G';@/
215 xchr[@'110]:='H';
216 xchr[@'111]:='I';
217 xchr[@'112]:='J';
218 xchr[@'113]:='K';
219 xchr[@'114]:='L';
220 xchr[@'115]:='M';
221 xchr[@'116]:='N';
222 xchr[@'117]:='O';@/
223 xchr[@'120]:='P';
224 xchr[@'121]:='Q';
225 xchr[@'122]:='R';
226 xchr[@'123]:='S';
227 xchr[@'124]:='T';
228 xchr[@'125]:='U';
229 xchr[@'126]:='V';
230 xchr[@'127]:='W';@/
231 xchr[@'130]:='X';
232 xchr[@'131]:='Y';
233 xchr[@'132]:='Z';
234 xchr[@'133]:='[';
235 xchr[@'134]:='\';
236 xchr[@'135]:=']';
237 xchr[@'136]:='^';
238 xchr[@'137]:='_';@/
239 xchr[@'140]:='`';
240 xchr[@'141]:='a';
241 xchr[@'142]:='b';
242 xchr[@'143]:='c';
243 xchr[@'144]:='d';
244 xchr[@'145]:='e';
245 xchr[@'146]:='f';
246 xchr[@'147]:='g';@/
247 xchr[@'150]:='h';
248 xchr[@'151]:='i';
249 xchr[@'152]:='j';
250 xchr[@'153]:='k';
251 xchr[@'154]:='l';
252 xchr[@'155]:='m';
253 xchr[@'156]:='n';
254 xchr[@'157]:='o';@/
255 xchr[@'160]:='p';
256 xchr[@'161]:='q';
257 xchr[@'162]:='r';
258 xchr[@'163]:='s';
259 xchr[@'164]:='t';
260 xchr[@'165]:='u';
261 xchr[@'166]:='v';
262 xchr[@'167]:='w';@/
263 xchr[@'170]:='x';
264 xchr[@'171]:='y';
265 xchr[@'172]:='z';
266 xchr[@'173]:='{';
267 xchr[@'174]:='|';
268 xchr[@'175]:='}';
269 xchr[@'176]:='~';
270 for i:=@'177 to 255 do xchr[i]:='?';
271
272 @ The following system-independent code makes the |xord| array contain a
273 suitable inverse to the information in |xchr|.
274
275 @<Set init...@>=
276 for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
277 for i:=" " to "~" do xord[xchr[i]]:=i;
278
279 @* Generic font file format.
280 The most important output produced by a typical run of \MF\ is the
281 ``generic font'' (\.{GF}) file that specifies the bit patterns of the
282 characters that have been drawn. The term {\sl generic\/} indicates that
283 this file format doesn't match the conventions of any name-brand manufacturer;
284 but it is easy to convert \.{GF} files to the special format required by
285 almost all digital phototypesetting equipment. There's a strong analogy
286 between the \.{DVI} files written by \TeX\ and the \.{GF} files written
287 by \MF; and, in fact, the file formats have a lot in common.
288
289 A \.{GF} file is a stream of 8-bit bytes that may be
290 regarded as a series of commands in a machine-like language. The first
291 byte of each command is the operation code, and this code is followed by
292 zero or more bytes that provide parameters to the command. The parameters
293 themselves may consist of several consecutive bytes; for example, the
294 `|boc|' (beginning of character) command has six parameters, each of
295 which is four bytes long. Parameters are usually regarded as nonnegative
296 integers; but four-byte-long parameters can be either positive or
297 negative, hence they range in value from $-2^{31}$ to $2^{31}-1$.
298 As in \.{TFM} files, numbers that occupy
299 more than one byte position appear in BigEndian order,
300 and negative numbers appear in two's complement notation.
301
302 A \.{GF} file consists of a ``preamble,'' followed by a sequence of one or
303 more ``characters,'' followed by a ``postamble.'' The preamble is simply a
304 |pre| command, with its parameters that introduce the file; this must come
305 first. Each ``character'' consists of a |boc| command, followed by any
306 number of other commands that specify ``black'' pixels,
307 followed by an |eoc| command. The characters appear in the order that \MF\
308 generated them. If we ignore no-op commands (which are allowed between any
309 two commands in the file), each |eoc| command is immediately followed by a
310 |boc| command, or by a |post| command; in the latter case, there are no
311 more characters in the file, and the remaining bytes form the postamble.
312 Further details about the postamble will be explained later.
313
314 Some parameters in \.{GF} commands are ``pointers.'' These are four-byte
315 quantities that give the location number of some other byte in the file;
316 the first file byte is number~0, then comes number~1, and so on.
317
318 @ The \.{GF} format is intended to be both compact and easily interpreted
319 by a machine. Compactness is achieved by making most of the information
320 relative instead of absolute. When a \.{GF}-reading program reads the
321 commands for a character, it keeps track of two quantities: (a)~the current
322 column number,~|m|; and (b)~the current row number,~|n|. These are 32-bit
323 signed integers, although most actual font formats produced from \.{GF}
324 files will need to curtail this vast range because of practical
325 limitations. (\MF\ output will never allow $\vert m\vert$ or $\vert
326 n\vert$ to get extremely large, but the \.{GF} format tries to be more
327 general.)
328
329 How do \.{GF}'s row and column numbers correspond to the conventions
330 of \TeX\ and \MF? Well, the ``reference point'' of a character, in \TeX's
331 view, is considered to be at the lower left corner of the pixel in row~0
332 and column~0. This point is the intersection of the baseline with the left
333 edge of the type; it corresponds to location $(0,0)$ in \MF\ programs.
334 Thus the pixel in \.{GF} row~0 and column~0 is \MF's unit square, comprising
335 the region of the plane whose coordinates both lie between 0 and~1. The
336 pixel in \.{GF} row~|n| and column~|m| consists of the points whose \MF\
337 coordinates |(x,y)| satisfy |m<=x<=m+1| and |n<=y<=n+1|. Negative values of
338 |m| and~|x| correspond to columns of pixels {\sl left\/} of the reference
339 point; negative values of |n| and~|y| correspond to rows of pixels {\sl
340 below\/} the baseline.
341
342 Besides |m| and |n|, there's also a third aspect of the current
343 state, namely the @!|paint_switch|, which is always either \\{black} or
344 \\{white}. Each \\{paint} command advances |m| by a specified amount~|d|,
345 and blackens the intervening pixels if |paint_switch=black|; then
346 the |paint_switch| changes to the opposite state. \.{GF}'s commands are
347 designed so that |m| will never decrease within a row, and |n| will never
348 increase within a character; hence there is no way to whiten a pixel that
349 has been blackened.
350
351 @ Here is a list of all the commands that may appear in a \.{GF} file. Each
352 command is specified by its symbolic name (e.g., |boc|), its opcode byte
353 (e.g., 67), and its parameters (if any). The parameters are followed
354 by a bracketed number telling how many bytes they occupy; for example,
355 `|d[2]|' means that parameter |d| is two bytes long.
356
357 \yskip\hang|paint_0| 0. This is a \\{paint} command with |d=0|; it does
358 nothing but change the |paint_switch| from \\{black} to \\{white} or
359 vice~versa.
360
361 \yskip\hang\\{paint\_1} through \\{paint\_63} (opcodes 1 to 63).
362 These are \\{paint} commands with |d=1| to~63, defined as follows: If
363 |paint_switch=black|, blacken |d|~pixels of the current row~|n|,
364 in columns |m| through |m+d-1| inclusive. Then, in any case,
365 complement the |paint_switch| and advance |m| by~|d|.
366
367 \yskip\hang|paint1| 64 |d[1]|. This is a \\{paint} command with a specified
368 value of~|d|; \MF\ uses it to paint when |64<=d<256|.
369
370 \yskip\hang|@!paint2| 65 |d[2]|. Same as |paint1|, but |d|~can be as high
371 as~65535.
372
373 \yskip\hang|@!paint3| 66 |d[3]|. Same as |paint1|, but |d|~can be as high
374 as $2^{24}-1$. \MF\ never needs this command, and it is hard to imagine
375 anybody making practical use of it; surely a more compact encoding will be
376 desirable when characters can be this large. But the command is there,
377 anyway, just in case.
378
379 \yskip\hang|boc| 67 |c[4]| |p[4]| |min_m[4]| |max_m[4]| |min_n[4]|
380 |max_n[4]|. Beginning of a character: Here |c| is the character code, and
381 |p| points to the previous character beginning (if any) for characters having
382 this code number modulo 256. (The pointer |p| is |-1| if there was no
383 prior character with an equivalent code.) The values of registers |m| and |n|
384 defined by the instructions that follow for this character must
385 satisfy |min_m<=m<=max_m| and |min_n<=n<=max_n|. (The values of |max_m| and
386 |min_n| need not be the tightest bounds possible.) When a \.{GF}-reading
387 program sees a |boc|, it can use |min_m|, |max_m|, |min_n|, and |max_n| to
388 initialize the bounds of an array. Then it sets |m:=min_m|, |n:=max_n|, and
389 |paint_switch:=white|.
390
391 \yskip\hang|boc1| 68 |c[1]| |@!del_m[1]| |max_m[1]| |@!del_n[1]| |max_n[1]|.
392 Same as |boc|, but |p| is assumed to be~$-1$; also |del_m=max_m-min_m|
393 and |del_n=max_n-min_n| are given instead of |min_m| and |min_n|.
394 The one-byte parameters must be between 0 and 255, inclusive.
395 \ (This abbreviated |boc| saves 19~bytes per character, in common cases.)
396
397 \yskip\hang|eoc| 69. End of character: All pixels blackened so far
398 constitute the pattern for this character. In particular, a completely
399 blank character might have |eoc| immediately following |boc|.
400
401 \yskip\hang|skip0| 70. Decrease |n| by 1 and set |m:=min_m|,
402 |paint_switch:=white|. \ (This finishes one row and begins another,
403 ready to whiten the leftmost pixel in the new row.)
404
405 \yskip\hang|skip1| 71 |d[1]|. Decrease |n| by |d+1|, set |m:=min_m|, and set
406 |paint_switch:=white|. This is a way to produce |d| all-white rows.
407
408 \yskip\hang|@!skip2| 72 |d[2]|. Same as |skip1|, but |d| can be as large
409 as 65535.
410
411 \yskip\hang|@!skip3| 73 |d[3]|. Same as |skip1|, but |d| can be as large
412 as $2^{24}-1$. \MF\ obviously never needs this command.
413
414 \yskip\hang|new_row_0| 74. Decrease |n| by 1 and set |m:=min_m|,
415 |paint_switch:=black|. \ (This finishes one row and begins another,
416 ready to {\sl blacken\/} the leftmost pixel in the new row.)
417
418 \yskip\hang|@!new_row_1| through |@!new_row_164| (opcodes 75 to 238). Same as
419 |new_row_0|, but with |m:=min_m+1| through |min_m+164|, respectively.
420
421 \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
422 general; it functions as a $(k+2)$-byte |no_op| unless special \.{GF}-reading
423 programs are being used. \MF\ generates \\{xxx} commands when encountering
424 a \&{special} string; this occurs in the \.{GF} file only between
425 characters, after the preamble, and before the postamble. However,
426 \\{xxx} commands might appear anywhere in \.{GF} files generated by other
427 processors. It is recommended that |x| be a string having the form of a
428 keyword followed by possible parameters relevant to that keyword.
429
430 \yskip\hang|@!xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
431
432 \yskip\hang|xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
433 \MF\ uses this when sending a \&{special} string whose length exceeds~255.
434
435 \yskip\hang|@!xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be
436 ridiculously large; |k| mustn't be negative.
437
438 \yskip\hang|yyy| 243 |y[4]|. This command is undefined in general;
439 it functions as a 5-byte |no_op| unless special \.{GF}-reading programs
440 are being used. \MF\ puts |scaled| numbers into |yyy|'s, as a
441 result of \&{numspecial} commands; the intent is to provide numeric
442 parameters to \\{xxx} commands that immediately precede.
443
444 \yskip\hang|no_op| 244. No operation, do nothing. Any number of |no_op|'s
445 may occur between \.{GF} commands, but a |no_op| cannot be inserted between
446 a command and its parameters or between two parameters.
447
448 \yskip\hang|char_loc| 245 |c[1]| |dx[4]| |dy[4]| |w[4]| |p[4]|.
449 This command will appear only in the postamble, which will be explained
450 shortly.
451
452 \yskip\hang|@!char_loc0| 246 |c[1]| |@!dm[1]| |w[4]| |p[4]|.
453 Same as |char_loc|, except that |dy| is assumed to be zero, and the value
454 of~|dx| is taken to be |65536*dm|, where |0<=dm<256|.
455
456 \yskip\hang|pre| 247 |i[1]| |k[1]| |x[k]|.
457 Beginning of the preamble; this must come at the very beginning of the
458 file. Parameter |i| is an identifying number for \.{GF} format, currently
459 131. The other information is merely commentary; it is not given
460 special interpretation like \\{xxx} commands are. (Note that \\{xxx}
461 commands may immediately follow the preamble, before the first |boc|.)
462
463 \yskip\hang|post| 248. Beginning of the postamble, see below.
464
465 \yskip\hang|post_post| 249. Ending of the postamble, see below.
466
467 \yskip\noindent Commands 250--255 are undefined at the present time.
468
469 @d gf_id_byte=131 {identifies the kind of \.{GF} files described here}
470
471 @ Here are the opcodes that \.{GFtoPK} actually refers to.
472
473 @d paint_0=0 {beginning of the \\{paint} commands}
474 @d paint1=64 {move right a given number of columns, then
475 black${}\leftrightarrow{}$white}
476 @d boc=67 {beginning of a character}
477 @d boc1=68 {abbreviated |boc|}
478 @d eoc=69 {end of a character}
479 @d skip0=70 {skip no blank rows}
480 @d skip1=71 {skip over blank rows}
481 @d new_row_0=74 {move down one row and then right}
482 @d max_new_row=238 {move down one row and then right}
483 @d no_op=247 {noop}
484 @d xxx1=239 {for \&{special} strings}
485 @d yyy=243 {for \&{numspecial} numbers}
486 @d nop=244 {no operation}
487 @d char_loc=245 {character locators in the postamble}
488 @d char_loc0=246 {character locators in the postamble}
489 @d pre=247 {preamble}
490 @d post=248 {postamble beginning}
491 @d post_post=249 {postamble ending}
492 @d undefined_commands==250,251,252,253,254,255
493
494 @ The last character in a \.{GF} file is followed by `|post|'; this command
495 introduces the postamble, which summarizes important facts that \MF\ has
496 accumulated. The postamble has the form
497 $$\vbox{\halign{\hbox{#\hfil}\cr
498 |post| |p[4]| |@!ds[4]| |@!cs[4]| |@!hppp[4]| |@!vppp[4]|
499 |@!min_m[4]| |@!max_m[4]| |@!min_n[4]| |@!max_n[4]|\cr
500 $\langle\,$character locators$\,\rangle$\cr
501 |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
502 Here |p| is a pointer to the byte following the final |eoc| in the file
503 (or to the byte following the preamble, if there are no characters);
504 it can be used to locate the beginning of \\{xxx} commands
505 that might have preceded the postamble. The |ds| and |cs| parameters
506 @^design size@> @^check sum@>
507 give the design size and check sum, respectively, which are exactly the
508 values put into the header of any \.{TFM} file that shares information with
509 this \.{GF} file. Parameters |hppp| and |vppp| are the ratios of
510 pixels per point, horizontally and vertically, expressed as |scaled| integers
511 (i.e., multiplied by $2^{16}$); they can be used to correlate the font
512 with specific device resolutions, magnifications, and ``at sizes.'' Then
513 come |min_m|, |max_m|, |min_n|, and |max_n|, which bound the values that
514 registers |m| and~|n| assume in all characters in this \.{GF} file.
515 (These bounds need not be the best possible; |max_m| and |min_n| may, on the
516 other hand, be tighter than the similar bounds in |boc| commands. For
517 example, some character may have |min_n=-100| in its |boc|, but it might
518 turn out that |n| never gets lower than |-50| in any character; then
519 |min_n| can have any value |<=-50|. If there are no characters in the file,
520 it's possible to have |min_m>max_m| and/or |min_n>max_n|.)
521
522 @ Character locators are introduced by |char_loc| commands,
523 which specify a character residue~|c|, character escapements (|dx,dy|),
524 a character width~|w|, and a pointer~|p|
525 to the beginning of that character. (If two or more characters have the
526 same code~|c| modulo 256, only the last will be indicated; the others can be
527 located by following backpointers. Characters whose codes differ by a
528 multiple of 256 are assumed to share the same font metric information,
529 hence the \.{TFM} file contains only residues of character codes modulo~256.
530 This convention is intended for oriental languages, when there are many
531 character shapes but few distinct widths.)
532 @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
533
534 The character escapements (|dx,dy|) are the values of \MF's \&{chardx}
535 and \&{chardy} parameters; they are in units of |scaled| pixels;
536 i.e., |dx| is in horizontal pixel units times $2^{16}$, and |dy| is in
537 vertical pixel units times $2^{16}$. This is the intended amount of
538 displacement after typesetting the character; for \.{DVI} files, |dy|
539 should be zero, but other document file formats allow nonzero vertical
540 escapement.
541
542 The character width~|w| duplicates the information in the \.{TFM} file; it
543 is $2^{20}$ times the ratio of the true width to the font's design size.
544
545 The backpointer |p| points to the character's |boc|, or to the first of
546 a sequence of consecutive \\{xxx} or |yyy| or |no_op| commands that
547 immediately precede the |boc|, if such commands exist; such ``special''
548 commands essentially belong to the characters, while the special commands
549 after the final character belong to the postamble (i.e., to the font
550 as a whole). This convention about |p| applies also to the backpointers
551 in |boc| commands, even though it wasn't explained in the description
552 of~|boc|. @^backpointers@>
553
554 Pointer |p| might be |-1| if the character exists in the \.{TFM} file
555 but not in the \.{GF} file. This unusual situation can arise in \MF\ output
556 if the user had |proofing<0| when the character was being shipped out,
557 but then made |proofing>=0| in order to get a \.{GF} file.
558
559 @ The last part of the postamble, following the |post_post| byte that
560 signifies the end of the character locators, contains |q|, a pointer to the
561 |post| command that started the postamble. An identification byte, |i|,
562 comes next; this currently equals~131, as in the preamble.
563
564 The |i| byte is followed by four or more bytes that are all equal to
565 the decimal number 223 (i.e., @'337 in octal). \MF\ puts out four to seven of
566 these trailing bytes, until the total length of the file is a multiple of
567 four bytes, since this works out best on machines that pack four bytes per
568 word; but any number of 223's is allowed, as long as there are at least four
569 of them. In effect, 223 is a sort of signature that is added at the very end.
570 @^Fuchs, David Raymond@>
571
572 This curious way to finish off a \.{GF} file makes it feasible for
573 \.{GF}-reading programs to find the postamble first, on most computers,
574 even though \MF\ wants to write the postamble last. Most operating
575 systems permit random access to individual words or bytes of a file, so
576 the \.{GF} reader can start at the end and skip backwards over the 223's
577 until finding the identification byte. Then it can back up four bytes, read
578 |q|, and move to byte |q| of the file. This byte should, of course,
579 contain the value 248 (|post|); now the postamble can be read, so the
580 \.{GF} reader can discover all the information needed for individual
581 characters.
582
583 Unfortunately, however, standard \PASCAL\ does not include the ability to
584 @^system dependencies@>
585 access a random position in a file, or even to determine the length of a file.
586 Almost all systems nowadays provide the necessary capabilities, so \.{GF}
587 format has been designed to work most efficiently with modern operating
588 systems. \.{GFtoPK} first reads the postamble, and then scans the file from
589 front to back.
590
591 @* Packed file format.
592 The packed file format is a compact representation of the data contained in a
593 \.{GF} file. The information content is the same, but packed (\.{PK}) files
594 are almost always less than half the size of their \.{GF} counterparts. They
595 are also easier to convert into a raster representation because they do not
596 have a profusion of \\{paint}, \\{skip}, and \\{new\_row} commands to be
597 separately interpreted. In addition, the \.{PK} format expressly forbids
598 \&{special} commands within a character. The minimum bounding box for each
599 character is explicit in the format, and does not need to be scanned for as in
600 the \.{GF} format. Finally, the width and escapement values are combined with
601 the raster information into character ``packets'', making it simpler in many
602 cases to process a character.
603
604 A \.{PK} file is organized as a stream of 8-bit bytes. At times, these bytes
605 might be split into 4-bit nybbles or single bits, or combined into multiple
606 byte parameters. When bytes are split into smaller pieces, the `first' piece
607 is always the most significant of the byte. For instance, the first bit of
608 a byte is the bit with value 128; the first nybble can be found by dividing
609 a byte by 16. Similarly, when bytes are combined into multiple byte
610 parameters, the first byte is the most significant of the parameter. If the
611 parameter is signed, it is represented by two's-complement notation.
612
613 The set of possible eight-bit values are separated into two sets, those that
614 introduce a character definition, and those that do not. The values that
615 introduce a character definition comprise the range from 0 to 239; byte values
616 above 239 are interpreted commands. Bytes which introduce character
617 definitions are called flag bytes, and various fields within the byte indicate
618 various things about how the character definition is encoded. Command bytes
619 have zero or more parameters, and can never appear within a character
620 definition or between parameters of another command, where they would be
621 interpreted as data.
622
623 A \.{PK} file consists of a preamble, followed by a sequence of one or more
624 character definitions, followed by a postamble. The preamble command must
625 be the first byte in the file, followed immediately by its parameters.
626 Any number of character definitions may follow, and any command but the
627 preamble command and the postamble command may occur between character
628 definitions. The very last command in the file must be the postamble.
629
630 @ The packed file format is intended to be easy to read and interpret by
631 device drivers. The small size of the file reduces the input/output overhead
632 each time a font is defined. For those drivers that load and save each font
633 file into memory, the small size also helps reduce the memory requirements.
634 The length of each character packet is specified, allowing the character raster
635 data to be loaded into memory by simply counting bytes, rather than
636 interpreting each command; then, each character can be interpreted on a demand
637 basis. This also makes it possible for a driver to skip a particular
638 character quickly if it knows that the character is unused.
639
640 @ First, the command bytes shall be presented; then the format of the
641 character definitions will be defined. Eight of the possible sixteen
642 commands (values 240 through 255) are currently defined; the others are
643 reserved for future extensions. The commands are listed below. Each command
644 is specified by its symbolic name (e.g., \\{pk\_no\_op}), its opcode byte,
645 and any parameters. The parameters are followed by a bracketed number
646 telling how many bytes they occupy, with the number preceded by a plus sign if
647 it is a signed quantity. (Four byte quantities are always signed, however.)
648
649 \yskip\hang|pk_xxx1| 240 |k[1]| |x[k]|. This command is undefined in general;
650 it functions as a $(k+2)$-byte \\{no\_op} unless special \.{PK}-reading
651 programs are being used. \MF\ generates \\{xxx} commands when encountering
652 a \&{special} string. It is recommended that |x| be a string having the form
653 of a keyword followed by possible parameters relevant to that keyword.
654
655 \yskip\hang\\{pk\_xxx2} 241 |k[2]| |x[k]|. Like |pk_xxx1|, but |0<=k<65536|.
656
657 \yskip\hang\\{pk\_xxx3} 242 |k[3]| |x[k]|. Like |pk_xxx1|, but
658 |0<=k<@t$2^{24}$@>|. \MF\ uses this when sending a \&{special} string whose
659 length exceeds~255.
660
661 \yskip\hang\\{pk\_xxx4} 243 |k[4]| |x[k]|. Like |pk_xxx1|, but |k| can be
662 ridiculously large; |k| mustn't be negative.
663
664 \yskip\hang|pk_yyy| 244 |y[4]|. This command is undefined in general; it
665 functions as a five-byte \\{no\_op} unless special \.{PK} reading programs
666 are being used. \MF\ puts |scaled| numbers into |yyy|'s, as a result of
667 \&{numspecial} commands; the intent is to provide numeric parameters to
668 \\{xxx} commands that immediately precede.
669
670 \yskip\hang|pk_post| 245. Beginning of the postamble. This command is
671 followed by enough |pk_no_op| commands to make the file a multiple
672 of four bytes long. Zero through three bytes are usual, but any number
673 is allowed.
674 This should make the file easy to read on machines which pack four bytes to
675 a word.
676
677 \yskip\hang|pk_no_op| 246. No operation, do nothing. Any number of
678 |pk_no_op|'s may appear between \.{PK} commands, but a |pk_no_op| cannot be
679 inserted between a command and its parameters, between two parameters, or
680 inside a character definition.
681
682 \yskip\hang|pk_pre| 247 |i[1]| |k[1]| |x[k]| |ds[4]| |cs[4]| |hppp[4]|
683 |vppp[4]|. Preamble command. Here, |i| is the identification byte of the
684 file, currently equal to 89. The string |x| is merely a comment, usually
685 indicating the source of the \.{PK} file. The parameters |ds| and |cs| are
686 the design size of the file in $1/2^{20}$ points, and the checksum of the
687 file, respectively. The checksum should match the \.{TFM} file and the
688 \.{GF} files for this font. Parameters |hppp| and |vppp| are the ratios
689 of pixels per point, horizontally and vertically, multiplied by $2^{16}$; they
690 can be used to correlate the font with specific device resolutions,
691 magnifications, and ``at sizes''. Usually, the name of the \.{PK} file is
692 formed by concatenating the font name (e.g., cmr10) with the resolution at
693 which the font is prepared in pixels per inch multiplied by the magnification
694 factor, and the letters \.{PK}. For instance, cmr10 at 300 dots per inch
695 should be named CMR10.300PK; at one thousand dots per inch and magstephalf,
696 it should be named CMR10.1095PK.
697
698 @ We put a few of the above opcodes into definitions for symbolic use by
699 this program.
700
701 @d pk_id = 89 {the version of \.{PK} file described}
702 @d pk_xxx1 = 240 {\&{special} commands}
703 @d pk_yyy = 244 {\&{numspecial} commands}
704 @d pk_post = 245 {postamble}
705 @d pk_no_op = 246 {no operation}
706 @d pk_pre = 247 {preamble}
707
708 @ The \.{PK} format has two conflicting goals; to pack character raster and
709 size information as compactly as possible, while retaining ease of translation
710 into raster and other forms. A suitable compromise was found in the use of
711 run-encoding of the raster information. Instead of packing the individual
712 bits of the character, we instead count the number of consecutive `black' or
713 `white' pixels in a horizontal raster row, and then encode this number. Run
714 counts are found for each row, from the top of the character to the bottom.
715 This is essentially the way the \.{GF} format works.
716 Instead of presenting each row individually, however, let us concatenate all
717 of the horizontal raster rows into one long string of pixels, and encode this
718 row. With knowledge of the width of the bit-map, the original character glyph
719 can be easily reconstructed. In addition, we do not need special commands to
720 mark the end of one row and the beginning of the next.
721
722 Next, let us put the burden of finding the minimum bounding box on the part
723 of the font generator, since the characters will usually be used much more
724 often than they are generated. The minimum bounding box is the smallest
725 rectangle which encloses all `black' pixels of a character. Let us also
726 eliminate the need for a special end of character marker, by supplying
727 exactly as many bits as are required to fill the minimum bounding box, from
728 which the end of the character is implicit.
729
730 Let us next consider the distribution of the run counts. Analysis of several
731 dozen pixel files at 300 dots per inch yields a distribution peaking at four,
732 falling off slowly until ten, then a bit more steeply until twenty, and then
733 asymptotically approaching the horizontal. Thus, the great majority of our
734 run counts will fit in a four-bit nybble. The eight-bit byte is attractive for
735 our run-counts, as it is the standard on many systems; however, the wasted four
736 bits in the majority of cases seems a high price to pay. Another possibility
737 is to use a Huffman-type encoding scheme with a variable number of bits for
738 each run-count; this was rejected because of the overhead in fetching and
739 examining individual bits in the file. Thus, the character raster definitions
740 in the \.{PK} file format are based on the four-bit nybble.
741
742 @ The analysis of the pixel files yielded another interesting statistic: fully
743 37\char`\%\
744 of the raster rows were duplicates of the previous row. Thus, the \.{PK}
745 format allows the specification of repeat counts, which indicate how many times
746 a horizontal raster row is to be repeated. These repeated rows are taken out
747 of the character glyph before individual rows are concatenated into the long
748 string of pixels.
749
750 For elegance, we disallow a run count of zero. The case of a null raster
751 description should be gleaned from the character width and height being equal
752 to zero, and no raster data should be read. No other zero counts are ever
753 necessary. Also, in the absence of repeat counts, the repeat value is set to
754 be zero (only the original row is sent.) If a repeat count is seen, it takes
755 effect on the current row. The current row is defined as the row on which the
756 first pixel of the next run count will lie. The repeat count is set back to
757 zero when the last pixel in the current row is seen, and the row is sent out.
758
759 This poses a problem for entirely black and entirely white rows, however. Let
760 us say that the current row ends with four white pixels, and then we have five
761 entirely empty rows, followed by a black pixel at the beginning of the next
762 row, and the character width is ten pixels. We would like to use a repeat
763 count, but there is no legal place to put it. If we put it before the white
764 run count, it will apply to the current row. If we put it after, it applies
765 to the row with the black pixel at the beginning. Thus, entirely white or
766 entirely black repeated rows are always packed as large run counts (in this
767 case, a white run count of 54) rather than repeat counts.
768
769 @ Now let us turn our attention to the actual packing of the run counts and
770 repeat counts into nybbles. There are only sixteen possible nybble values.
771 We need to indicate run counts and repeat counts. Since the run counts are
772 much more common, we will devote the majority of the nybble values to them.
773 We therefore indicate a repeat count by a nybble of 14 followed by a packed
774 number, where a packed number will be explained later. Since the repeat
775 count value of one is so common, we indicate a repeat one command by a single
776 nybble of 15. A 14 followed by the packed number 1 is still legal for a
777 repeat one count, however. The run counts are coded directly as packed
778 numbers.
779
780 For packed numbers, therefore, we have the nybble values 0 through 13. We
781 need to represent the positive integers up to, say, $2^{31}-1$. We would
782 like the more common smaller numbers to take only one or two nybbles, and
783 the infrequent large numbers to take three or more. We could therefore
784 allocate one nybble value to indicate a large run count taking three or more
785 nybbles. We do this with the value 0.
786
787 @ We are left with the values 1 through 13. We can allocate some of these, say
788 |dyn_f|, to be one-nybble run counts.
789 These will work for the run counts |1..dyn_f|. For subsequent run
790 counts, we will use a nybble greater than |dyn_f|, followed by a second nybble,
791 whose value can run from 0 through 15. Thus, the two-byte nybble values will
792 run from |dyn_f+1..(13-dyn_f)*16+dyn_f|. We have our definition of large run
793 count values now, being all counts greater than |(13-dyn_f)*16+dyn_f|.
794
795 We can analyze our several dozen pixel files and determine an optimal value of
796 |dyn_f|, and use this value for all of the characters. Unfortunately, values
797 of |dyn_f| that pack small characters well tend to pack the large characters
798 poorly, and values that pack large characters well are not efficient for the
799 smaller characters. Thus, we choose the optimal |dyn_f| on a character basis,
800 picking the value which will pack each individual character in the smallest
801 number of nybbles. Legal values of |dyn_f| run from 0 (with no one-byte run
802 counts) to 13 (with no two-byte run counts).
803
804 @ Our only remaining task in the coding of packed numbers is the large run
805 counts. We use a scheme suggested by D.~E.~Knuth
806 @^Knuth, D.~E.@>
807 which will simply and elegantly represent arbitrarily large values. The
808 general scheme to represent an integer |i| is to write its hexadecimal
809 representation, with leading zeros removed. Then we count the number of
810 digits, and prepend one less than that many zeros before the hexadecimal
811 representation. Thus, the values from one to fifteen occupy one nybble;
812 the values sixteen through 255 occupy three, the values 256 through 4095
813 require five, etc.
814
815 For our purposes, however, we have already represented the numbers one
816 through |(13-dyn_f)*16+dyn_f|. In addition, the one-nybble values have
817 already been taken by our other commands, which means that only the values
818 from sixteen up are available to us for long run counts. Thus, we simply
819 normalize our long run counts, by subtracting |(13-dyn_f)*16+dyn_f+1| and
820 adding 16, and then representing the result according to the scheme above.
821
822 @ The final algorithm for decoding the run counts based on the above scheme
823 might look like this, assuming a procedure called \\{pk\_nyb} is available
824 to get the next nybble from the file, and assuming that the global
825 |repeat_count| indicates whether a row needs to be repeated. Note that this
826 routine is recursive, but since a repeat count can never directly follow
827 another repeat count, it can only be recursive to one level.
828
829 @<Packed number procedure@>=
830 function pk_packed_num : integer ;
831 var i, j, k : integer ;
832 begin
833 i := get_nyb ;
834 if i = 0 then begin
835 repeat j := get_nyb ; incr(i) ; until j <> 0 ;
836 while i > 0 do begin j := j * 16 + get_nyb ; decr(i) ; end ;
837 pk_packed_num := j - 15 + (13-dyn_f)*16 + dyn_f ;
838 end else if i <= dyn_f then
839 pk_packed_num := i
840 else if i < 14 then
841 pk_packed_num := (i-dyn_f-1)*16+get_nyb+dyn_f+1
842 else begin
843 if i = 14 then
844 repeat_count := pk_packed_num
845 else
846 repeat_count := 1 ;
847 pk_packed_num := pk_packed_num ;
848 end ;
849 end ;
850
851 @ For low resolution fonts, or characters with `gray' areas, run encoding can
852 often make the character many times larger. Therefore, for those characters
853 that cannot be encoded efficiently with run counts, the \.{PK} format allows
854 bit-mapping of the characters. This is indicated by a |dyn_f| value of
855 14. The bits are packed tightly, by concatenating all of the horizontal raster
856 rows into one long string, and then packing this string eight bits to a byte.
857 The number of bytes required can be calculated by |(width*height+7) div 8|.
858 This format should only be used when packing the character by run counts takes
859 more bytes than this, although, of course, it is legal for any character.
860 Any extra bits in the last byte should be set to zero.
861
862 @ At this point, we are ready to introduce the format for a character
863 descriptor. It consists of three parts: a flag byte, a character preamble,
864 and the raster data. The most significant four bits of the flag byte
865 yield the |dyn_f| value for that character. (Notice that only values of
866 0 through 14 are legal for |dyn_f|, with 14 indicating a bit mapped character;
867 thus, the flag bytes do not conflict with the command bytes, whose upper nybble
868 is always 15.) The next bit (with weight 8) indicates whether the first run
869 count is a black count or a white count, with a one indicating a black count.
870 For bit-mapped characters, this bit should be set to a zero. The next bit
871 (with weight 4) indicates whether certain later parameters (referred to as size
872 parameters) are given in one-byte or two-byte quantities, with a one indicating
873 that they are in two-byte quantities. The last two bits are concatenated on to
874 the beginning of the length parameter in the character preamble, which will be
875 explained below.
876
877 However, if the last three bits of the flag byte are all set (normally
878 indicating that the size parameters are two-byte values and that a 3 should be
879 prepended to the length parameter), then a long format of the character
880 preamble should be used instead of one of the short forms.
881
882 Therefore, there are three formats for the character preamble, and which one
883 is used depends on the least significant three bits of the flag byte. If the
884 least significant three bits are in the range zero through three, the short
885 format is used. If they are in the range four through six, the extended short
886 format is used. Otherwise, if the least significant bits are all set, then
887 the long form of the character preamble is used. The preamble formats are
888 explained below.
889
890 \yskip\hang Short form: |flag[1]| |pl[1]| |cc[1]| |tfm[3]| |dm[1]| |w[1]|
891 |h[1]| |hoff[+1]| |voff[+1]|.
892 If this format of the character preamble is used, the above
893 parameters must all fit in the indicated number of bytes, signed or unsigned
894 as indicated. Almost all of the standard \TeX\ font characters fit; the few
895 exceptions are fonts such as \.{aminch}.
896
897 \yskip\hang Extended short form: |flag[1]| |pl[2]| |cc[1]| |tfm[3]| |dm[2]|
898 |w[2]| |h[2]| |hoff[+2]| |voff[+2]|. Larger characters use this extended
899 format.
900
901 \yskip\hang Long form: |flag[1]| |pl[4]| |cc[4]| |tfm[4]| |dx[4]| |dy[4]|
902 |w[4]| |h[4]| |hoff[4]| |voff[4]|. This is the general format which
903 allows all of the
904 parameters of the \.{GF} file format, including vertical escapement.
905 \vskip\baselineskip
906 The |flag| parameter is the flag byte. The parameter |pl| (packet length)
907 contains the offset
908 of the byte following this character descriptor, with respect to the beginning
909 of the |tfm| width parameter. This is given so a \.{PK} reading program can,
910 once it has read the flag byte, packet length, and character code (|cc|), skip
911 over the character by simply reading this many more bytes. For the two short
912 forms of the character preamble, the last two bits of the flag byte should be
913 considered the two most-significant bits of the packet length. For the short
914 format, the true packet length might be calculated as |(flag mod 4)*256+pl|;
915 for the extended format, it might be calculated as |(flag mod 4)*65536+pl|.
916
917 The |w| parameter is the width and the |h| parameter is the height in pixels
918 of the minimum bounding box. The |dx| and |dy| parameters are the horizontal
919 and vertical escapements, respectively. In the short formats, |dy| is assumed
920 to be zero and |dm| is |dy| but in pixels;
921 in the long format, |dx| and |dy| are both
922 in pixels multiplied by $2^{16}$. The |hoff| is the horizontal offset from the
923 upper left pixel to the reference pixel; the |voff| is the vertical offset.
924 They are both given in pixels, with right and down being positive. The
925 reference pixel is the pixel which occupies the unit square in \MF; the
926 \MF\ reference point is the lower left hand corner of this pixel. (See the
927 example below.)
928
929 @ \TeX\ requires that all characters which have the same character codes
930 modulo 256 also have the same |tfm| widths, and escapement values. The \.{PK}
931 format does not itself make this a requirement, but in order for the font to
932 work correctly with the \TeX\ software, this constraint should be observed.
933
934
935 Following the character preamble is the raster information for the
936 character, packed by run counts or by bits, as indicated by the flag byte.
937 If the character is packed by run counts and the required number of nybbles
938 is odd, then the last byte of the raster description should have a zero
939 for its least significant nybble.
940
941 @ As an illustration of the \.{PK} format, the character \char4\ from the font
942 amr10 at 300 dots per inch will be encoded. (Note: amr fonts are obsolete,
943 and the reference to this character is retained from an older version of
944 the Computer Modern fonts solely for illustration.) This character was chosen
945 because it illustrates some
946 of the borderline cases. The raster for the character looks like this (the
947 row numbers are chosen for convenience, and are not \MF's row numbers.)
948
949 \vskip\baselineskip
950 \centerline{\vbox{\baselineskip=10pt
951 \halign{\hfil#\quad&&\hfil#\hfil\cr
952 0& & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
953 1& & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
954 2& & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
955 3& & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
956 4& & &M&M& & & & & & & & & & & & & & & & &M&M\cr
957 5& & &M&M& & & & & & & & & & & & & & & & &M&M\cr
958 6& & &M&M& & & & & & & & & & & & & & & & &M&M\cr
959 7\cr
960 8\cr
961 9& & & & &M&M& & & & & & & & & & & & &M&M& & \cr
962 10& & & & &M&M& & & & & & & & & & & & &M&M& & \cr
963 11& & & & &M&M& & & & & & & & & & & & &M&M& & \cr
964 12& & & & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M& & \cr
965 13& & & & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M& & \cr
966 14& & & & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M& & \cr
967 15& & & & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M& & \cr
968 16& & & & &M&M& & & & & & & & & & & & &M&M& & \cr
969 17& & & & &M&M& & & & & & & & & & & & &M&M& & \cr
970 18& & & & &M&M& & & & & & & & & & & & &M&M& & \cr
971 19\cr
972 20\cr
973 21\cr
974 22& & &M&M& & & & & & & & & & & & & & & & &M&M\cr
975 23& & &M&M& & & & & & & & & & & & & & & & &M&M\cr
976 24& & &M&M& & & & & & & & & & & & & & & & &M&M\cr
977 25& & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
978 26& & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
979 27& & &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
980 28&*& &M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M&M\cr
981 &\hphantom{M}&\hphantom{M}\cr
982 }}}
983 The width of the minimum bounding box for this character is 20; its height
984 is 29. The `*' represents the reference pixel; notice how it lies outside the
985 minimum bounding box. The |hoff| value is $-2$, and the |voff| is~28.
986
987 The first task is to calculate the run counts and repeat counts. The repeat
988 counts are placed at the first transition (black to white or white to black)
989 in a row, and are enclosed in brackets. White counts are enclosed in
990 parentheses. It is relatively easy to generate the counts list:
991 \vskip\baselineskip
992 \centerline{82 [2] (16) 2 (42) [2] 2 (12) 2 (4) [3]}
993 \centerline{16 (4) [2] 2 (12) 2 (62) [2] 2 (16) 82}
994 \vskip\baselineskip
995 Note that any duplicated rows that are not all white or all black are removed
996 before the repeat counts are calculated. The rows thus removed are rows 5, 6,
997 10, 11, 13, 14, 15, 17, 18, 23, and 24.
998
999 @ The next step in the encoding of this character is to calculate the optimal
1000 value of |dyn_f|. The details of how this calculation is done are not
1001 important here; suffice it to say that there is a simple algorithm which in one
1002 pass over the count list can determine the best value of |dyn_f|. For this
1003 character, the optimal value turns out to be 8 (atypically low). Thus, all
1004 count values less than or equal to 8 are packed in one nybble; those from
1005 nine to $(13-8)*16+8$ or 88 are packed in two nybbles. The run encoded values
1006 now become (in hex, separated according to the above list):
1007 \vskip\baselineskip
1008 \centerline{\tt D9 E2 97 2 B1 E2 2 93 2 4 E3}
1009 \centerline{\tt 97 4 E2 2 93 2 C5 E2 2 97 D9}
1010 \vskip\baselineskip\noindent
1011 which comes to 36 nybbles, or 18 bytes. This is shorter than the 73 bytes
1012 required for the bit map, so we use the run count packing.
1013
1014 @ The short form of the character preamble is used because all of the
1015 parameters fit in their respective lengths. The packet length is therefore
1016 18 bytes for the raster, plus
1017 eight bytes for the character preamble parameters following the character
1018 code, or 26. The |tfm| width for this character is 640796, or {\tt 9C71C} in
1019 hexadecimal. The horizontal escapement is 25 pixels. The flag byte is
1020 88 hex, indicating the short preamble, the black first count, and the
1021 |dyn_f| value of 8. The final total character packet, in hexadecimal, is:
1022 \vskip\baselineskip
1023 $$\vbox{\halign{\hfil #\quad&&{\tt #\ }\cr
1024 Flag byte&88\cr
1025 Packet length&1A\cr
1026 Character code&04\cr
1027 |tfm| width&09&C7&1C\cr
1028 Horizontal escapement (pixels)&19\cr
1029 Width of bit map&14\cr
1030 Height of bit map&1D\cr
1031 Horizontal offset (signed)&FE\cr
1032 Vertical offset&1C\cr
1033 Raster data&D9&E2&97\cr
1034 &2B&1E&22\cr
1035 &93&24&E3\cr
1036 &97&4E&22\cr
1037 &93&2C&5E\cr
1038 &22&97&D9\cr}}$$
1039
1040 @ This format was written by Tomas Rokicki in August, 1985.
1041
1042 @* Input and output.
1043 There are two types of files that this program must deal with---standard
1044 text files and files of bytes (packed files and generic font files.)
1045 For our purposes, we shall consider an eight-bit byte to consist of the
1046 values |0..255|. If your system does not pack these values to a byte, it is
1047 no major difficulty; you must only insure that the input function
1048 |pk_byte| can read packed bytes, and that the output function |gf_byte|
1049 packs the bytes to be shipped.
1050
1051 @<Types...@>=
1052 @!eight_bits=0..255; {packed file byte}
1053 @!byte_file=packed file of eight_bits ; {for packed file words}
1054 @^system dependancies@>
1055
1056 @ @<Glob...@>=
1057 @!gf_file,@!pk_file:byte_file; {the I/O streams}
1058 @^system dependencies@>
1059
1060 @ To prepare these files for input, we |reset| them. An extension of
1061 \PASCAL\ is needed in the case of |gf_file|, since we want to associate
1062 it with external files whose names are specified dynamically (i.e., not
1063 known at compile time). The following code assumes that `|reset(f,s)|'
1064 does this, when |f| is a file variable and |s| is a string variable that
1065 specifies the file name. If |eof(f)| is true immediately after
1066 |reset(f,s)| has acted, we assume that no file named |s| is accessible.
1067 @^system dependencies@>
1068
1069 @p procedure open_gf_file; {prepares to write packed bytes in a |gf_file|}
1070 begin rewrite(gf_file,gf_name);
1071 gf_loc := 0 ;
1072 end;
1073 @#
1074 procedure open_pk_file; {prepares the input for reading}
1075 begin reset(pk_file,pk_name);
1076 pk_loc := 0 ;
1077 end;
1078
1079 @ We need a place to store the names of the input and output files, as well
1080 as a byte counter for the output file.
1081
1082 @<Glob...@>=
1083 @!gf_name,@!pk_name:packed array[1..name_length] of char; {names of input
1084 and output files}
1085 @!gf_loc, @!pk_loc:integer; {how many bytes have we sent?}
1086
1087 @ We need a procedure that will write a byte to the \.{GF} file. If the
1088 particular system
1089 @^system dependencies@>
1090 requires buffering, here is the place to do it.
1091
1092 @p procedure gf_byte (i : integer) ;
1093 begin gf_file^ := i ;
1094 put(gf_file) ;
1095 incr(gf_loc) ;
1096 end;
1097
1098 @ We also need a function that will get a single byte from the \.{PK} file.
1099 Again, buffering may be done in this procedure.
1100
1101 @p function pk_byte : eight_bits ;
1102 var nybble, temp : eight_bits ;
1103 begin
1104 temp := pk_file^ ;
1105 get(pk_file) ;
1106 pk_loc := pk_loc + 1 ;
1107 pk_byte := temp ;
1108 end ;
1109
1110 @ Now we are ready to open the files and write the identification of the
1111 pixel file.
1112
1113 @<Open files@>=
1114 open_pk_file ;
1115 open_gf_file
1116
1117 @ As we are reading the packed file, we often need to fetch 16 and 32 bit
1118 quantities. Here we have two procedures to do this.
1119
1120 @p function signed_byte : integer ;
1121 var a : integer ;
1122 begin
1123 a := pk_byte ;
1124 if a > 127 then
1125 a := a - 256 ;
1126 signed_byte := a ;
1127 end ;
1128 @#
1129 function get_16 : integer ;
1130 var a : integer ;
1131 begin
1132 a := pk_byte ;
1133 get_16 := a * 256 + pk_byte ;
1134 end ;
1135 @#
1136 function signed_16 : integer ;
1137 var a : integer ;
1138 begin
1139 a := signed_byte ;
1140 signed_16 := a * 256 + pk_byte ;
1141 end ;
1142 @#
1143 function get_32 : integer ;
1144 var a : integer ;
1145 begin
1146 a := get_16 ;
1147 if a > 32767 then a := a - 65536 ;
1148 get_32 := a * 65536 + get_16 ;
1149 end ;
1150
1151 @ As we are writing the \.{GF} file, we often need to write signed and
1152 unsigned, one, two, three, and four-byte values. These routines give
1153 us that capability.
1154
1155 @p procedure gf_sbyte(i : integer) ;
1156 begin
1157 if i < 0 then
1158 i := i + 256 ;
1159 gf_byte(i) ;
1160 end ;
1161 @#
1162 procedure gf_16(i : integer) ;
1163 begin
1164 gf_byte(i div 256) ;
1165 gf_byte(i mod 256) ;
1166 end ;
1167 @#
1168 procedure gf_24(i : integer) ;
1169 begin
1170 gf_byte(i div 65536) ;
1171 gf_16(i mod 65536) ;
1172 end ;
1173 @#
1174 procedure gf_quad(i : integer) ;
1175 begin
1176 if i >= 0 then begin
1177 gf_byte(i div 16777216) ;
1178 end else begin
1179 i := (i + 1073741824) + 1073741824 ;
1180 gf_byte(128 + (i div 16777216)) ;
1181 end ;
1182 gf_24(i mod 16777216) ;
1183 end ;
1184
1185 @* Character unpacking.
1186 Now we deal with unpacking characters into the \.{GF} representation.
1187
1188 @<Unpack and write character@>=
1189 dyn_f := flag_byte div 16 ;
1190 flag_byte := flag_byte mod 16 ;
1191 turn_on := flag_byte >= 8 ;
1192 if turn_on then flag_byte := flag_byte - 8 ;
1193 if flag_byte = 7 then
1194 @<Read long character preamble@>
1195 else if flag_byte > 3 then
1196 @<Read extended short character preamble@>
1197 else
1198 @<Read short character preamble@> ;
1199 @<Calculate and check |min_m|, |max_m|, |min_n|, and |max_n|@> ;
1200 @<Save character locator@> ;
1201 @<Write \(character preamble@> ;
1202 @<Read and translate raster description@> ;
1203 gf_byte(eoc) ;
1204 last_eoc := gf_loc ;
1205 if end_of_packet <> pk_loc then abort('Bad pk file! Bad packet length.')
1206
1207 @ We need a whole lot of globals used but not defined up there.
1208
1209 @<Glob...@>=
1210 @!i, @!j : integer ; {index pointers}
1211 @!end_of_packet : integer ; {where we expect the end of the packet to be}
1212 @!dyn_f : integer ; {dynamic packing variable}
1213 @!car : integer ; {the character we are reading}
1214 @!tfm_width : integer ; {the TFM width of the current character}
1215 @!x_off, @!y_off : integer ; {the offsets for the character}
1216
1217 @ Now we read and check the preamble of the \.{PK} file. In the preamble, we
1218 find the |hppp|, |design_size|, |checksum|. We write the relevant parameters
1219 to the \.{GF} file, including the preamble comment.
1220
1221 @<Read preamble@>=
1222 if pk_byte <> pk_pre then abort('Bad pk file! pre command missing.') ;
1223 gf_byte(pre) ;
1224 if pk_byte <> pk_id then abort('Wrong version of packed file!.') ;
1225 gf_byte(gf_id_byte) ;
1226 j := pk_byte ;
1227 for i := 1 to j do hppp := pk_byte ;
1228 gf_byte(comm_length) ;
1229 for i := 1 to comm_length do
1230 gf_byte(xord[comment[i]]) ;
1231 design_size := get_32 ;
1232 checksum := get_32 ;
1233 hppp := get_32 ; vppp := get_32 ;
1234 if hppp <> vppp then print_ln('Warning: aspect ratio not 1:1!') ;
1235 magnification := round(hppp * 72.27 * 5 / 65536) ;
1236 last_eoc := gf_loc
1237
1238 @ Of course, we need to define the above variables.
1239
1240 @<Glob...@>=
1241 @!comment : packed array[1..comm_length] of char ;
1242 @!magnification : integer ; {resolution at which pixel file is prepared}
1243 @!design_size : integer ; {design size in \.{FIXes}}
1244 @!checksum : integer ; {checksum of pixel file}
1245 @!hppp, @!vppp : integer ; {horizontal and vertical points per inch}
1246
1247 @ @<Set init...@>=
1248 comment := preamble_comment ;
1249
1250 @ Now, the character preamble reading modules. First, we have the general
1251 case: the long character preamble format.
1252
1253 @<Read long character preamble@>=
1254 begin
1255 packet_length := get_32 ; car := get_32 ;
1256 end_of_packet := packet_length + pk_loc ;
1257 tfm_width := get_32 ;
1258 hor_esc := get_32 ;
1259 ver_esc := get_32 ;
1260 c_width := get_32 ;
1261 c_height := get_32 ;
1262 word_width := (c_width + 31) div 32 ;
1263 x_off := get_32 ;
1264 y_off := get_32 ;
1265 end
1266
1267 @ This module reads the character preamble with double byte parameters.
1268
1269 @<Read extended short character preamble@>=
1270 begin
1271 packet_length := (flag_byte - 4) * 65536 + get_16 ;
1272 car := pk_byte ;
1273 end_of_packet := packet_length + pk_loc ;
1274 i := pk_byte ;
1275 tfm_width := i * 65536 + get_16 ;
1276 hor_esc := get_16 * 65536 ;
1277 ver_esc := 0 ;
1278 c_width := get_16 ;
1279 c_height := get_16 ;
1280 word_width := (c_width + 31) div 32 ;
1281 x_off := signed_16 ;
1282 y_off := signed_16 ;
1283 end
1284
1285 @ Here we read the most common character preamble, that with single byte
1286 parameters.
1287
1288 @<Read short character preamble@>=
1289 begin
1290 packet_length := flag_byte * 256 + pk_byte ;
1291 car := pk_byte ;
1292 end_of_packet := packet_length + pk_loc ;
1293 i := pk_byte ;
1294 tfm_width := i * 65536 + get_16 ;
1295 hor_esc := pk_byte * 65536 ;
1296 ver_esc := 0 ;
1297 c_width := pk_byte ;
1298 c_height := pk_byte ;
1299 word_width := (c_width + 31) div 32 ;
1300 x_off := signed_byte ;
1301 y_off := signed_byte ;
1302 end
1303
1304 @ Some more globals:
1305
1306 @<Glob...@>=
1307 @!c_height, @!c_width : integer ; {sizes of the character glyphs}
1308 @!word_width : integer ; {width of character in raster words}
1309 @!hor_esc, @!ver_esc : integer ; {the character escapement}
1310 @!packet_length : integer ; {the length of the packet in bytes}
1311 @!last_eoc : integer ; {the last end of character}
1312
1313 @ The \.{GF} format requires the minimum and maximum |m| and |n|
1314 values in the postamble, so we generate them here. One thing
1315 that should be noted, here. The value |max_n-min_n| will be the
1316 height of the character glyph, but for the width, you need to
1317 use |max_m-min_m-1|, because of the peculiarities of the \.{GF}
1318 format.
1319
1320 @<Calculate and check |min_m|, |max_m|, |min_n|, and |max_n|@>=
1321 if (c_height = 0) or (c_width = 0) then begin
1322 c_height := 0 ; c_width := 0 ; x_off := 0 ; y_off := 0 ;
1323 end ;
1324 min_m := - x_off ;
1325 if min_m < mmin_m then
1326 mmin_m := min_m ;
1327 max_m := c_width + min_m ;
1328 if max_m > mmax_m then
1329 mmax_m := max_m ;
1330 min_n := y_off - c_height + 1 ;
1331 max_n := y_off ;
1332 if min_n > max_n then
1333 min_n := max_n ;
1334 if min_n < mmin_n then
1335 mmin_n := min_n ;
1336 if max_n > mmax_n then
1337 mmax_n := max_n
1338
1339 @ We have to declare the variables which hold the bounding box. We
1340 also need the arrays that hold the back pointers to the characters,
1341 the horizontal and vertical escapements, and the \.{TFM} widths.
1342
1343 @<Glob...@>=
1344 @!min_m, @!max_m, @!min_n, @!max_n : integer ;
1345 @!mmin_m, @!mmax_m, @!mmin_n, @!mmax_n : integer ;
1346 @!char_pointer, @!s_tfm_width : array [0..255] of integer ;
1347 @!s_hor_esc, @!s_ver_esc : array [0..255] of integer ;
1348 @!this_char_ptr : integer ;
1349
1350 @ We initialize these bounding box values to be ridiculous, and say
1351 that there were no characters seen yet.
1352
1353 @<Set init...@>=
1354 mmin_m := 999999 ;
1355 mmin_n := 999999 ;
1356 mmax_m := -999999 ;
1357 mmax_n := -999999 ;
1358 for i := 0 to 255 do
1359 char_pointer[i] := -1 ;
1360
1361 @ This module takes care of the simple job of writing the character
1362 preamble, after picking one to fit.
1363
1364 @<Write \(character preamble@>=
1365 begin
1366 if (char_pointer[car mod 256] = -1) and
1367 (car >= 0) and (car < 256) and
1368 (max_m >= 0) and (max_m < 256) and
1369 (max_n >= 0) and (max_n < 256) and
1370 (max_m >= min_m) and (max_n >= min_n) and
1371 (max_m < min_m + 256) and (max_n < min_n + 256) then begin
1372 char_pointer[car mod 256] := this_char_ptr ;
1373 gf_byte(boc1) ;
1374 gf_byte(car) ;
1375 gf_byte(max_m - min_m) ;
1376 gf_byte(max_m) ;
1377 gf_byte(max_n - min_n) ;
1378 gf_byte(max_n) ;
1379 end else begin
1380 gf_byte(boc) ;
1381 gf_quad(car) ;
1382 gf_quad(char_pointer[car mod 256]) ;
1383 char_pointer[car mod 256] := this_char_ptr ;
1384 gf_quad(min_m) ;
1385 gf_quad(max_m) ;
1386 gf_quad(min_n) ;
1387 gf_quad(max_n) ;
1388 end ;
1389 end
1390
1391 @ In this routine we either save or check the current character
1392 parameters.
1393
1394 @<Save character locator@>=
1395 begin
1396 i := car mod 256 ;
1397 if (char_pointer[i] = -1) then begin
1398 s_ver_esc[i] := ver_esc ;
1399 s_hor_esc[i] := hor_esc ;
1400 s_tfm_width[i] := tfm_width ;
1401 end else begin
1402 if (s_ver_esc[i] <> ver_esc) or
1403 (s_hor_esc[i] <> hor_esc) or
1404 (s_tfm_width[i] <> tfm_width) then
1405 print_ln('Two characters mod ', i:1,' have mismatched parameters') ;
1406 end ;
1407 end
1408
1409 @ And another module to write out those character locators we have so
1410 carefully saved up the information for.
1411
1412 @<Write \(character locators@>=
1413 for i := 0 to 255 do
1414 if char_pointer[i] <> -1 then begin
1415 if (s_ver_esc[i] = 0) and (s_hor_esc[i] >= 0) and
1416 (s_hor_esc[i] < 16777216) and (s_hor_esc[i] mod 65536 = 0) then begin
1417 gf_byte(char_loc0) ;
1418 gf_byte(i) ;
1419 gf_byte(s_hor_esc[i] div 65536) ;
1420 end else begin
1421 gf_byte(char_loc) ;
1422 gf_byte(i) ;
1423 gf_quad(s_hor_esc[i]) ;
1424 gf_quad(s_ver_esc[i]) ;
1425 end ;
1426 gf_quad(s_tfm_width[i]) ;
1427 gf_quad(char_pointer[i]) ;
1428 end
1429
1430 @ Now we have the most important part of the program, where we actually
1431 interpret the commands in the raster description. First of all, we need
1432 a procedure to get a single nybble from the file, as well as one to get
1433 a single bit. We also use the |pk_packed_num| procedure defined in the
1434 \.{PK} file description.
1435
1436 @p function get_nyb : integer ;
1437 var temp : eight_bits ;
1438 begin
1439 if bit_weight = 0 then begin
1440 input_byte := pk_byte ;
1441 bit_weight := 16 ;
1442 end ;
1443 temp := input_byte div bit_weight ;
1444 input_byte := input_byte - temp * bit_weight ;
1445 bit_weight := bit_weight div 16 ;
1446 get_nyb := temp ;
1447 end ;
1448 @#
1449 function get_bit : boolean ;
1450 var temp : boolean ;
1451 begin
1452 bit_weight := bit_weight div 2 ;
1453 if bit_weight = 0 then begin
1454 input_byte := pk_byte ;
1455 bit_weight := 128 ;
1456 end ;
1457 temp := input_byte >= bit_weight ;
1458 if temp then
1459 input_byte := input_byte - bit_weight ;
1460 get_bit := temp ;
1461 end ;
1462 @<Packed number procedure@>
1463
1464 @ Now, the globals to help communication between these procedures, and a buffer
1465 for the raster row counts.
1466
1467 @<Glob...@>=
1468 @!input_byte : eight_bits ; {the byte we are currently decimating}
1469 @!bit_weight : eight_bits ; {weight of the current bit}
1470 @!nybble : eight_bits ; {the current nybble}
1471 @!row_counts : array [0..max_counts] of integer ;
1472 {where the row is constructed}
1473 @!rcp : integer ; { the row counts pointer }
1474
1475 @ Actually, if the character is a bit mapped character, then we
1476 make it look like run counts by determining the appropriate
1477 values ourselves. Thus, we have a routine which gets the next
1478 count value, below.
1479
1480 @<Get next count value into |count|@>=
1481 begin
1482 turn_on := not turn_on ;
1483 if dyn_f = 14 then begin
1484 count := 1 ;
1485 done := false ;
1486 while not done do begin
1487 if count_down <= 0 then
1488 done := true
1489 else if (turn_on = get_bit) then
1490 count := count + 1
1491 else
1492 done := true ;
1493 count_down := count_down - 1 ;
1494 end ;
1495 end else
1496 count := pk_packed_num ;
1497 end
1498
1499 @ And the main procedure.
1500
1501 @<Read and translate raster description@>=
1502 if (c_width > 0) and (c_height > 0) then begin
1503 bit_weight := 0 ;
1504 count_down := c_height * c_width - 1 ;
1505 if dyn_f = 14 then
1506 turn_on := get_bit ;
1507 repeat_count := 0 ;
1508 x_to_go := c_width ;
1509 y_to_go := c_height ;
1510 cur_n := c_height ;
1511 count := 0 ;
1512 first_on := turn_on ;
1513 turn_on := not turn_on ;
1514 rcp := 0 ;
1515 while y_to_go > 0 do begin
1516 if count = 0 then
1517 @<Get next count...@> ;
1518 if rcp = 0 then
1519 first_on := turn_on ;
1520 while count >= x_to_go do begin
1521 row_counts[rcp] := x_to_go ;
1522 count := count - x_to_go ;
1523 for i := 0 to repeat_count do begin
1524 @<Output row@> ;
1525 y_to_go := y_to_go - 1 ;
1526 end ;
1527 repeat_count := 0 ;
1528 x_to_go := c_width ;
1529 rcp := 0 ;
1530 if (count > 0) then
1531 first_on := turn_on ;
1532 end ;
1533 if count > 0 then begin
1534 row_counts[rcp] := count ;
1535 if rcp = 0 then
1536 first_on := turn_on ;
1537 rcp := rcp + 1 ;
1538 if rcp > max_counts then begin
1539 print_ln('A character had too many run counts') ;
1540 jump_out ;
1541 end ;
1542 x_to_go := x_to_go - count ;
1543 count := 0 ;
1544 end ;
1545 end ;
1546 end
1547
1548 @ This routine actually outputs a row to the \.{GF} file.
1549
1550 @<Output row@>=
1551 if (rcp > 0) or first_on then begin
1552 j := 0 ;
1553 max := rcp ;
1554 if not turn_on then
1555 max := max - 1 ;
1556 if cur_n - y_to_go = 1 then begin
1557 if first_on then
1558 gf_byte(new_row_0)
1559 else if row_counts[0] < 165 then begin
1560 gf_byte(new_row_0 + row_counts[0]) ;
1561 j := j + 1 ;
1562 end else
1563 gf_byte(skip0) ;
1564 end else if cur_n > y_to_go then begin
1565 if cur_n - y_to_go < 257 then begin
1566 gf_byte(skip1) ;
1567 gf_byte(cur_n - y_to_go - 1) ;
1568 end else begin
1569 gf_byte(skip1+1) ;
1570 gf_16(cur_n - y_to_go - 1) ;
1571 end ;
1572 if first_on then
1573 gf_byte(paint_0) ;
1574 end else if first_on then
1575 gf_byte(paint_0) ;
1576 cur_n := y_to_go ;
1577 while j <= max do begin
1578 if row_counts[j] < 64 then
1579 gf_byte(paint_0 + row_counts[j])
1580 else if row_counts[j] < 256 then begin
1581 gf_byte(paint1) ;
1582 gf_byte(row_counts[j]) ;
1583 end else begin
1584 gf_byte(paint1+1) ;
1585 gf_16(row_counts[j]) ;
1586 end ;
1587 j := j + 1 ;
1588 end ;
1589 end
1590
1591 @ Here we need the array which counts down the number of bits, and
1592 the current state flag.
1593
1594 @<Glob...@>=
1595 @!count_down : integer ; { have we run out of bits yet? }
1596 @!done : boolean ; { are we done yet? }
1597 @!max : integer ; { the maximum number of counts to output }
1598 @!repeat_count : integer ; {how many times to repeat the next row?}
1599 @!x_to_go, @!y_to_go : integer ; {how many columns/rows left?}
1600 @!turn_on, @!first_on : boolean ; {are we black here?}
1601 @!count : integer ; {how many bits of current color left?}
1602 @!cur_n : integer ; {what row are we at?}
1603
1604 @ To finish the \.{GF} file, we write out a postamble, including the
1605 character locators that we stored away.
1606
1607 @<Write \.{GF} postamble@>=
1608 j := gf_loc ;
1609 gf_byte(post) ;
1610 gf_quad(last_eoc) ;
1611 gf_quad(design_size) ;
1612 gf_quad(checksum) ;
1613 gf_quad(hppp) ;
1614 gf_quad(vppp) ;
1615 gf_quad(mmin_m) ;
1616 gf_quad(mmax_m) ;
1617 gf_quad(mmin_n) ;
1618 gf_quad(mmax_n) ;
1619 @<Write \(character locators@> ;
1620 gf_byte(post_post) ;
1621 gf_quad(j) ;
1622 gf_byte(gf_id_byte) ;
1623 for i := 0 to 3 do
1624 gf_byte(223) ;
1625 while gf_loc mod 4 <> 0 do
1626 gf_byte(223)
1627
1628 @ We need the |flag_byte| variable.
1629
1630 @<Glob...@>=
1631 @!flag_byte : integer ; {command or character flag byte}
1632
1633 @ Another necessary procedure skips over any specials between characters
1634 and before and after the postamble. (It echoes the specials exactly.)
1635
1636 @p procedure skip_specials ;
1637 var i, j, k : integer ;
1638 begin
1639 this_char_ptr := gf_loc ;
1640 repeat
1641 flag_byte := pk_byte ;
1642 if flag_byte >= 240 then
1643 case flag_byte of
1644 240, 241, 242, 243 :
1645 begin
1646 i := 0 ;
1647 gf_byte(flag_byte-1) ;
1648 for j := 240 to flag_byte do begin
1649 k := pk_byte ;
1650 gf_byte(k) ;
1651 i := 256 * i + k ;
1652 end ;
1653 for j := 1 to i do gf_byte(pk_byte) ;
1654 end ;
1655 244 :
1656 begin
1657 gf_byte(243) ;
1658 gf_quad(get_32) ;
1659 end ;
1660 245 : begin end ;
1661 246 : begin end ;
1662 247, 248, 249, 250, 251, 252, 253, 254, 255 :
1663 abort('Unexpected ', flag_byte:1,'!') ;
1664 endcases ;
1665 until (flag_byte < 240) or (flag_byte = pk_post) ;
1666 end ;
1667
1668 @* Terminal communication.
1669 We must get the file names and determine whether input is to be in
1670 hexadecimal or binary. To do this, we use the standard input path
1671 name. We need a procedure to flush the input buffer. For most systems,
1672 this will be an empty statement. For other systems, a |print_ln| will
1673 provide a quick fix. We also need a routine to get a line of input from
1674 the terminal. On some systems, a simple |read_ln| will do. Finally,
1675 a macro to print a string to the first blank is required.
1676
1677 @d flush_buffer == begin end
1678 @d get_line(#) == if eoln(input) then read_ln(input) ;
1679 i := 1 ;
1680 while not (eoln(input) or eof(input)) do begin
1681 #[i] := input^ ;
1682 incr(i) ;
1683 get(input) ;
1684 end ;
1685 #[i] := ' '
1686
1687 @ @p procedure dialog ;
1688 var i : integer ; {index variable}
1689 buffer : packed array [1..name_length] of char; {input buffer}
1690 begin
1691 for i := 1 to name_length do begin
1692 gf_name[i] := ' ' ;
1693 pk_name[i] := ' ' ;
1694 end;
1695 print('Input file name: ') ;
1696 flush_buffer ;
1697 get_line(pk_name) ;
1698 print('Output file name: ') ;
1699 flush_buffer ;
1700 get_line(gf_name) ;
1701 end ;
1702
1703 @* The main program.
1704 Now that we have all the pieces written, let us put them together.
1705
1706 @p begin
1707 initialize ;
1708 dialog ;
1709 @<Open files@> ;
1710 @<Read preamble@> ;
1711 skip_specials ;
1712 while flag_byte <> pk_post do begin
1713 @<Unpack and write character@> ;
1714 skip_specials ;
1715 end ;
1716 while not eof(pk_file) do i := pk_byte ;
1717 @<Write \.{GF} postamble@> ;
1718 print_ln(pk_loc:1,' bytes unpacked to ',gf_loc:1,' bytes.');
1719 final_end :
1720 end .
1721
1722 @* System-dependent changes.
1723 This section should be replaced, if necessary, by changes to the program
1724 that are necessary to make \.{PKtoGF} work at a particular installation.
1725 Any additional routines should be inserted here.
1726 @^system dependencies@>
1727
1728 @* Index.
1729 Pointers to error messages appear here together with the section numbers
1730 where each ident\-i\-fier is used.