Debian Codegraph @

converter/pbm/pbmtopk.c

raw
1 /*
2 pbmtopk, adapted from "pxtopk.c by tomas rokicki" by AJCD 1/8/90
3
4 References (retrieved May 31 2015):
5 Packed (PK) Font File Format
6 https://www.tug.org/TUGboat/tb06-3/tb13pk.pdf
7
8 Tex Font Metric Files (TFM)
9 https://www.tug.org/TUGboat/tb06-1/tb11gf.pdf
10 */
11
12 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
13 #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */
14 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
15
16 #include <stdio.h>
17 #include <math.h>
18 #include <ctype.h>
19 #include <string.h>
20
21 #include "pm_c_util.h"
22 #include "pbm.h"
23 #include "nstring.h"
24 #include "mallocvar.h"
25
26 #define MAXPKCHAR 256
27 #define MAXOPTLINE 200
28 #define MAXWIDTHTAB 256
29 #define MAXHEIGHTTAB 16
30 #define MAXDEPTHTAB 16
31 #define MAXITALICTAB 64
32 #define MAXPARAMS 30
33 #define NAMELENGTH 80
34
35 /*-----------------------------------------------------------------------
36 Macros to handle fixed point numbers
37
38 This program uses uses fixed-point numbers to store data where
39 normally a floating-point data type (float or double) would be
40 employed.
41
42 Numbers that contain fractions are stored as signed integers.
43 The 20 least-significant bits are for the fractional part, the rest
44 (12 bits assuming that int is 32 bit) are for the integer part.
45 The technical term for this is "Q20" or "Q12.20" notation.
46
47 Float/double data is converted to Q20 fixed point by multiplying
48 by 2^20 (= 1048576). The opposite conversion is conducted by
49 dividing by 2^20.
50
51 The Q20 data must be within the range -16 < r < 16. The reason
52 behind this restriction is unclear. The program generally writes
53 Q20 data to the output files in 32 bits. (Exception: in function
54 shipchar() there is a provision to write Q20 data in 24 bits,
55 provided that 24 bits is sufficient.)
56 ---------------------------------------------------------------------*/
57
58 #define fixword(d) ((int)((double)(d)*1048576))
59 #define unfixword(f) ((double)(f) / 1048576)
60 #define fixrange(f) ((f) < 16777216 && (f) > -16777216)
61 #define designunits(p) ((p)*72.27/(double)resolution/unfixword(designsize))
62
63 /* character flags: in order of appearance in option files. */
64 #define XOFFSET 1
65 #define YOFFSET 2
66 #define HORZESC 4
67 #define VERTESC 8
68 #define TFMWIDTH 16
69 #define TFMHEIGHT 32
70 #define TFMDEPTH 64
71 #define TFMITALIC 128
72
73 typedef int integer ;
74 typedef char quarterword ;
75 typedef char boolean ;
76 typedef quarterword ASCIIcode ;
77 typedef quarterword eightbits ;
78 typedef unsigned char byte ;
79
80 static integer resolution, designsize ;
81 static char *filename[MAXPKCHAR] ;
82
83 static integer xoffset[MAXPKCHAR] ;
84 static integer yoffset[MAXPKCHAR] ;
85 static integer horzesc[MAXPKCHAR] ;
86 static integer vertesc[MAXPKCHAR] ;
87
88 static byte tfmindex[MAXPKCHAR] ;
89 static byte hgtindex[MAXPKCHAR] ;
90 static byte depindex[MAXPKCHAR] ;
91 static byte italindex[MAXPKCHAR] ;
92 static byte charflags[MAXPKCHAR] ;
93
94 static bit **bitmap ;
95 static integer smallestch = MAXPKCHAR ;
96 static integer largestch = -1;
97 static integer emwidth = 0;
98 static integer checksum ;
99 static const char *codingscheme = "GRAPHIC" ;
100 static const char *familyname = "PBM" ;
101
102 static integer widthtab[MAXWIDTHTAB] = {0}; /* TFM widths */
103 static integer numwidth = 1; /* number of entries in width table */
104 static integer heighttab[MAXHEIGHTTAB] = {0};
105 static integer numheight = 1;
106 static integer depthtab[MAXDEPTHTAB] = {0};
107 static integer numdepth = 1;
108 static integer italictab[MAXITALICTAB] = {0};
109 static integer numitalic = 1;
110 static integer parameters[MAXPARAMS] = {0};
111 static integer numparam = 0;
112
113 static ASCIIcode xord[128] ;
114 static char xchr[256] = {
115 '?', '?', '?', '?', '?', '?', '?', '?',
116 '?', '?', '?', '?', '?', '?', '?', '?',
117 '?', '?', '?', '?', '?', '?', '?', '?',
118 '?', '?', '?', '?', '?', '?', '?', '?',
119 ' ', '!', '"', '#', '$', '%', '&', '\'',
120 '(', ')', '*', '+', ',', '-', '.', '/',
121 '0', '1', '2', '3', '4', '5', '6', '7',
122 '8', '9', ':', ';', '<', '=', '>', '?',
123 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
124 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
125 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
126 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
127 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
128 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
129 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
130 'x', 'y', 'z', '{', '|', '}', '~', '?' };
131
132 static FILE *tfmfile, *pkfile ;
133 static char tfmname[NAMELENGTH+1], pkname[NAMELENGTH+1] ;
134 static integer pbmtopk_pkloc = 0 ;
135 static integer bitweight ;
136 static integer outputbyte ;
137 static integer car ;
138 static integer hppp ;
139 static integer width ;
140 static integer height ;
141
142 /* check sum algorithm (in Pascal):
143
144 compute_checksum()
145 begin
146 c0:=bc; c1:=ec; c2:=bc; c3:=ec;
147 for c:=bc to ec do if char_wd[c]>0 then begin
148 temp_width:=memory[char_wd[c]];
149 if design_units<>unity then
150 temp_width:=round((temp_width/design_units)*1048576.0);
151 temp_width:=temp_width + (c+4)*@'20000000;
152 {this should be positive}
153 c0:=(c0+c0+temp_width) mod 255;
154 c1:=(c1+c1+temp_width) mod 253;
155 c2:=(c2+c2+temp_width) mod 251;
156 c3:=(c3+c3+temp_width) mod 247;
157 end;
158 header_bytes[check_sum_loc]:=c0;
159 header_bytes[check_sum_loc+1]:=c1;
160 header_bytes[check_sum_loc+2]:=c2;
161 header_bytes[check_sum_loc+3]:=c3;
162 end
163 */
164
165 #define add_tfmwidth(v) (add_tfmtable(widthtab, &numwidth, v, MAXWIDTHTAB,\
166 "TFM width"))
167 #define add_tfmheight(v) (add_tfmtable(heighttab, &numheight, v, MAXHEIGHTTAB,\
168 "TFM height"))
169 #define add_tfmdepth(v) (add_tfmtable(depthtab, &numdepth, v, MAXDEPTHTAB,\
170 "TFM depth"))
171 #define add_tfmitalic(v) (add_tfmtable(italictab, &numitalic, v, MAXITALICTAB,\
172 "Italic correction"))
173
174
175 static byte
176 add_tfmtable(int * const table,
177 int * const count,
178 int const value,
179 int const max_count,
180 const char * const name) {
181
182 integer i;
183 for (i = 0; i < *count; i++) /* search for value in tfm table */
184 if (table[i] == value) return (byte)i;
185 if (*count >= max_count)
186 pm_error("too many values in %s table", name) ;
187 if (!fixrange(value))
188 pm_error("%s %f for char %d out of range",
189 name, unfixword(value), car);
190 table[*count] = value ;
191 return (*count)++ ;
192 }
193
194
195
196 /* add a suffix to a filename in an allocated space */
197 static void
198 pbmtopk_add_suffix(char * const name,
199 const char * const suffix) {
200
201 char *slash = strrchr(name, '/');
202 char *dot = strrchr(name, '.');
203
204 if ((dot && slash ? dot < slash : !dot) && !streq(name, "-"))
205 strcat(name, suffix);
206 }
207
208
209
210 /* initialize the PK parameters */
211 static void
212 initialize_pk(void) {
213 integer i ;
214 pm_message("This is PBMtoPK, version 2.4") ;
215 for (i = 127 ; i <= 255 ; i ++) xchr[i] = '?' ;
216 for (i = 0 ; i <= 127 ; i ++) xord[i] = 32 ;
217 for (i = 32 ; i < 127 ; i ++) xord[(int)xchr[i]] = i ;
218 for (i = 0; i < MAXPKCHAR; i++) {
219 filename[i] = NULL;
220 charflags[i] = 0;
221 }
222 designsize = fixword(1.0) ;
223 }
224
225
226
227 /* write a single byte to the PK file */
228 static void
229 pbmtopk_pkbyte(integer const b_in) {
230 integer b;
231
232 b = b_in; /* initial value */
233
234 if (b < 0)
235 b += 256 ;
236 putc(b, pkfile) ;
237 pbmtopk_pkloc++ ;
238 }
239
240
241
242 /* write two bytes to the PK file */
243 static void
244 pkhalfword(integer const a_in) {
245 integer a;
246
247 a = a_in;
248
249 if (a < 0)
250 a += 65536 ;
251 pbmtopk_pkbyte(a >> 8) ;
252 pbmtopk_pkbyte(a & 255) ;
253 }
254
255
256
257 /* write three bytes to the PK file */
258 static void
259 pkthreebytes(integer const a) {
260
261 pbmtopk_pkbyte((a>>16) & 255) ;
262 pbmtopk_pkbyte((a>>8) & 255) ;
263 pbmtopk_pkbyte(a & 255) ;
264 }
265
266
267
268 /* write four bytes to the PK file */
269 static void
270 pkword(integer const a) {
271 pbmtopk_pkbyte((a>>24) & 255) ;
272 pbmtopk_pkbyte((a>>16) & 255) ;
273 pbmtopk_pkbyte((a>>8) & 255) ;
274 pbmtopk_pkbyte(a & 255) ;
275 }
276
277
278
279 /* write a nibble to the PK file */
280 static void
281 pknyb(integer const a) {
282
283 if (bitweight == 16) {
284 outputbyte = (a<<4) ;
285 bitweight = 1 ;
286 } else {
287 pbmtopk_pkbyte(outputbyte + a) ;
288 bitweight = 16 ;
289 }
290 }
291
292
293
294 /* write preamble to PK file */
295 static void
296 writepreamble(void) {
297 integer i ;
298 const char * const comment = "PBMtoPK 2.4 output" ;
299
300 pbmtopk_pkbyte(247) ; /* PRE command */
301 pbmtopk_pkbyte(89) ; /* PK file type */
302 pbmtopk_pkbyte(strlen(comment)) ; /* output comment */
303 for (i = 0 ; i < strlen(comment); i++)
304 pbmtopk_pkbyte(xord[(int)comment[i]]) ;
305 pkword(designsize) ; /* write designsize */
306 pkword(checksum) ; /* write checksum; calculate if possible */
307 pkword(hppp) ; /* write H pixels per point */
308 pkword(hppp) ; /* write V pixels per point */
309 }
310
311
312
313 /* write postamble to PK file, padded to word length */
314 static void
315 writepostamble(void) {
316 pbmtopk_pkbyte(245) ; /* POST command */
317 while (pbmtopk_pkloc % 4)
318 pbmtopk_pkbyte(246) ; /* pad with no-ops */
319 pm_message("%d bytes written to packed file.", pbmtopk_pkloc) ;
320 }
321
322
323
324 /* write a byte to the TFM file */
325 static void
326 tfmbyte(integer const b_in) {
327 integer b;
328
329 b = b_in;
330
331 if (b < 0) b += 256 ;
332 putc(b, tfmfile) ;
333 }
334
335
336
337 /* write a half word to the TFM file */
338 static void
339 tfmhalfword(integer const a_in) {
340
341 integer a;
342
343 a = a_in;
344
345 if (a < 0) a += 65536 ;
346 tfmbyte(a >> 8) ;
347 tfmbyte(a & 255) ;
348 }
349
350
351
352 /* write a word to the TFM file */
353 static void
354 tfmword(integer const a) {
355 tfmbyte((a>>24) & 255) ;
356 tfmbyte((a>>16) & 255) ;
357 tfmbyte((a>>8) & 255) ;
358 tfmbyte(a & 255) ;
359 }
360
361
362
363 /* write the whole TFM file for the font */
364 static void
365 writetfmfile(void) {
366 integer totallength ;
367 integer headersize = 17;
368 integer i ;
369
370 if (largestch - smallestch < 0) {
371 largestch = 0;
372 smallestch = 1;
373 }
374 if (numparam < 7) /* set default parameters */
375 switch (numparam) {
376 case 0: /* slant */
377 parameters[numparam++] = 0 ;
378 case 1: /* space */
379 parameters[numparam++] = fixword(designunits(emwidth/3.0));
380 case 2: /* space_stretch */
381 parameters[numparam++] = fixword(unfixword(parameters[1])/2.0) ;
382 case 3: /* space_shrink */
383 parameters[numparam++] = fixword(unfixword(parameters[1])/3.0) ;
384 case 4: /* x_height */
385 parameters[numparam++] = fixword(0.45);
386 case 5: /* quad */
387 parameters[numparam++] = fixword(designunits(emwidth)) ;
388 case 6: /* extra_space */
389 parameters[numparam++] = fixword(unfixword(parameters[1])/3.0) ;
390 }
391 totallength = 6 + headersize + (largestch+1-smallestch) +
392 numwidth + numheight + numdepth + numitalic + numparam ;
393 /* lengths */
394 tfmhalfword(totallength) ; /* write file TFM length */
395 tfmhalfword(headersize) ; /* write TFM header length */
396 tfmhalfword(smallestch) ; /* write lowest char index */
397 tfmhalfword(largestch) ; /* write highest char index */
398 tfmhalfword(numwidth) ; /* write number of widths */
399 tfmhalfword(numheight) ; /* write number of heights */
400 tfmhalfword(numdepth) ; /* write number of depths */
401 tfmhalfword(numitalic) ; /* write number of italcorrs */
402 tfmhalfword(0) ; /* lig/kern table */
403 tfmhalfword(0) ; /* kern table */
404 tfmhalfword(0) ; /* extensible char table */
405 tfmhalfword(numparam) ; /* number of fontdimens */
406 /* header */
407 tfmword(checksum) ; /* write checksum */
408 tfmword(designsize) ; /* write designsize */
409 if (strlen(codingscheme) > 39)
410 tfmbyte(39) ; /* write coding scheme len */
411 else
412 tfmbyte(strlen(codingscheme)) ;
413 for (i = 0; i < 39; i++) /* write coding scheme */
414 if
415 (*codingscheme) tfmbyte(xord[(int)(*codingscheme++)]) ;
416 else
417 tfmbyte(0) ;
418 if (strlen(familyname) > 19)
419 tfmbyte(19) ; /* write family length */
420 else
421 tfmbyte(strlen(familyname)) ;
422 for (i = 0; i < 19; i++) /* write family */
423 if (*familyname)
424 tfmbyte(xord[(int)(*familyname++)]) ;
425 else
426 tfmbyte(0) ;
427 /* char_info */
428 for (car = smallestch; car <= largestch; car++)
429 if (filename[car]) { /* write character info */
430 tfmbyte(tfmindex[car]) ;
431 tfmbyte((hgtindex[car]<<4) + depindex[car]) ;
432 tfmbyte(italindex[car]<<2) ;
433 tfmbyte(0) ;
434 } else
435 tfmword(0) ;
436 /* width table */
437 for (i = 0; i < numwidth; i++) tfmword(widthtab[i]) ;
438 /* height table */
439 for (i = 0; i < numheight; i++) tfmword(heighttab[i]) ;
440 /* depth table */
441 for (i = 0; i < numdepth; i++) tfmword(depthtab[i]) ;
442 /* italic correction table */
443 for (i = 0; i < numitalic; i++) tfmword(italictab[i]) ;
444 /* no lig_kern, kern, or extent tables */
445 /* fontdimen table */
446 for (i = 0; i < numparam; i++)
447 if (i && !fixrange(parameters[i]))
448 pm_error("parameter %d out of range (-p)", i);
449 else
450 tfmword(parameters[i]) ;
451 pm_message("%d bytes written to tfm file.", totallength*4) ;
452 }
453
454
455
456 /* read a character from a PBM file */
457 static void readcharacter(void) {
458 FILE *fp;
459
460 fp = pm_openr(filename[car]);
461 bitmap = pbm_readpbm(fp, &width, &height) ;
462 pm_close(fp) ;
463
464 if ((charflags[car] & HORZESC) == 0) horzesc[car] = width ;
465 if ((charflags[car] & VERTESC) == 0) vertesc[car] = 0;
466 if ((charflags[car] & XOFFSET) == 0) xoffset[car] = 0;
467 if ((charflags[car] & YOFFSET) == 0) yoffset[car] = height-1;
468 if ((charflags[car] & TFMWIDTH) == 0)
469 tfmindex[car] = add_tfmwidth(fixword(designunits(width)));
470 if ((charflags[car] & TFMHEIGHT) == 0)
471 hgtindex[car] = add_tfmheight(fixword(designunits(yoffset[car]+1)));
472 if ((charflags[car] & TFMDEPTH) == 0)
473 depindex[car] =
474 add_tfmdepth(fixword(designunits(height-1-yoffset[car])));
475 if ((charflags[car] & TFMITALIC) == 0) italindex[car] = 0;
476
477 if (car < smallestch) smallestch = car;
478 if (car > largestch) largestch = car;
479 if (width > emwidth) emwidth = width ;
480 }
481
482
483
484 /* test if two rows of the PBM are the same */
485 static int
486 equal(const bit * const row1,
487 const bit * const row2) {
488
489 integer i ;
490
491 for (i = 0; i < width; i++)
492 if (row1[i] != row2[i])
493 return (0) ;
494
495 return(1) ;
496 }
497
498
499
500 static void
501 shipcharacter(void) {
502
503 integer compsize ;
504 integer i, j, k ;
505 bit *zerorow, *onesrow ;
506 integer *repeatptr, *bitcounts ;
507 integer count ;
508 integer test ;
509 integer curptr, rowptr ;
510 integer bitval ;
511 integer repeatflag ;
512 integer colptr ;
513 integer currepeat ;
514 integer dynf ;
515 integer deriv[14] ;
516 integer bcompsize ;
517 boolean firston ;
518 integer flagbyte ;
519 boolean state ;
520 boolean on ;
521 integer hbit ;
522 integer pbit ;
523 boolean ron, son ;
524 integer rcount, scount ;
525 integer ri, si ;
526 integer max2 ;
527 integer predpkloc ;
528 integer buff ;
529
530 integer tfwid = widthtab[tfmindex[car]] ;
531 integer hesc = horzesc[car] ;
532 integer vesc = vertesc[car] ;
533 integer xoff = xoffset[car] ;
534 integer yoff = yoffset[car] ;
535
536 MALLOCARRAY(repeatptr, height + 1);
537 MALLOCARRAY(bitcounts, height * width);
538 if (repeatptr == NULL || bitcounts == NULL)
539 pm_error("out of memory while allocating bit counts");
540 zerorow = pbm_allocrow(width) ; /* initialize plain rows */
541 onesrow = pbm_allocrow(width) ;
542 for (i = 0 ; i < width ; i++) {
543 zerorow[i] = PBM_WHITE ;
544 onesrow[i] = PBM_BLACK ;
545 }
546 for (i=0; i < height; i = k) { /* set repeat pointers */
547 k = i + 1;
548 if (!equal(bitmap[i], zerorow) && !equal(bitmap[i], onesrow)) {
549 while (k < height && equal(bitmap[i], bitmap[k]))
550 k++;
551 repeatptr[i] = k - i - 1;
552 } else {
553 repeatptr[i] = 0;
554 }
555 }
556 repeatptr[height] = 0 ;
557 colptr = width - 1 ;
558 repeatflag = currepeat = curptr = count = rowptr = 0 ;
559 test = PBM_WHITE ;
560 do {
561 colptr++ ;
562 if (colptr == width) { /* end of row, get next row */
563 colptr = 0 ;
564 rowptr = currepeat ;
565 if (repeatptr[currepeat] > 0) {
566 repeatflag = repeatptr[currepeat] ;
567 currepeat += repeatflag ;
568 rowptr += repeatflag ;
569 }
570 currepeat++ ;
571 }
572 if (rowptr >= height) bitval = -1 ;
573 else bitval = bitmap[rowptr][colptr] ;
574 if (bitval == test) count++ ; /* count repeated pixels */
575 else { /* end of pixel run */
576 bitcounts[curptr++] = count ;
577 if (curptr+3 >= height*width)
578 pm_error("out of memory while saving character counts");
579 count = 1 ;
580 test = bitval ;
581 if (repeatflag > 0) {
582 bitcounts[curptr++] = -repeatflag ;
583 repeatflag = 0 ;
584 }
585 }
586 } while (test != -1) ;
587 bitcounts[curptr] = 0 ;
588 bitcounts[curptr + 1] = 0 ;
589 for (i = 1 ; i <= 13 ; i ++) deriv[i] = 0 ;
590 i = firston = (bitcounts[0] == 0) ;
591 compsize = 0 ;
592 while (bitcounts[i] != 0) { /* calculate dyn_f */
593 j = bitcounts[i] ;
594 if (j == -1) compsize++ ;
595 else {
596 if (j < 0) {
597 compsize++ ;
598 j = -j ;
599 }
600 if (j < 209) compsize += 2 ;
601 else {
602 k = j - 193 ;
603 while (k >= 16) {
604 k >>= 4 ;
605 compsize += 2 ;
606 }
607 compsize++ ;
608 }
609 if (j < 14) (deriv[j])-- ;
610 else if (j < 209) (deriv[(223 - j) / 15])++ ;
611 else {
612 k = 16 ;
613 while (((k<<4) < j + 3)) k <<= 4 ;
614 if (j - k <= 192)
615 deriv[(207 - j + k) / 15] += 2 ;
616 }
617 }
618 i++ ;
619 }
620 bcompsize = compsize ;
621 dynf = 0 ;
622 for (i = 1 ; i <= 13 ; i ++) {
623 compsize += deriv[i] ;
624 if (compsize <= bcompsize) {
625 bcompsize = compsize ;
626 dynf = i ;
627 }
628 }
629 compsize = ((bcompsize + 1)>>1) ;
630 if ((compsize > ((height*width+7)>>3)) || (height*width == 0)) {
631 compsize = ((height*width+7)>>3) ;
632 dynf = 14 ;
633 }
634 flagbyte = (dynf<<4) ;
635 if (firston) flagbyte |= 8 ;
636 if (tfwid > 16777215 || tfwid < 0 || hesc < 0 || vesc != 0 ||
637 compsize > 196579 || width > 65535 || height > 65535 ||
638 xoff > 32767 || yoff > 32767 || xoff < -32768 || yoff < -32768) {
639 flagbyte |= 7 ; /* long form preamble */
640 pbmtopk_pkbyte(flagbyte) ;
641 compsize += 28 ;
642 pkword(compsize) ; /* char packet size */
643 pkword(car) ; /* character number */
644 predpkloc = pbmtopk_pkloc + compsize ;
645 pkword(tfwid) ; /* TFM width */
646 pkword(hesc<<16) ; /* horiz escapement */
647 pkword(vesc<<16) ; /* vert escapement */
648 pkword(width) ; /* bounding box width */
649 pkword(height) ; /* bounding box height */
650 pkword(xoff) ; /* horiz offset */
651 pkword(yoff) ; /* vert offset */
652 } else if (hesc > 255 || width > 255 || height > 255 ||
653 xoff > 127 || yoff > 127 || xoff < -128 ||
654 yoff < -128 || compsize > 1016) {
655 compsize += 13 ; /* extended short preamble */
656 flagbyte += (compsize>>16) + 4 ;
657 pbmtopk_pkbyte(flagbyte) ;
658 pkhalfword(compsize & 65535) ; /* char packet size */
659 pbmtopk_pkbyte(car) ; /* character number */
660 predpkloc = pbmtopk_pkloc + compsize ;
661 pkthreebytes(tfwid) ; /* TFM width */
662 pkhalfword(hesc) ; /* horiz escapement */
663 pkhalfword(width) ; /* bounding box width */
664 pkhalfword(height) ; /* bounding box height */
665 pkhalfword(xoff) ; /* horiz offset */
666 pkhalfword(yoff) ; /* vert offset */
667 } else {
668 compsize += 8 ; /* short form preamble */
669 flagbyte = flagbyte + (compsize>>8) ;
670 pbmtopk_pkbyte(flagbyte) ;
671 pbmtopk_pkbyte(compsize & 255) ; /* char packet size */
672 pbmtopk_pkbyte(car) ; /* character number */
673 predpkloc = pbmtopk_pkloc + compsize ;
674 pkthreebytes(tfwid) ; /* TFM width */
675 pbmtopk_pkbyte(hesc) ; /* horiz escapement */
676 pbmtopk_pkbyte(width) ; /* bounding box width */
677 pbmtopk_pkbyte(height) ; /* bounding box height */
678 pbmtopk_pkbyte(xoff) ; /* horiz offset */
679 pbmtopk_pkbyte(yoff) ; /* vert offset */
680 }
681 if (dynf != 14) { /* write packed character */
682 bitweight = 16 ;
683 max2 = 208 - 15 * dynf ;
684 i = firston ;
685 while (bitcounts[i] != 0) {
686 j = bitcounts[i] ;
687 if (j == - 1) pknyb(15) ;
688 else {
689 if (j < 0) {
690 pknyb(14) ;
691 j = -j ;
692 }
693 if (j <= dynf) pknyb(j) ;
694 else if (j <= max2) {
695 j -= dynf + 1 ;
696 pknyb((j >> 4) + dynf + 1) ;
697 pknyb((j & 15)) ;
698 } else {
699 j -= max2 - 15 ;
700 k = 16 ;
701 while (k <= j) {
702 k <<= 4 ;
703 pknyb(0) ;
704 }
705 while (k > 1) {
706 k >>= 4 ;
707 pknyb(j / k) ;
708 j = j % k ;
709 }
710 }
711 }
712 i++ ;
713 }
714 if (bitweight != 16) pbmtopk_pkbyte(outputbyte) ;
715 } else { /* write bitmap character */
716 buff = 0 ;
717 pbit = 8 ;
718 i = firston ;
719 hbit = width ;
720 on = ! firston ;
721 state = 0 ;
722 count = repeatflag = 0 ;
723 while ((bitcounts[i] != 0) || state || (count > 0)) {
724 if (state) {
725 count = rcount ;
726 i = ri ;
727 on = ron ;
728 repeatflag-- ;
729 } else {
730 rcount = count ;
731 ri = i ;
732 ron = on ;
733 }
734 do {
735 if (count == 0) {
736 if (bitcounts[i] < 0) {
737 if (! state) repeatflag = -bitcounts[i] ;
738 i++ ;
739 }
740 count = bitcounts[i] ;
741 i++ ;
742 on = !on ;
743 }
744 if ((count >= pbit) && (pbit < hbit)) {
745 if (on) buff += (1 << pbit) - 1 ;
746 pbmtopk_pkbyte(buff) ;
747 buff = 0 ;
748 hbit -= pbit ;
749 count -= pbit ;
750 pbit = 8 ;
751 } else if ((count < pbit) && (count < hbit)) {
752 if (on) buff += (1 << pbit) - (1 << (pbit - count)) ;
753 pbit -= count ;
754 hbit -= count ;
755 count = 0 ;
756 } else {
757 if (on) buff += (1 << pbit) - (1 << (pbit - hbit)) ;
758 count -= hbit ;
759 pbit -= hbit ;
760 hbit = width ;
761 if (pbit == 0) {
762 pbmtopk_pkbyte(buff) ;
763 buff = 0 ;
764 pbit = 8 ;
765 }
766 }
767 } while (hbit != width) ;
768 if (state && (repeatflag == 0)) {
769 count = scount ;
770 i = si ;
771 on = son ;
772 state = 0 ;
773 } else if (! state && (repeatflag > 0)) {
774 scount = count ;
775 si = i ;
776 son = on ;
777 state = 1 ;
778 }
779 }
780 if (pbit != 8) pbmtopk_pkbyte(buff) ;
781 }
782 if (predpkloc != pbmtopk_pkloc)
783 pm_error("bad predicted character length: character %d", car);
784 pbm_freerow(zerorow);
785 pbm_freerow(onesrow);
786 free((char *)repeatptr);
787 free((char *)bitcounts);
788 }
789
790
791
792 /* check that character is in valid range */
793 static void
794 checkchar(void) {
795 if (car < 0 || car >= MAXPKCHAR)
796 pm_error("character must be in range 0 to %d", MAXPKCHAR-1) ;
797 }
798
799
800
801 /* read character information from an option file */
802 static void
803 optionfile(const char * const name) {
804
805 FILE *fp ;
806 char buffer[MAXOPTLINE] ;
807
808 fp = pm_openr(name);
809 while (!feof(fp)) {
810 char *here = buffer;
811
812 if (fgets(buffer, MAXOPTLINE, fp) == NULL) break ;
813 while (ISSPACE(*here)) here++ ;
814 if (*here && *here == '=') {
815 if (sscanf(here+1, "%d", &car) != 1)
816 pm_error("bad option file line %s", buffer) ;
817 } else if (*here && *here != '%' && *here != '#') {
818 char str[NAMELENGTH] ;
819 integer i, n;
820
821 checkchar() ;
822 if (sscanf(here, "%s%n", str, &n) != 1)
823 pm_error("bad option file line %s", buffer) ;
824 filename[car] = strdup(str);
825 if (filename[car] == NULL)
826 pm_error("out of memory allocating filename %s", str);
827 for (i = 1; i < 256; i<<=1) {
828 here += n;
829 if (sscanf(here, "%s%n", str, &n) != 1) break ;
830 if (strcmp(str, "*")) {
831 charflags[car] |= i ;
832 switch (i) {
833 case XOFFSET:
834 xoffset[car] = atoi(str) ;
835 break ;
836 case YOFFSET:
837 yoffset[car] = atoi(str) ;
838 break ;
839 case HORZESC:
840 horzesc[car] = atoi(str) ;
841 break ;
842 case VERTESC:
843 vertesc[car] = atoi(str) ;
844 break ;
845 case TFMWIDTH:
846 tfmindex[car] = add_tfmwidth(fixword(atof(str))) ;
847 break ;
848 case TFMHEIGHT:
849 hgtindex[car] = add_tfmheight(fixword(atof(str))) ;
850 break ;
851 case TFMDEPTH:
852 depindex[car] = add_tfmdepth(fixword(atof(str))) ;
853 break ;
854 case TFMITALIC:
855 italindex[car] = add_tfmitalic(fixword(atof(str))) ;
856 break ;
857 }
858 }
859 }
860 car++ ;
861 }
862 }
863 pm_close(fp) ;
864 }
865
866
867
868 int
869 main(int argc, char *argv[]) {
870 integer i, hesc, vesc, xoff, yoff, tfwid, tfdep, tfhgt, tfital ;
871 byte flags ;
872 const char * const usage = "pkfile[.pk] tfmfile[.tfm] dpi "
873 "[-s designsize] [-p num param...]\n"
874 "[-C codingscheme ] [-F family] [-c num | <char>]...\n"
875 "<char> is:\n"
876 "[-W tfmwidth] [-H tfmheight] [-D tfmdepth] [-I ital_corr] "
877 "[-h horiz]\n"
878 "[-v vert] [-x xoffset] [-y yoffset] file\n"
879 "or:\n"
880 "-f optfile\n" ;
881
882 pbm_init(&argc, argv);
883 initialize_pk() ;
884
885 if (--argc < 1) pm_usage(usage) ;
886 ++argv;
887 if(strlen(*argv) + 4 > NAMELENGTH)
888 pm_error("pkname is too long");
889 strcpy(pkname, *argv) ;
890 pbmtopk_add_suffix(pkname, ".pk") ;
891
892 if (--argc < 1) pm_usage(usage);
893 ++argv;
894 if(strlen(*argv) + 4 > NAMELENGTH)
895 pm_error("tfmname is too long");
896 strcpy(tfmname, *argv) ;
897 pbmtopk_add_suffix(tfmname, ".tfm") ;
898
899 if (--argc < 1) pm_usage(usage) ;
900 resolution = atoi(*++argv) ;
901 if (resolution < 1 || resolution > 32767)
902 pm_error("unlikely resolution %d dpi", resolution);
903
904 car = flags = hesc = vesc = xoff = yoff = tfwid = 0;
905 while (++argv, --argc) {
906 if (argv[0][0] == '-' && argv[0][1]) {
907 char c, *p;
908 c = argv[0][1] ;
909 if (argv[0][2]) p = *argv + 2 ; /* set argument pointer */
910 else if (++argv, --argc) p = *argv ;
911 else pm_usage(usage) ;
912 switch (c) {
913 case 'C':
914 codingscheme = p;
915 break ;
916 case 'F':
917 familyname = p;
918 break ;
919 case 'c':
920 car = atoi(p) ;
921 break ;
922 case 's':
923 designsize = fixword(atof(p));
924 if (designsize < 1048576)
925 pm_error("design size %f out of range",
926 unfixword(designsize));
927 case 'h':
928 hesc = atoi(p) ;
929 flags |= HORZESC ;
930 break ;
931 case 'v':
932 vesc = atoi(p) ;
933 flags |= VERTESC ;
934 break ;
935 case 'x':
936 xoff = atoi(p) ;
937 flags |= XOFFSET ;
938 break ;
939 case 'y':
940 yoff = atoi(p) ;
941 flags |= YOFFSET ;
942 break ;
943 case 'W':
944 tfwid = fixword(atof(p)) ;
945 flags |= TFMWIDTH ;
946 break ;
947 case 'H':
948 tfhgt = fixword(atof(p)) ;
949 flags |= TFMHEIGHT ;
950 break ;
951 case 'D':
952 tfdep = fixword(atof(p)) ;
953 flags |= TFMDEPTH ;
954 break ;
955 case 'I':
956 tfital = fixword(atof(p)) ;
957 flags |= TFMITALIC ;
958 break ;
959 case 'f':
960 optionfile(p) ;
961 break ;
962 case 'p':
963 numparam = atoi(p);
964 if (numparam < 1 || numparam > MAXPARAMS)
965 pm_error("parameter count %d out of range", numparam);
966 for (i=0; i<numparam; i++)
967 if (++argv,--argc)
968 parameters[i] = fixword(atof(*argv)) ;
969 else
970 pm_error("not enough parameters (-p)");
971 break ;
972 default:
973 pm_usage(usage) ;
974 }
975 } else {
976 checkchar() ;
977 if (flags & TFMWIDTH)
978 tfmindex[car] = add_tfmwidth(tfwid);
979 if (flags & TFMDEPTH)
980 depindex[car] = add_tfmdepth(tfdep);
981 if (flags & TFMHEIGHT)
982 hgtindex[car] = add_tfmheight(tfhgt);
983 if (flags & TFMITALIC)
984 italindex[car] = add_tfmitalic(tfital);
985 horzesc[car] = hesc ;
986 vertesc[car] = vesc ;
987 xoffset[car] = xoff ;
988 yoffset[car] = yoff ;
989 filename[car] = *argv ;
990 charflags[car] = flags ;
991 car++ ;
992 flags = 0;
993 }
994 }
995 hppp = ROUND((resolution<<16) / 72.27) ;
996 pkfile = pm_openw(pkname);
997 tfmfile = pm_openw(tfmname);
998 writepreamble() ;
999 for (car = 0 ; car < MAXPKCHAR ; car++)
1000 if (filename[car]) {
1001 readcharacter() ;
1002 shipcharacter() ;
1003 }
1004 writepostamble() ;
1005 writetfmfile() ;
1006 pm_close(pkfile) ;
1007 pm_close(tfmfile) ;
1008
1009 return 0;
1010 }
1011
1012
1013