1 /* otfopen.c -- OpenType font reader.
3 Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010
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;
336 static OTF_MemoryRecord *
337 allocate_memory_record (OTF *otf)
339 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
340 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
345 memrec->next = internal_data->memory_record;
346 internal_data->memory_record = memrec;
350 /* Memory allocation macros. */
352 #define OTF_MALLOC(p, size, arg) \
358 OTF_MemoryRecord *memrec \
359 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
360 (p) = malloc (sizeof (*(p)) * (size)); \
362 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
363 && ! (memrec = allocate_memory_record (otf)))) \
364 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
365 memrec->memory[memrec->used++] = (p); \
370 #define OTF_CALLOC(p, size, arg) \
376 OTF_MemoryRecord *memrec \
377 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
378 (p) = calloc ((size), sizeof (*(p))); \
380 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
381 && ! (memrec = allocate_memory_record (otf)))) \
382 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
383 memrec->memory[memrec->used++] = (p); \
388 /*** (1-2) "head" table */
391 read_head_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
393 OTF_Stream *stream = table->stream;
394 char *errfmt = "head%s";
398 OTF_CALLOC (head, 1, "");
399 READ_FIXED (stream, head->TableVersionNumber);
400 READ_FIXED (stream, head->fontRevision);
401 READ_ULONG (stream, head->checkSumAdjustment);
402 READ_ULONG (stream, head->magicNumber);
403 READ_USHORT (stream, head->flags);
404 READ_USHORT (stream, head->unitsPerEm);
406 *table->address = head;
411 /*** (1-3) "name" table */
414 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
418 OTF_StreamState state;
423 sprintf (errfmt, "nameID (%d)%%s", rec->nameID);
424 if (rec->platformID == 0)
425 ucs = (rec->encodingID <= 3) ? 2 : 4;
426 else if (rec->platformID == 1 && rec->encodingID == 0)
428 else if (rec->platformID == 3)
429 ucs = (rec->encodingID == 1 ? 2
430 : rec->encodingID == 10 ? 4
433 OTF_MALLOC (rec->name, rec->length + 1, "");
434 SAVE_STREAM (stream, state);
435 SEEK_STREAM (stream, stream->pos + rec->offset);
436 READ_BYTES (stream, rec->name, rec->length);
437 RESTORE_STREAM (stream, state);
438 rec->name[rec->length] = 0;
447 for (i = 0; i < rec->length / 2; i++)
449 if (rec->name[i * 2] > 0
450 || rec->name[i * 2 + 1] >= 128)
457 for (i = 0; i < rec->length / 2; i++)
458 rec->name[i] = rec->name[i * 2 + 1];
464 for (i = 0; i < rec->length / 4; i++)
466 if (rec->name[i * 4] > 0
467 || rec->name[i * 4 + 1] > 0
468 || rec->name[i * 4 + 2] > 0
469 || rec->name[i * 2 + 3] >= 128)
476 for (i = 0; i < rec->length / 4; i++)
477 rec->name[i] = rec->name[i * 4 + 3];
485 read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
487 OTF_Stream *stream = table->stream;
488 char *errfmt = "name%s";
493 OTF_CALLOC (name, 1, "");
494 READ_USHORT (stream, name->format);
495 READ_USHORT (stream, name->count);
496 READ_USHORT (stream, name->stringOffset);
497 OTF_MALLOC (name->nameRecord, name->count, "");
498 for (i = 0; i < name->count; i++)
500 OTF_NameRecord *rec = name->nameRecord + i;
502 READ_USHORT (stream, rec->platformID);
503 READ_USHORT (stream, rec->encodingID);
504 READ_USHORT (stream, rec->languageID);
505 READ_USHORT (stream, rec->nameID);
506 READ_USHORT (stream, rec->length);
507 READ_USHORT (stream, rec->offset);
509 for (i = 0; i < name->count; i++)
511 OTF_NameRecord *rec = name->nameRecord + i;
512 int nameID = rec->nameID;
514 read_name (otf, stream, rec);
516 if (nameID >= OTF_max_nameID)
518 if (! name->name[nameID]
520 name->name[nameID] = (char *) rec->name;
523 *table->address = name;
528 /*** (1-4) "cmap" table */
530 static OTF_EncodingSubtable14 *
531 read_cmap_uvs_table (OTF *otf, OTF_Stream *stream, OTF_Offset offset)
533 OTF_EncodingSubtable14 *sub14;
534 char *errfmt = "cmap-uvs%s";
539 OTF_MALLOC (sub14, 1, " (EncodingSubtable14)");
540 READ_ULONG (stream, nRecords);
541 sub14->nRecords = nRecords;
542 OTF_MALLOC (sub14->Records, nRecords, "(EncodingSubtable14-Records)");
543 for (i = 0; i < sub14->nRecords; i++)
545 unsigned varSelector=0, defaultUVSOffset, nonDefaultUVSOffset;
547 READ_UINT24 (stream, varSelector);
548 sub14->Records[i].varSelector = varSelector;
549 READ_ULONG (stream, defaultUVSOffset);
550 sub14->Records[i].defaultUVSOffset = defaultUVSOffset;
551 READ_ULONG (stream, nonDefaultUVSOffset);
552 sub14->Records[i].nonDefaultUVSOffset = nonDefaultUVSOffset;
554 for (i = 0; i < sub14->nRecords; i++)
556 OTF_VariationSelectorRecord *record = &sub14->Records[i];
557 unsigned defaultUVSOffset = record->defaultUVSOffset;
558 unsigned nonDefaultUVSOffset = record->nonDefaultUVSOffset;
560 if (defaultUVSOffset)
562 unsigned numUnicodeValueRanges;
564 SEEK_STREAM (stream, offset+defaultUVSOffset);
565 READ_ULONG (stream, numUnicodeValueRanges);
566 record->numUnicodeValueRanges = numUnicodeValueRanges;
567 OTF_MALLOC (record->unicodeValueRanges,
568 numUnicodeValueRanges,
569 "(EncodingSubtable14-Records-unicodeValueRanges)");
570 for (j = 0; j < numUnicodeValueRanges; j++)
572 OTF_UnicodeValueRange *unicodeValueRange
573 = &record->unicodeValueRanges[j];
574 unsigned startUnicodeValue;
575 char additionalCount;
577 READ_UINT24 (stream, startUnicodeValue);
578 unicodeValueRange->startUnicodeValue=startUnicodeValue;
579 READ_BYTES (stream, &additionalCount, 1);
580 unicodeValueRange->additionalCount
581 = (unsigned short) additionalCount;
584 if (nonDefaultUVSOffset)
586 unsigned numUVSMappings;
588 SEEK_STREAM (stream, offset+nonDefaultUVSOffset);
589 READ_ULONG (stream, numUVSMappings);
590 record->numUVSMappings = numUVSMappings;
591 OTF_MALLOC (record->uvsMappings, numUVSMappings,
592 "(EncodingSubtable14-Records-uvsMappings)");
593 for (j = 0; j < numUVSMappings; j++)
595 OTF_UVSMapping *uvsMapping = &record->uvsMappings[j];
596 unsigned unicodeValue;
597 unsigned short glyphID;
599 READ_UINT24 (stream, unicodeValue);
600 uvsMapping->unicodeValue = unicodeValue;
601 READ_USHORT (stream, glyphID);
602 uvsMapping->glyphID = glyphID;
610 read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
612 OTF_Stream *stream = table->stream;
613 char *errfmt = "cmap%s";
616 int unicode_bmp_index = -1, unicode_full_index = -1;
619 OTF_CALLOC (cmap, 1, "");
620 READ_USHORT (stream, cmap->version);
621 READ_USHORT (stream, cmap->numTables);
622 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
623 for (i = 0; i < cmap->numTables; i++)
625 unsigned platformID, encodingID;
627 READ_USHORT (stream, platformID);
628 cmap->EncodingRecord[i].platformID = platformID;
629 READ_USHORT (stream, encodingID);
630 cmap->EncodingRecord[i].encodingID = encodingID;
631 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
635 unicode_bmp_index = i;
637 unicode_full_index = i;
639 else if (platformID == 3)
642 unicode_bmp_index = i;
643 else if (encodingID == 10)
644 unicode_full_index = i;
647 cmap->table_index = unicode_full_index;
649 for (i = 0; i < cmap->numTables; i++)
653 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
654 READ_USHORT (stream, format);
655 cmap->EncodingRecord[i].subtable.format = format;
658 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
659 cmap->EncodingRecord[i].subtable.language = 0;
663 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
664 if (format == 8 || format == 10 || format == 12)
666 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
667 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
671 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
678 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
679 " (EncodingRecord)");
681 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
688 OTF_EncodingSubtable2 *sub2;
691 OTF_MALLOC (sub2, 1, " (EncodingSubtable2)");
692 cmap->EncodingRecord[i].subtable.f.f2 = sub2;
693 for (j = 0, max_key = 0; j < 256; j++)
695 READ_USHORT (stream, sub2->subHeaderKeys[j]);
696 if (max_key < sub2->subHeaderKeys[j])
697 max_key = sub2->subHeaderKeys[j];
700 sub2->subHeaderCount = max_key / 8;
701 OTF_MALLOC (sub2->subHeaders, max_key / 8, " (subHeaders)");
702 for (j = 0; j < sub2->subHeaderCount; j++)
704 READ_USHORT (stream, sub2->subHeaders[j].firstCode);
705 READ_USHORT (stream, sub2->subHeaders[j].entryCount);
706 READ_SHORT (stream, sub2->subHeaders[j].idDelta);
707 READ_USHORT (stream, sub2->subHeaders[j].idRangeOffset);
708 /* Make it offset from sub2->glyphIndexArray. */
709 sub2->subHeaders[j].idRangeOffset -= max_key - (j * 8 + 6);
711 sub2->glyphIndexCount = (cmap->EncodingRecord[i].subtable.length
713 OTF_MALLOC (sub2->glyphIndexArray, sub2->glyphIndexCount,
714 " (glyphIndexArray)");
715 READ_BYTES (stream, sub2->glyphIndexArray, sub2->glyphIndexCount);
721 OTF_EncodingSubtable4 *sub4;
726 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
727 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
728 READ_USHORT (stream, sub4->segCountX2);
729 segCount = sub4->segCountX2 / 2;
730 READ_USHORT (stream, sub4->searchRange);
731 READ_USHORT (stream, sub4->entrySelector);
732 READ_USHORT (stream, sub4->rangeShift);
733 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
734 for (j = 0; j < segCount; j++)
735 READ_USHORT (stream, sub4->segments[j].endCount);
736 READ_USHORT (stream, dummy);
737 for (j = 0; j < segCount; j++)
738 READ_USHORT (stream, sub4->segments[j].startCount);
739 for (j = 0; j < segCount; j++)
740 READ_SHORT (stream, sub4->segments[j].idDelta);
741 for (j = 0; j < segCount; j++)
744 unsigned rest = 2 * (segCount - j);
746 READ_USHORT (stream, off);
748 sub4->segments[j].idRangeOffset = 0xFFFF;
750 sub4->segments[j].idRangeOffset = (off - rest) / 2;
752 j = (cmap->EncodingRecord[i].subtable.length
753 - (14 + 2 * (segCount * 4 + 1)));
754 sub4->GlyphCount = j / 2;
755 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
756 for (j = 0; j < sub4->GlyphCount; j++)
757 READ_USHORT (stream, sub4->glyphIdArray[j]);
763 OTF_EncodingSubtable6 *sub6;
766 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
767 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
768 READ_USHORT (stream, sub6->firstCode);
769 READ_USHORT (stream, sub6->entryCount);
770 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
771 for (j = 0; j < sub6->entryCount; j++)
772 READ_USHORT (stream, sub6->glyphIdArray[j]);
778 OTF_EncodingSubtable8 *sub8;
781 OTF_MALLOC (sub8, 1, " (EncodingSubtable8)");
782 cmap->EncodingRecord[i].subtable.f.f8 = sub8;
783 for (j = 0; j < 8192; j++)
784 READ_BYTES (stream, sub8->is32, 8192);
785 READ_ULONG (stream, sub8->nGroups);
786 OTF_MALLOC (sub8->Groups, sub8->nGroups, " (Groups)");
787 for (j = 0; j < sub8->nGroups; j++)
789 READ_ULONG (stream, sub8->Groups[i].startCharCode);
790 READ_ULONG (stream, sub8->Groups[i].endCharCode);
791 READ_ULONG (stream, sub8->Groups[i].startGlyphID);
798 OTF_EncodingSubtable10 *sub10;
801 OTF_MALLOC (sub10, 1, " (EncodingSubtable10)");
802 cmap->EncodingRecord[i].subtable.f.f10 = sub10;
803 READ_ULONG (stream, sub10->startCharCode);
804 READ_ULONG (stream, sub10->numChars);
805 OTF_MALLOC (sub10->glyphs, sub10->numChars, " (GlyphCount)");
806 for (j = 0; j < sub10->numChars; j++)
807 READ_USHORT (stream, sub10->glyphs[j]);
813 OTF_EncodingSubtable12 *sub12;
816 OTF_MALLOC (sub12, 1, " (EncodingSubtable12)");
817 cmap->EncodingRecord[i].subtable.f.f12 = sub12;
818 READ_ULONG (stream, sub12->nGroups);
819 OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
820 for (j = 0; j < sub12->nGroups; j++)
822 READ_ULONG (stream, sub12->Groups[j].startCharCode);
823 READ_ULONG (stream, sub12->Groups[j].endCharCode);
824 READ_ULONG (stream, sub12->Groups[j].startGlyphID);
831 cmap->EncodingRecord[i].subtable.f.f14
832 = read_cmap_uvs_table (otf, stream,
833 cmap->EncodingRecord[i].offset);
838 OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
842 if (unicode_bmp_index >= 0)
844 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_bmp_index;
845 OTF_GlyphID glyph_id, max_glyph_id = 0;
847 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
848 switch (rec->subtable.format)
852 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
853 int segCount = sub4->segCountX2 / 2;
855 for (i = 0; i < segCount; i++)
857 OTF_cmapSegment *seg = sub4->segments + i;
860 if (seg->idRangeOffset == 0xFFFF)
861 for (c = seg->startCount; c <= seg->endCount; c++)
863 glyph_id = (c + seg->idDelta) % 0x10000;
864 cmap->unicode_table[c] = glyph_id;
865 if (glyph_id > max_glyph_id)
866 max_glyph_id = glyph_id;
869 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
872 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
873 + (c - seg->startCount)];
874 cmap->unicode_table[c] = glyph_id;
875 if (glyph_id > max_glyph_id)
876 max_glyph_id = glyph_id;
882 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
883 for (i = 0; i < 0x10000; i++)
884 if (cmap->unicode_table[i])
885 cmap->decode_table[cmap->unicode_table[i]] = i;
886 cmap->max_glyph_id = max_glyph_id;
889 *table->address = cmap;
894 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
896 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
897 Glyph-IDs there. If COUNT is negative, read the number of
898 Glyphs-IDs at first. MINUS if nozero is how few the actual
899 Glyph-IDs are in STREAM than COUNT. */
902 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
903 int minus, int count)
905 char *errfmt = "GlyphID List%s";
910 READ_UINT16 (stream, count);
913 OTF_MALLOC (*ids, count, "");
914 for (i = 0; i < count + minus; i++)
915 READ_GLYPHID (stream, (*ids)[i]);
920 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
922 char *errfmt = "RangeRecord%s";
927 READ_UINT16 (stream, count);
930 OTF_MALLOC (*record, count, "");
931 for (i = 0; i < count; i++)
933 READ_GLYPHID (stream, (*record)[i].Start);
934 READ_GLYPHID (stream, (*record)[i].End);
935 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
942 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
943 OTF_Coverage *coverage)
945 char *errfmt = "Coverage%s";
947 OTF_StreamState state;
950 READ_OFFSET (stream, coverage->offset);
951 SAVE_STREAM (stream, state);
952 SEEK_STREAM (stream, offset + coverage->offset);
953 READ_UINT16 (stream, coverage->CoverageFormat);
954 if (coverage->CoverageFormat == 1)
955 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
956 else if (coverage->CoverageFormat == 2)
957 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
959 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
962 coverage->Count = (unsigned) count;
963 RESTORE_STREAM (stream, state);
967 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
968 and store the Coverages there. If COUNT is negative, read the
969 number of Coverages at first. */
972 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
973 OTF_Coverage **coverage, int count)
975 char *errfmt = "Coverage List%s";
980 READ_UINT16 (stream, count);
983 OTF_MALLOC (*coverage, count, "");
984 for (i = 0; i < count; i++)
985 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
992 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
995 char *errfmt = "ClassDef%s";
998 SEEK_STREAM (stream, class->offset);
999 READ_UINT16 (stream, class->ClassFormat);
1000 if (class->ClassFormat == 1)
1002 READ_GLYPHID (stream, class->f.f1.StartGlyph);
1003 class->f.f1.GlyphCount
1005 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
1006 if (! class->f.f1.GlyphCount)
1007 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1009 else if (class->ClassFormat == 2)
1011 class->f.f2.ClassRangeCount
1012 = (read_range_records
1013 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
1014 if (! class->f.f2.ClassRangeCount)
1015 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1018 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1024 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
1026 char *errfmt = "ClassDef%s";
1028 OTF_StreamState state;
1030 READ_OFFSET (stream, class->offset);
1031 if (! class->offset)
1033 SAVE_STREAM (stream, state);
1034 SEEK_STREAM (stream, offset + class->offset);
1035 READ_UINT16 (stream, class->ClassFormat);
1036 if (class->ClassFormat == 1)
1038 READ_GLYPHID (stream, class->f.f1.StartGlyph);
1039 class->f.f1.GlyphCount
1040 = read_glyph_ids (otf, stream,
1041 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
1043 if (! class->f.f1.GlyphCount)
1046 else if (class->ClassFormat == 2)
1048 class->f.f2.ClassRangeCount
1049 = read_range_records (otf, stream,
1050 (OTF_RangeRecord **)
1051 &class->f.f2.ClassRangeRecord);
1052 if (! class->f.f2.ClassRangeCount)
1056 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1058 RESTORE_STREAM (stream, state);
1064 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
1065 OTF_DeviceTable *table)
1067 char *errfmt = "Device Table%s";
1078 SEEK_STREAM (stream, offset + table->offset);
1079 READ_UINT16 (stream, table->StartSize);
1080 READ_UINT16 (stream, table->EndSize);
1081 READ_UINT16 (stream, table->DeltaFormat);
1082 num = table->EndSize - table->StartSize + 1;
1083 if (num > 0 && table->DeltaFormat >= 1 && table->DeltaFormat <= 3)
1085 OTF_MALLOC (table->DeltaValue, num, "");
1087 if (table->DeltaFormat == 1)
1088 for (i = 0; i < num; i++)
1091 READ_UINT16 (stream, val);
1092 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
1093 table->DeltaValue[i] = intval.int2;
1095 else if (table->DeltaFormat == 2)
1096 for (i = 0; i < num; i++)
1099 READ_UINT16 (stream, val);
1100 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
1101 table->DeltaValue[i] = intval.int4;
1103 else /* (table->DeltaFormat == 3) */
1104 for (i = 0; i < num; i++)
1108 READ_UINT16 (stream, val);
1109 intval.int8 = val >> 8;
1110 table->DeltaValue[i] = intval.int8;
1114 intval.int8 = val >> 8;
1115 table->DeltaValue[i] = intval.int8;
1121 /* Invalid DeltaFormat but several fonts has such values (bug of
1122 fontforge?). So accept it with NULL delta values. */
1123 table->DeltaValue = NULL;
1129 /*** (1-6) "GDEF" table */
1132 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1133 OTF_AttachList *list)
1135 char *errfmt = "AttachList%s";
1139 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1141 READ_UINT16 (stream, list->GlyphCount);
1142 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1143 for (i = 0; i < list->GlyphCount; i++)
1144 READ_OFFSET (stream, list->AttachPoint[i].offset);
1145 for (i = 0; i < list->GlyphCount; i++)
1149 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1150 READ_UINT16 (stream, count);
1151 list->AttachPoint[i].PointCount = count;
1152 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1153 for (j = 0; j < count; j++)
1154 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1160 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1161 OTF_CaretValue *caret)
1163 char *errfmt = "CaretValue%s";
1166 SEEK_STREAM (stream, offset + caret->offset);
1167 READ_UINT16 (stream, caret->CaretValueFormat);
1168 if (caret->CaretValueFormat == 1)
1169 READ_INT16 (stream, caret->f.f1.Coordinate);
1170 else if (caret->CaretValueFormat == 2)
1171 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1172 else if (caret->CaretValueFormat == 3)
1174 READ_INT16 (stream, caret->f.f3.Coordinate);
1175 if (read_device_table (otf, stream, offset + caret->offset,
1176 &caret->f.f3.DeviceTable) < 0)
1180 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1185 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1186 OTF_LigCaretList *list)
1188 char *errfmt = "LigCaretList%s";
1192 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1194 READ_UINT16 (stream, list->LigGlyphCount);
1195 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1196 for (i = 0; i < list->LigGlyphCount; i++)
1197 READ_OFFSET (stream, list->LigGlyph[i].offset);
1198 for (i = 0; i < list->LigGlyphCount; i++)
1202 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1203 READ_UINT16 (stream, count);
1204 list->LigGlyph[i].CaretCount = count;
1205 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1206 for (j = 0; j < count; j++)
1207 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1208 for (j = 0; j < count; j++)
1209 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1210 &list->LigGlyph[i].CaretValue[j]) < 0)
1217 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1221 READ_FIXED (stream, header->Version);
1222 READ_OFFSET (stream, header->GlyphClassDef);
1223 READ_OFFSET (stream, header->AttachList);
1224 READ_OFFSET (stream, header->LigCaretList);
1225 READ_OFFSET (stream, header->MarkAttachClassDef);
1230 read_gdef_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
1232 OTF_Stream *stream = table->stream;
1233 char *errfmt = "GDEF%s";
1234 void *errret = NULL;
1237 OTF_CALLOC (gdef, 1, "");
1240 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1241 if (gdef->header.GlyphClassDef)
1243 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1244 read_class_def_without_offset (otf, stream,
1245 &gdef->glyph_class_def);
1247 if (gdef->header.AttachList)
1248 read_attach_list (otf, stream, gdef->header.AttachList,
1249 &gdef->attach_list);
1250 if (gdef->header.LigCaretList)
1251 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1252 &gdef->lig_caret_list);
1253 if (gdef->header.MarkAttachClassDef)
1255 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1256 read_class_def_without_offset (otf, stream,
1257 &gdef->mark_attach_class_def);
1261 *table->address = gdef;
1266 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
1269 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
1270 OTF_ScriptList *list)
1272 char *errfmt = "Script List%s";
1276 SEEK_STREAM (stream, offset);
1277 READ_USHORT (stream, list->ScriptCount);
1278 OTF_CALLOC (list->Script, list->ScriptCount, "");
1280 for (i = 0; i < list->ScriptCount; i++)
1282 READ_TAG (stream, list->Script[i].ScriptTag);
1283 READ_OFFSET (stream, list->Script[i].offset);
1285 for (i = 0; i < list->ScriptCount; i++)
1287 OTF_Script *script = list->Script + i;
1288 long script_offset = offset + script->offset;
1290 SEEK_STREAM (stream, script_offset);
1291 READ_OFFSET (stream, script->DefaultLangSysOffset);
1292 READ_USHORT (stream, script->LangSysCount);
1293 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1294 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1295 for (j = 0; j < script->LangSysCount; j++)
1297 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1298 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1301 if (script->DefaultLangSysOffset)
1303 OTF_LangSys *langsys = &script->DefaultLangSys;
1305 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1306 READ_OFFSET (stream, langsys->LookupOrder);
1307 READ_USHORT (stream, langsys->ReqFeatureIndex);
1308 READ_USHORT (stream, langsys->FeatureCount);
1309 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1311 for (k = 0; k < langsys->FeatureCount; k++)
1312 READ_USHORT (stream, langsys->FeatureIndex[k]);
1315 for (j = 0; j < script->LangSysCount; j++)
1317 OTF_LangSys *langsys = script->LangSys + j;
1319 SEEK_STREAM (stream,
1320 script_offset + script->LangSysRecord[j].LangSys);
1321 READ_OFFSET (stream, langsys->LookupOrder);
1322 READ_USHORT (stream, langsys->ReqFeatureIndex);
1323 READ_USHORT (stream, langsys->FeatureCount);
1324 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1326 for (k = 0; k < langsys->FeatureCount; k++)
1327 READ_USHORT (stream, langsys->FeatureIndex[k]);
1335 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1336 OTF_FeatureList *list)
1338 char *errfmt = "Feature List%s";
1342 SEEK_STREAM (stream, offset);
1343 READ_UINT16 (stream, list->FeatureCount);
1344 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1345 for (i = 0; i < list->FeatureCount; i++)
1347 READ_TAG (stream, list->Feature[i].FeatureTag);
1348 READ_OFFSET (stream, list->Feature[i].offset);
1350 for (i = 0; i < list->FeatureCount; i++)
1352 OTF_Feature *feature = list->Feature + i;
1354 SEEK_STREAM (stream, offset + feature->offset);
1355 READ_OFFSET (stream, feature->FeatureParams);
1356 READ_UINT16 (stream, feature->LookupCount);
1357 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1358 " (LookupListIndex)");
1359 for (j = 0; j < feature->LookupCount; j++)
1360 READ_UINT16 (stream, feature->LookupListIndex[j]);
1366 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1367 long offset, unsigned type,
1368 OTF_LookupSubTableGSUB *subtable);
1369 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1370 long offset, unsigned type,
1371 OTF_LookupSubTableGPOS *subtable);
1374 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1375 OTF_LookupList *list, int gsubp)
1377 char *errfmt = "Lookup List%s";
1381 SEEK_STREAM (stream, offset);
1382 READ_UINT16 (stream, list->LookupCount);
1383 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1385 for (i = 0; i < list->LookupCount; i++)
1386 READ_OFFSET (stream, list->Lookup[i].offset);
1387 for (i = 0; i < list->LookupCount; i++)
1389 OTF_Lookup *lookup = list->Lookup + i;
1391 SEEK_STREAM (stream, offset + lookup->offset);
1392 READ_UINT16 (stream, lookup->LookupType);
1393 READ_UINT16 (stream, lookup->LookupFlag);
1394 READ_UINT16 (stream, lookup->SubTableCount);
1395 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1396 " (SubTableOffset)");
1398 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1401 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1403 for (j = 0; j < lookup->SubTableCount; j++)
1404 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1405 for (j = 0; j < lookup->SubTableCount; j++)
1408 = offset + lookup->offset + lookup->SubTableOffset[j];
1411 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1413 lookup->SubTable.gsub + j) < 0
1414 : read_lookup_subtable_gpos (otf, stream, this_offset,
1416 lookup->SubTable.gpos + j) < 0)
1425 /*** (1-8) Structures common to GSUB and GPOS */
1428 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1429 OTF_LookupRecord **record, int count)
1431 char *errfmt = "LookupRecord%s";
1432 unsigned errret = 0;
1436 READ_UINT16 (stream, count);
1438 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1439 OTF_MALLOC (*record, count, "");
1440 for (i = 0; i < count; i++)
1442 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1443 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1449 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1451 char *errfmt = "List of Rule%s";
1452 unsigned errret = 0;
1453 OTF_StreamState state;
1457 READ_UINT16 (stream, count);
1459 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1460 OTF_MALLOC (*rule, count, "");
1461 for (i = 0; i < count; i++)
1463 READ_OFFSET (stream, (*rule)[i].offset);
1464 if (! (*rule)[i].offset)
1465 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1467 SAVE_STREAM (stream, state);
1468 for (i = 0; i < count; i++)
1470 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1471 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1472 if ((*rule)[i].GlyphCount == 0)
1473 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1474 READ_UINT16 (stream, (*rule)[i].LookupCount);
1475 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1476 (*rule)[i].GlyphCount) < 0)
1478 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1479 (*rule)[i].LookupCount) == 0)
1482 RESTORE_STREAM (stream, state);
1488 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1491 char *errfmt = "List of RuleSet%s";
1492 unsigned errret = 0;
1493 OTF_StreamState state;
1497 READ_UINT16 (stream, count);
1499 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1500 OTF_MALLOC (*set, count, "");
1501 for (i = 0; i < count; i++)
1503 READ_OFFSET (stream, (*set)[i].offset);
1504 if (! (*set)[i].offset)
1505 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1507 SAVE_STREAM (stream, state);
1508 for (i = 0; i < count; i++)
1510 SEEK_STREAM (stream, offset + (*set)[i].offset);
1512 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1514 if (! (*set)[i].RuleCount)
1517 RESTORE_STREAM (stream, state);
1522 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1523 OTF_ClassRule **rule)
1525 char *errfmt = "ClassRule%s";
1526 unsigned errret = 0;
1527 OTF_StreamState state;
1531 READ_UINT16 (stream, count);
1533 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1534 OTF_MALLOC (*rule, count, "");
1535 for (i = 0; i < count; i++)
1537 READ_OFFSET (stream, (*rule)[i].offset);
1538 if (! (*rule)[i].offset)
1539 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1541 SAVE_STREAM (stream, state);
1542 for (i = 0; i < count; i++)
1544 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1545 READ_USHORT (stream, (*rule)[i].GlyphCount);
1546 if (! (*rule)[i].GlyphCount)
1547 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1548 READ_USHORT (stream, (*rule)[i].LookupCount);
1549 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1550 0, (*rule)[i].GlyphCount - 1) < 0)
1552 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1553 (*rule)[i].LookupCount) == 0)
1556 RESTORE_STREAM (stream, state);
1561 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1564 char *errfmt = "ClassSet%s";
1565 unsigned errret = 0;
1566 OTF_StreamState state;
1570 READ_UINT16 (stream, count);
1572 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1573 OTF_CALLOC (*set, count, "");
1574 for (i = 0; i < count; i++)
1575 /* Offset can be zero. */
1576 READ_OFFSET (stream, (*set)[i].offset);
1577 SAVE_STREAM (stream, state);
1578 for (i = 0; i < count; i++)
1579 if ((*set)[i].offset)
1581 SEEK_STREAM (stream, offset + (*set)[i].offset);
1582 (*set)[i].ClassRuleCnt
1583 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1584 &(*set)[i].ClassRule);
1585 if (! (*set)[i].ClassRuleCnt)
1588 RESTORE_STREAM (stream, state);
1593 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1594 OTF_ChainRule **rule)
1596 char *errfmt = "ChainRule%s";
1597 unsigned errret = 0;
1601 READ_UINT16 (stream, count);
1603 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1604 OTF_MALLOC (*rule, count, "");
1605 for (i = 0; i < count; i++)
1606 READ_OFFSET (stream, (*rule)[i].offset);
1607 for (i = 0; i < count; i++)
1609 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1610 (*rule)[i].BacktrackGlyphCount
1611 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1612 (*rule)[i].InputGlyphCount
1613 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1614 if (! (*rule)[i].InputGlyphCount)
1615 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1616 (*rule)[i].LookaheadGlyphCount
1617 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1618 (*rule)[i].LookupCount
1619 = read_lookup_record_list (otf, stream,
1620 &(*rule)[i].LookupRecord, -1);
1621 if (! (*rule)[i].LookupCount)
1629 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1630 OTF_ChainRuleSet **set)
1632 char *errfmt = "ChainRuleSet%s";
1633 unsigned errret = 0;
1634 OTF_StreamState state;
1638 READ_UINT16 (stream, count);
1640 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1641 OTF_MALLOC (*set, count, "");
1642 for (i = 0; i < count; i++)
1644 READ_OFFSET (stream, (*set)[i].offset);
1645 if (! (*set)[i].offset)
1646 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1648 SAVE_STREAM (stream, state);
1649 for (i = 0; i < count; i++)
1651 SEEK_STREAM (stream, offset + (*set)[i].offset);
1652 (*set)[i].ChainRuleCount
1653 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1654 &(*set)[i].ChainRule);
1655 if (! (*set)[i].ChainRuleCount)
1658 RESTORE_STREAM (stream, state);
1663 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1664 OTF_ChainClassRule **rule)
1666 char *errfmt = "ChainClassRule%s";
1667 unsigned errret = 0;
1671 READ_UINT16 (stream, count);
1673 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1674 OTF_MALLOC (*rule, count, "");
1675 for (i = 0; i < count; i++)
1677 READ_OFFSET (stream, (*rule)[i].offset);
1678 if (! (*rule)[i].offset)
1679 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1681 for (i = 0; i < count; i++)
1683 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1684 (*rule)[i].BacktrackGlyphCount
1685 = read_glyph_ids (otf, stream,
1686 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1687 (*rule)[i].InputGlyphCount
1688 = read_glyph_ids (otf, stream,
1689 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1690 if (! (*rule)[i].InputGlyphCount)
1691 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1692 (*rule)[i].LookaheadGlyphCount
1693 = read_glyph_ids (otf, stream,
1694 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1695 (*rule)[i].LookupCount
1696 = read_lookup_record_list (otf, stream,
1697 &(*rule)[i].LookupRecord, -1);
1698 if (! (*rule)[i].LookupCount)
1705 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1706 OTF_ChainClassSet **set)
1708 char *errfmt = "ChainClassSet%s";
1709 unsigned errret = 0;
1710 OTF_StreamState state;
1714 READ_UINT16 (stream, count);
1716 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1717 OTF_MALLOC (*set, count, "");
1718 for (i = 0; i < count; i++)
1719 /* Offset may be zero. */
1720 READ_OFFSET (stream, (*set)[i].offset);
1721 SAVE_STREAM (stream, state);
1722 for (i = 0; i < count; i++)
1723 if ((*set)[i].offset)
1725 SEEK_STREAM (stream, offset + (*set)[i].offset);
1726 (*set)[i].ChainClassRuleCnt
1727 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1728 &(*set)[i].ChainClassRule);
1729 if (! (*set)[i].ChainClassRuleCnt)
1732 RESTORE_STREAM (stream, state);
1737 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1738 OTF_Coverage *coverage,OTF_Context1 *context1)
1740 if (read_coverage (otf, stream, offset, coverage) < 0)
1742 context1->RuleSetCount
1743 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1744 if (! context1->RuleSetCount)
1750 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1751 OTF_Coverage *coverage,OTF_Context2 *context2)
1753 if (read_coverage (otf, stream, offset, coverage) < 0
1754 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1756 context2->ClassSetCnt
1757 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1758 if (! context2->ClassSetCnt)
1764 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1765 OTF_Coverage *coverage,OTF_Context3 *context3)
1767 char *errfmt = "Context1%s";
1770 READ_USHORT (stream, context3->GlyphCount);
1771 if (context3->GlyphCount < 0)
1772 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1773 READ_USHORT (stream, context3->LookupCount);
1774 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1775 context3->GlyphCount) < 0)
1777 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1778 context3->LookupCount) < 0)
1784 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1785 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1787 if (read_coverage (otf, stream, offset, coverage) < 0)
1789 chain_context1->ChainRuleSetCount
1790 = read_chain_rule_set_list (otf, stream, offset,
1791 &chain_context1->ChainRuleSet);
1792 if (! chain_context1->ChainRuleSetCount)
1798 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1799 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1801 if (read_coverage (otf, stream, offset, coverage) < 0
1802 || read_class_def (otf, stream, offset,
1803 &chain_context2->BacktrackClassDef) < 0
1804 || read_class_def (otf, stream, offset,
1805 &chain_context2->InputClassDef) < 0
1806 || read_class_def (otf, stream, offset,
1807 &chain_context2->LookaheadClassDef) < 0)
1809 chain_context2->ChainClassSetCnt
1810 = read_chain_class_set_list (otf, stream, offset,
1811 &chain_context2->ChainClassSet);
1812 if (! chain_context2->ChainClassSetCnt)
1818 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1819 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1823 count = read_coverage_list (otf, stream, offset,
1824 &chain_context3->Backtrack, -1);
1827 chain_context3->BacktrackGlyphCount = (unsigned) count;
1828 count = read_coverage_list (otf, stream, offset,
1829 &chain_context3->Input, -1);
1832 chain_context3->InputGlyphCount = (unsigned) count;
1833 *coverage = chain_context3->Input[0];
1834 count = read_coverage_list (otf, stream, offset,
1835 &chain_context3->LookAhead, -1);
1836 chain_context3->LookaheadGlyphCount = (unsigned) count;
1837 chain_context3->LookupCount
1838 = read_lookup_record_list (otf, stream,
1839 &chain_context3->LookupRecord, -1);
1844 read_gsub_gpos_table (OTF *otf, OTF_TableInfo *table, int gsubp,
1845 enum OTF_ReaderFlag flag)
1847 OTF_Stream *stream = table->stream;
1848 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1849 void *errret = NULL;
1850 OTF_GSUB_GPOS *gsub_gpos = *table->address;
1853 SEEK_STREAM (stream, 10);
1856 SEEK_STREAM (stream, 0);
1857 OTF_CALLOC (gsub_gpos, 1, "");
1858 READ_FIXED (stream, gsub_gpos->Version);
1859 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1860 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1861 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1862 *table->address = gsub_gpos;
1865 if (! gsub_gpos->ScriptList.Script
1866 && read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1867 &gsub_gpos->ScriptList) < 0)
1869 if (flag != OTF_READ_SCRIPTS)
1871 if (! gsub_gpos->FeatureList.Feature
1872 && read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1873 &gsub_gpos->FeatureList) < 0)
1875 if (flag != OTF_READ_FEATURES)
1877 if (! gsub_gpos->LookupList.Lookup
1878 && read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1879 &gsub_gpos->LookupList, gsubp) < 0)
1888 /* (1-9) "GSUB" table */
1891 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1893 char *errfmt = "Sequence%s";
1894 unsigned errret = 0;
1898 READ_UINT16 (stream, count);
1900 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1901 OTF_MALLOC (*seq, count, "");
1902 for (i = 0; i < count; i++)
1903 READ_OFFSET (stream, (*seq)[i].offset);
1904 for (i = 0; i < count; i++)
1906 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1907 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1908 &(*seq)[i].Substitute, 0, -1);
1909 if (! (*seq)[i].GlyphCount)
1916 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1917 OTF_Ligature **ligature)
1919 char *errfmt = "Ligature%s";
1924 READ_UINT16 (stream, count);
1927 OTF_MALLOC (*ligature, count, "");
1928 for (i = 0; i < count; i++)
1929 READ_OFFSET (stream, (*ligature)[i].offset);
1930 for (i = 0; i < count; i++)
1932 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1933 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1934 (*ligature)[i].CompCount
1935 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1936 if (! (*ligature)[i].CompCount)
1943 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1944 OTF_LigatureSet **ligset)
1946 char *errfmt = "LigatureSet%s";
1951 READ_UINT16 (stream, count);
1954 OTF_MALLOC (*ligset, count, "");
1955 for (i = 0; i < count; i++)
1956 READ_OFFSET (stream, (*ligset)[i].offset);
1957 for (i = 0; i < count; i++)
1961 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1962 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1963 &(*ligset)[i].Ligature);
1966 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1972 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1973 OTF_AlternateSet **altset)
1975 char *errfmt = "AlternateSet%s";
1980 READ_UINT16 (stream, count);
1982 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1983 OTF_MALLOC (*altset, count, "");
1984 for (i = 0; i < count; i++)
1985 READ_OFFSET (stream, (*altset)[i].offset);
1986 for (i = 0; i < count; i++)
1990 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1991 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1994 (*altset)[i].GlyphCount = (unsigned) alt_count;
2000 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
2001 OTF_Coverage *coverage,
2002 OTF_GSUB_ReverseChain1 *reverse_chain)
2006 if (read_coverage (otf, stream, offset, coverage) < 0)
2008 count = read_coverage_list (otf, stream, offset,
2009 &reverse_chain->Backtrack, -1);
2012 reverse_chain->BacktrackGlyphCount = (unsigned) count;
2013 count = read_coverage_list (otf, stream, offset,
2014 &reverse_chain->LookAhead, -1);
2017 reverse_chain->LookaheadGlyphCount = (unsigned) count;
2018 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
2021 reverse_chain->GlyphCount = count;
2026 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
2027 unsigned type, OTF_LookupSubTableGSUB *subtable)
2032 SEEK_STREAM (stream, offset);
2033 READ_USHORT (stream, subtable->Format);
2034 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
2038 if (subtable->Format == 1)
2040 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2042 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
2044 else if (subtable->Format == 2)
2046 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2048 subtable->u.single2.GlyphCount
2049 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
2051 if (! subtable->u.single2.GlyphCount)
2055 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2059 if (subtable->Format == 1)
2061 read_coverage (otf, stream, offset, &subtable->Coverage);
2062 subtable->u.multiple1.SequenceCount
2063 = read_sequence (otf, stream, offset,
2064 &subtable->u.multiple1.Sequence);
2067 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2071 if (subtable->Format == 1)
2073 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2075 subtable->u.alternate1.AlternateSetCount
2076 = read_alternate_set_list (otf, stream, offset,
2077 &subtable->u.alternate1.AlternateSet);
2078 if (! subtable->u.alternate1.AlternateSetCount)
2082 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2086 if (subtable->Format == 1)
2088 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2090 subtable->u.ligature1.LigSetCount
2091 = read_ligature_set_list (otf, stream, offset,
2092 &subtable->u.ligature1.LigatureSet);
2093 if (! subtable->u.ligature1.LigSetCount)
2097 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2101 if (subtable->Format == 1)
2103 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2104 &subtable->u.context1) < 0)
2107 else if (subtable->Format == 2)
2109 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2110 &subtable->u.context2) < 0)
2113 else if (subtable->Format == 3)
2115 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2116 &subtable->u.context3) < 0)
2120 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2124 if (subtable->Format == 1)
2126 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2127 &subtable->u.chain_context1) < 0)
2130 else if (subtable->Format == 2)
2132 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2133 &subtable->u.chain_context2) < 0)
2136 else if (subtable->Format == 3)
2138 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2139 &subtable->u.chain_context3) < 0)
2143 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2147 if (subtable->Format == 1)
2151 OTF_LookupSubTableGSUB *ex_subtable;
2153 READ_USHORT (stream, ex_type);
2154 READ_ULONG (stream, ex_offset);
2155 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2156 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
2157 ex_type, ex_subtable) < 0)
2159 subtable->u.extension1.ExtensionLookupType = ex_type;
2160 subtable->u.extension1.ExtensionOffset = ex_offset;
2161 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2164 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2168 if (subtable->Format == 1)
2170 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
2171 &subtable->u.reverse_chain1) < 0)
2175 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2179 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2185 read_gsub_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2187 return read_gsub_gpos_table (otf, table, 1, flag);
2191 /* (1-10) "GPOS" table */
2194 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
2195 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
2198 OTF_StreamState state;
2201 memset (value_record, 0, sizeof (OTF_ValueRecord));
2204 for (i = 0, size = 0; i < 8; i++)
2208 if (bit & OTF_XPlacement)
2209 READ_INT16 (stream, value_record->XPlacement);
2210 if (bit & OTF_YPlacement)
2211 READ_INT16 (stream, value_record->YPlacement);
2212 if (bit & OTF_XAdvance)
2213 READ_INT16 (stream, value_record->XAdvance);
2214 if (bit & OTF_YAdvance)
2215 READ_INT16 (stream, value_record->YAdvance);
2216 if (bit & OTF_XPlaDevice)
2217 READ_OFFSET (stream, value_record->XPlaDevice.offset);
2218 if (bit & OTF_YPlaDevice)
2219 READ_OFFSET (stream, value_record->YPlaDevice.offset);
2220 if (bit & OTF_XAdvDevice)
2221 READ_OFFSET (stream, value_record->XAdvDevice.offset);
2222 if (bit & OTF_YAdvDevice)
2223 READ_OFFSET (stream, value_record->YAdvDevice.offset);
2224 SAVE_STREAM (stream, state);
2225 if (value_record->XPlaDevice.offset)
2227 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
2230 if (value_record->YPlaDevice.offset)
2232 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
2235 if (value_record->XAdvDevice.offset)
2237 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
2240 if (value_record->YAdvDevice.offset)
2242 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
2245 RESTORE_STREAM (stream, state);
2251 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
2253 char *errfmt = "Anchor%s";
2256 SEEK_STREAM (stream, offset + anchor->offset);
2257 READ_UINT16 (stream, anchor->AnchorFormat);
2258 READ_INT16 (stream, anchor->XCoordinate);
2259 READ_INT16 (stream, anchor->YCoordinate);
2260 if (anchor->AnchorFormat == 1)
2262 else if (anchor->AnchorFormat == 2)
2264 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
2266 else if (anchor->AnchorFormat == 3)
2268 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
2269 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
2270 if (anchor->f.f2.XDeviceTable.offset)
2272 if (read_device_table (otf, stream, offset + anchor->offset,
2273 &anchor->f.f2.XDeviceTable) < 0)
2276 if (anchor->f.f2.YDeviceTable.offset)
2278 if (read_device_table (otf, stream, offset + anchor->offset,
2279 &anchor->f.f2.YDeviceTable) < 0)
2284 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
2290 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
2291 OTF_MarkArray *array)
2293 char *errfmt = "MarkArray%s";
2295 OTF_StreamState state;
2298 READ_OFFSET (stream, array->offset);
2299 SAVE_STREAM (stream, state);
2300 SEEK_STREAM (stream, offset + array->offset);
2301 READ_UINT16 (stream, array->MarkCount);
2302 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
2303 for (i = 0; i < array->MarkCount; i++)
2305 READ_UINT16 (stream, array->MarkRecord[i].Class);
2306 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
2308 for (i = 0; i < array->MarkCount; i++)
2309 if (read_anchor (otf, stream, offset + array->offset,
2310 &array->MarkRecord[i].MarkAnchor) < 0)
2312 RESTORE_STREAM (stream, state);
2317 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2318 unsigned ClassCount, OTF_AnchorArray *array)
2320 char *errfmt = "AnchorArray%s";
2322 OTF_StreamState state;
2325 READ_OFFSET (stream, array->offset);
2326 SAVE_STREAM (stream, state);
2327 SEEK_STREAM (stream, offset + array->offset);
2328 READ_UINT16 (stream, array->Count);
2329 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2330 for (i = 0; i < array->Count; i++)
2332 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2334 for (j = 0; j < ClassCount; j++)
2335 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2337 for (i = 0; i < array->Count; i++)
2338 for (j = 0; j < ClassCount; j++)
2339 if (read_anchor (otf, stream, offset + array->offset,
2340 &array->AnchorRecord[i].Anchor[j]) < 0)
2342 RESTORE_STREAM (stream, state);
2346 static OTF_PairSet *
2347 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2348 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2350 char *errfmt = "PairSet%s";
2351 void *errret = NULL;
2352 OTF_StreamState state;
2356 OTF_MALLOC (set, num, "");
2357 for (i = 0; i < num; i++)
2358 READ_OFFSET (stream, set[i].offset);
2359 SAVE_STREAM (stream, state);
2360 for (i = 0; i < num; i++)
2362 SEEK_STREAM (stream, offset + set[i].offset);
2363 READ_UINT16 (stream, set[i].PairValueCount);
2364 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2365 for (j = 0; j < set[i].PairValueCount; j++)
2367 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2369 READ_UINT16 (stream, rec->SecondGlyph);
2370 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2371 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2374 RESTORE_STREAM (stream, state);
2378 static OTF_Class1Record *
2379 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2380 unsigned num1, enum OTF_ValueFormat bit1,
2381 unsigned num2, enum OTF_ValueFormat bit2)
2383 char *errfmt = "Class1Record%s";
2384 void *errret = NULL;
2385 OTF_Class1Record *rec;
2388 OTF_MALLOC (rec, num1, "");
2389 for (i = 0; i < num1; i++)
2391 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2392 for (j = 0; j < num2; j++)
2394 if (read_value_record (otf, stream, offset,
2395 bit1, &rec[i].Class2Record[j].Value1) < 0
2396 || read_value_record (otf, stream, offset,
2397 bit2, &rec[i].Class2Record[j].Value2) < 0)
2405 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2406 OTF_EntryExitRecord **rec)
2408 char *errfmt = "EntryExitSet%s";
2412 OTF_StreamState state;
2414 READ_UINT16 (stream, count);
2416 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2417 OTF_MALLOC (*rec, count, "");
2418 for (i = 0; i < count; i++)
2420 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2421 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2423 SAVE_STREAM (stream, state);
2424 for (i = 0; i < count; i++)
2426 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2428 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2431 RESTORE_STREAM (stream, state);
2436 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2437 unsigned ClassCount, OTF_LigatureAttach *attach)
2439 char *errfmt = "LigatureAttach%s";
2443 SEEK_STREAM (stream, offset + attach->offset);
2444 READ_UINT16 (stream, attach->ComponentCount);
2445 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2446 for (i = 0; i < attach->ComponentCount; i++)
2448 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2449 " (ComponentRecord)");
2450 for (j = 0; j < ClassCount; j++)
2451 READ_OFFSET (stream,
2452 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2454 for (i = 0; i < attach->ComponentCount; i++)
2455 for (j = 0; j < ClassCount; j++)
2457 if (attach->ComponentRecord[i].LigatureAnchor[j].offset)
2459 if (read_anchor (otf, stream, offset + attach->offset,
2460 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2464 attach->ComponentRecord[i].LigatureAnchor[j].AnchorFormat = 0;
2470 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2471 unsigned class_count, OTF_LigatureArray *array)
2473 char *errfmt = "LigatureArray%s";
2475 OTF_StreamState state;
2478 READ_OFFSET (stream, array->offset);
2479 SAVE_STREAM (stream, state);
2480 SEEK_STREAM (stream, offset + array->offset);
2481 READ_UINT16 (stream, array->LigatureCount);
2482 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2483 for (i = 0; i < array->LigatureCount; i++)
2484 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2485 for (i = 0; i < array->LigatureCount; i++)
2486 read_ligature_attach (otf, stream, offset + array->offset,
2487 class_count, array->LigatureAttach + i);
2488 RESTORE_STREAM (stream, state);
2493 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2494 long offset, unsigned type,
2495 OTF_LookupSubTableGPOS *subtable)
2500 SEEK_STREAM (stream, offset);
2501 READ_UINT16 (stream, subtable->Format);
2502 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2506 if (subtable->Format == 1)
2508 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2510 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2511 read_value_record (otf, stream, offset,
2512 subtable->u.single1.ValueFormat,
2513 &subtable->u.single1.Value);
2515 else if (subtable->Format == 2)
2517 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2520 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2522 READ_UINT16 (stream, single2->ValueFormat);
2523 READ_UINT16 (stream, single2->ValueCount);
2524 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2525 for (i = 0; i < single2->ValueCount; i++)
2526 read_value_record (otf, stream, offset, single2->ValueFormat,
2527 single2->Value + i);
2530 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2534 if (subtable->Format == 1)
2536 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2538 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2539 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2540 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2541 subtable->u.pair1.PairSet
2542 = read_pair_set_list (otf, stream, offset,
2543 subtable->u.pair1.PairSetCount,
2544 subtable->u.pair1.ValueFormat1,
2545 subtable->u.pair1.ValueFormat2);
2546 if (! subtable->u.pair1.PairSet)
2549 else if (subtable->Format == 2)
2551 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2553 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2554 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2555 if (read_class_def (otf, stream, offset,
2556 &subtable->u.pair2.ClassDef1) < 0
2557 || read_class_def (otf, stream, offset,
2558 &subtable->u.pair2.ClassDef2) < 0)
2560 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2561 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2562 subtable->u.pair2.Class1Record
2563 = read_class1_record_list (otf, stream, offset,
2564 subtable->u.pair2.Class1Count,
2565 subtable->u.pair2.ValueFormat1,
2566 subtable->u.pair2.Class2Count,
2567 subtable->u.pair2.ValueFormat2);
2568 if (! subtable->u.pair2.Class1Record)
2572 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2576 if (subtable->Format == 1)
2578 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2580 subtable->u.cursive1.EntryExitCount
2581 = read_entry_exit_list (otf, stream, offset,
2582 &subtable->u.cursive1.EntryExitRecord);
2583 if (! subtable->u.cursive1.EntryExitCount)
2587 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2591 if (subtable->Format == 1)
2593 read_coverage (otf, stream, offset, &subtable->Coverage);
2594 read_coverage (otf, stream, offset,
2595 &subtable->u.mark_base1.BaseCoverage);
2596 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2597 read_mark_array (otf, stream, offset,
2598 &subtable->u.mark_base1.MarkArray);
2599 read_anchor_array (otf, stream, offset,
2600 subtable->u.mark_base1.ClassCount,
2601 &subtable->u.mark_base1.BaseArray);
2604 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2608 if (subtable->Format == 1)
2610 read_coverage (otf, stream, offset, &subtable->Coverage);
2611 read_coverage (otf, stream, offset,
2612 &subtable->u.mark_lig1.LigatureCoverage);
2613 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2614 read_mark_array (otf, stream, offset,
2615 &subtable->u.mark_lig1.MarkArray);
2616 read_ligature_array (otf, stream, offset,
2617 subtable->u.mark_lig1.ClassCount,
2618 &subtable->u.mark_lig1.LigatureArray);
2623 if (subtable->Format == 1)
2625 read_coverage (otf, stream, offset, &subtable->Coverage);
2626 read_coverage (otf, stream, offset,
2627 &subtable->u.mark_mark1.Mark2Coverage);
2628 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2629 read_mark_array (otf, stream, offset,
2630 &subtable->u.mark_mark1.Mark1Array);
2631 read_anchor_array (otf, stream, offset,
2632 subtable->u.mark_mark1.ClassCount,
2633 &subtable->u.mark_mark1.Mark2Array);
2636 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2640 if (subtable->Format == 1)
2642 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2643 &subtable->u.context1) < 0)
2646 else if (subtable->Format == 2)
2648 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2649 &subtable->u.context2) < 0)
2652 else if (subtable->Format == 3)
2654 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2655 &subtable->u.context3) < 0)
2659 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2663 if (subtable->Format == 1)
2665 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2666 &subtable->u.chain_context1) < 0)
2669 else if (subtable->Format == 2)
2671 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2672 &subtable->u.chain_context2) < 0)
2675 else if (subtable->Format == 3)
2677 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2678 &subtable->u.chain_context3) < 0)
2682 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2686 if (subtable->Format == 1)
2690 OTF_LookupSubTableGPOS *ex_subtable;
2692 READ_USHORT (stream, ex_type);
2693 READ_ULONG (stream, ex_offset);
2694 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2695 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2696 ex_type, ex_subtable) < 0)
2698 subtable->u.extension1.ExtensionLookupType = ex_type;
2699 subtable->u.extension1.ExtensionOffset = ex_offset;
2700 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2703 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2707 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2713 read_gpos_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2715 return read_gsub_gpos_table (otf, table, 0, flag);
2723 read_base_table (OTF_Stream *stream, long offset)
2727 OTF_MALLOC (base, 1);
2736 read_jstf_table (OTF_Stream *stream, long offset)
2740 OTF_MALLOC (jstf, 1);
2746 /*** (1-11) Structure for OTF */
2749 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2753 READ_FIXED (stream, table->sfnt_version);
2754 READ_USHORT (stream, table->numTables);
2755 READ_USHORT (stream, table->searchRange);
2756 READ_USHORT (stream, table->enterSelector);
2757 READ_USHORT (stream, table->rangeShift);
2762 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2767 READ_TAG (stream, tag);
2769 table->name[0] = tag >> 24;
2770 table->name[1] = (tag >> 16) & 0xFF;
2771 table->name[2] = (tag >> 8) & 0xFF;
2772 table->name[3] = tag & 0xFF;
2773 table->name[4] = '\0';
2774 READ_ULONG (stream, table->checkSum);
2775 READ_ULONG (stream, table->offset);
2776 READ_ULONG (stream, table->length);
2781 read_header_part (OTF *otf, FILE *fp, FT_Face face)
2783 char *errfmt = "otf header%s";
2786 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2788 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2789 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2790 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2791 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2792 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2793 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2794 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2795 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2796 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2797 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2798 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2799 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2803 OTF_Tag head_tag = OTF_tag ("head");
2804 OTF_Tag name_tag = OTF_tag ("name");
2805 OTF_Tag cmap_tag = OTF_tag ("cmap");
2806 OTF_Tag gdef_tag = OTF_tag ("GDEF");
2807 OTF_Tag gsub_tag = OTF_tag ("GSUB");
2808 OTF_Tag gpos_tag = OTF_tag ("GPOS");
2809 OTF_Stream *stream = make_stream ("Offset Table");
2810 unsigned char ttctag[4];
2814 internal_data->header_stream = stream;
2816 /* Size of Offset Table is 12 bytes. Size of TTC header
2817 (including only the an offset of the first font) is 16. */
2818 if (setup_stream (stream, fp, 0, 16) < 0)
2820 READ_BYTES (stream, ttctag, 4);
2821 if (memcmp (ttctag, "ttcf", 4) == 0)
2823 /* This is a TrueType Collection file. We extract the first font. */
2824 unsigned version, numfonts, offset;
2825 READ_ULONG (stream, version);
2826 READ_ULONG (stream, numfonts);
2827 READ_ULONG (stream, offset); /* Offset of the first font. */
2828 if (setup_stream (stream, fp, offset, 12) < 0)
2832 SEEK_STREAM (stream, 0L);
2833 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2835 /* Size of each Table Directory is 16 bytes. */
2836 if (setup_stream (stream, fp, stream->pos,
2837 16 * otf->offset_table.numTables) < 0)
2840 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables,
2842 for (i = 0; i < otf->offset_table.numTables; i++)
2844 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2845 OTF_TableInfo *table_info = NULL;
2849 if (tag == head_tag)
2850 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2851 else if (tag == name_tag)
2852 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2853 else if (tag == cmap_tag)
2854 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2855 else if (tag == gdef_tag)
2856 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2857 else if (tag == gsub_tag)
2858 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2859 else if (tag == gpos_tag)
2860 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2864 table_info->stream = make_stream (otf->table_dirs[i].name);
2865 if (setup_stream (table_info->stream, fp,
2866 otf->table_dirs[i].offset,
2867 otf->table_dirs[i].length) < 0)
2872 internal_data->header_stream = NULL;
2873 free_stream (stream);
2879 internal_data->header_stream = NULL;
2880 if ((stream = make_stream_from_ft_face (face, "head")))
2881 internal_data->table_info[OTF_TABLE_TYPE_HEAD].stream = stream;
2882 if ((stream = make_stream_from_ft_face (face, "name")))
2883 internal_data->table_info[OTF_TABLE_TYPE_NAME].stream = stream;
2884 if ((stream = make_stream_from_ft_face (face, "cmap")))
2885 internal_data->table_info[OTF_TABLE_TYPE_CMAP].stream = stream;
2886 if ((stream = make_stream_from_ft_face (face, "GDEF")))
2887 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream = stream;
2888 if ((stream = make_stream_from_ft_face (face, "GSUB")))
2889 internal_data->table_info[OTF_TABLE_TYPE_GSUB].stream = stream;
2890 if ((stream = make_stream_from_ft_face (face, "GPOS")))
2891 internal_data->table_info[OTF_TABLE_TYPE_GPOS].stream = stream;
2894 if (! internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream)
2895 /* We can simulate the GDEF table. */
2896 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream
2897 = make_stream ("GDEF");
2901 static OTF_TableInfo *
2902 get_table_info (OTF *otf, const char *name)
2904 char *errfmt = "OTF Table Read%s";
2905 OTF_TableInfo *errret = NULL;
2906 OTF_InternalData *internal_data = otf->internal_data;
2907 OTF_TableInfo *table_info;
2908 OTF_Tag tag = OTF_tag (name);
2911 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2913 if (tag == OTF_tag ("head"))
2914 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2915 else if (tag == OTF_tag ("name"))
2916 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2917 else if (tag == OTF_tag ("cmap"))
2918 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2919 else if (tag == OTF_tag ("GDEF"))
2920 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2921 else if (tag == OTF_tag ("GSUB"))
2922 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2923 else if (tag == OTF_tag ("GPOS"))
2924 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2926 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2928 if (*table_info->address)
2931 if (! table_info->stream)
2932 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2933 if (! table_info->reader)
2934 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2940 /*** (2) API for reading OTF */
2942 /*** (2-1) OTF_open() */
2944 /* Note: We can't use memory allocation macros in the following
2945 functions because those macros return from the functions before
2946 freeing memory previously allocated. */
2949 OTF_open (const char *otf_name)
2952 char *errfmt = "opening otf (%s)";
2953 void *errret = NULL;
2955 OTF_InternalData *internal_data;
2956 int len = strlen (otf_name);
2957 const char *ext = otf_name + (len - 4);
2964 || (strncasecmp (ext + 1, "otf", 3)
2965 && strncasecmp (ext + 1, "ttf", 3)
2966 && strncasecmp (ext + 1, "ttc", 3)))
2967 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2968 fp = fopen (otf_name, "r");
2970 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2971 otf = calloc (1, sizeof (OTF));
2973 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2974 otf->filename = strdup (otf_name);
2975 if (! otf->filename)
2979 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2982 internal_data = calloc (1, sizeof (OTF_InternalData));
2983 if (! internal_data)
2984 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2985 otf->internal_data = internal_data;
2986 if (! allocate_memory_record (otf))
2987 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2989 /* Here after, all pointers to allocated memory are recorded in
2990 otf->internal_data->memory_record except for what allocated by
2991 the functions allocate_memory_record and make_stream. */
2993 if (read_header_part (otf, fp, NULL) < 0)
3005 OTF_open_ft_face (FT_Face face)
3007 char *errfmt = "opening otf from Freetype (%s)";
3008 void *errret = NULL;
3010 OTF_InternalData *internal_data;
3015 if (! FT_IS_SFNT (face))
3016 OTF_ERROR (OTF_ERROR_FILE, (char *) face->family_name);
3017 otf = calloc (1, sizeof (OTF));
3019 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
3020 otf->filename = NULL;
3022 internal_data = calloc (1, sizeof (OTF_InternalData));
3023 if (! internal_data)
3024 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
3025 otf->internal_data = internal_data;
3026 if (! allocate_memory_record (otf))
3027 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
3029 if (read_header_part (otf, NULL, face) < 0)
3038 /*** (2-2) OTF_close() */
3041 OTF_close (OTF *otf)
3043 OTF_InternalData *internal_data = otf->internal_data;
3048 OTF_MemoryRecord *memrec = internal_data->memory_record;
3049 OTF_ApplicationData *app_data = internal_data->app_data;
3051 if (internal_data->header_stream)
3052 free_stream (internal_data->header_stream);
3054 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
3055 if (internal_data->table_info[i].stream)
3056 free_stream (internal_data->table_info[i].stream);
3058 for (; app_data; app_data = app_data->next)
3059 if (app_data->data && app_data->freer)
3060 app_data->freer (app_data->data);
3064 OTF_MemoryRecord *next = memrec->next;
3066 for (i = memrec->used - 1; i >= 0; i--)
3067 free (memrec->memory[i]);
3072 free (internal_data);
3075 free (otf->filename);
3079 /*** (2-3) OTF_get_table() */
3082 OTF_get_table (OTF *otf, const char *name)
3084 OTF_TableInfo *table_info = get_table_info (otf, name);
3089 if (! table_info->stream)
3090 /* Already fully loaded. */
3093 address = (*table_info->reader) (otf, table_info, OTF_READ_FULL);
3094 free_stream (table_info->stream);
3095 table_info->stream = NULL;
3098 table_info->reader = NULL;
3104 /*** (2-4) OTF_check_table() */
3107 OTF_check_table (OTF *otf, const char *name)
3109 return (get_table_info (otf, name) ? 0 : -1);
3112 /*** (2-5) OTF_get_scripts() */
3115 OTF_get_scripts (OTF *otf, int gsubp)
3117 OTF_TableInfo *table_info
3118 = (otf->internal_data->table_info
3119 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3122 if (! table_info->reader)
3124 if (! table_info->stream)
3125 /* Already fully loaded. */
3128 address = (*table_info->reader) (otf, table_info, OTF_READ_SCRIPTS);
3131 table_info->reader = NULL;
3137 /*** (2-6) OTF_get_features() */
3140 OTF_get_features (OTF *otf, int gsubp)
3142 OTF_TableInfo *table_info
3143 = (otf->internal_data->table_info
3144 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3147 if (! table_info->reader)
3149 if (! table_info->stream)
3151 if (*table_info->address)
3152 /* Already fully loaded. */
3157 address = (*table_info->reader) (otf, table_info, OTF_READ_FEATURES);
3160 table_info->reader = NULL;
3166 /*** (2-7) OTF_check_features */
3169 OTF_check_features (OTF *otf, int gsubp,
3170 OTF_Tag script, OTF_Tag language, const OTF_Tag *features,
3173 OTF_ScriptList *script_list;
3174 OTF_Script *Script = NULL;
3175 OTF_LangSys *LangSys = NULL;
3176 OTF_FeatureList *feature_list;
3179 if (OTF_get_features (otf, gsubp) < 0)
3181 for (i = 0; i < n_features; i++)
3183 OTF_Tag feature = features[i];
3187 if ((((unsigned) feature) & 0x80000000) == 0)
3194 script_list = &otf->gsub->ScriptList;
3195 feature_list = &otf->gsub->FeatureList;
3199 script_list = &otf->gpos->ScriptList;
3200 feature_list = &otf->gpos->FeatureList;
3202 for (i = 0; i < script_list->ScriptCount && ! Script; i++)
3203 if (script_list->Script[i].ScriptTag == script)
3204 Script = script_list->Script + i;
3209 for (i = 0; i < Script->LangSysCount && ! LangSys; i++)
3210 if (Script->LangSysRecord[i].LangSysTag == language)
3211 LangSys = Script->LangSys + i;
3214 LangSys = &Script->DefaultLangSys;
3215 for (j = 0; j < n_features; j++)
3217 OTF_Tag feature = features[j];
3222 if (((unsigned) feature) & 0x80000000)
3224 feature = (OTF_Tag) (((unsigned) feature) & 0x7FFFFFFF);
3227 for (i = 0; i < LangSys->FeatureCount; i++)
3228 if (feature_list->Feature[LangSys->FeatureIndex[i]].FeatureTag
3235 if (i == LangSys->FeatureCount)
3242 /*** (5) API miscellaneous ***/
3245 OTF_tag (const char *name)
3247 const unsigned char *p = (unsigned char *) name;
3251 return (OTF_Tag) ((p[0] << 24)
3255 : (p[2] << 8) | p[3]))));
3259 OTF_tag_name (OTF_Tag tag, char *name)
3261 name[0] = (char) (tag >> 24);
3262 name[1] = (char) ((tag >> 16) & 0xFF);
3263 name[2] = (char) ((tag >> 8) & 0xFF);
3264 name[3] = (char) (tag & 0xFF);
3269 OTF_put_data (OTF *otf, char *id, void *data, void (*freer) (void *data))
3271 char *errfmt = "appdata %s";
3273 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3274 OTF_ApplicationData *app_data = internal_data->app_data;
3275 int len = strlen (id) + 1;
3277 for (; app_data; app_data = app_data->next)
3278 if (memcmp (app_data->id, id, len) == 0)
3280 if (app_data->data && app_data->freer)
3281 app_data->freer (app_data->data);
3286 OTF_MALLOC (app_data, sizeof (OTF_ApplicationData), id);
3287 app_data->next = internal_data->app_data;
3288 internal_data->app_data = app_data;
3289 OTF_MALLOC (app_data->id, len, id);
3290 memcpy (app_data->id, id, len);
3292 app_data->data = data;
3293 app_data->freer = freer;
3298 OTF_get_data (OTF *otf, char *id)
3300 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3301 OTF_ApplicationData *app_data = internal_data->app_data;
3303 for (; app_data; app_data = app_data->next)
3304 if (strcmp (app_data->id, id) == 0)
3305 return app_data->data;