| 1 |
% This program is not copyrighted and can be used freely. |
| 2 |
% PKtype.web |
| 3 |
% |
| 4 |
% PKtype verifies the correctness of a PK file. |
| 5 |
% |
| 6 |
% Preliminary 0.0 version: May, 1985 |
| 7 |
% First release, 0.9 version: 8 May 1985 |
| 8 |
% Updated to reflect new pk format, 2.0 version: 25 July 1985 |
| 9 |
% Updated again for new pk format, 2.1 version: 15 August 1985 |
| 10 |
% Documentation updated, 2.2 version: 17 November 1987 |
| 11 |
% |
| 12 |
% slight editing to correspond with MFware report -- don, 21 October 89 |
| 13 |
% additional editing suggested by P Breitenlohner -- don, 18 November 89 |
| 14 |
% Corrected two typos -- 21 December 96 (don) |
| 15 |
% More typos -- 23 April 20 (tgr/karl) |
| 16 |
|
| 17 |
\def\versiondate{23 April 2020} |
| 18 |
% |
| 19 |
\def\hang{\hangindent 3em\noindent\ignorespaces} |
| 20 |
\font\ninerm=cmr9 |
| 21 |
\font\logo=logo10 % font used for the METAFONT logo |
| 22 |
\def\MF{{\logo META}\-{\logo FONT}} |
| 23 |
\def\PASCAL{Pascal} |
| 24 |
\def\tamu{Texas A\char38 M} |
| 25 |
\def\(#1){} % this is used to make section names sort themselves better |
| 26 |
\def\9#1{} % this is used for sort keys in the index |
| 27 |
\def\title{PKtype} |
| 28 |
\def\contentspagenumber{1} |
| 29 |
\def\topofcontents{\null |
| 30 |
\titlefalse % include headline on the contents page |
| 31 |
\def\rheader{\mainfont\hfil \contentspagenumber} |
| 32 |
\vfill |
| 33 |
\centerline{\titlefont The {\ttitlefont PKtype} processor} |
| 34 |
\vskip 15pt |
| 35 |
\centerline{(Version 2.3, \versiondate)} |
| 36 |
\vfill} |
| 37 |
\def\botofcontents{\vfill |
| 38 |
\centerline{\hsize 5in\baselineskip9pt |
| 39 |
\vbox{\ninerm\noindent |
| 40 |
The preparation of this report |
| 41 |
was supported in part by the National Science |
| 42 |
Foundation under grants IST-8201926 and MCS-8300984, |
| 43 |
and by the System Development Foundation. `\TeX' is a |
| 44 |
trademark of the American Mathematical Society.}}} |
| 45 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 46 |
|
| 47 |
@* Introduction. |
| 48 |
This program reads a \.{PK} file, verifies that it is in the correct |
| 49 |
format, and writes it in textual format. |
| 50 |
|
| 51 |
@ The |banner| string defined here should be changed whenever \.{PKtype} |
| 52 |
gets modified. |
| 53 |
|
| 54 |
@d banner=='This is PKtype, Version 2.3' {printed when the program starts} |
| 55 |
|
| 56 |
@ This program is written in standard \PASCAL, except where it is necessary |
| 57 |
to use extensions; for example, \.{PKtype} must read files whose names |
| 58 |
are dynamically specified, and that would be impossible in pure \PASCAL. |
| 59 |
|
| 60 |
@d othercases == others: {default for cases not listed explicitly} |
| 61 |
@d endcases == @+end {follows the default case in an extended |case| statement} |
| 62 |
@f othercases == else |
| 63 |
@f endcases == end |
| 64 |
|
| 65 |
@ Both the input and output come from binary files. On line interaction |
| 66 |
is handled through \PASCAL's standard |input| and |output| files. Two macros |
| 67 |
are used to write to the type file, so this output can easily be redirected. |
| 68 |
|
| 69 |
@d print_ln(#)==write_ln(output,#) |
| 70 |
@d print(#)==write(output,#) |
| 71 |
@d t_print_ln(#)==write_ln(typ_file,#) |
| 72 |
@d t_print(#)==write(typ_file,#) |
| 73 |
|
| 74 |
@p program PKtype(@!input,@!output); |
| 75 |
label @<Labels in the outer block@>@/ |
| 76 |
const @<Constants in the outer block@>@/ |
| 77 |
type @<Types in the outer block@>@/ |
| 78 |
var @<Globals in the outer block@>@/ |
| 79 |
procedure initialize; {this procedure gets things started properly} |
| 80 |
var i:integer; {loop index for initializations} |
| 81 |
begin print_ln(banner);@/ |
| 82 |
@<Set initial values@>@/ |
| 83 |
end; |
| 84 |
|
| 85 |
@ If the program has to stop prematurely, it goes to the |
| 86 |
`|final_end|'. |
| 87 |
|
| 88 |
@d final_end=9999 {label for the end of it all} |
| 89 |
|
| 90 |
@<Labels...@>=final_end; |
| 91 |
|
| 92 |
@ These constants determine the maximum length of a file name and the length |
| 93 |
of the terminal line, as well as the widest character that can be translated. |
| 94 |
@^system dependencies@> |
| 95 |
|
| 96 |
@<Constants...@>= |
| 97 |
@!name_length=80; {maximum length of a file name} |
| 98 |
@!terminal_line_length=132; {maximum length of an input line} |
| 99 |
|
| 100 |
@ Here are some macros for common programming idioms. |
| 101 |
|
| 102 |
@d incr(#) == #:=#+1 {increase a variable by unity} |
| 103 |
@d decr(#) == #:=#-1 {decrease a variable by unity} |
| 104 |
@d do_nothing == {empty statement} |
| 105 |
|
| 106 |
@ It is possible that a malformed packed file (heaven forbid!) or some other |
| 107 |
error might be detected by this program. Such errors might occur in a deeply |
| 108 |
nested procedure, so the procedure called |jump_out| has been added to transfer |
| 109 |
to the very end of the program with an error message. |
| 110 |
|
| 111 |
@d abort(#)==begin print_ln(' ',#); t_print_ln(' ',#); jump_out; end |
| 112 |
|
| 113 |
@p procedure jump_out; |
| 114 |
begin goto final_end; |
| 115 |
end; |
| 116 |
|
| 117 |
@* The character set. |
| 118 |
Like all programs written with the \.{WEB} system, \.{PKtype} can be |
| 119 |
used with any character set. But it uses ASCII code internally, because |
| 120 |
the programming for portable input-output is easier when a fixed internal |
| 121 |
code is used. |
| 122 |
|
| 123 |
The next few sections of \.{PKtype} have therefore been copied from the |
| 124 |
analogous ones in the \.{WEB} system routines. They have been considerably |
| 125 |
simplified, since \.{PKtype} need not deal with the controversial |
| 126 |
ASCII codes less than @'40 or greater than @'176. |
| 127 |
|
| 128 |
@<Types...@>= |
| 129 |
@!ASCII_code=" ".."~"; {a subrange of the integers} |
| 130 |
|
| 131 |
@ The original \PASCAL\ compiler was designed in the late 60s, when six-bit |
| 132 |
character sets were common, so it did not make provision for lower case |
| 133 |
letters. Nowadays, of course, we need to deal with both upper and lower case |
| 134 |
alphabets in a convenient way, especially in a program like \.{PKtype}. |
| 135 |
So we shall assume that the \PASCAL\ system being used for \.{PKtype} |
| 136 |
has a character set containing at least the standard visible characters |
| 137 |
of ASCII code (|"!"| through |"~"|). |
| 138 |
|
| 139 |
Some \PASCAL\ compilers use the original name |char| for the data type |
| 140 |
associated with the characters in text files, while other \PASCAL s |
| 141 |
consider |char| to be a 64-element subrange of a larger data type that has |
| 142 |
some other name. In order to accommodate this difference, we shall use |
| 143 |
the name |text_char| to stand for the data type of the characters in the |
| 144 |
output file. We shall also assume that |text_char| consists of |
| 145 |
the elements |chr(first_text_char)| through |chr(last_text_char)|, |
| 146 |
inclusive. The following definitions should be adjusted if necessary. |
| 147 |
@^system dependencies@> |
| 148 |
|
| 149 |
@d text_char == char {the data type of characters in text files} |
| 150 |
@d first_text_char=0 {ordinal number of the smallest element of |text_char|} |
| 151 |
@d last_text_char=127 {ordinal number of the largest element of |text_char|} |
| 152 |
|
| 153 |
@<Types...@>= |
| 154 |
@!text_file=packed file of text_char; |
| 155 |
|
| 156 |
@ The \.{PKtype} processor converts between ASCII code and |
| 157 |
the user's external character set by means of arrays |xord| and |xchr| |
| 158 |
that are analogous to \PASCAL's |ord| and |chr| functions. |
| 159 |
|
| 160 |
@<Globals...@>= |
| 161 |
@!xord: array [text_char] of ASCII_code; |
| 162 |
{specifies conversion of input characters} |
| 163 |
@!xchr: array [0..255] of text_char; |
| 164 |
{specifies conversion of output characters} |
| 165 |
|
| 166 |
@ Under our assumption that the visible characters of standard ASCII are |
| 167 |
all present, the following assignment statements initialize the |
| 168 |
|xchr| array properly, without needing any system-dependent changes. |
| 169 |
|
| 170 |
@<Set init...@>= |
| 171 |
for i:=0 to @'37 do xchr[i]:='?'; |
| 172 |
xchr[@'40]:=' '; |
| 173 |
xchr[@'41]:='!'; |
| 174 |
xchr[@'42]:='"'; |
| 175 |
xchr[@'43]:='#'; |
| 176 |
xchr[@'44]:='$'; |
| 177 |
xchr[@'45]:='%'; |
| 178 |
xchr[@'46]:='&'; |
| 179 |
xchr[@'47]:='''';@/ |
| 180 |
xchr[@'50]:='('; |
| 181 |
xchr[@'51]:=')'; |
| 182 |
xchr[@'52]:='*'; |
| 183 |
xchr[@'53]:='+'; |
| 184 |
xchr[@'54]:=','; |
| 185 |
xchr[@'55]:='-'; |
| 186 |
xchr[@'56]:='.'; |
| 187 |
xchr[@'57]:='/';@/ |
| 188 |
xchr[@'60]:='0'; |
| 189 |
xchr[@'61]:='1'; |
| 190 |
xchr[@'62]:='2'; |
| 191 |
xchr[@'63]:='3'; |
| 192 |
xchr[@'64]:='4'; |
| 193 |
xchr[@'65]:='5'; |
| 194 |
xchr[@'66]:='6'; |
| 195 |
xchr[@'67]:='7';@/ |
| 196 |
xchr[@'70]:='8'; |
| 197 |
xchr[@'71]:='9'; |
| 198 |
xchr[@'72]:=':'; |
| 199 |
xchr[@'73]:=';'; |
| 200 |
xchr[@'74]:='<'; |
| 201 |
xchr[@'75]:='='; |
| 202 |
xchr[@'76]:='>'; |
| 203 |
xchr[@'77]:='?';@/ |
| 204 |
xchr[@'100]:='@@'; |
| 205 |
xchr[@'101]:='A'; |
| 206 |
xchr[@'102]:='B'; |
| 207 |
xchr[@'103]:='C'; |
| 208 |
xchr[@'104]:='D'; |
| 209 |
xchr[@'105]:='E'; |
| 210 |
xchr[@'106]:='F'; |
| 211 |
xchr[@'107]:='G';@/ |
| 212 |
xchr[@'110]:='H'; |
| 213 |
xchr[@'111]:='I'; |
| 214 |
xchr[@'112]:='J'; |
| 215 |
xchr[@'113]:='K'; |
| 216 |
xchr[@'114]:='L'; |
| 217 |
xchr[@'115]:='M'; |
| 218 |
xchr[@'116]:='N'; |
| 219 |
xchr[@'117]:='O';@/ |
| 220 |
xchr[@'120]:='P'; |
| 221 |
xchr[@'121]:='Q'; |
| 222 |
xchr[@'122]:='R'; |
| 223 |
xchr[@'123]:='S'; |
| 224 |
xchr[@'124]:='T'; |
| 225 |
xchr[@'125]:='U'; |
| 226 |
xchr[@'126]:='V'; |
| 227 |
xchr[@'127]:='W';@/ |
| 228 |
xchr[@'130]:='X'; |
| 229 |
xchr[@'131]:='Y'; |
| 230 |
xchr[@'132]:='Z'; |
| 231 |
xchr[@'133]:='['; |
| 232 |
xchr[@'134]:='\'; |
| 233 |
xchr[@'135]:=']'; |
| 234 |
xchr[@'136]:='^'; |
| 235 |
xchr[@'137]:='_';@/ |
| 236 |
xchr[@'140]:='`'; |
| 237 |
xchr[@'141]:='a'; |
| 238 |
xchr[@'142]:='b'; |
| 239 |
xchr[@'143]:='c'; |
| 240 |
xchr[@'144]:='d'; |
| 241 |
xchr[@'145]:='e'; |
| 242 |
xchr[@'146]:='f'; |
| 243 |
xchr[@'147]:='g';@/ |
| 244 |
xchr[@'150]:='h'; |
| 245 |
xchr[@'151]:='i'; |
| 246 |
xchr[@'152]:='j'; |
| 247 |
xchr[@'153]:='k'; |
| 248 |
xchr[@'154]:='l'; |
| 249 |
xchr[@'155]:='m'; |
| 250 |
xchr[@'156]:='n'; |
| 251 |
xchr[@'157]:='o';@/ |
| 252 |
xchr[@'160]:='p'; |
| 253 |
xchr[@'161]:='q'; |
| 254 |
xchr[@'162]:='r'; |
| 255 |
xchr[@'163]:='s'; |
| 256 |
xchr[@'164]:='t'; |
| 257 |
xchr[@'165]:='u'; |
| 258 |
xchr[@'166]:='v'; |
| 259 |
xchr[@'167]:='w';@/ |
| 260 |
xchr[@'170]:='x'; |
| 261 |
xchr[@'171]:='y'; |
| 262 |
xchr[@'172]:='z'; |
| 263 |
xchr[@'173]:='{'; |
| 264 |
xchr[@'174]:='|'; |
| 265 |
xchr[@'175]:='}'; |
| 266 |
xchr[@'176]:='~'; |
| 267 |
for i:=@'177 to 255 do xchr[i]:='?'; |
| 268 |
|
| 269 |
@ The following system-independent code makes the |xord| array contain a |
| 270 |
suitable inverse to the information in |xchr|. |
| 271 |
|
| 272 |
@<Set init...@>= |
| 273 |
for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40; |
| 274 |
for i:=" " to "~" do xord[xchr[i]]:=i; |
| 275 |
|
| 276 |
@* Packed file format. |
| 277 |
The packed file format is a compact representation of the data contained in a |
| 278 |
\.{GF} file. The information content is the same, but packed (\.{PK}) files |
| 279 |
are almost always less than half the size of their \.{GF} counterparts. They |
| 280 |
are also easier to convert into a raster representation because they do not |
| 281 |
have a profusion of \\{paint}, \\{skip}, and \\{new\_row} commands to be |
| 282 |
separately interpreted. In addition, the \.{PK} format expressly forbids |
| 283 |
\&{special} commands within a character. The minimum bounding box for each |
| 284 |
character is explicit in the format, and does not need to be scanned for as in |
| 285 |
the \.{GF} format. Finally, the width and escapement values are combined with |
| 286 |
the raster information into character ``packets'', making it simpler in many |
| 287 |
cases to process a character. |
| 288 |
|
| 289 |
A \.{PK} file is organized as a stream of 8-bit bytes. At times, these bytes |
| 290 |
might be split into 4-bit nybbles or single bits, or combined into multiple |
| 291 |
byte parameters. When bytes are split into smaller pieces, the `first' piece |
| 292 |
is always the most significant of the byte. For instance, the first bit of |
| 293 |
a byte is the bit with value 128; the first nybble can be found by dividing |
| 294 |
a byte by 16. Similarly, when bytes are combined into multiple byte |
| 295 |
parameters, the first byte is the most significant of the parameter. If the |
| 296 |
parameter is signed, it is represented by two's-complement notation. |
| 297 |
|
| 298 |
The set of possible eight-bit values is separated into two sets, those that |
| 299 |
introduce a character definition, and those that do not. The values that |
| 300 |
introduce a character definition range from 0 to 239; byte values |
| 301 |
above 239 are interpreted as commands. Bytes that introduce character |
| 302 |
definitions are called flag bytes, and various fields within the byte indicate |
| 303 |
various things about how the character definition is encoded. Command bytes |
| 304 |
have zero or more parameters, and can never appear within a character |
| 305 |
definition or between parameters of another command, where they would be |
| 306 |
interpreted as data. |
| 307 |
|
| 308 |
A \.{PK} file consists of a preamble, followed by a sequence of one or more |
| 309 |
character definitions, followed by a postamble. The preamble command must |
| 310 |
be the first byte in the file, followed immediately by its parameters. |
| 311 |
Any number of character definitions may follow, and any command but the |
| 312 |
preamble command and the postamble command may occur between character |
| 313 |
definitions. The very last command in the file must be the postamble. |
| 314 |
|
| 315 |
@ The packed file format is intended to be easy to read and interpret by |
| 316 |
device drivers. The small size of the file reduces the input/output overhead |
| 317 |
each time a font is loaded. For those drivers that load and save each font |
| 318 |
file into memory, the small size also helps reduce the memory requirements. |
| 319 |
The length of each character packet is specified, allowing the character raster |
| 320 |
data to be loaded into memory by simply counting bytes, rather than |
| 321 |
interpreting each command; then, each character can be interpreted on a demand |
| 322 |
basis. This also makes it possible for a driver to skip a particular |
| 323 |
character quickly if it knows that the character is unused. |
| 324 |
|
| 325 |
@ First, the command bytes will be presented; then the format of the |
| 326 |
character definitions will be defined. Eight of the possible sixteen |
| 327 |
commands (values 240 through 255) are currently defined; the others are |
| 328 |
reserved for future extensions. The commands are listed below. Each command |
| 329 |
is specified by its symbolic name (e.g., \\{pk\_no\_op}), its opcode byte, |
| 330 |
and any parameters. The parameters are followed by a bracketed number |
| 331 |
telling how many bytes they occupy, with the number preceded by a plus sign if |
| 332 |
it is a signed quantity. (Four byte quantities are always signed, however.) |
| 333 |
|
| 334 |
\yskip\hang|pk_xxx1| 240 |k[1]| |x[k]|. This command is undefined in general; |
| 335 |
it functions as a $(k+2)$-byte \\{no\_op} unless special \.{PK}-reading |
| 336 |
programs are being used. \MF\ generates \\{xxx} commands when encountering |
| 337 |
a \&{special} string. It is recommended that |x| be a string having the form |
| 338 |
of a keyword followed by possible parameters relevant to that keyword. |
| 339 |
|
| 340 |
\yskip\hang\\{pk\_xxx2} 241 |k[2]| |x[k]|. Like |pk_xxx1|, but |0<=k<65536|. |
| 341 |
|
| 342 |
\yskip\hang\\{pk\_xxx3} 242 |k[3]| |x[k]|. Like |pk_xxx1|, but |
| 343 |
|0<=k<@t$2^{24}$@>|. \MF\ uses this when sending a \&{special} string whose |
| 344 |
length exceeds~255. |
| 345 |
|
| 346 |
\yskip\hang\\{pk\_xxx4} 243 |k[4]| |x[k]|. Like |pk_xxx1|, but |k| can be |
| 347 |
ridiculously large; |k| mustn't be negative. |
| 348 |
|
| 349 |
\yskip\hang|pk_yyy| 244 |y[4]|. This command is undefined in general; it |
| 350 |
functions as a five-byte \\{no\_op} unless special \.{PK} reading programs |
| 351 |
are being used. \MF\ puts |scaled| numbers into |yyy|'s, as a result of |
| 352 |
\&{numspecial} commands; the intent is to provide numeric parameters to |
| 353 |
\\{xxx} commands that immediately precede. |
| 354 |
|
| 355 |
\yskip\hang|pk_post| 245. Beginning of the postamble. This command is |
| 356 |
followed by enough |pk_no_op| commands to make the file a multiple |
| 357 |
of four bytes long. Zero through three bytes are usual, but any number |
| 358 |
is allowed. |
| 359 |
This should make the file easy to read on machines that pack four bytes to |
| 360 |
a word. |
| 361 |
|
| 362 |
\yskip\hang|pk_no_op| 246. No operation, do nothing. Any number of |
| 363 |
|pk_no_op|'s may appear between \.{PK} commands, but a |pk_no_op| cannot be |
| 364 |
inserted between a command and its parameters, between two parameters, or |
| 365 |
inside a character definition. |
| 366 |
|
| 367 |
\yskip\hang|pk_pre| 247 |i[1]| |k[1]| |x[k]| |ds[4]| |cs[4]| |hppp[4]| |
| 368 |
|vppp[4]|. Preamble command. Here, |i| is the identification byte of the |
| 369 |
file, currently equal to 89. The string |x| is merely a comment, usually |
| 370 |
indicating the source of the \.{PK} file. The parameters |ds| and |cs| are |
| 371 |
the design size of the file in $1/2^{20}$ points, and the checksum of the |
| 372 |
file, respectively. The checksum should match the \.{TFM} file and the |
| 373 |
\.{GF} files for this font. Parameters |hppp| and |vppp| are the ratios |
| 374 |
of pixels per point, horizontally and vertically, multiplied by $2^{16}$; they |
| 375 |
can be used to correlate the font with specific device resolutions, |
| 376 |
magnifications, and ``at sizes''. Usually, the name of the \.{PK} file is |
| 377 |
formed by concatenating the font name (e.g., cmr10) with the resolution at |
| 378 |
which the font is prepared in pixels per inch multiplied by the magnification |
| 379 |
factor, and the letters \.{pk}. For instance, cmr10 at 300 dots per inch |
| 380 |
should be named \.{cmr10.300pk}; at one thousand dots per inch and magstephalf, |
| 381 |
it should be named \.{cmr10.1095pk}. |
| 382 |
|
| 383 |
@ We put a few of the above opcodes into definitions for symbolic use by |
| 384 |
this program. |
| 385 |
|
| 386 |
@d pk_id = 89 {the version of \.{PK} file described} |
| 387 |
@d pk_xxx1 = 240 {\&{special} commands} |
| 388 |
@d pk_yyy = 244 {\&{numspecial} commands} |
| 389 |
@d pk_post = 245 {postamble} |
| 390 |
@d pk_no_op = 246 {no operation} |
| 391 |
@d pk_pre = 247 {preamble} |
| 392 |
@d pk_undefined == 248, 249, 250, 251, 252, 253, 254, 255 |
| 393 |
|
| 394 |
@ The \.{PK} format has two conflicting goals: to pack character raster and |
| 395 |
size information as compactly as possible, while retaining ease of translation |
| 396 |
into raster and other forms. A suitable compromise was found in the use of |
| 397 |
run-encoding of the raster information. Instead of packing the individual |
| 398 |
bits of the character, we instead count the number of consecutive `black' or |
| 399 |
`white' pixels in a horizontal raster row, and then encode this number. Run |
| 400 |
counts are found for each row from left to right, traversing rows from the |
| 401 |
top to bottom. This is essentially the way the \.{GF} format works. |
| 402 |
Instead of presenting each row individually, however, we concatenate all |
| 403 |
of the horizontal raster rows into one long string of pixels, and encode this |
| 404 |
row. With knowledge of the width of the bit-map, the original character glyph |
| 405 |
can easily be reconstructed. In addition, we do not need special commands to |
| 406 |
mark the end of one row and the beginning of the next. |
| 407 |
|
| 408 |
Next, we place the burden of finding the minimum bounding box on the part |
| 409 |
of the font generator, since the characters will usually be used much more |
| 410 |
often than they are generated. The minimum bounding box is the smallest |
| 411 |
rectangle that encloses all `black' pixels of a character. We also |
| 412 |
eliminate the need for a special end of character marker, by supplying |
| 413 |
exactly as many bits as are required to fill the minimum bounding box, from |
| 414 |
which the end of the character is implicit. |
| 415 |
|
| 416 |
Let us next consider the distribution of the run counts. Analysis of several |
| 417 |
dozen pixel files at 300 dots per inch yields a distribution peaking at four, |
| 418 |
falling off slowly until ten, then a bit more steeply until twenty, and then |
| 419 |
asymptotically approaching the horizontal. Thus, the great majority of our |
| 420 |
run counts will fit in a four-bit nybble. The eight-bit byte is attractive for |
| 421 |
our run-counts, as it is the standard on many systems; however, the wasted four |
| 422 |
bits in the majority of cases seem a high price to pay. Another possibility |
| 423 |
is to use a Huffman-type encoding scheme with a variable number of bits for |
| 424 |
each run-count; this was rejected because of the overhead in fetching and |
| 425 |
examining individual bits in the file. Thus, the character raster definitions |
| 426 |
in the \.{PK} file format are based on the four-bit nybble. |
| 427 |
|
| 428 |
@ An analysis of typical pixel files yielded another interesting statistic: |
| 429 |
Fully 37\char`\%\ |
| 430 |
of the raster rows were duplicates of the previous row. Thus, the \.{PK} |
| 431 |
format allows the specification of repeat counts, which indicate how many times |
| 432 |
a horizontal raster row is to be repeated. These repeated rows are taken out |
| 433 |
of the character glyph before individual rows are concatenated into the long |
| 434 |
string of pixels. |
| 435 |
|
| 436 |
For elegance, we disallow a run count of zero. The case of a null raster |
| 437 |
description should be gleaned from the character width and height being equal |
| 438 |
to zero, and no raster data should be read. No other zero counts are ever |
| 439 |
necessary. Also, in the absence of repeat counts, the repeat value is set to |
| 440 |
be zero (only the original row is sent.) If a repeat count is seen, it takes |
| 441 |
effect on the current row. The current row is defined as the row on which the |
| 442 |
first pixel of the next run count will lie. The repeat count is set back to |
| 443 |
zero when the last pixel in the current row is seen, and the row is sent out. |
| 444 |
|
| 445 |
This poses a problem for entirely black and entirely white rows, however. Let |
| 446 |
us say that the current row ends with four white pixels, and then we have five |
| 447 |
entirely empty rows, followed by a black pixel at the beginning of the next |
| 448 |
row, and the character width is ten pixels. We would like to use a repeat |
| 449 |
count, but there is no legal place to put it. If we put it before the white |
| 450 |
run count, it will apply to the current row. If we put it after, it applies |
| 451 |
to the row with the black pixel at the beginning. Thus, entirely white or |
| 452 |
entirely black repeated rows are always packed as large run counts (in this |
| 453 |
case, a white run count of 54) rather than repeat counts. |
| 454 |
|
| 455 |
@ Now we turn our attention to the actual packing of the run counts and |
| 456 |
repeat counts into nybbles. There are only sixteen possible nybble values. |
| 457 |
We need to indicate run counts and repeat counts. Since the run counts are |
| 458 |
much more common, we will devote the majority of the nybble values to them. |
| 459 |
We therefore indicate a repeat count by a nybble of 14 followed by a packed |
| 460 |
number, where a packed number will be explained later. Since the repeat |
| 461 |
count value of one is so common, we indicate a repeat one command by a single |
| 462 |
nybble of 15. A 14 followed by the packed number 1 is still legal for a |
| 463 |
repeat one count. The run counts are coded directly as packed |
| 464 |
numbers. |
| 465 |
|
| 466 |
For packed numbers, therefore, we have the nybble values 0 through 13. We |
| 467 |
need to represent the positive integers up to, say, $2^{31}-1$. We would |
| 468 |
like the more common smaller numbers to take only one or two nybbles, and |
| 469 |
the infrequent large numbers to take three or more. We could therefore |
| 470 |
allocate one nybble value to indicate a large run count taking three or more |
| 471 |
nybbles. We do this with the value 0. |
| 472 |
|
| 473 |
@ We are left with the values 1 through 13. We can allocate some of these, say |
| 474 |
|dyn_f|, to be one-nybble run counts. |
| 475 |
These will work for the run counts |1..dyn_f|. For subsequent run |
| 476 |
counts, we will use a nybble greater than |dyn_f|, followed by a second nybble, |
| 477 |
whose value can run from 0 through 15. Thus, the two-nybble values will |
| 478 |
run from |dyn_f+1..(13-dyn_f)*16+dyn_f|. We have our definition of large run |
| 479 |
count values now, being all counts greater than |(13-dyn_f)*16+dyn_f|. |
| 480 |
|
| 481 |
We can analyze our several dozen pixel files and determine an optimal value of |
| 482 |
|dyn_f|, and use this value for all of the characters. Unfortunately, values |
| 483 |
of |dyn_f| that pack small characters well tend to pack the large characters |
| 484 |
poorly, and values that pack large characters well are not efficient for the |
| 485 |
smaller characters. Thus, we choose the optimal |dyn_f| on a character basis, |
| 486 |
picking the value that will pack each individual character in the smallest |
| 487 |
number of nybbles. Legal values of |dyn_f| run from 0 (with no one-nybble run |
| 488 |
counts) to 13 (with no two-nybble run counts). |
| 489 |
|
| 490 |
@ Our only remaining task in the coding of packed numbers is the large run |
| 491 |
counts. We use a scheme suggested by D.~E.~Knuth |
| 492 |
@^Knuth, Donald Ervin@> |
| 493 |
that simply and elegantly represents arbitrarily large values. The |
| 494 |
general scheme to represent an integer |i| is to write its hexadecimal |
| 495 |
representation, with leading zeros removed. Then we count the number of |
| 496 |
digits, and prepend one less than that many zeros before the hexadecimal |
| 497 |
representation. Thus, the values from one to fifteen occupy one nybble; |
| 498 |
the values sixteen through 255 occupy three, the values 256 through 4095 |
| 499 |
require five, etc. |
| 500 |
|
| 501 |
For our purposes, however, we have already represented the numbers one |
| 502 |
through |(13-dyn_f)*16+dyn_f|. In addition, the one-nybble values have |
| 503 |
already been taken by our other commands, which means that only the values |
| 504 |
from sixteen up are available to us for long run counts. Thus, we simply |
| 505 |
normalize our long run counts, by subtracting |(13-dyn_f)*16+dyn_f+1| and |
| 506 |
adding 16, and then we represent the result according to the scheme above. |
| 507 |
|
| 508 |
@ The final algorithm for decoding the run counts based on the above scheme |
| 509 |
looks like this, assuming that a procedure called \\{get\_nyb} is available |
| 510 |
to get the next nybble from the file, and assuming that the global |
| 511 |
|repeat_count| indicates whether a row needs to be repeated. Note that this |
| 512 |
routine is recursive, but since a repeat count can never directly follow |
| 513 |
another repeat count, it can only be recursive to one level. |
| 514 |
|
| 515 |
@<Packed number procedure@>= |
| 516 |
function pk_packed_num : integer ; |
| 517 |
var i, @!j : integer ; |
| 518 |
begin |
| 519 |
i := get_nyb ; |
| 520 |
if i = 0 then begin |
| 521 |
repeat j := get_nyb ; incr(i) ; until j <> 0 ; |
| 522 |
while i > 0 do begin j := j * 16 + get_nyb ; decr(i) ; end ; |
| 523 |
pk_packed_num := j - 15 + (13-dyn_f)*16 + dyn_f ; |
| 524 |
end else if i <= dyn_f then |
| 525 |
pk_packed_num := i |
| 526 |
else if i < 14 then |
| 527 |
pk_packed_num := (i-dyn_f-1)*16+get_nyb+dyn_f+1 |
| 528 |
else begin |
| 529 |
if repeat_count <> 0 then abort('Second repeat count for this row!') ; |
| 530 |
@.Second repeat count...@> |
| 531 |
repeat_count := 1; {prevent recursion more than one level} |
| 532 |
if i = 14 then repeat_count := pk_packed_num; |
| 533 |
send_out(true, repeat_count) ; |
| 534 |
pk_packed_num := pk_packed_num ; |
| 535 |
end ; |
| 536 |
end ; |
| 537 |
|
| 538 |
@ For low resolution fonts, or characters with `gray' areas, run encoding can |
| 539 |
often make the character many times larger. Therefore, for those characters |
| 540 |
that cannot be encoded efficiently with run counts, the \.{PK} format allows |
| 541 |
bit-mapping of the characters. This is indicated by a |dyn_f| value of |
| 542 |
14. The bits are packed tightly, by concatenating all of the horizontal raster |
| 543 |
rows into one long string, and then packing this string eight bits to a byte. |
| 544 |
The number of bytes required can be calculated by |(width*height+7) div 8|. |
| 545 |
This format should only be used when packing the character by run counts takes |
| 546 |
more bytes than this, although, of course, it is legal for any character. |
| 547 |
Any extra bits in the last byte should be set to zero. |
| 548 |
|
| 549 |
@ At this point, we are ready to introduce the format for a character |
| 550 |
descriptor. It consists of three parts: a flag byte, a character preamble, |
| 551 |
and the raster data. The most significant four bits of the flag byte |
| 552 |
yield the |dyn_f| value for that character. (Notice that only values of |
| 553 |
0 through 14 are legal for |dyn_f|, with 14 indicating a bit mapped character; |
| 554 |
thus, the flag bytes do not conflict with the command bytes, whose upper nybble |
| 555 |
is always 15.) The next bit (with weight 8) indicates whether the first run |
| 556 |
count is a black count or a white count, with a one indicating a black count. |
| 557 |
For bit-mapped characters, this bit should be set to a zero. The next bit |
| 558 |
(with weight 4) indicates whether certain later parameters (referred to as size |
| 559 |
parameters) are given in one-byte or two-byte quantities, with a one indicating |
| 560 |
that they are in two-byte quantities. The last two bits are concatenated on to |
| 561 |
the beginning of the packet-length parameter in the character preamble, |
| 562 |
which will be explained below. |
| 563 |
|
| 564 |
However, if the last three bits of the flag byte are all set (normally |
| 565 |
indicating that the size parameters are two-byte values and that a 3 should be |
| 566 |
prepended to the length parameter), then a long format of the character |
| 567 |
preamble should be used instead of one of the short forms. |
| 568 |
|
| 569 |
Therefore, there are three formats for the character preamble; the one that |
| 570 |
is used depends on the least significant three bits of the flag byte. If the |
| 571 |
least significant three bits are in the range zero through three, the short |
| 572 |
format is used. If they are in the range four through six, the extended short |
| 573 |
format is used. Otherwise, if the least significant bits are all set, then |
| 574 |
the long form of the character preamble is used. The preamble formats are |
| 575 |
explained below. |
| 576 |
|
| 577 |
\yskip\hang Short form: |flag[1]| |pl[1]| |cc[1]| |tfm[3]| |dm[1]| |w[1]| |
| 578 |
|h[1]| |hoff[+1]| |voff[+1]|. |
| 579 |
If this format of the character preamble is used, the above |
| 580 |
parameters must all fit in the indicated number of bytes, signed or unsigned |
| 581 |
as indicated. Almost all of the standard \TeX\ font characters fit; the few |
| 582 |
exceptions are fonts such as \.{cminch}. |
| 583 |
|
| 584 |
\yskip\hang Extended short form: |flag[1]| |pl[2]| |cc[1]| |tfm[3]| |dm[2]| |
| 585 |
|w[2]| |h[2]| |hoff[+2]| |voff[+2]|. Larger characters use this extended |
| 586 |
format. |
| 587 |
|
| 588 |
\yskip\hang Long form: |flag[1]| |pl[4]| |cc[4]| |tfm[4]| |dx[4]| |dy[4]| |
| 589 |
|w[4]| |h[4]| |hoff[4]| |voff[4]|. This is the general format that |
| 590 |
allows all of the |
| 591 |
parameters of the \.{GF} file format, including vertical escapement. |
| 592 |
\vskip\baselineskip |
| 593 |
The |flag| parameter is the flag byte. The parameter |pl| (packet length) |
| 594 |
contains the offset |
| 595 |
of the byte following this character descriptor, with respect to the beginning |
| 596 |
of the |tfm| width parameter. This is given so a \.{PK} reading program can, |
| 597 |
once it has read the flag byte, packet length, and character code (|cc|), skip |
| 598 |
over the character by simply reading this many more bytes. For the two short |
| 599 |
forms of the character preamble, the last two bits of the flag byte should be |
| 600 |
considered the two most-significant bits of the packet length. For the short |
| 601 |
format, the true packet length might be calculated as |(flag mod 4)*256+pl|; |
| 602 |
for the short extended format, it might be calculated as |
| 603 |
|(flag mod 4)*65536+pl|. |
| 604 |
|
| 605 |
The |w| parameter is the width and the |h| parameter is the height in pixels |
| 606 |
of the minimum bounding box. The |dx| and |dy| parameters are the horizontal |
| 607 |
and vertical escapements, respectively. In the short formats, |dy| is assumed |
| 608 |
to be zero and |dm| is |dx| but in pixels; |
| 609 |
in the long format, |dx| and |dy| are both |
| 610 |
in pixels multiplied by $2^{16}$. The |hoff| is the horizontal offset from the |
| 611 |
upper left pixel to the reference pixel; the |voff| is the vertical offset. |
| 612 |
They are both given in pixels, with right and down being positive. The |
| 613 |
reference pixel is the pixel that occupies the unit square in \MF; the |
| 614 |
\MF\ reference point is the lower left hand corner of this pixel. (See the |
| 615 |
example below.) |
| 616 |
|
| 617 |
@ \TeX\ requires all characters that have the same character codes |
| 618 |
modulo 256 to have also the same |tfm| widths and escapement values. The \.{PK} |
| 619 |
format does not itself make this a requirement, but in order for the font to |
| 620 |
work correctly with the \TeX\ software, this constraint should be observed. |
| 621 |
(The standard version of \TeX\ cannot output character codes greater |
| 622 |
than 255, but extended versions do exist.) |
| 623 |
|
| 624 |
Following the character preamble is the raster information for the |
| 625 |
character, packed by run counts or by bits, as indicated by the flag byte. |
| 626 |
If the character is packed by run counts and the required number of nybbles |
| 627 |
is odd, then the last byte of the raster description should have a zero |
| 628 |
for its least significant nybble. |
| 629 |
|
| 630 |
@ As an illustration of the \.{PK} format, the character \char4\ from the font |
| 631 |
amr10 at 300 dots per inch will be encoded. This character was chosen |
| 632 |
because it illustrates some |
| 633 |
of the borderline cases. The raster for the character looks like this (the |
| 634 |
row numbers are chosen for convenience, and are not \MF's row numbers.) |
| 635 |
|
| 636 |
\vskip\baselineskip |
| 637 |
{\def\smbox{\vrule height 7pt width 7pt depth 0pt \hskip 3pt}% |
| 638 |
\catcode`\*=\active \let*=\smbox |
| 639 |
\centerline{\vbox{\baselineskip=10pt |
| 640 |
\halign{\hfil#\quad&&\hfil#\hfil\cr |
| 641 |
0& & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 642 |
1& & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 643 |
2& & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 644 |
3& & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 645 |
4& & &*&*& & & & & & & & & & & & & & & & &*&*\cr |
| 646 |
5& & &*&*& & & & & & & & & & & & & & & & &*&*\cr |
| 647 |
6& & &*&*& & & & & & & & & & & & & & & & &*&*\cr |
| 648 |
7\cr |
| 649 |
8\cr |
| 650 |
9& & & & &*&*& & & & & & & & & & & & &*&*& & \cr |
| 651 |
10& & & & &*&*& & & & & & & & & & & & &*&*& & \cr |
| 652 |
11& & & & &*&*& & & & & & & & & & & & &*&*& & \cr |
| 653 |
12& & & & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*& & \cr |
| 654 |
13& & & & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*& & \cr |
| 655 |
14& & & & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*& & \cr |
| 656 |
15& & & & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*& & \cr |
| 657 |
16& & & & &*&*& & & & & & & & & & & & &*&*& & \cr |
| 658 |
17& & & & &*&*& & & & & & & & & & & & &*&*& & \cr |
| 659 |
18& & & & &*&*& & & & & & & & & & & & &*&*& & \cr |
| 660 |
19\cr |
| 661 |
20\cr |
| 662 |
21\cr |
| 663 |
22& & &*&*& & & & & & & & & & & & & & & & &*&*\cr |
| 664 |
23& & &*&*& & & & & & & & & & & & & & & & &*&*\cr |
| 665 |
24& & &*&*& & & & & & & & & & & & & & & & &*&*\cr |
| 666 |
25& & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 667 |
26& & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 668 |
27& & &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 669 |
28&+& &*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*\cr |
| 670 |
&\hphantom{*}&\hphantom{*}\cr |
| 671 |
}}}} |
| 672 |
The width of the minimum bounding box for this character is 20; its height |
| 673 |
is 29. The `+' represents the reference pixel; notice how it lies outside the |
| 674 |
minimum bounding box. The |hoff| value is $-2$, and the |voff| is~28. |
| 675 |
|
| 676 |
The first task is to calculate the run counts and repeat counts. The repeat |
| 677 |
counts are placed at the first transition (black to white or white to black) |
| 678 |
in a row, and are enclosed in brackets. White counts are enclosed in |
| 679 |
parentheses. It is relatively easy to generate the counts list: |
| 680 |
\vskip\baselineskip |
| 681 |
\centerline{82 [2] (16) 2 (42) [2] 2 (12) 2 (4) [3]} |
| 682 |
\centerline{16 (4) [2] 2 (12) 2 (62) [2] 2 (16) 82} |
| 683 |
\vskip\baselineskip |
| 684 |
Note that any duplicated rows that are not all white or all black are removed |
| 685 |
before the run counts are calculated. The rows thus removed are rows 5, 6, |
| 686 |
10, 11, 13, 14, 15, 17, 18, 23, and 24. |
| 687 |
|
| 688 |
@ The next step in the encoding of this character is to calculate the optimal |
| 689 |
value of |dyn_f|. The details of how this calculation is done are not |
| 690 |
important here; suffice it to say that there is a simple algorithm that can |
| 691 |
determine the best value of |dyn_f| in one pass over the count list. For this |
| 692 |
character, the optimal value turns out to be 8 (atypically low). Thus, all |
| 693 |
count values less than or equal to 8 are packed in one nybble; those from |
| 694 |
nine to $(13-8)*16+8$ or 88 are packed in two nybbles. The run encoded values |
| 695 |
now become (in hex, separated according to the above list): |
| 696 |
\vskip\baselineskip |
| 697 |
\centerline{\tt D9 E2 97 2 B1 E2 2 93 2 4 E3} |
| 698 |
\centerline{\tt 97 4 E2 2 93 2 C5 E2 2 97 D9} |
| 699 |
\vskip\baselineskip\noindent |
| 700 |
which comes to 36 nybbles, or 18 bytes. This is shorter than the 73 bytes |
| 701 |
required for the bit map, so we use the run count packing. |
| 702 |
|
| 703 |
@ The short form of the character preamble is used because all of the |
| 704 |
parameters fit in their respective lengths. The packet length is therefore |
| 705 |
18 bytes for the raster, plus |
| 706 |
eight bytes for the character preamble parameters following the character |
| 707 |
code, or 26. The |tfm| width for this character is 640796, or {\tt 9C71C} in |
| 708 |
hexadecimal. The horizontal escapement is 25 pixels. The flag byte is |
| 709 |
88 hex, indicating the short preamble, the black first count, and the |
| 710 |
|dyn_f| value of 8. The final total character packet, in hexadecimal, is: |
| 711 |
\vskip\baselineskip |
| 712 |
$$\vbox{\halign{\hfil #\quad&&{\tt #\ }\cr |
| 713 |
Flag byte&88\cr |
| 714 |
Packet length&1A\cr |
| 715 |
Character code&04\cr |
| 716 |
|tfm| width&09&C7&1C\cr |
| 717 |
Horizontal escapement (pixels)&19\cr |
| 718 |
Width of bit map&14\cr |
| 719 |
Height of bit map&1D\cr |
| 720 |
Horizontal offset (signed)&FE\cr |
| 721 |
Vertical offset&1C\cr |
| 722 |
Raster data&D9&E2&97\cr |
| 723 |
&2B&1E&22\cr |
| 724 |
&93&24&E3\cr |
| 725 |
&97&4E&22\cr |
| 726 |
&93&2C&5E\cr |
| 727 |
&22&97&D9\cr}}$$ |
| 728 |
|
| 729 |
@* Input and output. |
| 730 |
There are two types of files that this program must deal with---standard |
| 731 |
text files and files of bytes (packed files.) |
| 732 |
For our purposes, we shall consider an eight-bit byte to consist of the |
| 733 |
values |0..255|. If your system does not pack these values to a byte, it is |
| 734 |
no major difficulty; you must only insure that the input function |
| 735 |
|pk_byte| can read packed bytes. |
| 736 |
|
| 737 |
@<Types...@>= |
| 738 |
@!eight_bits=0..255; {packed file byte} |
| 739 |
@!byte_file=packed file of eight_bits ; {for packed file words} |
| 740 |
@^system dependencies@> |
| 741 |
|
| 742 |
@ @<Glob...@>= |
| 743 |
@!pk_file:byte_file; {where the input comes from} |
| 744 |
@!typ_file:text_file; {where the final output goes} |
| 745 |
@^system dependencies@> |
| 746 |
|
| 747 |
@ To prepare these files for input and output, we |reset| and |rewrite| them. |
| 748 |
An extension of \PASCAL\ is needed, since we want to associate files |
| 749 |
with external names that are specified dynamically (i.e., not |
| 750 |
known at compile time). The following code assumes that `|reset(f,s)|' |
| 751 |
does this, when |f| is a file variable and |s| is a string variable that |
| 752 |
specifies the file name. If |eof(f)| is true immediately after |
| 753 |
|reset(f,s)| has acted, we assume that no file named |s| is accessible. |
| 754 |
@^system dependencies@> |
| 755 |
|
| 756 |
@p procedure open_pk_file; {prepares the input for reading} |
| 757 |
begin reset(pk_file,pk_name); |
| 758 |
pk_loc := 0 ; |
| 759 |
end; |
| 760 |
@# |
| 761 |
procedure open_typ_file; {prepares to write text data to the |typ_file|} |
| 762 |
begin rewrite(typ_file,typ_name); |
| 763 |
end; |
| 764 |
|
| 765 |
@ We need a place to store the names of the input and output file, as well |
| 766 |
as a byte counter for the output file. |
| 767 |
|
| 768 |
@<Glob...@>= |
| 769 |
@!pk_name,@!typ_name:packed array[1..name_length] of char; {name of input |
| 770 |
and output files} |
| 771 |
@!pk_loc:integer; {how many bytes have we read?} |
| 772 |
|
| 773 |
@ We also need a function that will get a single byte from the \.{pk} file. |
| 774 |
|
| 775 |
@p function pk_byte : eight_bits ; |
| 776 |
var temp : eight_bits ; |
| 777 |
begin |
| 778 |
temp := pk_file^ ; |
| 779 |
get(pk_file) ; |
| 780 |
incr(pk_loc) ; |
| 781 |
pk_byte := temp ; |
| 782 |
end ; |
| 783 |
|
| 784 |
@ Now we are ready to open the files. |
| 785 |
|
| 786 |
@<Open files@>= |
| 787 |
open_pk_file ; |
| 788 |
open_typ_file ; |
| 789 |
t_print_ln(banner) ; |
| 790 |
t_print('Input file: ') ; |
| 791 |
i := 1 ; |
| 792 |
while pk_name[i] <> ' ' do begin |
| 793 |
t_print(pk_name[i]) ; incr(i) ; |
| 794 |
end ; |
| 795 |
t_print_ln(' ') |
| 796 |
|
| 797 |
@ As we are reading the packed file, we often need to fetch 16 and 32 bit |
| 798 |
quantities. Here we have two procedures to do this. |
| 799 |
|
| 800 |
@p function get_16 : integer ; |
| 801 |
var a : integer ; |
| 802 |
begin a := pk_byte ; get_16 := a * 256 + pk_byte ; end ; |
| 803 |
@# |
| 804 |
function get_32 : integer ; |
| 805 |
var a : integer ; |
| 806 |
begin a := get_16 ; if a > 32767 then a := a - 65536 ; |
| 807 |
get_32 := a * 65536 + get_16 ; end ; |
| 808 |
|
| 809 |
@ We still need the |term_pos| variable. |
| 810 |
|
| 811 |
@<Glob...@>= |
| 812 |
@!term_pos : integer ; {current terminal position} |
| 813 |
|
| 814 |
@ Now we read and check the preamble of the \.{PK} file. In the preamble, we |
| 815 |
find the |hppp|, |design_size|, |checksum|. |
| 816 |
|
| 817 |
@<Read preamble@>= |
| 818 |
if pk_byte <> pk_pre then abort('Bad PK file: pre command missing!') ; |
| 819 |
@.pre command missing@> |
| 820 |
if pk_byte <> pk_id then abort('Wrong version of PK file!') ; |
| 821 |
@.Wrong version of PK file@> |
| 822 |
j := pk_byte ; |
| 823 |
t_print('''') ; |
| 824 |
for i := 1 to j do t_print(xchr[pk_byte]) ; |
| 825 |
t_print_ln('''') ; |
| 826 |
design_size := get_32 ; |
| 827 |
t_print_ln('Design size = ',design_size:1) ; |
| 828 |
checksum := get_32 ; |
| 829 |
t_print_ln('Checksum = ',checksum:1) ; |
| 830 |
hppp := get_32 ; vppp := get_32 ; |
| 831 |
t_print('Resolution: horizontal = ',hppp:1,' vertical = ',vppp:1) ; |
| 832 |
magnification := round(hppp * 72.27 / 65536) ; |
| 833 |
t_print_ln(' (',magnification:1,' dpi)') ; |
| 834 |
if hppp <> vppp then print_ln('Warning: aspect ratio not 1:1!') |
| 835 |
|
| 836 |
@ Of course, we need to define the above variables. |
| 837 |
|
| 838 |
@<Glob...@>= |
| 839 |
@!magnification : integer ; {resolution at which pixel file is prepared} |
| 840 |
@!design_size : integer ; {design size in FIXes} |
| 841 |
@!checksum : integer ; {checksum of pixel file} |
| 842 |
@!hppp, @!vppp : integer ; {horizontal and vertical points per inch} |
| 843 |
|
| 844 |
@* Character unpacking. |
| 845 |
Here we simply unpack the character, writing the information we glean to the |
| 846 |
|typ_file|. |
| 847 |
|
| 848 |
@<Unpack and write character@>= |
| 849 |
t_print((pk_loc-1):1,': Flag byte = ',flag_byte:1) ; |
| 850 |
dyn_f := flag_byte div 16 ; |
| 851 |
flag_byte := flag_byte mod 16 ; |
| 852 |
turn_on := flag_byte >= 8 ; |
| 853 |
if turn_on then flag_byte := flag_byte - 8 ; |
| 854 |
if flag_byte = 7 then |
| 855 |
@<Read long character preamble@> |
| 856 |
else if flag_byte > 3 then |
| 857 |
@<Read extended short character preamble@> |
| 858 |
else |
| 859 |
@<Read short character preamble@> ; |
| 860 |
t_print_ln(' Character = ',car:1,' Packet length = ', |
| 861 |
packet_length:1) ; |
| 862 |
t_print_ln(' Dynamic packing variable = ',dyn_f:1) ; |
| 863 |
t_print(' TFM width = ',tfm_width:1,' dx = ',dx:1) ; |
| 864 |
if dy <> 0 then t_print_ln(' dy = ',dy:1) else t_print_ln(' ') ; |
| 865 |
t_print_ln(' Height = ',height:1,' Width = ',width:1,' X-offset = ', |
| 866 |
x_off:1, ' Y-offset = ',y_off:1) ; |
| 867 |
@<Read and translate raster description@> ; |
| 868 |
if end_of_packet <> pk_loc then abort('Bad PK file: Bad packet length!') |
| 869 |
@.Bad packet length@> |
| 870 |
|
| 871 |
@ We need a whole lot of globals used but not defined up there. |
| 872 |
|
| 873 |
@<Glob...@>= |
| 874 |
@!i, @!j : integer ; {index pointers} |
| 875 |
@!flag_byte : integer ; {the byte that introduces the character definition} |
| 876 |
@!end_of_packet : integer ; {where we expect the end of the packet to be} |
| 877 |
@!width, @!height : integer ; {width and height of character} |
| 878 |
@!x_off, @!y_off : integer ; {x and y offsets of character} |
| 879 |
@!tfm_width : integer ; {character tfm width} |
| 880 |
@!tfms : array [0..255] of integer ; {character tfm widths} |
| 881 |
@!dx, @!dy : integer ; {escapement values} |
| 882 |
@!dxs, @!dys : array [0..255] of integer ; {escapement values} |
| 883 |
@!status : array[0..255] of boolean ; {has the character been seen?} |
| 884 |
@!dyn_f : integer ; {dynamic packing variable} |
| 885 |
@!car : integer ; {the character we are reading} |
| 886 |
@!packet_length : integer ; {the length of the character packet} |
| 887 |
|
| 888 |
@ Now, the preamble reading modules. First, we have the general case: the |
| 889 |
long character preamble format. |
| 890 |
|
| 891 |
@<Read long character preamble@>= |
| 892 |
begin |
| 893 |
packet_length := get_32 ; car := get_32 ; |
| 894 |
end_of_packet := packet_length + pk_loc ; |
| 895 |
packet_length := packet_length + 9 ; |
| 896 |
tfm_width := get_32 ; |
| 897 |
dx := get_32 ; dy := get_32 ; |
| 898 |
width := get_32 ; |
| 899 |
height := get_32 ; |
| 900 |
x_off := get_32 ; y_off := get_32 ; |
| 901 |
end |
| 902 |
|
| 903 |
@ This module reads the character preamble with double byte parameters. |
| 904 |
|
| 905 |
@<Read extended short character preamble@>= |
| 906 |
begin |
| 907 |
packet_length := (flag_byte - 4) * 65536 + get_16 ; |
| 908 |
car := pk_byte ; |
| 909 |
end_of_packet := packet_length + pk_loc ; |
| 910 |
packet_length := packet_length + 4 ; |
| 911 |
i := pk_byte ; |
| 912 |
tfm_width := i * 65536 + get_16 ; |
| 913 |
dx := get_16 * 65536 ; |
| 914 |
dy := 0 ; |
| 915 |
width := get_16 ; |
| 916 |
height := get_16 ; |
| 917 |
x_off := get_16 ; y_off := get_16 ; |
| 918 |
if x_off > 32767 then x_off := x_off - 65536 ; |
| 919 |
if y_off > 32767 then y_off := y_off - 65536 ; |
| 920 |
end |
| 921 |
|
| 922 |
@ Here we read the most common character preamble, that with single byte |
| 923 |
parameters. |
| 924 |
|
| 925 |
@<Read short character preamble@>= |
| 926 |
begin |
| 927 |
packet_length := flag_byte * 256 + pk_byte ; |
| 928 |
car := pk_byte ; |
| 929 |
end_of_packet := packet_length + pk_loc ; |
| 930 |
packet_length := packet_length + 3 ; |
| 931 |
i := pk_byte ; |
| 932 |
tfm_width := i * 65536 + get_16 ; |
| 933 |
dx := pk_byte * 65536 ; |
| 934 |
dy := 0 ; |
| 935 |
width := pk_byte ; |
| 936 |
height := pk_byte ; |
| 937 |
x_off := pk_byte ; y_off := pk_byte ; |
| 938 |
if x_off > 127 then x_off := x_off - 256 ; |
| 939 |
if y_off > 127 then y_off := y_off - 256 ; |
| 940 |
end |
| 941 |
|
| 942 |
@ Now we have the most important part of the program, where we actually |
| 943 |
interpret the commands in the raster description. First of all, we need |
| 944 |
a procedure to get a single nybble from the file, as well as one to get |
| 945 |
a single bit. |
| 946 |
|
| 947 |
@p function get_nyb : integer ; |
| 948 |
var temp : eight_bits ; |
| 949 |
begin |
| 950 |
if bit_weight = 0 then begin |
| 951 |
input_byte := pk_byte ; |
| 952 |
bit_weight := 16 ; |
| 953 |
end ; |
| 954 |
temp := input_byte div bit_weight ; |
| 955 |
input_byte := input_byte - temp * bit_weight ; |
| 956 |
bit_weight := bit_weight div 16 ; |
| 957 |
get_nyb := temp ; |
| 958 |
end ; |
| 959 |
@# |
| 960 |
function get_bit : boolean ; |
| 961 |
var temp : boolean ; |
| 962 |
begin |
| 963 |
bit_weight := bit_weight div 2 ; |
| 964 |
if bit_weight = 0 then begin |
| 965 |
input_byte := pk_byte ; |
| 966 |
bit_weight := 128 ; |
| 967 |
end ; |
| 968 |
temp := input_byte >= bit_weight ; |
| 969 |
if temp then |
| 970 |
input_byte := input_byte - bit_weight ; |
| 971 |
get_bit := temp ; |
| 972 |
end ; |
| 973 |
|
| 974 |
@ We also need a function to write output to the screen. We put as many |
| 975 |
counts on a line as possible, to reduce the volume of output. Each count |
| 976 |
will appear as a number, with white counts enclosed by parentheses and repeat |
| 977 |
counts by brackets. |
| 978 |
|
| 979 |
@p procedure send_out(@!repeat_count: boolean ; @!value : integer ) ; |
| 980 |
var i, len : integer ; |
| 981 |
begin |
| 982 |
i := 10 ; len := 1 ; |
| 983 |
while value >= i do begin incr(len) ; i := i * 10 ; end ; |
| 984 |
if repeat_count or not turn_on then len := len + 2 ; |
| 985 |
if term_pos + len > 78 then begin |
| 986 |
term_pos := len + 2 ; t_print_ln(' ') ; t_print(' ') ; |
| 987 |
end else term_pos := term_pos + len ; |
| 988 |
if repeat_count then t_print('[', value:1, ']') |
| 989 |
else if turn_on then t_print(value:1) |
| 990 |
else t_print('(', value:1, ')') ; |
| 991 |
end ; |
| 992 |
@<Packed number procedure@> |
| 993 |
|
| 994 |
@ Now, the globals to help communication between these procedures. |
| 995 |
|
| 996 |
@<Glob...@>= |
| 997 |
@!input_byte : eight_bits ; {the byte we are currently decimating} |
| 998 |
@!bit_weight : eight_bits ; {weight of the current bit} |
| 999 |
@!nybble : eight_bits ; {the current nybble} |
| 1000 |
|
| 1001 |
@ And the main procedure. |
| 1002 |
|
| 1003 |
@<Read and translate raster description@>= |
| 1004 |
bit_weight := 0 ; |
| 1005 |
if dyn_f = 14 then |
| 1006 |
@<Get raster by bits@> |
| 1007 |
else @<Create normally packed raster@> |
| 1008 |
|
| 1009 |
@ If |dyn_f=14|, then we need to get the raster representation |
| 1010 |
one bit at a time. |
| 1011 |
|
| 1012 |
@<Get raster by bits@>= |
| 1013 |
begin |
| 1014 |
for i := 1 to height do begin |
| 1015 |
t_print(' ') ; |
| 1016 |
for j := 1 to width do |
| 1017 |
if get_bit then t_print('*') |
| 1018 |
else t_print('.') ; |
| 1019 |
t_print_ln(' ') ; |
| 1020 |
end; |
| 1021 |
end |
| 1022 |
|
| 1023 |
@ Otherwise, we translate the bit counts into the raster rows. |count| |
| 1024 |
contains the number of bits of the current color, and |turn_on| indicates |
| 1025 |
whether or not they should be black. |rows_left| contains the number of |
| 1026 |
rows to be sent. |
| 1027 |
|
| 1028 |
@<Create normally packed raster@>= |
| 1029 |
begin |
| 1030 |
term_pos := 2 ; |
| 1031 |
t_print(' ') ; |
| 1032 |
rows_left := height ; |
| 1033 |
h_bit := width ; |
| 1034 |
repeat_count := 0 ; |
| 1035 |
while rows_left > 0 do begin |
| 1036 |
count := pk_packed_num ; |
| 1037 |
send_out(false, count) ; |
| 1038 |
if count >= h_bit then begin |
| 1039 |
rows_left := rows_left - repeat_count - 1 ; |
| 1040 |
repeat_count := 0 ; |
| 1041 |
count := count - h_bit ; |
| 1042 |
h_bit := width ; |
| 1043 |
rows_left := rows_left - count div width ; |
| 1044 |
count := count mod width ; |
| 1045 |
end ; |
| 1046 |
h_bit := h_bit - count ; |
| 1047 |
turn_on := not turn_on ; |
| 1048 |
end ; |
| 1049 |
t_print_ln(' ') ; |
| 1050 |
if (rows_left <> 0) or (h_bit <> width) then |
| 1051 |
abort('Bad PK file: More bits than required!'); |
| 1052 |
@.More bits than required@> |
| 1053 |
end |
| 1054 |
|
| 1055 |
@ We need to declare the repeat flag, bit counter, and color flag here. |
| 1056 |
|
| 1057 |
@<Glob...@>= |
| 1058 |
@!repeat_count : integer ; {how many times to repeat the next row?} |
| 1059 |
@!rows_left : integer ; {how many rows left?} |
| 1060 |
@!turn_on : boolean ; {are we black here?} |
| 1061 |
@!h_bit : integer ; {what is our horizontal position?} |
| 1062 |
@!count : integer ; {how many bits of current color left?} |
| 1063 |
|
| 1064 |
@ If any specials are found, we write them out here. |
| 1065 |
|
| 1066 |
@d four_cases(#)==#,#+1,#+2,#+3 |
| 1067 |
|
| 1068 |
@p procedure skip_specials ; |
| 1069 |
var @!i, @!j: integer ; |
| 1070 |
begin |
| 1071 |
repeat |
| 1072 |
flag_byte := pk_byte ; |
| 1073 |
if flag_byte >= 240 then |
| 1074 |
case flag_byte of |
| 1075 |
four_cases(pk_xxx1): |
| 1076 |
begin |
| 1077 |
t_print((pk_loc-1):1,': Special: ''') ; |
| 1078 |
i := 0 ; |
| 1079 |
for j := pk_xxx1 to flag_byte do i := 256 * i + pk_byte ; |
| 1080 |
for j := 1 to i do t_print(xchr[pk_byte]) ; |
| 1081 |
t_print_ln('''') ; |
| 1082 |
end ; |
| 1083 |
pk_yyy : t_print_ln((pk_loc-1):1,': Num special: ',get_32:1) ; |
| 1084 |
pk_post : t_print_ln((pk_loc-1):1,': Postamble') ; |
| 1085 |
pk_no_op : t_print_ln((pk_loc-1):1,': No op') ; |
| 1086 |
pk_pre, pk_undefined : abort('Unexpected ', flag_byte:1,'!') ; |
| 1087 |
@.Unexpected bbb@> |
| 1088 |
endcases ; |
| 1089 |
until (flag_byte < 240) or (flag_byte = pk_post) ; |
| 1090 |
end ; |
| 1091 |
|
| 1092 |
@* Terminal communication. |
| 1093 |
We must get the file names and determine whether input is to be in |
| 1094 |
hexadecimal or binary. To do this, we use the standard input path |
| 1095 |
name. We need a procedure to flush the input buffer. For most systems, |
| 1096 |
this will be an empty statement. For other systems, a |print_ln| will |
| 1097 |
provide a quick fix. We also need a routine to get a line of input from |
| 1098 |
the terminal. On some systems, a simple |read_ln| will do. Finally, |
| 1099 |
a macro to print a string to the first blank is required. |
| 1100 |
|
| 1101 |
@d flush_buffer == begin end |
| 1102 |
@d get_line(#) == if eoln(input) then read_ln(input) ; |
| 1103 |
i := 1 ; |
| 1104 |
while not (eoln(input) or eof(input)) do begin |
| 1105 |
#[i] := input^ ; |
| 1106 |
incr(i) ; |
| 1107 |
get(input) ; |
| 1108 |
end ; |
| 1109 |
#[i] := ' ' |
| 1110 |
|
| 1111 |
@ @p procedure dialog ; |
| 1112 |
var i : integer ; {index variable} |
| 1113 |
buffer : packed array [1..name_length] of char; {input buffer} |
| 1114 |
begin |
| 1115 |
for i := 1 to name_length do begin |
| 1116 |
typ_name[i] := ' ' ; |
| 1117 |
pk_name[i] := ' ' ; |
| 1118 |
end; |
| 1119 |
print('Input file name: ') ; |
| 1120 |
flush_buffer ; |
| 1121 |
get_line(pk_name) ; |
| 1122 |
print('Output file name: ') ; |
| 1123 |
flush_buffer ; |
| 1124 |
get_line(typ_name) ; |
| 1125 |
end ; |
| 1126 |
|
| 1127 |
@* The main program. |
| 1128 |
Now that we have all the pieces written, let us put them together. |
| 1129 |
|
| 1130 |
@p begin |
| 1131 |
initialize ; |
| 1132 |
dialog ; |
| 1133 |
@<Open files@> ; |
| 1134 |
@<Read preamble@> ; |
| 1135 |
skip_specials ; |
| 1136 |
while flag_byte <> pk_post do begin |
| 1137 |
@<Unpack and write character@> ; |
| 1138 |
skip_specials ; |
| 1139 |
end ; |
| 1140 |
j := 0 ; |
| 1141 |
while not eof(pk_file) do begin |
| 1142 |
i := pk_byte ; |
| 1143 |
if i <> pk_no_op then abort('Bad byte at end of file: ',i:1) ; |
| 1144 |
@.Bad byte at end of file@> |
| 1145 |
t_print_ln((pk_loc-1):1,': No op') ; |
| 1146 |
incr(j) ; |
| 1147 |
end ; |
| 1148 |
t_print_ln(pk_loc:1,' bytes read from packed file.'); |
| 1149 |
final_end : |
| 1150 |
end . |
| 1151 |
|
| 1152 |
@* System-dependent changes. |
| 1153 |
This section should be replaced, if necessary, by changes to the program |
| 1154 |
that are necessary to make \.{PKtype} work at a particular installation. |
| 1155 |
Any additional routines should be inserted here. |
| 1156 |
@^system dependencies@> |
| 1157 |
|
| 1158 |
@* Index. |
| 1159 |
Pointers to error messages appear here together with the section numbers |
| 1160 |
where each ident\-i\-fier is used. |