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