1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#include <kpathsea/getopt.h>
#else
#include <unistd.h>
#endif
#include "config.h"
#include "ttf.h"
#include "ttfutil.h"
#include "ttc.h"
#ifdef MEMCHECK
#include <dmalloc.h>
#endif
/* $Id: ttfdump.c,v 1.2 1998/07/04 13:17:52 werner Exp $ */
#ifndef lint
static char vcid[] = "$Id: ttfdump.c,v 1.2 1998/07/04 13:17:52 werner Exp $";
#endif /* lint */
#define MAXLEN 256
#define ALL_GLYF -1
#define NO_GLYF -2
static char ttfname[MAXLEN], dumpname[MAXLEN];
static TTFontPtr font;
static void print_ttc(TTCHeaderPtr ttc, FILE *out);
static void print_prologue(FILE *out);
static void print_offset(FILE *out);
static void print_dir(FILE *out);
static void print_all_tables(FILE *dp_file);
static void print_table(FILE *out, char *tablename);
static void print_cmap(FILE *out);
static void print_glyf(FILE *out, ULONG index);
static void print_all_glyfs(FILE *out);
static void print_head(FILE *out);
static void print_hhea(FILE *out);
static void print_hmtx(FILE *out);
static void print_loca(FILE *out);
static void print_maxp(FILE *out);
static void print_name(FILE *out);
static void print_post(FILE *out);
static void print_os2(FILE *out);
static void print_cvt(FILE *out);
static void print_fpgm(FILE *out);
static void print_gasp(FILE *out);
static void print_hdmx(FILE *out);
static void print_kern(FILE *out);
static void print_ltsh(FILE *out);
static void print_pclt(FILE *out);
static void print_prep(FILE *out);
static void print_vdmx(FILE *out);
static void print_vhea(FILE *out);
static void print_vmtx(FILE *out);
static void dialog (int argc, char *argv[]);
static void usage (void);
static void add_suffix (char *name, char *suffix);
#if !defined(EXIT_FAILURE)
#define EXIT_FAILURE 1
#endif
#if !defined(EXIT_SUCCESS)
#define EXIT_SUCCESS 0
#endif
/* strdup() is not available on all platforms; this version has been
* contributed by Nelson Beebe. */
char *
Strdup(const char *s)
{
char *p;
p = malloc(((s == (const char *)NULL) ? 0 : strlen (s)) + 1);
if (p == (char *)NULL)
{
(void)fprintf(stderr, "Out of memory in Strdup()\n");
exit(EXIT_FAILURE);
}
return strcpy(p, s);
}
int
main(int argc, char *argv[])
{
FILE *tt_file, *dp_file;
TTCHeaderPtr ttc;
int c;
extern int optind;
extern char *optarg;
char *options, *value;
char *tablename = NULL;
/* print all glyphs by default */
int glyphnum = ALL_GLYF, collection = 0;
if (argc < 2)
{
usage();
exit(EXIT_FAILURE);
}
while ((c = getopt(argc, argv, "t:g:c:i:o:h")) != EOF)
{
switch (c)
{
case 't':
tablename = Strdup(optarg);
break;
case 'g':
if (strcmp(optarg, "x") == 0)
glyphnum = NO_GLYF;
else
glyphnum = atoi(optarg);
break;
case 'c':
collection = atoi(optarg);
break;
case 'i':
strcpy(ttfname, optarg);
break;
case 'o':
strcpy(dumpname, optarg);
break;
case 'h':
case '?':
usage();
exit(EXIT_FAILURE);
break;
}
}
/* processing ttf file if -i flag is not given */
if (*ttfname == 0 && optind < argc)
{
strcpy(ttfname, argv[optind]);
}
/* processing dumping file if -o flag is not given */
if (*dumpname == 0)
{
if (optind + 1 < argc)
{
/* no -o flag but dumping filename specified */
strcpy(dumpname, argv[optind + 1]);
}
else
strcpy(dumpname, "-");
}
if (!strcmp(dumpname, "-"))
{
dp_file = stdout;
}
else if ((dp_file = fopen(dumpname, "wt")) == NULL)
{
fprintf(stderr, "Can't open dumping file\n");
exit(EXIT_FAILURE);
}
if (strstr (ttfname, "ttc") != NULL)
{
ttc = ttfLoadTTCHeader(ttfname);
if (collection < ttc->DirCount)
font = ttc->font + collection;
else
fprintf (stderr, "TrueType collection number too large\n"
"should between 0 and %d\n", ttc->DirCount - 1);
}
else
font = ttfInitFont(ttfname);
if (font == NULL)
exit(EXIT_FAILURE);
print_prologue(dp_file);
if (strstr(ttfname, "ttc") != NULL)
{
print_ttc(ttc, dp_file);
}
print_offset(dp_file);
print_dir(dp_file);
if (tablename == NULL)
{
/* no table specified */
print_all_tables(dp_file);
if (glyphnum == ALL_GLYF)
print_all_glyfs(dp_file);
else if (glyphnum == NO_GLYF)
;
else
print_glyf(dp_file, glyphnum);
}
else
{
/* table specified */
print_table(dp_file, tablename);
if (!strcmp(tablename, "glyf"))
{
if (glyphnum == ALL_GLYF)
print_all_glyfs(dp_file);
else if (glyphnum == NO_GLYF)
;
else
print_glyf(dp_file, glyphnum);
}
}
if (strstr(ttfname, "ttc") != NULL)
{
ttfFreeTTCFont(ttc);
}
else
{
ttfFreeFont(font);
}
exit(EXIT_SUCCESS);
}
static void
print_table(FILE *out, char *tablename)
{
if (!strcmp(tablename, "cmap"))
print_cmap(out);
if (!strcmp(tablename, "head"))
print_head(out);
if (!strcmp(tablename, "hhea"))
print_hhea(out);
if (!strcmp(tablename, "hmtx"))
print_hmtx(out);
if (!strcmp(tablename, "loca"))
print_loca(out);
if (!strcmp(tablename, "maxp"))
print_maxp(out);
if (!strcmp(tablename, "name"))
print_name(out);
if (!strcmp(tablename, "post"))
print_post(out);
if (!strcmp(tablename, "OS/2"))
print_os2(out);
if (!strcmp(tablename, "cvt"))
print_cvt(out);
if (!strcmp(tablename, "fpgm"))
print_fpgm(out);
if (!strcmp(tablename, "gasp"))
print_gasp(out);
if (!strcmp(tablename, "hdmx"))
print_hdmx(out);
if (!strcmp(tablename, "kern"))
print_kern(out);
if (!strcmp(tablename, "LTSH"))
print_ltsh(out);
if (!strcmp(tablename, "prep"))
print_prep(out);
if (!strcmp(tablename, "VDMX"))
print_vdmx(out);
if (!strcmp(tablename, "vhea"))
print_vhea(out);
if (!strcmp(tablename, "vmtx"))
print_vmtx(out);
}
static void
print_all_tables(FILE *dp_file)
{
print_cmap(dp_file);
print_head(dp_file);
print_hhea(dp_file);
print_hmtx(dp_file);
print_loca(dp_file);
print_maxp(dp_file);
print_name(dp_file);
print_post(dp_file);
print_os2(dp_file);
/*optional tables */
print_cvt(dp_file);
print_gasp(dp_file);
print_hdmx(dp_file);
print_kern(dp_file);
print_ltsh(dp_file);
print_pclt(dp_file);
print_vdmx(dp_file);
print_vhea(dp_file);
print_vmtx(dp_file);
}
static void
print_ttc(TTCHeaderPtr ttc, FILE *out)
{
int i, b[2];
FixedSplit(ttc->version, b);
fprintf(out, "TrueType Collection Header\n");
fprintf(out, "--------------------------\n");
fprintf(out, "TTC version: \t\t %d.%d\n", b[1], b[0]);
fprintf(out, "Number of fonts:\t %d\n", ttc->DirCount);
for (i = 0; i < ttc->DirCount; i++)
fprintf(out, "Offset of Directory #%d:\t %d\n", i, ttc->offset[i]);
fprintf(out, "\n");
}
static void
print_prologue(FILE *out)
{
fprintf(out, "True Type Font File Dumper: v 0.5.5\n");
fprintf(out, "Copyright 1996-1998 ollie@ms1.hinet.net \n");
fprintf(out, "Dumping File:%s\n\n\n", font->ttfname);
}
static void
print_offset(FILE *out)
{
fprintf(out, "Offset Table \n");
fprintf(out, "------------ \n");
fprintf(out, "\t sfnt version:\n");
fprintf(out, "\t number of tables: %d\n", font->numTables);
}
static void
print_dir(FILE *out)
{
int i;
for (i = 0; i < font->numTables; i++)
{
fprintf(out, " %2d. ", i);
ttfPrintTableDir(out, font->dir + i);
}
fprintf(out, "\n");
}
static void
print_cmap(FILE *out)
{
ttfPrintCMAP(out, font->cmap);
fprintf(out, "\n");
}
static void
print_glyf(FILE *out, ULONG index)
{
GLYFPtr glyf;
glyf = ttfLoadGlyphIndex(font, index);
fprintf(out, "Glyph %6d.\n", index);
ttfPrintGLYF(out, glyf);
}
static void
print_all_glyfs(FILE *out)
{
int i;
GLYFPtr glyf;
for (i = 0; i < font->maxp->numGlyphs; i++)
{
glyf = ttfLoadGlyphIndex(font, i);
fprintf(out, "Glyph %6d.\n", i);
ttfPrintGLYF(out, glyf);
}
}
static void
print_head(FILE *out)
{
ttfPrintHEAD(out, font->head);
fprintf(out, "\n");
}
static void
print_hhea(FILE *out)
{
ttfPrintHHEA(out, font->hhea);
fprintf(out, "\n");
}
static void
print_hmtx(FILE *out)
{
/* because of interdependencies between tables
* we have to init hhea and maxp before hmtx */
ttfPrintHMTX(out, font->hmtx);
fprintf(out, "\n");
}
static void
print_loca(FILE *out)
{
/* because of interdependencies between tables
* we have to init head and maxp before loca */
ttfPrintLOCA(out, font->loca);
fprintf(out, "\n");
}
static void
print_maxp(FILE *out)
{
ttfPrintMAXP(out, font->maxp);
fprintf(out, "\n");
}
static void
print_name(FILE *out)
{
ttfPrintNAME(out, font->name);
fprintf(out, "\n");
}
static void
print_post(FILE *out)
{
ttfPrintPOST(out, font->post);
}
static void
print_os2(FILE *out)
{
ttfPrintOS2(out, font->os2);
}
static void
print_cvt(FILE *out)
{
if (font->cvt != NULL)
ttfPrintCVT(out, font->cvt, font->cvtLength);
}
static void
print_fpgm(FILE *out)
{
if (font->fpgm != NULL)
ttfPrintFPGM(out, font->fpgm, font->fpgmLength);
}
static void
print_gasp(FILE *out)
{
if (font->gasp != NULL)
ttfPrintGASP(out, font->gasp);
}
static void
print_hdmx(FILE *out)
{
if (font->hdmx != NULL)
ttfPrintHDMX(out, font->hdmx);
}
static void
print_kern(FILE *out)
{
if (font->kern != NULL)
ttfPrintKERN(out, font->kern);
}
static void
print_ltsh(FILE *out)
{
if (font->ltsh != NULL)
ttfPrintLTSH(out, font->ltsh);
}
static void
print_pclt(FILE *out)
{
if (font->pclt != NULL)
ttfPrintPCLT(out, font->pclt);
}
static void
print_prep(FILE *out)
{
if (font->prep != NULL)
ttfPrintPREP(out, font->prep, font->prepLength);
}
static void
print_vdmx(FILE *out)
{
if (font->vdmx != NULL)
ttfPrintVDMX(out, font->vdmx);
}
static void
print_vhea(FILE *out)
{
if (font->vhea != NULL)
ttfPrintVHEA(out, font->vhea);
}
static void
print_vmtx(FILE *out)
{
if (font->vmtx != NULL)
ttfPrintVMTX(out, font->vmtx);
}
static void
dialog(int argc, char *argv[])
{
if (--argc < 1)
usage();
strcpy(ttfname, *++argv);
add_suffix(ttfname, "ttf");
if (--argc < 1)
strcpy(dumpname, "-");
else
strcpy(dumpname, *++argv);
}
static void
usage(void)
{
fprintf(stderr, "Usage: ttfdump ttfname dumpname [options]\n");
exit(EXIT_FAILURE);
}
static void
add_suffix(char *name, char *suffix)
{
int haveext = 0;
if (name && strcmp(name, "-"))
{
while (*name)
{
if (*name == '/')
haveext = 0;
else if (*name == '.')
haveext = 1;
name++;
}
if (!haveext)
{
*name++ = '.';
strcpy(name, suffix);
}
}
}
/* end of ttfdump.c */
|