| 1 |
% gftopk.ch for C compilation with web2c. |
| 2 |
% |
| 3 |
% 09/19/88 Pierre A. MacKay Version 1.4. |
| 4 |
% 12/02/89 Karl Berry 2.1. |
| 5 |
% 01/20/90 Karl 2.2. |
| 6 |
% (more recent changes in the ChangeLog) |
| 7 |
% |
| 8 |
% One major change in output format is made by this change file. The |
| 9 |
% local gftopk preamble comment is ignored and the dated METAFONT |
| 10 |
% comment is passed through unaltered. This provides a continuous check |
| 11 |
% on the origin of fonts in both gf and pk formats. The program runs |
| 12 |
% silently unless it is given the -v switch in the command line. |
| 13 |
|
| 14 |
@x [0] l.41 |
| 15 |
\def\(#1){} % this is used to make section names sort themselves better |
| 16 |
@y |
| 17 |
\def\({} % this is used to make section names sort themselves better |
| 18 |
@z |
| 19 |
|
| 20 |
@x [0] l.44 |
| 21 |
\def\title{GFtoPK} |
| 22 |
@y |
| 23 |
\def\title{GF$\,$\lowercase{to}$\,$PK changes C} |
| 24 |
@z |
| 25 |
|
| 26 |
@x [0] WEAVE: print changes only. |
| 27 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 28 |
@y |
| 29 |
\pageno=\contentspagenumber \advance\pageno by 1 |
| 30 |
\let\maybe=\iffalse |
| 31 |
@z |
| 32 |
|
| 33 |
@x [1] Define my_name |
| 34 |
@d banner=='This is GFtoPK, Version 2.4' {printed when the program starts} |
| 35 |
@y |
| 36 |
@d my_name=='gftopk' |
| 37 |
@d banner=='This is GFtoPK, Version 2.4' {printed when the program starts} |
| 38 |
@z |
| 39 |
|
| 40 |
@x [4] No global labels. |
| 41 |
@ The binary input comes from |gf_file|, and the output font is written |
| 42 |
on |pk_file|. All text output is written on \PASCAL's standard |output| |
| 43 |
file. The term |print| is used instead of |write| when this program writes |
| 44 |
on |output|, so that all such output could easily be redirected if desired. |
| 45 |
|
| 46 |
@d print(#)==write(#) |
| 47 |
@d print_ln(#)==write_ln(#) |
| 48 |
|
| 49 |
@p program GFtoPK(@!gf_file,@!pk_file,@!output); |
| 50 |
label @<Labels in the outer block@>@/ |
| 51 |
const @<Constants in the outer block@>@/ |
| 52 |
type @<Types in the outer block@>@/ |
| 53 |
var @<Globals in the outer block@>@/ |
| 54 |
procedure initialize; {this procedure gets things started properly} |
| 55 |
var i:integer; {loop index for initializations} |
| 56 |
begin print_ln(banner);@/ |
| 57 |
@y |
| 58 |
@ The binary input comes from |gf_file|, and the output font is written |
| 59 |
on |pk_file|. All text output is written on \PASCAL's standard |output| |
| 60 |
file. The term |print| is used instead of |write| when this program writes |
| 61 |
on |output|, so that all such output could easily be redirected if desired. |
| 62 |
Since the terminal output is really not very interesting, it is |
| 63 |
produced only when the \.{-v} command line flag is presented. |
| 64 |
|
| 65 |
@d print(#)==if verbose then write(stdout, #) |
| 66 |
@d print_ln(#)==if verbose then write_ln(stdout, #) |
| 67 |
|
| 68 |
@p program GFtoPK(@!gf_file,@!pk_file,@!output); |
| 69 |
const @<Constants in the outer block@>@/ |
| 70 |
type @<Types in the outer block@>@/ |
| 71 |
var @<Globals in the outer block@>@/ |
| 72 |
@<Define \(|parse_arguments|@> |
| 73 |
procedure initialize; {this procedure gets things started properly} |
| 74 |
var i:integer; {loop index for initializations} |
| 75 |
begin |
| 76 |
kpse_set_program_name (argv[0], my_name); |
| 77 |
kpse_init_prog ('GFTOPK', 0, nil, nil); |
| 78 |
parse_arguments; |
| 79 |
print(banner); print_ln(version_string); |
| 80 |
@z |
| 81 |
|
| 82 |
@x [5] Eliminate the |final_end| label. |
| 83 |
@ If the program has to stop prematurely, it goes to the |
| 84 |
`|final_end|'. |
| 85 |
|
| 86 |
@d final_end=9999 {label for the end of it all} |
| 87 |
|
| 88 |
@<Labels...@>=final_end; |
| 89 |
@y |
| 90 |
@ This module is deleted, because it is only useful for |
| 91 |
a non-local |goto|\unskip, which we can't use in C. |
| 92 |
@z |
| 93 |
|
| 94 |
@x [6] Dynamic allocation of |row| array. |
| 95 |
@!max_row=16000; {largest index in the main |row| array} |
| 96 |
@y |
| 97 |
@!MAX_ROW=16000; {largest index in the initial main |row| array} |
| 98 |
@z |
| 99 |
|
| 100 |
@x [8] Make `abort' end with a newline, and remove the nonlocal goto. |
| 101 |
so a procedure called |jump_out| has been introduced. This procedure, which |
| 102 |
simply transfers control to the label |final_end| at the end of the program, |
| 103 |
contains the only non-local |goto| statement in \.{GFtoPK}. |
| 104 |
@y |
| 105 |
so we might want to |abort| the program with an error message. |
| 106 |
@z |
| 107 |
@x [8] |
| 108 |
@d abort(#)==begin print(' ',#); jump_out; |
| 109 |
end |
| 110 |
@d bad_gf(#)==abort('Bad GF file: ',#,'!') |
| 111 |
@.Bad GF file@> |
| 112 |
|
| 113 |
@p procedure jump_out; |
| 114 |
begin goto final_end; |
| 115 |
end; |
| 116 |
@y |
| 117 |
@d abort(#)==begin write_ln(stderr, #); uexit (1); |
| 118 |
end |
| 119 |
@d bad_gf(#)==abort('Bad GF file: ',#,'!') |
| 120 |
@.Bad GF file@> |
| 121 |
@z |
| 122 |
|
| 123 |
@x [10] Pascal Web's char |
| 124 |
@d text_char == char {the data type of characters in text files} |
| 125 |
@y |
| 126 |
@d char == 0..255 |
| 127 |
@d text_char == char {the data type of characters in text files} |
| 128 |
@z |
| 129 |
|
| 130 |
@x [39] Use paths in open_gf_file. |
| 131 |
@ To prepare the |gf_file| for input, we |reset| it. |
| 132 |
|
| 133 |
@p procedure open_gf_file; {prepares to read packed bytes in |gf_file|} |
| 134 |
begin reset(gf_file); |
| 135 |
gf_loc := 0 ; |
| 136 |
end; |
| 137 |
@y |
| 138 |
@ In C, we do path searching based on the user's environment or the |
| 139 |
default paths. |
| 140 |
|
| 141 |
@p procedure open_gf_file; {prepares to read packed bytes in |gf_file|} |
| 142 |
begin |
| 143 |
gf_file := kpse_open_file (gf_name, kpse_gf_format); |
| 144 |
gf_loc := 0; |
| 145 |
end; |
| 146 |
@z |
| 147 |
|
| 148 |
% [40] If the PK filename isn't given on the command line, we construct |
| 149 |
% it from the GF filename. |
| 150 |
@x [40] |
| 151 |
@p procedure open_pk_file; {prepares to write packed bytes in |pk_file|} |
| 152 |
begin rewrite(pk_file); |
| 153 |
pk_loc := 0 ; pk_open := true ; |
| 154 |
end; |
| 155 |
@y |
| 156 |
@p procedure open_pk_file; {prepares to write packed bytes in |pk_file|} |
| 157 |
begin |
| 158 |
rewritebin (pk_file, pk_name); |
| 159 |
pk_loc := 0; |
| 160 |
pk_open := true; |
| 161 |
end; |
| 162 |
@z |
| 163 |
|
| 164 |
@x [44] Redefine pk_byte, pk_halfword, pk_three_bytes, and pk_word. |
| 165 |
@ We also need a few routines to write data to the \.{PK} file. We write |
| 166 |
data in 4-, 8-, 16-, 24-, and 32-bit chunks, so we define the appropriate |
| 167 |
routines. We must be careful not to let the sign bit mess us up, as some |
| 168 |
\PASCAL s implement division of a negative integer differently. |
| 169 |
|
| 170 |
@p procedure pk_byte(a:integer) ; |
| 171 |
begin |
| 172 |
if pk_open then begin |
| 173 |
if a < 0 then a := a + 256 ; |
| 174 |
write(pk_file, a) ; |
| 175 |
incr(pk_loc) ; |
| 176 |
end ; |
| 177 |
end ; |
| 178 |
@# |
| 179 |
procedure pk_halfword(a:integer) ; |
| 180 |
begin |
| 181 |
if a < 0 then a := a + 65536 ; |
| 182 |
write(pk_file, a div 256) ; |
| 183 |
write(pk_file, a mod 256) ; |
| 184 |
pk_loc := pk_loc + 2 ; |
| 185 |
end ; |
| 186 |
@# |
| 187 |
procedure pk_three_bytes(a:integer); |
| 188 |
begin |
| 189 |
write(pk_file, a div 65536 mod 256) ; |
| 190 |
write(pk_file, a div 256 mod 256) ; |
| 191 |
write(pk_file, a mod 256) ; |
| 192 |
pk_loc := pk_loc + 3 ; |
| 193 |
end ; |
| 194 |
@# |
| 195 |
procedure pk_word(a:integer) ; |
| 196 |
var b : integer ; |
| 197 |
begin |
| 198 |
if pk_open then begin |
| 199 |
if a < 0 then begin |
| 200 |
a := a + @'10000000000 ; |
| 201 |
a := a + @'10000000000 ; |
| 202 |
b := 128 + a div 16777216 ; |
| 203 |
end else b := a div 16777216 ; |
| 204 |
write(pk_file, b) ; |
| 205 |
write(pk_file, a div 65536 mod 256) ; |
| 206 |
write(pk_file, a div 256 mod 256) ; |
| 207 |
write(pk_file, a mod 256) ; |
| 208 |
pk_loc := pk_loc + 4 ; |
| 209 |
end ; |
| 210 |
end ; |
| 211 |
@y |
| 212 |
@ We also need a few routines to write data to the \.{PK} file. We write |
| 213 |
data in 4-, 8-, 16-, 24-, and 32-bit chunks, so we define the appropriate |
| 214 |
routines. We must be careful not to let the sign bit mess us up, as some |
| 215 |
\PASCAL s implement division of a negative integer differently. |
| 216 |
|
| 217 |
Output is handled through |putbyte| which is supplied by web2c. |
| 218 |
|
| 219 |
@d pk_byte(#)==begin putbyte(#, pk_file); incr(pk_loc) end |
| 220 |
|
| 221 |
@p procedure pk_halfword(a:integer) ; |
| 222 |
begin |
| 223 |
if a < 0 then a := a + 65536 ; |
| 224 |
putbyte(a div 256, pk_file) ; |
| 225 |
putbyte(a mod 256, pk_file) ; |
| 226 |
pk_loc := pk_loc + 2 ; |
| 227 |
end ; |
| 228 |
@# |
| 229 |
procedure pk_three_bytes(a:integer); |
| 230 |
begin |
| 231 |
putbyte(a div 65536 mod 256, pk_file) ; |
| 232 |
putbyte(a div 256 mod 256, pk_file) ; |
| 233 |
putbyte(a mod 256, pk_file) ; |
| 234 |
pk_loc := pk_loc + 3 ; |
| 235 |
end ; |
| 236 |
@# |
| 237 |
procedure pk_word(a:integer) ; |
| 238 |
var b : integer ; |
| 239 |
begin |
| 240 |
if a < 0 then begin |
| 241 |
a := a + @'10000000000 ; |
| 242 |
a := a + @'10000000000 ; |
| 243 |
b := 128 + a div 16777216 ; |
| 244 |
end else b := a div 16777216 ; |
| 245 |
putbyte(b, pk_file) ; |
| 246 |
putbyte(a div 65536 mod 256, pk_file) ; |
| 247 |
putbyte(a div 256 mod 256, pk_file) ; |
| 248 |
putbyte(a mod 256, pk_file) ; |
| 249 |
pk_loc := pk_loc + 4 ; |
| 250 |
end ; |
| 251 |
@z |
| 252 |
|
| 253 |
@x [46] Redefine find_gf_length and move_to_byte. |
| 254 |
@p procedure find_gf_length ; |
| 255 |
begin |
| 256 |
set_pos(gf_file, -1) ; gf_len := cur_pos(gf_file) ; |
| 257 |
end ; |
| 258 |
@# |
| 259 |
procedure move_to_byte(@!n : integer) ; |
| 260 |
begin |
| 261 |
set_pos(gf_file, n); gf_loc := n ; |
| 262 |
end ; |
| 263 |
@y |
| 264 |
@d find_gf_length==gf_len:=gf_length |
| 265 |
|
| 266 |
@p function gf_length:integer; |
| 267 |
begin |
| 268 |
xfseek (gf_file, 0, 2, gf_name); |
| 269 |
gf_length := xftell (gf_file, gf_name); |
| 270 |
end; |
| 271 |
@# |
| 272 |
procedure move_to_byte (n:integer); |
| 273 |
begin xfseek (gf_file, n, 0, gf_name); |
| 274 |
end; |
| 275 |
@z |
| 276 |
|
| 277 |
@x [48] Dynamic allocation of |row| array. |
| 278 |
@!row: array[0..max_row] of integer; {the row counts for working} |
| 279 |
@y |
| 280 |
@!row: ^integer; {the row counts for working} |
| 281 |
@!max_row: integer; {largest index in the main |row| array} |
| 282 |
@z |
| 283 |
|
| 284 |
@x [49] Dynamic allocation of |row| array. |
| 285 |
@<Set init...@>= |
| 286 |
@y |
| 287 |
@<Set init...@>= |
| 288 |
row := xmalloc_array (integer, MAX_ROW); |
| 289 |
max_row := MAX_ROW; |
| 290 |
@z |
| 291 |
|
| 292 |
@x [51] Dynamic allocation of |row| array. |
| 293 |
procedure convert_gf_file; |
| 294 |
@y |
| 295 |
procedure row_overflow; |
| 296 |
var @!new_row: integer; |
| 297 |
begin |
| 298 |
new_row := max_row + MAX_ROW; |
| 299 |
print_ln('Reallocated row array to ', new_row:1, ' items from ', max_row:1, '.'); |
| 300 |
row := xrealloc_array (row, integer, new_row); |
| 301 |
max_row := new_row; |
| 302 |
end; |
| 303 |
@# |
| 304 |
procedure convert_gf_file; |
| 305 |
@z |
| 306 |
|
| 307 |
@x [still 51] Add do_the_rows to break up huge run of cases. |
| 308 |
repeat |
| 309 |
gf_com := gf_byte ; |
| 310 |
case gf_com of |
| 311 |
@y |
| 312 |
repeat |
| 313 |
gf_com := gf_byte ; |
| 314 |
do_the_rows:=false; |
| 315 |
case gf_com of |
| 316 |
@z |
| 317 |
|
| 318 |
@x [52] Declare |thirty_seven_cases| to help avoid breaking yacc. |
| 319 |
@d one_sixty_five_cases(#)==sixty_four_cases(#),sixty_four_cases(#+64), |
| 320 |
sixteen_cases(#+128),sixteen_cases(#+144),four_cases(#+160),#+164 |
| 321 |
@y |
| 322 |
@d thirty_seven_cases(#)==sixteen_cases(#),sixteen_cases(#+16), |
| 323 |
four_cases(#+32),#+36 |
| 324 |
@d new_row_64=new_row_0 + 64 |
| 325 |
@d new_row_128=new_row_64 + 64 |
| 326 |
@z |
| 327 |
|
| 328 |
@x [56] Dynamic allocation of |row| array. |
| 329 |
@d put_in_rows(#)==begin if row_ptr > max_row then bad := true else begin |
| 330 |
row[row_ptr]:=#; incr(row_ptr); end ; end |
| 331 |
@y |
| 332 |
@d put_in_rows(#)==begin if row_ptr > max_row then row_overflow; |
| 333 |
row[row_ptr]:=#; incr(row_ptr); end |
| 334 |
@z |
| 335 |
|
| 336 |
@x [57] Dynamic allocation of |row| array. |
| 337 |
bad := false ; |
| 338 |
@y |
| 339 |
@z |
| 340 |
|
| 341 |
@x [still 57] Break up an oversized sequence of cases for yacc. |
| 342 |
one_sixty_five_cases(new_row_0) : begin |
| 343 |
if on = state then put_in_rows(extra) ; |
| 344 |
put_in_rows(end_of_row) ; |
| 345 |
on := true ; extra := gf_com - new_row_0 ; state := false ; |
| 346 |
end ; |
| 347 |
@y |
| 348 |
sixty_four_cases(new_row_0) : do_the_rows:=true; |
| 349 |
sixty_four_cases(new_row_64) : do_the_rows:=true; |
| 350 |
thirty_seven_cases(new_row_128) : do_the_rows:=true; |
| 351 |
@z |
| 352 |
|
| 353 |
@x [still 57] Dynamic allocation of |row| array. |
| 354 |
if bad then abort('Ran out of internal memory for row counts!') ; |
| 355 |
@.Ran out of memory@> |
| 356 |
@y |
| 357 |
@z |
| 358 |
|
| 359 |
@x [still 57] Break up an oversized sequence of cases for yacc. |
| 360 |
endcases ; |
| 361 |
@y |
| 362 |
endcases ; |
| 363 |
if do_the_rows then begin |
| 364 |
do_the_rows:=false; |
| 365 |
if on = state then put_in_rows(extra) ; |
| 366 |
put_in_rows(end_of_row) ; |
| 367 |
on := true ; extra := gf_com - new_row_0 ; state := false ; |
| 368 |
end ; |
| 369 |
@z |
| 370 |
|
| 371 |
@x [58] Add do_the_rows to break up huge run of cases. |
| 372 |
@ A few more locals used above and below: |
| 373 |
|
| 374 |
@<Locals to |convert_gf_file|@>= |
| 375 |
@y |
| 376 |
@ A few more locals used above and below: |
| 377 |
|
| 378 |
@<Locals to |convert_gf_file|@>= |
| 379 |
@!do_the_rows:boolean; |
| 380 |
@z |
| 381 |
|
| 382 |
@x [still 58] Dynamic allocation of |row| array. |
| 383 |
@!bad : boolean ; {did we run out of space?} |
| 384 |
@y |
| 385 |
@z |
| 386 |
|
| 387 |
@x [81] Don't add `GFtoPK 2.3 output from ' to the font comment. |
| 388 |
@d comm_length = 23 {length of |preamble_comment|} |
| 389 |
@d from_length = 6 {length of its |' from '| part} |
| 390 |
@y |
| 391 |
@d comm_length = 0 {length of |preamble_comment|} |
| 392 |
@d from_length = 0 {length of its |' from '| part} |
| 393 |
@z |
| 394 |
|
| 395 |
@x [83] Don't do any assignments to |preamble_comment|. |
| 396 |
@ @<Set init...@>= |
| 397 |
comment := preamble_comment ; |
| 398 |
@y |
| 399 |
@ This module is empty in the C version. |
| 400 |
@z |
| 401 |
|
| 402 |
@x [86] Remove the final_end label |
| 403 |
final_end : end . |
| 404 |
@y |
| 405 |
end. |
| 406 |
@z |
| 407 |
|
| 408 |
@x [88] System-dependent changes. |
| 409 |
This section should be replaced, if necessary, by changes to the program |
| 410 |
that are necessary to make \.{GFtoPK} work at a particular installation. |
| 411 |
It is usually best to design your change file so that all changes to |
| 412 |
previous sections preserve the section numbering; then everybody's version |
| 413 |
will be consistent with the printed program. More extensive changes, |
| 414 |
which introduce new sections, can be inserted here; then only the index |
| 415 |
itself will get a new section number. |
| 416 |
@^system dependencies@> |
| 417 |
@y |
| 418 |
Parse a Unix-style command line. |
| 419 |
|
| 420 |
@d argument_is (#) == (strcmp (long_options[option_index].name, #) = 0) |
| 421 |
@d do_nothing == {empty statement} |
| 422 |
|
| 423 |
@<Define \(|parse_arguments|@> = |
| 424 |
procedure parse_arguments; |
| 425 |
const n_options = 3; {Pascal won't count array lengths for us.} |
| 426 |
var @!long_options: array[0..n_options] of getopt_struct; |
| 427 |
@!getopt_return_val: integer; |
| 428 |
@!option_index: c_int_type; |
| 429 |
@!current_option: 0..n_options; |
| 430 |
begin |
| 431 |
@<Initialize the option variables@>; |
| 432 |
@<Define the option table@>; |
| 433 |
repeat |
| 434 |
getopt_return_val := getopt_long_only (argc, argv, '', long_options, |
| 435 |
address_of (option_index)); |
| 436 |
if getopt_return_val = -1 then begin |
| 437 |
do_nothing; {End of arguments; we exit the loop below.} |
| 438 |
|
| 439 |
end else if getopt_return_val = "?" then begin |
| 440 |
usage (my_name); {|getopt| has already given an error message.} |
| 441 |
|
| 442 |
end else if argument_is ('help') then begin |
| 443 |
usage_help (GFTOPK_HELP, nil); |
| 444 |
|
| 445 |
end else if argument_is ('version') then begin |
| 446 |
print_version_and_exit (banner, nil, 'Tomas Rokicki', nil); |
| 447 |
|
| 448 |
end; {Else it was a flag; |getopt| has already done the assignment.} |
| 449 |
until getopt_return_val = -1; |
| 450 |
|
| 451 |
{Now |optind| is the index of first non-option on the command line. |
| 452 |
We must have one or two remaining arguments.} |
| 453 |
if (optind + 1 <> argc) and (optind + 2 <> argc) then begin |
| 454 |
write_ln (stderr, my_name, ': Need one or two file arguments.'); |
| 455 |
usage (my_name); |
| 456 |
end; |
| 457 |
|
| 458 |
gf_name := cmdline (optind); |
| 459 |
|
| 460 |
{If an explicit output filename isn't given, construct it from |gf_name|.} |
| 461 |
if optind + 2 = argc then begin |
| 462 |
pk_name := cmdline (optind + 1); |
| 463 |
end else begin |
| 464 |
pk_name := basename_change_suffix (gf_name, 'gf', 'pk'); |
| 465 |
end; |
| 466 |
end; |
| 467 |
|
| 468 |
@ Here are the options we allow. The first is one of the standard GNU options. |
| 469 |
@.-help@> |
| 470 |
|
| 471 |
@<Define the option...@> = |
| 472 |
current_option := 0; |
| 473 |
long_options[current_option].name := 'help'; |
| 474 |
long_options[current_option].has_arg := 0; |
| 475 |
long_options[current_option].flag := 0; |
| 476 |
long_options[current_option].val := 0; |
| 477 |
incr (current_option); |
| 478 |
|
| 479 |
@ Another of the standard options. |
| 480 |
@.-version@> |
| 481 |
|
| 482 |
@<Define the option...@> = |
| 483 |
long_options[current_option].name := 'version'; |
| 484 |
long_options[current_option].has_arg := 0; |
| 485 |
long_options[current_option].flag := 0; |
| 486 |
long_options[current_option].val := 0; |
| 487 |
incr (current_option); |
| 488 |
|
| 489 |
@ Print progress information? |
| 490 |
|
| 491 |
@<Define the option...@> = |
| 492 |
long_options[current_option].name := 'verbose'; |
| 493 |
long_options[current_option].has_arg := 0; |
| 494 |
long_options[current_option].flag := address_of (verbose); |
| 495 |
long_options[current_option].val := 1; |
| 496 |
incr (current_option); |
| 497 |
|
| 498 |
@ @<Glob...@> = |
| 499 |
@!verbose: c_int_type; |
| 500 |
|
| 501 |
@ @<Initialize the option...@> = |
| 502 |
verbose := false; |
| 503 |
|
| 504 |
@ An element with all zeros always ends the list. |
| 505 |
|
| 506 |
@<Define the option...@> = |
| 507 |
long_options[current_option].name := 0; |
| 508 |
long_options[current_option].has_arg := 0; |
| 509 |
long_options[current_option].flag := 0; |
| 510 |
long_options[current_option].val := 0; |
| 511 |
|
| 512 |
@ Global filenames. |
| 513 |
|
| 514 |
@<Global...@> = |
| 515 |
@!gf_name, @!pk_name:const_c_string; |
| 516 |
@z |