1 /* otfopen.c -- OpenType font reader.
3 Copyright (C) 2003, 2004
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H15PRO167
7 This file is part of libotf.
9 Libotf is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 Libotf is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library, in a file named COPYING; if not,
21 write to the Free Software Foundation, Inc., 59 Temple Place, Suite
22 330, Boston, MA 02111-1307, USA. */
33 Table of contents (almost parallel to otf.h):
37 (1) Readers for OTF Layout tables and OTF itself
38 (1-1) Basic types and functions
42 (1-5) Structures common to GDEF, GSUB, and GPOS
44 (1-7) Structures for ScriptList, FeatureList, and LookupList
45 (1-8) Structures common to GSUB and GPOS
48 (1-11) Structure for OTF
50 (2) API for reading OTF
54 (2-4) OTF_check_table()
61 Example of typical usage of OTF_Stream.
65 OTF_StreamState state;
68 OPEN_STREAM (_FILE_NAME_, stream);
71 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
72 offset = READ_OFFSET (stream);
73 nbytes = READ_ULONG (stream);
74 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
76 CLOSE_STREAM (stream);
91 typedef long OTF_StreamState;
97 char *errfmt = "stream creation%s";
100 stream = calloc (1, sizeof (OTF_Stream));
102 OTF_ERROR (OTF_ERROR_MEMORY, "");
107 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
110 char *errfmt = "stream setup for %s";
115 if (stream->allocated < nbytes)
117 unsigned char *buf = malloc (nbytes);
120 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
124 stream->allocated = nbytes;
126 stream->bufsize = nbytes;
127 if (fseek (fp, offset, SEEK_SET) < 0)
128 OTF_ERROR (OTF_ERROR_FILE, stream->name);
129 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
130 OTF_ERROR (OTF_ERROR_FILE, stream->name);
136 free_stream (OTF_Stream *stream)
142 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
143 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
144 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
146 #define STREAM_CHECK_SIZE(stream, size) \
147 if ((stream)->pos + (size) > (stream)->bufsize) \
149 char *errfmt = "buffer overrun in %s"; \
151 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
157 #define READ_USHORT(stream, var) \
159 STREAM_CHECK_SIZE ((stream), 2); \
160 (var) = (((stream)->buf[(stream)->pos] << 8) \
161 | (stream)->buf[(stream)->pos + 1]); \
162 (stream)->pos += 2; \
165 #define READ_SHORT(stream, var) \
167 STREAM_CHECK_SIZE ((stream), 2); \
168 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
169 | (stream)->buf[(stream)->pos + 1]); \
170 (stream)->pos += 2; \
173 #define READ_ULONG(stream, var) \
175 STREAM_CHECK_SIZE ((stream), 4); \
176 (var) = (((stream)->buf[(stream)->pos] << 24) \
177 | ((stream)->buf[(stream)->pos + 1] << 16) \
178 | ((stream)->buf[(stream)->pos + 2] << 8) \
179 | (stream)->buf[(stream)->pos + 3]); \
180 (stream)->pos += 4; \
183 #define READ_LONG(stream, var) \
185 STREAM_CHECK_SIZE ((stream), 4); \
186 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
187 | ((stream)->buf[(stream)->pos + 1] << 16) \
188 | ((stream)->buf[(stream)->pos + 2] << 8) \
189 | (stream)->buf[(stream)->pos + 3]); \
190 (stream)->pos += 4; \
194 #define READ_FIXED(stream, fixed) \
196 READ_USHORT ((stream), (fixed).high); \
197 READ_USHORT ((stream), (fixed).low); \
201 #define READ_BYTES(stream, p, nbytes) \
203 STREAM_CHECK_SIZE ((stream), (nbytes)); \
204 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
205 (stream)->pos += (nbytes); \
209 #define READ_TAG READ_ULONG
210 #define READ_OFFSET READ_USHORT
211 #define READ_UINT16 READ_USHORT
212 #define READ_INT16 READ_SHORT
213 #define READ_GLYPHID READ_USHORT
216 /*** (1) Structures for OTF Layout tables and OTF itself */
218 /*** (1-1) Basic types and functions */
231 #define OTF_MEMORY_RECORD_SIZE 1024
233 struct OTF_MemoryRecord
236 void *memory[OTF_MEMORY_RECORD_SIZE];
237 struct OTF_MemoryRecord *next;
240 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
244 /* Points to one of OTF->head, OTF->name, etc. */
246 /* Function to read one of OTF tables. */
247 void *(*reader) (OTF *otf, OTF_Stream *stream);
248 /* Stream given to <reader>. */
252 struct OTF_InternalData
254 /* Information about each OTF table. */
255 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
257 /* Stream used to read the header part of OTF. */
258 OTF_Stream *header_stream;
260 /* Records of allocated memories. */
261 OTF_MemoryRecord *memory_record;
265 static OTF_MemoryRecord *
266 allocate_memory_record (OTF *otf)
268 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
269 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
274 memrec->next = internal_data->memory_record;
275 internal_data->memory_record = memrec;
279 /* Memory allocation macros. */
281 #define OTF_MALLOC(p, size, arg) \
287 OTF_MemoryRecord *memrec \
288 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
289 (p) = malloc (sizeof (*(p)) * (size)); \
291 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
292 && ! (memrec = allocate_memory_record (otf)))) \
293 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
294 memrec->memory[memrec->used++] = (p); \
299 #define OTF_CALLOC(p, size, arg) \
305 OTF_MemoryRecord *memrec \
306 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
307 (p) = calloc ((size), sizeof (*(p))); \
309 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
310 && ! (memrec = allocate_memory_record (otf)))) \
311 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
312 memrec->memory[memrec->used++] = (p); \
317 /*** (1-2) "head" table */
320 read_head_table (OTF *otf, OTF_Stream *stream)
322 char *errfmt = "head%s";
326 OTF_CALLOC (head, 1, "");
327 READ_FIXED (stream, head->TableVersionNumber);
328 READ_FIXED (stream, head->fontRevision);
329 READ_ULONG (stream, head->checkSumAdjustment);
330 READ_ULONG (stream, head->magicNumber);
331 READ_USHORT (stream, head->flags);
332 READ_USHORT (stream, head->unitsPerEm);
338 /*** (1-3) "name" table */
341 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
343 char *errfmt = "nameID (%d)";
345 OTF_StreamState state;
350 if (rec->platformID == 0)
351 ucs = (rec->encodingID <= 3) ? 2 : 4;
352 else if (rec->platformID == 1 && rec->encodingID == 0)
354 else if (rec->platformID == 3)
355 ucs = (rec->encodingID == 1 ? 2
356 : rec->encodingID == 10 ? 4
359 OTF_MALLOC (rec->name, rec->length + 1, (void *) rec->nameID);
360 SAVE_STREAM (stream, state);
361 SEEK_STREAM (stream, stream->pos + rec->offset);
362 READ_BYTES (stream, rec->name, rec->length);
363 RESTORE_STREAM (stream, state);
364 rec->name[rec->length] = 0;
373 for (i = 0; i < rec->length / 2; i++)
375 if (rec->name[i * 2] > 0
376 || rec->name[i * 2 + 1] >= 128)
383 for (i = 0; i < rec->length / 2; i++)
384 rec->name[i] = rec->name[i * 2 + 1];
390 for (i = 0; i < rec->length / 4; i++)
392 if (rec->name[i * 4] > 0
393 || rec->name[i * 4 + 1] > 0
394 || rec->name[i * 4 + 2] > 0
395 || rec->name[i * 2 + 3] >= 128)
402 for (i = 0; i < rec->length / 4; i++)
403 rec->name[i] = rec->name[i * 4 + 3];
411 read_name_table (OTF *otf, OTF_Stream *stream)
413 char *errfmt = "name%s";
418 OTF_CALLOC (name, 1, "");
419 READ_USHORT (stream, name->format);
420 READ_USHORT (stream, name->count);
421 READ_USHORT (stream, name->stringOffset);
422 OTF_MALLOC (name->nameRecord, name->count, "");
423 for (i = 0; i < name->count; i++)
425 OTF_NameRecord *rec = name->nameRecord + i;
427 READ_USHORT (stream, rec->platformID);
428 READ_USHORT (stream, rec->encodingID);
429 READ_USHORT (stream, rec->languageID);
430 READ_USHORT (stream, rec->nameID);
431 READ_USHORT (stream, rec->length);
432 READ_USHORT (stream, rec->offset);
434 for (i = 0; i < name->count; i++)
436 OTF_NameRecord *rec = name->nameRecord + i;
437 int nameID = rec->nameID;
439 read_name (otf, stream, rec);
441 if (nameID >= OTF_max_nameID)
443 if (! name->name[nameID]
445 name->name[nameID] = (char *) rec->name;
452 /*** (1-4) "cmap" table */
455 read_cmap_table (OTF *otf, OTF_Stream *stream)
457 char *errfmt = "cmap%s";
460 int unicode_bmp_index = -1, unicode_full_index = -1;
463 OTF_CALLOC (cmap, 1, "");
464 READ_USHORT (stream, cmap->version);
465 READ_USHORT (stream, cmap->numTables);
466 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
467 for (i = 0; i < cmap->numTables; i++)
469 unsigned platformID, encodingID;
471 READ_USHORT (stream, platformID);
472 cmap->EncodingRecord[i].platformID = platformID;
473 READ_USHORT (stream, encodingID);
474 cmap->EncodingRecord[i].encodingID = encodingID;
475 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
479 unicode_bmp_index = i;
481 unicode_full_index = i;
483 else if (platformID == 3)
486 unicode_bmp_index = i;
487 else if (encodingID == 10)
488 unicode_full_index = i;
491 for (i = 0; i < cmap->numTables; i++)
495 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
496 READ_USHORT (stream, format);
497 cmap->EncodingRecord[i].subtable.format = format;
498 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
499 if (format == 8 || format == 10 || format == 12)
501 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
502 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
506 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
512 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
513 " (EncodingRecord)");
515 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
521 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
526 OTF_EncodingSubtable4 *sub4;
531 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
532 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
533 READ_USHORT (stream, sub4->segCountX2);
534 segCount = sub4->segCountX2 / 2;
535 READ_USHORT (stream, sub4->searchRange);
536 READ_USHORT (stream, sub4->entrySelector);
537 READ_USHORT (stream, sub4->rangeShift);
538 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
539 for (j = 0; j < segCount; j++)
540 READ_USHORT (stream, sub4->segments[j].endCount);
541 READ_USHORT (stream, dummy);
542 for (j = 0; j < segCount; j++)
543 READ_USHORT (stream, sub4->segments[j].startCount);
544 for (j = 0; j < segCount; j++)
545 READ_SHORT (stream, sub4->segments[j].idDelta);
546 for (j = 0; j < segCount; j++)
549 unsigned rest = 2 * (segCount - j);
551 READ_USHORT (stream, off);
553 sub4->segments[j].idRangeOffset = 0xFFFF;
555 sub4->segments[j].idRangeOffset = (off - rest) / 2;
557 j = (cmap->EncodingRecord[i].subtable.length
558 - (14 + 2 * (segCount * 4 + 1)));
559 sub4->GlyphCount = j / 2;
560 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
561 for (j = 0; j < sub4->GlyphCount; j++)
562 READ_USHORT (stream, sub4->glyphIdArray[j]);
568 OTF_EncodingSubtable6 *sub6;
571 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
572 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
573 READ_USHORT (stream, sub6->firstCode);
574 READ_USHORT (stream, sub6->entryCount);
575 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
576 for (j = 0; j < sub6->entryCount; j++)
577 READ_USHORT (stream, sub6->glyphIdArray[j]);
583 OTF_EncodingSubtable8 *sub8;
586 OTF_MALLOC (sub8, 1, " (EncodingSubtable8)");
587 cmap->EncodingRecord[i].subtable.f.f8 = sub8;
588 for (j = 0; j < 8192; j++)
589 READ_BYTES (stream, sub8->is32, 8192);
590 READ_ULONG (stream, sub8->nGroups);
591 OTF_MALLOC (sub8->Groups, sub8->nGroups, " (Groups)");
592 for (j = 0; j < sub8->nGroups; j++)
594 READ_ULONG (stream, sub8->Groups[i].startCharCode);
595 READ_ULONG (stream, sub8->Groups[i].endCharCode);
596 READ_ULONG (stream, sub8->Groups[i].startGlyphID);
603 OTF_EncodingSubtable10 *sub10;
606 OTF_MALLOC (sub10, 1, " (EncodingSubtable10)");
607 cmap->EncodingRecord[i].subtable.f.f10 = sub10;
608 READ_ULONG (stream, sub10->startCharCode);
609 READ_ULONG (stream, sub10->numChars);
610 OTF_MALLOC (sub10->glyphs, sub10->numChars, " (GlyphCount)");
611 for (j = 0; j < sub10->numChars; j++)
612 READ_USHORT (stream, sub10->glyphs[j]);
618 OTF_EncodingSubtable12 *sub12;
621 OTF_MALLOC (sub12, 1, " (EncodingSubtable12)");
622 cmap->EncodingRecord[i].subtable.f.f12 = sub12;
623 READ_ULONG (stream, sub12->nGroups);
624 OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
625 for (j = 0; j < sub12->nGroups; j++)
627 READ_ULONG (stream, sub12->Groups[i].startCharCode);
628 READ_ULONG (stream, sub12->Groups[i].endCharCode);
629 READ_ULONG (stream, sub12->Groups[i].startGlyphID);
635 OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
639 if (unicode_bmp_index >= 0)
641 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_bmp_index;
642 OTF_GlyphID glyph_id, max_glyph_id = 0;
644 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
645 switch (rec->subtable.format)
649 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
650 int segCount = sub4->segCountX2 / 2;
652 for (i = 0; i < segCount; i++)
654 OTF_cmapSegument *seg = sub4->segments + i;
657 if (seg->idRangeOffset == 0xFFFF)
658 for (c = seg->startCount; c <= seg->endCount; c++)
660 glyph_id = c + seg->idDelta;
661 cmap->unicode_table[c] = glyph_id;
662 if (glyph_id > max_glyph_id)
663 max_glyph_id = glyph_id;
666 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
669 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
670 + (c - seg->startCount)];
671 cmap->unicode_table[c] = glyph_id;
672 if (glyph_id > max_glyph_id)
673 max_glyph_id = glyph_id;
679 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
680 for (i = 0; i < 0x10000; i++)
681 if (cmap->unicode_table[i])
682 cmap->decode_table[cmap->unicode_table[i]] = i;
683 cmap->max_glyph_id = max_glyph_id;
690 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
692 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
693 Glyph-IDs there. If COUNT is negative, read the number of
694 Glyphs-IDs at first. MINUS if nozero is how few the actual
695 Glyph-IDs are in STREAM than COUNT. */
698 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
699 int minus, int count)
701 char *errfmt = "GlyphID List%s";
706 READ_UINT16 (stream, count);
709 OTF_MALLOC (*ids, count, "");
710 for (i = 0; i < count + minus; i++)
711 READ_GLYPHID (stream, (*ids)[i]);
716 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
718 char *errfmt = "RangeRecord%s";
723 READ_UINT16 (stream, count);
726 OTF_MALLOC (*record, count, "");
727 for (i = 0; i < count; i++)
729 READ_GLYPHID (stream, (*record)[i].Start);
730 READ_GLYPHID (stream, (*record)[i].End);
731 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
738 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
739 OTF_Coverage *coverage)
741 char *errfmt = "Coverage%s";
743 OTF_StreamState state;
746 READ_OFFSET (stream, coverage->offset);
747 SAVE_STREAM (stream, state);
748 SEEK_STREAM (stream, offset + coverage->offset);
749 READ_UINT16 (stream, coverage->CoverageFormat);
750 if (coverage->CoverageFormat == 1)
751 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
752 else if (coverage->CoverageFormat == 2)
753 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
755 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
758 coverage->Count = (unsigned) count;
759 RESTORE_STREAM (stream, state);
763 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
764 and store the Coverages there. If COUNT is negative, read the
765 number of Coverages at first. */
768 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
769 OTF_Coverage **coverage, int count)
771 char *errfmt = "Coverage List%s";
776 READ_UINT16 (stream, count);
779 OTF_MALLOC (*coverage, count, "");
780 for (i = 0; i < count; i++)
781 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
788 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
791 char *errfmt = "ClassDef%s";
794 SEEK_STREAM (stream, class->offset);
795 READ_UINT16 (stream, class->ClassFormat);
796 if (class->ClassFormat == 1)
798 READ_GLYPHID (stream, class->f.f1.StartGlyph);
799 class->f.f1.GlyphCount
801 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
802 if (! class->f.f1.GlyphCount)
803 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
805 else if (class->ClassFormat == 2)
807 class->f.f2.ClassRangeCount
808 = (read_range_records
809 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
810 if (! class->f.f2.ClassRangeCount)
811 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
814 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
820 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
822 char *errfmt = "ClassDef%s";
824 OTF_StreamState state;
826 READ_OFFSET (stream, class->offset);
829 SAVE_STREAM (stream, state);
830 SEEK_STREAM (stream, offset + class->offset);
831 READ_UINT16 (stream, class->ClassFormat);
832 if (class->ClassFormat == 1)
834 READ_GLYPHID (stream, class->f.f1.StartGlyph);
835 class->f.f1.GlyphCount
836 = read_glyph_ids (otf, stream,
837 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
839 if (! class->f.f1.GlyphCount)
842 else if (class->ClassFormat == 2)
844 class->f.f2.ClassRangeCount
845 = read_range_records (otf, stream,
847 &class->f.f2.ClassRangeRecord);
848 if (! class->f.f2.ClassRangeCount)
852 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
854 RESTORE_STREAM (stream, state);
860 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
861 OTF_DeviceTable *table)
863 char *errfmt = "Device Table%s";
874 SEEK_STREAM (stream, offset + table->offset);
875 READ_UINT16 (stream, table->StartSize);
876 READ_UINT16 (stream, table->EndSize);
877 READ_UINT16 (stream, table->DeltaFormat);
878 num = table->EndSize - table->StartSize + 1;
879 OTF_MALLOC (table->DeltaValue, num, "");
881 if (table->DeltaFormat == 1)
882 for (i = 0; i < num; i++)
885 READ_UINT16 (stream, val);
886 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
887 table->DeltaValue[i] = intval.int2;
889 else if (table->DeltaFormat == 2)
890 for (i = 0; i < num; i++)
893 READ_UINT16 (stream, val);
894 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
895 table->DeltaValue[i] = intval.int4;
897 else if (table->DeltaFormat == 3)
898 for (i = 0; i < num; i++)
902 READ_UINT16 (stream, val);
903 intval.int8 = val >> 8;
904 table->DeltaValue[i] = intval.int8;
908 intval.int8 = val >> 8;
909 table->DeltaValue[i] = intval.int8;
913 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
918 /*** (1-6) "GDEF" table */
921 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
922 OTF_AttachList *list)
924 char *errfmt = "AttachList%s";
928 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
930 READ_UINT16 (stream, list->GlyphCount);
931 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
932 for (i = 0; i < list->GlyphCount; i++)
933 READ_OFFSET (stream, list->AttachPoint[i].offset);
934 for (i = 0; i < list->GlyphCount; i++)
938 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
939 READ_UINT16 (stream, count);
940 list->AttachPoint[i].PointCount = count;
941 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
942 for (j = 0; j < count; j++)
943 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
949 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
950 OTF_CaretValue *caret)
952 char *errfmt = "CaretValue%s";
955 SEEK_STREAM (stream, offset + caret->offset);
956 READ_UINT16 (stream, caret->CaretValueFormat);
957 if (caret->CaretValueFormat == 1)
958 READ_INT16 (stream, caret->f.f1.Coordinate);
959 else if (caret->CaretValueFormat == 2)
960 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
961 else if (caret->CaretValueFormat == 3)
963 READ_INT16 (stream, caret->f.f3.Coordinate);
964 if (read_device_table (otf, stream, offset + caret->offset,
965 &caret->f.f3.DeviceTable) < 0)
969 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
974 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
975 OTF_LigCaretList *list)
977 char *errfmt = "LigCaretList%s";
981 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
983 READ_UINT16 (stream, list->LigGlyphCount);
984 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
985 for (i = 0; i < list->LigGlyphCount; i++)
986 READ_OFFSET (stream, list->LigGlyph[i].offset);
987 for (i = 0; i < list->LigGlyphCount; i++)
991 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
992 READ_UINT16 (stream, count);
993 list->LigGlyph[i].CaretCount = count;
994 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
995 for (j = 0; j < count; j++)
996 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
997 for (j = 0; j < count; j++)
998 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
999 &list->LigGlyph[i].CaretValue[j]) < 0)
1006 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1010 READ_FIXED (stream, header->Version);
1011 READ_OFFSET (stream, header->GlyphClassDef);
1012 READ_OFFSET (stream, header->AttachList);
1013 READ_OFFSET (stream, header->LigCaretList);
1014 READ_OFFSET (stream, header->MarkAttachClassDef);
1019 read_gdef_table (OTF *otf, OTF_Stream *stream)
1021 char *errfmt = "GDEF%s";
1022 void *errret = NULL;
1025 OTF_CALLOC (gdef, 1, "");
1026 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1027 if (gdef->header.GlyphClassDef)
1029 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1030 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1032 if (gdef->header.AttachList)
1033 read_attach_list (otf, stream, gdef->header.AttachList,
1034 &gdef->attach_list);
1035 if (gdef->header.LigCaretList)
1036 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1037 &gdef->lig_caret_list);
1038 if (gdef->header.MarkAttachClassDef)
1040 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1041 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1048 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
1051 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
1052 OTF_ScriptList *list)
1054 char *errfmt = "Script List%s";
1058 SEEK_STREAM (stream, offset);
1059 READ_USHORT (stream, list->ScriptCount);
1060 OTF_CALLOC (list->Script, list->ScriptCount, "");
1062 for (i = 0; i < list->ScriptCount; i++)
1064 READ_TAG (stream, list->Script[i].ScriptTag);
1065 READ_OFFSET (stream, list->Script[i].offset);
1067 for (i = 0; i < list->ScriptCount; i++)
1069 OTF_Script *script = list->Script + i;
1070 long script_offset = offset + script->offset;
1072 SEEK_STREAM (stream, script_offset);
1073 READ_OFFSET (stream, script->DefaultLangSysOffset);
1074 READ_USHORT (stream, script->LangSysCount);
1075 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1076 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1077 for (j = 0; j < script->LangSysCount; j++)
1079 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1080 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1083 if (script->DefaultLangSysOffset)
1085 OTF_LangSys *langsys = &script->DefaultLangSys;
1087 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1088 READ_OFFSET (stream, langsys->LookupOrder);
1089 READ_USHORT (stream, langsys->ReqFeatureIndex);
1090 READ_USHORT (stream, langsys->FeatureCount);
1091 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1093 for (k = 0; k < langsys->FeatureCount; k++)
1094 READ_USHORT (stream, langsys->FeatureIndex[k]);
1097 for (j = 0; j < script->LangSysCount; j++)
1099 OTF_LangSys *langsys = script->LangSys + j;
1101 SEEK_STREAM (stream,
1102 script_offset + script->LangSysRecord[j].LangSys);
1103 READ_OFFSET (stream, langsys->LookupOrder);
1104 READ_USHORT (stream, langsys->ReqFeatureIndex);
1105 READ_USHORT (stream, langsys->FeatureCount);
1106 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1108 for (k = 0; k < langsys->FeatureCount; k++)
1109 READ_USHORT (stream, langsys->FeatureIndex[k]);
1117 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1118 OTF_FeatureList *list)
1120 char *errfmt = "Feature List%s";
1124 READ_UINT16 (stream, list->FeatureCount);
1125 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1126 for (i = 0; i < list->FeatureCount; i++)
1128 READ_TAG (stream, list->Feature[i].FeatureTag);
1129 READ_OFFSET (stream, list->Feature[i].offset);
1131 for (i = 0; i < list->FeatureCount; i++)
1133 OTF_Feature *feature = list->Feature + i;
1135 SEEK_STREAM (stream, offset + feature->offset);
1136 READ_OFFSET (stream, feature->FeatureParams);
1137 READ_UINT16 (stream, feature->LookupCount);
1138 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1139 " (LookupListIndex)");
1140 for (j = 0; j < feature->LookupCount; j++)
1141 READ_UINT16 (stream, feature->LookupListIndex[j]);
1147 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1148 long offset, unsigned type,
1149 OTF_LookupSubTableGSUB *subtable);
1150 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1151 long offset, unsigned type,
1152 OTF_LookupSubTableGPOS *subtable);
1155 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1156 OTF_LookupList *list, int gsubp)
1158 char *errfmt = "Lookup List%s";
1162 SEEK_STREAM (stream, offset);
1163 READ_UINT16 (stream, list->LookupCount);
1164 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1166 for (i = 0; i < list->LookupCount; i++)
1167 READ_OFFSET (stream, list->Lookup[i].offset);
1168 for (i = 0; i < list->LookupCount; i++)
1170 OTF_Lookup *lookup = list->Lookup + i;
1172 SEEK_STREAM (stream, offset + lookup->offset);
1173 READ_UINT16 (stream, lookup->LookupType);
1174 READ_UINT16 (stream, lookup->LookupFlag);
1175 READ_UINT16 (stream, lookup->SubTableCount);
1176 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1177 " (SubTableOffset)");
1179 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1182 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1184 for (j = 0; j < lookup->SubTableCount; j++)
1185 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1186 for (j = 0; j < lookup->SubTableCount; j++)
1189 = offset + lookup->offset + lookup->SubTableOffset[j];
1192 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1194 lookup->SubTable.gsub + j) < 0
1195 : read_lookup_subtable_gpos (otf, stream, this_offset,
1197 lookup->SubTable.gpos + j) < 0)
1206 /*** (1-8) Structures common to GSUB and GPOS */
1209 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1210 OTF_LookupRecord **record, int count)
1212 char *errfmt = "LookupRecord%s";
1213 unsigned errret = 0;
1217 READ_UINT16 (stream, count);
1219 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1220 OTF_MALLOC (*record, count, "");
1221 for (i = 0; i < count; i++)
1223 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1224 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1230 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1232 char *errfmt = "List of Rule%s";
1233 unsigned errret = 0;
1234 OTF_StreamState state;
1238 READ_UINT16 (stream, count);
1240 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1241 OTF_MALLOC (*rule, count, "");
1242 for (i = 0; i < count; i++)
1244 READ_OFFSET (stream, (*rule)[i].offset);
1245 if (! (*rule)[i].offset)
1246 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1248 SAVE_STREAM (stream, state);
1249 for (i = 0; i < count; i++)
1251 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1252 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1253 if ((*rule)[i].GlyphCount == 0)
1254 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1255 READ_UINT16 (stream, (*rule)[i].LookupCount);
1256 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1257 (*rule)[i].GlyphCount) < 0)
1259 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1260 (*rule)[i].LookupCount) == 0)
1263 RESTORE_STREAM (stream, state);
1269 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1272 char *errfmt = "List of RuleSet%s";
1273 unsigned errret = 0;
1274 OTF_StreamState state;
1278 READ_UINT16 (stream, count);
1280 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1281 OTF_MALLOC (*set, count, "");
1282 for (i = 0; i < count; i++)
1284 READ_OFFSET (stream, (*set)[i].offset);
1285 if (! (*set)[i].offset)
1286 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1288 SAVE_STREAM (stream, state);
1289 for (i = 0; i < count; i++)
1291 SEEK_STREAM (stream, offset + (*set)[i].offset);
1293 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1295 if (! (*set)[i].RuleCount)
1298 RESTORE_STREAM (stream, state);
1303 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1304 OTF_ClassRule **rule)
1306 char *errfmt = "ClassRule%s";
1307 unsigned errret = 0;
1308 OTF_StreamState state;
1312 READ_UINT16 (stream, count);
1314 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1315 OTF_MALLOC (*rule, count, "");
1316 for (i = 0; i < count; i++)
1318 READ_OFFSET (stream, (*rule)[i].offset);
1319 if (! (*rule)[i].offset)
1320 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1322 SAVE_STREAM (stream, state);
1323 for (i = 0; i < count; i++)
1325 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1326 READ_USHORT (stream, (*rule)[i].GlyphCount);
1327 if (! (*rule)[i].GlyphCount)
1328 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1329 READ_USHORT (stream, (*rule)[i].LookupCount);
1330 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1331 0, (*rule)[i].GlyphCount - 1) < 0)
1333 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1334 (*rule)[i].LookupCount) == 0)
1337 RESTORE_STREAM (stream, state);
1342 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1345 char *errfmt = "ClassSet%s";
1346 unsigned errret = 0;
1347 OTF_StreamState state;
1351 READ_UINT16 (stream, count);
1353 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1354 OTF_CALLOC (*set, count, "");
1355 for (i = 0; i < count; i++)
1356 /* Offset can be zero. */
1357 READ_OFFSET (stream, (*set)[i].offset);
1358 SAVE_STREAM (stream, state);
1359 for (i = 0; i < count; i++)
1360 if ((*set)[i].offset)
1362 SEEK_STREAM (stream, offset + (*set)[i].offset);
1363 (*set)[i].ClassRuleCnt
1364 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1365 &(*set)[i].ClassRule);
1366 if (! (*set)[i].ClassRuleCnt)
1369 RESTORE_STREAM (stream, state);
1374 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1375 OTF_ChainRule **rule)
1377 char *errfmt = "ChainRule%s";
1378 unsigned errret = 0;
1382 READ_UINT16 (stream, count);
1384 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1385 OTF_MALLOC (*rule, count, "");
1386 for (i = 0; i < count; i++)
1387 READ_OFFSET (stream, (*rule)[i].offset);
1388 for (i = 0; i < count; i++)
1390 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1391 (*rule)[i].BacktrackGlyphCount
1392 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1393 (*rule)[i].InputGlyphCount
1394 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1395 if (! (*rule)[i].InputGlyphCount)
1396 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1397 (*rule)[i].LookaheadGlyphCount
1398 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1399 (*rule)[i].LookupCount
1400 = read_lookup_record_list (otf, stream,
1401 &(*rule)[i].LookupRecord, -1);
1402 if (! (*rule)[i].LookupCount)
1410 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1411 OTF_ChainRuleSet **set)
1413 char *errfmt = "ChainRuleSet%s";
1414 unsigned errret = 0;
1415 OTF_StreamState state;
1419 READ_UINT16 (stream, count);
1421 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1422 OTF_MALLOC (*set, count, "");
1423 for (i = 0; i < count; i++)
1425 READ_OFFSET (stream, (*set)[i].offset);
1426 if (! (*set)[i].offset)
1427 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1429 SAVE_STREAM (stream, state);
1430 for (i = 0; i < count; i++)
1432 SEEK_STREAM (stream, offset + (*set)[i].offset);
1433 (*set)[i].ChainRuleCount
1434 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1435 &(*set)[i].ChainRule);
1436 if (! (*set)[i].ChainRuleCount)
1439 RESTORE_STREAM (stream, state);
1444 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1445 OTF_ChainClassRule **rule)
1447 char *errfmt = "ChainClassRule%s";
1448 unsigned errret = 0;
1452 READ_UINT16 (stream, count);
1454 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1455 OTF_MALLOC (*rule, count, "");
1456 for (i = 0; i < count; i++)
1458 READ_OFFSET (stream, (*rule)[i].offset);
1459 if (! (*rule)[i].offset)
1460 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1462 for (i = 0; i < count; i++)
1464 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1465 (*rule)[i].BacktrackGlyphCount
1466 = read_glyph_ids (otf, stream,
1467 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1468 (*rule)[i].InputGlyphCount
1469 = read_glyph_ids (otf, stream,
1470 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1471 if (! (*rule)[i].InputGlyphCount)
1472 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1473 (*rule)[i].LookaheadGlyphCount
1474 = read_glyph_ids (otf, stream,
1475 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1476 (*rule)[i].LookupCount
1477 = read_lookup_record_list (otf, stream,
1478 &(*rule)[i].LookupRecord, -1);
1479 if (! (*rule)[i].LookupCount)
1486 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1487 OTF_ChainClassSet **set)
1489 char *errfmt = "ChainClassSet%s";
1490 unsigned errret = 0;
1491 OTF_StreamState state;
1495 READ_UINT16 (stream, count);
1497 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1498 OTF_MALLOC (*set, count, "");
1499 for (i = 0; i < count; i++)
1500 /* Offset may be zero. */
1501 READ_OFFSET (stream, (*set)[i].offset);
1502 SAVE_STREAM (stream, state);
1503 for (i = 0; i < count; i++)
1504 if ((*set)[i].offset)
1506 SEEK_STREAM (stream, offset + (*set)[i].offset);
1507 (*set)[i].ChainClassRuleCnt
1508 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1509 &(*set)[i].ChainClassRule);
1510 if (! (*set)[i].ChainClassRuleCnt)
1513 RESTORE_STREAM (stream, state);
1518 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1519 OTF_Coverage *coverage,OTF_Context1 *context1)
1521 if (read_coverage (otf, stream, offset, coverage) < 0)
1523 context1->RuleSetCount
1524 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1525 if (! context1->RuleSetCount)
1531 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1532 OTF_Coverage *coverage,OTF_Context2 *context2)
1534 if (read_coverage (otf, stream, offset, coverage) < 0
1535 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1537 context2->ClassSetCnt
1538 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1539 if (! context2->ClassSetCnt)
1545 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1546 OTF_Coverage *coverage,OTF_Context3 *context3)
1548 char *errfmt = "Context1%s";
1551 READ_USHORT (stream, context3->GlyphCount);
1552 if (context3->GlyphCount < 0)
1553 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1554 READ_USHORT (stream, context3->LookupCount);
1555 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1556 context3->GlyphCount) < 0)
1558 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1559 context3->LookupCount) < 0)
1565 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1566 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1568 if (read_coverage (otf, stream, offset, coverage) < 0)
1570 chain_context1->ChainRuleSetCount
1571 = read_chain_rule_set_list (otf, stream, offset,
1572 &chain_context1->ChainRuleSet);
1573 if (! chain_context1->ChainRuleSetCount)
1579 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1580 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1582 if (read_coverage (otf, stream, offset, coverage) < 0
1583 || read_class_def (otf, stream, offset,
1584 &chain_context2->BacktrackClassDef) < 0
1585 || read_class_def (otf, stream, offset,
1586 &chain_context2->InputClassDef) < 0
1587 || read_class_def (otf, stream, offset,
1588 &chain_context2->LookaheadClassDef) < 0)
1590 chain_context2->ChainClassSetCnt
1591 = read_chain_class_set_list (otf, stream, offset,
1592 &chain_context2->ChainClassSet);
1593 if (! chain_context2->ChainClassSetCnt)
1599 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1600 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1604 count = read_coverage_list (otf, stream, offset,
1605 &chain_context3->Backtrack, -1);
1608 chain_context3->BacktrackGlyphCount = (unsigned) count;
1609 count = read_coverage_list (otf, stream, offset,
1610 &chain_context3->Input, -1);
1613 chain_context3->InputGlyphCount = (unsigned) count;
1614 *coverage = chain_context3->Input[0];
1615 count = read_coverage_list (otf, stream, offset,
1616 &chain_context3->LookAhead, -1);
1617 chain_context3->LookaheadGlyphCount = (unsigned) count;
1618 chain_context3->LookupCount
1619 = read_lookup_record_list (otf, stream,
1620 &chain_context3->LookupRecord, -1);
1625 read_gsub_gpos_table (OTF *otf, OTF_Stream *stream, int gsubp)
1627 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1628 void *errret = NULL;
1629 OTF_GSUB_GPOS *gsub_gpos;
1631 OTF_CALLOC (gsub_gpos, 1, "");
1632 READ_FIXED (stream, gsub_gpos->Version);
1633 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1634 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1635 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1637 if (read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1638 &gsub_gpos->ScriptList) < 0
1639 || read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1640 &gsub_gpos->FeatureList) < 0
1641 || read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1642 &gsub_gpos->LookupList, gsubp) < 0)
1648 /* (1-9) "GSUB" table */
1651 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1653 char *errfmt = "Sequence%s";
1654 unsigned errret = 0;
1658 READ_UINT16 (stream, count);
1660 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1661 OTF_MALLOC (*seq, count, "");
1662 for (i = 0; i < count; i++)
1663 READ_OFFSET (stream, (*seq)[i].offset);
1664 for (i = 0; i < count; i++)
1666 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1667 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1668 &(*seq)[i].Substitute, 0, -1);
1669 if (! (*seq)[i].GlyphCount)
1676 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1677 OTF_Ligature **ligature)
1679 char *errfmt = "Ligature%s";
1684 READ_UINT16 (stream, count);
1687 OTF_MALLOC (*ligature, count, "");
1688 for (i = 0; i < count; i++)
1689 READ_OFFSET (stream, (*ligature)[i].offset);
1690 for (i = 0; i < count; i++)
1692 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1693 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1694 (*ligature)[i].CompCount
1695 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1696 if (! (*ligature)[i].CompCount)
1703 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1704 OTF_LigatureSet **ligset)
1706 char *errfmt = "LigatureSet%s";
1711 READ_UINT16 (stream, count);
1714 OTF_MALLOC (*ligset, count, "");
1715 for (i = 0; i < count; i++)
1716 READ_OFFSET (stream, (*ligset)[i].offset);
1717 for (i = 0; i < count; i++)
1721 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1722 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1723 &(*ligset)[i].Ligature);
1726 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1732 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1733 OTF_AlternateSet **altset)
1735 char *errfmt = "AlternateSet%s";
1740 READ_UINT16 (stream, count);
1742 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1743 OTF_MALLOC (*altset, count, "");
1744 for (i = 0; i < count; i++)
1745 READ_OFFSET (stream, (*altset)[i].offset);
1746 for (i = 0; i < count; i++)
1750 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1751 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1754 (*altset)[i].GlyphCount = (unsigned) alt_count;
1760 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1761 OTF_Coverage *coverage,
1762 OTF_GSUB_ReverseChain1 *reverse_chain)
1766 if (read_coverage (otf, stream, offset, coverage) < 0)
1768 count = read_coverage_list (otf, stream, offset,
1769 &reverse_chain->Backtrack, -1);
1772 reverse_chain->BacktrackGlyphCount = (unsigned) count;
1773 count = read_coverage_list (otf, stream, offset,
1774 &reverse_chain->LookAhead, -1);
1777 reverse_chain->LookaheadGlyphCount = (unsigned) count;
1778 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
1781 reverse_chain->GlyphCount = count;
1786 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1787 unsigned type, OTF_LookupSubTableGSUB *subtable)
1792 SEEK_STREAM (stream, offset);
1793 READ_USHORT (stream, subtable->Format);
1794 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1798 if (subtable->Format == 1)
1800 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1802 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1804 else if (subtable->Format == 2)
1806 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1808 subtable->u.single2.GlyphCount
1809 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1811 if (! subtable->u.single2.GlyphCount)
1815 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1819 if (subtable->Format == 1)
1821 read_coverage (otf, stream, offset, &subtable->Coverage);
1822 subtable->u.multiple1.SequenceCount
1823 = read_sequence (otf, stream, offset,
1824 &subtable->u.multiple1.Sequence);
1827 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1831 if (subtable->Format == 1)
1833 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1835 subtable->u.alternate1.AlternateSetCount
1836 = read_alternate_set_list (otf, stream, offset,
1837 &subtable->u.alternate1.AlternateSet);
1838 if (! subtable->u.alternate1.AlternateSetCount)
1842 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1846 if (subtable->Format == 1)
1848 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1850 subtable->u.ligature1.LigSetCount
1851 = read_ligature_set_list (otf, stream, offset,
1852 &subtable->u.ligature1.LigatureSet);
1853 if (! subtable->u.ligature1.LigSetCount)
1857 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1861 if (subtable->Format == 1)
1863 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1864 &subtable->u.context1) < 0)
1867 else if (subtable->Format == 2)
1869 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1870 &subtable->u.context2) < 0)
1873 else if (subtable->Format == 3)
1875 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1876 &subtable->u.context3) < 0)
1880 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1884 if (subtable->Format == 1)
1886 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1887 &subtable->u.chain_context1) < 0)
1890 else if (subtable->Format == 2)
1892 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1893 &subtable->u.chain_context2) < 0)
1896 else if (subtable->Format == 3)
1898 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1899 &subtable->u.chain_context3) < 0)
1903 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1907 if (subtable->Format == 1)
1911 OTF_LookupSubTableGSUB *ex_subtable;
1913 READ_USHORT (stream, ex_type);
1914 READ_ULONG (stream, ex_offset);
1915 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
1916 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
1917 ex_type, ex_subtable) < 0)
1919 subtable->u.extension1.ExtensionLookupType = ex_type;
1920 subtable->u.extension1.ExtensionOffset = ex_offset;
1921 subtable->u.extension1.ExtensionSubtable = ex_subtable;
1924 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1928 if (subtable->Format == 1)
1930 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
1931 &subtable->u.reverse_chain1) < 0)
1935 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1939 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1945 read_gsub_table (OTF *otf, OTF_Stream *stream)
1947 return read_gsub_gpos_table (otf, stream, 1);
1951 /* (1-10) "GPOS" table */
1954 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1955 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1958 OTF_StreamState state;
1961 memset (value_record, 0, sizeof (OTF_ValueRecord));
1964 for (i = 0, size = 0; i < 8; i++)
1968 if (bit & OTF_XPlacement)
1969 READ_INT16 (stream, value_record->XPlacement);
1970 if (bit & OTF_XPlacement)
1971 READ_INT16 (stream, value_record->YPlacement);
1972 if (bit & OTF_XAdvance)
1973 READ_INT16 (stream, value_record->XAdvance);
1974 if (bit & OTF_YAdvance)
1975 READ_INT16 (stream, value_record->YAdvance);
1976 if (bit & OTF_XPlaDevice)
1977 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1978 if (bit & OTF_YPlaDevice)
1979 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1980 if (bit & OTF_XAdvDevice)
1981 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1982 if (bit & OTF_YAdvDevice)
1983 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1984 SAVE_STREAM (stream, state);
1985 if (value_record->XPlaDevice.offset)
1987 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1990 if (value_record->YPlaDevice.offset)
1992 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1995 if (value_record->XAdvDevice.offset)
1997 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
2000 if (value_record->YAdvDevice.offset)
2002 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
2005 RESTORE_STREAM (stream, state);
2011 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
2013 char *errfmt = "Anchor%s";
2016 SEEK_STREAM (stream, offset + anchor->offset);
2017 READ_UINT16 (stream, anchor->AnchorFormat);
2018 READ_INT16 (stream, anchor->XCoordinate);
2019 READ_INT16 (stream, anchor->YCoordinate);
2020 if (anchor->AnchorFormat == 1)
2022 else if (anchor->AnchorFormat == 2)
2024 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
2026 else if (anchor->AnchorFormat == 3)
2028 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
2029 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
2030 if (anchor->f.f2.XDeviceTable.offset)
2032 if (read_device_table (otf, stream, offset + anchor->offset,
2033 &anchor->f.f2.XDeviceTable) < 0)
2036 if (anchor->f.f2.YDeviceTable.offset)
2038 if (read_device_table (otf, stream, offset + anchor->offset,
2039 &anchor->f.f2.YDeviceTable) < 0)
2044 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
2050 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
2051 OTF_MarkArray *array)
2053 char *errfmt = "MarkArray%s";
2055 OTF_StreamState state;
2058 READ_OFFSET (stream, array->offset);
2059 SAVE_STREAM (stream, state);
2060 SEEK_STREAM (stream, offset + array->offset);
2061 READ_UINT16 (stream, array->MarkCount);
2062 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
2063 for (i = 0; i < array->MarkCount; i++)
2065 READ_UINT16 (stream, array->MarkRecord[i].Class);
2066 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
2068 for (i = 0; i < array->MarkCount; i++)
2069 if (read_anchor (otf, stream, offset + array->offset,
2070 &array->MarkRecord[i].MarkAnchor) < 0)
2072 RESTORE_STREAM (stream, state);
2077 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2078 unsigned ClassCount, OTF_AnchorArray *array)
2080 char *errfmt = "AnchorArray%s";
2082 OTF_StreamState state;
2085 READ_OFFSET (stream, array->offset);
2086 SAVE_STREAM (stream, state);
2087 SEEK_STREAM (stream, offset + array->offset);
2088 READ_UINT16 (stream, array->Count);
2089 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2090 for (i = 0; i < array->Count; i++)
2092 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2094 for (j = 0; j < ClassCount; j++)
2095 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2097 for (i = 0; i < array->Count; i++)
2098 for (j = 0; j < ClassCount; j++)
2099 if (read_anchor (otf, stream, offset + array->offset,
2100 &array->AnchorRecord[i].Anchor[j]) < 0)
2102 RESTORE_STREAM (stream, state);
2106 static OTF_PairSet *
2107 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2108 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2110 char *errfmt = "PairSet%s";
2111 void *errret = NULL;
2112 OTF_StreamState state;
2116 OTF_MALLOC (set, num, "");
2117 for (i = 0; i < num; i++)
2118 READ_OFFSET (stream, set[i].offset);
2119 SAVE_STREAM (stream, state);
2120 for (i = 0; i < num; i++)
2122 SEEK_STREAM (stream, offset + set[i].offset);
2123 READ_UINT16 (stream, set[i].PairValueCount);
2124 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2125 for (j = 0; j < set[i].PairValueCount; j++)
2127 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2129 READ_UINT16 (stream, rec->SecondGlyph);
2130 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2131 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2134 RESTORE_STREAM (stream, state);
2138 static OTF_Class1Record *
2139 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2140 unsigned num1, enum OTF_ValueFormat bit1,
2141 unsigned num2, enum OTF_ValueFormat bit2)
2143 char *errfmt = "Class1Record%s";
2144 void *errret = NULL;
2145 OTF_Class1Record *rec;
2148 OTF_MALLOC (rec, num1, "");
2149 for (i = 0; i < num1; i++)
2151 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2152 for (j = 0; j < num2; j++)
2154 if (read_value_record (otf, stream, offset,
2155 bit1, &rec[i].Class2Record[j].Value1) < 0
2156 || read_value_record (otf, stream, offset,
2157 bit2, &rec[i].Class2Record[j].Value2) < 0)
2165 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2166 OTF_EntryExitRecord **rec)
2168 char *errfmt = "EntryExitSet%s";
2172 OTF_StreamState state;
2174 READ_UINT16 (stream, count);
2176 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2177 OTF_MALLOC (*rec, count, "");
2178 for (i = 0; i < count; i++)
2180 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2181 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2183 SAVE_STREAM (stream, state);
2184 for (i = 0; i < count; i++)
2186 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2188 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2191 RESTORE_STREAM (stream, state);
2196 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2197 unsigned ClassCount, OTF_LigatureAttach *attach)
2199 char *errfmt = "LigatureAttach%s";
2203 SEEK_STREAM (stream, offset + attach->offset);
2204 READ_UINT16 (stream, attach->ComponentCount);
2205 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2206 for (i = 0; i < attach->ComponentCount; i++)
2208 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2209 " (ComponentRecord)");
2210 for (j = 0; j < ClassCount; j++)
2211 READ_OFFSET (stream,
2212 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2214 for (i = 0; i < attach->ComponentCount; i++)
2215 for (j = 0; j < ClassCount; j++)
2216 if (read_anchor (otf, stream, offset + attach->offset,
2217 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2223 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2224 unsigned class_count, OTF_LigatureArray *array)
2226 char *errfmt = "LigatureArray%s";
2228 OTF_StreamState state;
2231 READ_OFFSET (stream, array->offset);
2232 SAVE_STREAM (stream, state);
2233 SEEK_STREAM (stream, offset + array->offset);
2234 READ_UINT16 (stream, array->LigatureCount);
2235 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2236 for (i = 0; i < array->LigatureCount; i++)
2237 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2238 for (i = 0; i < array->LigatureCount; i++)
2239 read_ligature_attach (otf, stream, offset + array->offset,
2240 class_count, array->LigatureAttach + i);
2241 RESTORE_STREAM (stream, state);
2246 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2247 long offset, unsigned type,
2248 OTF_LookupSubTableGPOS *subtable)
2253 SEEK_STREAM (stream, offset);
2254 READ_UINT16 (stream, subtable->Format);
2255 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2259 if (subtable->Format == 1)
2261 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2263 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2264 read_value_record (otf, stream, offset,
2265 subtable->u.single1.ValueFormat,
2266 &subtable->u.single1.Value);
2268 else if (subtable->Format == 2)
2270 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2273 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2275 READ_UINT16 (stream, single2->ValueFormat);
2276 READ_UINT16 (stream, single2->ValueCount);
2277 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2278 for (i = 0; i < single2->ValueCount; i++)
2279 read_value_record (otf, stream, offset, single2->ValueFormat,
2280 single2->Value + i);
2283 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2287 if (subtable->Format == 1)
2289 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2291 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2292 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2293 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2294 subtable->u.pair1.PairSet
2295 = read_pair_set_list (otf, stream, offset,
2296 subtable->u.pair1.PairSetCount,
2297 subtable->u.pair1.ValueFormat1,
2298 subtable->u.pair1.ValueFormat2);
2299 if (! subtable->u.pair1.PairSet)
2302 else if (subtable->Format == 2)
2304 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2306 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2307 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2308 if (read_class_def (otf, stream, offset,
2309 &subtable->u.pair2.ClassDef1) < 0
2310 || read_class_def (otf, stream, offset,
2311 &subtable->u.pair2.ClassDef2) < 0)
2313 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2314 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2315 subtable->u.pair2.Class1Record
2316 = read_class1_record_list (otf, stream, offset,
2317 subtable->u.pair2.Class1Count,
2318 subtable->u.pair2.ValueFormat1,
2319 subtable->u.pair2.Class2Count,
2320 subtable->u.pair2.ValueFormat2);
2321 if (! subtable->u.pair2.Class1Record)
2325 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2329 if (subtable->Format == 1)
2331 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2333 subtable->u.cursive1.EntryExitCount
2334 = read_entry_exit_list (otf, stream, offset,
2335 &subtable->u.cursive1.EntryExitRecord);
2336 if (! subtable->u.cursive1.EntryExitCount)
2340 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2344 if (subtable->Format == 1)
2346 read_coverage (otf, stream, offset, &subtable->Coverage);
2347 read_coverage (otf, stream, offset,
2348 &subtable->u.mark_base1.BaseCoverage);
2349 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2350 read_mark_array (otf, stream, offset,
2351 &subtable->u.mark_base1.MarkArray);
2352 read_anchor_array (otf, stream, offset,
2353 subtable->u.mark_base1.ClassCount,
2354 &subtable->u.mark_base1.BaseArray);
2357 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2361 if (subtable->Format == 1)
2363 read_coverage (otf, stream, offset, &subtable->Coverage);
2364 read_coverage (otf, stream, offset,
2365 &subtable->u.mark_lig1.LigatureCoverage);
2366 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2367 read_mark_array (otf, stream, offset,
2368 &subtable->u.mark_lig1.MarkArray);
2369 read_ligature_array (otf, stream, offset,
2370 subtable->u.mark_lig1.ClassCount,
2371 &subtable->u.mark_lig1.LigatureArray);
2376 if (subtable->Format == 1)
2378 read_coverage (otf, stream, offset, &subtable->Coverage);
2379 read_coverage (otf, stream, offset,
2380 &subtable->u.mark_mark1.Mark2Coverage);
2381 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2382 read_mark_array (otf, stream, offset,
2383 &subtable->u.mark_mark1.Mark1Array);
2384 read_anchor_array (otf, stream, offset,
2385 subtable->u.mark_mark1.ClassCount,
2386 &subtable->u.mark_mark1.Mark2Array);
2389 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2393 if (subtable->Format == 1)
2395 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2396 &subtable->u.context1) < 0)
2399 else if (subtable->Format == 2)
2401 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2402 &subtable->u.context2) < 0)
2405 else if (subtable->Format == 3)
2407 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2408 &subtable->u.context3) < 0)
2412 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2416 if (subtable->Format == 1)
2418 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2419 &subtable->u.chain_context1) < 0)
2422 else if (subtable->Format == 2)
2424 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2425 &subtable->u.chain_context2) < 0)
2428 else if (subtable->Format == 3)
2430 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2431 &subtable->u.chain_context3) < 0)
2435 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2439 if (subtable->Format == 1)
2443 OTF_LookupSubTableGPOS *ex_subtable;
2445 READ_USHORT (stream, ex_type);
2446 READ_ULONG (stream, ex_offset);
2447 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2448 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2449 ex_type, ex_subtable) < 0)
2451 subtable->u.extension1.ExtensionLookupType = ex_type;
2452 subtable->u.extension1.ExtensionOffset = ex_offset;
2453 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2456 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2460 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2466 read_gpos_table (OTF *otf, OTF_Stream *stream)
2468 return read_gsub_gpos_table (otf, stream, 0);
2476 read_base_table (OTF_Stream *stream, long offset)
2480 OTF_MALLOC (base, 1);
2489 read_jstf_table (OTF_Stream *stream, long offset)
2493 OTF_MALLOC (jstf, 1);
2499 /*** (1-11) Structure for OTF */
2502 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2506 READ_FIXED (stream, table->sfnt_version);
2507 READ_USHORT (stream, table->numTables);
2508 READ_USHORT (stream, table->searchRange);
2509 READ_USHORT (stream, table->enterSelector);
2510 READ_USHORT (stream, table->rangeShift);
2515 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2520 READ_TAG (stream, tag);
2522 table->name[0] = tag >> 24;
2523 table->name[1] = (tag >> 16) & 0xFF;
2524 table->name[0] = (tag >> 8) & 0xFF;
2525 table->name[0] = tag >> 8;
2526 table->name[0] = '\0';
2527 READ_ULONG (stream, table->checkSum);
2528 READ_ULONG (stream, table->offset);
2529 READ_ULONG (stream, table->length);
2534 read_header_part (OTF *otf, FILE *fp)
2536 char *errfmt = "otf header%s";
2538 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2541 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2543 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2544 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2545 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2546 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2547 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2548 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2549 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2550 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2551 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2552 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2553 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2554 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2556 head_tag = OTF_tag ("head");
2557 name_tag = OTF_tag ("name");
2558 cmap_tag = OTF_tag ("cmap");
2559 gdef_tag = OTF_tag ("GDEF");
2560 gsub_tag = OTF_tag ("GSUB");
2561 gpos_tag = OTF_tag ("GPOS");
2563 stream = make_stream ();
2567 internal_data->header_stream = stream;
2569 /* Size of Offset Table is 12 bytes. */
2570 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2572 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2575 /* Size of each Table Directory is 16 bytes. */
2576 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2577 "Table Directory") < 0)
2580 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2581 for (i = 0; i < otf->offset_table.numTables; i++)
2583 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2584 OTF_TableInfo *table_info = NULL;
2588 if (tag == head_tag)
2589 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2590 else if (tag == name_tag)
2591 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2592 else if (tag == cmap_tag)
2593 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2594 else if (tag == gdef_tag)
2595 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2596 else if (tag == gsub_tag)
2597 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2598 else if (tag == gpos_tag)
2599 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2603 table_info->stream = make_stream ();
2604 if (setup_stream (table_info->stream, fp,
2605 otf->table_dirs[i].offset,
2606 otf->table_dirs[i].length,
2607 otf->table_dirs[i].name) < 0)
2612 internal_data->header_stream = NULL;
2613 free_stream (stream);
2617 static OTF_TableInfo *
2618 get_table_info (OTF *otf, char *name)
2620 char *errfmt = "OTF Table Read%s";
2621 OTF_TableInfo *errret = NULL;
2622 OTF_InternalData *internal_data = otf->internal_data;
2623 OTF_TableInfo *table_info;
2624 OTF_Tag tag = OTF_tag (name);
2627 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2629 if (tag == OTF_tag ("head"))
2630 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2631 else if (tag == OTF_tag ("name"))
2632 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2633 else if (tag == OTF_tag ("cmap"))
2634 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2635 else if (tag == OTF_tag ("GDEF"))
2636 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2637 else if (tag == OTF_tag ("GSUB"))
2638 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2639 else if (tag == OTF_tag ("GPOS"))
2640 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2642 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2644 if (*table_info->address)
2647 if (! table_info->stream)
2648 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2649 if (! table_info->reader)
2650 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2656 /*** (2) API for reading OTF */
2658 /*** (2-1) OTF_open() */
2660 /* Note: We can't use memory allocation macros in the following
2661 functions because those macros return from the functions before
2662 freeing memory previously allocated. */
2665 OTF_open (char *otf_name)
2668 char *errfmt = "opening otf (%s)";
2669 void *errret = NULL;
2671 OTF_InternalData *internal_data;
2672 int len = strlen (otf_name);
2673 const char *ext = otf_name + (len - 4);
2677 || (ext[1] != 'O' && ext[1] != 'T' && ext[1] != 'o' && ext[1] != 't')
2678 || (ext[2] != 'T' && ext[2] != 't')
2679 || (ext[3] != 'F' && ext[3] != 'f'))
2680 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2681 fp = fopen (otf_name, "r");
2683 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2684 otf = calloc (1, sizeof (OTF));
2686 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2687 otf->filename = strdup (otf_name);
2688 if (! otf->filename)
2692 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2695 internal_data = calloc (1, sizeof (OTF_InternalData));
2696 if (! internal_data)
2697 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2698 otf->internal_data = internal_data;
2699 if (! allocate_memory_record (otf))
2700 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2702 /* Here after, all pointers to allocated memory are recorded in
2703 otf->internal_data->memory_record except for what allocated by
2704 the functions allocate_memory_record and make_stream. */
2706 if (read_header_part (otf, fp) < 0)
2717 /*** (2-2) OTF_close() */
2720 OTF_close (OTF *otf)
2722 OTF_InternalData *internal_data = otf->internal_data;
2727 OTF_MemoryRecord *memrec = internal_data->memory_record;
2729 if (internal_data->header_stream)
2730 free_stream (internal_data->header_stream);
2732 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2733 if (internal_data->table_info[i].stream)
2734 free_stream (internal_data->table_info[i].stream);
2738 OTF_MemoryRecord *next = memrec->next;
2740 for (i = memrec->used - 1; i >= 0; i--)
2741 free (memrec->memory[i]);
2745 free (internal_data);
2748 free (otf->filename);
2752 /*** (2-3) OTF_get_table() */
2755 OTF_get_table (OTF *otf, char *name)
2757 OTF_TableInfo *table_info = get_table_info (otf, name);
2762 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2763 free_stream (table_info->stream);
2764 table_info->stream = NULL;
2765 if (! *table_info->address)
2767 table_info->reader = NULL;
2773 /*** (2-4) OTF_check_table() */
2776 OTF_check_table (OTF *otf, char *name)
2778 return (get_table_info (otf, name) ? 0 : -1);
2784 /*** (5) API miscellaneous ***/
2787 OTF_tag (char *name)
2789 unsigned char *p = (unsigned char *) name;
2793 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2797 OTF_tag_name (OTF_Tag tag, char *name)
2799 name[0] = (char) (tag >> 24);
2800 name[1] = (char) ((tag >> 16) & 0xFF);
2801 name[2] = (char) ((tag >> 8) & 0xFF);
2802 name[3] = (char) (tag & 0xFF);