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()
63 Example of typical usage of OTF_Stream.
67 OTF_StreamState state;
70 OPEN_STREAM (_FILE_NAME_, stream);
73 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
74 offset = READ_OFFSET (stream);
75 nbytes = READ_ULONG (stream);
76 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
78 CLOSE_STREAM (stream);
92 typedef long OTF_StreamState;
95 make_stream (const char *name)
98 char *errfmt = "stream creation%s";
101 stream = calloc (1, sizeof (OTF_Stream));
103 OTF_ERROR (OTF_ERROR_MEMORY, "");
109 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes)
111 char *errfmt = "stream setup for %s";
115 if (stream->allocated < nbytes)
117 unsigned char *buf = malloc (nbytes);
120 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
124 stream->allocated = nbytes;
126 stream->bufsize = nbytes;
127 if (fseek (fp, offset, SEEK_SET) < 0)
128 OTF_ERROR (OTF_ERROR_FILE, stream->name);
129 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
130 OTF_ERROR (OTF_ERROR_FILE, stream->name);
135 make_stream_from_ft_face (FT_Face face, const char *name)
137 char *errfmt = "FT_Face stream creation for %s";
142 FT_ULong tag = FT_MAKE_TAG (name[0], name[1], name[2], name[3]);
144 if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &nbytes))
146 buf = malloc (nbytes);
148 OTF_ERROR (OTF_ERROR_MEMORY, name);
149 if (FT_Load_Sfnt_Table (face, tag, 0, buf, &nbytes))
152 OTF_ERROR (OTF_ERROR_FT_FACE, name);
154 stream = make_stream (name);
159 stream->allocated = nbytes;
160 stream->bufsize = nbytes;
165 free_stream (OTF_Stream *stream)
172 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
173 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
174 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
176 #define STREAM_CHECK_SIZE(stream, size) \
177 if ((stream)->pos + (size) > (stream)->bufsize) \
179 char *errfmt = "buffer overrun in %s"; \
181 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
187 #define READ_USHORT(stream, var) \
189 STREAM_CHECK_SIZE ((stream), 2); \
190 (var) = (((stream)->buf[(stream)->pos] << 8) \
191 | (stream)->buf[(stream)->pos + 1]); \
192 (stream)->pos += 2; \
195 #define READ_SHORT(stream, var) \
197 STREAM_CHECK_SIZE ((stream), 2); \
198 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
199 | (stream)->buf[(stream)->pos + 1]); \
200 (stream)->pos += 2; \
203 #define READ_UINT24(stream, var) \
205 STREAM_CHECK_SIZE ((stream), 3); \
206 (var) = (((stream)->buf[(stream)->pos ] << 16) \
207 | ((stream)->buf[(stream)->pos + 1] << 8) \
208 | (stream)->buf[(stream)->pos + 2]); \
209 (stream)->pos += 3; \
212 #define READ_ULONG(stream, var) \
214 STREAM_CHECK_SIZE ((stream), 4); \
215 (var) = (((stream)->buf[(stream)->pos] << 24) \
216 | ((stream)->buf[(stream)->pos + 1] << 16) \
217 | ((stream)->buf[(stream)->pos + 2] << 8) \
218 | (stream)->buf[(stream)->pos + 3]); \
219 (stream)->pos += 4; \
222 #define READ_LONG(stream, var) \
224 STREAM_CHECK_SIZE ((stream), 4); \
225 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
226 | ((stream)->buf[(stream)->pos + 1] << 16) \
227 | ((stream)->buf[(stream)->pos + 2] << 8) \
228 | (stream)->buf[(stream)->pos + 3]); \
229 (stream)->pos += 4; \
233 #define READ_FIXED(stream, fixed) \
235 READ_USHORT ((stream), (fixed).high); \
236 READ_USHORT ((stream), (fixed).low); \
240 #define READ_BYTES(stream, p, nbytes) \
242 STREAM_CHECK_SIZE ((stream), (nbytes)); \
243 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
244 (stream)->pos += (nbytes); \
248 #define READ_TAG READ_ULONG
249 #define READ_OFFSET READ_USHORT
250 #define READ_UINT16 READ_USHORT
251 #define READ_INT16 READ_SHORT
252 #define READ_GLYPHID READ_USHORT
255 /*** (1) Structures for OTF Layout tables and OTF itself */
257 /*** (1-1) Basic types and functions */
270 #define OTF_MEMORY_RECORD_SIZE 1024
272 struct OTF_MemoryRecord
275 void *memory[OTF_MEMORY_RECORD_SIZE];
276 struct OTF_MemoryRecord *next;
279 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
289 struct _OTF_TableInfo;
290 typedef struct _OTF_TableInfo OTF_TableInfo;
292 struct _OTF_TableInfo
294 /* Points to one of OTF->head, OTF->name, etc. */
296 /* Function to read one of OTF tables. */
297 void *(*reader) (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag);
298 /* Stream given to <reader>. */
302 struct _OTF_ApplicationData
306 void (*freer) (void *data);
307 struct _OTF_ApplicationData *next;
310 typedef struct _OTF_ApplicationData OTF_ApplicationData;
312 struct OTF_InternalData
314 /* Information about each OTF table. */
315 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
317 /* Stream used to read the header part of OTF. */
318 OTF_Stream *header_stream;
320 /* Records of allocated memories. */
321 OTF_MemoryRecord *memory_record;
323 /* Root of application data chain. */
324 OTF_ApplicationData *app_data;
328 static OTF_MemoryRecord *
329 allocate_memory_record (OTF *otf)
331 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
332 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
337 memrec->next = internal_data->memory_record;
338 internal_data->memory_record = memrec;
342 /* Memory allocation macros. */
344 #define OTF_MALLOC(p, size, arg) \
350 OTF_MemoryRecord *memrec \
351 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
352 (p) = malloc (sizeof (*(p)) * (size)); \
354 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
355 && ! (memrec = allocate_memory_record (otf)))) \
356 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
357 memrec->memory[memrec->used++] = (p); \
362 #define OTF_CALLOC(p, size, arg) \
368 OTF_MemoryRecord *memrec \
369 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
370 (p) = calloc ((size), sizeof (*(p))); \
372 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
373 && ! (memrec = allocate_memory_record (otf)))) \
374 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
375 memrec->memory[memrec->used++] = (p); \
380 /*** (1-2) "head" table */
383 read_head_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
385 OTF_Stream *stream = table->stream;
386 char *errfmt = "head%s";
390 OTF_CALLOC (head, 1, "");
391 READ_FIXED (stream, head->TableVersionNumber);
392 READ_FIXED (stream, head->fontRevision);
393 READ_ULONG (stream, head->checkSumAdjustment);
394 READ_ULONG (stream, head->magicNumber);
395 READ_USHORT (stream, head->flags);
396 READ_USHORT (stream, head->unitsPerEm);
398 *table->address = head;
403 /*** (1-3) "name" table */
406 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
410 OTF_StreamState state;
415 sprintf (errfmt, "nameID (%d)%%s", rec->nameID);
416 if (rec->platformID == 0)
417 ucs = (rec->encodingID <= 3) ? 2 : 4;
418 else if (rec->platformID == 1 && rec->encodingID == 0)
420 else if (rec->platformID == 3)
421 ucs = (rec->encodingID == 1 ? 2
422 : rec->encodingID == 10 ? 4
425 OTF_MALLOC (rec->name, rec->length + 1, "");
426 SAVE_STREAM (stream, state);
427 SEEK_STREAM (stream, stream->pos + rec->offset);
428 READ_BYTES (stream, rec->name, rec->length);
429 RESTORE_STREAM (stream, state);
430 rec->name[rec->length] = 0;
439 for (i = 0; i < rec->length / 2; i++)
441 if (rec->name[i * 2] > 0
442 || rec->name[i * 2 + 1] >= 128)
449 for (i = 0; i < rec->length / 2; i++)
450 rec->name[i] = rec->name[i * 2 + 1];
456 for (i = 0; i < rec->length / 4; i++)
458 if (rec->name[i * 4] > 0
459 || rec->name[i * 4 + 1] > 0
460 || rec->name[i * 4 + 2] > 0
461 || rec->name[i * 2 + 3] >= 128)
468 for (i = 0; i < rec->length / 4; i++)
469 rec->name[i] = rec->name[i * 4 + 3];
477 read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
479 OTF_Stream *stream = table->stream;
480 char *errfmt = "name%s";
485 OTF_CALLOC (name, 1, "");
486 READ_USHORT (stream, name->format);
487 READ_USHORT (stream, name->count);
488 READ_USHORT (stream, name->stringOffset);
489 OTF_MALLOC (name->nameRecord, name->count, "");
490 for (i = 0; i < name->count; i++)
492 OTF_NameRecord *rec = name->nameRecord + i;
494 READ_USHORT (stream, rec->platformID);
495 READ_USHORT (stream, rec->encodingID);
496 READ_USHORT (stream, rec->languageID);
497 READ_USHORT (stream, rec->nameID);
498 READ_USHORT (stream, rec->length);
499 READ_USHORT (stream, rec->offset);
501 for (i = 0; i < name->count; i++)
503 OTF_NameRecord *rec = name->nameRecord + i;
504 int nameID = rec->nameID;
506 read_name (otf, stream, rec);
508 if (nameID >= OTF_max_nameID)
510 if (! name->name[nameID]
512 name->name[nameID] = (char *) rec->name;
515 *table->address = name;
520 /*** (1-4) "cmap" table */
522 static OTF_EncodingSubtable14 *
523 read_cmap_uvs_table (OTF *otf, OTF_Stream *stream, OTF_Offset offset)
525 OTF_EncodingSubtable14 *sub14;
526 char *errfmt = "cmap-uvs%s";
531 OTF_MALLOC (sub14, 1, " (EncodingSubtable14)");
532 READ_ULONG (stream, nRecords);
533 sub14->nRecords = nRecords;
534 OTF_MALLOC (sub14->Records, nRecords, "(EncodingSubtable14-Records)");
535 for (i = 0; i < sub14->nRecords; i++)
537 unsigned varSelector=0, defaultUVSOffset, nonDefaultUVSOffset;
539 READ_UINT24 (stream, varSelector);
540 sub14->Records[i].varSelector = varSelector;
541 READ_ULONG (stream, defaultUVSOffset);
542 sub14->Records[i].defaultUVSOffset = defaultUVSOffset;
543 READ_ULONG (stream, nonDefaultUVSOffset);
544 sub14->Records[i].nonDefaultUVSOffset = nonDefaultUVSOffset;
546 for (i = 0; i < sub14->nRecords; i++)
548 OTF_VariationSelectorRecord *record = &sub14->Records[i];
549 unsigned defaultUVSOffset = record->defaultUVSOffset;
550 unsigned nonDefaultUVSOffset = record->nonDefaultUVSOffset;
552 if (defaultUVSOffset)
554 unsigned numUnicodeValueRanges;
556 SEEK_STREAM (stream, offset+defaultUVSOffset);
557 READ_ULONG (stream, numUnicodeValueRanges);
558 record->numUnicodeValueRanges = numUnicodeValueRanges;
559 OTF_MALLOC (record->unicodeValueRanges,
560 numUnicodeValueRanges,
561 "(EncodingSubtable14-Records-unicodeValueRanges)");
562 for (j = 0; j < numUnicodeValueRanges; j++)
564 OTF_UnicodeValueRange *unicodeValueRange
565 = &record->unicodeValueRanges[j];
566 unsigned startUnicodeValue;
567 char additionalCount;
569 READ_UINT24 (stream, startUnicodeValue);
570 unicodeValueRange->startUnicodeValue=startUnicodeValue;
571 READ_BYTES (stream, &additionalCount, 1);
572 unicodeValueRange->additionalCount
573 = (unsigned short) additionalCount;
576 if (nonDefaultUVSOffset)
578 unsigned numUVSMappings;
580 SEEK_STREAM (stream, offset+nonDefaultUVSOffset);
581 READ_ULONG (stream, numUVSMappings);
582 record->numUVSMappings = numUVSMappings;
583 OTF_MALLOC (record->uvsMappings, numUVSMappings,
584 "(EncodingSubtable14-Records-uvsMappings)");
585 for (j = 0; j < numUVSMappings; j++)
587 OTF_UVSMapping *uvsMapping = &record->uvsMappings[j];
588 unsigned unicodeValue;
589 unsigned short glyphID;
591 READ_UINT24 (stream, unicodeValue);
592 uvsMapping->unicodeValue = unicodeValue;
593 READ_USHORT (stream, glyphID);
594 uvsMapping->glyphID = glyphID;
602 read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
604 OTF_Stream *stream = table->stream;
605 char *errfmt = "cmap%s";
608 int unicode_bmp_index = -1, unicode_full_index = -1;
611 OTF_CALLOC (cmap, 1, "");
612 READ_USHORT (stream, cmap->version);
613 READ_USHORT (stream, cmap->numTables);
614 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
615 for (i = 0; i < cmap->numTables; i++)
617 unsigned platformID, encodingID;
619 READ_USHORT (stream, platformID);
620 cmap->EncodingRecord[i].platformID = platformID;
621 READ_USHORT (stream, encodingID);
622 cmap->EncodingRecord[i].encodingID = encodingID;
623 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
627 unicode_bmp_index = i;
629 unicode_full_index = i;
631 else if (platformID == 3)
634 unicode_bmp_index = i;
635 else if (encodingID == 10)
636 unicode_full_index = i;
639 cmap->table_index = unicode_full_index;
641 for (i = 0; i < cmap->numTables; i++)
645 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
646 READ_USHORT (stream, format);
647 cmap->EncodingRecord[i].subtable.format = format;
650 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
651 cmap->EncodingRecord[i].subtable.language = 0;
655 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
656 if (format == 8 || format == 10 || format == 12)
658 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
659 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
663 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
670 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
671 " (EncodingRecord)");
673 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
680 OTF_EncodingSubtable2 *sub2;
683 OTF_MALLOC (sub2, 1, " (EncodingSubtable2)");
684 cmap->EncodingRecord[i].subtable.f.f2 = sub2;
685 for (j = 0, max_key = 0; j < 256; j++)
687 READ_USHORT (stream, sub2->subHeaderKeys[j]);
688 if (max_key < sub2->subHeaderKeys[j])
689 max_key = sub2->subHeaderKeys[j];
692 sub2->subHeaderCount = max_key / 8;
693 OTF_MALLOC (sub2->subHeaders, max_key / 8, " (subHeaders)");
694 for (j = 0; j < sub2->subHeaderCount; j++)
696 READ_USHORT (stream, sub2->subHeaders[j].firstCode);
697 READ_USHORT (stream, sub2->subHeaders[j].entryCount);
698 READ_SHORT (stream, sub2->subHeaders[j].idDelta);
699 READ_USHORT (stream, sub2->subHeaders[j].idRangeOffset);
700 /* Make it offset from sub2->glyphIndexArray. */
701 sub2->subHeaders[j].idRangeOffset -= max_key - (j * 8 + 6);
703 sub2->glyphIndexCount = (cmap->EncodingRecord[i].subtable.length
705 OTF_MALLOC (sub2->glyphIndexArray, sub2->glyphIndexCount,
706 " (glyphIndexArray)");
707 READ_BYTES (stream, sub2->glyphIndexArray, sub2->glyphIndexCount);
713 OTF_EncodingSubtable4 *sub4;
718 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
719 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
720 READ_USHORT (stream, sub4->segCountX2);
721 segCount = sub4->segCountX2 / 2;
722 READ_USHORT (stream, sub4->searchRange);
723 READ_USHORT (stream, sub4->entrySelector);
724 READ_USHORT (stream, sub4->rangeShift);
725 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
726 for (j = 0; j < segCount; j++)
727 READ_USHORT (stream, sub4->segments[j].endCount);
728 READ_USHORT (stream, dummy);
729 for (j = 0; j < segCount; j++)
730 READ_USHORT (stream, sub4->segments[j].startCount);
731 for (j = 0; j < segCount; j++)
732 READ_SHORT (stream, sub4->segments[j].idDelta);
733 for (j = 0; j < segCount; j++)
736 unsigned rest = 2 * (segCount - j);
738 READ_USHORT (stream, off);
740 sub4->segments[j].idRangeOffset = 0xFFFF;
742 sub4->segments[j].idRangeOffset = (off - rest) / 2;
744 j = (cmap->EncodingRecord[i].subtable.length
745 - (14 + 2 * (segCount * 4 + 1)));
746 sub4->GlyphCount = j / 2;
747 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
748 for (j = 0; j < sub4->GlyphCount; j++)
749 READ_USHORT (stream, sub4->glyphIdArray[j]);
755 OTF_EncodingSubtable6 *sub6;
758 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
759 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
760 READ_USHORT (stream, sub6->firstCode);
761 READ_USHORT (stream, sub6->entryCount);
762 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
763 for (j = 0; j < sub6->entryCount; j++)
764 READ_USHORT (stream, sub6->glyphIdArray[j]);
770 OTF_EncodingSubtable8 *sub8;
773 OTF_MALLOC (sub8, 1, " (EncodingSubtable8)");
774 cmap->EncodingRecord[i].subtable.f.f8 = sub8;
775 for (j = 0; j < 8192; j++)
776 READ_BYTES (stream, sub8->is32, 8192);
777 READ_ULONG (stream, sub8->nGroups);
778 OTF_MALLOC (sub8->Groups, sub8->nGroups, " (Groups)");
779 for (j = 0; j < sub8->nGroups; j++)
781 READ_ULONG (stream, sub8->Groups[i].startCharCode);
782 READ_ULONG (stream, sub8->Groups[i].endCharCode);
783 READ_ULONG (stream, sub8->Groups[i].startGlyphID);
790 OTF_EncodingSubtable10 *sub10;
793 OTF_MALLOC (sub10, 1, " (EncodingSubtable10)");
794 cmap->EncodingRecord[i].subtable.f.f10 = sub10;
795 READ_ULONG (stream, sub10->startCharCode);
796 READ_ULONG (stream, sub10->numChars);
797 OTF_MALLOC (sub10->glyphs, sub10->numChars, " (GlyphCount)");
798 for (j = 0; j < sub10->numChars; j++)
799 READ_USHORT (stream, sub10->glyphs[j]);
805 OTF_EncodingSubtable12 *sub12;
808 OTF_MALLOC (sub12, 1, " (EncodingSubtable12)");
809 cmap->EncodingRecord[i].subtable.f.f12 = sub12;
810 READ_ULONG (stream, sub12->nGroups);
811 OTF_MALLOC (sub12->Groups, sub12->nGroups, " (Groups)");
812 for (j = 0; j < sub12->nGroups; j++)
814 READ_ULONG (stream, sub12->Groups[j].startCharCode);
815 READ_ULONG (stream, sub12->Groups[j].endCharCode);
816 READ_ULONG (stream, sub12->Groups[j].startGlyphID);
823 cmap->EncodingRecord[i].subtable.f.f14
824 = read_cmap_uvs_table (otf, stream,
825 cmap->EncodingRecord[i].offset);
830 OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
834 if (unicode_bmp_index >= 0)
836 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_bmp_index;
837 OTF_GlyphID glyph_id, max_glyph_id = 0;
839 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
840 switch (rec->subtable.format)
844 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
845 int segCount = sub4->segCountX2 / 2;
847 for (i = 0; i < segCount; i++)
849 OTF_cmapSegment *seg = sub4->segments + i;
852 if (seg->idRangeOffset == 0xFFFF)
853 for (c = seg->startCount; c <= seg->endCount; c++)
855 glyph_id = (c + seg->idDelta) % 0x10000;
856 cmap->unicode_table[c] = glyph_id;
857 if (glyph_id > max_glyph_id)
858 max_glyph_id = glyph_id;
861 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
864 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
865 + (c - seg->startCount)];
866 cmap->unicode_table[c] = glyph_id;
867 if (glyph_id > max_glyph_id)
868 max_glyph_id = glyph_id;
874 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
875 for (i = 0; i < 0x10000; i++)
876 if (cmap->unicode_table[i])
877 cmap->decode_table[cmap->unicode_table[i]] = i;
878 cmap->max_glyph_id = max_glyph_id;
881 *table->address = cmap;
886 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
888 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
889 Glyph-IDs there. If COUNT is negative, read the number of
890 Glyphs-IDs at first. MINUS if nozero is how few the actual
891 Glyph-IDs are in STREAM than COUNT. */
894 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
895 int minus, int count)
897 char *errfmt = "GlyphID List%s";
902 READ_UINT16 (stream, count);
905 OTF_MALLOC (*ids, count, "");
906 for (i = 0; i < count + minus; i++)
907 READ_GLYPHID (stream, (*ids)[i]);
912 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
914 char *errfmt = "RangeRecord%s";
919 READ_UINT16 (stream, count);
922 OTF_MALLOC (*record, count, "");
923 for (i = 0; i < count; i++)
925 READ_GLYPHID (stream, (*record)[i].Start);
926 READ_GLYPHID (stream, (*record)[i].End);
927 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
934 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
935 OTF_Coverage *coverage)
937 char *errfmt = "Coverage%s";
939 OTF_StreamState state;
942 READ_OFFSET (stream, coverage->offset);
943 SAVE_STREAM (stream, state);
944 SEEK_STREAM (stream, offset + coverage->offset);
945 READ_UINT16 (stream, coverage->CoverageFormat);
946 if (coverage->CoverageFormat == 1)
947 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
948 else if (coverage->CoverageFormat == 2)
949 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
951 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
954 coverage->Count = (unsigned) count;
955 RESTORE_STREAM (stream, state);
959 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
960 and store the Coverages there. If COUNT is negative, read the
961 number of Coverages at first. */
964 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
965 OTF_Coverage **coverage, int count)
967 char *errfmt = "Coverage List%s";
972 READ_UINT16 (stream, count);
975 OTF_MALLOC (*coverage, count, "");
976 for (i = 0; i < count; i++)
977 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
984 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
987 char *errfmt = "ClassDef%s";
990 SEEK_STREAM (stream, class->offset);
991 READ_UINT16 (stream, class->ClassFormat);
992 if (class->ClassFormat == 1)
994 READ_GLYPHID (stream, class->f.f1.StartGlyph);
995 class->f.f1.GlyphCount
997 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
998 if (! class->f.f1.GlyphCount)
999 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1001 else if (class->ClassFormat == 2)
1003 class->f.f2.ClassRangeCount
1004 = (read_range_records
1005 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
1006 if (! class->f.f2.ClassRangeCount)
1007 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1010 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1016 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
1018 char *errfmt = "ClassDef%s";
1020 OTF_StreamState state;
1022 READ_OFFSET (stream, class->offset);
1023 if (! class->offset)
1025 SAVE_STREAM (stream, state);
1026 SEEK_STREAM (stream, offset + class->offset);
1027 READ_UINT16 (stream, class->ClassFormat);
1028 if (class->ClassFormat == 1)
1030 READ_GLYPHID (stream, class->f.f1.StartGlyph);
1031 class->f.f1.GlyphCount
1032 = read_glyph_ids (otf, stream,
1033 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
1035 if (! class->f.f1.GlyphCount)
1038 else if (class->ClassFormat == 2)
1040 class->f.f2.ClassRangeCount
1041 = read_range_records (otf, stream,
1042 (OTF_RangeRecord **)
1043 &class->f.f2.ClassRangeRecord);
1044 if (! class->f.f2.ClassRangeCount)
1048 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1050 RESTORE_STREAM (stream, state);
1056 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
1057 OTF_DeviceTable *table)
1059 char *errfmt = "Device Table%s";
1070 SEEK_STREAM (stream, offset + table->offset);
1071 READ_UINT16 (stream, table->StartSize);
1072 READ_UINT16 (stream, table->EndSize);
1073 READ_UINT16 (stream, table->DeltaFormat);
1074 num = table->EndSize - table->StartSize + 1;
1075 if (num > 0 && table->DeltaFormat >= 1 && table->DeltaFormat <= 3)
1077 OTF_MALLOC (table->DeltaValue, num, "");
1079 if (table->DeltaFormat == 1)
1080 for (i = 0; i < num; i++)
1083 READ_UINT16 (stream, val);
1084 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
1085 table->DeltaValue[i] = intval.int2;
1087 else if (table->DeltaFormat == 2)
1088 for (i = 0; i < num; i++)
1091 READ_UINT16 (stream, val);
1092 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
1093 table->DeltaValue[i] = intval.int4;
1095 else /* (table->DeltaFormat == 3) */
1096 for (i = 0; i < num; i++)
1100 READ_UINT16 (stream, val);
1101 intval.int8 = val >> 8;
1102 table->DeltaValue[i] = intval.int8;
1106 intval.int8 = val >> 8;
1107 table->DeltaValue[i] = intval.int8;
1113 /* Invalid DeltaFormat but several fonts has such values (bug of
1114 fontforge?). So accept it with NULL delta values. */
1115 table->DeltaValue = NULL;
1121 /*** (1-6) "GDEF" table */
1124 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1125 OTF_AttachList *list)
1127 char *errfmt = "AttachList%s";
1131 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1133 READ_UINT16 (stream, list->GlyphCount);
1134 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1135 for (i = 0; i < list->GlyphCount; i++)
1136 READ_OFFSET (stream, list->AttachPoint[i].offset);
1137 for (i = 0; i < list->GlyphCount; i++)
1141 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1142 READ_UINT16 (stream, count);
1143 list->AttachPoint[i].PointCount = count;
1144 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1145 for (j = 0; j < count; j++)
1146 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1152 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1153 OTF_CaretValue *caret)
1155 char *errfmt = "CaretValue%s";
1158 SEEK_STREAM (stream, offset + caret->offset);
1159 READ_UINT16 (stream, caret->CaretValueFormat);
1160 if (caret->CaretValueFormat == 1)
1161 READ_INT16 (stream, caret->f.f1.Coordinate);
1162 else if (caret->CaretValueFormat == 2)
1163 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1164 else if (caret->CaretValueFormat == 3)
1166 READ_INT16 (stream, caret->f.f3.Coordinate);
1167 if (read_device_table (otf, stream, offset + caret->offset,
1168 &caret->f.f3.DeviceTable) < 0)
1172 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1177 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1178 OTF_LigCaretList *list)
1180 char *errfmt = "LigCaretList%s";
1184 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1186 READ_UINT16 (stream, list->LigGlyphCount);
1187 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1188 for (i = 0; i < list->LigGlyphCount; i++)
1189 READ_OFFSET (stream, list->LigGlyph[i].offset);
1190 for (i = 0; i < list->LigGlyphCount; i++)
1194 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1195 READ_UINT16 (stream, count);
1196 list->LigGlyph[i].CaretCount = count;
1197 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1198 for (j = 0; j < count; j++)
1199 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1200 for (j = 0; j < count; j++)
1201 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1202 &list->LigGlyph[i].CaretValue[j]) < 0)
1209 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1213 READ_FIXED (stream, header->Version);
1214 READ_OFFSET (stream, header->GlyphClassDef);
1215 READ_OFFSET (stream, header->AttachList);
1216 READ_OFFSET (stream, header->LigCaretList);
1217 READ_OFFSET (stream, header->MarkAttachClassDef);
1222 read_gdef_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
1224 OTF_Stream *stream = table->stream;
1225 char *errfmt = "GDEF%s";
1226 void *errret = NULL;
1229 OTF_CALLOC (gdef, 1, "");
1232 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1233 if (gdef->header.GlyphClassDef)
1235 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1236 read_class_def_without_offset (otf, stream,
1237 &gdef->glyph_class_def);
1239 if (gdef->header.AttachList)
1240 read_attach_list (otf, stream, gdef->header.AttachList,
1241 &gdef->attach_list);
1242 if (gdef->header.LigCaretList)
1243 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1244 &gdef->lig_caret_list);
1245 if (gdef->header.MarkAttachClassDef)
1247 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1248 read_class_def_without_offset (otf, stream,
1249 &gdef->mark_attach_class_def);
1253 *table->address = gdef;
1258 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
1261 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
1262 OTF_ScriptList *list)
1264 char *errfmt = "Script List%s";
1268 SEEK_STREAM (stream, offset);
1269 READ_USHORT (stream, list->ScriptCount);
1270 OTF_CALLOC (list->Script, list->ScriptCount, "");
1272 for (i = 0; i < list->ScriptCount; i++)
1274 READ_TAG (stream, list->Script[i].ScriptTag);
1275 READ_OFFSET (stream, list->Script[i].offset);
1277 for (i = 0; i < list->ScriptCount; i++)
1279 OTF_Script *script = list->Script + i;
1280 long script_offset = offset + script->offset;
1282 SEEK_STREAM (stream, script_offset);
1283 READ_OFFSET (stream, script->DefaultLangSysOffset);
1284 READ_USHORT (stream, script->LangSysCount);
1285 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1286 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1287 for (j = 0; j < script->LangSysCount; j++)
1289 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1290 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1293 if (script->DefaultLangSysOffset)
1295 OTF_LangSys *langsys = &script->DefaultLangSys;
1297 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1298 READ_OFFSET (stream, langsys->LookupOrder);
1299 READ_USHORT (stream, langsys->ReqFeatureIndex);
1300 READ_USHORT (stream, langsys->FeatureCount);
1301 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1303 for (k = 0; k < langsys->FeatureCount; k++)
1304 READ_USHORT (stream, langsys->FeatureIndex[k]);
1307 for (j = 0; j < script->LangSysCount; j++)
1309 OTF_LangSys *langsys = script->LangSys + j;
1311 SEEK_STREAM (stream,
1312 script_offset + script->LangSysRecord[j].LangSys);
1313 READ_OFFSET (stream, langsys->LookupOrder);
1314 READ_USHORT (stream, langsys->ReqFeatureIndex);
1315 READ_USHORT (stream, langsys->FeatureCount);
1316 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1318 for (k = 0; k < langsys->FeatureCount; k++)
1319 READ_USHORT (stream, langsys->FeatureIndex[k]);
1327 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1328 OTF_FeatureList *list)
1330 char *errfmt = "Feature List%s";
1334 SEEK_STREAM (stream, offset);
1335 READ_UINT16 (stream, list->FeatureCount);
1336 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1337 for (i = 0; i < list->FeatureCount; i++)
1339 READ_TAG (stream, list->Feature[i].FeatureTag);
1340 READ_OFFSET (stream, list->Feature[i].offset);
1342 for (i = 0; i < list->FeatureCount; i++)
1344 OTF_Feature *feature = list->Feature + i;
1346 SEEK_STREAM (stream, offset + feature->offset);
1347 READ_OFFSET (stream, feature->FeatureParams);
1348 READ_UINT16 (stream, feature->LookupCount);
1349 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1350 " (LookupListIndex)");
1351 for (j = 0; j < feature->LookupCount; j++)
1352 READ_UINT16 (stream, feature->LookupListIndex[j]);
1358 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1359 long offset, unsigned type,
1360 OTF_LookupSubTableGSUB *subtable);
1361 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1362 long offset, unsigned type,
1363 OTF_LookupSubTableGPOS *subtable);
1366 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1367 OTF_LookupList *list, int gsubp)
1369 char *errfmt = "Lookup List%s";
1373 SEEK_STREAM (stream, offset);
1374 READ_UINT16 (stream, list->LookupCount);
1375 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1377 for (i = 0; i < list->LookupCount; i++)
1378 READ_OFFSET (stream, list->Lookup[i].offset);
1379 for (i = 0; i < list->LookupCount; i++)
1381 OTF_Lookup *lookup = list->Lookup + i;
1383 SEEK_STREAM (stream, offset + lookup->offset);
1384 READ_UINT16 (stream, lookup->LookupType);
1385 READ_UINT16 (stream, lookup->LookupFlag);
1386 READ_UINT16 (stream, lookup->SubTableCount);
1387 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1388 " (SubTableOffset)");
1390 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1393 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1395 for (j = 0; j < lookup->SubTableCount; j++)
1396 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1397 for (j = 0; j < lookup->SubTableCount; j++)
1400 = offset + lookup->offset + lookup->SubTableOffset[j];
1403 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1405 lookup->SubTable.gsub + j) < 0
1406 : read_lookup_subtable_gpos (otf, stream, this_offset,
1408 lookup->SubTable.gpos + j) < 0)
1417 /*** (1-8) Structures common to GSUB and GPOS */
1420 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1421 OTF_LookupRecord **record, int count)
1423 char *errfmt = "LookupRecord%s";
1424 unsigned errret = 0;
1428 READ_UINT16 (stream, count);
1430 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1431 OTF_MALLOC (*record, count, "");
1432 for (i = 0; i < count; i++)
1434 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1435 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1441 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1443 char *errfmt = "List of Rule%s";
1444 unsigned errret = 0;
1445 OTF_StreamState state;
1449 READ_UINT16 (stream, count);
1451 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1452 OTF_MALLOC (*rule, count, "");
1453 for (i = 0; i < count; i++)
1455 READ_OFFSET (stream, (*rule)[i].offset);
1456 if (! (*rule)[i].offset)
1457 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1459 SAVE_STREAM (stream, state);
1460 for (i = 0; i < count; i++)
1462 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1463 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1464 if ((*rule)[i].GlyphCount == 0)
1465 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1466 READ_UINT16 (stream, (*rule)[i].LookupCount);
1467 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1468 (*rule)[i].GlyphCount) < 0)
1470 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1471 (*rule)[i].LookupCount) == 0)
1474 RESTORE_STREAM (stream, state);
1480 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1483 char *errfmt = "List of RuleSet%s";
1484 unsigned errret = 0;
1485 OTF_StreamState state;
1489 READ_UINT16 (stream, count);
1491 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1492 OTF_MALLOC (*set, count, "");
1493 for (i = 0; i < count; i++)
1495 READ_OFFSET (stream, (*set)[i].offset);
1496 if (! (*set)[i].offset)
1497 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1499 SAVE_STREAM (stream, state);
1500 for (i = 0; i < count; i++)
1502 SEEK_STREAM (stream, offset + (*set)[i].offset);
1504 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1506 if (! (*set)[i].RuleCount)
1509 RESTORE_STREAM (stream, state);
1514 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1515 OTF_ClassRule **rule)
1517 char *errfmt = "ClassRule%s";
1518 unsigned errret = 0;
1519 OTF_StreamState state;
1523 READ_UINT16 (stream, count);
1525 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1526 OTF_MALLOC (*rule, count, "");
1527 for (i = 0; i < count; i++)
1529 READ_OFFSET (stream, (*rule)[i].offset);
1530 if (! (*rule)[i].offset)
1531 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1533 SAVE_STREAM (stream, state);
1534 for (i = 0; i < count; i++)
1536 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1537 READ_USHORT (stream, (*rule)[i].GlyphCount);
1538 if (! (*rule)[i].GlyphCount)
1539 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1540 READ_USHORT (stream, (*rule)[i].LookupCount);
1541 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1542 0, (*rule)[i].GlyphCount - 1) < 0)
1544 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1545 (*rule)[i].LookupCount) == 0)
1548 RESTORE_STREAM (stream, state);
1553 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1556 char *errfmt = "ClassSet%s";
1557 unsigned errret = 0;
1558 OTF_StreamState state;
1562 READ_UINT16 (stream, count);
1564 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1565 OTF_CALLOC (*set, count, "");
1566 for (i = 0; i < count; i++)
1567 /* Offset can be zero. */
1568 READ_OFFSET (stream, (*set)[i].offset);
1569 SAVE_STREAM (stream, state);
1570 for (i = 0; i < count; i++)
1571 if ((*set)[i].offset)
1573 SEEK_STREAM (stream, offset + (*set)[i].offset);
1574 (*set)[i].ClassRuleCnt
1575 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1576 &(*set)[i].ClassRule);
1577 if (! (*set)[i].ClassRuleCnt)
1580 RESTORE_STREAM (stream, state);
1585 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1586 OTF_ChainRule **rule)
1588 char *errfmt = "ChainRule%s";
1589 unsigned errret = 0;
1593 READ_UINT16 (stream, count);
1595 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1596 OTF_MALLOC (*rule, count, "");
1597 for (i = 0; i < count; i++)
1598 READ_OFFSET (stream, (*rule)[i].offset);
1599 for (i = 0; i < count; i++)
1601 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1602 (*rule)[i].BacktrackGlyphCount
1603 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1604 (*rule)[i].InputGlyphCount
1605 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1606 if (! (*rule)[i].InputGlyphCount)
1607 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1608 (*rule)[i].LookaheadGlyphCount
1609 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1610 (*rule)[i].LookupCount
1611 = read_lookup_record_list (otf, stream,
1612 &(*rule)[i].LookupRecord, -1);
1613 if (! (*rule)[i].LookupCount)
1621 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1622 OTF_ChainRuleSet **set)
1624 char *errfmt = "ChainRuleSet%s";
1625 unsigned errret = 0;
1626 OTF_StreamState state;
1630 READ_UINT16 (stream, count);
1632 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1633 OTF_MALLOC (*set, count, "");
1634 for (i = 0; i < count; i++)
1636 READ_OFFSET (stream, (*set)[i].offset);
1637 if (! (*set)[i].offset)
1638 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1640 SAVE_STREAM (stream, state);
1641 for (i = 0; i < count; i++)
1643 SEEK_STREAM (stream, offset + (*set)[i].offset);
1644 (*set)[i].ChainRuleCount
1645 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1646 &(*set)[i].ChainRule);
1647 if (! (*set)[i].ChainRuleCount)
1650 RESTORE_STREAM (stream, state);
1655 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1656 OTF_ChainClassRule **rule)
1658 char *errfmt = "ChainClassRule%s";
1659 unsigned errret = 0;
1663 READ_UINT16 (stream, count);
1665 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1666 OTF_MALLOC (*rule, count, "");
1667 for (i = 0; i < count; i++)
1669 READ_OFFSET (stream, (*rule)[i].offset);
1670 if (! (*rule)[i].offset)
1671 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1673 for (i = 0; i < count; i++)
1675 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1676 (*rule)[i].BacktrackGlyphCount
1677 = read_glyph_ids (otf, stream,
1678 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1679 (*rule)[i].InputGlyphCount
1680 = read_glyph_ids (otf, stream,
1681 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1682 if (! (*rule)[i].InputGlyphCount)
1683 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1684 (*rule)[i].LookaheadGlyphCount
1685 = read_glyph_ids (otf, stream,
1686 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1687 (*rule)[i].LookupCount
1688 = read_lookup_record_list (otf, stream,
1689 &(*rule)[i].LookupRecord, -1);
1690 if (! (*rule)[i].LookupCount)
1697 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1698 OTF_ChainClassSet **set)
1700 char *errfmt = "ChainClassSet%s";
1701 unsigned errret = 0;
1702 OTF_StreamState state;
1706 READ_UINT16 (stream, count);
1708 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1709 OTF_MALLOC (*set, count, "");
1710 for (i = 0; i < count; i++)
1711 /* Offset may be zero. */
1712 READ_OFFSET (stream, (*set)[i].offset);
1713 SAVE_STREAM (stream, state);
1714 for (i = 0; i < count; i++)
1715 if ((*set)[i].offset)
1717 SEEK_STREAM (stream, offset + (*set)[i].offset);
1718 (*set)[i].ChainClassRuleCnt
1719 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1720 &(*set)[i].ChainClassRule);
1721 if (! (*set)[i].ChainClassRuleCnt)
1724 RESTORE_STREAM (stream, state);
1729 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1730 OTF_Coverage *coverage,OTF_Context1 *context1)
1732 if (read_coverage (otf, stream, offset, coverage) < 0)
1734 context1->RuleSetCount
1735 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1736 if (! context1->RuleSetCount)
1742 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1743 OTF_Coverage *coverage,OTF_Context2 *context2)
1745 if (read_coverage (otf, stream, offset, coverage) < 0
1746 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1748 context2->ClassSetCnt
1749 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1750 if (! context2->ClassSetCnt)
1756 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1757 OTF_Coverage *coverage,OTF_Context3 *context3)
1759 char *errfmt = "Context1%s";
1762 READ_USHORT (stream, context3->GlyphCount);
1763 if (context3->GlyphCount < 0)
1764 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1765 READ_USHORT (stream, context3->LookupCount);
1766 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1767 context3->GlyphCount) < 0)
1769 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1770 context3->LookupCount) < 0)
1776 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1777 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1779 if (read_coverage (otf, stream, offset, coverage) < 0)
1781 chain_context1->ChainRuleSetCount
1782 = read_chain_rule_set_list (otf, stream, offset,
1783 &chain_context1->ChainRuleSet);
1784 if (! chain_context1->ChainRuleSetCount)
1790 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1791 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1793 if (read_coverage (otf, stream, offset, coverage) < 0
1794 || read_class_def (otf, stream, offset,
1795 &chain_context2->BacktrackClassDef) < 0
1796 || read_class_def (otf, stream, offset,
1797 &chain_context2->InputClassDef) < 0
1798 || read_class_def (otf, stream, offset,
1799 &chain_context2->LookaheadClassDef) < 0)
1801 chain_context2->ChainClassSetCnt
1802 = read_chain_class_set_list (otf, stream, offset,
1803 &chain_context2->ChainClassSet);
1804 if (! chain_context2->ChainClassSetCnt)
1810 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1811 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1815 count = read_coverage_list (otf, stream, offset,
1816 &chain_context3->Backtrack, -1);
1819 chain_context3->BacktrackGlyphCount = (unsigned) count;
1820 count = read_coverage_list (otf, stream, offset,
1821 &chain_context3->Input, -1);
1824 chain_context3->InputGlyphCount = (unsigned) count;
1825 *coverage = chain_context3->Input[0];
1826 count = read_coverage_list (otf, stream, offset,
1827 &chain_context3->LookAhead, -1);
1828 chain_context3->LookaheadGlyphCount = (unsigned) count;
1829 chain_context3->LookupCount
1830 = read_lookup_record_list (otf, stream,
1831 &chain_context3->LookupRecord, -1);
1836 read_gsub_gpos_table (OTF *otf, OTF_TableInfo *table, int gsubp,
1837 enum OTF_ReaderFlag flag)
1839 OTF_Stream *stream = table->stream;
1840 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1841 void *errret = NULL;
1842 OTF_GSUB_GPOS *gsub_gpos = *table->address;
1845 SEEK_STREAM (stream, 10);
1848 SEEK_STREAM (stream, 0);
1849 OTF_CALLOC (gsub_gpos, 1, "");
1850 READ_FIXED (stream, gsub_gpos->Version);
1851 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1852 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1853 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1854 *table->address = gsub_gpos;
1857 if (! gsub_gpos->ScriptList.Script
1858 && read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1859 &gsub_gpos->ScriptList) < 0)
1861 if (flag != OTF_READ_SCRIPTS)
1863 if (! gsub_gpos->FeatureList.Feature
1864 && read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1865 &gsub_gpos->FeatureList) < 0)
1867 if (flag != OTF_READ_FEATURES)
1869 if (! gsub_gpos->LookupList.Lookup
1870 && read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1871 &gsub_gpos->LookupList, gsubp) < 0)
1880 /* (1-9) "GSUB" table */
1883 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1885 char *errfmt = "Sequence%s";
1886 unsigned errret = 0;
1890 READ_UINT16 (stream, count);
1892 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1893 OTF_MALLOC (*seq, count, "");
1894 for (i = 0; i < count; i++)
1895 READ_OFFSET (stream, (*seq)[i].offset);
1896 for (i = 0; i < count; i++)
1898 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1899 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1900 &(*seq)[i].Substitute, 0, -1);
1901 if (! (*seq)[i].GlyphCount)
1908 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1909 OTF_Ligature **ligature)
1911 char *errfmt = "Ligature%s";
1916 READ_UINT16 (stream, count);
1919 OTF_MALLOC (*ligature, count, "");
1920 for (i = 0; i < count; i++)
1921 READ_OFFSET (stream, (*ligature)[i].offset);
1922 for (i = 0; i < count; i++)
1924 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1925 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1926 (*ligature)[i].CompCount
1927 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1928 if (! (*ligature)[i].CompCount)
1935 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1936 OTF_LigatureSet **ligset)
1938 char *errfmt = "LigatureSet%s";
1943 READ_UINT16 (stream, count);
1946 OTF_MALLOC (*ligset, count, "");
1947 for (i = 0; i < count; i++)
1948 READ_OFFSET (stream, (*ligset)[i].offset);
1949 for (i = 0; i < count; i++)
1953 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1954 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1955 &(*ligset)[i].Ligature);
1958 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1964 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1965 OTF_AlternateSet **altset)
1967 char *errfmt = "AlternateSet%s";
1972 READ_UINT16 (stream, count);
1974 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1975 OTF_MALLOC (*altset, count, "");
1976 for (i = 0; i < count; i++)
1977 READ_OFFSET (stream, (*altset)[i].offset);
1978 for (i = 0; i < count; i++)
1982 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1983 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1986 (*altset)[i].GlyphCount = (unsigned) alt_count;
1992 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1993 OTF_Coverage *coverage,
1994 OTF_GSUB_ReverseChain1 *reverse_chain)
1998 if (read_coverage (otf, stream, offset, coverage) < 0)
2000 count = read_coverage_list (otf, stream, offset,
2001 &reverse_chain->Backtrack, -1);
2004 reverse_chain->BacktrackGlyphCount = (unsigned) count;
2005 count = read_coverage_list (otf, stream, offset,
2006 &reverse_chain->LookAhead, -1);
2009 reverse_chain->LookaheadGlyphCount = (unsigned) count;
2010 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
2013 reverse_chain->GlyphCount = count;
2018 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
2019 unsigned type, OTF_LookupSubTableGSUB *subtable)
2024 SEEK_STREAM (stream, offset);
2025 READ_USHORT (stream, subtable->Format);
2026 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
2030 if (subtable->Format == 1)
2032 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2034 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
2036 else if (subtable->Format == 2)
2038 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2040 subtable->u.single2.GlyphCount
2041 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
2043 if (! subtable->u.single2.GlyphCount)
2047 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2051 if (subtable->Format == 1)
2053 read_coverage (otf, stream, offset, &subtable->Coverage);
2054 subtable->u.multiple1.SequenceCount
2055 = read_sequence (otf, stream, offset,
2056 &subtable->u.multiple1.Sequence);
2059 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2063 if (subtable->Format == 1)
2065 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2067 subtable->u.alternate1.AlternateSetCount
2068 = read_alternate_set_list (otf, stream, offset,
2069 &subtable->u.alternate1.AlternateSet);
2070 if (! subtable->u.alternate1.AlternateSetCount)
2074 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2078 if (subtable->Format == 1)
2080 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2082 subtable->u.ligature1.LigSetCount
2083 = read_ligature_set_list (otf, stream, offset,
2084 &subtable->u.ligature1.LigatureSet);
2085 if (! subtable->u.ligature1.LigSetCount)
2089 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2093 if (subtable->Format == 1)
2095 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2096 &subtable->u.context1) < 0)
2099 else if (subtable->Format == 2)
2101 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2102 &subtable->u.context2) < 0)
2105 else if (subtable->Format == 3)
2107 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2108 &subtable->u.context3) < 0)
2112 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2116 if (subtable->Format == 1)
2118 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2119 &subtable->u.chain_context1) < 0)
2122 else if (subtable->Format == 2)
2124 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2125 &subtable->u.chain_context2) < 0)
2128 else if (subtable->Format == 3)
2130 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2131 &subtable->u.chain_context3) < 0)
2135 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2139 if (subtable->Format == 1)
2143 OTF_LookupSubTableGSUB *ex_subtable;
2145 READ_USHORT (stream, ex_type);
2146 READ_ULONG (stream, ex_offset);
2147 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2148 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
2149 ex_type, ex_subtable) < 0)
2151 subtable->u.extension1.ExtensionLookupType = ex_type;
2152 subtable->u.extension1.ExtensionOffset = ex_offset;
2153 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2156 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2160 if (subtable->Format == 1)
2162 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
2163 &subtable->u.reverse_chain1) < 0)
2167 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2171 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2177 read_gsub_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2179 return read_gsub_gpos_table (otf, table, 1, flag);
2183 /* (1-10) "GPOS" table */
2186 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
2187 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
2190 OTF_StreamState state;
2193 memset (value_record, 0, sizeof (OTF_ValueRecord));
2196 for (i = 0, size = 0; i < 8; i++)
2200 if (bit & OTF_XPlacement)
2201 READ_INT16 (stream, value_record->XPlacement);
2202 if (bit & OTF_YPlacement)
2203 READ_INT16 (stream, value_record->YPlacement);
2204 if (bit & OTF_XAdvance)
2205 READ_INT16 (stream, value_record->XAdvance);
2206 if (bit & OTF_YAdvance)
2207 READ_INT16 (stream, value_record->YAdvance);
2208 if (bit & OTF_XPlaDevice)
2209 READ_OFFSET (stream, value_record->XPlaDevice.offset);
2210 if (bit & OTF_YPlaDevice)
2211 READ_OFFSET (stream, value_record->YPlaDevice.offset);
2212 if (bit & OTF_XAdvDevice)
2213 READ_OFFSET (stream, value_record->XAdvDevice.offset);
2214 if (bit & OTF_YAdvDevice)
2215 READ_OFFSET (stream, value_record->YAdvDevice.offset);
2216 SAVE_STREAM (stream, state);
2217 if (value_record->XPlaDevice.offset)
2219 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
2222 if (value_record->YPlaDevice.offset)
2224 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
2227 if (value_record->XAdvDevice.offset)
2229 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
2232 if (value_record->YAdvDevice.offset)
2234 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
2237 RESTORE_STREAM (stream, state);
2243 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
2245 char *errfmt = "Anchor%s";
2248 SEEK_STREAM (stream, offset + anchor->offset);
2249 READ_UINT16 (stream, anchor->AnchorFormat);
2250 READ_INT16 (stream, anchor->XCoordinate);
2251 READ_INT16 (stream, anchor->YCoordinate);
2252 if (anchor->AnchorFormat == 1)
2254 else if (anchor->AnchorFormat == 2)
2256 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
2258 else if (anchor->AnchorFormat == 3)
2260 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
2261 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
2262 if (anchor->f.f2.XDeviceTable.offset)
2264 if (read_device_table (otf, stream, offset + anchor->offset,
2265 &anchor->f.f2.XDeviceTable) < 0)
2268 if (anchor->f.f2.YDeviceTable.offset)
2270 if (read_device_table (otf, stream, offset + anchor->offset,
2271 &anchor->f.f2.YDeviceTable) < 0)
2276 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
2282 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
2283 OTF_MarkArray *array)
2285 char *errfmt = "MarkArray%s";
2287 OTF_StreamState state;
2290 READ_OFFSET (stream, array->offset);
2291 SAVE_STREAM (stream, state);
2292 SEEK_STREAM (stream, offset + array->offset);
2293 READ_UINT16 (stream, array->MarkCount);
2294 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
2295 for (i = 0; i < array->MarkCount; i++)
2297 READ_UINT16 (stream, array->MarkRecord[i].Class);
2298 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
2300 for (i = 0; i < array->MarkCount; i++)
2301 if (read_anchor (otf, stream, offset + array->offset,
2302 &array->MarkRecord[i].MarkAnchor) < 0)
2304 RESTORE_STREAM (stream, state);
2309 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2310 unsigned ClassCount, OTF_AnchorArray *array)
2312 char *errfmt = "AnchorArray%s";
2314 OTF_StreamState state;
2317 READ_OFFSET (stream, array->offset);
2318 SAVE_STREAM (stream, state);
2319 SEEK_STREAM (stream, offset + array->offset);
2320 READ_UINT16 (stream, array->Count);
2321 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2322 for (i = 0; i < array->Count; i++)
2324 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2326 for (j = 0; j < ClassCount; j++)
2327 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2329 for (i = 0; i < array->Count; i++)
2330 for (j = 0; j < ClassCount; j++)
2331 if (read_anchor (otf, stream, offset + array->offset,
2332 &array->AnchorRecord[i].Anchor[j]) < 0)
2334 RESTORE_STREAM (stream, state);
2338 static OTF_PairSet *
2339 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2340 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2342 char *errfmt = "PairSet%s";
2343 void *errret = NULL;
2344 OTF_StreamState state;
2348 OTF_MALLOC (set, num, "");
2349 for (i = 0; i < num; i++)
2350 READ_OFFSET (stream, set[i].offset);
2351 SAVE_STREAM (stream, state);
2352 for (i = 0; i < num; i++)
2354 SEEK_STREAM (stream, offset + set[i].offset);
2355 READ_UINT16 (stream, set[i].PairValueCount);
2356 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2357 for (j = 0; j < set[i].PairValueCount; j++)
2359 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2361 READ_UINT16 (stream, rec->SecondGlyph);
2362 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2363 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2366 RESTORE_STREAM (stream, state);
2370 static OTF_Class1Record *
2371 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2372 unsigned num1, enum OTF_ValueFormat bit1,
2373 unsigned num2, enum OTF_ValueFormat bit2)
2375 char *errfmt = "Class1Record%s";
2376 void *errret = NULL;
2377 OTF_Class1Record *rec;
2380 OTF_MALLOC (rec, num1, "");
2381 for (i = 0; i < num1; i++)
2383 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2384 for (j = 0; j < num2; j++)
2386 if (read_value_record (otf, stream, offset,
2387 bit1, &rec[i].Class2Record[j].Value1) < 0
2388 || read_value_record (otf, stream, offset,
2389 bit2, &rec[i].Class2Record[j].Value2) < 0)
2397 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2398 OTF_EntryExitRecord **rec)
2400 char *errfmt = "EntryExitSet%s";
2404 OTF_StreamState state;
2406 READ_UINT16 (stream, count);
2408 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2409 OTF_MALLOC (*rec, count, "");
2410 for (i = 0; i < count; i++)
2412 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2413 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2415 SAVE_STREAM (stream, state);
2416 for (i = 0; i < count; i++)
2418 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2420 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2423 RESTORE_STREAM (stream, state);
2428 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2429 unsigned ClassCount, OTF_LigatureAttach *attach)
2431 char *errfmt = "LigatureAttach%s";
2435 SEEK_STREAM (stream, offset + attach->offset);
2436 READ_UINT16 (stream, attach->ComponentCount);
2437 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2438 for (i = 0; i < attach->ComponentCount; i++)
2440 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2441 " (ComponentRecord)");
2442 for (j = 0; j < ClassCount; j++)
2443 READ_OFFSET (stream,
2444 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2446 for (i = 0; i < attach->ComponentCount; i++)
2447 for (j = 0; j < ClassCount; j++)
2449 if (attach->ComponentRecord[i].LigatureAnchor[j].offset)
2451 if (read_anchor (otf, stream, offset + attach->offset,
2452 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2456 attach->ComponentRecord[i].LigatureAnchor[j].AnchorFormat = 0;
2462 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2463 unsigned class_count, OTF_LigatureArray *array)
2465 char *errfmt = "LigatureArray%s";
2467 OTF_StreamState state;
2470 READ_OFFSET (stream, array->offset);
2471 SAVE_STREAM (stream, state);
2472 SEEK_STREAM (stream, offset + array->offset);
2473 READ_UINT16 (stream, array->LigatureCount);
2474 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2475 for (i = 0; i < array->LigatureCount; i++)
2476 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2477 for (i = 0; i < array->LigatureCount; i++)
2478 read_ligature_attach (otf, stream, offset + array->offset,
2479 class_count, array->LigatureAttach + i);
2480 RESTORE_STREAM (stream, state);
2485 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2486 long offset, unsigned type,
2487 OTF_LookupSubTableGPOS *subtable)
2492 SEEK_STREAM (stream, offset);
2493 READ_UINT16 (stream, subtable->Format);
2494 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2498 if (subtable->Format == 1)
2500 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2502 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2503 read_value_record (otf, stream, offset,
2504 subtable->u.single1.ValueFormat,
2505 &subtable->u.single1.Value);
2507 else if (subtable->Format == 2)
2509 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2512 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2514 READ_UINT16 (stream, single2->ValueFormat);
2515 READ_UINT16 (stream, single2->ValueCount);
2516 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2517 for (i = 0; i < single2->ValueCount; i++)
2518 read_value_record (otf, stream, offset, single2->ValueFormat,
2519 single2->Value + i);
2522 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2526 if (subtable->Format == 1)
2528 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2530 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2531 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2532 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2533 subtable->u.pair1.PairSet
2534 = read_pair_set_list (otf, stream, offset,
2535 subtable->u.pair1.PairSetCount,
2536 subtable->u.pair1.ValueFormat1,
2537 subtable->u.pair1.ValueFormat2);
2538 if (! subtable->u.pair1.PairSet)
2541 else if (subtable->Format == 2)
2543 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2545 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2546 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2547 if (read_class_def (otf, stream, offset,
2548 &subtable->u.pair2.ClassDef1) < 0
2549 || read_class_def (otf, stream, offset,
2550 &subtable->u.pair2.ClassDef2) < 0)
2552 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2553 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2554 subtable->u.pair2.Class1Record
2555 = read_class1_record_list (otf, stream, offset,
2556 subtable->u.pair2.Class1Count,
2557 subtable->u.pair2.ValueFormat1,
2558 subtable->u.pair2.Class2Count,
2559 subtable->u.pair2.ValueFormat2);
2560 if (! subtable->u.pair2.Class1Record)
2564 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2568 if (subtable->Format == 1)
2570 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2572 subtable->u.cursive1.EntryExitCount
2573 = read_entry_exit_list (otf, stream, offset,
2574 &subtable->u.cursive1.EntryExitRecord);
2575 if (! subtable->u.cursive1.EntryExitCount)
2579 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2583 if (subtable->Format == 1)
2585 read_coverage (otf, stream, offset, &subtable->Coverage);
2586 read_coverage (otf, stream, offset,
2587 &subtable->u.mark_base1.BaseCoverage);
2588 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2589 read_mark_array (otf, stream, offset,
2590 &subtable->u.mark_base1.MarkArray);
2591 read_anchor_array (otf, stream, offset,
2592 subtable->u.mark_base1.ClassCount,
2593 &subtable->u.mark_base1.BaseArray);
2596 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2600 if (subtable->Format == 1)
2602 read_coverage (otf, stream, offset, &subtable->Coverage);
2603 read_coverage (otf, stream, offset,
2604 &subtable->u.mark_lig1.LigatureCoverage);
2605 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2606 read_mark_array (otf, stream, offset,
2607 &subtable->u.mark_lig1.MarkArray);
2608 read_ligature_array (otf, stream, offset,
2609 subtable->u.mark_lig1.ClassCount,
2610 &subtable->u.mark_lig1.LigatureArray);
2615 if (subtable->Format == 1)
2617 read_coverage (otf, stream, offset, &subtable->Coverage);
2618 read_coverage (otf, stream, offset,
2619 &subtable->u.mark_mark1.Mark2Coverage);
2620 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2621 read_mark_array (otf, stream, offset,
2622 &subtable->u.mark_mark1.Mark1Array);
2623 read_anchor_array (otf, stream, offset,
2624 subtable->u.mark_mark1.ClassCount,
2625 &subtable->u.mark_mark1.Mark2Array);
2628 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2632 if (subtable->Format == 1)
2634 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2635 &subtable->u.context1) < 0)
2638 else if (subtable->Format == 2)
2640 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2641 &subtable->u.context2) < 0)
2644 else if (subtable->Format == 3)
2646 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2647 &subtable->u.context3) < 0)
2651 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2655 if (subtable->Format == 1)
2657 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2658 &subtable->u.chain_context1) < 0)
2661 else if (subtable->Format == 2)
2663 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2664 &subtable->u.chain_context2) < 0)
2667 else if (subtable->Format == 3)
2669 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2670 &subtable->u.chain_context3) < 0)
2674 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2678 if (subtable->Format == 1)
2682 OTF_LookupSubTableGPOS *ex_subtable;
2684 READ_USHORT (stream, ex_type);
2685 READ_ULONG (stream, ex_offset);
2686 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2687 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2688 ex_type, ex_subtable) < 0)
2690 subtable->u.extension1.ExtensionLookupType = ex_type;
2691 subtable->u.extension1.ExtensionOffset = ex_offset;
2692 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2695 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2699 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2705 read_gpos_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
2707 return read_gsub_gpos_table (otf, table, 0, flag);
2715 read_base_table (OTF_Stream *stream, long offset)
2719 OTF_MALLOC (base, 1);
2728 read_jstf_table (OTF_Stream *stream, long offset)
2732 OTF_MALLOC (jstf, 1);
2738 /*** (1-11) Structure for OTF */
2741 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2745 READ_FIXED (stream, table->sfnt_version);
2746 READ_USHORT (stream, table->numTables);
2747 READ_USHORT (stream, table->searchRange);
2748 READ_USHORT (stream, table->enterSelector);
2749 READ_USHORT (stream, table->rangeShift);
2754 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2759 READ_TAG (stream, tag);
2761 table->name[0] = tag >> 24;
2762 table->name[1] = (tag >> 16) & 0xFF;
2763 table->name[2] = (tag >> 8) & 0xFF;
2764 table->name[3] = tag & 0xFF;
2765 table->name[4] = '\0';
2766 READ_ULONG (stream, table->checkSum);
2767 READ_ULONG (stream, table->offset);
2768 READ_ULONG (stream, table->length);
2773 read_header_part (OTF *otf, FILE *fp, FT_Face face)
2775 char *errfmt = "otf header%s";
2778 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2780 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2781 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2782 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2783 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2784 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2785 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2786 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2787 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2788 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2789 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2790 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2791 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2795 OTF_Tag head_tag = OTF_tag ("head");
2796 OTF_Tag name_tag = OTF_tag ("name");
2797 OTF_Tag cmap_tag = OTF_tag ("cmap");
2798 OTF_Tag gdef_tag = OTF_tag ("GDEF");
2799 OTF_Tag gsub_tag = OTF_tag ("GSUB");
2800 OTF_Tag gpos_tag = OTF_tag ("GPOS");
2801 OTF_Stream *stream = make_stream ("Offset Table");
2805 internal_data->header_stream = stream;
2807 /* Size of Offset Table is 12 bytes. */
2808 if (setup_stream (stream, fp, 0, 12) < 0)
2810 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2813 /* Size of each Table Directory is 16 bytes. */
2814 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables) < 0)
2817 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables,
2819 for (i = 0; i < otf->offset_table.numTables; i++)
2821 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2822 OTF_TableInfo *table_info = NULL;
2826 if (tag == head_tag)
2827 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2828 else if (tag == name_tag)
2829 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2830 else if (tag == cmap_tag)
2831 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2832 else if (tag == gdef_tag)
2833 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2834 else if (tag == gsub_tag)
2835 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2836 else if (tag == gpos_tag)
2837 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2841 table_info->stream = make_stream (otf->table_dirs[i].name);
2842 if (setup_stream (table_info->stream, fp,
2843 otf->table_dirs[i].offset,
2844 otf->table_dirs[i].length) < 0)
2849 internal_data->header_stream = NULL;
2850 free_stream (stream);
2856 internal_data->header_stream = NULL;
2857 if ((stream = make_stream_from_ft_face (face, "head")))
2858 internal_data->table_info[OTF_TABLE_TYPE_HEAD].stream = stream;
2859 if ((stream = make_stream_from_ft_face (face, "name")))
2860 internal_data->table_info[OTF_TABLE_TYPE_NAME].stream = stream;
2861 if ((stream = make_stream_from_ft_face (face, "cmap")))
2862 internal_data->table_info[OTF_TABLE_TYPE_CMAP].stream = stream;
2863 if ((stream = make_stream_from_ft_face (face, "GDEF")))
2864 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream = stream;
2865 if ((stream = make_stream_from_ft_face (face, "GSUB")))
2866 internal_data->table_info[OTF_TABLE_TYPE_GSUB].stream = stream;
2867 if ((stream = make_stream_from_ft_face (face, "GPOS")))
2868 internal_data->table_info[OTF_TABLE_TYPE_GPOS].stream = stream;
2871 if (! internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream)
2872 /* We can simulate the GDEF table. */
2873 internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream
2874 = make_stream ("GDEF");
2878 static OTF_TableInfo *
2879 get_table_info (OTF *otf, const char *name)
2881 char *errfmt = "OTF Table Read%s";
2882 OTF_TableInfo *errret = NULL;
2883 OTF_InternalData *internal_data = otf->internal_data;
2884 OTF_TableInfo *table_info;
2885 OTF_Tag tag = OTF_tag (name);
2888 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2890 if (tag == OTF_tag ("head"))
2891 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2892 else if (tag == OTF_tag ("name"))
2893 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2894 else if (tag == OTF_tag ("cmap"))
2895 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2896 else if (tag == OTF_tag ("GDEF"))
2897 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2898 else if (tag == OTF_tag ("GSUB"))
2899 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2900 else if (tag == OTF_tag ("GPOS"))
2901 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2903 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2905 if (*table_info->address)
2908 if (! table_info->stream)
2909 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2910 if (! table_info->reader)
2911 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2917 /*** (2) API for reading OTF */
2919 /*** (2-1) OTF_open() */
2921 /* Note: We can't use memory allocation macros in the following
2922 functions because those macros return from the functions before
2923 freeing memory previously allocated. */
2926 OTF_open (const char *otf_name)
2929 char *errfmt = "opening otf (%s)";
2930 void *errret = NULL;
2932 OTF_InternalData *internal_data;
2933 int len = strlen (otf_name);
2934 const char *ext = otf_name + (len - 4);
2938 || (ext[1] != 'O' && ext[1] != 'T' && ext[1] != 'o' && ext[1] != 't')
2939 || (ext[2] != 'T' && ext[2] != 't')
2940 || (ext[3] != 'F' && ext[3] != 'f'))
2941 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2942 fp = fopen (otf_name, "r");
2944 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2945 otf = calloc (1, sizeof (OTF));
2947 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2948 otf->filename = strdup (otf_name);
2949 if (! otf->filename)
2953 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2956 internal_data = calloc (1, sizeof (OTF_InternalData));
2957 if (! internal_data)
2958 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2959 otf->internal_data = internal_data;
2960 if (! allocate_memory_record (otf))
2961 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2963 /* Here after, all pointers to allocated memory are recorded in
2964 otf->internal_data->memory_record except for what allocated by
2965 the functions allocate_memory_record and make_stream. */
2967 if (read_header_part (otf, fp, NULL) < 0)
2979 OTF_open_ft_face (FT_Face face)
2981 char *errfmt = "opening otf from Freetype (%s)";
2982 void *errret = NULL;
2984 OTF_InternalData *internal_data;
2986 if (! FT_IS_SFNT (face))
2987 OTF_ERROR (OTF_ERROR_FILE, (char *) face->family_name);
2988 otf = calloc (1, sizeof (OTF));
2990 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2991 otf->filename = NULL;
2993 internal_data = calloc (1, sizeof (OTF_InternalData));
2994 if (! internal_data)
2995 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2996 otf->internal_data = internal_data;
2997 if (! allocate_memory_record (otf))
2998 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
3000 if (read_header_part (otf, NULL, face) < 0)
3009 /*** (2-2) OTF_close() */
3012 OTF_close (OTF *otf)
3014 OTF_InternalData *internal_data = otf->internal_data;
3019 OTF_MemoryRecord *memrec = internal_data->memory_record;
3020 OTF_ApplicationData *app_data = internal_data->app_data;
3022 if (internal_data->header_stream)
3023 free_stream (internal_data->header_stream);
3025 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
3026 if (internal_data->table_info[i].stream)
3027 free_stream (internal_data->table_info[i].stream);
3029 for (; app_data; app_data = app_data->next)
3030 if (app_data->data && app_data->freer)
3031 app_data->freer (app_data->data);
3035 OTF_MemoryRecord *next = memrec->next;
3037 for (i = memrec->used - 1; i >= 0; i--)
3038 free (memrec->memory[i]);
3043 free (internal_data);
3046 free (otf->filename);
3050 /*** (2-3) OTF_get_table() */
3053 OTF_get_table (OTF *otf, const char *name)
3055 OTF_TableInfo *table_info = get_table_info (otf, name);
3060 if (! table_info->stream)
3061 /* Already fully loaded. */
3064 address = (*table_info->reader) (otf, table_info, OTF_READ_FULL);
3065 free_stream (table_info->stream);
3066 table_info->stream = NULL;
3069 table_info->reader = NULL;
3075 /*** (2-4) OTF_check_table() */
3078 OTF_check_table (OTF *otf, const char *name)
3080 return (get_table_info (otf, name) ? 0 : -1);
3083 /*** (2-5) OTF_get_scripts() */
3086 OTF_get_scripts (OTF *otf, int gsubp)
3088 OTF_TableInfo *table_info
3089 = (otf->internal_data->table_info
3090 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3093 if (! table_info->reader)
3095 if (! table_info->stream)
3096 /* Already fully loaded. */
3099 address = (*table_info->reader) (otf, table_info, OTF_READ_SCRIPTS);
3102 table_info->reader = NULL;
3108 /*** (2-6) OTF_get_features() */
3111 OTF_get_features (OTF *otf, int gsubp)
3113 OTF_TableInfo *table_info
3114 = (otf->internal_data->table_info
3115 + (gsubp ? OTF_TABLE_TYPE_GSUB : OTF_TABLE_TYPE_GPOS));
3118 if (! table_info->reader)
3120 if (! table_info->stream)
3122 if (*table_info->address)
3123 /* Already fully loaded. */
3128 address = (*table_info->reader) (otf, table_info, OTF_READ_FEATURES);
3131 table_info->reader = NULL;
3137 /*** (2-7) OTF_check_features */
3140 OTF_check_features (OTF *otf, int gsubp,
3141 OTF_Tag script, OTF_Tag language, const OTF_Tag *features,
3144 OTF_ScriptList *script_list;
3145 OTF_Script *Script = NULL;
3146 OTF_LangSys *LangSys = NULL;
3147 OTF_FeatureList *feature_list;
3150 if (OTF_get_features (otf, gsubp) < 0)
3152 for (i = 0; i < n_features; i++)
3154 OTF_Tag feature = features[i];
3158 if ((((unsigned) feature) & 0x80000000) == 0)
3165 script_list = &otf->gsub->ScriptList;
3166 feature_list = &otf->gsub->FeatureList;
3170 script_list = &otf->gpos->ScriptList;
3171 feature_list = &otf->gpos->FeatureList;
3173 for (i = 0; i < script_list->ScriptCount && ! Script; i++)
3174 if (script_list->Script[i].ScriptTag == script)
3175 Script = script_list->Script + i;
3180 for (i = 0; i < Script->LangSysCount && ! LangSys; i++)
3181 if (Script->LangSysRecord[i].LangSysTag == language)
3182 LangSys = Script->LangSys + i;
3185 LangSys = &Script->DefaultLangSys;
3186 for (j = 0; j < n_features; j++)
3188 OTF_Tag feature = features[j];
3193 if (((unsigned) feature) & 0x80000000)
3195 feature = (OTF_Tag) (((unsigned) feature) & 0x7FFFFFFF);
3198 for (i = 0; i < LangSys->FeatureCount; i++)
3199 if (feature_list->Feature[LangSys->FeatureIndex[i]].FeatureTag
3206 if (i == LangSys->FeatureCount)
3213 /*** (5) API miscellaneous ***/
3216 OTF_tag (const char *name)
3218 const unsigned char *p = (unsigned char *) name;
3222 return (OTF_Tag) ((p[0] << 24)
3226 : (p[2] << 8) | p[3]))));
3230 OTF_tag_name (OTF_Tag tag, char *name)
3232 name[0] = (char) (tag >> 24);
3233 name[1] = (char) ((tag >> 16) & 0xFF);
3234 name[2] = (char) ((tag >> 8) & 0xFF);
3235 name[3] = (char) (tag & 0xFF);
3240 OTF_put_data (OTF *otf, char *id, void *data, void (*freer) (void *data))
3242 char *errfmt = "appdata %s";
3244 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3245 OTF_ApplicationData *app_data = internal_data->app_data;
3246 int len = strlen (id) + 1;
3248 for (; app_data; app_data = app_data->next)
3249 if (memcmp (app_data->id, id, len) == 0)
3251 if (app_data->data && app_data->freer)
3252 app_data->freer (app_data->data);
3257 OTF_MALLOC (app_data, sizeof (OTF_ApplicationData), id);
3258 app_data->next = internal_data->app_data;
3259 internal_data->app_data = app_data;
3260 OTF_MALLOC (app_data->id, len, id);
3261 memcpy (app_data->id, id, len);
3263 app_data->data = data;
3264 app_data->freer = freer;
3269 OTF_get_data (OTF *otf, char *id)
3271 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
3272 OTF_ApplicationData *app_data = internal_data->app_data;
3274 for (; app_data; app_data = app_data->next)
3275 if (strcmp (app_data->id, id) == 0)
3276 return app_data->data;