Debian Codegraph @

texk/ttf2pk2/vplaux.c

raw
1 /*
2 * vplaux.c
3 *
4 * This file is part of the ttf2pk package.
5 *
6 * Copyright 1997-1999, 2000 by
7 * Frederic Loyer <[email protected]>
8 * Werner Lemberg <[email protected]>
9 */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <math.h>
19
20 #include "ttf2tfm.h"
21 #include "newobj.h"
22 #include "ttfenc.h"
23 #include "texenc.h"
24 #include "tfmaux.h"
25 #include "vplaux.h"
26 #include "errormsg.h"
27 #include "case.h"
28
29
30 #undef PI
31 #define PI 3.14159265358979323846264338327
32
33
34 #define vout(s) fprintf(out, s)
35
36 #define voutln(str) {fprintf(out, "%s\n", str); vlevout(level);}
37 #define voutln2(f, s) {fprintf(out, f, s); vlevnlout(level);}
38 #define voutln3(f, a, b) {fprintf(out, f, a, b); vlevnlout(level);}
39 #define voutln4(f, a, b, c) {fprintf(out, f, a, b, c); vlevnlout(level);}
40
41
42 static char vcharbuf[6];
43 static char vnamebuf[100];
44
45 /* the depth of parenthesis nesting in VPL file being written */
46 static int level;
47
48
49 static FILE *out;
50
51
52 static void
53 vlevout(register int l)
54 {
55 while (l--)
56 vout(" ");
57 }
58
59
60 static void
61 vlevnlout(int level)
62 {
63 vout("\n");
64 vlevout(level);
65 }
66
67
68 static void
69 vleft(int *levelp)
70 {
71 (*levelp)++;
72 vout("(");
73 }
74
75
76 static void
77 vright(int *levelp)
78 {
79 (*levelp)--;
80 voutln(")");
81 }
82
83
84 static char *
85 vchar(int c,
86 char *buf,
87 Boolean forceoctal)
88 {
89 if (forceoctal == 0 && isalnum(c))
90 (void)sprintf(buf, "C %c", c);
91 else
92 (void)sprintf(buf, "O %o", (unsigned)c);
93 return buf;
94 }
95
96
97 static char *
98 vname(int c,
99 char *buf,
100 ttfinfo **array,
101 Boolean forceoctal)
102 {
103 if (!forceoctal && isalnum(c))
104 buf[0] = '\0';
105 else
106 sprintf(buf, " (comment %s)", array[c]->adobename);
107 return buf;
108 }
109
110
111 static int
112 texheight(register ttfinfo *ti,
113 ttfinfo *ac,
114 int xh)
115 {
116 register const char **p;
117 register ttfinfo *aci, *acci;
118 char buffer[200];
119
120
121 if (xh <= 50 || *(ti->adobename + 1))
122 return ti->ury; /* that was the simple case */
123
124 for (p = accents; *p; p++) /* otherwise we look for accented letters. */
125 /* We even check glyphs not in any encoding */
126 if (NULL != (aci = findadobe(*p, ac)))
127 {
128 strcpy(buffer, ti->adobename);
129 strcat(buffer, *p);
130 if (NULL != (acci = findadobe(buffer, ac)))
131 return acci->ury - aci->ury + xh;
132 }
133 return ti->ury;
134 }
135
136
137 /*
138 * Compute uppercase mapping, when making a small caps font.
139 */
140
141 void
142 upmap(Font *fnt)
143 {
144 register ttfinfo *ti, *Ti;
145 register const char *p;
146 register char *q;
147 register pcc *np, *nq;
148 int i, j;
149 char lwr[50];
150
151
152 for (Ti = fnt->charlist; Ti; Ti = Ti->next)
153 {
154 p = Ti->adobename;
155 if (isupper((unsigned char)*p))
156 {
157 q = lwr;
158 for (; *p; p++)
159 *q++ = tolower((unsigned char)*p);
160 *q = '\0';
161
162 if (NULL != (ti = findmappedadobe(lwr, fnt->inencptrs)))
163 {
164 for (i = ti->outcode; i >= 0; i = fnt->nextout[i])
165 fnt->uppercase[i] = Ti;
166 for (i = Ti->outcode; i >= 0; i = fnt->nextout[i])
167 fnt->lowercase[i] = ti;
168 }
169 }
170 }
171
172 /*
173 * Note that, contrary to the normal true/false conventions,
174 * uppercase[i] is NULL and lowercase[i] is non-NULL when `i' is the
175 * ASCII code of an uppercase letter; and vice versa for lowercase
176 * letters.
177 */
178
179 if (NULL != (ti = findmappedadobe("germandbls", fnt->inencptrs)))
180 if (NULL != (Ti = findmappedadobe("S", fnt->inencptrs)))
181 /* we also construct SS */
182 {
183 for (i = ti->outcode; i >= 0; i = fnt->nextout[i])
184 fnt->uppercase[i] = ti;
185 ti->incode = -1;
186 ti->width = Ti->width << 1;
187 ti->llx = Ti->llx;
188 ti->lly = Ti->lly;
189 ti->urx = Ti->width + Ti->urx;
190 ti->ury = Ti->ury;
191 ti->kerns = Ti->kerns;
192
193 np = newpcc();
194 np->partname = "S";
195 nq = newpcc();
196 nq->partname = "S";
197 nq->xoffset = Ti->width;
198 np->next = nq;
199 ti->pccs = np;
200 ti->constructed = True;
201 }
202
203 for (i = 0; casetable[i].upper; i++)
204 {
205 if ((ti = findmappedadobe(casetable[i].lower, fnt->inencptrs)))
206 for (j = ti->outcode; j >= 0; j = fnt->nextout[j])
207 fnt->uppercase[j] = findmappedadobe(casetable[i].upper,
208 fnt->inencptrs);
209 }
210 }
211
212 /*
213 * The logic above seems to work well enough, but it leaves useless
214 * characters like `fi' and `fl' in the font if they were present
215 * initially, and it omits characters like `dotlessj' if they are
216 * absent initially.
217 */
218
219
220 void
221 writevpl(Font *fnt, char makevpl, Boolean forceoctal)
222 {
223 register int i, j, k;
224 register ttfinfo *ti;
225 register lig *nlig;
226 register kern *nkern;
227 register pcc *npcc;
228 ttfinfo *asucc, *asub, *api;
229 ttfptr *kern_eq;
230 int xoff, yoff, ht;
231 int bc, ec;
232 char buf[200];
233 char header[256];
234 Boolean unlabeled;
235 float Slant;
236
237
238 out = fnt->vplout;
239
240 header[0] = '\0';
241 strncat(header, "Created by `", 12);
242 strncat(header, fnt->titlebuf, 255 - 12 - 1);
243 strncat(header, "'", 1);
244
245 voutln2("(VTITLE %s)", header);
246 voutln("(COMMENT Please change VTITLE if you edit this file)");
247 (void)sprintf(buf, "TeX-%s%s%s%s",
248 fnt->fullname,
249 (fnt->efactor == 1.0 ? "" : "-E"),
250 (fnt->slant == 0.0 ? "" : "-S"),
251 (makevpl == 1 ? "" : "-CSC"));
252
253 if (strlen(buf) > 19) /* too long, will retain first 9 and last 10 chars */
254 {
255 register char *p, *q;
256
257
258 for (p = &buf[9], q = &buf[strlen(buf)-10]; p < &buf[19];
259 p++, q++)
260 *p = *q;
261 buf[19] = '\0';
262 }
263 voutln2("(FAMILY %s)", buf);
264
265 {
266 char tbuf[300];
267 const char *base_encoding = fnt->codingscheme;
268
269
270 if (strcmp(fnt->outencoding->name, base_encoding) == 0)
271 sprintf(tbuf, "%s", fnt->outencoding->name);
272 else
273 sprintf(tbuf, "%s + %s", base_encoding, fnt->outencoding->name);
274
275 if (strlen(tbuf) > 39)
276 {
277 warning("Coding scheme too long; shortening to 39 characters");
278 tbuf[39] = '\0';
279 }
280 voutln2("(CODINGSCHEME %s)", tbuf);
281 }
282
283 {
284 long t, sc;
285 char *s;
286 int n, pos;
287
288
289 s = header;
290 n = strlen(s);
291 t = ((long)n) << 24;
292 sc = 16;
293 pos = 18;
294
295 voutln(
296 "(COMMENT The following `HEADER' lines are equivalent to the string)");
297 voutln2("(COMMENT \"%s\")", header);
298
299 while (n > 0)
300 {
301 t |= ((long)(*(unsigned char *)s++)) << sc;
302 sc -= 8;
303 if (sc < 0)
304 {
305 voutln3("(HEADER D %d O %lo)", pos, t);
306 t = 0;
307 sc = 24;
308 pos++;
309 }
310 n--;
311 }
312 if (t)
313 voutln3("(HEADER D %d O %lo)", pos, t);
314 }
315
316 voutln("(DESIGNSIZE R 10.0)");
317 voutln("(DESIGNUNITS R 1000)");
318 voutln("(COMMENT DESIGNSIZE (1 em) IS IN POINTS)");
319 voutln("(COMMENT OTHER DIMENSIONS ARE MULTIPLES OF DESIGNSIZE/1000)");
320
321 #if 0
322 /* Let vptovf compute the checksum. */
323 voutln2("(CHECKSUM O %lo)", cksum ^ 0xFFFFFFFF);
324 #endif
325
326 if (fnt->boundarychar >= 0)
327 voutln2("(BOUNDARYCHAR O %lo)", (unsigned long)fnt->boundarychar);
328
329 vleft(&level);
330 voutln("FONTDIMEN");
331
332 Slant = fnt->slant - fnt->efactor * tan(fnt->italicangle * (PI / 180.0));
333
334 if (Slant)
335 voutln2("(SLANT R %f)", Slant);
336 voutln2("(SPACE D %d)", fnt->fontspace);
337 if (!fnt->fixedpitch)
338 {
339 voutln2("(STRETCH D %d)", transform(200, 0, fnt->efactor, fnt->slant));
340 voutln2("(SHRINK D %d)", transform(100, 0, fnt->efactor, fnt->slant));
341 }
342 voutln2("(XHEIGHT D %d)", fnt->xheight);
343 voutln2("(QUAD D %d)", transform(1000, 0, fnt->efactor, fnt->slant));
344 voutln2("(EXTRASPACE D %d)",
345 fnt->fixedpitch ? fnt->fontspace :
346 transform(111, 0, fnt->efactor, fnt->slant));
347 vright(&level);
348
349 vleft(&level);
350 voutln("MAPFONT D 0");
351 voutln2("(FONTNAME %s)", fnt->fullname);
352 #if 0
353 voutln2("(FONTCHECKSUM O %lo)", (unsigned long)cksum);
354 #endif
355 vright(&level);
356
357 if (makevpl > 1)
358 {
359 vleft(&level);
360 voutln("MAPFONT D 1");
361 voutln2("(FONTNAME %s)", fnt->fullname);
362 voutln2("(FONTAT D %d)", (int)(1000.0 * fnt->capheight + 0.5));
363 #if 0
364 voutln2("(FONTCHECKSUM O %lo)", (unsigned long)cksum);
365 #endif
366 vright(&level);
367 }
368
369 for (i = 0; i <= 0xFF && fnt->outencptrs[i] == NULL; i++)
370 ;
371 bc = i;
372 for (i = 0xFF; i >= 0 && fnt->outencptrs[i] == NULL; i--)
373 ;
374 ec = i;
375
376 vleft(&level);
377 voutln("LIGTABLE");
378 ti = findadobe("||", fnt->charlist);
379 unlabeled = True;
380 for (nlig = ti->ligs; nlig; nlig = nlig->next)
381 if (NULL != (asucc = findmappedadobe(nlig->succ, fnt->inencptrs)))
382 {
383 if (NULL != (asub = findmappedadobe(nlig->sub, fnt->inencptrs)))
384 if (asucc->outcode >= 0)
385 if (asub->outcode >= 0)
386 {
387 if (unlabeled)
388 {
389 voutln("(LABEL BOUNDARYCHAR)");
390 unlabeled = False;
391 }
392 for (j = asucc->outcode; j >= 0; j = fnt->nextout[j])
393 voutln4("(%s %s O %o)", vplligops[nlig->op],
394 vchar(j, vcharbuf, forceoctal),
395 (unsigned)asub->outcode);
396 }
397 }
398 if (!unlabeled)
399 voutln("(STOP)");
400
401 for (i = bc; i <= ec; i++)
402 if ((ti = fnt->outencptrs[i]) && ti->outcode == i)
403 {
404 unlabeled = True;
405 if (fnt->uppercase[i] == NULL)
406 /* omit ligatures from smallcap lowercase */
407 for (nlig = ti->ligs; nlig; nlig = nlig->next)
408 if ((((NULL != (asucc = findmappedadobe(nlig->succ, fnt->inencptrs)))
409 && (asucc->outcode >= 0))
410 || ((strcmp(nlig->succ, "||") == 0)
411 && (fnt->boundarychar >= 0)))
412 && (NULL != (asub = findmappedadobe(nlig->sub, fnt->inencptrs)))
413 && (asub->outcode >= 0))
414 {
415 if (unlabeled)
416 {
417 for (j = ti->outcode; j >= 0; j = fnt->nextout[j])
418 voutln3("(LABEL %s)%s",
419 vchar(j, vcharbuf, forceoctal),
420 vname(j, vnamebuf,
421 fnt->outencptrs, forceoctal));
422 unlabeled = False;
423 }
424 if (asucc != NULL)
425 for (j = asucc->outcode; j >= 0; j = fnt->nextout[j])
426 {
427 voutln4("(%s %s O %o)", vplligops[nlig->op],
428 vchar(j, vcharbuf, forceoctal),
429 (unsigned)asub->outcode);
430 if (nlig->boundleft)
431 break;
432 }
433 else /* boundary char */
434 {
435 voutln4("(%s %s O %o)", vplligops[nlig->op],
436 vchar(fnt->boundarychar, vcharbuf, forceoctal),
437 (unsigned)asub->outcode);
438 }
439 }
440
441 for (nkern = (fnt->uppercase[i] ? fnt->uppercase[i]->kerns : ti->kerns);
442 nkern; nkern=nkern->next)
443 if (NULL != (asucc = findmappedadobe(nkern->succ, fnt->inencptrs)))
444 for (j = asucc->outcode; j >= 0; j = fnt->nextout[j])
445 {
446 if (fnt->uppercase[j] == NULL)
447 {
448 if (unlabeled)
449 {
450 for (k = ti->outcode; k >= 0; k = fnt->nextout[k])
451 voutln3("(LABEL %s)%s",
452 vchar(k, vcharbuf, forceoctal),
453 vname(k, vnamebuf, fnt->outencptrs, forceoctal));
454 unlabeled = False;
455 }
456
457 /*
458 * If other characters have the same kerns as this
459 * one, output the label here. This makes the TFM
460 * file much smaller than if we output all the
461 * kerns again under a different label.
462 */
463
464 for (kern_eq = ti->kern_equivs; kern_eq;
465 kern_eq = kern_eq->next)
466 {
467 k = kern_eq->ch->outcode;
468 if (k >= 0 && k <= 0xFF)
469 voutln3("(LABEL %s)%s",
470 vchar(k, vcharbuf, forceoctal),
471 vname(k, vnamebuf, fnt->outencptrs, forceoctal));
472 }
473 ti->kern_equivs = NULL; /* Only output those labels once. */
474
475 if (fnt->uppercase[i])
476 {
477 if (fnt->lowercase[j])
478 {
479 for (k = fnt->lowercase[j]->outcode; k >= 0;
480 k = fnt->nextout[k])
481 voutln4("(KRN %s R %.1f)%s",
482 vchar(k, vcharbuf, forceoctal),
483 fnt->capheight * nkern->delta,
484 vname(k, vnamebuf, fnt->outencptrs, forceoctal));
485 }
486 else
487 voutln4("(KRN %s R %.1f)%s",
488 vchar(j, vcharbuf, forceoctal),
489 fnt->capheight * nkern->delta,
490 vname(j, vnamebuf, fnt->outencptrs, forceoctal));
491 }
492 else
493 {
494 voutln4("(KRN %s R %d)%s",
495 vchar(j, vcharbuf, forceoctal),
496 nkern->delta,
497 vname(j, vnamebuf, fnt->outencptrs, forceoctal));
498 if (fnt->lowercase[j])
499 for (k = fnt->lowercase[j]->outcode; k >= 0;
500 k = fnt->nextout[k])
501 voutln4("(KRN %s R %.1f)%s",
502 vchar(k, vcharbuf, forceoctal),
503 fnt->capheight * nkern->delta,
504 vname(k, vnamebuf, fnt->outencptrs, forceoctal));
505 }
506 }
507 }
508 if (!unlabeled)
509 voutln("(STOP)");
510 }
511 vright(&level);
512
513 for (i = bc; i <= ec; i++)
514 if (NULL != (ti = fnt->outencptrs[i]))
515 {
516 vleft(&level);
517 fprintf(out, "CHARACTER %s%s\n ",
518 vchar(i, vcharbuf, forceoctal),
519 vname(i, vnamebuf, fnt->outencptrs, forceoctal));
520
521 if (fnt->uppercase[i])
522 {
523 ti = fnt->uppercase[i];
524 voutln2("(CHARWD R %.1f)", fnt->capheight * (ti->width));
525 if (0 != (ht = texheight(ti, fnt->charlist, fnt->xheight)))
526 voutln2("(CHARHT R %.1f)", fnt->capheight * ht);
527 if (ti->lly)
528 voutln2("(CHARDP R %.1f)", -fnt->capheight * ti->lly);
529 if (ti->urx > ti->width)
530 voutln2("(CHARIC R %.1f)", fnt->capheight * (ti->urx - ti->width));
531 }
532 else
533 {
534 voutln2("(CHARWD R %d)", ti->width);
535 if (0 != (ht = texheight(ti, fnt->charlist, fnt->xheight)))
536 voutln2("(CHARHT R %d)", ht);
537 if (ti->lly)
538 voutln2("(CHARDP R %d)", -ti->lly);
539 if (ti->urx > ti->width)
540 voutln2("(CHARIC R %d)", ti->urx - ti->width);
541 }
542
543 if (ti->incode != i || fnt->uppercase[i] || ti->constructed)
544 {
545 vleft(&level);
546 voutln("MAP");
547 if (fnt->uppercase[i])
548 voutln("(SELECTFONT D 1)");
549
550 if (ti->pccs && (ti->incode < 0 || ti->constructed))
551 {
552 xoff = 0;
553 yoff = 0;
554
555 for (npcc = ti->pccs; npcc; npcc = npcc->next)
556 if (NULL != (api = findmappedadobe(npcc->partname,
557 fnt->inencptrs)))
558 if (api->outcode >= 0)
559 {
560 if (npcc->xoffset != xoff)
561 {
562 if (fnt->uppercase[i])
563 {
564 voutln2("(MOVERIGHT R %.1f)",
565 fnt->capheight * (npcc->xoffset - xoff));
566 }
567 else
568 voutln2("(MOVERIGHT R %d)", npcc->xoffset - xoff);
569
570 xoff = npcc->xoffset;
571 }
572
573 if (npcc->yoffset != yoff)
574 {
575 if (fnt->uppercase[i])
576 {
577 voutln2("(MOVEUP R %.1f)",
578 fnt->capheight * (npcc->yoffset - yoff));
579 }
580 else
581 voutln2("(MOVEUP R %d)", npcc->yoffset - yoff);
582
583 yoff = npcc->yoffset;
584 }
585
586 voutln2("(SETCHAR O %o)", (unsigned)api->incode);
587 xoff += fnt->outencptrs[api->outcode]->width;
588 }
589 }
590 else
591 voutln2("(SETCHAR O %o)", (unsigned)ti->incode);
592 vright(&level);
593 }
594 vright(&level);
595 }
596
597 if (level)
598 oops("I forgot to match the parentheses.");
599 }
600
601
602 void
603 writeovp(Font *fnt)
604 {
605 register int i;
606 register ttfinfo *ti;
607 long bc, ec;
608 char header[256];
609 float Slant;
610 ttfinfo *ofm_array[65536];
611
612
613 out = fnt->vplout;
614
615 header[0] = '\0';
616 strncat(header, "Created by `", 12);
617 strncat(header, fnt->titlebuf, 255 - 12 - 1);
618 strncat(header, "'", 1);
619
620 voutln2("(VTITLE %s)", header);
621 voutln("(COMMENT Please change VTITLE if you edit this file)");
622
623 voutln("(OFMLEVEL H 1)");
624 voutln("(FONTDIR TL)");
625 voutln2("(FAMILY %s)", fnt->outname);
626 voutln("(FACE F MRR)");
627 voutln("(CODINGSCHEME UNSPECIFIED)");
628 voutln("(DESIGNSIZE R 10.0)");
629 voutln("(DESIGNUNITS R 1000)");
630 voutln("(COMMENT DESIGNSIZE (1 em) IS IN POINTS)");
631 voutln("(COMMENT OTHER DIMENSIONS ARE MULTIPLES OF DESIGNSIZE/1000)");
632
633 #if 0
634 /* Let vptovf compute the checksum. */
635 voutln2("(CHECKSUM O %lo)", cksum ^ 0xFFFFFFFF);
636 #endif
637
638 vleft(&level);
639 voutln("FONTDIMEN");
640
641 Slant = fnt->slant - fnt->efactor * tan(fnt->italicangle * (PI / 180.0));
642
643 if (Slant)
644 voutln2("(SLANT R %f)", Slant);
645 voutln2("(SPACE D %d)", fnt->fontspace);
646 if (!fnt->fixedpitch)
647 {
648 voutln2("(STRETCH D %d)", transform(200, 0, fnt->efactor, fnt->slant));
649 voutln2("(SHRINK D %d)", transform(100, 0, fnt->efactor, fnt->slant));
650 }
651 voutln2("(XHEIGHT D %d)", fnt->xheight);
652 voutln2("(QUAD D %d)", transform(1000, 0, fnt->efactor, fnt->slant));
653 voutln2("(EXTRASPACE D %d)",
654 fnt->fixedpitch ? fnt->fontspace :
655 transform(111, 0, fnt->efactor, fnt->slant));
656 vright(&level);
657
658 for (i = 0; i < fnt->subfont_num; i++)
659 {
660 vleft(&level);
661 voutln2("MAPFONT D %d", i);
662 voutln2("(FONTNAME %s)", (fnt->subfont_list[i]).name);
663 voutln2("(FONTCHECKSUM O %lo)", (fnt->subfont_list[i]).cksum);
664 vright(&level);
665 }
666
667 for (i = 0; i <= 0xFFFF; i++)
668 ofm_array[i] = NULL;
669
670 for (ti = fnt->charlist; ti != NULL; ti = ti->next)
671 if (ti->charcode >= 0 && ti->charcode <= 0xFFFF)
672 ofm_array[ti->charcode] = ti;
673
674 for (i = 0; i <= 0xFFFF && ofm_array[i] == NULL; i++)
675 ;
676 bc = i;
677 for (i = 0xFFFF; i >= 0 && ofm_array[i] == NULL; i--)
678 ;
679 ec = i;
680
681 if (ec < bc)
682 oops("No TTF characters.");
683
684 for (i = bc; i <= ec; i++)
685 {
686 if ((ti = ofm_array[i]) == NULL) continue;
687
688 vleft(&level);
689 fprintf(out, "CHARACTER H %lX\n ", ti->charcode);
690
691 voutln2("(CHARWD R %d)", ti->width);
692 if (ti->ury)
693 voutln2("(CHARHT R %d)", ti->ury);
694 if (ti->lly)
695 voutln2("(CHARDP R %d)", -ti->lly);
696 if (ti->urx > ti->width)
697 voutln2("(CHARIC R %d)", ti->urx - ti->width);
698
699 vleft(&level);
700 voutln("MAP");
701 if (ti->fntnum)
702 voutln2("(SELECTFONT D %d)", ti->fntnum);
703
704 voutln2("(SETCHAR H %X)", (unsigned)ti->incode);
705 vright(&level);
706
707 vright(&level);
708 }
709
710 if (level)
711 oops("I forgot to match the parentheses.");
712 }
713
714
715 /* end */