Debian Codegraph @

contrib/fonttools/ttf2eps.c

raw symbols SCIP
1 /* Copyright (C) 2001 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <gutils.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <time.h>
33
34 typedef struct basepoint {
35 double x, y;
36 } BasePoint;
37
38 struct dup {
39 int glyph;
40 int enc;
41 struct dup *prev;
42 };
43
44 struct ttfinfo {
45 int emsize; /* from head table */
46 int ascent, descent; /* from hhea table */
47 int glyph_cnt; /* from maxp table */
48 int width_cnt; /* from the hhea table */
49 unsigned int index_to_loc_is_long:1; /* in head table */
50 /* Mac fonts platform=0/1, platform specific enc id, roman=0, english is lang code 0 */
51 /* iso platform=2, platform specific enc id, latin1=0/2, no language */
52 /* microsoft platform=3, platform specific enc id, 1, english is lang code 0x??09 */
53 int numtables;
54 /* cmap */
55 int encoding_start; /* Offset from sof to start of encoding table */
56 /* glyf */
57 int glyph_start; /* Offset from sof to start of glyph table */
58 /* EBDT */
59 int bitmapdata_start; /* Offset to start of bitmap data */
60 /* EBLT */
61 int bitmaploc_start; /* Offset to start of bitmap locator data */
62 /* head */
63 int head_start;
64 /* hhea */
65 int hhea_start;
66 /* hmtx */
67 int hmetrics_start;
68 /* kern */
69 int kern_start;
70 /* loca */
71 int glyphlocations_start; /* there are glyph_cnt of these, from maxp tab */
72 int loca_length; /* actually glypn_cnt is wrong. Use the table length (divided by size) instead */
73 /* maxp */
74 int maxp_start; /* maximum number of glyphs */
75 /* name */
76 int copyright_start; /* copyright and fontname */
77 /* post */
78 int postscript_start; /* names for the glyphs, italic angle, etc. */
79 int postscript_len; /* names for the glyphs, italic angle, etc. */
80 /* OS/2 */
81 int os2_start;
82 int os2_len;
83
84 int fpgm_start;
85 int fpgm_len;
86 int prep_start;
87 int prep_len;
88
89 unsigned int *glyph_offsets;
90 char **glyph_names;
91 unsigned short *glyph_unicode;
92 int *glyph_widths;
93 struct dup *dups;
94 };
95
96 static char *standardnames[258] = {
97 ".notdef",
98 ".null",
99 "nonmarkingreturn",
100 "space",
101 "exclam",
102 "quotedbl",
103 "numbersign",
104 "dollar",
105 "percent",
106 "ampersand",
107 "quotesingle",
108 "parenleft",
109 "parenright",
110 "asterisk",
111 "plus",
112 "comma",
113 "hyphen",
114 "period",
115 "slash",
116 "zero",
117 "one",
118 "two",
119 "three",
120 "four",
121 "five",
122 "six",
123 "seven",
124 "eight",
125 "nine",
126 "colon",
127 "semicolon",
128 "less",
129 "equal",
130 "greater",
131 "question",
132 "at",
133 "A",
134 "B",
135 "C",
136 "D",
137 "E",
138 "F",
139 "G",
140 "H",
141 "I",
142 "J",
143 "K",
144 "L",
145 "M",
146 "N",
147 "O",
148 "P",
149 "Q",
150 "R",
151 "S",
152 "T",
153 "U",
154 "V",
155 "W",
156 "X",
157 "Y",
158 "Z",
159 "bracketleft",
160 "backslash",
161 "bracketright",
162 "asciicircum",
163 "underscore",
164 "grave",
165 "a",
166 "b",
167 "c",
168 "d",
169 "e",
170 "f",
171 "g",
172 "h",
173 "i",
174 "j",
175 "k",
176 "l",
177 "m",
178 "n",
179 "o",
180 "p",
181 "q",
182 "r",
183 "s",
184 "t",
185 "u",
186 "v",
187 "w",
188 "x",
189 "y",
190 "z",
191 "braceleft",
192 "bar",
193 "braceright",
194 "asciitilde",
195 "Adieresis",
196 "Aring",
197 "Ccedilla",
198 "Eacute",
199 "Ntilde",
200 "Odieresis",
201 "Udieresis",
202 "aacute",
203 "agrave",
204 "acircumflex",
205 "adieresis",
206 "atilde",
207 "aring",
208 "ccedilla",
209 "eacute",
210 "egrave",
211 "ecircumflex",
212 "edieresis",
213 "iacute",
214 "igrave",
215 "icircumflex",
216 "idieresis",
217 "ntilde",
218 "oacute",
219 "ograve",
220 "ocircumflex",
221 "odieresis",
222 "otilde",
223 "uacute",
224 "ugrave",
225 "ucircumflex",
226 "udieresis",
227 "dagger",
228 "degree",
229 "cent",
230 "sterling",
231 "section",
232 "bullet",
233 "paragraph",
234 "germandbls",
235 "registered",
236 "copyright",
237 "trademark",
238 "acute",
239 "dieresis",
240 "notequal",
241 "AE",
242 "Oslash",
243 "infinity",
244 "plusminus",
245 "lessequal",
246 "greaterequal",
247 "yen",
248 "mu",
249 "partialdiff",
250 "summation",
251 "product",
252 "pi",
253 "integral",
254 "ordfeminine",
255 "ordmasculine",
256 "Omega",
257 "ae",
258 "oslash",
259 "questiondown",
260 "exclamdown",
261 "logicalnot",
262 "radical",
263 "florin",
264 "approxequal",
265 "Delta",
266 "guillemotleft",
267 "guillemotright",
268 "ellipsis",
269 "nonbreakingspace",
270 "Agrave",
271 "Atilde",
272 "Otilde",
273 "OE",
274 "oe",
275 "endash",
276 "emdash",
277 "quotedblleft",
278 "quotedblright",
279 "quoteleft",
280 "quoteright",
281 "divide",
282 "lozenge",
283 "ydieresis",
284 "Ydieresis",
285 "fraction",
286 "currency",
287 "guilsinglleft",
288 "guilsinglright",
289 "fi",
290 "fl",
291 "daggerdbl",
292 "periodcentered",
293 "quotesinglbase",
294 "quotedblbase",
295 "perthousand",
296 "Acircumflex",
297 "Ecircumflex",
298 "Aacute",
299 "Edieresis",
300 "Egrave",
301 "Iacute",
302 "Icircumflex",
303 "Idieresis",
304 "Igrave",
305 "Oacute",
306 "Ocircumflex",
307 "apple",
308 "Ograve",
309 "Uacute",
310 "Ucircumflex",
311 "Ugrave",
312 "dotlessi",
313 "circumflex",
314 "tilde",
315 "macron",
316 "breve",
317 "dotaccent",
318 "ring",
319 "cedilla",
320 "hungarumlaut",
321 "ogonek",
322 "caron",
323 "Lslash",
324 "lslash",
325 "Scaron",
326 "scaron",
327 "Zcaron",
328 "zcaron",
329 "brokenbar",
330 "Eth",
331 "eth",
332 "Yacute",
333 "yacute",
334 "Thorn",
335 "thorn",
336 "minus",
337 "multiply",
338 "onesuperior",
339 "twosuperior",
340 "threesuperior",
341 "onehalf",
342 "onequarter",
343 "threequarters",
344 "franc",
345 "Gbreve",
346 "gbreve",
347 "Idotaccent",
348 "Scedilla",
349 "scedilla",
350 "Cacute",
351 "cacute",
352 "Ccaron",
353 "ccaron",
354 "dcroat"
355 };
356
357 const unsigned short unicode_from_mac[] = {
358 0x0000, 0x00d0, 0x00f0, 0x0141, 0x0142, 0x0160, 0x0161, 0x00dd,
359 0x00fd, 0x0009, 0x000a, 0x00de, 0x00fe, 0x000d, 0x017d, 0x017e,
360 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x00bd, 0x00bc, 0x00b9,
361 0x00be, 0x00b3, 0x00b2, 0x00a6, 0x00ad, 0x00d7, 0x001e, 0x001f,
362 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
363 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
364 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
365 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
366 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
367 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
368 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
369 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
370 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
371 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
372 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
373 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
374 0x00c4, 0x00c5, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1,
375 0x00e0, 0x00e2, 0x00e4, 0x00e3, 0x00e5, 0x00e7, 0x00e9, 0x00e8,
376 0x00ea, 0x00eb, 0x00ed, 0x00ec, 0x00ee, 0x00ef, 0x00f1, 0x00f3,
377 0x00f2, 0x00f4, 0x00f6, 0x00f5, 0x00fa, 0x00f9, 0x00fb, 0x00fc,
378 0x2020, 0x00b0, 0x00a2, 0x00a3, 0x00a7, 0x2022, 0x00b6, 0x00df,
379 0x00ae, 0x00a9, 0x2122, 0x00b4, 0x00a8, 0x2260, 0x00c6, 0x00d8,
380 0x221e, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x00b5, 0x2202, 0x2211,
381 0x220f, 0x03c0, 0x222b, 0x00aa, 0x00ba, 0x2126, 0x00e6, 0x00f8,
382 0x00bf, 0x00a1, 0x00ac, 0x221a, 0x0192, 0x2248, 0x2206, 0x00ab,
383 0x00bb, 0x2026, 0x00a0, 0x00c0, 0x00c3, 0x00d5, 0x0152, 0x0153,
384 0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x25ca,
385 0x00ff, 0x0178, 0x2044, 0x00a4, 0x2039, 0x203a, 0xfb01, 0xfb02,
386 0x2021, 0x00b7, 0x201a, 0x201e, 0x2030, 0x00c2, 0x00ca, 0x00c1,
387 0x00cb, 0x00c8, 0x00cd, 0x00ce, 0x00cf, 0x00cc, 0x00d3, 0x00d4,
388 0xf8ff, 0x00d2, 0x00da, 0x00db, 0x00d9, 0x0131, 0x02c6, 0x02dc,
389 0x00af, 0x02d8, 0x02d9, 0x02da, 0x00b8, 0x02dd, 0x02db, 0x02c7
390 };
391
392 static int getushort(FILE *ttf) {
393 int ch1 = getc(ttf);
394 int ch2 = getc(ttf);
395 if ( ch2==EOF )
396 return( EOF );
397 return( (ch1<<8)|ch2 );
398 }
399
400 static int getlong(FILE *ttf) {
401 int ch1 = getc(ttf);
402 int ch2 = getc(ttf);
403 int ch3 = getc(ttf);
404 int ch4 = getc(ttf);
405 if ( ch4==EOF )
406 return( EOF );
407 return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
408 }
409
410 static double getfixed(FILE *ttf) {
411 int val = getlong(ttf);
412 int mant = val&0xffff;
413 /* This oddity may be needed to deal with the first 16 bits being signed */
414 /* and the low-order bits unsigned */
415 return( (double) (val>>16) + (mant/65536.0) );
416 }
417
418 static double get2dot14(FILE *ttf) {
419 int val = getushort(ttf);
420 int mant = val&0x3fff;
421 /* This oddity may be needed to deal with the first 2 bits being signed */
422 /* and the low-order bits unsigned */
423 return( (double) ((val<<16)>>(16+14)) + (mant/16384.0) );
424 }
425
426 static void readttfhead(FILE *ttf, struct ttfinfo *info) {
427 int i;
428
429 fseek(ttf,info->head_start+4*4+2,SEEK_SET); /* skip over the version number and a bunch of junk */
430 info->emsize = getushort(ttf);
431 for ( i=0; i<15; ++i )
432 getushort(ttf);
433 info->index_to_loc_is_long = getushort(ttf);
434 if ( info->index_to_loc_is_long )
435 info->glyph_cnt = info->loca_length/4-1;
436 }
437
438 static void readttfhhea(FILE *ttf,struct ttfinfo *info) {
439 /* Here I want ascent, descent and the number of horizontal metrics */
440 int i;
441
442 fseek(ttf,info->hhea_start+4,SEEK_SET); /* skip over the version number */
443 info->ascent = getushort(ttf);
444 info->descent = -(short) getushort(ttf);
445
446 /* fontographer puts the max ascender/min descender here instead. idiots */
447 if ( info->ascent==0 && info->descent==0 )
448 info->ascent = .8*info->emsize;
449 if ( info->ascent+info->descent!=info->emsize )
450 info->ascent = info->ascent * ((double) info->emsize)/(info->ascent+info->descent);
451 info->descent = info->emsize-info->ascent;
452
453 for ( i=0; i<13; ++i )
454 getushort(ttf);
455 info->width_cnt = getushort(ttf);
456 }
457
458 static void readttfpost(FILE *ttf, struct ttfinfo *info) {
459 int format;
460 int gc;
461 int i, j, ind, extras;
462 int len;
463 unsigned short *indexes;
464 char **names, *name;
465
466 fseek(ttf,info->postscript_start,SEEK_SET);
467 format = getlong(ttf);
468 getlong(ttf);
469 getlong(ttf);
470 getlong(ttf);
471 getlong(ttf);
472 getlong(ttf);
473 getlong(ttf);
474 getlong(ttf);
475 if ( format==0x00020000 ) {
476 gc = getushort(ttf);
477 indexes = calloc(65536,sizeof(unsigned short));
478 names = calloc(info->glyph_cnt,sizeof(char *));
479 extras = 0;
480 for ( i=0; i<gc; ++i ) {
481 ind = getushort(ttf);
482 if ( ind>=258 ) ++extras;
483 indexes[ind] = i;
484 }
485 for ( i=0; i<258; ++i ) if ( indexes[i]!=0 )
486 names[indexes[i]] = standardnames[i];
487 for ( i=258; i<258+extras; ++i ) {
488 if ( indexes[i]==0 )
489 break;
490 len = getc(ttf);
491 name = malloc(len+1);
492 for ( j=0; j<len; ++j )
493 name[j] = getc(ttf);
494 name[j] = '\0';
495 names[indexes[i]] = name;
496 }
497 if ( names[0]==NULL )
498 names[0] = standardnames[0];
499 free(indexes);
500 info->glyph_names = names;
501 }
502 }
503
504 static struct dup *makedup(int glyph, int uni, struct dup *prev) {
505 struct dup *d = calloc(1,sizeof(struct dup));
506
507 d->glyph = glyph;
508 d->enc = uni;
509 d->prev = prev;
510 return( d );
511 }
512
513 static void readttfencodings(FILE *ttf,struct ttfinfo *info) {
514 int i,j;
515 int nencs, version;
516 int enc = 0;
517 int platform, specific;
518 int offset, encoff;
519 int format, len;
520 const unsigned short *table;
521 int segCount;
522 unsigned short *endchars, *startchars, *delta, *rangeOffset, *glyphs;
523 int index;
524
525 fseek(ttf,info->encoding_start,SEEK_SET);
526 version = getushort(ttf);
527 nencs = getushort(ttf);
528 if ( version!=0 && nencs==0 )
529 nencs = version; /* Sometimes they are backwards */
530 for ( i=0; i<nencs; ++i ) {
531 platform = getushort(ttf);
532 specific = getushort(ttf);
533 offset = getlong(ttf);
534 if ( platform==3 && specific==1 ) {
535 enc = 1;
536 encoff = offset;
537 } else if ( platform==1 && specific==0 && enc!=1 ) {
538 enc = 2;
539 encoff = offset;
540 }
541 }
542 if ( enc!=0 ) {
543 info->glyph_unicode = calloc(info->glyph_cnt,sizeof(unsigned short));
544 fseek(ttf,info->encoding_start+encoff,SEEK_SET);
545 format = getushort(ttf);
546 len = getushort(ttf);
547 /* version = */ getushort(ttf);
548 if ( format==0 ) {
549 table = unicode_from_mac;
550 for ( i=0; i<256; ++i ) {
551 info->glyph_unicode[i] = table[i];
552 if ( i<32 && table[i]==0 )
553 info->glyph_unicode[i] = i;
554 }
555 } else if ( format==4 ) {
556 segCount = getushort(ttf)/2;
557 /* searchRange = */ getushort(ttf);
558 /* entrySelector = */ getushort(ttf);
559 /* rangeShift = */ getushort(ttf);
560 endchars = malloc(segCount*sizeof(unsigned short));
561 for ( i=0; i<segCount; ++i )
562 endchars[i] = getushort(ttf);
563 if ( getushort(ttf)!=0 )
564 fprintf(stderr,"Expected 0 in true type font");
565 startchars = malloc(segCount*sizeof(unsigned short));
566 for ( i=0; i<segCount; ++i )
567 startchars[i] = getushort(ttf);
568 delta = malloc(segCount*sizeof(unsigned short));
569 for ( i=0; i<segCount; ++i )
570 delta[i] = getushort(ttf);
571 rangeOffset = malloc(segCount*sizeof(unsigned short));
572 for ( i=0; i<segCount; ++i )
573 rangeOffset[i] = getushort(ttf);
574 len -= 8*sizeof(unsigned short) +
575 4*segCount*sizeof(unsigned short);
576 /* that's the amount of space left in the subtable and it must */
577 /* be filled with glyphIDs */
578 glyphs = malloc(len);
579 for ( i=0; i<len/2; ++i )
580 glyphs[i] = getushort(ttf);
581 for ( i=0; i<segCount; ++i ) {
582 if ( rangeOffset[i]==0 ) {
583 /* It's ok for a glyph to have several different encodings*/
584 /* "A" might work for "Alpha" for example */
585 for ( j=startchars[i]; j<=endchars[i]; ++j )
586 if ( info->glyph_unicode[(unsigned short) (j+delta[i])]!=0 )
587 info->dups = makedup((unsigned short) (j+delta[i]),j,info->dups);
588 else
589 info->glyph_unicode[(unsigned short) (j+delta[i])] = j;
590 } else if ( rangeOffset[i]!=0xffff ) {
591 /* It isn't explicitly mentioned by a rangeOffset of 0xffff*/
592 /* means no glyph */
593 for ( j=startchars[i]; j<=endchars[i]; ++j ) {
594 index = glyphs[ (i-segCount+rangeOffset[i]/2) +
595 j-startchars[i] ];
596 if ( index!=0 ) {
597 index = (unsigned short) (index+delta[i]);
598 if ( info->glyph_unicode[index]!=0 )
599 info->dups = makedup(index,j,info->dups);
600 else
601 info->glyph_unicode[index] = j;
602 }
603 }
604 }
605 }
606 free(glyphs);
607 free(rangeOffset);
608 free(delta);
609 free(startchars);
610 free(endchars);
611 } else if ( format==6 ) {
612 /* Apple's unicode format */
613 int first, count;
614 first = getushort(ttf);
615 count = getushort(ttf);
616 for ( i=0; i<count; ++i )
617 info->glyph_unicode[getushort(ttf)] = first+i;
618 } else if ( format==2 ) {
619 fprintf(stderr,"I don't support mixed 8/16 bit characters (no shit jis for me)");
620 } else if ( format==8 ) {
621 fprintf(stderr,"I don't support mixed 16/32 bit characters (no unicode surogates)");
622 } else if ( format==10 || format==12 ) {
623 fprintf(stderr,"I don't support 32 bit characters");
624 }
625 }
626 }
627
628 static void readttflocations(FILE *ttf,struct ttfinfo *info) {
629 int i;
630 info->glyph_offsets = malloc((info->glyph_cnt+1)*sizeof(unsigned int));
631
632 /* First we read all the locations. This might not be needed, they may */
633 /* just follow one another, but nothing I've noticed says that so let's */
634 /* be careful */
635 fseek(ttf,info->glyphlocations_start,SEEK_SET);
636 if ( info->index_to_loc_is_long ) {
637 for ( i=0; i<=info->glyph_cnt ; ++i )
638 info->glyph_offsets[i] = getlong(ttf);
639 } else {
640 for ( i=0; i<=info->glyph_cnt ; ++i )
641 info->glyph_offsets[i] = 2*getushort(ttf);
642 }
643 }
644
645 static void readttfwidths(FILE *ttf,struct ttfinfo *info) {
646 int i,j;
647
648 fseek(ttf,info->hmetrics_start,SEEK_SET);
649 info->glyph_widths = calloc(info->glyph_cnt,sizeof(int));
650 for ( i=0; i<info->width_cnt && i<info->glyph_cnt; ++i ) {
651 info->glyph_widths[i] = getushort(ttf);
652 /* lsb = */ getushort(ttf);
653 }
654 for ( j=i; j<info->glyph_cnt; ++j )
655 info->glyph_widths[j] = info->glyph_widths[i-1];
656 }
657
658 #define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4))
659
660 static int readttfheader(FILE *ttf, struct ttfinfo *info) {
661 int i;
662 int tag, offset, length;
663
664 /* version = */ getfixed(ttf);
665 info->numtables = getushort(ttf);
666 /* sr = */ getushort(ttf),
667 /* es = */ getushort(ttf),
668 /* rs = */ getushort(ttf);
669
670 for ( i=0; i<info->numtables; ++i ) {
671 tag = getlong(ttf);
672 /* checksum = */ getlong(ttf);
673 offset = getlong(ttf);
674 length = getlong(ttf);
675 switch ( tag ) {
676 case CHR('c','m','a','p'):
677 info->encoding_start = offset;
678 break;
679 case CHR('g','l','y','f'):
680 info->glyph_start = offset;
681 break;
682 case CHR('E','B','D','T'):
683 info->bitmapdata_start = offset;
684 break;
685 case CHR('E','B','L','T'):
686 info->bitmaploc_start = offset;
687 break;
688 case CHR('h','e','a','d'):
689 info->head_start = offset;
690 if ( length!=54 ) fprintf( stderr, "> ! Unexpected length for head table, was %d expected 54 ! <\n", length );
691 break;
692 case CHR('h','h','e','a'):
693 info->hhea_start = offset;
694 if ( length!=36 ) fprintf( stderr, "> ! Unexpected length for hhea table, was %d expected 36 ! <\n", length );
695 break;
696 case CHR('h','m','t','x'):
697 info->hmetrics_start = offset;
698 break;
699 case CHR('k','e','r','n'):
700 info->kern_start = offset;
701 break;
702 case CHR('l','o','c','a'):
703 info->glyphlocations_start = offset;
704 info->loca_length = length;
705 info->glyph_cnt = length/2-1; /* we don't always have a maxp table. the minus one is because there is one extra entry to give the length of the last glyph */
706 break;
707 case CHR('m','a','x','p'):
708 info->maxp_start = offset;
709 if ( length!=32 ) fprintf( stderr, "> ! Unexpected length for maxp table, was %d expected 32 ! <\n", length );
710 break;
711 case CHR('n','a','m','e'):
712 info->copyright_start = offset;
713 break;
714 case CHR('p','o','s','t'):
715 info->postscript_start = offset;
716 info->postscript_len = length;
717 break;
718 case CHR('O','S','/','2'):
719 info->os2_start = offset;
720 info->os2_len = length;
721 if ( length!=78 && length!=86 )
722 fprintf( stderr, "> ! Unexpected length for OS/2 table, was %d expected either 78 or 86 ! <\n", length );
723 break;
724 case CHR('p','r','e','p'):
725 info->prep_start = offset;
726 info->prep_len = length;
727 break;
728 case CHR('f','p','g','m'):
729 info->fpgm_start = offset;
730 info->fpgm_len = length;
731 break;
732 }
733 }
734 if ( info->encoding_start==0 || info->glyph_start==0 || info->head_start==0 ||
735 info->hhea_start==0 || info->hmetrics_start==0 || info->glyphlocations_start == 0 ||
736 info->maxp_start==0 || info->copyright_start==0 || info->postscript_start==0 ||
737 info->os2_start==0 ) {
738 fprintf( stderr, "Required table(s) missing: " );
739 if ( info->encoding_start==0 ) fprintf(stderr,"cmap ");
740 if ( info->glyph_start==0 ) fprintf(stderr,"glyf ");
741 if ( info->head_start==0 ) fprintf(stderr,"head ");
742 if ( info->hhea_start==0 ) fprintf(stderr,"hhea ");
743 if ( info->hmetrics_start==0 ) fprintf(stderr,"hmtx ");
744 if ( info->glyphlocations_start==0 ) fprintf(stderr,"loca ");
745 if ( info->maxp_start==0 ) fprintf(stderr,"maxp ");
746 if ( info->copyright_start==0 ) fprintf(stderr,"name ");
747 if ( info->postscript_start==0 ) fprintf(stderr,"post ");
748 if ( info->os2_start==0 ) fprintf(stderr,"OS/2");
749 fprintf(stderr,"\n");
750 exit(1);
751 }
752 readttfhead(ttf,info);
753 readttfhhea(ttf,info);
754 readttfpost(ttf,info);
755 readttfencodings(ttf,info);
756 readttflocations(ttf,info);
757 readttfwidths(ttf,info);
758 return( 1 );
759 }
760
761 static char *glyph2name(int glyph, struct ttfinfo *info) {
762 char buffer[20];
763
764 if ( info->glyph_names!=NULL )
765 if ( info->glyph_names[glyph]!=NULL )
766 return( info->glyph_names[glyph] );
767 sprintf( buffer,"glyph%d", glyph);
768 return( strdup(buffer));
769 }
770
771 static int name2glyph(char *name, struct ttfinfo *info) {
772 int i;
773
774 if ( info->glyph_names!=NULL ) {
775 for ( i=0; i<info->glyph_cnt; ++i ) if ( info->glyph_names[i]!=NULL )
776 if ( strcmp(name,info->glyph_names[i])==0 )
777 return( i );
778 }
779 fprintf( stderr, "Could not find a glyph named %s\n", name );
780 exit(1);
781 return( -1 );
782 }
783
784 static void DumpEpsHeader(FILE *eps, struct ttfinfo *info, int glyph,
785 double xmin, double ymin, double xmax, double ymax ) {
786 time_t now;
787 struct tm *tm;
788
789 fprintf( eps, "%%!PS-Adobe-3.0 EPSF-3.0\n" );
790 fprintf( eps, "%%%%BoundingBox: %g %g %g %g\n", xmin, ymin, xmax, ymax );
791 fprintf( eps, "%%%%Pages: 0\n" );
792 fprintf( eps, "%%%%Title: Glyph %d ", glyph );
793 if ( info->glyph_names!=NULL && info->glyph_names[glyph]!=NULL )
794 fprintf( eps, " Name: %s", info->glyph_names[glyph]);
795 if ( info->glyph_unicode!=NULL && info->glyph_unicode[glyph]!=0 )
796 fprintf(eps, " Unicode %04x", info->glyph_unicode[glyph]);
797 fprintf( eps, "\n" );
798 fprintf( eps, "%%%%Creator: ttf2eps\n" );
799 now = GetTime();
800 if (!getenv("SOURCE_DATE_EPOCH")) {
801 tm = localtime(&now);
802 } else {
803 tm = gmtime(&now);
804 }
805 fprintf( eps, "%%%%CreationDate: %d:%02d %d-%d-%d\n", tm->tm_hour, tm->tm_min,
806 tm->tm_mday, tm->tm_mon+1, 1900+tm->tm_year );
807 fprintf( eps, "%%%%EndComments\n" );
808 fprintf( eps, "%%%%EndProlog\n" );
809 fprintf( eps, "%%Number of units in Em: %d\n", info->emsize );
810 fprintf( eps, "%%Ascent: %d\n", info->ascent );
811 fprintf( eps, "%%Descent: %d\n", info->descent );
812 if ( info->glyph_widths!=NULL )
813 fprintf( eps, "%%Character Width %d\n", info->glyph_widths[glyph]);
814 if ( info->glyph_names!=NULL && info->glyph_names[glyph]!=NULL )
815 fprintf( eps, "%%%%Page: \"%s\" 1\n", info->glyph_names[glyph] );
816 else
817 fprintf( eps, "%%%%Page: \"Glyph%d\" 1\n", glyph );
818 }
819
820 static void DoDumpGlyph(FILE *ttf, FILE *eps, struct ttfinfo *info, int glyph,
821 double trans[6], int top);
822
823 #define _ARGS_ARE_WORDS 1
824 #define _ARGS_ARE_XY 2
825 #define _ROUND 4 /* to grid? What's that? */
826 #define _SCALE 8
827 #define _XY_SCALE 0x40
828 #define _MATRIX 0x80
829 #define _MORE 0x20
830 #define _INSTR 0x100
831 #define _MY_METRICS 0x200
832
833
834 #define _On_Curve 1
835 #define _X_Short 2
836 #define _Y_Short 4
837 #define _Repeat 8
838 #define _X_Same 0x10
839 #define _Y_Same 0x20
840
841 static void DumpCompositGlyph(FILE *ttf,FILE *eps,struct ttfinfo *info,double trans[6]) {
842 int flags, arg1, arg2;
843 int sub_glyph;
844 int oldpos;
845 double transform[6], restrans[6];
846
847 do {
848 flags = getushort(ttf);
849 sub_glyph = getushort(ttf);
850 transform[0] = transform[3] = 1;
851 transform[1] = transform[2] = transform[4] = transform[5] = 0;
852 if ( flags&_ARGS_ARE_WORDS ) {
853 arg1 = (short) getushort(ttf);
854 arg2 = (short) getushort(ttf);
855 } else {
856 arg1 = (signed char) getc(ttf);
857 arg2 = (signed char) getc(ttf);
858 }
859 if ( flags & _ARGS_ARE_XY ) {
860 transform[4] = arg1;
861 transform[5] = arg2;
862 } else
863 fprintf(stderr,"Don't know how to deal with !(flags&_ARGS_ARE_XY)" );
864 if ( flags & _SCALE )
865 transform[0] = transform[3] = get2dot14(ttf);
866 else if ( flags & _XY_SCALE ) {
867 transform[0] = get2dot14(ttf);
868 transform[3] = get2dot14(ttf);
869 } else if ( flags & _MATRIX ) {
870 transform[0] = get2dot14(ttf);
871 transform[1] = get2dot14(ttf);
872 transform[2] = get2dot14(ttf);
873 transform[3] = get2dot14(ttf);
874 }
875 restrans[0] = transform[0]*trans[0] +
876 transform[1]*trans[2];
877 restrans[1] = transform[0]*trans[1] +
878 transform[1]*trans[3];
879 restrans[2] = transform[2]*trans[0] +
880 transform[3]*trans[2];
881 restrans[3] = transform[2]*trans[1] +
882 transform[3]*trans[3];
883 restrans[4] = transform[4]*trans[0] +
884 transform[5]*trans[2] +
885 trans[4];
886 restrans[5] = transform[4]*trans[1] +
887 transform[5]*trans[3] +
888 trans[5];
889 oldpos = ftell(ttf);
890 DoDumpGlyph(ttf, eps, info, sub_glyph, restrans, 0);
891 fseek(ttf,oldpos,SEEK_SET);
892 } while ( flags&_MORE );
893 /* I'm ignoring many things that I don't understand here */
894 /* Instructions, USE_MY_METRICS, ROUND, what happens if ARGS AREN'T XY */
895 }
896
897 static void TransformPoint(BasePoint *bp,double trans[6]) {
898 double x;
899 x = trans[0]*bp->x+trans[2]*bp->y+trans[4];
900 bp->y=trans[1]*bp->x+trans[3]*bp->y+trans[5];
901 bp->x = x;
902 }
903
904 static void DumpSpline(FILE *eps, BasePoint *from, BasePoint *cp, BasePoint *to) {
905 double b, c, d;
906 BasePoint fn, tp;
907
908 d = from->x;
909 c = 2*cp->x - 2*from->x;
910 b = to->x+from->x-2*cp->x;
911 fn.x = d+c/3;
912 tp.x = fn.x + (c+b)/3;
913
914 d = from->y;
915 c = 2*cp->y - 2*from->y;
916 b = to->y+from->y-2*cp->y;
917 fn.y = d+c/3;
918 tp.y = fn.y + (c+b)/3;
919
920 fprintf( eps, " %g %g %g %g %g %g curveto\n", fn.x, fn.y, tp.x, tp.y,
921 to->x, to->y);
922 }
923
924 static void DumpLine(FILE *eps,BasePoint *from,BasePoint *to ) {
925 fprintf( eps, " %g %g lineto\n", to->x, to->y );
926 }
927
928 static void DumpMoveTo(FILE *eps,BasePoint *to ) {
929 fprintf( eps, " %g %g moveto\n", to->x, to->y );
930 }
931
932 static void DumpSimpleGlyph(FILE *ttf,FILE *eps,struct ttfinfo *info,
933 double trans[6],int path_cnt) {
934 int i, path, start, last_off;
935 unsigned short *endpt = malloc((path_cnt+1)*sizeof(unsigned short));
936 BasePoint mid, last, begin;
937 char *flags;
938 BasePoint *pts;
939 int j, tot, len;
940 int last_pos, first;
941
942 for ( i=0; i<path_cnt; ++i )
943 endpt[i] = getushort(ttf);
944 tot = endpt[path_cnt-1]+1;
945 pts = malloc(tot*sizeof(BasePoint));
946
947 /* ignore instructions */
948 len = getushort(ttf);
949 for ( i=0; i<len; ++i )
950 getc(ttf);
951
952 flags = malloc(tot);
953 for ( i=0; i<tot; ++i ) {
954 flags[i] = getc(ttf);
955 if ( flags[i]&_Repeat ) {
956 int cnt = getc(ttf);
957 for ( j=0; j<cnt; ++j )
958 flags[i+j+1] = flags[i];
959 i += cnt;
960 }
961 }
962 if ( i>tot )
963 fprintf(stderr,"Flag count is wrong (or total is): %d %d", i, tot );
964
965 last_pos = 0;
966 for ( i=0; i<tot; ++i ) {
967 if ( flags[i]&_X_Short ) {
968 int off = getc(ttf);
969 if ( !(flags[i]&_X_Same ) )
970 off = -off;
971 pts[i].x = last_pos + off;
972 } else if ( flags[i]&_X_Same )
973 pts[i].x = last_pos;
974 else
975 pts[i].x = last_pos + (short) getushort(ttf);
976 last_pos = pts[i].x;
977 }
978
979 last_pos = 0;
980 for ( i=0; i<tot; ++i ) {
981 if ( flags[i]&_Y_Short ) {
982 int off = getc(ttf);
983 if ( !(flags[i]&_Y_Same ) )
984 off = -off;
985 pts[i].y = last_pos + off;
986 } else if ( flags[i]&_Y_Same )
987 pts[i].y = last_pos;
988 else
989 pts[i].y = last_pos + (short) getushort(ttf);
990 last_pos = pts[i].y;
991 }
992
993 for ( i=0; i<tot; ++i )
994 TransformPoint(&pts[i],trans);
995
996 for ( path=i=0; path<path_cnt; ++path ) {
997 start = i;
998 first = 1;
999 while ( i<=endpt[path] ) {
1000 if ( flags[i]&_On_Curve ) {
1001 if ( first )
1002 DumpMoveTo(eps,&pts[i]);
1003 else if ( last_off )
1004 DumpSpline(eps,&last,&pts[i-1],&pts[i]);
1005 else
1006 DumpLine(eps,&last,&pts[i]);
1007 first = 0;
1008 last = pts[i];
1009 last_off = 0;
1010 } else if ( last_off ) {
1011 /* two off curve points get a third on curve point created */
1012 /* half-way between them. Now isn't that special */
1013 mid.x = (pts[i].x+pts[i-1].x)/2;
1014 mid.y = (pts[i].y+pts[i-1].y)/2;
1015 if ( first )
1016 DumpMoveTo(eps,&mid);
1017 else
1018 DumpSpline(eps,&last,&pts[i-1],&mid);
1019 first = 0;
1020 last = mid;
1021 /* last_off continues to be true */
1022 } else {
1023 last_off = 1;
1024 }
1025 ++i;
1026 }
1027 if ( !(flags[start]&_On_Curve) && !(flags[i-1]&_On_Curve) ) {
1028 mid.x = (pts[start].x+pts[i-1].x)/2;
1029 mid.y = (pts[start].y+pts[i-1].y)/2;
1030 DumpSpline(eps,&last,&pts[i-1],&mid);
1031 if ( flags[start+1]&_On_Curve )
1032 begin = pts[start+1];
1033 else {
1034 begin.x = (pts[start].x+pts[start+1].x)/2;
1035 begin.y = (pts[start].y+pts[start+1].y)/2;
1036 }
1037 DumpSpline(eps,&mid,&pts[start],&begin);
1038 } else if ( !(flags[i-1]&_On_Curve))
1039 DumpSpline(eps,&last,&pts[i-1],&pts[start]);
1040 else if ( !(flags[start]&_On_Curve) )
1041 DumpSpline(eps,&last,&pts[start],&pts[start+1]);
1042 fprintf( eps,"closepath\n" );
1043 }
1044 free(endpt);
1045 free(flags);
1046 free(pts);
1047 fprintf( eps,"fill\n" );
1048 }
1049
1050 static void DoDumpGlyph(FILE *ttf, FILE *eps, struct ttfinfo *info, int glyph,
1051 double trans[6], int top) {
1052 int start, end;
1053 int path_cnt=-1;
1054 double xmin, xmax, ymin, ymax;
1055
1056 start = info->glyph_offsets[glyph];
1057 end = info->glyph_offsets[glyph+1];
1058 xmin = xmax = ymin = ymax = 0;
1059 if ( start!=end ) {
1060 fseek(ttf,info->glyph_start+start,SEEK_SET);
1061 path_cnt = (short) getushort(ttf);
1062 xmin = (short) getushort(ttf);
1063 ymin = (short) getushort(ttf);
1064 xmax = (short) getushort(ttf);
1065 ymax = (short) getushort(ttf);
1066 }
1067 if ( top ) {
1068 DumpEpsHeader(eps,info,glyph,xmin,ymin,xmax,ymax);
1069 fprintf( eps,"newpath\n" );
1070 } else {
1071 fprintf( eps, "%% Glyph %d ", glyph );
1072 if ( info->glyph_names!=NULL && info->glyph_names[glyph]!=NULL )
1073 fprintf( eps, " Name: %s", info->glyph_names[glyph]);
1074 if ( info->glyph_unicode!=NULL && info->glyph_unicode[glyph]!=0 )
1075 fprintf(eps, " Unicode %04x", info->glyph_unicode[glyph]);
1076 }
1077 if ( start==end )
1078 return;
1079 if ( path_cnt<0 )
1080 DumpCompositGlyph(ttf,eps,info,trans);
1081 else
1082 DumpSimpleGlyph(ttf,eps,info,trans,path_cnt);
1083 }
1084
1085 static void DumpGlyph(FILE *ttf, struct ttfinfo *info, int glyph) {
1086 char *name = glyph2name(glyph,info);
1087 char buffer[300];
1088 FILE *eps;
1089 double trans[6];
1090
1091 sprintf(buffer,"%s.eps", name);
1092 eps = fopen(buffer,"w");
1093 if ( eps==NULL ) {
1094 fprintf( stderr, "Could not open %s for writing\n", buffer );
1095 exit(1);
1096 }
1097 trans[0] = trans[3] = 1;
1098 trans[1] = trans[2] = trans[4] = trans[5] = 0;
1099 DoDumpGlyph(ttf, eps, info, glyph, trans, 1);
1100 fprintf( eps, "%%%%EOF\n" );
1101 free(name);
1102 fclose(eps);
1103 }
1104
1105 int main(int argc, char **argv) {
1106 FILE *ttf;
1107 struct ttfinfo info;
1108 char *filename=NULL;
1109 int doall=0, i, val;
1110 struct spec { enum { is_glyph, is_unicode, is_name } type; char *arg; struct spec *next; } *specs=NULL, *temp;
1111 char *end;
1112 struct dup *dup;
1113
1114 for ( i=1; i<argc; ++i ) {
1115 if ( *argv[i]!='-' ) {
1116 if ( filename == NULL )
1117 filename = argv[i];
1118 else {
1119 fprintf( stderr, "Must have exactly one truetype filename argument\n" );
1120 exit(1);
1121 }
1122 } else if ( strcmp(argv[i],"-all")==0 )
1123 doall=1;
1124 else if ( strcmp(argv[i],"-glyph")==0 ||
1125 strcmp(argv[i],"-unicode")==0 ||
1126 strcmp(argv[i],"-uni")==0 ||
1127 strcmp(argv[i],"-name")==0 ) {
1128 temp = calloc(1,sizeof(struct spec));
1129 temp->type = strcmp(argv[i],"-glyph")==0? is_glyph :
1130 strcmp(argv[i],"-name")==0? is_name : is_unicode;
1131 temp->arg = argv[++i];
1132 temp->next = specs;
1133 specs = temp;
1134 } else {
1135 fprintf( stderr, "Unknown argument %s\n", argv[i]);
1136 fprintf( stderr, "Usage: %s [-all] {-glyph num | -name name | -unicode hex | -uni hex} truetypefile\n", argv[0] );
1137 exit(1);
1138 }
1139 }
1140 if ( filename==NULL ) {
1141 fprintf( stderr, "Must have exactly one truetype filename argument\n" );
1142 return( 1 );
1143 }
1144 ttf = fopen(filename,"rb");
1145 if ( ttf==NULL ) {
1146 fprintf( stderr, "Can't open %s\n", filename);
1147 return( 1 );
1148 }
1149
1150 memset(&info,'\0',sizeof(info));
1151 readttfheader(ttf,&info);
1152 if ( doall )
1153 for ( i=0; i<info.glyph_cnt; ++i )
1154 DumpGlyph(ttf,&info,i);
1155 else {
1156 for ( temp=specs; temp!=NULL; temp=temp->next ) {
1157 if ( temp->type==is_glyph ) {
1158 val = strtol(temp->arg,&end,10);
1159 if ( *end!='\0' || val>=info.glyph_cnt || val<0 ) {
1160 fprintf( stderr, "Bad glyph index %s\n", temp->arg);
1161 exit(1);
1162 }
1163 } else if ( temp->type==is_unicode ) {
1164 val = strtol(temp->arg,&end,16);
1165 if ( *end!='\0' || val<0 || val>65535 || info.glyph_unicode==NULL ) {
1166 fprintf( stderr, "Bad Unicode value %s\n", temp->arg);
1167 exit(1);
1168 }
1169 for ( i=0; i<info.glyph_cnt && info.glyph_unicode[i]!=val; ++i );
1170 if ( i==info.glyph_cnt ) {
1171 for ( dup = info.dups; dup!=NULL && dup->enc!=val; dup = dup->prev );
1172 if ( dup==NULL ) {
1173 fprintf( stderr, "This Unicode value %04X not encoded in this font\n", val );
1174 exit(1);
1175 }
1176 i = dup->glyph;
1177 }
1178 val = i;
1179 } else if ( temp->type==is_name ) {
1180 val = name2glyph(temp->arg,&info);
1181 }
1182 DumpGlyph(ttf,&info,val);
1183 }
1184 }
1185 return( 0 );
1186 }