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