(read_value_record): Clear value_record at
[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   memset (value_record, 0, sizeof (OTF_ValueRecord));
1874   if (! bit)
1875     return 0;
1876   for (i = 0, size = 0; i < 8; i++)
1877     if (bit & (1 << i))
1878       size += 2;
1879
1880   if (bit & OTF_XPlacement)
1881     READ_INT16 (stream, value_record->XPlacement);
1882   if (bit & OTF_XPlacement)
1883     READ_INT16 (stream, value_record->YPlacement);
1884   if (bit & OTF_XAdvance)
1885     READ_INT16 (stream, value_record->XAdvance);
1886   if (bit & OTF_YAdvance)
1887     READ_INT16 (stream, value_record->YAdvance);
1888   if (bit & OTF_XPlaDevice)
1889     READ_OFFSET (stream, value_record->XPlaDevice.offset);
1890   if (bit & OTF_YPlaDevice)
1891     READ_OFFSET (stream, value_record->YPlaDevice.offset);
1892   if (bit & OTF_XAdvDevice)
1893     READ_OFFSET (stream, value_record->XAdvDevice.offset);
1894   if (bit & OTF_YAdvDevice)
1895     READ_OFFSET (stream, value_record->YAdvDevice.offset);
1896   SAVE_STREAM (stream, state);
1897   if (value_record->XPlaDevice.offset)
1898     {
1899       if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1900         return -1;
1901     }
1902   if (value_record->YPlaDevice.offset)
1903     {
1904       if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1905         return -1;
1906     }
1907   if (value_record->XAdvDevice.offset)
1908     {
1909       if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1910         return -1;
1911     }
1912   if (value_record->YAdvDevice.offset)
1913     {
1914       if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1915         return -1;
1916     }
1917   RESTORE_STREAM (stream, state);
1918   return 0;
1919 }
1920
1921
1922 static int
1923 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1924 {
1925   char *errfmt = "Anchor%s";
1926   int errret = -1;
1927
1928   SEEK_STREAM (stream, offset + anchor->offset);
1929   READ_UINT16 (stream, anchor->AnchorFormat);
1930   READ_INT16 (stream, anchor->XCoordinate);
1931   READ_INT16 (stream, anchor->YCoordinate);
1932   if (anchor->AnchorFormat == 1)
1933     ;
1934   else if (anchor->AnchorFormat == 2)
1935     {
1936       READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1937     }
1938   else if (anchor->AnchorFormat == 3)
1939     {
1940       READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1941       READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1942       if (anchor->f.f2.XDeviceTable.offset)
1943         {
1944           if (read_device_table (otf, stream, offset + anchor->offset,
1945                                  &anchor->f.f2.XDeviceTable) < 0)
1946             return -1;
1947         }
1948       if (anchor->f.f2.YDeviceTable.offset)
1949         {
1950           if (read_device_table (otf, stream, offset + anchor->offset,
1951                                  &anchor->f.f2.YDeviceTable) < 0)
1952             return -1;
1953         }
1954     }
1955   else
1956     OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1957
1958   return 0;
1959 }
1960
1961 static int
1962 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1963                  OTF_MarkArray *array)
1964 {
1965   char *errfmt = "MarkArray%s";
1966   int errret = -1;
1967   OTF_StreamState state;
1968   int i;
1969
1970   READ_OFFSET (stream, array->offset);
1971   SAVE_STREAM (stream, state);
1972   SEEK_STREAM (stream, offset + array->offset);
1973   READ_UINT16 (stream, array->MarkCount);
1974   OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1975   for (i = 0; i < array->MarkCount; i++)
1976     {
1977       READ_UINT16 (stream, array->MarkRecord[i].Class);
1978       READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1979     }
1980   for (i = 0; i < array->MarkCount; i++)
1981     if (read_anchor (otf, stream, offset + array->offset,
1982                      &array->MarkRecord[i].MarkAnchor) < 0)
1983       return -1;;
1984   RESTORE_STREAM (stream, state);
1985   return 0;
1986 }
1987
1988 static int
1989 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1990                    unsigned ClassCount, OTF_AnchorArray *array)
1991 {
1992   char *errfmt = "AnchorArray%s";
1993   int errret = -1;
1994   OTF_StreamState state;
1995   int i, j;
1996
1997   READ_OFFSET (stream, array->offset);
1998   SAVE_STREAM (stream, state);
1999   SEEK_STREAM (stream, offset + array->offset);
2000   READ_UINT16 (stream, array->Count);
2001   OTF_MALLOC (array->AnchorRecord, array->Count, "");
2002   for (i = 0; i < array->Count; i++)
2003     {
2004       OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2005                   " (AnchorRecord)");
2006       for (j = 0; j < ClassCount; j++)
2007         READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2008     }
2009   for (i = 0; i < array->Count; i++)
2010     for (j = 0; j < ClassCount; j++)
2011       if (read_anchor (otf, stream, offset + array->offset,
2012                        &array->AnchorRecord[i].Anchor[j]) < 0)
2013         return -1;
2014   RESTORE_STREAM (stream, state);
2015   return 0;
2016 }
2017
2018 static OTF_PairSet *
2019 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2020                     enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2021 {
2022   char *errfmt = "PairSet%s";
2023   void *errret = NULL;
2024   OTF_StreamState state;
2025   OTF_PairSet *set;
2026   int i, j;
2027
2028   OTF_MALLOC (set, num, "");
2029   for (i = 0; i < num; i++)
2030     READ_OFFSET (stream, set[i].offset);
2031   SAVE_STREAM (stream, state);
2032   for (i = 0; i < num; i++)
2033     {
2034       SEEK_STREAM (stream, offset + set[i].offset);
2035       READ_UINT16 (stream, set[i].PairValueCount);
2036       OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2037       for (j = 0; j < set[i].PairValueCount; j++)
2038         {
2039           OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2040
2041           READ_UINT16 (stream, rec->SecondGlyph);
2042           read_value_record (otf, stream, offset, bit1, &rec->Value1);
2043           read_value_record (otf, stream, offset, bit2, &rec->Value2);
2044         }
2045     }
2046   RESTORE_STREAM (stream, state);
2047   return set;
2048 }
2049
2050 static OTF_Class1Record *
2051 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2052                          unsigned num1, enum OTF_ValueFormat bit1,
2053                          unsigned num2, enum OTF_ValueFormat bit2)
2054 {
2055   char *errfmt = "Class1Record%s";
2056   void *errret = NULL;
2057   OTF_Class1Record *rec;
2058   int i, j;
2059
2060   OTF_MALLOC (rec, num1, "");
2061   for (i = 0; i < num1; i++)
2062     {
2063       OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2064       for (j = 0; j < num2; j++)
2065         {
2066           if (read_value_record (otf, stream, offset,
2067                                  bit1, &rec[i].Class2Record[j].Value1) < 0
2068               || read_value_record (otf, stream, offset,
2069                                     bit2, &rec[i].Class2Record[j].Value2) < 0)
2070             return NULL;
2071         }
2072     }
2073   return rec;
2074 }
2075
2076 static unsigned
2077 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2078                       OTF_EntryExitRecord **rec)
2079 {
2080   char *errfmt = "EntryExitSet%s";
2081   int errret = 0;
2082   unsigned count;
2083   int i;
2084   OTF_StreamState state;
2085
2086   READ_UINT16 (stream, count);
2087   if (! count)
2088     OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2089   OTF_MALLOC (*rec, count, "");
2090   for (i = 0; i < count; i++)
2091     {
2092       READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2093       READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2094     }
2095   SAVE_STREAM (stream, state);
2096   for (i = 0; i < count; i++)
2097     {
2098       if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2099         return -1;
2100       if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2101         return -1;
2102     }
2103   RESTORE_STREAM (stream, state);
2104   return count;
2105 }
2106
2107 static int
2108 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2109                       unsigned ClassCount, OTF_LigatureAttach *attach)
2110 {
2111   char *errfmt = "LigatureAttach%s";
2112   int errret = 1;
2113   int i, j;
2114
2115   SEEK_STREAM (stream, offset + attach->offset);
2116   READ_UINT16 (stream, attach->ComponentCount);
2117   OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2118   for (i = 0; i < attach->ComponentCount; i++)
2119     {
2120       OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2121                   " (ComponentRecord)");
2122       for (j = 0; j < ClassCount; j++)
2123         READ_OFFSET (stream,
2124                      attach->ComponentRecord[i].LigatureAnchor[j].offset);
2125     }
2126   for (i = 0; i < attach->ComponentCount; i++)
2127     for (j = 0; j < ClassCount; j++)
2128       if (read_anchor (otf, stream, offset + attach->offset,
2129                        &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2130         return -1;
2131   return 0;
2132 }
2133
2134 static int
2135 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2136                      unsigned class_count, OTF_LigatureArray *array)
2137 {
2138   char *errfmt = "LigatureArray%s";
2139   int errret = -1;
2140   OTF_StreamState state;
2141   int i;
2142
2143   READ_OFFSET (stream, array->offset);
2144   SAVE_STREAM (stream, state);
2145   SEEK_STREAM (stream, offset + array->offset);
2146   READ_UINT16 (stream, array->LigatureCount);
2147   OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2148   for (i = 0; i < array->LigatureCount; i++)
2149     READ_OFFSET (stream, array->LigatureAttach[i].offset);
2150   for (i = 0; i < array->LigatureCount; i++)
2151     read_ligature_attach (otf, stream, offset + array->offset,
2152                           class_count, array->LigatureAttach + i);
2153   RESTORE_STREAM (stream, state);
2154   return 0;
2155 }
2156
2157 static int
2158 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2159                            long offset, unsigned type,
2160                            OTF_LookupSubTableGPOS *subtable)
2161 {
2162   char errfmt[256];
2163   int errret = -1;
2164
2165   SEEK_STREAM (stream, offset);
2166   READ_UINT16 (stream, subtable->Format);
2167   sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2168   switch (type)
2169     {
2170     case 1:
2171       if (subtable->Format == 1)
2172         {
2173           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2174             return -1;
2175           READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2176           read_value_record (otf, stream, offset,
2177                              subtable->u.single1.ValueFormat,
2178                              &subtable->u.single1.Value);
2179         }
2180       else if (subtable->Format == 2)
2181         {
2182           OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2183           int i;
2184
2185           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2186             return -1;
2187           READ_UINT16 (stream, single2->ValueFormat);
2188           READ_UINT16 (stream, single2->ValueCount);
2189           OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2190           for (i = 0; i < single2->ValueCount; i++)
2191             read_value_record (otf, stream, offset, single2->ValueFormat,
2192                                single2->Value + i);
2193         }
2194       else
2195         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2196       break;
2197
2198     case 2:
2199       if (subtable->Format == 1)
2200         {
2201           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2202             return -1;
2203           READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2204           READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2205           READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2206           subtable->u.pair1.PairSet
2207             = read_pair_set_list (otf, stream, offset,
2208                                   subtable->u.pair1.PairSetCount,
2209                                   subtable->u.pair1.ValueFormat1,
2210                                   subtable->u.pair1.ValueFormat2);
2211           if (! subtable->u.pair1.PairSet)
2212             return -1;
2213         }
2214       else if (subtable->Format == 2)
2215         {
2216           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2217             return -1;
2218           READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2219           READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2220           if (read_class_def (otf, stream, offset,
2221                               &subtable->u.pair2.ClassDef1) < 0
2222               || read_class_def (otf, stream, offset,
2223                                  &subtable->u.pair2.ClassDef2) < 0)
2224             return -1;
2225           READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2226           READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2227           subtable->u.pair2.Class1Record
2228             = read_class1_record_list (otf, stream, offset,
2229                                        subtable->u.pair2.Class1Count,
2230                                        subtable->u.pair2.ValueFormat1,
2231                                        subtable->u.pair2.Class2Count,
2232                                        subtable->u.pair2.ValueFormat2);
2233           if (! subtable->u.pair2.Class1Record)
2234             return -1;
2235         }
2236       else
2237         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2238       break;
2239
2240     case 3:
2241       if (subtable->Format == 1)
2242         {
2243           if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2244             return -1;
2245           subtable->u.cursive1.EntryExitCount
2246             = read_entry_exit_list (otf, stream, offset,
2247                                     &subtable->u.cursive1.EntryExitRecord);
2248           if (! subtable->u.cursive1.EntryExitCount)
2249             return -1;
2250         }
2251       else
2252         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2253       break;
2254
2255     case 4:
2256       if (subtable->Format == 1)
2257         {
2258           read_coverage (otf, stream, offset, &subtable->Coverage);
2259           read_coverage (otf, stream, offset,
2260                          &subtable->u.mark_base1.BaseCoverage);
2261           READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2262           read_mark_array (otf, stream, offset,
2263                            &subtable->u.mark_base1.MarkArray);
2264           read_anchor_array (otf, stream, offset,
2265                              subtable->u.mark_base1.ClassCount,
2266                              &subtable->u.mark_base1.BaseArray);
2267         }
2268       else
2269         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2270       break;
2271
2272     case 5:
2273       if (subtable->Format == 1)
2274         {
2275           read_coverage (otf, stream, offset, &subtable->Coverage);
2276           read_coverage (otf, stream, offset,
2277                          &subtable->u.mark_lig1.LigatureCoverage);
2278           READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2279           read_mark_array (otf, stream, offset,
2280                            &subtable->u.mark_lig1.MarkArray);
2281           read_ligature_array (otf, stream, offset,
2282                                subtable->u.mark_lig1.ClassCount,
2283                                &subtable->u.mark_lig1.LigatureArray);
2284         }
2285       break;
2286
2287     case 6:
2288       if (subtable->Format == 1)
2289         {
2290           read_coverage (otf, stream, offset, &subtable->Coverage);
2291           read_coverage (otf, stream, offset,
2292                          &subtable->u.mark_mark1.Mark2Coverage);
2293           READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2294           read_mark_array (otf, stream, offset,
2295                            &subtable->u.mark_mark1.Mark1Array);
2296           read_anchor_array (otf, stream, offset,
2297                              subtable->u.mark_mark1.ClassCount,
2298                              &subtable->u.mark_mark1.Mark2Array);
2299         }
2300       else
2301         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2302       break;
2303
2304     case 7:
2305       if (subtable->Format == 1)
2306         {
2307           if (read_context1 (otf, stream, offset, &subtable->Coverage,
2308                              &subtable->u.context1) < 0)
2309             return errret;
2310         }
2311       else if (subtable->Format == 2)
2312         {
2313           if (read_context2 (otf, stream, offset, &subtable->Coverage,
2314                              &subtable->u.context2) < 0)
2315             return errret;
2316         }
2317       else if (subtable->Format == 3)
2318         {
2319           if (read_context3 (otf, stream, offset, &subtable->Coverage,
2320                              &subtable->u.context3) < 0)
2321             return errret;
2322         }
2323       else
2324         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2325       break;
2326
2327     case 8:
2328       if (subtable->Format == 1)
2329         {
2330           if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2331                                    &subtable->u.chain_context1) < 0)
2332             return errret;
2333         }
2334       else if (subtable->Format == 2)
2335         {
2336           if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2337                                    &subtable->u.chain_context2) < 0)
2338             return errret;
2339         }
2340       else if (subtable->Format == 3)
2341         {
2342           if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2343                                    &subtable->u.chain_context3) < 0)
2344             return errret;
2345         }
2346       else
2347         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2348       break;
2349
2350     case 9:
2351       if (subtable->Format == 1)
2352         {
2353           unsigned ex_type;
2354           long ex_offset;
2355           OTF_LookupSubTableGPOS *ex_subtable;
2356
2357           READ_USHORT (stream, ex_type);
2358           READ_ULONG (stream, ex_offset);
2359           OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2360           if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2361                                          ex_type, ex_subtable) < 0)
2362             return errret;
2363           subtable->u.extension1.ExtensionLookupType = ex_type;
2364           subtable->u.extension1.ExtensionOffset = ex_offset;
2365           subtable->u.extension1.ExtensionSubtable = ex_subtable;
2366         }
2367       else
2368         OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2369       break;
2370
2371     default:
2372       OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2373     }
2374   return 0;
2375 }
2376
2377 static void *
2378 read_gpos_table (OTF *otf, OTF_Stream *stream)
2379 {
2380   return read_gsub_gpos_table (otf, stream, 0);
2381 }
2382
2383 \f
2384 #if 0
2385 /* BASE */
2386
2387 static OTF_BASE *
2388 read_base_table (OTF_Stream *stream, long offset)
2389 {
2390   OTF_BASE *base;
2391
2392   OTF_MALLOC (base, 1);
2393
2394   return base;
2395 }
2396
2397 \f
2398 /* JSTF */
2399
2400 static OTF_JSTF *
2401 read_jstf_table (OTF_Stream *stream, long offset)
2402 {
2403   OTF_JSTF *jstf;
2404
2405   OTF_MALLOC (jstf, 1);
2406
2407   return jstf;
2408 }
2409 #endif /* 0 */
2410 \f
2411 /*** (1-11) Structure for OTF */
2412
2413 int
2414 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2415 {
2416   int errret = -1;
2417
2418   READ_FIXED (stream, table->sfnt_version);
2419   READ_USHORT (stream, table->numTables);
2420   READ_USHORT (stream, table->searchRange);
2421   READ_USHORT (stream, table->enterSelector);
2422   READ_USHORT (stream, table->rangeShift);
2423   return 0;
2424 }
2425
2426 static OTF_Tag
2427 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2428 {
2429   int errret = 0;
2430   OTF_Tag tag;
2431
2432   READ_TAG (stream, tag);
2433   table->tag = tag;
2434   table->name[0] = tag >> 24;
2435   table->name[1] = (tag >> 16) & 0xFF;
2436   table->name[0] = (tag >> 8) & 0xFF;
2437   table->name[0] = tag >> 8;
2438   table->name[0] = '\0';
2439   READ_ULONG (stream, table->checkSum);
2440   READ_ULONG (stream, table->offset);
2441   READ_ULONG (stream, table->length);
2442   return tag;
2443 }
2444
2445 static int
2446 read_header_part (OTF *otf, FILE *fp)
2447 {
2448   char *errfmt = "otf header%s";
2449   int errret = -1;
2450   OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2451   OTF_Stream *stream;
2452   int i;
2453   OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2454
2455   internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2456   internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2457   internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2458   internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2459   internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2460   internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2461   internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2462   internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2463   internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2464   internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2465   internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2466   internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2467
2468   head_tag = OTF_tag ("head");
2469   name_tag = OTF_tag ("name");
2470   cmap_tag = OTF_tag ("cmap");
2471   gdef_tag = OTF_tag ("GDEF");
2472   gsub_tag = OTF_tag ("GSUB");
2473   gpos_tag = OTF_tag ("GPOS");
2474
2475   stream = make_stream ();
2476   if (! stream)
2477     return -1;
2478
2479   internal_data->header_stream = stream;
2480
2481   /* Size of Offset Table is 12 bytes.  */
2482   if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2483     return -1;
2484   if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2485     return -1;
2486
2487   /* Size of each Table Directory is 16 bytes.  */
2488   if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2489                     "Table Directory") < 0)
2490     return -1;
2491
2492   OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2493   for (i = 0; i < otf->offset_table.numTables; i++)
2494     {
2495       OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2496       OTF_TableInfo *table_info = NULL;
2497
2498       if (! tag)
2499         return -1;
2500       if (tag == head_tag)
2501         table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2502       else if (tag == name_tag)
2503         table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2504       else if (tag == cmap_tag)
2505         table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2506       else if (tag == gdef_tag)
2507         table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2508       else if (tag == gsub_tag)
2509         table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2510       else if (tag == gpos_tag)
2511         table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2512
2513       if (table_info)
2514         {
2515           table_info->stream = make_stream ();
2516           if (setup_stream (table_info->stream, fp,
2517                             otf->table_dirs[i].offset,
2518                             otf->table_dirs[i].length,
2519                             otf->table_dirs[i].name) < 0)
2520             return -1;
2521         }
2522     }
2523
2524   internal_data->header_stream = NULL;
2525   free_stream (stream);
2526   return 0;
2527 }
2528
2529 static OTF_TableInfo *
2530 get_table_info (OTF *otf, char *name)
2531 {
2532   char *errfmt = "OTF Table Read%s";
2533   OTF_TableInfo *errret = NULL;
2534   OTF_InternalData *internal_data = otf->internal_data;
2535   OTF_TableInfo *table_info;
2536   OTF_Tag tag = OTF_tag (name);
2537
2538   if (! tag)
2539     OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2540
2541   if (tag == OTF_tag ("head"))
2542     table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2543   else if (tag == OTF_tag ("name"))
2544     table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2545   else if (tag == OTF_tag ("cmap"))
2546     table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2547   else if (tag == OTF_tag ("GDEF"))
2548     table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2549   else if (tag == OTF_tag ("GSUB"))
2550     table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2551   else if (tag == OTF_tag ("GPOS"))
2552     table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2553   else
2554     OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2555
2556   if (*table_info->address)
2557     /* Already read.  */
2558     return table_info;
2559   if (! table_info->stream)
2560     OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2561   if (! table_info->reader)
2562     OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2563   return table_info;
2564 }
2565
2566
2567 \f
2568 /*** (2) API for reading OTF */
2569
2570 /*** (2-1) OTF_open() */
2571
2572 /* Note: We can't use memory allocation macros in the following
2573    functions because those macros return from the functions before
2574    freeing memory previously allocated.  */
2575
2576 OTF *
2577 OTF_open (char *otf_name)
2578 {
2579   FILE *fp;
2580   char *errfmt = "opening otf (%s)";
2581   void *errret = NULL;
2582   OTF *otf;
2583   OTF_InternalData *internal_data;
2584
2585   fp = fopen (otf_name, "r");
2586   if (! fp)
2587     OTF_ERROR (OTF_ERROR_FILE, otf_name);
2588   otf = calloc (1, sizeof (OTF));
2589   if (! otf)
2590     OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2591   otf->filename = strdup (otf_name);
2592   if (! otf->filename)
2593     {
2594       OTF_close (otf);
2595       fclose (fp);
2596       OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2597     }
2598
2599   internal_data = calloc (1, sizeof (OTF_InternalData));
2600   if (! internal_data)
2601     OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2602   otf->internal_data = internal_data;
2603   if (! allocate_memory_record (otf))
2604     OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2605
2606   /* Here after, all pointers to allocated memory are recorded in
2607      otf->internal_data->memory_record except for what allocated by
2608      the functions allocate_memory_record and make_stream.  */
2609
2610   if (read_header_part (otf, fp) < 0)
2611     {
2612       OTF_close (otf);
2613       fclose (fp);
2614       return NULL;
2615     }
2616
2617   fclose (fp);
2618   return otf;
2619 }
2620
2621 /*** (2-2) OTF_close() */
2622
2623 void
2624 OTF_close (OTF *otf)
2625 {
2626   OTF_InternalData *internal_data = otf->internal_data;
2627   int i;
2628
2629   if (internal_data)
2630     {
2631       OTF_MemoryRecord *memrec = internal_data->memory_record;
2632
2633       if (internal_data->header_stream)
2634         free_stream (internal_data->header_stream);
2635
2636       for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2637         if (internal_data->table_info[i].stream)
2638           free_stream (internal_data->table_info[i].stream);
2639
2640       while (memrec)
2641         {
2642           OTF_MemoryRecord *next = memrec->next;
2643
2644           for (i = memrec->used - 1; i >= 0; i--)
2645             free (memrec->memory[i]);
2646           free (memrec);
2647           memrec = next;
2648         }
2649       free (internal_data);
2650     }
2651   if (otf->filename)
2652     free (otf->filename);
2653   free (otf);
2654 }
2655
2656 /*** (2-3) OTF_get_table() */
2657
2658 int
2659 OTF_get_table (OTF *otf, char *name)
2660 {
2661   OTF_TableInfo *table_info = get_table_info (otf, name);
2662
2663   if (! table_info)
2664     return -1;
2665
2666   *table_info->address = (*table_info->reader) (otf, table_info->stream);
2667   free_stream (table_info->stream);
2668   table_info->stream = NULL;
2669   if (! *table_info->address)
2670     {
2671       table_info->reader = NULL;
2672       return -1;
2673     }
2674   return 0;
2675 }
2676
2677 /*** (2-4) OTF_check_table() */
2678
2679 int
2680 OTF_check_table (OTF *otf, char *name)
2681 {
2682   return (get_table_info (otf, name) ? 0 : -1);
2683 }
2684
2685
2686
2687 \f
2688 /*** (5) API miscellaneous ***/
2689
2690 OTF_Tag
2691 OTF_tag (char *name)
2692 {
2693   unsigned char *p = (unsigned char *) name;
2694
2695   if (! name)
2696     return (OTF_Tag) 0;
2697   return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2698 }
2699
2700 void
2701 OTF_tag_name (OTF_Tag tag, char *name)
2702 {
2703   name[0] = (char) (tag >> 24);
2704   name[1] = (char) ((tag >> 16) & 0xFF);
2705   name[2] = (char) ((tag >> 8) & 0xFF);
2706   name[3] = (char) (tag & 0xFF);
2707   name[4] = '\0';
2708 }