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;
212 allocate_memory_record (OTF *otf)
214 OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
215 OTF_MemoryRecord *mem_rec = calloc (1, sizeof (OTF_MemoryRecord));
220 mem_rec->next = internal_data->memory_record;
221 internal_data->memory_record = mem_rec;
225 /* Memory allocation macros. */
227 #define OTF_MALLOC(p, size, arg) \
229 (p) = malloc (sizeof (*(p)) * (size)); \
231 || ((((OTF_InternalData *) otf->internal_data)->memory_record->used \
232 >= OTF_MEMORY_RECORD_SIZE) \
233 && (allocate_memory_record (otf) < 0))) \
234 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
235 ((OTF_InternalData *) otf->internal_data)->memory_record->memory \
236 [((OTF_InternalData *) otf->internal_data)->memory_record->used++] \
241 #define OTF_CALLOC(p, size, arg) \
243 (p) = calloc ((size), sizeof (*(p))); \
245 || ((((OTF_InternalData *) otf->internal_data)->memory_record->used \
246 >= OTF_MEMORY_RECORD_SIZE) \
247 && (allocate_memory_record (otf) < 0))) \
248 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
249 ((OTF_InternalData *) otf->internal_data)->memory_record->memory \
250 [((OTF_InternalData *) otf->internal_data)->memory_record->used++] \
258 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
262 READ_FIXED (stream, table->sfnt_version);
263 READ_USHORT (stream, table->numTables);
264 READ_USHORT (stream, table->searchRange);
265 READ_USHORT (stream, table->enterSelector);
266 READ_USHORT (stream, table->rangeShift);
271 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
276 READ_TAG (stream, tag);
278 table->name[0] = tag >> 24;
279 table->name[1] = (tag >> 16) & 0xFF;
280 table->name[0] = (tag >> 8) & 0xFF;
281 table->name[0] = tag >> 8;
282 table->name[0] = '\0';
283 READ_ULONG (stream, table->checkSum);
284 READ_ULONG (stream, table->offset);
285 READ_ULONG (stream, table->length);
290 read_header_part (OTF *otf, FILE *fp)
292 char *errfmt = "otf header%s";
294 OTF_InternalData *internal_data;
295 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
299 OTF_CALLOC (internal_data, 1, " (InternalData");
300 otf->internal_data = internal_data;
301 internal_data->table_info[OTF_TABLE_TYPE_HEAD].address = &otf->head;
302 internal_data->table_info[OTF_TABLE_TYPE_HEAD].reader = read_head_table;
303 internal_data->table_info[OTF_TABLE_TYPE_NAME].address = &otf->name;
304 internal_data->table_info[OTF_TABLE_TYPE_NAME].reader = read_name_table;
305 internal_data->table_info[OTF_TABLE_TYPE_CMAP].address = &otf->cmap;
306 internal_data->table_info[OTF_TABLE_TYPE_CMAP].reader = read_cmap_table;
307 internal_data->table_info[OTF_TABLE_TYPE_GDEF].address = &otf->gdef;
308 internal_data->table_info[OTF_TABLE_TYPE_GDEF].reader = read_gdef_table;
309 internal_data->table_info[OTF_TABLE_TYPE_GSUB].address = &otf->gsub;
310 internal_data->table_info[OTF_TABLE_TYPE_GSUB].reader = read_gsub_table;
311 internal_data->table_info[OTF_TABLE_TYPE_GPOS].address = &otf->gpos;
312 internal_data->table_info[OTF_TABLE_TYPE_GPOS].reader = read_gpos_table;
314 if (allocate_memory_record (otf) < 0)
315 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
317 head_tag = otf_tag ("head");
318 name_tag = otf_tag ("name");
319 cmap_tag = otf_tag ("cmap");
320 gdef_tag = otf_tag ("GDEF");
321 gsub_tag = otf_tag ("GSUB");
322 gpos_tag = otf_tag ("GPOS");
324 stream = make_stream ();
328 internal_data->header_stream = stream;
330 /* Size of Offset Table is 12 bytes. */
331 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
333 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
336 /* Size of each Table Directory is 16 bytes. */
337 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
338 "Table Directory") < 0)
341 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
342 for (i = 0; i < otf->offset_table.numTables; i++)
344 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
345 OTF_TableInfo *table_info = NULL;
350 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
351 else if (tag == name_tag)
352 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
353 else if (tag == cmap_tag)
354 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
355 else if (tag == gdef_tag)
356 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
357 else if (tag == gsub_tag)
358 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
359 else if (tag == gpos_tag)
360 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
364 table_info->stream = make_stream ();
365 if (setup_stream (table_info->stream, fp,
366 otf->table_dirs[i].offset,
367 otf->table_dirs[i].length,
368 otf->table_dirs[i].name) < 0)
373 internal_data->header_stream = NULL;
374 free_stream (stream);
382 read_head_table (OTF *otf, OTF_Stream *stream)
384 char *errfmt = "head%s";
388 OTF_CALLOC (head, 1, "");
389 READ_FIXED (stream, head->TableVersionNumber);
390 READ_FIXED (stream, head->fontRevision);
391 READ_ULONG (stream, head->checkSumAdjustment);
392 READ_ULONG (stream, head->magicNumber);
393 READ_USHORT (stream, head->flags);
394 READ_USHORT (stream, head->unitsPerEm);
402 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
403 OTF_ScriptList *list)
405 char *errfmt = "Script List%s";
409 SEEK_STREAM (stream, offset);
410 READ_USHORT (stream, list->ScriptCount);
411 OTF_CALLOC (list->Script, list->ScriptCount, "");
413 for (i = 0; i < list->ScriptCount; i++)
415 READ_TAG (stream, list->Script[i].ScriptTag);
416 READ_OFFSET (stream, list->Script[i].offset);
418 for (i = 0; i < list->ScriptCount; i++)
420 OTF_Script *script = list->Script + i;
421 long script_offset = offset + script->offset;
423 SEEK_STREAM (stream, script_offset);
424 READ_OFFSET (stream, script->DefaultLangSysOffset);
425 READ_USHORT (stream, script->LangSysCount);
426 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
427 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
428 for (j = 0; j < script->LangSysCount; j++)
430 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
431 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
434 if (script->DefaultLangSysOffset)
436 OTF_LangSys *langsys = &script->DefaultLangSys;
438 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
439 READ_OFFSET (stream, langsys->LookupOrder);
440 READ_USHORT (stream, langsys->ReqFeatureIndex);
441 READ_USHORT (stream, langsys->FeatureCount);
442 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
444 for (k = 0; k < langsys->FeatureCount; k++)
445 READ_USHORT (stream, langsys->FeatureIndex[k]);
448 for (j = 0; j < script->LangSysCount; j++)
450 OTF_LangSys *langsys = script->LangSys + j;
453 script_offset + script->LangSysRecord[j].LangSys);
454 READ_OFFSET (stream, langsys->LookupOrder);
455 READ_USHORT (stream, langsys->ReqFeatureIndex);
456 READ_USHORT (stream, langsys->FeatureCount);
457 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
459 for (k = 0; k < langsys->FeatureCount; k++)
460 READ_USHORT (stream, langsys->FeatureIndex[k]);
468 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
469 OTF_FeatureList *list)
471 char *errfmt = "Feature List%s";
475 READ_UINT16 (stream, list->FeatureCount);
476 OTF_CALLOC (list->Feature, list->FeatureCount, "");
477 for (i = 0; i < list->FeatureCount; i++)
479 READ_TAG (stream, list->Feature[i].FeatureTag);
480 READ_OFFSET (stream, list->Feature[i].offset);
482 for (i = 0; i < list->FeatureCount; i++)
484 OTF_Feature *feature = list->Feature + i;
486 SEEK_STREAM (stream, offset + feature->offset);
487 READ_OFFSET (stream, feature->FeatureParams);
488 READ_UINT16 (stream, feature->LookupCount);
489 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
490 " (LookupListIndex)");
491 for (j = 0; j < feature->LookupCount; j++)
492 READ_UINT16 (stream, feature->LookupListIndex[j]);
498 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
499 long offset, unsigned type,
500 OTF_LookupSubTable *subtable);
501 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
502 long offset, unsigned type,
503 OTF_LookupSubTable *subtable);
506 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
507 OTF_LookupList *list, int gsub)
509 char *errfmt = "Lookup List%s";
513 SEEK_STREAM (stream, offset);
514 READ_UINT16 (stream, list->LookupCount);
515 OTF_CALLOC (list->Lookup, list->LookupCount, "");
517 for (i = 0; i < list->LookupCount; i++)
518 READ_OFFSET (stream, list->Lookup[i].offset);
519 for (i = 0; i < list->LookupCount; i++)
521 OTF_Lookup *lookup = list->Lookup + i;
523 SEEK_STREAM (stream, offset + lookup->offset);
524 READ_UINT16 (stream, lookup->LookupType);
525 READ_UINT16 (stream, lookup->LookupFlag);
526 READ_UINT16 (stream, lookup->SubTableCount);
527 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
528 " (SubTableOffset)");
529 OTF_CALLOC (lookup->SubTable, lookup->SubTableCount,
531 for (j = 0; j < lookup->SubTableCount; j++)
532 READ_OFFSET (stream, lookup->SubTableOffset[j]);
534 for (j = 0; j < lookup->SubTableCount; j++)
537 = offset + lookup->offset + lookup->SubTableOffset[j];
539 if (read_lookup_subtable_gsub (otf, stream, this_offset,
541 lookup->SubTable + j) < 0)
545 for (j = 0; j < lookup->SubTableCount; j++)
548 = offset + lookup->offset + lookup->SubTableOffset[j];
550 if (read_lookup_subtable_gpos (otf, stream, this_offset,
552 lookup->SubTable + j) < 0)
562 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids, int minus)
564 char *errfmt = "GlyphID List%s";
569 READ_UINT16 (stream, count);
572 OTF_MALLOC (*ids, count, "");
573 for (i = 0; i < count + minus; i++)
574 READ_GLYPHID (stream, (*ids)[i]);
579 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
581 char *errfmt = "RangeRecord%s";
586 READ_UINT16 (stream, count);
589 OTF_MALLOC (*record, count, "");
590 for (i = 0; i < count; i++)
592 READ_GLYPHID (stream, (*record)[i].Start);
593 READ_GLYPHID (stream, (*record)[i].End);
594 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
601 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
602 OTF_Coverage *coverage)
604 char *errfmt = "Coverage%s";
606 OTF_StreamState state;
609 READ_OFFSET (stream, coverage->offset);
610 SAVE_STREAM (stream, state);
611 SEEK_STREAM (stream, offset + coverage->offset);
612 READ_UINT16 (stream, coverage->CoverageFormat);
613 if (coverage->CoverageFormat == 1)
614 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0);
615 else if (coverage->CoverageFormat == 2)
616 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
618 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
621 coverage->Count = (unsigned) count;
622 RESTORE_STREAM (stream, state);
627 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
628 OTF_Coverage **coverage)
630 char *errfmt = "Coverage List%s";
635 READ_UINT16 (stream, count);
638 OTF_MALLOC (*coverage, count, "");
639 for (i = 0; i < count; i++)
640 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
647 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
650 char *errfmt = "ClassDef%s";
653 SEEK_STREAM (stream, class->offset);
654 READ_UINT16 (stream, class->ClassFormat);
655 if (class->ClassFormat == 1)
657 READ_GLYPHID (stream, class->f.f1.StartGlyph);
658 class->f.f1.GlyphCount
660 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
661 if (! class->f.f1.GlyphCount)
662 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
664 else if (class->ClassFormat == 2)
666 class->f.f2.ClassRangeCount
667 = (read_range_records
668 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
669 if (! class->f.f2.ClassRangeCount)
670 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
673 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
679 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
681 char *errfmt = "ClassDef%s";
683 OTF_StreamState state;
685 READ_OFFSET (stream, class->offset);
686 SAVE_STREAM (stream, state);
687 SEEK_STREAM (stream, offset + class->offset);
688 READ_UINT16 (stream, class->ClassFormat);
689 if (class->ClassFormat == 1)
691 READ_GLYPHID (stream, class->f.f1.StartGlyph);
692 class->f.f1.GlyphCount
694 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
695 if (! class->f.f1.GlyphCount)
698 else if (class->ClassFormat == 2)
700 class->f.f2.ClassRangeCount
701 = (read_range_records
702 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
703 if (! class->f.f2.ClassRangeCount)
707 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
709 RESTORE_STREAM (stream, state);
715 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
716 OTF_DeviceTable *table)
718 char *errfmt = "Device Table%s";
729 SEEK_STREAM (stream, offset + table->offset);
730 READ_UINT16 (stream, table->StartSize);
731 READ_UINT16 (stream, table->EndSize);
732 READ_UINT16 (stream, table->DeltaFormat);
733 num = table->EndSize - table->StartSize + 1;
734 OTF_MALLOC (table->DeltaValue, num, "");
736 if (table->DeltaFormat == 1)
737 for (i = 0; i < num; i++)
740 READ_UINT16 (stream, val);
741 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
742 table->DeltaValue[i] = intval.int2;
744 else if (table->DeltaFormat == 2)
745 for (i = 0; i < num; i++)
748 READ_UINT16 (stream, val);
749 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
750 table->DeltaValue[i] = intval.int4;
752 else if (table->DeltaFormat == 3)
753 for (i = 0; i < num; i++)
757 READ_UINT16 (stream, val);
758 intval.int8 = val >> 8;
759 table->DeltaValue[i] = intval.int8;
763 intval.int8 = val >> 8;
764 table->DeltaValue[i] = intval.int8;
768 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
776 read_gsub_table (OTF *otf, OTF_Stream *stream)
778 char *errfmt = "GSUB%s";
782 OTF_CALLOC (gsub, 1, "");
783 READ_FIXED (stream, gsub->Version);
784 READ_OFFSET (stream, gsub->ScriptList.offset);
785 READ_OFFSET (stream, gsub->FeatureList.offset);
786 READ_OFFSET (stream, gsub->LookupList.offset);
788 if (read_script_list (otf, stream, gsub->ScriptList.offset,
789 &gsub->ScriptList) < 0
790 || read_feature_list (otf, stream, gsub->FeatureList.offset,
791 &gsub->FeatureList) < 0
792 || read_lookup_list (otf, stream, gsub->LookupList.offset,
793 &gsub->LookupList, 1) < 0)
799 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
801 char *errfmt = "Sequence%s";
806 READ_UINT16 (stream, count);
807 OTF_MALLOC (*seq, count, "");
809 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
810 for (i = 0; i < count; i++)
811 READ_OFFSET (stream, (*seq)[i].offset);
812 for (i = 0; i < count; i++)
814 SEEK_STREAM (stream, offset + (*seq)[i].offset);
815 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
816 &(*seq)[i].Substitute, 0);
817 if (! (*seq)[i].GlyphCount)
824 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
825 OTF_Ligature **ligature)
827 char *errfmt = "Ligature%s";
832 READ_UINT16 (stream, count);
835 OTF_MALLOC (*ligature, count, "");
836 for (i = 0; i < count; i++)
837 READ_OFFSET (stream, (*ligature)[i].offset);
838 for (i = 0; i < count; i++)
840 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
841 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
842 (*ligature)[i].CompCount
843 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1);
844 if (! (*ligature)[i].CompCount)
851 read_ligature_set (OTF *otf, OTF_Stream *stream, long offset,
852 OTF_LigatureSet **ligset)
854 char *errfmt = "LigatureSet%s";
859 READ_UINT16 (stream, count);
862 OTF_MALLOC (*ligset, count, "");
863 for (i = 0; i < count; i++)
864 READ_OFFSET (stream, (*ligset)[i].offset);
865 for (i = 0; i < count; i++)
869 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
870 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
871 &(*ligset)[i].Ligature);
874 (*ligset)[i].LigatureCount = (unsigned) lig_count;
880 read_subst_lookup_record (OTF *otf, OTF_Stream *stream,
881 OTF_SubstLookupRecord **record)
883 char *errfmt = "SubstLookupRecord%s";
888 READ_UINT16 (stream, count);
890 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
891 OTF_MALLOC (*record, count, "");
892 for (i = 0; i < count; i++)
894 READ_UINT16 (stream, (*record)[i].SequenceIndex);
895 READ_UINT16 (stream, (*record)[i].LookupListIndex);
901 read_chain_subrule (OTF *otf, OTF_Stream *stream, long offset,
902 OTF_ChainSubRule **rule)
904 char *errfmt = "ChainSubRule%s";
909 READ_UINT16 (stream, count);
911 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
912 OTF_MALLOC (*rule, count, "");
913 for (i = 0; i < count; i++)
914 READ_OFFSET (stream, (*rule)[i].offset);
915 for (i = 0; i < count; i++)
917 SEEK_STREAM (stream, offset + (*rule)[i].offset);
918 (*rule)[i].BacktrackGlyphCount
919 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0);
920 if (! (*rule)[i].BacktrackGlyphCount)
922 (*rule)[i].InputGlyphCount
923 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1);
924 if (! (*rule)[i].InputGlyphCount)
926 (*rule)[i].LookaheadGlyphCount
927 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0);
928 if (! (*rule)[i].LookaheadGlyphCount)
930 (*rule)[i].SubstCount
931 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
932 if (! (*rule)[i].SubstCount)
940 read_chain_subrule_set (OTF *otf, OTF_Stream *stream, long offset,
941 OTF_ChainSubRuleSet **set)
943 char *errfmt = "ChainSubRuleSet%s";
948 READ_UINT16 (stream, count);
950 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
951 OTF_MALLOC (*set, count, "");
952 for (i = 0; i < count; i++)
953 READ_OFFSET (stream, (*set)[i].offset);
954 for (i = 0; i < count; i++)
956 SEEK_STREAM (stream, offset + (*set)[i].offset);
957 (*set)[i].ChainSubRuleCount
958 = read_chain_subrule (otf, stream, offset + (*set)[i].offset,
959 &(*set)[i].ChainSubRule);
960 if (! (*set)[i].ChainSubRuleCount)
967 read_chain_subclass_rule (OTF *otf, OTF_Stream *stream, long offset,
968 OTF_ChainSubClassRule **rule)
970 char *errfmt = "ChainSubClassRule%s";
975 READ_UINT16 (stream, count);
977 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
978 OTF_MALLOC (*rule, count, "");
979 for (i = 0; i < count; i++)
980 READ_OFFSET (stream, (*rule)[i].offset);
981 for (i = 0; i < count; i++)
983 SEEK_STREAM (stream, offset + (*rule)[i].offset);
984 (*rule)[i].BacktrackGlyphCount
985 = read_glyph_ids (otf, stream,
986 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0);
987 if (! (*rule)[i].BacktrackGlyphCount)
989 (*rule)[i].InputGlyphCount
990 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Input, -1);
991 if (! (*rule)[i].InputGlyphCount)
993 (*rule)[i].LookaheadGlyphCount
994 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].LookAhead, 0);
995 if (! (*rule)[i].LookaheadGlyphCount)
997 (*rule)[i].SubstCount
998 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
999 if (! (*rule)[i].SubstCount)
1006 read_chain_subclass_set (OTF *otf, OTF_Stream *stream, long offset,
1007 OTF_ChainSubClassSet **set)
1009 char *errfmt = "ChainSubClassSet%s";
1010 unsigned errret = 0;
1014 READ_UINT16 (stream, count);
1016 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1017 OTF_MALLOC (*set, count, "");
1018 for (i = 0; i < count; i++)
1019 READ_OFFSET (stream, (*set)[i].offset);
1020 for (i = 0; i < count; i++)
1022 SEEK_STREAM (stream, offset + (*set)[i].offset);
1023 (*set)[i].ChainSubClassRuleCnt
1024 = read_chain_subclass_rule (otf, stream, offset + (*set)[i].offset,
1025 &(*set)[i].ChainSubClassRule);
1026 if (! (*set)[i].ChainSubClassRuleCnt)
1034 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1035 unsigned type, OTF_LookupSubTable *subtable)
1037 char *errfmt = "GSUB LookupSubTable%s";
1041 SEEK_STREAM (stream, offset);
1042 READ_UINT16 (stream, subtable->Format);
1046 if (subtable->Format == 1)
1048 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1050 READ_INT16 (stream, subtable->sub.gsub.single1.DeltaGlyphID);
1052 else if (subtable->Format == 2)
1054 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1056 subtable->sub.gsub.single2.GlyphCount
1057 = read_glyph_ids (otf, stream, &subtable->sub.gsub.single2.Substitute,
1059 if (! subtable->sub.gsub.single2.GlyphCount)
1063 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1067 if (subtable->Format == 1)
1069 read_coverage (otf, stream, offset, &subtable->Coverage);
1070 subtable->sub.gsub.multiple1.SequenceCount
1071 = read_sequence (otf, stream, offset,
1072 &subtable->sub.gsub.multiple1.Sequence);
1075 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1082 if (subtable->Format == 1)
1084 read_coverage (otf, stream, offset, &subtable->Coverage);
1085 count = (read_ligature_set
1086 (otf, stream, offset,
1087 &subtable->sub.gsub.ligature1.LigatureSet));
1090 subtable->sub.gsub.ligature1.LigSetCount = (unsigned) count;
1093 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1097 if (subtable->Format == 1)
1099 read_coverage (otf, stream, offset, &subtable->Coverage);
1100 subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1101 = (read_chain_subrule_set
1102 (otf, stream, offset,
1103 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1105 else if (subtable->Format == 2)
1107 read_coverage (otf, stream, offset, &subtable->Coverage);
1108 read_class_def (otf, stream, offset,
1109 &subtable->sub.gsub.chain_context2.Backtrack);
1110 read_class_def (otf, stream, offset,
1111 &subtable->sub.gsub.chain_context2.Input);
1112 read_class_def (otf, stream, offset,
1113 &subtable->sub.gsub.chain_context2.LookAhead);
1114 subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1115 = (read_chain_subclass_set
1116 (otf, stream, offset,
1117 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1119 else if (subtable->Format == 3)
1121 count = (read_coverage_list
1122 (otf, stream, offset,
1123 &subtable->sub.gsub.chain_context3.Backtrack));
1126 subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1128 count = (read_coverage_list
1129 (otf, stream, offset,
1130 &subtable->sub.gsub.chain_context3.Input));
1133 subtable->sub.gsub.chain_context3.InputGlyphCount
1135 subtable->Coverage = subtable->sub.gsub.chain_context3.Input[0];
1136 count = (read_coverage_list
1137 (otf, stream, offset,
1138 &subtable->sub.gsub.chain_context3.LookAhead));
1139 subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1141 subtable->sub.gsub.chain_context3.SubstCount
1142 = (read_subst_lookup_record
1144 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1147 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1152 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1161 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1162 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1165 OTF_StreamState state;
1170 for (i = 0, size = 0; i < 8; i++)
1174 if (bit & OTF_XPlacement)
1175 READ_INT16 (stream, value_record->XPlacement);
1176 if (bit & OTF_XPlacement)
1177 READ_INT16 (stream, value_record->YPlacement);
1178 if (bit & OTF_XAdvance)
1179 READ_INT16 (stream, value_record->XAdvance);
1180 if (bit & OTF_YAdvance)
1181 READ_INT16 (stream, value_record->YAdvance);
1182 if (bit & OTF_XPlaDevice)
1183 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1184 if (bit & OTF_YPlaDevice)
1185 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1186 if (bit & OTF_XAdvDevice)
1187 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1188 if (bit & OTF_YAdvDevice)
1189 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1190 SAVE_STREAM (stream, state);
1191 if (value_record->XPlaDevice.offset)
1193 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1196 if (value_record->YPlaDevice.offset)
1198 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1201 if (value_record->XAdvDevice.offset)
1203 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1206 if (value_record->YAdvDevice.offset)
1208 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1211 RESTORE_STREAM (stream, state);
1217 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1219 char *errfmt = "Anchor%s";
1222 SEEK_STREAM (stream, offset + anchor->offset);
1223 READ_UINT16 (stream, anchor->AnchorFormat);
1224 READ_INT16 (stream, anchor->XCoordinate);
1225 READ_INT16 (stream, anchor->YCoordinate);
1226 if (anchor->AnchorFormat == 1)
1228 else if (anchor->AnchorFormat == 2)
1230 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1232 else if (anchor->AnchorFormat == 3)
1234 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1235 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1236 if (anchor->f.f2.XDeviceTable.offset)
1238 if (read_device_table (otf, stream, offset + anchor->offset,
1239 &anchor->f.f2.XDeviceTable) < 0)
1242 if (anchor->f.f2.YDeviceTable.offset)
1244 if (read_device_table (otf, stream, offset + anchor->offset,
1245 &anchor->f.f2.YDeviceTable) < 0)
1250 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1256 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1257 OTF_MarkArray *array)
1259 char *errfmt = "MarkArray%s";
1261 OTF_StreamState state;
1264 READ_OFFSET (stream, array->offset);
1265 SAVE_STREAM (stream, state);
1266 SEEK_STREAM (stream, offset + array->offset);
1267 READ_UINT16 (stream, array->MarkCount);
1268 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1269 for (i = 0; i < array->MarkCount; i++)
1271 READ_UINT16 (stream, array->MarkRecord[i].Class);
1272 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1274 for (i = 0; i < array->MarkCount; i++)
1275 if (read_anchor (otf, stream, offset + array->offset,
1276 &array->MarkRecord[i].MarkAnchor) < 0)
1278 RESTORE_STREAM (stream, state);
1283 read_base_array (OTF *otf, OTF_Stream *stream, long offset,
1284 unsigned ClassCount, OTF_BaseArray *array)
1286 char *errfmt = "BaseArray%s";
1288 OTF_StreamState state;
1291 READ_OFFSET (stream, array->offset);
1292 SAVE_STREAM (stream, state);
1293 SEEK_STREAM (stream, offset + array->offset);
1294 READ_UINT16 (stream, array->BaseCount);
1295 OTF_MALLOC (array->BaseRecord, array->BaseCount, "");
1296 for (i = 0; i < array->BaseCount; i++)
1298 OTF_MALLOC (array->BaseRecord[i].BaseAnchor, ClassCount,
1300 for (j = 0; j < ClassCount; j++)
1301 READ_OFFSET (stream, array->BaseRecord[i].BaseAnchor[j].offset);
1303 for (i = 0; i < array->BaseCount; i++)
1304 for (j = 0; j < ClassCount; j++)
1305 if (read_anchor (otf, stream, offset + array->offset,
1306 &array->BaseRecord[i].BaseAnchor[j]) < 0)
1308 RESTORE_STREAM (stream, state);
1313 static OTF_Class1Record *
1314 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1315 unsigned num1, enum OTF_ValueFormat bit1,
1316 unsigned num2, enum OTF_ValueFormat bit2)
1318 char *errfmt = "Class1Record%s";
1319 void *errret = NULL;
1320 OTF_Class1Record *rec;
1323 OTF_MALLOC (rec, num1, "");
1324 for (i = 0; i < num1; i++)
1326 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1327 for (j = 0; j < num2; j++)
1329 if (read_value_record (otf, stream, offset,
1330 bit1, &rec[i].Class2Record[j].Value1) < 0
1331 || read_value_record (otf, stream, offset,
1332 bit2, &rec[i].Class2Record[j].Value2) < 0)
1341 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1342 long offset, unsigned type,
1343 OTF_LookupSubTable *subtable)
1345 char *errfmt = "GPOS LookupSubTable%s";
1348 SEEK_STREAM (stream, offset);
1349 READ_UINT16 (stream, subtable->Format);
1354 if (subtable->Format == 1)
1356 read_coverage (otf, stream, offset, &subtable->Coverage);
1357 subtable->sub.gsub.single1.DeltaGlyphID = READ_INT16 (stream);
1359 else if (subtable->Format == 2)
1361 read_coverage (otf, stream, offset, &subtable->Coverage);
1362 subtable->sub.gsub.single2.GlyphCount
1363 = read_glyph_ids (otf, stream,
1364 &subtable->sub.gsub.single2.Substitute, 0);
1367 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1372 if (subtable->Format == 1)
1374 read_coverage (otf, stream, offset, &subtable->Coverage);
1376 else if (subtable->Format == 2)
1378 SEEK_STREAM (stream, offset + 2);
1379 read_coverage (otf, stream, offset, &subtable->Coverage);
1380 READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat1);
1381 READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat2);
1382 read_class_def (otf, stream, offset,
1383 &subtable->sub.gpos.pair2.ClassDef1);
1384 read_class_def (otf, stream, offset,
1385 &subtable->sub.gpos.pair2.ClassDef2);
1386 READ_UINT16 (stream, subtable->sub.gpos.pair2.Class1Count);
1387 READ_UINT16 (stream, subtable->sub.gpos.pair2.Class2Count);
1388 subtable->sub.gpos.pair2.Class1Record
1389 = read_class1_record_list (otf, stream, offset,
1390 subtable->sub.gpos.pair2.Class1Count,
1391 subtable->sub.gpos.pair2.ValueFormat1,
1392 subtable->sub.gpos.pair2.Class2Count,
1393 subtable->sub.gpos.pair2.ValueFormat2);
1396 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1400 if (subtable->Format == 1)
1402 read_coverage (otf, stream, offset, &subtable->Coverage);
1403 read_coverage (otf, stream, offset,
1404 &subtable->sub.gpos.mark_base1.BaseCoverage);
1405 READ_UINT16 (stream, subtable->sub.gpos.mark_base1.ClassCount);
1406 read_mark_array (otf, stream, offset,
1407 &subtable->sub.gpos.mark_base1.MarkArray);
1408 read_base_array (otf, stream, offset,
1409 subtable->sub.gpos.mark_base1.ClassCount,
1410 &subtable->sub.gpos.mark_base1.BaseArray);
1413 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1418 if (subtable->Format == 1)
1420 read_coverage (otf, stream, offset,
1421 &subtable->sub.gsub.chain_context1.Coverage);
1422 subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1423 = (read_chain_subrule_set
1424 (otf, stream, offset,
1425 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1427 else if (subtable->Format == 2)
1429 read_coverage (otf, stream, offset,
1430 &subtable->sub.gsub.chain_context2.Coverage);
1431 read_class_def (otf, stream, offset,
1432 &subtable->sub.gsub.chain_context2.Backtrack);
1433 read_class_def (otf, stream, offset,
1434 &subtable->sub.gsub.chain_context2.Input);
1435 read_class_def (otf, stream, offset,
1436 &subtable->sub.gsub.chain_context2.LookAhead);
1437 subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1438 = (read_chain_subclass_set
1439 (otf, stream, offset,
1440 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1442 else if (subtable->Format == 3)
1444 subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1445 = (read_coverage_list
1446 (otf, stream, offset,
1447 &subtable->sub.gsub.chain_context3.Backtrack));
1448 subtable->sub.gsub.chain_context3.InputGlyphCount
1449 = (read_coverage_list
1450 (otf, stream, offset,
1451 &subtable->sub.gsub.chain_context3.Input));
1452 subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1453 = (read_coverage_list
1454 (otf, stream, offset,
1455 &subtable->sub.gsub.chain_context3.LookAhead));
1456 subtable->sub.gsub.chain_context3.SubstCount
1457 = (read_subst_lookup_record
1459 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1462 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1466 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1473 read_gpos_table (OTF *otf, OTF_Stream *stream)
1475 char *errfmt = "GPOS%s";
1476 void *errret = NULL;
1479 OTF_CALLOC (gpos, 1, "");
1480 READ_FIXED (stream, gpos->Version);
1481 READ_OFFSET (stream, gpos->ScriptList.offset);
1482 READ_OFFSET (stream, gpos->FeatureList.offset);
1483 READ_OFFSET (stream, gpos->LookupList.offset);
1485 if (read_script_list (otf, stream, gpos->ScriptList.offset,
1486 &gpos->ScriptList) < 0
1487 || read_feature_list (otf, stream, gpos->FeatureList.offset,
1488 &gpos->FeatureList) < 0
1489 || read_lookup_list (otf, stream, gpos->LookupList.offset,
1490 &gpos->LookupList, 0) < 0)
1500 read_base_table (OTF_Stream *stream, long offset)
1504 OTF_MALLOC (base, 1);
1513 read_jstf_table (OTF_Stream *stream, long offset)
1517 OTF_MALLOC (jstf, 1);
1525 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1526 OTF_AttachList *list)
1528 char *errfmt = "AttachList%s";
1532 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1534 READ_UINT16 (stream, list->GlyphCount);
1535 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1536 for (i = 0; i < list->GlyphCount; i++)
1537 READ_OFFSET (stream, list->AttachPoint[i].offset);
1538 for (i = 0; i < list->GlyphCount; i++)
1542 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1543 READ_UINT16 (stream, count);
1544 list->AttachPoint[i].PointCount = count;
1545 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1546 for (j = 0; j < count; j++)
1547 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1553 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1554 OTF_CaretValue *caret)
1556 char *errfmt = "CaretValue%s";
1559 SEEK_STREAM (stream, offset + caret->offset);
1560 READ_UINT16 (stream, caret->CaretValueFormat);
1561 if (caret->CaretValueFormat == 1)
1562 READ_INT16 (stream, caret->f.f1.Coordinate);
1563 else if (caret->CaretValueFormat == 2)
1564 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1565 else if (caret->CaretValueFormat == 3)
1567 READ_INT16 (stream, caret->f.f3.Coordinate);
1568 if (read_device_table (otf, stream, offset + caret->offset,
1569 &caret->f.f3.DeviceTable) < 0)
1573 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1578 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1579 OTF_LigCaretList *list)
1581 char *errfmt = "LigCaretList%s";
1585 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1587 READ_UINT16 (stream, list->LigGlyphCount);
1588 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1589 for (i = 0; i < list->LigGlyphCount; i++)
1590 READ_OFFSET (stream, list->LigGlyph[i].offset);
1591 for (i = 0; i < list->LigGlyphCount; i++)
1595 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1596 READ_UINT16 (stream, count);
1597 list->LigGlyph[i].CaretCount = count;
1598 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1599 for (j = 0; j < count; j++)
1600 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1601 for (j = 0; j < count; j++)
1602 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1603 &list->LigGlyph[i].CaretValue[j]) < 0)
1610 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1614 READ_FIXED (stream, header->Version);
1615 READ_OFFSET (stream, header->GlyphClassDef);
1616 READ_OFFSET (stream, header->AttachList);
1617 READ_OFFSET (stream, header->LigCaretList);
1618 READ_OFFSET (stream, header->MarkAttachClassDef);
1623 read_gdef_table (OTF *otf, OTF_Stream *stream)
1625 char *errfmt = "GDEF%s";
1626 void *errret = NULL;
1629 OTF_CALLOC (gdef, 1, "");
1630 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1631 if (gdef->header.GlyphClassDef)
1633 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1634 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1636 if (gdef->header.AttachList)
1637 read_attach_list (otf, stream, gdef->header.AttachList,
1638 &gdef->attach_list);
1639 if (gdef->header.LigCaretList)
1640 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1641 &gdef->lig_caret_list);
1642 if (gdef->header.MarkAttachClassDef)
1644 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1645 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1656 read_cmap_table (OTF *otf, OTF_Stream *stream)
1658 char *errfmt = "cmap%s";
1659 void *errret = NULL;
1663 OTF_CALLOC (cmap, 1, "");
1664 READ_USHORT (stream, cmap->version);
1665 READ_USHORT (stream, cmap->numTables);
1666 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
1667 for (i = 0; i < cmap->numTables; i++)
1669 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
1670 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
1671 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
1672 if (cmap->EncodingRecord[i].platformID == 3
1673 && cmap->EncodingRecord[i].encodingID == 1)
1674 cmap->Unicode = cmap->EncodingRecord + i;
1676 for (i = 0; i < cmap->numTables; i++)
1680 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
1681 READ_USHORT (stream, format);
1682 cmap->EncodingRecord[i].subtable.format = format;
1683 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
1684 if (format == 8 || format == 10 || format == 12)
1686 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
1687 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
1691 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
1697 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
1698 " (EncodingRecord)");
1700 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
1710 OTF_EncodingSubtable4 *sub4;
1715 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
1716 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
1717 READ_USHORT (stream, sub4->segCountX2);
1718 segCount = sub4->segCountX2 / 2;
1719 READ_USHORT (stream, sub4->searchRange);
1720 READ_USHORT (stream, sub4->entrySelector);
1721 READ_USHORT (stream, sub4->rangeShift);
1722 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
1723 for (j = 0; j < segCount; j++)
1724 READ_USHORT (stream, sub4->segments[j].endCount);
1725 READ_USHORT (stream, dummy);
1726 for (j = 0; j < segCount; j++)
1727 READ_USHORT (stream, sub4->segments[j].startCount);
1728 for (j = 0; j < segCount; j++)
1729 READ_SHORT (stream, sub4->segments[j].idDelta);
1730 for (j = 0; j < segCount; j++)
1733 unsigned rest = 2 * (segCount - j);
1735 READ_USHORT (stream, off);
1737 sub4->segments[j].idRangeOffset = 0xFFFF;
1739 sub4->segments[j].idRangeOffset = (off - rest) / 2;
1741 j = (cmap->EncodingRecord[i].subtable.length
1742 - (14 + 2 * (segCount * 4 + 1)));
1743 sub4->GlyphCount = j / 2;
1744 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
1745 for (j = 0; j < sub4->GlyphCount; j++)
1746 READ_USHORT (stream, sub4->glyphIdArray[j]);
1758 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
1760 char *errfmt = "nameID (%d)";
1761 void *errret = NULL;
1762 OTF_StreamState state;
1767 SAVE_STREAM (stream, state);
1768 SEEK_STREAM (stream, stream->pos + rec->offset);
1772 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
1773 READ_BYTES (stream, str, rec->length);
1774 for (i = 0; i < rec->length; i++)
1778 else if (bytes == 2)
1780 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
1781 for (i = 0; i < rec->length / 2; i++)
1783 READ_USHORT (stream, c);
1789 else if (bytes == 4)
1791 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
1792 for (i = 0; i < rec->length / 4; i++)
1794 READ_ULONG (stream, c);
1801 RESTORE_STREAM (stream, state);
1806 read_name_table (OTF *otf, OTF_Stream *stream)
1808 char *errfmt = "name%s";
1809 void *errret = NULL;
1813 OTF_CALLOC (name, 1, "");
1814 READ_USHORT (stream, name->format);
1815 READ_USHORT (stream, name->count);
1816 READ_USHORT (stream, name->stringOffset);
1817 OTF_MALLOC (name->nameRecord, name->count, "");
1818 for (i = 0; i < name->count; i++)
1820 OTF_NameRecord *rec = name->nameRecord + i;
1822 READ_USHORT (stream, rec->platformID);
1823 READ_USHORT (stream, rec->encodingID);
1824 READ_USHORT (stream, rec->languageID);
1825 READ_USHORT (stream, rec->nameID);
1826 READ_USHORT (stream, rec->length);
1827 READ_USHORT (stream, rec->offset);
1829 for (i = 0; i < name->count; i++)
1831 OTF_NameRecord *rec = name->nameRecord + i;
1832 int nameID = rec->nameID;
1834 if (nameID <= OTF_max_nameID
1835 && ! name->name[nameID])
1837 if (rec->platformID == 0)
1838 name->name[nameID] = read_name (otf, stream, rec,
1839 rec->encodingID <= 3 ? 2 : 4);
1840 else if (rec->platformID == 1
1841 && rec->encodingID == 0)
1842 name->name[nameID] = read_name (otf, stream, rec, 1);
1843 else if (rec->platformID == 3
1844 && (rec->encodingID == 1 || rec->encodingID == 10))
1845 name->name[nameID] = read_name (otf, stream,
1846 rec, rec->encodingID == 1 ? 2 : 4);
1857 /* We can't use memory allocation macros in the following functions
1858 because those macros returns from the functions before freeing
1859 memory previously allocated. */
1862 otf_open (char *otf_name)
1865 char *errfmt = "opening otf (%s)";
1866 void *errret = NULL;
1869 fp = fopen (otf_name, "r");
1871 OTF_ERROR (OTF_ERROR_FILE, otf_name);
1872 otf = calloc (1, sizeof (OTF));
1874 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
1875 otf->filename = strdup (otf_name);
1876 if (! otf->filename)
1880 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
1883 if (read_header_part (otf, fp) < 0)
1896 otf_close (OTF *otf)
1898 OTF_InternalData *internal_data = otf->internal_data;
1902 free (otf->filename);
1903 if (otf->table_dirs)
1904 free (otf->table_dirs);
1907 OTF_MemoryRecord *memrec = internal_data->memory_record;
1909 if (internal_data->header_stream)
1910 free_stream (internal_data->header_stream);
1912 if (internal_data->streams)
1913 for (i = 0; i < otf->offset_table.numTables; i++)
1914 if (internal_data->streams[i])
1915 free_stream (internal_data->streams[i]);
1919 OTF_MemoryRecord *next = memrec->next;
1921 for (i = 0; i < memrec->used; i++)
1922 free (memrec->memory[i]);
1926 free (internal_data);
1932 otf_get_table (OTF *otf, OTF_Tag tag)
1934 char *errfmt = "OTF Table Read";
1936 OTF_InternalData *internal_data = otf->internal_data;
1937 OTF_TableInfo *table_info;
1939 if (tag == otf_tag ("head"))
1940 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
1941 else if (tag == otf_tag ("name"))
1942 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
1943 else if (tag == otf_tag ("cmap"))
1944 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
1945 else if (tag == otf_tag ("GDEF"))
1946 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
1947 else if (tag == otf_tag ("GSUB"))
1948 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
1949 else if (tag == otf_tag ("GPOS"))
1950 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
1952 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported)");
1954 if (*table_info->address)
1956 if (! table_info->stream)
1957 OTF_ERROR (OTF_ERROR_TABLE, " (not found)");
1958 if (! table_info->reader)
1959 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
1961 *table_info->address = (*table_info->reader) (otf, table_info->stream);
1962 free_stream (table_info->stream);
1963 table_info->stream = NULL;
1964 if (! *table_info->address)
1966 table_info->reader = NULL;
1967 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");