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