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