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