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
59 Example of typical usage of OTF_Stream.
63 OTF_StreamState state;
66 OPEN_STREAM (_FILE_NAME_, stream);
69 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
70 offset = READ_OFFSET (stream);
71 nbytes = READ_ULONG (stream);
72 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
74 CLOSE_STREAM (stream);
89 typedef long OTF_StreamState;
95 char *errfmt = "stream creation%s";
98 stream = calloc (1, sizeof (OTF_Stream));
100 OTF_ERROR (OTF_ERROR_MEMORY, "");
105 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
108 char *errfmt = "stream setup for %s";
113 if (stream->allocated < nbytes)
115 unsigned char *buf = malloc (nbytes);
118 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
122 stream->allocated = nbytes;
124 stream->bufsize = nbytes;
125 if (fseek (fp, offset, SEEK_SET) < 0)
126 OTF_ERROR (OTF_ERROR_FILE, stream->name);
127 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
128 OTF_ERROR (OTF_ERROR_FILE, stream->name);
134 free_stream (OTF_Stream *stream)
140 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
141 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
142 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
144 #define STREAM_CHECK_SIZE(stream, size) \
145 if ((stream)->pos + (size) > (stream)->bufsize) \
147 char *errfmt = "buffer overrun in %s"; \
149 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
155 #define READ_USHORT(stream, var) \
157 STREAM_CHECK_SIZE ((stream), 2); \
158 (var) = (((stream)->buf[(stream)->pos] << 8) \
159 | (stream)->buf[(stream)->pos + 1]); \
160 (stream)->pos += 2; \
163 #define READ_SHORT(stream, var) \
165 STREAM_CHECK_SIZE ((stream), 2); \
166 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
167 | (stream)->buf[(stream)->pos + 1]); \
168 (stream)->pos += 2; \
171 #define READ_ULONG(stream, var) \
173 STREAM_CHECK_SIZE ((stream), 4); \
174 (var) = (((stream)->buf[(stream)->pos] << 24) \
175 | ((stream)->buf[(stream)->pos + 1] << 16) \
176 | ((stream)->buf[(stream)->pos + 2] << 8) \
177 | (stream)->buf[(stream)->pos + 3]); \
178 (stream)->pos += 4; \
181 #define READ_LONG(stream, var) \
183 STREAM_CHECK_SIZE ((stream), 4); \
184 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
185 | ((stream)->buf[(stream)->pos + 1] << 16) \
186 | ((stream)->buf[(stream)->pos + 2] << 8) \
187 | (stream)->buf[(stream)->pos + 3]); \
188 (stream)->pos += 4; \
192 #define READ_FIXED(stream, fixed) \
194 READ_USHORT ((stream), (fixed).high); \
195 READ_USHORT ((stream), (fixed).low); \
199 #define READ_BYTES(stream, p, nbytes) \
201 STREAM_CHECK_SIZE ((stream), (nbytes)); \
202 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
203 (stream)->pos += (nbytes); \
207 #define READ_TAG READ_ULONG
208 #define READ_OFFSET READ_USHORT
209 #define READ_UINT16 READ_USHORT
210 #define READ_INT16 READ_SHORT
211 #define READ_GLYPHID READ_USHORT
214 /*** (1) Structures for OTF Layout tables and OTF itself */
216 /*** (1-1) Basic types and functions */
229 #define OTF_MEMORY_RECORD_SIZE 1024
231 struct OTF_MemoryRecord
234 void *memory[OTF_MEMORY_RECORD_SIZE];
235 struct OTF_MemoryRecord *next;
238 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
242 /* Points to one of OTF->head, OTF->name, etc. */
244 /* Function to read one of OTF tables. */
245 void *(*reader) (OTF *otf, OTF_Stream *stream);
246 /* Stream given to <reader>. */
250 struct OTF_InternalData
252 /* Information about each OTF table. */
253 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
255 /* Stream used to read the header part of OTF. */
256 OTF_Stream *header_stream;
258 /* Records of allocated memories. */
259 OTF_MemoryRecord *memory_record;
263 static OTF_MemoryRecord *
264 allocate_memory_record (OTF *otf)
266 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
267 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
272 memrec->next = internal_data->memory_record;
273 internal_data->memory_record = memrec;
277 /* Memory allocation macros. */
279 #define OTF_MALLOC(p, size, arg) \
285 OTF_MemoryRecord *memrec \
286 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
287 (p) = malloc (sizeof (*(p)) * (size)); \
289 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
290 && ! (memrec = allocate_memory_record (otf)))) \
291 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
292 memrec->memory[memrec->used++] = (p); \
297 #define OTF_CALLOC(p, size, arg) \
303 OTF_MemoryRecord *memrec \
304 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
305 (p) = calloc ((size), sizeof (*(p))); \
307 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
308 && ! (memrec = allocate_memory_record (otf)))) \
309 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
310 memrec->memory[memrec->used++] = (p); \
315 /*** (1-2) "head" table */
318 read_head_table (OTF *otf, OTF_Stream *stream)
320 char *errfmt = "head%s";
324 OTF_CALLOC (head, 1, "");
325 READ_FIXED (stream, head->TableVersionNumber);
326 READ_FIXED (stream, head->fontRevision);
327 READ_ULONG (stream, head->checkSumAdjustment);
328 READ_ULONG (stream, head->magicNumber);
329 READ_USHORT (stream, head->flags);
330 READ_USHORT (stream, head->unitsPerEm);
336 /*** (1-3) "name" table */
339 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
341 char *errfmt = "nameID (%d)";
343 OTF_StreamState state;
348 SAVE_STREAM (stream, state);
349 SEEK_STREAM (stream, stream->pos + rec->offset);
353 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
354 READ_BYTES (stream, str, rec->length);
355 for (i = 0; i < rec->length; i++)
361 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
362 for (i = 0; i < rec->length / 2; i++)
364 READ_USHORT (stream, c);
372 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
373 for (i = 0; i < rec->length / 4; i++)
375 READ_ULONG (stream, c);
382 RESTORE_STREAM (stream, state);
387 read_name_table (OTF *otf, OTF_Stream *stream)
389 char *errfmt = "name%s";
394 OTF_CALLOC (name, 1, "");
395 READ_USHORT (stream, name->format);
396 READ_USHORT (stream, name->count);
397 READ_USHORT (stream, name->stringOffset);
398 OTF_MALLOC (name->nameRecord, name->count, "");
399 for (i = 0; i < name->count; i++)
401 OTF_NameRecord *rec = name->nameRecord + i;
403 READ_USHORT (stream, rec->platformID);
404 READ_USHORT (stream, rec->encodingID);
405 READ_USHORT (stream, rec->languageID);
406 READ_USHORT (stream, rec->nameID);
407 READ_USHORT (stream, rec->length);
408 READ_USHORT (stream, rec->offset);
410 for (i = 0; i < name->count; i++)
412 OTF_NameRecord *rec = name->nameRecord + i;
413 int nameID = rec->nameID;
415 if (nameID <= OTF_max_nameID
416 && ! name->name[nameID])
418 if (rec->platformID == 0)
419 name->name[nameID] = read_name (otf, stream, rec,
420 rec->encodingID <= 3 ? 2 : 4);
421 else if (rec->platformID == 1
422 && rec->encodingID == 0)
423 name->name[nameID] = read_name (otf, stream, rec, 1);
424 else if (rec->platformID == 3
425 && (rec->encodingID == 1 || rec->encodingID == 10))
426 name->name[nameID] = read_name (otf, stream,
427 rec, rec->encodingID == 1 ? 2 : 4);
435 /*** (1-4) "cmap" table */
438 read_cmap_table (OTF *otf, OTF_Stream *stream)
440 char *errfmt = "cmap%s";
445 OTF_CALLOC (cmap, 1, "");
446 READ_USHORT (stream, cmap->version);
447 READ_USHORT (stream, cmap->numTables);
448 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
449 for (i = 0; i < cmap->numTables; i++)
451 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
452 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
453 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
454 if (cmap->EncodingRecord[i].platformID == 3
455 && cmap->EncodingRecord[i].encodingID == 1)
456 cmap->Unicode = cmap->EncodingRecord + i;
458 for (i = 0; i < cmap->numTables; i++)
462 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
463 READ_USHORT (stream, format);
464 cmap->EncodingRecord[i].subtable.format = format;
465 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
466 if (format == 8 || format == 10 || format == 12)
468 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
469 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
473 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
479 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
480 " (EncodingRecord)");
482 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
492 OTF_EncodingSubtable4 *sub4;
497 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
498 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
499 READ_USHORT (stream, sub4->segCountX2);
500 segCount = sub4->segCountX2 / 2;
501 READ_USHORT (stream, sub4->searchRange);
502 READ_USHORT (stream, sub4->entrySelector);
503 READ_USHORT (stream, sub4->rangeShift);
504 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
505 for (j = 0; j < segCount; j++)
506 READ_USHORT (stream, sub4->segments[j].endCount);
507 READ_USHORT (stream, dummy);
508 for (j = 0; j < segCount; j++)
509 READ_USHORT (stream, sub4->segments[j].startCount);
510 for (j = 0; j < segCount; j++)
511 READ_SHORT (stream, sub4->segments[j].idDelta);
512 for (j = 0; j < segCount; j++)
515 unsigned rest = 2 * (segCount - j);
517 READ_USHORT (stream, off);
519 sub4->segments[j].idRangeOffset = 0xFFFF;
521 sub4->segments[j].idRangeOffset = (off - rest) / 2;
523 j = (cmap->EncodingRecord[i].subtable.length
524 - (14 + 2 * (segCount * 4 + 1)));
525 sub4->GlyphCount = j / 2;
526 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
527 for (j = 0; j < sub4->GlyphCount; j++)
528 READ_USHORT (stream, sub4->glyphIdArray[j]);
536 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
538 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
539 Glyph-IDs there. If COUNT is negative, read the number of
540 Glyphs-IDs at first. MINUS if nozero is how few the actual
541 Glyph-IDs are in STREAM than COUNT. */
544 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
545 int minus, int count)
547 char *errfmt = "GlyphID List%s";
552 READ_UINT16 (stream, count);
555 OTF_MALLOC (*ids, count, "");
556 for (i = 0; i < count + minus; i++)
557 READ_GLYPHID (stream, (*ids)[i]);
562 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
564 char *errfmt = "RangeRecord%s";
569 READ_UINT16 (stream, count);
572 OTF_MALLOC (*record, count, "");
573 for (i = 0; i < count; i++)
575 READ_GLYPHID (stream, (*record)[i].Start);
576 READ_GLYPHID (stream, (*record)[i].End);
577 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
584 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
585 OTF_Coverage *coverage)
587 char *errfmt = "Coverage%s";
589 OTF_StreamState state;
592 READ_OFFSET (stream, coverage->offset);
593 SAVE_STREAM (stream, state);
594 SEEK_STREAM (stream, offset + coverage->offset);
595 READ_UINT16 (stream, coverage->CoverageFormat);
596 if (coverage->CoverageFormat == 1)
597 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
598 else if (coverage->CoverageFormat == 2)
599 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
601 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
604 coverage->Count = (unsigned) count;
605 RESTORE_STREAM (stream, state);
609 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
610 and store the Coverages there. If COUNT is negative, read the
611 number of Coverages at first. */
614 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
615 OTF_Coverage **coverage, int count)
617 char *errfmt = "Coverage List%s";
622 READ_UINT16 (stream, count);
625 OTF_MALLOC (*coverage, count, "");
626 for (i = 0; i < count; i++)
627 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
634 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
637 char *errfmt = "ClassDef%s";
640 SEEK_STREAM (stream, class->offset);
641 READ_UINT16 (stream, class->ClassFormat);
642 if (class->ClassFormat == 1)
644 READ_GLYPHID (stream, class->f.f1.StartGlyph);
645 class->f.f1.GlyphCount
647 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
648 if (! class->f.f1.GlyphCount)
649 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
651 else if (class->ClassFormat == 2)
653 class->f.f2.ClassRangeCount
654 = (read_range_records
655 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
656 if (! class->f.f2.ClassRangeCount)
657 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
660 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
666 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
668 char *errfmt = "ClassDef%s";
670 OTF_StreamState state;
672 READ_OFFSET (stream, class->offset);
675 SAVE_STREAM (stream, state);
676 SEEK_STREAM (stream, offset + class->offset);
677 READ_UINT16 (stream, class->ClassFormat);
678 if (class->ClassFormat == 1)
680 READ_GLYPHID (stream, class->f.f1.StartGlyph);
681 class->f.f1.GlyphCount
682 = read_glyph_ids (otf, stream,
683 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
685 if (! class->f.f1.GlyphCount)
688 else if (class->ClassFormat == 2)
690 class->f.f2.ClassRangeCount
691 = read_range_records (otf, stream,
693 &class->f.f2.ClassRangeRecord);
694 if (! class->f.f2.ClassRangeCount)
698 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
700 RESTORE_STREAM (stream, state);
706 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
707 OTF_DeviceTable *table)
709 char *errfmt = "Device Table%s";
720 SEEK_STREAM (stream, offset + table->offset);
721 READ_UINT16 (stream, table->StartSize);
722 READ_UINT16 (stream, table->EndSize);
723 READ_UINT16 (stream, table->DeltaFormat);
724 num = table->EndSize - table->StartSize + 1;
725 OTF_MALLOC (table->DeltaValue, num, "");
727 if (table->DeltaFormat == 1)
728 for (i = 0; i < num; i++)
731 READ_UINT16 (stream, val);
732 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
733 table->DeltaValue[i] = intval.int2;
735 else if (table->DeltaFormat == 2)
736 for (i = 0; i < num; i++)
739 READ_UINT16 (stream, val);
740 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
741 table->DeltaValue[i] = intval.int4;
743 else if (table->DeltaFormat == 3)
744 for (i = 0; i < num; i++)
748 READ_UINT16 (stream, val);
749 intval.int8 = val >> 8;
750 table->DeltaValue[i] = intval.int8;
754 intval.int8 = val >> 8;
755 table->DeltaValue[i] = intval.int8;
759 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
764 /*** (1-6) "GDEF" table */
767 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
768 OTF_AttachList *list)
770 char *errfmt = "AttachList%s";
774 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
776 READ_UINT16 (stream, list->GlyphCount);
777 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
778 for (i = 0; i < list->GlyphCount; i++)
779 READ_OFFSET (stream, list->AttachPoint[i].offset);
780 for (i = 0; i < list->GlyphCount; i++)
784 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
785 READ_UINT16 (stream, count);
786 list->AttachPoint[i].PointCount = count;
787 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
788 for (j = 0; j < count; j++)
789 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
795 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
796 OTF_CaretValue *caret)
798 char *errfmt = "CaretValue%s";
801 SEEK_STREAM (stream, offset + caret->offset);
802 READ_UINT16 (stream, caret->CaretValueFormat);
803 if (caret->CaretValueFormat == 1)
804 READ_INT16 (stream, caret->f.f1.Coordinate);
805 else if (caret->CaretValueFormat == 2)
806 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
807 else if (caret->CaretValueFormat == 3)
809 READ_INT16 (stream, caret->f.f3.Coordinate);
810 if (read_device_table (otf, stream, offset + caret->offset,
811 &caret->f.f3.DeviceTable) < 0)
815 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
820 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
821 OTF_LigCaretList *list)
823 char *errfmt = "LigCaretList%s";
827 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
829 READ_UINT16 (stream, list->LigGlyphCount);
830 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
831 for (i = 0; i < list->LigGlyphCount; i++)
832 READ_OFFSET (stream, list->LigGlyph[i].offset);
833 for (i = 0; i < list->LigGlyphCount; i++)
837 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
838 READ_UINT16 (stream, count);
839 list->LigGlyph[i].CaretCount = count;
840 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
841 for (j = 0; j < count; j++)
842 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
843 for (j = 0; j < count; j++)
844 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
845 &list->LigGlyph[i].CaretValue[j]) < 0)
852 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
856 READ_FIXED (stream, header->Version);
857 READ_OFFSET (stream, header->GlyphClassDef);
858 READ_OFFSET (stream, header->AttachList);
859 READ_OFFSET (stream, header->LigCaretList);
860 READ_OFFSET (stream, header->MarkAttachClassDef);
865 read_gdef_table (OTF *otf, OTF_Stream *stream)
867 char *errfmt = "GDEF%s";
871 OTF_CALLOC (gdef, 1, "");
872 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
873 if (gdef->header.GlyphClassDef)
875 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
876 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
878 if (gdef->header.AttachList)
879 read_attach_list (otf, stream, gdef->header.AttachList,
881 if (gdef->header.LigCaretList)
882 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
883 &gdef->lig_caret_list);
884 if (gdef->header.MarkAttachClassDef)
886 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
887 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
894 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
897 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
898 OTF_ScriptList *list)
900 char *errfmt = "Script List%s";
904 SEEK_STREAM (stream, offset);
905 READ_USHORT (stream, list->ScriptCount);
906 OTF_CALLOC (list->Script, list->ScriptCount, "");
908 for (i = 0; i < list->ScriptCount; i++)
910 READ_TAG (stream, list->Script[i].ScriptTag);
911 READ_OFFSET (stream, list->Script[i].offset);
913 for (i = 0; i < list->ScriptCount; i++)
915 OTF_Script *script = list->Script + i;
916 long script_offset = offset + script->offset;
918 SEEK_STREAM (stream, script_offset);
919 READ_OFFSET (stream, script->DefaultLangSysOffset);
920 READ_USHORT (stream, script->LangSysCount);
921 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
922 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
923 for (j = 0; j < script->LangSysCount; j++)
925 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
926 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
929 if (script->DefaultLangSysOffset)
931 OTF_LangSys *langsys = &script->DefaultLangSys;
933 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
934 READ_OFFSET (stream, langsys->LookupOrder);
935 READ_USHORT (stream, langsys->ReqFeatureIndex);
936 READ_USHORT (stream, langsys->FeatureCount);
937 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
939 for (k = 0; k < langsys->FeatureCount; k++)
940 READ_USHORT (stream, langsys->FeatureIndex[k]);
943 for (j = 0; j < script->LangSysCount; j++)
945 OTF_LangSys *langsys = script->LangSys + j;
948 script_offset + script->LangSysRecord[j].LangSys);
949 READ_OFFSET (stream, langsys->LookupOrder);
950 READ_USHORT (stream, langsys->ReqFeatureIndex);
951 READ_USHORT (stream, langsys->FeatureCount);
952 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
954 for (k = 0; k < langsys->FeatureCount; k++)
955 READ_USHORT (stream, langsys->FeatureIndex[k]);
963 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
964 OTF_FeatureList *list)
966 char *errfmt = "Feature List%s";
970 READ_UINT16 (stream, list->FeatureCount);
971 OTF_CALLOC (list->Feature, list->FeatureCount, "");
972 for (i = 0; i < list->FeatureCount; i++)
974 READ_TAG (stream, list->Feature[i].FeatureTag);
975 READ_OFFSET (stream, list->Feature[i].offset);
977 for (i = 0; i < list->FeatureCount; i++)
979 OTF_Feature *feature = list->Feature + i;
981 SEEK_STREAM (stream, offset + feature->offset);
982 READ_OFFSET (stream, feature->FeatureParams);
983 READ_UINT16 (stream, feature->LookupCount);
984 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
985 " (LookupListIndex)");
986 for (j = 0; j < feature->LookupCount; j++)
987 READ_UINT16 (stream, feature->LookupListIndex[j]);
993 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
994 long offset, unsigned type,
995 OTF_LookupSubTableGSUB *subtable);
996 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
997 long offset, unsigned type,
998 OTF_LookupSubTableGPOS *subtable);
1001 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1002 OTF_LookupList *list, int gsubp)
1004 char *errfmt = "Lookup List%s";
1008 SEEK_STREAM (stream, offset);
1009 READ_UINT16 (stream, list->LookupCount);
1010 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1012 for (i = 0; i < list->LookupCount; i++)
1013 READ_OFFSET (stream, list->Lookup[i].offset);
1014 for (i = 0; i < list->LookupCount; i++)
1016 OTF_Lookup *lookup = list->Lookup + i;
1018 SEEK_STREAM (stream, offset + lookup->offset);
1019 READ_UINT16 (stream, lookup->LookupType);
1020 READ_UINT16 (stream, lookup->LookupFlag);
1021 READ_UINT16 (stream, lookup->SubTableCount);
1022 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1023 " (SubTableOffset)");
1025 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1028 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1030 for (j = 0; j < lookup->SubTableCount; j++)
1031 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1032 for (j = 0; j < lookup->SubTableCount; j++)
1035 = offset + lookup->offset + lookup->SubTableOffset[j];
1038 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1040 lookup->SubTable.gsub + j) < 0
1041 : read_lookup_subtable_gpos (otf, stream, this_offset,
1043 lookup->SubTable.gpos + j) < 0)
1052 /*** (1-8) Structures common to GSUB and GPOS */
1055 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1056 OTF_LookupRecord **record, int count)
1058 char *errfmt = "LookupRecord%s";
1059 unsigned errret = 0;
1063 READ_UINT16 (stream, count);
1065 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1066 OTF_MALLOC (*record, count, "");
1067 for (i = 0; i < count; i++)
1069 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1070 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1076 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1078 char *errfmt = "List of Rule%s";
1079 unsigned errret = 0;
1080 OTF_StreamState state;
1084 READ_UINT16 (stream, count);
1086 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1087 OTF_MALLOC (*rule, count, "");
1088 for (i = 0; i < count; i++)
1090 READ_OFFSET (stream, (*rule)[i].offset);
1091 if (! (*rule)[i].offset)
1092 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1094 SAVE_STREAM (stream, state);
1095 for (i = 0; i < count; i++)
1097 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1098 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1099 if ((*rule)[i].GlyphCount == 0)
1100 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1101 READ_UINT16 (stream, (*rule)[i].LookupCount);
1102 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1103 (*rule)[i].GlyphCount) < 0)
1105 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1106 (*rule)[i].LookupCount) == 0)
1109 RESTORE_STREAM (stream, state);
1115 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1118 char *errfmt = "List of RuleSet%s";
1119 unsigned errret = 0;
1120 OTF_StreamState state;
1124 READ_UINT16 (stream, count);
1126 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1127 OTF_MALLOC (*set, count, "");
1128 for (i = 0; i < count; i++)
1130 READ_OFFSET (stream, (*set)[i].offset);
1131 if (! (*set)[i].offset)
1132 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1134 SAVE_STREAM (stream, state);
1135 for (i = 0; i < count; i++)
1137 SEEK_STREAM (stream, offset + (*set)[i].offset);
1139 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1141 if (! (*set)[i].RuleCount)
1144 RESTORE_STREAM (stream, state);
1149 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1150 OTF_ClassRule **rule)
1152 char *errfmt = "ClassRule%s";
1153 unsigned errret = 0;
1154 OTF_StreamState state;
1158 READ_UINT16 (stream, count);
1160 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1161 OTF_MALLOC (*rule, count, "");
1162 for (i = 0; i < count; i++)
1164 READ_OFFSET (stream, (*rule)[i].offset);
1165 if (! (*rule)[i].offset)
1166 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1168 SAVE_STREAM (stream, state);
1169 for (i = 0; i < count; i++)
1171 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1172 READ_USHORT (stream, (*rule)[i].GlyphCount);
1173 if (! (*rule)[i].GlyphCount)
1174 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1175 READ_USHORT (stream, (*rule)[i].LookupCount);
1176 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1177 0, (*rule)[i].GlyphCount - 1) < 0)
1179 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1180 (*rule)[i].LookupCount) == 0)
1183 RESTORE_STREAM (stream, state);
1188 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1191 char *errfmt = "ClassSet%s";
1192 unsigned errret = 0;
1193 OTF_StreamState state;
1197 READ_UINT16 (stream, count);
1199 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1200 OTF_MALLOC (*set, count, "");
1201 for (i = 0; i < count; i++)
1202 /* Offset can be zero. */
1203 READ_OFFSET (stream, (*set)[i].offset);
1204 SAVE_STREAM (stream, state);
1205 for (i = 0; i < count; i++)
1206 if ((*set)[i].offset)
1208 SEEK_STREAM (stream, offset + (*set)[i].offset);
1209 (*set)[i].ClassRuleCnt
1210 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1211 &(*set)[i].ClassRule);
1212 if (! (*set)[i].ClassRuleCnt)
1215 RESTORE_STREAM (stream, state);
1220 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1221 OTF_ChainRule **rule)
1223 char *errfmt = "ChainRule%s";
1224 unsigned errret = 0;
1228 READ_UINT16 (stream, count);
1230 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1231 OTF_MALLOC (*rule, count, "");
1232 for (i = 0; i < count; i++)
1233 READ_OFFSET (stream, (*rule)[i].offset);
1234 for (i = 0; i < count; i++)
1236 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1237 (*rule)[i].BacktrackGlyphCount
1238 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1239 (*rule)[i].InputGlyphCount
1240 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1241 if (! (*rule)[i].InputGlyphCount)
1242 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1243 (*rule)[i].LookaheadGlyphCount
1244 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1245 (*rule)[i].LookupCount
1246 = read_lookup_record_list (otf, stream,
1247 &(*rule)[i].LookupRecord, -1);
1248 if (! (*rule)[i].LookupCount)
1256 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1257 OTF_ChainRuleSet **set)
1259 char *errfmt = "ChainRuleSet%s";
1260 unsigned errret = 0;
1261 OTF_StreamState state;
1265 READ_UINT16 (stream, count);
1267 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1268 OTF_MALLOC (*set, count, "");
1269 for (i = 0; i < count; i++)
1271 READ_OFFSET (stream, (*set)[i].offset);
1272 if (! (*set)[i].offset)
1273 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1275 SAVE_STREAM (stream, state);
1276 for (i = 0; i < count; i++)
1278 SEEK_STREAM (stream, offset + (*set)[i].offset);
1279 (*set)[i].ChainRuleCount
1280 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1281 &(*set)[i].ChainRule);
1282 if (! (*set)[i].ChainRuleCount)
1285 RESTORE_STREAM (stream, state);
1290 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1291 OTF_ChainClassRule **rule)
1293 char *errfmt = "ChainClassRule%s";
1294 unsigned errret = 0;
1298 READ_UINT16 (stream, count);
1300 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1301 OTF_MALLOC (*rule, count, "");
1302 for (i = 0; i < count; i++)
1304 READ_OFFSET (stream, (*rule)[i].offset);
1305 if (! (*rule)[i].offset)
1306 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1308 for (i = 0; i < count; i++)
1310 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1311 (*rule)[i].BacktrackGlyphCount
1312 = read_glyph_ids (otf, stream,
1313 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1314 (*rule)[i].InputGlyphCount
1315 = read_glyph_ids (otf, stream,
1316 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1317 if (! (*rule)[i].InputGlyphCount)
1318 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1319 (*rule)[i].LookaheadGlyphCount
1320 = read_glyph_ids (otf, stream,
1321 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1322 (*rule)[i].LookupCount
1323 = read_lookup_record_list (otf, stream,
1324 &(*rule)[i].LookupRecord, -1);
1325 if (! (*rule)[i].LookupCount)
1332 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1333 OTF_ChainClassSet **set)
1335 char *errfmt = "ChainClassSet%s";
1336 unsigned errret = 0;
1337 OTF_StreamState state;
1341 READ_UINT16 (stream, count);
1343 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1344 OTF_MALLOC (*set, count, "");
1345 for (i = 0; i < count; i++)
1346 /* Offset may be zero. */
1347 READ_OFFSET (stream, (*set)[i].offset);
1348 SAVE_STREAM (stream, state);
1349 for (i = 0; i < count; i++)
1350 if ((*set)[i].offset)
1352 SEEK_STREAM (stream, offset + (*set)[i].offset);
1353 (*set)[i].ChainClassRuleCnt
1354 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1355 &(*set)[i].ChainClassRule);
1356 if (! (*set)[i].ChainClassRuleCnt)
1359 RESTORE_STREAM (stream, state);
1364 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1365 OTF_Coverage *coverage,OTF_Context1 *context1)
1367 if (read_coverage (otf, stream, offset, coverage) < 0)
1369 context1->RuleSetCount
1370 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1371 if (! context1->RuleSetCount)
1377 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1378 OTF_Coverage *coverage,OTF_Context2 *context2)
1380 if (read_coverage (otf, stream, offset, coverage) < 0
1381 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1383 context2->ClassSetCnt
1384 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1385 if (! context2->ClassSetCnt)
1391 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1392 OTF_Coverage *coverage,OTF_Context3 *context3)
1394 char *errfmt = "Context1%s";
1397 READ_USHORT (stream, context3->GlyphCount);
1398 if (context3->GlyphCount < 0)
1399 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1400 READ_USHORT (stream, context3->LookupCount);
1401 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1402 context3->GlyphCount) < 0)
1404 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1405 context3->LookupCount) < 0)
1411 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1412 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1414 if (read_coverage (otf, stream, offset, coverage) < 0)
1416 chain_context1->ChainRuleSetCount
1417 = read_chain_rule_set_list (otf, stream, offset,
1418 &chain_context1->ChainRuleSet);
1419 if (! chain_context1->ChainRuleSetCount)
1425 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1426 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1428 if (read_coverage (otf, stream, offset, coverage) < 0
1429 || read_class_def (otf, stream, offset,
1430 &chain_context2->BacktrackClassDef) < 0
1431 || read_class_def (otf, stream, offset,
1432 &chain_context2->InputClassDef) < 0
1433 || read_class_def (otf, stream, offset,
1434 &chain_context2->LookaheadClassDef) < 0)
1436 chain_context2->ChainClassSetCnt
1437 = read_chain_class_set_list (otf, stream, offset,
1438 &chain_context2->ChainClassSet);
1439 if (! chain_context2->ChainClassSetCnt)
1445 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1446 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1450 count = read_coverage_list (otf, stream, offset,
1451 &chain_context3->Backtrack, -1);
1454 chain_context3->BacktrackGlyphCount = (unsigned) count;
1455 count = read_coverage_list (otf, stream, offset,
1456 &chain_context3->Input, -1);
1459 chain_context3->InputGlyphCount = (unsigned) count;
1460 *coverage = chain_context3->Input[0];
1461 count = read_coverage_list (otf, stream, offset,
1462 &chain_context3->LookAhead, -1);
1463 chain_context3->LookaheadGlyphCount = (unsigned) count;
1464 chain_context3->LookupCount
1465 = read_lookup_record_list (otf, stream,
1466 &chain_context3->LookupRecord, -1);
1471 read_gsub_gpos_table (OTF *otf, OTF_Stream *stream, int gsubp)
1473 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1474 void *errret = NULL;
1475 OTF_GSUB_GPOS *gsub_gpos;
1477 OTF_CALLOC (gsub_gpos, 1, "");
1478 READ_FIXED (stream, gsub_gpos->Version);
1479 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1480 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1481 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1483 if (read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1484 &gsub_gpos->ScriptList) < 0
1485 || read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1486 &gsub_gpos->FeatureList) < 0
1487 || read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1488 &gsub_gpos->LookupList, gsubp) < 0)
1494 /* (1-9) "GSUB" table */
1497 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1499 char *errfmt = "Sequence%s";
1500 unsigned errret = 0;
1504 READ_UINT16 (stream, count);
1506 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1507 OTF_MALLOC (*seq, count, "");
1508 for (i = 0; i < count; i++)
1509 READ_OFFSET (stream, (*seq)[i].offset);
1510 for (i = 0; i < count; i++)
1512 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1513 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1514 &(*seq)[i].Substitute, 0, -1);
1515 if (! (*seq)[i].GlyphCount)
1522 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1523 OTF_Ligature **ligature)
1525 char *errfmt = "Ligature%s";
1530 READ_UINT16 (stream, count);
1533 OTF_MALLOC (*ligature, count, "");
1534 for (i = 0; i < count; i++)
1535 READ_OFFSET (stream, (*ligature)[i].offset);
1536 for (i = 0; i < count; i++)
1538 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1539 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1540 (*ligature)[i].CompCount
1541 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1542 if (! (*ligature)[i].CompCount)
1549 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1550 OTF_LigatureSet **ligset)
1552 char *errfmt = "LigatureSet%s";
1557 READ_UINT16 (stream, count);
1560 OTF_MALLOC (*ligset, count, "");
1561 for (i = 0; i < count; i++)
1562 READ_OFFSET (stream, (*ligset)[i].offset);
1563 for (i = 0; i < count; i++)
1567 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1568 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1569 &(*ligset)[i].Ligature);
1572 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1578 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1579 OTF_AlternateSet **altset)
1581 char *errfmt = "AlternateSet%s";
1586 READ_UINT16 (stream, count);
1588 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1589 OTF_MALLOC (*altset, count, "");
1590 for (i = 0; i < count; i++)
1591 READ_OFFSET (stream, (*altset)[i].offset);
1592 for (i = 0; i < count; i++)
1596 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1597 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1600 (*altset)[i].GlyphCount = (unsigned) alt_count;
1606 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1607 unsigned type, OTF_LookupSubTableGSUB *subtable)
1612 SEEK_STREAM (stream, offset);
1613 READ_UINT16 (stream, subtable->Format);
1614 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1618 if (subtable->Format == 1)
1620 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1622 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1624 else if (subtable->Format == 2)
1626 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1628 subtable->u.single2.GlyphCount
1629 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1631 if (! subtable->u.single2.GlyphCount)
1635 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1639 if (subtable->Format == 1)
1641 read_coverage (otf, stream, offset, &subtable->Coverage);
1642 subtable->u.multiple1.SequenceCount
1643 = read_sequence (otf, stream, offset,
1644 &subtable->u.multiple1.Sequence);
1647 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1651 if (subtable->Format == 1)
1653 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1655 subtable->u.alternate1.AlternateSetCount
1656 = read_alternate_set_list (otf, stream, offset,
1657 &subtable->u.alternate1.AlternateSet);
1658 if (! subtable->u.alternate1.AlternateSetCount)
1662 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1666 if (subtable->Format == 1)
1668 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1670 subtable->u.ligature1.LigSetCount
1671 = read_ligature_set_list (otf, stream, offset,
1672 &subtable->u.ligature1.LigatureSet);
1673 if (! subtable->u.ligature1.LigSetCount)
1677 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1681 if (subtable->Format == 1)
1683 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1684 &subtable->u.context1) < 0)
1687 else if (subtable->Format == 2)
1689 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1690 &subtable->u.context2) < 0)
1693 else if (subtable->Format == 3)
1695 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1696 &subtable->u.context3) < 0)
1700 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1704 if (subtable->Format == 1)
1706 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1707 &subtable->u.chain_context1) < 0)
1710 else if (subtable->Format == 2)
1712 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1713 &subtable->u.chain_context2) < 0)
1716 else if (subtable->Format == 3)
1718 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1719 &subtable->u.chain_context3) < 0)
1723 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1728 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1732 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1738 read_gsub_table (OTF *otf, OTF_Stream *stream)
1740 return read_gsub_gpos_table (otf, stream, 1);
1744 /* (1-10) "GPOS" table */
1747 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1748 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1751 OTF_StreamState state;
1756 for (i = 0, size = 0; i < 8; i++)
1760 if (bit & OTF_XPlacement)
1761 READ_INT16 (stream, value_record->XPlacement);
1762 if (bit & OTF_XPlacement)
1763 READ_INT16 (stream, value_record->YPlacement);
1764 if (bit & OTF_XAdvance)
1765 READ_INT16 (stream, value_record->XAdvance);
1766 if (bit & OTF_YAdvance)
1767 READ_INT16 (stream, value_record->YAdvance);
1768 if (bit & OTF_XPlaDevice)
1769 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1770 if (bit & OTF_YPlaDevice)
1771 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1772 if (bit & OTF_XAdvDevice)
1773 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1774 if (bit & OTF_YAdvDevice)
1775 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1776 SAVE_STREAM (stream, state);
1777 if (value_record->XPlaDevice.offset)
1779 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1782 if (value_record->YPlaDevice.offset)
1784 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1787 if (value_record->XAdvDevice.offset)
1789 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1792 if (value_record->YAdvDevice.offset)
1794 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1797 RESTORE_STREAM (stream, state);
1803 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1805 char *errfmt = "Anchor%s";
1808 SEEK_STREAM (stream, offset + anchor->offset);
1809 READ_UINT16 (stream, anchor->AnchorFormat);
1810 READ_INT16 (stream, anchor->XCoordinate);
1811 READ_INT16 (stream, anchor->YCoordinate);
1812 if (anchor->AnchorFormat == 1)
1814 else if (anchor->AnchorFormat == 2)
1816 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1818 else if (anchor->AnchorFormat == 3)
1820 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1821 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1822 if (anchor->f.f2.XDeviceTable.offset)
1824 if (read_device_table (otf, stream, offset + anchor->offset,
1825 &anchor->f.f2.XDeviceTable) < 0)
1828 if (anchor->f.f2.YDeviceTable.offset)
1830 if (read_device_table (otf, stream, offset + anchor->offset,
1831 &anchor->f.f2.YDeviceTable) < 0)
1836 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1842 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1843 OTF_MarkArray *array)
1845 char *errfmt = "MarkArray%s";
1847 OTF_StreamState state;
1850 READ_OFFSET (stream, array->offset);
1851 SAVE_STREAM (stream, state);
1852 SEEK_STREAM (stream, offset + array->offset);
1853 READ_UINT16 (stream, array->MarkCount);
1854 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1855 for (i = 0; i < array->MarkCount; i++)
1857 READ_UINT16 (stream, array->MarkRecord[i].Class);
1858 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1860 for (i = 0; i < array->MarkCount; i++)
1861 if (read_anchor (otf, stream, offset + array->offset,
1862 &array->MarkRecord[i].MarkAnchor) < 0)
1864 RESTORE_STREAM (stream, state);
1869 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1870 unsigned ClassCount, OTF_AnchorArray *array)
1872 char *errfmt = "AnchorArray%s";
1874 OTF_StreamState state;
1877 READ_OFFSET (stream, array->offset);
1878 SAVE_STREAM (stream, state);
1879 SEEK_STREAM (stream, offset + array->offset);
1880 READ_UINT16 (stream, array->Count);
1881 OTF_MALLOC (array->AnchorRecord, array->Count, "");
1882 for (i = 0; i < array->Count; i++)
1884 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
1886 for (j = 0; j < ClassCount; j++)
1887 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
1889 for (i = 0; i < array->Count; i++)
1890 for (j = 0; j < ClassCount; j++)
1891 if (read_anchor (otf, stream, offset + array->offset,
1892 &array->AnchorRecord[i].Anchor[j]) < 0)
1894 RESTORE_STREAM (stream, state);
1899 static OTF_Class1Record *
1900 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1901 unsigned num1, enum OTF_ValueFormat bit1,
1902 unsigned num2, enum OTF_ValueFormat bit2)
1904 char *errfmt = "Class1Record%s";
1905 void *errret = NULL;
1906 OTF_Class1Record *rec;
1909 OTF_MALLOC (rec, num1, "");
1910 for (i = 0; i < num1; i++)
1912 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1913 for (j = 0; j < num2; j++)
1915 if (read_value_record (otf, stream, offset,
1916 bit1, &rec[i].Class2Record[j].Value1) < 0
1917 || read_value_record (otf, stream, offset,
1918 bit2, &rec[i].Class2Record[j].Value2) < 0)
1926 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1927 long offset, unsigned type,
1928 OTF_LookupSubTableGPOS *subtable)
1933 SEEK_STREAM (stream, offset);
1934 READ_UINT16 (stream, subtable->Format);
1935 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
1939 if (subtable->Format == 1)
1941 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
1942 read_value_record (otf, stream, offset,
1943 subtable->u.single1.ValueFormat,
1944 &subtable->u.single1.Value);
1946 else if (subtable->Format == 2)
1948 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
1951 READ_UINT16 (stream, single2->ValueFormat);
1952 READ_UINT16 (stream, single2->ValueCount);
1953 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
1954 for (i = 0; i < single2->ValueCount; i++)
1955 read_value_record (otf, stream, offset, single2->ValueFormat,
1956 single2->Value + i);
1959 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1963 if (subtable->Format == 1)
1965 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1967 else if (subtable->Format == 2)
1969 SEEK_STREAM (stream, offset + 2);
1970 read_coverage (otf, stream, offset, &subtable->Coverage);
1971 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
1972 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
1973 read_class_def (otf, stream, offset,
1974 &subtable->u.pair2.ClassDef1);
1975 read_class_def (otf, stream, offset,
1976 &subtable->u.pair2.ClassDef2);
1977 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
1978 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
1979 subtable->u.pair2.Class1Record
1980 = read_class1_record_list (otf, stream, offset,
1981 subtable->u.pair2.Class1Count,
1982 subtable->u.pair2.ValueFormat1,
1983 subtable->u.pair2.Class2Count,
1984 subtable->u.pair2.ValueFormat2);
1987 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1991 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1995 if (subtable->Format == 1)
1997 read_coverage (otf, stream, offset, &subtable->Coverage);
1998 read_coverage (otf, stream, offset,
1999 &subtable->u.mark_base1.BaseCoverage);
2000 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2001 read_mark_array (otf, stream, offset,
2002 &subtable->u.mark_base1.MarkArray);
2003 read_anchor_array (otf, stream, offset,
2004 subtable->u.mark_base1.ClassCount,
2005 &subtable->u.mark_base1.BaseArray);
2008 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2012 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
2016 if (subtable->Format == 1)
2018 read_coverage (otf, stream, offset, &subtable->Coverage);
2019 read_coverage (otf, stream, offset,
2020 &subtable->u.mark_mark1.Mark2Coverage);
2021 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2022 read_mark_array (otf, stream, offset,
2023 &subtable->u.mark_mark1.Mark1Array);
2024 read_anchor_array (otf, stream, offset,
2025 subtable->u.mark_mark1.ClassCount,
2026 &subtable->u.mark_mark1.Mark2Array);
2029 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2033 if (subtable->Format == 1)
2035 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2036 &subtable->u.context1) < 0)
2039 else if (subtable->Format == 2)
2041 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2042 &subtable->u.context2) < 0)
2045 else if (subtable->Format == 3)
2047 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2048 &subtable->u.context3) < 0)
2052 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2056 if (subtable->Format == 1)
2058 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2059 &subtable->u.chain_context1) < 0)
2062 else if (subtable->Format == 2)
2064 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2065 &subtable->u.chain_context2) < 0)
2068 else if (subtable->Format == 3)
2070 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2071 &subtable->u.chain_context3) < 0)
2075 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2079 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
2083 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2089 read_gpos_table (OTF *otf, OTF_Stream *stream)
2091 return read_gsub_gpos_table (otf, stream, 0);
2099 read_base_table (OTF_Stream *stream, long offset)
2103 OTF_MALLOC (base, 1);
2112 read_jstf_table (OTF_Stream *stream, long offset)
2116 OTF_MALLOC (jstf, 1);
2122 /*** (1-11) Structure for OTF */
2125 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2129 READ_FIXED (stream, table->sfnt_version);
2130 READ_USHORT (stream, table->numTables);
2131 READ_USHORT (stream, table->searchRange);
2132 READ_USHORT (stream, table->enterSelector);
2133 READ_USHORT (stream, table->rangeShift);
2138 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2143 READ_TAG (stream, tag);
2145 table->name[0] = tag >> 24;
2146 table->name[1] = (tag >> 16) & 0xFF;
2147 table->name[0] = (tag >> 8) & 0xFF;
2148 table->name[0] = tag >> 8;
2149 table->name[0] = '\0';
2150 READ_ULONG (stream, table->checkSum);
2151 READ_ULONG (stream, table->offset);
2152 READ_ULONG (stream, table->length);
2157 read_header_part (OTF *otf, FILE *fp)
2159 char *errfmt = "otf header%s";
2161 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2164 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2166 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2167 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2168 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2169 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2170 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2171 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2172 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2173 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2174 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2175 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2176 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2177 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2179 head_tag = OTF_tag ("head");
2180 name_tag = OTF_tag ("name");
2181 cmap_tag = OTF_tag ("cmap");
2182 gdef_tag = OTF_tag ("GDEF");
2183 gsub_tag = OTF_tag ("GSUB");
2184 gpos_tag = OTF_tag ("GPOS");
2186 stream = make_stream ();
2190 internal_data->header_stream = stream;
2192 /* Size of Offset Table is 12 bytes. */
2193 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2195 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2198 /* Size of each Table Directory is 16 bytes. */
2199 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2200 "Table Directory") < 0)
2203 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2204 for (i = 0; i < otf->offset_table.numTables; i++)
2206 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2207 OTF_TableInfo *table_info = NULL;
2211 if (tag == head_tag)
2212 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2213 else if (tag == name_tag)
2214 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2215 else if (tag == cmap_tag)
2216 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2217 else if (tag == gdef_tag)
2218 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2219 else if (tag == gsub_tag)
2220 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2221 else if (tag == gpos_tag)
2222 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2226 table_info->stream = make_stream ();
2227 if (setup_stream (table_info->stream, fp,
2228 otf->table_dirs[i].offset,
2229 otf->table_dirs[i].length,
2230 otf->table_dirs[i].name) < 0)
2235 internal_data->header_stream = NULL;
2236 free_stream (stream);
2241 /*** (2) API for reading OTF */
2243 /*** (2-1) OTF_open() */
2245 /* Note: We can't use memory allocation macros in the following
2246 functions because those macros return from the functions before
2247 freeing memory previously allocated. */
2250 OTF_open (char *otf_name)
2253 char *errfmt = "opening otf (%s)";
2254 void *errret = NULL;
2256 OTF_InternalData *internal_data;
2258 fp = fopen (otf_name, "r");
2260 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2261 otf = calloc (1, sizeof (OTF));
2263 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2264 otf->filename = strdup (otf_name);
2265 if (! otf->filename)
2269 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2272 internal_data = calloc (1, sizeof (OTF_InternalData));
2273 if (! internal_data)
2274 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2275 otf->internal_data = internal_data;
2276 if (! allocate_memory_record (otf))
2277 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2279 /* Here after, all pointers to allocated memory are recorded in
2280 otf->internal_data->memory_record except for what allocated by
2281 the functions allocate_memory_record and make_stream. */
2283 if (read_header_part (otf, fp) < 0)
2294 /*** (2-2) OTF_close() */
2297 OTF_close (OTF *otf)
2299 OTF_InternalData *internal_data = otf->internal_data;
2304 OTF_MemoryRecord *memrec = internal_data->memory_record;
2306 if (internal_data->header_stream)
2307 free_stream (internal_data->header_stream);
2309 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2310 if (internal_data->table_info[i].stream)
2311 free_stream (internal_data->table_info[i].stream);
2315 OTF_MemoryRecord *next = memrec->next;
2317 for (i = memrec->used - 1; i >= 0; i--)
2318 free (memrec->memory[i]);
2322 free (internal_data);
2327 /*** (2-3) OTF_get_table() */
2330 OTF_get_table (OTF *otf, char *name)
2332 char *errfmt = "OTF Table Read%s";
2334 OTF_InternalData *internal_data = otf->internal_data;
2335 OTF_TableInfo *table_info;
2336 OTF_Tag tag = OTF_tag (name);
2339 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2341 if (tag == OTF_tag ("head"))
2342 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2343 else if (tag == OTF_tag ("name"))
2344 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2345 else if (tag == OTF_tag ("cmap"))
2346 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2347 else if (tag == OTF_tag ("GDEF"))
2348 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2349 else if (tag == OTF_tag ("GSUB"))
2350 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2351 else if (tag == OTF_tag ("GPOS"))
2352 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2354 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported table name)");
2356 if (*table_info->address)
2359 if (! table_info->stream)
2360 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2361 if (! table_info->reader)
2362 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2364 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2365 free_stream (table_info->stream);
2366 table_info->stream = NULL;
2367 if (! *table_info->address)
2369 table_info->reader = NULL;
2377 /*** (5) API miscellaneous ***/
2380 OTF_tag (char *name)
2382 unsigned char *p = (unsigned char *) name;
2386 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2390 OTF_tag_name (OTF_Tag tag, char *name)
2392 name[0] = (char) (tag >> 24);
2393 name[1] = (char) ((tag >> 16) & 0xFF);
2394 name[2] = (char) ((tag >> 8) & 0xFF);
2395 name[3] = (char) (tag & 0xFF);