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