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";
446 OTF_CALLOC (cmap, 1, "");
447 READ_USHORT (stream, cmap->version);
448 READ_USHORT (stream, cmap->numTables);
449 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
450 for (i = 0; i < cmap->numTables; i++)
452 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
453 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
454 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
455 if (cmap->EncodingRecord[i].platformID == 3
456 && cmap->EncodingRecord[i].encodingID == 1)
457 cmap->Unicode = cmap->EncodingRecord + i;
459 for (i = 0; i < cmap->numTables; i++)
463 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
464 READ_USHORT (stream, format);
465 cmap->EncodingRecord[i].subtable.format = format;
466 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
467 if (format == 8 || format == 10 || format == 12)
469 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
470 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
474 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
480 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
481 " (EncodingRecord)");
483 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
493 OTF_EncodingSubtable4 *sub4;
498 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
499 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
500 READ_USHORT (stream, sub4->segCountX2);
501 segCount = sub4->segCountX2 / 2;
502 READ_USHORT (stream, sub4->searchRange);
503 READ_USHORT (stream, sub4->entrySelector);
504 READ_USHORT (stream, sub4->rangeShift);
505 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
506 for (j = 0; j < segCount; j++)
507 READ_USHORT (stream, sub4->segments[j].endCount);
508 READ_USHORT (stream, dummy);
509 for (j = 0; j < segCount; j++)
510 READ_USHORT (stream, sub4->segments[j].startCount);
511 for (j = 0; j < segCount; j++)
512 READ_SHORT (stream, sub4->segments[j].idDelta);
513 for (j = 0; j < segCount; j++)
516 unsigned rest = 2 * (segCount - j);
518 READ_USHORT (stream, off);
520 sub4->segments[j].idRangeOffset = 0xFFFF;
522 sub4->segments[j].idRangeOffset = (off - rest) / 2;
524 j = (cmap->EncodingRecord[i].subtable.length
525 - (14 + 2 * (segCount * 4 + 1)));
526 sub4->GlyphCount = j / 2;
527 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
528 for (j = 0; j < sub4->GlyphCount; j++)
529 READ_USHORT (stream, sub4->glyphIdArray[j]);
537 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
539 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
540 Glyph-IDs there. If COUNT is negative, read the number of
541 Glyphs-IDs at first. MINUS if nozero is how few the actual
542 Glyph-IDs are in STREAM than COUNT. */
545 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
546 int minus, int count)
548 char *errfmt = "GlyphID List%s";
553 READ_UINT16 (stream, count);
556 OTF_MALLOC (*ids, count, "");
557 for (i = 0; i < count + minus; i++)
558 READ_GLYPHID (stream, (*ids)[i]);
563 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
565 char *errfmt = "RangeRecord%s";
570 READ_UINT16 (stream, count);
573 OTF_MALLOC (*record, count, "");
574 for (i = 0; i < count; i++)
576 READ_GLYPHID (stream, (*record)[i].Start);
577 READ_GLYPHID (stream, (*record)[i].End);
578 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
585 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
586 OTF_Coverage *coverage)
588 char *errfmt = "Coverage%s";
590 OTF_StreamState state;
593 READ_OFFSET (stream, coverage->offset);
594 SAVE_STREAM (stream, state);
595 SEEK_STREAM (stream, offset + coverage->offset);
596 READ_UINT16 (stream, coverage->CoverageFormat);
597 if (coverage->CoverageFormat == 1)
598 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
599 else if (coverage->CoverageFormat == 2)
600 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
602 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
605 coverage->Count = (unsigned) count;
606 RESTORE_STREAM (stream, state);
610 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
611 and store the Coverages there. If COUNT is negative, read the
612 number of Coverages at first. */
615 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
616 OTF_Coverage **coverage, int count)
618 char *errfmt = "Coverage List%s";
623 READ_UINT16 (stream, count);
626 OTF_MALLOC (*coverage, count, "");
627 for (i = 0; i < count; i++)
628 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
635 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
638 char *errfmt = "ClassDef%s";
641 SEEK_STREAM (stream, class->offset);
642 READ_UINT16 (stream, class->ClassFormat);
643 if (class->ClassFormat == 1)
645 READ_GLYPHID (stream, class->f.f1.StartGlyph);
646 class->f.f1.GlyphCount
648 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
649 if (! class->f.f1.GlyphCount)
650 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
652 else if (class->ClassFormat == 2)
654 class->f.f2.ClassRangeCount
655 = (read_range_records
656 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
657 if (! class->f.f2.ClassRangeCount)
658 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
661 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
667 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
669 char *errfmt = "ClassDef%s";
671 OTF_StreamState state;
673 READ_OFFSET (stream, class->offset);
676 SAVE_STREAM (stream, state);
677 SEEK_STREAM (stream, offset + class->offset);
678 READ_UINT16 (stream, class->ClassFormat);
679 if (class->ClassFormat == 1)
681 READ_GLYPHID (stream, class->f.f1.StartGlyph);
682 class->f.f1.GlyphCount
683 = read_glyph_ids (otf, stream,
684 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
686 if (! class->f.f1.GlyphCount)
689 else if (class->ClassFormat == 2)
691 class->f.f2.ClassRangeCount
692 = read_range_records (otf, stream,
694 &class->f.f2.ClassRangeRecord);
695 if (! class->f.f2.ClassRangeCount)
699 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
701 RESTORE_STREAM (stream, state);
707 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
708 OTF_DeviceTable *table)
710 char *errfmt = "Device Table%s";
721 SEEK_STREAM (stream, offset + table->offset);
722 READ_UINT16 (stream, table->StartSize);
723 READ_UINT16 (stream, table->EndSize);
724 READ_UINT16 (stream, table->DeltaFormat);
725 num = table->EndSize - table->StartSize + 1;
726 OTF_MALLOC (table->DeltaValue, num, "");
728 if (table->DeltaFormat == 1)
729 for (i = 0; i < num; i++)
732 READ_UINT16 (stream, val);
733 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
734 table->DeltaValue[i] = intval.int2;
736 else if (table->DeltaFormat == 2)
737 for (i = 0; i < num; i++)
740 READ_UINT16 (stream, val);
741 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
742 table->DeltaValue[i] = intval.int4;
744 else if (table->DeltaFormat == 3)
745 for (i = 0; i < num; i++)
749 READ_UINT16 (stream, val);
750 intval.int8 = val >> 8;
751 table->DeltaValue[i] = intval.int8;
755 intval.int8 = val >> 8;
756 table->DeltaValue[i] = intval.int8;
760 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
765 /*** (1-6) "GDEF" table */
768 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
769 OTF_AttachList *list)
771 char *errfmt = "AttachList%s";
775 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
777 READ_UINT16 (stream, list->GlyphCount);
778 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
779 for (i = 0; i < list->GlyphCount; i++)
780 READ_OFFSET (stream, list->AttachPoint[i].offset);
781 for (i = 0; i < list->GlyphCount; i++)
785 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
786 READ_UINT16 (stream, count);
787 list->AttachPoint[i].PointCount = count;
788 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
789 for (j = 0; j < count; j++)
790 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
796 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
797 OTF_CaretValue *caret)
799 char *errfmt = "CaretValue%s";
802 SEEK_STREAM (stream, offset + caret->offset);
803 READ_UINT16 (stream, caret->CaretValueFormat);
804 if (caret->CaretValueFormat == 1)
805 READ_INT16 (stream, caret->f.f1.Coordinate);
806 else if (caret->CaretValueFormat == 2)
807 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
808 else if (caret->CaretValueFormat == 3)
810 READ_INT16 (stream, caret->f.f3.Coordinate);
811 if (read_device_table (otf, stream, offset + caret->offset,
812 &caret->f.f3.DeviceTable) < 0)
816 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
821 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
822 OTF_LigCaretList *list)
824 char *errfmt = "LigCaretList%s";
828 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
830 READ_UINT16 (stream, list->LigGlyphCount);
831 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
832 for (i = 0; i < list->LigGlyphCount; i++)
833 READ_OFFSET (stream, list->LigGlyph[i].offset);
834 for (i = 0; i < list->LigGlyphCount; i++)
838 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
839 READ_UINT16 (stream, count);
840 list->LigGlyph[i].CaretCount = count;
841 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
842 for (j = 0; j < count; j++)
843 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
844 for (j = 0; j < count; j++)
845 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
846 &list->LigGlyph[i].CaretValue[j]) < 0)
853 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
857 READ_FIXED (stream, header->Version);
858 READ_OFFSET (stream, header->GlyphClassDef);
859 READ_OFFSET (stream, header->AttachList);
860 READ_OFFSET (stream, header->LigCaretList);
861 READ_OFFSET (stream, header->MarkAttachClassDef);
866 read_gdef_table (OTF *otf, OTF_Stream *stream)
868 char *errfmt = "GDEF%s";
872 OTF_CALLOC (gdef, 1, "");
873 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
874 if (gdef->header.GlyphClassDef)
876 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
877 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
879 if (gdef->header.AttachList)
880 read_attach_list (otf, stream, gdef->header.AttachList,
882 if (gdef->header.LigCaretList)
883 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
884 &gdef->lig_caret_list);
885 if (gdef->header.MarkAttachClassDef)
887 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
888 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
895 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
898 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
899 OTF_ScriptList *list)
901 char *errfmt = "Script List%s";
905 SEEK_STREAM (stream, offset);
906 READ_USHORT (stream, list->ScriptCount);
907 OTF_CALLOC (list->Script, list->ScriptCount, "");
909 for (i = 0; i < list->ScriptCount; i++)
911 READ_TAG (stream, list->Script[i].ScriptTag);
912 READ_OFFSET (stream, list->Script[i].offset);
914 for (i = 0; i < list->ScriptCount; i++)
916 OTF_Script *script = list->Script + i;
917 long script_offset = offset + script->offset;
919 SEEK_STREAM (stream, script_offset);
920 READ_OFFSET (stream, script->DefaultLangSysOffset);
921 READ_USHORT (stream, script->LangSysCount);
922 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
923 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
924 for (j = 0; j < script->LangSysCount; j++)
926 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
927 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
930 if (script->DefaultLangSysOffset)
932 OTF_LangSys *langsys = &script->DefaultLangSys;
934 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
935 READ_OFFSET (stream, langsys->LookupOrder);
936 READ_USHORT (stream, langsys->ReqFeatureIndex);
937 READ_USHORT (stream, langsys->FeatureCount);
938 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
940 for (k = 0; k < langsys->FeatureCount; k++)
941 READ_USHORT (stream, langsys->FeatureIndex[k]);
944 for (j = 0; j < script->LangSysCount; j++)
946 OTF_LangSys *langsys = script->LangSys + j;
949 script_offset + script->LangSysRecord[j].LangSys);
950 READ_OFFSET (stream, langsys->LookupOrder);
951 READ_USHORT (stream, langsys->ReqFeatureIndex);
952 READ_USHORT (stream, langsys->FeatureCount);
953 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
955 for (k = 0; k < langsys->FeatureCount; k++)
956 READ_USHORT (stream, langsys->FeatureIndex[k]);
964 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
965 OTF_FeatureList *list)
967 char *errfmt = "Feature List%s";
971 READ_UINT16 (stream, list->FeatureCount);
972 OTF_CALLOC (list->Feature, list->FeatureCount, "");
973 for (i = 0; i < list->FeatureCount; i++)
975 READ_TAG (stream, list->Feature[i].FeatureTag);
976 READ_OFFSET (stream, list->Feature[i].offset);
978 for (i = 0; i < list->FeatureCount; i++)
980 OTF_Feature *feature = list->Feature + i;
982 SEEK_STREAM (stream, offset + feature->offset);
983 READ_OFFSET (stream, feature->FeatureParams);
984 READ_UINT16 (stream, feature->LookupCount);
985 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
986 " (LookupListIndex)");
987 for (j = 0; j < feature->LookupCount; j++)
988 READ_UINT16 (stream, feature->LookupListIndex[j]);
994 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
995 long offset, unsigned type,
996 OTF_LookupSubTableGSUB *subtable);
997 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
998 long offset, unsigned type,
999 OTF_LookupSubTableGPOS *subtable);
1002 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1003 OTF_LookupList *list, int gsubp)
1005 char *errfmt = "Lookup List%s";
1009 SEEK_STREAM (stream, offset);
1010 READ_UINT16 (stream, list->LookupCount);
1011 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1013 for (i = 0; i < list->LookupCount; i++)
1014 READ_OFFSET (stream, list->Lookup[i].offset);
1015 for (i = 0; i < list->LookupCount; i++)
1017 OTF_Lookup *lookup = list->Lookup + i;
1019 SEEK_STREAM (stream, offset + lookup->offset);
1020 READ_UINT16 (stream, lookup->LookupType);
1021 READ_UINT16 (stream, lookup->LookupFlag);
1022 READ_UINT16 (stream, lookup->SubTableCount);
1023 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1024 " (SubTableOffset)");
1026 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1029 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1031 for (j = 0; j < lookup->SubTableCount; j++)
1032 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1033 for (j = 0; j < lookup->SubTableCount; j++)
1036 = offset + lookup->offset + lookup->SubTableOffset[j];
1039 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1041 lookup->SubTable.gsub + j) < 0
1042 : read_lookup_subtable_gpos (otf, stream, this_offset,
1044 lookup->SubTable.gpos + j) < 0)
1053 /*** (1-8) Structures common to GSUB and GPOS */
1056 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1057 OTF_LookupRecord **record, int count)
1059 char *errfmt = "LookupRecord%s";
1060 unsigned errret = 0;
1064 READ_UINT16 (stream, count);
1066 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1067 OTF_MALLOC (*record, count, "");
1068 for (i = 0; i < count; i++)
1070 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1071 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1077 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1079 char *errfmt = "List of Rule%s";
1080 unsigned errret = 0;
1081 OTF_StreamState state;
1085 READ_UINT16 (stream, count);
1087 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1088 OTF_MALLOC (*rule, count, "");
1089 for (i = 0; i < count; i++)
1091 READ_OFFSET (stream, (*rule)[i].offset);
1092 if (! (*rule)[i].offset)
1093 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1095 SAVE_STREAM (stream, state);
1096 for (i = 0; i < count; i++)
1098 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1099 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1100 if ((*rule)[i].GlyphCount == 0)
1101 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1102 READ_UINT16 (stream, (*rule)[i].LookupCount);
1103 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1104 (*rule)[i].GlyphCount) < 0)
1106 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1107 (*rule)[i].LookupCount) == 0)
1110 RESTORE_STREAM (stream, state);
1116 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1119 char *errfmt = "List of RuleSet%s";
1120 unsigned errret = 0;
1121 OTF_StreamState state;
1125 READ_UINT16 (stream, count);
1127 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1128 OTF_MALLOC (*set, count, "");
1129 for (i = 0; i < count; i++)
1131 READ_OFFSET (stream, (*set)[i].offset);
1132 if (! (*set)[i].offset)
1133 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1135 SAVE_STREAM (stream, state);
1136 for (i = 0; i < count; i++)
1138 SEEK_STREAM (stream, offset + (*set)[i].offset);
1140 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1142 if (! (*set)[i].RuleCount)
1145 RESTORE_STREAM (stream, state);
1150 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1151 OTF_ClassRule **rule)
1153 char *errfmt = "ClassRule%s";
1154 unsigned errret = 0;
1155 OTF_StreamState state;
1159 READ_UINT16 (stream, count);
1161 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1162 OTF_MALLOC (*rule, count, "");
1163 for (i = 0; i < count; i++)
1165 READ_OFFSET (stream, (*rule)[i].offset);
1166 if (! (*rule)[i].offset)
1167 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1169 SAVE_STREAM (stream, state);
1170 for (i = 0; i < count; i++)
1172 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1173 READ_USHORT (stream, (*rule)[i].GlyphCount);
1174 if (! (*rule)[i].GlyphCount)
1175 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1176 READ_USHORT (stream, (*rule)[i].LookupCount);
1177 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1178 0, (*rule)[i].GlyphCount - 1) < 0)
1180 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1181 (*rule)[i].LookupCount) == 0)
1184 RESTORE_STREAM (stream, state);
1189 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1192 char *errfmt = "ClassSet%s";
1193 unsigned errret = 0;
1194 OTF_StreamState state;
1198 READ_UINT16 (stream, count);
1200 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1201 OTF_MALLOC (*set, count, "");
1202 for (i = 0; i < count; i++)
1203 /* Offset can be zero. */
1204 READ_OFFSET (stream, (*set)[i].offset);
1205 SAVE_STREAM (stream, state);
1206 for (i = 0; i < count; i++)
1207 if ((*set)[i].offset)
1209 SEEK_STREAM (stream, offset + (*set)[i].offset);
1210 (*set)[i].ClassRuleCnt
1211 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1212 &(*set)[i].ClassRule);
1213 if (! (*set)[i].ClassRuleCnt)
1216 RESTORE_STREAM (stream, state);
1221 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1222 OTF_ChainRule **rule)
1224 char *errfmt = "ChainRule%s";
1225 unsigned errret = 0;
1229 READ_UINT16 (stream, count);
1231 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1232 OTF_MALLOC (*rule, count, "");
1233 for (i = 0; i < count; i++)
1234 READ_OFFSET (stream, (*rule)[i].offset);
1235 for (i = 0; i < count; i++)
1237 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1238 (*rule)[i].BacktrackGlyphCount
1239 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1240 (*rule)[i].InputGlyphCount
1241 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1242 if (! (*rule)[i].InputGlyphCount)
1243 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1244 (*rule)[i].LookaheadGlyphCount
1245 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1246 (*rule)[i].LookupCount
1247 = read_lookup_record_list (otf, stream,
1248 &(*rule)[i].LookupRecord, -1);
1249 if (! (*rule)[i].LookupCount)
1257 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1258 OTF_ChainRuleSet **set)
1260 char *errfmt = "ChainRuleSet%s";
1261 unsigned errret = 0;
1262 OTF_StreamState state;
1266 READ_UINT16 (stream, count);
1268 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1269 OTF_MALLOC (*set, count, "");
1270 for (i = 0; i < count; i++)
1272 READ_OFFSET (stream, (*set)[i].offset);
1273 if (! (*set)[i].offset)
1274 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1276 SAVE_STREAM (stream, state);
1277 for (i = 0; i < count; i++)
1279 SEEK_STREAM (stream, offset + (*set)[i].offset);
1280 (*set)[i].ChainRuleCount
1281 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1282 &(*set)[i].ChainRule);
1283 if (! (*set)[i].ChainRuleCount)
1286 RESTORE_STREAM (stream, state);
1291 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1292 OTF_ChainClassRule **rule)
1294 char *errfmt = "ChainClassRule%s";
1295 unsigned errret = 0;
1299 READ_UINT16 (stream, count);
1301 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1302 OTF_MALLOC (*rule, count, "");
1303 for (i = 0; i < count; i++)
1305 READ_OFFSET (stream, (*rule)[i].offset);
1306 if (! (*rule)[i].offset)
1307 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1309 for (i = 0; i < count; i++)
1311 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1312 (*rule)[i].BacktrackGlyphCount
1313 = read_glyph_ids (otf, stream,
1314 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1315 (*rule)[i].InputGlyphCount
1316 = read_glyph_ids (otf, stream,
1317 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1318 if (! (*rule)[i].InputGlyphCount)
1319 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1320 (*rule)[i].LookaheadGlyphCount
1321 = read_glyph_ids (otf, stream,
1322 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1323 (*rule)[i].LookupCount
1324 = read_lookup_record_list (otf, stream,
1325 &(*rule)[i].LookupRecord, -1);
1326 if (! (*rule)[i].LookupCount)
1333 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1334 OTF_ChainClassSet **set)
1336 char *errfmt = "ChainClassSet%s";
1337 unsigned errret = 0;
1338 OTF_StreamState state;
1342 READ_UINT16 (stream, count);
1344 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1345 OTF_MALLOC (*set, count, "");
1346 for (i = 0; i < count; i++)
1347 /* Offset may be zero. */
1348 READ_OFFSET (stream, (*set)[i].offset);
1349 SAVE_STREAM (stream, state);
1350 for (i = 0; i < count; i++)
1351 if ((*set)[i].offset)
1353 SEEK_STREAM (stream, offset + (*set)[i].offset);
1354 (*set)[i].ChainClassRuleCnt
1355 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1356 &(*set)[i].ChainClassRule);
1357 if (! (*set)[i].ChainClassRuleCnt)
1360 RESTORE_STREAM (stream, state);
1365 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1366 OTF_Coverage *coverage,OTF_Context1 *context1)
1368 if (read_coverage (otf, stream, offset, coverage) < 0)
1370 context1->RuleSetCount
1371 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1372 if (! context1->RuleSetCount)
1378 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1379 OTF_Coverage *coverage,OTF_Context2 *context2)
1381 if (read_coverage (otf, stream, offset, coverage) < 0
1382 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1384 context2->ClassSetCnt
1385 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1386 if (! context2->ClassSetCnt)
1392 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1393 OTF_Coverage *coverage,OTF_Context3 *context3)
1395 char *errfmt = "Context1%s";
1398 READ_USHORT (stream, context3->GlyphCount);
1399 if (context3->GlyphCount < 0)
1400 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1401 READ_USHORT (stream, context3->LookupCount);
1402 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1403 context3->GlyphCount) < 0)
1405 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1406 context3->LookupCount) < 0)
1412 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1413 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1415 if (read_coverage (otf, stream, offset, coverage) < 0)
1417 chain_context1->ChainRuleSetCount
1418 = read_chain_rule_set_list (otf, stream, offset,
1419 &chain_context1->ChainRuleSet);
1420 if (! chain_context1->ChainRuleSetCount)
1426 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1427 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1429 if (read_coverage (otf, stream, offset, coverage) < 0
1430 || read_class_def (otf, stream, offset,
1431 &chain_context2->BacktrackClassDef) < 0
1432 || read_class_def (otf, stream, offset,
1433 &chain_context2->InputClassDef) < 0
1434 || read_class_def (otf, stream, offset,
1435 &chain_context2->LookaheadClassDef) < 0)
1437 chain_context2->ChainClassSetCnt
1438 = read_chain_class_set_list (otf, stream, offset,
1439 &chain_context2->ChainClassSet);
1440 if (! chain_context2->ChainClassSetCnt)
1446 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1447 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1451 count = read_coverage_list (otf, stream, offset,
1452 &chain_context3->Backtrack, -1);
1455 chain_context3->BacktrackGlyphCount = (unsigned) count;
1456 count = read_coverage_list (otf, stream, offset,
1457 &chain_context3->Input, -1);
1460 chain_context3->InputGlyphCount = (unsigned) count;
1461 *coverage = chain_context3->Input[0];
1462 count = read_coverage_list (otf, stream, offset,
1463 &chain_context3->LookAhead, -1);
1464 chain_context3->LookaheadGlyphCount = (unsigned) count;
1465 chain_context3->LookupCount
1466 = read_lookup_record_list (otf, stream,
1467 &chain_context3->LookupRecord, -1);
1472 read_gsub_gpos_table (OTF *otf, OTF_Stream *stream, int gsubp)
1474 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1475 void *errret = NULL;
1476 OTF_GSUB_GPOS *gsub_gpos;
1478 OTF_CALLOC (gsub_gpos, 1, "");
1479 READ_FIXED (stream, gsub_gpos->Version);
1480 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1481 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1482 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1484 if (read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1485 &gsub_gpos->ScriptList) < 0
1486 || read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1487 &gsub_gpos->FeatureList) < 0
1488 || read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1489 &gsub_gpos->LookupList, gsubp) < 0)
1495 /* (1-9) "GSUB" table */
1498 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1500 char *errfmt = "Sequence%s";
1501 unsigned errret = 0;
1505 READ_UINT16 (stream, count);
1507 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1508 OTF_MALLOC (*seq, count, "");
1509 for (i = 0; i < count; i++)
1510 READ_OFFSET (stream, (*seq)[i].offset);
1511 for (i = 0; i < count; i++)
1513 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1514 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1515 &(*seq)[i].Substitute, 0, -1);
1516 if (! (*seq)[i].GlyphCount)
1523 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1524 OTF_Ligature **ligature)
1526 char *errfmt = "Ligature%s";
1531 READ_UINT16 (stream, count);
1534 OTF_MALLOC (*ligature, count, "");
1535 for (i = 0; i < count; i++)
1536 READ_OFFSET (stream, (*ligature)[i].offset);
1537 for (i = 0; i < count; i++)
1539 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1540 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1541 (*ligature)[i].CompCount
1542 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1543 if (! (*ligature)[i].CompCount)
1550 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1551 OTF_LigatureSet **ligset)
1553 char *errfmt = "LigatureSet%s";
1558 READ_UINT16 (stream, count);
1561 OTF_MALLOC (*ligset, count, "");
1562 for (i = 0; i < count; i++)
1563 READ_OFFSET (stream, (*ligset)[i].offset);
1564 for (i = 0; i < count; i++)
1568 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1569 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1570 &(*ligset)[i].Ligature);
1573 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1579 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1580 OTF_AlternateSet **altset)
1582 char *errfmt = "AlternateSet%s";
1587 READ_UINT16 (stream, count);
1589 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1590 OTF_MALLOC (*altset, count, "");
1591 for (i = 0; i < count; i++)
1592 READ_OFFSET (stream, (*altset)[i].offset);
1593 for (i = 0; i < count; i++)
1597 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1598 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1601 (*altset)[i].GlyphCount = (unsigned) alt_count;
1607 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1608 unsigned type, OTF_LookupSubTableGSUB *subtable)
1613 SEEK_STREAM (stream, offset);
1614 READ_UINT16 (stream, subtable->Format);
1615 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1619 if (subtable->Format == 1)
1621 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1623 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1625 else if (subtable->Format == 2)
1627 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1629 subtable->u.single2.GlyphCount
1630 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1632 if (! subtable->u.single2.GlyphCount)
1636 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1640 if (subtable->Format == 1)
1642 read_coverage (otf, stream, offset, &subtable->Coverage);
1643 subtable->u.multiple1.SequenceCount
1644 = read_sequence (otf, stream, offset,
1645 &subtable->u.multiple1.Sequence);
1648 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1652 if (subtable->Format == 1)
1654 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1656 subtable->u.alternate1.AlternateSetCount
1657 = read_alternate_set_list (otf, stream, offset,
1658 &subtable->u.alternate1.AlternateSet);
1659 if (! subtable->u.alternate1.AlternateSetCount)
1663 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1667 if (subtable->Format == 1)
1669 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1671 subtable->u.ligature1.LigSetCount
1672 = read_ligature_set_list (otf, stream, offset,
1673 &subtable->u.ligature1.LigatureSet);
1674 if (! subtable->u.ligature1.LigSetCount)
1678 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1682 if (subtable->Format == 1)
1684 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1685 &subtable->u.context1) < 0)
1688 else if (subtable->Format == 2)
1690 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1691 &subtable->u.context2) < 0)
1694 else if (subtable->Format == 3)
1696 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1697 &subtable->u.context3) < 0)
1701 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1705 if (subtable->Format == 1)
1707 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1708 &subtable->u.chain_context1) < 0)
1711 else if (subtable->Format == 2)
1713 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1714 &subtable->u.chain_context2) < 0)
1717 else if (subtable->Format == 3)
1719 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1720 &subtable->u.chain_context3) < 0)
1724 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1729 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1733 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1739 read_gsub_table (OTF *otf, OTF_Stream *stream)
1741 return read_gsub_gpos_table (otf, stream, 1);
1745 /* (1-10) "GPOS" table */
1748 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1749 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1752 OTF_StreamState state;
1757 for (i = 0, size = 0; i < 8; i++)
1761 if (bit & OTF_XPlacement)
1762 READ_INT16 (stream, value_record->XPlacement);
1763 if (bit & OTF_XPlacement)
1764 READ_INT16 (stream, value_record->YPlacement);
1765 if (bit & OTF_XAdvance)
1766 READ_INT16 (stream, value_record->XAdvance);
1767 if (bit & OTF_YAdvance)
1768 READ_INT16 (stream, value_record->YAdvance);
1769 if (bit & OTF_XPlaDevice)
1770 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1771 if (bit & OTF_YPlaDevice)
1772 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1773 if (bit & OTF_XAdvDevice)
1774 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1775 if (bit & OTF_YAdvDevice)
1776 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1777 SAVE_STREAM (stream, state);
1778 if (value_record->XPlaDevice.offset)
1780 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1783 if (value_record->YPlaDevice.offset)
1785 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1788 if (value_record->XAdvDevice.offset)
1790 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1793 if (value_record->YAdvDevice.offset)
1795 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1798 RESTORE_STREAM (stream, state);
1804 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1806 char *errfmt = "Anchor%s";
1809 SEEK_STREAM (stream, offset + anchor->offset);
1810 READ_UINT16 (stream, anchor->AnchorFormat);
1811 READ_INT16 (stream, anchor->XCoordinate);
1812 READ_INT16 (stream, anchor->YCoordinate);
1813 if (anchor->AnchorFormat == 1)
1815 else if (anchor->AnchorFormat == 2)
1817 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1819 else if (anchor->AnchorFormat == 3)
1821 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1822 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1823 if (anchor->f.f2.XDeviceTable.offset)
1825 if (read_device_table (otf, stream, offset + anchor->offset,
1826 &anchor->f.f2.XDeviceTable) < 0)
1829 if (anchor->f.f2.YDeviceTable.offset)
1831 if (read_device_table (otf, stream, offset + anchor->offset,
1832 &anchor->f.f2.YDeviceTable) < 0)
1837 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1843 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1844 OTF_MarkArray *array)
1846 char *errfmt = "MarkArray%s";
1848 OTF_StreamState state;
1851 READ_OFFSET (stream, array->offset);
1852 SAVE_STREAM (stream, state);
1853 SEEK_STREAM (stream, offset + array->offset);
1854 READ_UINT16 (stream, array->MarkCount);
1855 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1856 for (i = 0; i < array->MarkCount; i++)
1858 READ_UINT16 (stream, array->MarkRecord[i].Class);
1859 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1861 for (i = 0; i < array->MarkCount; i++)
1862 if (read_anchor (otf, stream, offset + array->offset,
1863 &array->MarkRecord[i].MarkAnchor) < 0)
1865 RESTORE_STREAM (stream, state);
1870 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1871 unsigned ClassCount, OTF_AnchorArray *array)
1873 char *errfmt = "AnchorArray%s";
1875 OTF_StreamState state;
1878 READ_OFFSET (stream, array->offset);
1879 SAVE_STREAM (stream, state);
1880 SEEK_STREAM (stream, offset + array->offset);
1881 READ_UINT16 (stream, array->Count);
1882 OTF_MALLOC (array->AnchorRecord, array->Count, "");
1883 for (i = 0; i < array->Count; i++)
1885 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
1887 for (j = 0; j < ClassCount; j++)
1888 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
1890 for (i = 0; i < array->Count; i++)
1891 for (j = 0; j < ClassCount; j++)
1892 if (read_anchor (otf, stream, offset + array->offset,
1893 &array->AnchorRecord[i].Anchor[j]) < 0)
1895 RESTORE_STREAM (stream, state);
1900 static OTF_Class1Record *
1901 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1902 unsigned num1, enum OTF_ValueFormat bit1,
1903 unsigned num2, enum OTF_ValueFormat bit2)
1905 char *errfmt = "Class1Record%s";
1906 void *errret = NULL;
1907 OTF_Class1Record *rec;
1910 OTF_MALLOC (rec, num1, "");
1911 for (i = 0; i < num1; i++)
1913 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1914 for (j = 0; j < num2; j++)
1916 if (read_value_record (otf, stream, offset,
1917 bit1, &rec[i].Class2Record[j].Value1) < 0
1918 || read_value_record (otf, stream, offset,
1919 bit2, &rec[i].Class2Record[j].Value2) < 0)
1927 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1928 long offset, unsigned type,
1929 OTF_LookupSubTableGPOS *subtable)
1934 SEEK_STREAM (stream, offset);
1935 READ_UINT16 (stream, subtable->Format);
1936 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
1940 if (subtable->Format == 1)
1942 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
1943 read_value_record (otf, stream, offset,
1944 subtable->u.single1.ValueFormat,
1945 &subtable->u.single1.Value);
1947 else if (subtable->Format == 2)
1949 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
1952 READ_UINT16 (stream, single2->ValueFormat);
1953 READ_UINT16 (stream, single2->ValueCount);
1954 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
1955 for (i = 0; i < single2->ValueCount; i++)
1956 read_value_record (otf, stream, offset, single2->ValueFormat,
1957 single2->Value + i);
1960 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1964 if (subtable->Format == 1)
1966 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1968 else if (subtable->Format == 2)
1970 SEEK_STREAM (stream, offset + 2);
1971 read_coverage (otf, stream, offset, &subtable->Coverage);
1972 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
1973 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
1974 read_class_def (otf, stream, offset,
1975 &subtable->u.pair2.ClassDef1);
1976 read_class_def (otf, stream, offset,
1977 &subtable->u.pair2.ClassDef2);
1978 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
1979 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
1980 subtable->u.pair2.Class1Record
1981 = read_class1_record_list (otf, stream, offset,
1982 subtable->u.pair2.Class1Count,
1983 subtable->u.pair2.ValueFormat1,
1984 subtable->u.pair2.Class2Count,
1985 subtable->u.pair2.ValueFormat2);
1988 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1992 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1996 if (subtable->Format == 1)
1998 read_coverage (otf, stream, offset, &subtable->Coverage);
1999 read_coverage (otf, stream, offset,
2000 &subtable->u.mark_base1.BaseCoverage);
2001 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2002 read_mark_array (otf, stream, offset,
2003 &subtable->u.mark_base1.MarkArray);
2004 read_anchor_array (otf, stream, offset,
2005 subtable->u.mark_base1.ClassCount,
2006 &subtable->u.mark_base1.BaseArray);
2009 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2013 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
2017 if (subtable->Format == 1)
2019 read_coverage (otf, stream, offset, &subtable->Coverage);
2020 read_coverage (otf, stream, offset,
2021 &subtable->u.mark_mark1.Mark2Coverage);
2022 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2023 read_mark_array (otf, stream, offset,
2024 &subtable->u.mark_mark1.Mark1Array);
2025 read_anchor_array (otf, stream, offset,
2026 subtable->u.mark_mark1.ClassCount,
2027 &subtable->u.mark_mark1.Mark2Array);
2030 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2034 if (subtable->Format == 1)
2036 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2037 &subtable->u.context1) < 0)
2040 else if (subtable->Format == 2)
2042 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2043 &subtable->u.context2) < 0)
2046 else if (subtable->Format == 3)
2048 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2049 &subtable->u.context3) < 0)
2053 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2057 if (subtable->Format == 1)
2059 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2060 &subtable->u.chain_context1) < 0)
2063 else if (subtable->Format == 2)
2065 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2066 &subtable->u.chain_context2) < 0)
2069 else if (subtable->Format == 3)
2071 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2072 &subtable->u.chain_context3) < 0)
2076 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2080 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
2084 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2090 read_gpos_table (OTF *otf, OTF_Stream *stream)
2092 return read_gsub_gpos_table (otf, stream, 0);
2100 read_base_table (OTF_Stream *stream, long offset)
2104 OTF_MALLOC (base, 1);
2113 read_jstf_table (OTF_Stream *stream, long offset)
2117 OTF_MALLOC (jstf, 1);
2123 /*** (1-11) Structure for OTF */
2126 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2130 READ_FIXED (stream, table->sfnt_version);
2131 READ_USHORT (stream, table->numTables);
2132 READ_USHORT (stream, table->searchRange);
2133 READ_USHORT (stream, table->enterSelector);
2134 READ_USHORT (stream, table->rangeShift);
2139 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2144 READ_TAG (stream, tag);
2146 table->name[0] = tag >> 24;
2147 table->name[1] = (tag >> 16) & 0xFF;
2148 table->name[0] = (tag >> 8) & 0xFF;
2149 table->name[0] = tag >> 8;
2150 table->name[0] = '\0';
2151 READ_ULONG (stream, table->checkSum);
2152 READ_ULONG (stream, table->offset);
2153 READ_ULONG (stream, table->length);
2158 read_header_part (OTF *otf, FILE *fp)
2160 char *errfmt = "otf header%s";
2162 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2165 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2167 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2168 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2169 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2170 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2171 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2172 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2173 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2174 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2175 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2176 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2177 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2178 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2180 head_tag = OTF_tag ("head");
2181 name_tag = OTF_tag ("name");
2182 cmap_tag = OTF_tag ("cmap");
2183 gdef_tag = OTF_tag ("GDEF");
2184 gsub_tag = OTF_tag ("GSUB");
2185 gpos_tag = OTF_tag ("GPOS");
2187 stream = make_stream ();
2191 internal_data->header_stream = stream;
2193 /* Size of Offset Table is 12 bytes. */
2194 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2196 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2199 /* Size of each Table Directory is 16 bytes. */
2200 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2201 "Table Directory") < 0)
2204 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2205 for (i = 0; i < otf->offset_table.numTables; i++)
2207 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2208 OTF_TableInfo *table_info = NULL;
2212 if (tag == head_tag)
2213 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2214 else if (tag == name_tag)
2215 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2216 else if (tag == cmap_tag)
2217 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2218 else if (tag == gdef_tag)
2219 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2220 else if (tag == gsub_tag)
2221 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2222 else if (tag == gpos_tag)
2223 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2227 table_info->stream = make_stream ();
2228 if (setup_stream (table_info->stream, fp,
2229 otf->table_dirs[i].offset,
2230 otf->table_dirs[i].length,
2231 otf->table_dirs[i].name) < 0)
2236 internal_data->header_stream = NULL;
2237 free_stream (stream);
2241 static OTF_TableInfo *
2242 get_table_info (OTF *otf, char *name)
2244 char *errfmt = "OTF Table Read%s";
2245 OTF_TableInfo *errret = NULL;
2246 OTF_InternalData *internal_data = otf->internal_data;
2247 OTF_TableInfo *table_info;
2248 OTF_Tag tag = OTF_tag (name);
2251 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2253 if (tag == OTF_tag ("head"))
2254 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2255 else if (tag == OTF_tag ("name"))
2256 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2257 else if (tag == OTF_tag ("cmap"))
2258 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2259 else if (tag == OTF_tag ("GDEF"))
2260 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2261 else if (tag == OTF_tag ("GSUB"))
2262 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2263 else if (tag == OTF_tag ("GPOS"))
2264 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2266 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported table name)");
2268 if (*table_info->address)
2271 if (! table_info->stream)
2272 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2273 if (! table_info->reader)
2274 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2280 /*** (2) API for reading OTF */
2282 /*** (2-1) OTF_open() */
2284 /* Note: We can't use memory allocation macros in the following
2285 functions because those macros return from the functions before
2286 freeing memory previously allocated. */
2289 OTF_open (char *otf_name)
2292 char *errfmt = "opening otf (%s)";
2293 void *errret = NULL;
2295 OTF_InternalData *internal_data;
2297 fp = fopen (otf_name, "r");
2299 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2300 otf = calloc (1, sizeof (OTF));
2302 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2303 otf->filename = strdup (otf_name);
2304 if (! otf->filename)
2308 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2311 internal_data = calloc (1, sizeof (OTF_InternalData));
2312 if (! internal_data)
2313 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2314 otf->internal_data = internal_data;
2315 if (! allocate_memory_record (otf))
2316 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2318 /* Here after, all pointers to allocated memory are recorded in
2319 otf->internal_data->memory_record except for what allocated by
2320 the functions allocate_memory_record and make_stream. */
2322 if (read_header_part (otf, fp) < 0)
2333 /*** (2-2) OTF_close() */
2336 OTF_close (OTF *otf)
2338 OTF_InternalData *internal_data = otf->internal_data;
2343 OTF_MemoryRecord *memrec = internal_data->memory_record;
2345 if (internal_data->header_stream)
2346 free_stream (internal_data->header_stream);
2348 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2349 if (internal_data->table_info[i].stream)
2350 free_stream (internal_data->table_info[i].stream);
2354 OTF_MemoryRecord *next = memrec->next;
2356 for (i = memrec->used - 1; i >= 0; i--)
2357 free (memrec->memory[i]);
2361 free (internal_data);
2366 /*** (2-3) OTF_get_table() */
2369 OTF_get_table (OTF *otf, char *name)
2371 OTF_TableInfo *table_info = get_table_info (otf, name);
2376 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2377 free_stream (table_info->stream);
2378 table_info->stream = NULL;
2379 if (! *table_info->address)
2381 table_info->reader = NULL;
2387 /*** (2-4) OTF_check_table() */
2390 OTF_check_table (OTF *otf, char *name)
2392 return (get_table_info (otf, name) ? 0 : -1);
2398 /*** (5) API miscellaneous ***/
2401 OTF_tag (char *name)
2403 unsigned char *p = (unsigned char *) name;
2407 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2411 OTF_tag_name (OTF_Tag tag, char *name)
2413 name[0] = (char) (tag >> 24);
2414 name[1] = (char) ((tag >> 16) & 0xFF);
2415 name[2] = (char) ((tag >> 8) & 0xFF);
2416 name[3] = (char) (tag & 0xFF);