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++)
70 if (records[i].platformID == 3 && /* Microsoft */
71 records[i].encodingID == 1 && /* UGL */
72 records[i].languageID == 0x0409 && /* US English */
73 records[i].nameID <= 7)
75 strings[records[i].nameID] = mymalloc (records[i].length / 2 + 1);
76 unistrncpy (strings[records[i].nameID],
77 data + records[i].offset, records[i].length);
79 fprintf (stderr, "%d: %s\n", records[i].nameID,
80 strings[records[i].nameID]);
89 readMaxpTable (int fd)
96 surely_read (fd, &data, sizeof (data));
97 FIX_Fixed (data.version);
98 FIX_UH (data.nglyphs);
100 fprintf (stderr, " version %d.%u\n",
101 data.version.mantissa, data.version.fraction);
106 readHeadTable (int fd, struct HeadTable *ht)
108 surely_read (fd, ht, sizeof (struct HeadTable));
112 fprintf (stderr, " version %d.%d\n",
113 ht->version.mantissa, ht->version.fraction);
114 fprintf (stderr, " font revision %d.%d\n",
115 ht->fontRevision.mantissa, ht->fontRevision.fraction);
117 if (ht->magicNumber != 0x5F0F3CF5)
118 error ("Bad magic number in TTF file");
120 fprintf (stderr, " %d units per Em\n", ht->unitsPerEm);
124 readPostTable (int fd, int nglyphs, struct PostTable *pt,
125 struct GlyphName **gt)
128 USHORT *glyphNameIndex;
129 struct GlyphName *glyphNames;
130 char **glyphNamesTemp;
134 surely_read (fd, pt, sizeof (struct PostTable));
137 fprintf (stderr, " format type %d.%u\n",
138 pt->formatType.mantissa, pt->formatType.fraction);
140 switch (pt->formatType.mantissa)
143 return 1; /* MacGlyphEncoding */
145 if (pt->formatType.fraction != 0)
146 error ("Unsupported `post' table format");
147 surely_read (fd, &nglyphspost, sizeof (USHORT));
148 FIX_UH (nglyphspost);
149 if (nglyphspost != nglyphs)
150 error ("Inconsistency between `maxp' and `nglyphs' tables!");
152 fprintf (stderr, " %d glyphs\n", nglyphs);
153 glyphNameIndex = mymalloc (sizeof (USHORT) * nglyphs);
154 surely_read (fd, glyphNameIndex, sizeof (USHORT) * nglyphs);
155 glyphNames = mymalloc (sizeof (struct GlyphName) * nglyphs);
156 for (i = 0, maxIndex = -1; i < nglyphs; i++)
158 FIX_UH (glyphNameIndex[i]);
159 if (glyphNameIndex[i] < 258)
161 glyphNames[i].type = 0;
162 glyphNames[i].name.index = glyphNameIndex[i];
167 glyphNames[i].type = 1;
168 index = glyphNameIndex[i] - 258;
169 glyphNames[i].name.index = index;
170 maxIndex = maxIndex > index ? maxIndex : index;
173 free (glyphNameIndex);
176 glyphNamesTemp = mymalloc (sizeof (char *) * (maxIndex + 1));
177 while (i <= maxIndex)
179 surely_read (fd, &c, 1);
180 glyphNamesTemp[i] = mymalloc (c + 1);
181 surely_read (fd, glyphNamesTemp[i], c);
182 glyphNamesTemp[i][c] = '\0';
184 fprintf (stderr, " %d: %s\n", i, glyphNamesTemp[i]);
187 for (i = 0; i < nglyphs; i++)
188 if (glyphNames[i].type == 1)
189 glyphNames[i].name.name = glyphNamesTemp[glyphNames[i].name.index];
190 free (glyphNamesTemp);
194 return 3; /* no name table */
201 readLocaTable (int fd, int nglyphs, int format)
209 offsets = mymalloc ((nglyphs + 1) * sizeof (USHORT));
210 surely_read (fd, offsets, (nglyphs + 1) * sizeof (USHORT));
211 for (i = 0; i <= nglyphs; i++)
215 /*NOTREACHED*/ case 1:
218 offsets = mymalloc ((nglyphs + 1) * sizeof (ULONG));
219 surely_read (fd, offsets, (nglyphs + 1) * sizeof (ULONG));
220 for (i = 0; i <= nglyphs; i++)
224 /*NOTREACHED*/ default:
225 error ("Unknown `loca' table format");
230 readGlyfTable (int fd, int nglyphs, int format, void *loca)
236 base = surely_lseek (fd, 0, SEEK_CUR);
238 bbox = mymalloc (nglyphs * sizeof (struct Box));
239 for (i = 0; i < nglyphs; i++)
242 offset = 2 * ((USHORT *) loca)[i];
244 offset = ((ULONG *) loca)[i];
245 surely_lseek (fd, base + offset + sizeof (SHORT), SEEK_SET);
246 surely_read (fd, bbox + i, sizeof (struct Box));
253 readHmtxTable (int fd, int nummetrics)
255 longHorMetric *metrics;
258 metrics = mymalloc (nummetrics * sizeof (longHorMetric));
259 surely_read (fd, metrics, nummetrics * sizeof (longHorMetric));
260 for (i = 0; i < nummetrics; i++)
262 FIX_longHorMetric (metrics[i]);
268 readHheaTable (int fd)
270 struct HheaTable *hhea;
271 hhea = mymalloc (sizeof (struct HheaTable));
272 surely_read (fd, hhea, sizeof (struct HheaTable));
273 FIX_HheaTable (*hhea);
275 fprintf (stderr, " version %d.%u\n",
276 hhea->version.mantissa, hhea->version.fraction);
277 if (hhea->metricDataFormat != 0)
278 error ("Unknown metric data format");
283 readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
286 struct KernSubTableHeader ksth;
287 struct KernSubTable0 kst;
290 struct KernEntry0 **ke;
292 surely_read (fd, &kt, sizeof (struct KernTable));
296 fprintf (stderr, " version %d\n", kt.version);
297 fprintf (stderr, " %d subtables\n", kt.nTables);
299 nke = mymalloc (kt.nTables * sizeof (int));
300 ke = mymalloc (kt.nTables * sizeof (struct KernEntry0 *));
302 for (i = 0; i < kt.nTables; i++)
304 surely_read (fd, &ksth, sizeof (struct KernSubTableHeader));
305 FIX_KernSubTableHeader (ksth);
307 fprintf (stderr, " analyzing subtable %d, version %d... ",
309 if ((ksth.coverage & kernHorizontal) &&
310 !(ksth.coverage & kernMinimum) &&
311 !(ksth.coverage & kernCrossStream) &&
312 (kernFormat (ksth.coverage) == 0))
314 surely_read (fd, &kst, sizeof (struct KernSubTable0));
315 FIX_KernSubTable0 (kst);
317 fprintf (stderr, "reading %d entries.\n", kst.nPairs);
319 ke[i] = mymalloc (kst.nPairs * sizeof (struct KernEntry0));
320 surely_read (fd, ke[i], kst.nPairs * sizeof (struct KernEntry0));
321 for (j = 0; j < kst.nPairs; j++)
322 FIX_KernEntry0 (ke[i][j]);
327 fprintf (stderr, "skipping.\n");
328 surely_lseek (fd, ksth.length - sizeof (struct KernSubTableHeader),