db35e6f7180caf550a830e433dc0fb21f4229cac
[m17n/libotf.git] / src / otfopen.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "otf.h"
6 #include "otfutil.h"
7
8 /* OTF_Stream
9
10    Example of typical usage of OTF_Stream.
11
12     {
13       OTF_Stream *stream;
14       OTF_StreamState state;
15       int offset, nbytes;
16
17       OPEN_STREAM (_FILE_NAME_, stream);
18       if (! stream)
19         _ERROR_;
20       SETUP_STREAM (stream, fp, 0, 256, _NAME_);
21       offset = READ_OFFSET (stream);
22       nbytes = READ_ULONG (stream);
23       SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
24       ...;
25       CLOSE_STREAM (stream);
26     }
27
28 */
29
30 typedef struct
31 {
32   FILE *fp;
33   char *name;
34   long pos;
35   long bufsize;
36   long allocated;
37   unsigned char *buf;
38 } OTF_Stream;
39
40 typedef long OTF_StreamState;
41
42 OTF_Stream *
43 make_stream ()
44 {
45   OTF_Stream *stream;
46   char *errfmt = "stream creation%s";
47   void *errret = NULL;
48
49   stream = malloc (sizeof (OTF_Stream));
50   if (! stream)
51     OTF_ERROR (OTF_ERROR_MEMORY, "");
52   return stream;
53 }
54
55 int
56 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
57               char *name)
58 {
59   char *errfmt = "stream setup for %s";
60   int errret = -1;
61
62   stream->name = name;
63   stream->pos = 0;
64   if (stream->allocated < nbytes)
65     {
66       unsigned char *buf = malloc (nbytes);
67
68       if (! buf)
69         OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
70       if (stream->buf)
71         free (stream->buf);
72       stream->buf = buf;
73       stream->allocated = nbytes;
74     }
75   stream->bufsize = nbytes;
76   if (fseek (fp, offset, SEEK_SET) < 0)
77     OTF_ERROR (OTF_ERROR_FILE, stream->name);
78   if (fread (stream->buf, 1, nbytes, fp) != nbytes)
79     OTF_ERROR (OTF_ERROR_FILE, stream->name);
80   return 0;
81 }
82
83
84 void
85 free_stream (OTF_Stream *stream)
86 {
87   free (stream->buf);
88   free (stream);
89 }
90
91 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
92 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
93 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
94
95 #define STREAM_CHECK_SIZE(stream, size)                 \
96   if ((stream)->pos + (size) > (stream)->bufsize)       \
97     {                                                   \
98       char *errfmt = "buffer overrun in %s";            \
99                                                         \
100       OTF_ERROR (OTF_ERROR_TABLE, (stream)->name);      \
101       return errret;                                    \
102     }                                                   \
103   else
104
105
106 #define READ_USHORT(stream, var)                        \
107   do {                                                  \
108     STREAM_CHECK_SIZE ((stream), 2);                    \
109     (var) = (((stream)->buf[(stream)->pos] << 8)        \
110              | (stream)->buf[(stream)->pos + 1]);       \
111     (stream)->pos += 2;                                 \
112   } while (0)
113
114 #define READ_SHORT(stream, var)                                 \
115   do {                                                          \
116     STREAM_CHECK_SIZE ((stream), 2);                            \
117     (var) = (short) (((stream)->buf[(stream)->pos] << 8)        \
118                      | (stream)->buf[(stream)->pos + 1]);       \
119     (stream)->pos += 2;                                         \
120   } while (0)
121
122 #define READ_ULONG(stream, var)                         \
123   do {                                                  \
124     STREAM_CHECK_SIZE ((stream), 4);                    \
125     (var) = (((stream)->buf[(stream)->pos] << 24)       \
126              | ((stream)->buf[(stream)->pos + 1] << 16) \
127              | ((stream)->buf[(stream)->pos + 2] << 8)  \
128              | (stream)->buf[(stream)->pos + 3]);       \
129     (stream)->pos += 4;                                 \
130   } while (0)
131
132 #define READ_LONG(stream, var)                                  \
133   do {                                                          \
134     STREAM_CHECK_SIZE ((stream), 4);                            \
135     (var) = (int) (((stream)->buf[(stream)->pos] << 24)         \
136                    | ((stream)->buf[(stream)->pos + 1] << 16)   \
137                    | ((stream)->buf[(stream)->pos + 2] << 8)    \
138                    | (stream)->buf[(stream)->pos + 3]);         \
139     (stream)->pos += 4;                                         \
140   } while (0)
141
142
143 #define READ_FIXED(stream, fixed)               \
144   do {                                          \
145     READ_USHORT ((stream), (fixed).high);       \
146     READ_USHORT ((stream), (fixed).low);        \
147   } while (0)
148
149
150 #define READ_BYTES(stream, p, nbytes)                           \
151   do {                                                          \
152     STREAM_CHECK_SIZE ((stream), (nbytes));                     \
153     memcpy ((p), (stream)->buf + (stream)->pos, (nbytes));      \
154     (stream)->pos += (nbytes);                                  \
155   } while (0)
156
157
158 #define READ_TAG READ_ULONG
159 #define READ_OFFSET READ_USHORT
160 #define READ_UINT16 READ_USHORT
161 #define READ_INT16 READ_SHORT
162 #define READ_GLYPHID READ_USHORT
163
164 \f
165
166 enum OTF_TableType
167   {
168     OTF_TABLE_TYPE_HEAD,
169     OTF_TABLE_TYPE_NAME,
170     OTF_TABLE_TYPE_CMAP,
171     OTF_TABLE_TYPE_GDEF,
172     OTF_TABLE_TYPE_GSUB,
173     OTF_TABLE_TYPE_GPOS,
174     OTF_TABLE_TYPE_MAX
175   };
176
177 #define OTF_MEMORY_RECORD_SIZE 1024
178
179 struct OTF_MemoryRecord
180 {
181   int used;
182   void *memory[OTF_MEMORY_RECORD_SIZE];
183   struct OTF_MemoryRecord *next;
184 };
185
186 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
187
188 typedef struct
189 {
190   /* Points to one of OTF->head, OTF->name, etc.  */
191   void **address;
192   /* Function to read one of OTF tables.  */
193   void *(*reader) (OTF *otf, OTF_Stream *stream);
194   /* Stream given to <reader>.  */
195   OTF_Stream *stream;
196 } OTF_TableInfo;
197
198 struct OTF_InternalData
199 {
200   /* Information about each OTF table.  */
201   OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
202
203   /* Stream used to read the header part of OTF.  */
204   OTF_Stream *header_stream;
205
206   /* Records of allocated memories.  */
207   OTF_MemoryRecord *memory_record;
208 };
209
210
211 static OTF_MemoryRecord *
212 allocate_memory_record (OTF *otf)
213 {
214   OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
215   OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
216
217   if (! memrec)
218     return NULL;
219   memrec->used = 0;
220   memrec->next = internal_data->memory_record;
221   internal_data->memory_record = memrec;
222   return memrec;
223 }
224
225 /* Memory allocation macros.  */
226
227 #define OTF_MALLOC(p, size, arg)                                        \
228   do {                                                                  \
229     if (size == 0)                                                      \
230       (p) = NULL;                                                       \
231     else                                                                \
232       {                                                                 \
233         OTF_MemoryRecord *memrec                                        \
234           = ((OTF_InternalData *) otf->internal_data)->memory_record;   \
235         (p) = malloc (sizeof (*(p)) * (size));                          \
236         if (! (p)                                                       \
237             || (memrec->used >= OTF_MEMORY_RECORD_SIZE                  \
238                 && ! (memrec = allocate_memory_record (otf))))          \
239           OTF_ERROR (OTF_ERROR_MEMORY, (arg));                          \
240         memrec->memory[memrec->used++] = (p);                           \
241       }                                                                 \
242   } while (0)
243
244
245 #define OTF_CALLOC(p, size, arg)                                        \
246   do {                                                                  \
247     if (size == 0)                                                      \
248       (p) = NULL;                                                       \
249     else                                                                \
250       {                                                                 \
251         OTF_MemoryRecord *memrec                                        \
252           = ((OTF_InternalData *) otf->internal_data)->memory_record;   \
253         (p) = calloc ((size), sizeof (*(p)));                           \
254         if (! (p)                                                       \
255             || (memrec->used >= OTF_MEMORY_RECORD_SIZE                  \
256                 && ! (memrec = allocate_memory_record (otf))))          \
257           OTF_ERROR (OTF_ERROR_MEMORY, (arg));                          \
258         memrec->memory[memrec->used++] = (p);                           \
259       }                                                                 \
260   } while (0)
261
262
263 \f
264
265 static void *read_head_table (OTF *otf, OTF_Stream *stream);
266 static void *read_name_table (OTF *otf, OTF_Stream *stream);
267 static void *read_cmap_table (OTF *otf, OTF_Stream *stream);
268 static void *read_gdef_table (OTF *otf, OTF_Stream *stream);
269 static void *read_gsub_table (OTF *otf, OTF_Stream *stream);
270 static void *read_gpos_table (OTF *otf, OTF_Stream *stream);
271
272 int
273 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
274 {  
275   int errret = -1;
276
277   READ_FIXED (stream, table->sfnt_version);
278   READ_USHORT (stream, table->numTables);
279   READ_USHORT (stream, table->searchRange);
280   READ_USHORT (stream, table->enterSelector);
281   READ_USHORT (stream, table->rangeShift);
282   return 0;
283 }
284
285 static OTF_Tag
286 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
287 {
288   int errret = 0;
289   OTF_Tag tag;
290
291   READ_TAG (stream, tag);
292   table->tag = tag;
293   table->name[0] = tag >> 24;
294   table->name[1] = (tag >> 16) & 0xFF;
295   table->name[0] = (tag >> 8) & 0xFF;
296   table->name[0] = tag >> 8;
297   table->name[0] = '\0';
298   READ_ULONG (stream, table->checkSum);
299   READ_ULONG (stream, table->offset);
300   READ_ULONG (stream, table->length);
301   return tag;
302 }
303
304 static int
305 read_header_part (OTF *otf, FILE *fp)
306 {
307   char *errfmt = "otf header%s";
308   int errret = -1;
309   OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
310   OTF_Stream *stream;
311   int i;
312   OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
313
314   internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
315   internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
316   internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
317   internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
318   internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
319   internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
320   internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
321   internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
322   internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
323   internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
324   internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
325   internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
326
327   head_tag = otf_tag ("head");
328   name_tag = otf_tag ("name");
329   cmap_tag = otf_tag ("cmap");
330   gdef_tag = otf_tag ("GDEF");
331   gsub_tag = otf_tag ("GSUB");
332   gpos_tag = otf_tag ("GPOS");
333
334   stream = make_stream ();
335   if (! stream)
336     return -1;
337
338   internal_data->header_stream = stream;
339   
340   /* Size of Offset Table is 12 bytes.  */
341   if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
342     return -1;
343   if (read_offset_table (otf, stream, &otf->offset_table) < 0)
344     return -1;
345
346   /* Size of each Table Directory is 16 bytes.  */
347   if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
348                     "Table Directory") < 0)
349     return -1;
350
351   OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
352   for (i = 0; i < otf->offset_table.numTables; i++)
353     {
354       OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
355       OTF_TableInfo *table_info = NULL;
356
357       if (! tag)
358         return -1;
359       if (tag == head_tag)
360         table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
361       else if (tag == name_tag)
362         table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
363       else if (tag == cmap_tag)
364         table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
365       else if (tag == gdef_tag)
366         table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
367       else if (tag == gsub_tag)
368         table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
369       else if (tag == gpos_tag)
370         table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
371
372       if (table_info)
373         {
374           table_info->stream = make_stream ();
375           if (setup_stream (table_info->stream, fp,
376                             otf->table_dirs[i].offset,
377                             otf->table_dirs[i].length,
378                             otf->table_dirs[i].name) < 0)
379             return -1;
380         }
381     }
382
383   internal_data->header_stream = NULL;
384   free_stream (stream);
385   return 0;
386 }
387
388
389 \f
390
391 static void *
392 read_head_table (OTF *otf, OTF_Stream *stream)
393 {
394   char *errfmt = "head%s";
395   void *errret = NULL;
396   OTF_head *head;
397
398   OTF_CALLOC (head, 1, "");
399   READ_FIXED (stream, head->TableVersionNumber);
400   READ_FIXED (stream, head->fontRevision);
401   READ_ULONG (stream, head->checkSumAdjustment);
402   READ_ULONG (stream, head->magicNumber);
403   READ_USHORT (stream, head->flags);
404   READ_USHORT (stream, head->unitsPerEm);
405
406   return head;
407 }
408
409 \f
410
411 static int
412 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
413                   OTF_ScriptList *list)
414 {
415   char *errfmt = "Script List%s";
416   int errret = -1;
417   int i, j, k;
418
419   SEEK_STREAM (stream, offset);
420   READ_USHORT (stream, list->ScriptCount);
421   OTF_CALLOC (list->Script, list->ScriptCount, "");
422
423   for (i = 0; i < list->ScriptCount; i++)
424     {
425       READ_TAG (stream, list->Script[i].ScriptTag);
426       READ_OFFSET (stream, list->Script[i].offset);
427     }
428   for (i = 0;  i < list->ScriptCount; i++)
429     {
430       OTF_Script *script = list->Script + i;
431       long script_offset = offset + script->offset;
432
433       SEEK_STREAM (stream, script_offset);
434       READ_OFFSET (stream, script->DefaultLangSysOffset);
435       READ_USHORT (stream, script->LangSysCount);
436       OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
437       OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
438       for (j = 0; j < script->LangSysCount; j++)
439         {
440           READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
441           READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
442         }
443
444       if (script->DefaultLangSysOffset)
445         {
446           OTF_LangSys *langsys = &script->DefaultLangSys;
447
448           SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
449           READ_OFFSET (stream, langsys->LookupOrder);
450           READ_USHORT (stream, langsys->ReqFeatureIndex);
451           READ_USHORT (stream, langsys->FeatureCount);
452           OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
453                       " (FeatureIndex)");
454           for (k = 0; k < langsys->FeatureCount; k++)
455             READ_USHORT (stream, langsys->FeatureIndex[k]);
456         }
457           
458       for (j = 0; j < script->LangSysCount; j++)
459         {
460           OTF_LangSys *langsys = script->LangSys + j;
461
462           SEEK_STREAM (stream,
463                        script_offset + script->LangSysRecord[j].LangSys);
464           READ_OFFSET (stream, langsys->LookupOrder);
465           READ_USHORT (stream, langsys->ReqFeatureIndex);
466           READ_USHORT (stream, langsys->FeatureCount);
467           OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
468                       " (FeatureIndex)");
469           for (k = 0; k < langsys->FeatureCount; k++)
470             READ_USHORT (stream, langsys->FeatureIndex[k]);
471         }
472     }
473
474   return 0;
475 }
476
477 static int
478 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
479                    OTF_FeatureList *list)
480 {
481   char *errfmt = "Feature List%s";
482   int errret = -1;
483   int i, j;
484
485   READ_UINT16 (stream, list->FeatureCount);
486   OTF_CALLOC (list->Feature, list->FeatureCount, "");
487   for (i = 0; i < list->FeatureCount; i++)
488     {
489       READ_TAG (stream, list->Feature[i].FeatureTag);
490       READ_OFFSET (stream, list->Feature[i].offset);
491     }
492   for (i = 0; i < list->FeatureCount; i++)
493     {
494       OTF_Feature *feature = list->Feature + i;
495
496       SEEK_STREAM (stream, offset + feature->offset);
497       READ_OFFSET (stream, feature->FeatureParams);
498       READ_UINT16 (stream, feature->LookupCount);
499       OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
500                   " (LookupListIndex)");
501       for (j = 0; j < feature->LookupCount; j++)
502         READ_UINT16 (stream, feature->LookupListIndex[j]);
503     }
504
505   return 0;
506 }
507
508 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
509                                       long offset, unsigned type,
510                                       OTF_LookupSubTable *subtable);
511 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
512                                       long offset, unsigned type,
513                                       OTF_LookupSubTable *subtable);
514
515 static int
516 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
517                   OTF_LookupList *list, int gsub)
518 {
519   char *errfmt = "Lookup List%s";
520   int errret = -1;
521   int i, j;
522
523   SEEK_STREAM (stream, offset);
524   READ_UINT16 (stream, list->LookupCount);
525   OTF_CALLOC (list->Lookup, list->LookupCount, "");
526
527   for (i = 0; i < list->LookupCount; i++)
528     READ_OFFSET (stream, list->Lookup[i].offset);
529   for (i = 0; i < list->LookupCount; i++)
530     {
531       OTF_Lookup *lookup = list->Lookup + i;
532
533       SEEK_STREAM (stream, offset + lookup->offset);
534       READ_UINT16 (stream, lookup->LookupType);
535       READ_UINT16 (stream, lookup->LookupFlag);
536       READ_UINT16 (stream, lookup->SubTableCount);
537       OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
538                   " (SubTableOffset)");
539       OTF_CALLOC (lookup->SubTable, lookup->SubTableCount,
540                   " (SubTable)");
541       for (j = 0; j < lookup->SubTableCount; j++)
542         READ_OFFSET (stream, lookup->SubTableOffset[j]);
543       if (gsub)
544         for (j = 0; j < lookup->SubTableCount; j++)
545           {
546             long this_offset
547               = offset + lookup->offset + lookup->SubTableOffset[j];
548
549             if (read_lookup_subtable_gsub (otf, stream, this_offset,
550                                            lookup->LookupType,
551                                            lookup->SubTable + j) < 0)
552               return errret;
553           }
554       else
555         for (j = 0; j < lookup->SubTableCount; j++)
556           {
557             long this_offset
558               = offset + lookup->offset + lookup->SubTableOffset[j];
559
560             if (read_lookup_subtable_gpos (otf, stream, this_offset,
561                                            lookup->LookupType,
562                                            lookup->SubTable + j) < 0)
563               return errret;
564           }
565     }
566
567   return 0;
568 }
569
570
571 static int
572 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids, int minus)
573 {
574   char *errfmt = "GlyphID List%s";
575   int errret = -1;
576   unsigned count;
577   int i;
578
579   READ_UINT16 (stream, count);
580   if (! count)
581     return 0;
582   OTF_MALLOC (*ids, count, "");
583   for (i = 0; i < count + minus; i++)
584     READ_GLYPHID (stream, (*ids)[i]);
585   return (int) count;
586 }
587      
588 static unsigned
589 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
590 {
591   char *errfmt = "RangeRecord%s";
592   unsigned errret = 0;
593   unsigned count;
594   int i;
595
596   READ_UINT16 (stream, count);
597   if (! count)
598     return 0;
599   OTF_MALLOC (*record, count, "");
600   for (i = 0; i < count; i++)
601     {
602       READ_GLYPHID (stream, (*record)[i].Start);
603       READ_GLYPHID (stream, (*record)[i].End);
604       READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
605     }
606   return count;
607 }
608
609
610 static int
611 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
612                OTF_Coverage *coverage)
613 {
614   char *errfmt = "Coverage%s";
615   int errret = -1;
616   OTF_StreamState state;
617   int count;
618   
619   READ_OFFSET (stream, coverage->offset);
620   SAVE_STREAM (stream, state);
621   SEEK_STREAM (stream, offset + coverage->offset);
622   READ_UINT16 (stream, coverage->CoverageFormat);
623   if (coverage->CoverageFormat == 1)
624     count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0);
625   else if (coverage->CoverageFormat == 2)
626     count = read_range_records (otf, stream, &coverage->table.RangeRecord);
627   else
628     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
629   if (count < 0)
630     return -1;
631   coverage->Count = (unsigned) count;
632   RESTORE_STREAM (stream, state);
633   return 0;
634 }
635
636 static int
637 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
638                     OTF_Coverage **coverage)
639 {
640   char *errfmt = "Coverage List%s";
641   int errret = -1;
642   int count;
643   int i;
644
645   READ_UINT16 (stream, count);
646   if (! count)
647     return 0;
648   OTF_MALLOC (*coverage, count, "");
649   for (i = 0; i < count; i++)
650     if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
651       return -1;
652   return count;
653 }
654
655
656 static int
657 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
658                                OTF_ClassDef *class)
659 {
660   char *errfmt = "ClassDef%s";
661   int errret = -1;
662
663   SEEK_STREAM (stream, class->offset);
664   READ_UINT16 (stream, class->ClassFormat);
665   if (class->ClassFormat == 1)
666     {
667       READ_GLYPHID (stream, class->f.f1.StartGlyph);
668       class->f.f1.GlyphCount
669         = (read_glyph_ids
670            (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
671       if (! class->f.f1.GlyphCount)
672         OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
673     }
674   else if (class->ClassFormat == 2)
675     {
676       class->f.f2.ClassRangeCount
677         = (read_range_records
678            (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
679       if (! class->f.f2.ClassRangeCount)
680         OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
681     }
682   else
683     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
684   return 0;
685 }
686
687
688 static int
689 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
690 {
691   char *errfmt = "ClassDef%s";
692   int errret = -1;
693   OTF_StreamState state;
694   
695   READ_OFFSET (stream, class->offset);
696   SAVE_STREAM (stream, state);
697   SEEK_STREAM (stream, offset + class->offset);
698   READ_UINT16 (stream, class->ClassFormat);
699   if (class->ClassFormat == 1)
700     {
701       READ_GLYPHID (stream, class->f.f1.StartGlyph);
702       class->f.f1.GlyphCount
703         = (read_glyph_ids
704            (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
705       if (! class->f.f1.GlyphCount)
706         return -1;
707     }
708   else if (class->ClassFormat == 2)
709     {
710       class->f.f2.ClassRangeCount
711         = (read_range_records
712            (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
713       if (! class->f.f2.ClassRangeCount)
714         return -1;
715     }
716   else
717     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
718
719   RESTORE_STREAM (stream, state);
720   return 0;
721 }
722
723
724 static int
725 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
726                    OTF_DeviceTable *table)
727 {
728   char *errfmt = "Device Table%s";
729   int errret = -1;
730
731   int num, i;
732   unsigned val;
733   struct {
734     int int2 : 2;
735     int int4 : 4;
736     int int8 : 8;
737   } intval;
738
739   SEEK_STREAM (stream, offset + table->offset);
740   READ_UINT16 (stream, table->StartSize);
741   READ_UINT16 (stream, table->EndSize);
742   READ_UINT16 (stream, table->DeltaFormat);
743   num = table->EndSize - table->StartSize + 1;
744   OTF_MALLOC (table->DeltaValue, num, "");
745
746   if (table->DeltaFormat == 1)
747     for (i = 0; i < num; i++)
748       {
749         if ((i % 8) == 0)
750           READ_UINT16 (stream, val);
751         intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
752         table->DeltaValue[i] = intval.int2;
753       }
754   else if (table->DeltaFormat == 2)
755     for (i = 0; i < num; i++)
756       {
757         if ((i % 4) == 0)
758           READ_UINT16 (stream, val);
759         intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
760         table->DeltaValue[i] = intval.int4;
761       }
762   else if (table->DeltaFormat == 3)
763     for (i = 0; i < num; i++)
764       {
765         if ((i % 2) == 0)
766           {
767             READ_UINT16 (stream, val);
768             intval.int8 = val >> 8;
769             table->DeltaValue[i] = intval.int8;
770           }
771         else
772           {
773             intval.int8 = val >> 8;
774             table->DeltaValue[i] = intval.int8;
775           }
776       }
777   else
778     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
779   return 0;
780 }
781
782 \f
783 /* GSUB */
784
785 static void *
786 read_gsub_table (OTF *otf, OTF_Stream *stream)
787 {
788   char *errfmt = "GSUB%s";
789   void *errret = NULL;
790   OTF_GSUB *gsub;
791
792   OTF_CALLOC (gsub, 1, "");
793   READ_FIXED (stream, gsub->Version);
794   READ_OFFSET (stream, gsub->ScriptList.offset);
795   READ_OFFSET (stream, gsub->FeatureList.offset);
796   READ_OFFSET (stream, gsub->LookupList.offset);
797   
798   if (read_script_list (otf, stream, gsub->ScriptList.offset,
799                            &gsub->ScriptList) < 0
800       || read_feature_list (otf, stream, gsub->FeatureList.offset,
801                             &gsub->FeatureList) < 0
802       || read_lookup_list (otf, stream, gsub->LookupList.offset,
803                            &gsub->LookupList, 1) < 0)
804     return NULL;
805   return gsub;
806 }
807
808 static unsigned
809 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
810 {
811   char *errfmt = "Sequence%s";
812   unsigned errret = 0;
813   unsigned count;
814   int i;
815
816   READ_UINT16 (stream, count);
817   OTF_MALLOC (*seq, count, "");
818   if (! count)
819     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
820   for (i = 0; i < count; i++)
821     READ_OFFSET (stream, (*seq)[i].offset);
822   for (i = 0; i < count; i++)
823     {
824       SEEK_STREAM (stream, offset + (*seq)[i].offset);
825       (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
826                                              &(*seq)[i].Substitute, 0);
827       if (! (*seq)[i].GlyphCount)
828         return 0;
829     }
830   return count;
831 }
832
833 static int
834 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
835                OTF_Ligature **ligature)
836 {
837   char *errfmt = "Ligature%s";
838   int errret = -1;
839   int count;
840   int i;
841
842   READ_UINT16 (stream, count);
843   if (! count)
844     return 0;
845   OTF_MALLOC (*ligature, count, "");
846   for (i = 0; i < count; i++)
847     READ_OFFSET (stream, (*ligature)[i].offset);
848   for (i = 0; i < count; i++)
849     {
850       SEEK_STREAM (stream, offset + (*ligature)[i].offset);
851       READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
852       (*ligature)[i].CompCount
853         = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1);
854       if (! (*ligature)[i].CompCount)
855         return -1;
856     }
857   return count;
858 }
859
860 static int
861 read_ligature_set (OTF *otf, OTF_Stream *stream, long offset,
862                    OTF_LigatureSet **ligset)
863 {
864   char *errfmt = "LigatureSet%s";
865   int errret = -1;
866   int count;
867   int i;
868
869   READ_UINT16 (stream, count);
870   if (! count)
871     return 0;
872   OTF_MALLOC (*ligset, count, "");
873   for (i = 0; i < count; i++)
874     READ_OFFSET (stream, (*ligset)[i].offset);
875   for (i = 0; i < count; i++)
876     {
877       int lig_count;
878
879       SEEK_STREAM (stream, offset + (*ligset)[i].offset);
880       lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
881                                  &(*ligset)[i].Ligature);
882       if (lig_count < 0)
883         return -1;
884       (*ligset)[i].LigatureCount = (unsigned) lig_count;
885     }
886   return count;
887 }
888
889 static unsigned
890 read_subst_lookup_record (OTF *otf, OTF_Stream *stream,
891                           OTF_SubstLookupRecord **record)
892 {
893   char *errfmt = "SubstLookupRecord%s";
894   unsigned errret = 0;
895   unsigned count;
896   int i;
897
898   READ_UINT16 (stream, count);
899   if (! count)
900     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
901   OTF_MALLOC (*record, count, "");
902   for (i = 0; i < count; i++)
903     {
904       READ_UINT16 (stream, (*record)[i].SequenceIndex);
905       READ_UINT16 (stream, (*record)[i].LookupListIndex);
906     }
907   return count;
908 }
909
910 static unsigned
911 read_chain_subrule (OTF *otf, OTF_Stream *stream, long offset,
912                     OTF_ChainSubRule **rule)
913 {
914   char *errfmt = "ChainSubRule%s";
915   unsigned errret = 0;
916   unsigned count;
917   int i;
918
919   READ_UINT16 (stream, count);
920   if (! count)
921     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
922   OTF_MALLOC (*rule, count, "");
923   for (i = 0; i < count; i++)
924     READ_OFFSET (stream, (*rule)[i].offset);
925   for (i = 0; i < count; i++)
926     {
927       SEEK_STREAM (stream, offset + (*rule)[i].offset);
928       (*rule)[i].BacktrackGlyphCount
929         = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0);
930       if (! (*rule)[i].BacktrackGlyphCount)
931         return 0;
932       (*rule)[i].InputGlyphCount
933         = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1);
934       if (! (*rule)[i].InputGlyphCount)
935         return 0;
936       (*rule)[i].LookaheadGlyphCount
937         = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0);
938       if (! (*rule)[i].LookaheadGlyphCount)
939         return 0;
940       (*rule)[i].SubstCount
941         = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
942       if (! (*rule)[i].SubstCount)
943         return 0;
944     }
945   return count;
946 }
947
948
949 static unsigned
950 read_chain_subrule_set (OTF *otf, OTF_Stream *stream, long offset,
951                         OTF_ChainSubRuleSet **set)
952 {
953   char *errfmt = "ChainSubRuleSet%s";
954   unsigned errret = 0;
955   unsigned count;
956   int i;
957
958   READ_UINT16 (stream, count);
959   if (! count)
960     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
961   OTF_MALLOC (*set, count, "");
962   for (i = 0; i < count; i++)
963     READ_OFFSET (stream, (*set)[i].offset);
964   for (i = 0; i < count; i++)
965     {
966       SEEK_STREAM (stream, offset + (*set)[i].offset);
967       (*set)[i].ChainSubRuleCount
968         = read_chain_subrule (otf, stream, offset + (*set)[i].offset,
969                                &(*set)[i].ChainSubRule);
970       if (! (*set)[i].ChainSubRuleCount)
971         return 0;
972     }
973   return count;
974 }
975
976 static unsigned
977 read_chain_subclass_rule (OTF *otf, OTF_Stream *stream, long offset,
978                           OTF_ChainSubClassRule **rule)
979 {
980   char *errfmt = "ChainSubClassRule%s";
981   unsigned errret = 0;
982   unsigned count;
983   int i;
984
985   READ_UINT16 (stream, count);
986   if (! count)
987     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
988   OTF_MALLOC (*rule, count, "");
989   for (i = 0; i < count; i++)
990     READ_OFFSET (stream, (*rule)[i].offset);
991   for (i = 0; i < count; i++)
992     {
993       SEEK_STREAM (stream, offset + (*rule)[i].offset);
994       (*rule)[i].BacktrackGlyphCount
995         = read_glyph_ids (otf, stream,
996                           (OTF_GlyphID **) &(*rule)[i].Backtrack, 0);
997       if (! (*rule)[i].BacktrackGlyphCount)
998         return 0;
999       (*rule)[i].InputGlyphCount
1000         = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Input, -1);
1001       if (! (*rule)[i].InputGlyphCount)
1002         return 0;
1003       (*rule)[i].LookaheadGlyphCount
1004         = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].LookAhead, 0);
1005       if (! (*rule)[i].LookaheadGlyphCount)
1006         return 0;
1007       (*rule)[i].SubstCount
1008         = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
1009       if (! (*rule)[i].SubstCount)
1010         return 0;
1011     }
1012   return count;
1013 }
1014
1015 static unsigned
1016 read_chain_subclass_set (OTF *otf, OTF_Stream *stream, long offset,
1017                          OTF_ChainSubClassSet **set)
1018 {
1019   char *errfmt = "ChainSubClassSet%s";
1020   unsigned errret = 0;
1021   unsigned count;
1022   int i;
1023
1024   READ_UINT16 (stream, count);
1025   if (! count)
1026     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1027   OTF_MALLOC (*set, count, "");
1028   for (i = 0; i < count; i++)
1029     READ_OFFSET (stream, (*set)[i].offset);
1030   for (i = 0; i < count; i++)
1031     {
1032       SEEK_STREAM (stream, offset + (*set)[i].offset);
1033       (*set)[i].ChainSubClassRuleCnt
1034         = read_chain_subclass_rule (otf, stream, offset + (*set)[i].offset,
1035                                     &(*set)[i].ChainSubClassRule);
1036       if (! (*set)[i].ChainSubClassRuleCnt)
1037         return 0;
1038     }
1039   return count;
1040 }
1041
1042
1043 static int 
1044 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1045                            unsigned type, OTF_LookupSubTable *subtable)
1046 {
1047   char *errfmt = "GSUB LookupSubTable%s";
1048   int errret = -1;
1049   int count;
1050
1051   SEEK_STREAM (stream, offset);
1052   READ_UINT16 (stream, subtable->Format);
1053   switch (type)
1054     {
1055     case 1:
1056       if (subtable->Format == 1)
1057         {
1058           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1059             return -1;
1060           READ_INT16 (stream, subtable->sub.gsub.single1.DeltaGlyphID);
1061         }
1062       else if (subtable->Format == 2)
1063         {
1064           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1065             return -1;
1066           subtable->sub.gsub.single2.GlyphCount
1067             = read_glyph_ids (otf, stream, &subtable->sub.gsub.single2.Substitute,
1068                               0);
1069           if (! subtable->sub.gsub.single2.GlyphCount)
1070             return -1;
1071         }
1072       else
1073         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1074       break;
1075
1076     case 2:
1077       if (subtable->Format == 1)
1078         {
1079           read_coverage (otf, stream, offset, &subtable->Coverage);
1080           subtable->sub.gsub.multiple1.SequenceCount
1081             = read_sequence (otf, stream, offset,
1082                              &subtable->sub.gsub.multiple1.Sequence);
1083         }
1084       else
1085         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1086       break;
1087       
1088     case 3:
1089       break;
1090
1091     case 4:
1092       if (subtable->Format == 1)
1093         {
1094           read_coverage (otf, stream, offset, &subtable->Coverage);
1095           count = (read_ligature_set
1096                    (otf, stream, offset,
1097                     &subtable->sub.gsub.ligature1.LigatureSet));
1098           if (count < 0)
1099             return -1;
1100           subtable->sub.gsub.ligature1.LigSetCount = (unsigned) count;
1101         }
1102       else
1103         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1104       break;
1105
1106     case 6:
1107       if (subtable->Format == 1)
1108         {
1109           read_coverage (otf, stream, offset, &subtable->Coverage);
1110           subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1111             = (read_chain_subrule_set
1112                (otf, stream, offset,
1113                 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1114         }
1115       else if (subtable->Format == 2)
1116         {
1117           read_coverage (otf, stream, offset, &subtable->Coverage);
1118           read_class_def (otf, stream, offset,
1119                           &subtable->sub.gsub.chain_context2.Backtrack);
1120           read_class_def (otf, stream, offset,
1121                           &subtable->sub.gsub.chain_context2.Input);
1122           read_class_def (otf, stream, offset,
1123                           &subtable->sub.gsub.chain_context2.LookAhead);
1124           subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1125             = (read_chain_subclass_set
1126                (otf, stream, offset,
1127                 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1128         }
1129       else if (subtable->Format == 3)
1130         {
1131           count = (read_coverage_list
1132                    (otf, stream, offset,
1133                     &subtable->sub.gsub.chain_context3.Backtrack));
1134           if (count < 0)
1135             return -1;
1136           subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1137             = (unsigned) count;
1138           count = (read_coverage_list
1139                    (otf, stream, offset,
1140                     &subtable->sub.gsub.chain_context3.Input));
1141           if (count <= 0)
1142             return -1;
1143           subtable->sub.gsub.chain_context3.InputGlyphCount
1144             = (unsigned) count;
1145           subtable->Coverage = subtable->sub.gsub.chain_context3.Input[0];
1146           count = (read_coverage_list
1147                    (otf, stream, offset,
1148                     &subtable->sub.gsub.chain_context3.LookAhead));
1149           subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1150             = (unsigned) count;
1151           subtable->sub.gsub.chain_context3.SubstCount
1152             = (read_subst_lookup_record
1153                (otf, stream,
1154                 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1155         }
1156       else
1157         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1158       break;
1159
1160
1161     default:
1162         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1163     }
1164   return 0;
1165 }
1166
1167 \f
1168 /* GPOS */
1169
1170 static int
1171 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1172                    enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1173 {
1174   int errret = -1;
1175   OTF_StreamState state;
1176   int size, i;
1177
1178   if (! bit)
1179     return 0;
1180   for (i = 0, size = 0; i < 8; i++)
1181     if (bit & (1 << i))
1182       size += 2;
1183
1184   if (bit & OTF_XPlacement)
1185     READ_INT16 (stream, value_record->XPlacement);
1186   if (bit & OTF_XPlacement)
1187     READ_INT16 (stream, value_record->YPlacement);
1188   if (bit & OTF_XAdvance)
1189     READ_INT16 (stream, value_record->XAdvance);
1190   if (bit & OTF_YAdvance)
1191     READ_INT16 (stream, value_record->YAdvance);
1192   if (bit & OTF_XPlaDevice)
1193     READ_OFFSET (stream, value_record->XPlaDevice.offset);
1194   if (bit & OTF_YPlaDevice)
1195     READ_OFFSET (stream, value_record->YPlaDevice.offset);
1196   if (bit & OTF_XAdvDevice)
1197     READ_OFFSET (stream, value_record->XAdvDevice.offset);
1198   if (bit & OTF_YAdvDevice)
1199     READ_OFFSET (stream, value_record->YAdvDevice.offset);
1200   SAVE_STREAM (stream, state);
1201   if (value_record->XPlaDevice.offset)
1202     {
1203       if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1204         return -1;
1205     }
1206   if (value_record->YPlaDevice.offset)
1207     {
1208       if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1209         return -1;
1210     }
1211   if (value_record->XAdvDevice.offset)
1212     {
1213       if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1214         return -1;
1215     }
1216   if (value_record->YAdvDevice.offset)
1217     {
1218       if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1219         return -1;
1220     }
1221   RESTORE_STREAM (stream, state);
1222   return 0;
1223 }
1224
1225
1226 static int
1227 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1228 {
1229   char *errfmt = "Anchor%s";
1230   int errret = -1;
1231
1232   SEEK_STREAM (stream, offset + anchor->offset);
1233   READ_UINT16 (stream, anchor->AnchorFormat);
1234   READ_INT16 (stream, anchor->XCoordinate);
1235   READ_INT16 (stream, anchor->YCoordinate);
1236   if (anchor->AnchorFormat == 1)
1237     ;
1238   else if (anchor->AnchorFormat == 2)
1239     {
1240       READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1241     }
1242   else if (anchor->AnchorFormat == 3)
1243     {
1244       READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1245       READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1246       if (anchor->f.f2.XDeviceTable.offset)
1247         {
1248           if (read_device_table (otf, stream, offset + anchor->offset,
1249                                  &anchor->f.f2.XDeviceTable) < 0)
1250             return -1;
1251         }
1252       if (anchor->f.f2.YDeviceTable.offset)
1253         {
1254           if (read_device_table (otf, stream, offset + anchor->offset,
1255                                  &anchor->f.f2.YDeviceTable) < 0)
1256             return -1;
1257         }
1258     }
1259   else
1260     OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1261
1262   return 0;
1263 }
1264
1265 static int
1266 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1267                  OTF_MarkArray *array)
1268 {
1269   char *errfmt = "MarkArray%s";
1270   int errret = -1;
1271   OTF_StreamState state;
1272   int i;
1273   
1274   READ_OFFSET (stream, array->offset);
1275   SAVE_STREAM (stream, state);
1276   SEEK_STREAM (stream, offset + array->offset);
1277   READ_UINT16 (stream, array->MarkCount);
1278   OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1279   for (i = 0; i < array->MarkCount; i++)
1280     {
1281       READ_UINT16 (stream, array->MarkRecord[i].Class);
1282       READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1283     }
1284   for (i = 0; i < array->MarkCount; i++)
1285     if (read_anchor (otf, stream, offset + array->offset,
1286                      &array->MarkRecord[i].MarkAnchor) < 0)
1287       return -1;;
1288   RESTORE_STREAM (stream, state);
1289   return 0;
1290 }
1291
1292 static int
1293 read_base_array (OTF *otf, OTF_Stream *stream, long offset,
1294                  unsigned ClassCount, OTF_BaseArray *array)
1295 {
1296   char *errfmt = "BaseArray%s";
1297   int errret = -1;
1298   OTF_StreamState state;
1299   int i, j;
1300   
1301   READ_OFFSET (stream, array->offset);
1302   SAVE_STREAM (stream, state);
1303   SEEK_STREAM (stream, offset + array->offset);
1304   READ_UINT16 (stream, array->BaseCount);
1305   OTF_MALLOC (array->BaseRecord, array->BaseCount, "");
1306   for (i = 0; i < array->BaseCount; i++)
1307     {
1308       OTF_MALLOC (array->BaseRecord[i].BaseAnchor, ClassCount,
1309                   " (BaseRecord)");
1310       for (j = 0; j < ClassCount; j++)
1311         READ_OFFSET (stream, array->BaseRecord[i].BaseAnchor[j].offset);
1312     }
1313   for (i = 0; i < array->BaseCount; i++)
1314     for (j = 0; j < ClassCount; j++)
1315       if (read_anchor (otf, stream, offset + array->offset,
1316                        &array->BaseRecord[i].BaseAnchor[j]) < 0)
1317         return -1;
1318   RESTORE_STREAM (stream, state);
1319   return 0;
1320 }
1321
1322
1323 static OTF_Class1Record *
1324 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1325                          unsigned num1, enum OTF_ValueFormat bit1,
1326                          unsigned num2, enum OTF_ValueFormat bit2)
1327 {
1328   char *errfmt = "Class1Record%s";
1329   void *errret = NULL;
1330   OTF_Class1Record *rec;
1331   int i, j;
1332
1333   OTF_MALLOC (rec, num1, "");
1334   for (i = 0; i < num1; i++)
1335     {
1336       OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1337       for (j = 0; j < num2; j++)
1338         {
1339           if (read_value_record (otf, stream, offset,
1340                                  bit1, &rec[i].Class2Record[j].Value1) < 0
1341               || read_value_record (otf, stream, offset,
1342                                     bit2, &rec[i].Class2Record[j].Value2) < 0)
1343             return NULL;
1344         }
1345     }
1346   return rec;
1347 }
1348
1349
1350 static int 
1351 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1352                            long offset, unsigned type,
1353                            OTF_LookupSubTable *subtable)
1354 {
1355   char *errfmt = "GPOS LookupSubTable%s";
1356   int errret = -1;
1357
1358   SEEK_STREAM (stream, offset);
1359   READ_UINT16 (stream, subtable->Format);
1360   switch (type)
1361     {
1362     case 1:
1363 #if 0
1364       if (subtable->Format == 1)
1365         {
1366           read_coverage (otf, stream, offset, &subtable->Coverage);
1367           subtable->sub.gsub.single1.DeltaGlyphID = READ_INT16 (stream);
1368         }
1369       else if (subtable->Format == 2)
1370         {
1371           read_coverage (otf, stream, offset, &subtable->Coverage);
1372           subtable->sub.gsub.single2.GlyphCount
1373             = read_glyph_ids (otf, stream,
1374                               &subtable->sub.gsub.single2.Substitute, 0);
1375         }
1376       else
1377         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1378 #endif
1379       break;
1380
1381     case 2:
1382       if (subtable->Format == 1)
1383         {
1384           read_coverage (otf, stream, offset, &subtable->Coverage);
1385         }
1386       else if (subtable->Format == 2)
1387         {
1388           SEEK_STREAM (stream, offset + 2);
1389           read_coverage (otf, stream, offset, &subtable->Coverage);
1390           READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat1);
1391           READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat2);
1392           read_class_def (otf, stream, offset,
1393                           &subtable->sub.gpos.pair2.ClassDef1);
1394           read_class_def (otf, stream, offset,
1395                           &subtable->sub.gpos.pair2.ClassDef2);
1396           READ_UINT16 (stream, subtable->sub.gpos.pair2.Class1Count);
1397           READ_UINT16 (stream, subtable->sub.gpos.pair2.Class2Count);
1398           subtable->sub.gpos.pair2.Class1Record
1399             = read_class1_record_list (otf, stream, offset,
1400                                        subtable->sub.gpos.pair2.Class1Count,
1401                                        subtable->sub.gpos.pair2.ValueFormat1,
1402                                        subtable->sub.gpos.pair2.Class2Count,
1403                                        subtable->sub.gpos.pair2.ValueFormat2);
1404         }
1405       else
1406         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1407       break;
1408       
1409     case 4:
1410       if (subtable->Format == 1)
1411         {
1412           read_coverage (otf, stream, offset, &subtable->Coverage);
1413           read_coverage (otf, stream, offset,
1414                          &subtable->sub.gpos.mark_base1.BaseCoverage);
1415           READ_UINT16 (stream, subtable->sub.gpos.mark_base1.ClassCount);
1416           read_mark_array (otf, stream, offset,
1417                            &subtable->sub.gpos.mark_base1.MarkArray);
1418           read_base_array (otf, stream, offset,
1419                            subtable->sub.gpos.mark_base1.ClassCount,
1420                            &subtable->sub.gpos.mark_base1.BaseArray);
1421         }
1422       else
1423         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1424       break;
1425
1426     case 6:
1427 #if 0
1428       if (subtable->Format == 1)
1429         {
1430           read_coverage (otf, stream, offset,
1431                          &subtable->sub.gsub.chain_context1.Coverage);
1432           subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1433             = (read_chain_subrule_set
1434                (otf, stream, offset,
1435                 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1436         }
1437       else if (subtable->Format == 2)
1438         {
1439           read_coverage (otf, stream, offset,
1440                          &subtable->sub.gsub.chain_context2.Coverage);
1441           read_class_def (otf, stream, offset,
1442                           &subtable->sub.gsub.chain_context2.Backtrack);
1443           read_class_def (otf, stream, offset,
1444                           &subtable->sub.gsub.chain_context2.Input);
1445           read_class_def (otf, stream, offset,
1446                           &subtable->sub.gsub.chain_context2.LookAhead);
1447           subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1448             = (read_chain_subclass_set
1449                (otf, stream, offset,
1450                 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1451         }
1452       else if (subtable->Format == 3)
1453         {
1454           subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1455             = (read_coverage_list
1456                (otf, stream, offset,
1457                 &subtable->sub.gsub.chain_context3.Backtrack));
1458           subtable->sub.gsub.chain_context3.InputGlyphCount
1459             = (read_coverage_list
1460                (otf, stream, offset,
1461                 &subtable->sub.gsub.chain_context3.Input));
1462           subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1463             = (read_coverage_list
1464                (otf, stream, offset,
1465                 &subtable->sub.gsub.chain_context3.LookAhead));
1466           subtable->sub.gsub.chain_context3.SubstCount
1467             = (read_subst_lookup_record
1468                (otf, stream,
1469                 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1470         }
1471       else
1472         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1473 #endif
1474
1475     default:
1476         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1477     }
1478   return 0;
1479 }
1480
1481
1482 static void *
1483 read_gpos_table (OTF *otf, OTF_Stream *stream)
1484 {
1485   char *errfmt = "GPOS%s";
1486   void *errret = NULL;
1487   OTF_GPOS *gpos;
1488
1489   OTF_CALLOC (gpos, 1, "");
1490   READ_FIXED (stream, gpos->Version);
1491   READ_OFFSET (stream, gpos->ScriptList.offset);
1492   READ_OFFSET (stream, gpos->FeatureList.offset);
1493   READ_OFFSET (stream, gpos->LookupList.offset);
1494
1495   if (read_script_list (otf, stream, gpos->ScriptList.offset,
1496                            &gpos->ScriptList) < 0
1497       || read_feature_list (otf, stream, gpos->FeatureList.offset,
1498                             &gpos->FeatureList) < 0
1499       || read_lookup_list (otf, stream, gpos->LookupList.offset,
1500                            &gpos->LookupList, 0) < 0)
1501     return NULL;
1502   return gpos;
1503 }
1504
1505 \f
1506 #if 0
1507 /* BASE */
1508
1509 static OTF_BASE *
1510 read_base_table (OTF_Stream *stream, long offset)
1511 {
1512   OTF_BASE *base;
1513
1514   OTF_MALLOC (base, 1);
1515
1516   return base;
1517 }
1518
1519 \f
1520 /* JSTF */
1521
1522 static OTF_JSTF *
1523 read_jstf_table (OTF_Stream *stream, long offset)
1524 {
1525   OTF_JSTF *jstf;
1526
1527   OTF_MALLOC (jstf, 1);
1528
1529   return jstf;
1530 }
1531 #endif
1532 \f
1533 /* GDEF */
1534 static int
1535 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1536                   OTF_AttachList *list)
1537 {
1538   char *errfmt = "AttachList%s";
1539   int errret = -1;
1540   int i, j;
1541
1542   if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1543     return -1;
1544   READ_UINT16 (stream, list->GlyphCount);
1545   OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1546   for (i = 0; i < list->GlyphCount; i++)
1547     READ_OFFSET (stream, list->AttachPoint[i].offset);
1548   for (i = 0; i < list->GlyphCount; i++)
1549     {
1550       int count;
1551
1552       SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1553       READ_UINT16 (stream, count);
1554       list->AttachPoint[i].PointCount = count;
1555       OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1556       for (j = 0; j < count; j++)
1557         READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1558     }
1559   return 0;
1560 }
1561
1562 static int
1563 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1564                   OTF_CaretValue *caret)
1565 {
1566   char *errfmt = "CaretValue%s";
1567   int errret = -1;
1568
1569   SEEK_STREAM (stream, offset + caret->offset);
1570   READ_UINT16 (stream, caret->CaretValueFormat);
1571   if (caret->CaretValueFormat == 1)
1572     READ_INT16 (stream, caret->f.f1.Coordinate);
1573   else if (caret->CaretValueFormat == 2)
1574     READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1575   else if (caret->CaretValueFormat == 3)
1576     {
1577       READ_INT16 (stream, caret->f.f3.Coordinate);
1578       if (read_device_table (otf, stream, offset + caret->offset,
1579                              &caret->f.f3.DeviceTable) < 0)
1580         return -1;
1581     }
1582   else
1583     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1584   return 0;
1585 }
1586
1587 static int
1588 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1589                      OTF_LigCaretList *list)
1590 {
1591   char *errfmt = "LigCaretList%s";
1592   int errret = -1;
1593   int i, j;
1594
1595   if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1596     return -1;
1597   READ_UINT16 (stream, list->LigGlyphCount);
1598   OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1599   for (i = 0; i < list->LigGlyphCount; i++)
1600     READ_OFFSET (stream, list->LigGlyph[i].offset);
1601   for (i = 0; i < list->LigGlyphCount; i++)
1602     {
1603       int count;
1604
1605       SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1606       READ_UINT16 (stream, count);
1607       list->LigGlyph[i].CaretCount = count;
1608       OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1609       for (j = 0; j < count; j++)
1610         READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1611       for (j = 0; j < count; j++)
1612         if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1613                               &list->LigGlyph[i].CaretValue[j]) < 0)
1614           return -1;
1615     }
1616   return 0;
1617 }
1618
1619 static int
1620 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1621 {
1622   int errret = -1;
1623
1624   READ_FIXED (stream, header->Version);
1625   READ_OFFSET (stream, header->GlyphClassDef);
1626   READ_OFFSET (stream, header->AttachList);
1627   READ_OFFSET (stream, header->LigCaretList);
1628   READ_OFFSET (stream, header->MarkAttachClassDef);
1629   return 0;
1630 }
1631
1632 static void *
1633 read_gdef_table (OTF *otf, OTF_Stream *stream)
1634 {
1635   char *errfmt = "GDEF%s";
1636   void *errret = NULL;
1637   OTF_GDEF *gdef;
1638
1639   OTF_CALLOC (gdef, 1, "");
1640   read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1641   if (gdef->header.GlyphClassDef)
1642     {
1643       gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1644       read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1645     }
1646   if (gdef->header.AttachList)
1647     read_attach_list (otf, stream, gdef->header.AttachList,
1648                       &gdef->attach_list);
1649   if (gdef->header.LigCaretList)
1650     read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1651                          &gdef->lig_caret_list);
1652   if (gdef->header.MarkAttachClassDef)
1653     {
1654       gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1655       read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1656     }
1657
1658   return gdef;
1659 }
1660
1661 \f
1662
1663 /* cmap */
1664
1665 static void *
1666 read_cmap_table (OTF *otf, OTF_Stream *stream)
1667 {
1668   char *errfmt = "cmap%s";
1669   void *errret = NULL;
1670   OTF_cmap *cmap;
1671   int i;
1672
1673   OTF_CALLOC (cmap, 1, "");
1674   READ_USHORT (stream, cmap->version);
1675   READ_USHORT (stream, cmap->numTables);
1676   OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
1677   for (i = 0; i < cmap->numTables; i++)
1678     {
1679       READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
1680       READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
1681       READ_ULONG (stream, cmap->EncodingRecord[i].offset);
1682       if (cmap->EncodingRecord[i].platformID == 3
1683           && cmap->EncodingRecord[i].encodingID == 1)
1684         cmap->Unicode = cmap->EncodingRecord + i;
1685     }
1686   for (i = 0; i < cmap->numTables; i++)
1687     {
1688       unsigned format;
1689
1690       SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
1691       READ_USHORT (stream, format);
1692       cmap->EncodingRecord[i].subtable.format = format;
1693       READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
1694       if (format == 8 || format == 10 || format == 12)
1695         {
1696           READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
1697           READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
1698         }
1699       else
1700         {
1701           READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
1702         }
1703       switch (format)
1704         {
1705         case 0:
1706           {
1707             OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
1708                         " (EncodingRecord)");
1709             READ_BYTES (stream,
1710                         cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
1711                         256);
1712           }
1713           break;
1714
1715         case 2:
1716           break;
1717
1718         case 4:
1719           {
1720             OTF_EncodingSubtable4 *sub4;
1721             int segCount;
1722             int j;
1723             unsigned dummy;
1724
1725             OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
1726             cmap->EncodingRecord[i].subtable.f.f4 = sub4;
1727             READ_USHORT (stream, sub4->segCountX2);
1728             segCount = sub4->segCountX2 / 2;
1729             READ_USHORT (stream, sub4->searchRange);
1730             READ_USHORT (stream, sub4->entrySelector);
1731             READ_USHORT (stream, sub4->rangeShift);
1732             OTF_MALLOC (sub4->segments, segCount, " (segCount)");
1733             for (j = 0; j < segCount; j++)
1734               READ_USHORT (stream, sub4->segments[j].endCount);
1735             READ_USHORT (stream, dummy);
1736             for (j = 0; j < segCount; j++)
1737               READ_USHORT (stream, sub4->segments[j].startCount);
1738             for (j = 0; j < segCount; j++)
1739               READ_SHORT (stream, sub4->segments[j].idDelta);
1740             for (j = 0; j < segCount; j++)
1741               {
1742                 unsigned off;
1743                 unsigned rest = 2 * (segCount - j);
1744                 
1745                 READ_USHORT (stream, off);
1746                 if (off == 0)
1747                   sub4->segments[j].idRangeOffset = 0xFFFF;
1748                 else
1749                   sub4->segments[j].idRangeOffset = (off - rest) / 2;
1750               }
1751             j = (cmap->EncodingRecord[i].subtable.length
1752                  - (14 + 2 * (segCount * 4 + 1)));
1753             sub4->GlyphCount = j / 2;
1754             OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
1755             for (j = 0; j < sub4->GlyphCount; j++)
1756               READ_USHORT (stream, sub4->glyphIdArray[j]);
1757           }
1758         }
1759     }
1760   return cmap;
1761 }
1762
1763 \f
1764
1765 /* TABLE: name */
1766
1767 static char *
1768 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
1769 {
1770   char *errfmt = "nameID (%d)";
1771   void *errret = NULL;
1772   OTF_StreamState state;
1773   char *str;
1774   int i;
1775   int c;
1776
1777   SAVE_STREAM (stream, state);
1778   SEEK_STREAM (stream, stream->pos + rec->offset);
1779
1780   if (bytes == 1)
1781     {
1782       OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
1783       READ_BYTES (stream, str, rec->length);
1784       for (i = 0; i < rec->length; i++)
1785         if (str[i] < 0)
1786           str[i] = '?';
1787     }
1788   else if (bytes == 2)
1789     {
1790       OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
1791       for (i = 0; i < rec->length / 2; i++)
1792         {
1793           READ_USHORT (stream, c);
1794           if (c >= 128)
1795             c = '?';
1796           str[i] = c;
1797         }
1798     }
1799   else if (bytes == 4)
1800     {
1801       OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
1802       for (i = 0; i < rec->length / 4; i++)
1803         {
1804           READ_ULONG (stream, c);
1805           if (c >= 128)
1806             c = '?';
1807           str[i] = c;
1808         }
1809     }
1810   str[i] = '\0';
1811   RESTORE_STREAM (stream, state);
1812   return str;
1813 }
1814
1815 static void *
1816 read_name_table (OTF *otf, OTF_Stream *stream)
1817 {
1818   char *errfmt = "name%s";
1819   void *errret = NULL;
1820   OTF_name *name;
1821   int i;
1822
1823   OTF_CALLOC (name, 1, "");
1824   READ_USHORT (stream, name->format);
1825   READ_USHORT (stream, name->count);
1826   READ_USHORT (stream, name->stringOffset);
1827   OTF_MALLOC (name->nameRecord, name->count, "");
1828   for (i = 0; i < name->count; i++)
1829     {
1830       OTF_NameRecord *rec = name->nameRecord + i;
1831
1832       READ_USHORT (stream, rec->platformID);
1833       READ_USHORT (stream, rec->encodingID);
1834       READ_USHORT (stream, rec->languageID);
1835       READ_USHORT (stream, rec->nameID);
1836       READ_USHORT (stream, rec->length);
1837       READ_USHORT (stream, rec->offset);
1838     }
1839   for (i = 0; i < name->count; i++)
1840     {
1841       OTF_NameRecord *rec = name->nameRecord + i;
1842       int nameID = rec->nameID;
1843
1844       if (nameID <= OTF_max_nameID
1845           && ! name->name[nameID])
1846         {
1847           if (rec->platformID == 0)
1848             name->name[nameID] = read_name (otf, stream, rec,
1849                                             rec->encodingID <= 3 ? 2 : 4);
1850           else if (rec->platformID == 1
1851                    && rec->encodingID == 0)
1852             name->name[nameID] = read_name (otf, stream, rec, 1);
1853           else if (rec->platformID == 3
1854                    && (rec->encodingID == 1 || rec->encodingID == 10))
1855             name->name[nameID] = read_name (otf, stream,
1856                                             rec, rec->encodingID == 1 ? 2 : 4);
1857         }
1858     }
1859
1860   return name;
1861 }
1862
1863 \f
1864
1865 /* APIs */
1866
1867 OTF_Tag
1868 otf_tag (char *name)
1869 {
1870   unsigned char *p = (unsigned char *) name;
1871
1872   if (! name)
1873     return (OTF_Tag) 0;
1874   return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1875 }
1876
1877 void
1878 otf_tag_name (OTF_Tag tag, char *name)
1879 {
1880   name[0] = (char) (tag >> 24);
1881   name[1] = (char) ((tag >> 16) & 0xFF);
1882   name[2] = (char) ((tag >> 8) & 0xFF);
1883   name[3] = (char) (tag & 0xFF);
1884   name[4] = '\0';
1885 }
1886
1887
1888 /* We can't use memory allocation macros in the following functions
1889    because those macros returns from the functions before freeing
1890    memory previously allocated.  */
1891
1892 OTF *
1893 otf_open (char *otf_name)
1894 {
1895   FILE *fp;
1896   char *errfmt = "opening otf (%s)";
1897   void *errret = NULL;
1898   OTF *otf;
1899   OTF_InternalData *internal_data;
1900
1901   fp = fopen (otf_name, "r");
1902   if (! fp)
1903     OTF_ERROR (OTF_ERROR_FILE, otf_name);
1904   otf = calloc (1, sizeof (OTF));
1905   if (! otf)
1906     OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
1907   otf->filename = strdup (otf_name);
1908   if (! otf->filename)
1909     {
1910       otf_close (otf);
1911       fclose (fp);
1912       OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
1913     }
1914
1915   internal_data = calloc (1, sizeof (OTF_InternalData));
1916   if (! internal_data)
1917     OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
1918   otf->internal_data = internal_data;
1919   if (! allocate_memory_record (otf))
1920     OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
1921
1922   /* Here after, all pointers to allocated memory are recorded in
1923      otf->internal_data->memory_record except for what allocated by
1924      the functions allocate_memory_record and make_stream.  */
1925
1926   if (read_header_part (otf, fp) < 0)
1927     {
1928       otf_close (otf);
1929       fclose (fp);
1930       return NULL;
1931     }
1932
1933   fclose (fp);
1934   return otf;
1935 }
1936
1937
1938 void
1939 otf_close (OTF *otf)
1940 {
1941   OTF_InternalData *internal_data = otf->internal_data;
1942   int i;
1943
1944   //if (otf->filename)
1945   //free (otf->filename);
1946   if (internal_data)
1947     {
1948       OTF_MemoryRecord *memrec = internal_data->memory_record;
1949
1950       if (internal_data->header_stream)
1951         free_stream (internal_data->header_stream);
1952
1953       for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
1954         if (internal_data->table_info[i].stream)
1955           free_stream (internal_data->table_info[i].stream);
1956
1957       while (memrec)
1958         {
1959           OTF_MemoryRecord *next = memrec->next;
1960
1961           for (i = memrec->used - 1; i >= 0; i--)
1962             free (memrec->memory[i]);
1963           free (memrec);
1964           memrec = next;
1965         }
1966       free (internal_data);
1967     }
1968 }
1969
1970
1971 int
1972 otf_get_table (OTF *otf, char *name)
1973 {
1974   char *errfmt = "OTF Table Read";
1975   int errret = -1;
1976   OTF_InternalData *internal_data = otf->internal_data;
1977   OTF_TableInfo *table_info;
1978   OTF_Tag tag = otf_tag (name);
1979
1980   if (! tag)
1981     OTF_ERROR (OTF_ERROR_TABLE, " (unknown)");
1982
1983   if (tag == otf_tag ("head"))
1984     table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
1985   else if (tag == otf_tag ("name"))
1986     table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
1987   else if (tag == otf_tag ("cmap"))
1988     table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
1989   else if (tag == otf_tag ("GDEF"))
1990     table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
1991   else if (tag == otf_tag ("GSUB"))
1992     table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
1993   else if (tag == otf_tag ("GPOS"))
1994     table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
1995   else
1996     OTF_ERROR (OTF_ERROR_TABLE, " (unsupported)");
1997
1998   if (*table_info->address)
1999     return 0;
2000   if (! table_info->stream)
2001     OTF_ERROR (OTF_ERROR_TABLE, " (not found)");
2002   if (! table_info->reader)
2003     OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2004
2005   *table_info->address = (*table_info->reader) (otf, table_info->stream);
2006   free_stream (table_info->stream);
2007   table_info->stream = NULL;
2008   if (! *table_info->address)
2009     {
2010       table_info->reader = NULL;
2011       OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2012     }
2013
2014   return 0;
2015 }