Debian Codegraph @

texk/web2c/pdftexdir/ttf2afm.c

raw
1 /*
2 Copyright 1996-2021 Han The Thanh, <[email protected]>
3
4 This file is part of pdfTeX.
5
6 pdfTeX is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 pdfTeX is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 #include <kpathsea/c-auto.h>
22 #include <kpathsea/c-fopen.h>
23 #include <kpathsea/c-limits.h>
24 #include <kpathsea/c-memstr.h>
25 #include <kpathsea/c-std.h>
26 #include <kpathsea/c-unistd.h>
27 #include <stdarg.h>
28 #include <kpathsea/getopt.h>
29 */
30 #include <kpathsea/kpathsea.h>
31 #include <time.h>
32 #include <pdftexdir/ptexmac.h>
33 #include <pdftexdir/writettf.h>
34 #include <string.h>
35
36 /* constants used for print_glyph */
37 #define AS_NAME 0
38 #define AS_INDEX 1
39 #define AS_UNICODE 2
40
41 #define VERSION "1.02"
42
43 /* FIXME */
44 #define NOGLYPH_ASSIGNED_YET USHRT_MAX
45
46 #define enc_getchar() xgetc(encfile)
47 #define enc_eof() feof(encfile)
48 #define pdftex_fail ttf_fail
49
50 #define print_str(S) if (S != NULL) fprintf(outfile, #S " %s\n", escape_eol(S))
51 #define print_dimen(N) if (N != 0) fprintf(outfile, #N " %i\n", (int)get_ttf_funit(N))
52
53 #define get_ttf_funit(n) \
54 (n < 0 ? -((-n/upem)*1000 + ((-n%upem)*1000)/upem) :\
55 ((n/upem)*1000 + ((n%upem)*1000)/upem))
56
57 typedef struct _unicode_entry {
58 TTF_USHORT code;
59 struct _unicode_entry *next;
60 } unicode_entry;
61
62 typedef struct {
63 TTF_ULONG wx;
64 const char *name;
65 TTF_USHORT index;
66 TTF_LONG bbox[4];
67 TTF_LONG offset;
68 char found;
69 unicode_entry *unicode_list;
70 } mtx_entry;
71
72 typedef struct _kern_entry {
73 TTF_FWORD value;
74 TTF_USHORT adjacent;
75 struct _kern_entry *next;
76 } kern_entry;
77
78
79 char *FontName = NULL;
80 char *FullName = NULL;
81 char *FamilyName = NULL;
82 char *Notice = NULL;
83 char *Version = NULL;
84 char *Weight = NULL;
85 TTF_LONG ItalicAngle = 0;
86 TTF_LONG IsFixedPitch = 0;
87 TTF_LONG FontBBox1 = 0;
88 TTF_LONG FontBBox2 = 0;
89 TTF_LONG FontBBox3 = 0;
90 TTF_LONG FontBBox4 = 0;
91 TTF_LONG UnderlinePosition = 0;
92 TTF_LONG UnderlineThickness = 0;
93 TTF_LONG CapHeight = 0;
94 TTF_LONG XHeight = 0;
95 TTF_LONG Ascender = 0;
96 TTF_LONG Descender = 0;
97
98 char *cur_file_name = NULL;
99 char *bname = NULL;
100 FILE *fontfile, *encfile, *outfile = NULL;
101 char enc_line[ENC_BUF_SIZE];
102 int print_glyph = AS_NAME; /* print glyph as names by default */
103 int print_cmap = 0;
104 int use_ext_enc = 0; /* use external encoding? */
105 int select_unicode = 1; /* use the first unicode mapping by default */
106 int printing_enc = 0; /* set to 1 while printing encodings */
107
108
109 TTF_USHORT upem;
110 TTF_USHORT ntabs;
111 int nhmtx;
112 int post_format;
113 int loca_format;
114 long nglyphs;
115 int nkernpairs = 0;
116 int names_count = 0;
117 char *ps_glyphs_buf = NULL;
118 dirtab_entry *dir_tab;
119 mtx_entry *mtx_tab;
120 kern_entry *kern_tab;
121 char *enc_names[256];
122
123 cmap_entry *cmap_tab;
124 TTF_USHORT ncmapsubtabs;
125 long cmap_offset;
126
127 TTF_USHORT unicode_map[0xFFFF];
128
129 #include "macnames.c"
130
131 #if defined __GNUC__ && __GNUC__ >=3
132 __attribute__((__noreturn__))
133 #endif
134 static void ttf_fail(const char *fmt, ...)
135 {
136 va_list args;
137 va_start(args, fmt);
138 fprintf(stderr, "\nError: ttf2afm");
139 if (cur_file_name)
140 fprintf(stderr, " (file %s)", cur_file_name);
141 fprintf(stderr, ": ");
142 vfprintf(stderr, fmt, args);
143 fprintf(stderr, "\n");
144 va_end(args);
145 exit(-1);
146 }
147
148 static void ttf_warn(const char *fmt, ...)
149 {
150 va_list args;
151 va_start(args, fmt);
152 fprintf(stderr, "\nWarning: ttf2afm");
153 if (cur_file_name)
154 fprintf(stderr, " (file %s)", cur_file_name);
155 fprintf(stderr, ": ");
156 vfprintf(stderr, fmt, args);
157 fprintf(stderr, "\n");
158 va_end(args);
159 }
160
161 static int xgetc(FILE * stream)
162 {
163 int c = getc(stream);
164 if (c < 0 && c != EOF)
165 ttf_fail("getc() failed");
166 return c;
167 }
168
169 static long ttf_getnum(int s)
170 {
171 long i = 0;
172 int c;
173 while (s > 0) {
174 if ((c = xgetc(fontfile)) < 0)
175 ttf_fail("unexpected EOF");
176 i = (i << 8) + c;
177 s--;
178 }
179 return i;
180 }
181
182 static dirtab_entry *name_lookup(const char *s)
183 {
184 dirtab_entry *p;
185 for (p = dir_tab; p - dir_tab < ntabs; p++)
186 if (strncmp(p->tag, s, 4) == 0)
187 break;
188 if (p - dir_tab == ntabs)
189 p = NULL;
190 return p;
191 }
192
193 static void ttf_seek_tab(const char *name, TTF_LONG offset)
194 {
195 dirtab_entry *p = name_lookup(name);
196 if (p == NULL)
197 ttf_fail("can't find table `%s'", name);
198 if (fseek(fontfile, p->offset + offset, SEEK_SET) < 0)
199 ttf_fail("fseek() failed while reading `%s' table", name);
200 }
201
202 static void ttf_seek_off(const char *name, TTF_LONG offset)
203 {
204 if (fseek(fontfile, offset, SEEK_SET) < 0)
205 ttf_fail("fseek() failed while reading `%s' table", name);
206 }
207
208 static void store_kern_value(TTF_USHORT i, TTF_USHORT j, TTF_FWORD v)
209 {
210 kern_entry *pk;
211 for (pk = kern_tab + i; pk->next != NULL; pk = pk->next);
212 pk->next = xtalloc(1, kern_entry);
213 pk = pk->next;
214 pk->next = NULL;
215 pk->adjacent = j;
216 pk->value = v;
217 }
218
219 #if 0 /* unused */
220 static TTF_FWORD get_kern_value(TTF_USHORT i, TTF_USHORT j)
221 {
222 kern_entry *pk;
223 for (pk = kern_tab + i; pk->next != NULL; pk = pk->next)
224 if (pk->adjacent == j)
225 return pk->value;
226 return 0;
227 }
228 #endif
229
230 static void free_tabs(void)
231 {
232 int i;
233 kern_entry *p, *q, *r;
234 unicode_entry *u, *v;
235 mtx_entry *pm;
236 xfree(ps_glyphs_buf);
237 xfree(dir_tab);
238 xfree(cmap_tab);
239 for (pm = mtx_tab; pm - mtx_tab < nglyphs; pm++)
240 if (pm->unicode_list != NULL) {
241 for (u = pm->unicode_list; u != NULL; u = v) {
242 v = u->next;
243 xfree(u);
244 }
245 }
246 xfree(mtx_tab);
247 for (i = 0; i < 256; i++)
248 if (enc_names[i] != notdef)
249 free(enc_names[i]);
250 if (kern_tab == NULL)
251 return;
252 for (p = kern_tab; p - kern_tab < nglyphs; p++)
253 if (p->next != NULL) {
254 for (q = p->next; q != NULL; q = r) {
255 r = q->next;
256 xfree(q);
257 }
258 }
259 xfree(kern_tab);
260 }
261
262 static void enc_getline(void)
263 {
264 char *p;
265 int c;
266 restart:
267 if (enc_eof())
268 ttf_fail("unexpected end of file");
269 p = enc_line;
270 do {
271 c = enc_getchar();
272 append_char_to_buf(c, p, enc_line, ENC_BUF_SIZE);
273 } while (c != 10);
274 append_eol(p, enc_line, ENC_BUF_SIZE);
275 if (p - enc_line <= 2 || *enc_line == '%')
276 goto restart;
277 }
278
279 static void read_encoding(char *encname)
280 {
281 char buf[ENC_BUF_SIZE], *q, *r;
282 int i;
283 cur_file_name = encname;
284 if ((encfile = kpse_open_file(encname, kpse_enc_format)) == NULL)
285 ttf_fail("can't open encoding file for reading");
286 enc_getline();
287 if (*enc_line != '/' || (r = strchr(enc_line, '[')) == 0)
288 ttf_fail("invalid encoding vector: name or `[' missing:\n%s", enc_line);
289 for (i = 0; i < 256; i++)
290 enc_names[i] = notdef;
291 if (r[1] == 32)
292 r += 2;
293 else
294 r++;
295 for (;;) {
296 while (*r == '/') {
297 for (q = buf, r++; *r != ' ' && *r != 10 && *r != ']' && *r != '/';
298 *q++ = *r++);
299 *q = 0;
300 if (*r == ' ')
301 r++;
302 if (strcmp(buf, notdef) != 0)
303 enc_names[names_count] = xstrdup(buf);
304 if (++names_count > 256)
305 ttf_fail("encoding vector contains more than 256 names");
306 }
307 if (*r != 10 && *r != '%') {
308 if (str_prefix(r, "] def"))
309 goto done;
310 else
311 ttf_fail
312 ("invalid encoding vector: a name or `] def' expected:\n%s",
313 enc_line);
314 }
315 enc_getline();
316 r = enc_line;
317 }
318 done:
319 xfclose(encfile, cur_file_name);
320 if (names_count != 256)
321 ttf_warn("encoding vector contains only %i names (expected %i)",
322 names_count, 256);
323 }
324
325 static void append_unicode(long glyph_index, TTF_USHORT code)
326 {
327 mtx_entry *m;
328 unicode_entry *u, *v;
329 assert(glyph_index >= 0 && glyph_index < nglyphs);
330 u = xtalloc(1, unicode_entry);
331 m = mtx_tab + glyph_index;
332 u->next = NULL;
333 u->code = code;
334 if (m->unicode_list == NULL)
335 m->unicode_list = u;
336 else {
337 for (v = m->unicode_list; v->next != NULL; v = v->next);
338 v->next = u;
339 }
340 }
341
342 static void read_cmap(void)
343 {
344 cmap_entry *e;
345 seg_entry *seg_tab, *s;
346 TTF_USHORT *glyphId, format, segCount;
347 long int n, i, k, length, index;
348 int unicode_map_count = 0;
349 ttf_seek_tab("cmap", TTF_USHORT_SIZE); /* skip the table version number (=0) */
350 ncmapsubtabs = get_ushort();
351 cmap_offset = xftell(fontfile, cur_file_name) - 2 * TTF_USHORT_SIZE;
352 cmap_tab = xtalloc(ncmapsubtabs, cmap_entry);
353 for (e = cmap_tab; e - cmap_tab < ncmapsubtabs; e++) {
354 e->platform_id = get_ushort();
355 e->encoding_id = get_ushort();
356 e->offset = get_ulong();
357 }
358 for (i = 0; i < 0xFFFF; ++i)
359 unicode_map[i] = NOGLYPH_ASSIGNED_YET;
360 for (e = cmap_tab; e - cmap_tab < ncmapsubtabs; e++) {
361 ttf_seek_off("cmap", cmap_offset + e->offset);
362 format = get_ushort();
363 if (is_unicode_mapping(e) && format == 4) {
364 ++unicode_map_count;
365 if (unicode_map_count == select_unicode)
366 goto read_unicode_mapping;
367 }
368 continue;
369 read_unicode_mapping:
370 length = get_ushort(); /* length of subtable */
371 (void) get_ushort(); /* skip the version number */
372 segCount = get_ushort() / 2;
373 (void) get_ushort(); /* skip searchRange */
374 (void) get_ushort(); /* skip entrySelector */
375 (void) get_ushort(); /* skip rangeShift */
376 seg_tab = xtalloc(segCount, seg_entry);
377 for (s = seg_tab; s - seg_tab < segCount; s++)
378 s->endCode = get_ushort();
379 (void) get_ushort(); /* skip reversedPad */
380 for (s = seg_tab; s - seg_tab < segCount; s++)
381 s->startCode = get_ushort();
382 for (s = seg_tab; s - seg_tab < segCount; s++)
383 s->idDelta = get_ushort();
384 for (s = seg_tab; s - seg_tab < segCount; s++)
385 s->idRangeOffset = get_ushort();
386 length -= 8 * TTF_USHORT_SIZE + 4 * segCount * TTF_USHORT_SIZE;
387 n = length / TTF_USHORT_SIZE; /* number of glyphID's */
388 glyphId = xtalloc(n, TTF_USHORT);
389 for (i = 0; i < n; i++)
390 glyphId[i] = get_ushort();
391 for (s = seg_tab; s - seg_tab < segCount; s++) {
392 for (i = s->startCode; i <= s->endCode; i++) {
393 if (i == 0xFFFF)
394 break;
395 if (s->idRangeOffset != 0xFFFF) {
396 if (s->idRangeOffset == 0)
397 index = (s->idDelta + i) & 0xFFFF;
398 else {
399 k = (i - s->startCode) + s->idRangeOffset / 2 +
400 (s - seg_tab) - segCount;
401 assert(k >= 0 && k < n);
402 index = glyphId[k];
403 if (index != 0)
404 index = (index + s->idDelta) & 0xFFFF;
405 }
406 if (index < 0 || index >= nglyphs)
407 ttf_fail("cmap: glyph index out of range [0..%i)",
408 nglyphs);
409 if (unicode_map[i] != NOGLYPH_ASSIGNED_YET)
410 ttf_fail
411 ("cmap: unicode %.4X is mapped to multiple glyphs",
412 i);
413 if (unicode_map[i] == 0)
414 ttf_warn("unicode %.4X is mapped to glyph 0", i);
415 unicode_map[i] = index;
416 append_unicode(index, i);
417 }
418 }
419 }
420 xfree(seg_tab);
421 xfree(glyphId);
422 break;
423 }
424 if (e - cmap_tab == ncmapsubtabs)
425 ttf_fail("Invalid argument `-m %i': out of range [1..%i]",
426 select_unicode, unicode_map_count);
427 }
428
429 static char *make_name(long platform_id, int len)
430 {
431 unsigned char buf[1024];
432 unsigned char *p = buf;
433 int i = 0;
434
435 if (len >= sizeof(buf))
436 len = sizeof(buf) - 1;
437 while (i < len) {
438 *p = (unsigned char) get_char();
439 i++;
440 if (*p == 0 && platform_id == 3) {
441 /* assume this is an UTF-16BE encoded string but contains latin
442 * chars, which is the most common case; simply copy the 2nd byte.
443 * Note: will not work for non-latin text */
444 *p = (unsigned char) get_char();
445 i++;
446 }
447 /* sometime a UTF-16BE string will contain chars where the 1st
448 or 2nd byte is in range (0..32) */
449 if (*p < 32
450 && *p != '\r'
451 && *p != '\n'
452 && *p != '\t'
453 ) {
454 ttf_warn("skipping unsafe character: %i", *p);
455 } else {
456 p++;
457 }
458 }
459 *p = 0;
460 return xstrdup(buf);
461 }
462
463 static void read_font(void)
464 {
465 long i, j, k, l, n, m, platform_id, encoding_id, name_id;
466 TTF_FWORD kern_value;
467 char *p;
468 dirtab_entry *pd;
469 kern_entry *pk;
470 mtx_entry *pm;
471 ttf_skip(TTF_FIXED_SIZE);
472 ntabs = get_ushort();
473 ttf_skip(3 * TTF_USHORT_SIZE);
474 dir_tab = xtalloc(ntabs, dirtab_entry);
475 for (pd = dir_tab; pd - dir_tab < ntabs; pd++) {
476 pd->tag[0] = get_char();
477 pd->tag[1] = get_char();
478 pd->tag[2] = get_char();
479 pd->tag[3] = get_char();
480 ttf_skip(TTF_ULONG_SIZE);
481 pd->offset = get_ulong();
482 pd->length = get_ulong();
483 }
484 ttf_seek_tab("head",
485 2 * TTF_FIXED_SIZE + 2 * TTF_ULONG_SIZE + TTF_USHORT_SIZE);
486 upem = get_ushort();
487 ttf_skip(16);
488 FontBBox1 = get_fword();
489 FontBBox2 = get_fword();
490 FontBBox3 = get_fword();
491 FontBBox4 = get_fword();
492 ttf_skip(TTF_USHORT_SIZE);
493 ttf_skip(TTF_USHORT_SIZE + TTF_SHORT_SIZE);
494 loca_format = get_short();
495 ttf_seek_tab("maxp", TTF_FIXED_SIZE);
496 nglyphs = get_ushort();
497 mtx_tab = xtalloc(nglyphs + 1, mtx_entry);
498 for (pm = mtx_tab; pm - mtx_tab < nglyphs + 1; pm++) {
499 pm->name = NULL; /* notdef */
500 pm->found = 0;
501 pm->unicode_list = NULL;
502 }
503 ttf_seek_tab("hhea", TTF_FIXED_SIZE);
504 Ascender = get_fword();
505 Descender = get_fword();
506 ttf_skip(TTF_FWORD_SIZE + TTF_UFWORD_SIZE + 3 * TTF_FWORD_SIZE +
507 8 * TTF_SHORT_SIZE);
508 nhmtx = get_ushort();
509 ttf_seek_tab("hmtx", 0);
510 for (pm = mtx_tab; pm - mtx_tab < nhmtx; pm++) {
511 pm->wx = get_ufword();
512 ttf_skip(TTF_FWORD_SIZE);
513 }
514 i = pm[-1].wx;
515 for (; pm - mtx_tab < nglyphs; pm++)
516 pm->wx = i;
517 ttf_seek_tab("post", 0);
518 post_format = get_fixed();
519 ItalicAngle = get_fixed();
520 UnderlinePosition = get_fword();
521 UnderlineThickness = get_fword();
522 IsFixedPitch = get_ulong();
523 ttf_skip(4 * TTF_ULONG_SIZE);
524 switch (post_format) {
525 case 0x00010000:
526 for (pm = mtx_tab; pm - mtx_tab < NMACGLYPHS; pm++)
527 pm->name = mac_glyph_names[pm - mtx_tab];
528 break;
529 case 0x00020000:
530 l = get_ushort(); /* some fonts have this value different from nglyphs */
531 for (pm = mtx_tab; pm - mtx_tab < l; pm++)
532 pm->index = get_ushort();
533 if ((pd = name_lookup("post")) == NULL)
534 ttf_fail("can't find table `post'");
535 n = pd->length - (xftell(fontfile, cur_file_name) - pd->offset);
536 ps_glyphs_buf = xtalloc(n + 1, char);
537 for (m = 0, p = ps_glyphs_buf; p - ps_glyphs_buf < n;) {
538 for (i = get_byte(); i > 0; i--)
539 *p++ = get_char();
540 *p++ = 0;
541 m++;
542 }
543 for (pm = mtx_tab; pm - mtx_tab < l; pm++) {
544 if (pm->index < NMACGLYPHS)
545 pm->name = mac_glyph_names[pm->index];
546 else {
547 k = pm->index - NMACGLYPHS;
548 if (k < m) {
549 for (p = ps_glyphs_buf; k > 0; k--)
550 p = (char *) strend(p) + 1;
551 pm->name = p;
552 } else {
553 pm->name = NULL; /* index out of valid range, fix name to notdef */
554 }
555 }
556 }
557 break;
558 default:
559 ttf_warn("unsupported format (%.8X) of `post' table, assuming 3.0",
560 post_format);
561 case 0x00030000:
562 if (print_glyph == AS_NAME) {
563 ttf_warn
564 ("no names available in `post' table, print glyph names as indices");
565 print_glyph = AS_INDEX;
566 }
567 }
568 ttf_seek_tab("loca", 0);
569 if (loca_format == 1) {
570 for (pm = mtx_tab; pm - mtx_tab < nglyphs + 1; pm++)
571 pm->offset = get_ulong();
572 } else {
573 for (pm = mtx_tab; pm - mtx_tab < nglyphs + 1; pm++)
574 pm->offset = get_ushort() << 1;
575 }
576 if ((pd = name_lookup("glyf")) == NULL)
577 ttf_fail("can't find table `glyf'");
578 for (n = pd->offset, pm = mtx_tab; pm - mtx_tab < nglyphs; pm++) {
579 ttf_seek_off("glyf", n + pm->offset);
580 ttf_skip(TTF_SHORT_SIZE);
581 pm->bbox[0] = get_fword();
582 pm->bbox[1] = get_fword();
583 pm->bbox[2] = get_fword();
584 pm->bbox[3] = get_fword();
585 }
586
587 ttf_seek_tab("name", 0);
588 i = ftell(fontfile);
589 (void) get_ushort(); /* skip Format selector (=0) */
590 n = get_ushort(); /* number of name records */
591 j = get_ushort() + i; /* start of string storage */
592 i += 3 * TTF_USHORT_SIZE; /* update the current offset */
593 while (n-- > 0) {
594 ttf_seek_off("name", i);
595 platform_id = get_ushort();
596 encoding_id = get_ushort();
597 (void) get_ushort(); /* skip language_id */
598 name_id = get_ushort(); /* name_id */
599 l = get_ushort(); /* string length */
600 if (((platform_id == 1 && encoding_id == 0) || /* legacy */
601 (platform_id == 3 || encoding_id == 1) /* most common for modern fonts */
602 ) &&
603 (name_id == 0 || name_id == 1 || name_id == 4 || name_id == 5 || name_id == 6)
604 )
605 {
606 ttf_seek_off("name", j + get_ushort());
607 p = make_name(platform_id, l);
608 switch (name_id) {
609 case 0:
610 Notice = p;
611 break;
612 case 1:
613 FamilyName = p;
614 break;
615 case 4:
616 FullName = p;
617 break;
618 case 5:
619 Version = p;
620 break;
621 case 6:
622 FontName = p;
623 break;
624 }
625 if (Notice != NULL && FamilyName != NULL &&
626 FullName != NULL && FontName != NULL && Version != NULL)
627 break;
628 }
629 i += 6 * TTF_USHORT_SIZE;
630 }
631 if ((pd = name_lookup("PCLT")) != NULL) {
632 ttf_seek_off("PCLT",
633 pd->offset + TTF_FIXED_SIZE + TTF_ULONG_SIZE +
634 TTF_USHORT_SIZE);
635 XHeight = get_ushort();
636 if (XHeight > FontBBox4) {
637 ttf_warn("XHeight is too big (%i)\n"
638 "This is likely a font bug, so I discarded this parameter.",
639 (int) get_ttf_funit(XHeight));
640 XHeight = 0;
641 }
642 ttf_skip(2 * TTF_USHORT_SIZE);
643 CapHeight = get_ushort();
644 if (CapHeight > FontBBox4) {
645 ttf_warn("CapHeight is too big (%i)\n"
646 "This is likely a font bug, so I discarded this parameter.",
647 (int) get_ttf_funit(CapHeight));
648 CapHeight = 0;
649 }
650 }
651 if ((pd = name_lookup("OS/2")) != NULL) {
652 ttf_seek_off("OS/2", pd->offset + TTF_USHORT_SIZE + TTF_SHORT_SIZE);
653 switch (get_ushort()) {
654 case 100:
655 Weight = xstrdup("Thin");
656 break;
657 case 200:
658 Weight = xstrdup("ExtraLight");
659 break;
660 case 300:
661 Weight = xstrdup("Light");
662 break;
663 case 400:
664 Weight = xstrdup("Normal");
665 break;
666 case 500:
667 Weight = xstrdup("Medium");
668 break;
669 case 600:
670 Weight = xstrdup("SemiBold");
671 break;
672 case 700:
673 Weight = xstrdup("Bold");
674 break;
675 case 800:
676 Weight = xstrdup("ExtraBold");
677 break;
678 case 900:
679 Weight = xstrdup("Black");
680 break;
681 }
682 }
683 read_cmap();
684 if ((pd = name_lookup("kern")) == NULL)
685 return;
686 kern_tab = xtalloc(nglyphs, kern_entry);
687 for (pk = kern_tab; pk - kern_tab < nglyphs; pk++) {
688 pk->next = NULL;
689 pk->value = 0;
690 }
691 ttf_seek_off("kern", pd->offset + TTF_USHORT_SIZE);
692 for (n = get_ushort(); n > 0; n--) {
693 ttf_skip(2 * TTF_USHORT_SIZE);
694 k = get_ushort();
695 if (!(k & 1) || (k & 2) || (k & 4))
696 return;
697 if (k >> 8 != 0) {
698 ttf_warn("warning: only format 0 supported of `kern' \
699 subtables, others are ignored\n");
700 continue;
701 }
702 k = get_ushort();
703 ttf_skip(3 * TTF_USHORT_SIZE);
704 while (k-- > 0) {
705 i = get_ushort();
706 j = get_ushort();
707 kern_value = get_fword();
708 if (kern_value != 0) {
709 store_kern_value(i, j, kern_value);
710 nkernpairs++;
711 }
712 }
713 }
714 }
715
716 static int null_glyph(const char *s)
717 {
718 return (s == NULL || s == notdef);
719 /*
720 strcmp(s, ".null") == 0 ||
721 strcmp(s, ".notdef") == 0 ||
722 strcmp(s, "CR") == 0 ||
723 strcmp(s, "nonmarkingreturn") == 0
724 */
725 }
726
727 static void print_glyph_name(FILE * f, long glyph_index, int convention)
728 {
729 unicode_entry *u;
730 static char buf[1024];
731 const char *n;
732 assert(glyph_index >= 0 && glyph_index < nglyphs);
733 n = mtx_tab[glyph_index].name;
734 if (printing_enc && (n == notdef || glyph_index == 0)) {
735 fputs(notdef, f);
736 return;
737 }
738 switch (convention) {
739 case AS_NAME:
740 if (!null_glyph(n))
741 fprintf(f, "%s", mtx_tab[glyph_index].name);
742 else if (n == notdef && glyph_index == 0)
743 fputs(notdef, f);
744 else
745 fprintf(f, "%s%li", GLYPH_PREFIX_INDEX, glyph_index);
746 break;
747 case AS_INDEX:
748 fprintf(f, "%s%li", GLYPH_PREFIX_INDEX, glyph_index);
749 break;
750 case AS_UNICODE:
751 u = mtx_tab[glyph_index].unicode_list;
752 if (glyph_index == 0 || u == NULL)
753 fprintf(f, "%s%li", GLYPH_PREFIX_INDEX, glyph_index);
754 else {
755 fprintf(f, "%s%.4X", GLYPH_PREFIX_UNICODE, u->code);
756 if (u->next != NULL) {
757 *buf = 0;
758 for (; u != NULL; u = u->next) {
759 assert(strlen(buf) + strlen(GLYPH_PREFIX_UNICODE) + 4 <
760 sizeof(buf));
761 sprintf(strend(buf), "%s%.4X ", GLYPH_PREFIX_UNICODE,
762 u->code);
763 }
764 ttf_warn
765 ("glyph %li has multiple encodings (the first one being used): %s",
766 glyph_index, buf);
767 }
768 }
769 break;
770 }
771 }
772
773 static void print_char_metric(FILE * f, int charcode, long glyph_index)
774 {
775 assert(glyph_index >= 0 && glyph_index < nglyphs);
776 fprintf(f, "C %i ; WX %i ; N ", (int) charcode,
777 (int) get_ttf_funit((int)mtx_tab[glyph_index].wx));
778 print_glyph_name(f, glyph_index, print_glyph);
779 fprintf(f, " ; B %i %i %i %i ;\n",
780 (int) get_ttf_funit(mtx_tab[glyph_index].bbox[0]),
781 (int) get_ttf_funit(mtx_tab[glyph_index].bbox[1]),
782 (int) get_ttf_funit(mtx_tab[glyph_index].bbox[2]),
783 (int) get_ttf_funit(mtx_tab[glyph_index].bbox[3]));
784 }
785
786 static char * escape_eol(char *s)
787 {
788 char *p, *e;
789 char *dest, *d;
790
791 if (s == NULL)
792 return NULL;
793
794 dest = d = xtalloc(2*strlen(s), char);
795 e = strend(s);
796 for (p = s; p < e; p++) {
797 if (*p == 10 || *p == 13) {
798 *d++ = '\\';
799 *d++ = 'n';
800 }
801 else
802 *d++ = *p;
803 }
804 *d = 0;
805 return dest;
806 }
807
808 static void print_afm(char *date, char *fontname)
809 {
810 int ncharmetrics;
811 mtx_entry *pm;
812 long mtx_index[256], *idx;
813 unsigned int index;
814 char **pe;
815 kern_entry *pk, *qk;
816 char *p;
817 double d;
818 long l;
819 fputs("StartFontMetrics 2.0\n", outfile);
820 fprintf(outfile,
821 "Comment Converted at %s by ttf2afm from font file `%s'\n", date,
822 fontname);
823 print_str(FontName);
824 print_str(FullName);
825 print_str(FamilyName);
826 print_str(Weight);
827 l = ItalicAngle >> 16;
828 if (l > 0x8000)
829 l = l - 0x10000;
830 d = (ItalicAngle & 0xffff)/65536.0;
831 if (d >= 0.1)
832 fprintf(outfile, "ItalicAngle %.1f", d + l);
833 else
834 fprintf(outfile, "ItalicAngle %li", l);
835 fputs("\n", outfile);
836 fprintf(outfile, "IsFixedPitch %s\n", IsFixedPitch ? "true" : "false");
837 fprintf(outfile, "FontBBox %i %i %i %i\n",
838 (int) get_ttf_funit(FontBBox1),
839 (int) get_ttf_funit(FontBBox2),
840 (int) get_ttf_funit(FontBBox3), (int) get_ttf_funit(FontBBox4));
841 print_dimen(UnderlinePosition);
842 print_dimen(UnderlineThickness);
843 print_str(Version);
844 /* remove trailing whitespaces from Notice */
845 if (Notice != NULL) {
846 for (p = strend(Notice); p > Notice && isspace((unsigned char)*p); p--);
847 *p = 0;
848 }
849 print_str(Notice);
850 fputs("EncodingScheme FontSpecific\n", outfile);
851 print_dimen(CapHeight);
852 print_dimen(XHeight);
853 print_dimen(Ascender);
854 print_dimen(Descender);
855 ncharmetrics = nglyphs;
856 if (use_ext_enc == 0) { /* external encoding vector not given */
857 fprintf(outfile, "StartCharMetrics %u\n", ncharmetrics);
858 for (pm = mtx_tab; pm - mtx_tab < nglyphs; pm++) {
859 pm->found = 1;
860 print_char_metric(outfile, -1, pm - mtx_tab);
861 }
862 } else { /* external encoding vector given */
863 for (idx = mtx_index; idx - mtx_index < 256; *idx++ = 0);
864 for (pe = enc_names; pe - enc_names < names_count; pe++) {
865 if (*pe == notdef)
866 continue;
867 /* scan form `uniABCD' */
868 if (sscanf(*pe, GLYPH_PREFIX_UNICODE "%4X", &index) == 1) {
869 if (unicode_map[index] != NOGLYPH_ASSIGNED_YET) {
870 pm = mtx_tab + unicode_map[index];
871 mtx_index[pe - enc_names] = pm - mtx_tab;
872 pm->found = 1;
873 } else
874 ttf_warn("`unicode %s%.4X' is not mapped to any glyph",
875 GLYPH_PREFIX_UNICODE, index);
876 continue;
877 }
878 /* scan form `index123' */
879 if (sscanf(*pe, GLYPH_PREFIX_INDEX "%u", &index) == 1) {
880 if (index >= (unsigned int) nglyphs)
881 ttf_fail("`%s' out of valid range [0..%i)", *pe, nglyphs);
882 pm = mtx_tab + index;
883 mtx_index[pe - enc_names] = pm - mtx_tab;
884 pm->found = 1;
885 continue;
886 }
887 for (pm = mtx_tab; pm - mtx_tab < nglyphs; pm++)
888 if (pm->name != NULL && strcmp(*pe, pm->name) == 0)
889 break;
890 if (pm - mtx_tab < nglyphs) {
891 mtx_index[pe - enc_names] = pm - mtx_tab;
892 pm->found = 1;
893 continue;
894 } else
895 ttf_warn("glyph `%s' not found", *pe);
896 }
897 fprintf(outfile, "StartCharMetrics %u\n", ncharmetrics);
898 for (idx = mtx_index; idx - mtx_index < 256; idx++) {
899 if (*idx != 0 && mtx_tab[*idx].found == 1)
900 print_char_metric(outfile, idx - mtx_index, *idx);
901 }
902 for (pm = mtx_tab; pm - mtx_tab < nglyphs; pm++) {
903 if (pm->found == 0)
904 print_char_metric(outfile, -1, pm - mtx_tab);
905 }
906 }
907 fputs("EndCharMetrics\n", outfile);
908 if (nkernpairs == 0)
909 goto end_kerns;
910 fprintf(outfile, "StartKernData\nStartKernPairs %i\n", nkernpairs);
911 for (pk = kern_tab; pk - kern_tab < nglyphs; pk++)
912 for (qk = pk; qk != NULL; qk = qk->next)
913 if (qk->value != 0) {
914 fputs("KPX ", outfile);
915 print_glyph_name(outfile, pk - kern_tab, print_glyph);
916 fputs(" ", outfile);
917 print_glyph_name(outfile, qk->adjacent, print_glyph);
918 fprintf(outfile, " %i\n", get_ttf_funit(qk->value));
919 }
920 fputs("EndKernPairs\nEndKernData\n", outfile);
921 end_kerns:
922 fputs("EndFontMetrics\n", outfile);
923 }
924
925 static void print_encoding(char *fontname)
926 {
927 long int i, k, first_code, length;
928 FILE *file;
929 TTF_USHORT format;
930 char *enc_name, *end_enc_name;
931 cmap_entry *e;
932 printing_enc = 1;
933 enc_name = xtalloc(strlen(bname) + 20, char);
934 strcpy(enc_name, bname);
935 end_enc_name = strend(enc_name);
936 for (e = cmap_tab; e - cmap_tab < ncmapsubtabs; e++) {
937 ttf_seek_off("cmap", cmap_offset + e->offset);
938 format = get_ushort();
939 if (format != 0 && format != 4 && format != 6) {
940 ttf_warn("format %i of encoding subtable unsupported",
941 (int) format);
942 continue;
943 }
944 sprintf(end_enc_name, ".e%i%i",
945 (int) e->platform_id, (int) e->encoding_id);
946 if ((file = xfopen(enc_name, FOPEN_W_MODE)) == NULL)
947 ttf_fail("cannot open file for writing (%s)\n", enc_name);
948 fprintf(file, "%% Encoding table from font file %s\n", fontname);
949 fprintf(file, "%% Platform ID %i", (int) e->platform_id);
950 switch (e->platform_id) {
951 case 0:
952 fprintf(file, " (Apple Unicode)");
953 break;
954 case 1:
955 fprintf(file, " (Macintosh)");
956 break;
957 case 2:
958 fprintf(file, " (ISO)");
959 break;
960 case 3:
961 fprintf(file, " (Microsoft)");
962 break;
963 }
964 fprintf(file, "\n");
965 fprintf(file, "%% Encoding ID %i", (int) e->encoding_id);
966 if (e->platform_id == 1 && e->encoding_id == 0)
967 fprintf(file, " (Roman)");
968 if (e->platform_id == 3)
969 switch (e->encoding_id) {
970 case 0:
971 fprintf(file, " (Symbol)");
972 break;
973 case 1:
974 fprintf(file, " (Unicode)");
975 break;
976 }
977 fprintf(file, "\n");
978 fprintf(file, "%% Format %i", (int) (format));
979 switch (format) {
980 case 0:
981 fprintf(file, " (byte encoding table)");
982 break;
983 case 4:
984 fprintf(file, " (segment mapping to delta values)");
985 break;
986 case 6:
987 fprintf(file, " (trimmed table mapping)");
988 break;
989 }
990 fprintf(file, "\n");
991 fprintf(file, "/Encoding%i [\n", (int) (e - cmap_tab + 1));
992 switch (format) {
993 case 0:
994 (void) get_ushort(); /* skip length */
995 (void) get_ushort(); /* skip version number */
996 for (i = 0; i < 256; i++) {
997 fputs("/", file);
998 print_glyph_name(file, get_byte(), print_glyph);
999 fputs("\n", file);
1000 }
1001 break;
1002 case 4:
1003 for (i = 0; i < nglyphs; ++i) {
1004 fprintf(file, "%% Glyph %li -> ", i);
1005 print_glyph_name(file, i, AS_UNICODE);
1006 fputs("\n", file);
1007 }
1008 break;
1009 case 6:
1010 (void) get_ushort(); /* skip table length */
1011 (void) get_ushort(); /* skip version number */
1012 first_code = get_ushort(); /* first character code */
1013 for (i = 0; i < first_code; ++i)
1014 fprintf(file, "/%s\n", notdef);
1015 length = get_ushort(); /* number of character codes */
1016 for (i = first_code; i < first_code + length; i++) {
1017 k = get_ushort();
1018 if (i > 255)
1019 fputs("% ", file);
1020 fputs("/", file);
1021 print_glyph_name(file, k, print_glyph);
1022 fputs("\n", file);
1023 }
1024 for (i = first_code + length; i < 256; i++)
1025 fprintf(file, "/%s\n", notdef);
1026 break;
1027 default:
1028 ttf_warn("format %i of encoding subtable unsupported",
1029 (int) format);
1030 }
1031 fprintf(file, "] def\n");
1032 }
1033 xfree(enc_name);
1034 }
1035
1036 static void usage(void)
1037 {
1038 cur_file_name = NULL;
1039 fprintf(stderr,
1040 "Usage: ttf2afm [-i|-u|-c|-v] [-e enc] [-o filename] [-m NUM] fontfile\n"
1041 " -i: force printing glyph names in form 'index123'\n"
1042 " -u: force printing glyph names in form 'uniABCD'\n"
1043 " -c: print encoding tables to `basename.e<platformID><encodingID>'\n"
1044 " -v: print version\n"
1045 " -e enc: encode the AFM output using encoding vector from `enc'\n"
1046 " -o filename: write output to file `filename' instead of stdout\n"
1047 " -m NUM: select unicode mapping (default = 1, ie the first)\n"
1048 " fontfile: the TrueType fontfile\n"
1049 "\nPlease send bug reports or feature requests to <[email protected]>\n");
1050 _exit(-1);
1051 }
1052
1053 int main(int argc, char **argv)
1054 {
1055 char date[128], *s;
1056 time_t t = time(&t);
1057 int c;
1058 kpse_set_program_name(argv[0], "ttf2afm");
1059 kpse_init_prog("ttf2afm", 0, 0, 0);
1060 while ((c = getopt(argc, argv, "iucve:o:m:")) != -1)
1061 switch (c) {
1062 case 'i':
1063 print_glyph = AS_INDEX;
1064 break;
1065 case 'u':
1066 print_glyph = AS_UNICODE;
1067 break;
1068 case 'c':
1069 print_cmap = 1;
1070 break;
1071 case 'e':
1072 cur_file_name = optarg;
1073 read_encoding(cur_file_name);
1074 use_ext_enc = 1;
1075 break;
1076 case 'o':
1077 cur_file_name = optarg;
1078 outfile = xfopen(cur_file_name, FOPEN_W_MODE);
1079 if (outfile == NULL)
1080 ttf_fail("cannot open file for writing");
1081 break;
1082 case 'm':
1083 select_unicode = atoi(optarg);
1084 break;
1085 case 'v':
1086 fprintf(stderr,
1087 "ttf2afm version " VERSION "\n"
1088 "Copyright (C) 1997-2014 Han The Thanh.\n"
1089 "There is NO warranty. Redistribution of this software is\n"
1090 "covered by the terms of both the pdfTeX copyright and\n"
1091 "the GNU General Public License.\n"
1092 "For more information about these matters, see the files\n"
1093 "named COPYING and the pdfTeX source.\n"
1094 "Primary author of ttf2afm: Han The Thanh.\n");
1095 _exit(0);
1096 default:
1097 usage();
1098 }
1099 if (argc - optind != 1)
1100 usage();
1101 sprintf(date, "%s\n", ctime(&t));
1102 *(char *) strchr(date, '\n') = 0;
1103 cur_file_name = argv[optind];
1104 if (print_cmap) {
1105 bname = xstrdup(xbasename(cur_file_name));
1106 if ((s = strrchr(bname, '.')) != NULL)
1107 *s = 0;
1108 }
1109 if ((fontfile =
1110 kpse_open_file(cur_file_name, kpse_truetype_format)) == NULL)
1111 ttf_fail("can't open font file for reading");
1112 read_font();
1113 if (outfile == NULL)
1114 outfile = stdout;
1115 print_afm(date, cur_file_name);
1116 if (print_cmap)
1117 print_encoding(cur_file_name);
1118 xfree(FontName);
1119 xfree(FullName);
1120 xfree(FamilyName);
1121 xfree(Notice);
1122 xfree(Version);
1123 xfree(Weight);
1124 free_tabs();
1125 xfclose(fontfile, cur_file_name);
1126 return 0;
1127 }