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