1 /* otfopen.c -- OpenType font reader.
3 Copyright (C) 2003, 2004, 2005
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;
250 struct _OTF_TableInfo;
251 typedef struct _OTF_TableInfo OTF_TableInfo;
253 struct _OTF_TableInfo
255 /* Points to one of OTF->head, OTF->name, etc. */
257 /* Function to read one of OTF tables. */
258 void *(*reader) (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag);
259 /* Stream given to <reader>. */
263 struct OTF_InternalData
265 /* Information about each OTF table. */
266 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
268 /* Stream used to read the header part of OTF. */
269 OTF_Stream *header_stream;
271 /* Records of allocated memories. */
272 OTF_MemoryRecord *memory_record;
276 static OTF_MemoryRecord *
277 allocate_memory_record (OTF *otf)
279 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
280 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
285 memrec->next = internal_data->memory_record;
286 internal_data->memory_record = memrec;
290 /* Memory allocation macros. */
292 #define OTF_MALLOC(p, size, arg) \
298 OTF_MemoryRecord *memrec \
299 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
300 (p) = malloc (sizeof (*(p)) * (size)); \
302 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
303 && ! (memrec = allocate_memory_record (otf)))) \
304 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
305 memrec->memory[memrec->used++] = (p); \
310 #define OTF_CALLOC(p, size, arg) \
316 OTF_MemoryRecord *memrec \
317 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
318 (p) = calloc ((size), sizeof (*(p))); \
320 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
321 && ! (memrec = allocate_memory_record (otf)))) \
322 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
323 memrec->memory[memrec->used++] = (p); \
328 /*** (1-2) "head" table */
331 read_head_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
333 OTF_Stream *stream = table->stream;
334 char *errfmt = "head%s";
338 OTF_CALLOC (head, 1, "");
339 READ_FIXED (stream, head->TableVersionNumber);
340 READ_FIXED (stream, head->fontRevision);
341 READ_ULONG (stream, head->checkSumAdjustment);
342 READ_ULONG (stream, head->magicNumber);
343 READ_USHORT (stream, head->flags);
344 READ_USHORT (stream, head->unitsPerEm);
346 *table->address = head;
351 /*** (1-3) "name" table */
354 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
356 char *errfmt = "nameID (%d)";
358 OTF_StreamState state;
363 if (rec->platformID == 0)
364 ucs = (rec->encodingID <= 3) ? 2 : 4;
365 else if (rec->platformID == 1 && rec->encodingID == 0)
367 else if (rec->platformID == 3)
368 ucs = (rec->encodingID == 1 ? 2
369 : rec->encodingID == 10 ? 4
372 OTF_MALLOC (rec->name, rec->length + 1, (void *) rec->nameID);
373 SAVE_STREAM (stream, state);
374 SEEK_STREAM (stream, stream->pos + rec->offset);
375 READ_BYTES (stream, rec->name, rec->length);
376 RESTORE_STREAM (stream, state);
377 rec->name[rec->length] = 0;
386 for (i = 0; i < rec->length / 2; i++)
388 if (rec->name[i * 2] > 0
389 || rec->name[i * 2 + 1] >= 128)
396 for (i = 0; i < rec->length / 2; i++)
397 rec->name[i] = rec->name[i * 2 + 1];
403 for (i = 0; i < rec->length / 4; i++)
405 if (rec->name[i * 4] > 0
406 || rec->name[i * 4 + 1] > 0
407 || rec->name[i * 4 + 2] > 0
408 || rec->name[i * 2 + 3] >= 128)
415 for (i = 0; i < rec->length / 4; i++)
416 rec->name[i] = rec->name[i * 4 + 3];
424 read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
426 OTF_Stream *stream = table->stream;
427 char *errfmt = "name%s";
432 OTF_CALLOC (name, 1, "");
433 READ_USHORT (stream, name->format);
434 READ_USHORT (stream, name->count);
435 READ_USHORT (stream, name->stringOffset);
436 OTF_MALLOC (name->nameRecord, name->count, "");
437 for (i = 0; i < name->count; i++)
439 OTF_NameRecord *rec = name->nameRecord + i;
441 READ_USHORT (stream, rec->platformID);
442 READ_USHORT (stream, rec->encodingID);
443 READ_USHORT (stream, rec->languageID);
444 READ_USHORT (stream, rec->nameID);
445 READ_USHORT (stream, rec->length);
446 READ_USHORT (stream, rec->offset);
448 for (i = 0; i < name->count; i++)
450 OTF_NameRecord *rec = name->nameRecord + i;
451 int nameID = rec->nameID;
453 read_name (otf, stream, rec);
455 if (nameID >= OTF_max_nameID)
457 if (! name->name[nameID]
459 name->name[nameID] = (char *) rec->name;
462 *table->address = name;
467 /*** (1-4) "cmap" table */
470 read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
472 OTF_Stream *stream = table->stream;
473 char *errfmt = "cmap%s";
476 int unicode_bmp_index = -1, unicode_full_index = -1;
479 OTF_CALLOC (cmap, 1, "");
480 READ_USHORT (stream, cmap->version);
481 READ_USHORT (stream, cmap->numTables);
482 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
483 for (i = 0; i < cmap->numTables; i++)
485 unsigned platformID, encodingID;
487 READ_USHORT (stream, platformID);
488 cmap->EncodingRecord[i].platformID = platformID;
489 READ_USHORT (stream, encodingID);
490 cmap->EncodingRecord[i].encodingID = encodingID;
491 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
495 unicode_bmp_index = i;
497 unicode_full_index = i;
499 else if (platformID == 3)
502 unicode_bmp_index = i;
503 else if (encodingID == 10)
504 unicode_full_index = i;
507 for (i = 0; i < cmap->numTables; i++)
511 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
512 READ_USHORT (stream, format);
513 cmap->EncodingRecord[i].subtable.format = format;
514 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
515 if (format == 8 || format == 10 || format == 12)
517 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
518 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
522 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
528 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
529 " (EncodingRecord)");
531 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
538 OTF_EncodingSubtable2 *sub2;
541 OTF_MALLOC (sub2, 1, " (EncodingSubtable2)");
542 cmap->EncodingRecord[i].subtable.f.f2 = sub2;
543 for (j = 0, max_key = 0; j < 256; j++)
545 READ_USHORT (stream, sub2->subHeaderKeys[j]);
546 if (max_key < sub2->subHeaderKeys[j])
547 max_key = sub2->subHeaderKeys[j];
550 sub2->subHeaderCount = max_key / 8;
551 OTF_MALLOC (sub2->subHeaders, max_key / 8, " (subHeaders)");
552 for (j = 0; j < sub2->subHeaderCount; j++)
554 READ_USHORT (stream, sub2->subHeaders[j].firstCode);
555 READ_USHORT (stream, sub2->subHeaders[j].entryCount);
556 READ_SHORT (stream, sub2->subHeaders[j].idDelta);
557 READ_USHORT (stream, sub2->subHeaders[j].idRangeOffset);
558 /* Make it offset from sub2->glyphIndexArray. */
559 sub2->subHeaders[j].idRangeOffset -= max_key - (j * 8 + 6);
561 sub2->glyphIndexCount = (cmap->EncodingRecord[i].subtable.length
563 OTF_MALLOC (sub2->glyphIndexArray, sub2->glyphIndexCount,
564 " (glyphIndexArray)");
565 READ_BYTES (stream, sub2->glyphIndexArray, sub2->glyphIndexCount);
571 OTF_EncodingSubtable4 *sub4;
576 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
577 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
578 READ_USHORT (stream, sub4->segCountX2);
579 segCount = sub4->segCountX2 / 2;
580 READ_USHORT (stream, sub4->searchRange);
581 READ_USHORT (stream, sub4->entrySelector);
582 READ_USHORT (stream, sub4->rangeShift);
583 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
584 for (j = 0; j < segCount; j++)
585 READ_USHORT (stream, sub4->segments[j].endCount);
586 READ_USHORT (stream, dummy);
587 for (j = 0; j < segCount; j++)
588 READ_USHORT (stream, sub4->segments[j].startCount);
589 for (j = 0; j < segCount; j++)
590 READ_SHORT (stream, sub4->segments[j].idDelta);
591 for (j = 0; j < segCount; j++)
594 unsigned rest = 2 * (segCount - j);
596 READ_USHORT (stream, off);
598 sub4->segments[j].idRangeOffset = 0xFFFF;
600 sub4->segments[j].idRangeOffset = (off - rest) / 2;
602 j = (cmap->EncodingRecord[i].subtable.length
603 - (14 + 2 * (segCount * 4 + 1)));
604 sub4->GlyphCount = j / 2;
605 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
606 for (j = 0; j < sub4->GlyphCount; j++)
607 READ_USHORT (stream, sub4->glyphIdArray[j]);
613 OTF_EncodingSubtable6 *sub6;
616 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
617 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
618 READ_USHORT (stream, sub6->firstCode);
619 READ_USHORT (stream, sub6->entryCount);
620 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
621 for (j = 0; j < sub6->entryCount; j++)
622 READ_USHORT (stream, sub6->glyphIdArray[j]);
628 OTF_EncodingSubtable8 *sub8;
631 OTF_MALLOC (sub8, 1, " (EncodingSubtable8)");
632 cmap->EncodingRecord[i].subtable.f.f8 = sub8;
633 for (j = 0; j < 8192; j++)
634 READ_BYTES (stream, sub8->is32, 8192);
635 READ_ULONG (stream, sub8->nGroups);
636 OTF_MALLOC (sub8->Groups, sub8->nGroups, " (Groups)");
637 for (j = 0; j < sub8->nGroups; j++)
639 READ_ULONG (stream, sub8->Groups[i].startCharCode);
640 READ_ULONG (stream, sub8->Groups[i].endCharCode);
641 READ_ULONG (stream, sub8->Groups[i].startGlyphID);
648 OTF_EncodingSubtable10 *sub10;
651 OTF_MALLOC (sub10, 1, " (EncodingSubtable10)");
652 cmap->EncodingRecord[i].subtable.f.f10 = sub10;
653 READ_ULONG (stream, sub10->startCharCode);
654 READ_ULONG (stream, sub10->numChars);
655 OTF_MALLOC (sub10->glyphs, sub10->numChars, " (GlyphCount)");
656 for (j = 0; j < sub10->numChars; j++)
657 READ_USHORT (stream, sub10->glyphs[j]);
663 OTF_EncodingSubtable12 *sub12;
666 OTF_MALLOC (sub12, 1, " (EncodingSubtable12)");
667 cmap->EncodingRecord[i].subtable.f.f12 = sub12;
668 READ_ULONG (stream, sub12->nGroups);
669 OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
670 for (j = 0; j < sub12->nGroups; j++)
672 READ_ULONG (stream, sub12->Groups[i].startCharCode);
673 READ_ULONG (stream, sub12->Groups[i].endCharCode);
674 READ_ULONG (stream, sub12->Groups[i].startGlyphID);
680 OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
684 if (unicode_bmp_index >= 0)
686 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_bmp_index;
687 OTF_GlyphID glyph_id, max_glyph_id = 0;
689 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
690 switch (rec->subtable.format)
694 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
695 int segCount = sub4->segCountX2 / 2;
697 for (i = 0; i < segCount; i++)
699 OTF_cmapSegument *seg = sub4->segments + i;
702 if (seg->idRangeOffset == 0xFFFF)
703 for (c = seg->startCount; c <= seg->endCount; c++)
705 glyph_id = c + seg->idDelta;
706 cmap->unicode_table[c] = glyph_id;
707 if (glyph_id > max_glyph_id)
708 max_glyph_id = glyph_id;
711 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
714 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
715 + (c - seg->startCount)];
716 cmap->unicode_table[c] = glyph_id;
717 if (glyph_id > max_glyph_id)
718 max_glyph_id = glyph_id;
724 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
725 for (i = 0; i < 0x10000; i++)
726 if (cmap->unicode_table[i])
727 cmap->decode_table[cmap->unicode_table[i]] = i;
728 cmap->max_glyph_id = max_glyph_id;
731 *table->address = cmap;
736 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
738 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
739 Glyph-IDs there. If COUNT is negative, read the number of
740 Glyphs-IDs at first. MINUS if nozero is how few the actual
741 Glyph-IDs are in STREAM than COUNT. */
744 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
745 int minus, int count)
747 char *errfmt = "GlyphID List%s";
752 READ_UINT16 (stream, count);
755 OTF_MALLOC (*ids, count, "");
756 for (i = 0; i < count + minus; i++)
757 READ_GLYPHID (stream, (*ids)[i]);
762 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
764 char *errfmt = "RangeRecord%s";
769 READ_UINT16 (stream, count);
772 OTF_MALLOC (*record, count, "");
773 for (i = 0; i < count; i++)
775 READ_GLYPHID (stream, (*record)[i].Start);
776 READ_GLYPHID (stream, (*record)[i].End);
777 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
784 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
785 OTF_Coverage *coverage)
787 char *errfmt = "Coverage%s";
789 OTF_StreamState state;
792 READ_OFFSET (stream, coverage->offset);
793 SAVE_STREAM (stream, state);
794 SEEK_STREAM (stream, offset + coverage->offset);
795 READ_UINT16 (stream, coverage->CoverageFormat);
796 if (coverage->CoverageFormat == 1)
797 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
798 else if (coverage->CoverageFormat == 2)
799 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
801 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
804 coverage->Count = (unsigned) count;
805 RESTORE_STREAM (stream, state);
809 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
810 and store the Coverages there. If COUNT is negative, read the
811 number of Coverages at first. */
814 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
815 OTF_Coverage **coverage, int count)
817 char *errfmt = "Coverage List%s";
822 READ_UINT16 (stream, count);
825 OTF_MALLOC (*coverage, count, "");
826 for (i = 0; i < count; i++)
827 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
834 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
837 char *errfmt = "ClassDef%s";
840 SEEK_STREAM (stream, class->offset);
841 READ_UINT16 (stream, class->ClassFormat);
842 if (class->ClassFormat == 1)
844 READ_GLYPHID (stream, class->f.f1.StartGlyph);
845 class->f.f1.GlyphCount
847 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
848 if (! class->f.f1.GlyphCount)
849 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
851 else if (class->ClassFormat == 2)
853 class->f.f2.ClassRangeCount
854 = (read_range_records
855 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
856 if (! class->f.f2.ClassRangeCount)
857 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
860 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
866 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
868 char *errfmt = "ClassDef%s";
870 OTF_StreamState state;
872 READ_OFFSET (stream, class->offset);
875 SAVE_STREAM (stream, state);
876 SEEK_STREAM (stream, offset + class->offset);
877 READ_UINT16 (stream, class->ClassFormat);
878 if (class->ClassFormat == 1)
880 READ_GLYPHID (stream, class->f.f1.StartGlyph);
881 class->f.f1.GlyphCount
882 = read_glyph_ids (otf, stream,
883 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
885 if (! class->f.f1.GlyphCount)
888 else if (class->ClassFormat == 2)
890 class->f.f2.ClassRangeCount
891 = read_range_records (otf, stream,
893 &class->f.f2.ClassRangeRecord);
894 if (! class->f.f2.ClassRangeCount)
898 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
900 RESTORE_STREAM (stream, state);
906 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
907 OTF_DeviceTable *table)
909 char *errfmt = "Device Table%s";
920 SEEK_STREAM (stream, offset + table->offset);
921 READ_UINT16 (stream, table->StartSize);
922 READ_UINT16 (stream, table->EndSize);
923 READ_UINT16 (stream, table->DeltaFormat);
924 num = table->EndSize - table->StartSize + 1;
925 OTF_MALLOC (table->DeltaValue, num, "");
927 if (table->DeltaFormat == 1)
928 for (i = 0; i < num; i++)
931 READ_UINT16 (stream, val);
932 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
933 table->DeltaValue[i] = intval.int2;
935 else if (table->DeltaFormat == 2)
936 for (i = 0; i < num; i++)
939 READ_UINT16 (stream, val);
940 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
941 table->DeltaValue[i] = intval.int4;
943 else if (table->DeltaFormat == 3)
944 for (i = 0; i < num; i++)
948 READ_UINT16 (stream, val);
949 intval.int8 = val >> 8;
950 table->DeltaValue[i] = intval.int8;
954 intval.int8 = val >> 8;
955 table->DeltaValue[i] = intval.int8;
959 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
964 /*** (1-6) "GDEF" table */
967 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
968 OTF_AttachList *list)
970 char *errfmt = "AttachList%s";
974 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
976 READ_UINT16 (stream, list->GlyphCount);
977 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
978 for (i = 0; i < list->GlyphCount; i++)
979 READ_OFFSET (stream, list->AttachPoint[i].offset);
980 for (i = 0; i < list->GlyphCount; i++)
984 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
985 READ_UINT16 (stream, count);
986 list->AttachPoint[i].PointCount = count;
987 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
988 for (j = 0; j < count; j++)
989 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
995 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
996 OTF_CaretValue *caret)
998 char *errfmt = "CaretValue%s";
1001 SEEK_STREAM (stream, offset + caret->offset);
1002 READ_UINT16 (stream, caret->CaretValueFormat);
1003 if (caret->CaretValueFormat == 1)
1004 READ_INT16 (stream, caret->f.f1.Coordinate);
1005 else if (caret->CaretValueFormat == 2)
1006 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1007 else if (caret->CaretValueFormat == 3)
1009 READ_INT16 (stream, caret->f.f3.Coordinate);
1010 if (read_device_table (otf, stream, offset + caret->offset,
1011 &caret->f.f3.DeviceTable) < 0)
1015 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1020 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1021 OTF_LigCaretList *list)
1023 char *errfmt = "LigCaretList%s";
1027 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1029 READ_UINT16 (stream, list->LigGlyphCount);
1030 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1031 for (i = 0; i < list->LigGlyphCount; i++)
1032 READ_OFFSET (stream, list->LigGlyph[i].offset);
1033 for (i = 0; i < list->LigGlyphCount; i++)
1037 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1038 READ_UINT16 (stream, count);
1039 list->LigGlyph[i].CaretCount = count;
1040 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1041 for (j = 0; j < count; j++)
1042 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1043 for (j = 0; j < count; j++)
1044 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1045 &list->LigGlyph[i].CaretValue[j]) < 0)
1052 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1056 READ_FIXED (stream, header->Version);
1057 READ_OFFSET (stream, header->GlyphClassDef);
1058 READ_OFFSET (stream, header->AttachList);
1059 READ_OFFSET (stream, header->LigCaretList);
1060 READ_OFFSET (stream, header->MarkAttachClassDef);
1065 read_gdef_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
1067 OTF_Stream *stream = table->stream;
1068 char *errfmt = "GDEF%s";
1069 void *errret = NULL;
1072 OTF_CALLOC (gdef, 1, "");
1073 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1074 if (gdef->header.GlyphClassDef)
1076 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1077 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1079 if (gdef->header.AttachList)
1080 read_attach_list (otf, stream, gdef->header.AttachList,
1081 &gdef->attach_list);
1082 if (gdef->header.LigCaretList)
1083 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1084 &gdef->lig_caret_list);
1085 if (gdef->header.MarkAttachClassDef)
1087 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1088 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1091 *table->address = gdef;
1096 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
1099 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
1100 OTF_ScriptList *list)
1102 char *errfmt = "Script List%s";
1106 SEEK_STREAM (stream, offset);
1107 READ_USHORT (stream, list->ScriptCount);
1108 OTF_CALLOC (list->Script, list->ScriptCount, "");
1110 for (i = 0; i < list->ScriptCount; i++)
1112 READ_TAG (stream, list->Script[i].ScriptTag);
1113 READ_OFFSET (stream, list->Script[i].offset);
1115 for (i = 0; i < list->ScriptCount; i++)
1117 OTF_Script *script = list->Script + i;
1118 long script_offset = offset + script->offset;
1120 SEEK_STREAM (stream, script_offset);
1121 READ_OFFSET (stream, script->DefaultLangSysOffset);
1122 READ_USHORT (stream, script->LangSysCount);
1123 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1124 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1125 for (j = 0; j < script->LangSysCount; j++)
1127 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1128 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1131 if (script->DefaultLangSysOffset)
1133 OTF_LangSys *langsys = &script->DefaultLangSys;
1135 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1136 READ_OFFSET (stream, langsys->LookupOrder);
1137 READ_USHORT (stream, langsys->ReqFeatureIndex);
1138 READ_USHORT (stream, langsys->FeatureCount);
1139 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1141 for (k = 0; k < langsys->FeatureCount; k++)
1142 READ_USHORT (stream, langsys->FeatureIndex[k]);
1145 for (j = 0; j < script->LangSysCount; j++)
1147 OTF_LangSys *langsys = script->LangSys + j;
1149 SEEK_STREAM (stream,
1150 script_offset + script->LangSysRecord[j].LangSys);
1151 READ_OFFSET (stream, langsys->LookupOrder);
1152 READ_USHORT (stream, langsys->ReqFeatureIndex);
1153 READ_USHORT (stream, langsys->FeatureCount);
1154 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1156 for (k = 0; k < langsys->FeatureCount; k++)
1157 READ_USHORT (stream, langsys->FeatureIndex[k]);
1165 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1166 OTF_FeatureList *list)
1168 char *errfmt = "Feature List%s";
1172 SEEK_STREAM (stream, offset);
1173 READ_UINT16 (stream, list->FeatureCount);
1174 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1175 for (i = 0; i < list->FeatureCount; i++)
1177 READ_TAG (stream, list->Feature[i].FeatureTag);
1178 READ_OFFSET (stream, list->Feature[i].offset);
1180 for (i = 0; i < list->FeatureCount; i++)
1182 OTF_Feature *feature = list->Feature + i;
1184 SEEK_STREAM (stream, offset + feature->offset);
1185 READ_OFFSET (stream, feature->FeatureParams);
1186 READ_UINT16 (stream, feature->LookupCount);
1187 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1188 " (LookupListIndex)");
1189 for (j = 0; j < feature->LookupCount; j++)
1190 READ_UINT16 (stream, feature->LookupListIndex[j]);
1196 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1197 long offset, unsigned type,
1198 OTF_LookupSubTableGSUB *subtable);
1199 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1200 long offset, unsigned type,
1201 OTF_LookupSubTableGPOS *subtable);
1204 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1205 OTF_LookupList *list, int gsubp)
1207 char *errfmt = "Lookup List%s";
1211 SEEK_STREAM (stream, offset);
1212 READ_UINT16 (stream, list->LookupCount);
1213 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1215 for (i = 0; i < list->LookupCount; i++)
1216 READ_OFFSET (stream, list->Lookup[i].offset);
1217 for (i = 0; i < list->LookupCount; i++)
1219 OTF_Lookup *lookup = list->Lookup + i;
1221 SEEK_STREAM (stream, offset + lookup->offset);
1222 READ_UINT16 (stream, lookup->LookupType);
1223 READ_UINT16 (stream, lookup->LookupFlag);
1224 READ_UINT16 (stream, lookup->SubTableCount);
1225 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1226 " (SubTableOffset)");
1228 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1231 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1233 for (j = 0; j < lookup->SubTableCount; j++)
1234 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1235 for (j = 0; j < lookup->SubTableCount; j++)
1238 = offset + lookup->offset + lookup->SubTableOffset[j];
1241 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1243 lookup->SubTable.gsub + j) < 0
1244 : read_lookup_subtable_gpos (otf, stream, this_offset,
1246 lookup->SubTable.gpos + j) < 0)
1255 /*** (1-8) Structures common to GSUB and GPOS */
1258 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1259 OTF_LookupRecord **record, int count)
1261 char *errfmt = "LookupRecord%s";
1262 unsigned errret = 0;
1266 READ_UINT16 (stream, count);
1268 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1269 OTF_MALLOC (*record, count, "");
1270 for (i = 0; i < count; i++)
1272 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1273 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1279 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1281 char *errfmt = "List of Rule%s";
1282 unsigned errret = 0;
1283 OTF_StreamState state;
1287 READ_UINT16 (stream, count);
1289 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1290 OTF_MALLOC (*rule, count, "");
1291 for (i = 0; i < count; i++)
1293 READ_OFFSET (stream, (*rule)[i].offset);
1294 if (! (*rule)[i].offset)
1295 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1297 SAVE_STREAM (stream, state);
1298 for (i = 0; i < count; i++)
1300 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1301 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1302 if ((*rule)[i].GlyphCount == 0)
1303 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1304 READ_UINT16 (stream, (*rule)[i].LookupCount);
1305 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1306 (*rule)[i].GlyphCount) < 0)
1308 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1309 (*rule)[i].LookupCount) == 0)
1312 RESTORE_STREAM (stream, state);
1318 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1321 char *errfmt = "List of RuleSet%s";
1322 unsigned errret = 0;
1323 OTF_StreamState state;
1327 READ_UINT16 (stream, count);
1329 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1330 OTF_MALLOC (*set, count, "");
1331 for (i = 0; i < count; i++)
1333 READ_OFFSET (stream, (*set)[i].offset);
1334 if (! (*set)[i].offset)
1335 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1337 SAVE_STREAM (stream, state);
1338 for (i = 0; i < count; i++)
1340 SEEK_STREAM (stream, offset + (*set)[i].offset);
1342 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1344 if (! (*set)[i].RuleCount)
1347 RESTORE_STREAM (stream, state);
1352 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1353 OTF_ClassRule **rule)
1355 char *errfmt = "ClassRule%s";
1356 unsigned errret = 0;
1357 OTF_StreamState state;
1361 READ_UINT16 (stream, count);
1363 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1364 OTF_MALLOC (*rule, count, "");
1365 for (i = 0; i < count; i++)
1367 READ_OFFSET (stream, (*rule)[i].offset);
1368 if (! (*rule)[i].offset)
1369 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1371 SAVE_STREAM (stream, state);
1372 for (i = 0; i < count; i++)
1374 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1375 READ_USHORT (stream, (*rule)[i].GlyphCount);
1376 if (! (*rule)[i].GlyphCount)
1377 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1378 READ_USHORT (stream, (*rule)[i].LookupCount);
1379 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1380 0, (*rule)[i].GlyphCount - 1) < 0)
1382 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1383 (*rule)[i].LookupCount) == 0)
1386 RESTORE_STREAM (stream, state);
1391 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1394 char *errfmt = "ClassSet%s";
1395 unsigned errret = 0;
1396 OTF_StreamState state;
1400 READ_UINT16 (stream, count);
1402 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1403 OTF_CALLOC (*set, count, "");
1404 for (i = 0; i < count; i++)
1405 /* Offset can be zero. */
1406 READ_OFFSET (stream, (*set)[i].offset);
1407 SAVE_STREAM (stream, state);
1408 for (i = 0; i < count; i++)
1409 if ((*set)[i].offset)
1411 SEEK_STREAM (stream, offset + (*set)[i].offset);
1412 (*set)[i].ClassRuleCnt
1413 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1414 &(*set)[i].ClassRule);
1415 if (! (*set)[i].ClassRuleCnt)
1418 RESTORE_STREAM (stream, state);
1423 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1424 OTF_ChainRule **rule)
1426 char *errfmt = "ChainRule%s";
1427 unsigned errret = 0;
1431 READ_UINT16 (stream, count);
1433 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1434 OTF_MALLOC (*rule, count, "");
1435 for (i = 0; i < count; i++)
1436 READ_OFFSET (stream, (*rule)[i].offset);
1437 for (i = 0; i < count; i++)
1439 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1440 (*rule)[i].BacktrackGlyphCount
1441 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1442 (*rule)[i].InputGlyphCount
1443 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1444 if (! (*rule)[i].InputGlyphCount)
1445 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1446 (*rule)[i].LookaheadGlyphCount
1447 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1448 (*rule)[i].LookupCount
1449 = read_lookup_record_list (otf, stream,
1450 &(*rule)[i].LookupRecord, -1);
1451 if (! (*rule)[i].LookupCount)
1459 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1460 OTF_ChainRuleSet **set)
1462 char *errfmt = "ChainRuleSet%s";
1463 unsigned errret = 0;
1464 OTF_StreamState state;
1468 READ_UINT16 (stream, count);
1470 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1471 OTF_MALLOC (*set, count, "");
1472 for (i = 0; i < count; i++)
1474 READ_OFFSET (stream, (*set)[i].offset);
1475 if (! (*set)[i].offset)
1476 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1478 SAVE_STREAM (stream, state);
1479 for (i = 0; i < count; i++)
1481 SEEK_STREAM (stream, offset + (*set)[i].offset);
1482 (*set)[i].ChainRuleCount
1483 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1484 &(*set)[i].ChainRule);
1485 if (! (*set)[i].ChainRuleCount)
1488 RESTORE_STREAM (stream, state);
1493 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1494 OTF_ChainClassRule **rule)
1496 char *errfmt = "ChainClassRule%s";
1497 unsigned errret = 0;
1501 READ_UINT16 (stream, count);
1503 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1504 OTF_MALLOC (*rule, count, "");
1505 for (i = 0; i < count; i++)
1507 READ_OFFSET (stream, (*rule)[i].offset);
1508 if (! (*rule)[i].offset)
1509 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1511 for (i = 0; i < count; i++)
1513 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1514 (*rule)[i].BacktrackGlyphCount
1515 = read_glyph_ids (otf, stream,
1516 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1517 (*rule)[i].InputGlyphCount
1518 = read_glyph_ids (otf, stream,
1519 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1520 if (! (*rule)[i].InputGlyphCount)
1521 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1522 (*rule)[i].LookaheadGlyphCount
1523 = read_glyph_ids (otf, stream,
1524 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1525 (*rule)[i].LookupCount
1526 = read_lookup_record_list (otf, stream,
1527 &(*rule)[i].LookupRecord, -1);
1528 if (! (*rule)[i].LookupCount)
1535 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1536 OTF_ChainClassSet **set)
1538 char *errfmt = "ChainClassSet%s";
1539 unsigned errret = 0;
1540 OTF_StreamState state;
1544 READ_UINT16 (stream, count);
1546 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1547 OTF_MALLOC (*set, count, "");
1548 for (i = 0; i < count; i++)
1549 /* Offset may be zero. */
1550 READ_OFFSET (stream, (*set)[i].offset);
1551 SAVE_STREAM (stream, state);
1552 for (i = 0; i < count; i++)
1553 if ((*set)[i].offset)
1555 SEEK_STREAM (stream, offset + (*set)[i].offset);
1556 (*set)[i].ChainClassRuleCnt
1557 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1558 &(*set)[i].ChainClassRule);
1559 if (! (*set)[i].ChainClassRuleCnt)
1562 RESTORE_STREAM (stream, state);
1567 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1568 OTF_Coverage *coverage,OTF_Context1 *context1)
1570 if (read_coverage (otf, stream, offset, coverage) < 0)
1572 context1->RuleSetCount
1573 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1574 if (! context1->RuleSetCount)
1580 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1581 OTF_Coverage *coverage,OTF_Context2 *context2)
1583 if (read_coverage (otf, stream, offset, coverage) < 0
1584 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1586 context2->ClassSetCnt
1587 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1588 if (! context2->ClassSetCnt)
1594 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1595 OTF_Coverage *coverage,OTF_Context3 *context3)
1597 char *errfmt = "Context1%s";
1600 READ_USHORT (stream, context3->GlyphCount);
1601 if (context3->GlyphCount < 0)
1602 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1603 READ_USHORT (stream, context3->LookupCount);
1604 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1605 context3->GlyphCount) < 0)
1607 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1608 context3->LookupCount) < 0)
1614 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1615 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1617 if (read_coverage (otf, stream, offset, coverage) < 0)
1619 chain_context1->ChainRuleSetCount
1620 = read_chain_rule_set_list (otf, stream, offset,
1621 &chain_context1->ChainRuleSet);
1622 if (! chain_context1->ChainRuleSetCount)
1628 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1629 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1631 if (read_coverage (otf, stream, offset, coverage) < 0
1632 || read_class_def (otf, stream, offset,
1633 &chain_context2->BacktrackClassDef) < 0
1634 || read_class_def (otf, stream, offset,
1635 &chain_context2->InputClassDef) < 0
1636 || read_class_def (otf, stream, offset,
1637 &chain_context2->LookaheadClassDef) < 0)
1639 chain_context2->ChainClassSetCnt
1640 = read_chain_class_set_list (otf, stream, offset,
1641 &chain_context2->ChainClassSet);
1642 if (! chain_context2->ChainClassSetCnt)
1648 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1649 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1653 count = read_coverage_list (otf, stream, offset,
1654 &chain_context3->Backtrack, -1);
1657 chain_context3->BacktrackGlyphCount = (unsigned) count;
1658 count = read_coverage_list (otf, stream, offset,
1659 &chain_context3->Input, -1);
1662 chain_context3->InputGlyphCount = (unsigned) count;
1663 *coverage = chain_context3->Input[0];
1664 count = read_coverage_list (otf, stream, offset,
1665 &chain_context3->LookAhead, -1);
1666 chain_context3->LookaheadGlyphCount = (unsigned) count;
1667 chain_context3->LookupCount
1668 = read_lookup_record_list (otf, stream,
1669 &chain_context3->LookupRecord, -1);
1674 read_gsub_gpos_table (OTF *otf, OTF_TableInfo *table, int gsubp,
1675 enum OTF_ReaderFlag flag)
1677 OTF_Stream *stream = table->stream;
1678 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1679 void *errret = NULL;
1680 OTF_GSUB_GPOS *gsub_gpos = *table->address;
1683 SEEK_STREAM (stream, 10);
1686 SEEK_STREAM (stream, 0);
1687 OTF_CALLOC (gsub_gpos, 1, "");
1688 READ_FIXED (stream, gsub_gpos->Version);
1689 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1690 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1691 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1694 if (! gsub_gpos->ScriptList.Script
1695 && read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1696 &gsub_gpos->ScriptList) < 0)
1698 if (flag != OTF_READ_SCRIPTS)
1700 if (! gsub_gpos->FeatureList.Feature
1701 && read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1702 &gsub_gpos->FeatureList) < 0)
1704 if (flag != OTF_READ_FEATURES)
1706 if (! gsub_gpos->LookupList.Lookup
1707 && read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1708 &gsub_gpos->LookupList, gsubp) < 0)
1713 if (! *table->address)
1714 *table->address = gsub_gpos;
1719 /* (1-9) "GSUB" table */
1722 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1724 char *errfmt = "Sequence%s";
1725 unsigned errret = 0;
1729 READ_UINT16 (stream, count);
1731 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1732 OTF_MALLOC (*seq, count, "");
1733 for (i = 0; i < count; i++)
1734 READ_OFFSET (stream, (*seq)[i].offset);
1735 for (i = 0; i < count; i++)
1737 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1738 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1739 &(*seq)[i].Substitute, 0, -1);
1740 if (! (*seq)[i].GlyphCount)
1747 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1748 OTF_Ligature **ligature)
1750 char *errfmt = "Ligature%s";
1755 READ_UINT16 (stream, count);
1758 OTF_MALLOC (*ligature, count, "");
1759 for (i = 0; i < count; i++)
1760 READ_OFFSET (stream, (*ligature)[i].offset);
1761 for (i = 0; i < count; i++)
1763 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1764 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1765 (*ligature)[i].CompCount
1766 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1767 if (! (*ligature)[i].CompCount)
1774 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1775 OTF_LigatureSet **ligset)
1777 char *errfmt = "LigatureSet%s";
1782 READ_UINT16 (stream, count);
1785 OTF_MALLOC (*ligset, count, "");
1786 for (i = 0; i < count; i++)
1787 READ_OFFSET (stream, (*ligset)[i].offset);
1788 for (i = 0; i < count; i++)
1792 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1793 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1794 &(*ligset)[i].Ligature);
1797 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1803 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1804 OTF_AlternateSet **altset)
1806 char *errfmt = "AlternateSet%s";
1811 READ_UINT16 (stream, count);
1813 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1814 OTF_MALLOC (*altset, count, "");
1815 for (i = 0; i < count; i++)
1816 READ_OFFSET (stream, (*altset)[i].offset);
1817 for (i = 0; i < count; i++)
1821 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1822 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1825 (*altset)[i].GlyphCount = (unsigned) alt_count;
1831 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1832 OTF_Coverage *coverage,
1833 OTF_GSUB_ReverseChain1 *reverse_chain)
1837 if (read_coverage (otf, stream, offset, coverage) < 0)
1839 count = read_coverage_list (otf, stream, offset,
1840 &reverse_chain->Backtrack, -1);
1843 reverse_chain->BacktrackGlyphCount = (unsigned) count;
1844 count = read_coverage_list (otf, stream, offset,
1845 &reverse_chain->LookAhead, -1);
1848 reverse_chain->LookaheadGlyphCount = (unsigned) count;
1849 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
1852 reverse_chain->GlyphCount = count;
1857 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1858 unsigned type, OTF_LookupSubTableGSUB *subtable)
1863 SEEK_STREAM (stream, offset);
1864 READ_USHORT (stream, subtable->Format);
1865 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1869 if (subtable->Format == 1)
1871 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1873 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1875 else if (subtable->Format == 2)
1877 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1879 subtable->u.single2.GlyphCount
1880 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1882 if (! subtable->u.single2.GlyphCount)
1886 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1890 if (subtable->Format == 1)
1892 read_coverage (otf, stream, offset, &subtable->Coverage);
1893 subtable->u.multiple1.SequenceCount
1894 = read_sequence (otf, stream, offset,
1895 &subtable->u.multiple1.Sequence);
1898 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1902 if (subtable->Format == 1)
1904 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1906 subtable->u.alternate1.AlternateSetCount
1907 = read_alternate_set_list (otf, stream, offset,
1908 &subtable->u.alternate1.AlternateSet);
1909 if (! subtable->u.alternate1.AlternateSetCount)
1913 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1917 if (subtable->Format == 1)
1919 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1921 subtable->u.ligature1.LigSetCount
1922 = read_ligature_set_list (otf, stream, offset,
1923 &subtable->u.ligature1.LigatureSet);
1924 if (! subtable->u.ligature1.LigSetCount)
1928 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1932 if (subtable->Format == 1)
1934 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1935 &subtable->u.context1) < 0)
1938 else if (subtable->Format == 2)
1940 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1941 &subtable->u.context2) < 0)
1944 else if (subtable->Format == 3)
1946 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1947 &subtable->u.context3) < 0)
1951 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1955 if (subtable->Format == 1)
1957 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1958 &subtable->u.chain_context1) < 0)
1961 else if (subtable->Format == 2)
1963 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1964 &subtable->u.chain_context2) < 0)
1967 else if (subtable->Format == 3)
1969 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1970 &subtable->u.chain_context3) < 0)
1974 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1978 if (subtable->Format == 1)
1982 OTF_LookupSubTableGSUB *ex_subtable;
1984 READ_USHORT (stream, ex_type);
1985 READ_ULONG (stream, ex_offset);
1986 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
1987 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
1988 ex_type, ex_subtable) < 0)
1990 subtable->u.extension1.ExtensionLookupType = ex_type;
1991 subtable->u.extension1.ExtensionOffset = ex_offset;
1992 subtable->u.extension1.ExtensionSubtable = ex_subtable;
1995 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1999 if (subtable->Format == 1)
2001 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
2002 &subtable->u.reverse_chain1) < 0)
2006 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2010 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2016 read_gsub_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2018 return read_gsub_gpos_table (otf, table, 1, flag);
2022 /* (1-10) "GPOS" table */
2025 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
2026 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
2029 OTF_StreamState state;
2032 memset (value_record, 0, sizeof (OTF_ValueRecord));
2035 for (i = 0, size = 0; i < 8; i++)
2039 if (bit & OTF_XPlacement)
2040 READ_INT16 (stream, value_record->XPlacement);
2041 if (bit & OTF_YPlacement)
2042 READ_INT16 (stream, value_record->YPlacement);
2043 if (bit & OTF_XAdvance)
2044 READ_INT16 (stream, value_record->XAdvance);
2045 if (bit & OTF_YAdvance)
2046 READ_INT16 (stream, value_record->YAdvance);
2047 if (bit & OTF_XPlaDevice)
2048 READ_OFFSET (stream, value_record->XPlaDevice.offset);
2049 if (bit & OTF_YPlaDevice)
2050 READ_OFFSET (stream, value_record->YPlaDevice.offset);
2051 if (bit & OTF_XAdvDevice)
2052 READ_OFFSET (stream, value_record->XAdvDevice.offset);
2053 if (bit & OTF_YAdvDevice)
2054 READ_OFFSET (stream, value_record->YAdvDevice.offset);
2055 SAVE_STREAM (stream, state);
2056 if (value_record->XPlaDevice.offset)
2058 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
2061 if (value_record->YPlaDevice.offset)
2063 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
2066 if (value_record->XAdvDevice.offset)
2068 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
2071 if (value_record->YAdvDevice.offset)
2073 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
2076 RESTORE_STREAM (stream, state);
2082 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
2084 char *errfmt = "Anchor%s";
2087 SEEK_STREAM (stream, offset + anchor->offset);
2088 READ_UINT16 (stream, anchor->AnchorFormat);
2089 READ_INT16 (stream, anchor->XCoordinate);
2090 READ_INT16 (stream, anchor->YCoordinate);
2091 if (anchor->AnchorFormat == 1)
2093 else if (anchor->AnchorFormat == 2)
2095 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
2097 else if (anchor->AnchorFormat == 3)
2099 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
2100 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
2101 if (anchor->f.f2.XDeviceTable.offset)
2103 if (read_device_table (otf, stream, offset + anchor->offset,
2104 &anchor->f.f2.XDeviceTable) < 0)
2107 if (anchor->f.f2.YDeviceTable.offset)
2109 if (read_device_table (otf, stream, offset + anchor->offset,
2110 &anchor->f.f2.YDeviceTable) < 0)
2115 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
2121 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
2122 OTF_MarkArray *array)
2124 char *errfmt = "MarkArray%s";
2126 OTF_StreamState state;
2129 READ_OFFSET (stream, array->offset);
2130 SAVE_STREAM (stream, state);
2131 SEEK_STREAM (stream, offset + array->offset);
2132 READ_UINT16 (stream, array->MarkCount);
2133 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
2134 for (i = 0; i < array->MarkCount; i++)
2136 READ_UINT16 (stream, array->MarkRecord[i].Class);
2137 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
2139 for (i = 0; i < array->MarkCount; i++)
2140 if (read_anchor (otf, stream, offset + array->offset,
2141 &array->MarkRecord[i].MarkAnchor) < 0)
2143 RESTORE_STREAM (stream, state);
2148 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2149 unsigned ClassCount, OTF_AnchorArray *array)
2151 char *errfmt = "AnchorArray%s";
2153 OTF_StreamState state;
2156 READ_OFFSET (stream, array->offset);
2157 SAVE_STREAM (stream, state);
2158 SEEK_STREAM (stream, offset + array->offset);
2159 READ_UINT16 (stream, array->Count);
2160 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2161 for (i = 0; i < array->Count; i++)
2163 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2165 for (j = 0; j < ClassCount; j++)
2166 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2168 for (i = 0; i < array->Count; i++)
2169 for (j = 0; j < ClassCount; j++)
2170 if (read_anchor (otf, stream, offset + array->offset,
2171 &array->AnchorRecord[i].Anchor[j]) < 0)
2173 RESTORE_STREAM (stream, state);
2177 static OTF_PairSet *
2178 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2179 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2181 char *errfmt = "PairSet%s";
2182 void *errret = NULL;
2183 OTF_StreamState state;
2187 OTF_MALLOC (set, num, "");
2188 for (i = 0; i < num; i++)
2189 READ_OFFSET (stream, set[i].offset);
2190 SAVE_STREAM (stream, state);
2191 for (i = 0; i < num; i++)
2193 SEEK_STREAM (stream, offset + set[i].offset);
2194 READ_UINT16 (stream, set[i].PairValueCount);
2195 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2196 for (j = 0; j < set[i].PairValueCount; j++)
2198 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2200 READ_UINT16 (stream, rec->SecondGlyph);
2201 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2202 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2205 RESTORE_STREAM (stream, state);
2209 static OTF_Class1Record *
2210 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2211 unsigned num1, enum OTF_ValueFormat bit1,
2212 unsigned num2, enum OTF_ValueFormat bit2)
2214 char *errfmt = "Class1Record%s";
2215 void *errret = NULL;
2216 OTF_Class1Record *rec;
2219 OTF_MALLOC (rec, num1, "");
2220 for (i = 0; i < num1; i++)
2222 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2223 for (j = 0; j < num2; j++)
2225 if (read_value_record (otf, stream, offset,
2226 bit1, &rec[i].Class2Record[j].Value1) < 0
2227 || read_value_record (otf, stream, offset,
2228 bit2, &rec[i].Class2Record[j].Value2) < 0)
2236 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2237 OTF_EntryExitRecord **rec)
2239 char *errfmt = "EntryExitSet%s";
2243 OTF_StreamState state;
2245 READ_UINT16 (stream, count);
2247 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2248 OTF_MALLOC (*rec, count, "");
2249 for (i = 0; i < count; i++)
2251 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2252 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2254 SAVE_STREAM (stream, state);
2255 for (i = 0; i < count; i++)
2257 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2259 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2262 RESTORE_STREAM (stream, state);
2267 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2268 unsigned ClassCount, OTF_LigatureAttach *attach)
2270 char *errfmt = "LigatureAttach%s";
2274 SEEK_STREAM (stream, offset + attach->offset);
2275 READ_UINT16 (stream, attach->ComponentCount);
2276 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2277 for (i = 0; i < attach->ComponentCount; i++)
2279 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2280 " (ComponentRecord)");
2281 for (j = 0; j < ClassCount; j++)
2282 READ_OFFSET (stream,
2283 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2285 for (i = 0; i < attach->ComponentCount; i++)
2286 for (j = 0; j < ClassCount; j++)
2288 if (attach->ComponentRecord[i].LigatureAnchor[j].offset)
2290 if (read_anchor (otf, stream, offset + attach->offset,
2291 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2295 attach->ComponentRecord[i].LigatureAnchor[j].AnchorFormat = 0;
2301 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2302 unsigned class_count, OTF_LigatureArray *array)
2304 char *errfmt = "LigatureArray%s";
2306 OTF_StreamState state;
2309 READ_OFFSET (stream, array->offset);
2310 SAVE_STREAM (stream, state);
2311 SEEK_STREAM (stream, offset + array->offset);
2312 READ_UINT16 (stream, array->LigatureCount);
2313 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2314 for (i = 0; i < array->LigatureCount; i++)
2315 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2316 for (i = 0; i < array->LigatureCount; i++)
2317 read_ligature_attach (otf, stream, offset + array->offset,
2318 class_count, array->LigatureAttach + i);
2319 RESTORE_STREAM (stream, state);
2324 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2325 long offset, unsigned type,
2326 OTF_LookupSubTableGPOS *subtable)
2331 SEEK_STREAM (stream, offset);
2332 READ_UINT16 (stream, subtable->Format);
2333 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2337 if (subtable->Format == 1)
2339 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2341 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2342 read_value_record (otf, stream, offset,
2343 subtable->u.single1.ValueFormat,
2344 &subtable->u.single1.Value);
2346 else if (subtable->Format == 2)
2348 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2351 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2353 READ_UINT16 (stream, single2->ValueFormat);
2354 READ_UINT16 (stream, single2->ValueCount);
2355 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2356 for (i = 0; i < single2->ValueCount; i++)
2357 read_value_record (otf, stream, offset, single2->ValueFormat,
2358 single2->Value + i);
2361 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2365 if (subtable->Format == 1)
2367 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2369 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2370 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2371 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2372 subtable->u.pair1.PairSet
2373 = read_pair_set_list (otf, stream, offset,
2374 subtable->u.pair1.PairSetCount,
2375 subtable->u.pair1.ValueFormat1,
2376 subtable->u.pair1.ValueFormat2);
2377 if (! subtable->u.pair1.PairSet)
2380 else if (subtable->Format == 2)
2382 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2384 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2385 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2386 if (read_class_def (otf, stream, offset,
2387 &subtable->u.pair2.ClassDef1) < 0
2388 || read_class_def (otf, stream, offset,
2389 &subtable->u.pair2.ClassDef2) < 0)
2391 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2392 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2393 subtable->u.pair2.Class1Record
2394 = read_class1_record_list (otf, stream, offset,
2395 subtable->u.pair2.Class1Count,
2396 subtable->u.pair2.ValueFormat1,
2397 subtable->u.pair2.Class2Count,
2398 subtable->u.pair2.ValueFormat2);
2399 if (! subtable->u.pair2.Class1Record)
2403 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2407 if (subtable->Format == 1)
2409 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2411 subtable->u.cursive1.EntryExitCount
2412 = read_entry_exit_list (otf, stream, offset,
2413 &subtable->u.cursive1.EntryExitRecord);
2414 if (! subtable->u.cursive1.EntryExitCount)
2418 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2422 if (subtable->Format == 1)
2424 read_coverage (otf, stream, offset, &subtable->Coverage);
2425 read_coverage (otf, stream, offset,
2426 &subtable->u.mark_base1.BaseCoverage);
2427 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2428 read_mark_array (otf, stream, offset,
2429 &subtable->u.mark_base1.MarkArray);
2430 read_anchor_array (otf, stream, offset,
2431 subtable->u.mark_base1.ClassCount,
2432 &subtable->u.mark_base1.BaseArray);
2435 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2439 if (subtable->Format == 1)
2441 read_coverage (otf, stream, offset, &subtable->Coverage);
2442 read_coverage (otf, stream, offset,
2443 &subtable->u.mark_lig1.LigatureCoverage);
2444 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2445 read_mark_array (otf, stream, offset,
2446 &subtable->u.mark_lig1.MarkArray);
2447 read_ligature_array (otf, stream, offset,
2448 subtable->u.mark_lig1.ClassCount,
2449 &subtable->u.mark_lig1.LigatureArray);
2454 if (subtable->Format == 1)
2456 read_coverage (otf, stream, offset, &subtable->Coverage);
2457 read_coverage (otf, stream, offset,
2458 &subtable->u.mark_mark1.Mark2Coverage);
2459 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2460 read_mark_array (otf, stream, offset,
2461 &subtable->u.mark_mark1.Mark1Array);
2462 read_anchor_array (otf, stream, offset,
2463 subtable->u.mark_mark1.ClassCount,
2464 &subtable->u.mark_mark1.Mark2Array);
2467 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2471 if (subtable->Format == 1)
2473 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2474 &subtable->u.context1) < 0)
2477 else if (subtable->Format == 2)
2479 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2480 &subtable->u.context2) < 0)
2483 else if (subtable->Format == 3)
2485 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2486 &subtable->u.context3) < 0)
2490 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2494 if (subtable->Format == 1)
2496 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2497 &subtable->u.chain_context1) < 0)
2500 else if (subtable->Format == 2)
2502 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2503 &subtable->u.chain_context2) < 0)
2506 else if (subtable->Format == 3)
2508 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2509 &subtable->u.chain_context3) < 0)
2513 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2517 if (subtable->Format == 1)
2521 OTF_LookupSubTableGPOS *ex_subtable;
2523 READ_USHORT (stream, ex_type);
2524 READ_ULONG (stream, ex_offset);
2525 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2526 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2527 ex_type, ex_subtable) < 0)
2529 subtable->u.extension1.ExtensionLookupType = ex_type;
2530 subtable->u.extension1.ExtensionOffset = ex_offset;
2531 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2534 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2538 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2544 read_gpos_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2546 return read_gsub_gpos_table (otf, table, 0, flag);
2554 read_base_table (OTF_Stream *stream, long offset)
2558 OTF_MALLOC (base, 1);
2567 read_jstf_table (OTF_Stream *stream, long offset)
2571 OTF_MALLOC (jstf, 1);
2577 /*** (1-11) Structure for OTF */
2580 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2584 READ_FIXED (stream, table->sfnt_version);
2585 READ_USHORT (stream, table->numTables);
2586 READ_USHORT (stream, table->searchRange);
2587 READ_USHORT (stream, table->enterSelector);
2588 READ_USHORT (stream, table->rangeShift);
2593 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2598 READ_TAG (stream, tag);
2600 table->name[0] = tag >> 24;
2601 table->name[1] = (tag >> 16) & 0xFF;
2602 table->name[0] = (tag >> 8) & 0xFF;
2603 table->name[0] = tag >> 8;
2604 table->name[0] = '\0';
2605 READ_ULONG (stream, table->checkSum);
2606 READ_ULONG (stream, table->offset);
2607 READ_ULONG (stream, table->length);
2612 read_header_part (OTF *otf, FILE *fp)
2614 char *errfmt = "otf header%s";
2616 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2619 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2621 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2622 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2623 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2624 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2625 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2626 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2627 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2628 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2629 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2630 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2631 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2632 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2634 head_tag = OTF_tag ("head");
2635 name_tag = OTF_tag ("name");
2636 cmap_tag = OTF_tag ("cmap");
2637 gdef_tag = OTF_tag ("GDEF");
2638 gsub_tag = OTF_tag ("GSUB");
2639 gpos_tag = OTF_tag ("GPOS");
2641 stream = make_stream ();
2645 internal_data->header_stream = stream;
2647 /* Size of Offset Table is 12 bytes. */
2648 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2650 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2653 /* Size of each Table Directory is 16 bytes. */
2654 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2655 "Table Directory") < 0)
2658 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2659 for (i = 0; i < otf->offset_table.numTables; i++)
2661 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2662 OTF_TableInfo *table_info = NULL;
2666 if (tag == head_tag)
2667 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2668 else if (tag == name_tag)
2669 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2670 else if (tag == cmap_tag)
2671 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2672 else if (tag == gdef_tag)
2673 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2674 else if (tag == gsub_tag)
2675 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2676 else if (tag == gpos_tag)
2677 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2681 table_info->stream = make_stream ();
2682 if (setup_stream (table_info->stream, fp,
2683 otf->table_dirs[i].offset,
2684 otf->table_dirs[i].length,
2685 otf->table_dirs[i].name) < 0)
2690 internal_data->header_stream = NULL;
2691 free_stream (stream);
2695 static OTF_TableInfo *
2696 get_table_info (OTF *otf, char *name)
2698 char *errfmt = "OTF Table Read%s";
2699 OTF_TableInfo *errret = NULL;
2700 OTF_InternalData *internal_data = otf->internal_data;
2701 OTF_TableInfo *table_info;
2702 OTF_Tag tag = OTF_tag (name);
2705 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2707 if (tag == OTF_tag ("head"))
2708 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2709 else if (tag == OTF_tag ("name"))
2710 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2711 else if (tag == OTF_tag ("cmap"))
2712 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2713 else if (tag == OTF_tag ("GDEF"))
2714 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2715 else if (tag == OTF_tag ("GSUB"))
2716 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2717 else if (tag == OTF_tag ("GPOS"))
2718 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2720 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2722 if (*table_info->address)
2725 if (! table_info->stream)
2726 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2727 if (! table_info->reader)
2728 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2734 /*** (2) API for reading OTF */
2736 /*** (2-1) OTF_open() */
2738 /* Note: We can't use memory allocation macros in the following
2739 functions because those macros return from the functions before
2740 freeing memory previously allocated. */
2743 OTF_open (char *otf_name)
2746 char *errfmt = "opening otf (%s)";
2747 void *errret = NULL;
2749 OTF_InternalData *internal_data;
2750 int len = strlen (otf_name);
2751 const char *ext = otf_name + (len - 4);
2755 || (ext[1] != 'O' && ext[1] != 'T' && ext[1] != 'o' && ext[1] != 't')
2756 || (ext[2] != 'T' && ext[2] != 't')
2757 || (ext[3] != 'F' && ext[3] != 'f'))
2758 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2759 fp = fopen (otf_name, "r");
2761 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2762 otf = calloc (1, sizeof (OTF));
2764 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2765 otf->filename = strdup (otf_name);
2766 if (! otf->filename)
2770 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2773 internal_data = calloc (1, sizeof (OTF_InternalData));
2774 if (! internal_data)
2775 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2776 otf->internal_data = internal_data;
2777 if (! allocate_memory_record (otf))
2778 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2780 /* Here after, all pointers to allocated memory are recorded in
2781 otf->internal_data->memory_record except for what allocated by
2782 the functions allocate_memory_record and make_stream. */
2784 if (read_header_part (otf, fp) < 0)
2795 /*** (2-2) OTF_close() */
2798 OTF_close (OTF *otf)
2800 OTF_InternalData *internal_data = otf->internal_data;
2805 OTF_MemoryRecord *memrec = internal_data->memory_record;
2807 if (internal_data->header_stream)
2808 free_stream (internal_data->header_stream);
2810 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2811 if (internal_data->table_info[i].stream)
2812 free_stream (internal_data->table_info[i].stream);
2816 OTF_MemoryRecord *next = memrec->next;
2818 for (i = memrec->used - 1; i >= 0; i--)
2819 free (memrec->memory[i]);
2823 free (internal_data);
2826 free (otf->filename);
2830 /*** (2-3) OTF_get_table() */
2833 OTF_get_table (OTF *otf, char *name)
2835 OTF_TableInfo *table_info = get_table_info (otf, name);
2840 if (! table_info->stream)
2841 /* Already fully loaded. */
2844 address = (*table_info->reader) (otf, table_info, OTF_READ_FULL);
2845 free_stream (table_info->stream);
2846 table_info->stream = NULL;
2849 table_info->reader = NULL;
2855 /*** (2-4) OTF_check_table() */
2858 OTF_check_table (OTF *otf, char *name)
2860 return (get_table_info (otf, name) ? 0 : -1);
2863 /*** (2-5) OTF_get_scripts() */
2866 OTF_get_scripts (OTF *otf, int gsubp)
2868 OTF_TableInfo *table_info
2869 = (otf->internal_data->table_info
2870 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
2873 if (! table_info->reader)
2875 if (! table_info->stream)
2876 /* Already fully loaded. */
2879 address = (*table_info->reader) (otf, table_info, OTF_READ_SCRIPTS);
2882 table_info->reader = NULL;
2888 /*** (2-6) OTF_get_features() */
2891 OTF_get_features (OTF *otf, int gsubp)
2893 OTF_TableInfo *table_info
2894 = (otf->internal_data->table_info
2895 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
2898 if (! table_info->reader)
2900 if (! table_info->stream)
2902 if (*table_info->address)
2903 /* Already fully loaded. */
2908 address = (*table_info->reader) (otf, table_info, OTF_READ_FEATURES);
2911 table_info->reader = NULL;
2917 /*** (2-7) OTF_check_features */
2920 OTF_check_features (OTF *otf, int gsubp,
2921 OTF_Tag script, OTF_Tag language, OTF_Tag *features,
2924 OTF_ScriptList *script_list;
2925 OTF_Script *Script = NULL;
2926 OTF_LangSys *LangSys = NULL;
2927 OTF_FeatureList *feature_list;
2930 if (OTF_get_features (otf, gsubp) < 0)
2934 script_list = &otf->gsub->ScriptList;
2935 feature_list = &otf->gsub->FeatureList;
2939 script_list = &otf->gpos->ScriptList;
2940 feature_list = &otf->gpos->FeatureList;
2942 for (i = 0; i < script_list->ScriptCount && ! Script; i++)
2943 if (script_list->Script[i].ScriptTag == script)
2944 Script = script_list->Script + i;
2949 for (i = 0; i < Script->LangSysCount && ! LangSys; i++)
2950 if (Script->LangSysRecord[i].LangSysTag == language)
2951 LangSys = Script->LangSys + i;
2954 LangSys = &Script->DefaultLangSys;
2955 for (j = 0; j < n_features; j++)
2957 OTF_Tag feature = features[j];
2959 for (i = 0; i < LangSys->FeatureCount; i++)
2960 if (feature_list->Feature[LangSys->FeatureIndex[i]].FeatureTag
2963 if (i == LangSys->FeatureCount)
2970 /*** (5) API miscellaneous ***/
2973 OTF_tag (char *name)
2975 unsigned char *p = (unsigned char *) name;
2979 return (OTF_Tag) ((p[0] << 24)
2983 : (p[2] << 8) | p[3]))));
2987 OTF_tag_name (OTF_Tag tag, char *name)
2989 name[0] = (char) (tag >> 24);
2990 name[1] = (char) ((tag >> 16) & 0xFF);
2991 name[2] = (char) ((tag >> 8) & 0xFF);
2992 name[3] = (char) (tag & 0xFF);