1 /* Copyright (c) 1997-1998 by Juliusz Chroboczek */
10 struct TableDirectoryEntry
*
11 readDirectory (int fd
, struct OffsetTable
*ot
)
16 struct TableDirectoryEntry
*td
;
17 surely_read (fd
, ot
, sizeof (struct OffsetTable
));
18 FIX_OffsetTable (*ot
);
20 fprintf (stderr
, "%d tables\n", ot
->numTables
);
21 n
= sizeof (struct TableDirectoryEntry
) * ot
->numTables
;
23 surely_read (fd
, td
, n
);
24 for (i
= 0; i
< ot
->numTables
; i
++)
25 FIX_TableDirectoryEntry (td
[i
]);
30 readNamingTable (int fd
)
36 int i
, index
, maxIndex
;
37 struct NameRecord
*records
;
41 position
= surely_lseek (fd
, 0, SEEK_CUR
);
43 surely_read (fd
, &format
, sizeof (USHORT
));
46 error ("Bad TTF file\n");
47 surely_read (fd
, &nrecords
, sizeof (USHORT
));
49 surely_read (fd
, &offset
, sizeof (USHORT
));
51 records
= mymalloc (nrecords
* sizeof (struct NameRecord
));
52 surely_read (fd
, records
, nrecords
* sizeof (struct NameRecord
));
54 for (i
= 0, maxIndex
= -1; i
< nrecords
; i
++)
56 FIX_NameRecord (records
[i
]);
57 index
= records
[i
].offset
+ records
[i
].length
;
58 maxIndex
= maxIndex
> index
? maxIndex
: index
;
60 data
= mymalloc (maxIndex
);
61 surely_lseek (fd
, position
+ offset
, SEEK_SET
);
62 surely_read (fd
, data
, maxIndex
);
64 strings
= mymalloc (8 * sizeof (char *));
65 for (i
= 0; i
< 8; i
++)
68 for (i
= 0; i
< nrecords
; i
++)
71 ((records
[i
].platformID
== 3 && /* Microsoft */
72 records
[i
].encodingID
== 1 && /* UGL */
73 records
[i
].languageID
== 0x0409 && /* US English */
74 records
[i
].nameID
<= 7))
76 strings
[records
[i
].nameID
] = mymalloc (records
[i
].length
/ 2 + 1);
77 unistrncpy (strings
[records
[i
].nameID
],
78 data
+ records
[i
].offset
, records
[i
].length
);
80 fprintf (stderr
, "%d: %s\n", records
[i
].nameID
,
81 strings
[records
[i
].nameID
]);
86 for (i
= 0; i
< nrecords
; i
++)
88 int id
= records
[i
].nameID
;
89 if (records
[i
].platformID
== 1 && /* Apple */
90 records
[i
].encodingID
== 0 && /* 8bit */
95 strings
[id
] = mymalloc (records
[i
].length
+ 1);
97 data
+ records
[i
].offset
, records
[i
].length
);
98 strings
[id
][records
[i
].length
] = 0;
100 fprintf (stderr
, "%d: %s\n", records
[i
].nameID
,
101 strings
[records
[i
].nameID
]);
111 readMaxpTable (int fd
)
118 surely_read (fd
, &data
, sizeof (data
));
119 FIX_Fixed (data
.version
);
120 FIX_UH (data
.nglyphs
);
122 fprintf (stderr
, " version %d.%u\n",
123 data
.version
.mantissa
, data
.version
.fraction
);
128 readHeadTable (int fd
, struct HeadTable
*ht
)
130 surely_read (fd
, ht
, sizeof (struct HeadTable
));
134 fprintf (stderr
, " version %d.%d\n",
135 ht
->version
.mantissa
, ht
->version
.fraction
);
136 fprintf (stderr
, " font revision %d.%d\n",
137 ht
->fontRevision
.mantissa
, ht
->fontRevision
.fraction
);
139 if (ht
->magicNumber
!= 0x5F0F3CF5)
140 error ("Bad magic number in TTF file");
142 fprintf (stderr
, " %d units per Em\n", ht
->unitsPerEm
);
146 readPostTable (int fd
, int nglyphs
, struct PostTable
*pt
,
147 struct GlyphName
**gt
)
150 USHORT
*glyphNameIndex
;
151 struct GlyphName
*glyphNames
;
152 char **glyphNamesTemp
;
156 surely_read (fd
, pt
, sizeof (struct PostTable
));
159 fprintf (stderr
, " format type %d.%u\n",
160 pt
->formatType
.mantissa
, pt
->formatType
.fraction
);
162 switch (pt
->formatType
.mantissa
)
165 return 1; /* MacGlyphEncoding */
167 if (pt
->formatType
.fraction
!= 0)
168 error ("Unsupported `post' table format");
169 surely_read (fd
, &nglyphspost
, sizeof (USHORT
));
170 FIX_UH (nglyphspost
);
171 if (nglyphspost
!= nglyphs
)
172 error ("Inconsistency between `maxp' and `nglyphs' tables!");
174 fprintf (stderr
, " %d glyphs\n", nglyphs
);
175 glyphNameIndex
= mymalloc (sizeof (USHORT
) * nglyphs
);
176 surely_read (fd
, glyphNameIndex
, sizeof (USHORT
) * nglyphs
);
177 glyphNames
= mymalloc (sizeof (struct GlyphName
) * nglyphs
);
178 for (i
= 0, maxIndex
= -1; i
< nglyphs
; i
++)
180 FIX_UH (glyphNameIndex
[i
]);
181 if (glyphNameIndex
[i
] < 258)
183 glyphNames
[i
].type
= 0;
184 glyphNames
[i
].name
.index
= glyphNameIndex
[i
];
189 glyphNames
[i
].type
= 1;
190 index
= glyphNameIndex
[i
] - 258;
191 glyphNames
[i
].name
.index
= index
;
192 maxIndex
= maxIndex
> index
? maxIndex
: index
;
195 free (glyphNameIndex
);
198 glyphNamesTemp
= mymalloc (sizeof (char *) * (maxIndex
+ 1));
199 while (i
<= maxIndex
)
201 surely_read (fd
, &c
, 1);
202 glyphNamesTemp
[i
] = mymalloc (c
+ 1);
203 surely_read (fd
, glyphNamesTemp
[i
], c
);
204 glyphNamesTemp
[i
][c
] = '\0';
206 fprintf (stderr
, " %d: %s\n", i
, glyphNamesTemp
[i
]);
209 for (i
= 0; i
< nglyphs
; i
++)
210 if (glyphNames
[i
].type
== 1)
211 glyphNames
[i
].name
.name
= glyphNamesTemp
[glyphNames
[i
].name
.index
];
212 free (glyphNamesTemp
);
216 return 3; /* no name table */
223 readLocaTable (int fd
, int nglyphs
, int format
)
231 offsets
= mymalloc ((nglyphs
+ 1) * sizeof (USHORT
));
232 surely_read (fd
, offsets
, (nglyphs
+ 1) * sizeof (USHORT
));
233 for (i
= 0; i
<= nglyphs
; i
++)
237 /*NOTREACHED*/ case 1:
240 offsets
= mymalloc ((nglyphs
+ 1) * sizeof (ULONG
));
241 surely_read (fd
, offsets
, (nglyphs
+ 1) * sizeof (ULONG
));
242 for (i
= 0; i
<= nglyphs
; i
++)
246 /*NOTREACHED*/ default:
247 error ("Unknown `loca' table format");
252 readGlyfTable (int fd
, int nglyphs
, int format
, void *loca
)
258 base
= surely_lseek (fd
, 0, SEEK_CUR
);
260 bbox
= mymalloc (nglyphs
* sizeof (struct Box
));
261 for (i
= 0; i
< nglyphs
; i
++)
264 offset
= 2 * ((USHORT
*) loca
)[i
];
266 offset
= ((ULONG
*) loca
)[i
];
267 surely_lseek (fd
, base
+ offset
+ sizeof (SHORT
), SEEK_SET
);
268 surely_read (fd
, bbox
+ i
, sizeof (struct Box
));
275 readHmtxTable (int fd
, int nummetrics
)
277 longHorMetric
*metrics
;
280 metrics
= mymalloc (nummetrics
* sizeof (longHorMetric
));
281 surely_read (fd
, metrics
, nummetrics
* sizeof (longHorMetric
));
282 for (i
= 0; i
< nummetrics
; i
++)
284 FIX_longHorMetric (metrics
[i
]);
290 readHheaTable (int fd
)
292 struct HheaTable
*hhea
;
293 hhea
= mymalloc (sizeof (struct HheaTable
));
294 surely_read (fd
, hhea
, sizeof (struct HheaTable
));
295 FIX_HheaTable (*hhea
);
297 fprintf (stderr
, " version %d.%u\n",
298 hhea
->version
.mantissa
, hhea
->version
.fraction
);
299 if (hhea
->metricDataFormat
!= 0)
300 error ("Unknown metric data format");
305 readKernTable (int fd
, int **nkep
, struct KernEntry0
***kep
)
308 struct KernSubTableHeader ksth
;
309 struct KernSubTable0 kst
;
312 struct KernEntry0
**ke
;
314 surely_read (fd
, &kt
, sizeof (struct KernTable
));
318 fprintf (stderr
, " version %d\n", kt
.version
);
319 fprintf (stderr
, " %d subtables\n", kt
.nTables
);
321 nke
= mymalloc (kt
.nTables
* sizeof (int));
322 ke
= mymalloc (kt
.nTables
* sizeof (struct KernEntry0
*));
324 for (i
= 0; i
< kt
.nTables
; i
++)
326 surely_read (fd
, &ksth
, sizeof (struct KernSubTableHeader
));
327 FIX_KernSubTableHeader (ksth
);
329 fprintf (stderr
, " analyzing subtable %d, version %d... ",
331 if ((ksth
.coverage
& kernHorizontal
) &&
332 !(ksth
.coverage
& kernMinimum
) &&
333 !(ksth
.coverage
& kernCrossStream
) &&
334 (kernFormat (ksth
.coverage
) == 0))
336 surely_read (fd
, &kst
, sizeof (struct KernSubTable0
));
337 FIX_KernSubTable0 (kst
);
339 fprintf (stderr
, "reading %d entries.\n", kst
.nPairs
);
341 ke
[i
] = mymalloc (kst
.nPairs
* sizeof (struct KernEntry0
));
342 surely_read (fd
, ke
[i
], kst
.nPairs
* sizeof (struct KernEntry0
));
343 for (j
= 0; j
< kst
.nPairs
; j
++)
344 FIX_KernEntry0 (ke
[i
][j
]);
349 fprintf (stderr
, "skipping.\n");
350 surely_lseek (fd
, ksth
.length
- sizeof (struct KernSubTableHeader
),