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