cef7e16739a44aa1c6959fa80dc79376fdb58194
[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 "otferror.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_LookupSubTableGSUB *subtable);
511 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
512                                       long offset, unsigned type,
513                                       OTF_LookupSubTableGPOS *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       if (gsub)
540         OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
541                     " (SubTable)");
542       else
543         OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
544                     " (SubTable)");
545       for (j = 0; j < lookup->SubTableCount; j++)
546         READ_OFFSET (stream, lookup->SubTableOffset[j]);
547       for (j = 0; j < lookup->SubTableCount; j++)
548         {
549           long this_offset
550             = offset + lookup->offset + lookup->SubTableOffset[j];
551
552           if (gsub
553               ? read_lookup_subtable_gsub (otf, stream, this_offset,
554                                            lookup->LookupType,
555                                            lookup->SubTable.gsub + j) < 0
556               : read_lookup_subtable_gpos (otf, stream, this_offset,
557                                            lookup->LookupType,
558                                            lookup->SubTable.gpos + j) < 0)
559             return errret;
560         }
561     }
562
563   return 0;
564 }
565
566
567 static int
568 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids, int minus)
569 {
570   char *errfmt = "GlyphID List%s";
571   int errret = -1;
572   unsigned count;
573   int i;
574
575   READ_UINT16 (stream, count);
576   if (! count)
577     return 0;
578   OTF_MALLOC (*ids, count, "");
579   for (i = 0; i < count + minus; i++)
580     READ_GLYPHID (stream, (*ids)[i]);
581   return (int) count;
582 }
583      
584 static unsigned
585 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
586 {
587   char *errfmt = "RangeRecord%s";
588   unsigned errret = 0;
589   unsigned count;
590   int i;
591
592   READ_UINT16 (stream, count);
593   if (! count)
594     return 0;
595   OTF_MALLOC (*record, count, "");
596   for (i = 0; i < count; i++)
597     {
598       READ_GLYPHID (stream, (*record)[i].Start);
599       READ_GLYPHID (stream, (*record)[i].End);
600       READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
601     }
602   return count;
603 }
604
605
606 static int
607 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
608                OTF_Coverage *coverage)
609 {
610   char *errfmt = "Coverage%s";
611   int errret = -1;
612   OTF_StreamState state;
613   int count;
614   
615   READ_OFFSET (stream, coverage->offset);
616   SAVE_STREAM (stream, state);
617   SEEK_STREAM (stream, offset + coverage->offset);
618   READ_UINT16 (stream, coverage->CoverageFormat);
619   if (coverage->CoverageFormat == 1)
620     count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0);
621   else if (coverage->CoverageFormat == 2)
622     count = read_range_records (otf, stream, &coverage->table.RangeRecord);
623   else
624     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
625   if (count < 0)
626     return -1;
627   coverage->Count = (unsigned) count;
628   RESTORE_STREAM (stream, state);
629   return 0;
630 }
631
632 static int
633 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
634                     OTF_Coverage **coverage)
635 {
636   char *errfmt = "Coverage List%s";
637   int errret = -1;
638   int count;
639   int i;
640
641   READ_UINT16 (stream, count);
642   if (! count)
643     return 0;
644   OTF_MALLOC (*coverage, count, "");
645   for (i = 0; i < count; i++)
646     if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
647       return -1;
648   return count;
649 }
650
651
652 static int
653 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
654                                OTF_ClassDef *class)
655 {
656   char *errfmt = "ClassDef%s";
657   int errret = -1;
658
659   SEEK_STREAM (stream, class->offset);
660   READ_UINT16 (stream, class->ClassFormat);
661   if (class->ClassFormat == 1)
662     {
663       READ_GLYPHID (stream, class->f.f1.StartGlyph);
664       class->f.f1.GlyphCount
665         = (read_glyph_ids
666            (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
667       if (! class->f.f1.GlyphCount)
668         OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
669     }
670   else if (class->ClassFormat == 2)
671     {
672       class->f.f2.ClassRangeCount
673         = (read_range_records
674            (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
675       if (! class->f.f2.ClassRangeCount)
676         OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
677     }
678   else
679     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
680   return 0;
681 }
682
683
684 static int
685 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
686 {
687   char *errfmt = "ClassDef%s";
688   int errret = -1;
689   OTF_StreamState state;
690   
691   READ_OFFSET (stream, class->offset);
692   SAVE_STREAM (stream, state);
693   SEEK_STREAM (stream, offset + class->offset);
694   READ_UINT16 (stream, class->ClassFormat);
695   if (class->ClassFormat == 1)
696     {
697       READ_GLYPHID (stream, class->f.f1.StartGlyph);
698       class->f.f1.GlyphCount
699         = (read_glyph_ids
700            (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
701       if (! class->f.f1.GlyphCount)
702         return -1;
703     }
704   else if (class->ClassFormat == 2)
705     {
706       class->f.f2.ClassRangeCount
707         = (read_range_records
708            (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
709       if (! class->f.f2.ClassRangeCount)
710         return -1;
711     }
712   else
713     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
714
715   RESTORE_STREAM (stream, state);
716   return 0;
717 }
718
719
720 static int
721 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
722                    OTF_DeviceTable *table)
723 {
724   char *errfmt = "Device Table%s";
725   int errret = -1;
726
727   int num, i;
728   unsigned val;
729   struct {
730     int int2 : 2;
731     int int4 : 4;
732     int int8 : 8;
733   } intval;
734
735   SEEK_STREAM (stream, offset + table->offset);
736   READ_UINT16 (stream, table->StartSize);
737   READ_UINT16 (stream, table->EndSize);
738   READ_UINT16 (stream, table->DeltaFormat);
739   num = table->EndSize - table->StartSize + 1;
740   OTF_MALLOC (table->DeltaValue, num, "");
741
742   if (table->DeltaFormat == 1)
743     for (i = 0; i < num; i++)
744       {
745         if ((i % 8) == 0)
746           READ_UINT16 (stream, val);
747         intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
748         table->DeltaValue[i] = intval.int2;
749       }
750   else if (table->DeltaFormat == 2)
751     for (i = 0; i < num; i++)
752       {
753         if ((i % 4) == 0)
754           READ_UINT16 (stream, val);
755         intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
756         table->DeltaValue[i] = intval.int4;
757       }
758   else if (table->DeltaFormat == 3)
759     for (i = 0; i < num; i++)
760       {
761         if ((i % 2) == 0)
762           {
763             READ_UINT16 (stream, val);
764             intval.int8 = val >> 8;
765             table->DeltaValue[i] = intval.int8;
766           }
767         else
768           {
769             intval.int8 = val >> 8;
770             table->DeltaValue[i] = intval.int8;
771           }
772       }
773   else
774     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
775   return 0;
776 }
777
778 \f
779 /* GSUB */
780
781 static void *
782 read_gsub_table (OTF *otf, OTF_Stream *stream)
783 {
784   char *errfmt = "GSUB%s";
785   void *errret = NULL;
786   OTF_GSUB *gsub;
787
788   OTF_CALLOC (gsub, 1, "");
789   READ_FIXED (stream, gsub->Version);
790   READ_OFFSET (stream, gsub->ScriptList.offset);
791   READ_OFFSET (stream, gsub->FeatureList.offset);
792   READ_OFFSET (stream, gsub->LookupList.offset);
793   
794   if (read_script_list (otf, stream, gsub->ScriptList.offset,
795                            &gsub->ScriptList) < 0
796       || read_feature_list (otf, stream, gsub->FeatureList.offset,
797                             &gsub->FeatureList) < 0
798       || read_lookup_list (otf, stream, gsub->LookupList.offset,
799                            &gsub->LookupList, 1) < 0)
800     return NULL;
801   return gsub;
802 }
803
804 static unsigned
805 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
806 {
807   char *errfmt = "Sequence%s";
808   unsigned errret = 0;
809   unsigned count;
810   int i;
811
812   READ_UINT16 (stream, count);
813   OTF_MALLOC (*seq, count, "");
814   if (! count)
815     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
816   for (i = 0; i < count; i++)
817     READ_OFFSET (stream, (*seq)[i].offset);
818   for (i = 0; i < count; i++)
819     {
820       SEEK_STREAM (stream, offset + (*seq)[i].offset);
821       (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
822                                              &(*seq)[i].Substitute, 0);
823       if (! (*seq)[i].GlyphCount)
824         return 0;
825     }
826   return count;
827 }
828
829 static int
830 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
831                OTF_Ligature **ligature)
832 {
833   char *errfmt = "Ligature%s";
834   int errret = -1;
835   int count;
836   int i;
837
838   READ_UINT16 (stream, count);
839   if (! count)
840     return 0;
841   OTF_MALLOC (*ligature, count, "");
842   for (i = 0; i < count; i++)
843     READ_OFFSET (stream, (*ligature)[i].offset);
844   for (i = 0; i < count; i++)
845     {
846       SEEK_STREAM (stream, offset + (*ligature)[i].offset);
847       READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
848       (*ligature)[i].CompCount
849         = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1);
850       if (! (*ligature)[i].CompCount)
851         return -1;
852     }
853   return count;
854 }
855
856 static int
857 read_ligature_set (OTF *otf, OTF_Stream *stream, long offset,
858                    OTF_LigatureSet **ligset)
859 {
860   char *errfmt = "LigatureSet%s";
861   int errret = -1;
862   int count;
863   int i;
864
865   READ_UINT16 (stream, count);
866   if (! count)
867     return 0;
868   OTF_MALLOC (*ligset, count, "");
869   for (i = 0; i < count; i++)
870     READ_OFFSET (stream, (*ligset)[i].offset);
871   for (i = 0; i < count; i++)
872     {
873       int lig_count;
874
875       SEEK_STREAM (stream, offset + (*ligset)[i].offset);
876       lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
877                                  &(*ligset)[i].Ligature);
878       if (lig_count < 0)
879         return -1;
880       (*ligset)[i].LigatureCount = (unsigned) lig_count;
881     }
882   return count;
883 }
884
885 static unsigned
886 read_subst_lookup_record (OTF *otf, OTF_Stream *stream,
887                           OTF_SubstLookupRecord **record)
888 {
889   char *errfmt = "SubstLookupRecord%s";
890   unsigned errret = 0;
891   unsigned count;
892   int i;
893
894   READ_UINT16 (stream, count);
895   if (! count)
896     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
897   OTF_MALLOC (*record, count, "");
898   for (i = 0; i < count; i++)
899     {
900       READ_UINT16 (stream, (*record)[i].SequenceIndex);
901       READ_UINT16 (stream, (*record)[i].LookupListIndex);
902     }
903   return count;
904 }
905
906 static unsigned
907 read_chain_subrule (OTF *otf, OTF_Stream *stream, long offset,
908                     OTF_ChainSubRule **rule)
909 {
910   char *errfmt = "ChainSubRule%s";
911   unsigned errret = 0;
912   unsigned count;
913   int i;
914
915   READ_UINT16 (stream, count);
916   if (! count)
917     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
918   OTF_MALLOC (*rule, count, "");
919   for (i = 0; i < count; i++)
920     READ_OFFSET (stream, (*rule)[i].offset);
921   for (i = 0; i < count; i++)
922     {
923       SEEK_STREAM (stream, offset + (*rule)[i].offset);
924       (*rule)[i].BacktrackGlyphCount
925         = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0);
926       if (! (*rule)[i].BacktrackGlyphCount)
927         return 0;
928       (*rule)[i].InputGlyphCount
929         = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1);
930       if (! (*rule)[i].InputGlyphCount)
931         return 0;
932       (*rule)[i].LookaheadGlyphCount
933         = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0);
934       if (! (*rule)[i].LookaheadGlyphCount)
935         return 0;
936       (*rule)[i].SubstCount
937         = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
938       if (! (*rule)[i].SubstCount)
939         return 0;
940     }
941   return count;
942 }
943
944
945 static unsigned
946 read_chain_subrule_set (OTF *otf, OTF_Stream *stream, long offset,
947                         OTF_ChainSubRuleSet **set)
948 {
949   char *errfmt = "ChainSubRuleSet%s";
950   unsigned errret = 0;
951   unsigned count;
952   int i;
953
954   READ_UINT16 (stream, count);
955   if (! count)
956     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
957   OTF_MALLOC (*set, count, "");
958   for (i = 0; i < count; i++)
959     READ_OFFSET (stream, (*set)[i].offset);
960   for (i = 0; i < count; i++)
961     {
962       SEEK_STREAM (stream, offset + (*set)[i].offset);
963       (*set)[i].ChainSubRuleCount
964         = read_chain_subrule (otf, stream, offset + (*set)[i].offset,
965                                &(*set)[i].ChainSubRule);
966       if (! (*set)[i].ChainSubRuleCount)
967         return 0;
968     }
969   return count;
970 }
971
972 static unsigned
973 read_chain_subclass_rule (OTF *otf, OTF_Stream *stream, long offset,
974                           OTF_ChainSubClassRule **rule)
975 {
976   char *errfmt = "ChainSubClassRule%s";
977   unsigned errret = 0;
978   unsigned count;
979   int i;
980
981   READ_UINT16 (stream, count);
982   if (! count)
983     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
984   OTF_MALLOC (*rule, count, "");
985   for (i = 0; i < count; i++)
986     READ_OFFSET (stream, (*rule)[i].offset);
987   for (i = 0; i < count; i++)
988     {
989       SEEK_STREAM (stream, offset + (*rule)[i].offset);
990       (*rule)[i].BacktrackGlyphCount
991         = read_glyph_ids (otf, stream,
992                           (OTF_GlyphID **) &(*rule)[i].Backtrack, 0);
993       if (! (*rule)[i].BacktrackGlyphCount)
994         return 0;
995       (*rule)[i].InputGlyphCount
996         = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Input, -1);
997       if (! (*rule)[i].InputGlyphCount)
998         return 0;
999       (*rule)[i].LookaheadGlyphCount
1000         = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].LookAhead, 0);
1001       if (! (*rule)[i].LookaheadGlyphCount)
1002         return 0;
1003       (*rule)[i].SubstCount
1004         = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
1005       if (! (*rule)[i].SubstCount)
1006         return 0;
1007     }
1008   return count;
1009 }
1010
1011 static unsigned
1012 read_chain_subclass_set (OTF *otf, OTF_Stream *stream, long offset,
1013                          OTF_ChainSubClassSet **set)
1014 {
1015   char *errfmt = "ChainSubClassSet%s";
1016   unsigned errret = 0;
1017   unsigned count;
1018   int i;
1019
1020   READ_UINT16 (stream, count);
1021   if (! count)
1022     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1023   OTF_MALLOC (*set, count, "");
1024   for (i = 0; i < count; i++)
1025     READ_OFFSET (stream, (*set)[i].offset);
1026   for (i = 0; i < count; i++)
1027     {
1028       SEEK_STREAM (stream, offset + (*set)[i].offset);
1029       (*set)[i].ChainSubClassRuleCnt
1030         = read_chain_subclass_rule (otf, stream, offset + (*set)[i].offset,
1031                                     &(*set)[i].ChainSubClassRule);
1032       if (! (*set)[i].ChainSubClassRuleCnt)
1033         return 0;
1034     }
1035   return count;
1036 }
1037
1038
1039 static int 
1040 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1041                            unsigned type, OTF_LookupSubTableGSUB *subtable)
1042 {
1043   char *errfmt = "GSUB LookupSubTable%s";
1044   int errret = -1;
1045   int count;
1046
1047   SEEK_STREAM (stream, offset);
1048   READ_UINT16 (stream, subtable->Format);
1049   switch (type)
1050     {
1051     case 1:
1052       if (subtable->Format == 1)
1053         {
1054           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1055             return -1;
1056           READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1057         }
1058       else if (subtable->Format == 2)
1059         {
1060           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1061             return -1;
1062           subtable->u.single2.GlyphCount
1063             = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1064                               0);
1065           if (! subtable->u.single2.GlyphCount)
1066             return -1;
1067         }
1068       else
1069         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1070       break;
1071
1072     case 2:
1073       if (subtable->Format == 1)
1074         {
1075           read_coverage (otf, stream, offset, &subtable->Coverage);
1076           subtable->u.multiple1.SequenceCount
1077             = read_sequence (otf, stream, offset,
1078                              &subtable->u.multiple1.Sequence);
1079         }
1080       else
1081         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1082       break;
1083       
1084     case 3:
1085       break;
1086
1087     case 4:
1088       if (subtable->Format == 1)
1089         {
1090           read_coverage (otf, stream, offset, &subtable->Coverage);
1091           count = (read_ligature_set
1092                    (otf, stream, offset,
1093                     &subtable->u.ligature1.LigatureSet));
1094           if (count < 0)
1095             return -1;
1096           subtable->u.ligature1.LigSetCount = (unsigned) count;
1097         }
1098       else
1099         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1100       break;
1101
1102     case 6:
1103       if (subtable->Format == 1)
1104         {
1105           read_coverage (otf, stream, offset, &subtable->Coverage);
1106           subtable->u.chain_context1.ChainSubRuleSetCount
1107             = (read_chain_subrule_set
1108                (otf, stream, offset,
1109                 &subtable->u.chain_context1.ChainSubRuleSet));
1110         }
1111       else if (subtable->Format == 2)
1112         {
1113           read_coverage (otf, stream, offset, &subtable->Coverage);
1114           read_class_def (otf, stream, offset,
1115                           &subtable->u.chain_context2.Backtrack);
1116           read_class_def (otf, stream, offset,
1117                           &subtable->u.chain_context2.Input);
1118           read_class_def (otf, stream, offset,
1119                           &subtable->u.chain_context2.LookAhead);
1120           subtable->u.chain_context2.ChainSubClassSetCnt
1121             = (read_chain_subclass_set
1122                (otf, stream, offset,
1123                 &subtable->u.chain_context2.ChainSubClassSet));
1124         }
1125       else if (subtable->Format == 3)
1126         {
1127           count = (read_coverage_list
1128                    (otf, stream, offset,
1129                     &subtable->u.chain_context3.Backtrack));
1130           if (count < 0)
1131             return -1;
1132           subtable->u.chain_context3.BacktrackGlyphCount
1133             = (unsigned) count;
1134           count = (read_coverage_list
1135                    (otf, stream, offset,
1136                     &subtable->u.chain_context3.Input));
1137           if (count <= 0)
1138             return -1;
1139           subtable->u.chain_context3.InputGlyphCount
1140             = (unsigned) count;
1141           subtable->Coverage = subtable->u.chain_context3.Input[0];
1142           count = (read_coverage_list
1143                    (otf, stream, offset,
1144                     &subtable->u.chain_context3.LookAhead));
1145           subtable->u.chain_context3.LookaheadGlyphCount
1146             = (unsigned) count;
1147           subtable->u.chain_context3.SubstCount
1148             = (read_subst_lookup_record
1149                (otf, stream,
1150                 &subtable->u.chain_context3.SubstLookupRecord));
1151         }
1152       else
1153         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1154       break;
1155
1156
1157     default:
1158         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1159     }
1160   return 0;
1161 }
1162
1163 \f
1164 /* GPOS */
1165
1166 static int
1167 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1168                    enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1169 {
1170   int errret = -1;
1171   OTF_StreamState state;
1172   int size, i;
1173
1174   if (! bit)
1175     return 0;
1176   for (i = 0, size = 0; i < 8; i++)
1177     if (bit & (1 << i))
1178       size += 2;
1179
1180   if (bit & OTF_XPlacement)
1181     READ_INT16 (stream, value_record->XPlacement);
1182   if (bit & OTF_XPlacement)
1183     READ_INT16 (stream, value_record->YPlacement);
1184   if (bit & OTF_XAdvance)
1185     READ_INT16 (stream, value_record->XAdvance);
1186   if (bit & OTF_YAdvance)
1187     READ_INT16 (stream, value_record->YAdvance);
1188   if (bit & OTF_XPlaDevice)
1189     READ_OFFSET (stream, value_record->XPlaDevice.offset);
1190   if (bit & OTF_YPlaDevice)
1191     READ_OFFSET (stream, value_record->YPlaDevice.offset);
1192   if (bit & OTF_XAdvDevice)
1193     READ_OFFSET (stream, value_record->XAdvDevice.offset);
1194   if (bit & OTF_YAdvDevice)
1195     READ_OFFSET (stream, value_record->YAdvDevice.offset);
1196   SAVE_STREAM (stream, state);
1197   if (value_record->XPlaDevice.offset)
1198     {
1199       if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1200         return -1;
1201     }
1202   if (value_record->YPlaDevice.offset)
1203     {
1204       if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1205         return -1;
1206     }
1207   if (value_record->XAdvDevice.offset)
1208     {
1209       if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1210         return -1;
1211     }
1212   if (value_record->YAdvDevice.offset)
1213     {
1214       if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1215         return -1;
1216     }
1217   RESTORE_STREAM (stream, state);
1218   return 0;
1219 }
1220
1221
1222 static int
1223 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1224 {
1225   char *errfmt = "Anchor%s";
1226   int errret = -1;
1227
1228   SEEK_STREAM (stream, offset + anchor->offset);
1229   READ_UINT16 (stream, anchor->AnchorFormat);
1230   READ_INT16 (stream, anchor->XCoordinate);
1231   READ_INT16 (stream, anchor->YCoordinate);
1232   if (anchor->AnchorFormat == 1)
1233     ;
1234   else if (anchor->AnchorFormat == 2)
1235     {
1236       READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1237     }
1238   else if (anchor->AnchorFormat == 3)
1239     {
1240       READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1241       READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1242       if (anchor->f.f2.XDeviceTable.offset)
1243         {
1244           if (read_device_table (otf, stream, offset + anchor->offset,
1245                                  &anchor->f.f2.XDeviceTable) < 0)
1246             return -1;
1247         }
1248       if (anchor->f.f2.YDeviceTable.offset)
1249         {
1250           if (read_device_table (otf, stream, offset + anchor->offset,
1251                                  &anchor->f.f2.YDeviceTable) < 0)
1252             return -1;
1253         }
1254     }
1255   else
1256     OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1257
1258   return 0;
1259 }
1260
1261 static int
1262 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1263                  OTF_MarkArray *array)
1264 {
1265   char *errfmt = "MarkArray%s";
1266   int errret = -1;
1267   OTF_StreamState state;
1268   int i;
1269   
1270   READ_OFFSET (stream, array->offset);
1271   SAVE_STREAM (stream, state);
1272   SEEK_STREAM (stream, offset + array->offset);
1273   READ_UINT16 (stream, array->MarkCount);
1274   OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1275   for (i = 0; i < array->MarkCount; i++)
1276     {
1277       READ_UINT16 (stream, array->MarkRecord[i].Class);
1278       READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1279     }
1280   for (i = 0; i < array->MarkCount; i++)
1281     if (read_anchor (otf, stream, offset + array->offset,
1282                      &array->MarkRecord[i].MarkAnchor) < 0)
1283       return -1;;
1284   RESTORE_STREAM (stream, state);
1285   return 0;
1286 }
1287
1288 static int
1289 read_base_array (OTF *otf, OTF_Stream *stream, long offset,
1290                  unsigned ClassCount, OTF_BaseArray *array)
1291 {
1292   char *errfmt = "BaseArray%s";
1293   int errret = -1;
1294   OTF_StreamState state;
1295   int i, j;
1296   
1297   READ_OFFSET (stream, array->offset);
1298   SAVE_STREAM (stream, state);
1299   SEEK_STREAM (stream, offset + array->offset);
1300   READ_UINT16 (stream, array->BaseCount);
1301   OTF_MALLOC (array->BaseRecord, array->BaseCount, "");
1302   for (i = 0; i < array->BaseCount; i++)
1303     {
1304       OTF_MALLOC (array->BaseRecord[i].BaseAnchor, ClassCount,
1305                   " (BaseRecord)");
1306       for (j = 0; j < ClassCount; j++)
1307         READ_OFFSET (stream, array->BaseRecord[i].BaseAnchor[j].offset);
1308     }
1309   for (i = 0; i < array->BaseCount; i++)
1310     for (j = 0; j < ClassCount; j++)
1311       if (read_anchor (otf, stream, offset + array->offset,
1312                        &array->BaseRecord[i].BaseAnchor[j]) < 0)
1313         return -1;
1314   RESTORE_STREAM (stream, state);
1315   return 0;
1316 }
1317
1318
1319 static OTF_Class1Record *
1320 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1321                          unsigned num1, enum OTF_ValueFormat bit1,
1322                          unsigned num2, enum OTF_ValueFormat bit2)
1323 {
1324   char *errfmt = "Class1Record%s";
1325   void *errret = NULL;
1326   OTF_Class1Record *rec;
1327   int i, j;
1328
1329   OTF_MALLOC (rec, num1, "");
1330   for (i = 0; i < num1; i++)
1331     {
1332       OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1333       for (j = 0; j < num2; j++)
1334         {
1335           if (read_value_record (otf, stream, offset,
1336                                  bit1, &rec[i].Class2Record[j].Value1) < 0
1337               || read_value_record (otf, stream, offset,
1338                                     bit2, &rec[i].Class2Record[j].Value2) < 0)
1339             return NULL;
1340         }
1341     }
1342   return rec;
1343 }
1344
1345
1346 static int 
1347 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1348                            long offset, unsigned type,
1349                            OTF_LookupSubTableGPOS *subtable)
1350 {
1351   char *errfmt = "GPOS LookupSubTable%s";
1352   int errret = -1;
1353
1354   SEEK_STREAM (stream, offset);
1355   READ_UINT16 (stream, subtable->Format);
1356   switch (type)
1357     {
1358     case 1:
1359 #if 0
1360       if (subtable->Format == 1)
1361         {
1362           read_coverage (otf, stream, offset, &subtable->Coverage);
1363           subtable->u.single1.DeltaGlyphID = READ_INT16 (stream);
1364         }
1365       else if (subtable->Format == 2)
1366         {
1367           read_coverage (otf, stream, offset, &subtable->Coverage);
1368           subtable->u.single2.GlyphCount
1369             = read_glyph_ids (otf, stream,
1370                               &subtable->u.single2.Substitute, 0);
1371         }
1372       else
1373         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1374 #endif
1375       break;
1376
1377     case 2:
1378       if (subtable->Format == 1)
1379         {
1380           read_coverage (otf, stream, offset, &subtable->Coverage);
1381         }
1382       else if (subtable->Format == 2)
1383         {
1384           SEEK_STREAM (stream, offset + 2);
1385           read_coverage (otf, stream, offset, &subtable->Coverage);
1386           READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
1387           READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
1388           read_class_def (otf, stream, offset,
1389                           &subtable->u.pair2.ClassDef1);
1390           read_class_def (otf, stream, offset,
1391                           &subtable->u.pair2.ClassDef2);
1392           READ_UINT16 (stream, subtable->u.pair2.Class1Count);
1393           READ_UINT16 (stream, subtable->u.pair2.Class2Count);
1394           subtable->u.pair2.Class1Record
1395             = read_class1_record_list (otf, stream, offset,
1396                                        subtable->u.pair2.Class1Count,
1397                                        subtable->u.pair2.ValueFormat1,
1398                                        subtable->u.pair2.Class2Count,
1399                                        subtable->u.pair2.ValueFormat2);
1400         }
1401       else
1402         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1403       break;
1404       
1405     case 4:
1406       if (subtable->Format == 1)
1407         {
1408           read_coverage (otf, stream, offset, &subtable->Coverage);
1409           read_coverage (otf, stream, offset,
1410                          &subtable->u.mark_base1.BaseCoverage);
1411           READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
1412           read_mark_array (otf, stream, offset,
1413                            &subtable->u.mark_base1.MarkArray);
1414           read_base_array (otf, stream, offset,
1415                            subtable->u.mark_base1.ClassCount,
1416                            &subtable->u.mark_base1.BaseArray);
1417         }
1418       else
1419         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1420       break;
1421
1422     case 6:
1423 #if 0
1424       if (subtable->Format == 1)
1425         {
1426           read_coverage (otf, stream, offset,
1427                          &subtable->u.chain_context1.Coverage);
1428           subtable->u.chain_context1.ChainSubRuleSetCount
1429             = (read_chain_subrule_set
1430                (otf, stream, offset,
1431                 &subtable->u.chain_context1.ChainSubRuleSet));
1432         }
1433       else if (subtable->Format == 2)
1434         {
1435           read_coverage (otf, stream, offset,
1436                          &subtable->u.chain_context2.Coverage);
1437           read_class_def (otf, stream, offset,
1438                           &subtable->u.chain_context2.Backtrack);
1439           read_class_def (otf, stream, offset,
1440                           &subtable->u.chain_context2.Input);
1441           read_class_def (otf, stream, offset,
1442                           &subtable->u.chain_context2.LookAhead);
1443           subtable->u.chain_context2.ChainSubClassSetCnt
1444             = (read_chain_subclass_set
1445                (otf, stream, offset,
1446                 &subtable->u.chain_context2.ChainSubClassSet));
1447         }
1448       else if (subtable->Format == 3)
1449         {
1450           subtable->u.chain_context3.BacktrackGlyphCount
1451             = (read_coverage_list
1452                (otf, stream, offset,
1453                 &subtable->u.chain_context3.Backtrack));
1454           subtable->u.chain_context3.InputGlyphCount
1455             = (read_coverage_list
1456                (otf, stream, offset,
1457                 &subtable->u.chain_context3.Input));
1458           subtable->u.chain_context3.LookaheadGlyphCount
1459             = (read_coverage_list
1460                (otf, stream, offset,
1461                 &subtable->u.chain_context3.LookAhead));
1462           subtable->u.chain_context3.SubstCount
1463             = (read_subst_lookup_record
1464                (otf, stream,
1465                 &subtable->u.chain_context3.SubstLookupRecord));
1466         }
1467       else
1468         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1469 #endif
1470
1471     default:
1472         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1473     }
1474   return 0;
1475 }
1476
1477
1478 static void *
1479 read_gpos_table (OTF *otf, OTF_Stream *stream)
1480 {
1481   char *errfmt = "GPOS%s";
1482   void *errret = NULL;
1483   OTF_GPOS *gpos;
1484
1485   OTF_CALLOC (gpos, 1, "");
1486   READ_FIXED (stream, gpos->Version);
1487   READ_OFFSET (stream, gpos->ScriptList.offset);
1488   READ_OFFSET (stream, gpos->FeatureList.offset);
1489   READ_OFFSET (stream, gpos->LookupList.offset);
1490
1491   if (read_script_list (otf, stream, gpos->ScriptList.offset,
1492                            &gpos->ScriptList) < 0
1493       || read_feature_list (otf, stream, gpos->FeatureList.offset,
1494                             &gpos->FeatureList) < 0
1495       || read_lookup_list (otf, stream, gpos->LookupList.offset,
1496                            &gpos->LookupList, 0) < 0)
1497     return NULL;
1498   return gpos;
1499 }
1500
1501 \f
1502 #if 0
1503 /* BASE */
1504
1505 static OTF_BASE *
1506 read_base_table (OTF_Stream *stream, long offset)
1507 {
1508   OTF_BASE *base;
1509
1510   OTF_MALLOC (base, 1);
1511
1512   return base;
1513 }
1514
1515 \f
1516 /* JSTF */
1517
1518 static OTF_JSTF *
1519 read_jstf_table (OTF_Stream *stream, long offset)
1520 {
1521   OTF_JSTF *jstf;
1522
1523   OTF_MALLOC (jstf, 1);
1524
1525   return jstf;
1526 }
1527 #endif
1528 \f
1529 /* GDEF */
1530 static int
1531 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1532                   OTF_AttachList *list)
1533 {
1534   char *errfmt = "AttachList%s";
1535   int errret = -1;
1536   int i, j;
1537
1538   if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1539     return -1;
1540   READ_UINT16 (stream, list->GlyphCount);
1541   OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1542   for (i = 0; i < list->GlyphCount; i++)
1543     READ_OFFSET (stream, list->AttachPoint[i].offset);
1544   for (i = 0; i < list->GlyphCount; i++)
1545     {
1546       int count;
1547
1548       SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1549       READ_UINT16 (stream, count);
1550       list->AttachPoint[i].PointCount = count;
1551       OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1552       for (j = 0; j < count; j++)
1553         READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1554     }
1555   return 0;
1556 }
1557
1558 static int
1559 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1560                   OTF_CaretValue *caret)
1561 {
1562   char *errfmt = "CaretValue%s";
1563   int errret = -1;
1564
1565   SEEK_STREAM (stream, offset + caret->offset);
1566   READ_UINT16 (stream, caret->CaretValueFormat);
1567   if (caret->CaretValueFormat == 1)
1568     READ_INT16 (stream, caret->f.f1.Coordinate);
1569   else if (caret->CaretValueFormat == 2)
1570     READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1571   else if (caret->CaretValueFormat == 3)
1572     {
1573       READ_INT16 (stream, caret->f.f3.Coordinate);
1574       if (read_device_table (otf, stream, offset + caret->offset,
1575                              &caret->f.f3.DeviceTable) < 0)
1576         return -1;
1577     }
1578   else
1579     OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1580   return 0;
1581 }
1582
1583 static int
1584 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1585                      OTF_LigCaretList *list)
1586 {
1587   char *errfmt = "LigCaretList%s";
1588   int errret = -1;
1589   int i, j;
1590
1591   if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1592     return -1;
1593   READ_UINT16 (stream, list->LigGlyphCount);
1594   OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1595   for (i = 0; i < list->LigGlyphCount; i++)
1596     READ_OFFSET (stream, list->LigGlyph[i].offset);
1597   for (i = 0; i < list->LigGlyphCount; i++)
1598     {
1599       int count;
1600
1601       SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1602       READ_UINT16 (stream, count);
1603       list->LigGlyph[i].CaretCount = count;
1604       OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1605       for (j = 0; j < count; j++)
1606         READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1607       for (j = 0; j < count; j++)
1608         if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1609                               &list->LigGlyph[i].CaretValue[j]) < 0)
1610           return -1;
1611     }
1612   return 0;
1613 }
1614
1615 static int
1616 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1617 {
1618   int errret = -1;
1619
1620   READ_FIXED (stream, header->Version);
1621   READ_OFFSET (stream, header->GlyphClassDef);
1622   READ_OFFSET (stream, header->AttachList);
1623   READ_OFFSET (stream, header->LigCaretList);
1624   READ_OFFSET (stream, header->MarkAttachClassDef);
1625   return 0;
1626 }
1627
1628 static void *
1629 read_gdef_table (OTF *otf, OTF_Stream *stream)
1630 {
1631   char *errfmt = "GDEF%s";
1632   void *errret = NULL;
1633   OTF_GDEF *gdef;
1634
1635   OTF_CALLOC (gdef, 1, "");
1636   read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1637   if (gdef->header.GlyphClassDef)
1638     {
1639       gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1640       read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1641     }
1642   if (gdef->header.AttachList)
1643     read_attach_list (otf, stream, gdef->header.AttachList,
1644                       &gdef->attach_list);
1645   if (gdef->header.LigCaretList)
1646     read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1647                          &gdef->lig_caret_list);
1648   if (gdef->header.MarkAttachClassDef)
1649     {
1650       gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1651       read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1652     }
1653
1654   return gdef;
1655 }
1656
1657 \f
1658
1659 /* cmap */
1660
1661 static void *
1662 read_cmap_table (OTF *otf, OTF_Stream *stream)
1663 {
1664   char *errfmt = "cmap%s";
1665   void *errret = NULL;
1666   OTF_cmap *cmap;
1667   int i;
1668
1669   OTF_CALLOC (cmap, 1, "");
1670   READ_USHORT (stream, cmap->version);
1671   READ_USHORT (stream, cmap->numTables);
1672   OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
1673   for (i = 0; i < cmap->numTables; i++)
1674     {
1675       READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
1676       READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
1677       READ_ULONG (stream, cmap->EncodingRecord[i].offset);
1678       if (cmap->EncodingRecord[i].platformID == 3
1679           && cmap->EncodingRecord[i].encodingID == 1)
1680         cmap->Unicode = cmap->EncodingRecord + i;
1681     }
1682   for (i = 0; i < cmap->numTables; i++)
1683     {
1684       unsigned format;
1685
1686       SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
1687       READ_USHORT (stream, format);
1688       cmap->EncodingRecord[i].subtable.format = format;
1689       READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
1690       if (format == 8 || format == 10 || format == 12)
1691         {
1692           READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
1693           READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
1694         }
1695       else
1696         {
1697           READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
1698         }
1699       switch (format)
1700         {
1701         case 0:
1702           {
1703             OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
1704                         " (EncodingRecord)");
1705             READ_BYTES (stream,
1706                         cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
1707                         256);
1708           }
1709           break;
1710
1711         case 2:
1712           break;
1713
1714         case 4:
1715           {
1716             OTF_EncodingSubtable4 *sub4;
1717             int segCount;
1718             int j;
1719             unsigned dummy;
1720
1721             OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
1722             cmap->EncodingRecord[i].subtable.f.f4 = sub4;
1723             READ_USHORT (stream, sub4->segCountX2);
1724             segCount = sub4->segCountX2 / 2;
1725             READ_USHORT (stream, sub4->searchRange);
1726             READ_USHORT (stream, sub4->entrySelector);
1727             READ_USHORT (stream, sub4->rangeShift);
1728             OTF_MALLOC (sub4->segments, segCount, " (segCount)");
1729             for (j = 0; j < segCount; j++)
1730               READ_USHORT (stream, sub4->segments[j].endCount);
1731             READ_USHORT (stream, dummy);
1732             for (j = 0; j < segCount; j++)
1733               READ_USHORT (stream, sub4->segments[j].startCount);
1734             for (j = 0; j < segCount; j++)
1735               READ_SHORT (stream, sub4->segments[j].idDelta);
1736             for (j = 0; j < segCount; j++)
1737               {
1738                 unsigned off;
1739                 unsigned rest = 2 * (segCount - j);
1740                 
1741                 READ_USHORT (stream, off);
1742                 if (off == 0)
1743                   sub4->segments[j].idRangeOffset = 0xFFFF;
1744                 else
1745                   sub4->segments[j].idRangeOffset = (off - rest) / 2;
1746               }
1747             j = (cmap->EncodingRecord[i].subtable.length
1748                  - (14 + 2 * (segCount * 4 + 1)));
1749             sub4->GlyphCount = j / 2;
1750             OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
1751             for (j = 0; j < sub4->GlyphCount; j++)
1752               READ_USHORT (stream, sub4->glyphIdArray[j]);
1753           }
1754         }
1755     }
1756   return cmap;
1757 }
1758
1759 \f
1760
1761 /* TABLE: name */
1762
1763 static char *
1764 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
1765 {
1766   char *errfmt = "nameID (%d)";
1767   void *errret = NULL;
1768   OTF_StreamState state;
1769   char *str;
1770   int i;
1771   int c;
1772
1773   SAVE_STREAM (stream, state);
1774   SEEK_STREAM (stream, stream->pos + rec->offset);
1775
1776   if (bytes == 1)
1777     {
1778       OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
1779       READ_BYTES (stream, str, rec->length);
1780       for (i = 0; i < rec->length; i++)
1781         if (str[i] < 0)
1782           str[i] = '?';
1783     }
1784   else if (bytes == 2)
1785     {
1786       OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
1787       for (i = 0; i < rec->length / 2; i++)
1788         {
1789           READ_USHORT (stream, c);
1790           if (c >= 128)
1791             c = '?';
1792           str[i] = c;
1793         }
1794     }
1795   else if (bytes == 4)
1796     {
1797       OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
1798       for (i = 0; i < rec->length / 4; i++)
1799         {
1800           READ_ULONG (stream, c);
1801           if (c >= 128)
1802             c = '?';
1803           str[i] = c;
1804         }
1805     }
1806   str[i] = '\0';
1807   RESTORE_STREAM (stream, state);
1808   return str;
1809 }
1810
1811 static void *
1812 read_name_table (OTF *otf, OTF_Stream *stream)
1813 {
1814   char *errfmt = "name%s";
1815   void *errret = NULL;
1816   OTF_name *name;
1817   int i;
1818
1819   OTF_CALLOC (name, 1, "");
1820   READ_USHORT (stream, name->format);
1821   READ_USHORT (stream, name->count);
1822   READ_USHORT (stream, name->stringOffset);
1823   OTF_MALLOC (name->nameRecord, name->count, "");
1824   for (i = 0; i < name->count; i++)
1825     {
1826       OTF_NameRecord *rec = name->nameRecord + i;
1827
1828       READ_USHORT (stream, rec->platformID);
1829       READ_USHORT (stream, rec->encodingID);
1830       READ_USHORT (stream, rec->languageID);
1831       READ_USHORT (stream, rec->nameID);
1832       READ_USHORT (stream, rec->length);
1833       READ_USHORT (stream, rec->offset);
1834     }
1835   for (i = 0; i < name->count; i++)
1836     {
1837       OTF_NameRecord *rec = name->nameRecord + i;
1838       int nameID = rec->nameID;
1839
1840       if (nameID <= OTF_max_nameID
1841           && ! name->name[nameID])
1842         {
1843           if (rec->platformID == 0)
1844             name->name[nameID] = read_name (otf, stream, rec,
1845                                             rec->encodingID <= 3 ? 2 : 4);
1846           else if (rec->platformID == 1
1847                    && rec->encodingID == 0)
1848             name->name[nameID] = read_name (otf, stream, rec, 1);
1849           else if (rec->platformID == 3
1850                    && (rec->encodingID == 1 || rec->encodingID == 10))
1851             name->name[nameID] = read_name (otf, stream,
1852                                             rec, rec->encodingID == 1 ? 2 : 4);
1853         }
1854     }
1855
1856   return name;
1857 }
1858
1859 \f
1860
1861 /* APIs */
1862
1863 OTF_Tag
1864 otf_tag (char *name)
1865 {
1866   unsigned char *p = (unsigned char *) name;
1867
1868   if (! name)
1869     return (OTF_Tag) 0;
1870   return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1871 }
1872
1873 void
1874 otf_tag_name (OTF_Tag tag, char *name)
1875 {
1876   name[0] = (char) (tag >> 24);
1877   name[1] = (char) ((tag >> 16) & 0xFF);
1878   name[2] = (char) ((tag >> 8) & 0xFF);
1879   name[3] = (char) (tag & 0xFF);
1880   name[4] = '\0';
1881 }
1882
1883
1884 /* We can't use memory allocation macros in the following functions
1885    because those macros returns from the functions before freeing
1886    memory previously allocated.  */
1887
1888 OTF *
1889 otf_open (char *otf_name)
1890 {
1891   FILE *fp;
1892   char *errfmt = "opening otf (%s)";
1893   void *errret = NULL;
1894   OTF *otf;
1895   OTF_InternalData *internal_data;
1896
1897   fp = fopen (otf_name, "r");
1898   if (! fp)
1899     OTF_ERROR (OTF_ERROR_FILE, otf_name);
1900   otf = calloc (1, sizeof (OTF));
1901   if (! otf)
1902     OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
1903   otf->filename = strdup (otf_name);
1904   if (! otf->filename)
1905     {
1906       otf_close (otf);
1907       fclose (fp);
1908       OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
1909     }
1910
1911   internal_data = calloc (1, sizeof (OTF_InternalData));
1912   if (! internal_data)
1913     OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
1914   otf->internal_data = internal_data;
1915   if (! allocate_memory_record (otf))
1916     OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
1917
1918   /* Here after, all pointers to allocated memory are recorded in
1919      otf->internal_data->memory_record except for what allocated by
1920      the functions allocate_memory_record and make_stream.  */
1921
1922   if (read_header_part (otf, fp) < 0)
1923     {
1924       otf_close (otf);
1925       fclose (fp);
1926       return NULL;
1927     }
1928
1929   fclose (fp);
1930   return otf;
1931 }
1932
1933
1934 void
1935 otf_close (OTF *otf)
1936 {
1937   OTF_InternalData *internal_data = otf->internal_data;
1938   int i;
1939
1940   if (internal_data)
1941     {
1942       OTF_MemoryRecord *memrec = internal_data->memory_record;
1943
1944       if (internal_data->header_stream)
1945         free_stream (internal_data->header_stream);
1946
1947       for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
1948         if (internal_data->table_info[i].stream)
1949           free_stream (internal_data->table_info[i].stream);
1950
1951       while (memrec)
1952         {
1953           OTF_MemoryRecord *next = memrec->next;
1954
1955           for (i = memrec->used - 1; i >= 0; i--)
1956             free (memrec->memory[i]);
1957           free (memrec);
1958           memrec = next;
1959         }
1960       free (internal_data);
1961     }
1962   free (otf);
1963 }
1964
1965
1966 int
1967 otf_get_table (OTF *otf, char *name)
1968 {
1969   char *errfmt = "OTF Table Read";
1970   int errret = -1;
1971   OTF_InternalData *internal_data = otf->internal_data;
1972   OTF_TableInfo *table_info;
1973   OTF_Tag tag = otf_tag (name);
1974
1975   if (! tag)
1976     OTF_ERROR (OTF_ERROR_TABLE, " (unknown)");
1977
1978   if (tag == otf_tag ("head"))
1979     table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
1980   else if (tag == otf_tag ("name"))
1981     table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
1982   else if (tag == otf_tag ("cmap"))
1983     table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
1984   else if (tag == otf_tag ("GDEF"))
1985     table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
1986   else if (tag == otf_tag ("GSUB"))
1987     table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
1988   else if (tag == otf_tag ("GPOS"))
1989     table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
1990   else
1991     OTF_ERROR (OTF_ERROR_TABLE, " (unsupported)");
1992
1993   if (*table_info->address)
1994     return 0;
1995   if (! table_info->stream)
1996     OTF_ERROR (OTF_ERROR_TABLE, " (not found)");
1997   if (! table_info->reader)
1998     OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
1999
2000   *table_info->address = (*table_info->reader) (otf, table_info->stream);
2001   free_stream (table_info->stream);
2002   table_info->stream = NULL;
2003   if (! *table_info->address)
2004     {
2005       table_info->reader = NULL;
2006       OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2007     }
2008
2009   return 0;
2010 }