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;
1873 memset (value_record, 0, sizeof (OTF_ValueRecord));
1876 for (i = 0, size = 0; i < 8; i++)
1880 if (bit & OTF_XPlacement)
1881 READ_INT16 (stream, value_record->XPlacement);
1882 if (bit & OTF_XPlacement)
1883 READ_INT16 (stream, value_record->YPlacement);
1884 if (bit & OTF_XAdvance)
1885 READ_INT16 (stream, value_record->XAdvance);
1886 if (bit & OTF_YAdvance)
1887 READ_INT16 (stream, value_record->YAdvance);
1888 if (bit & OTF_XPlaDevice)
1889 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1890 if (bit & OTF_YPlaDevice)
1891 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1892 if (bit & OTF_XAdvDevice)
1893 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1894 if (bit & OTF_YAdvDevice)
1895 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1896 SAVE_STREAM (stream, state);
1897 if (value_record->XPlaDevice.offset)
1899 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1902 if (value_record->YPlaDevice.offset)
1904 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1907 if (value_record->XAdvDevice.offset)
1909 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1912 if (value_record->YAdvDevice.offset)
1914 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1917 RESTORE_STREAM (stream, state);
1923 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1925 char *errfmt = "Anchor%s";
1928 SEEK_STREAM (stream, offset + anchor->offset);
1929 READ_UINT16 (stream, anchor->AnchorFormat);
1930 READ_INT16 (stream, anchor->XCoordinate);
1931 READ_INT16 (stream, anchor->YCoordinate);
1932 if (anchor->AnchorFormat == 1)
1934 else if (anchor->AnchorFormat == 2)
1936 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1938 else if (anchor->AnchorFormat == 3)
1940 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1941 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1942 if (anchor->f.f2.XDeviceTable.offset)
1944 if (read_device_table (otf, stream, offset + anchor->offset,
1945 &anchor->f.f2.XDeviceTable) < 0)
1948 if (anchor->f.f2.YDeviceTable.offset)
1950 if (read_device_table (otf, stream, offset + anchor->offset,
1951 &anchor->f.f2.YDeviceTable) < 0)
1956 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1962 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1963 OTF_MarkArray *array)
1965 char *errfmt = "MarkArray%s";
1967 OTF_StreamState state;
1970 READ_OFFSET (stream, array->offset);
1971 SAVE_STREAM (stream, state);
1972 SEEK_STREAM (stream, offset + array->offset);
1973 READ_UINT16 (stream, array->MarkCount);
1974 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1975 for (i = 0; i < array->MarkCount; i++)
1977 READ_UINT16 (stream, array->MarkRecord[i].Class);
1978 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1980 for (i = 0; i < array->MarkCount; i++)
1981 if (read_anchor (otf, stream, offset + array->offset,
1982 &array->MarkRecord[i].MarkAnchor) < 0)
1984 RESTORE_STREAM (stream, state);
1989 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1990 unsigned ClassCount, OTF_AnchorArray *array)
1992 char *errfmt = "AnchorArray%s";
1994 OTF_StreamState state;
1997 READ_OFFSET (stream, array->offset);
1998 SAVE_STREAM (stream, state);
1999 SEEK_STREAM (stream, offset + array->offset);
2000 READ_UINT16 (stream, array->Count);
2001 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2002 for (i = 0; i < array->Count; i++)
2004 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2006 for (j = 0; j < ClassCount; j++)
2007 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2009 for (i = 0; i < array->Count; i++)
2010 for (j = 0; j < ClassCount; j++)
2011 if (read_anchor (otf, stream, offset + array->offset,
2012 &array->AnchorRecord[i].Anchor[j]) < 0)
2014 RESTORE_STREAM (stream, state);
2018 static OTF_PairSet *
2019 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2020 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2022 char *errfmt = "PairSet%s";
2023 void *errret = NULL;
2024 OTF_StreamState state;
2028 OTF_MALLOC (set, num, "");
2029 for (i = 0; i < num; i++)
2030 READ_OFFSET (stream, set[i].offset);
2031 SAVE_STREAM (stream, state);
2032 for (i = 0; i < num; i++)
2034 SEEK_STREAM (stream, offset + set[i].offset);
2035 READ_UINT16 (stream, set[i].PairValueCount);
2036 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2037 for (j = 0; j < set[i].PairValueCount; j++)
2039 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2041 READ_UINT16 (stream, rec->SecondGlyph);
2042 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2043 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2046 RESTORE_STREAM (stream, state);
2050 static OTF_Class1Record *
2051 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2052 unsigned num1, enum OTF_ValueFormat bit1,
2053 unsigned num2, enum OTF_ValueFormat bit2)
2055 char *errfmt = "Class1Record%s";
2056 void *errret = NULL;
2057 OTF_Class1Record *rec;
2060 OTF_MALLOC (rec, num1, "");
2061 for (i = 0; i < num1; i++)
2063 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2064 for (j = 0; j < num2; j++)
2066 if (read_value_record (otf, stream, offset,
2067 bit1, &rec[i].Class2Record[j].Value1) < 0
2068 || read_value_record (otf, stream, offset,
2069 bit2, &rec[i].Class2Record[j].Value2) < 0)
2077 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2078 OTF_EntryExitRecord **rec)
2080 char *errfmt = "EntryExitSet%s";
2084 OTF_StreamState state;
2086 READ_UINT16 (stream, count);
2088 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2089 OTF_MALLOC (*rec, count, "");
2090 for (i = 0; i < count; i++)
2092 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2093 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2095 SAVE_STREAM (stream, state);
2096 for (i = 0; i < count; i++)
2098 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2100 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2103 RESTORE_STREAM (stream, state);
2108 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2109 unsigned ClassCount, OTF_LigatureAttach *attach)
2111 char *errfmt = "LigatureAttach%s";
2115 SEEK_STREAM (stream, offset + attach->offset);
2116 READ_UINT16 (stream, attach->ComponentCount);
2117 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2118 for (i = 0; i < attach->ComponentCount; i++)
2120 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2121 " (ComponentRecord)");
2122 for (j = 0; j < ClassCount; j++)
2123 READ_OFFSET (stream,
2124 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2126 for (i = 0; i < attach->ComponentCount; i++)
2127 for (j = 0; j < ClassCount; j++)
2128 if (read_anchor (otf, stream, offset + attach->offset,
2129 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2135 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2136 unsigned class_count, OTF_LigatureArray *array)
2138 char *errfmt = "LigatureArray%s";
2140 OTF_StreamState state;
2143 READ_OFFSET (stream, array->offset);
2144 SAVE_STREAM (stream, state);
2145 SEEK_STREAM (stream, offset + array->offset);
2146 READ_UINT16 (stream, array->LigatureCount);
2147 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2148 for (i = 0; i < array->LigatureCount; i++)
2149 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2150 for (i = 0; i < array->LigatureCount; i++)
2151 read_ligature_attach (otf, stream, offset + array->offset,
2152 class_count, array->LigatureAttach + i);
2153 RESTORE_STREAM (stream, state);
2158 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2159 long offset, unsigned type,
2160 OTF_LookupSubTableGPOS *subtable)
2165 SEEK_STREAM (stream, offset);
2166 READ_UINT16 (stream, subtable->Format);
2167 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2171 if (subtable->Format == 1)
2173 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2175 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2176 read_value_record (otf, stream, offset,
2177 subtable->u.single1.ValueFormat,
2178 &subtable->u.single1.Value);
2180 else if (subtable->Format == 2)
2182 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2185 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2187 READ_UINT16 (stream, single2->ValueFormat);
2188 READ_UINT16 (stream, single2->ValueCount);
2189 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2190 for (i = 0; i < single2->ValueCount; i++)
2191 read_value_record (otf, stream, offset, single2->ValueFormat,
2192 single2->Value + i);
2195 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2199 if (subtable->Format == 1)
2201 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2203 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2204 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2205 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2206 subtable->u.pair1.PairSet
2207 = read_pair_set_list (otf, stream, offset,
2208 subtable->u.pair1.PairSetCount,
2209 subtable->u.pair1.ValueFormat1,
2210 subtable->u.pair1.ValueFormat2);
2211 if (! subtable->u.pair1.PairSet)
2214 else if (subtable->Format == 2)
2216 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2218 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2219 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2220 if (read_class_def (otf, stream, offset,
2221 &subtable->u.pair2.ClassDef1) < 0
2222 || read_class_def (otf, stream, offset,
2223 &subtable->u.pair2.ClassDef2) < 0)
2225 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2226 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2227 subtable->u.pair2.Class1Record
2228 = read_class1_record_list (otf, stream, offset,
2229 subtable->u.pair2.Class1Count,
2230 subtable->u.pair2.ValueFormat1,
2231 subtable->u.pair2.Class2Count,
2232 subtable->u.pair2.ValueFormat2);
2233 if (! subtable->u.pair2.Class1Record)
2237 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2241 if (subtable->Format == 1)
2243 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2245 subtable->u.cursive1.EntryExitCount
2246 = read_entry_exit_list (otf, stream, offset,
2247 &subtable->u.cursive1.EntryExitRecord);
2248 if (! subtable->u.cursive1.EntryExitCount)
2252 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2256 if (subtable->Format == 1)
2258 read_coverage (otf, stream, offset, &subtable->Coverage);
2259 read_coverage (otf, stream, offset,
2260 &subtable->u.mark_base1.BaseCoverage);
2261 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2262 read_mark_array (otf, stream, offset,
2263 &subtable->u.mark_base1.MarkArray);
2264 read_anchor_array (otf, stream, offset,
2265 subtable->u.mark_base1.ClassCount,
2266 &subtable->u.mark_base1.BaseArray);
2269 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2273 if (subtable->Format == 1)
2275 read_coverage (otf, stream, offset, &subtable->Coverage);
2276 read_coverage (otf, stream, offset,
2277 &subtable->u.mark_lig1.LigatureCoverage);
2278 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2279 read_mark_array (otf, stream, offset,
2280 &subtable->u.mark_lig1.MarkArray);
2281 read_ligature_array (otf, stream, offset,
2282 subtable->u.mark_lig1.ClassCount,
2283 &subtable->u.mark_lig1.LigatureArray);
2288 if (subtable->Format == 1)
2290 read_coverage (otf, stream, offset, &subtable->Coverage);
2291 read_coverage (otf, stream, offset,
2292 &subtable->u.mark_mark1.Mark2Coverage);
2293 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2294 read_mark_array (otf, stream, offset,
2295 &subtable->u.mark_mark1.Mark1Array);
2296 read_anchor_array (otf, stream, offset,
2297 subtable->u.mark_mark1.ClassCount,
2298 &subtable->u.mark_mark1.Mark2Array);
2301 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2305 if (subtable->Format == 1)
2307 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2308 &subtable->u.context1) < 0)
2311 else if (subtable->Format == 2)
2313 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2314 &subtable->u.context2) < 0)
2317 else if (subtable->Format == 3)
2319 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2320 &subtable->u.context3) < 0)
2324 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2328 if (subtable->Format == 1)
2330 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2331 &subtable->u.chain_context1) < 0)
2334 else if (subtable->Format == 2)
2336 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2337 &subtable->u.chain_context2) < 0)
2340 else if (subtable->Format == 3)
2342 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2343 &subtable->u.chain_context3) < 0)
2347 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2351 if (subtable->Format == 1)
2355 OTF_LookupSubTableGPOS *ex_subtable;
2357 READ_USHORT (stream, ex_type);
2358 READ_ULONG (stream, ex_offset);
2359 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2360 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2361 ex_type, ex_subtable) < 0)
2363 subtable->u.extension1.ExtensionLookupType = ex_type;
2364 subtable->u.extension1.ExtensionOffset = ex_offset;
2365 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2368 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2372 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2378 read_gpos_table (OTF *otf, OTF_Stream *stream)
2380 return read_gsub_gpos_table (otf, stream, 0);
2388 read_base_table (OTF_Stream *stream, long offset)
2392 OTF_MALLOC (base, 1);
2401 read_jstf_table (OTF_Stream *stream, long offset)
2405 OTF_MALLOC (jstf, 1);
2411 /*** (1-11) Structure for OTF */
2414 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2418 READ_FIXED (stream, table->sfnt_version);
2419 READ_USHORT (stream, table->numTables);
2420 READ_USHORT (stream, table->searchRange);
2421 READ_USHORT (stream, table->enterSelector);
2422 READ_USHORT (stream, table->rangeShift);
2427 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2432 READ_TAG (stream, tag);
2434 table->name[0] = tag >> 24;
2435 table->name[1] = (tag >> 16) & 0xFF;
2436 table->name[0] = (tag >> 8) & 0xFF;
2437 table->name[0] = tag >> 8;
2438 table->name[0] = '\0';
2439 READ_ULONG (stream, table->checkSum);
2440 READ_ULONG (stream, table->offset);
2441 READ_ULONG (stream, table->length);
2446 read_header_part (OTF *otf, FILE *fp)
2448 char *errfmt = "otf header%s";
2450 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2453 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2455 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2456 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2457 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2458 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2459 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2460 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2461 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2462 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2463 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2464 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2465 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2466 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2468 head_tag = OTF_tag ("head");
2469 name_tag = OTF_tag ("name");
2470 cmap_tag = OTF_tag ("cmap");
2471 gdef_tag = OTF_tag ("GDEF");
2472 gsub_tag = OTF_tag ("GSUB");
2473 gpos_tag = OTF_tag ("GPOS");
2475 stream = make_stream ();
2479 internal_data->header_stream = stream;
2481 /* Size of Offset Table is 12 bytes. */
2482 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2484 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2487 /* Size of each Table Directory is 16 bytes. */
2488 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2489 "Table Directory") < 0)
2492 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2493 for (i = 0; i < otf->offset_table.numTables; i++)
2495 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2496 OTF_TableInfo *table_info = NULL;
2500 if (tag == head_tag)
2501 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2502 else if (tag == name_tag)
2503 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2504 else if (tag == cmap_tag)
2505 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2506 else if (tag == gdef_tag)
2507 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2508 else if (tag == gsub_tag)
2509 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2510 else if (tag == gpos_tag)
2511 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2515 table_info->stream = make_stream ();
2516 if (setup_stream (table_info->stream, fp,
2517 otf->table_dirs[i].offset,
2518 otf->table_dirs[i].length,
2519 otf->table_dirs[i].name) < 0)
2524 internal_data->header_stream = NULL;
2525 free_stream (stream);
2529 static OTF_TableInfo *
2530 get_table_info (OTF *otf, char *name)
2532 char *errfmt = "OTF Table Read%s";
2533 OTF_TableInfo *errret = NULL;
2534 OTF_InternalData *internal_data = otf->internal_data;
2535 OTF_TableInfo *table_info;
2536 OTF_Tag tag = OTF_tag (name);
2539 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2541 if (tag == OTF_tag ("head"))
2542 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2543 else if (tag == OTF_tag ("name"))
2544 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2545 else if (tag == OTF_tag ("cmap"))
2546 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2547 else if (tag == OTF_tag ("GDEF"))
2548 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2549 else if (tag == OTF_tag ("GSUB"))
2550 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2551 else if (tag == OTF_tag ("GPOS"))
2552 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2554 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2556 if (*table_info->address)
2559 if (! table_info->stream)
2560 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2561 if (! table_info->reader)
2562 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2568 /*** (2) API for reading OTF */
2570 /*** (2-1) OTF_open() */
2572 /* Note: We can't use memory allocation macros in the following
2573 functions because those macros return from the functions before
2574 freeing memory previously allocated. */
2577 OTF_open (char *otf_name)
2580 char *errfmt = "opening otf (%s)";
2581 void *errret = NULL;
2583 OTF_InternalData *internal_data;
2585 fp = fopen (otf_name, "r");
2587 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2588 otf = calloc (1, sizeof (OTF));
2590 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2591 otf->filename = strdup (otf_name);
2592 if (! otf->filename)
2596 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2599 internal_data = calloc (1, sizeof (OTF_InternalData));
2600 if (! internal_data)
2601 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2602 otf->internal_data = internal_data;
2603 if (! allocate_memory_record (otf))
2604 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2606 /* Here after, all pointers to allocated memory are recorded in
2607 otf->internal_data->memory_record except for what allocated by
2608 the functions allocate_memory_record and make_stream. */
2610 if (read_header_part (otf, fp) < 0)
2621 /*** (2-2) OTF_close() */
2624 OTF_close (OTF *otf)
2626 OTF_InternalData *internal_data = otf->internal_data;
2631 OTF_MemoryRecord *memrec = internal_data->memory_record;
2633 if (internal_data->header_stream)
2634 free_stream (internal_data->header_stream);
2636 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2637 if (internal_data->table_info[i].stream)
2638 free_stream (internal_data->table_info[i].stream);
2642 OTF_MemoryRecord *next = memrec->next;
2644 for (i = memrec->used - 1; i >= 0; i--)
2645 free (memrec->memory[i]);
2649 free (internal_data);
2652 free (otf->filename);
2656 /*** (2-3) OTF_get_table() */
2659 OTF_get_table (OTF *otf, char *name)
2661 OTF_TableInfo *table_info = get_table_info (otf, name);
2666 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2667 free_stream (table_info->stream);
2668 table_info->stream = NULL;
2669 if (! *table_info->address)
2671 table_info->reader = NULL;
2677 /*** (2-4) OTF_check_table() */
2680 OTF_check_table (OTF *otf, char *name)
2682 return (get_table_info (otf, name) ? 0 : -1);
2688 /*** (5) API miscellaneous ***/
2691 OTF_tag (char *name)
2693 unsigned char *p = (unsigned char *) name;
2697 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2701 OTF_tag_name (OTF_Tag tag, char *name)
2703 name[0] = (char) (tag >> 24);
2704 name[1] = (char) ((tag >> 16) & 0xFF);
2705 name[2] = (char) ((tag >> 8) & 0xFF);
2706 name[3] = (char) (tag & 0xFF);