Debian Codegraph @

texk/ttfdump/libttf/tabledir.c

raw
1 /* loadtable.c
2 * Load the tables of the Table Directory of a True Type font file
3 * Copyright (C) 1996 Li-Da Lho, All right reserved
4 */
5 #ifdef HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include "ttf.h"
13 #include "ttfutil.h"
14
15 /* $Id: tabledir.c,v 1.2 1998/07/04 13:17:51 werner Exp $ */
16
17 static void ttfLoadTableDir(FILE *fp,TableDirPtr p,ULONG offset);
18
19 #define Offset 12 /* start point of table dir */
20
21 void ttfInitTableDir(TTFontPtr font, ULONG offset)
22 {
23 int i,pos; /* table directory starts form position 12 */
24
25 font->dir = XCALLOC (font->numTables, TableDir);
26 pos = Offset + offset;
27 for (i=0;i<font->numTables;i++)
28 {
29 ttfLoadTableDir(font->fp,font->dir+i,pos);
30 pos += sizeof(TableDir);
31 }
32 }
33
34 static void ttfLoadTableDir(FILE *fp,TableDirPtr p,ULONG offset)
35 {
36 xfseek(fp, offset, SEEK_SET, "ttfLoadTableDir");
37
38 p -> tag = ttfGetULONG(fp);
39 p -> checksum = ttfGetULONG(fp);
40 p -> offset = ttfGetULONG(fp);
41 p -> length = ttfGetULONG(fp);
42 }
43
44 void ttfPrintTableDir(FILE *fp,TableDirPtr p)
45 {
46 fprintf(fp,"'%s' - checksum = 0x%08x, offset = 0x%08x, len = %9u\n",
47 TagToStr(p->tag),p->checksum,p->offset,p->length);
48 }
49
50 void ttfFreeTableDir(TableDirPtr p)
51 {
52 free(p);
53 }
54
55 /* ttfLookUpTableDir
56 * lookup the specified table in an array of TableDir
57 * Linear search at present, should change to binary search in the
58 * future to improve effcience
59 * The tag name are sorted in ascent order in ttf file.
60 */
61 TableDirPtr ttfLookUpTableDir(ULONG tagname,TTFontPtr font)
62 {
63 USHORT i,n = font->numTables;
64 TableDirPtr ptable = font->dir;
65
66 for (i=0;i<n;i++,ptable++)
67 {
68 if (ptable->tag == tagname)
69 return ptable;
70 }
71 return NULL;
72 }
73
74 #if 0
75 /* Not used */
76 /* calculate table check sum */
77 /* can't be done until the data abstraction have been finished */
78 /* not finished yet */
79 ULONG ttfCalcTableCheckSum(ULONG tagname,TTFontPtr font)
80 {
81 ULONG sum = 0L,Length;
82 TableDirPtr ptable;
83
84 Length = ((ptable->length+3) & ~3) / sizeof(ULONG);
85
86 ptable = ttfLookUpTableDir(tagname, font);
87 xfseek(font->fp, ptable->offset, SEEK_SET, "ttfCalcTableCheckSum");
88
89 while (Length--)
90 {
91 sum += ttfGetULONG(font->fp);
92 }
93
94 return sum;
95 }
96 #endif