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