1 /* otfopen.c -- OpenType font reader.
3 Copyright (C) 2003, 2004, 2005, 2006
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. */
32 #include FT_TRUETYPE_TABLES_H
35 Table of contents (almost parallel to otf.h):
39 (1) Readers for OTF Layout tables and OTF itself
40 (1-1) Basic types and functions
44 (1-5) Structures common to GDEF, GSUB, and GPOS
46 (1-7) Structures for ScriptList, FeatureList, and LookupList
47 (1-8) Structures common to GSUB and GPOS
50 (1-11) Structure for OTF
52 (2) API for reading OTF
56 (2-4) OTF_check_table()
63 Example of typical usage of OTF_Stream.
67 OTF_StreamState state;
70 OPEN_STREAM (_FILE_NAME_, stream);
73 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
74 offset = READ_OFFSET (stream);
75 nbytes = READ_ULONG (stream);
76 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
78 CLOSE_STREAM (stream);
92 typedef long OTF_StreamState;
95 make_stream (const char *name)
98 char *errfmt = "stream creation%s";
101 stream = calloc (1, sizeof (OTF_Stream));
103 OTF_ERROR (OTF_ERROR_MEMORY, "");
109 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes)
111 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);
135 make_stream_from_ft_face (FT_Face face, const char *name)
137 char *errfmt = "FT_Face stream creation for %s";
142 FT_ULong tag = FT_MAKE_TAG (name[0], name[1], name[2], name[3]);
144 if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &nbytes))
146 buf = malloc (nbytes);
148 OTF_ERROR (OTF_ERROR_MEMORY, name);
149 if (FT_Load_Sfnt_Table (face, tag, 0, buf, &nbytes))
152 OTF_ERROR (OTF_ERROR_FT_FACE, name);
154 stream = make_stream (name);
159 stream->allocated = nbytes;
160 stream->bufsize = nbytes;
165 free_stream (OTF_Stream *stream)
171 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
172 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
173 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
175 #define STREAM_CHECK_SIZE(stream, size) \
176 if ((stream)->pos + (size) > (stream)->bufsize) \
178 char *errfmt = "buffer overrun in %s"; \
180 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
186 #define READ_USHORT(stream, var) \
188 STREAM_CHECK_SIZE ((stream), 2); \
189 (var) = (((stream)->buf[(stream)->pos] << 8) \
190 | (stream)->buf[(stream)->pos + 1]); \
191 (stream)->pos += 2; \
194 #define READ_SHORT(stream, var) \
196 STREAM_CHECK_SIZE ((stream), 2); \
197 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
198 | (stream)->buf[(stream)->pos + 1]); \
199 (stream)->pos += 2; \
202 #define READ_ULONG(stream, var) \
204 STREAM_CHECK_SIZE ((stream), 4); \
205 (var) = (((stream)->buf[(stream)->pos] << 24) \
206 | ((stream)->buf[(stream)->pos + 1] << 16) \
207 | ((stream)->buf[(stream)->pos + 2] << 8) \
208 | (stream)->buf[(stream)->pos + 3]); \
209 (stream)->pos += 4; \
212 #define READ_LONG(stream, var) \
214 STREAM_CHECK_SIZE ((stream), 4); \
215 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
216 | ((stream)->buf[(stream)->pos + 1] << 16) \
217 | ((stream)->buf[(stream)->pos + 2] << 8) \
218 | (stream)->buf[(stream)->pos + 3]); \
219 (stream)->pos += 4; \
223 #define READ_FIXED(stream, fixed) \
225 READ_USHORT ((stream), (fixed).high); \
226 READ_USHORT ((stream), (fixed).low); \
230 #define READ_BYTES(stream, p, nbytes) \
232 STREAM_CHECK_SIZE ((stream), (nbytes)); \
233 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
234 (stream)->pos += (nbytes); \
238 #define READ_TAG READ_ULONG
239 #define READ_OFFSET READ_USHORT
240 #define READ_UINT16 READ_USHORT
241 #define READ_INT16 READ_SHORT
242 #define READ_GLYPHID READ_USHORT
245 /*** (1) Structures for OTF Layout tables and OTF itself */
247 /*** (1-1) Basic types and functions */
260 #define OTF_MEMORY_RECORD_SIZE 1024
262 struct OTF_MemoryRecord
265 void *memory[OTF_MEMORY_RECORD_SIZE];
266 struct OTF_MemoryRecord *next;
269 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
279 struct _OTF_TableInfo;
280 typedef struct _OTF_TableInfo OTF_TableInfo;
282 struct _OTF_TableInfo
284 /* Points to one of OTF->head, OTF->name, etc. */
286 /* Function to read one of OTF tables. */
287 void *(*reader) (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag);
288 /* Stream given to <reader>. */
292 struct OTF_InternalData
294 /* Information about each OTF table. */
295 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
297 /* Stream used to read the header part of OTF. */
298 OTF_Stream *header_stream;
300 /* Records of allocated memories. */
301 OTF_MemoryRecord *memory_record;
305 static OTF_MemoryRecord *
306 allocate_memory_record (OTF *otf)
308 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
309 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
314 memrec->next = internal_data->memory_record;
315 internal_data->memory_record = memrec;
319 /* Memory allocation macros. */
321 #define OTF_MALLOC(p, size, arg) \
327 OTF_MemoryRecord *memrec \
328 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
329 (p) = malloc (sizeof (*(p)) * (size)); \
331 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
332 && ! (memrec = allocate_memory_record (otf)))) \
333 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
334 memrec->memory[memrec->used++] = (p); \
339 #define OTF_CALLOC(p, size, arg) \
345 OTF_MemoryRecord *memrec \
346 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
347 (p) = calloc ((size), sizeof (*(p))); \
349 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
350 && ! (memrec = allocate_memory_record (otf)))) \
351 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
352 memrec->memory[memrec->used++] = (p); \
357 /*** (1-2) "head" table */
360 read_head_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
362 OTF_Stream *stream = table->stream;
363 char *errfmt = "head%s";
367 OTF_CALLOC (head, 1, "");
368 READ_FIXED (stream, head->TableVersionNumber);
369 READ_FIXED (stream, head->fontRevision);
370 READ_ULONG (stream, head->checkSumAdjustment);
371 READ_ULONG (stream, head->magicNumber);
372 READ_USHORT (stream, head->flags);
373 READ_USHORT (stream, head->unitsPerEm);
375 *table->address = head;
380 /*** (1-3) "name" table */
383 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
385 char *errfmt = "nameID (%d)";
387 OTF_StreamState state;
392 if (rec->platformID == 0)
393 ucs = (rec->encodingID <= 3) ? 2 : 4;
394 else if (rec->platformID == 1 && rec->encodingID == 0)
396 else if (rec->platformID == 3)
397 ucs = (rec->encodingID == 1 ? 2
398 : rec->encodingID == 10 ? 4
401 OTF_MALLOC (rec->name, rec->length + 1, (void *) rec->nameID);
402 SAVE_STREAM (stream, state);
403 SEEK_STREAM (stream, stream->pos + rec->offset);
404 READ_BYTES (stream, rec->name, rec->length);
405 RESTORE_STREAM (stream, state);
406 rec->name[rec->length] = 0;
415 for (i = 0; i < rec->length / 2; i++)
417 if (rec->name[i * 2] > 0
418 || rec->name[i * 2 + 1] >= 128)
425 for (i = 0; i < rec->length / 2; i++)
426 rec->name[i] = rec->name[i * 2 + 1];
432 for (i = 0; i < rec->length / 4; i++)
434 if (rec->name[i * 4] > 0
435 || rec->name[i * 4 + 1] > 0
436 || rec->name[i * 4 + 2] > 0
437 || rec->name[i * 2 + 3] >= 128)
444 for (i = 0; i < rec->length / 4; i++)
445 rec->name[i] = rec->name[i * 4 + 3];
453 read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
455 OTF_Stream *stream = table->stream;
456 char *errfmt = "name%s";
461 OTF_CALLOC (name, 1, "");
462 READ_USHORT (stream, name->format);
463 READ_USHORT (stream, name->count);
464 READ_USHORT (stream, name->stringOffset);
465 OTF_MALLOC (name->nameRecord, name->count, "");
466 for (i = 0; i < name->count; i++)
468 OTF_NameRecord *rec = name->nameRecord + i;
470 READ_USHORT (stream, rec->platformID);
471 READ_USHORT (stream, rec->encodingID);
472 READ_USHORT (stream, rec->languageID);
473 READ_USHORT (stream, rec->nameID);
474 READ_USHORT (stream, rec->length);
475 READ_USHORT (stream, rec->offset);
477 for (i = 0; i < name->count; i++)
479 OTF_NameRecord *rec = name->nameRecord + i;
480 int nameID = rec->nameID;
482 read_name (otf, stream, rec);
484 if (nameID >= OTF_max_nameID)
486 if (! name->name[nameID]
488 name->name[nameID] = (char *) rec->name;
491 *table->address = name;
496 /*** (1-4) "cmap" table */
499 read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
501 OTF_Stream *stream = table->stream;
502 char *errfmt = "cmap%s";
505 int unicode_bmp_index = -1, unicode_full_index = -1;
508 OTF_CALLOC (cmap, 1, "");
509 READ_USHORT (stream, cmap->version);
510 READ_USHORT (stream, cmap->numTables);
511 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
512 for (i = 0; i < cmap->numTables; i++)
514 unsigned platformID, encodingID;
516 READ_USHORT (stream, platformID);
517 cmap->EncodingRecord[i].platformID = platformID;
518 READ_USHORT (stream, encodingID);
519 cmap->EncodingRecord[i].encodingID = encodingID;
520 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
524 unicode_bmp_index = i;
526 unicode_full_index = i;
528 else if (platformID == 3)
531 unicode_bmp_index = i;
532 else if (encodingID == 10)
533 unicode_full_index = i;
536 for (i = 0; i < cmap->numTables; i++)
540 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
541 READ_USHORT (stream, format);
542 cmap->EncodingRecord[i].subtable.format = format;
543 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
544 if (format == 8 || format == 10 || format == 12)
546 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
547 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
551 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
557 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
558 " (EncodingRecord)");
560 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
567 OTF_EncodingSubtable2 *sub2;
570 OTF_MALLOC (sub2, 1, " (EncodingSubtable2)");
571 cmap->EncodingRecord[i].subtable.f.f2 = sub2;
572 for (j = 0, max_key = 0; j < 256; j++)
574 READ_USHORT (stream, sub2->subHeaderKeys[j]);
575 if (max_key < sub2->subHeaderKeys[j])
576 max_key = sub2->subHeaderKeys[j];
579 sub2->subHeaderCount = max_key / 8;
580 OTF_MALLOC (sub2->subHeaders, max_key / 8, " (subHeaders)");
581 for (j = 0; j < sub2->subHeaderCount; j++)
583 READ_USHORT (stream, sub2->subHeaders[j].firstCode);
584 READ_USHORT (stream, sub2->subHeaders[j].entryCount);
585 READ_SHORT (stream, sub2->subHeaders[j].idDelta);
586 READ_USHORT (stream, sub2->subHeaders[j].idRangeOffset);
587 /* Make it offset from sub2->glyphIndexArray. */
588 sub2->subHeaders[j].idRangeOffset -= max_key - (j * 8 + 6);
590 sub2->glyphIndexCount = (cmap->EncodingRecord[i].subtable.length
592 OTF_MALLOC (sub2->glyphIndexArray, sub2->glyphIndexCount,
593 " (glyphIndexArray)");
594 READ_BYTES (stream, sub2->glyphIndexArray, sub2->glyphIndexCount);
600 OTF_EncodingSubtable4 *sub4;
605 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
606 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
607 READ_USHORT (stream, sub4->segCountX2);
608 segCount = sub4->segCountX2 / 2;
609 READ_USHORT (stream, sub4->searchRange);
610 READ_USHORT (stream, sub4->entrySelector);
611 READ_USHORT (stream, sub4->rangeShift);
612 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
613 for (j = 0; j < segCount; j++)
614 READ_USHORT (stream, sub4->segments[j].endCount);
615 READ_USHORT (stream, dummy);
616 for (j = 0; j < segCount; j++)
617 READ_USHORT (stream, sub4->segments[j].startCount);
618 for (j = 0; j < segCount; j++)
619 READ_SHORT (stream, sub4->segments[j].idDelta);
620 for (j = 0; j < segCount; j++)
623 unsigned rest = 2 * (segCount - j);
625 READ_USHORT (stream, off);
627 sub4->segments[j].idRangeOffset = 0xFFFF;
629 sub4->segments[j].idRangeOffset = (off - rest) / 2;
631 j = (cmap->EncodingRecord[i].subtable.length
632 - (14 + 2 * (segCount * 4 + 1)));
633 sub4->GlyphCount = j / 2;
634 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
635 for (j = 0; j < sub4->GlyphCount; j++)
636 READ_USHORT (stream, sub4->glyphIdArray[j]);
642 OTF_EncodingSubtable6 *sub6;
645 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
646 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
647 READ_USHORT (stream, sub6->firstCode);
648 READ_USHORT (stream, sub6->entryCount);
649 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
650 for (j = 0; j < sub6->entryCount; j++)
651 READ_USHORT (stream, sub6->glyphIdArray[j]);
657 OTF_EncodingSubtable8 *sub8;
660 OTF_MALLOC (sub8, 1, " (EncodingSubtable8)");
661 cmap->EncodingRecord[i].subtable.f.f8 = sub8;
662 for (j = 0; j < 8192; j++)
663 READ_BYTES (stream, sub8->is32, 8192);
664 READ_ULONG (stream, sub8->nGroups);
665 OTF_MALLOC (sub8->Groups, sub8->nGroups, " (Groups)");
666 for (j = 0; j < sub8->nGroups; j++)
668 READ_ULONG (stream, sub8->Groups[i].startCharCode);
669 READ_ULONG (stream, sub8->Groups[i].endCharCode);
670 READ_ULONG (stream, sub8->Groups[i].startGlyphID);
677 OTF_EncodingSubtable10 *sub10;
680 OTF_MALLOC (sub10, 1, " (EncodingSubtable10)");
681 cmap->EncodingRecord[i].subtable.f.f10 = sub10;
682 READ_ULONG (stream, sub10->startCharCode);
683 READ_ULONG (stream, sub10->numChars);
684 OTF_MALLOC (sub10->glyphs, sub10->numChars, " (GlyphCount)");
685 for (j = 0; j < sub10->numChars; j++)
686 READ_USHORT (stream, sub10->glyphs[j]);
692 OTF_EncodingSubtable12 *sub12;
695 OTF_MALLOC (sub12, 1, " (EncodingSubtable12)");
696 cmap->EncodingRecord[i].subtable.f.f12 = sub12;
697 READ_ULONG (stream, sub12->nGroups);
698 OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
699 for (j = 0; j < sub12->nGroups; j++)
701 READ_ULONG (stream, sub12->Groups[j].startCharCode);
702 READ_ULONG (stream, sub12->Groups[j].endCharCode);
703 READ_ULONG (stream, sub12->Groups[j].startGlyphID);
709 OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
713 if (unicode_bmp_index >= 0)
715 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_bmp_index;
716 OTF_GlyphID glyph_id, max_glyph_id = 0;
718 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
719 switch (rec->subtable.format)
723 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
724 int segCount = sub4->segCountX2 / 2;
726 for (i = 0; i < segCount; i++)
728 OTF_cmapSegument *seg = sub4->segments + i;
731 if (seg->idRangeOffset == 0xFFFF)
732 for (c = seg->startCount; c <= seg->endCount; c++)
734 glyph_id = c + seg->idDelta;
735 cmap->unicode_table[c] = glyph_id;
736 if (glyph_id > max_glyph_id)
737 max_glyph_id = glyph_id;
740 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
743 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
744 + (c - seg->startCount)];
745 cmap->unicode_table[c] = glyph_id;
746 if (glyph_id > max_glyph_id)
747 max_glyph_id = glyph_id;
753 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
754 for (i = 0; i < 0x10000; i++)
755 if (cmap->unicode_table[i])
756 cmap->decode_table[cmap->unicode_table[i]] = i;
757 cmap->max_glyph_id = max_glyph_id;
760 *table->address = cmap;
765 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
767 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
768 Glyph-IDs there. If COUNT is negative, read the number of
769 Glyphs-IDs at first. MINUS if nozero is how few the actual
770 Glyph-IDs are in STREAM than COUNT. */
773 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
774 int minus, int count)
776 char *errfmt = "GlyphID List%s";
781 READ_UINT16 (stream, count);
784 OTF_MALLOC (*ids, count, "");
785 for (i = 0; i < count + minus; i++)
786 READ_GLYPHID (stream, (*ids)[i]);
791 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
793 char *errfmt = "RangeRecord%s";
798 READ_UINT16 (stream, count);
801 OTF_MALLOC (*record, count, "");
802 for (i = 0; i < count; i++)
804 READ_GLYPHID (stream, (*record)[i].Start);
805 READ_GLYPHID (stream, (*record)[i].End);
806 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
813 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
814 OTF_Coverage *coverage)
816 char *errfmt = "Coverage%s";
818 OTF_StreamState state;
821 READ_OFFSET (stream, coverage->offset);
822 SAVE_STREAM (stream, state);
823 SEEK_STREAM (stream, offset + coverage->offset);
824 READ_UINT16 (stream, coverage->CoverageFormat);
825 if (coverage->CoverageFormat == 1)
826 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
827 else if (coverage->CoverageFormat == 2)
828 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
830 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
833 coverage->Count = (unsigned) count;
834 RESTORE_STREAM (stream, state);
838 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
839 and store the Coverages there. If COUNT is negative, read the
840 number of Coverages at first. */
843 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
844 OTF_Coverage **coverage, int count)
846 char *errfmt = "Coverage List%s";
851 READ_UINT16 (stream, count);
854 OTF_MALLOC (*coverage, count, "");
855 for (i = 0; i < count; i++)
856 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
863 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
866 char *errfmt = "ClassDef%s";
869 SEEK_STREAM (stream, class->offset);
870 READ_UINT16 (stream, class->ClassFormat);
871 if (class->ClassFormat == 1)
873 READ_GLYPHID (stream, class->f.f1.StartGlyph);
874 class->f.f1.GlyphCount
876 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
877 if (! class->f.f1.GlyphCount)
878 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
880 else if (class->ClassFormat == 2)
882 class->f.f2.ClassRangeCount
883 = (read_range_records
884 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
885 if (! class->f.f2.ClassRangeCount)
886 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
889 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
895 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
897 char *errfmt = "ClassDef%s";
899 OTF_StreamState state;
901 READ_OFFSET (stream, class->offset);
904 SAVE_STREAM (stream, state);
905 SEEK_STREAM (stream, offset + class->offset);
906 READ_UINT16 (stream, class->ClassFormat);
907 if (class->ClassFormat == 1)
909 READ_GLYPHID (stream, class->f.f1.StartGlyph);
910 class->f.f1.GlyphCount
911 = read_glyph_ids (otf, stream,
912 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
914 if (! class->f.f1.GlyphCount)
917 else if (class->ClassFormat == 2)
919 class->f.f2.ClassRangeCount
920 = read_range_records (otf, stream,
922 &class->f.f2.ClassRangeRecord);
923 if (! class->f.f2.ClassRangeCount)
927 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
929 RESTORE_STREAM (stream, state);
935 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
936 OTF_DeviceTable *table)
938 char *errfmt = "Device Table%s";
949 SEEK_STREAM (stream, offset + table->offset);
950 READ_UINT16 (stream, table->StartSize);
951 READ_UINT16 (stream, table->EndSize);
952 READ_UINT16 (stream, table->DeltaFormat);
953 num = table->EndSize - table->StartSize + 1;
954 if (num > 0 && table->DeltaFormat >= 1 && table->DeltaFormat <= 3)
956 OTF_MALLOC (table->DeltaValue, num, "");
958 if (table->DeltaFormat == 1)
959 for (i = 0; i < num; i++)
962 READ_UINT16 (stream, val);
963 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
964 table->DeltaValue[i] = intval.int2;
966 else if (table->DeltaFormat == 2)
967 for (i = 0; i < num; i++)
970 READ_UINT16 (stream, val);
971 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
972 table->DeltaValue[i] = intval.int4;
974 else /* (table->DeltaFormat == 3) */
975 for (i = 0; i < num; i++)
979 READ_UINT16 (stream, val);
980 intval.int8 = val >> 8;
981 table->DeltaValue[i] = intval.int8;
985 intval.int8 = val >> 8;
986 table->DeltaValue[i] = intval.int8;
992 /* Invalid DeltaFormat but several fonts has such values (bug of
993 fontforge?). So accept it with NULL delta values. */
994 table->DeltaValue = NULL;
1000 /*** (1-6) "GDEF" table */
1003 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1004 OTF_AttachList *list)
1006 char *errfmt = "AttachList%s";
1010 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1012 READ_UINT16 (stream, list->GlyphCount);
1013 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1014 for (i = 0; i < list->GlyphCount; i++)
1015 READ_OFFSET (stream, list->AttachPoint[i].offset);
1016 for (i = 0; i < list->GlyphCount; i++)
1020 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1021 READ_UINT16 (stream, count);
1022 list->AttachPoint[i].PointCount = count;
1023 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1024 for (j = 0; j < count; j++)
1025 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1031 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1032 OTF_CaretValue *caret)
1034 char *errfmt = "CaretValue%s";
1037 SEEK_STREAM (stream, offset + caret->offset);
1038 READ_UINT16 (stream, caret->CaretValueFormat);
1039 if (caret->CaretValueFormat == 1)
1040 READ_INT16 (stream, caret->f.f1.Coordinate);
1041 else if (caret->CaretValueFormat == 2)
1042 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1043 else if (caret->CaretValueFormat == 3)
1045 READ_INT16 (stream, caret->f.f3.Coordinate);
1046 if (read_device_table (otf, stream, offset + caret->offset,
1047 &caret->f.f3.DeviceTable) < 0)
1051 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1056 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1057 OTF_LigCaretList *list)
1059 char *errfmt = "LigCaretList%s";
1063 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1065 READ_UINT16 (stream, list->LigGlyphCount);
1066 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1067 for (i = 0; i < list->LigGlyphCount; i++)
1068 READ_OFFSET (stream, list->LigGlyph[i].offset);
1069 for (i = 0; i < list->LigGlyphCount; i++)
1073 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1074 READ_UINT16 (stream, count);
1075 list->LigGlyph[i].CaretCount = count;
1076 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1077 for (j = 0; j < count; j++)
1078 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1079 for (j = 0; j < count; j++)
1080 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1081 &list->LigGlyph[i].CaretValue[j]) < 0)
1088 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1092 READ_FIXED (stream, header->Version);
1093 READ_OFFSET (stream, header->GlyphClassDef);
1094 READ_OFFSET (stream, header->AttachList);
1095 READ_OFFSET (stream, header->LigCaretList);
1096 READ_OFFSET (stream, header->MarkAttachClassDef);
1101 read_gdef_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
1103 OTF_Stream *stream = table->stream;
1104 char *errfmt = "GDEF%s";
1105 void *errret = NULL;
1108 OTF_CALLOC (gdef, 1, "");
1109 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1110 if (gdef->header.GlyphClassDef)
1112 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1113 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1115 if (gdef->header.AttachList)
1116 read_attach_list (otf, stream, gdef->header.AttachList,
1117 &gdef->attach_list);
1118 if (gdef->header.LigCaretList)
1119 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1120 &gdef->lig_caret_list);
1121 if (gdef->header.MarkAttachClassDef)
1123 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1124 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1127 *table->address = gdef;
1132 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
1135 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
1136 OTF_ScriptList *list)
1138 char *errfmt = "Script List%s";
1142 SEEK_STREAM (stream, offset);
1143 READ_USHORT (stream, list->ScriptCount);
1144 OTF_CALLOC (list->Script, list->ScriptCount, "");
1146 for (i = 0; i < list->ScriptCount; i++)
1148 READ_TAG (stream, list->Script[i].ScriptTag);
1149 READ_OFFSET (stream, list->Script[i].offset);
1151 for (i = 0; i < list->ScriptCount; i++)
1153 OTF_Script *script = list->Script + i;
1154 long script_offset = offset + script->offset;
1156 SEEK_STREAM (stream, script_offset);
1157 READ_OFFSET (stream, script->DefaultLangSysOffset);
1158 READ_USHORT (stream, script->LangSysCount);
1159 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1160 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1161 for (j = 0; j < script->LangSysCount; j++)
1163 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1164 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1167 if (script->DefaultLangSysOffset)
1169 OTF_LangSys *langsys = &script->DefaultLangSys;
1171 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1172 READ_OFFSET (stream, langsys->LookupOrder);
1173 READ_USHORT (stream, langsys->ReqFeatureIndex);
1174 READ_USHORT (stream, langsys->FeatureCount);
1175 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1177 for (k = 0; k < langsys->FeatureCount; k++)
1178 READ_USHORT (stream, langsys->FeatureIndex[k]);
1181 for (j = 0; j < script->LangSysCount; j++)
1183 OTF_LangSys *langsys = script->LangSys + j;
1185 SEEK_STREAM (stream,
1186 script_offset + script->LangSysRecord[j].LangSys);
1187 READ_OFFSET (stream, langsys->LookupOrder);
1188 READ_USHORT (stream, langsys->ReqFeatureIndex);
1189 READ_USHORT (stream, langsys->FeatureCount);
1190 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1192 for (k = 0; k < langsys->FeatureCount; k++)
1193 READ_USHORT (stream, langsys->FeatureIndex[k]);
1201 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1202 OTF_FeatureList *list)
1204 char *errfmt = "Feature List%s";
1208 SEEK_STREAM (stream, offset);
1209 READ_UINT16 (stream, list->FeatureCount);
1210 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1211 for (i = 0; i < list->FeatureCount; i++)
1213 READ_TAG (stream, list->Feature[i].FeatureTag);
1214 READ_OFFSET (stream, list->Feature[i].offset);
1216 for (i = 0; i < list->FeatureCount; i++)
1218 OTF_Feature *feature = list->Feature + i;
1220 SEEK_STREAM (stream, offset + feature->offset);
1221 READ_OFFSET (stream, feature->FeatureParams);
1222 READ_UINT16 (stream, feature->LookupCount);
1223 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1224 " (LookupListIndex)");
1225 for (j = 0; j < feature->LookupCount; j++)
1226 READ_UINT16 (stream, feature->LookupListIndex[j]);
1232 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1233 long offset, unsigned type,
1234 OTF_LookupSubTableGSUB *subtable);
1235 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1236 long offset, unsigned type,
1237 OTF_LookupSubTableGPOS *subtable);
1240 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1241 OTF_LookupList *list, int gsubp)
1243 char *errfmt = "Lookup List%s";
1247 SEEK_STREAM (stream, offset);
1248 READ_UINT16 (stream, list->LookupCount);
1249 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1251 for (i = 0; i < list->LookupCount; i++)
1252 READ_OFFSET (stream, list->Lookup[i].offset);
1253 for (i = 0; i < list->LookupCount; i++)
1255 OTF_Lookup *lookup = list->Lookup + i;
1257 SEEK_STREAM (stream, offset + lookup->offset);
1258 READ_UINT16 (stream, lookup->LookupType);
1259 READ_UINT16 (stream, lookup->LookupFlag);
1260 READ_UINT16 (stream, lookup->SubTableCount);
1261 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1262 " (SubTableOffset)");
1264 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1267 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1269 for (j = 0; j < lookup->SubTableCount; j++)
1270 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1271 for (j = 0; j < lookup->SubTableCount; j++)
1274 = offset + lookup->offset + lookup->SubTableOffset[j];
1277 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1279 lookup->SubTable.gsub + j) < 0
1280 : read_lookup_subtable_gpos (otf, stream, this_offset,
1282 lookup->SubTable.gpos + j) < 0)
1291 /*** (1-8) Structures common to GSUB and GPOS */
1294 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1295 OTF_LookupRecord **record, int count)
1297 char *errfmt = "LookupRecord%s";
1298 unsigned errret = 0;
1302 READ_UINT16 (stream, count);
1304 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1305 OTF_MALLOC (*record, count, "");
1306 for (i = 0; i < count; i++)
1308 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1309 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1315 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1317 char *errfmt = "List of Rule%s";
1318 unsigned errret = 0;
1319 OTF_StreamState state;
1323 READ_UINT16 (stream, count);
1325 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1326 OTF_MALLOC (*rule, count, "");
1327 for (i = 0; i < count; i++)
1329 READ_OFFSET (stream, (*rule)[i].offset);
1330 if (! (*rule)[i].offset)
1331 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1333 SAVE_STREAM (stream, state);
1334 for (i = 0; i < count; i++)
1336 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1337 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1338 if ((*rule)[i].GlyphCount == 0)
1339 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1340 READ_UINT16 (stream, (*rule)[i].LookupCount);
1341 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1342 (*rule)[i].GlyphCount) < 0)
1344 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1345 (*rule)[i].LookupCount) == 0)
1348 RESTORE_STREAM (stream, state);
1354 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1357 char *errfmt = "List of RuleSet%s";
1358 unsigned errret = 0;
1359 OTF_StreamState state;
1363 READ_UINT16 (stream, count);
1365 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1366 OTF_MALLOC (*set, count, "");
1367 for (i = 0; i < count; i++)
1369 READ_OFFSET (stream, (*set)[i].offset);
1370 if (! (*set)[i].offset)
1371 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1373 SAVE_STREAM (stream, state);
1374 for (i = 0; i < count; i++)
1376 SEEK_STREAM (stream, offset + (*set)[i].offset);
1378 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1380 if (! (*set)[i].RuleCount)
1383 RESTORE_STREAM (stream, state);
1388 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1389 OTF_ClassRule **rule)
1391 char *errfmt = "ClassRule%s";
1392 unsigned errret = 0;
1393 OTF_StreamState state;
1397 READ_UINT16 (stream, count);
1399 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1400 OTF_MALLOC (*rule, count, "");
1401 for (i = 0; i < count; i++)
1403 READ_OFFSET (stream, (*rule)[i].offset);
1404 if (! (*rule)[i].offset)
1405 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1407 SAVE_STREAM (stream, state);
1408 for (i = 0; i < count; i++)
1410 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1411 READ_USHORT (stream, (*rule)[i].GlyphCount);
1412 if (! (*rule)[i].GlyphCount)
1413 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1414 READ_USHORT (stream, (*rule)[i].LookupCount);
1415 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1416 0, (*rule)[i].GlyphCount - 1) < 0)
1418 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1419 (*rule)[i].LookupCount) == 0)
1422 RESTORE_STREAM (stream, state);
1427 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1430 char *errfmt = "ClassSet%s";
1431 unsigned errret = 0;
1432 OTF_StreamState state;
1436 READ_UINT16 (stream, count);
1438 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1439 OTF_CALLOC (*set, count, "");
1440 for (i = 0; i < count; i++)
1441 /* Offset can be zero. */
1442 READ_OFFSET (stream, (*set)[i].offset);
1443 SAVE_STREAM (stream, state);
1444 for (i = 0; i < count; i++)
1445 if ((*set)[i].offset)
1447 SEEK_STREAM (stream, offset + (*set)[i].offset);
1448 (*set)[i].ClassRuleCnt
1449 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1450 &(*set)[i].ClassRule);
1451 if (! (*set)[i].ClassRuleCnt)
1454 RESTORE_STREAM (stream, state);
1459 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1460 OTF_ChainRule **rule)
1462 char *errfmt = "ChainRule%s";
1463 unsigned errret = 0;
1467 READ_UINT16 (stream, count);
1469 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1470 OTF_MALLOC (*rule, count, "");
1471 for (i = 0; i < count; i++)
1472 READ_OFFSET (stream, (*rule)[i].offset);
1473 for (i = 0; i < count; i++)
1475 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1476 (*rule)[i].BacktrackGlyphCount
1477 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1478 (*rule)[i].InputGlyphCount
1479 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1480 if (! (*rule)[i].InputGlyphCount)
1481 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1482 (*rule)[i].LookaheadGlyphCount
1483 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1484 (*rule)[i].LookupCount
1485 = read_lookup_record_list (otf, stream,
1486 &(*rule)[i].LookupRecord, -1);
1487 if (! (*rule)[i].LookupCount)
1495 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1496 OTF_ChainRuleSet **set)
1498 char *errfmt = "ChainRuleSet%s";
1499 unsigned errret = 0;
1500 OTF_StreamState state;
1504 READ_UINT16 (stream, count);
1506 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1507 OTF_MALLOC (*set, count, "");
1508 for (i = 0; i < count; i++)
1510 READ_OFFSET (stream, (*set)[i].offset);
1511 if (! (*set)[i].offset)
1512 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1514 SAVE_STREAM (stream, state);
1515 for (i = 0; i < count; i++)
1517 SEEK_STREAM (stream, offset + (*set)[i].offset);
1518 (*set)[i].ChainRuleCount
1519 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1520 &(*set)[i].ChainRule);
1521 if (! (*set)[i].ChainRuleCount)
1524 RESTORE_STREAM (stream, state);
1529 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1530 OTF_ChainClassRule **rule)
1532 char *errfmt = "ChainClassRule%s";
1533 unsigned errret = 0;
1537 READ_UINT16 (stream, count);
1539 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1540 OTF_MALLOC (*rule, count, "");
1541 for (i = 0; i < count; i++)
1543 READ_OFFSET (stream, (*rule)[i].offset);
1544 if (! (*rule)[i].offset)
1545 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1547 for (i = 0; i < count; i++)
1549 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1550 (*rule)[i].BacktrackGlyphCount
1551 = read_glyph_ids (otf, stream,
1552 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1553 (*rule)[i].InputGlyphCount
1554 = read_glyph_ids (otf, stream,
1555 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1556 if (! (*rule)[i].InputGlyphCount)
1557 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1558 (*rule)[i].LookaheadGlyphCount
1559 = read_glyph_ids (otf, stream,
1560 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1561 (*rule)[i].LookupCount
1562 = read_lookup_record_list (otf, stream,
1563 &(*rule)[i].LookupRecord, -1);
1564 if (! (*rule)[i].LookupCount)
1571 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1572 OTF_ChainClassSet **set)
1574 char *errfmt = "ChainClassSet%s";
1575 unsigned errret = 0;
1576 OTF_StreamState state;
1580 READ_UINT16 (stream, count);
1582 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1583 OTF_MALLOC (*set, count, "");
1584 for (i = 0; i < count; i++)
1585 /* Offset may be zero. */
1586 READ_OFFSET (stream, (*set)[i].offset);
1587 SAVE_STREAM (stream, state);
1588 for (i = 0; i < count; i++)
1589 if ((*set)[i].offset)
1591 SEEK_STREAM (stream, offset + (*set)[i].offset);
1592 (*set)[i].ChainClassRuleCnt
1593 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1594 &(*set)[i].ChainClassRule);
1595 if (! (*set)[i].ChainClassRuleCnt)
1598 RESTORE_STREAM (stream, state);
1603 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1604 OTF_Coverage *coverage,OTF_Context1 *context1)
1606 if (read_coverage (otf, stream, offset, coverage) < 0)
1608 context1->RuleSetCount
1609 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1610 if (! context1->RuleSetCount)
1616 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1617 OTF_Coverage *coverage,OTF_Context2 *context2)
1619 if (read_coverage (otf, stream, offset, coverage) < 0
1620 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1622 context2->ClassSetCnt
1623 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1624 if (! context2->ClassSetCnt)
1630 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1631 OTF_Coverage *coverage,OTF_Context3 *context3)
1633 char *errfmt = "Context1%s";
1636 READ_USHORT (stream, context3->GlyphCount);
1637 if (context3->GlyphCount < 0)
1638 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1639 READ_USHORT (stream, context3->LookupCount);
1640 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1641 context3->GlyphCount) < 0)
1643 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1644 context3->LookupCount) < 0)
1650 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1651 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1653 if (read_coverage (otf, stream, offset, coverage) < 0)
1655 chain_context1->ChainRuleSetCount
1656 = read_chain_rule_set_list (otf, stream, offset,
1657 &chain_context1->ChainRuleSet);
1658 if (! chain_context1->ChainRuleSetCount)
1664 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1665 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1667 if (read_coverage (otf, stream, offset, coverage) < 0
1668 || read_class_def (otf, stream, offset,
1669 &chain_context2->BacktrackClassDef) < 0
1670 || read_class_def (otf, stream, offset,
1671 &chain_context2->InputClassDef) < 0
1672 || read_class_def (otf, stream, offset,
1673 &chain_context2->LookaheadClassDef) < 0)
1675 chain_context2->ChainClassSetCnt
1676 = read_chain_class_set_list (otf, stream, offset,
1677 &chain_context2->ChainClassSet);
1678 if (! chain_context2->ChainClassSetCnt)
1684 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1685 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1689 count = read_coverage_list (otf, stream, offset,
1690 &chain_context3->Backtrack, -1);
1693 chain_context3->BacktrackGlyphCount = (unsigned) count;
1694 count = read_coverage_list (otf, stream, offset,
1695 &chain_context3->Input, -1);
1698 chain_context3->InputGlyphCount = (unsigned) count;
1699 *coverage = chain_context3->Input[0];
1700 count = read_coverage_list (otf, stream, offset,
1701 &chain_context3->LookAhead, -1);
1702 chain_context3->LookaheadGlyphCount = (unsigned) count;
1703 chain_context3->LookupCount
1704 = read_lookup_record_list (otf, stream,
1705 &chain_context3->LookupRecord, -1);
1710 read_gsub_gpos_table (OTF *otf, OTF_TableInfo *table, int gsubp,
1711 enum OTF_ReaderFlag flag)
1713 OTF_Stream *stream = table->stream;
1714 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1715 void *errret = NULL;
1716 OTF_GSUB_GPOS *gsub_gpos = *table->address;
1719 SEEK_STREAM (stream, 10);
1722 SEEK_STREAM (stream, 0);
1723 OTF_CALLOC (gsub_gpos, 1, "");
1724 READ_FIXED (stream, gsub_gpos->Version);
1725 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1726 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1727 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1728 *table->address = gsub_gpos;
1731 if (! gsub_gpos->ScriptList.Script
1732 && read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1733 &gsub_gpos->ScriptList) < 0)
1735 if (flag != OTF_READ_SCRIPTS)
1737 if (! gsub_gpos->FeatureList.Feature
1738 && read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1739 &gsub_gpos->FeatureList) < 0)
1741 if (flag != OTF_READ_FEATURES)
1743 if (! gsub_gpos->LookupList.Lookup
1744 && read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1745 &gsub_gpos->LookupList, gsubp) < 0)
1754 /* (1-9) "GSUB" table */
1757 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1759 char *errfmt = "Sequence%s";
1760 unsigned errret = 0;
1764 READ_UINT16 (stream, count);
1766 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1767 OTF_MALLOC (*seq, count, "");
1768 for (i = 0; i < count; i++)
1769 READ_OFFSET (stream, (*seq)[i].offset);
1770 for (i = 0; i < count; i++)
1772 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1773 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1774 &(*seq)[i].Substitute, 0, -1);
1775 if (! (*seq)[i].GlyphCount)
1782 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1783 OTF_Ligature **ligature)
1785 char *errfmt = "Ligature%s";
1790 READ_UINT16 (stream, count);
1793 OTF_MALLOC (*ligature, count, "");
1794 for (i = 0; i < count; i++)
1795 READ_OFFSET (stream, (*ligature)[i].offset);
1796 for (i = 0; i < count; i++)
1798 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1799 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1800 (*ligature)[i].CompCount
1801 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1802 if (! (*ligature)[i].CompCount)
1809 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1810 OTF_LigatureSet **ligset)
1812 char *errfmt = "LigatureSet%s";
1817 READ_UINT16 (stream, count);
1820 OTF_MALLOC (*ligset, count, "");
1821 for (i = 0; i < count; i++)
1822 READ_OFFSET (stream, (*ligset)[i].offset);
1823 for (i = 0; i < count; i++)
1827 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1828 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1829 &(*ligset)[i].Ligature);
1832 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1838 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1839 OTF_AlternateSet **altset)
1841 char *errfmt = "AlternateSet%s";
1846 READ_UINT16 (stream, count);
1848 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1849 OTF_MALLOC (*altset, count, "");
1850 for (i = 0; i < count; i++)
1851 READ_OFFSET (stream, (*altset)[i].offset);
1852 for (i = 0; i < count; i++)
1856 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1857 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1860 (*altset)[i].GlyphCount = (unsigned) alt_count;
1866 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1867 OTF_Coverage *coverage,
1868 OTF_GSUB_ReverseChain1 *reverse_chain)
1872 if (read_coverage (otf, stream, offset, coverage) < 0)
1874 count = read_coverage_list (otf, stream, offset,
1875 &reverse_chain->Backtrack, -1);
1878 reverse_chain->BacktrackGlyphCount = (unsigned) count;
1879 count = read_coverage_list (otf, stream, offset,
1880 &reverse_chain->LookAhead, -1);
1883 reverse_chain->LookaheadGlyphCount = (unsigned) count;
1884 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
1887 reverse_chain->GlyphCount = count;
1892 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1893 unsigned type, OTF_LookupSubTableGSUB *subtable)
1898 SEEK_STREAM (stream, offset);
1899 READ_USHORT (stream, subtable->Format);
1900 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1904 if (subtable->Format == 1)
1906 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1908 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1910 else if (subtable->Format == 2)
1912 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1914 subtable->u.single2.GlyphCount
1915 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1917 if (! subtable->u.single2.GlyphCount)
1921 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1925 if (subtable->Format == 1)
1927 read_coverage (otf, stream, offset, &subtable->Coverage);
1928 subtable->u.multiple1.SequenceCount
1929 = read_sequence (otf, stream, offset,
1930 &subtable->u.multiple1.Sequence);
1933 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1937 if (subtable->Format == 1)
1939 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1941 subtable->u.alternate1.AlternateSetCount
1942 = read_alternate_set_list (otf, stream, offset,
1943 &subtable->u.alternate1.AlternateSet);
1944 if (! subtable->u.alternate1.AlternateSetCount)
1948 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1952 if (subtable->Format == 1)
1954 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1956 subtable->u.ligature1.LigSetCount
1957 = read_ligature_set_list (otf, stream, offset,
1958 &subtable->u.ligature1.LigatureSet);
1959 if (! subtable->u.ligature1.LigSetCount)
1963 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1967 if (subtable->Format == 1)
1969 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1970 &subtable->u.context1) < 0)
1973 else if (subtable->Format == 2)
1975 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1976 &subtable->u.context2) < 0)
1979 else if (subtable->Format == 3)
1981 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1982 &subtable->u.context3) < 0)
1986 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1990 if (subtable->Format == 1)
1992 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1993 &subtable->u.chain_context1) < 0)
1996 else if (subtable->Format == 2)
1998 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1999 &subtable->u.chain_context2) < 0)
2002 else if (subtable->Format == 3)
2004 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2005 &subtable->u.chain_context3) < 0)
2009 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2013 if (subtable->Format == 1)
2017 OTF_LookupSubTableGSUB *ex_subtable;
2019 READ_USHORT (stream, ex_type);
2020 READ_ULONG (stream, ex_offset);
2021 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2022 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
2023 ex_type, ex_subtable) < 0)
2025 subtable->u.extension1.ExtensionLookupType = ex_type;
2026 subtable->u.extension1.ExtensionOffset = ex_offset;
2027 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2030 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2034 if (subtable->Format == 1)
2036 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
2037 &subtable->u.reverse_chain1) < 0)
2041 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2045 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2051 read_gsub_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2053 return read_gsub_gpos_table (otf, table, 1, flag);
2057 /* (1-10) "GPOS" table */
2060 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
2061 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
2064 OTF_StreamState state;
2067 memset (value_record, 0, sizeof (OTF_ValueRecord));
2070 for (i = 0, size = 0; i < 8; i++)
2074 if (bit & OTF_XPlacement)
2075 READ_INT16 (stream, value_record->XPlacement);
2076 if (bit & OTF_YPlacement)
2077 READ_INT16 (stream, value_record->YPlacement);
2078 if (bit & OTF_XAdvance)
2079 READ_INT16 (stream, value_record->XAdvance);
2080 if (bit & OTF_YAdvance)
2081 READ_INT16 (stream, value_record->YAdvance);
2082 if (bit & OTF_XPlaDevice)
2083 READ_OFFSET (stream, value_record->XPlaDevice.offset);
2084 if (bit & OTF_YPlaDevice)
2085 READ_OFFSET (stream, value_record->YPlaDevice.offset);
2086 if (bit & OTF_XAdvDevice)
2087 READ_OFFSET (stream, value_record->XAdvDevice.offset);
2088 if (bit & OTF_YAdvDevice)
2089 READ_OFFSET (stream, value_record->YAdvDevice.offset);
2090 SAVE_STREAM (stream, state);
2091 if (value_record->XPlaDevice.offset)
2093 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
2096 if (value_record->YPlaDevice.offset)
2098 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
2101 if (value_record->XAdvDevice.offset)
2103 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
2106 if (value_record->YAdvDevice.offset)
2108 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
2111 RESTORE_STREAM (stream, state);
2117 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
2119 char *errfmt = "Anchor%s";
2122 SEEK_STREAM (stream, offset + anchor->offset);
2123 READ_UINT16 (stream, anchor->AnchorFormat);
2124 READ_INT16 (stream, anchor->XCoordinate);
2125 READ_INT16 (stream, anchor->YCoordinate);
2126 if (anchor->AnchorFormat == 1)
2128 else if (anchor->AnchorFormat == 2)
2130 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
2132 else if (anchor->AnchorFormat == 3)
2134 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
2135 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
2136 if (anchor->f.f2.XDeviceTable.offset)
2138 if (read_device_table (otf, stream, offset + anchor->offset,
2139 &anchor->f.f2.XDeviceTable) < 0)
2142 if (anchor->f.f2.YDeviceTable.offset)
2144 if (read_device_table (otf, stream, offset + anchor->offset,
2145 &anchor->f.f2.YDeviceTable) < 0)
2150 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
2156 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
2157 OTF_MarkArray *array)
2159 char *errfmt = "MarkArray%s";
2161 OTF_StreamState state;
2164 READ_OFFSET (stream, array->offset);
2165 SAVE_STREAM (stream, state);
2166 SEEK_STREAM (stream, offset + array->offset);
2167 READ_UINT16 (stream, array->MarkCount);
2168 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
2169 for (i = 0; i < array->MarkCount; i++)
2171 READ_UINT16 (stream, array->MarkRecord[i].Class);
2172 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
2174 for (i = 0; i < array->MarkCount; i++)
2175 if (read_anchor (otf, stream, offset + array->offset,
2176 &array->MarkRecord[i].MarkAnchor) < 0)
2178 RESTORE_STREAM (stream, state);
2183 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2184 unsigned ClassCount, OTF_AnchorArray *array)
2186 char *errfmt = "AnchorArray%s";
2188 OTF_StreamState state;
2191 READ_OFFSET (stream, array->offset);
2192 SAVE_STREAM (stream, state);
2193 SEEK_STREAM (stream, offset + array->offset);
2194 READ_UINT16 (stream, array->Count);
2195 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2196 for (i = 0; i < array->Count; i++)
2198 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2200 for (j = 0; j < ClassCount; j++)
2201 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2203 for (i = 0; i < array->Count; i++)
2204 for (j = 0; j < ClassCount; j++)
2205 if (read_anchor (otf, stream, offset + array->offset,
2206 &array->AnchorRecord[i].Anchor[j]) < 0)
2208 RESTORE_STREAM (stream, state);
2212 static OTF_PairSet *
2213 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2214 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2216 char *errfmt = "PairSet%s";
2217 void *errret = NULL;
2218 OTF_StreamState state;
2222 OTF_MALLOC (set, num, "");
2223 for (i = 0; i < num; i++)
2224 READ_OFFSET (stream, set[i].offset);
2225 SAVE_STREAM (stream, state);
2226 for (i = 0; i < num; i++)
2228 SEEK_STREAM (stream, offset + set[i].offset);
2229 READ_UINT16 (stream, set[i].PairValueCount);
2230 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2231 for (j = 0; j < set[i].PairValueCount; j++)
2233 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2235 READ_UINT16 (stream, rec->SecondGlyph);
2236 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2237 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2240 RESTORE_STREAM (stream, state);
2244 static OTF_Class1Record *
2245 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2246 unsigned num1, enum OTF_ValueFormat bit1,
2247 unsigned num2, enum OTF_ValueFormat bit2)
2249 char *errfmt = "Class1Record%s";
2250 void *errret = NULL;
2251 OTF_Class1Record *rec;
2254 OTF_MALLOC (rec, num1, "");
2255 for (i = 0; i < num1; i++)
2257 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2258 for (j = 0; j < num2; j++)
2260 if (read_value_record (otf, stream, offset,
2261 bit1, &rec[i].Class2Record[j].Value1) < 0
2262 || read_value_record (otf, stream, offset,
2263 bit2, &rec[i].Class2Record[j].Value2) < 0)
2271 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2272 OTF_EntryExitRecord **rec)
2274 char *errfmt = "EntryExitSet%s";
2278 OTF_StreamState state;
2280 READ_UINT16 (stream, count);
2282 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2283 OTF_MALLOC (*rec, count, "");
2284 for (i = 0; i < count; i++)
2286 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2287 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2289 SAVE_STREAM (stream, state);
2290 for (i = 0; i < count; i++)
2292 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2294 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2297 RESTORE_STREAM (stream, state);
2302 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2303 unsigned ClassCount, OTF_LigatureAttach *attach)
2305 char *errfmt = "LigatureAttach%s";
2309 SEEK_STREAM (stream, offset + attach->offset);
2310 READ_UINT16 (stream, attach->ComponentCount);
2311 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2312 for (i = 0; i < attach->ComponentCount; i++)
2314 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2315 " (ComponentRecord)");
2316 for (j = 0; j < ClassCount; j++)
2317 READ_OFFSET (stream,
2318 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2320 for (i = 0; i < attach->ComponentCount; i++)
2321 for (j = 0; j < ClassCount; j++)
2323 if (attach->ComponentRecord[i].LigatureAnchor[j].offset)
2325 if (read_anchor (otf, stream, offset + attach->offset,
2326 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2330 attach->ComponentRecord[i].LigatureAnchor[j].AnchorFormat = 0;
2336 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2337 unsigned class_count, OTF_LigatureArray *array)
2339 char *errfmt = "LigatureArray%s";
2341 OTF_StreamState state;
2344 READ_OFFSET (stream, array->offset);
2345 SAVE_STREAM (stream, state);
2346 SEEK_STREAM (stream, offset + array->offset);
2347 READ_UINT16 (stream, array->LigatureCount);
2348 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2349 for (i = 0; i < array->LigatureCount; i++)
2350 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2351 for (i = 0; i < array->LigatureCount; i++)
2352 read_ligature_attach (otf, stream, offset + array->offset,
2353 class_count, array->LigatureAttach + i);
2354 RESTORE_STREAM (stream, state);
2359 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2360 long offset, unsigned type,
2361 OTF_LookupSubTableGPOS *subtable)
2366 SEEK_STREAM (stream, offset);
2367 READ_UINT16 (stream, subtable->Format);
2368 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2372 if (subtable->Format == 1)
2374 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2376 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2377 read_value_record (otf, stream, offset,
2378 subtable->u.single1.ValueFormat,
2379 &subtable->u.single1.Value);
2381 else if (subtable->Format == 2)
2383 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2386 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2388 READ_UINT16 (stream, single2->ValueFormat);
2389 READ_UINT16 (stream, single2->ValueCount);
2390 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2391 for (i = 0; i < single2->ValueCount; i++)
2392 read_value_record (otf, stream, offset, single2->ValueFormat,
2393 single2->Value + i);
2396 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2400 if (subtable->Format == 1)
2402 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2404 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2405 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2406 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2407 subtable->u.pair1.PairSet
2408 = read_pair_set_list (otf, stream, offset,
2409 subtable->u.pair1.PairSetCount,
2410 subtable->u.pair1.ValueFormat1,
2411 subtable->u.pair1.ValueFormat2);
2412 if (! subtable->u.pair1.PairSet)
2415 else if (subtable->Format == 2)
2417 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2419 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2420 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2421 if (read_class_def (otf, stream, offset,
2422 &subtable->u.pair2.ClassDef1) < 0
2423 || read_class_def (otf, stream, offset,
2424 &subtable->u.pair2.ClassDef2) < 0)
2426 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2427 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2428 subtable->u.pair2.Class1Record
2429 = read_class1_record_list (otf, stream, offset,
2430 subtable->u.pair2.Class1Count,
2431 subtable->u.pair2.ValueFormat1,
2432 subtable->u.pair2.Class2Count,
2433 subtable->u.pair2.ValueFormat2);
2434 if (! subtable->u.pair2.Class1Record)
2438 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2442 if (subtable->Format == 1)
2444 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2446 subtable->u.cursive1.EntryExitCount
2447 = read_entry_exit_list (otf, stream, offset,
2448 &subtable->u.cursive1.EntryExitRecord);
2449 if (! subtable->u.cursive1.EntryExitCount)
2453 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2457 if (subtable->Format == 1)
2459 read_coverage (otf, stream, offset, &subtable->Coverage);
2460 read_coverage (otf, stream, offset,
2461 &subtable->u.mark_base1.BaseCoverage);
2462 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2463 read_mark_array (otf, stream, offset,
2464 &subtable->u.mark_base1.MarkArray);
2465 read_anchor_array (otf, stream, offset,
2466 subtable->u.mark_base1.ClassCount,
2467 &subtable->u.mark_base1.BaseArray);
2470 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2474 if (subtable->Format == 1)
2476 read_coverage (otf, stream, offset, &subtable->Coverage);
2477 read_coverage (otf, stream, offset,
2478 &subtable->u.mark_lig1.LigatureCoverage);
2479 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2480 read_mark_array (otf, stream, offset,
2481 &subtable->u.mark_lig1.MarkArray);
2482 read_ligature_array (otf, stream, offset,
2483 subtable->u.mark_lig1.ClassCount,
2484 &subtable->u.mark_lig1.LigatureArray);
2489 if (subtable->Format == 1)
2491 read_coverage (otf, stream, offset, &subtable->Coverage);
2492 read_coverage (otf, stream, offset,
2493 &subtable->u.mark_mark1.Mark2Coverage);
2494 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2495 read_mark_array (otf, stream, offset,
2496 &subtable->u.mark_mark1.Mark1Array);
2497 read_anchor_array (otf, stream, offset,
2498 subtable->u.mark_mark1.ClassCount,
2499 &subtable->u.mark_mark1.Mark2Array);
2502 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2506 if (subtable->Format == 1)
2508 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2509 &subtable->u.context1) < 0)
2512 else if (subtable->Format == 2)
2514 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2515 &subtable->u.context2) < 0)
2518 else if (subtable->Format == 3)
2520 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2521 &subtable->u.context3) < 0)
2525 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2529 if (subtable->Format == 1)
2531 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2532 &subtable->u.chain_context1) < 0)
2535 else if (subtable->Format == 2)
2537 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2538 &subtable->u.chain_context2) < 0)
2541 else if (subtable->Format == 3)
2543 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2544 &subtable->u.chain_context3) < 0)
2548 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2552 if (subtable->Format == 1)
2556 OTF_LookupSubTableGPOS *ex_subtable;
2558 READ_USHORT (stream, ex_type);
2559 READ_ULONG (stream, ex_offset);
2560 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2561 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2562 ex_type, ex_subtable) < 0)
2564 subtable->u.extension1.ExtensionLookupType = ex_type;
2565 subtable->u.extension1.ExtensionOffset = ex_offset;
2566 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2569 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2573 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2579 read_gpos_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2581 return read_gsub_gpos_table (otf, table, 0, flag);
2589 read_base_table (OTF_Stream *stream, long offset)
2593 OTF_MALLOC (base, 1);
2602 read_jstf_table (OTF_Stream *stream, long offset)
2606 OTF_MALLOC (jstf, 1);
2612 /*** (1-11) Structure for OTF */
2615 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2619 READ_FIXED (stream, table->sfnt_version);
2620 READ_USHORT (stream, table->numTables);
2621 READ_USHORT (stream, table->searchRange);
2622 READ_USHORT (stream, table->enterSelector);
2623 READ_USHORT (stream, table->rangeShift);
2628 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2633 READ_TAG (stream, tag);
2635 table->name[0] = tag >> 24;
2636 table->name[1] = (tag >> 16) & 0xFF;
2637 table->name[2] = (tag >> 8) & 0xFF;
2638 table->name[3] = tag & 0xFF;
2639 table->name[4] = '\0';
2640 READ_ULONG (stream, table->checkSum);
2641 READ_ULONG (stream, table->offset);
2642 READ_ULONG (stream, table->length);
2647 read_header_part (OTF *otf, FILE *fp, FT_Face face)
2649 char *errfmt = "otf header%s";
2652 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2654 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2655 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2656 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2657 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2658 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2659 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2660 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2661 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2662 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2663 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2664 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2665 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2669 OTF_Tag head_tag = OTF_tag ("head");
2670 OTF_Tag name_tag = OTF_tag ("name");
2671 OTF_Tag cmap_tag = OTF_tag ("cmap");
2672 OTF_Tag gdef_tag = OTF_tag ("GDEF");
2673 OTF_Tag gsub_tag = OTF_tag ("GSUB");
2674 OTF_Tag gpos_tag = OTF_tag ("GPOS");
2675 OTF_Stream *stream = make_stream ("Offset Table");
2679 internal_data->header_stream = stream;
2681 /* Size of Offset Table is 12 bytes. */
2682 if (setup_stream (stream, fp, 0, 12) < 0)
2684 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2687 /* Size of each Table Directory is 16 bytes. */
2688 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables) < 0)
2691 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables,
2693 for (i = 0; i < otf->offset_table.numTables; i++)
2695 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2696 OTF_TableInfo *table_info = NULL;
2700 if (tag == head_tag)
2701 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2702 else if (tag == name_tag)
2703 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2704 else if (tag == cmap_tag)
2705 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2706 else if (tag == gdef_tag)
2707 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2708 else if (tag == gsub_tag)
2709 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2710 else if (tag == gpos_tag)
2711 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2715 table_info->stream = make_stream (otf->table_dirs[i].name);
2716 if (setup_stream (table_info->stream, fp,
2717 otf->table_dirs[i].offset,
2718 otf->table_dirs[i].length) < 0)
2723 internal_data->header_stream = NULL;
2724 free_stream (stream);
2730 internal_data->header_stream = NULL;
2731 if ((stream = make_stream_from_ft_face (face, "head")))
2732 internal_data->table_info[OTF_TABLE_TYPE_HEAD].stream = stream;
2733 if ((stream = make_stream_from_ft_face (face, "name")))
2734 internal_data->table_info[OTF_TABLE_TYPE_NAME].stream = stream;
2735 if ((stream = make_stream_from_ft_face (face, "cmap")))
2736 internal_data->table_info[OTF_TABLE_TYPE_CMAP].stream = stream;
2737 if ((stream = make_stream_from_ft_face (face, "GDEF")))
2738 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream = stream;
2739 if ((stream = make_stream_from_ft_face (face, "GSUB")))
2740 internal_data->table_info[OTF_TABLE_TYPE_GSUB].stream = stream;
2741 if ((stream = make_stream_from_ft_face (face, "GPOS")))
2742 internal_data->table_info[OTF_TABLE_TYPE_GPOS].stream = stream;
2748 static OTF_TableInfo *
2749 get_table_info (OTF *otf, const char *name)
2751 char *errfmt = "OTF Table Read%s";
2752 OTF_TableInfo *errret = NULL;
2753 OTF_InternalData *internal_data = otf->internal_data;
2754 OTF_TableInfo *table_info;
2755 OTF_Tag tag = OTF_tag (name);
2758 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2760 if (tag == OTF_tag ("head"))
2761 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2762 else if (tag == OTF_tag ("name"))
2763 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2764 else if (tag == OTF_tag ("cmap"))
2765 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2766 else if (tag == OTF_tag ("GDEF"))
2767 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2768 else if (tag == OTF_tag ("GSUB"))
2769 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2770 else if (tag == OTF_tag ("GPOS"))
2771 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2773 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2775 if (*table_info->address)
2778 if (! table_info->stream)
2779 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2780 if (! table_info->reader)
2781 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2787 /*** (2) API for reading OTF */
2789 /*** (2-1) OTF_open() */
2791 /* Note: We can't use memory allocation macros in the following
2792 functions because those macros return from the functions before
2793 freeing memory previously allocated. */
2796 OTF_open (const char *otf_name)
2799 char *errfmt = "opening otf (%s)";
2800 void *errret = NULL;
2802 OTF_InternalData *internal_data;
2803 int len = strlen (otf_name);
2804 const char *ext = otf_name + (len - 4);
2808 || (ext[1] != 'O' && ext[1] != 'T' && ext[1] != 'o' && ext[1] != 't')
2809 || (ext[2] != 'T' && ext[2] != 't')
2810 || (ext[3] != 'F' && ext[3] != 'f'))
2811 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2812 fp = fopen (otf_name, "r");
2814 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2815 otf = calloc (1, sizeof (OTF));
2817 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2818 otf->filename = strdup (otf_name);
2819 if (! otf->filename)
2823 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2826 internal_data = calloc (1, sizeof (OTF_InternalData));
2827 if (! internal_data)
2828 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2829 otf->internal_data = internal_data;
2830 if (! allocate_memory_record (otf))
2831 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2833 /* Here after, all pointers to allocated memory are recorded in
2834 otf->internal_data->memory_record except for what allocated by
2835 the functions allocate_memory_record and make_stream. */
2837 if (read_header_part (otf, fp, NULL) < 0)
2849 OTF_open_ft_face (FT_Face face)
2851 char *errfmt = "opening otf from Freetype (%s)";
2852 void *errret = NULL;
2854 OTF_InternalData *internal_data;
2856 if (! FT_IS_SFNT (face))
2857 OTF_ERROR (OTF_ERROR_FILE, (char *) face->family_name);
2858 otf = calloc (1, sizeof (OTF));
2860 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2861 otf->filename = NULL;
2863 internal_data = calloc (1, sizeof (OTF_InternalData));
2864 if (! internal_data)
2865 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2866 otf->internal_data = internal_data;
2867 if (! allocate_memory_record (otf))
2868 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2870 if (read_header_part (otf, NULL, face) < 0)
2879 /*** (2-2) OTF_close() */
2882 OTF_close (OTF *otf)
2884 OTF_InternalData *internal_data = otf->internal_data;
2889 OTF_MemoryRecord *memrec = internal_data->memory_record;
2891 if (internal_data->header_stream)
2892 free_stream (internal_data->header_stream);
2894 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2895 if (internal_data->table_info[i].stream)
2896 free_stream (internal_data->table_info[i].stream);
2900 OTF_MemoryRecord *next = memrec->next;
2902 for (i = memrec->used - 1; i >= 0; i--)
2903 free (memrec->memory[i]);
2907 free (internal_data);
2910 free (otf->filename);
2914 /*** (2-3) OTF_get_table() */
2917 OTF_get_table (OTF *otf, const char *name)
2919 OTF_TableInfo *table_info = get_table_info (otf, name);
2924 if (! table_info->stream)
2925 /* Already fully loaded. */
2928 address = (*table_info->reader) (otf, table_info, OTF_READ_FULL);
2929 free_stream (table_info->stream);
2930 table_info->stream = NULL;
2933 table_info->reader = NULL;
2939 /*** (2-4) OTF_check_table() */
2942 OTF_check_table (OTF *otf, const char *name)
2944 return (get_table_info (otf, name) ? 0 : -1);
2947 /*** (2-5) OTF_get_scripts() */
2950 OTF_get_scripts (OTF *otf, int gsubp)
2952 OTF_TableInfo *table_info
2953 = (otf->internal_data->table_info
2954 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
2957 if (! table_info->reader)
2959 if (! table_info->stream)
2960 /* Already fully loaded. */
2963 address = (*table_info->reader) (otf, table_info, OTF_READ_SCRIPTS);
2966 table_info->reader = NULL;
2972 /*** (2-6) OTF_get_features() */
2975 OTF_get_features (OTF *otf, int gsubp)
2977 OTF_TableInfo *table_info
2978 = (otf->internal_data->table_info
2979 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
2982 if (! table_info->reader)
2984 if (! table_info->stream)
2986 if (*table_info->address)
2987 /* Already fully loaded. */
2992 address = (*table_info->reader) (otf, table_info, OTF_READ_FEATURES);
2995 table_info->reader = NULL;
3001 /*** (2-7) OTF_check_features */
3004 OTF_check_features (OTF *otf, int gsubp,
3005 OTF_Tag script, OTF_Tag language, const OTF_Tag *features,
3008 OTF_ScriptList *script_list;
3009 OTF_Script *Script = NULL;
3010 OTF_LangSys *LangSys = NULL;
3011 OTF_FeatureList *feature_list;
3014 if (OTF_get_features (otf, gsubp) < 0)
3016 for (i = 0; i < n_features; i++)
3018 OTF_Tag feature = features[i];
3022 if ((((unsigned) feature) & 0x80000000) == 0)
3029 script_list = &otf->gsub->ScriptList;
3030 feature_list = &otf->gsub->FeatureList;
3034 script_list = &otf->gpos->ScriptList;
3035 feature_list = &otf->gpos->FeatureList;
3037 for (i = 0; i < script_list->ScriptCount && ! Script; i++)
3038 if (script_list->Script[i].ScriptTag == script)
3039 Script = script_list->Script + i;
3044 for (i = 0; i < Script->LangSysCount && ! LangSys; i++)
3045 if (Script->LangSysRecord[i].LangSysTag == language)
3046 LangSys = Script->LangSys + i;
3049 LangSys = &Script->DefaultLangSys;
3050 for (j = 0; j < n_features; j++)
3052 OTF_Tag feature = features[j];
3057 if (((unsigned) feature) & 0x80000000)
3059 feature = (OTF_Tag) (((unsigned) feature) & 0x7FFFFFFF);
3062 for (i = 0; i < LangSys->FeatureCount; i++)
3063 if (feature_list->Feature[LangSys->FeatureIndex[i]].FeatureTag
3070 if (i == LangSys->FeatureCount)
3077 /*** (5) API miscellaneous ***/
3080 OTF_tag (const char *name)
3082 const unsigned char *p = (unsigned char *) name;
3086 return (OTF_Tag) ((p[0] << 24)
3090 : (p[2] << 8) | p[3]))));
3094 OTF_tag_name (OTF_Tag tag, char *name)
3096 name[0] = (char) (tag >> 24);
3097 name[1] = (char) ((tag >> 16) & 0xFF);
3098 name[2] = (char) ((tag >> 8) & 0xFF);
3099 name[3] = (char) (tag & 0xFF);