Debian Codegraph @

texk/ttf2pk2/tfmaux.c

raw
1 /*
2 * tfmaux.c
3 *
4 * This file is part of the ttf2pk package.
5 *
6 * Copyright 1997-1999, 2000, 2002 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 <stdlib.h>
17 #include <string.h>
18 #include <math.h>
19
20 #include "ttf2tfm.h"
21 #include "newobj.h"
22 #include "tfmaux.h"
23 #include "errormsg.h"
24
25
26 #undef PI
27 #define PI 3.14159265358979323846264338327
28
29
30 struct sf /* we need this for subfont ligatures */
31 {
32 long sf_code;
33 int position;
34 };
35
36
37 static long nextd; /* smallest value that will give a different mincover */
38
39
40 static int lf, lh, nw, nh, nd, ni, nl, nk, ne, np;
41 static int bc, ec;
42
43 static long *header, *charinfo,
44 *width, *height, *depth,
45 *ligkern, *kerns, *tparam,
46 *italic;
47
48
49 static int source[257]; /* utility variables for sorting tfm arrays */
50 static int unsort[257];
51
52
53 /*
54 * A simple function for sorting sf_array (in inverse order)
55 */
56
57 static int
58 compare_sf(const void *a, const void *b)
59 {
60 return (int)(((const struct sf *)b)->sf_code - ((const struct sf *)a)->sf_code);
61 }
62
63
64 /*
65 * The next routine simply scales something.
66 * Input is in TFM units per em. Output is in FIXFACTORths of units
67 * per em. We use 1 em = 1000 TFM units.
68 */
69
70 static long
71 scale(long what)
72 {
73 return ((what / 1000) * FIXFACTOR) +
74 (((what % 1000) * FIXFACTOR) + 500) / 1000;
75 }
76
77
78 /*
79 * Next we need a routine to reduce the number of distinct dimensions
80 * in a TFM file. Given an array what[0]..what[oldn-1], we want to
81 * group its elements into newn clusters, in such a way that the maximum
82 * difference between elements of a cluster is as small as possible.
83 * Furthermore, what[0]=0, and this value must remain in a cluster by
84 * itself. Data such as `0 4 6 7 9' with newn=3 shows that an iterative
85 * scheme in which 6 is first clustered with 7 will not work. So we
86 * borrow a neat algorithm from METAFONT to find the true optimum.
87 * Memory location what[oldn] is set to 0x7FFFFFFFL for convenience.
88 */
89
90
91 /*
92 * Tells how many clusters result, given max difference d.
93 */
94
95 static int
96 mincover(long *what,
97 register long d)
98 {
99 register int m;
100 register long l;
101 register long *p;
102
103
104 nextd = 0x7FFFFFFFL;
105 p = what+1;
106 m = 1;
107
108 while (*p < 0x7FFFFFFFL)
109 {
110 m++;
111 l = *p;
112 while (*++p <= l + d)
113 ;
114 if (*p - l < nextd)
115 nextd = *p - l;
116 }
117 return m;
118 }
119
120
121 static void
122 remap(long *what,
123 int oldn,
124 int newn,
125 int *source,
126 int *unsort)
127 {
128 register int i, j;
129 register long d, l;
130
131 what[oldn] = 0x7FFFFFFFL;
132 for (i = oldn-1; i > 0; i--)
133 {
134 d = what[i];
135 for (j = i; what[j+1] < d; j++)
136 {
137 what[j] = what[j+1];
138 source[j] = source[j+1];
139 }
140 what[j] = d;
141 source[j] = i;
142 }
143
144 i = mincover(what, 0L);
145 d = nextd;
146 while (mincover(what, d + d) > newn)
147 d += d;
148 while (mincover(what, d) > newn)
149 d = nextd;
150
151 i = 1;
152 j = 0;
153 while (i < oldn)
154 {
155 j++;
156 l = what[i];
157 unsort[source[i]] = j;
158 while (what[++i] <= l + d)
159 {
160 unsort[source[i]] = j;
161 if (i - j == oldn - newn)
162 d = 0;
163 }
164 what[j] = (l + what[i-1])/2;
165 }
166 }
167
168
169 static void
170 write16(int what, FILE *out)
171 {
172 (void)fputc(what >> 8, out);
173 (void)fputc(what & 0xFF, out);
174 }
175
176
177 static void
178 writearr(register long *p,
179 register int n,
180 register FILE *out)
181 {
182 while (n)
183 {
184 write16((short)(*p >> 16), out);
185 write16((short)(*p & 65535), out);
186 p++;
187 n--;
188 }
189 }
190
191
192 static void
193 writesarr(long *what,
194 int len,
195 FILE *out)
196 {
197 register long *p;
198 int i;
199
200
201 p = what;
202 i = len;
203 while (i)
204 {
205 *p = scale(*p);
206 (void)scale(*p); /* need this kludge for some compilers */
207 p++;
208 i--;
209 }
210 writearr(what, len, out);
211 }
212
213
214 static long *
215 makebcpl(register long *p,
216 register const char *s,
217 register int n)
218 {
219 register long t;
220 register long sc;
221
222
223 if (strlen(s) < n)
224 n = strlen(s);
225 t = ((long)n) << 24;
226 sc = 16;
227
228 while (n > 0)
229 {
230 t |= ((long)(*(unsigned const char *)s++)) << sc;
231 sc -= 8;
232 if (sc < 0)
233 {
234 *p++ = t;
235 t = 0;
236 sc = 24;
237 }
238 n--;
239 }
240 if (t)
241 *p++ = t;
242
243 return p;
244 }
245
246
247 static long
248 checksum(ttfinfo **array)
249 {
250 int i;
251 unsigned long s1 = 0, s2 = 0;
252 const char *p;
253 ttfinfo *ti;
254
255
256 for (i = 0; i < 256; i++)
257 if (NULL != (ti = array[i]))
258 {
259 s1 = ((s1 << 1) ^ (s1 >> 31)) ^ ti->width; /* cyclic left shift */
260 s1 &= 0xFFFFFFFF; /* in case we're on a 64-bit machine */
261
262 for (p = ti->adobename; *p; p++)
263 s2 = (s2 * 3) + *p;
264 }
265
266 s1 = (s1 << 1) ^ s2;
267 return s1;
268 }
269
270
271 int
272 transform(register int x, register int y,
273 float ef, float sl)
274 {
275 register double acc;
276
277
278 acc = ef * x + sl * y;
279 return (int)(acc >= 0 ? floor(acc + 0.5) : ceil(acc - 0.5));
280 }
281
282
283 int
284 buildtfm(Font *fnt)
285 {
286 register int i, j;
287 register ttfinfo *ti;
288 int byte1, old_byte1, byte2;
289 double Slant;
290 char buffer[256];
291 struct sf sf_array[256];
292
293
294 if (fnt->subfont_ligs)
295 {
296 if (fnt->ligname)
297 {
298 for (i = 0; i < 256; i++)
299 {
300 sf_array[i].sf_code = fnt->sf_code[i];
301 sf_array[i].position = (fnt->sf_code[i] == -1 ? -1 : i);
302 }
303 }
304 else
305 {
306 for (i = 0; i < 256; i++)
307 {
308 ti = fnt->inencptrs[i];
309 if (ti)
310 {
311 sf_array[i].sf_code = ti->charcode;
312 sf_array[i].position = i;
313 }
314 else
315 {
316 sf_array[i].sf_code = -1;
317 sf_array[i].position = -1;
318 }
319 }
320 }
321 /* we sort the subfont character codes before we build a ligkern list */
322 qsort(sf_array, 256, sizeof (struct sf), compare_sf);
323
324 /* we need to create dummy characters for the ligatures in case the
325 character slots of the affected codes are unused */
326 i = 0;
327 while (i < 256 && sf_array[i].sf_code > -1)
328 {
329 byte1 = sf_array[i].sf_code >> 8;
330 byte2 = sf_array[i].sf_code & 0xFF;
331 if (!fnt->inencptrs[byte1])
332 {
333 ti = newchar(fnt);
334 ti->llx = ti->lly = 0;
335 ti->urx = ti->ury = 0;
336 ti->width = 0;
337 fnt->inencptrs[byte1] = ti;
338 ti->incode = byte1;
339 ti->adobename = ".dummy";
340 }
341 if (!fnt->inencptrs[byte2])
342 {
343 ti = newchar(fnt);
344 ti->llx = ti->lly = 0;
345 ti->urx = ti->ury = 0;
346 ti->width = 0;
347 fnt->inencptrs[byte2] = ti;
348 ti->incode = byte2;
349 ti->adobename = ".dummy";
350 }
351 i++;
352 }
353 }
354
355 for (i = 0; i <= 0xFF && fnt->inencptrs[i] == NULL; i++)
356 ;
357 bc = i;
358 for (i = 0xFF; i >= 0 && fnt->inencptrs[i] == NULL; i--)
359 ;
360 ec = i;
361
362 if (ec < bc)
363 {
364 if (fnt->sfdname)
365 return 0;
366 else
367 oops("No TTF characters.");
368 }
369
370 header = (long *)mycalloc(40000L, sizeof(char));
371 fnt->cksum = checksum(fnt->inencptrs);
372 header[0] = fnt->cksum;
373 header[1] = 0xA00000; /* 10pt design size */
374
375 (void)makebcpl(header + 2, fnt->codingscheme, 39);
376 (void)makebcpl(header + 12, fnt->fullname, 19);
377
378 /* 4 bytes are left free for the unused FACE value */
379
380 buffer[0] = '\0';
381 strncat(buffer, "Created by `", 12);
382 strncat(buffer, fnt->titlebuf, 255 - 12 - 1);
383 strncat(buffer, "'", 1);
384 charinfo = makebcpl(header + 18, buffer, 255);
385 lh = charinfo - header;
386
387 width = charinfo + (ec - bc + 1);
388 width[0] = 0;
389 nw = 1;
390
391 for (i = bc; i <= ec; i++)
392 if (NULL != (ti = fnt->inencptrs[i]))
393 {
394 width[nw] = ti->width;
395 for (j = 1; width[j] != ti->width; j++)
396 ;
397 ti->wptr = j;
398 if (j == nw)
399 nw++;
400 }
401 if (nw > 256)
402 oops("256 chars with different widths.");
403
404 depth = width + nw;
405 depth[0] = 0;
406 nd = 1;
407
408 for (i = bc; i <= ec; i++)
409 if (NULL != (ti = fnt->inencptrs[i]))
410 {
411 depth[nd] = -ti->lly;
412 for (j = 0; depth[j] != -ti->lly; j++)
413 ;
414 ti->dptr = j;
415 if (j == nd)
416 nd++;
417 }
418
419 if (nd > 16)
420 {
421 remap(depth, nd, 16, source, unsort);
422 nd = 16;
423 for (i = bc; i <= ec; i++)
424 if (NULL != (ti = fnt->inencptrs[i]))
425 ti->dptr = unsort[ti->dptr];
426 }
427
428 height = depth + nd;
429 height[0] = 0;
430 nh = 1;
431
432 for (i = bc; i <= ec; i++)
433 if (NULL != (ti = fnt->inencptrs[i]))
434 {
435 height[nh] = ti->ury;
436 for (j = 0; height[j] != ti->ury; j++)
437 ;
438 ti->hptr = j;
439 if (j == nh)
440 nh++;
441 }
442
443 if (nh > 16)
444 {
445 remap(height, nh, 16, source, unsort);
446 nh = 16;
447 for (i = bc; i <= ec; i++)
448 if (NULL != (ti = fnt->inencptrs[i]))
449 ti->hptr = unsort[ti->hptr];
450 }
451
452 italic = height + nh;
453 italic[0] = 0;
454 ni = 1;
455
456 for (i = bc; i <= ec; i++)
457 if (NULL != (ti = fnt->inencptrs[i]))
458 {
459 italic[ni] = ti->urx - ti->width;
460 if (italic[ni] < 0)
461 italic[ni] = 0;
462 for (j = 0; italic[j] != italic[ni]; j++)
463 ;
464 ti->iptr = j;
465 if (j == ni)
466 ni++;
467 }
468
469 if (ni > 64)
470 {
471 remap(italic, ni, 64, source, unsort);
472 ni = 64;
473 for (i = bc; i <= ec; i++)
474 if (NULL != (ti = fnt->inencptrs[i]))
475 ti->iptr = unsort[ti->iptr];
476 }
477
478 for (i = bc; i <= ec; i++)
479 if (NULL != (ti = fnt->inencptrs[i]))
480 charinfo[i - bc] = ((long)(ti->wptr) << 24) +
481 ((long)(ti->hptr) << 20) +
482 ((long)(ti->dptr) << 16) +
483 ((long)(ti->iptr) << 10);
484 else
485 charinfo[i - bc] = 0;
486
487 ligkern = italic + ni;
488 nl = 0;
489
490 if (fnt->subfont_ligs)
491 {
492 /* Now we build the ligature list. The ligature consisting of character
493 code byte1 + byte2 should yield the actual character. The fonts of
494 the HLaTeX package for Korean use this mechanism. */
495
496 old_byte1 = -1;
497 while (nl < 256 && sf_array[nl].sf_code > -1)
498 {
499 byte1 = sf_array[nl].sf_code >> 8;
500 byte2 = sf_array[nl].sf_code & 0xFF;
501 if (byte1 != old_byte1)
502 {
503 charinfo[byte1 - bc] += 0x100L + /* set the lig tag */
504 nl; /* set the position in array */
505 if (old_byte1 > -1)
506 ligkern[nl - 1] |= 0x80000000L; /* set the STOP byte in previous
507 ligkern command */
508 }
509
510 ligkern[nl] = ((long)byte2 << 16) +
511 (long)sf_array[nl].position;
512 old_byte1 = byte1;
513 nl++;
514 }
515 if (nl > 0)
516 ligkern[nl - 1] |= 0x80000000L;
517 }
518
519 kerns = ligkern + nl;
520 nk = 0; /* kerns are omitted from raw TeX font */
521
522 Slant = fnt->slant - fnt->efactor * tan(fnt->italicangle * (PI / 180.0));
523
524 tparam = kerns + nk;
525 tparam[0] = (long)(FIXFACTOR * Slant + 0.5);
526 tparam[1] = scale((long)fnt->fontspace);
527 tparam[2] = (fnt->fixedpitch ? 0 : scale((long)(300 * fnt->efactor + 0.5)));
528 tparam[3] = (fnt->fixedpitch ? 0 : scale((long)(100 * fnt->efactor + 0.5)));
529 tparam[4] = scale((long)fnt->xheight);
530 tparam[5] = scale((long)(1000 * fnt->efactor + 0.5));
531 np = 6;
532
533 return 1;
534 }
535
536
537 void
538 writetfm(Font *fnt)
539 {
540 FILE *out;
541 char *tfm_name;
542 int len = 0;
543
544
545 if (fnt->tfm_path)
546 len += strlen(fnt->tfm_path);
547 len += strlen(fnt->fullname);
548 len += strlen(fnt->tfm_ext);
549 len++;
550
551 tfm_name = (char *)mymalloc(len);
552 tfm_name[0] = '\0';
553 if (fnt->tfm_path)
554 strcat(tfm_name, fnt->tfm_path);
555 strcat(tfm_name, fnt->fullname);
556 strcat(tfm_name, fnt->tfm_ext);
557
558 if ((out = fopen(tfm_name, "wb")) == NULL)
559 oops("Cannot open tfm file `%s'.", tfm_name);
560
561 free(tfm_name);
562
563
564 lf = 6 + lh + (ec - bc + 1) + nw + nh + nd + ni + nl + nk + ne + np;
565
566 write16(lf, out);
567 write16(lh, out);
568 write16(bc, out);
569 write16(ec, out);
570 write16(nw, out);
571 write16(nh, out);
572 write16(nd, out);
573 write16(ni, out);
574 write16(nl, out);
575 write16(nk, out);
576 write16(ne, out);
577 write16(np, out);
578 writearr(header, lh, out);
579 writearr(charinfo, ec - bc + 1, out);
580 writesarr(width, nw, out);
581 writesarr(height, nh, out);
582 writesarr(depth, nd, out);
583 writesarr(italic, ni, out);
584 writearr(ligkern, nl, out);
585 writesarr(kerns, nk, out);
586 writearr(tparam, np, out);
587
588 free(header);
589 fclose(out);
590 }
591
592
593 void
594 writeenc(Font *fnt)
595 {
596 FILE *out;
597 char *enc_name;
598 int i, len = 0;
599
600
601 if (fnt->tfm_path)
602 len += strlen(fnt->tfm_path);
603 len += strlen(fnt->fullname);
604 len += 5; /* ".enc" + 1 */
605
606 enc_name = (char *)mymalloc(len);
607 enc_name[0] = '\0';
608 if (fnt->tfm_path)
609 strcat(enc_name, fnt->tfm_path);
610 strcat(enc_name, fnt->fullname);
611 strcat(enc_name, ".enc");
612
613 if ((out = fopen(enc_name, "wb")) == NULL)
614 oops("Cannot open enc file `%s'.", enc_name);
615
616 free(enc_name);
617
618 fprintf(out, "/%sEncoding [", fnt->fullname);
619 for (i = 0; i < 256; i++)
620 {
621 if (i % 4 == 0)
622 fputc('\n', out);
623 if (fnt->inencptrs[i] == NULL)
624 fputs("/.notdef ", out);
625 else if (strcmp(fnt->inencptrs[i]->adobename, ".notdef") == 0 ||
626 strcmp(fnt->inencptrs[i]->adobename, ".null") == 0 ||
627 strcmp(fnt->inencptrs[i]->adobename, ".nonmarkingreturn") == 0 ||
628 strcmp(fnt->inencptrs[i]->adobename, ".dummy") == 0)
629 fputs("/.notdef ", out);
630 else
631 fprintf(out, "/index0x%04X ", fnt->inencptrs[i]->glyphindex);
632 }
633 fputs("\n] def\n", out);
634
635 fclose(out);
636 }
637
638
639 /* end */