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