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;
554 OTF_MALLOC (cmap->unicode_table, 0x10000, "");
555 switch (rec->subtable.format)
559 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
560 int segCount = sub4->segCountX2 / 2;
562 for (i = 0; i < segCount; i++)
564 OTF_cmapSegument *seg = sub4->segments + i;
567 if (seg->idRangeOffset == 0xFFFF)
568 for (c = seg->startCount; c <= seg->endCount; c++)
569 cmap->unicode_table[c] = c + seg->idDelta;
571 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
573 cmap->unicode_table[c]
574 = sub4->glyphIdArray[seg->idRangeOffset
575 + (c - seg->startCount)];
586 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
588 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
589 Glyph-IDs there. If COUNT is negative, read the number of
590 Glyphs-IDs at first. MINUS if nozero is how few the actual
591 Glyph-IDs are in STREAM than COUNT. */
594 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
595 int minus, int count)
597 char *errfmt = "GlyphID List%s";
602 READ_UINT16 (stream, count);
605 OTF_MALLOC (*ids, count, "");
606 for (i = 0; i < count + minus; i++)
607 READ_GLYPHID (stream, (*ids)[i]);
612 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
614 char *errfmt = "RangeRecord%s";
619 READ_UINT16 (stream, count);
622 OTF_MALLOC (*record, count, "");
623 for (i = 0; i < count; i++)
625 READ_GLYPHID (stream, (*record)[i].Start);
626 READ_GLYPHID (stream, (*record)[i].End);
627 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
634 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
635 OTF_Coverage *coverage)
637 char *errfmt = "Coverage%s";
639 OTF_StreamState state;
642 READ_OFFSET (stream, coverage->offset);
643 SAVE_STREAM (stream, state);
644 SEEK_STREAM (stream, offset + coverage->offset);
645 READ_UINT16 (stream, coverage->CoverageFormat);
646 if (coverage->CoverageFormat == 1)
647 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
648 else if (coverage->CoverageFormat == 2)
649 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
651 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
654 coverage->Count = (unsigned) count;
655 RESTORE_STREAM (stream, state);
659 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
660 and store the Coverages there. If COUNT is negative, read the
661 number of Coverages at first. */
664 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
665 OTF_Coverage **coverage, int count)
667 char *errfmt = "Coverage List%s";
672 READ_UINT16 (stream, count);
675 OTF_MALLOC (*coverage, count, "");
676 for (i = 0; i < count; i++)
677 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
684 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
687 char *errfmt = "ClassDef%s";
690 SEEK_STREAM (stream, class->offset);
691 READ_UINT16 (stream, class->ClassFormat);
692 if (class->ClassFormat == 1)
694 READ_GLYPHID (stream, class->f.f1.StartGlyph);
695 class->f.f1.GlyphCount
697 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
698 if (! class->f.f1.GlyphCount)
699 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
701 else if (class->ClassFormat == 2)
703 class->f.f2.ClassRangeCount
704 = (read_range_records
705 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
706 if (! class->f.f2.ClassRangeCount)
707 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
710 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
716 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
718 char *errfmt = "ClassDef%s";
720 OTF_StreamState state;
722 READ_OFFSET (stream, class->offset);
725 SAVE_STREAM (stream, state);
726 SEEK_STREAM (stream, offset + class->offset);
727 READ_UINT16 (stream, class->ClassFormat);
728 if (class->ClassFormat == 1)
730 READ_GLYPHID (stream, class->f.f1.StartGlyph);
731 class->f.f1.GlyphCount
732 = read_glyph_ids (otf, stream,
733 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
735 if (! class->f.f1.GlyphCount)
738 else if (class->ClassFormat == 2)
740 class->f.f2.ClassRangeCount
741 = read_range_records (otf, stream,
743 &class->f.f2.ClassRangeRecord);
744 if (! class->f.f2.ClassRangeCount)
748 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
750 RESTORE_STREAM (stream, state);
756 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
757 OTF_DeviceTable *table)
759 char *errfmt = "Device Table%s";
770 SEEK_STREAM (stream, offset + table->offset);
771 READ_UINT16 (stream, table->StartSize);
772 READ_UINT16 (stream, table->EndSize);
773 READ_UINT16 (stream, table->DeltaFormat);
774 num = table->EndSize - table->StartSize + 1;
775 OTF_MALLOC (table->DeltaValue, num, "");
777 if (table->DeltaFormat == 1)
778 for (i = 0; i < num; i++)
781 READ_UINT16 (stream, val);
782 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
783 table->DeltaValue[i] = intval.int2;
785 else if (table->DeltaFormat == 2)
786 for (i = 0; i < num; i++)
789 READ_UINT16 (stream, val);
790 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
791 table->DeltaValue[i] = intval.int4;
793 else if (table->DeltaFormat == 3)
794 for (i = 0; i < num; i++)
798 READ_UINT16 (stream, val);
799 intval.int8 = val >> 8;
800 table->DeltaValue[i] = intval.int8;
804 intval.int8 = val >> 8;
805 table->DeltaValue[i] = intval.int8;
809 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
814 /*** (1-6) "GDEF" table */
817 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
818 OTF_AttachList *list)
820 char *errfmt = "AttachList%s";
824 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
826 READ_UINT16 (stream, list->GlyphCount);
827 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
828 for (i = 0; i < list->GlyphCount; i++)
829 READ_OFFSET (stream, list->AttachPoint[i].offset);
830 for (i = 0; i < list->GlyphCount; i++)
834 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
835 READ_UINT16 (stream, count);
836 list->AttachPoint[i].PointCount = count;
837 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
838 for (j = 0; j < count; j++)
839 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
845 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
846 OTF_CaretValue *caret)
848 char *errfmt = "CaretValue%s";
851 SEEK_STREAM (stream, offset + caret->offset);
852 READ_UINT16 (stream, caret->CaretValueFormat);
853 if (caret->CaretValueFormat == 1)
854 READ_INT16 (stream, caret->f.f1.Coordinate);
855 else if (caret->CaretValueFormat == 2)
856 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
857 else if (caret->CaretValueFormat == 3)
859 READ_INT16 (stream, caret->f.f3.Coordinate);
860 if (read_device_table (otf, stream, offset + caret->offset,
861 &caret->f.f3.DeviceTable) < 0)
865 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
870 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
871 OTF_LigCaretList *list)
873 char *errfmt = "LigCaretList%s";
877 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
879 READ_UINT16 (stream, list->LigGlyphCount);
880 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
881 for (i = 0; i < list->LigGlyphCount; i++)
882 READ_OFFSET (stream, list->LigGlyph[i].offset);
883 for (i = 0; i < list->LigGlyphCount; i++)
887 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
888 READ_UINT16 (stream, count);
889 list->LigGlyph[i].CaretCount = count;
890 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
891 for (j = 0; j < count; j++)
892 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
893 for (j = 0; j < count; j++)
894 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
895 &list->LigGlyph[i].CaretValue[j]) < 0)
902 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
906 READ_FIXED (stream, header->Version);
907 READ_OFFSET (stream, header->GlyphClassDef);
908 READ_OFFSET (stream, header->AttachList);
909 READ_OFFSET (stream, header->LigCaretList);
910 READ_OFFSET (stream, header->MarkAttachClassDef);
915 read_gdef_table (OTF *otf, OTF_Stream *stream)
917 char *errfmt = "GDEF%s";
921 OTF_CALLOC (gdef, 1, "");
922 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
923 if (gdef->header.GlyphClassDef)
925 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
926 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
928 if (gdef->header.AttachList)
929 read_attach_list (otf, stream, gdef->header.AttachList,
931 if (gdef->header.LigCaretList)
932 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
933 &gdef->lig_caret_list);
934 if (gdef->header.MarkAttachClassDef)
936 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
937 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
944 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
947 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
948 OTF_ScriptList *list)
950 char *errfmt = "Script List%s";
954 SEEK_STREAM (stream, offset);
955 READ_USHORT (stream, list->ScriptCount);
956 OTF_CALLOC (list->Script, list->ScriptCount, "");
958 for (i = 0; i < list->ScriptCount; i++)
960 READ_TAG (stream, list->Script[i].ScriptTag);
961 READ_OFFSET (stream, list->Script[i].offset);
963 for (i = 0; i < list->ScriptCount; i++)
965 OTF_Script *script = list->Script + i;
966 long script_offset = offset + script->offset;
968 SEEK_STREAM (stream, script_offset);
969 READ_OFFSET (stream, script->DefaultLangSysOffset);
970 READ_USHORT (stream, script->LangSysCount);
971 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
972 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
973 for (j = 0; j < script->LangSysCount; j++)
975 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
976 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
979 if (script->DefaultLangSysOffset)
981 OTF_LangSys *langsys = &script->DefaultLangSys;
983 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
984 READ_OFFSET (stream, langsys->LookupOrder);
985 READ_USHORT (stream, langsys->ReqFeatureIndex);
986 READ_USHORT (stream, langsys->FeatureCount);
987 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
989 for (k = 0; k < langsys->FeatureCount; k++)
990 READ_USHORT (stream, langsys->FeatureIndex[k]);
993 for (j = 0; j < script->LangSysCount; j++)
995 OTF_LangSys *langsys = script->LangSys + j;
998 script_offset + script->LangSysRecord[j].LangSys);
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]);
1013 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1014 OTF_FeatureList *list)
1016 char *errfmt = "Feature List%s";
1020 READ_UINT16 (stream, list->FeatureCount);
1021 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1022 for (i = 0; i < list->FeatureCount; i++)
1024 READ_TAG (stream, list->Feature[i].FeatureTag);
1025 READ_OFFSET (stream, list->Feature[i].offset);
1027 for (i = 0; i < list->FeatureCount; i++)
1029 OTF_Feature *feature = list->Feature + i;
1031 SEEK_STREAM (stream, offset + feature->offset);
1032 READ_OFFSET (stream, feature->FeatureParams);
1033 READ_UINT16 (stream, feature->LookupCount);
1034 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1035 " (LookupListIndex)");
1036 for (j = 0; j < feature->LookupCount; j++)
1037 READ_UINT16 (stream, feature->LookupListIndex[j]);
1043 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1044 long offset, unsigned type,
1045 OTF_LookupSubTableGSUB *subtable);
1046 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1047 long offset, unsigned type,
1048 OTF_LookupSubTableGPOS *subtable);
1051 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1052 OTF_LookupList *list, int gsubp)
1054 char *errfmt = "Lookup List%s";
1058 SEEK_STREAM (stream, offset);
1059 READ_UINT16 (stream, list->LookupCount);
1060 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1062 for (i = 0; i < list->LookupCount; i++)
1063 READ_OFFSET (stream, list->Lookup[i].offset);
1064 for (i = 0; i < list->LookupCount; i++)
1066 OTF_Lookup *lookup = list->Lookup + i;
1068 SEEK_STREAM (stream, offset + lookup->offset);
1069 READ_UINT16 (stream, lookup->LookupType);
1070 READ_UINT16 (stream, lookup->LookupFlag);
1071 READ_UINT16 (stream, lookup->SubTableCount);
1072 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1073 " (SubTableOffset)");
1075 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1078 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1080 for (j = 0; j < lookup->SubTableCount; j++)
1081 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1082 for (j = 0; j < lookup->SubTableCount; j++)
1085 = offset + lookup->offset + lookup->SubTableOffset[j];
1088 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1090 lookup->SubTable.gsub + j) < 0
1091 : read_lookup_subtable_gpos (otf, stream, this_offset,
1093 lookup->SubTable.gpos + j) < 0)
1102 /*** (1-8) Structures common to GSUB and GPOS */
1105 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1106 OTF_LookupRecord **record, int count)
1108 char *errfmt = "LookupRecord%s";
1109 unsigned errret = 0;
1113 READ_UINT16 (stream, count);
1115 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1116 OTF_MALLOC (*record, count, "");
1117 for (i = 0; i < count; i++)
1119 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1120 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1126 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1128 char *errfmt = "List of Rule%s";
1129 unsigned errret = 0;
1130 OTF_StreamState state;
1134 READ_UINT16 (stream, count);
1136 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1137 OTF_MALLOC (*rule, count, "");
1138 for (i = 0; i < count; i++)
1140 READ_OFFSET (stream, (*rule)[i].offset);
1141 if (! (*rule)[i].offset)
1142 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1144 SAVE_STREAM (stream, state);
1145 for (i = 0; i < count; i++)
1147 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1148 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1149 if ((*rule)[i].GlyphCount == 0)
1150 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1151 READ_UINT16 (stream, (*rule)[i].LookupCount);
1152 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1153 (*rule)[i].GlyphCount) < 0)
1155 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1156 (*rule)[i].LookupCount) == 0)
1159 RESTORE_STREAM (stream, state);
1165 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1168 char *errfmt = "List of RuleSet%s";
1169 unsigned errret = 0;
1170 OTF_StreamState state;
1174 READ_UINT16 (stream, count);
1176 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1177 OTF_MALLOC (*set, count, "");
1178 for (i = 0; i < count; i++)
1180 READ_OFFSET (stream, (*set)[i].offset);
1181 if (! (*set)[i].offset)
1182 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1184 SAVE_STREAM (stream, state);
1185 for (i = 0; i < count; i++)
1187 SEEK_STREAM (stream, offset + (*set)[i].offset);
1189 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1191 if (! (*set)[i].RuleCount)
1194 RESTORE_STREAM (stream, state);
1199 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1200 OTF_ClassRule **rule)
1202 char *errfmt = "ClassRule%s";
1203 unsigned errret = 0;
1204 OTF_StreamState state;
1208 READ_UINT16 (stream, count);
1210 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1211 OTF_MALLOC (*rule, count, "");
1212 for (i = 0; i < count; i++)
1214 READ_OFFSET (stream, (*rule)[i].offset);
1215 if (! (*rule)[i].offset)
1216 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1218 SAVE_STREAM (stream, state);
1219 for (i = 0; i < count; i++)
1221 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1222 READ_USHORT (stream, (*rule)[i].GlyphCount);
1223 if (! (*rule)[i].GlyphCount)
1224 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1225 READ_USHORT (stream, (*rule)[i].LookupCount);
1226 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1227 0, (*rule)[i].GlyphCount - 1) < 0)
1229 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1230 (*rule)[i].LookupCount) == 0)
1233 RESTORE_STREAM (stream, state);
1238 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1241 char *errfmt = "ClassSet%s";
1242 unsigned errret = 0;
1243 OTF_StreamState state;
1247 READ_UINT16 (stream, count);
1249 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1250 OTF_MALLOC (*set, count, "");
1251 for (i = 0; i < count; i++)
1252 /* Offset can be zero. */
1253 READ_OFFSET (stream, (*set)[i].offset);
1254 SAVE_STREAM (stream, state);
1255 for (i = 0; i < count; i++)
1256 if ((*set)[i].offset)
1258 SEEK_STREAM (stream, offset + (*set)[i].offset);
1259 (*set)[i].ClassRuleCnt
1260 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1261 &(*set)[i].ClassRule);
1262 if (! (*set)[i].ClassRuleCnt)
1265 RESTORE_STREAM (stream, state);
1270 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1271 OTF_ChainRule **rule)
1273 char *errfmt = "ChainRule%s";
1274 unsigned errret = 0;
1278 READ_UINT16 (stream, count);
1280 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1281 OTF_MALLOC (*rule, count, "");
1282 for (i = 0; i < count; i++)
1283 READ_OFFSET (stream, (*rule)[i].offset);
1284 for (i = 0; i < count; i++)
1286 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1287 (*rule)[i].BacktrackGlyphCount
1288 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1289 (*rule)[i].InputGlyphCount
1290 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1291 if (! (*rule)[i].InputGlyphCount)
1292 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1293 (*rule)[i].LookaheadGlyphCount
1294 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1295 (*rule)[i].LookupCount
1296 = read_lookup_record_list (otf, stream,
1297 &(*rule)[i].LookupRecord, -1);
1298 if (! (*rule)[i].LookupCount)
1306 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1307 OTF_ChainRuleSet **set)
1309 char *errfmt = "ChainRuleSet%s";
1310 unsigned errret = 0;
1311 OTF_StreamState state;
1315 READ_UINT16 (stream, count);
1317 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1318 OTF_MALLOC (*set, count, "");
1319 for (i = 0; i < count; i++)
1321 READ_OFFSET (stream, (*set)[i].offset);
1322 if (! (*set)[i].offset)
1323 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1325 SAVE_STREAM (stream, state);
1326 for (i = 0; i < count; i++)
1328 SEEK_STREAM (stream, offset + (*set)[i].offset);
1329 (*set)[i].ChainRuleCount
1330 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1331 &(*set)[i].ChainRule);
1332 if (! (*set)[i].ChainRuleCount)
1335 RESTORE_STREAM (stream, state);
1340 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1341 OTF_ChainClassRule **rule)
1343 char *errfmt = "ChainClassRule%s";
1344 unsigned errret = 0;
1348 READ_UINT16 (stream, count);
1350 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1351 OTF_MALLOC (*rule, count, "");
1352 for (i = 0; i < count; i++)
1354 READ_OFFSET (stream, (*rule)[i].offset);
1355 if (! (*rule)[i].offset)
1356 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1358 for (i = 0; i < count; i++)
1360 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1361 (*rule)[i].BacktrackGlyphCount
1362 = read_glyph_ids (otf, stream,
1363 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1364 (*rule)[i].InputGlyphCount
1365 = read_glyph_ids (otf, stream,
1366 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1367 if (! (*rule)[i].InputGlyphCount)
1368 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1369 (*rule)[i].LookaheadGlyphCount
1370 = read_glyph_ids (otf, stream,
1371 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1372 (*rule)[i].LookupCount
1373 = read_lookup_record_list (otf, stream,
1374 &(*rule)[i].LookupRecord, -1);
1375 if (! (*rule)[i].LookupCount)
1382 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1383 OTF_ChainClassSet **set)
1385 char *errfmt = "ChainClassSet%s";
1386 unsigned errret = 0;
1387 OTF_StreamState state;
1391 READ_UINT16 (stream, count);
1393 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1394 OTF_MALLOC (*set, count, "");
1395 for (i = 0; i < count; i++)
1396 /* Offset may be zero. */
1397 READ_OFFSET (stream, (*set)[i].offset);
1398 SAVE_STREAM (stream, state);
1399 for (i = 0; i < count; i++)
1400 if ((*set)[i].offset)
1402 SEEK_STREAM (stream, offset + (*set)[i].offset);
1403 (*set)[i].ChainClassRuleCnt
1404 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1405 &(*set)[i].ChainClassRule);
1406 if (! (*set)[i].ChainClassRuleCnt)
1409 RESTORE_STREAM (stream, state);
1414 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1415 OTF_Coverage *coverage,OTF_Context1 *context1)
1417 if (read_coverage (otf, stream, offset, coverage) < 0)
1419 context1->RuleSetCount
1420 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1421 if (! context1->RuleSetCount)
1427 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1428 OTF_Coverage *coverage,OTF_Context2 *context2)
1430 if (read_coverage (otf, stream, offset, coverage) < 0
1431 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1433 context2->ClassSetCnt
1434 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1435 if (! context2->ClassSetCnt)
1441 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1442 OTF_Coverage *coverage,OTF_Context3 *context3)
1444 char *errfmt = "Context1%s";
1447 READ_USHORT (stream, context3->GlyphCount);
1448 if (context3->GlyphCount < 0)
1449 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1450 READ_USHORT (stream, context3->LookupCount);
1451 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1452 context3->GlyphCount) < 0)
1454 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1455 context3->LookupCount) < 0)
1461 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1462 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1464 if (read_coverage (otf, stream, offset, coverage) < 0)
1466 chain_context1->ChainRuleSetCount
1467 = read_chain_rule_set_list (otf, stream, offset,
1468 &chain_context1->ChainRuleSet);
1469 if (! chain_context1->ChainRuleSetCount)
1475 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1476 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1478 if (read_coverage (otf, stream, offset, coverage) < 0
1479 || read_class_def (otf, stream, offset,
1480 &chain_context2->BacktrackClassDef) < 0
1481 || read_class_def (otf, stream, offset,
1482 &chain_context2->InputClassDef) < 0
1483 || read_class_def (otf, stream, offset,
1484 &chain_context2->LookaheadClassDef) < 0)
1486 chain_context2->ChainClassSetCnt
1487 = read_chain_class_set_list (otf, stream, offset,
1488 &chain_context2->ChainClassSet);
1489 if (! chain_context2->ChainClassSetCnt)
1495 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1496 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1500 count = read_coverage_list (otf, stream, offset,
1501 &chain_context3->Backtrack, -1);
1504 chain_context3->BacktrackGlyphCount = (unsigned) count;
1505 count = read_coverage_list (otf, stream, offset,
1506 &chain_context3->Input, -1);
1509 chain_context3->InputGlyphCount = (unsigned) count;
1510 *coverage = chain_context3->Input[0];
1511 count = read_coverage_list (otf, stream, offset,
1512 &chain_context3->LookAhead, -1);
1513 chain_context3->LookaheadGlyphCount = (unsigned) count;
1514 chain_context3->LookupCount
1515 = read_lookup_record_list (otf, stream,
1516 &chain_context3->LookupRecord, -1);
1521 read_gsub_gpos_table (OTF *otf, OTF_Stream *stream, int gsubp)
1523 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1524 void *errret = NULL;
1525 OTF_GSUB_GPOS *gsub_gpos;
1527 OTF_CALLOC (gsub_gpos, 1, "");
1528 READ_FIXED (stream, gsub_gpos->Version);
1529 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1530 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1531 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1533 if (read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1534 &gsub_gpos->ScriptList) < 0
1535 || read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1536 &gsub_gpos->FeatureList) < 0
1537 || read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1538 &gsub_gpos->LookupList, gsubp) < 0)
1544 /* (1-9) "GSUB" table */
1547 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1549 char *errfmt = "Sequence%s";
1550 unsigned errret = 0;
1554 READ_UINT16 (stream, count);
1556 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1557 OTF_MALLOC (*seq, count, "");
1558 for (i = 0; i < count; i++)
1559 READ_OFFSET (stream, (*seq)[i].offset);
1560 for (i = 0; i < count; i++)
1562 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1563 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1564 &(*seq)[i].Substitute, 0, -1);
1565 if (! (*seq)[i].GlyphCount)
1572 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1573 OTF_Ligature **ligature)
1575 char *errfmt = "Ligature%s";
1580 READ_UINT16 (stream, count);
1583 OTF_MALLOC (*ligature, count, "");
1584 for (i = 0; i < count; i++)
1585 READ_OFFSET (stream, (*ligature)[i].offset);
1586 for (i = 0; i < count; i++)
1588 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1589 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1590 (*ligature)[i].CompCount
1591 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1592 if (! (*ligature)[i].CompCount)
1599 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1600 OTF_LigatureSet **ligset)
1602 char *errfmt = "LigatureSet%s";
1607 READ_UINT16 (stream, count);
1610 OTF_MALLOC (*ligset, count, "");
1611 for (i = 0; i < count; i++)
1612 READ_OFFSET (stream, (*ligset)[i].offset);
1613 for (i = 0; i < count; i++)
1617 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1618 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1619 &(*ligset)[i].Ligature);
1622 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1628 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1629 OTF_AlternateSet **altset)
1631 char *errfmt = "AlternateSet%s";
1636 READ_UINT16 (stream, count);
1638 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1639 OTF_MALLOC (*altset, count, "");
1640 for (i = 0; i < count; i++)
1641 READ_OFFSET (stream, (*altset)[i].offset);
1642 for (i = 0; i < count; i++)
1646 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1647 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1650 (*altset)[i].GlyphCount = (unsigned) alt_count;
1656 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1657 OTF_Coverage *coverage,
1658 OTF_GSUB_ReverseChain1 *reverse_chain)
1662 if (read_coverage (otf, stream, offset, coverage) < 0)
1664 count = read_coverage_list (otf, stream, offset,
1665 &reverse_chain->Backtrack, -1);
1668 reverse_chain->BacktrackGlyphCount = (unsigned) count;
1669 count = read_coverage_list (otf, stream, offset,
1670 &reverse_chain->LookAhead, -1);
1673 reverse_chain->LookaheadGlyphCount = (unsigned) count;
1674 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
1677 reverse_chain->GlyphCount = count;
1682 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1683 unsigned type, OTF_LookupSubTableGSUB *subtable)
1688 SEEK_STREAM (stream, offset);
1689 READ_USHORT (stream, subtable->Format);
1690 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1694 if (subtable->Format == 1)
1696 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1698 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1700 else if (subtable->Format == 2)
1702 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1704 subtable->u.single2.GlyphCount
1705 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1707 if (! subtable->u.single2.GlyphCount)
1711 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1715 if (subtable->Format == 1)
1717 read_coverage (otf, stream, offset, &subtable->Coverage);
1718 subtable->u.multiple1.SequenceCount
1719 = read_sequence (otf, stream, offset,
1720 &subtable->u.multiple1.Sequence);
1723 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1727 if (subtable->Format == 1)
1729 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1731 subtable->u.alternate1.AlternateSetCount
1732 = read_alternate_set_list (otf, stream, offset,
1733 &subtable->u.alternate1.AlternateSet);
1734 if (! subtable->u.alternate1.AlternateSetCount)
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.ligature1.LigSetCount
1747 = read_ligature_set_list (otf, stream, offset,
1748 &subtable->u.ligature1.LigatureSet);
1749 if (! subtable->u.ligature1.LigSetCount)
1753 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1757 if (subtable->Format == 1)
1759 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1760 &subtable->u.context1) < 0)
1763 else if (subtable->Format == 2)
1765 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1766 &subtable->u.context2) < 0)
1769 else if (subtable->Format == 3)
1771 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1772 &subtable->u.context3) < 0)
1776 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1780 if (subtable->Format == 1)
1782 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1783 &subtable->u.chain_context1) < 0)
1786 else if (subtable->Format == 2)
1788 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1789 &subtable->u.chain_context2) < 0)
1792 else if (subtable->Format == 3)
1794 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1795 &subtable->u.chain_context3) < 0)
1799 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1803 if (subtable->Format == 1)
1807 OTF_LookupSubTableGSUB *ex_subtable;
1809 READ_USHORT (stream, ex_type);
1810 READ_ULONG (stream, ex_offset);
1811 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
1812 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
1813 ex_type, ex_subtable) < 0)
1815 subtable->u.extension1.ExtensionLookupType = ex_type;
1816 subtable->u.extension1.ExtensionOffset = ex_offset;
1817 subtable->u.extension1.ExtensionSubtable = ex_subtable;
1820 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1824 if (subtable->Format == 1)
1826 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
1827 &subtable->u.reverse_chain1) < 0)
1831 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1835 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1841 read_gsub_table (OTF *otf, OTF_Stream *stream)
1843 return read_gsub_gpos_table (otf, stream, 1);
1847 /* (1-10) "GPOS" table */
1850 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1851 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1854 OTF_StreamState state;
1859 for (i = 0, size = 0; i < 8; i++)
1863 if (bit & OTF_XPlacement)
1864 READ_INT16 (stream, value_record->XPlacement);
1865 if (bit & OTF_XPlacement)
1866 READ_INT16 (stream, value_record->YPlacement);
1867 if (bit & OTF_XAdvance)
1868 READ_INT16 (stream, value_record->XAdvance);
1869 if (bit & OTF_YAdvance)
1870 READ_INT16 (stream, value_record->YAdvance);
1871 if (bit & OTF_XPlaDevice)
1872 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1873 if (bit & OTF_YPlaDevice)
1874 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1875 if (bit & OTF_XAdvDevice)
1876 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1877 if (bit & OTF_YAdvDevice)
1878 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1879 SAVE_STREAM (stream, state);
1880 if (value_record->XPlaDevice.offset)
1882 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1885 if (value_record->YPlaDevice.offset)
1887 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1890 if (value_record->XAdvDevice.offset)
1892 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1895 if (value_record->YAdvDevice.offset)
1897 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1900 RESTORE_STREAM (stream, state);
1906 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1908 char *errfmt = "Anchor%s";
1911 SEEK_STREAM (stream, offset + anchor->offset);
1912 READ_UINT16 (stream, anchor->AnchorFormat);
1913 READ_INT16 (stream, anchor->XCoordinate);
1914 READ_INT16 (stream, anchor->YCoordinate);
1915 if (anchor->AnchorFormat == 1)
1917 else if (anchor->AnchorFormat == 2)
1919 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1921 else if (anchor->AnchorFormat == 3)
1923 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1924 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1925 if (anchor->f.f2.XDeviceTable.offset)
1927 if (read_device_table (otf, stream, offset + anchor->offset,
1928 &anchor->f.f2.XDeviceTable) < 0)
1931 if (anchor->f.f2.YDeviceTable.offset)
1933 if (read_device_table (otf, stream, offset + anchor->offset,
1934 &anchor->f.f2.YDeviceTable) < 0)
1939 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1945 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1946 OTF_MarkArray *array)
1948 char *errfmt = "MarkArray%s";
1950 OTF_StreamState state;
1953 READ_OFFSET (stream, array->offset);
1954 SAVE_STREAM (stream, state);
1955 SEEK_STREAM (stream, offset + array->offset);
1956 READ_UINT16 (stream, array->MarkCount);
1957 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1958 for (i = 0; i < array->MarkCount; i++)
1960 READ_UINT16 (stream, array->MarkRecord[i].Class);
1961 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1963 for (i = 0; i < array->MarkCount; i++)
1964 if (read_anchor (otf, stream, offset + array->offset,
1965 &array->MarkRecord[i].MarkAnchor) < 0)
1967 RESTORE_STREAM (stream, state);
1972 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1973 unsigned ClassCount, OTF_AnchorArray *array)
1975 char *errfmt = "AnchorArray%s";
1977 OTF_StreamState state;
1980 READ_OFFSET (stream, array->offset);
1981 SAVE_STREAM (stream, state);
1982 SEEK_STREAM (stream, offset + array->offset);
1983 READ_UINT16 (stream, array->Count);
1984 OTF_MALLOC (array->AnchorRecord, array->Count, "");
1985 for (i = 0; i < array->Count; i++)
1987 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
1989 for (j = 0; j < ClassCount; j++)
1990 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
1992 for (i = 0; i < array->Count; i++)
1993 for (j = 0; j < ClassCount; j++)
1994 if (read_anchor (otf, stream, offset + array->offset,
1995 &array->AnchorRecord[i].Anchor[j]) < 0)
1997 RESTORE_STREAM (stream, state);
2001 static OTF_PairSet *
2002 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2003 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2005 char *errfmt = "PairSet%s";
2006 void *errret = NULL;
2007 OTF_StreamState state;
2011 OTF_MALLOC (set, num, "");
2012 for (i = 0; i < num; i++)
2013 READ_OFFSET (stream, set[i].offset);
2014 SAVE_STREAM (stream, state);
2015 for (i = 0; i < num; i++)
2017 SEEK_STREAM (stream, offset + set[i].offset);
2018 READ_UINT16 (stream, set[i].PairValueCount);
2019 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2020 for (j = 0; j < set[i].PairValueCount; j++)
2022 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2024 READ_UINT16 (stream, rec->SecondGlyph);
2025 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2026 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2029 RESTORE_STREAM (stream, state);
2033 static OTF_Class1Record *
2034 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2035 unsigned num1, enum OTF_ValueFormat bit1,
2036 unsigned num2, enum OTF_ValueFormat bit2)
2038 char *errfmt = "Class1Record%s";
2039 void *errret = NULL;
2040 OTF_Class1Record *rec;
2043 OTF_MALLOC (rec, num1, "");
2044 for (i = 0; i < num1; i++)
2046 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2047 for (j = 0; j < num2; j++)
2049 if (read_value_record (otf, stream, offset,
2050 bit1, &rec[i].Class2Record[j].Value1) < 0
2051 || read_value_record (otf, stream, offset,
2052 bit2, &rec[i].Class2Record[j].Value2) < 0)
2060 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2061 OTF_EntryExitRecord **rec)
2063 char *errfmt = "EntryExitSet%s";
2067 OTF_StreamState state;
2069 READ_UINT16 (stream, count);
2071 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2072 OTF_MALLOC (*rec, count, "");
2073 for (i = 0; i < count; i++)
2075 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2076 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2078 SAVE_STREAM (stream, state);
2079 for (i = 0; i < count; i++)
2081 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2083 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2086 RESTORE_STREAM (stream, state);
2091 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2092 unsigned ClassCount, OTF_LigatureAttach *attach)
2094 char *errfmt = "LigatureAttach%s";
2098 SEEK_STREAM (stream, offset + attach->offset);
2099 READ_UINT16 (stream, attach->ComponentCount);
2100 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2101 for (i = 0; i < attach->ComponentCount; i++)
2103 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2104 " (ComponentRecord)");
2105 for (j = 0; j < ClassCount; j++)
2106 READ_OFFSET (stream,
2107 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2109 for (i = 0; i < attach->ComponentCount; i++)
2110 for (j = 0; j < ClassCount; j++)
2111 if (read_anchor (otf, stream, offset + attach->offset,
2112 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2118 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2119 unsigned class_count, OTF_LigatureArray *array)
2121 char *errfmt = "LigatureArray%s";
2123 OTF_StreamState state;
2126 READ_OFFSET (stream, array->offset);
2127 SAVE_STREAM (stream, state);
2128 SEEK_STREAM (stream, offset + array->offset);
2129 READ_UINT16 (stream, array->LigatureCount);
2130 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2131 for (i = 0; i < array->LigatureCount; i++)
2132 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2133 for (i = 0; i < array->LigatureCount; i++)
2134 read_ligature_attach (otf, stream, offset + array->offset,
2135 class_count, array->LigatureAttach + i);
2136 RESTORE_STREAM (stream, state);
2141 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2142 long offset, unsigned type,
2143 OTF_LookupSubTableGPOS *subtable)
2148 SEEK_STREAM (stream, offset);
2149 READ_UINT16 (stream, subtable->Format);
2150 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2154 if (subtable->Format == 1)
2156 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2157 read_value_record (otf, stream, offset,
2158 subtable->u.single1.ValueFormat,
2159 &subtable->u.single1.Value);
2161 else if (subtable->Format == 2)
2163 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2166 READ_UINT16 (stream, single2->ValueFormat);
2167 READ_UINT16 (stream, single2->ValueCount);
2168 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2169 for (i = 0; i < single2->ValueCount; i++)
2170 read_value_record (otf, stream, offset, single2->ValueFormat,
2171 single2->Value + i);
2174 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2178 if (subtable->Format == 1)
2180 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2182 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2183 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2184 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2185 subtable->u.pair1.PairSet
2186 = read_pair_set_list (otf, stream, offset,
2187 subtable->u.pair1.PairSetCount,
2188 subtable->u.pair1.ValueFormat1,
2189 subtable->u.pair1.ValueFormat2);
2190 if (! subtable->u.pair1.PairSet)
2193 else if (subtable->Format == 2)
2195 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2197 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2198 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2199 if (read_class_def (otf, stream, offset,
2200 &subtable->u.pair2.ClassDef1) < 0
2201 || read_class_def (otf, stream, offset,
2202 &subtable->u.pair2.ClassDef2) < 0)
2204 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2205 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2206 subtable->u.pair2.Class1Record
2207 = read_class1_record_list (otf, stream, offset,
2208 subtable->u.pair2.Class1Count,
2209 subtable->u.pair2.ValueFormat1,
2210 subtable->u.pair2.Class2Count,
2211 subtable->u.pair2.ValueFormat2);
2212 if (! subtable->u.pair2.Class1Record)
2216 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2220 if (subtable->Format == 1)
2222 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2224 subtable->u.cursive1.EntryExitCount
2225 = read_entry_exit_list (otf, stream, offset,
2226 &subtable->u.cursive1.EntryExitRecord);
2227 if (! subtable->u.cursive1.EntryExitCount)
2231 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2235 if (subtable->Format == 1)
2237 read_coverage (otf, stream, offset, &subtable->Coverage);
2238 read_coverage (otf, stream, offset,
2239 &subtable->u.mark_base1.BaseCoverage);
2240 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2241 read_mark_array (otf, stream, offset,
2242 &subtable->u.mark_base1.MarkArray);
2243 read_anchor_array (otf, stream, offset,
2244 subtable->u.mark_base1.ClassCount,
2245 &subtable->u.mark_base1.BaseArray);
2248 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2252 if (subtable->Format == 1)
2254 read_coverage (otf, stream, offset, &subtable->Coverage);
2255 read_coverage (otf, stream, offset,
2256 &subtable->u.mark_lig1.LigatureCoverage);
2257 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2258 read_mark_array (otf, stream, offset,
2259 &subtable->u.mark_lig1.MarkArray);
2260 read_ligature_array (otf, stream, offset,
2261 subtable->u.mark_lig1.ClassCount,
2262 &subtable->u.mark_lig1.LigatureArray);
2267 if (subtable->Format == 1)
2269 read_coverage (otf, stream, offset, &subtable->Coverage);
2270 read_coverage (otf, stream, offset,
2271 &subtable->u.mark_mark1.Mark2Coverage);
2272 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2273 read_mark_array (otf, stream, offset,
2274 &subtable->u.mark_mark1.Mark1Array);
2275 read_anchor_array (otf, stream, offset,
2276 subtable->u.mark_mark1.ClassCount,
2277 &subtable->u.mark_mark1.Mark2Array);
2280 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2284 if (subtable->Format == 1)
2286 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2287 &subtable->u.context1) < 0)
2290 else if (subtable->Format == 2)
2292 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2293 &subtable->u.context2) < 0)
2296 else if (subtable->Format == 3)
2298 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2299 &subtable->u.context3) < 0)
2303 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2307 if (subtable->Format == 1)
2309 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2310 &subtable->u.chain_context1) < 0)
2313 else if (subtable->Format == 2)
2315 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2316 &subtable->u.chain_context2) < 0)
2319 else if (subtable->Format == 3)
2321 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2322 &subtable->u.chain_context3) < 0)
2326 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2330 if (subtable->Format == 1)
2334 OTF_LookupSubTableGPOS *ex_subtable;
2336 READ_USHORT (stream, ex_type);
2337 READ_ULONG (stream, ex_offset);
2338 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2339 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2340 ex_type, ex_subtable) < 0)
2342 subtable->u.extension1.ExtensionLookupType = ex_type;
2343 subtable->u.extension1.ExtensionOffset = ex_offset;
2344 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2347 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2351 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2357 read_gpos_table (OTF *otf, OTF_Stream *stream)
2359 return read_gsub_gpos_table (otf, stream, 0);
2367 read_base_table (OTF_Stream *stream, long offset)
2371 OTF_MALLOC (base, 1);
2380 read_jstf_table (OTF_Stream *stream, long offset)
2384 OTF_MALLOC (jstf, 1);
2390 /*** (1-11) Structure for OTF */
2393 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2397 READ_FIXED (stream, table->sfnt_version);
2398 READ_USHORT (stream, table->numTables);
2399 READ_USHORT (stream, table->searchRange);
2400 READ_USHORT (stream, table->enterSelector);
2401 READ_USHORT (stream, table->rangeShift);
2406 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2411 READ_TAG (stream, tag);
2413 table->name[0] = tag >> 24;
2414 table->name[1] = (tag >> 16) & 0xFF;
2415 table->name[0] = (tag >> 8) & 0xFF;
2416 table->name[0] = tag >> 8;
2417 table->name[0] = '\0';
2418 READ_ULONG (stream, table->checkSum);
2419 READ_ULONG (stream, table->offset);
2420 READ_ULONG (stream, table->length);
2425 read_header_part (OTF *otf, FILE *fp)
2427 char *errfmt = "otf header%s";
2429 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2432 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2434 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2435 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2436 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2437 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2438 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2439 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2440 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2441 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2442 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2443 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2444 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2445 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2447 head_tag = OTF_tag ("head");
2448 name_tag = OTF_tag ("name");
2449 cmap_tag = OTF_tag ("cmap");
2450 gdef_tag = OTF_tag ("GDEF");
2451 gsub_tag = OTF_tag ("GSUB");
2452 gpos_tag = OTF_tag ("GPOS");
2454 stream = make_stream ();
2458 internal_data->header_stream = stream;
2460 /* Size of Offset Table is 12 bytes. */
2461 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2463 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2466 /* Size of each Table Directory is 16 bytes. */
2467 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2468 "Table Directory") < 0)
2471 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2472 for (i = 0; i < otf->offset_table.numTables; i++)
2474 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2475 OTF_TableInfo *table_info = NULL;
2479 if (tag == head_tag)
2480 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2481 else if (tag == name_tag)
2482 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2483 else if (tag == cmap_tag)
2484 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2485 else if (tag == gdef_tag)
2486 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2487 else if (tag == gsub_tag)
2488 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2489 else if (tag == gpos_tag)
2490 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2494 table_info->stream = make_stream ();
2495 if (setup_stream (table_info->stream, fp,
2496 otf->table_dirs[i].offset,
2497 otf->table_dirs[i].length,
2498 otf->table_dirs[i].name) < 0)
2503 internal_data->header_stream = NULL;
2504 free_stream (stream);
2508 static OTF_TableInfo *
2509 get_table_info (OTF *otf, char *name)
2511 char *errfmt = "OTF Table Read%s";
2512 OTF_TableInfo *errret = NULL;
2513 OTF_InternalData *internal_data = otf->internal_data;
2514 OTF_TableInfo *table_info;
2515 OTF_Tag tag = OTF_tag (name);
2518 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2520 if (tag == OTF_tag ("head"))
2521 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2522 else if (tag == OTF_tag ("name"))
2523 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2524 else if (tag == OTF_tag ("cmap"))
2525 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2526 else if (tag == OTF_tag ("GDEF"))
2527 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2528 else if (tag == OTF_tag ("GSUB"))
2529 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2530 else if (tag == OTF_tag ("GPOS"))
2531 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2533 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2535 if (*table_info->address)
2538 if (! table_info->stream)
2539 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2540 if (! table_info->reader)
2541 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2547 /*** (2) API for reading OTF */
2549 /*** (2-1) OTF_open() */
2551 /* Note: We can't use memory allocation macros in the following
2552 functions because those macros return from the functions before
2553 freeing memory previously allocated. */
2556 OTF_open (char *otf_name)
2559 char *errfmt = "opening otf (%s)";
2560 void *errret = NULL;
2562 OTF_InternalData *internal_data;
2564 fp = fopen (otf_name, "r");
2566 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2567 otf = calloc (1, sizeof (OTF));
2569 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2570 otf->filename = strdup (otf_name);
2571 if (! otf->filename)
2575 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2578 internal_data = calloc (1, sizeof (OTF_InternalData));
2579 if (! internal_data)
2580 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2581 otf->internal_data = internal_data;
2582 if (! allocate_memory_record (otf))
2583 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2585 /* Here after, all pointers to allocated memory are recorded in
2586 otf->internal_data->memory_record except for what allocated by
2587 the functions allocate_memory_record and make_stream. */
2589 if (read_header_part (otf, fp) < 0)
2600 /*** (2-2) OTF_close() */
2603 OTF_close (OTF *otf)
2605 OTF_InternalData *internal_data = otf->internal_data;
2610 OTF_MemoryRecord *memrec = internal_data->memory_record;
2612 if (internal_data->header_stream)
2613 free_stream (internal_data->header_stream);
2615 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2616 if (internal_data->table_info[i].stream)
2617 free_stream (internal_data->table_info[i].stream);
2621 OTF_MemoryRecord *next = memrec->next;
2623 for (i = memrec->used - 1; i >= 0; i--)
2624 free (memrec->memory[i]);
2628 free (internal_data);
2633 /*** (2-3) OTF_get_table() */
2636 OTF_get_table (OTF *otf, char *name)
2638 OTF_TableInfo *table_info = get_table_info (otf, name);
2643 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2644 free_stream (table_info->stream);
2645 table_info->stream = NULL;
2646 if (! *table_info->address)
2648 table_info->reader = NULL;
2654 /*** (2-4) OTF_check_table() */
2657 OTF_check_table (OTF *otf, char *name)
2659 return (get_table_info (otf, name) ? 0 : -1);
2665 /*** (5) API miscellaneous ***/
2668 OTF_tag (char *name)
2670 unsigned char *p = (unsigned char *) name;
2674 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2678 OTF_tag_name (OTF_Tag tag, char *name)
2680 name[0] = (char) (tag >> 24);
2681 name[1] = (char) ((tag >> 16) & 0xFF);
2682 name[2] = (char) ((tag >> 8) & 0xFF);
2683 name[3] = (char) (tag & 0xFF);