1 /* otfopen.c -- OpenType font reader.
4 by AIST (National Institute of Advanced Industrial Science and Technology)
5 Registration Number H15PRO???
7 This file is part of the OTF library.
9 The OTF library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 The OTF library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with the OTF library; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
32 Table of contents (almost parallel to otf.h):
36 (1) Readers for OTF Layout tables and OTF itself
37 (1-1) Basic types and functions
41 (1-5) Structures common to GDEF, GSUB, and GPOS
43 (1-7) Structures for ScriptList, FeatureList, and LookupList
44 (1-8) Structures common to GSUB and GPOS
47 (1-11) Structure for OTF
49 (2) API for reading OTF
53 (2-4) OTF_check_table()
60 Example of typical usage of OTF_Stream.
64 OTF_StreamState state;
67 OPEN_STREAM (_FILE_NAME_, stream);
70 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
71 offset = READ_OFFSET (stream);
72 nbytes = READ_ULONG (stream);
73 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
75 CLOSE_STREAM (stream);
90 typedef long OTF_StreamState;
96 char *errfmt = "stream creation%s";
99 stream = calloc (1, sizeof (OTF_Stream));
101 OTF_ERROR (OTF_ERROR_MEMORY, "");
106 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
109 char *errfmt = "stream setup for %s";
114 if (stream->allocated < nbytes)
116 unsigned char *buf = malloc (nbytes);
119 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
123 stream->allocated = nbytes;
125 stream->bufsize = nbytes;
126 if (fseek (fp, offset, SEEK_SET) < 0)
127 OTF_ERROR (OTF_ERROR_FILE, stream->name);
128 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
129 OTF_ERROR (OTF_ERROR_FILE, stream->name);
135 free_stream (OTF_Stream *stream)
141 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
142 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
143 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
145 #define STREAM_CHECK_SIZE(stream, size) \
146 if ((stream)->pos + (size) > (stream)->bufsize) \
148 char *errfmt = "buffer overrun in %s"; \
150 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
156 #define READ_USHORT(stream, var) \
158 STREAM_CHECK_SIZE ((stream), 2); \
159 (var) = (((stream)->buf[(stream)->pos] << 8) \
160 | (stream)->buf[(stream)->pos + 1]); \
161 (stream)->pos += 2; \
164 #define READ_SHORT(stream, var) \
166 STREAM_CHECK_SIZE ((stream), 2); \
167 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
168 | (stream)->buf[(stream)->pos + 1]); \
169 (stream)->pos += 2; \
172 #define READ_ULONG(stream, var) \
174 STREAM_CHECK_SIZE ((stream), 4); \
175 (var) = (((stream)->buf[(stream)->pos] << 24) \
176 | ((stream)->buf[(stream)->pos + 1] << 16) \
177 | ((stream)->buf[(stream)->pos + 2] << 8) \
178 | (stream)->buf[(stream)->pos + 3]); \
179 (stream)->pos += 4; \
182 #define READ_LONG(stream, var) \
184 STREAM_CHECK_SIZE ((stream), 4); \
185 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
186 | ((stream)->buf[(stream)->pos + 1] << 16) \
187 | ((stream)->buf[(stream)->pos + 2] << 8) \
188 | (stream)->buf[(stream)->pos + 3]); \
189 (stream)->pos += 4; \
193 #define READ_FIXED(stream, fixed) \
195 READ_USHORT ((stream), (fixed).high); \
196 READ_USHORT ((stream), (fixed).low); \
200 #define READ_BYTES(stream, p, nbytes) \
202 STREAM_CHECK_SIZE ((stream), (nbytes)); \
203 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
204 (stream)->pos += (nbytes); \
208 #define READ_TAG READ_ULONG
209 #define READ_OFFSET READ_USHORT
210 #define READ_UINT16 READ_USHORT
211 #define READ_INT16 READ_SHORT
212 #define READ_GLYPHID READ_USHORT
215 /*** (1) Structures for OTF Layout tables and OTF itself */
217 /*** (1-1) Basic types and functions */
230 #define OTF_MEMORY_RECORD_SIZE 1024
232 struct OTF_MemoryRecord
235 void *memory[OTF_MEMORY_RECORD_SIZE];
236 struct OTF_MemoryRecord *next;
239 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
243 /* Points to one of OTF->head, OTF->name, etc. */
245 /* Function to read one of OTF tables. */
246 void *(*reader) (OTF *otf, OTF_Stream *stream);
247 /* Stream given to <reader>. */
251 struct OTF_InternalData
253 /* Information about each OTF table. */
254 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
256 /* Stream used to read the header part of OTF. */
257 OTF_Stream *header_stream;
259 /* Records of allocated memories. */
260 OTF_MemoryRecord *memory_record;
264 static OTF_MemoryRecord *
265 allocate_memory_record (OTF *otf)
267 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
268 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
273 memrec->next = internal_data->memory_record;
274 internal_data->memory_record = memrec;
278 /* Memory allocation macros. */
280 #define OTF_MALLOC(p, size, arg) \
286 OTF_MemoryRecord *memrec \
287 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
288 (p) = malloc (sizeof (*(p)) * (size)); \
290 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
291 && ! (memrec = allocate_memory_record (otf)))) \
292 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
293 memrec->memory[memrec->used++] = (p); \
298 #define OTF_CALLOC(p, size, arg) \
304 OTF_MemoryRecord *memrec \
305 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
306 (p) = calloc ((size), sizeof (*(p))); \
308 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
309 && ! (memrec = allocate_memory_record (otf)))) \
310 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
311 memrec->memory[memrec->used++] = (p); \
316 /*** (1-2) "head" table */
319 read_head_table (OTF *otf, OTF_Stream *stream)
321 char *errfmt = "head%s";
325 OTF_CALLOC (head, 1, "");
326 READ_FIXED (stream, head->TableVersionNumber);
327 READ_FIXED (stream, head->fontRevision);
328 READ_ULONG (stream, head->checkSumAdjustment);
329 READ_ULONG (stream, head->magicNumber);
330 READ_USHORT (stream, head->flags);
331 READ_USHORT (stream, head->unitsPerEm);
337 /*** (1-3) "name" table */
340 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
342 char *errfmt = "nameID (%d)";
344 OTF_StreamState state;
349 SAVE_STREAM (stream, state);
350 SEEK_STREAM (stream, stream->pos + rec->offset);
354 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
355 READ_BYTES (stream, str, rec->length);
356 for (i = 0; i < rec->length; i++)
362 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
363 for (i = 0; i < rec->length / 2; i++)
365 READ_USHORT (stream, c);
373 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
374 for (i = 0; i < rec->length / 4; i++)
376 READ_ULONG (stream, c);
383 RESTORE_STREAM (stream, state);
388 read_name_table (OTF *otf, OTF_Stream *stream)
390 char *errfmt = "name%s";
395 OTF_CALLOC (name, 1, "");
396 READ_USHORT (stream, name->format);
397 READ_USHORT (stream, name->count);
398 READ_USHORT (stream, name->stringOffset);
399 OTF_MALLOC (name->nameRecord, name->count, "");
400 for (i = 0; i < name->count; i++)
402 OTF_NameRecord *rec = name->nameRecord + i;
404 READ_USHORT (stream, rec->platformID);
405 READ_USHORT (stream, rec->encodingID);
406 READ_USHORT (stream, rec->languageID);
407 READ_USHORT (stream, rec->nameID);
408 READ_USHORT (stream, rec->length);
409 READ_USHORT (stream, rec->offset);
411 for (i = 0; i < name->count; i++)
413 OTF_NameRecord *rec = name->nameRecord + i;
414 int nameID = rec->nameID;
416 if (nameID <= OTF_max_nameID
417 && ! name->name[nameID])
419 if (rec->platformID == 0)
420 name->name[nameID] = read_name (otf, stream, rec,
421 rec->encodingID <= 3 ? 2 : 4);
422 else if (rec->platformID == 1
423 && rec->encodingID == 0)
424 name->name[nameID] = read_name (otf, stream, rec, 1);
425 else if (rec->platformID == 3
426 && (rec->encodingID == 1 || rec->encodingID == 10))
427 name->name[nameID] = read_name (otf, stream,
428 rec, rec->encodingID == 1 ? 2 : 4);
436 /*** (1-4) "cmap" table */
439 read_cmap_table (OTF *otf, OTF_Stream *stream)
441 char *errfmt = "cmap%s";
444 int unicode_index = -1;
447 OTF_CALLOC (cmap, 1, "");
448 READ_USHORT (stream, cmap->version);
449 READ_USHORT (stream, cmap->numTables);
450 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
451 for (i = 0; i < cmap->numTables; i++)
453 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
454 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
455 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
456 if (cmap->EncodingRecord[i].platformID == 3
457 && cmap->EncodingRecord[i].encodingID == 1)
460 for (i = 0; i < cmap->numTables; i++)
464 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
465 READ_USHORT (stream, format);
466 cmap->EncodingRecord[i].subtable.format = format;
467 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
468 if (format == 8 || format == 10 || format == 12)
470 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
471 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
475 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
481 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
482 " (EncodingRecord)");
484 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
494 OTF_EncodingSubtable4 *sub4;
499 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
500 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
501 READ_USHORT (stream, sub4->segCountX2);
502 segCount = sub4->segCountX2 / 2;
503 READ_USHORT (stream, sub4->searchRange);
504 READ_USHORT (stream, sub4->entrySelector);
505 READ_USHORT (stream, sub4->rangeShift);
506 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
507 for (j = 0; j < segCount; j++)
508 READ_USHORT (stream, sub4->segments[j].endCount);
509 READ_USHORT (stream, dummy);
510 for (j = 0; j < segCount; j++)
511 READ_USHORT (stream, sub4->segments[j].startCount);
512 for (j = 0; j < segCount; j++)
513 READ_SHORT (stream, sub4->segments[j].idDelta);
514 for (j = 0; j < segCount; j++)
517 unsigned rest = 2 * (segCount - j);
519 READ_USHORT (stream, off);
521 sub4->segments[j].idRangeOffset = 0xFFFF;
523 sub4->segments[j].idRangeOffset = (off - rest) / 2;
525 j = (cmap->EncodingRecord[i].subtable.length
526 - (14 + 2 * (segCount * 4 + 1)));
527 sub4->GlyphCount = j / 2;
528 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
529 for (j = 0; j < sub4->GlyphCount; j++)
530 READ_USHORT (stream, sub4->glyphIdArray[j]);
536 OTF_EncodingSubtable6 *sub6;
539 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
540 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
541 READ_USHORT (stream, sub6->firstCode);
542 READ_USHORT (stream, sub6->entryCount);
543 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
544 for (j = 0; j < sub6->entryCount; j++)
545 READ_USHORT (stream, sub6->glyphIdArray[j]);
550 if (unicode_index >= 0)
552 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_index;
553 OTF_GlyphID glyph_id, max_glyph_id = 0;
555 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
556 switch (rec->subtable.format)
560 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
561 int segCount = sub4->segCountX2 / 2;
563 for (i = 0; i < segCount; i++)
565 OTF_cmapSegument *seg = sub4->segments + i;
568 if (seg->idRangeOffset == 0xFFFF)
569 for (c = seg->startCount; c <= seg->endCount; c++)
571 glyph_id = c + seg->idDelta;
572 cmap->unicode_table[c] = glyph_id;
573 if (glyph_id > max_glyph_id)
574 max_glyph_id = glyph_id;
577 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
580 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
581 + (c - seg->startCount)];
582 cmap->unicode_table[c] = glyph_id;
583 if (glyph_id > max_glyph_id)
584 max_glyph_id = glyph_id;
590 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
591 for (i = 0; i < 0x10000; i++)
592 if (cmap->unicode_table[i])
593 cmap->decode_table[cmap->unicode_table[i]] = i;
594 cmap->max_glyph_id = max_glyph_id;
601 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
603 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
604 Glyph-IDs there. If COUNT is negative, read the number of
605 Glyphs-IDs at first. MINUS if nozero is how few the actual
606 Glyph-IDs are in STREAM than COUNT. */
609 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
610 int minus, int count)
612 char *errfmt = "GlyphID List%s";
617 READ_UINT16 (stream, count);
620 OTF_MALLOC (*ids, count, "");
621 for (i = 0; i < count + minus; i++)
622 READ_GLYPHID (stream, (*ids)[i]);
627 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
629 char *errfmt = "RangeRecord%s";
634 READ_UINT16 (stream, count);
637 OTF_MALLOC (*record, count, "");
638 for (i = 0; i < count; i++)
640 READ_GLYPHID (stream, (*record)[i].Start);
641 READ_GLYPHID (stream, (*record)[i].End);
642 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
649 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
650 OTF_Coverage *coverage)
652 char *errfmt = "Coverage%s";
654 OTF_StreamState state;
657 READ_OFFSET (stream, coverage->offset);
658 SAVE_STREAM (stream, state);
659 SEEK_STREAM (stream, offset + coverage->offset);
660 READ_UINT16 (stream, coverage->CoverageFormat);
661 if (coverage->CoverageFormat == 1)
662 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
663 else if (coverage->CoverageFormat == 2)
664 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
666 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
669 coverage->Count = (unsigned) count;
670 RESTORE_STREAM (stream, state);
674 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
675 and store the Coverages there. If COUNT is negative, read the
676 number of Coverages at first. */
679 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
680 OTF_Coverage **coverage, int count)
682 char *errfmt = "Coverage List%s";
687 READ_UINT16 (stream, count);
690 OTF_MALLOC (*coverage, count, "");
691 for (i = 0; i < count; i++)
692 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
699 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
702 char *errfmt = "ClassDef%s";
705 SEEK_STREAM (stream, class->offset);
706 READ_UINT16 (stream, class->ClassFormat);
707 if (class->ClassFormat == 1)
709 READ_GLYPHID (stream, class->f.f1.StartGlyph);
710 class->f.f1.GlyphCount
712 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
713 if (! class->f.f1.GlyphCount)
714 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
716 else if (class->ClassFormat == 2)
718 class->f.f2.ClassRangeCount
719 = (read_range_records
720 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
721 if (! class->f.f2.ClassRangeCount)
722 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
725 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
731 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
733 char *errfmt = "ClassDef%s";
735 OTF_StreamState state;
737 READ_OFFSET (stream, class->offset);
740 SAVE_STREAM (stream, state);
741 SEEK_STREAM (stream, offset + class->offset);
742 READ_UINT16 (stream, class->ClassFormat);
743 if (class->ClassFormat == 1)
745 READ_GLYPHID (stream, class->f.f1.StartGlyph);
746 class->f.f1.GlyphCount
747 = read_glyph_ids (otf, stream,
748 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
750 if (! class->f.f1.GlyphCount)
753 else if (class->ClassFormat == 2)
755 class->f.f2.ClassRangeCount
756 = read_range_records (otf, stream,
758 &class->f.f2.ClassRangeRecord);
759 if (! class->f.f2.ClassRangeCount)
763 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
765 RESTORE_STREAM (stream, state);
771 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
772 OTF_DeviceTable *table)
774 char *errfmt = "Device Table%s";
785 SEEK_STREAM (stream, offset + table->offset);
786 READ_UINT16 (stream, table->StartSize);
787 READ_UINT16 (stream, table->EndSize);
788 READ_UINT16 (stream, table->DeltaFormat);
789 num = table->EndSize - table->StartSize + 1;
790 OTF_MALLOC (table->DeltaValue, num, "");
792 if (table->DeltaFormat == 1)
793 for (i = 0; i < num; i++)
796 READ_UINT16 (stream, val);
797 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
798 table->DeltaValue[i] = intval.int2;
800 else if (table->DeltaFormat == 2)
801 for (i = 0; i < num; i++)
804 READ_UINT16 (stream, val);
805 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
806 table->DeltaValue[i] = intval.int4;
808 else if (table->DeltaFormat == 3)
809 for (i = 0; i < num; i++)
813 READ_UINT16 (stream, val);
814 intval.int8 = val >> 8;
815 table->DeltaValue[i] = intval.int8;
819 intval.int8 = val >> 8;
820 table->DeltaValue[i] = intval.int8;
824 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
829 /*** (1-6) "GDEF" table */
832 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
833 OTF_AttachList *list)
835 char *errfmt = "AttachList%s";
839 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
841 READ_UINT16 (stream, list->GlyphCount);
842 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
843 for (i = 0; i < list->GlyphCount; i++)
844 READ_OFFSET (stream, list->AttachPoint[i].offset);
845 for (i = 0; i < list->GlyphCount; i++)
849 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
850 READ_UINT16 (stream, count);
851 list->AttachPoint[i].PointCount = count;
852 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
853 for (j = 0; j < count; j++)
854 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
860 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
861 OTF_CaretValue *caret)
863 char *errfmt = "CaretValue%s";
866 SEEK_STREAM (stream, offset + caret->offset);
867 READ_UINT16 (stream, caret->CaretValueFormat);
868 if (caret->CaretValueFormat == 1)
869 READ_INT16 (stream, caret->f.f1.Coordinate);
870 else if (caret->CaretValueFormat == 2)
871 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
872 else if (caret->CaretValueFormat == 3)
874 READ_INT16 (stream, caret->f.f3.Coordinate);
875 if (read_device_table (otf, stream, offset + caret->offset,
876 &caret->f.f3.DeviceTable) < 0)
880 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
885 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
886 OTF_LigCaretList *list)
888 char *errfmt = "LigCaretList%s";
892 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
894 READ_UINT16 (stream, list->LigGlyphCount);
895 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
896 for (i = 0; i < list->LigGlyphCount; i++)
897 READ_OFFSET (stream, list->LigGlyph[i].offset);
898 for (i = 0; i < list->LigGlyphCount; i++)
902 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
903 READ_UINT16 (stream, count);
904 list->LigGlyph[i].CaretCount = count;
905 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
906 for (j = 0; j < count; j++)
907 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
908 for (j = 0; j < count; j++)
909 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
910 &list->LigGlyph[i].CaretValue[j]) < 0)
917 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
921 READ_FIXED (stream, header->Version);
922 READ_OFFSET (stream, header->GlyphClassDef);
923 READ_OFFSET (stream, header->AttachList);
924 READ_OFFSET (stream, header->LigCaretList);
925 READ_OFFSET (stream, header->MarkAttachClassDef);
930 read_gdef_table (OTF *otf, OTF_Stream *stream)
932 char *errfmt = "GDEF%s";
936 OTF_CALLOC (gdef, 1, "");
937 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
938 if (gdef->header.GlyphClassDef)
940 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
941 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
943 if (gdef->header.AttachList)
944 read_attach_list (otf, stream, gdef->header.AttachList,
946 if (gdef->header.LigCaretList)
947 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
948 &gdef->lig_caret_list);
949 if (gdef->header.MarkAttachClassDef)
951 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
952 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
959 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
962 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
963 OTF_ScriptList *list)
965 char *errfmt = "Script List%s";
969 SEEK_STREAM (stream, offset);
970 READ_USHORT (stream, list->ScriptCount);
971 OTF_CALLOC (list->Script, list->ScriptCount, "");
973 for (i = 0; i < list->ScriptCount; i++)
975 READ_TAG (stream, list->Script[i].ScriptTag);
976 READ_OFFSET (stream, list->Script[i].offset);
978 for (i = 0; i < list->ScriptCount; i++)
980 OTF_Script *script = list->Script + i;
981 long script_offset = offset + script->offset;
983 SEEK_STREAM (stream, script_offset);
984 READ_OFFSET (stream, script->DefaultLangSysOffset);
985 READ_USHORT (stream, script->LangSysCount);
986 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
987 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
988 for (j = 0; j < script->LangSysCount; j++)
990 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
991 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
994 if (script->DefaultLangSysOffset)
996 OTF_LangSys *langsys = &script->DefaultLangSys;
998 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
999 READ_OFFSET (stream, langsys->LookupOrder);
1000 READ_USHORT (stream, langsys->ReqFeatureIndex);
1001 READ_USHORT (stream, langsys->FeatureCount);
1002 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1004 for (k = 0; k < langsys->FeatureCount; k++)
1005 READ_USHORT (stream, langsys->FeatureIndex[k]);
1008 for (j = 0; j < script->LangSysCount; j++)
1010 OTF_LangSys *langsys = script->LangSys + j;
1012 SEEK_STREAM (stream,
1013 script_offset + script->LangSysRecord[j].LangSys);
1014 READ_OFFSET (stream, langsys->LookupOrder);
1015 READ_USHORT (stream, langsys->ReqFeatureIndex);
1016 READ_USHORT (stream, langsys->FeatureCount);
1017 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1019 for (k = 0; k < langsys->FeatureCount; k++)
1020 READ_USHORT (stream, langsys->FeatureIndex[k]);
1028 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1029 OTF_FeatureList *list)
1031 char *errfmt = "Feature List%s";
1035 READ_UINT16 (stream, list->FeatureCount);
1036 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1037 for (i = 0; i < list->FeatureCount; i++)
1039 READ_TAG (stream, list->Feature[i].FeatureTag);
1040 READ_OFFSET (stream, list->Feature[i].offset);
1042 for (i = 0; i < list->FeatureCount; i++)
1044 OTF_Feature *feature = list->Feature + i;
1046 SEEK_STREAM (stream, offset + feature->offset);
1047 READ_OFFSET (stream, feature->FeatureParams);
1048 READ_UINT16 (stream, feature->LookupCount);
1049 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1050 " (LookupListIndex)");
1051 for (j = 0; j < feature->LookupCount; j++)
1052 READ_UINT16 (stream, feature->LookupListIndex[j]);
1058 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1059 long offset, unsigned type,
1060 OTF_LookupSubTableGSUB *subtable);
1061 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1062 long offset, unsigned type,
1063 OTF_LookupSubTableGPOS *subtable);
1066 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1067 OTF_LookupList *list, int gsubp)
1069 char *errfmt = "Lookup List%s";
1073 SEEK_STREAM (stream, offset);
1074 READ_UINT16 (stream, list->LookupCount);
1075 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1077 for (i = 0; i < list->LookupCount; i++)
1078 READ_OFFSET (stream, list->Lookup[i].offset);
1079 for (i = 0; i < list->LookupCount; i++)
1081 OTF_Lookup *lookup = list->Lookup + i;
1083 SEEK_STREAM (stream, offset + lookup->offset);
1084 READ_UINT16 (stream, lookup->LookupType);
1085 READ_UINT16 (stream, lookup->LookupFlag);
1086 READ_UINT16 (stream, lookup->SubTableCount);
1087 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1088 " (SubTableOffset)");
1090 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1093 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1095 for (j = 0; j < lookup->SubTableCount; j++)
1096 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1097 for (j = 0; j < lookup->SubTableCount; j++)
1100 = offset + lookup->offset + lookup->SubTableOffset[j];
1103 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1105 lookup->SubTable.gsub + j) < 0
1106 : read_lookup_subtable_gpos (otf, stream, this_offset,
1108 lookup->SubTable.gpos + j) < 0)
1117 /*** (1-8) Structures common to GSUB and GPOS */
1120 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1121 OTF_LookupRecord **record, int count)
1123 char *errfmt = "LookupRecord%s";
1124 unsigned errret = 0;
1128 READ_UINT16 (stream, count);
1130 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1131 OTF_MALLOC (*record, count, "");
1132 for (i = 0; i < count; i++)
1134 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1135 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1141 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1143 char *errfmt = "List of Rule%s";
1144 unsigned errret = 0;
1145 OTF_StreamState state;
1149 READ_UINT16 (stream, count);
1151 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1152 OTF_MALLOC (*rule, count, "");
1153 for (i = 0; i < count; i++)
1155 READ_OFFSET (stream, (*rule)[i].offset);
1156 if (! (*rule)[i].offset)
1157 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1159 SAVE_STREAM (stream, state);
1160 for (i = 0; i < count; i++)
1162 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1163 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1164 if ((*rule)[i].GlyphCount == 0)
1165 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1166 READ_UINT16 (stream, (*rule)[i].LookupCount);
1167 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1168 (*rule)[i].GlyphCount) < 0)
1170 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1171 (*rule)[i].LookupCount) == 0)
1174 RESTORE_STREAM (stream, state);
1180 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1183 char *errfmt = "List of RuleSet%s";
1184 unsigned errret = 0;
1185 OTF_StreamState state;
1189 READ_UINT16 (stream, count);
1191 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1192 OTF_MALLOC (*set, count, "");
1193 for (i = 0; i < count; i++)
1195 READ_OFFSET (stream, (*set)[i].offset);
1196 if (! (*set)[i].offset)
1197 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1199 SAVE_STREAM (stream, state);
1200 for (i = 0; i < count; i++)
1202 SEEK_STREAM (stream, offset + (*set)[i].offset);
1204 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1206 if (! (*set)[i].RuleCount)
1209 RESTORE_STREAM (stream, state);
1214 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1215 OTF_ClassRule **rule)
1217 char *errfmt = "ClassRule%s";
1218 unsigned errret = 0;
1219 OTF_StreamState state;
1223 READ_UINT16 (stream, count);
1225 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1226 OTF_MALLOC (*rule, count, "");
1227 for (i = 0; i < count; i++)
1229 READ_OFFSET (stream, (*rule)[i].offset);
1230 if (! (*rule)[i].offset)
1231 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1233 SAVE_STREAM (stream, state);
1234 for (i = 0; i < count; i++)
1236 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1237 READ_USHORT (stream, (*rule)[i].GlyphCount);
1238 if (! (*rule)[i].GlyphCount)
1239 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1240 READ_USHORT (stream, (*rule)[i].LookupCount);
1241 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1242 0, (*rule)[i].GlyphCount - 1) < 0)
1244 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1245 (*rule)[i].LookupCount) == 0)
1248 RESTORE_STREAM (stream, state);
1253 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1256 char *errfmt = "ClassSet%s";
1257 unsigned errret = 0;
1258 OTF_StreamState state;
1262 READ_UINT16 (stream, count);
1264 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1265 OTF_MALLOC (*set, count, "");
1266 for (i = 0; i < count; i++)
1267 /* Offset can be zero. */
1268 READ_OFFSET (stream, (*set)[i].offset);
1269 SAVE_STREAM (stream, state);
1270 for (i = 0; i < count; i++)
1271 if ((*set)[i].offset)
1273 SEEK_STREAM (stream, offset + (*set)[i].offset);
1274 (*set)[i].ClassRuleCnt
1275 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1276 &(*set)[i].ClassRule);
1277 if (! (*set)[i].ClassRuleCnt)
1280 RESTORE_STREAM (stream, state);
1285 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1286 OTF_ChainRule **rule)
1288 char *errfmt = "ChainRule%s";
1289 unsigned errret = 0;
1293 READ_UINT16 (stream, count);
1295 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1296 OTF_MALLOC (*rule, count, "");
1297 for (i = 0; i < count; i++)
1298 READ_OFFSET (stream, (*rule)[i].offset);
1299 for (i = 0; i < count; i++)
1301 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1302 (*rule)[i].BacktrackGlyphCount
1303 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1304 (*rule)[i].InputGlyphCount
1305 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1306 if (! (*rule)[i].InputGlyphCount)
1307 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1308 (*rule)[i].LookaheadGlyphCount
1309 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1310 (*rule)[i].LookupCount
1311 = read_lookup_record_list (otf, stream,
1312 &(*rule)[i].LookupRecord, -1);
1313 if (! (*rule)[i].LookupCount)
1321 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1322 OTF_ChainRuleSet **set)
1324 char *errfmt = "ChainRuleSet%s";
1325 unsigned errret = 0;
1326 OTF_StreamState state;
1330 READ_UINT16 (stream, count);
1332 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1333 OTF_MALLOC (*set, count, "");
1334 for (i = 0; i < count; i++)
1336 READ_OFFSET (stream, (*set)[i].offset);
1337 if (! (*set)[i].offset)
1338 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1340 SAVE_STREAM (stream, state);
1341 for (i = 0; i < count; i++)
1343 SEEK_STREAM (stream, offset + (*set)[i].offset);
1344 (*set)[i].ChainRuleCount
1345 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1346 &(*set)[i].ChainRule);
1347 if (! (*set)[i].ChainRuleCount)
1350 RESTORE_STREAM (stream, state);
1355 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1356 OTF_ChainClassRule **rule)
1358 char *errfmt = "ChainClassRule%s";
1359 unsigned errret = 0;
1363 READ_UINT16 (stream, count);
1365 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1366 OTF_MALLOC (*rule, count, "");
1367 for (i = 0; i < count; i++)
1369 READ_OFFSET (stream, (*rule)[i].offset);
1370 if (! (*rule)[i].offset)
1371 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1373 for (i = 0; i < count; i++)
1375 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1376 (*rule)[i].BacktrackGlyphCount
1377 = read_glyph_ids (otf, stream,
1378 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1379 (*rule)[i].InputGlyphCount
1380 = read_glyph_ids (otf, stream,
1381 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1382 if (! (*rule)[i].InputGlyphCount)
1383 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1384 (*rule)[i].LookaheadGlyphCount
1385 = read_glyph_ids (otf, stream,
1386 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1387 (*rule)[i].LookupCount
1388 = read_lookup_record_list (otf, stream,
1389 &(*rule)[i].LookupRecord, -1);
1390 if (! (*rule)[i].LookupCount)
1397 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1398 OTF_ChainClassSet **set)
1400 char *errfmt = "ChainClassSet%s";
1401 unsigned errret = 0;
1402 OTF_StreamState state;
1406 READ_UINT16 (stream, count);
1408 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1409 OTF_MALLOC (*set, count, "");
1410 for (i = 0; i < count; i++)
1411 /* Offset may be zero. */
1412 READ_OFFSET (stream, (*set)[i].offset);
1413 SAVE_STREAM (stream, state);
1414 for (i = 0; i < count; i++)
1415 if ((*set)[i].offset)
1417 SEEK_STREAM (stream, offset + (*set)[i].offset);
1418 (*set)[i].ChainClassRuleCnt
1419 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1420 &(*set)[i].ChainClassRule);
1421 if (! (*set)[i].ChainClassRuleCnt)
1424 RESTORE_STREAM (stream, state);
1429 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1430 OTF_Coverage *coverage,OTF_Context1 *context1)
1432 if (read_coverage (otf, stream, offset, coverage) < 0)
1434 context1->RuleSetCount
1435 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1436 if (! context1->RuleSetCount)
1442 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1443 OTF_Coverage *coverage,OTF_Context2 *context2)
1445 if (read_coverage (otf, stream, offset, coverage) < 0
1446 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1448 context2->ClassSetCnt
1449 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1450 if (! context2->ClassSetCnt)
1456 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1457 OTF_Coverage *coverage,OTF_Context3 *context3)
1459 char *errfmt = "Context1%s";
1462 READ_USHORT (stream, context3->GlyphCount);
1463 if (context3->GlyphCount < 0)
1464 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1465 READ_USHORT (stream, context3->LookupCount);
1466 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1467 context3->GlyphCount) < 0)
1469 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1470 context3->LookupCount) < 0)
1476 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1477 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1479 if (read_coverage (otf, stream, offset, coverage) < 0)
1481 chain_context1->ChainRuleSetCount
1482 = read_chain_rule_set_list (otf, stream, offset,
1483 &chain_context1->ChainRuleSet);
1484 if (! chain_context1->ChainRuleSetCount)
1490 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1491 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1493 if (read_coverage (otf, stream, offset, coverage) < 0
1494 || read_class_def (otf, stream, offset,
1495 &chain_context2->BacktrackClassDef) < 0
1496 || read_class_def (otf, stream, offset,
1497 &chain_context2->InputClassDef) < 0
1498 || read_class_def (otf, stream, offset,
1499 &chain_context2->LookaheadClassDef) < 0)
1501 chain_context2->ChainClassSetCnt
1502 = read_chain_class_set_list (otf, stream, offset,
1503 &chain_context2->ChainClassSet);
1504 if (! chain_context2->ChainClassSetCnt)
1510 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1511 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1515 count = read_coverage_list (otf, stream, offset,
1516 &chain_context3->Backtrack, -1);
1519 chain_context3->BacktrackGlyphCount = (unsigned) count;
1520 count = read_coverage_list (otf, stream, offset,
1521 &chain_context3->Input, -1);
1524 chain_context3->InputGlyphCount = (unsigned) count;
1525 *coverage = chain_context3->Input[0];
1526 count = read_coverage_list (otf, stream, offset,
1527 &chain_context3->LookAhead, -1);
1528 chain_context3->LookaheadGlyphCount = (unsigned) count;
1529 chain_context3->LookupCount
1530 = read_lookup_record_list (otf, stream,
1531 &chain_context3->LookupRecord, -1);
1536 read_gsub_gpos_table (OTF *otf, OTF_Stream *stream, int gsubp)
1538 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1539 void *errret = NULL;
1540 OTF_GSUB_GPOS *gsub_gpos;
1542 OTF_CALLOC (gsub_gpos, 1, "");
1543 READ_FIXED (stream, gsub_gpos->Version);
1544 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1545 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1546 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1548 if (read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1549 &gsub_gpos->ScriptList) < 0
1550 || read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1551 &gsub_gpos->FeatureList) < 0
1552 || read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1553 &gsub_gpos->LookupList, gsubp) < 0)
1559 /* (1-9) "GSUB" table */
1562 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1564 char *errfmt = "Sequence%s";
1565 unsigned errret = 0;
1569 READ_UINT16 (stream, count);
1571 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1572 OTF_MALLOC (*seq, count, "");
1573 for (i = 0; i < count; i++)
1574 READ_OFFSET (stream, (*seq)[i].offset);
1575 for (i = 0; i < count; i++)
1577 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1578 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1579 &(*seq)[i].Substitute, 0, -1);
1580 if (! (*seq)[i].GlyphCount)
1587 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1588 OTF_Ligature **ligature)
1590 char *errfmt = "Ligature%s";
1595 READ_UINT16 (stream, count);
1598 OTF_MALLOC (*ligature, count, "");
1599 for (i = 0; i < count; i++)
1600 READ_OFFSET (stream, (*ligature)[i].offset);
1601 for (i = 0; i < count; i++)
1603 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1604 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1605 (*ligature)[i].CompCount
1606 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1607 if (! (*ligature)[i].CompCount)
1614 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1615 OTF_LigatureSet **ligset)
1617 char *errfmt = "LigatureSet%s";
1622 READ_UINT16 (stream, count);
1625 OTF_MALLOC (*ligset, count, "");
1626 for (i = 0; i < count; i++)
1627 READ_OFFSET (stream, (*ligset)[i].offset);
1628 for (i = 0; i < count; i++)
1632 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1633 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1634 &(*ligset)[i].Ligature);
1637 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1643 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1644 OTF_AlternateSet **altset)
1646 char *errfmt = "AlternateSet%s";
1651 READ_UINT16 (stream, count);
1653 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1654 OTF_MALLOC (*altset, count, "");
1655 for (i = 0; i < count; i++)
1656 READ_OFFSET (stream, (*altset)[i].offset);
1657 for (i = 0; i < count; i++)
1661 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1662 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1665 (*altset)[i].GlyphCount = (unsigned) alt_count;
1671 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1672 OTF_Coverage *coverage,
1673 OTF_GSUB_ReverseChain1 *reverse_chain)
1677 if (read_coverage (otf, stream, offset, coverage) < 0)
1679 count = read_coverage_list (otf, stream, offset,
1680 &reverse_chain->Backtrack, -1);
1683 reverse_chain->BacktrackGlyphCount = (unsigned) count;
1684 count = read_coverage_list (otf, stream, offset,
1685 &reverse_chain->LookAhead, -1);
1688 reverse_chain->LookaheadGlyphCount = (unsigned) count;
1689 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
1692 reverse_chain->GlyphCount = count;
1697 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1698 unsigned type, OTF_LookupSubTableGSUB *subtable)
1703 SEEK_STREAM (stream, offset);
1704 READ_USHORT (stream, subtable->Format);
1705 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1709 if (subtable->Format == 1)
1711 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1713 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1715 else if (subtable->Format == 2)
1717 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1719 subtable->u.single2.GlyphCount
1720 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1722 if (! subtable->u.single2.GlyphCount)
1726 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1730 if (subtable->Format == 1)
1732 read_coverage (otf, stream, offset, &subtable->Coverage);
1733 subtable->u.multiple1.SequenceCount
1734 = read_sequence (otf, stream, offset,
1735 &subtable->u.multiple1.Sequence);
1738 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1742 if (subtable->Format == 1)
1744 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1746 subtable->u.alternate1.AlternateSetCount
1747 = read_alternate_set_list (otf, stream, offset,
1748 &subtable->u.alternate1.AlternateSet);
1749 if (! subtable->u.alternate1.AlternateSetCount)
1753 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1757 if (subtable->Format == 1)
1759 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1761 subtable->u.ligature1.LigSetCount
1762 = read_ligature_set_list (otf, stream, offset,
1763 &subtable->u.ligature1.LigatureSet);
1764 if (! subtable->u.ligature1.LigSetCount)
1768 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1772 if (subtable->Format == 1)
1774 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1775 &subtable->u.context1) < 0)
1778 else if (subtable->Format == 2)
1780 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1781 &subtable->u.context2) < 0)
1784 else if (subtable->Format == 3)
1786 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1787 &subtable->u.context3) < 0)
1791 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1795 if (subtable->Format == 1)
1797 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1798 &subtable->u.chain_context1) < 0)
1801 else if (subtable->Format == 2)
1803 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1804 &subtable->u.chain_context2) < 0)
1807 else if (subtable->Format == 3)
1809 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1810 &subtable->u.chain_context3) < 0)
1814 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1818 if (subtable->Format == 1)
1822 OTF_LookupSubTableGSUB *ex_subtable;
1824 READ_USHORT (stream, ex_type);
1825 READ_ULONG (stream, ex_offset);
1826 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
1827 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
1828 ex_type, ex_subtable) < 0)
1830 subtable->u.extension1.ExtensionLookupType = ex_type;
1831 subtable->u.extension1.ExtensionOffset = ex_offset;
1832 subtable->u.extension1.ExtensionSubtable = ex_subtable;
1835 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1839 if (subtable->Format == 1)
1841 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
1842 &subtable->u.reverse_chain1) < 0)
1846 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1850 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1856 read_gsub_table (OTF *otf, OTF_Stream *stream)
1858 return read_gsub_gpos_table (otf, stream, 1);
1862 /* (1-10) "GPOS" table */
1865 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1866 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1869 OTF_StreamState state;
1874 for (i = 0, size = 0; i < 8; i++)
1878 if (bit & OTF_XPlacement)
1879 READ_INT16 (stream, value_record->XPlacement);
1880 if (bit & OTF_XPlacement)
1881 READ_INT16 (stream, value_record->YPlacement);
1882 if (bit & OTF_XAdvance)
1883 READ_INT16 (stream, value_record->XAdvance);
1884 if (bit & OTF_YAdvance)
1885 READ_INT16 (stream, value_record->YAdvance);
1886 if (bit & OTF_XPlaDevice)
1887 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1888 if (bit & OTF_YPlaDevice)
1889 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1890 if (bit & OTF_XAdvDevice)
1891 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1892 if (bit & OTF_YAdvDevice)
1893 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1894 SAVE_STREAM (stream, state);
1895 if (value_record->XPlaDevice.offset)
1897 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1900 if (value_record->YPlaDevice.offset)
1902 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1905 if (value_record->XAdvDevice.offset)
1907 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1910 if (value_record->YAdvDevice.offset)
1912 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1915 RESTORE_STREAM (stream, state);
1921 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1923 char *errfmt = "Anchor%s";
1926 SEEK_STREAM (stream, offset + anchor->offset);
1927 READ_UINT16 (stream, anchor->AnchorFormat);
1928 READ_INT16 (stream, anchor->XCoordinate);
1929 READ_INT16 (stream, anchor->YCoordinate);
1930 if (anchor->AnchorFormat == 1)
1932 else if (anchor->AnchorFormat == 2)
1934 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1936 else if (anchor->AnchorFormat == 3)
1938 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1939 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1940 if (anchor->f.f2.XDeviceTable.offset)
1942 if (read_device_table (otf, stream, offset + anchor->offset,
1943 &anchor->f.f2.XDeviceTable) < 0)
1946 if (anchor->f.f2.YDeviceTable.offset)
1948 if (read_device_table (otf, stream, offset + anchor->offset,
1949 &anchor->f.f2.YDeviceTable) < 0)
1954 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1960 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1961 OTF_MarkArray *array)
1963 char *errfmt = "MarkArray%s";
1965 OTF_StreamState state;
1968 READ_OFFSET (stream, array->offset);
1969 SAVE_STREAM (stream, state);
1970 SEEK_STREAM (stream, offset + array->offset);
1971 READ_UINT16 (stream, array->MarkCount);
1972 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1973 for (i = 0; i < array->MarkCount; i++)
1975 READ_UINT16 (stream, array->MarkRecord[i].Class);
1976 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1978 for (i = 0; i < array->MarkCount; i++)
1979 if (read_anchor (otf, stream, offset + array->offset,
1980 &array->MarkRecord[i].MarkAnchor) < 0)
1982 RESTORE_STREAM (stream, state);
1987 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1988 unsigned ClassCount, OTF_AnchorArray *array)
1990 char *errfmt = "AnchorArray%s";
1992 OTF_StreamState state;
1995 READ_OFFSET (stream, array->offset);
1996 SAVE_STREAM (stream, state);
1997 SEEK_STREAM (stream, offset + array->offset);
1998 READ_UINT16 (stream, array->Count);
1999 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2000 for (i = 0; i < array->Count; i++)
2002 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2004 for (j = 0; j < ClassCount; j++)
2005 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2007 for (i = 0; i < array->Count; i++)
2008 for (j = 0; j < ClassCount; j++)
2009 if (read_anchor (otf, stream, offset + array->offset,
2010 &array->AnchorRecord[i].Anchor[j]) < 0)
2012 RESTORE_STREAM (stream, state);
2016 static OTF_PairSet *
2017 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2018 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2020 char *errfmt = "PairSet%s";
2021 void *errret = NULL;
2022 OTF_StreamState state;
2026 OTF_MALLOC (set, num, "");
2027 for (i = 0; i < num; i++)
2028 READ_OFFSET (stream, set[i].offset);
2029 SAVE_STREAM (stream, state);
2030 for (i = 0; i < num; i++)
2032 SEEK_STREAM (stream, offset + set[i].offset);
2033 READ_UINT16 (stream, set[i].PairValueCount);
2034 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2035 for (j = 0; j < set[i].PairValueCount; j++)
2037 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2039 READ_UINT16 (stream, rec->SecondGlyph);
2040 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2041 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2044 RESTORE_STREAM (stream, state);
2048 static OTF_Class1Record *
2049 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2050 unsigned num1, enum OTF_ValueFormat bit1,
2051 unsigned num2, enum OTF_ValueFormat bit2)
2053 char *errfmt = "Class1Record%s";
2054 void *errret = NULL;
2055 OTF_Class1Record *rec;
2058 OTF_MALLOC (rec, num1, "");
2059 for (i = 0; i < num1; i++)
2061 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2062 for (j = 0; j < num2; j++)
2064 if (read_value_record (otf, stream, offset,
2065 bit1, &rec[i].Class2Record[j].Value1) < 0
2066 || read_value_record (otf, stream, offset,
2067 bit2, &rec[i].Class2Record[j].Value2) < 0)
2075 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2076 OTF_EntryExitRecord **rec)
2078 char *errfmt = "EntryExitSet%s";
2082 OTF_StreamState state;
2084 READ_UINT16 (stream, count);
2086 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2087 OTF_MALLOC (*rec, count, "");
2088 for (i = 0; i < count; i++)
2090 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2091 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2093 SAVE_STREAM (stream, state);
2094 for (i = 0; i < count; i++)
2096 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2098 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2101 RESTORE_STREAM (stream, state);
2106 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2107 unsigned ClassCount, OTF_LigatureAttach *attach)
2109 char *errfmt = "LigatureAttach%s";
2113 SEEK_STREAM (stream, offset + attach->offset);
2114 READ_UINT16 (stream, attach->ComponentCount);
2115 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2116 for (i = 0; i < attach->ComponentCount; i++)
2118 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2119 " (ComponentRecord)");
2120 for (j = 0; j < ClassCount; j++)
2121 READ_OFFSET (stream,
2122 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2124 for (i = 0; i < attach->ComponentCount; i++)
2125 for (j = 0; j < ClassCount; j++)
2126 if (read_anchor (otf, stream, offset + attach->offset,
2127 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2133 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2134 unsigned class_count, OTF_LigatureArray *array)
2136 char *errfmt = "LigatureArray%s";
2138 OTF_StreamState state;
2141 READ_OFFSET (stream, array->offset);
2142 SAVE_STREAM (stream, state);
2143 SEEK_STREAM (stream, offset + array->offset);
2144 READ_UINT16 (stream, array->LigatureCount);
2145 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2146 for (i = 0; i < array->LigatureCount; i++)
2147 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2148 for (i = 0; i < array->LigatureCount; i++)
2149 read_ligature_attach (otf, stream, offset + array->offset,
2150 class_count, array->LigatureAttach + i);
2151 RESTORE_STREAM (stream, state);
2156 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2157 long offset, unsigned type,
2158 OTF_LookupSubTableGPOS *subtable)
2163 SEEK_STREAM (stream, offset);
2164 READ_UINT16 (stream, subtable->Format);
2165 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2169 if (subtable->Format == 1)
2171 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2172 read_value_record (otf, stream, offset,
2173 subtable->u.single1.ValueFormat,
2174 &subtable->u.single1.Value);
2176 else if (subtable->Format == 2)
2178 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2181 READ_UINT16 (stream, single2->ValueFormat);
2182 READ_UINT16 (stream, single2->ValueCount);
2183 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2184 for (i = 0; i < single2->ValueCount; i++)
2185 read_value_record (otf, stream, offset, single2->ValueFormat,
2186 single2->Value + i);
2189 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2193 if (subtable->Format == 1)
2195 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2197 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2198 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2199 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2200 subtable->u.pair1.PairSet
2201 = read_pair_set_list (otf, stream, offset,
2202 subtable->u.pair1.PairSetCount,
2203 subtable->u.pair1.ValueFormat1,
2204 subtable->u.pair1.ValueFormat2);
2205 if (! subtable->u.pair1.PairSet)
2208 else if (subtable->Format == 2)
2210 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2212 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2213 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2214 if (read_class_def (otf, stream, offset,
2215 &subtable->u.pair2.ClassDef1) < 0
2216 || read_class_def (otf, stream, offset,
2217 &subtable->u.pair2.ClassDef2) < 0)
2219 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2220 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2221 subtable->u.pair2.Class1Record
2222 = read_class1_record_list (otf, stream, offset,
2223 subtable->u.pair2.Class1Count,
2224 subtable->u.pair2.ValueFormat1,
2225 subtable->u.pair2.Class2Count,
2226 subtable->u.pair2.ValueFormat2);
2227 if (! subtable->u.pair2.Class1Record)
2231 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2235 if (subtable->Format == 1)
2237 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2239 subtable->u.cursive1.EntryExitCount
2240 = read_entry_exit_list (otf, stream, offset,
2241 &subtable->u.cursive1.EntryExitRecord);
2242 if (! subtable->u.cursive1.EntryExitCount)
2246 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2250 if (subtable->Format == 1)
2252 read_coverage (otf, stream, offset, &subtable->Coverage);
2253 read_coverage (otf, stream, offset,
2254 &subtable->u.mark_base1.BaseCoverage);
2255 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2256 read_mark_array (otf, stream, offset,
2257 &subtable->u.mark_base1.MarkArray);
2258 read_anchor_array (otf, stream, offset,
2259 subtable->u.mark_base1.ClassCount,
2260 &subtable->u.mark_base1.BaseArray);
2263 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2267 if (subtable->Format == 1)
2269 read_coverage (otf, stream, offset, &subtable->Coverage);
2270 read_coverage (otf, stream, offset,
2271 &subtable->u.mark_lig1.LigatureCoverage);
2272 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2273 read_mark_array (otf, stream, offset,
2274 &subtable->u.mark_lig1.MarkArray);
2275 read_ligature_array (otf, stream, offset,
2276 subtable->u.mark_lig1.ClassCount,
2277 &subtable->u.mark_lig1.LigatureArray);
2282 if (subtable->Format == 1)
2284 read_coverage (otf, stream, offset, &subtable->Coverage);
2285 read_coverage (otf, stream, offset,
2286 &subtable->u.mark_mark1.Mark2Coverage);
2287 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2288 read_mark_array (otf, stream, offset,
2289 &subtable->u.mark_mark1.Mark1Array);
2290 read_anchor_array (otf, stream, offset,
2291 subtable->u.mark_mark1.ClassCount,
2292 &subtable->u.mark_mark1.Mark2Array);
2295 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2299 if (subtable->Format == 1)
2301 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2302 &subtable->u.context1) < 0)
2305 else if (subtable->Format == 2)
2307 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2308 &subtable->u.context2) < 0)
2311 else if (subtable->Format == 3)
2313 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2314 &subtable->u.context3) < 0)
2318 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2322 if (subtable->Format == 1)
2324 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2325 &subtable->u.chain_context1) < 0)
2328 else if (subtable->Format == 2)
2330 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2331 &subtable->u.chain_context2) < 0)
2334 else if (subtable->Format == 3)
2336 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2337 &subtable->u.chain_context3) < 0)
2341 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2345 if (subtable->Format == 1)
2349 OTF_LookupSubTableGPOS *ex_subtable;
2351 READ_USHORT (stream, ex_type);
2352 READ_ULONG (stream, ex_offset);
2353 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2354 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2355 ex_type, ex_subtable) < 0)
2357 subtable->u.extension1.ExtensionLookupType = ex_type;
2358 subtable->u.extension1.ExtensionOffset = ex_offset;
2359 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2362 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2366 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2372 read_gpos_table (OTF *otf, OTF_Stream *stream)
2374 return read_gsub_gpos_table (otf, stream, 0);
2382 read_base_table (OTF_Stream *stream, long offset)
2386 OTF_MALLOC (base, 1);
2395 read_jstf_table (OTF_Stream *stream, long offset)
2399 OTF_MALLOC (jstf, 1);
2405 /*** (1-11) Structure for OTF */
2408 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2412 READ_FIXED (stream, table->sfnt_version);
2413 READ_USHORT (stream, table->numTables);
2414 READ_USHORT (stream, table->searchRange);
2415 READ_USHORT (stream, table->enterSelector);
2416 READ_USHORT (stream, table->rangeShift);
2421 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2426 READ_TAG (stream, tag);
2428 table->name[0] = tag >> 24;
2429 table->name[1] = (tag >> 16) & 0xFF;
2430 table->name[0] = (tag >> 8) & 0xFF;
2431 table->name[0] = tag >> 8;
2432 table->name[0] = '\0';
2433 READ_ULONG (stream, table->checkSum);
2434 READ_ULONG (stream, table->offset);
2435 READ_ULONG (stream, table->length);
2440 read_header_part (OTF *otf, FILE *fp)
2442 char *errfmt = "otf header%s";
2444 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2447 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2449 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2450 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2451 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2452 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2453 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2454 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2455 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2456 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2457 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2458 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2459 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2460 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2462 head_tag = OTF_tag ("head");
2463 name_tag = OTF_tag ("name");
2464 cmap_tag = OTF_tag ("cmap");
2465 gdef_tag = OTF_tag ("GDEF");
2466 gsub_tag = OTF_tag ("GSUB");
2467 gpos_tag = OTF_tag ("GPOS");
2469 stream = make_stream ();
2473 internal_data->header_stream = stream;
2475 /* Size of Offset Table is 12 bytes. */
2476 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2478 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2481 /* Size of each Table Directory is 16 bytes. */
2482 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2483 "Table Directory") < 0)
2486 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2487 for (i = 0; i < otf->offset_table.numTables; i++)
2489 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2490 OTF_TableInfo *table_info = NULL;
2494 if (tag == head_tag)
2495 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2496 else if (tag == name_tag)
2497 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2498 else if (tag == cmap_tag)
2499 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2500 else if (tag == gdef_tag)
2501 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2502 else if (tag == gsub_tag)
2503 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2504 else if (tag == gpos_tag)
2505 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2509 table_info->stream = make_stream ();
2510 if (setup_stream (table_info->stream, fp,
2511 otf->table_dirs[i].offset,
2512 otf->table_dirs[i].length,
2513 otf->table_dirs[i].name) < 0)
2518 internal_data->header_stream = NULL;
2519 free_stream (stream);
2523 static OTF_TableInfo *
2524 get_table_info (OTF *otf, char *name)
2526 char *errfmt = "OTF Table Read%s";
2527 OTF_TableInfo *errret = NULL;
2528 OTF_InternalData *internal_data = otf->internal_data;
2529 OTF_TableInfo *table_info;
2530 OTF_Tag tag = OTF_tag (name);
2533 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2535 if (tag == OTF_tag ("head"))
2536 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2537 else if (tag == OTF_tag ("name"))
2538 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2539 else if (tag == OTF_tag ("cmap"))
2540 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2541 else if (tag == OTF_tag ("GDEF"))
2542 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2543 else if (tag == OTF_tag ("GSUB"))
2544 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2545 else if (tag == OTF_tag ("GPOS"))
2546 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2548 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2550 if (*table_info->address)
2553 if (! table_info->stream)
2554 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2555 if (! table_info->reader)
2556 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2562 /*** (2) API for reading OTF */
2564 /*** (2-1) OTF_open() */
2566 /* Note: We can't use memory allocation macros in the following
2567 functions because those macros return from the functions before
2568 freeing memory previously allocated. */
2571 OTF_open (char *otf_name)
2574 char *errfmt = "opening otf (%s)";
2575 void *errret = NULL;
2577 OTF_InternalData *internal_data;
2579 fp = fopen (otf_name, "r");
2581 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2582 otf = calloc (1, sizeof (OTF));
2584 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2585 otf->filename = strdup (otf_name);
2586 if (! otf->filename)
2590 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2593 internal_data = calloc (1, sizeof (OTF_InternalData));
2594 if (! internal_data)
2595 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2596 otf->internal_data = internal_data;
2597 if (! allocate_memory_record (otf))
2598 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2600 /* Here after, all pointers to allocated memory are recorded in
2601 otf->internal_data->memory_record except for what allocated by
2602 the functions allocate_memory_record and make_stream. */
2604 if (read_header_part (otf, fp) < 0)
2615 /*** (2-2) OTF_close() */
2618 OTF_close (OTF *otf)
2620 OTF_InternalData *internal_data = otf->internal_data;
2625 OTF_MemoryRecord *memrec = internal_data->memory_record;
2627 if (internal_data->header_stream)
2628 free_stream (internal_data->header_stream);
2630 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2631 if (internal_data->table_info[i].stream)
2632 free_stream (internal_data->table_info[i].stream);
2636 OTF_MemoryRecord *next = memrec->next;
2638 for (i = memrec->used - 1; i >= 0; i--)
2639 free (memrec->memory[i]);
2643 free (internal_data);
2648 /*** (2-3) OTF_get_table() */
2651 OTF_get_table (OTF *otf, char *name)
2653 OTF_TableInfo *table_info = get_table_info (otf, name);
2658 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2659 free_stream (table_info->stream);
2660 table_info->stream = NULL;
2661 if (! *table_info->address)
2663 table_info->reader = NULL;
2669 /*** (2-4) OTF_check_table() */
2672 OTF_check_table (OTF *otf, char *name)
2674 return (get_table_info (otf, name) ? 0 : -1);
2680 /*** (5) API miscellaneous ***/
2683 OTF_tag (char *name)
2685 unsigned char *p = (unsigned char *) name;
2689 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2693 OTF_tag_name (OTF_Tag tag, char *name)
2695 name[0] = (char) (tag >> 24);
2696 name[1] = (char) ((tag >> 16) & 0xFF);
2697 name[2] = (char) ((tag >> 8) & 0xFF);
2698 name[3] = (char) (tag & 0xFF);