| 1 |
/* |
| 2 |
* NAME |
| 3 |
* pkin.c - implementation of readchar() |
| 4 |
* DESCRIPTION |
| 5 |
* This implementation of readchar() uses parts of the program dvips |
| 6 |
* written by Tomas Rokicki--the inventor of the pkformat--(loadfont.c, |
| 7 |
* download.c and unpack.c). Dvips in turn is derived from pktype. |
| 8 |
* Pktype(TeX) is described in debt in ``The PKtype processor'', |
| 9 |
* which is available as pktype.weave as part of the METAFONTware. |
| 10 |
* What was needed to implement readchar() is rearranged in pkfile.c to |
| 11 |
* get more modularity in the style of MODULA2. |
| 12 |
* REDESIGN |
| 13 |
* Piet Tutelaers |
| 14 |
*/ |
| 15 |
|
| 16 |
#ifdef HAVE_CONFIG_H |
| 17 |
#include "c-auto.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
#ifdef DEBUG |
| 21 |
#include <stdio.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <stdlib.h> /* malloc() */ |
| 25 |
#include "basics.h" /* basic definitions */ |
| 26 |
#include "pkin.h" |
| 27 |
|
| 28 |
/* |
| 29 |
* Now we have some routines to get stuff from the pk file. pkbyte returns |
| 30 |
* the next byte from the pk file. |
| 31 |
*/ |
| 32 |
|
| 33 |
static FILE *pkfile ; |
| 34 |
|
| 35 |
static shalfword |
| 36 |
pkbyte(void) |
| 37 |
{ |
| 38 |
register shalfword i ; |
| 39 |
|
| 40 |
if ((i=getc(pkfile))==EOF) |
| 41 |
fatal("unexpected eof in pk file\n") ; |
| 42 |
return(i) ; |
| 43 |
} |
| 44 |
|
| 45 |
static shalfword |
| 46 |
pksbyte(void) |
| 47 |
{ |
| 48 |
register shalfword i ; |
| 49 |
|
| 50 |
i = pkbyte() ; |
| 51 |
return i > 127 ? i -256 : i ; |
| 52 |
} |
| 53 |
|
| 54 |
static integer |
| 55 |
pkquad(void) |
| 56 |
{ |
| 57 |
register integer i ; |
| 58 |
|
| 59 |
i = pksbyte() ; |
| 60 |
i = i * 256 + pkbyte() ; |
| 61 |
i = i * 256 + pkbyte() ; |
| 62 |
i = i * 256 + pkbyte() ; |
| 63 |
return(i) ; |
| 64 |
} |
| 65 |
|
| 66 |
static integer |
| 67 |
pktrio(void) |
| 68 |
{ |
| 69 |
register integer i ; |
| 70 |
|
| 71 |
i = pkbyte() ; |
| 72 |
i = i * 256 + pkbyte() ; |
| 73 |
i = i * 256 + pkbyte() ; |
| 74 |
return(i) ; |
| 75 |
} |
| 76 |
|
| 77 |
static integer |
| 78 |
pkpair(void) |
| 79 |
{ |
| 80 |
register integer i ; |
| 81 |
|
| 82 |
i = pkbyte() ; |
| 83 |
i = i * 256 + pkbyte() ; |
| 84 |
return (i); |
| 85 |
} |
| 86 |
|
| 87 |
static integer |
| 88 |
pkspair(void) |
| 89 |
{ |
| 90 |
register integer i ; |
| 91 |
|
| 92 |
i = pksbyte() ; |
| 93 |
i = i * 256 + pkbyte(); |
| 94 |
return (i); |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* pkopen opens the pk file. This is system dependent. |
| 99 |
*/ |
| 100 |
|
| 101 |
static Boolean |
| 102 |
pkopen(const char *name) |
| 103 |
{ |
| 104 |
if ((pkfile=fopen(name, RB))==NULL) { |
| 105 |
fatal("Could not open %s\n", name) ; |
| 106 |
} else |
| 107 |
return(1) ; |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
* The next part is devoted to unpacking the character data. |
| 112 |
*/ |
| 113 |
|
| 114 |
/* |
| 115 |
* We need procedures to get a nybble, bit, and packed word from the |
| 116 |
* packed data structure. |
| 117 |
*/ |
| 118 |
|
| 119 |
static halfword inputbyte, flagbyte ; |
| 120 |
static halfword bitweight ; |
| 121 |
static halfword dynf ; |
| 122 |
static halfword repeatcount ; |
| 123 |
|
| 124 |
static shalfword |
| 125 |
getnyb (void) |
| 126 |
{ halfword temp; |
| 127 |
if ( bitweight == 0 ) |
| 128 |
{ bitweight = 16 ; |
| 129 |
inputbyte = pkbyte(); |
| 130 |
temp = inputbyte >> 4 ; |
| 131 |
} else { |
| 132 |
bitweight = 0 ; |
| 133 |
temp = inputbyte & 15 ; |
| 134 |
} |
| 135 |
return(temp); |
| 136 |
} |
| 137 |
|
| 138 |
static Boolean |
| 139 |
getbit (void) |
| 140 |
{ |
| 141 |
bitweight >>= 1 ; |
| 142 |
if ( bitweight == 0 ) |
| 143 |
{ inputbyte = pkbyte(); |
| 144 |
bitweight = 128 ; |
| 145 |
} |
| 146 |
return(inputbyte & bitweight) ; |
| 147 |
} |
| 148 |
|
| 149 |
static halfword (*realfunc)() ; |
| 150 |
long PKremainder ; |
| 151 |
static halfword handlehuge(halfword i, halfword k) ; |
| 152 |
|
| 153 |
static halfword |
| 154 |
pkpackednum (void) |
| 155 |
{ |
| 156 |
register halfword i, j ; |
| 157 |
i = getnyb () ; |
| 158 |
if ( i == 0 ) |
| 159 |
{ do { j = getnyb () ; |
| 160 |
i++ ; |
| 161 |
} while ( ! ( j != 0 ) ) ; |
| 162 |
if ( i > 3 ) { |
| 163 |
/* |
| 164 |
* Damn, we got a huge count! We *fake* it by giving an artificially |
| 165 |
* large repeat count. |
| 166 |
*/ |
| 167 |
return( handlehuge ( i , j ) ) ; |
| 168 |
} else { |
| 169 |
while ( i > 0 ) |
| 170 |
{ j = j * 16 + getnyb () ; |
| 171 |
i-- ; |
| 172 |
} |
| 173 |
return ( j - 15 + ( 13 - dynf ) * 16 + dynf ) ; |
| 174 |
} |
| 175 |
} |
| 176 |
else if ( i <= dynf ) return ( i ) ; |
| 177 |
else if ( i < 14 ) return ( ( i - dynf - 1 ) * 16 + getnyb () + dynf + 1 |
| 178 |
) ; |
| 179 |
else |
| 180 |
{ if ( i == 14 ) repeatcount = pkpackednum () ; |
| 181 |
else repeatcount = 1 ; |
| 182 |
#ifdef DEBUG |
| 183 |
printf("[%d]", repeatcount); |
| 184 |
#endif |
| 185 |
return ( (*realfunc)() ) ; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static halfword rest (void) |
| 190 |
{ |
| 191 |
halfword i ; |
| 192 |
|
| 193 |
if (PKremainder < 0) { |
| 194 |
PKremainder = - PKremainder ; |
| 195 |
return ( 0 ) ; |
| 196 |
} else if (PKremainder > 0) { |
| 197 |
if (PKremainder > 4000) { |
| 198 |
PKremainder = 4000 - PKremainder ; |
| 199 |
return ( 4000 ) ; |
| 200 |
} else { |
| 201 |
i = PKremainder ; |
| 202 |
PKremainder = 0 ; |
| 203 |
realfunc = pkpackednum ; |
| 204 |
return ( i ) ; |
| 205 |
} |
| 206 |
} else { |
| 207 |
fatal("shouldn't happen\n") ; |
| 208 |
} |
| 209 |
|
| 210 |
/*NOTREACHED*/ |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static halfword |
| 215 |
handlehuge (halfword i, halfword k) |
| 216 |
{ |
| 217 |
register long j = k ; |
| 218 |
|
| 219 |
while (i) { |
| 220 |
j = (j << 4L) + getnyb() ; |
| 221 |
i-- ; |
| 222 |
} |
| 223 |
PKremainder = j - 15 + ( 13 - dynf ) * 16 + dynf ; |
| 224 |
realfunc = rest ; |
| 225 |
return ( rest() ) ; |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* And now we have our unpacking routine. |
| 230 |
*/ |
| 231 |
|
| 232 |
static halfword gpower[17] = { 0 , 1 , 3 , 7 , 15 , 31 , 63 , 127 , |
| 233 |
255 , 511 , 1023 , 2047 , 4095 , 8191 , 16383 , 32767 , 65535 } ; |
| 234 |
|
| 235 |
static void |
| 236 |
unpack(chardesc *cd) |
| 237 |
{ |
| 238 |
register integer i, j ; |
| 239 |
register halfword word, wordweight ; |
| 240 |
halfword *raster; |
| 241 |
shalfword rowsleft ; |
| 242 |
Boolean turnon ; |
| 243 |
shalfword hbit ; |
| 244 |
halfword count ; |
| 245 |
shalfword wordwidth ; |
| 246 |
|
| 247 |
wordwidth = (cd->cwidth + 15) / 16 ; |
| 248 |
i = 2 * cd->cheight * (long)wordwidth ; |
| 249 |
if (i <= 0) |
| 250 |
i = 2 ; |
| 251 |
cd->raster = (halfword *)malloc((unsigned)i) ; |
| 252 |
if (cd->raster == NULL) |
| 253 |
fatal("out of memory during allocation\n") ; |
| 254 |
raster = cd->raster; |
| 255 |
realfunc = pkpackednum ; |
| 256 |
dynf = flagbyte / 16 ; |
| 257 |
turnon = flagbyte & 8 ; |
| 258 |
if ( dynf == 14 ) |
| 259 |
{ bitweight = 0 ; |
| 260 |
for ( i = 1 ; i <= cd->cheight ; i ++ ) |
| 261 |
{ word = 0 ; |
| 262 |
wordweight = 32768 ; |
| 263 |
for ( j = 1 ; j <= cd->cwidth ; j ++ ) |
| 264 |
{ if ( getbit () ) word += wordweight ; |
| 265 |
wordweight >>= 1 ; |
| 266 |
if ( wordweight == 0 ) |
| 267 |
{ *raster++ = word ; |
| 268 |
word = 0 ; |
| 269 |
wordweight = 32768 ; |
| 270 |
} |
| 271 |
} |
| 272 |
if ( wordweight != 32768 ) |
| 273 |
*raster++ = word ; |
| 274 |
} |
| 275 |
} else { |
| 276 |
rowsleft = cd->cheight ; |
| 277 |
hbit = cd->cwidth ; |
| 278 |
repeatcount = 0 ; |
| 279 |
wordweight = 16 ; |
| 280 |
word = 0 ; |
| 281 |
bitweight = 0 ; |
| 282 |
while ( rowsleft > 0 ) |
| 283 |
{ count = (*realfunc)() ; |
| 284 |
#ifdef DEBUG |
| 285 |
if (turnon) printf("(%d) ",count); |
| 286 |
else printf("%d ",count); |
| 287 |
#endif |
| 288 |
while ( count != 0 ) |
| 289 |
{ if ( ( count < wordweight ) && ( count < hbit ) ) |
| 290 |
{ if ( turnon ) word += gpower [ wordweight ] - gpower |
| 291 |
[ wordweight - count ] ; |
| 292 |
hbit -= count ; |
| 293 |
wordweight -= count ; |
| 294 |
count = 0 ; |
| 295 |
} |
| 296 |
else if ( ( count >= hbit ) && ( hbit <= wordweight ) ) |
| 297 |
{ if ( turnon ) |
| 298 |
word += gpower [ wordweight ] - gpower |
| 299 |
[ wordweight - hbit ] ; |
| 300 |
*raster++ = word ; |
| 301 |
for ( i = 1 ; i <= repeatcount ; i ++ ) { |
| 302 |
for ( j = 1 ; j <= wordwidth ; j ++ ) { |
| 303 |
*raster = *(raster - wordwidth) ; |
| 304 |
raster++ ; |
| 305 |
} |
| 306 |
} |
| 307 |
rowsleft -= repeatcount + 1 ; |
| 308 |
repeatcount = 0 ; |
| 309 |
word = 0 ; |
| 310 |
wordweight = 16 ; |
| 311 |
count -= hbit ; |
| 312 |
hbit = cd->cwidth ; |
| 313 |
} |
| 314 |
else |
| 315 |
{ if ( turnon ) word += gpower [ wordweight ] ; |
| 316 |
*raster++ = word ; |
| 317 |
word = 0 ; |
| 318 |
count -= wordweight ; |
| 319 |
hbit -= wordweight ; |
| 320 |
wordweight = 16 ; |
| 321 |
} |
| 322 |
} |
| 323 |
turnon = ! turnon ; |
| 324 |
} |
| 325 |
putchar('\n') ; |
| 326 |
if ( ( rowsleft != 0 ) || ( hbit != cd->cwidth ) ) |
| 327 |
fatal( "error while unpacking; more bits than required\n" ) ; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
/* |
| 332 |
* readchar(): the main routine |
| 333 |
* Reads the character definition of character `c' into `cd' if available, |
| 334 |
* return FALSE (0) otherwise. |
| 335 |
*/ |
| 336 |
int |
| 337 |
readchar(char *name, integer c, chardesc *cd) |
| 338 |
{ |
| 339 |
register shalfword i ; |
| 340 |
register integer k ; |
| 341 |
register integer length = 0; |
| 342 |
|
| 343 |
if (!pkopen(name)) return(0); |
| 344 |
/* |
| 345 |
* Check the preamble of the pkfile |
| 346 |
*/ |
| 347 |
if (pkbyte()!=247) |
| 348 |
fatal("bad pk file, expected pre\n") ; |
| 349 |
if (pkbyte()!=89) |
| 350 |
fatal("bad version of pk file\n") ; |
| 351 |
for(i=pkbyte(); i>0; i--) /* creator of pkfile */ |
| 352 |
(void)pkbyte() ; |
| 353 |
(void) pkquad() ; /* design size */ |
| 354 |
(void) pkquad() ; /* checksum */ |
| 355 |
(void) pkquad() ; /* hppp */ |
| 356 |
(void) pkquad() ; /* vppp */ |
| 357 |
/* |
| 358 |
* Now we skip to the desired character definition |
| 359 |
*/ |
| 360 |
while ((flagbyte=pkbyte())!=245) { |
| 361 |
if (flagbyte < 240) { |
| 362 |
switch (flagbyte & 7) { |
| 363 |
case 0: case 1: case 2: case 3: |
| 364 |
length = (flagbyte & 7) * 256 + pkbyte() - 4 ; |
| 365 |
cd->charcode = pkbyte() ; |
| 366 |
(void) pktrio() ; /* TFMwidth */ |
| 367 |
(void) pkbyte() ; /* pixel width */ |
| 368 |
break ; |
| 369 |
case 4: case 5: case 6: |
| 370 |
length = (flagbyte & 3) * 65536 + pkpair() * 256 - 5 ; |
| 371 |
cd->charcode = pkbyte() ; |
| 372 |
(void) pktrio() ; /* TFMwidth */ |
| 373 |
(void) pkpair() ; /* pixelwidth */ |
| 374 |
break ; |
| 375 |
case 7: |
| 376 |
length = pkquad() - 12 ; |
| 377 |
cd->charcode = pkquad() ; |
| 378 |
(void) pkquad() ; /* TFMwidth */ |
| 379 |
(void) pkquad(); /* pixelwidth */ |
| 380 |
(void) pkquad(); /* pixelheight */ |
| 381 |
} |
| 382 |
if (length <= 0) |
| 383 |
fatal("packet length (%d) too small\n", length) ; |
| 384 |
if (cd->charcode == c) { |
| 385 |
if (flagbyte & 4) { |
| 386 |
if ((flagbyte & 7) == 7) { /* long format */ |
| 387 |
cd->cwidth = pkquad() ; |
| 388 |
cd->cheight = pkquad() ; |
| 389 |
cd->xoff = pkquad() ; |
| 390 |
cd->yoff = pkquad() ; |
| 391 |
} else { /* extended format */ |
| 392 |
cd->cwidth = pkpair(); ; |
| 393 |
cd->cheight = pkpair() ; |
| 394 |
cd->xoff = pkspair() ; |
| 395 |
cd->yoff = pkspair() ; |
| 396 |
} |
| 397 |
} else { /* short format */ |
| 398 |
cd->cwidth = pkbyte() ; |
| 399 |
cd->cheight = pkbyte() ; |
| 400 |
cd->xoff = pksbyte() ; |
| 401 |
cd->yoff = pksbyte() ; |
| 402 |
} |
| 403 |
unpack(cd); |
| 404 |
(void)fclose(pkfile) ; |
| 405 |
return(1); |
| 406 |
} |
| 407 |
for (; length>0; length--) /* skip this character */ |
| 408 |
(void) pkbyte() ; |
| 409 |
} else { |
| 410 |
k = 0 ; |
| 411 |
switch (flagbyte) { |
| 412 |
case 243: |
| 413 |
k = pksbyte() ; |
| 414 |
case 242: |
| 415 |
k = k * 256 + pkbyte() ; |
| 416 |
case 241: |
| 417 |
k = k * 256 + pkbyte() ; |
| 418 |
case 240: |
| 419 |
k = k * 256 + pkbyte() ; |
| 420 |
while (k-- > 0) |
| 421 |
i = pkbyte() ; |
| 422 |
break ; |
| 423 |
case 244: |
| 424 |
k = pkquad() ; |
| 425 |
break ; |
| 426 |
case 246: |
| 427 |
break ; |
| 428 |
default: |
| 429 |
fatal("unexpected command (%d)\n", flagbyte) ; |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
(void)fclose(pkfile) ; |
| 434 |
return(0); /* character not found */ |
| 435 |
} |