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