1 /* otfopen.c -- OpenType font reader.
3 Copyright (C) 2003, 2004
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H15PRO167
7 This file is part of libotf.
9 Libotf is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 Libotf is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library, in a file named COPYING; if not,
21 write to the Free Software Foundation, Inc., 59 Temple Place, Suite
22 330, Boston, MA 02111-1307, USA. */
33 Table of contents (almost parallel to otf.h):
37 (1) Readers for OTF Layout tables and OTF itself
38 (1-1) Basic types and functions
42 (1-5) Structures common to GDEF, GSUB, and GPOS
44 (1-7) Structures for ScriptList, FeatureList, and LookupList
45 (1-8) Structures common to GSUB and GPOS
48 (1-11) Structure for OTF
50 (2) API for reading OTF
54 (2-4) OTF_check_table()
61 Example of typical usage of OTF_Stream.
65 OTF_StreamState state;
68 OPEN_STREAM (_FILE_NAME_, stream);
71 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
72 offset = READ_OFFSET (stream);
73 nbytes = READ_ULONG (stream);
74 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
76 CLOSE_STREAM (stream);
91 typedef long OTF_StreamState;
97 char *errfmt = "stream creation%s";
100 stream = calloc (1, sizeof (OTF_Stream));
102 OTF_ERROR (OTF_ERROR_MEMORY, "");
107 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
110 char *errfmt = "stream setup for %s";
115 if (stream->allocated < nbytes)
117 unsigned char *buf = malloc (nbytes);
120 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
124 stream->allocated = nbytes;
126 stream->bufsize = nbytes;
127 if (fseek (fp, offset, SEEK_SET) < 0)
128 OTF_ERROR (OTF_ERROR_FILE, stream->name);
129 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
130 OTF_ERROR (OTF_ERROR_FILE, stream->name);
136 free_stream (OTF_Stream *stream)
142 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
143 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
144 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
146 #define STREAM_CHECK_SIZE(stream, size) \
147 if ((stream)->pos + (size) > (stream)->bufsize) \
149 char *errfmt = "buffer overrun in %s"; \
151 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
157 #define READ_USHORT(stream, var) \
159 STREAM_CHECK_SIZE ((stream), 2); \
160 (var) = (((stream)->buf[(stream)->pos] << 8) \
161 | (stream)->buf[(stream)->pos + 1]); \
162 (stream)->pos += 2; \
165 #define READ_SHORT(stream, var) \
167 STREAM_CHECK_SIZE ((stream), 2); \
168 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
169 | (stream)->buf[(stream)->pos + 1]); \
170 (stream)->pos += 2; \
173 #define READ_ULONG(stream, var) \
175 STREAM_CHECK_SIZE ((stream), 4); \
176 (var) = (((stream)->buf[(stream)->pos] << 24) \
177 | ((stream)->buf[(stream)->pos + 1] << 16) \
178 | ((stream)->buf[(stream)->pos + 2] << 8) \
179 | (stream)->buf[(stream)->pos + 3]); \
180 (stream)->pos += 4; \
183 #define READ_LONG(stream, var) \
185 STREAM_CHECK_SIZE ((stream), 4); \
186 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
187 | ((stream)->buf[(stream)->pos + 1] << 16) \
188 | ((stream)->buf[(stream)->pos + 2] << 8) \
189 | (stream)->buf[(stream)->pos + 3]); \
190 (stream)->pos += 4; \
194 #define READ_FIXED(stream, fixed) \
196 READ_USHORT ((stream), (fixed).high); \
197 READ_USHORT ((stream), (fixed).low); \
201 #define READ_BYTES(stream, p, nbytes) \
203 STREAM_CHECK_SIZE ((stream), (nbytes)); \
204 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
205 (stream)->pos += (nbytes); \
209 #define READ_TAG READ_ULONG
210 #define READ_OFFSET READ_USHORT
211 #define READ_UINT16 READ_USHORT
212 #define READ_INT16 READ_SHORT
213 #define READ_GLYPHID READ_USHORT
216 /*** (1) Structures for OTF Layout tables and OTF itself */
218 /*** (1-1) Basic types and functions */
231 #define OTF_MEMORY_RECORD_SIZE 1024
233 struct OTF_MemoryRecord
236 void *memory[OTF_MEMORY_RECORD_SIZE];
237 struct OTF_MemoryRecord *next;
240 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
244 /* Points to one of OTF->head, OTF->name, etc. */
246 /* Function to read one of OTF tables. */
247 void *(*reader) (OTF *otf, OTF_Stream *stream);
248 /* Stream given to <reader>. */
252 struct OTF_InternalData
254 /* Information about each OTF table. */
255 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
257 /* Stream used to read the header part of OTF. */
258 OTF_Stream *header_stream;
260 /* Records of allocated memories. */
261 OTF_MemoryRecord *memory_record;
265 static OTF_MemoryRecord *
266 allocate_memory_record (OTF *otf)
268 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
269 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
274 memrec->next = internal_data->memory_record;
275 internal_data->memory_record = memrec;
279 /* Memory allocation macros. */
281 #define OTF_MALLOC(p, size, arg) \
287 OTF_MemoryRecord *memrec \
288 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
289 (p) = malloc (sizeof (*(p)) * (size)); \
291 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
292 && ! (memrec = allocate_memory_record (otf)))) \
293 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
294 memrec->memory[memrec->used++] = (p); \
299 #define OTF_CALLOC(p, size, arg) \
305 OTF_MemoryRecord *memrec \
306 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
307 (p) = calloc ((size), sizeof (*(p))); \
309 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
310 && ! (memrec = allocate_memory_record (otf)))) \
311 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
312 memrec->memory[memrec->used++] = (p); \
317 /*** (1-2) "head" table */
320 read_head_table (OTF *otf, OTF_Stream *stream)
322 char *errfmt = "head%s";
326 OTF_CALLOC (head, 1, "");
327 READ_FIXED (stream, head->TableVersionNumber);
328 READ_FIXED (stream, head->fontRevision);
329 READ_ULONG (stream, head->checkSumAdjustment);
330 READ_ULONG (stream, head->magicNumber);
331 READ_USHORT (stream, head->flags);
332 READ_USHORT (stream, head->unitsPerEm);
338 /*** (1-3) "name" table */
341 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
343 char *errfmt = "nameID (%d)";
345 OTF_StreamState state;
350 SAVE_STREAM (stream, state);
351 SEEK_STREAM (stream, stream->pos + rec->offset);
355 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
356 READ_BYTES (stream, str, rec->length);
357 for (i = 0; i < rec->length; i++)
363 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
364 for (i = 0; i < rec->length / 2; i++)
366 READ_USHORT (stream, c);
374 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
375 for (i = 0; i < rec->length / 4; i++)
377 READ_ULONG (stream, c);
384 RESTORE_STREAM (stream, state);
389 read_name_table (OTF *otf, OTF_Stream *stream)
391 char *errfmt = "name%s";
396 OTF_CALLOC (name, 1, "");
397 READ_USHORT (stream, name->format);
398 READ_USHORT (stream, name->count);
399 READ_USHORT (stream, name->stringOffset);
400 OTF_MALLOC (name->nameRecord, name->count, "");
401 for (i = 0; i < name->count; i++)
403 OTF_NameRecord *rec = name->nameRecord + i;
405 READ_USHORT (stream, rec->platformID);
406 READ_USHORT (stream, rec->encodingID);
407 READ_USHORT (stream, rec->languageID);
408 READ_USHORT (stream, rec->nameID);
409 READ_USHORT (stream, rec->length);
410 READ_USHORT (stream, rec->offset);
412 for (i = 0; i < name->count; i++)
414 OTF_NameRecord *rec = name->nameRecord + i;
415 int nameID = rec->nameID;
417 if (nameID <= OTF_max_nameID
418 && ! name->name[nameID])
420 if (rec->platformID == 0)
421 name->name[nameID] = read_name (otf, stream, rec,
422 rec->encodingID <= 3 ? 2 : 4);
423 else if (rec->platformID == 1
424 && rec->encodingID == 0)
425 name->name[nameID] = read_name (otf, stream, rec, 1);
426 else if (rec->platformID == 3
427 && (rec->encodingID == 1 || rec->encodingID == 10))
428 name->name[nameID] = read_name (otf, stream,
429 rec, rec->encodingID == 1 ? 2 : 4);
437 /*** (1-4) "cmap" table */
440 read_cmap_table (OTF *otf, OTF_Stream *stream)
442 char *errfmt = "cmap%s";
445 int unicode_index = -1;
448 OTF_CALLOC (cmap, 1, "");
449 READ_USHORT (stream, cmap->version);
450 READ_USHORT (stream, cmap->numTables);
451 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
452 for (i = 0; i < cmap->numTables; i++)
454 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
455 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
456 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
457 if (cmap->EncodingRecord[i].platformID == 3
458 && cmap->EncodingRecord[i].encodingID == 1)
461 for (i = 0; i < cmap->numTables; i++)
465 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
466 READ_USHORT (stream, format);
467 cmap->EncodingRecord[i].subtable.format = format;
468 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
469 if (format == 8 || format == 10 || format == 12)
471 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
472 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
476 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
482 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
483 " (EncodingRecord)");
485 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
495 OTF_EncodingSubtable4 *sub4;
500 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
501 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
502 READ_USHORT (stream, sub4->segCountX2);
503 segCount = sub4->segCountX2 / 2;
504 READ_USHORT (stream, sub4->searchRange);
505 READ_USHORT (stream, sub4->entrySelector);
506 READ_USHORT (stream, sub4->rangeShift);
507 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
508 for (j = 0; j < segCount; j++)
509 READ_USHORT (stream, sub4->segments[j].endCount);
510 READ_USHORT (stream, dummy);
511 for (j = 0; j < segCount; j++)
512 READ_USHORT (stream, sub4->segments[j].startCount);
513 for (j = 0; j < segCount; j++)
514 READ_SHORT (stream, sub4->segments[j].idDelta);
515 for (j = 0; j < segCount; j++)
518 unsigned rest = 2 * (segCount - j);
520 READ_USHORT (stream, off);
522 sub4->segments[j].idRangeOffset = 0xFFFF;
524 sub4->segments[j].idRangeOffset = (off - rest) / 2;
526 j = (cmap->EncodingRecord[i].subtable.length
527 - (14 + 2 * (segCount * 4 + 1)));
528 sub4->GlyphCount = j / 2;
529 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
530 for (j = 0; j < sub4->GlyphCount; j++)
531 READ_USHORT (stream, sub4->glyphIdArray[j]);
537 OTF_EncodingSubtable6 *sub6;
540 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
541 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
542 READ_USHORT (stream, sub6->firstCode);
543 READ_USHORT (stream, sub6->entryCount);
544 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
545 for (j = 0; j < sub6->entryCount; j++)
546 READ_USHORT (stream, sub6->glyphIdArray[j]);
551 if (unicode_index >= 0)
553 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_index;
554 OTF_GlyphID glyph_id, max_glyph_id = 0;
556 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
557 switch (rec->subtable.format)
561 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
562 int segCount = sub4->segCountX2 / 2;
564 for (i = 0; i < segCount; i++)
566 OTF_cmapSegument *seg = sub4->segments + i;
569 if (seg->idRangeOffset == 0xFFFF)
570 for (c = seg->startCount; c <= seg->endCount; c++)
572 glyph_id = c + seg->idDelta;
573 cmap->unicode_table[c] = glyph_id;
574 if (glyph_id > max_glyph_id)
575 max_glyph_id = glyph_id;
578 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
581 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
582 + (c - seg->startCount)];
583 cmap->unicode_table[c] = glyph_id;
584 if (glyph_id > max_glyph_id)
585 max_glyph_id = glyph_id;
591 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
592 for (i = 0; i < 0x10000; i++)
593 if (cmap->unicode_table[i])
594 cmap->decode_table[cmap->unicode_table[i]] = i;
595 cmap->max_glyph_id = max_glyph_id;
602 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
604 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
605 Glyph-IDs there. If COUNT is negative, read the number of
606 Glyphs-IDs at first. MINUS if nozero is how few the actual
607 Glyph-IDs are in STREAM than COUNT. */
610 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
611 int minus, int count)
613 char *errfmt = "GlyphID List%s";
618 READ_UINT16 (stream, count);
621 OTF_MALLOC (*ids, count, "");
622 for (i = 0; i < count + minus; i++)
623 READ_GLYPHID (stream, (*ids)[i]);
628 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
630 char *errfmt = "RangeRecord%s";
635 READ_UINT16 (stream, count);
638 OTF_MALLOC (*record, count, "");
639 for (i = 0; i < count; i++)
641 READ_GLYPHID (stream, (*record)[i].Start);
642 READ_GLYPHID (stream, (*record)[i].End);
643 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
650 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
651 OTF_Coverage *coverage)
653 char *errfmt = "Coverage%s";
655 OTF_StreamState state;
658 READ_OFFSET (stream, coverage->offset);
659 SAVE_STREAM (stream, state);
660 SEEK_STREAM (stream, offset + coverage->offset);
661 READ_UINT16 (stream, coverage->CoverageFormat);
662 if (coverage->CoverageFormat == 1)
663 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
664 else if (coverage->CoverageFormat == 2)
665 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
667 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
670 coverage->Count = (unsigned) count;
671 RESTORE_STREAM (stream, state);
675 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
676 and store the Coverages there. If COUNT is negative, read the
677 number of Coverages at first. */
680 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
681 OTF_Coverage **coverage, int count)
683 char *errfmt = "Coverage List%s";
688 READ_UINT16 (stream, count);
691 OTF_MALLOC (*coverage, count, "");
692 for (i = 0; i < count; i++)
693 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
700 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
703 char *errfmt = "ClassDef%s";
706 SEEK_STREAM (stream, class->offset);
707 READ_UINT16 (stream, class->ClassFormat);
708 if (class->ClassFormat == 1)
710 READ_GLYPHID (stream, class->f.f1.StartGlyph);
711 class->f.f1.GlyphCount
713 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
714 if (! class->f.f1.GlyphCount)
715 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
717 else if (class->ClassFormat == 2)
719 class->f.f2.ClassRangeCount
720 = (read_range_records
721 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
722 if (! class->f.f2.ClassRangeCount)
723 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
726 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
732 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
734 char *errfmt = "ClassDef%s";
736 OTF_StreamState state;
738 READ_OFFSET (stream, class->offset);
741 SAVE_STREAM (stream, state);
742 SEEK_STREAM (stream, offset + class->offset);
743 READ_UINT16 (stream, class->ClassFormat);
744 if (class->ClassFormat == 1)
746 READ_GLYPHID (stream, class->f.f1.StartGlyph);
747 class->f.f1.GlyphCount
748 = read_glyph_ids (otf, stream,
749 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
751 if (! class->f.f1.GlyphCount)
754 else if (class->ClassFormat == 2)
756 class->f.f2.ClassRangeCount
757 = read_range_records (otf, stream,
759 &class->f.f2.ClassRangeRecord);
760 if (! class->f.f2.ClassRangeCount)
764 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
766 RESTORE_STREAM (stream, state);
772 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
773 OTF_DeviceTable *table)
775 char *errfmt = "Device Table%s";
786 SEEK_STREAM (stream, offset + table->offset);
787 READ_UINT16 (stream, table->StartSize);
788 READ_UINT16 (stream, table->EndSize);
789 READ_UINT16 (stream, table->DeltaFormat);
790 num = table->EndSize - table->StartSize + 1;
791 OTF_MALLOC (table->DeltaValue, num, "");
793 if (table->DeltaFormat == 1)
794 for (i = 0; i < num; i++)
797 READ_UINT16 (stream, val);
798 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
799 table->DeltaValue[i] = intval.int2;
801 else if (table->DeltaFormat == 2)
802 for (i = 0; i < num; i++)
805 READ_UINT16 (stream, val);
806 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
807 table->DeltaValue[i] = intval.int4;
809 else if (table->DeltaFormat == 3)
810 for (i = 0; i < num; i++)
814 READ_UINT16 (stream, val);
815 intval.int8 = val >> 8;
816 table->DeltaValue[i] = intval.int8;
820 intval.int8 = val >> 8;
821 table->DeltaValue[i] = intval.int8;
825 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
830 /*** (1-6) "GDEF" table */
833 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
834 OTF_AttachList *list)
836 char *errfmt = "AttachList%s";
840 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
842 READ_UINT16 (stream, list->GlyphCount);
843 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
844 for (i = 0; i < list->GlyphCount; i++)
845 READ_OFFSET (stream, list->AttachPoint[i].offset);
846 for (i = 0; i < list->GlyphCount; i++)
850 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
851 READ_UINT16 (stream, count);
852 list->AttachPoint[i].PointCount = count;
853 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
854 for (j = 0; j < count; j++)
855 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
861 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
862 OTF_CaretValue *caret)
864 char *errfmt = "CaretValue%s";
867 SEEK_STREAM (stream, offset + caret->offset);
868 READ_UINT16 (stream, caret->CaretValueFormat);
869 if (caret->CaretValueFormat == 1)
870 READ_INT16 (stream, caret->f.f1.Coordinate);
871 else if (caret->CaretValueFormat == 2)
872 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
873 else if (caret->CaretValueFormat == 3)
875 READ_INT16 (stream, caret->f.f3.Coordinate);
876 if (read_device_table (otf, stream, offset + caret->offset,
877 &caret->f.f3.DeviceTable) < 0)
881 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
886 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
887 OTF_LigCaretList *list)
889 char *errfmt = "LigCaretList%s";
893 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
895 READ_UINT16 (stream, list->LigGlyphCount);
896 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
897 for (i = 0; i < list->LigGlyphCount; i++)
898 READ_OFFSET (stream, list->LigGlyph[i].offset);
899 for (i = 0; i < list->LigGlyphCount; i++)
903 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
904 READ_UINT16 (stream, count);
905 list->LigGlyph[i].CaretCount = count;
906 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
907 for (j = 0; j < count; j++)
908 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
909 for (j = 0; j < count; j++)
910 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
911 &list->LigGlyph[i].CaretValue[j]) < 0)
918 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
922 READ_FIXED (stream, header->Version);
923 READ_OFFSET (stream, header->GlyphClassDef);
924 READ_OFFSET (stream, header->AttachList);
925 READ_OFFSET (stream, header->LigCaretList);
926 READ_OFFSET (stream, header->MarkAttachClassDef);
931 read_gdef_table (OTF *otf, OTF_Stream *stream)
933 char *errfmt = "GDEF%s";
937 OTF_CALLOC (gdef, 1, "");
938 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
939 if (gdef->header.GlyphClassDef)
941 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
942 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
944 if (gdef->header.AttachList)
945 read_attach_list (otf, stream, gdef->header.AttachList,
947 if (gdef->header.LigCaretList)
948 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
949 &gdef->lig_caret_list);
950 if (gdef->header.MarkAttachClassDef)
952 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
953 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
960 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
963 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
964 OTF_ScriptList *list)
966 char *errfmt = "Script List%s";
970 SEEK_STREAM (stream, offset);
971 READ_USHORT (stream, list->ScriptCount);
972 OTF_CALLOC (list->Script, list->ScriptCount, "");
974 for (i = 0; i < list->ScriptCount; i++)
976 READ_TAG (stream, list->Script[i].ScriptTag);
977 READ_OFFSET (stream, list->Script[i].offset);
979 for (i = 0; i < list->ScriptCount; i++)
981 OTF_Script *script = list->Script + i;
982 long script_offset = offset + script->offset;
984 SEEK_STREAM (stream, script_offset);
985 READ_OFFSET (stream, script->DefaultLangSysOffset);
986 READ_USHORT (stream, script->LangSysCount);
987 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
988 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
989 for (j = 0; j < script->LangSysCount; j++)
991 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
992 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
995 if (script->DefaultLangSysOffset)
997 OTF_LangSys *langsys = &script->DefaultLangSys;
999 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1000 READ_OFFSET (stream, langsys->LookupOrder);
1001 READ_USHORT (stream, langsys->ReqFeatureIndex);
1002 READ_USHORT (stream, langsys->FeatureCount);
1003 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1005 for (k = 0; k < langsys->FeatureCount; k++)
1006 READ_USHORT (stream, langsys->FeatureIndex[k]);
1009 for (j = 0; j < script->LangSysCount; j++)
1011 OTF_LangSys *langsys = script->LangSys + j;
1013 SEEK_STREAM (stream,
1014 script_offset + script->LangSysRecord[j].LangSys);
1015 READ_OFFSET (stream, langsys->LookupOrder);
1016 READ_USHORT (stream, langsys->ReqFeatureIndex);
1017 READ_USHORT (stream, langsys->FeatureCount);
1018 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1020 for (k = 0; k < langsys->FeatureCount; k++)
1021 READ_USHORT (stream, langsys->FeatureIndex[k]);
1029 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1030 OTF_FeatureList *list)
1032 char *errfmt = "Feature List%s";
1036 READ_UINT16 (stream, list->FeatureCount);
1037 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1038 for (i = 0; i < list->FeatureCount; i++)
1040 READ_TAG (stream, list->Feature[i].FeatureTag);
1041 READ_OFFSET (stream, list->Feature[i].offset);
1043 for (i = 0; i < list->FeatureCount; i++)
1045 OTF_Feature *feature = list->Feature + i;
1047 SEEK_STREAM (stream, offset + feature->offset);
1048 READ_OFFSET (stream, feature->FeatureParams);
1049 READ_UINT16 (stream, feature->LookupCount);
1050 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1051 " (LookupListIndex)");
1052 for (j = 0; j < feature->LookupCount; j++)
1053 READ_UINT16 (stream, feature->LookupListIndex[j]);
1059 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1060 long offset, unsigned type,
1061 OTF_LookupSubTableGSUB *subtable);
1062 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1063 long offset, unsigned type,
1064 OTF_LookupSubTableGPOS *subtable);
1067 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1068 OTF_LookupList *list, int gsubp)
1070 char *errfmt = "Lookup List%s";
1074 SEEK_STREAM (stream, offset);
1075 READ_UINT16 (stream, list->LookupCount);
1076 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1078 for (i = 0; i < list->LookupCount; i++)
1079 READ_OFFSET (stream, list->Lookup[i].offset);
1080 for (i = 0; i < list->LookupCount; i++)
1082 OTF_Lookup *lookup = list->Lookup + i;
1084 SEEK_STREAM (stream, offset + lookup->offset);
1085 READ_UINT16 (stream, lookup->LookupType);
1086 READ_UINT16 (stream, lookup->LookupFlag);
1087 READ_UINT16 (stream, lookup->SubTableCount);
1088 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1089 " (SubTableOffset)");
1091 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1094 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1096 for (j = 0; j < lookup->SubTableCount; j++)
1097 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1098 for (j = 0; j < lookup->SubTableCount; j++)
1101 = offset + lookup->offset + lookup->SubTableOffset[j];
1104 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1106 lookup->SubTable.gsub + j) < 0
1107 : read_lookup_subtable_gpos (otf, stream, this_offset,
1109 lookup->SubTable.gpos + j) < 0)
1118 /*** (1-8) Structures common to GSUB and GPOS */
1121 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1122 OTF_LookupRecord **record, int count)
1124 char *errfmt = "LookupRecord%s";
1125 unsigned errret = 0;
1129 READ_UINT16 (stream, count);
1131 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1132 OTF_MALLOC (*record, count, "");
1133 for (i = 0; i < count; i++)
1135 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1136 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1142 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1144 char *errfmt = "List of Rule%s";
1145 unsigned errret = 0;
1146 OTF_StreamState state;
1150 READ_UINT16 (stream, count);
1152 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1153 OTF_MALLOC (*rule, count, "");
1154 for (i = 0; i < count; i++)
1156 READ_OFFSET (stream, (*rule)[i].offset);
1157 if (! (*rule)[i].offset)
1158 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1160 SAVE_STREAM (stream, state);
1161 for (i = 0; i < count; i++)
1163 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1164 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1165 if ((*rule)[i].GlyphCount == 0)
1166 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1167 READ_UINT16 (stream, (*rule)[i].LookupCount);
1168 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1169 (*rule)[i].GlyphCount) < 0)
1171 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1172 (*rule)[i].LookupCount) == 0)
1175 RESTORE_STREAM (stream, state);
1181 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1184 char *errfmt = "List of RuleSet%s";
1185 unsigned errret = 0;
1186 OTF_StreamState state;
1190 READ_UINT16 (stream, count);
1192 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1193 OTF_MALLOC (*set, count, "");
1194 for (i = 0; i < count; i++)
1196 READ_OFFSET (stream, (*set)[i].offset);
1197 if (! (*set)[i].offset)
1198 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1200 SAVE_STREAM (stream, state);
1201 for (i = 0; i < count; i++)
1203 SEEK_STREAM (stream, offset + (*set)[i].offset);
1205 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1207 if (! (*set)[i].RuleCount)
1210 RESTORE_STREAM (stream, state);
1215 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1216 OTF_ClassRule **rule)
1218 char *errfmt = "ClassRule%s";
1219 unsigned errret = 0;
1220 OTF_StreamState state;
1224 READ_UINT16 (stream, count);
1226 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1227 OTF_MALLOC (*rule, count, "");
1228 for (i = 0; i < count; i++)
1230 READ_OFFSET (stream, (*rule)[i].offset);
1231 if (! (*rule)[i].offset)
1232 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1234 SAVE_STREAM (stream, state);
1235 for (i = 0; i < count; i++)
1237 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1238 READ_USHORT (stream, (*rule)[i].GlyphCount);
1239 if (! (*rule)[i].GlyphCount)
1240 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1241 READ_USHORT (stream, (*rule)[i].LookupCount);
1242 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1243 0, (*rule)[i].GlyphCount - 1) < 0)
1245 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1246 (*rule)[i].LookupCount) == 0)
1249 RESTORE_STREAM (stream, state);
1254 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1257 char *errfmt = "ClassSet%s";
1258 unsigned errret = 0;
1259 OTF_StreamState state;
1263 READ_UINT16 (stream, count);
1265 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1266 OTF_MALLOC (*set, count, "");
1267 for (i = 0; i < count; i++)
1268 /* Offset can be zero. */
1269 READ_OFFSET (stream, (*set)[i].offset);
1270 SAVE_STREAM (stream, state);
1271 for (i = 0; i < count; i++)
1272 if ((*set)[i].offset)
1274 SEEK_STREAM (stream, offset + (*set)[i].offset);
1275 (*set)[i].ClassRuleCnt
1276 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1277 &(*set)[i].ClassRule);
1278 if (! (*set)[i].ClassRuleCnt)
1281 RESTORE_STREAM (stream, state);
1286 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1287 OTF_ChainRule **rule)
1289 char *errfmt = "ChainRule%s";
1290 unsigned errret = 0;
1294 READ_UINT16 (stream, count);
1296 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1297 OTF_MALLOC (*rule, count, "");
1298 for (i = 0; i < count; i++)
1299 READ_OFFSET (stream, (*rule)[i].offset);
1300 for (i = 0; i < count; i++)
1302 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1303 (*rule)[i].BacktrackGlyphCount
1304 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1305 (*rule)[i].InputGlyphCount
1306 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1307 if (! (*rule)[i].InputGlyphCount)
1308 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1309 (*rule)[i].LookaheadGlyphCount
1310 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1311 (*rule)[i].LookupCount
1312 = read_lookup_record_list (otf, stream,
1313 &(*rule)[i].LookupRecord, -1);
1314 if (! (*rule)[i].LookupCount)
1322 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1323 OTF_ChainRuleSet **set)
1325 char *errfmt = "ChainRuleSet%s";
1326 unsigned errret = 0;
1327 OTF_StreamState state;
1331 READ_UINT16 (stream, count);
1333 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1334 OTF_MALLOC (*set, count, "");
1335 for (i = 0; i < count; i++)
1337 READ_OFFSET (stream, (*set)[i].offset);
1338 if (! (*set)[i].offset)
1339 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1341 SAVE_STREAM (stream, state);
1342 for (i = 0; i < count; i++)
1344 SEEK_STREAM (stream, offset + (*set)[i].offset);
1345 (*set)[i].ChainRuleCount
1346 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1347 &(*set)[i].ChainRule);
1348 if (! (*set)[i].ChainRuleCount)
1351 RESTORE_STREAM (stream, state);
1356 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1357 OTF_ChainClassRule **rule)
1359 char *errfmt = "ChainClassRule%s";
1360 unsigned errret = 0;
1364 READ_UINT16 (stream, count);
1366 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1367 OTF_MALLOC (*rule, count, "");
1368 for (i = 0; i < count; i++)
1370 READ_OFFSET (stream, (*rule)[i].offset);
1371 if (! (*rule)[i].offset)
1372 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1374 for (i = 0; i < count; i++)
1376 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1377 (*rule)[i].BacktrackGlyphCount
1378 = read_glyph_ids (otf, stream,
1379 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1380 (*rule)[i].InputGlyphCount
1381 = read_glyph_ids (otf, stream,
1382 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1383 if (! (*rule)[i].InputGlyphCount)
1384 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1385 (*rule)[i].LookaheadGlyphCount
1386 = read_glyph_ids (otf, stream,
1387 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1388 (*rule)[i].LookupCount
1389 = read_lookup_record_list (otf, stream,
1390 &(*rule)[i].LookupRecord, -1);
1391 if (! (*rule)[i].LookupCount)
1398 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1399 OTF_ChainClassSet **set)
1401 char *errfmt = "ChainClassSet%s";
1402 unsigned errret = 0;
1403 OTF_StreamState state;
1407 READ_UINT16 (stream, count);
1409 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1410 OTF_MALLOC (*set, count, "");
1411 for (i = 0; i < count; i++)
1412 /* Offset may be zero. */
1413 READ_OFFSET (stream, (*set)[i].offset);
1414 SAVE_STREAM (stream, state);
1415 for (i = 0; i < count; i++)
1416 if ((*set)[i].offset)
1418 SEEK_STREAM (stream, offset + (*set)[i].offset);
1419 (*set)[i].ChainClassRuleCnt
1420 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1421 &(*set)[i].ChainClassRule);
1422 if (! (*set)[i].ChainClassRuleCnt)
1425 RESTORE_STREAM (stream, state);
1430 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1431 OTF_Coverage *coverage,OTF_Context1 *context1)
1433 if (read_coverage (otf, stream, offset, coverage) < 0)
1435 context1->RuleSetCount
1436 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1437 if (! context1->RuleSetCount)
1443 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1444 OTF_Coverage *coverage,OTF_Context2 *context2)
1446 if (read_coverage (otf, stream, offset, coverage) < 0
1447 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1449 context2->ClassSetCnt
1450 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1451 if (! context2->ClassSetCnt)
1457 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1458 OTF_Coverage *coverage,OTF_Context3 *context3)
1460 char *errfmt = "Context1%s";
1463 READ_USHORT (stream, context3->GlyphCount);
1464 if (context3->GlyphCount < 0)
1465 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1466 READ_USHORT (stream, context3->LookupCount);
1467 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1468 context3->GlyphCount) < 0)
1470 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1471 context3->LookupCount) < 0)
1477 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1478 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1480 if (read_coverage (otf, stream, offset, coverage) < 0)
1482 chain_context1->ChainRuleSetCount
1483 = read_chain_rule_set_list (otf, stream, offset,
1484 &chain_context1->ChainRuleSet);
1485 if (! chain_context1->ChainRuleSetCount)
1491 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1492 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1494 if (read_coverage (otf, stream, offset, coverage) < 0
1495 || read_class_def (otf, stream, offset,
1496 &chain_context2->BacktrackClassDef) < 0
1497 || read_class_def (otf, stream, offset,
1498 &chain_context2->InputClassDef) < 0
1499 || read_class_def (otf, stream, offset,
1500 &chain_context2->LookaheadClassDef) < 0)
1502 chain_context2->ChainClassSetCnt
1503 = read_chain_class_set_list (otf, stream, offset,
1504 &chain_context2->ChainClassSet);
1505 if (! chain_context2->ChainClassSetCnt)
1511 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1512 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1516 count = read_coverage_list (otf, stream, offset,
1517 &chain_context3->Backtrack, -1);
1520 chain_context3->BacktrackGlyphCount = (unsigned) count;
1521 count = read_coverage_list (otf, stream, offset,
1522 &chain_context3->Input, -1);
1525 chain_context3->InputGlyphCount = (unsigned) count;
1526 *coverage = chain_context3->Input[0];
1527 count = read_coverage_list (otf, stream, offset,
1528 &chain_context3->LookAhead, -1);
1529 chain_context3->LookaheadGlyphCount = (unsigned) count;
1530 chain_context3->LookupCount
1531 = read_lookup_record_list (otf, stream,
1532 &chain_context3->LookupRecord, -1);
1537 read_gsub_gpos_table (OTF *otf, OTF_Stream *stream, int gsubp)
1539 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1540 void *errret = NULL;
1541 OTF_GSUB_GPOS *gsub_gpos;
1543 OTF_CALLOC (gsub_gpos, 1, "");
1544 READ_FIXED (stream, gsub_gpos->Version);
1545 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1546 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1547 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1549 if (read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1550 &gsub_gpos->ScriptList) < 0
1551 || read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1552 &gsub_gpos->FeatureList) < 0
1553 || read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1554 &gsub_gpos->LookupList, gsubp) < 0)
1560 /* (1-9) "GSUB" table */
1563 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1565 char *errfmt = "Sequence%s";
1566 unsigned errret = 0;
1570 READ_UINT16 (stream, count);
1572 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1573 OTF_MALLOC (*seq, count, "");
1574 for (i = 0; i < count; i++)
1575 READ_OFFSET (stream, (*seq)[i].offset);
1576 for (i = 0; i < count; i++)
1578 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1579 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1580 &(*seq)[i].Substitute, 0, -1);
1581 if (! (*seq)[i].GlyphCount)
1588 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1589 OTF_Ligature **ligature)
1591 char *errfmt = "Ligature%s";
1596 READ_UINT16 (stream, count);
1599 OTF_MALLOC (*ligature, count, "");
1600 for (i = 0; i < count; i++)
1601 READ_OFFSET (stream, (*ligature)[i].offset);
1602 for (i = 0; i < count; i++)
1604 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1605 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1606 (*ligature)[i].CompCount
1607 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1608 if (! (*ligature)[i].CompCount)
1615 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1616 OTF_LigatureSet **ligset)
1618 char *errfmt = "LigatureSet%s";
1623 READ_UINT16 (stream, count);
1626 OTF_MALLOC (*ligset, count, "");
1627 for (i = 0; i < count; i++)
1628 READ_OFFSET (stream, (*ligset)[i].offset);
1629 for (i = 0; i < count; i++)
1633 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1634 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1635 &(*ligset)[i].Ligature);
1638 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1644 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1645 OTF_AlternateSet **altset)
1647 char *errfmt = "AlternateSet%s";
1652 READ_UINT16 (stream, count);
1654 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1655 OTF_MALLOC (*altset, count, "");
1656 for (i = 0; i < count; i++)
1657 READ_OFFSET (stream, (*altset)[i].offset);
1658 for (i = 0; i < count; i++)
1662 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1663 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1666 (*altset)[i].GlyphCount = (unsigned) alt_count;
1672 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1673 OTF_Coverage *coverage,
1674 OTF_GSUB_ReverseChain1 *reverse_chain)
1678 if (read_coverage (otf, stream, offset, coverage) < 0)
1680 count = read_coverage_list (otf, stream, offset,
1681 &reverse_chain->Backtrack, -1);
1684 reverse_chain->BacktrackGlyphCount = (unsigned) count;
1685 count = read_coverage_list (otf, stream, offset,
1686 &reverse_chain->LookAhead, -1);
1689 reverse_chain->LookaheadGlyphCount = (unsigned) count;
1690 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
1693 reverse_chain->GlyphCount = count;
1698 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1699 unsigned type, OTF_LookupSubTableGSUB *subtable)
1704 SEEK_STREAM (stream, offset);
1705 READ_USHORT (stream, subtable->Format);
1706 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1710 if (subtable->Format == 1)
1712 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1714 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1716 else if (subtable->Format == 2)
1718 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1720 subtable->u.single2.GlyphCount
1721 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1723 if (! subtable->u.single2.GlyphCount)
1727 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1731 if (subtable->Format == 1)
1733 read_coverage (otf, stream, offset, &subtable->Coverage);
1734 subtable->u.multiple1.SequenceCount
1735 = read_sequence (otf, stream, offset,
1736 &subtable->u.multiple1.Sequence);
1739 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1743 if (subtable->Format == 1)
1745 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1747 subtable->u.alternate1.AlternateSetCount
1748 = read_alternate_set_list (otf, stream, offset,
1749 &subtable->u.alternate1.AlternateSet);
1750 if (! subtable->u.alternate1.AlternateSetCount)
1754 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1758 if (subtable->Format == 1)
1760 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1762 subtable->u.ligature1.LigSetCount
1763 = read_ligature_set_list (otf, stream, offset,
1764 &subtable->u.ligature1.LigatureSet);
1765 if (! subtable->u.ligature1.LigSetCount)
1769 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1773 if (subtable->Format == 1)
1775 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1776 &subtable->u.context1) < 0)
1779 else if (subtable->Format == 2)
1781 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1782 &subtable->u.context2) < 0)
1785 else if (subtable->Format == 3)
1787 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1788 &subtable->u.context3) < 0)
1792 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1796 if (subtable->Format == 1)
1798 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1799 &subtable->u.chain_context1) < 0)
1802 else if (subtable->Format == 2)
1804 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1805 &subtable->u.chain_context2) < 0)
1808 else if (subtable->Format == 3)
1810 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1811 &subtable->u.chain_context3) < 0)
1815 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1819 if (subtable->Format == 1)
1823 OTF_LookupSubTableGSUB *ex_subtable;
1825 READ_USHORT (stream, ex_type);
1826 READ_ULONG (stream, ex_offset);
1827 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
1828 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
1829 ex_type, ex_subtable) < 0)
1831 subtable->u.extension1.ExtensionLookupType = ex_type;
1832 subtable->u.extension1.ExtensionOffset = ex_offset;
1833 subtable->u.extension1.ExtensionSubtable = ex_subtable;
1836 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1840 if (subtable->Format == 1)
1842 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
1843 &subtable->u.reverse_chain1) < 0)
1847 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1851 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1857 read_gsub_table (OTF *otf, OTF_Stream *stream)
1859 return read_gsub_gpos_table (otf, stream, 1);
1863 /* (1-10) "GPOS" table */
1866 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1867 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1870 OTF_StreamState state;
1875 for (i = 0, size = 0; i < 8; i++)
1879 if (bit & OTF_XPlacement)
1880 READ_INT16 (stream, value_record->XPlacement);
1881 if (bit & OTF_XPlacement)
1882 READ_INT16 (stream, value_record->YPlacement);
1883 if (bit & OTF_XAdvance)
1884 READ_INT16 (stream, value_record->XAdvance);
1885 if (bit & OTF_YAdvance)
1886 READ_INT16 (stream, value_record->YAdvance);
1887 if (bit & OTF_XPlaDevice)
1888 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1889 if (bit & OTF_YPlaDevice)
1890 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1891 if (bit & OTF_XAdvDevice)
1892 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1893 if (bit & OTF_YAdvDevice)
1894 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1895 SAVE_STREAM (stream, state);
1896 if (value_record->XPlaDevice.offset)
1898 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1901 if (value_record->YPlaDevice.offset)
1903 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1906 if (value_record->XAdvDevice.offset)
1908 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1911 if (value_record->YAdvDevice.offset)
1913 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1916 RESTORE_STREAM (stream, state);
1922 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1924 char *errfmt = "Anchor%s";
1927 SEEK_STREAM (stream, offset + anchor->offset);
1928 READ_UINT16 (stream, anchor->AnchorFormat);
1929 READ_INT16 (stream, anchor->XCoordinate);
1930 READ_INT16 (stream, anchor->YCoordinate);
1931 if (anchor->AnchorFormat == 1)
1933 else if (anchor->AnchorFormat == 2)
1935 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1937 else if (anchor->AnchorFormat == 3)
1939 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1940 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1941 if (anchor->f.f2.XDeviceTable.offset)
1943 if (read_device_table (otf, stream, offset + anchor->offset,
1944 &anchor->f.f2.XDeviceTable) < 0)
1947 if (anchor->f.f2.YDeviceTable.offset)
1949 if (read_device_table (otf, stream, offset + anchor->offset,
1950 &anchor->f.f2.YDeviceTable) < 0)
1955 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1961 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1962 OTF_MarkArray *array)
1964 char *errfmt = "MarkArray%s";
1966 OTF_StreamState state;
1969 READ_OFFSET (stream, array->offset);
1970 SAVE_STREAM (stream, state);
1971 SEEK_STREAM (stream, offset + array->offset);
1972 READ_UINT16 (stream, array->MarkCount);
1973 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1974 for (i = 0; i < array->MarkCount; i++)
1976 READ_UINT16 (stream, array->MarkRecord[i].Class);
1977 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1979 for (i = 0; i < array->MarkCount; i++)
1980 if (read_anchor (otf, stream, offset + array->offset,
1981 &array->MarkRecord[i].MarkAnchor) < 0)
1983 RESTORE_STREAM (stream, state);
1988 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1989 unsigned ClassCount, OTF_AnchorArray *array)
1991 char *errfmt = "AnchorArray%s";
1993 OTF_StreamState state;
1996 READ_OFFSET (stream, array->offset);
1997 SAVE_STREAM (stream, state);
1998 SEEK_STREAM (stream, offset + array->offset);
1999 READ_UINT16 (stream, array->Count);
2000 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2001 for (i = 0; i < array->Count; i++)
2003 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2005 for (j = 0; j < ClassCount; j++)
2006 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2008 for (i = 0; i < array->Count; i++)
2009 for (j = 0; j < ClassCount; j++)
2010 if (read_anchor (otf, stream, offset + array->offset,
2011 &array->AnchorRecord[i].Anchor[j]) < 0)
2013 RESTORE_STREAM (stream, state);
2017 static OTF_PairSet *
2018 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2019 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2021 char *errfmt = "PairSet%s";
2022 void *errret = NULL;
2023 OTF_StreamState state;
2027 OTF_MALLOC (set, num, "");
2028 for (i = 0; i < num; i++)
2029 READ_OFFSET (stream, set[i].offset);
2030 SAVE_STREAM (stream, state);
2031 for (i = 0; i < num; i++)
2033 SEEK_STREAM (stream, offset + set[i].offset);
2034 READ_UINT16 (stream, set[i].PairValueCount);
2035 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2036 for (j = 0; j < set[i].PairValueCount; j++)
2038 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2040 READ_UINT16 (stream, rec->SecondGlyph);
2041 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2042 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2045 RESTORE_STREAM (stream, state);
2049 static OTF_Class1Record *
2050 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2051 unsigned num1, enum OTF_ValueFormat bit1,
2052 unsigned num2, enum OTF_ValueFormat bit2)
2054 char *errfmt = "Class1Record%s";
2055 void *errret = NULL;
2056 OTF_Class1Record *rec;
2059 OTF_MALLOC (rec, num1, "");
2060 for (i = 0; i < num1; i++)
2062 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2063 for (j = 0; j < num2; j++)
2065 if (read_value_record (otf, stream, offset,
2066 bit1, &rec[i].Class2Record[j].Value1) < 0
2067 || read_value_record (otf, stream, offset,
2068 bit2, &rec[i].Class2Record[j].Value2) < 0)
2076 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2077 OTF_EntryExitRecord **rec)
2079 char *errfmt = "EntryExitSet%s";
2083 OTF_StreamState state;
2085 READ_UINT16 (stream, count);
2087 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2088 OTF_MALLOC (*rec, count, "");
2089 for (i = 0; i < count; i++)
2091 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2092 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2094 SAVE_STREAM (stream, state);
2095 for (i = 0; i < count; i++)
2097 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2099 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2102 RESTORE_STREAM (stream, state);
2107 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2108 unsigned ClassCount, OTF_LigatureAttach *attach)
2110 char *errfmt = "LigatureAttach%s";
2114 SEEK_STREAM (stream, offset + attach->offset);
2115 READ_UINT16 (stream, attach->ComponentCount);
2116 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2117 for (i = 0; i < attach->ComponentCount; i++)
2119 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2120 " (ComponentRecord)");
2121 for (j = 0; j < ClassCount; j++)
2122 READ_OFFSET (stream,
2123 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2125 for (i = 0; i < attach->ComponentCount; i++)
2126 for (j = 0; j < ClassCount; j++)
2127 if (read_anchor (otf, stream, offset + attach->offset,
2128 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2134 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2135 unsigned class_count, OTF_LigatureArray *array)
2137 char *errfmt = "LigatureArray%s";
2139 OTF_StreamState state;
2142 READ_OFFSET (stream, array->offset);
2143 SAVE_STREAM (stream, state);
2144 SEEK_STREAM (stream, offset + array->offset);
2145 READ_UINT16 (stream, array->LigatureCount);
2146 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2147 for (i = 0; i < array->LigatureCount; i++)
2148 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2149 for (i = 0; i < array->LigatureCount; i++)
2150 read_ligature_attach (otf, stream, offset + array->offset,
2151 class_count, array->LigatureAttach + i);
2152 RESTORE_STREAM (stream, state);
2157 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2158 long offset, unsigned type,
2159 OTF_LookupSubTableGPOS *subtable)
2164 SEEK_STREAM (stream, offset);
2165 READ_UINT16 (stream, subtable->Format);
2166 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2170 if (subtable->Format == 1)
2172 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2173 read_value_record (otf, stream, offset,
2174 subtable->u.single1.ValueFormat,
2175 &subtable->u.single1.Value);
2177 else if (subtable->Format == 2)
2179 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2182 READ_UINT16 (stream, single2->ValueFormat);
2183 READ_UINT16 (stream, single2->ValueCount);
2184 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2185 for (i = 0; i < single2->ValueCount; i++)
2186 read_value_record (otf, stream, offset, single2->ValueFormat,
2187 single2->Value + i);
2190 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2194 if (subtable->Format == 1)
2196 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2198 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2199 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2200 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2201 subtable->u.pair1.PairSet
2202 = read_pair_set_list (otf, stream, offset,
2203 subtable->u.pair1.PairSetCount,
2204 subtable->u.pair1.ValueFormat1,
2205 subtable->u.pair1.ValueFormat2);
2206 if (! subtable->u.pair1.PairSet)
2209 else if (subtable->Format == 2)
2211 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2213 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2214 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2215 if (read_class_def (otf, stream, offset,
2216 &subtable->u.pair2.ClassDef1) < 0
2217 || read_class_def (otf, stream, offset,
2218 &subtable->u.pair2.ClassDef2) < 0)
2220 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2221 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2222 subtable->u.pair2.Class1Record
2223 = read_class1_record_list (otf, stream, offset,
2224 subtable->u.pair2.Class1Count,
2225 subtable->u.pair2.ValueFormat1,
2226 subtable->u.pair2.Class2Count,
2227 subtable->u.pair2.ValueFormat2);
2228 if (! subtable->u.pair2.Class1Record)
2232 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2236 if (subtable->Format == 1)
2238 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2240 subtable->u.cursive1.EntryExitCount
2241 = read_entry_exit_list (otf, stream, offset,
2242 &subtable->u.cursive1.EntryExitRecord);
2243 if (! subtable->u.cursive1.EntryExitCount)
2247 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2251 if (subtable->Format == 1)
2253 read_coverage (otf, stream, offset, &subtable->Coverage);
2254 read_coverage (otf, stream, offset,
2255 &subtable->u.mark_base1.BaseCoverage);
2256 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2257 read_mark_array (otf, stream, offset,
2258 &subtable->u.mark_base1.MarkArray);
2259 read_anchor_array (otf, stream, offset,
2260 subtable->u.mark_base1.ClassCount,
2261 &subtable->u.mark_base1.BaseArray);
2264 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2268 if (subtable->Format == 1)
2270 read_coverage (otf, stream, offset, &subtable->Coverage);
2271 read_coverage (otf, stream, offset,
2272 &subtable->u.mark_lig1.LigatureCoverage);
2273 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2274 read_mark_array (otf, stream, offset,
2275 &subtable->u.mark_lig1.MarkArray);
2276 read_ligature_array (otf, stream, offset,
2277 subtable->u.mark_lig1.ClassCount,
2278 &subtable->u.mark_lig1.LigatureArray);
2283 if (subtable->Format == 1)
2285 read_coverage (otf, stream, offset, &subtable->Coverage);
2286 read_coverage (otf, stream, offset,
2287 &subtable->u.mark_mark1.Mark2Coverage);
2288 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2289 read_mark_array (otf, stream, offset,
2290 &subtable->u.mark_mark1.Mark1Array);
2291 read_anchor_array (otf, stream, offset,
2292 subtable->u.mark_mark1.ClassCount,
2293 &subtable->u.mark_mark1.Mark2Array);
2296 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2300 if (subtable->Format == 1)
2302 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2303 &subtable->u.context1) < 0)
2306 else if (subtable->Format == 2)
2308 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2309 &subtable->u.context2) < 0)
2312 else if (subtable->Format == 3)
2314 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2315 &subtable->u.context3) < 0)
2319 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2323 if (subtable->Format == 1)
2325 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2326 &subtable->u.chain_context1) < 0)
2329 else if (subtable->Format == 2)
2331 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2332 &subtable->u.chain_context2) < 0)
2335 else if (subtable->Format == 3)
2337 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2338 &subtable->u.chain_context3) < 0)
2342 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2346 if (subtable->Format == 1)
2350 OTF_LookupSubTableGPOS *ex_subtable;
2352 READ_USHORT (stream, ex_type);
2353 READ_ULONG (stream, ex_offset);
2354 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2355 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2356 ex_type, ex_subtable) < 0)
2358 subtable->u.extension1.ExtensionLookupType = ex_type;
2359 subtable->u.extension1.ExtensionOffset = ex_offset;
2360 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2363 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2367 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2373 read_gpos_table (OTF *otf, OTF_Stream *stream)
2375 return read_gsub_gpos_table (otf, stream, 0);
2383 read_base_table (OTF_Stream *stream, long offset)
2387 OTF_MALLOC (base, 1);
2396 read_jstf_table (OTF_Stream *stream, long offset)
2400 OTF_MALLOC (jstf, 1);
2406 /*** (1-11) Structure for OTF */
2409 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2413 READ_FIXED (stream, table->sfnt_version);
2414 READ_USHORT (stream, table->numTables);
2415 READ_USHORT (stream, table->searchRange);
2416 READ_USHORT (stream, table->enterSelector);
2417 READ_USHORT (stream, table->rangeShift);
2422 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2427 READ_TAG (stream, tag);
2429 table->name[0] = tag >> 24;
2430 table->name[1] = (tag >> 16) & 0xFF;
2431 table->name[0] = (tag >> 8) & 0xFF;
2432 table->name[0] = tag >> 8;
2433 table->name[0] = '\0';
2434 READ_ULONG (stream, table->checkSum);
2435 READ_ULONG (stream, table->offset);
2436 READ_ULONG (stream, table->length);
2441 read_header_part (OTF *otf, FILE *fp)
2443 char *errfmt = "otf header%s";
2445 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2448 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2450 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2451 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2452 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2453 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2454 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2455 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2456 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2457 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2458 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2459 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2460 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2461 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2463 head_tag = OTF_tag ("head");
2464 name_tag = OTF_tag ("name");
2465 cmap_tag = OTF_tag ("cmap");
2466 gdef_tag = OTF_tag ("GDEF");
2467 gsub_tag = OTF_tag ("GSUB");
2468 gpos_tag = OTF_tag ("GPOS");
2470 stream = make_stream ();
2474 internal_data->header_stream = stream;
2476 /* Size of Offset Table is 12 bytes. */
2477 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2479 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2482 /* Size of each Table Directory is 16 bytes. */
2483 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2484 "Table Directory") < 0)
2487 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2488 for (i = 0; i < otf->offset_table.numTables; i++)
2490 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2491 OTF_TableInfo *table_info = NULL;
2495 if (tag == head_tag)
2496 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2497 else if (tag == name_tag)
2498 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2499 else if (tag == cmap_tag)
2500 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2501 else if (tag == gdef_tag)
2502 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2503 else if (tag == gsub_tag)
2504 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2505 else if (tag == gpos_tag)
2506 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2510 table_info->stream = make_stream ();
2511 if (setup_stream (table_info->stream, fp,
2512 otf->table_dirs[i].offset,
2513 otf->table_dirs[i].length,
2514 otf->table_dirs[i].name) < 0)
2519 internal_data->header_stream = NULL;
2520 free_stream (stream);
2524 static OTF_TableInfo *
2525 get_table_info (OTF *otf, char *name)
2527 char *errfmt = "OTF Table Read%s";
2528 OTF_TableInfo *errret = NULL;
2529 OTF_InternalData *internal_data = otf->internal_data;
2530 OTF_TableInfo *table_info;
2531 OTF_Tag tag = OTF_tag (name);
2534 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2536 if (tag == OTF_tag ("head"))
2537 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2538 else if (tag == OTF_tag ("name"))
2539 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2540 else if (tag == OTF_tag ("cmap"))
2541 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2542 else if (tag == OTF_tag ("GDEF"))
2543 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2544 else if (tag == OTF_tag ("GSUB"))
2545 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2546 else if (tag == OTF_tag ("GPOS"))
2547 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2549 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2551 if (*table_info->address)
2554 if (! table_info->stream)
2555 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2556 if (! table_info->reader)
2557 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2563 /*** (2) API for reading OTF */
2565 /*** (2-1) OTF_open() */
2567 /* Note: We can't use memory allocation macros in the following
2568 functions because those macros return from the functions before
2569 freeing memory previously allocated. */
2572 OTF_open (char *otf_name)
2575 char *errfmt = "opening otf (%s)";
2576 void *errret = NULL;
2578 OTF_InternalData *internal_data;
2580 fp = fopen (otf_name, "r");
2582 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2583 otf = calloc (1, sizeof (OTF));
2585 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2586 otf->filename = strdup (otf_name);
2587 if (! otf->filename)
2591 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2594 internal_data = calloc (1, sizeof (OTF_InternalData));
2595 if (! internal_data)
2596 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2597 otf->internal_data = internal_data;
2598 if (! allocate_memory_record (otf))
2599 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2601 /* Here after, all pointers to allocated memory are recorded in
2602 otf->internal_data->memory_record except for what allocated by
2603 the functions allocate_memory_record and make_stream. */
2605 if (read_header_part (otf, fp) < 0)
2616 /*** (2-2) OTF_close() */
2619 OTF_close (OTF *otf)
2621 OTF_InternalData *internal_data = otf->internal_data;
2626 OTF_MemoryRecord *memrec = internal_data->memory_record;
2628 if (internal_data->header_stream)
2629 free_stream (internal_data->header_stream);
2631 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2632 if (internal_data->table_info[i].stream)
2633 free_stream (internal_data->table_info[i].stream);
2637 OTF_MemoryRecord *next = memrec->next;
2639 for (i = memrec->used - 1; i >= 0; i--)
2640 free (memrec->memory[i]);
2644 free (internal_data);
2647 free (otf->filename);
2651 /*** (2-3) OTF_get_table() */
2654 OTF_get_table (OTF *otf, char *name)
2656 OTF_TableInfo *table_info = get_table_info (otf, name);
2661 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2662 free_stream (table_info->stream);
2663 table_info->stream = NULL;
2664 if (! *table_info->address)
2666 table_info->reader = NULL;
2672 /*** (2-4) OTF_check_table() */
2675 OTF_check_table (OTF *otf, char *name)
2677 return (get_table_info (otf, name) ? 0 : -1);
2683 /*** (5) API miscellaneous ***/
2686 OTF_tag (char *name)
2688 unsigned char *p = (unsigned char *) name;
2692 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2696 OTF_tag_name (OTF_Tag tag, char *name)
2698 name[0] = (char) (tag >> 24);
2699 name[1] = (char) ((tag >> 16) & 0xFF);
2700 name[2] = (char) ((tag >> 8) & 0xFF);
2701 name[3] = (char) (tag & 0xFF);