Debian Codegraph @

texk/ps2pk/pkout.c

raw
1 /* FILE: pkout.c
2 * PURPOSE: implementation of PK output functions
3 * COMMENT: functions are derived from `The GFtoPK processor' but
4 * presented in a modular fashion so they can be used in other
5 * programs.
6 * (Porting `top-down' WEB code into modular readable C-code is
7 * not a trivial exercise.)
8 *
9 * AUTHOR: Piet Tutelaers
10 * (see README for license)
11 */
12
13 /* Some constants */
14 #define PK_ID 89
15 #define PK_SPC1 240 /* All we need, max length always < 255 */
16 #define PK_NUMSPC 244
17 #define PK_NOOP 246
18 #define PK_PRE 247
19 #define PK_POST 245
20
21 #define DPI 72.27
22 #define MIN(a,b) ( (a<b)? (a): (b))
23
24 #include "basics.h" /* definitions and fatal() */
25 #include <stdio.h>
26 #include <stdarg.h> /* va_start(), va_end() */
27 #include <stdlib.h> /* malloc() */
28 #include "pkout.h"
29
30 #undef DEBUG
31 static int pk_len = 0;
32 static FILE *pkfile;
33
34 void pk_open(char *pkname)
35 {
36 pkfile = fopen(pkname, WB);
37 if (pkfile == NULL) fatal("Can not open %s\n", pkname);
38 }
39
40 void pk_close(void)
41 {
42 fclose(pkfile);
43 }
44
45 /* Portable Big Endian output functions */
46
47 /* Nybble buffer */
48 static int pk_output_byte, bitweight;
49 static void pk1(int);
50
51 static void pk_nybble(int x)
52 {
53 if (bitweight == 16) {
54 bitweight = 1; pk_output_byte = x*16;
55 }
56 else {
57 bitweight = 16; pk1(pk_output_byte + x);
58 }
59 }
60
61 static void pk1(int x)
62 {
63 if (x < 0) x += 256;
64 putc(x & 0x00ff, pkfile);
65 pk_len++;
66 }
67
68 static void pk2(int x)
69 {
70 if (x < 0) x += 65536;
71 pk1((x & 0x00ff00) >> 8);
72 pk1(x & 0x0000ff);
73 }
74
75 static void pk3(int32_t x)
76 {
77 pk1((x & 0x00ff0000) >> 16);
78 pk1((x & 0x0000ff00) >> 8);
79 pk1(x & 0x000000ff);
80 }
81
82 static void pk4(int32_t x)
83 {
84 if (x < 0) { /* next two lines from [email protected] */
85 x += 2<<29; x += 2<<29;
86 pk1(((x & 0x7f000000) >> 24) + 128);
87 }
88 else pk1((x & 0x7f000000) >> 24);
89 pk1((x & 0x00ff0000) >> 16);
90 pk1((x & 0x0000ff00) >> 8);
91 pk1(x & 0x000000ff);
92 }
93
94 static int MAX_COUNTS; /* length of array to hold runlengths */
95
96 void pk_preamble(char *comment, /* source of the font */
97 float pointsize, /* pointsize in points */
98 int32_t checksum, /* should equal to tfm-value */
99 unsigned int h_res, /* horizontal resolution (dpi) */
100 unsigned int v_res) /* vertical resolution (dpi) */
101 {
102 int i, len;
103
104 /* compute MAX_COUNTS based upon pointsize, h_res and v_res */
105 MAX_COUNTS = pointsize / DPI * h_res * (pointsize / DPI * v_res + 1);
106
107 pk1(PK_PRE);
108 pk1(PK_ID);
109 len = strlen(comment);
110 len = len>255? 255: len;
111 pk1(len);
112 for (i=0; i<len; i++) pk1(*comment++);
113
114 pk4((int32_t)(pointsize * (1<<20) + 0.5));
115 pk4(checksum);
116 pk4((int32_t)(h_res / DPI * (1<<16))); pk4((int32_t)(v_res / DPI * (1<<16)));
117 }
118
119 /* From `The GFtoPK processor', pp. 231 */
120
121 static int optimal_size(int W, int H, int cnt, int count[], int *dyn_f)
122 { int comp_size = 0, b_comp_size, deriv[14], i, j, k;
123
124 i = (count[0] == 0? 1: 0); /* skip first empty runlength */
125 /* compute comp_size and deriv[1..13] */
126 for (j=1; j<14; j++) deriv[j] = 0;
127 while (i<cnt) {
128 j = count[i++];
129 if (j == -1) { comp_size++; continue; }
130 if (j < 0) { j = -j; comp_size++; }
131 if (j < 209) comp_size += 2;
132 else {
133 k = j - 193;
134 while (k >= 16) { k=k/16; comp_size += 2; }
135 comp_size++;
136 }
137 if (j < 14) deriv[j]--;
138 else {
139 if (j < 209) deriv[(223 - j) / 15]++;
140 else {
141 k = 16;
142 while (k*16 < j+3) k *= 16;
143 if (j-k <= 192) deriv[(207-j+k)/15] += 2;
144 }
145 }
146 }
147 /* find minimal value */
148 b_comp_size = comp_size; *dyn_f = 0;
149 for (i=1; i<14; i++) {
150 comp_size += deriv[i];
151 if (comp_size <= b_comp_size) {
152 b_comp_size = comp_size; *dyn_f = i;
153 }
154 }
155 comp_size = (b_comp_size + 1) / 2;
156 if (H*W == 0 || (comp_size > (H*W + 7) / 8)) {
157 comp_size = (H*W + 7) / 8; *dyn_f = 14;
158 }
159 return comp_size;
160 }
161
162 /* Next array contains the runlengths */
163 static int *count = NULL;
164
165 /* Global variables (interface between pk_runlengths() and pk_char() */
166 static int dyn_f, comp_size, cnt;
167
168 static void pk_runlengths(int W, int H, int (*next_pixel)())
169 { int current_value, pixel, i, j, max_counts, first_count;
170 int total_pixels, rc, row, col, runlength;
171 int color2, total_pixels2, /* counting equal rows variables */
172 i1, i2, newrow;
173
174 first_count = H; max_counts = first_count + H * W;
175 if (count == NULL) {
176 count = malloc(MAX_COUNTS * sizeof(int));
177 if (count == NULL) fatal("Out of memory\n");
178 }
179
180 if (max_counts > MAX_COUNTS) {
181 free(count);
182 count = malloc(max_counts * sizeof(int));
183 if (count == NULL) fatal("Out of memory\n");
184 MAX_COUNTS = max_counts;
185 }
186
187 /* 1: compute transitions BLACK<->WHITE */
188
189 cnt = first_count;
190 runlength = 0; current_value = BLACK;
191 for (row=0; row < H; row++) {
192 for (col=0; col<W; col++) {
193 pixel = (*next_pixel)();
194 if (pixel == current_value) runlength++;
195 else if (pixel == OTHER(current_value)) {
196 count[cnt++] = runlength;
197 current_value = OTHER(current_value);
198 runlength = 1;
199 }
200 }
201 }
202 if (runlength>0)
203 count[cnt++] = runlength;
204
205 /* for an empty glyph */
206 if (cnt == first_count) count[cnt++] = 0;
207
208 #ifdef DEBUG
209 current_value = BLACK;
210 for (i=first_count; i<cnt; i++)
211 if (count[i] < 0) printf("[%d]", -count[i]);
212 else {
213 if (current_value == BLACK) printf("%d", count[i]);
214 else printf("(%d)", count[i]);
215 current_value = OTHER(current_value);
216 }
217 putchar('\n');
218 #endif
219
220 /* 2: Now remove equal lines and add a repeat count at the first
221 transition of a row */
222
223 i = first_count; j = 0;
224 total_pixels = 0; row = -1;
225 current_value = BLACK;
226 if (count[i] == 0) {
227 count[j++] = count[i++];
228 current_value = WHITE;
229 }
230 while (i < cnt) {
231 if (j >= i) fatal("Program error: report to author!\n");
232 count[j++] = count[i];
233 total_pixels += count[i++];
234 current_value = OTHER(current_value);
235 newrow = total_pixels / W; col = total_pixels % W;
236 /* if transition starts in column zero or in a previous row
237 then continue */
238 if (newrow == row || row == H-1 || col == 0) continue;
239 row = newrow;
240
241 /* count equal rows */
242
243 /* 1: goto first transition of next row */
244 color2 = current_value;
245 total_pixels2 = total_pixels; i2 = i;
246 while (i2 < cnt && total_pixels2 / W == row) {
247 total_pixels2 += count[i2++];
248 color2 = OTHER(color2);
249 }
250
251 /* 2: do we need to compute a repeat count? */
252 if (color2 != current_value
253 || total_pixels2 - total_pixels != W) continue;
254
255 /* 3: count them */
256 rc = 0; i1 = i;
257 while (i2 < cnt && count[i1] == count[i2]) {
258 #ifdef DEBUG
259 printf("%d (%d)", count[i1], count[i2]);
260 #endif
261 total_pixels2 += count[i2++]; i1++;
262 }
263 rc = total_pixels2 / W - row; /* enclosed rows */
264 if (MIN(count[i1], count[i2]) + total_pixels2 % W < W)
265 rc--;
266 #ifdef DEBUG
267 printf(" row %d: rc = %d\n", row, rc);
268 #endif
269
270 /* 3: now remove the equal rows and finish last row */
271 if (rc > 0) {
272 /* insert a repeat count */
273 if (j >= i) fatal("Program error: report to author\n");
274 count[j++] = -rc;
275
276 /* finish runlengths of current row */
277 while (total_pixels + count[i] < row*W+W) {
278 if (j >= i) fatal("Program error: increase FIRST_COUNT!\n");
279 count[j++] = count[i];
280 total_pixels += count[i++];
281 current_value = OTHER(current_value);
282 }
283
284 /* skip runlengths of equal rows */
285 while (total_pixels + count[i] < (row+rc+1)*W) {
286 total_pixels += count[i++];
287 current_value = OTHER(current_value);
288 }
289 row += rc;
290 }
291 }
292 cnt = j; /* that is what we have now */
293
294 /* 3: compute optimal packing size */
295
296 comp_size = optimal_size(H, W, cnt, count, &dyn_f);
297 #ifdef DEBUG
298 current_value = BLACK;
299 for (i=0; i<cnt; i++)
300 if (count[i] < 0) printf("[%d]", -count[i]);
301 else {
302 if (current_value == BLACK) printf("%d", count[i]);
303 else printf("(%d)", count[i]);
304 current_value = OTHER(current_value);
305 }
306 printf("\nOptimal packing with dyn_f = %d total size %d\n",
307 dyn_f, comp_size);
308 #endif
309 }
310
311 #define MAX_TWOBYTE_NYBBLE (208 - 15*dyn_f)
312
313 static void pk_number(int x)
314 { int k;
315
316 if (x < 0) { /* repeat count */
317 if (x == -1) pk_nybble(0xF);
318 else { pk_nybble(0xE);
319 pk_number(-x);
320 }
321 }
322 else if (x <= dyn_f) pk_nybble(x);
323 else if (x <= MAX_TWOBYTE_NYBBLE) { /* two nybble values */
324 x-= dyn_f+1;
325 pk_nybble(dyn_f + 1 + x / 16);
326 pk_nybble(x % 16);
327 }
328 else { /* huge counts */
329 x = x - MAX_TWOBYTE_NYBBLE + 15; k = 16;
330 while (k <= x) { k *= 16; pk_nybble(0x0); }
331 while (k > 1) { k = k / 16; pk_nybble(x / k); x = x % k; }
332 }
333 }
334
335 /* in case runlength encoding is not optimal we send a compressed
336 bitmap in stead of the packed runlengths
337 See GFtoPK processor, pp. 235 */
338 static void pk_bitmap(int width, int cnt, int runlength[])
339 { int count, /* number of bits in the current state to send */
340 buff, /* byte buffer */
341 h_bit, /* horizontal bit count for each runlength */
342 p_bit, /* what bit are we about to send out? */
343 r_on, s_on, /* state saving variables */
344 r_count, s_count,/* dito */
345 r_i, s_i, /* dito */
346 rc, /* repeat count */
347 state, /* state variable (what state?) */
348 on, /* starting with black? */
349 i; /* points to next runlength */
350 static int power[9] = {1, 2, 4, 8, 16, 32, 64, 128, 256};
351
352 buff = 0; p_bit = 8; h_bit = width;
353 state = 0; rc = 0;
354 r_on = s_on = r_count = s_count = r_i = s_i = 0; /* avoid warnings */
355 on = 1; count = runlength[0]; i = 1;
356 while (i < cnt || state || count>0) {
357 if (state) {
358 count = r_count; i = r_i; on = r_on; rc--;
359 }
360 else {
361 r_count = count; r_i = i; r_on = on;
362 }
363
364 /* send one row of width bits */
365 do {
366 if (count == 0) {
367 if (runlength[i] < 0) {
368 if (!state) rc = - runlength[i];
369 i++;
370 }
371 count = runlength[i]; i++; on = !on;
372 }
373 if (count >= p_bit && p_bit < h_bit) {
374 /* we end a byte, we don't end a runlength */
375 if (on) buff += power[p_bit] - 1;
376 pk1(buff);
377 buff = 0; h_bit -= p_bit;
378 count -= p_bit; p_bit = 8;
379 }
380 else {
381 if (count < p_bit && count < h_bit) {
382 /* we neither end the row nor the byte */
383 if (on) buff += power[p_bit] - power[p_bit-count];
384 p_bit -= count; h_bit -= count; count = 0;
385 }
386 else { /* we end a row and maybe a byte */
387 if (on) buff += power[p_bit] - power[p_bit-h_bit];
388 count -= h_bit; p_bit -= h_bit; h_bit = width;
389 if (p_bit == 0) {
390 pk1(buff); buff = 0; p_bit = 8;
391 }
392 }
393 }
394 } while (h_bit != width);
395
396 if (state && rc == 0) {
397 count = s_count; i = s_i; on = s_on; state = 0;
398 }
399 else if (!state && rc >0) {
400 s_count = count; s_i = i; s_on = on; state = 1;
401 }
402 }
403 if (p_bit != 8) pk1(buff);
404 }
405
406 /* For packing a character */
407 void pk_char(int char_code, /* character code 0..255 */
408 int32_t tfm_width, /* TFM width of character */
409 int h_escapement, /* horizontal escapement in pixels */
410 unsigned int width, /* width of bounding box */
411 unsigned int height, /* height of bounding box */
412 int h_offset, /* horizontal offset to reference point */
413 int v_offset, /* vertical offset to reference point */
414 int (*next_pixel)()) /* user's next pixel generator */
415 { int i;
416 unsigned short flag_byte;
417
418 pk_runlengths(width, height, next_pixel);
419
420 /* write a character preamble */
421
422 flag_byte = dyn_f * 16;
423 if (count[0] > 0) flag_byte += 8; /* starting in BLACK */
424 if (tfm_width > 0XFFFFFF || width > 65535 || height > 65535
425 || h_offset > 32767 || v_offset > 32767
426 || h_offset < -32768 || v_offset < -32768
427 || comp_size > 196594)
428 { /* write long format */
429 fatal("Can't handle long format yet!\n");
430 }
431 else if (h_escapement > 255 || width > 255 || height > 255
432 || h_offset > 127 || v_offset > 127
433 || h_offset < -128 || v_offset < -128
434 || comp_size > 1015)
435 { /* write two-byte short character preamble */
436 comp_size += 13; flag_byte += comp_size / 65535 + 4;
437 pk1(flag_byte);
438 pk2(comp_size % 65535);
439 pk1(char_code);
440 pk3(tfm_width);
441 pk2(h_escapement); /* pixels ! */
442 pk2(width);
443 pk2(height);
444 pk2(h_offset);
445 pk2(v_offset);
446 }
447 else { /* write one-byte short character preamble */
448 comp_size += 8; flag_byte += comp_size / 256;
449 pk1(flag_byte);
450 pk1(comp_size % 256);
451 pk1(char_code);
452 pk3(tfm_width);
453 pk1(h_escapement); /* pixels ! */
454 pk1(width);
455 pk1(height);
456 pk1(h_offset);
457 pk1(v_offset);
458 }
459
460 /* and now the character itself */
461 if (dyn_f != 14) { /* send compressed format */
462 if (count[0] == 0) i = 1; else i = 0;
463 bitweight = 16;
464 for (i=i; i<cnt; i++) pk_number(count[i]);
465 if (bitweight != 16) pk1(pk_output_byte);
466 }
467 else /* send bitmap */
468 if (height > 0) pk_bitmap(width, cnt, count);
469
470 }
471
472 /*
473 * Output small string special (<255 chars)
474 */
475 static void pkstring(const char *fmt, ...) {
476 char buf[256]; int i, len;
477 va_list args;
478
479 va_start(args, fmt);
480 #ifdef CHARSPRINTF
481 (void) vsprintf(buf, fmt, args);
482 len = strlen(buf);
483 #else
484 len = vsprintf(buf, fmt, args);
485 #endif
486 if (len > 255 || len < 0) fatal("PK string exceeds 255 characters\n");
487 va_end(args);
488
489 pk1(PK_SPC1);
490 pk1(len); i = 0;
491 for (i = 0; i < len; i++) pk1(buf[i]);
492 }
493
494 /*
495 * Compute METAFONT magnification string for <dpi>
496 */
497
498 static int PSPKINT(float x) {
499 return (int) x;
500 }
501
502 static char mag_str[64];
503
504 static char *magnification (int dpi, int BDPI) {
505 double size, magstep;
506
507 if (dpi == BDPI) {
508 sprintf(mag_str, "magstep(0)");
509 return mag_str;
510 }
511 size = BDPI; magstep = 0;
512 while (dpi < size) {
513 size = size / 1.095445115;
514 magstep -= 0.5;
515 if (dpi == PSPKINT(size + 0.5)) {
516 sprintf(mag_str, "magstep(%.1f)", magstep);
517 return mag_str;
518 }
519 if (dpi > size) {
520 sprintf(mag_str, "%d+%d/%d", PSPKINT(dpi/BDPI), dpi%BDPI, BDPI);
521 return mag_str;
522 }
523 }
524 while (dpi > size) {
525 size = size * 1.095445115;
526 magstep += 0.5;
527 if (dpi == PSPKINT(size + 0.5)) {
528 sprintf(mag_str, "magstep(%.1f)", magstep);
529 return mag_str;
530 }
531 if (dpi < size) {
532 sprintf(mag_str, "%d+%d/%d", PSPKINT(dpi/BDPI), dpi%BDPI, BDPI);
533 return mag_str;
534 }
535 }
536 fatal("PK could not determine magnification\n");
537 }
538
539 /*
540 * Barebone postample
541 */
542 void pk_postamble(void)
543 {
544 pk1(PK_POST);
545 while (pk_len % 4 != 0) pk1(PK_NOOP);
546 }
547
548 /*
549 * Write special PK strings in the postamble for identification
550 * purposes as proposed by [email protected].
551 */
552 void ps2pk_postamble(char *fontname, /* The real FontName from the afm */
553 char *encname, /* The actual name, not the filename */
554 int base_res, /* basic resolution */
555 int h_res, /* Match against base_res for mag */
556 int v_res, /* Match against h_res for aspect_ratio */
557 float pointsize, /* Used for fontfacebyte calculation */
558 char *args) /* Essential ps2pk args */
559 {
560 int i;
561
562 pkstring("ps2pk options: %s", args);
563 pkstring("fontid=%s", fontname);
564 if (encname) pkstring("codingscheme=%s", encname);
565 pkstring("fontfacebyte");
566 pk1(PK_NUMSPC);
567 i = (pointsize < 127.0) ?
568 ((254 - (int)((2 * pointsize)+0.5)) * (1 << 16)) : 0;
569 pk4(i);
570 pkstring("pixels_per_inch=%d", base_res);
571 pkstring("mag=%s", magnification(h_res, base_res));
572 if (v_res != h_res)
573 pkstring("aspect ratio=%d / %d",
574 (int)((1.0 * v_res / h_res * base_res) + 0.5), base_res);
575
576 pk1(PK_POST);
577 while (pk_len % 4 != 0) pk1(PK_NOOP);
578 }