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