1 /* otfopen.c -- OpenType font reader.
3 Copyright (C) 2003, 2004
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H15PRO167
7 This file is part of libotf.
9 Libotf is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 Libotf is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library, in a file named COPYING; if not,
21 write to the Free Software Foundation, Inc., 59 Temple Place, Suite
22 330, Boston, MA 02111-1307, USA. */
33 Table of contents (almost parallel to otf.h):
37 (1) Readers for OTF Layout tables and OTF itself
38 (1-1) Basic types and functions
42 (1-5) Structures common to GDEF, GSUB, and GPOS
44 (1-7) Structures for ScriptList, FeatureList, and LookupList
45 (1-8) Structures common to GSUB and GPOS
48 (1-11) Structure for OTF
50 (2) API for reading OTF
54 (2-4) OTF_check_table()
61 Example of typical usage of OTF_Stream.
65 OTF_StreamState state;
68 OPEN_STREAM (_FILE_NAME_, stream);
71 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
72 offset = READ_OFFSET (stream);
73 nbytes = READ_ULONG (stream);
74 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
76 CLOSE_STREAM (stream);
91 typedef long OTF_StreamState;
97 char *errfmt = "stream creation%s";
100 stream = calloc (1, sizeof (OTF_Stream));
102 OTF_ERROR (OTF_ERROR_MEMORY, "");
107 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
110 char *errfmt = "stream setup for %s";
115 if (stream->allocated < nbytes)
117 unsigned char *buf = malloc (nbytes);
120 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
124 stream->allocated = nbytes;
126 stream->bufsize = nbytes;
127 if (fseek (fp, offset, SEEK_SET) < 0)
128 OTF_ERROR (OTF_ERROR_FILE, stream->name);
129 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
130 OTF_ERROR (OTF_ERROR_FILE, stream->name);
136 free_stream (OTF_Stream *stream)
142 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
143 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
144 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
146 #define STREAM_CHECK_SIZE(stream, size) \
147 if ((stream)->pos + (size) > (stream)->bufsize) \
149 char *errfmt = "buffer overrun in %s"; \
151 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
157 #define READ_USHORT(stream, var) \
159 STREAM_CHECK_SIZE ((stream), 2); \
160 (var) = (((stream)->buf[(stream)->pos] << 8) \
161 | (stream)->buf[(stream)->pos + 1]); \
162 (stream)->pos += 2; \
165 #define READ_SHORT(stream, var) \
167 STREAM_CHECK_SIZE ((stream), 2); \
168 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
169 | (stream)->buf[(stream)->pos + 1]); \
170 (stream)->pos += 2; \
173 #define READ_ULONG(stream, var) \
175 STREAM_CHECK_SIZE ((stream), 4); \
176 (var) = (((stream)->buf[(stream)->pos] << 24) \
177 | ((stream)->buf[(stream)->pos + 1] << 16) \
178 | ((stream)->buf[(stream)->pos + 2] << 8) \
179 | (stream)->buf[(stream)->pos + 3]); \
180 (stream)->pos += 4; \
183 #define READ_LONG(stream, var) \
185 STREAM_CHECK_SIZE ((stream), 4); \
186 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
187 | ((stream)->buf[(stream)->pos + 1] << 16) \
188 | ((stream)->buf[(stream)->pos + 2] << 8) \
189 | (stream)->buf[(stream)->pos + 3]); \
190 (stream)->pos += 4; \
194 #define READ_FIXED(stream, fixed) \
196 READ_USHORT ((stream), (fixed).high); \
197 READ_USHORT ((stream), (fixed).low); \
201 #define READ_BYTES(stream, p, nbytes) \
203 STREAM_CHECK_SIZE ((stream), (nbytes)); \
204 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
205 (stream)->pos += (nbytes); \
209 #define READ_TAG READ_ULONG
210 #define READ_OFFSET READ_USHORT
211 #define READ_UINT16 READ_USHORT
212 #define READ_INT16 READ_SHORT
213 #define READ_GLYPHID READ_USHORT
216 /*** (1) Structures for OTF Layout tables and OTF itself */
218 /*** (1-1) Basic types and functions */
231 #define OTF_MEMORY_RECORD_SIZE 1024
233 struct OTF_MemoryRecord
236 void *memory[OTF_MEMORY_RECORD_SIZE];
237 struct OTF_MemoryRecord *next;
240 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
244 /* Points to one of OTF->head, OTF->name, etc. */
246 /* Function to read one of OTF tables. */
247 void *(*reader) (OTF *otf, OTF_Stream *stream);
248 /* Stream given to <reader>. */
252 struct OTF_InternalData
254 /* Information about each OTF table. */
255 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
257 /* Stream used to read the header part of OTF. */
258 OTF_Stream *header_stream;
260 /* Records of allocated memories. */
261 OTF_MemoryRecord *memory_record;
265 static OTF_MemoryRecord *
266 allocate_memory_record (OTF *otf)
268 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
269 OTF_MemoryRecord *memrec = malloc (sizeof (OTF_MemoryRecord));
274 memrec->next = internal_data->memory_record;
275 internal_data->memory_record = memrec;
279 /* Memory allocation macros. */
281 #define OTF_MALLOC(p, size, arg) \
287 OTF_MemoryRecord *memrec \
288 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
289 (p) = malloc (sizeof (*(p)) * (size)); \
291 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
292 && ! (memrec = allocate_memory_record (otf)))) \
293 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
294 memrec->memory[memrec->used++] = (p); \
299 #define OTF_CALLOC(p, size, arg) \
305 OTF_MemoryRecord *memrec \
306 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
307 (p) = calloc ((size), sizeof (*(p))); \
309 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
310 && ! (memrec = allocate_memory_record (otf)))) \
311 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
312 memrec->memory[memrec->used++] = (p); \
317 /*** (1-2) "head" table */
320 read_head_table (OTF *otf, OTF_Stream *stream)
322 char *errfmt = "head%s";
326 OTF_CALLOC (head, 1, "");
327 READ_FIXED (stream, head->TableVersionNumber);
328 READ_FIXED (stream, head->fontRevision);
329 READ_ULONG (stream, head->checkSumAdjustment);
330 READ_ULONG (stream, head->magicNumber);
331 READ_USHORT (stream, head->flags);
332 READ_USHORT (stream, head->unitsPerEm);
338 /*** (1-3) "name" table */
341 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
343 char *errfmt = "nameID (%d)";
345 OTF_StreamState state;
350 if (rec->platformID == 0)
351 ucs = (rec->encodingID <= 3) ? 2 : 4;
352 else if (rec->platformID == 1 && rec->encodingID == 0)
354 else if (rec->platformID == 3)
355 ucs = (rec->encodingID == 1 ? 2
356 : rec->encodingID == 10 ? 4
359 OTF_MALLOC (rec->name, rec->length + 1, (void *) rec->nameID);
360 SAVE_STREAM (stream, state);
361 SEEK_STREAM (stream, stream->pos + rec->offset);
362 READ_BYTES (stream, rec->name, rec->length);
363 RESTORE_STREAM (stream, state);
364 rec->name[rec->length] = 0;
373 for (i = 0; i < rec->length / 2; i++)
375 if (rec->name[i * 2] > 0
376 || rec->name[i * 2 + 1] >= 128)
383 for (i = 0; i < rec->length / 2; i++)
384 rec->name[i] = rec->name[i * 2 + 1];
390 for (i = 0; i < rec->length / 4; i++)
392 if (rec->name[i * 4] > 0
393 || rec->name[i * 4 + 1] > 0
394 || rec->name[i * 4 + 2] > 0
395 || rec->name[i * 2 + 3] >= 128)
402 for (i = 0; i < rec->length / 4; i++)
403 rec->name[i] = rec->name[i * 4 + 3];
411 read_name_table (OTF *otf, OTF_Stream *stream)
413 char *errfmt = "name%s";
418 OTF_CALLOC (name, 1, "");
419 READ_USHORT (stream, name->format);
420 READ_USHORT (stream, name->count);
421 READ_USHORT (stream, name->stringOffset);
422 OTF_MALLOC (name->nameRecord, name->count, "");
423 for (i = 0; i < name->count; i++)
425 OTF_NameRecord *rec = name->nameRecord + i;
427 READ_USHORT (stream, rec->platformID);
428 READ_USHORT (stream, rec->encodingID);
429 READ_USHORT (stream, rec->languageID);
430 READ_USHORT (stream, rec->nameID);
431 READ_USHORT (stream, rec->length);
432 READ_USHORT (stream, rec->offset);
434 for (i = 0; i < name->count; i++)
436 OTF_NameRecord *rec = name->nameRecord + i;
437 int nameID = rec->nameID;
439 read_name (otf, stream, rec);
441 if (nameID >= OTF_max_nameID)
443 if (! name->name[nameID]
445 name->name[nameID] = (char *) rec->name;
452 /*** (1-4) "cmap" table */
455 read_cmap_table (OTF *otf, OTF_Stream *stream)
457 char *errfmt = "cmap%s";
460 int unicode_index = -1;
463 OTF_CALLOC (cmap, 1, "");
464 READ_USHORT (stream, cmap->version);
465 READ_USHORT (stream, cmap->numTables);
466 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
467 for (i = 0; i < cmap->numTables; i++)
469 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
470 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
471 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
472 if (cmap->EncodingRecord[i].platformID == 3
473 && cmap->EncodingRecord[i].encodingID == 1)
476 for (i = 0; i < cmap->numTables; i++)
480 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
481 READ_USHORT (stream, format);
482 cmap->EncodingRecord[i].subtable.format = format;
483 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
484 if (format == 8 || format == 10 || format == 12)
486 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
487 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
491 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
497 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
498 " (EncodingRecord)");
500 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
510 OTF_EncodingSubtable4 *sub4;
515 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
516 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
517 READ_USHORT (stream, sub4->segCountX2);
518 segCount = sub4->segCountX2 / 2;
519 READ_USHORT (stream, sub4->searchRange);
520 READ_USHORT (stream, sub4->entrySelector);
521 READ_USHORT (stream, sub4->rangeShift);
522 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
523 for (j = 0; j < segCount; j++)
524 READ_USHORT (stream, sub4->segments[j].endCount);
525 READ_USHORT (stream, dummy);
526 for (j = 0; j < segCount; j++)
527 READ_USHORT (stream, sub4->segments[j].startCount);
528 for (j = 0; j < segCount; j++)
529 READ_SHORT (stream, sub4->segments[j].idDelta);
530 for (j = 0; j < segCount; j++)
533 unsigned rest = 2 * (segCount - j);
535 READ_USHORT (stream, off);
537 sub4->segments[j].idRangeOffset = 0xFFFF;
539 sub4->segments[j].idRangeOffset = (off - rest) / 2;
541 j = (cmap->EncodingRecord[i].subtable.length
542 - (14 + 2 * (segCount * 4 + 1)));
543 sub4->GlyphCount = j / 2;
544 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
545 for (j = 0; j < sub4->GlyphCount; j++)
546 READ_USHORT (stream, sub4->glyphIdArray[j]);
552 OTF_EncodingSubtable6 *sub6;
555 OTF_MALLOC (sub6, 1, " (EncodingSubtable6)");
556 cmap->EncodingRecord[i].subtable.f.f6 = sub6;
557 READ_USHORT (stream, sub6->firstCode);
558 READ_USHORT (stream, sub6->entryCount);
559 OTF_MALLOC (sub6->glyphIdArray, sub6->entryCount, " (GlyphCount)");
560 for (j = 0; j < sub6->entryCount; j++)
561 READ_USHORT (stream, sub6->glyphIdArray[j]);
566 if (unicode_index >= 0)
568 OTF_EncodingRecord *rec = cmap->EncodingRecord + unicode_index;
569 OTF_GlyphID glyph_id, max_glyph_id = 0;
571 OTF_CALLOC (cmap->unicode_table, 0x10000, "");
572 switch (rec->subtable.format)
576 OTF_EncodingSubtable4 *sub4 = rec->subtable.f.f4;
577 int segCount = sub4->segCountX2 / 2;
579 for (i = 0; i < segCount; i++)
581 OTF_cmapSegument *seg = sub4->segments + i;
584 if (seg->idRangeOffset == 0xFFFF)
585 for (c = seg->startCount; c <= seg->endCount; c++)
587 glyph_id = c + seg->idDelta;
588 cmap->unicode_table[c] = glyph_id;
589 if (glyph_id > max_glyph_id)
590 max_glyph_id = glyph_id;
593 for (c = seg->startCount; c <= seg->endCount && c != 0xFFFF;
596 glyph_id = sub4->glyphIdArray[seg->idRangeOffset
597 + (c - seg->startCount)];
598 cmap->unicode_table[c] = glyph_id;
599 if (glyph_id > max_glyph_id)
600 max_glyph_id = glyph_id;
606 OTF_CALLOC (cmap->decode_table, max_glyph_id + 1, "");
607 for (i = 0; i < 0x10000; i++)
608 if (cmap->unicode_table[i])
609 cmap->decode_table[cmap->unicode_table[i]] = i;
610 cmap->max_glyph_id = max_glyph_id;
617 /*** (1-5) Structures common to GDEF, GSUB, and GPOS */
619 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
620 Glyph-IDs there. If COUNT is negative, read the number of
621 Glyphs-IDs at first. MINUS if nozero is how few the actual
622 Glyph-IDs are in STREAM than COUNT. */
625 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
626 int minus, int count)
628 char *errfmt = "GlyphID List%s";
633 READ_UINT16 (stream, count);
636 OTF_MALLOC (*ids, count, "");
637 for (i = 0; i < count + minus; i++)
638 READ_GLYPHID (stream, (*ids)[i]);
643 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
645 char *errfmt = "RangeRecord%s";
650 READ_UINT16 (stream, count);
653 OTF_MALLOC (*record, count, "");
654 for (i = 0; i < count; i++)
656 READ_GLYPHID (stream, (*record)[i].Start);
657 READ_GLYPHID (stream, (*record)[i].End);
658 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
665 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
666 OTF_Coverage *coverage)
668 char *errfmt = "Coverage%s";
670 OTF_StreamState state;
673 READ_OFFSET (stream, coverage->offset);
674 SAVE_STREAM (stream, state);
675 SEEK_STREAM (stream, offset + coverage->offset);
676 READ_UINT16 (stream, coverage->CoverageFormat);
677 if (coverage->CoverageFormat == 1)
678 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
679 else if (coverage->CoverageFormat == 2)
680 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
682 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
685 coverage->Count = (unsigned) count;
686 RESTORE_STREAM (stream, state);
690 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
691 and store the Coverages there. If COUNT is negative, read the
692 number of Coverages at first. */
695 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
696 OTF_Coverage **coverage, int count)
698 char *errfmt = "Coverage List%s";
703 READ_UINT16 (stream, count);
706 OTF_MALLOC (*coverage, count, "");
707 for (i = 0; i < count; i++)
708 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
715 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
718 char *errfmt = "ClassDef%s";
721 SEEK_STREAM (stream, class->offset);
722 READ_UINT16 (stream, class->ClassFormat);
723 if (class->ClassFormat == 1)
725 READ_GLYPHID (stream, class->f.f1.StartGlyph);
726 class->f.f1.GlyphCount
728 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
729 if (! class->f.f1.GlyphCount)
730 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
732 else if (class->ClassFormat == 2)
734 class->f.f2.ClassRangeCount
735 = (read_range_records
736 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
737 if (! class->f.f2.ClassRangeCount)
738 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
741 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
747 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
749 char *errfmt = "ClassDef%s";
751 OTF_StreamState state;
753 READ_OFFSET (stream, class->offset);
756 SAVE_STREAM (stream, state);
757 SEEK_STREAM (stream, offset + class->offset);
758 READ_UINT16 (stream, class->ClassFormat);
759 if (class->ClassFormat == 1)
761 READ_GLYPHID (stream, class->f.f1.StartGlyph);
762 class->f.f1.GlyphCount
763 = read_glyph_ids (otf, stream,
764 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
766 if (! class->f.f1.GlyphCount)
769 else if (class->ClassFormat == 2)
771 class->f.f2.ClassRangeCount
772 = read_range_records (otf, stream,
774 &class->f.f2.ClassRangeRecord);
775 if (! class->f.f2.ClassRangeCount)
779 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
781 RESTORE_STREAM (stream, state);
787 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
788 OTF_DeviceTable *table)
790 char *errfmt = "Device Table%s";
801 SEEK_STREAM (stream, offset + table->offset);
802 READ_UINT16 (stream, table->StartSize);
803 READ_UINT16 (stream, table->EndSize);
804 READ_UINT16 (stream, table->DeltaFormat);
805 num = table->EndSize - table->StartSize + 1;
806 OTF_MALLOC (table->DeltaValue, num, "");
808 if (table->DeltaFormat == 1)
809 for (i = 0; i < num; i++)
812 READ_UINT16 (stream, val);
813 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
814 table->DeltaValue[i] = intval.int2;
816 else if (table->DeltaFormat == 2)
817 for (i = 0; i < num; i++)
820 READ_UINT16 (stream, val);
821 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
822 table->DeltaValue[i] = intval.int4;
824 else if (table->DeltaFormat == 3)
825 for (i = 0; i < num; i++)
829 READ_UINT16 (stream, val);
830 intval.int8 = val >> 8;
831 table->DeltaValue[i] = intval.int8;
835 intval.int8 = val >> 8;
836 table->DeltaValue[i] = intval.int8;
840 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
845 /*** (1-6) "GDEF" table */
848 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
849 OTF_AttachList *list)
851 char *errfmt = "AttachList%s";
855 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
857 READ_UINT16 (stream, list->GlyphCount);
858 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
859 for (i = 0; i < list->GlyphCount; i++)
860 READ_OFFSET (stream, list->AttachPoint[i].offset);
861 for (i = 0; i < list->GlyphCount; i++)
865 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
866 READ_UINT16 (stream, count);
867 list->AttachPoint[i].PointCount = count;
868 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
869 for (j = 0; j < count; j++)
870 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
876 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
877 OTF_CaretValue *caret)
879 char *errfmt = "CaretValue%s";
882 SEEK_STREAM (stream, offset + caret->offset);
883 READ_UINT16 (stream, caret->CaretValueFormat);
884 if (caret->CaretValueFormat == 1)
885 READ_INT16 (stream, caret->f.f1.Coordinate);
886 else if (caret->CaretValueFormat == 2)
887 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
888 else if (caret->CaretValueFormat == 3)
890 READ_INT16 (stream, caret->f.f3.Coordinate);
891 if (read_device_table (otf, stream, offset + caret->offset,
892 &caret->f.f3.DeviceTable) < 0)
896 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
901 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
902 OTF_LigCaretList *list)
904 char *errfmt = "LigCaretList%s";
908 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
910 READ_UINT16 (stream, list->LigGlyphCount);
911 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
912 for (i = 0; i < list->LigGlyphCount; i++)
913 READ_OFFSET (stream, list->LigGlyph[i].offset);
914 for (i = 0; i < list->LigGlyphCount; i++)
918 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
919 READ_UINT16 (stream, count);
920 list->LigGlyph[i].CaretCount = count;
921 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
922 for (j = 0; j < count; j++)
923 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
924 for (j = 0; j < count; j++)
925 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
926 &list->LigGlyph[i].CaretValue[j]) < 0)
933 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
937 READ_FIXED (stream, header->Version);
938 READ_OFFSET (stream, header->GlyphClassDef);
939 READ_OFFSET (stream, header->AttachList);
940 READ_OFFSET (stream, header->LigCaretList);
941 READ_OFFSET (stream, header->MarkAttachClassDef);
946 read_gdef_table (OTF *otf, OTF_Stream *stream)
948 char *errfmt = "GDEF%s";
952 OTF_CALLOC (gdef, 1, "");
953 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
954 if (gdef->header.GlyphClassDef)
956 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
957 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
959 if (gdef->header.AttachList)
960 read_attach_list (otf, stream, gdef->header.AttachList,
962 if (gdef->header.LigCaretList)
963 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
964 &gdef->lig_caret_list);
965 if (gdef->header.MarkAttachClassDef)
967 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
968 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
975 /*** (1-7) Structures for ScriptList, FeatureList, and LookupList */
978 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
979 OTF_ScriptList *list)
981 char *errfmt = "Script List%s";
985 SEEK_STREAM (stream, offset);
986 READ_USHORT (stream, list->ScriptCount);
987 OTF_CALLOC (list->Script, list->ScriptCount, "");
989 for (i = 0; i < list->ScriptCount; i++)
991 READ_TAG (stream, list->Script[i].ScriptTag);
992 READ_OFFSET (stream, list->Script[i].offset);
994 for (i = 0; i < list->ScriptCount; i++)
996 OTF_Script *script = list->Script + i;
997 long script_offset = offset + script->offset;
999 SEEK_STREAM (stream, script_offset);
1000 READ_OFFSET (stream, script->DefaultLangSysOffset);
1001 READ_USHORT (stream, script->LangSysCount);
1002 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
1003 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
1004 for (j = 0; j < script->LangSysCount; j++)
1006 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
1007 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
1010 if (script->DefaultLangSysOffset)
1012 OTF_LangSys *langsys = &script->DefaultLangSys;
1014 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
1015 READ_OFFSET (stream, langsys->LookupOrder);
1016 READ_USHORT (stream, langsys->ReqFeatureIndex);
1017 READ_USHORT (stream, langsys->FeatureCount);
1018 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1020 for (k = 0; k < langsys->FeatureCount; k++)
1021 READ_USHORT (stream, langsys->FeatureIndex[k]);
1024 for (j = 0; j < script->LangSysCount; j++)
1026 OTF_LangSys *langsys = script->LangSys + j;
1028 SEEK_STREAM (stream,
1029 script_offset + script->LangSysRecord[j].LangSys);
1030 READ_OFFSET (stream, langsys->LookupOrder);
1031 READ_USHORT (stream, langsys->ReqFeatureIndex);
1032 READ_USHORT (stream, langsys->FeatureCount);
1033 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
1035 for (k = 0; k < langsys->FeatureCount; k++)
1036 READ_USHORT (stream, langsys->FeatureIndex[k]);
1044 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
1045 OTF_FeatureList *list)
1047 char *errfmt = "Feature List%s";
1051 READ_UINT16 (stream, list->FeatureCount);
1052 OTF_CALLOC (list->Feature, list->FeatureCount, "");
1053 for (i = 0; i < list->FeatureCount; i++)
1055 READ_TAG (stream, list->Feature[i].FeatureTag);
1056 READ_OFFSET (stream, list->Feature[i].offset);
1058 for (i = 0; i < list->FeatureCount; i++)
1060 OTF_Feature *feature = list->Feature + i;
1062 SEEK_STREAM (stream, offset + feature->offset);
1063 READ_OFFSET (stream, feature->FeatureParams);
1064 READ_UINT16 (stream, feature->LookupCount);
1065 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
1066 " (LookupListIndex)");
1067 for (j = 0; j < feature->LookupCount; j++)
1068 READ_UINT16 (stream, feature->LookupListIndex[j]);
1074 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
1075 long offset, unsigned type,
1076 OTF_LookupSubTableGSUB *subtable);
1077 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1078 long offset, unsigned type,
1079 OTF_LookupSubTableGPOS *subtable);
1082 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
1083 OTF_LookupList *list, int gsubp)
1085 char *errfmt = "Lookup List%s";
1089 SEEK_STREAM (stream, offset);
1090 READ_UINT16 (stream, list->LookupCount);
1091 OTF_CALLOC (list->Lookup, list->LookupCount, "");
1093 for (i = 0; i < list->LookupCount; i++)
1094 READ_OFFSET (stream, list->Lookup[i].offset);
1095 for (i = 0; i < list->LookupCount; i++)
1097 OTF_Lookup *lookup = list->Lookup + i;
1099 SEEK_STREAM (stream, offset + lookup->offset);
1100 READ_UINT16 (stream, lookup->LookupType);
1101 READ_UINT16 (stream, lookup->LookupFlag);
1102 READ_UINT16 (stream, lookup->SubTableCount);
1103 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
1104 " (SubTableOffset)");
1106 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
1109 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
1111 for (j = 0; j < lookup->SubTableCount; j++)
1112 READ_OFFSET (stream, lookup->SubTableOffset[j]);
1113 for (j = 0; j < lookup->SubTableCount; j++)
1116 = offset + lookup->offset + lookup->SubTableOffset[j];
1119 ? read_lookup_subtable_gsub (otf, stream, this_offset,
1121 lookup->SubTable.gsub + j) < 0
1122 : read_lookup_subtable_gpos (otf, stream, this_offset,
1124 lookup->SubTable.gpos + j) < 0)
1133 /*** (1-8) Structures common to GSUB and GPOS */
1136 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
1137 OTF_LookupRecord **record, int count)
1139 char *errfmt = "LookupRecord%s";
1140 unsigned errret = 0;
1144 READ_UINT16 (stream, count);
1146 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1147 OTF_MALLOC (*record, count, "");
1148 for (i = 0; i < count; i++)
1150 READ_UINT16 (stream, (*record)[i].SequenceIndex);
1151 READ_UINT16 (stream, (*record)[i].LookupListIndex);
1157 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
1159 char *errfmt = "List of Rule%s";
1160 unsigned errret = 0;
1161 OTF_StreamState state;
1165 READ_UINT16 (stream, count);
1167 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1168 OTF_MALLOC (*rule, count, "");
1169 for (i = 0; i < count; i++)
1171 READ_OFFSET (stream, (*rule)[i].offset);
1172 if (! (*rule)[i].offset)
1173 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1175 SAVE_STREAM (stream, state);
1176 for (i = 0; i < count; i++)
1178 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1179 READ_UINT16 (stream, (*rule)[i].GlyphCount);
1180 if ((*rule)[i].GlyphCount == 0)
1181 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1182 READ_UINT16 (stream, (*rule)[i].LookupCount);
1183 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
1184 (*rule)[i].GlyphCount) < 0)
1186 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1187 (*rule)[i].LookupCount) == 0)
1190 RESTORE_STREAM (stream, state);
1196 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1199 char *errfmt = "List of RuleSet%s";
1200 unsigned errret = 0;
1201 OTF_StreamState state;
1205 READ_UINT16 (stream, count);
1207 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1208 OTF_MALLOC (*set, count, "");
1209 for (i = 0; i < count; i++)
1211 READ_OFFSET (stream, (*set)[i].offset);
1212 if (! (*set)[i].offset)
1213 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1215 SAVE_STREAM (stream, state);
1216 for (i = 0; i < count; i++)
1218 SEEK_STREAM (stream, offset + (*set)[i].offset);
1220 = read_rule_list (otf, stream, offset + (*set)[i].offset,
1222 if (! (*set)[i].RuleCount)
1225 RESTORE_STREAM (stream, state);
1230 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1231 OTF_ClassRule **rule)
1233 char *errfmt = "ClassRule%s";
1234 unsigned errret = 0;
1235 OTF_StreamState state;
1239 READ_UINT16 (stream, count);
1241 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1242 OTF_MALLOC (*rule, count, "");
1243 for (i = 0; i < count; i++)
1245 READ_OFFSET (stream, (*rule)[i].offset);
1246 if (! (*rule)[i].offset)
1247 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1249 SAVE_STREAM (stream, state);
1250 for (i = 0; i < count; i++)
1252 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1253 READ_USHORT (stream, (*rule)[i].GlyphCount);
1254 if (! (*rule)[i].GlyphCount)
1255 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1256 READ_USHORT (stream, (*rule)[i].LookupCount);
1257 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
1258 0, (*rule)[i].GlyphCount - 1) < 0)
1260 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
1261 (*rule)[i].LookupCount) == 0)
1264 RESTORE_STREAM (stream, state);
1269 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1272 char *errfmt = "ClassSet%s";
1273 unsigned errret = 0;
1274 OTF_StreamState state;
1278 READ_UINT16 (stream, count);
1280 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1281 OTF_MALLOC (*set, count, "");
1282 for (i = 0; i < count; i++)
1283 /* Offset can be zero. */
1284 READ_OFFSET (stream, (*set)[i].offset);
1285 SAVE_STREAM (stream, state);
1286 for (i = 0; i < count; i++)
1287 if ((*set)[i].offset)
1289 SEEK_STREAM (stream, offset + (*set)[i].offset);
1290 (*set)[i].ClassRuleCnt
1291 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
1292 &(*set)[i].ClassRule);
1293 if (! (*set)[i].ClassRuleCnt)
1296 RESTORE_STREAM (stream, state);
1301 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1302 OTF_ChainRule **rule)
1304 char *errfmt = "ChainRule%s";
1305 unsigned errret = 0;
1309 READ_UINT16 (stream, count);
1311 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1312 OTF_MALLOC (*rule, count, "");
1313 for (i = 0; i < count; i++)
1314 READ_OFFSET (stream, (*rule)[i].offset);
1315 for (i = 0; i < count; i++)
1317 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1318 (*rule)[i].BacktrackGlyphCount
1319 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
1320 (*rule)[i].InputGlyphCount
1321 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
1322 if (! (*rule)[i].InputGlyphCount)
1323 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1324 (*rule)[i].LookaheadGlyphCount
1325 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
1326 (*rule)[i].LookupCount
1327 = read_lookup_record_list (otf, stream,
1328 &(*rule)[i].LookupRecord, -1);
1329 if (! (*rule)[i].LookupCount)
1337 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
1338 OTF_ChainRuleSet **set)
1340 char *errfmt = "ChainRuleSet%s";
1341 unsigned errret = 0;
1342 OTF_StreamState state;
1346 READ_UINT16 (stream, count);
1348 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1349 OTF_MALLOC (*set, count, "");
1350 for (i = 0; i < count; i++)
1352 READ_OFFSET (stream, (*set)[i].offset);
1353 if (! (*set)[i].offset)
1354 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1356 SAVE_STREAM (stream, state);
1357 for (i = 0; i < count; i++)
1359 SEEK_STREAM (stream, offset + (*set)[i].offset);
1360 (*set)[i].ChainRuleCount
1361 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1362 &(*set)[i].ChainRule);
1363 if (! (*set)[i].ChainRuleCount)
1366 RESTORE_STREAM (stream, state);
1371 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1372 OTF_ChainClassRule **rule)
1374 char *errfmt = "ChainClassRule%s";
1375 unsigned errret = 0;
1379 READ_UINT16 (stream, count);
1381 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1382 OTF_MALLOC (*rule, count, "");
1383 for (i = 0; i < count; i++)
1385 READ_OFFSET (stream, (*rule)[i].offset);
1386 if (! (*rule)[i].offset)
1387 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1389 for (i = 0; i < count; i++)
1391 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1392 (*rule)[i].BacktrackGlyphCount
1393 = read_glyph_ids (otf, stream,
1394 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1395 (*rule)[i].InputGlyphCount
1396 = read_glyph_ids (otf, stream,
1397 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1398 if (! (*rule)[i].InputGlyphCount)
1399 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1400 (*rule)[i].LookaheadGlyphCount
1401 = read_glyph_ids (otf, stream,
1402 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1403 (*rule)[i].LookupCount
1404 = read_lookup_record_list (otf, stream,
1405 &(*rule)[i].LookupRecord, -1);
1406 if (! (*rule)[i].LookupCount)
1413 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1414 OTF_ChainClassSet **set)
1416 char *errfmt = "ChainClassSet%s";
1417 unsigned errret = 0;
1418 OTF_StreamState state;
1422 READ_UINT16 (stream, count);
1424 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1425 OTF_MALLOC (*set, count, "");
1426 for (i = 0; i < count; i++)
1427 /* Offset may be zero. */
1428 READ_OFFSET (stream, (*set)[i].offset);
1429 SAVE_STREAM (stream, state);
1430 for (i = 0; i < count; i++)
1431 if ((*set)[i].offset)
1433 SEEK_STREAM (stream, offset + (*set)[i].offset);
1434 (*set)[i].ChainClassRuleCnt
1435 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1436 &(*set)[i].ChainClassRule);
1437 if (! (*set)[i].ChainClassRuleCnt)
1440 RESTORE_STREAM (stream, state);
1445 read_context1 (OTF *otf, OTF_Stream *stream, long offset,
1446 OTF_Coverage *coverage,OTF_Context1 *context1)
1448 if (read_coverage (otf, stream, offset, coverage) < 0)
1450 context1->RuleSetCount
1451 = read_rule_set_list (otf, stream, offset, &context1->RuleSet);
1452 if (! context1->RuleSetCount)
1458 read_context2 (OTF *otf, OTF_Stream *stream, long offset,
1459 OTF_Coverage *coverage,OTF_Context2 *context2)
1461 if (read_coverage (otf, stream, offset, coverage) < 0
1462 || read_class_def (otf, stream, offset, &context2->ClassDef) < 0)
1464 context2->ClassSetCnt
1465 = read_class_set_list (otf, stream, offset, &context2->ClassSet);
1466 if (! context2->ClassSetCnt)
1472 read_context3 (OTF *otf, OTF_Stream *stream, long offset,
1473 OTF_Coverage *coverage,OTF_Context3 *context3)
1475 char *errfmt = "Context1%s";
1478 READ_USHORT (stream, context3->GlyphCount);
1479 if (context3->GlyphCount < 0)
1480 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1481 READ_USHORT (stream, context3->LookupCount);
1482 if (read_coverage_list (otf, stream, offset, &context3->Coverage,
1483 context3->GlyphCount) < 0)
1485 if (read_lookup_record_list (otf, stream, &context3->LookupRecord,
1486 context3->LookupCount) < 0)
1492 read_chain_context1 (OTF *otf, OTF_Stream *stream, long offset,
1493 OTF_Coverage *coverage, OTF_ChainContext1 *chain_context1)
1495 if (read_coverage (otf, stream, offset, coverage) < 0)
1497 chain_context1->ChainRuleSetCount
1498 = read_chain_rule_set_list (otf, stream, offset,
1499 &chain_context1->ChainRuleSet);
1500 if (! chain_context1->ChainRuleSetCount)
1506 read_chain_context2 (OTF *otf, OTF_Stream *stream, long offset,
1507 OTF_Coverage *coverage, OTF_ChainContext2 *chain_context2)
1509 if (read_coverage (otf, stream, offset, coverage) < 0
1510 || read_class_def (otf, stream, offset,
1511 &chain_context2->BacktrackClassDef) < 0
1512 || read_class_def (otf, stream, offset,
1513 &chain_context2->InputClassDef) < 0
1514 || read_class_def (otf, stream, offset,
1515 &chain_context2->LookaheadClassDef) < 0)
1517 chain_context2->ChainClassSetCnt
1518 = read_chain_class_set_list (otf, stream, offset,
1519 &chain_context2->ChainClassSet);
1520 if (! chain_context2->ChainClassSetCnt)
1526 read_chain_context3 (OTF *otf, OTF_Stream *stream, long offset,
1527 OTF_Coverage *coverage, OTF_ChainContext3 *chain_context3)
1531 count = read_coverage_list (otf, stream, offset,
1532 &chain_context3->Backtrack, -1);
1535 chain_context3->BacktrackGlyphCount = (unsigned) count;
1536 count = read_coverage_list (otf, stream, offset,
1537 &chain_context3->Input, -1);
1540 chain_context3->InputGlyphCount = (unsigned) count;
1541 *coverage = chain_context3->Input[0];
1542 count = read_coverage_list (otf, stream, offset,
1543 &chain_context3->LookAhead, -1);
1544 chain_context3->LookaheadGlyphCount = (unsigned) count;
1545 chain_context3->LookupCount
1546 = read_lookup_record_list (otf, stream,
1547 &chain_context3->LookupRecord, -1);
1552 read_gsub_gpos_table (OTF *otf, OTF_Stream *stream, int gsubp)
1554 char *errfmt = gsubp ? "GSUB%s" : "GPOS%s";
1555 void *errret = NULL;
1556 OTF_GSUB_GPOS *gsub_gpos;
1558 OTF_CALLOC (gsub_gpos, 1, "");
1559 READ_FIXED (stream, gsub_gpos->Version);
1560 READ_OFFSET (stream, gsub_gpos->ScriptList.offset);
1561 READ_OFFSET (stream, gsub_gpos->FeatureList.offset);
1562 READ_OFFSET (stream, gsub_gpos->LookupList.offset);
1564 if (read_script_list (otf, stream, gsub_gpos->ScriptList.offset,
1565 &gsub_gpos->ScriptList) < 0
1566 || read_feature_list (otf, stream, gsub_gpos->FeatureList.offset,
1567 &gsub_gpos->FeatureList) < 0
1568 || read_lookup_list (otf, stream, gsub_gpos->LookupList.offset,
1569 &gsub_gpos->LookupList, gsubp) < 0)
1575 /* (1-9) "GSUB" table */
1578 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1580 char *errfmt = "Sequence%s";
1581 unsigned errret = 0;
1585 READ_UINT16 (stream, count);
1587 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1588 OTF_MALLOC (*seq, count, "");
1589 for (i = 0; i < count; i++)
1590 READ_OFFSET (stream, (*seq)[i].offset);
1591 for (i = 0; i < count; i++)
1593 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1594 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1595 &(*seq)[i].Substitute, 0, -1);
1596 if (! (*seq)[i].GlyphCount)
1603 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1604 OTF_Ligature **ligature)
1606 char *errfmt = "Ligature%s";
1611 READ_UINT16 (stream, count);
1614 OTF_MALLOC (*ligature, count, "");
1615 for (i = 0; i < count; i++)
1616 READ_OFFSET (stream, (*ligature)[i].offset);
1617 for (i = 0; i < count; i++)
1619 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1620 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1621 (*ligature)[i].CompCount
1622 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1623 if (! (*ligature)[i].CompCount)
1630 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1631 OTF_LigatureSet **ligset)
1633 char *errfmt = "LigatureSet%s";
1638 READ_UINT16 (stream, count);
1641 OTF_MALLOC (*ligset, count, "");
1642 for (i = 0; i < count; i++)
1643 READ_OFFSET (stream, (*ligset)[i].offset);
1644 for (i = 0; i < count; i++)
1648 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1649 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1650 &(*ligset)[i].Ligature);
1653 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1659 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1660 OTF_AlternateSet **altset)
1662 char *errfmt = "AlternateSet%s";
1667 READ_UINT16 (stream, count);
1669 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1670 OTF_MALLOC (*altset, count, "");
1671 for (i = 0; i < count; i++)
1672 READ_OFFSET (stream, (*altset)[i].offset);
1673 for (i = 0; i < count; i++)
1677 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1678 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1681 (*altset)[i].GlyphCount = (unsigned) alt_count;
1687 read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
1688 OTF_Coverage *coverage,
1689 OTF_GSUB_ReverseChain1 *reverse_chain)
1693 if (read_coverage (otf, stream, offset, coverage) < 0)
1695 count = read_coverage_list (otf, stream, offset,
1696 &reverse_chain->Backtrack, -1);
1699 reverse_chain->BacktrackGlyphCount = (unsigned) count;
1700 count = read_coverage_list (otf, stream, offset,
1701 &reverse_chain->LookAhead, -1);
1704 reverse_chain->LookaheadGlyphCount = (unsigned) count;
1705 count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
1708 reverse_chain->GlyphCount = count;
1713 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1714 unsigned type, OTF_LookupSubTableGSUB *subtable)
1719 SEEK_STREAM (stream, offset);
1720 READ_USHORT (stream, subtable->Format);
1721 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1725 if (subtable->Format == 1)
1727 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1729 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1731 else if (subtable->Format == 2)
1733 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1735 subtable->u.single2.GlyphCount
1736 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1738 if (! subtable->u.single2.GlyphCount)
1742 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1746 if (subtable->Format == 1)
1748 read_coverage (otf, stream, offset, &subtable->Coverage);
1749 subtable->u.multiple1.SequenceCount
1750 = read_sequence (otf, stream, offset,
1751 &subtable->u.multiple1.Sequence);
1754 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1758 if (subtable->Format == 1)
1760 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1762 subtable->u.alternate1.AlternateSetCount
1763 = read_alternate_set_list (otf, stream, offset,
1764 &subtable->u.alternate1.AlternateSet);
1765 if (! subtable->u.alternate1.AlternateSetCount)
1769 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1773 if (subtable->Format == 1)
1775 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1777 subtable->u.ligature1.LigSetCount
1778 = read_ligature_set_list (otf, stream, offset,
1779 &subtable->u.ligature1.LigatureSet);
1780 if (! subtable->u.ligature1.LigSetCount)
1784 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1788 if (subtable->Format == 1)
1790 if (read_context1 (otf, stream, offset, &subtable->Coverage,
1791 &subtable->u.context1) < 0)
1794 else if (subtable->Format == 2)
1796 if (read_context2 (otf, stream, offset, &subtable->Coverage,
1797 &subtable->u.context2) < 0)
1800 else if (subtable->Format == 3)
1802 if (read_context3 (otf, stream, offset, &subtable->Coverage,
1803 &subtable->u.context3) < 0)
1807 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1811 if (subtable->Format == 1)
1813 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
1814 &subtable->u.chain_context1) < 0)
1817 else if (subtable->Format == 2)
1819 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
1820 &subtable->u.chain_context2) < 0)
1823 else if (subtable->Format == 3)
1825 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
1826 &subtable->u.chain_context3) < 0)
1830 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1834 if (subtable->Format == 1)
1838 OTF_LookupSubTableGSUB *ex_subtable;
1840 READ_USHORT (stream, ex_type);
1841 READ_ULONG (stream, ex_offset);
1842 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
1843 if (read_lookup_subtable_gsub (otf, stream, offset + ex_offset,
1844 ex_type, ex_subtable) < 0)
1846 subtable->u.extension1.ExtensionLookupType = ex_type;
1847 subtable->u.extension1.ExtensionOffset = ex_offset;
1848 subtable->u.extension1.ExtensionSubtable = ex_subtable;
1851 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1855 if (subtable->Format == 1)
1857 if (read_reverse_chain1 (otf, stream, offset, &subtable->Coverage,
1858 &subtable->u.reverse_chain1) < 0)
1862 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1866 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1872 read_gsub_table (OTF *otf, OTF_Stream *stream)
1874 return read_gsub_gpos_table (otf, stream, 1);
1878 /* (1-10) "GPOS" table */
1881 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1882 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1885 OTF_StreamState state;
1888 memset (value_record, 0, sizeof (OTF_ValueRecord));
1891 for (i = 0, size = 0; i < 8; i++)
1895 if (bit & OTF_XPlacement)
1896 READ_INT16 (stream, value_record->XPlacement);
1897 if (bit & OTF_XPlacement)
1898 READ_INT16 (stream, value_record->YPlacement);
1899 if (bit & OTF_XAdvance)
1900 READ_INT16 (stream, value_record->XAdvance);
1901 if (bit & OTF_YAdvance)
1902 READ_INT16 (stream, value_record->YAdvance);
1903 if (bit & OTF_XPlaDevice)
1904 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1905 if (bit & OTF_YPlaDevice)
1906 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1907 if (bit & OTF_XAdvDevice)
1908 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1909 if (bit & OTF_YAdvDevice)
1910 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1911 SAVE_STREAM (stream, state);
1912 if (value_record->XPlaDevice.offset)
1914 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1917 if (value_record->YPlaDevice.offset)
1919 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1922 if (value_record->XAdvDevice.offset)
1924 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1927 if (value_record->YAdvDevice.offset)
1929 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1932 RESTORE_STREAM (stream, state);
1938 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1940 char *errfmt = "Anchor%s";
1943 SEEK_STREAM (stream, offset + anchor->offset);
1944 READ_UINT16 (stream, anchor->AnchorFormat);
1945 READ_INT16 (stream, anchor->XCoordinate);
1946 READ_INT16 (stream, anchor->YCoordinate);
1947 if (anchor->AnchorFormat == 1)
1949 else if (anchor->AnchorFormat == 2)
1951 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1953 else if (anchor->AnchorFormat == 3)
1955 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1956 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1957 if (anchor->f.f2.XDeviceTable.offset)
1959 if (read_device_table (otf, stream, offset + anchor->offset,
1960 &anchor->f.f2.XDeviceTable) < 0)
1963 if (anchor->f.f2.YDeviceTable.offset)
1965 if (read_device_table (otf, stream, offset + anchor->offset,
1966 &anchor->f.f2.YDeviceTable) < 0)
1971 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1977 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1978 OTF_MarkArray *array)
1980 char *errfmt = "MarkArray%s";
1982 OTF_StreamState state;
1985 READ_OFFSET (stream, array->offset);
1986 SAVE_STREAM (stream, state);
1987 SEEK_STREAM (stream, offset + array->offset);
1988 READ_UINT16 (stream, array->MarkCount);
1989 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1990 for (i = 0; i < array->MarkCount; i++)
1992 READ_UINT16 (stream, array->MarkRecord[i].Class);
1993 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1995 for (i = 0; i < array->MarkCount; i++)
1996 if (read_anchor (otf, stream, offset + array->offset,
1997 &array->MarkRecord[i].MarkAnchor) < 0)
1999 RESTORE_STREAM (stream, state);
2004 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
2005 unsigned ClassCount, OTF_AnchorArray *array)
2007 char *errfmt = "AnchorArray%s";
2009 OTF_StreamState state;
2012 READ_OFFSET (stream, array->offset);
2013 SAVE_STREAM (stream, state);
2014 SEEK_STREAM (stream, offset + array->offset);
2015 READ_UINT16 (stream, array->Count);
2016 OTF_MALLOC (array->AnchorRecord, array->Count, "");
2017 for (i = 0; i < array->Count; i++)
2019 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
2021 for (j = 0; j < ClassCount; j++)
2022 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
2024 for (i = 0; i < array->Count; i++)
2025 for (j = 0; j < ClassCount; j++)
2026 if (read_anchor (otf, stream, offset + array->offset,
2027 &array->AnchorRecord[i].Anchor[j]) < 0)
2029 RESTORE_STREAM (stream, state);
2033 static OTF_PairSet *
2034 read_pair_set_list (OTF *otf, OTF_Stream *stream, long offset, unsigned num,
2035 enum OTF_ValueFormat bit1, enum OTF_ValueFormat bit2)
2037 char *errfmt = "PairSet%s";
2038 void *errret = NULL;
2039 OTF_StreamState state;
2043 OTF_MALLOC (set, num, "");
2044 for (i = 0; i < num; i++)
2045 READ_OFFSET (stream, set[i].offset);
2046 SAVE_STREAM (stream, state);
2047 for (i = 0; i < num; i++)
2049 SEEK_STREAM (stream, offset + set[i].offset);
2050 READ_UINT16 (stream, set[i].PairValueCount);
2051 OTF_MALLOC (set[i].PairValueRecord, set[i].PairValueCount, "");
2052 for (j = 0; j < set[i].PairValueCount; j++)
2054 OTF_PairValueRecord *rec = set[i].PairValueRecord + j;
2056 READ_UINT16 (stream, rec->SecondGlyph);
2057 read_value_record (otf, stream, offset, bit1, &rec->Value1);
2058 read_value_record (otf, stream, offset, bit2, &rec->Value2);
2061 RESTORE_STREAM (stream, state);
2065 static OTF_Class1Record *
2066 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
2067 unsigned num1, enum OTF_ValueFormat bit1,
2068 unsigned num2, enum OTF_ValueFormat bit2)
2070 char *errfmt = "Class1Record%s";
2071 void *errret = NULL;
2072 OTF_Class1Record *rec;
2075 OTF_MALLOC (rec, num1, "");
2076 for (i = 0; i < num1; i++)
2078 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
2079 for (j = 0; j < num2; j++)
2081 if (read_value_record (otf, stream, offset,
2082 bit1, &rec[i].Class2Record[j].Value1) < 0
2083 || read_value_record (otf, stream, offset,
2084 bit2, &rec[i].Class2Record[j].Value2) < 0)
2092 read_entry_exit_list (OTF *otf, OTF_Stream *stream, long offset,
2093 OTF_EntryExitRecord **rec)
2095 char *errfmt = "EntryExitSet%s";
2099 OTF_StreamState state;
2101 READ_UINT16 (stream, count);
2103 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
2104 OTF_MALLOC (*rec, count, "");
2105 for (i = 0; i < count; i++)
2107 READ_OFFSET (stream, (*rec)[i].EntryAnchor.offset);
2108 READ_OFFSET (stream, (*rec)[i].ExitAnchor.offset);
2110 SAVE_STREAM (stream, state);
2111 for (i = 0; i < count; i++)
2113 if (read_anchor (otf, stream, offset, &(*rec)[i].EntryAnchor) < 0)
2115 if (read_anchor (otf, stream, offset, &(*rec)[i].ExitAnchor) < 0)
2118 RESTORE_STREAM (stream, state);
2123 read_ligature_attach (OTF *otf, OTF_Stream *stream, long offset,
2124 unsigned ClassCount, OTF_LigatureAttach *attach)
2126 char *errfmt = "LigatureAttach%s";
2130 SEEK_STREAM (stream, offset + attach->offset);
2131 READ_UINT16 (stream, attach->ComponentCount);
2132 OTF_MALLOC (attach->ComponentRecord, attach->ComponentCount, "");
2133 for (i = 0; i < attach->ComponentCount; i++)
2135 OTF_MALLOC (attach->ComponentRecord[i].LigatureAnchor, ClassCount,
2136 " (ComponentRecord)");
2137 for (j = 0; j < ClassCount; j++)
2138 READ_OFFSET (stream,
2139 attach->ComponentRecord[i].LigatureAnchor[j].offset);
2141 for (i = 0; i < attach->ComponentCount; i++)
2142 for (j = 0; j < ClassCount; j++)
2143 if (read_anchor (otf, stream, offset + attach->offset,
2144 &attach->ComponentRecord[i].LigatureAnchor[j]) < 0)
2150 read_ligature_array (OTF *otf, OTF_Stream *stream, long offset,
2151 unsigned class_count, OTF_LigatureArray *array)
2153 char *errfmt = "LigatureArray%s";
2155 OTF_StreamState state;
2158 READ_OFFSET (stream, array->offset);
2159 SAVE_STREAM (stream, state);
2160 SEEK_STREAM (stream, offset + array->offset);
2161 READ_UINT16 (stream, array->LigatureCount);
2162 OTF_MALLOC (array->LigatureAttach, array->LigatureCount, "");
2163 for (i = 0; i < array->LigatureCount; i++)
2164 READ_OFFSET (stream, array->LigatureAttach[i].offset);
2165 for (i = 0; i < array->LigatureCount; i++)
2166 read_ligature_attach (otf, stream, offset + array->offset,
2167 class_count, array->LigatureAttach + i);
2168 RESTORE_STREAM (stream, state);
2173 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
2174 long offset, unsigned type,
2175 OTF_LookupSubTableGPOS *subtable)
2180 SEEK_STREAM (stream, offset);
2181 READ_UINT16 (stream, subtable->Format);
2182 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
2186 if (subtable->Format == 1)
2188 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2190 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
2191 read_value_record (otf, stream, offset,
2192 subtable->u.single1.ValueFormat,
2193 &subtable->u.single1.Value);
2195 else if (subtable->Format == 2)
2197 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
2200 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2202 READ_UINT16 (stream, single2->ValueFormat);
2203 READ_UINT16 (stream, single2->ValueCount);
2204 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
2205 for (i = 0; i < single2->ValueCount; i++)
2206 read_value_record (otf, stream, offset, single2->ValueFormat,
2207 single2->Value + i);
2210 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2214 if (subtable->Format == 1)
2216 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2218 READ_UINT16 (stream, subtable->u.pair1.ValueFormat1);
2219 READ_UINT16 (stream, subtable->u.pair1.ValueFormat2);
2220 READ_UINT16 (stream, subtable->u.pair1.PairSetCount);
2221 subtable->u.pair1.PairSet
2222 = read_pair_set_list (otf, stream, offset,
2223 subtable->u.pair1.PairSetCount,
2224 subtable->u.pair1.ValueFormat1,
2225 subtable->u.pair1.ValueFormat2);
2226 if (! subtable->u.pair1.PairSet)
2229 else if (subtable->Format == 2)
2231 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2233 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
2234 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
2235 if (read_class_def (otf, stream, offset,
2236 &subtable->u.pair2.ClassDef1) < 0
2237 || read_class_def (otf, stream, offset,
2238 &subtable->u.pair2.ClassDef2) < 0)
2240 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
2241 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
2242 subtable->u.pair2.Class1Record
2243 = read_class1_record_list (otf, stream, offset,
2244 subtable->u.pair2.Class1Count,
2245 subtable->u.pair2.ValueFormat1,
2246 subtable->u.pair2.Class2Count,
2247 subtable->u.pair2.ValueFormat2);
2248 if (! subtable->u.pair2.Class1Record)
2252 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2256 if (subtable->Format == 1)
2258 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
2260 subtable->u.cursive1.EntryExitCount
2261 = read_entry_exit_list (otf, stream, offset,
2262 &subtable->u.cursive1.EntryExitRecord);
2263 if (! subtable->u.cursive1.EntryExitCount)
2267 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2271 if (subtable->Format == 1)
2273 read_coverage (otf, stream, offset, &subtable->Coverage);
2274 read_coverage (otf, stream, offset,
2275 &subtable->u.mark_base1.BaseCoverage);
2276 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2277 read_mark_array (otf, stream, offset,
2278 &subtable->u.mark_base1.MarkArray);
2279 read_anchor_array (otf, stream, offset,
2280 subtable->u.mark_base1.ClassCount,
2281 &subtable->u.mark_base1.BaseArray);
2284 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2288 if (subtable->Format == 1)
2290 read_coverage (otf, stream, offset, &subtable->Coverage);
2291 read_coverage (otf, stream, offset,
2292 &subtable->u.mark_lig1.LigatureCoverage);
2293 READ_UINT16 (stream, subtable->u.mark_lig1.ClassCount);
2294 read_mark_array (otf, stream, offset,
2295 &subtable->u.mark_lig1.MarkArray);
2296 read_ligature_array (otf, stream, offset,
2297 subtable->u.mark_lig1.ClassCount,
2298 &subtable->u.mark_lig1.LigatureArray);
2303 if (subtable->Format == 1)
2305 read_coverage (otf, stream, offset, &subtable->Coverage);
2306 read_coverage (otf, stream, offset,
2307 &subtable->u.mark_mark1.Mark2Coverage);
2308 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
2309 read_mark_array (otf, stream, offset,
2310 &subtable->u.mark_mark1.Mark1Array);
2311 read_anchor_array (otf, stream, offset,
2312 subtable->u.mark_mark1.ClassCount,
2313 &subtable->u.mark_mark1.Mark2Array);
2316 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2320 if (subtable->Format == 1)
2322 if (read_context1 (otf, stream, offset, &subtable->Coverage,
2323 &subtable->u.context1) < 0)
2326 else if (subtable->Format == 2)
2328 if (read_context2 (otf, stream, offset, &subtable->Coverage,
2329 &subtable->u.context2) < 0)
2332 else if (subtable->Format == 3)
2334 if (read_context3 (otf, stream, offset, &subtable->Coverage,
2335 &subtable->u.context3) < 0)
2339 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2343 if (subtable->Format == 1)
2345 if (read_chain_context1 (otf, stream, offset, &subtable->Coverage,
2346 &subtable->u.chain_context1) < 0)
2349 else if (subtable->Format == 2)
2351 if (read_chain_context2 (otf, stream, offset, &subtable->Coverage,
2352 &subtable->u.chain_context2) < 0)
2355 else if (subtable->Format == 3)
2357 if (read_chain_context3 (otf, stream, offset, &subtable->Coverage,
2358 &subtable->u.chain_context3) < 0)
2362 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2366 if (subtable->Format == 1)
2370 OTF_LookupSubTableGPOS *ex_subtable;
2372 READ_USHORT (stream, ex_type);
2373 READ_ULONG (stream, ex_offset);
2374 OTF_CALLOC (ex_subtable, 1, " (SubTable)");
2375 if (read_lookup_subtable_gpos (otf, stream, offset + ex_offset,
2376 ex_type, ex_subtable) < 0)
2378 subtable->u.extension1.ExtensionLookupType = ex_type;
2379 subtable->u.extension1.ExtensionOffset = ex_offset;
2380 subtable->u.extension1.ExtensionSubtable = ex_subtable;
2383 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
2387 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
2393 read_gpos_table (OTF *otf, OTF_Stream *stream)
2395 return read_gsub_gpos_table (otf, stream, 0);
2403 read_base_table (OTF_Stream *stream, long offset)
2407 OTF_MALLOC (base, 1);
2416 read_jstf_table (OTF_Stream *stream, long offset)
2420 OTF_MALLOC (jstf, 1);
2426 /*** (1-11) Structure for OTF */
2429 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
2433 READ_FIXED (stream, table->sfnt_version);
2434 READ_USHORT (stream, table->numTables);
2435 READ_USHORT (stream, table->searchRange);
2436 READ_USHORT (stream, table->enterSelector);
2437 READ_USHORT (stream, table->rangeShift);
2442 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
2447 READ_TAG (stream, tag);
2449 table->name[0] = tag >> 24;
2450 table->name[1] = (tag >> 16) & 0xFF;
2451 table->name[0] = (tag >> 8) & 0xFF;
2452 table->name[0] = tag >> 8;
2453 table->name[0] = '\0';
2454 READ_ULONG (stream, table->checkSum);
2455 READ_ULONG (stream, table->offset);
2456 READ_ULONG (stream, table->length);
2461 read_header_part (OTF *otf, FILE *fp)
2463 char *errfmt = "otf header%s";
2465 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
2468 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
2470 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
2471 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
2472 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
2473 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
2474 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
2475 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
2476 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
2477 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
2478 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
2479 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
2480 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
2481 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
2483 head_tag = OTF_tag ("head");
2484 name_tag = OTF_tag ("name");
2485 cmap_tag = OTF_tag ("cmap");
2486 gdef_tag = OTF_tag ("GDEF");
2487 gsub_tag = OTF_tag ("GSUB");
2488 gpos_tag = OTF_tag ("GPOS");
2490 stream = make_stream ();
2494 internal_data->header_stream = stream;
2496 /* Size of Offset Table is 12 bytes. */
2497 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
2499 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
2502 /* Size of each Table Directory is 16 bytes. */
2503 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
2504 "Table Directory") < 0)
2507 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
2508 for (i = 0; i < otf->offset_table.numTables; i++)
2510 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
2511 OTF_TableInfo *table_info = NULL;
2515 if (tag == head_tag)
2516 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2517 else if (tag == name_tag)
2518 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2519 else if (tag == cmap_tag)
2520 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2521 else if (tag == gdef_tag)
2522 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2523 else if (tag == gsub_tag)
2524 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2525 else if (tag == gpos_tag)
2526 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2530 table_info->stream = make_stream ();
2531 if (setup_stream (table_info->stream, fp,
2532 otf->table_dirs[i].offset,
2533 otf->table_dirs[i].length,
2534 otf->table_dirs[i].name) < 0)
2539 internal_data->header_stream = NULL;
2540 free_stream (stream);
2544 static OTF_TableInfo *
2545 get_table_info (OTF *otf, char *name)
2547 char *errfmt = "OTF Table Read%s";
2548 OTF_TableInfo *errret = NULL;
2549 OTF_InternalData *internal_data = otf->internal_data;
2550 OTF_TableInfo *table_info;
2551 OTF_Tag tag = OTF_tag (name);
2554 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2556 if (tag == OTF_tag ("head"))
2557 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2558 else if (tag == OTF_tag ("name"))
2559 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2560 else if (tag == OTF_tag ("cmap"))
2561 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2562 else if (tag == OTF_tag ("GDEF"))
2563 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2564 else if (tag == OTF_tag ("GSUB"))
2565 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2566 else if (tag == OTF_tag ("GPOS"))
2567 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2569 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported table name)");
2571 if (*table_info->address)
2574 if (! table_info->stream)
2575 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2576 if (! table_info->reader)
2577 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2583 /*** (2) API for reading OTF */
2585 /*** (2-1) OTF_open() */
2587 /* Note: We can't use memory allocation macros in the following
2588 functions because those macros return from the functions before
2589 freeing memory previously allocated. */
2592 OTF_open (char *otf_name)
2595 char *errfmt = "opening otf (%s)";
2596 void *errret = NULL;
2598 OTF_InternalData *internal_data;
2600 fp = fopen (otf_name, "r");
2602 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2603 otf = calloc (1, sizeof (OTF));
2605 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2606 otf->filename = strdup (otf_name);
2607 if (! otf->filename)
2611 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2614 internal_data = calloc (1, sizeof (OTF_InternalData));
2615 if (! internal_data)
2616 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2617 otf->internal_data = internal_data;
2618 if (! allocate_memory_record (otf))
2619 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2621 /* Here after, all pointers to allocated memory are recorded in
2622 otf->internal_data->memory_record except for what allocated by
2623 the functions allocate_memory_record and make_stream. */
2625 if (read_header_part (otf, fp) < 0)
2636 /*** (2-2) OTF_close() */
2639 OTF_close (OTF *otf)
2641 OTF_InternalData *internal_data = otf->internal_data;
2646 OTF_MemoryRecord *memrec = internal_data->memory_record;
2648 if (internal_data->header_stream)
2649 free_stream (internal_data->header_stream);
2651 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2652 if (internal_data->table_info[i].stream)
2653 free_stream (internal_data->table_info[i].stream);
2657 OTF_MemoryRecord *next = memrec->next;
2659 for (i = memrec->used - 1; i >= 0; i--)
2660 free (memrec->memory[i]);
2664 free (internal_data);
2667 free (otf->filename);
2671 /*** (2-3) OTF_get_table() */
2674 OTF_get_table (OTF *otf, char *name)
2676 OTF_TableInfo *table_info = get_table_info (otf, name);
2681 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2682 free_stream (table_info->stream);
2683 table_info->stream = NULL;
2684 if (! *table_info->address)
2686 table_info->reader = NULL;
2692 /*** (2-4) OTF_check_table() */
2695 OTF_check_table (OTF *otf, char *name)
2697 return (get_table_info (otf, name) ? 0 : -1);
2703 /*** (5) API miscellaneous ***/
2706 OTF_tag (char *name)
2708 unsigned char *p = (unsigned char *) name;
2712 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2716 OTF_tag_name (OTF_Tag tag, char *name)
2718 name[0] = (char) (tag >> 24);
2719 name[1] = (char) ((tag >> 16) & 0xFF);
2720 name[2] = (char) ((tag >> 8) & 0xFF);
2721 name[3] = (char) (tag & 0xFF);