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