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