10 Example of typical usage of OTF_Stream.
14 OTF_StreamState state;
17 OPEN_STREAM (_FILE_NAME_, stream);
20 SETUP_STREAM (stream, fp, 0, 256, _NAME_);
21 offset = READ_OFFSET (stream);
22 nbytes = READ_ULONG (stream);
23 SETUP_STREAM (stream, fp, offset, nbytes, _NAME2_);
25 CLOSE_STREAM (stream);
40 typedef long OTF_StreamState;
46 char *errfmt = "stream creation%s";
49 stream = malloc (sizeof (OTF_Stream));
51 OTF_ERROR (OTF_ERROR_MEMORY, "");
56 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes,
59 char *errfmt = "stream setup for %s";
64 if (stream->allocated < nbytes)
66 unsigned char *buf = malloc (nbytes);
69 OTF_ERROR (OTF_ERROR_MEMORY, stream->name);
73 stream->allocated = nbytes;
75 stream->bufsize = nbytes;
76 if (fseek (fp, offset, SEEK_SET) < 0)
77 OTF_ERROR (OTF_ERROR_FILE, stream->name);
78 if (fread (stream->buf, 1, nbytes, fp) != nbytes)
79 OTF_ERROR (OTF_ERROR_FILE, stream->name);
85 free_stream (OTF_Stream *stream)
91 #define SAVE_STREAM(stream, state) ((state) = (stream)->pos)
92 #define RESTORE_STREAM(stream, state) ((stream)->pos = (state))
93 #define SEEK_STREAM(stream, offset) ((stream)->pos = (offset))
95 #define STREAM_CHECK_SIZE(stream, size) \
96 if ((stream)->pos + (size) > (stream)->bufsize) \
98 char *errfmt = "buffer overrun in %s"; \
100 OTF_ERROR (OTF_ERROR_TABLE, (stream)->name); \
106 #define READ_USHORT(stream, var) \
108 STREAM_CHECK_SIZE ((stream), 2); \
109 (var) = (((stream)->buf[(stream)->pos] << 8) \
110 | (stream)->buf[(stream)->pos + 1]); \
111 (stream)->pos += 2; \
114 #define READ_SHORT(stream, var) \
116 STREAM_CHECK_SIZE ((stream), 2); \
117 (var) = (short) (((stream)->buf[(stream)->pos] << 8) \
118 | (stream)->buf[(stream)->pos + 1]); \
119 (stream)->pos += 2; \
122 #define READ_ULONG(stream, var) \
124 STREAM_CHECK_SIZE ((stream), 4); \
125 (var) = (((stream)->buf[(stream)->pos] << 24) \
126 | ((stream)->buf[(stream)->pos + 1] << 16) \
127 | ((stream)->buf[(stream)->pos + 2] << 8) \
128 | (stream)->buf[(stream)->pos + 3]); \
129 (stream)->pos += 4; \
132 #define READ_LONG(stream, var) \
134 STREAM_CHECK_SIZE ((stream), 4); \
135 (var) = (int) (((stream)->buf[(stream)->pos] << 24) \
136 | ((stream)->buf[(stream)->pos + 1] << 16) \
137 | ((stream)->buf[(stream)->pos + 2] << 8) \
138 | (stream)->buf[(stream)->pos + 3]); \
139 (stream)->pos += 4; \
143 #define READ_FIXED(stream, fixed) \
145 READ_USHORT ((stream), (fixed).high); \
146 READ_USHORT ((stream), (fixed).low); \
150 #define READ_BYTES(stream, p, nbytes) \
152 STREAM_CHECK_SIZE ((stream), (nbytes)); \
153 memcpy ((p), (stream)->buf + (stream)->pos, (nbytes)); \
154 (stream)->pos += (nbytes); \
158 #define READ_TAG READ_ULONG
159 #define READ_OFFSET READ_USHORT
160 #define READ_UINT16 READ_USHORT
161 #define READ_INT16 READ_SHORT
162 #define READ_GLYPHID READ_USHORT
177 #define OTF_MEMORY_RECORD_SIZE 1024
179 struct OTF_MemoryRecord
182 void *memory[OTF_MEMORY_RECORD_SIZE];
183 struct OTF_MemoryRecord *next;
186 typedef struct OTF_MemoryRecord OTF_MemoryRecord;
190 /* Points to one of OTF->head, OTF->name, etc. */
192 /* Function to read one of OTF tables. */
193 void *(*reader) (OTF *otf, OTF_Stream *stream);
194 /* Stream given to <reader>. */
198 struct OTF_InternalData
200 /* Information about each OTF table. */
201 OTF_TableInfo table_info[OTF_TABLE_TYPE_MAX];
203 /* Stream used to read the header part of OTF. */
204 OTF_Stream *header_stream;
206 /* Records of allocated memories. */
207 OTF_MemoryRecord *memory_record;
211 static OTF_MemoryRecord *
212 allocate_memory_record (OTF *otf)
214 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
215 OTF_MemoryRecord *memrec = calloc (1, sizeof (OTF_MemoryRecord));
220 memrec->next = internal_data->memory_record;
221 internal_data->memory_record = memrec;
225 /* Memory allocation macros. */
227 #define OTF_MALLOC(p, size, arg) \
233 OTF_MemoryRecord *memrec \
234 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
235 (p) = malloc (sizeof (*(p)) * (size)); \
237 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
238 && ! (memrec = allocate_memory_record (otf)))) \
239 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
240 memrec->memory[memrec->used++] = (p); \
245 #define OTF_CALLOC(p, size, arg) \
247 (p) = calloc ((size), sizeof (*(p))); \
249 || ((((OTF_InternalData *) otf->internal_data)->memory_record->used \
250 >= OTF_MEMORY_RECORD_SIZE) \
251 && (allocate_memory_record (otf) < 0))) \
252 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
253 ((OTF_InternalData *) otf->internal_data)->memory_record->memory \
254 [((OTF_InternalData *) otf->internal_data)->memory_record->used++] \
261 static void *read_head_table (OTF *otf, OTF_Stream *stream);
262 static void *read_name_table (OTF *otf, OTF_Stream *stream);
263 static void *read_cmap_table (OTF *otf, OTF_Stream *stream);
264 static void *read_gdef_table (OTF *otf, OTF_Stream *stream);
265 static void *read_gsub_table (OTF *otf, OTF_Stream *stream);
266 static void *read_gpos_table (OTF *otf, OTF_Stream *stream);
269 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
273 READ_FIXED (stream, table->sfnt_version);
274 READ_USHORT (stream, table->numTables);
275 READ_USHORT (stream, table->searchRange);
276 READ_USHORT (stream, table->enterSelector);
277 READ_USHORT (stream, table->rangeShift);
282 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
287 READ_TAG (stream, tag);
289 table->name[0] = tag >> 24;
290 table->name[1] = (tag >> 16) & 0xFF;
291 table->name[0] = (tag >> 8) & 0xFF;
292 table->name[0] = tag >> 8;
293 table->name[0] = '\0';
294 READ_ULONG (stream, table->checkSum);
295 READ_ULONG (stream, table->offset);
296 READ_ULONG (stream, table->length);
301 read_header_part (OTF *otf, FILE *fp)
303 char *errfmt = "otf header%s";
305 OTF_InternalData *internal_data;
306 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
310 internal_data = calloc (1, sizeof (OTF_InternalData));
312 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
313 otf->internal_data = internal_data;
314 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = (void *) &otf->head;
315 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
316 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = (void *) &otf->name;
317 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
318 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = (void *) &otf->cmap;
319 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
320 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = (void *) &otf->gdef;
321 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
322 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = (void *) &otf->gsub;
323 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
324 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = (void *) &otf->gpos;
325 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
327 if (allocate_memory_record (otf) < 0)
328 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
330 head_tag = otf_tag ("head");
331 name_tag = otf_tag ("name");
332 cmap_tag = otf_tag ("cmap");
333 gdef_tag = otf_tag ("GDEF");
334 gsub_tag = otf_tag ("GSUB");
335 gpos_tag = otf_tag ("GPOS");
337 stream = make_stream ();
341 internal_data->header_stream = stream;
343 /* Size of Offset Table is 12 bytes. */
344 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
346 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
349 /* Size of each Table Directory is 16 bytes. */
350 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
351 "Table Directory") < 0)
354 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
355 for (i = 0; i < otf->offset_table.numTables; i++)
357 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
358 OTF_TableInfo *table_info = NULL;
363 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
364 else if (tag == name_tag)
365 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
366 else if (tag == cmap_tag)
367 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
368 else if (tag == gdef_tag)
369 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
370 else if (tag == gsub_tag)
371 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
372 else if (tag == gpos_tag)
373 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
377 table_info->stream = make_stream ();
378 if (setup_stream (table_info->stream, fp,
379 otf->table_dirs[i].offset,
380 otf->table_dirs[i].length,
381 otf->table_dirs[i].name) < 0)
386 internal_data->header_stream = NULL;
387 free_stream (stream);
395 read_head_table (OTF *otf, OTF_Stream *stream)
397 char *errfmt = "head%s";
401 OTF_CALLOC (head, 1, "");
402 READ_FIXED (stream, head->TableVersionNumber);
403 READ_FIXED (stream, head->fontRevision);
404 READ_ULONG (stream, head->checkSumAdjustment);
405 READ_ULONG (stream, head->magicNumber);
406 READ_USHORT (stream, head->flags);
407 READ_USHORT (stream, head->unitsPerEm);
415 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
416 OTF_ScriptList *list)
418 char *errfmt = "Script List%s";
422 SEEK_STREAM (stream, offset);
423 READ_USHORT (stream, list->ScriptCount);
424 OTF_CALLOC (list->Script, list->ScriptCount, "");
426 for (i = 0; i < list->ScriptCount; i++)
428 READ_TAG (stream, list->Script[i].ScriptTag);
429 READ_OFFSET (stream, list->Script[i].offset);
431 for (i = 0; i < list->ScriptCount; i++)
433 OTF_Script *script = list->Script + i;
434 long script_offset = offset + script->offset;
436 SEEK_STREAM (stream, script_offset);
437 READ_OFFSET (stream, script->DefaultLangSysOffset);
438 READ_USHORT (stream, script->LangSysCount);
439 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
440 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
441 for (j = 0; j < script->LangSysCount; j++)
443 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
444 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
447 if (script->DefaultLangSysOffset)
449 OTF_LangSys *langsys = &script->DefaultLangSys;
451 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
452 READ_OFFSET (stream, langsys->LookupOrder);
453 READ_USHORT (stream, langsys->ReqFeatureIndex);
454 READ_USHORT (stream, langsys->FeatureCount);
455 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
457 for (k = 0; k < langsys->FeatureCount; k++)
458 READ_USHORT (stream, langsys->FeatureIndex[k]);
461 for (j = 0; j < script->LangSysCount; j++)
463 OTF_LangSys *langsys = script->LangSys + j;
466 script_offset + script->LangSysRecord[j].LangSys);
467 READ_OFFSET (stream, langsys->LookupOrder);
468 READ_USHORT (stream, langsys->ReqFeatureIndex);
469 READ_USHORT (stream, langsys->FeatureCount);
470 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
472 for (k = 0; k < langsys->FeatureCount; k++)
473 READ_USHORT (stream, langsys->FeatureIndex[k]);
481 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
482 OTF_FeatureList *list)
484 char *errfmt = "Feature List%s";
488 READ_UINT16 (stream, list->FeatureCount);
489 OTF_CALLOC (list->Feature, list->FeatureCount, "");
490 for (i = 0; i < list->FeatureCount; i++)
492 READ_TAG (stream, list->Feature[i].FeatureTag);
493 READ_OFFSET (stream, list->Feature[i].offset);
495 for (i = 0; i < list->FeatureCount; i++)
497 OTF_Feature *feature = list->Feature + i;
499 SEEK_STREAM (stream, offset + feature->offset);
500 READ_OFFSET (stream, feature->FeatureParams);
501 READ_UINT16 (stream, feature->LookupCount);
502 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
503 " (LookupListIndex)");
504 for (j = 0; j < feature->LookupCount; j++)
505 READ_UINT16 (stream, feature->LookupListIndex[j]);
511 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
512 long offset, unsigned type,
513 OTF_LookupSubTable *subtable);
514 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
515 long offset, unsigned type,
516 OTF_LookupSubTable *subtable);
519 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
520 OTF_LookupList *list, int gsub)
522 char *errfmt = "Lookup List%s";
526 SEEK_STREAM (stream, offset);
527 READ_UINT16 (stream, list->LookupCount);
528 OTF_CALLOC (list->Lookup, list->LookupCount, "");
530 for (i = 0; i < list->LookupCount; i++)
531 READ_OFFSET (stream, list->Lookup[i].offset);
532 for (i = 0; i < list->LookupCount; i++)
534 OTF_Lookup *lookup = list->Lookup + i;
536 SEEK_STREAM (stream, offset + lookup->offset);
537 READ_UINT16 (stream, lookup->LookupType);
538 READ_UINT16 (stream, lookup->LookupFlag);
539 READ_UINT16 (stream, lookup->SubTableCount);
540 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
541 " (SubTableOffset)");
542 OTF_CALLOC (lookup->SubTable, lookup->SubTableCount,
544 for (j = 0; j < lookup->SubTableCount; j++)
545 READ_OFFSET (stream, lookup->SubTableOffset[j]);
547 for (j = 0; j < lookup->SubTableCount; j++)
550 = offset + lookup->offset + lookup->SubTableOffset[j];
552 if (read_lookup_subtable_gsub (otf, stream, this_offset,
554 lookup->SubTable + j) < 0)
558 for (j = 0; j < lookup->SubTableCount; j++)
561 = offset + lookup->offset + lookup->SubTableOffset[j];
563 if (read_lookup_subtable_gpos (otf, stream, this_offset,
565 lookup->SubTable + j) < 0)
575 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids, int minus)
577 char *errfmt = "GlyphID List%s";
582 READ_UINT16 (stream, count);
585 OTF_MALLOC (*ids, count, "");
586 for (i = 0; i < count + minus; i++)
587 READ_GLYPHID (stream, (*ids)[i]);
592 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
594 char *errfmt = "RangeRecord%s";
599 READ_UINT16 (stream, count);
602 OTF_MALLOC (*record, count, "");
603 for (i = 0; i < count; i++)
605 READ_GLYPHID (stream, (*record)[i].Start);
606 READ_GLYPHID (stream, (*record)[i].End);
607 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
614 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
615 OTF_Coverage *coverage)
617 char *errfmt = "Coverage%s";
619 OTF_StreamState state;
622 READ_OFFSET (stream, coverage->offset);
623 SAVE_STREAM (stream, state);
624 SEEK_STREAM (stream, offset + coverage->offset);
625 READ_UINT16 (stream, coverage->CoverageFormat);
626 if (coverage->CoverageFormat == 1)
627 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0);
628 else if (coverage->CoverageFormat == 2)
629 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
631 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
634 coverage->Count = (unsigned) count;
635 RESTORE_STREAM (stream, state);
640 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
641 OTF_Coverage **coverage)
643 char *errfmt = "Coverage List%s";
648 READ_UINT16 (stream, count);
651 OTF_MALLOC (*coverage, count, "");
652 for (i = 0; i < count; i++)
653 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
660 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
663 char *errfmt = "ClassDef%s";
666 SEEK_STREAM (stream, class->offset);
667 READ_UINT16 (stream, class->ClassFormat);
668 if (class->ClassFormat == 1)
670 READ_GLYPHID (stream, class->f.f1.StartGlyph);
671 class->f.f1.GlyphCount
673 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
674 if (! class->f.f1.GlyphCount)
675 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
677 else if (class->ClassFormat == 2)
679 class->f.f2.ClassRangeCount
680 = (read_range_records
681 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
682 if (! class->f.f2.ClassRangeCount)
683 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
686 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
692 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
694 char *errfmt = "ClassDef%s";
696 OTF_StreamState state;
698 READ_OFFSET (stream, class->offset);
699 SAVE_STREAM (stream, state);
700 SEEK_STREAM (stream, offset + class->offset);
701 READ_UINT16 (stream, class->ClassFormat);
702 if (class->ClassFormat == 1)
704 READ_GLYPHID (stream, class->f.f1.StartGlyph);
705 class->f.f1.GlyphCount
707 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
708 if (! class->f.f1.GlyphCount)
711 else if (class->ClassFormat == 2)
713 class->f.f2.ClassRangeCount
714 = (read_range_records
715 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
716 if (! class->f.f2.ClassRangeCount)
720 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
722 RESTORE_STREAM (stream, state);
728 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
729 OTF_DeviceTable *table)
731 char *errfmt = "Device Table%s";
742 SEEK_STREAM (stream, offset + table->offset);
743 READ_UINT16 (stream, table->StartSize);
744 READ_UINT16 (stream, table->EndSize);
745 READ_UINT16 (stream, table->DeltaFormat);
746 num = table->EndSize - table->StartSize + 1;
747 OTF_MALLOC (table->DeltaValue, num, "");
749 if (table->DeltaFormat == 1)
750 for (i = 0; i < num; i++)
753 READ_UINT16 (stream, val);
754 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
755 table->DeltaValue[i] = intval.int2;
757 else if (table->DeltaFormat == 2)
758 for (i = 0; i < num; i++)
761 READ_UINT16 (stream, val);
762 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
763 table->DeltaValue[i] = intval.int4;
765 else if (table->DeltaFormat == 3)
766 for (i = 0; i < num; i++)
770 READ_UINT16 (stream, val);
771 intval.int8 = val >> 8;
772 table->DeltaValue[i] = intval.int8;
776 intval.int8 = val >> 8;
777 table->DeltaValue[i] = intval.int8;
781 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
789 read_gsub_table (OTF *otf, OTF_Stream *stream)
791 char *errfmt = "GSUB%s";
795 OTF_CALLOC (gsub, 1, "");
796 READ_FIXED (stream, gsub->Version);
797 READ_OFFSET (stream, gsub->ScriptList.offset);
798 READ_OFFSET (stream, gsub->FeatureList.offset);
799 READ_OFFSET (stream, gsub->LookupList.offset);
801 if (read_script_list (otf, stream, gsub->ScriptList.offset,
802 &gsub->ScriptList) < 0
803 || read_feature_list (otf, stream, gsub->FeatureList.offset,
804 &gsub->FeatureList) < 0
805 || read_lookup_list (otf, stream, gsub->LookupList.offset,
806 &gsub->LookupList, 1) < 0)
812 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
814 char *errfmt = "Sequence%s";
819 READ_UINT16 (stream, count);
820 OTF_MALLOC (*seq, count, "");
822 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
823 for (i = 0; i < count; i++)
824 READ_OFFSET (stream, (*seq)[i].offset);
825 for (i = 0; i < count; i++)
827 SEEK_STREAM (stream, offset + (*seq)[i].offset);
828 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
829 &(*seq)[i].Substitute, 0);
830 if (! (*seq)[i].GlyphCount)
837 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
838 OTF_Ligature **ligature)
840 char *errfmt = "Ligature%s";
845 READ_UINT16 (stream, count);
848 OTF_MALLOC (*ligature, count, "");
849 for (i = 0; i < count; i++)
850 READ_OFFSET (stream, (*ligature)[i].offset);
851 for (i = 0; i < count; i++)
853 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
854 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
855 (*ligature)[i].CompCount
856 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1);
857 if (! (*ligature)[i].CompCount)
864 read_ligature_set (OTF *otf, OTF_Stream *stream, long offset,
865 OTF_LigatureSet **ligset)
867 char *errfmt = "LigatureSet%s";
872 READ_UINT16 (stream, count);
875 OTF_MALLOC (*ligset, count, "");
876 for (i = 0; i < count; i++)
877 READ_OFFSET (stream, (*ligset)[i].offset);
878 for (i = 0; i < count; i++)
882 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
883 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
884 &(*ligset)[i].Ligature);
887 (*ligset)[i].LigatureCount = (unsigned) lig_count;
893 read_subst_lookup_record (OTF *otf, OTF_Stream *stream,
894 OTF_SubstLookupRecord **record)
896 char *errfmt = "SubstLookupRecord%s";
901 READ_UINT16 (stream, count);
903 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
904 OTF_MALLOC (*record, count, "");
905 for (i = 0; i < count; i++)
907 READ_UINT16 (stream, (*record)[i].SequenceIndex);
908 READ_UINT16 (stream, (*record)[i].LookupListIndex);
914 read_chain_subrule (OTF *otf, OTF_Stream *stream, long offset,
915 OTF_ChainSubRule **rule)
917 char *errfmt = "ChainSubRule%s";
922 READ_UINT16 (stream, count);
924 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
925 OTF_MALLOC (*rule, count, "");
926 for (i = 0; i < count; i++)
927 READ_OFFSET (stream, (*rule)[i].offset);
928 for (i = 0; i < count; i++)
930 SEEK_STREAM (stream, offset + (*rule)[i].offset);
931 (*rule)[i].BacktrackGlyphCount
932 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0);
933 if (! (*rule)[i].BacktrackGlyphCount)
935 (*rule)[i].InputGlyphCount
936 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1);
937 if (! (*rule)[i].InputGlyphCount)
939 (*rule)[i].LookaheadGlyphCount
940 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0);
941 if (! (*rule)[i].LookaheadGlyphCount)
943 (*rule)[i].SubstCount
944 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
945 if (! (*rule)[i].SubstCount)
953 read_chain_subrule_set (OTF *otf, OTF_Stream *stream, long offset,
954 OTF_ChainSubRuleSet **set)
956 char *errfmt = "ChainSubRuleSet%s";
961 READ_UINT16 (stream, count);
963 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
964 OTF_MALLOC (*set, count, "");
965 for (i = 0; i < count; i++)
966 READ_OFFSET (stream, (*set)[i].offset);
967 for (i = 0; i < count; i++)
969 SEEK_STREAM (stream, offset + (*set)[i].offset);
970 (*set)[i].ChainSubRuleCount
971 = read_chain_subrule (otf, stream, offset + (*set)[i].offset,
972 &(*set)[i].ChainSubRule);
973 if (! (*set)[i].ChainSubRuleCount)
980 read_chain_subclass_rule (OTF *otf, OTF_Stream *stream, long offset,
981 OTF_ChainSubClassRule **rule)
983 char *errfmt = "ChainSubClassRule%s";
988 READ_UINT16 (stream, count);
990 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
991 OTF_MALLOC (*rule, count, "");
992 for (i = 0; i < count; i++)
993 READ_OFFSET (stream, (*rule)[i].offset);
994 for (i = 0; i < count; i++)
996 SEEK_STREAM (stream, offset + (*rule)[i].offset);
997 (*rule)[i].BacktrackGlyphCount
998 = read_glyph_ids (otf, stream,
999 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0);
1000 if (! (*rule)[i].BacktrackGlyphCount)
1002 (*rule)[i].InputGlyphCount
1003 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Input, -1);
1004 if (! (*rule)[i].InputGlyphCount)
1006 (*rule)[i].LookaheadGlyphCount
1007 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].LookAhead, 0);
1008 if (! (*rule)[i].LookaheadGlyphCount)
1010 (*rule)[i].SubstCount
1011 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
1012 if (! (*rule)[i].SubstCount)
1019 read_chain_subclass_set (OTF *otf, OTF_Stream *stream, long offset,
1020 OTF_ChainSubClassSet **set)
1022 char *errfmt = "ChainSubClassSet%s";
1023 unsigned errret = 0;
1027 READ_UINT16 (stream, count);
1029 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1030 OTF_MALLOC (*set, count, "");
1031 for (i = 0; i < count; i++)
1032 READ_OFFSET (stream, (*set)[i].offset);
1033 for (i = 0; i < count; i++)
1035 SEEK_STREAM (stream, offset + (*set)[i].offset);
1036 (*set)[i].ChainSubClassRuleCnt
1037 = read_chain_subclass_rule (otf, stream, offset + (*set)[i].offset,
1038 &(*set)[i].ChainSubClassRule);
1039 if (! (*set)[i].ChainSubClassRuleCnt)
1047 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1048 unsigned type, OTF_LookupSubTable *subtable)
1050 char *errfmt = "GSUB LookupSubTable%s";
1054 SEEK_STREAM (stream, offset);
1055 READ_UINT16 (stream, subtable->Format);
1059 if (subtable->Format == 1)
1061 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1063 READ_INT16 (stream, subtable->sub.gsub.single1.DeltaGlyphID);
1065 else if (subtable->Format == 2)
1067 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1069 subtable->sub.gsub.single2.GlyphCount
1070 = read_glyph_ids (otf, stream, &subtable->sub.gsub.single2.Substitute,
1072 if (! subtable->sub.gsub.single2.GlyphCount)
1076 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1080 if (subtable->Format == 1)
1082 read_coverage (otf, stream, offset, &subtable->Coverage);
1083 subtable->sub.gsub.multiple1.SequenceCount
1084 = read_sequence (otf, stream, offset,
1085 &subtable->sub.gsub.multiple1.Sequence);
1088 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1095 if (subtable->Format == 1)
1097 read_coverage (otf, stream, offset, &subtable->Coverage);
1098 count = (read_ligature_set
1099 (otf, stream, offset,
1100 &subtable->sub.gsub.ligature1.LigatureSet));
1103 subtable->sub.gsub.ligature1.LigSetCount = (unsigned) count;
1106 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1110 if (subtable->Format == 1)
1112 read_coverage (otf, stream, offset, &subtable->Coverage);
1113 subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1114 = (read_chain_subrule_set
1115 (otf, stream, offset,
1116 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1118 else if (subtable->Format == 2)
1120 read_coverage (otf, stream, offset, &subtable->Coverage);
1121 read_class_def (otf, stream, offset,
1122 &subtable->sub.gsub.chain_context2.Backtrack);
1123 read_class_def (otf, stream, offset,
1124 &subtable->sub.gsub.chain_context2.Input);
1125 read_class_def (otf, stream, offset,
1126 &subtable->sub.gsub.chain_context2.LookAhead);
1127 subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1128 = (read_chain_subclass_set
1129 (otf, stream, offset,
1130 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1132 else if (subtable->Format == 3)
1134 count = (read_coverage_list
1135 (otf, stream, offset,
1136 &subtable->sub.gsub.chain_context3.Backtrack));
1139 subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1141 count = (read_coverage_list
1142 (otf, stream, offset,
1143 &subtable->sub.gsub.chain_context3.Input));
1146 subtable->sub.gsub.chain_context3.InputGlyphCount
1148 subtable->Coverage = subtable->sub.gsub.chain_context3.Input[0];
1149 count = (read_coverage_list
1150 (otf, stream, offset,
1151 &subtable->sub.gsub.chain_context3.LookAhead));
1152 subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1154 subtable->sub.gsub.chain_context3.SubstCount
1155 = (read_subst_lookup_record
1157 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1160 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1165 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1174 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1175 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1178 OTF_StreamState state;
1183 for (i = 0, size = 0; i < 8; i++)
1187 if (bit & OTF_XPlacement)
1188 READ_INT16 (stream, value_record->XPlacement);
1189 if (bit & OTF_XPlacement)
1190 READ_INT16 (stream, value_record->YPlacement);
1191 if (bit & OTF_XAdvance)
1192 READ_INT16 (stream, value_record->XAdvance);
1193 if (bit & OTF_YAdvance)
1194 READ_INT16 (stream, value_record->YAdvance);
1195 if (bit & OTF_XPlaDevice)
1196 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1197 if (bit & OTF_YPlaDevice)
1198 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1199 if (bit & OTF_XAdvDevice)
1200 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1201 if (bit & OTF_YAdvDevice)
1202 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1203 SAVE_STREAM (stream, state);
1204 if (value_record->XPlaDevice.offset)
1206 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1209 if (value_record->YPlaDevice.offset)
1211 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1214 if (value_record->XAdvDevice.offset)
1216 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1219 if (value_record->YAdvDevice.offset)
1221 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1224 RESTORE_STREAM (stream, state);
1230 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1232 char *errfmt = "Anchor%s";
1235 SEEK_STREAM (stream, offset + anchor->offset);
1236 READ_UINT16 (stream, anchor->AnchorFormat);
1237 READ_INT16 (stream, anchor->XCoordinate);
1238 READ_INT16 (stream, anchor->YCoordinate);
1239 if (anchor->AnchorFormat == 1)
1241 else if (anchor->AnchorFormat == 2)
1243 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1245 else if (anchor->AnchorFormat == 3)
1247 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1248 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1249 if (anchor->f.f2.XDeviceTable.offset)
1251 if (read_device_table (otf, stream, offset + anchor->offset,
1252 &anchor->f.f2.XDeviceTable) < 0)
1255 if (anchor->f.f2.YDeviceTable.offset)
1257 if (read_device_table (otf, stream, offset + anchor->offset,
1258 &anchor->f.f2.YDeviceTable) < 0)
1263 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1269 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1270 OTF_MarkArray *array)
1272 char *errfmt = "MarkArray%s";
1274 OTF_StreamState state;
1277 READ_OFFSET (stream, array->offset);
1278 SAVE_STREAM (stream, state);
1279 SEEK_STREAM (stream, offset + array->offset);
1280 READ_UINT16 (stream, array->MarkCount);
1281 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1282 for (i = 0; i < array->MarkCount; i++)
1284 READ_UINT16 (stream, array->MarkRecord[i].Class);
1285 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1287 for (i = 0; i < array->MarkCount; i++)
1288 if (read_anchor (otf, stream, offset + array->offset,
1289 &array->MarkRecord[i].MarkAnchor) < 0)
1291 RESTORE_STREAM (stream, state);
1296 read_base_array (OTF *otf, OTF_Stream *stream, long offset,
1297 unsigned ClassCount, OTF_BaseArray *array)
1299 char *errfmt = "BaseArray%s";
1301 OTF_StreamState state;
1304 READ_OFFSET (stream, array->offset);
1305 SAVE_STREAM (stream, state);
1306 SEEK_STREAM (stream, offset + array->offset);
1307 READ_UINT16 (stream, array->BaseCount);
1308 OTF_MALLOC (array->BaseRecord, array->BaseCount, "");
1309 for (i = 0; i < array->BaseCount; i++)
1311 OTF_MALLOC (array->BaseRecord[i].BaseAnchor, ClassCount,
1313 for (j = 0; j < ClassCount; j++)
1314 READ_OFFSET (stream, array->BaseRecord[i].BaseAnchor[j].offset);
1316 for (i = 0; i < array->BaseCount; i++)
1317 for (j = 0; j < ClassCount; j++)
1318 if (read_anchor (otf, stream, offset + array->offset,
1319 &array->BaseRecord[i].BaseAnchor[j]) < 0)
1321 RESTORE_STREAM (stream, state);
1326 static OTF_Class1Record *
1327 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1328 unsigned num1, enum OTF_ValueFormat bit1,
1329 unsigned num2, enum OTF_ValueFormat bit2)
1331 char *errfmt = "Class1Record%s";
1332 void *errret = NULL;
1333 OTF_Class1Record *rec;
1336 OTF_MALLOC (rec, num1, "");
1337 for (i = 0; i < num1; i++)
1339 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1340 for (j = 0; j < num2; j++)
1342 if (read_value_record (otf, stream, offset,
1343 bit1, &rec[i].Class2Record[j].Value1) < 0
1344 || read_value_record (otf, stream, offset,
1345 bit2, &rec[i].Class2Record[j].Value2) < 0)
1354 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1355 long offset, unsigned type,
1356 OTF_LookupSubTable *subtable)
1358 char *errfmt = "GPOS LookupSubTable%s";
1361 SEEK_STREAM (stream, offset);
1362 READ_UINT16 (stream, subtable->Format);
1367 if (subtable->Format == 1)
1369 read_coverage (otf, stream, offset, &subtable->Coverage);
1370 subtable->sub.gsub.single1.DeltaGlyphID = READ_INT16 (stream);
1372 else if (subtable->Format == 2)
1374 read_coverage (otf, stream, offset, &subtable->Coverage);
1375 subtable->sub.gsub.single2.GlyphCount
1376 = read_glyph_ids (otf, stream,
1377 &subtable->sub.gsub.single2.Substitute, 0);
1380 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1385 if (subtable->Format == 1)
1387 read_coverage (otf, stream, offset, &subtable->Coverage);
1389 else if (subtable->Format == 2)
1391 SEEK_STREAM (stream, offset + 2);
1392 read_coverage (otf, stream, offset, &subtable->Coverage);
1393 READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat1);
1394 READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat2);
1395 read_class_def (otf, stream, offset,
1396 &subtable->sub.gpos.pair2.ClassDef1);
1397 read_class_def (otf, stream, offset,
1398 &subtable->sub.gpos.pair2.ClassDef2);
1399 READ_UINT16 (stream, subtable->sub.gpos.pair2.Class1Count);
1400 READ_UINT16 (stream, subtable->sub.gpos.pair2.Class2Count);
1401 subtable->sub.gpos.pair2.Class1Record
1402 = read_class1_record_list (otf, stream, offset,
1403 subtable->sub.gpos.pair2.Class1Count,
1404 subtable->sub.gpos.pair2.ValueFormat1,
1405 subtable->sub.gpos.pair2.Class2Count,
1406 subtable->sub.gpos.pair2.ValueFormat2);
1409 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1413 if (subtable->Format == 1)
1415 read_coverage (otf, stream, offset, &subtable->Coverage);
1416 read_coverage (otf, stream, offset,
1417 &subtable->sub.gpos.mark_base1.BaseCoverage);
1418 READ_UINT16 (stream, subtable->sub.gpos.mark_base1.ClassCount);
1419 read_mark_array (otf, stream, offset,
1420 &subtable->sub.gpos.mark_base1.MarkArray);
1421 read_base_array (otf, stream, offset,
1422 subtable->sub.gpos.mark_base1.ClassCount,
1423 &subtable->sub.gpos.mark_base1.BaseArray);
1426 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1431 if (subtable->Format == 1)
1433 read_coverage (otf, stream, offset,
1434 &subtable->sub.gsub.chain_context1.Coverage);
1435 subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1436 = (read_chain_subrule_set
1437 (otf, stream, offset,
1438 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1440 else if (subtable->Format == 2)
1442 read_coverage (otf, stream, offset,
1443 &subtable->sub.gsub.chain_context2.Coverage);
1444 read_class_def (otf, stream, offset,
1445 &subtable->sub.gsub.chain_context2.Backtrack);
1446 read_class_def (otf, stream, offset,
1447 &subtable->sub.gsub.chain_context2.Input);
1448 read_class_def (otf, stream, offset,
1449 &subtable->sub.gsub.chain_context2.LookAhead);
1450 subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1451 = (read_chain_subclass_set
1452 (otf, stream, offset,
1453 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1455 else if (subtable->Format == 3)
1457 subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1458 = (read_coverage_list
1459 (otf, stream, offset,
1460 &subtable->sub.gsub.chain_context3.Backtrack));
1461 subtable->sub.gsub.chain_context3.InputGlyphCount
1462 = (read_coverage_list
1463 (otf, stream, offset,
1464 &subtable->sub.gsub.chain_context3.Input));
1465 subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1466 = (read_coverage_list
1467 (otf, stream, offset,
1468 &subtable->sub.gsub.chain_context3.LookAhead));
1469 subtable->sub.gsub.chain_context3.SubstCount
1470 = (read_subst_lookup_record
1472 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1475 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1479 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1486 read_gpos_table (OTF *otf, OTF_Stream *stream)
1488 char *errfmt = "GPOS%s";
1489 void *errret = NULL;
1492 OTF_CALLOC (gpos, 1, "");
1493 READ_FIXED (stream, gpos->Version);
1494 READ_OFFSET (stream, gpos->ScriptList.offset);
1495 READ_OFFSET (stream, gpos->FeatureList.offset);
1496 READ_OFFSET (stream, gpos->LookupList.offset);
1498 if (read_script_list (otf, stream, gpos->ScriptList.offset,
1499 &gpos->ScriptList) < 0
1500 || read_feature_list (otf, stream, gpos->FeatureList.offset,
1501 &gpos->FeatureList) < 0
1502 || read_lookup_list (otf, stream, gpos->LookupList.offset,
1503 &gpos->LookupList, 0) < 0)
1513 read_base_table (OTF_Stream *stream, long offset)
1517 OTF_MALLOC (base, 1);
1526 read_jstf_table (OTF_Stream *stream, long offset)
1530 OTF_MALLOC (jstf, 1);
1538 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1539 OTF_AttachList *list)
1541 char *errfmt = "AttachList%s";
1545 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1547 READ_UINT16 (stream, list->GlyphCount);
1548 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1549 for (i = 0; i < list->GlyphCount; i++)
1550 READ_OFFSET (stream, list->AttachPoint[i].offset);
1551 for (i = 0; i < list->GlyphCount; i++)
1555 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1556 READ_UINT16 (stream, count);
1557 list->AttachPoint[i].PointCount = count;
1558 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1559 for (j = 0; j < count; j++)
1560 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1566 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1567 OTF_CaretValue *caret)
1569 char *errfmt = "CaretValue%s";
1572 SEEK_STREAM (stream, offset + caret->offset);
1573 READ_UINT16 (stream, caret->CaretValueFormat);
1574 if (caret->CaretValueFormat == 1)
1575 READ_INT16 (stream, caret->f.f1.Coordinate);
1576 else if (caret->CaretValueFormat == 2)
1577 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1578 else if (caret->CaretValueFormat == 3)
1580 READ_INT16 (stream, caret->f.f3.Coordinate);
1581 if (read_device_table (otf, stream, offset + caret->offset,
1582 &caret->f.f3.DeviceTable) < 0)
1586 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1591 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1592 OTF_LigCaretList *list)
1594 char *errfmt = "LigCaretList%s";
1598 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1600 READ_UINT16 (stream, list->LigGlyphCount);
1601 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1602 for (i = 0; i < list->LigGlyphCount; i++)
1603 READ_OFFSET (stream, list->LigGlyph[i].offset);
1604 for (i = 0; i < list->LigGlyphCount; i++)
1608 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1609 READ_UINT16 (stream, count);
1610 list->LigGlyph[i].CaretCount = count;
1611 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1612 for (j = 0; j < count; j++)
1613 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1614 for (j = 0; j < count; j++)
1615 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1616 &list->LigGlyph[i].CaretValue[j]) < 0)
1623 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1627 READ_FIXED (stream, header->Version);
1628 READ_OFFSET (stream, header->GlyphClassDef);
1629 READ_OFFSET (stream, header->AttachList);
1630 READ_OFFSET (stream, header->LigCaretList);
1631 READ_OFFSET (stream, header->MarkAttachClassDef);
1636 read_gdef_table (OTF *otf, OTF_Stream *stream)
1638 char *errfmt = "GDEF%s";
1639 void *errret = NULL;
1642 OTF_CALLOC (gdef, 1, "");
1643 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1644 if (gdef->header.GlyphClassDef)
1646 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1647 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1649 if (gdef->header.AttachList)
1650 read_attach_list (otf, stream, gdef->header.AttachList,
1651 &gdef->attach_list);
1652 if (gdef->header.LigCaretList)
1653 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1654 &gdef->lig_caret_list);
1655 if (gdef->header.MarkAttachClassDef)
1657 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1658 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1669 read_cmap_table (OTF *otf, OTF_Stream *stream)
1671 char *errfmt = "cmap%s";
1672 void *errret = NULL;
1676 OTF_CALLOC (cmap, 1, "");
1677 READ_USHORT (stream, cmap->version);
1678 READ_USHORT (stream, cmap->numTables);
1679 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
1680 for (i = 0; i < cmap->numTables; i++)
1682 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
1683 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
1684 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
1685 if (cmap->EncodingRecord[i].platformID == 3
1686 && cmap->EncodingRecord[i].encodingID == 1)
1687 cmap->Unicode = cmap->EncodingRecord + i;
1689 for (i = 0; i < cmap->numTables; i++)
1693 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
1694 READ_USHORT (stream, format);
1695 cmap->EncodingRecord[i].subtable.format = format;
1696 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
1697 if (format == 8 || format == 10 || format == 12)
1699 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
1700 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
1704 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
1710 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
1711 " (EncodingRecord)");
1713 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
1723 OTF_EncodingSubtable4 *sub4;
1728 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
1729 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
1730 READ_USHORT (stream, sub4->segCountX2);
1731 segCount = sub4->segCountX2 / 2;
1732 READ_USHORT (stream, sub4->searchRange);
1733 READ_USHORT (stream, sub4->entrySelector);
1734 READ_USHORT (stream, sub4->rangeShift);
1735 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
1736 for (j = 0; j < segCount; j++)
1737 READ_USHORT (stream, sub4->segments[j].endCount);
1738 READ_USHORT (stream, dummy);
1739 for (j = 0; j < segCount; j++)
1740 READ_USHORT (stream, sub4->segments[j].startCount);
1741 for (j = 0; j < segCount; j++)
1742 READ_SHORT (stream, sub4->segments[j].idDelta);
1743 for (j = 0; j < segCount; j++)
1746 unsigned rest = 2 * (segCount - j);
1748 READ_USHORT (stream, off);
1750 sub4->segments[j].idRangeOffset = 0xFFFF;
1752 sub4->segments[j].idRangeOffset = (off - rest) / 2;
1754 j = (cmap->EncodingRecord[i].subtable.length
1755 - (14 + 2 * (segCount * 4 + 1)));
1756 sub4->GlyphCount = j / 2;
1757 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
1758 for (j = 0; j < sub4->GlyphCount; j++)
1759 READ_USHORT (stream, sub4->glyphIdArray[j]);
1771 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
1773 char *errfmt = "nameID (%d)";
1774 void *errret = NULL;
1775 OTF_StreamState state;
1780 SAVE_STREAM (stream, state);
1781 SEEK_STREAM (stream, stream->pos + rec->offset);
1785 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
1786 READ_BYTES (stream, str, rec->length);
1787 for (i = 0; i < rec->length; i++)
1791 else if (bytes == 2)
1793 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
1794 for (i = 0; i < rec->length / 2; i++)
1796 READ_USHORT (stream, c);
1802 else if (bytes == 4)
1804 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
1805 for (i = 0; i < rec->length / 4; i++)
1807 READ_ULONG (stream, c);
1814 RESTORE_STREAM (stream, state);
1819 read_name_table (OTF *otf, OTF_Stream *stream)
1821 char *errfmt = "name%s";
1822 void *errret = NULL;
1826 OTF_CALLOC (name, 1, "");
1827 READ_USHORT (stream, name->format);
1828 READ_USHORT (stream, name->count);
1829 READ_USHORT (stream, name->stringOffset);
1830 OTF_MALLOC (name->nameRecord, name->count, "");
1831 for (i = 0; i < name->count; i++)
1833 OTF_NameRecord *rec = name->nameRecord + i;
1835 READ_USHORT (stream, rec->platformID);
1836 READ_USHORT (stream, rec->encodingID);
1837 READ_USHORT (stream, rec->languageID);
1838 READ_USHORT (stream, rec->nameID);
1839 READ_USHORT (stream, rec->length);
1840 READ_USHORT (stream, rec->offset);
1842 for (i = 0; i < name->count; i++)
1844 OTF_NameRecord *rec = name->nameRecord + i;
1845 int nameID = rec->nameID;
1847 if (nameID <= OTF_max_nameID
1848 && ! name->name[nameID])
1850 if (rec->platformID == 0)
1851 name->name[nameID] = read_name (otf, stream, rec,
1852 rec->encodingID <= 3 ? 2 : 4);
1853 else if (rec->platformID == 1
1854 && rec->encodingID == 0)
1855 name->name[nameID] = read_name (otf, stream, rec, 1);
1856 else if (rec->platformID == 3
1857 && (rec->encodingID == 1 || rec->encodingID == 10))
1858 name->name[nameID] = read_name (otf, stream,
1859 rec, rec->encodingID == 1 ? 2 : 4);
1870 /* We can't use memory allocation macros in the following functions
1871 because those macros returns from the functions before freeing
1872 memory previously allocated. */
1875 otf_open (char *otf_name)
1878 char *errfmt = "opening otf (%s)";
1879 void *errret = NULL;
1882 fp = fopen (otf_name, "r");
1884 OTF_ERROR (OTF_ERROR_FILE, otf_name);
1885 otf = calloc (1, sizeof (OTF));
1887 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
1888 otf->filename = strdup (otf_name);
1889 if (! otf->filename)
1893 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
1896 if (read_header_part (otf, fp) < 0)
1909 otf_close (OTF *otf)
1911 OTF_InternalData *internal_data = otf->internal_data;
1915 free (otf->filename);
1916 if (otf->table_dirs)
1917 free (otf->table_dirs);
1920 OTF_MemoryRecord *memrec = internal_data->memory_record;
1922 if (internal_data->header_stream)
1923 free_stream (internal_data->header_stream);
1925 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
1926 if (internal_data->table_info[i].stream)
1927 free_stream (internal_data->table_info[i].stream);
1931 OTF_MemoryRecord *next = memrec->next;
1933 for (i = 0; i < memrec->used; i++)
1934 free (memrec->memory[i]);
1938 free (internal_data);
1944 otf_get_table (OTF *otf, char *name)
1946 char *errfmt = "OTF Table Read";
1948 OTF_InternalData *internal_data = otf->internal_data;
1949 OTF_TableInfo *table_info;
1950 OTF_Tag tag = otf_tag (name);
1953 OTF_ERROR (OTF_ERROR_TABLE, " (unknown)");
1955 if (tag == otf_tag ("head"))
1956 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
1957 else if (tag == otf_tag ("name"))
1958 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
1959 else if (tag == otf_tag ("cmap"))
1960 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
1961 else if (tag == otf_tag ("GDEF"))
1962 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
1963 else if (tag == otf_tag ("GSUB"))
1964 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
1965 else if (tag == otf_tag ("GPOS"))
1966 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
1968 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported)");
1970 if (*table_info->address)
1972 if (! table_info->stream)
1973 OTF_ERROR (OTF_ERROR_TABLE, " (not found)");
1974 if (! table_info->reader)
1975 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
1977 *table_info->address = (*table_info->reader) (otf, table_info->stream);
1978 free_stream (table_info->stream);
1979 table_info->stream = NULL;
1980 if (! *table_info->address)
1982 table_info->reader = NULL;
1983 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");