1 /* otfopen.c -- OpenType font reader.
3 Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009
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()
66 debug_flag = getenv ("LIBOTF_DEBUG") != NULL;
72 Example of typical usage of OTF_Stream.
76 OTF_StreamState state;
79 OPEN_STREAM (_FILE_NAME_, stream);
82 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
83 offset = READ_OFFSET (stream);
84 nbytes = READ_ULONG (stream);
85 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
87 CLOSE_STREAM (stream);
101 typedef long OTF_StreamState;
104 make_stream (const char *name)
107 char *errfmt = "stream creation%s";
110 stream = calloc (1, sizeof (OTF_Stream));
112 OTF_ERROR (OTF_ERROR_MEMORY, "");
118 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes)
120 char *errfmt = "stream setup for %s";
124 if (stream->allocated < nbytes)
126 unsigned char *buf = malloc (nbytes);
129 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
133 stream->allocated = nbytes;
135 stream->bufsize = nbytes;
136 if (fseek (fp, offset, SEEK_SET) < 0)
137 OTF_ERROR (OTF_ERROR_FILE, stream->name);
138 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
139 OTF_ERROR (OTF_ERROR_FILE, stream->name);
144 make_stream_from_ft_face (FT_Face face, const char *name)
146 char *errfmt = "FT_Face stream creation for %s";
151 FT_ULong tag = FT_MAKE_TAG (name[0], name[1], name[2], name[3]);
153 if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &nbytes))
155 buf = malloc (nbytes);
157 OTF_ERROR (OTF_ERROR_MEMORY, name);
158 if (FT_Load_Sfnt_Table (face, tag, 0, buf, &nbytes))
161 OTF_ERROR (OTF_ERROR_FT_FACE, name);
163 stream = make_stream (name);
168 stream->allocated = nbytes;
169 stream->bufsize = nbytes;
174 free_stream (OTF_Stream *stream)
181 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
182 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
183 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
185 #define STREAM_CHECK_SIZE(stream, size) \
186 if ((stream)->pos + (size) > (stream)->bufsize) \
188 char *errfmt = "buffer overrun in %s"; \
190 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
196 #define READ_USHORT(stream, var) \
198 STREAM_CHECK_SIZE ((stream), 2); \
199 (var) = (((stream)->buf[(stream)->pos] << 8) \
200 | (stream)->buf[(stream)->pos + 1]); \
201 (stream)->pos += 2; \
204 #define READ_SHORT(stream, var) \
206 STREAM_CHECK_SIZE ((stream), 2); \
207 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
208 | (stream)->buf[(stream)->pos + 1]); \
209 (stream)->pos += 2; \
212 #define READ_UINT24(stream, var) \
214 STREAM_CHECK_SIZE ((stream), 3); \
215 (var) = (((stream)->buf[(stream)->pos ] << 16) \
216 | ((stream)->buf[(stream)->pos + 1] << 8) \
217 | (stream)->buf[(stream)->pos + 2]); \
218 (stream)->pos += 3; \
221 #define READ_ULONG(stream, var) \
223 STREAM_CHECK_SIZE ((stream), 4); \
224 (var) = (((stream)->buf[(stream)->pos] << 24) \
225 | ((stream)->buf[(stream)->pos + 1] << 16) \
226 | ((stream)->buf[(stream)->pos + 2] << 8) \
227 | (stream)->buf[(stream)->pos + 3]); \
228 (stream)->pos += 4; \
231 #define READ_LONG(stream, var) \
233 STREAM_CHECK_SIZE ((stream), 4); \
234 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
235 | ((stream)->buf[(stream)->pos + 1] << 16) \
236 | ((stream)->buf[(stream)->pos + 2] << 8) \
237 | (stream)->buf[(stream)->pos + 3]); \
238 (stream)->pos += 4; \
242 #define READ_FIXED(stream, fixed) \
244 READ_USHORT ((stream), (fixed).high); \
245 READ_USHORT ((stream), (fixed).low); \
249 #define READ_BYTES(stream, p, nbytes) \
251 STREAM_CHECK_SIZE ((stream), (nbytes)); \
252 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
253 (stream)->pos += (nbytes); \
257 #define READ_TAG READ_ULONG
258 #define READ_OFFSET READ_USHORT
259 #define READ_UINT16 READ_USHORT
260 #define READ_INT16 READ_SHORT
261 #define READ_GLYPHID READ_USHORT
264 /*** (1) Structures for OTF Layout tables and OTF itself */
266 /*** (1-1) Basic types and functions */
279 #define OTF_MEMORY_RECORD_SIZE 1024
281 struct OTF_MemoryRecord
284 void *memory[OTF_MEMORY_RECORD_SIZE];
285 struct OTF_MemoryRecord *next;
288 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
298 struct _OTF_TableInfo;
299 typedef struct _OTF_TableInfo OTF_TableInfo;
301 struct _OTF_TableInfo
303 /* Points to one of OTF->head, OTF->name, etc. */
305 /* Function to read one of OTF tables. */
306 void *(*reader) (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag);
307 /* Stream given to <reader>. */
311 struct _OTF_ApplicationData
315 void (*freer) (void *data);
316 struct _OTF_ApplicationData *next;
319 typedef struct _OTF_ApplicationData OTF_ApplicationData;
321 struct OTF_InternalData
323 /* Information about each OTF table. */
324 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
326 /* Stream used to read the header part of OTF. */
327 OTF_Stream *header_stream;
329 /* Records of allocated memories. */
330 OTF_MemoryRecord *memory_record;
332 /* Root of application data chain. */
333 OTF_ApplicationData *app_data;
337 static OTF_MemoryRecord *
338 allocate_memory_record (OTF *otf)
340 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
341 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
346 memrec->next = internal_data->memory_record;
347 internal_data->memory_record = memrec;
351 /* Memory allocation macros. */
353 #define OTF_MALLOC(p, size, arg) \
359 OTF_MemoryRecord *memrec \
360 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
361 (p) = malloc (sizeof (*(p)) * (size)); \
363 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
364 && ! (memrec = allocate_memory_record (otf)))) \
365 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
366 memrec->memory[memrec->used++] = (p); \
371 #define OTF_CALLOC(p, size, arg) \
377 OTF_MemoryRecord *memrec \
378 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
379 (p) = calloc ((size), sizeof (*(p))); \
381 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
382 && ! (memrec = allocate_memory_record (otf)))) \
383 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
384 memrec->memory[memrec->used++] = (p); \
389 /*** (1-2) "head" table */
392 read_head_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
394 OTF_Stream *stream = table->stream;
395 char *errfmt = "head%s";
399 OTF_CALLOC (head, 1, "");
400 READ_FIXED (stream, head->TableVersionNumber);
401 READ_FIXED (stream, head->fontRevision);
402 READ_ULONG (stream, head->checkSumAdjustment);
403 READ_ULONG (stream, head->magicNumber);
404 READ_USHORT (stream, head->flags);
405 READ_USHORT (stream, head->unitsPerEm);
407 *table->address = head;
412 /*** (1-3) "name" table */
415 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
419 OTF_StreamState state;
424 sprintf (errfmt, "nameID (%d)%%s", rec->nameID);
425 if (rec->platformID == 0)
426 ucs = (rec->encodingID <= 3) ? 2 : 4;
427 else if (rec->platformID == 1 && rec->encodingID == 0)
429 else if (rec->platformID == 3)
430 ucs = (rec->encodingID == 1 ? 2
431 : rec->encodingID == 10 ? 4
434 OTF_MALLOC (rec->name, rec->length + 1, "");
435 SAVE_STREAM (stream, state);
436 SEEK_STREAM (stream, stream->pos + rec->offset);
437 READ_BYTES (stream, rec->name, rec->length);
438 RESTORE_STREAM (stream, state);
439 rec->name[rec->length] = 0;
448 for (i = 0; i < rec->length / 2; i++)
450 if (rec->name[i * 2] > 0
451 || rec->name[i * 2 + 1] >= 128)
458 for (i = 0; i < rec->length / 2; i++)
459 rec->name[i] = rec->name[i * 2 + 1];
465 for (i = 0; i < rec->length / 4; i++)
467 if (rec->name[i * 4] > 0
468 || rec->name[i * 4 + 1] > 0
469 || rec->name[i * 4 + 2] > 0
470 || rec->name[i * 2 + 3] >= 128)
477 for (i = 0; i < rec->length / 4; i++)
478 rec->name[i] = rec->name[i * 4 + 3];
486 read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
488 OTF_Stream *stream = table->stream;
489 char *errfmt = "name%s";
494 OTF_CALLOC (name, 1, "");
495 READ_USHORT (stream, name->format);
496 READ_USHORT (stream, name->count);
497 READ_USHORT (stream, name->stringOffset);
498 OTF_MALLOC (name->nameRecord, name->count, "");
499 for (i = 0; i < name->count; i++)
501 OTF_NameRecord *rec = name->nameRecord + i;
503 READ_USHORT (stream, rec->platformID);
504 READ_USHORT (stream, rec->encodingID);
505 READ_USHORT (stream, rec->languageID);
506 READ_USHORT (stream, rec->nameID);
507 READ_USHORT (stream, rec->length);
508 READ_USHORT (stream, rec->offset);
510 for (i = 0; i < name->count; i++)
512 OTF_NameRecord *rec = name->nameRecord + i;
513 int nameID = rec->nameID;
515 read_name (otf, stream, rec);
517 if (nameID >= OTF_max_nameID)
519 if (! name->name[nameID]
521 name->name[nameID] = (char *) rec->name;
524 *table->address = name;
529 /*** (1-4) "cmap" table */
531 static OTF_EncodingSubtable14 *
532 read_cmap_uvs_table (OTF *otf, OTF_Stream *stream, OTF_Offset offset)
534 OTF_EncodingSubtable14 *sub14;
535 char *errfmt = "cmap-uvs%s";
540 OTF_MALLOC (sub14, 1, " (EncodingSubtable14)");
541 READ_ULONG (stream, nRecords);
542 sub14->nRecords = nRecords;
543 OTF_MALLOC (sub14->Records, nRecords, "(EncodingSubtable14-Records)");
544 for (i = 0; i < sub14->nRecords; i++)
546 unsigned varSelector=0, defaultUVSOffset, nonDefaultUVSOffset;
548 READ_UINT24 (stream, varSelector);
549 sub14->Records[i].varSelector = varSelector;
550 READ_ULONG (stream, defaultUVSOffset);
551 sub14->Records[i].defaultUVSOffset = defaultUVSOffset;
552 READ_ULONG (stream, nonDefaultUVSOffset);
553 sub14->Records[i].nonDefaultUVSOffset = nonDefaultUVSOffset;
555 for (i = 0; i < sub14->nRecords; i++)
557 OTF_VariationSelectorRecord *record = &sub14->Records[i];
558 unsigned defaultUVSOffset = record->defaultUVSOffset;
559 unsigned nonDefaultUVSOffset = record->nonDefaultUVSOffset;
561 if (defaultUVSOffset)
563 unsigned numUnicodeValueRanges;
565 SEEK_STREAM (stream, offset+defaultUVSOffset);
566 READ_ULONG (stream, numUnicodeValueRanges);
567 record->numUnicodeValueRanges = numUnicodeValueRanges;
568 OTF_MALLOC (record->unicodeValueRanges,
569 numUnicodeValueRanges,
570 "(EncodingSubtable14-Records-unicodeValueRanges)");
571 for (j = 0; j < numUnicodeValueRanges; j++)
573 OTF_UnicodeValueRange *unicodeValueRange
574 = &record->unicodeValueRanges[j];
575 unsigned startUnicodeValue;
576 char additionalCount;
578 READ_UINT24 (stream, startUnicodeValue);
579 unicodeValueRange->startUnicodeValue=startUnicodeValue;
580 READ_BYTES (stream, &additionalCount, 1);
581 unicodeValueRange->additionalCount
582 = (unsigned short) additionalCount;
585 if (nonDefaultUVSOffset)
587 unsigned numUVSMappings;
589 SEEK_STREAM (stream, offset+nonDefaultUVSOffset);
590 READ_ULONG (stream, numUVSMappings);
591 record->numUVSMappings = numUVSMappings;
592 OTF_MALLOC (record->uvsMappings, numUVSMappings,
593 "(EncodingSubtable14-Records-uvsMappings)");
594 for (j = 0; j < numUVSMappings; j++)
596 OTF_UVSMapping *uvsMapping = &record->uvsMappings[j];
597 unsigned unicodeValue;
598 unsigned short glyphID;
600 READ_UINT24 (stream, unicodeValue);
601 uvsMapping->unicodeValue = unicodeValue;
602 READ_USHORT (stream, glyphID);
603 uvsMapping->glyphID = glyphID;
611 read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
613 OTF_Stream *stream = table->stream;
614 char *errfmt = "cmap%s";
617 int unicode_bmp_index = -1, unicode_full_index = -1;
620 OTF_CALLOC (cmap, 1, "");
621 READ_USHORT (stream, cmap->version);
622 READ_USHORT (stream, cmap->numTables);
623 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
624 for (i = 0; i < cmap->numTables; i++)
626 unsigned platformID, encodingID;
628 READ_USHORT (stream, platformID);
629 cmap->EncodingRecord[i].platformID = platformID;
630 READ_USHORT (stream, encodingID);
631 cmap->EncodingRecord[i].encodingID = encodingID;
632 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
636 unicode_bmp_index = i;
638 unicode_full_index = i;
640 else if (platformID == 3)
643 unicode_bmp_index = i;
644 else if (encodingID == 10)
645 unicode_full_index = i;
648 cmap->table_index = unicode_full_index;
650 for (i = 0; i < cmap->numTables; i++)
654 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
655 READ_USHORT (stream, format);
656 cmap->EncodingRecord[i].subtable.format = format;
659 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
660 cmap->EncodingRecord[i].subtable.language = 0;
664 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
665 if (format == 8 || format == 10 || format == 12)
667 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
668 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
672 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
679 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
680 " (EncodingRecord)");
682 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
689 OTF_EncodingSubtable2 *sub2;
692 OTF_MALLOC (sub2, 1, " (EncodingSubtable2)");
693 cmap->EncodingRecord[i].subtable.f.f2 = sub2;
694 for (j = 0, max_key = 0; j < 256; j++)
696 READ_USHORT (stream, sub2->subHeaderKeys[j]);
697 if (max_key < sub2->subHeaderKeys[j])
698 max_key = sub2->subHeaderKeys[j];
701 sub2->subHeaderCount = max_key / 8;
702 OTF_MALLOC (sub2->subHeaders, max_key / 8, " (subHeaders)");
703 for (j = 0; j < sub2->subHeaderCount; j++)
705 READ_USHORT (stream, sub2->subHeaders[j].firstCode);
706 READ_USHORT (stream, sub2->subHeaders[j].entryCount);
707 READ_SHORT (stream, sub2->subHeaders[j].idDelta);
708 READ_USHORT (stream, sub2->subHeaders[j].idRangeOffset);
709 /* Make it offset from sub2->glyphIndexArray. */
710 sub2->subHeaders[j].idRangeOffset -= max_key - (j * 8 + 6);
712 sub2->glyphIndexCount = (cmap->EncodingRecord[i].subtable.length
714 OTF_MALLOC (sub2->glyphIndexArray, sub2->glyphIndexCount,
715 " (glyphIndexArray)");
716 READ_BYTES (stream, sub2->glyphIndexArray, sub2->glyphIndexCount);
722 OTF_EncodingSubtable4 *sub4;
727 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
728 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
729 READ_USHORT (stream, sub4->segCountX2);
730 segCount = sub4->segCountX2 / 2;
731 READ_USHORT (stream, sub4->searchRange);
732 READ_USHORT (stream, sub4->entrySelector);
733 READ_USHORT (stream, sub4->rangeShift);
734 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
735 for (j = 0; j < segCount; j++)
736 READ_USHORT (stream, sub4->segments[j].endCount);
737 READ_USHORT (stream, dummy);
738 for (j = 0; j < segCount; j++)
739 READ_USHORT (stream, sub4->segments[j].startCount);
740 for (j = 0; j < segCount; j++)
741 READ_SHORT (stream, sub4->segments[j].idDelta);
742 for (j = 0; j < segCount; j++)
745 unsigned rest = 2 * (segCount - j);
747 READ_USHORT (stream, off);
749 sub4->segments[j].idRangeOffset = 0xFFFF;
751 sub4->segments[j].idRangeOffset = (off - rest) / 2;
753 j = (cmap->EncodingRecord[i].subtable.length
754 - (14 + 2 * (segCount * 4 + 1)));
755 sub4->GlyphCount = j / 2;
756 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
757 for (j = 0; j < sub4->GlyphCount; j++)
758 READ_USHORT (stream, sub4->glyphIdArray[j]);
764 OTF_EncodingSubtable6 *sub6;
767 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
768 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
769 READ_USHORT (stream, sub6->firstCode);
770 READ_USHORT (stream, sub6->entryCount);
771 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
772 for (j = 0; j < sub6->entryCount; j++)
773 READ_USHORT (stream, sub6->glyphIdArray[j]);
779 OTF_EncodingSubtable8 *sub8;
782 OTF_MALLOC (sub8, 1, " (EncodingSubtable8)");
783 cmap->EncodingRecord[i].subtable.f.f8 = sub8;
784 for (j = 0; j < 8192; j++)
785 READ_BYTES (stream, sub8->is32, 8192);
786 READ_ULONG (stream, sub8->nGroups);
787 OTF_MALLOC (sub8->Groups, sub8->nGroups, " (Groups)");
788 for (j = 0; j < sub8->nGroups; j++)
790 READ_ULONG (stream, sub8->Groups[i].startCharCode);
791 READ_ULONG (stream, sub8->Groups[i].endCharCode);
792 READ_ULONG (stream, sub8->Groups[i].startGlyphID);
799 OTF_EncodingSubtable10 *sub10;
802 OTF_MALLOC (sub10, 1, " (EncodingSubtable10)");
803 cmap->EncodingRecord[i].subtable.f.f10 = sub10;
804 READ_ULONG (stream, sub10->startCharCode);
805 READ_ULONG (stream, sub10->numChars);
806 OTF_MALLOC (sub10->glyphs, sub10->numChars, " (GlyphCount)");
807 for (j = 0; j < sub10->numChars; j++)
808 READ_USHORT (stream, sub10->glyphs[j]);
814 OTF_EncodingSubtable12 *sub12;
817 OTF_MALLOC (sub12, 1, " (EncodingSubtable12)");
818 cmap->EncodingRecord[i].subtable.f.f12 = sub12;
819 READ_ULONG (stream, sub12->nGroups);
820 OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
821 for (j = 0; j < sub12->nGroups; j++)
823 READ_ULONG (stream, sub12->Groups[j].startCharCode);
824 READ_ULONG (stream, sub12->Groups[j].endCharCode);
825 READ_ULONG (stream, sub12->Groups[j].startGlyphID);
832 cmap->EncodingRecord[i].subtable.f.f14
833 = read_cmap_uvs_table (otf, stream,
834 cmap->EncodingRecord[i].offset);
839 OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
843 if (unicode_bmp_index >= 0)
845 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_bmp_index;
846 OTF_GlyphID glyph_id, max_glyph_id = 0;
848 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
849 switch (rec->subtable.format)
853 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
854 int segCount = sub4->segCountX2 / 2;
856 for (i = 0; i < segCount; i++)
858 OTF_cmapSegment *seg = sub4->segments + i;
861 if (seg->idRangeOffset == 0xFFFF)
862 for (c = seg->startCount; c <= seg->endCount; c++)
864 glyph_id = (c + seg->idDelta) % 0x10000;
865 cmap->unicode_table[c] = glyph_id;
866 if (glyph_id > max_glyph_id)
867 max_glyph_id = glyph_id;
870 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
873 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
874 + (c - seg->startCount)];
875 cmap->unicode_table[c] = glyph_id;
876 if (glyph_id > max_glyph_id)
877 max_glyph_id = glyph_id;
883 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
884 for (i = 0; i < 0x10000; i++)
885 if (cmap->unicode_table[i])
886 cmap->decode_table[cmap->unicode_table[i]] = i;
887 cmap->max_glyph_id = max_glyph_id;
890 *table->address = cmap;
895 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
897 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
898 Glyph-IDs there. If COUNT is negative, read the number of
899 Glyphs-IDs at first. MINUS if nozero is how few the actual
900 Glyph-IDs are in STREAM than COUNT. */
903 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
904 int minus, int count)
906 char *errfmt = "GlyphID List%s";
911 READ_UINT16 (stream, count);
914 OTF_MALLOC (*ids, count, "");
915 for (i = 0; i < count + minus; i++)
916 READ_GLYPHID (stream, (*ids)[i]);
921 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
923 char *errfmt = "RangeRecord%s";
928 READ_UINT16 (stream, count);
931 OTF_MALLOC (*record, count, "");
932 for (i = 0; i < count; i++)
934 READ_GLYPHID (stream, (*record)[i].Start);
935 READ_GLYPHID (stream, (*record)[i].End);
936 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
943 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
944 OTF_Coverage *coverage)
946 char *errfmt = "Coverage%s";
948 OTF_StreamState state;
951 READ_OFFSET (stream, coverage->offset);
952 SAVE_STREAM (stream, state);
953 SEEK_STREAM (stream, offset + coverage->offset);
954 READ_UINT16 (stream, coverage->CoverageFormat);
955 if (coverage->CoverageFormat == 1)
956 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
957 else if (coverage->CoverageFormat == 2)
958 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
960 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
963 coverage->Count = (unsigned) count;
964 RESTORE_STREAM (stream, state);
968 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
969 and store the Coverages there. If COUNT is negative, read the
970 number of Coverages at first. */
973 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
974 OTF_Coverage **coverage, int count)
976 char *errfmt = "Coverage List%s";
981 READ_UINT16 (stream, count);
984 OTF_MALLOC (*coverage, count, "");
985 for (i = 0; i < count; i++)
986 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
993 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
996 char *errfmt = "ClassDef%s";
999 SEEK_STREAM (stream, class->offset);
1000 READ_UINT16 (stream, class->ClassFormat);
1001 if (class->ClassFormat == 1)
1003 READ_GLYPHID (stream, class->f.f1.StartGlyph);
1004 class->f.f1.GlyphCount
1006 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
1007 if (! class->f.f1.GlyphCount)
1008 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1010 else if (class->ClassFormat == 2)
1012 class->f.f2.ClassRangeCount
1013 = (read_range_records
1014 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
1015 if (! class->f.f2.ClassRangeCount)
1016 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1019 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1025 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
1027 char *errfmt = "ClassDef%s";
1029 OTF_StreamState state;
1031 READ_OFFSET (stream, class->offset);
1032 if (! class->offset)
1034 SAVE_STREAM (stream, state);
1035 SEEK_STREAM (stream, offset + class->offset);
1036 READ_UINT16 (stream, class->ClassFormat);
1037 if (class->ClassFormat == 1)
1039 READ_GLYPHID (stream, class->f.f1.StartGlyph);
1040 class->f.f1.GlyphCount
1041 = read_glyph_ids (otf, stream,
1042 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
1044 if (! class->f.f1.GlyphCount)
1047 else if (class->ClassFormat == 2)
1049 class->f.f2.ClassRangeCount
1050 = read_range_records (otf, stream,
1051 (OTF_RangeRecord **)
1052 &class->f.f2.ClassRangeRecord);
1053 if (! class->f.f2.ClassRangeCount)
1057 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1059 RESTORE_STREAM (stream, state);
1065 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
1066 OTF_DeviceTable *table)
1068 char *errfmt = "Device Table%s";
1079 SEEK_STREAM (stream, offset + table->offset);
1080 READ_UINT16 (stream, table->StartSize);
1081 READ_UINT16 (stream, table->EndSize);
1082 READ_UINT16 (stream, table->DeltaFormat);
1083 num = table->EndSize - table->StartSize + 1;
1084 if (num > 0 && table->DeltaFormat >= 1 && table->DeltaFormat <= 3)
1086 OTF_MALLOC (table->DeltaValue, num, "");
1088 if (table->DeltaFormat == 1)
1089 for (i = 0; i < num; i++)
1092 READ_UINT16 (stream, val);
1093 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
1094 table->DeltaValue[i] = intval.int2;
1096 else if (table->DeltaFormat == 2)
1097 for (i = 0; i < num; i++)
1100 READ_UINT16 (stream, val);
1101 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
1102 table->DeltaValue[i] = intval.int4;
1104 else /* (table->DeltaFormat == 3) */
1105 for (i = 0; i < num; i++)
1109 READ_UINT16 (stream, val);
1110 intval.int8 = val >> 8;
1111 table->DeltaValue[i] = intval.int8;
1115 intval.int8 = val >> 8;
1116 table->DeltaValue[i] = intval.int8;
1122 /* Invalid DeltaFormat but several fonts has such values (bug of
1123 fontforge?). So accept it with NULL delta values. */
1124 table->DeltaValue = NULL;
1130 /*** (1-6) "GDEF" table */
1133 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1134 OTF_AttachList *list)
1136 char *errfmt = "AttachList%s";
1140 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1142 READ_UINT16 (stream, list->GlyphCount);
1143 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1144 for (i = 0; i < list->GlyphCount; i++)
1145 READ_OFFSET (stream, list->AttachPoint[i].offset);
1146 for (i = 0; i < list->GlyphCount; i++)
1150 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1151 READ_UINT16 (stream, count);
1152 list->AttachPoint[i].PointCount = count;
1153 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1154 for (j = 0; j < count; j++)
1155 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1161 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1162 OTF_CaretValue *caret)
1164 char *errfmt = "CaretValue%s";
1167 SEEK_STREAM (stream, offset + caret->offset);
1168 READ_UINT16 (stream, caret->CaretValueFormat);
1169 if (caret->CaretValueFormat == 1)
1170 READ_INT16 (stream, caret->f.f1.Coordinate);
1171 else if (caret->CaretValueFormat == 2)
1172 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1173 else if (caret->CaretValueFormat == 3)
1175 READ_INT16 (stream, caret->f.f3.Coordinate);
1176 if (read_device_table (otf, stream, offset + caret->offset,
1177 &caret->f.f3.DeviceTable) < 0)
1181 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1186 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1187 OTF_LigCaretList *list)
1189 char *errfmt = "LigCaretList%s";
1193 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1195 READ_UINT16 (stream, list->LigGlyphCount);
1196 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1197 for (i = 0; i < list->LigGlyphCount; i++)
1198 READ_OFFSET (stream, list->LigGlyph[i].offset);
1199 for (i = 0; i < list->LigGlyphCount; i++)
1203 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1204 READ_UINT16 (stream, count);
1205 list->LigGlyph[i].CaretCount = count;
1206 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1207 for (j = 0; j < count; j++)
1208 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1209 for (j = 0; j < count; j++)
1210 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1211 &list->LigGlyph[i].CaretValue[j]) < 0)
1218 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1222 READ_FIXED (stream, header->Version);
1223 READ_OFFSET (stream, header->GlyphClassDef);
1224 READ_OFFSET (stream, header->AttachList);
1225 READ_OFFSET (stream, header->LigCaretList);
1226 READ_OFFSET (stream, header->MarkAttachClassDef);
1231 read_gdef_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
1233 OTF_Stream *stream = table->stream;
1234 char *errfmt = "GDEF%s";
1235 void *errret = NULL;
1238 OTF_CALLOC (gdef, 1, "");
1241 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1242 if (gdef->header.GlyphClassDef)
1244 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1245 read_class_def_without_offset (otf, stream,
1246 &gdef->glyph_class_def);
1248 if (gdef->header.AttachList)
1249 read_attach_list (otf, stream, gdef->header.AttachList,
1250 &gdef->attach_list);
1251 if (gdef->header.LigCaretList)
1252 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1253 &gdef->lig_caret_list);
1254 if (gdef->header.MarkAttachClassDef)
1256 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1257 read_class_def_without_offset (otf, stream,
1258 &gdef->mark_attach_class_def);
1262 *table->address = gdef;
1267 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
1270 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
1271 OTF_ScriptList *list)
1273 char *errfmt = "Script List%s";
1277 SEEK_STREAM (stream, offset);
1278 READ_USHORT (stream, list->ScriptCount);
1279 OTF_CALLOC (list->Script, list->ScriptCount, "");
1281 for (i = 0; i < list->ScriptCount; i++)
1283 READ_TAG (stream, list->Script[i].ScriptTag);
1284 READ_OFFSET (stream, list->Script[i].offset);
1286 for (i = 0; i < list->ScriptCount; i++)
1288 OTF_Script *script = list->Script + i;
1289 long script_offset = offset + script->offset;
1291 SEEK_STREAM (stream, script_offset);
1292 READ_OFFSET (stream, script->DefaultLangSysOffset);
1293 READ_USHORT (stream, script->LangSysCount);
1294 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1295 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1296 for (j = 0; j < script->LangSysCount; j++)
1298 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1299 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1302 if (script->DefaultLangSysOffset)
1304 OTF_LangSys *langsys = &script->DefaultLangSys;
1306 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1307 READ_OFFSET (stream, langsys->LookupOrder);
1308 READ_USHORT (stream, langsys->ReqFeatureIndex);
1309 READ_USHORT (stream, langsys->FeatureCount);
1310 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1312 for (k = 0; k < langsys->FeatureCount; k++)
1313 READ_USHORT (stream, langsys->FeatureIndex[k]);
1316 for (j = 0; j < script->LangSysCount; j++)
1318 OTF_LangSys *langsys = script->LangSys + j;
1320 SEEK_STREAM (stream,
1321 script_offset + script->LangSysRecord[j].LangSys);
1322 READ_OFFSET (stream, langsys->LookupOrder);
1323 READ_USHORT (stream, langsys->ReqFeatureIndex);
1324 READ_USHORT (stream, langsys->FeatureCount);
1325 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1327 for (k = 0; k < langsys->FeatureCount; k++)
1328 READ_USHORT (stream, langsys->FeatureIndex[k]);
1336 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1337 OTF_FeatureList *list)
1339 char *errfmt = "Feature List%s";
1343 SEEK_STREAM (stream, offset);
1344 READ_UINT16 (stream, list->FeatureCount);
1345 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1346 for (i = 0; i < list->FeatureCount; i++)
1348 READ_TAG (stream, list->Feature[i].FeatureTag);
1349 READ_OFFSET (stream, list->Feature[i].offset);
1351 for (i = 0; i < list->FeatureCount; i++)
1353 OTF_Feature *feature = list->Feature + i;
1355 SEEK_STREAM (stream, offset + feature->offset);
1356 READ_OFFSET (stream, feature->FeatureParams);
1357 READ_UINT16 (stream, feature->LookupCount);
1358 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1359 " (LookupListIndex)");
1360 for (j = 0; j < feature->LookupCount; j++)
1361 READ_UINT16 (stream, feature->LookupListIndex[j]);
1367 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1368 long offset, unsigned type,
1369 OTF_LookupSubTableGSUB *subtable);
1370 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1371 long offset, unsigned type,
1372 OTF_LookupSubTableGPOS *subtable);
1375 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1376 OTF_LookupList *list, int gsubp)
1378 char *errfmt = "Lookup List%s";
1382 SEEK_STREAM (stream, offset);
1383 READ_UINT16 (stream, list->LookupCount);
1384 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1386 for (i = 0; i < list->LookupCount; i++)
1387 READ_OFFSET (stream, list->Lookup[i].offset);
1388 for (i = 0; i < list->LookupCount; i++)
1390 OTF_Lookup *lookup = list->Lookup + i;
1392 SEEK_STREAM (stream, offset + lookup->offset);
1393 READ_UINT16 (stream, lookup->LookupType);
1394 READ_UINT16 (stream, lookup->LookupFlag);
1395 READ_UINT16 (stream, lookup->SubTableCount);
1396 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1397 " (SubTableOffset)");
1399 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1402 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1404 for (j = 0; j < lookup->SubTableCount; j++)
1405 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1406 for (j = 0; j < lookup->SubTableCount; j++)
1409 = offset + lookup->offset + lookup->SubTableOffset[j];
1412 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1414 lookup->SubTable.gsub + j) < 0
1415 : read_lookup_subtable_gpos (otf, stream, this_offset,
1417 lookup->SubTable.gpos + j) < 0)
1426 /*** (1-8) Structures common to GSUB and GPOS */
1429 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1430 OTF_LookupRecord **record, int count)
1432 char *errfmt = "LookupRecord%s";
1433 unsigned errret = 0;
1437 READ_UINT16 (stream, count);
1439 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1440 OTF_MALLOC (*record, count, "");
1441 for (i = 0; i < count; i++)
1443 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1444 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1450 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1452 char *errfmt = "List of Rule%s";
1453 unsigned errret = 0;
1454 OTF_StreamState state;
1458 READ_UINT16 (stream, count);
1460 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1461 OTF_MALLOC (*rule, count, "");
1462 for (i = 0; i < count; i++)
1464 READ_OFFSET (stream, (*rule)[i].offset);
1465 if (! (*rule)[i].offset)
1466 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1468 SAVE_STREAM (stream, state);
1469 for (i = 0; i < count; i++)
1471 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1472 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1473 if ((*rule)[i].GlyphCount == 0)
1474 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1475 READ_UINT16 (stream, (*rule)[i].LookupCount);
1476 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1477 (*rule)[i].GlyphCount) < 0)
1479 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1480 (*rule)[i].LookupCount) == 0)
1483 RESTORE_STREAM (stream, state);
1489 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1492 char *errfmt = "List of RuleSet%s";
1493 unsigned errret = 0;
1494 OTF_StreamState state;
1498 READ_UINT16 (stream, count);
1500 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1501 OTF_MALLOC (*set, count, "");
1502 for (i = 0; i < count; i++)
1504 READ_OFFSET (stream, (*set)[i].offset);
1505 if (! (*set)[i].offset)
1506 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1508 SAVE_STREAM (stream, state);
1509 for (i = 0; i < count; i++)
1511 SEEK_STREAM (stream, offset + (*set)[i].offset);
1513 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1515 if (! (*set)[i].RuleCount)
1518 RESTORE_STREAM (stream, state);
1523 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1524 OTF_ClassRule **rule)
1526 char *errfmt = "ClassRule%s";
1527 unsigned errret = 0;
1528 OTF_StreamState state;
1532 READ_UINT16 (stream, count);
1534 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1535 OTF_MALLOC (*rule, count, "");
1536 for (i = 0; i < count; i++)
1538 READ_OFFSET (stream, (*rule)[i].offset);
1539 if (! (*rule)[i].offset)
1540 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1542 SAVE_STREAM (stream, state);
1543 for (i = 0; i < count; i++)
1545 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1546 READ_USHORT (stream, (*rule)[i].GlyphCount);
1547 if (! (*rule)[i].GlyphCount)
1548 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1549 READ_USHORT (stream, (*rule)[i].LookupCount);
1550 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1551 0, (*rule)[i].GlyphCount - 1) < 0)
1553 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1554 (*rule)[i].LookupCount) == 0)
1557 RESTORE_STREAM (stream, state);
1562 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1565 char *errfmt = "ClassSet%s";
1566 unsigned errret = 0;
1567 OTF_StreamState state;
1571 READ_UINT16 (stream, count);
1573 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1574 OTF_CALLOC (*set, count, "");
1575 for (i = 0; i < count; i++)
1576 /* Offset can be zero. */
1577 READ_OFFSET (stream, (*set)[i].offset);
1578 SAVE_STREAM (stream, state);
1579 for (i = 0; i < count; i++)
1580 if ((*set)[i].offset)
1582 SEEK_STREAM (stream, offset + (*set)[i].offset);
1583 (*set)[i].ClassRuleCnt
1584 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1585 &(*set)[i].ClassRule);
1586 if (! (*set)[i].ClassRuleCnt)
1589 RESTORE_STREAM (stream, state);
1594 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1595 OTF_ChainRule **rule)
1597 char *errfmt = "ChainRule%s";
1598 unsigned errret = 0;
1602 READ_UINT16 (stream, count);
1604 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1605 OTF_MALLOC (*rule, count, "");
1606 for (i = 0; i < count; i++)
1607 READ_OFFSET (stream, (*rule)[i].offset);
1608 for (i = 0; i < count; i++)
1610 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1611 (*rule)[i].BacktrackGlyphCount
1612 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1613 (*rule)[i].InputGlyphCount
1614 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1615 if (! (*rule)[i].InputGlyphCount)
1616 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1617 (*rule)[i].LookaheadGlyphCount
1618 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1619 (*rule)[i].LookupCount
1620 = read_lookup_record_list (otf, stream,
1621 &(*rule)[i].LookupRecord, -1);
1622 if (! (*rule)[i].LookupCount)
1630 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1631 OTF_ChainRuleSet **set)
1633 char *errfmt = "ChainRuleSet%s";
1634 unsigned errret = 0;
1635 OTF_StreamState state;
1639 READ_UINT16 (stream, count);
1641 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1642 OTF_MALLOC (*set, count, "");
1643 for (i = 0; i < count; i++)
1645 READ_OFFSET (stream, (*set)[i].offset);
1646 if (! (*set)[i].offset)
1647 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1649 SAVE_STREAM (stream, state);
1650 for (i = 0; i < count; i++)
1652 SEEK_STREAM (stream, offset + (*set)[i].offset);
1653 (*set)[i].ChainRuleCount
1654 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1655 &(*set)[i].ChainRule);
1656 if (! (*set)[i].ChainRuleCount)
1659 RESTORE_STREAM (stream, state);
1664 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1665 OTF_ChainClassRule **rule)
1667 char *errfmt = "ChainClassRule%s";
1668 unsigned errret = 0;
1672 READ_UINT16 (stream, count);
1674 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1675 OTF_MALLOC (*rule, count, "");
1676 for (i = 0; i < count; i++)
1678 READ_OFFSET (stream, (*rule)[i].offset);
1679 if (! (*rule)[i].offset)
1680 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1682 for (i = 0; i < count; i++)
1684 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1685 (*rule)[i].BacktrackGlyphCount
1686 = read_glyph_ids (otf, stream,
1687 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1688 (*rule)[i].InputGlyphCount
1689 = read_glyph_ids (otf, stream,
1690 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1691 if (! (*rule)[i].InputGlyphCount)
1692 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1693 (*rule)[i].LookaheadGlyphCount
1694 = read_glyph_ids (otf, stream,
1695 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1696 (*rule)[i].LookupCount
1697 = read_lookup_record_list (otf, stream,
1698 &(*rule)[i].LookupRecord, -1);
1699 if (! (*rule)[i].LookupCount)
1706 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1707 OTF_ChainClassSet **set)
1709 char *errfmt = "ChainClassSet%s";
1710 unsigned errret = 0;
1711 OTF_StreamState state;
1715 READ_UINT16 (stream, count);
1717 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1718 OTF_MALLOC (*set, count, "");
1719 for (i = 0; i < count; i++)
1720 /* Offset may be zero. */
1721 READ_OFFSET (stream, (*set)[i].offset);
1722 SAVE_STREAM (stream, state);
1723 for (i = 0; i < count; i++)
1724 if ((*set)[i].offset)
1726 SEEK_STREAM (stream, offset + (*set)[i].offset);
1727 (*set)[i].ChainClassRuleCnt
1728 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1729 &(*set)[i].ChainClassRule);
1730 if (! (*set)[i].ChainClassRuleCnt)
1733 RESTORE_STREAM (stream, state);
1738 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1739 OTF_Coverage *coverage,OTF_Context1 *context1)
1741 if (read_coverage (otf, stream, offset, coverage) < 0)
1743 context1->RuleSetCount
1744 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1745 if (! context1->RuleSetCount)
1751 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1752 OTF_Coverage *coverage,OTF_Context2 *context2)
1754 if (read_coverage (otf, stream, offset, coverage) < 0
1755 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1757 context2->ClassSetCnt
1758 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1759 if (! context2->ClassSetCnt)
1765 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1766 OTF_Coverage *coverage,OTF_Context3 *context3)
1768 char *errfmt = "Context1%s";
1771 READ_USHORT (stream, context3->GlyphCount);
1772 if (context3->GlyphCount < 0)
1773 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1774 READ_USHORT (stream, context3->LookupCount);
1775 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1776 context3->GlyphCount) < 0)
1778 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1779 context3->LookupCount) < 0)
1785 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1786 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1788 if (read_coverage (otf, stream, offset, coverage) < 0)
1790 chain_context1->ChainRuleSetCount
1791 = read_chain_rule_set_list (otf, stream, offset,
1792 &chain_context1->ChainRuleSet);
1793 if (! chain_context1->ChainRuleSetCount)
1799 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1800 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1802 if (read_coverage (otf, stream, offset, coverage) < 0
1803 || read_class_def (otf, stream, offset,
1804 &chain_context2->BacktrackClassDef) < 0
1805 || read_class_def (otf, stream, offset,
1806 &chain_context2->InputClassDef) < 0
1807 || read_class_def (otf, stream, offset,
1808 &chain_context2->LookaheadClassDef) < 0)
1810 chain_context2->ChainClassSetCnt
1811 = read_chain_class_set_list (otf, stream, offset,
1812 &chain_context2->ChainClassSet);
1813 if (! chain_context2->ChainClassSetCnt)
1819 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1820 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1824 count = read_coverage_list (otf, stream, offset,
1825 &chain_context3->Backtrack, -1);
1828 chain_context3->BacktrackGlyphCount = (unsigned) count;
1829 count = read_coverage_list (otf, stream, offset,
1830 &chain_context3->Input, -1);
1833 chain_context3->InputGlyphCount = (unsigned) count;
1834 *coverage = chain_context3->Input[0];
1835 count = read_coverage_list (otf, stream, offset,
1836 &chain_context3->LookAhead, -1);
1837 chain_context3->LookaheadGlyphCount = (unsigned) count;
1838 chain_context3->LookupCount
1839 = read_lookup_record_list (otf, stream,
1840 &chain_context3->LookupRecord, -1);
1845 read_gsub_gpos_table (OTF *otf, OTF_TableInfo *table, int gsubp,
1846 enum OTF_ReaderFlag flag)
1848 OTF_Stream *stream = table->stream;
1849 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1850 void *errret = NULL;
1851 OTF_GSUB_GPOS *gsub_gpos = *table->address;
1854 SEEK_STREAM (stream, 10);
1857 SEEK_STREAM (stream, 0);
1858 OTF_CALLOC (gsub_gpos, 1, "");
1859 READ_FIXED (stream, gsub_gpos->Version);
1860 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1861 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1862 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1863 *table->address = gsub_gpos;
1866 if (! gsub_gpos->ScriptList.Script
1867 && read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1868 &gsub_gpos->ScriptList) < 0)
1870 if (flag != OTF_READ_SCRIPTS)
1872 if (! gsub_gpos->FeatureList.Feature
1873 && read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1874 &gsub_gpos->FeatureList) < 0)
1876 if (flag != OTF_READ_FEATURES)
1878 if (! gsub_gpos->LookupList.Lookup
1879 && read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1880 &gsub_gpos->LookupList, gsubp) < 0)
1889 /* (1-9) "GSUB" table */
1892 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1894 char *errfmt = "Sequence%s";
1895 unsigned errret = 0;
1899 READ_UINT16 (stream, count);
1901 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1902 OTF_MALLOC (*seq, count, "");
1903 for (i = 0; i < count; i++)
1904 READ_OFFSET (stream, (*seq)[i].offset);
1905 for (i = 0; i < count; i++)
1907 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1908 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1909 &(*seq)[i].Substitute, 0, -1);
1910 if (! (*seq)[i].GlyphCount)
1917 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1918 OTF_Ligature **ligature)
1920 char *errfmt = "Ligature%s";
1925 READ_UINT16 (stream, count);
1928 OTF_MALLOC (*ligature, count, "");
1929 for (i = 0; i < count; i++)
1930 READ_OFFSET (stream, (*ligature)[i].offset);
1931 for (i = 0; i < count; i++)
1933 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1934 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1935 (*ligature)[i].CompCount
1936 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1937 if (! (*ligature)[i].CompCount)
1944 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1945 OTF_LigatureSet **ligset)
1947 char *errfmt = "LigatureSet%s";
1952 READ_UINT16 (stream, count);
1955 OTF_MALLOC (*ligset, count, "");
1956 for (i = 0; i < count; i++)
1957 READ_OFFSET (stream, (*ligset)[i].offset);
1958 for (i = 0; i < count; i++)
1962 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1963 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1964 &(*ligset)[i].Ligature);
1967 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1973 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1974 OTF_AlternateSet **altset)
1976 char *errfmt = "AlternateSet%s";
1981 READ_UINT16 (stream, count);
1983 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1984 OTF_MALLOC (*altset, count, "");
1985 for (i = 0; i < count; i++)
1986 READ_OFFSET (stream, (*altset)[i].offset);
1987 for (i = 0; i < count; i++)
1991 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1992 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1995 (*altset)[i].GlyphCount = (unsigned) alt_count;
2001 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
2002 OTF_Coverage *coverage,
2003 OTF_GSUB_ReverseChain1 *reverse_chain)
2007 if (read_coverage (otf, stream, offset, coverage) < 0)
2009 count = read_coverage_list (otf, stream, offset,
2010 &reverse_chain->Backtrack, -1);
2013 reverse_chain->BacktrackGlyphCount = (unsigned) count;
2014 count = read_coverage_list (otf, stream, offset,
2015 &reverse_chain->LookAhead, -1);
2018 reverse_chain->LookaheadGlyphCount = (unsigned) count;
2019 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
2022 reverse_chain->GlyphCount = count;
2027 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
2028 unsigned type, OTF_LookupSubTableGSUB *subtable)
2033 SEEK_STREAM (stream, offset);
2034 READ_USHORT (stream, subtable->Format);
2035 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
2039 if (subtable->Format == 1)
2041 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2043 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
2045 else if (subtable->Format == 2)
2047 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2049 subtable->u.single2.GlyphCount
2050 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
2052 if (! subtable->u.single2.GlyphCount)
2056 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2060 if (subtable->Format == 1)
2062 read_coverage (otf, stream, offset, &subtable->Coverage);
2063 subtable->u.multiple1.SequenceCount
2064 = read_sequence (otf, stream, offset,
2065 &subtable->u.multiple1.Sequence);
2068 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2072 if (subtable->Format == 1)
2074 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2076 subtable->u.alternate1.AlternateSetCount
2077 = read_alternate_set_list (otf, stream, offset,
2078 &subtable->u.alternate1.AlternateSet);
2079 if (! subtable->u.alternate1.AlternateSetCount)
2083 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2087 if (subtable->Format == 1)
2089 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2091 subtable->u.ligature1.LigSetCount
2092 = read_ligature_set_list (otf, stream, offset,
2093 &subtable->u.ligature1.LigatureSet);
2094 if (! subtable->u.ligature1.LigSetCount)
2098 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2102 if (subtable->Format == 1)
2104 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2105 &subtable->u.context1) < 0)
2108 else if (subtable->Format == 2)
2110 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2111 &subtable->u.context2) < 0)
2114 else if (subtable->Format == 3)
2116 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2117 &subtable->u.context3) < 0)
2121 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2125 if (subtable->Format == 1)
2127 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2128 &subtable->u.chain_context1) < 0)
2131 else if (subtable->Format == 2)
2133 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2134 &subtable->u.chain_context2) < 0)
2137 else if (subtable->Format == 3)
2139 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2140 &subtable->u.chain_context3) < 0)
2144 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2148 if (subtable->Format == 1)
2152 OTF_LookupSubTableGSUB *ex_subtable;
2154 READ_USHORT (stream, ex_type);
2155 READ_ULONG (stream, ex_offset);
2156 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2157 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
2158 ex_type, ex_subtable) < 0)
2160 subtable->u.extension1.ExtensionLookupType = ex_type;
2161 subtable->u.extension1.ExtensionOffset = ex_offset;
2162 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2165 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2169 if (subtable->Format == 1)
2171 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
2172 &subtable->u.reverse_chain1) < 0)
2176 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2180 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2186 read_gsub_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2188 return read_gsub_gpos_table (otf, table, 1, flag);
2192 /* (1-10) "GPOS" table */
2195 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
2196 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
2199 OTF_StreamState state;
2202 memset (value_record, 0, sizeof (OTF_ValueRecord));
2205 for (i = 0, size = 0; i < 8; i++)
2209 if (bit & OTF_XPlacement)
2210 READ_INT16 (stream, value_record->XPlacement);
2211 if (bit & OTF_YPlacement)
2212 READ_INT16 (stream, value_record->YPlacement);
2213 if (bit & OTF_XAdvance)
2214 READ_INT16 (stream, value_record->XAdvance);
2215 if (bit & OTF_YAdvance)
2216 READ_INT16 (stream, value_record->YAdvance);
2217 if (bit & OTF_XPlaDevice)
2218 READ_OFFSET (stream, value_record->XPlaDevice.offset);
2219 if (bit & OTF_YPlaDevice)
2220 READ_OFFSET (stream, value_record->YPlaDevice.offset);
2221 if (bit & OTF_XAdvDevice)
2222 READ_OFFSET (stream, value_record->XAdvDevice.offset);
2223 if (bit & OTF_YAdvDevice)
2224 READ_OFFSET (stream, value_record->YAdvDevice.offset);
2225 SAVE_STREAM (stream, state);
2226 if (value_record->XPlaDevice.offset)
2228 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
2231 if (value_record->YPlaDevice.offset)
2233 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
2236 if (value_record->XAdvDevice.offset)
2238 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
2241 if (value_record->YAdvDevice.offset)
2243 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
2246 RESTORE_STREAM (stream, state);
2252 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
2254 char *errfmt = "Anchor%s";
2257 SEEK_STREAM (stream, offset + anchor->offset);
2258 READ_UINT16 (stream, anchor->AnchorFormat);
2259 READ_INT16 (stream, anchor->XCoordinate);
2260 READ_INT16 (stream, anchor->YCoordinate);
2261 if (anchor->AnchorFormat == 1)
2263 else if (anchor->AnchorFormat == 2)
2265 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
2267 else if (anchor->AnchorFormat == 3)
2269 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
2270 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
2271 if (anchor->f.f2.XDeviceTable.offset)
2273 if (read_device_table (otf, stream, offset + anchor->offset,
2274 &anchor->f.f2.XDeviceTable) < 0)
2277 if (anchor->f.f2.YDeviceTable.offset)
2279 if (read_device_table (otf, stream, offset + anchor->offset,
2280 &anchor->f.f2.YDeviceTable) < 0)
2285 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
2291 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
2292 OTF_MarkArray *array)
2294 char *errfmt = "MarkArray%s";
2296 OTF_StreamState state;
2299 READ_OFFSET (stream, array->offset);
2300 SAVE_STREAM (stream, state);
2301 SEEK_STREAM (stream, offset + array->offset);
2302 READ_UINT16 (stream, array->MarkCount);
2303 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
2304 for (i = 0; i < array->MarkCount; i++)
2306 READ_UINT16 (stream, array->MarkRecord[i].Class);
2307 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
2309 for (i = 0; i < array->MarkCount; i++)
2310 if (read_anchor (otf, stream, offset + array->offset,
2311 &array->MarkRecord[i].MarkAnchor) < 0)
2313 RESTORE_STREAM (stream, state);
2318 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2319 unsigned ClassCount, OTF_AnchorArray *array)
2321 char *errfmt = "AnchorArray%s";
2323 OTF_StreamState state;
2326 READ_OFFSET (stream, array->offset);
2327 SAVE_STREAM (stream, state);
2328 SEEK_STREAM (stream, offset + array->offset);
2329 READ_UINT16 (stream, array->Count);
2330 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2331 for (i = 0; i < array->Count; i++)
2333 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2335 for (j = 0; j < ClassCount; j++)
2336 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2338 for (i = 0; i < array->Count; i++)
2339 for (j = 0; j < ClassCount; j++)
2340 if (read_anchor (otf, stream, offset + array->offset,
2341 &array->AnchorRecord[i].Anchor[j]) < 0)
2343 RESTORE_STREAM (stream, state);
2347 static OTF_PairSet *
2348 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2349 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2351 char *errfmt = "PairSet%s";
2352 void *errret = NULL;
2353 OTF_StreamState state;
2357 OTF_MALLOC (set, num, "");
2358 for (i = 0; i < num; i++)
2359 READ_OFFSET (stream, set[i].offset);
2360 SAVE_STREAM (stream, state);
2361 for (i = 0; i < num; i++)
2363 SEEK_STREAM (stream, offset + set[i].offset);
2364 READ_UINT16 (stream, set[i].PairValueCount);
2365 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2366 for (j = 0; j < set[i].PairValueCount; j++)
2368 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2370 READ_UINT16 (stream, rec->SecondGlyph);
2371 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2372 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2375 RESTORE_STREAM (stream, state);
2379 static OTF_Class1Record *
2380 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2381 unsigned num1, enum OTF_ValueFormat bit1,
2382 unsigned num2, enum OTF_ValueFormat bit2)
2384 char *errfmt = "Class1Record%s";
2385 void *errret = NULL;
2386 OTF_Class1Record *rec;
2389 OTF_MALLOC (rec, num1, "");
2390 for (i = 0; i < num1; i++)
2392 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2393 for (j = 0; j < num2; j++)
2395 if (read_value_record (otf, stream, offset,
2396 bit1, &rec[i].Class2Record[j].Value1) < 0
2397 || read_value_record (otf, stream, offset,
2398 bit2, &rec[i].Class2Record[j].Value2) < 0)
2406 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2407 OTF_EntryExitRecord **rec)
2409 char *errfmt = "EntryExitSet%s";
2413 OTF_StreamState state;
2415 READ_UINT16 (stream, count);
2417 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2418 OTF_MALLOC (*rec, count, "");
2419 for (i = 0; i < count; i++)
2421 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2422 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2424 SAVE_STREAM (stream, state);
2425 for (i = 0; i < count; i++)
2427 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2429 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2432 RESTORE_STREAM (stream, state);
2437 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2438 unsigned ClassCount, OTF_LigatureAttach *attach)
2440 char *errfmt = "LigatureAttach%s";
2444 SEEK_STREAM (stream, offset + attach->offset);
2445 READ_UINT16 (stream, attach->ComponentCount);
2446 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2447 for (i = 0; i < attach->ComponentCount; i++)
2449 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2450 " (ComponentRecord)");
2451 for (j = 0; j < ClassCount; j++)
2452 READ_OFFSET (stream,
2453 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2455 for (i = 0; i < attach->ComponentCount; i++)
2456 for (j = 0; j < ClassCount; j++)
2458 if (attach->ComponentRecord[i].LigatureAnchor[j].offset)
2460 if (read_anchor (otf, stream, offset + attach->offset,
2461 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2465 attach->ComponentRecord[i].LigatureAnchor[j].AnchorFormat = 0;
2471 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2472 unsigned class_count, OTF_LigatureArray *array)
2474 char *errfmt = "LigatureArray%s";
2476 OTF_StreamState state;
2479 READ_OFFSET (stream, array->offset);
2480 SAVE_STREAM (stream, state);
2481 SEEK_STREAM (stream, offset + array->offset);
2482 READ_UINT16 (stream, array->LigatureCount);
2483 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2484 for (i = 0; i < array->LigatureCount; i++)
2485 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2486 for (i = 0; i < array->LigatureCount; i++)
2487 read_ligature_attach (otf, stream, offset + array->offset,
2488 class_count, array->LigatureAttach + i);
2489 RESTORE_STREAM (stream, state);
2494 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2495 long offset, unsigned type,
2496 OTF_LookupSubTableGPOS *subtable)
2501 SEEK_STREAM (stream, offset);
2502 READ_UINT16 (stream, subtable->Format);
2503 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2507 if (subtable->Format == 1)
2509 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2511 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2512 read_value_record (otf, stream, offset,
2513 subtable->u.single1.ValueFormat,
2514 &subtable->u.single1.Value);
2516 else if (subtable->Format == 2)
2518 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2521 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2523 READ_UINT16 (stream, single2->ValueFormat);
2524 READ_UINT16 (stream, single2->ValueCount);
2525 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2526 for (i = 0; i < single2->ValueCount; i++)
2527 read_value_record (otf, stream, offset, single2->ValueFormat,
2528 single2->Value + i);
2531 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2535 if (subtable->Format == 1)
2537 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2539 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2540 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2541 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2542 subtable->u.pair1.PairSet
2543 = read_pair_set_list (otf, stream, offset,
2544 subtable->u.pair1.PairSetCount,
2545 subtable->u.pair1.ValueFormat1,
2546 subtable->u.pair1.ValueFormat2);
2547 if (! subtable->u.pair1.PairSet)
2550 else if (subtable->Format == 2)
2552 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2554 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2555 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2556 if (read_class_def (otf, stream, offset,
2557 &subtable->u.pair2.ClassDef1) < 0
2558 || read_class_def (otf, stream, offset,
2559 &subtable->u.pair2.ClassDef2) < 0)
2561 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2562 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2563 subtable->u.pair2.Class1Record
2564 = read_class1_record_list (otf, stream, offset,
2565 subtable->u.pair2.Class1Count,
2566 subtable->u.pair2.ValueFormat1,
2567 subtable->u.pair2.Class2Count,
2568 subtable->u.pair2.ValueFormat2);
2569 if (! subtable->u.pair2.Class1Record)
2573 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2577 if (subtable->Format == 1)
2579 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2581 subtable->u.cursive1.EntryExitCount
2582 = read_entry_exit_list (otf, stream, offset,
2583 &subtable->u.cursive1.EntryExitRecord);
2584 if (! subtable->u.cursive1.EntryExitCount)
2588 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2592 if (subtable->Format == 1)
2594 read_coverage (otf, stream, offset, &subtable->Coverage);
2595 read_coverage (otf, stream, offset,
2596 &subtable->u.mark_base1.BaseCoverage);
2597 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2598 read_mark_array (otf, stream, offset,
2599 &subtable->u.mark_base1.MarkArray);
2600 read_anchor_array (otf, stream, offset,
2601 subtable->u.mark_base1.ClassCount,
2602 &subtable->u.mark_base1.BaseArray);
2605 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2609 if (subtable->Format == 1)
2611 read_coverage (otf, stream, offset, &subtable->Coverage);
2612 read_coverage (otf, stream, offset,
2613 &subtable->u.mark_lig1.LigatureCoverage);
2614 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2615 read_mark_array (otf, stream, offset,
2616 &subtable->u.mark_lig1.MarkArray);
2617 read_ligature_array (otf, stream, offset,
2618 subtable->u.mark_lig1.ClassCount,
2619 &subtable->u.mark_lig1.LigatureArray);
2624 if (subtable->Format == 1)
2626 read_coverage (otf, stream, offset, &subtable->Coverage);
2627 read_coverage (otf, stream, offset,
2628 &subtable->u.mark_mark1.Mark2Coverage);
2629 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2630 read_mark_array (otf, stream, offset,
2631 &subtable->u.mark_mark1.Mark1Array);
2632 read_anchor_array (otf, stream, offset,
2633 subtable->u.mark_mark1.ClassCount,
2634 &subtable->u.mark_mark1.Mark2Array);
2637 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2641 if (subtable->Format == 1)
2643 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2644 &subtable->u.context1) < 0)
2647 else if (subtable->Format == 2)
2649 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2650 &subtable->u.context2) < 0)
2653 else if (subtable->Format == 3)
2655 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2656 &subtable->u.context3) < 0)
2660 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2664 if (subtable->Format == 1)
2666 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2667 &subtable->u.chain_context1) < 0)
2670 else if (subtable->Format == 2)
2672 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2673 &subtable->u.chain_context2) < 0)
2676 else if (subtable->Format == 3)
2678 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2679 &subtable->u.chain_context3) < 0)
2683 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2687 if (subtable->Format == 1)
2691 OTF_LookupSubTableGPOS *ex_subtable;
2693 READ_USHORT (stream, ex_type);
2694 READ_ULONG (stream, ex_offset);
2695 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2696 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2697 ex_type, ex_subtable) < 0)
2699 subtable->u.extension1.ExtensionLookupType = ex_type;
2700 subtable->u.extension1.ExtensionOffset = ex_offset;
2701 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2704 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2708 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2714 read_gpos_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2716 return read_gsub_gpos_table (otf, table, 0, flag);
2724 read_base_table (OTF_Stream *stream, long offset)
2728 OTF_MALLOC (base, 1);
2737 read_jstf_table (OTF_Stream *stream, long offset)
2741 OTF_MALLOC (jstf, 1);
2747 /*** (1-11) Structure for OTF */
2750 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2754 READ_FIXED (stream, table->sfnt_version);
2755 READ_USHORT (stream, table->numTables);
2756 READ_USHORT (stream, table->searchRange);
2757 READ_USHORT (stream, table->enterSelector);
2758 READ_USHORT (stream, table->rangeShift);
2763 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2768 READ_TAG (stream, tag);
2770 table->name[0] = tag >> 24;
2771 table->name[1] = (tag >> 16) & 0xFF;
2772 table->name[2] = (tag >> 8) & 0xFF;
2773 table->name[3] = tag & 0xFF;
2774 table->name[4] = '\0';
2775 READ_ULONG (stream, table->checkSum);
2776 READ_ULONG (stream, table->offset);
2777 READ_ULONG (stream, table->length);
2782 read_header_part (OTF *otf, FILE *fp, FT_Face face)
2784 char *errfmt = "otf header%s";
2787 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2789 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2790 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2791 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2792 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2793 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2794 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2795 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2796 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2797 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2798 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2799 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2800 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2804 OTF_Tag head_tag = OTF_tag ("head");
2805 OTF_Tag name_tag = OTF_tag ("name");
2806 OTF_Tag cmap_tag = OTF_tag ("cmap");
2807 OTF_Tag gdef_tag = OTF_tag ("GDEF");
2808 OTF_Tag gsub_tag = OTF_tag ("GSUB");
2809 OTF_Tag gpos_tag = OTF_tag ("GPOS");
2810 OTF_Stream *stream = make_stream ("Offset Table");
2814 internal_data->header_stream = stream;
2816 /* Size of Offset Table is 12 bytes. */
2817 if (setup_stream (stream, fp, 0, 12) < 0)
2819 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2822 /* Size of each Table Directory is 16 bytes. */
2823 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables) < 0)
2826 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables,
2828 for (i = 0; i < otf->offset_table.numTables; i++)
2830 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2831 OTF_TableInfo *table_info = NULL;
2835 if (tag == head_tag)
2836 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2837 else if (tag == name_tag)
2838 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2839 else if (tag == cmap_tag)
2840 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2841 else if (tag == gdef_tag)
2842 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2843 else if (tag == gsub_tag)
2844 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2845 else if (tag == gpos_tag)
2846 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2850 table_info->stream = make_stream (otf->table_dirs[i].name);
2851 if (setup_stream (table_info->stream, fp,
2852 otf->table_dirs[i].offset,
2853 otf->table_dirs[i].length) < 0)
2858 internal_data->header_stream = NULL;
2859 free_stream (stream);
2865 internal_data->header_stream = NULL;
2866 if ((stream = make_stream_from_ft_face (face, "head")))
2867 internal_data->table_info[OTF_TABLE_TYPE_HEAD].stream = stream;
2868 if ((stream = make_stream_from_ft_face (face, "name")))
2869 internal_data->table_info[OTF_TABLE_TYPE_NAME].stream = stream;
2870 if ((stream = make_stream_from_ft_face (face, "cmap")))
2871 internal_data->table_info[OTF_TABLE_TYPE_CMAP].stream = stream;
2872 if ((stream = make_stream_from_ft_face (face, "GDEF")))
2873 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream = stream;
2874 if ((stream = make_stream_from_ft_face (face, "GSUB")))
2875 internal_data->table_info[OTF_TABLE_TYPE_GSUB].stream = stream;
2876 if ((stream = make_stream_from_ft_face (face, "GPOS")))
2877 internal_data->table_info[OTF_TABLE_TYPE_GPOS].stream = stream;
2880 if (! internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream)
2881 /* We can simulate the GDEF table. */
2882 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream
2883 = make_stream ("GDEF");
2887 static OTF_TableInfo *
2888 get_table_info (OTF *otf, const char *name)
2890 char *errfmt = "OTF Table Read%s";
2891 OTF_TableInfo *errret = NULL;
2892 OTF_InternalData *internal_data = otf->internal_data;
2893 OTF_TableInfo *table_info;
2894 OTF_Tag tag = OTF_tag (name);
2897 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2899 if (tag == OTF_tag ("head"))
2900 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2901 else if (tag == OTF_tag ("name"))
2902 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2903 else if (tag == OTF_tag ("cmap"))
2904 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2905 else if (tag == OTF_tag ("GDEF"))
2906 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2907 else if (tag == OTF_tag ("GSUB"))
2908 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2909 else if (tag == OTF_tag ("GPOS"))
2910 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2912 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2914 if (*table_info->address)
2917 if (! table_info->stream)
2918 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2919 if (! table_info->reader)
2920 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2926 /*** (2) API for reading OTF */
2928 /*** (2-1) OTF_open() */
2930 /* Note: We can't use memory allocation macros in the following
2931 functions because those macros return from the functions before
2932 freeing memory previously allocated. */
2935 OTF_open (const char *otf_name)
2938 char *errfmt = "opening otf (%s)";
2939 void *errret = NULL;
2941 OTF_InternalData *internal_data;
2942 int len = strlen (otf_name);
2943 const char *ext = otf_name + (len - 4);
2950 || (ext[1] != 'O' && ext[1] != 'T' && ext[1] != 'o' && ext[1] != 't')
2951 || (ext[2] != 'T' && ext[2] != 't')
2952 || (ext[3] != 'F' && ext[3] != 'f'))
2953 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2954 fp = fopen (otf_name, "r");
2956 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2957 otf = calloc (1, sizeof (OTF));
2959 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2960 otf->filename = strdup (otf_name);
2961 if (! otf->filename)
2965 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2968 internal_data = calloc (1, sizeof (OTF_InternalData));
2969 if (! internal_data)
2970 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2971 otf->internal_data = internal_data;
2972 if (! allocate_memory_record (otf))
2973 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2975 /* Here after, all pointers to allocated memory are recorded in
2976 otf->internal_data->memory_record except for what allocated by
2977 the functions allocate_memory_record and make_stream. */
2979 if (read_header_part (otf, fp, NULL) < 0)
2991 OTF_open_ft_face (FT_Face face)
2993 char *errfmt = "opening otf from Freetype (%s)";
2994 void *errret = NULL;
2996 OTF_InternalData *internal_data;
3001 if (! FT_IS_SFNT (face))
3002 OTF_ERROR (OTF_ERROR_FILE, (char *) face->family_name);
3003 otf = calloc (1, sizeof (OTF));
3005 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
3006 otf->filename = NULL;
3008 internal_data = calloc (1, sizeof (OTF_InternalData));
3009 if (! internal_data)
3010 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
3011 otf->internal_data = internal_data;
3012 if (! allocate_memory_record (otf))
3013 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
3015 if (read_header_part (otf, NULL, face) < 0)
3024 /*** (2-2) OTF_close() */
3027 OTF_close (OTF *otf)
3029 OTF_InternalData *internal_data = otf->internal_data;
3034 OTF_MemoryRecord *memrec = internal_data->memory_record;
3035 OTF_ApplicationData *app_data = internal_data->app_data;
3037 if (internal_data->header_stream)
3038 free_stream (internal_data->header_stream);
3040 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
3041 if (internal_data->table_info[i].stream)
3042 free_stream (internal_data->table_info[i].stream);
3044 for (; app_data; app_data = app_data->next)
3045 if (app_data->data && app_data->freer)
3046 app_data->freer (app_data->data);
3050 OTF_MemoryRecord *next = memrec->next;
3052 for (i = memrec->used - 1; i >= 0; i--)
3053 free (memrec->memory[i]);
3058 free (internal_data);
3061 free (otf->filename);
3065 /*** (2-3) OTF_get_table() */
3068 OTF_get_table (OTF *otf, const char *name)
3070 OTF_TableInfo *table_info = get_table_info (otf, name);
3075 if (! table_info->stream)
3076 /* Already fully loaded. */
3079 address = (*table_info->reader) (otf, table_info, OTF_READ_FULL);
3080 free_stream (table_info->stream);
3081 table_info->stream = NULL;
3084 table_info->reader = NULL;
3090 /*** (2-4) OTF_check_table() */
3093 OTF_check_table (OTF *otf, const char *name)
3095 return (get_table_info (otf, name) ? 0 : -1);
3098 /*** (2-5) OTF_get_scripts() */
3101 OTF_get_scripts (OTF *otf, int gsubp)
3103 OTF_TableInfo *table_info
3104 = (otf->internal_data->table_info
3105 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3108 if (! table_info->reader)
3110 if (! table_info->stream)
3111 /* Already fully loaded. */
3114 address = (*table_info->reader) (otf, table_info, OTF_READ_SCRIPTS);
3117 table_info->reader = NULL;
3123 /*** (2-6) OTF_get_features() */
3126 OTF_get_features (OTF *otf, int gsubp)
3128 OTF_TableInfo *table_info
3129 = (otf->internal_data->table_info
3130 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3133 if (! table_info->reader)
3135 if (! table_info->stream)
3137 if (*table_info->address)
3138 /* Already fully loaded. */
3143 address = (*table_info->reader) (otf, table_info, OTF_READ_FEATURES);
3146 table_info->reader = NULL;
3152 /*** (2-7) OTF_check_features */
3155 OTF_check_features (OTF *otf, int gsubp,
3156 OTF_Tag script, OTF_Tag language, const OTF_Tag *features,
3159 OTF_ScriptList *script_list;
3160 OTF_Script *Script = NULL;
3161 OTF_LangSys *LangSys = NULL;
3162 OTF_FeatureList *feature_list;
3165 if (OTF_get_features (otf, gsubp) < 0)
3167 for (i = 0; i < n_features; i++)
3169 OTF_Tag feature = features[i];
3173 if ((((unsigned) feature) & 0x80000000) == 0)
3180 script_list = &otf->gsub->ScriptList;
3181 feature_list = &otf->gsub->FeatureList;
3185 script_list = &otf->gpos->ScriptList;
3186 feature_list = &otf->gpos->FeatureList;
3188 for (i = 0; i < script_list->ScriptCount && ! Script; i++)
3189 if (script_list->Script[i].ScriptTag == script)
3190 Script = script_list->Script + i;
3195 for (i = 0; i < Script->LangSysCount && ! LangSys; i++)
3196 if (Script->LangSysRecord[i].LangSysTag == language)
3197 LangSys = Script->LangSys + i;
3200 LangSys = &Script->DefaultLangSys;
3201 for (j = 0; j < n_features; j++)
3203 OTF_Tag feature = features[j];
3208 if (((unsigned) feature) & 0x80000000)
3210 feature = (OTF_Tag) (((unsigned) feature) & 0x7FFFFFFF);
3213 for (i = 0; i < LangSys->FeatureCount; i++)
3214 if (feature_list->Feature[LangSys->FeatureIndex[i]].FeatureTag
3221 if (i == LangSys->FeatureCount)
3228 /*** (5) API miscellaneous ***/
3231 OTF_tag (const char *name)
3233 const unsigned char *p = (unsigned char *) name;
3237 return (OTF_Tag) ((p[0] << 24)
3241 : (p[2] << 8) | p[3]))));
3245 OTF_tag_name (OTF_Tag tag, char *name)
3247 name[0] = (char) (tag >> 24);
3248 name[1] = (char) ((tag >> 16) & 0xFF);
3249 name[2] = (char) ((tag >> 8) & 0xFF);
3250 name[3] = (char) (tag & 0xFF);
3255 OTF_put_data (OTF *otf, char *id, void *data, void (*freer) (void *data))
3257 char *errfmt = "appdata %s";
3259 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3260 OTF_ApplicationData *app_data = internal_data->app_data;
3261 int len = strlen (id) + 1;
3263 for (; app_data; app_data = app_data->next)
3264 if (memcmp (app_data->id, id, len) == 0)
3266 if (app_data->data && app_data->freer)
3267 app_data->freer (app_data->data);
3272 OTF_MALLOC (app_data, sizeof (OTF_ApplicationData), id);
3273 app_data->next = internal_data->app_data;
3274 internal_data->app_data = app_data;
3275 OTF_MALLOC (app_data->id, len, id);
3276 memcpy (app_data->id, id, len);
3278 app_data->data = data;
3279 app_data->freer = freer;
3284 OTF_get_data (OTF *otf, char *id)
3286 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3287 OTF_ApplicationData *app_data = internal_data->app_data;
3289 for (; app_data; app_data = app_data->next)
3290 if (strcmp (app_data->id, id) == 0)
3291 return app_data->data;