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