Debian Codegraph @

texk/xdvipsk/repack.c

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