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