| 1 |
/* |
| 2 |
* Compressed TeX fonts in PostScript. |
| 3 |
* By Radical Eye Software. |
| 4 |
* (Slight mods by Don Knuth in December 89.) |
| 5 |
*/ |
| 6 |
#include "dvips.h" /* The copyright notice in that file is included too! */ |
| 7 |
/* |
| 8 |
* The external declarations: |
| 9 |
*/ |
| 10 |
#include "protos.h" |
| 11 |
|
| 12 |
/* Given a raster that has been unpacked from PK format, |
| 13 |
* we compress it by another scheme that is suitable for |
| 14 |
* a PostScript-oriented unpacking. We write instructions for |
| 15 |
* a little interpreter whose one-byte instructions have the form |
| 16 |
* 18*opcode+parameter. The interpreter forms each row based |
| 17 |
* on simple transformations to the previous row. */ |
| 18 |
|
| 19 |
#define MAXOUT (18) |
| 20 |
#define CMD(n) (MAXOUT*(n)) |
| 21 |
#define ADVXCHG1 CMD(0) |
| 22 |
#define ADVXCHG1END CMD(1) |
| 23 |
#define CHGX CMD(2)-1 |
| 24 |
#define CHGXEND CMD(3)-1 |
| 25 |
#define ADVXLSH CMD(4) |
| 26 |
#define ADVXLSHEND CMD(5) |
| 27 |
#define ADVXRSH CMD(6) |
| 28 |
#define ADVXRSHEND CMD(7) |
| 29 |
#define ADVX CMD(8)-1 |
| 30 |
#define REPX CMD(9)-1 |
| 31 |
#define SETX CMD(10)-1 |
| 32 |
#define CLRX CMD(11)-1 |
| 33 |
#define ADVXCHG2 CMD(12) |
| 34 |
#define ADVXCHG2END CMD(13) |
| 35 |
#define END CMD(14) |
| 36 |
|
| 37 |
static int rowlength = -1; |
| 38 |
static unsigned char *specdata; |
| 39 |
static long tslen = 0; |
| 40 |
static unsigned char *tempstore, *tsp, *tsend; |
| 41 |
|
| 42 |
static void |
| 43 |
putlong(register char *a, long i) |
| 44 |
{ |
| 45 |
a[0] = i >> 24; |
| 46 |
a[1] = i >> 16; |
| 47 |
a[2] = i >> 8; |
| 48 |
a[3] = i; |
| 49 |
} |
| 50 |
|
| 51 |
long |
| 52 |
getlong(register unsigned char *a) |
| 53 |
{ |
| 54 |
return ((((((a[0] << 8L) + a[1]) << 8L) + a[2]) << 8L) + a[3]); |
| 55 |
} |
| 56 |
|
| 57 |
/* First, a routine that appends one byte to the compressed output. */ |
| 58 |
|
| 59 |
#define addtse(n) {lcm=tsp-tempstore;addts(n);} /* mark END option position */ |
| 60 |
|
| 61 |
static void |
| 62 |
addts(register unsigned char what) |
| 63 |
{ |
| 64 |
register unsigned char *p, *q; |
| 65 |
|
| 66 |
if (tsp >= tsend) { |
| 67 |
if (tempstore == NULL) { |
| 68 |
tslen = 2020; |
| 69 |
tempstore = (unsigned char *)mymalloc((integer)tslen); |
| 70 |
tsp = tempstore; |
| 71 |
} else { |
| 72 |
tslen = 2 * tslen; |
| 73 |
tsp = (unsigned char *)mymalloc((integer)tslen); |
| 74 |
for (p=tempstore, q=tsp; p<tsend; p++, q++) |
| 75 |
*q = *p; |
| 76 |
free((char *)tempstore); |
| 77 |
tempstore = tsp; |
| 78 |
tsp = q; |
| 79 |
} |
| 80 |
tsend = tempstore + tslen; |
| 81 |
} |
| 82 |
*tsp++ = what; |
| 83 |
} |
| 84 |
|
| 85 |
/* Next, a routine that discovers how to do the compression. */ |
| 86 |
|
| 87 |
#define rsh(a,b) ( ((a)==0) ? ((b)==128) : ( ((a)==255) ? ((b)==127) :\ |
| 88 |
((b)==(((a)>>1)|((a)&128))) )) |
| 89 |
#define lsh(a,b) ( ((a)==0) ? ((b)==1) : ( ((a)==255) ? ((b)==254) :\ |
| 90 |
((b)==((((a)<<1)&255)|((a)&1))) )) |
| 91 |
#define DIFFERENT (1) |
| 92 |
#define LSHPOSSIB (2) |
| 93 |
#define RSHPOSSIB (4) |
| 94 |
#define BLKPOSSIB (8) |
| 95 |
#define WHTPOSSIB (16) |
| 96 |
#define ENDROW (32) |
| 97 |
#define NOPOSSIB(n) ((n&(LSHPOSSIB|RSHPOSSIB|BLKPOSSIB|WHTPOSSIB))==0) |
| 98 |
#define NOSHIFT(n) ((n&(LSHPOSSIB|RSHPOSSIB))==0) |
| 99 |
/* |
| 100 |
* Our input bytes are packed to the 16-bit word. On output, |
| 101 |
* they're packed to bytes. Thus, we may have to skip a byte at |
| 102 |
* the end of each row. |
| 103 |
*/ |
| 104 |
void |
| 105 |
dochar(unsigned char *from, short width, short height) |
| 106 |
{ |
| 107 |
register int i; |
| 108 |
register unsigned char *f, *t, *d; |
| 109 |
register unsigned char *e; |
| 110 |
register int accum; |
| 111 |
int j, k, kk; |
| 112 |
int diffrow; |
| 113 |
int repeatcount; |
| 114 |
int lit, pos, cmd = 0; |
| 115 |
long lcm; |
| 116 |
int widthc; |
| 117 |
|
| 118 |
widthc = width + (width & 1); /* halfword correction */ |
| 119 |
lcm = -1; |
| 120 |
if (widthc > rowlength) { |
| 121 |
if (rowlength > 0) |
| 122 |
free((char *)specdata); |
| 123 |
rowlength = widthc + 30; |
| 124 |
specdata = (unsigned char *)mymalloc((integer)(rowlength + 15)); |
| 125 |
} |
| 126 |
for (i= -15, t=specdata; i<=widthc; i++, t++) |
| 127 |
*t = 0; |
| 128 |
repeatcount = 0; |
| 129 |
f = specdata + 2; |
| 130 |
for (j=0; j<height; j++, f = from, from += widthc) { |
| 131 |
diffrow = 0; |
| 132 |
for (i=0, t=from, d=specdata; i<width; i++, f++, t++, d++) { |
| 133 |
if (*f == *t) { |
| 134 |
if (*t == 0) |
| 135 |
*d = WHTPOSSIB; |
| 136 |
else if (*t == 255) |
| 137 |
*d = BLKPOSSIB; |
| 138 |
else |
| 139 |
*d = 0; |
| 140 |
} else { |
| 141 |
accum = DIFFERENT; |
| 142 |
if (rsh(*f, *t)) |
| 143 |
accum |= RSHPOSSIB; |
| 144 |
else if (lsh(*f, *t)) |
| 145 |
accum |= LSHPOSSIB; |
| 146 |
if (*t == 0) |
| 147 |
accum |= WHTPOSSIB; |
| 148 |
else if (*t == 255) |
| 149 |
accum |= BLKPOSSIB; |
| 150 |
*d = accum; |
| 151 |
diffrow++; |
| 152 |
} |
| 153 |
} /* end 'for i' */ |
| 154 |
*d = ENDROW; |
| 155 |
if (diffrow == 0) { |
| 156 |
repeatcount++; |
| 157 |
} else { |
| 158 |
if (repeatcount) { |
| 159 |
while (repeatcount > MAXOUT) { |
| 160 |
addts((unsigned char)(REPX+MAXOUT)); |
| 161 |
repeatcount -= MAXOUT; |
| 162 |
} |
| 163 |
addts((unsigned char)(REPX+repeatcount)); |
| 164 |
repeatcount = 0; |
| 165 |
} |
| 166 |
pos = 0; |
| 167 |
for (i=0, d=specdata, f=t-width; i<width;) { |
| 168 |
if ((*d & DIFFERENT) == 0) { |
| 169 |
i++; |
| 170 |
d++; |
| 171 |
f++; |
| 172 |
} else { |
| 173 |
accum = 0; |
| 174 |
if (pos != i) |
| 175 |
lit = NOSHIFT(*d); |
| 176 |
else /* N.B.: 'lit' does not imply literate programming here */ |
| 177 |
lit = NOPOSSIB(*d); |
| 178 |
for (e=d;;e++) { |
| 179 |
if (NOPOSSIB(*e)) |
| 180 |
lit = 1; |
| 181 |
if ((*e & DIFFERENT) == 0) |
| 182 |
break; |
| 183 |
if ((*e & WHTPOSSIB) && |
| 184 |
(e[1] & WHTPOSSIB)) { |
| 185 |
while (*e & WHTPOSSIB) { |
| 186 |
e++; |
| 187 |
accum++; |
| 188 |
} |
| 189 |
cmd = CLRX; |
| 190 |
e -= accum; |
| 191 |
break; |
| 192 |
} else if ((*e & BLKPOSSIB) && |
| 193 |
(e[1] & BLKPOSSIB)) { |
| 194 |
while (*e & BLKPOSSIB) { |
| 195 |
e++; |
| 196 |
accum++; |
| 197 |
} |
| 198 |
cmd = SETX; |
| 199 |
e -= accum; |
| 200 |
break; |
| 201 |
} |
| 202 |
} /* end 'for e'; d pts to first bad byte, e to next good one */ |
| 203 |
while (i - pos > MAXOUT) { |
| 204 |
addts((unsigned char)(ADVX+MAXOUT)); |
| 205 |
pos += MAXOUT; |
| 206 |
} |
| 207 |
if (0 != (k = (e - d))) { |
| 208 |
if (lit) { |
| 209 |
if (k > 2) { |
| 210 |
if (i > pos) { |
| 211 |
addts((unsigned char)(ADVX + i - pos)); |
| 212 |
pos = i; |
| 213 |
} |
| 214 |
while (k > MAXOUT) { |
| 215 |
addts((unsigned char)(CHGX + MAXOUT)); |
| 216 |
for (kk=0; kk<MAXOUT; kk++) |
| 217 |
addts((unsigned char)(*f++)); |
| 218 |
d += MAXOUT; |
| 219 |
pos += MAXOUT; |
| 220 |
i += MAXOUT; |
| 221 |
k -= MAXOUT; |
| 222 |
} |
| 223 |
addtse((unsigned char)(CHGX + k)); |
| 224 |
pos += k; |
| 225 |
for (; d<e; d++, i++, f++) |
| 226 |
addts((unsigned char)(*f)); |
| 227 |
} else { |
| 228 |
if (k == 1) { |
| 229 |
if (i == pos+MAXOUT) { |
| 230 |
addts((unsigned char)(ADVX + MAXOUT)); |
| 231 |
pos = i; |
| 232 |
} |
| 233 |
addtse((unsigned char)(ADVXCHG1 + i - pos)); |
| 234 |
addts((unsigned char)(*f)); |
| 235 |
i++; |
| 236 |
pos = i; |
| 237 |
d++; |
| 238 |
f++; |
| 239 |
} else { |
| 240 |
if (i == pos+MAXOUT) { |
| 241 |
addts((unsigned char)(ADVX + MAXOUT)); |
| 242 |
pos = i; |
| 243 |
} |
| 244 |
addtse((unsigned char)(ADVXCHG2 + i - pos)); |
| 245 |
addts((unsigned char)(*f)); |
| 246 |
addts((unsigned char)(f[1])); |
| 247 |
i += 2; |
| 248 |
pos = i; |
| 249 |
d += 2; |
| 250 |
f += 2; |
| 251 |
} |
| 252 |
} |
| 253 |
} else { |
| 254 |
while (e > d) { |
| 255 |
if (*d & LSHPOSSIB) { |
| 256 |
if (i == pos+MAXOUT) { |
| 257 |
addts((unsigned char)(ADVX + MAXOUT)); |
| 258 |
pos = i; |
| 259 |
} |
| 260 |
addtse((unsigned char)(ADVXLSH + i - pos)); |
| 261 |
} else if (*d & RSHPOSSIB) { |
| 262 |
if (i == pos+MAXOUT) { |
| 263 |
addts((unsigned char)(ADVX + MAXOUT)); |
| 264 |
pos = i; |
| 265 |
} |
| 266 |
addtse((unsigned char)(ADVXRSH + i - pos)); |
| 267 |
} else if (*d & WHTPOSSIB) { /* i==pos */ |
| 268 |
addts((unsigned char)(CLRX + 1)); lcm = -1; |
| 269 |
} else if (*d & BLKPOSSIB) { /* i==pos */ |
| 270 |
addts((unsigned char)(SETX + 1)); lcm = -1; |
| 271 |
} else |
| 272 |
error("! bug"); /* why wasn't lit true? */ |
| 273 |
d++; |
| 274 |
f++; |
| 275 |
i++; |
| 276 |
pos = i; |
| 277 |
} /* end 'while e>d' */ |
| 278 |
} |
| 279 |
} /* end 'if e>d' */ |
| 280 |
if (accum > 0) { |
| 281 |
if (i > pos) { |
| 282 |
addts((unsigned char)(ADVX + i - pos)); |
| 283 |
pos = i; |
| 284 |
} |
| 285 |
i += accum; |
| 286 |
d += accum; |
| 287 |
f += accum; |
| 288 |
while (accum > MAXOUT) { |
| 289 |
addts((unsigned char)(cmd + MAXOUT)); |
| 290 |
accum -= MAXOUT; |
| 291 |
pos += MAXOUT; |
| 292 |
} |
| 293 |
lcm = -1; |
| 294 |
addts((unsigned char)(cmd + accum)); |
| 295 |
pos += accum; |
| 296 |
} |
| 297 |
} /* end 'else DIFFERENT' */ |
| 298 |
} /* end 'for i' */ |
| 299 |
if (lcm != -1) { |
| 300 |
tempstore[lcm] += MAXOUT; /* append END */ |
| 301 |
lcm = -1; |
| 302 |
} else { |
| 303 |
addts((unsigned char)(END)); |
| 304 |
} |
| 305 |
} /* end 'else rows different' */ |
| 306 |
#ifdef DEBUG |
| 307 |
if (d != specdata + width) |
| 308 |
error("! internal inconsistency in repack"); |
| 309 |
#endif |
| 310 |
} /* end 'for j' */ |
| 311 |
if (repeatcount) { |
| 312 |
while (repeatcount > MAXOUT) { |
| 313 |
addts((unsigned char)(REPX+MAXOUT)); |
| 314 |
repeatcount -= MAXOUT; |
| 315 |
} |
| 316 |
addts((unsigned char)(REPX+repeatcount)); |
| 317 |
repeatcount = 0; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
long mbytesleft; |
| 322 |
quarterword *mraster; |
| 323 |
|
| 324 |
static char * |
| 325 |
makecopy(register unsigned char *what, |
| 326 |
register long len, |
| 327 |
register unsigned char *p) |
| 328 |
{ |
| 329 |
register unsigned char *q; |
| 330 |
|
| 331 |
if (p == NULL) { |
| 332 |
if (len > MINCHUNK) |
| 333 |
p = (unsigned char *)mymalloc((integer)len); |
| 334 |
else { |
| 335 |
if (bytesleft < len) { |
| 336 |
raster = (quarterword *)mymalloc((integer)RASTERCHUNK); |
| 337 |
bytesleft = RASTERCHUNK; |
| 338 |
} |
| 339 |
p = (unsigned char *)raster; |
| 340 |
bytesleft -= len; |
| 341 |
raster += len; |
| 342 |
} |
| 343 |
} |
| 344 |
q = p; |
| 345 |
while (len > 0) { |
| 346 |
*p++ = *what++; |
| 347 |
len --; |
| 348 |
} |
| 349 |
return ((char *)q); |
| 350 |
} |
| 351 |
|
| 352 |
/* Now the main routine, which is called when a character is used |
| 353 |
for the very first time. */ |
| 354 |
void |
| 355 |
repack(register chardesctype *cp) |
| 356 |
{ |
| 357 |
register long i, j; |
| 358 |
register unsigned char *p; |
| 359 |
register int width, height; |
| 360 |
register int wwidth; |
| 361 |
char startbytes; |
| 362 |
int smallchar; |
| 363 |
|
| 364 |
p = cp->packptr; |
| 365 |
if (p == NULL) |
| 366 |
error("! no raster?"); |
| 367 |
tsp = tempstore; |
| 368 |
tsend = tempstore + tslen; |
| 369 |
addts(*p); /* copy the PK flag byte */ |
| 370 |
if (*p & 4) { |
| 371 |
if ((*p & 7) == 7) { |
| 372 |
startbytes = 17; |
| 373 |
width = getlong(p + 1); |
| 374 |
height = getlong(p + 5); |
| 375 |
for (i=0; i<12; i++) |
| 376 |
addts(*++p); |
| 377 |
} else { |
| 378 |
startbytes = 9; |
| 379 |
width = p[1] * 256 + p[2]; |
| 380 |
height = p[3] * 256 + p[4]; |
| 381 |
addts(*++p); |
| 382 |
addts(*++p); |
| 383 |
addts(*++p); |
| 384 |
addts(*++p); |
| 385 |
} |
| 386 |
} else { |
| 387 |
startbytes = 5; |
| 388 |
width = p[1]; |
| 389 |
height = p[2]; |
| 390 |
} |
| 391 |
addts(*++p); |
| 392 |
addts(*++p); |
| 393 |
addts(*++p); |
| 394 |
addts(*++p); |
| 395 |
p++; /* OK, p now points to beginning of the nibbles */ |
| 396 |
addts((unsigned char)0); |
| 397 |
addts((unsigned char)0); |
| 398 |
addts((unsigned char)0); |
| 399 |
addts((unsigned char)0); /* leave room to stick in a new length field */ |
| 400 |
wwidth = (width + 15) / 16; |
| 401 |
i = 2 * height * (long)wwidth; |
| 402 |
if (i <= 0) |
| 403 |
i = 2; |
| 404 |
if ((cp->flags & BIGCHAR) == 0) |
| 405 |
smallchar = 5; |
| 406 |
else |
| 407 |
smallchar = 0; |
| 408 |
i += smallchar; |
| 409 |
if (mbytesleft < i) { |
| 410 |
if (mbytesleft >= RASTERCHUNK) |
| 411 |
(void) free((char *) mraster); |
| 412 |
if (RASTERCHUNK > i) { |
| 413 |
mraster = (quarterword *)mymalloc((integer)RASTERCHUNK); |
| 414 |
mbytesleft = RASTERCHUNK; |
| 415 |
} else { |
| 416 |
i += i / 4; |
| 417 |
mraster = (quarterword *)mymalloc((integer)(i + 3)); |
| 418 |
mbytesleft = i; |
| 419 |
} |
| 420 |
} |
| 421 |
while (i > 0) |
| 422 |
mraster[--i] = 0; |
| 423 |
i = startbytes + unpack(p, (halfword *)mraster, (halfword)width, |
| 424 |
(halfword)height, *(cp->packptr)); |
| 425 |
dochar(mraster, (short)((width + 7) >> 3), (short)height); |
| 426 |
if (smallchar) { |
| 427 |
addts((unsigned char)0); |
| 428 |
addts((unsigned char)0); |
| 429 |
addts((unsigned char)0); |
| 430 |
addts((unsigned char)0); |
| 431 |
addts((unsigned char)0); |
| 432 |
} |
| 433 |
j = tsp - tempstore; |
| 434 |
#ifdef DEBUG |
| 435 |
if (dd(D_COMPRESS)) |
| 436 |
fprintf(stderr,"PK %ld bytes, unpacked %ld, compressed %ld\n", |
| 437 |
i-(long)startbytes, (long)((width+7L)/8)*height, j-(long)startbytes-4); |
| 438 |
#endif /* DEBUG */ |
| 439 |
if ( i < j ) { |
| 440 |
if (i > MINCHUNK) |
| 441 |
free(cp->packptr); |
| 442 |
cp->packptr = |
| 443 |
(unsigned char *)makecopy(tempstore, j, (unsigned char *)0); |
| 444 |
} else |
| 445 |
makecopy(tempstore, j, (unsigned char *)cp->packptr); |
| 446 |
putlong((char *)(cp->packptr+startbytes), j-startbytes-4-smallchar); |
| 447 |
cp->flags |= REPACKED; |
| 448 |
} |