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 ttf_error ("Format should be 0\n");
48 surely_read (fd, &nrecords, sizeof (USHORT));
50 surely_read (fd, &offset, sizeof (USHORT));
52 records = mymalloc (nrecords * sizeof (struct NameRecord));
53 surely_read (fd, records, nrecords * sizeof (struct NameRecord));
55 for (i = 0, maxIndex = -1; i < nrecords; i++)
57 FIX_NameRecord (records[i]);
58 index = records[i].offset + records[i].length;
59 maxIndex = maxIndex > index ? maxIndex : index;
61 data = mymalloc (maxIndex);
62 surely_lseek (fd, position + offset, SEEK_SET);
63 surely_read (fd, data, maxIndex);
65 strings = mymalloc (8 * sizeof (char *));
66 for (i = 0; i < 8; i++)
69 for (i = 0; i < nrecords; i++)
72 ((records[i].platformID == 3 && /* Microsoft */
73 records[i].encodingID == 1 && /* UGL */
74 records[i].languageID == 0x0409 && /* US English */
75 records[i].nameID <= 7))
77 strings[records[i].nameID] = mymalloc (records[i].length / 2 + 1);
78 unistrncpy (strings[records[i].nameID],
79 data + records[i].offset, records[i].length);
81 fprintf (stderr, "%d: %s\n", records[i].nameID,
82 strings[records[i].nameID]);
87 for (i = 0; i < nrecords; i++)
89 int id = records[i].nameID;
90 if (records[i].platformID == 1 && /* Apple */
91 records[i].encodingID == 0 && /* 8bit */
96 strings[id] = mymalloc (records[i].length + 1);
98 data + records[i].offset, records[i].length);
99 strings[id][records[i].length] = 0;
101 fprintf (stderr, "%d: %s\n", records[i].nameID,
102 strings[records[i].nameID]);
112 readMaxpTable (int fd)
119 surely_read (fd, &data, sizeof (data));
120 FIX_Fixed (data.version);
121 FIX_UH (data.nglyphs);
123 fprintf (stderr, " version %d.%u\n",
124 data.version.mantissa, data.version.fraction);
129 readHeadTable (int fd, struct HeadTable *ht)
131 surely_read (fd, ht, sizeof (struct HeadTable));
135 fprintf (stderr, " version %d.%d\n",
136 ht->version.mantissa, ht->version.fraction);
137 fprintf (stderr, " font revision %d.%d\n",
138 ht->fontRevision.mantissa, ht->fontRevision.fraction);
140 if (ht->magicNumber != 0x5F0F3CF5)
141 ttf_error ("Bad magic number");
143 fprintf (stderr, " %d units per Em\n", ht->unitsPerEm);
147 readPostTable (int fd, int nglyphs, struct PostTable *pt,
148 struct GlyphName **gt)
151 USHORT *glyphNameIndex;
152 struct GlyphName *glyphNames;
153 char **glyphNamesTemp;
157 surely_read (fd, pt, sizeof (struct PostTable));
160 fprintf (stderr, " format type %d.%u\n",
161 pt->formatType.mantissa, pt->formatType.fraction);
163 switch (pt->formatType.mantissa)
166 return 1; /* MacGlyphEncoding */
168 if (pt->formatType.fraction != 0)
169 ttf_error ("Unsupported `post' table format");
170 surely_read (fd, &nglyphspost, sizeof (USHORT));
171 FIX_UH (nglyphspost);
172 if (nglyphspost != nglyphs)
173 ttf_error ("Inconsistency between `maxp' and `nglyphs' tables!");
175 fprintf (stderr, " %d glyphs\n", nglyphs);
176 glyphNameIndex = mymalloc (sizeof (USHORT) * nglyphs);
177 surely_read (fd, glyphNameIndex, sizeof (USHORT) * nglyphs);
178 glyphNames = mymalloc (sizeof (struct GlyphName) * nglyphs);
179 for (i = 0, maxIndex = -1; i < nglyphs; i++)
181 FIX_UH (glyphNameIndex[i]);
182 if (glyphNameIndex[i] < 258)
184 glyphNames[i].type = 0;
185 glyphNames[i].name.index = glyphNameIndex[i];
190 glyphNames[i].type = 1;
191 index = glyphNameIndex[i] - 258;
192 glyphNames[i].name.index = index;
193 maxIndex = maxIndex > index ? maxIndex : index;
196 free (glyphNameIndex);
199 glyphNamesTemp = mymalloc (sizeof (char *) * (maxIndex + 1));
200 while (i <= maxIndex)
202 surely_read (fd, &c, 1);
203 glyphNamesTemp[i] = mymalloc (c + 1);
204 surely_read (fd, glyphNamesTemp[i], c);
205 glyphNamesTemp[i][c] = '\0';
207 fprintf (stderr, " %d: %s\n", i, glyphNamesTemp[i]);
210 for (i = 0; i < nglyphs; i++)
211 if (glyphNames[i].type == 1)
212 glyphNames[i].name.name = glyphNamesTemp[glyphNames[i].name.index];
213 free (glyphNamesTemp);
217 return 3; /* no name table */
224 readLocaTable (int fd, int nglyphs, int format)
232 offsets = mymalloc ((nglyphs + 1) * sizeof (USHORT));
233 surely_read (fd, offsets, (nglyphs + 1) * sizeof (USHORT));
234 for (i = 0; i <= nglyphs; i++)
238 /*NOTREACHED*/ case 1:
241 offsets = mymalloc ((nglyphs + 1) * sizeof (ULONG));
242 surely_read (fd, offsets, (nglyphs + 1) * sizeof (ULONG));
243 for (i = 0; i <= nglyphs; i++)
247 /*NOTREACHED*/ default:
248 ttf_error ("Unknown `loca' table format");
253 readGlyfTable (int fd, int nglyphs, int format, void *loca)
259 base = surely_lseek (fd, 0, SEEK_CUR);
261 bbox = mymalloc (nglyphs * sizeof (struct Box));
262 for (i = 0; i < nglyphs; i++)
265 offset = 2 * ((USHORT *) loca)[i];
267 offset = ((ULONG *) loca)[i];
268 surely_lseek (fd, base + offset + sizeof (SHORT), SEEK_SET);
269 surely_read (fd, bbox + i, sizeof (struct Box));
276 readHmtxTable (int fd, int nummetrics)
278 longHorMetric *metrics;
281 metrics = mymalloc (nummetrics * sizeof (longHorMetric));
282 surely_read (fd, metrics, nummetrics * sizeof (longHorMetric));
283 for (i = 0; i < nummetrics; i++)
285 FIX_longHorMetric (metrics[i]);
291 readHheaTable (int fd)
293 struct HheaTable *hhea;
294 hhea = mymalloc (sizeof (struct HheaTable));
295 surely_read (fd, hhea, sizeof (struct HheaTable));
296 FIX_HheaTable (*hhea);
298 fprintf (stderr, " version %d.%u\n",
299 hhea->version.mantissa, hhea->version.fraction);
300 if (hhea->metricDataFormat != 0)
301 ttf_error ("Unknown metric data format");
306 readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
309 struct KernSubTableHeader ksth;
310 struct KernSubTable0 kst;
313 struct KernEntry0 **ke;
315 surely_read (fd, &kt, sizeof (struct KernTable));
319 fprintf (stderr, " version %d\n", kt.version);
320 fprintf (stderr, " %d subtables\n", kt.nTables);
322 nke = mymalloc (kt.nTables * sizeof (int));
323 ke = mymalloc (kt.nTables * sizeof (struct KernEntry0 *));
325 for (i = 0; i < kt.nTables; i++)
327 surely_read (fd, &ksth, sizeof (struct KernSubTableHeader));
328 FIX_KernSubTableHeader (ksth);
330 fprintf (stderr, " analyzing subtable %d, version %d... ",
332 if ((ksth.coverage & kernHorizontal) &&
333 !(ksth.coverage & kernMinimum) &&
334 !(ksth.coverage & kernCrossStream) &&
335 (kernFormat (ksth.coverage) == 0))
337 surely_read (fd, &kst, sizeof (struct KernSubTable0));
338 FIX_KernSubTable0 (kst);
340 fprintf (stderr, "reading %d entries.\n", kst.nPairs);
342 ke[i] = mymalloc (kst.nPairs * sizeof (struct KernEntry0));
343 surely_read (fd, ke[i], kst.nPairs * sizeof (struct KernEntry0));
344 for (j = 0; j < kst.nPairs; j++)
345 FIX_KernEntry0 (ke[i][j]);
350 fprintf (stderr, "skipping.\n");
351 surely_lseek (fd, ksth.length - sizeof (struct KernSubTableHeader),