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 = malloc (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) \
251 OTF_MemoryRecord *memrec \
252 = ((OTF_InternalData *) otf->internal_data)->memory_record; \
253 (p) = calloc ((size), sizeof (*(p))); \
255 || (memrec->used >= OTF_MEMORY_RECORD_SIZE \
256 && ! (memrec = allocate_memory_record (otf)))) \
257 OTF_ERROR (OTF_ERROR_MEMORY, (arg)); \
258 memrec->memory[memrec->used++] = (p); \
265 static void *read_head_table (OTF *otf, OTF_Stream *stream);
266 static void *read_name_table (OTF *otf, OTF_Stream *stream);
267 static void *read_cmap_table (OTF *otf, OTF_Stream *stream);
268 static void *read_gdef_table (OTF *otf, OTF_Stream *stream);
269 static void *read_gsub_table (OTF *otf, OTF_Stream *stream);
270 static void *read_gpos_table (OTF *otf, OTF_Stream *stream);
273 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
277 READ_FIXED (stream, table->sfnt_version);
278 READ_USHORT (stream, table->numTables);
279 READ_USHORT (stream, table->searchRange);
280 READ_USHORT (stream, table->enterSelector);
281 READ_USHORT (stream, table->rangeShift);
286 read_table_directory (OTF_Stream *stream, OTF_TableDirectory *table)
291 READ_TAG (stream, tag);
293 table->name[0] = tag >> 24;
294 table->name[1] = (tag >> 16) & 0xFF;
295 table->name[0] = (tag >> 8) & 0xFF;
296 table->name[0] = tag >> 8;
297 table->name[0] = '\0';
298 READ_ULONG (stream, table->checkSum);
299 READ_ULONG (stream, table->offset);
300 READ_ULONG (stream, table->length);
305 read_header_part (OTF *otf, FILE *fp)
307 char *errfmt = "otf header%s";
309 OTF_Tag head_tag, name_tag, cmap_tag, gdef_tag, gsub_tag, gpos_tag;
312 OTF_InternalData *internal_data = (OTF_InternalData *) otf->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 head_tag = otf_tag ("head");
328 name_tag = otf_tag ("name");
329 cmap_tag = otf_tag ("cmap");
330 gdef_tag = otf_tag ("GDEF");
331 gsub_tag = otf_tag ("GSUB");
332 gpos_tag = otf_tag ("GPOS");
334 stream = make_stream ();
338 internal_data->header_stream = stream;
340 /* Size of Offset Table is 12 bytes. */
341 if (setup_stream (stream, fp, 0, 12, "Offset Table") < 0)
343 if (read_offset_table (otf, stream, &otf->offset_table) < 0)
346 /* Size of each Table Directory is 16 bytes. */
347 if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables,
348 "Table Directory") < 0)
351 OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables, " (OffsetTable)");
352 for (i = 0; i < otf->offset_table.numTables; i++)
354 OTF_Tag tag = read_table_directory (stream, otf->table_dirs + i);
355 OTF_TableInfo *table_info = NULL;
360 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
361 else if (tag == name_tag)
362 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
363 else if (tag == cmap_tag)
364 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
365 else if (tag == gdef_tag)
366 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
367 else if (tag == gsub_tag)
368 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
369 else if (tag == gpos_tag)
370 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
374 table_info->stream = make_stream ();
375 if (setup_stream (table_info->stream, fp,
376 otf->table_dirs[i].offset,
377 otf->table_dirs[i].length,
378 otf->table_dirs[i].name) < 0)
383 internal_data->header_stream = NULL;
384 free_stream (stream);
392 read_head_table (OTF *otf, OTF_Stream *stream)
394 char *errfmt = "head%s";
398 OTF_CALLOC (head, 1, "");
399 READ_FIXED (stream, head->TableVersionNumber);
400 READ_FIXED (stream, head->fontRevision);
401 READ_ULONG (stream, head->checkSumAdjustment);
402 READ_ULONG (stream, head->magicNumber);
403 READ_USHORT (stream, head->flags);
404 READ_USHORT (stream, head->unitsPerEm);
412 read_script_list (OTF *otf, OTF_Stream *stream, long offset,
413 OTF_ScriptList *list)
415 char *errfmt = "Script List%s";
419 SEEK_STREAM (stream, offset);
420 READ_USHORT (stream, list->ScriptCount);
421 OTF_CALLOC (list->Script, list->ScriptCount, "");
423 for (i = 0; i < list->ScriptCount; i++)
425 READ_TAG (stream, list->Script[i].ScriptTag);
426 READ_OFFSET (stream, list->Script[i].offset);
428 for (i = 0; i < list->ScriptCount; i++)
430 OTF_Script *script = list->Script + i;
431 long script_offset = offset + script->offset;
433 SEEK_STREAM (stream, script_offset);
434 READ_OFFSET (stream, script->DefaultLangSysOffset);
435 READ_USHORT (stream, script->LangSysCount);
436 OTF_MALLOC (script->LangSysRecord, script->LangSysCount, " (LangSys)");
437 OTF_CALLOC (script->LangSys, script->LangSysCount, " (LangSys)");
438 for (j = 0; j < script->LangSysCount; j++)
440 READ_TAG (stream, script->LangSysRecord[j].LangSysTag);
441 READ_OFFSET (stream, script->LangSysRecord[j].LangSys);
444 if (script->DefaultLangSysOffset)
446 OTF_LangSys *langsys = &script->DefaultLangSys;
448 SEEK_STREAM (stream, script_offset + script->DefaultLangSysOffset);
449 READ_OFFSET (stream, langsys->LookupOrder);
450 READ_USHORT (stream, langsys->ReqFeatureIndex);
451 READ_USHORT (stream, langsys->FeatureCount);
452 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
454 for (k = 0; k < langsys->FeatureCount; k++)
455 READ_USHORT (stream, langsys->FeatureIndex[k]);
458 for (j = 0; j < script->LangSysCount; j++)
460 OTF_LangSys *langsys = script->LangSys + j;
463 script_offset + script->LangSysRecord[j].LangSys);
464 READ_OFFSET (stream, langsys->LookupOrder);
465 READ_USHORT (stream, langsys->ReqFeatureIndex);
466 READ_USHORT (stream, langsys->FeatureCount);
467 OTF_MALLOC (langsys->FeatureIndex, langsys->FeatureCount,
469 for (k = 0; k < langsys->FeatureCount; k++)
470 READ_USHORT (stream, langsys->FeatureIndex[k]);
478 read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
479 OTF_FeatureList *list)
481 char *errfmt = "Feature List%s";
485 READ_UINT16 (stream, list->FeatureCount);
486 OTF_CALLOC (list->Feature, list->FeatureCount, "");
487 for (i = 0; i < list->FeatureCount; i++)
489 READ_TAG (stream, list->Feature[i].FeatureTag);
490 READ_OFFSET (stream, list->Feature[i].offset);
492 for (i = 0; i < list->FeatureCount; i++)
494 OTF_Feature *feature = list->Feature + i;
496 SEEK_STREAM (stream, offset + feature->offset);
497 READ_OFFSET (stream, feature->FeatureParams);
498 READ_UINT16 (stream, feature->LookupCount);
499 OTF_MALLOC (feature->LookupListIndex, feature->LookupCount,
500 " (LookupListIndex)");
501 for (j = 0; j < feature->LookupCount; j++)
502 READ_UINT16 (stream, feature->LookupListIndex[j]);
508 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
509 long offset, unsigned type,
510 OTF_LookupSubTableGSUB *subtable);
511 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
512 long offset, unsigned type,
513 OTF_LookupSubTableGPOS *subtable);
516 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
517 OTF_LookupList *list, int gsub)
519 char *errfmt = "Lookup List%s";
523 SEEK_STREAM (stream, offset);
524 READ_UINT16 (stream, list->LookupCount);
525 OTF_CALLOC (list->Lookup, list->LookupCount, "");
527 for (i = 0; i < list->LookupCount; i++)
528 READ_OFFSET (stream, list->Lookup[i].offset);
529 for (i = 0; i < list->LookupCount; i++)
531 OTF_Lookup *lookup = list->Lookup + i;
533 SEEK_STREAM (stream, offset + lookup->offset);
534 READ_UINT16 (stream, lookup->LookupType);
535 READ_UINT16 (stream, lookup->LookupFlag);
536 READ_UINT16 (stream, lookup->SubTableCount);
537 OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
538 " (SubTableOffset)");
540 OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
543 OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
545 for (j = 0; j < lookup->SubTableCount; j++)
546 READ_OFFSET (stream, lookup->SubTableOffset[j]);
547 for (j = 0; j < lookup->SubTableCount; j++)
550 = offset + lookup->offset + lookup->SubTableOffset[j];
553 ? read_lookup_subtable_gsub (otf, stream, this_offset,
555 lookup->SubTable.gsub + j) < 0
556 : read_lookup_subtable_gpos (otf, stream, this_offset,
558 lookup->SubTable.gpos + j) < 0)
568 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids, int minus)
570 char *errfmt = "GlyphID List%s";
575 READ_UINT16 (stream, count);
578 OTF_MALLOC (*ids, count, "");
579 for (i = 0; i < count + minus; i++)
580 READ_GLYPHID (stream, (*ids)[i]);
585 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
587 char *errfmt = "RangeRecord%s";
592 READ_UINT16 (stream, count);
595 OTF_MALLOC (*record, count, "");
596 for (i = 0; i < count; i++)
598 READ_GLYPHID (stream, (*record)[i].Start);
599 READ_GLYPHID (stream, (*record)[i].End);
600 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
607 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
608 OTF_Coverage *coverage)
610 char *errfmt = "Coverage%s";
612 OTF_StreamState state;
615 READ_OFFSET (stream, coverage->offset);
616 SAVE_STREAM (stream, state);
617 SEEK_STREAM (stream, offset + coverage->offset);
618 READ_UINT16 (stream, coverage->CoverageFormat);
619 if (coverage->CoverageFormat == 1)
620 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0);
621 else if (coverage->CoverageFormat == 2)
622 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
624 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
627 coverage->Count = (unsigned) count;
628 RESTORE_STREAM (stream, state);
633 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
634 OTF_Coverage **coverage)
636 char *errfmt = "Coverage List%s";
641 READ_UINT16 (stream, count);
644 OTF_MALLOC (*coverage, count, "");
645 for (i = 0; i < count; i++)
646 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
653 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
656 char *errfmt = "ClassDef%s";
659 SEEK_STREAM (stream, class->offset);
660 READ_UINT16 (stream, class->ClassFormat);
661 if (class->ClassFormat == 1)
663 READ_GLYPHID (stream, class->f.f1.StartGlyph);
664 class->f.f1.GlyphCount
666 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
667 if (! class->f.f1.GlyphCount)
668 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
670 else if (class->ClassFormat == 2)
672 class->f.f2.ClassRangeCount
673 = (read_range_records
674 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
675 if (! class->f.f2.ClassRangeCount)
676 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
679 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
685 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
687 char *errfmt = "ClassDef%s";
689 OTF_StreamState state;
691 READ_OFFSET (stream, class->offset);
692 SAVE_STREAM (stream, state);
693 SEEK_STREAM (stream, offset + class->offset);
694 READ_UINT16 (stream, class->ClassFormat);
695 if (class->ClassFormat == 1)
697 READ_GLYPHID (stream, class->f.f1.StartGlyph);
698 class->f.f1.GlyphCount
700 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
701 if (! class->f.f1.GlyphCount)
704 else if (class->ClassFormat == 2)
706 class->f.f2.ClassRangeCount
707 = (read_range_records
708 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
709 if (! class->f.f2.ClassRangeCount)
713 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
715 RESTORE_STREAM (stream, state);
721 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
722 OTF_DeviceTable *table)
724 char *errfmt = "Device Table%s";
735 SEEK_STREAM (stream, offset + table->offset);
736 READ_UINT16 (stream, table->StartSize);
737 READ_UINT16 (stream, table->EndSize);
738 READ_UINT16 (stream, table->DeltaFormat);
739 num = table->EndSize - table->StartSize + 1;
740 OTF_MALLOC (table->DeltaValue, num, "");
742 if (table->DeltaFormat == 1)
743 for (i = 0; i < num; i++)
746 READ_UINT16 (stream, val);
747 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
748 table->DeltaValue[i] = intval.int2;
750 else if (table->DeltaFormat == 2)
751 for (i = 0; i < num; i++)
754 READ_UINT16 (stream, val);
755 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
756 table->DeltaValue[i] = intval.int4;
758 else if (table->DeltaFormat == 3)
759 for (i = 0; i < num; i++)
763 READ_UINT16 (stream, val);
764 intval.int8 = val >> 8;
765 table->DeltaValue[i] = intval.int8;
769 intval.int8 = val >> 8;
770 table->DeltaValue[i] = intval.int8;
774 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
782 read_gsub_table (OTF *otf, OTF_Stream *stream)
784 char *errfmt = "GSUB%s";
788 OTF_CALLOC (gsub, 1, "");
789 READ_FIXED (stream, gsub->Version);
790 READ_OFFSET (stream, gsub->ScriptList.offset);
791 READ_OFFSET (stream, gsub->FeatureList.offset);
792 READ_OFFSET (stream, gsub->LookupList.offset);
794 if (read_script_list (otf, stream, gsub->ScriptList.offset,
795 &gsub->ScriptList) < 0
796 || read_feature_list (otf, stream, gsub->FeatureList.offset,
797 &gsub->FeatureList) < 0
798 || read_lookup_list (otf, stream, gsub->LookupList.offset,
799 &gsub->LookupList, 1) < 0)
805 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
807 char *errfmt = "Sequence%s";
812 READ_UINT16 (stream, count);
813 OTF_MALLOC (*seq, count, "");
815 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
816 for (i = 0; i < count; i++)
817 READ_OFFSET (stream, (*seq)[i].offset);
818 for (i = 0; i < count; i++)
820 SEEK_STREAM (stream, offset + (*seq)[i].offset);
821 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
822 &(*seq)[i].Substitute, 0);
823 if (! (*seq)[i].GlyphCount)
830 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
831 OTF_Ligature **ligature)
833 char *errfmt = "Ligature%s";
838 READ_UINT16 (stream, count);
841 OTF_MALLOC (*ligature, count, "");
842 for (i = 0; i < count; i++)
843 READ_OFFSET (stream, (*ligature)[i].offset);
844 for (i = 0; i < count; i++)
846 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
847 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
848 (*ligature)[i].CompCount
849 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1);
850 if (! (*ligature)[i].CompCount)
857 read_ligature_set (OTF *otf, OTF_Stream *stream, long offset,
858 OTF_LigatureSet **ligset)
860 char *errfmt = "LigatureSet%s";
865 READ_UINT16 (stream, count);
868 OTF_MALLOC (*ligset, count, "");
869 for (i = 0; i < count; i++)
870 READ_OFFSET (stream, (*ligset)[i].offset);
871 for (i = 0; i < count; i++)
875 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
876 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
877 &(*ligset)[i].Ligature);
880 (*ligset)[i].LigatureCount = (unsigned) lig_count;
886 read_subst_lookup_record (OTF *otf, OTF_Stream *stream,
887 OTF_SubstLookupRecord **record)
889 char *errfmt = "SubstLookupRecord%s";
894 READ_UINT16 (stream, count);
896 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
897 OTF_MALLOC (*record, count, "");
898 for (i = 0; i < count; i++)
900 READ_UINT16 (stream, (*record)[i].SequenceIndex);
901 READ_UINT16 (stream, (*record)[i].LookupListIndex);
907 read_chain_subrule (OTF *otf, OTF_Stream *stream, long offset,
908 OTF_ChainSubRule **rule)
910 char *errfmt = "ChainSubRule%s";
915 READ_UINT16 (stream, count);
917 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
918 OTF_MALLOC (*rule, count, "");
919 for (i = 0; i < count; i++)
920 READ_OFFSET (stream, (*rule)[i].offset);
921 for (i = 0; i < count; i++)
923 SEEK_STREAM (stream, offset + (*rule)[i].offset);
924 (*rule)[i].BacktrackGlyphCount
925 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0);
926 if (! (*rule)[i].BacktrackGlyphCount)
928 (*rule)[i].InputGlyphCount
929 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1);
930 if (! (*rule)[i].InputGlyphCount)
932 (*rule)[i].LookaheadGlyphCount
933 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0);
934 if (! (*rule)[i].LookaheadGlyphCount)
936 (*rule)[i].SubstCount
937 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
938 if (! (*rule)[i].SubstCount)
946 read_chain_subrule_set (OTF *otf, OTF_Stream *stream, long offset,
947 OTF_ChainSubRuleSet **set)
949 char *errfmt = "ChainSubRuleSet%s";
954 READ_UINT16 (stream, count);
956 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
957 OTF_MALLOC (*set, count, "");
958 for (i = 0; i < count; i++)
959 READ_OFFSET (stream, (*set)[i].offset);
960 for (i = 0; i < count; i++)
962 SEEK_STREAM (stream, offset + (*set)[i].offset);
963 (*set)[i].ChainSubRuleCount
964 = read_chain_subrule (otf, stream, offset + (*set)[i].offset,
965 &(*set)[i].ChainSubRule);
966 if (! (*set)[i].ChainSubRuleCount)
973 read_chain_subclass_rule (OTF *otf, OTF_Stream *stream, long offset,
974 OTF_ChainSubClassRule **rule)
976 char *errfmt = "ChainSubClassRule%s";
981 READ_UINT16 (stream, count);
983 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
984 OTF_MALLOC (*rule, count, "");
985 for (i = 0; i < count; i++)
986 READ_OFFSET (stream, (*rule)[i].offset);
987 for (i = 0; i < count; i++)
989 SEEK_STREAM (stream, offset + (*rule)[i].offset);
990 (*rule)[i].BacktrackGlyphCount
991 = read_glyph_ids (otf, stream,
992 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0);
993 if (! (*rule)[i].BacktrackGlyphCount)
995 (*rule)[i].InputGlyphCount
996 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Input, -1);
997 if (! (*rule)[i].InputGlyphCount)
999 (*rule)[i].LookaheadGlyphCount
1000 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].LookAhead, 0);
1001 if (! (*rule)[i].LookaheadGlyphCount)
1003 (*rule)[i].SubstCount
1004 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
1005 if (! (*rule)[i].SubstCount)
1012 read_chain_subclass_set (OTF *otf, OTF_Stream *stream, long offset,
1013 OTF_ChainSubClassSet **set)
1015 char *errfmt = "ChainSubClassSet%s";
1016 unsigned errret = 0;
1020 READ_UINT16 (stream, count);
1022 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1023 OTF_MALLOC (*set, count, "");
1024 for (i = 0; i < count; i++)
1025 READ_OFFSET (stream, (*set)[i].offset);
1026 for (i = 0; i < count; i++)
1028 SEEK_STREAM (stream, offset + (*set)[i].offset);
1029 (*set)[i].ChainSubClassRuleCnt
1030 = read_chain_subclass_rule (otf, stream, offset + (*set)[i].offset,
1031 &(*set)[i].ChainSubClassRule);
1032 if (! (*set)[i].ChainSubClassRuleCnt)
1040 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1041 unsigned type, OTF_LookupSubTableGSUB *subtable)
1043 char *errfmt = "GSUB LookupSubTable%s";
1047 SEEK_STREAM (stream, offset);
1048 READ_UINT16 (stream, subtable->Format);
1052 if (subtable->Format == 1)
1054 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1056 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1058 else if (subtable->Format == 2)
1060 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1062 subtable->u.single2.GlyphCount
1063 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1065 if (! subtable->u.single2.GlyphCount)
1069 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1073 if (subtable->Format == 1)
1075 read_coverage (otf, stream, offset, &subtable->Coverage);
1076 subtable->u.multiple1.SequenceCount
1077 = read_sequence (otf, stream, offset,
1078 &subtable->u.multiple1.Sequence);
1081 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1088 if (subtable->Format == 1)
1090 read_coverage (otf, stream, offset, &subtable->Coverage);
1091 count = (read_ligature_set
1092 (otf, stream, offset,
1093 &subtable->u.ligature1.LigatureSet));
1096 subtable->u.ligature1.LigSetCount = (unsigned) count;
1099 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1103 if (subtable->Format == 1)
1105 read_coverage (otf, stream, offset, &subtable->Coverage);
1106 subtable->u.chain_context1.ChainSubRuleSetCount
1107 = (read_chain_subrule_set
1108 (otf, stream, offset,
1109 &subtable->u.chain_context1.ChainSubRuleSet));
1111 else if (subtable->Format == 2)
1113 read_coverage (otf, stream, offset, &subtable->Coverage);
1114 read_class_def (otf, stream, offset,
1115 &subtable->u.chain_context2.Backtrack);
1116 read_class_def (otf, stream, offset,
1117 &subtable->u.chain_context2.Input);
1118 read_class_def (otf, stream, offset,
1119 &subtable->u.chain_context2.LookAhead);
1120 subtable->u.chain_context2.ChainSubClassSetCnt
1121 = (read_chain_subclass_set
1122 (otf, stream, offset,
1123 &subtable->u.chain_context2.ChainSubClassSet));
1125 else if (subtable->Format == 3)
1127 count = (read_coverage_list
1128 (otf, stream, offset,
1129 &subtable->u.chain_context3.Backtrack));
1132 subtable->u.chain_context3.BacktrackGlyphCount
1134 count = (read_coverage_list
1135 (otf, stream, offset,
1136 &subtable->u.chain_context3.Input));
1139 subtable->u.chain_context3.InputGlyphCount
1141 subtable->Coverage = subtable->u.chain_context3.Input[0];
1142 count = (read_coverage_list
1143 (otf, stream, offset,
1144 &subtable->u.chain_context3.LookAhead));
1145 subtable->u.chain_context3.LookaheadGlyphCount
1147 subtable->u.chain_context3.SubstCount
1148 = (read_subst_lookup_record
1150 &subtable->u.chain_context3.SubstLookupRecord));
1153 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1158 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1167 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1168 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1171 OTF_StreamState state;
1176 for (i = 0, size = 0; i < 8; i++)
1180 if (bit & OTF_XPlacement)
1181 READ_INT16 (stream, value_record->XPlacement);
1182 if (bit & OTF_XPlacement)
1183 READ_INT16 (stream, value_record->YPlacement);
1184 if (bit & OTF_XAdvance)
1185 READ_INT16 (stream, value_record->XAdvance);
1186 if (bit & OTF_YAdvance)
1187 READ_INT16 (stream, value_record->YAdvance);
1188 if (bit & OTF_XPlaDevice)
1189 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1190 if (bit & OTF_YPlaDevice)
1191 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1192 if (bit & OTF_XAdvDevice)
1193 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1194 if (bit & OTF_YAdvDevice)
1195 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1196 SAVE_STREAM (stream, state);
1197 if (value_record->XPlaDevice.offset)
1199 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1202 if (value_record->YPlaDevice.offset)
1204 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1207 if (value_record->XAdvDevice.offset)
1209 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1212 if (value_record->YAdvDevice.offset)
1214 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1217 RESTORE_STREAM (stream, state);
1223 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1225 char *errfmt = "Anchor%s";
1228 SEEK_STREAM (stream, offset + anchor->offset);
1229 READ_UINT16 (stream, anchor->AnchorFormat);
1230 READ_INT16 (stream, anchor->XCoordinate);
1231 READ_INT16 (stream, anchor->YCoordinate);
1232 if (anchor->AnchorFormat == 1)
1234 else if (anchor->AnchorFormat == 2)
1236 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1238 else if (anchor->AnchorFormat == 3)
1240 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1241 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1242 if (anchor->f.f2.XDeviceTable.offset)
1244 if (read_device_table (otf, stream, offset + anchor->offset,
1245 &anchor->f.f2.XDeviceTable) < 0)
1248 if (anchor->f.f2.YDeviceTable.offset)
1250 if (read_device_table (otf, stream, offset + anchor->offset,
1251 &anchor->f.f2.YDeviceTable) < 0)
1256 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1262 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1263 OTF_MarkArray *array)
1265 char *errfmt = "MarkArray%s";
1267 OTF_StreamState state;
1270 READ_OFFSET (stream, array->offset);
1271 SAVE_STREAM (stream, state);
1272 SEEK_STREAM (stream, offset + array->offset);
1273 READ_UINT16 (stream, array->MarkCount);
1274 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1275 for (i = 0; i < array->MarkCount; i++)
1277 READ_UINT16 (stream, array->MarkRecord[i].Class);
1278 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1280 for (i = 0; i < array->MarkCount; i++)
1281 if (read_anchor (otf, stream, offset + array->offset,
1282 &array->MarkRecord[i].MarkAnchor) < 0)
1284 RESTORE_STREAM (stream, state);
1289 read_base_array (OTF *otf, OTF_Stream *stream, long offset,
1290 unsigned ClassCount, OTF_BaseArray *array)
1292 char *errfmt = "BaseArray%s";
1294 OTF_StreamState state;
1297 READ_OFFSET (stream, array->offset);
1298 SAVE_STREAM (stream, state);
1299 SEEK_STREAM (stream, offset + array->offset);
1300 READ_UINT16 (stream, array->BaseCount);
1301 OTF_MALLOC (array->BaseRecord, array->BaseCount, "");
1302 for (i = 0; i < array->BaseCount; i++)
1304 OTF_MALLOC (array->BaseRecord[i].BaseAnchor, ClassCount,
1306 for (j = 0; j < ClassCount; j++)
1307 READ_OFFSET (stream, array->BaseRecord[i].BaseAnchor[j].offset);
1309 for (i = 0; i < array->BaseCount; i++)
1310 for (j = 0; j < ClassCount; j++)
1311 if (read_anchor (otf, stream, offset + array->offset,
1312 &array->BaseRecord[i].BaseAnchor[j]) < 0)
1314 RESTORE_STREAM (stream, state);
1319 static OTF_Class1Record *
1320 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1321 unsigned num1, enum OTF_ValueFormat bit1,
1322 unsigned num2, enum OTF_ValueFormat bit2)
1324 char *errfmt = "Class1Record%s";
1325 void *errret = NULL;
1326 OTF_Class1Record *rec;
1329 OTF_MALLOC (rec, num1, "");
1330 for (i = 0; i < num1; i++)
1332 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1333 for (j = 0; j < num2; j++)
1335 if (read_value_record (otf, stream, offset,
1336 bit1, &rec[i].Class2Record[j].Value1) < 0
1337 || read_value_record (otf, stream, offset,
1338 bit2, &rec[i].Class2Record[j].Value2) < 0)
1347 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1348 long offset, unsigned type,
1349 OTF_LookupSubTableGPOS *subtable)
1351 char *errfmt = "GPOS LookupSubTable%s";
1354 SEEK_STREAM (stream, offset);
1355 READ_UINT16 (stream, subtable->Format);
1360 if (subtable->Format == 1)
1362 read_coverage (otf, stream, offset, &subtable->Coverage);
1363 subtable->u.single1.DeltaGlyphID = READ_INT16 (stream);
1365 else if (subtable->Format == 2)
1367 read_coverage (otf, stream, offset, &subtable->Coverage);
1368 subtable->u.single2.GlyphCount
1369 = read_glyph_ids (otf, stream,
1370 &subtable->u.single2.Substitute, 0);
1373 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1378 if (subtable->Format == 1)
1380 read_coverage (otf, stream, offset, &subtable->Coverage);
1382 else if (subtable->Format == 2)
1384 SEEK_STREAM (stream, offset + 2);
1385 read_coverage (otf, stream, offset, &subtable->Coverage);
1386 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
1387 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
1388 read_class_def (otf, stream, offset,
1389 &subtable->u.pair2.ClassDef1);
1390 read_class_def (otf, stream, offset,
1391 &subtable->u.pair2.ClassDef2);
1392 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
1393 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
1394 subtable->u.pair2.Class1Record
1395 = read_class1_record_list (otf, stream, offset,
1396 subtable->u.pair2.Class1Count,
1397 subtable->u.pair2.ValueFormat1,
1398 subtable->u.pair2.Class2Count,
1399 subtable->u.pair2.ValueFormat2);
1402 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1406 if (subtable->Format == 1)
1408 read_coverage (otf, stream, offset, &subtable->Coverage);
1409 read_coverage (otf, stream, offset,
1410 &subtable->u.mark_base1.BaseCoverage);
1411 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
1412 read_mark_array (otf, stream, offset,
1413 &subtable->u.mark_base1.MarkArray);
1414 read_base_array (otf, stream, offset,
1415 subtable->u.mark_base1.ClassCount,
1416 &subtable->u.mark_base1.BaseArray);
1419 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1424 if (subtable->Format == 1)
1426 read_coverage (otf, stream, offset,
1427 &subtable->u.chain_context1.Coverage);
1428 subtable->u.chain_context1.ChainSubRuleSetCount
1429 = (read_chain_subrule_set
1430 (otf, stream, offset,
1431 &subtable->u.chain_context1.ChainSubRuleSet));
1433 else if (subtable->Format == 2)
1435 read_coverage (otf, stream, offset,
1436 &subtable->u.chain_context2.Coverage);
1437 read_class_def (otf, stream, offset,
1438 &subtable->u.chain_context2.Backtrack);
1439 read_class_def (otf, stream, offset,
1440 &subtable->u.chain_context2.Input);
1441 read_class_def (otf, stream, offset,
1442 &subtable->u.chain_context2.LookAhead);
1443 subtable->u.chain_context2.ChainSubClassSetCnt
1444 = (read_chain_subclass_set
1445 (otf, stream, offset,
1446 &subtable->u.chain_context2.ChainSubClassSet));
1448 else if (subtable->Format == 3)
1450 subtable->u.chain_context3.BacktrackGlyphCount
1451 = (read_coverage_list
1452 (otf, stream, offset,
1453 &subtable->u.chain_context3.Backtrack));
1454 subtable->u.chain_context3.InputGlyphCount
1455 = (read_coverage_list
1456 (otf, stream, offset,
1457 &subtable->u.chain_context3.Input));
1458 subtable->u.chain_context3.LookaheadGlyphCount
1459 = (read_coverage_list
1460 (otf, stream, offset,
1461 &subtable->u.chain_context3.LookAhead));
1462 subtable->u.chain_context3.SubstCount
1463 = (read_subst_lookup_record
1465 &subtable->u.chain_context3.SubstLookupRecord));
1468 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1472 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1479 read_gpos_table (OTF *otf, OTF_Stream *stream)
1481 char *errfmt = "GPOS%s";
1482 void *errret = NULL;
1485 OTF_CALLOC (gpos, 1, "");
1486 READ_FIXED (stream, gpos->Version);
1487 READ_OFFSET (stream, gpos->ScriptList.offset);
1488 READ_OFFSET (stream, gpos->FeatureList.offset);
1489 READ_OFFSET (stream, gpos->LookupList.offset);
1491 if (read_script_list (otf, stream, gpos->ScriptList.offset,
1492 &gpos->ScriptList) < 0
1493 || read_feature_list (otf, stream, gpos->FeatureList.offset,
1494 &gpos->FeatureList) < 0
1495 || read_lookup_list (otf, stream, gpos->LookupList.offset,
1496 &gpos->LookupList, 0) < 0)
1506 read_base_table (OTF_Stream *stream, long offset)
1510 OTF_MALLOC (base, 1);
1519 read_jstf_table (OTF_Stream *stream, long offset)
1523 OTF_MALLOC (jstf, 1);
1531 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1532 OTF_AttachList *list)
1534 char *errfmt = "AttachList%s";
1538 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1540 READ_UINT16 (stream, list->GlyphCount);
1541 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1542 for (i = 0; i < list->GlyphCount; i++)
1543 READ_OFFSET (stream, list->AttachPoint[i].offset);
1544 for (i = 0; i < list->GlyphCount; i++)
1548 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1549 READ_UINT16 (stream, count);
1550 list->AttachPoint[i].PointCount = count;
1551 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1552 for (j = 0; j < count; j++)
1553 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1559 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1560 OTF_CaretValue *caret)
1562 char *errfmt = "CaretValue%s";
1565 SEEK_STREAM (stream, offset + caret->offset);
1566 READ_UINT16 (stream, caret->CaretValueFormat);
1567 if (caret->CaretValueFormat == 1)
1568 READ_INT16 (stream, caret->f.f1.Coordinate);
1569 else if (caret->CaretValueFormat == 2)
1570 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1571 else if (caret->CaretValueFormat == 3)
1573 READ_INT16 (stream, caret->f.f3.Coordinate);
1574 if (read_device_table (otf, stream, offset + caret->offset,
1575 &caret->f.f3.DeviceTable) < 0)
1579 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1584 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1585 OTF_LigCaretList *list)
1587 char *errfmt = "LigCaretList%s";
1591 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1593 READ_UINT16 (stream, list->LigGlyphCount);
1594 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1595 for (i = 0; i < list->LigGlyphCount; i++)
1596 READ_OFFSET (stream, list->LigGlyph[i].offset);
1597 for (i = 0; i < list->LigGlyphCount; i++)
1601 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1602 READ_UINT16 (stream, count);
1603 list->LigGlyph[i].CaretCount = count;
1604 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1605 for (j = 0; j < count; j++)
1606 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1607 for (j = 0; j < count; j++)
1608 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1609 &list->LigGlyph[i].CaretValue[j]) < 0)
1616 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1620 READ_FIXED (stream, header->Version);
1621 READ_OFFSET (stream, header->GlyphClassDef);
1622 READ_OFFSET (stream, header->AttachList);
1623 READ_OFFSET (stream, header->LigCaretList);
1624 READ_OFFSET (stream, header->MarkAttachClassDef);
1629 read_gdef_table (OTF *otf, OTF_Stream *stream)
1631 char *errfmt = "GDEF%s";
1632 void *errret = NULL;
1635 OTF_CALLOC (gdef, 1, "");
1636 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1637 if (gdef->header.GlyphClassDef)
1639 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1640 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1642 if (gdef->header.AttachList)
1643 read_attach_list (otf, stream, gdef->header.AttachList,
1644 &gdef->attach_list);
1645 if (gdef->header.LigCaretList)
1646 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1647 &gdef->lig_caret_list);
1648 if (gdef->header.MarkAttachClassDef)
1650 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1651 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1662 read_cmap_table (OTF *otf, OTF_Stream *stream)
1664 char *errfmt = "cmap%s";
1665 void *errret = NULL;
1669 OTF_CALLOC (cmap, 1, "");
1670 READ_USHORT (stream, cmap->version);
1671 READ_USHORT (stream, cmap->numTables);
1672 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
1673 for (i = 0; i < cmap->numTables; i++)
1675 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
1676 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
1677 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
1678 if (cmap->EncodingRecord[i].platformID == 3
1679 && cmap->EncodingRecord[i].encodingID == 1)
1680 cmap->Unicode = cmap->EncodingRecord + i;
1682 for (i = 0; i < cmap->numTables; i++)
1686 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
1687 READ_USHORT (stream, format);
1688 cmap->EncodingRecord[i].subtable.format = format;
1689 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
1690 if (format == 8 || format == 10 || format == 12)
1692 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
1693 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
1697 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
1703 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
1704 " (EncodingRecord)");
1706 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
1716 OTF_EncodingSubtable4 *sub4;
1721 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
1722 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
1723 READ_USHORT (stream, sub4->segCountX2);
1724 segCount = sub4->segCountX2 / 2;
1725 READ_USHORT (stream, sub4->searchRange);
1726 READ_USHORT (stream, sub4->entrySelector);
1727 READ_USHORT (stream, sub4->rangeShift);
1728 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
1729 for (j = 0; j < segCount; j++)
1730 READ_USHORT (stream, sub4->segments[j].endCount);
1731 READ_USHORT (stream, dummy);
1732 for (j = 0; j < segCount; j++)
1733 READ_USHORT (stream, sub4->segments[j].startCount);
1734 for (j = 0; j < segCount; j++)
1735 READ_SHORT (stream, sub4->segments[j].idDelta);
1736 for (j = 0; j < segCount; j++)
1739 unsigned rest = 2 * (segCount - j);
1741 READ_USHORT (stream, off);
1743 sub4->segments[j].idRangeOffset = 0xFFFF;
1745 sub4->segments[j].idRangeOffset = (off - rest) / 2;
1747 j = (cmap->EncodingRecord[i].subtable.length
1748 - (14 + 2 * (segCount * 4 + 1)));
1749 sub4->GlyphCount = j / 2;
1750 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
1751 for (j = 0; j < sub4->GlyphCount; j++)
1752 READ_USHORT (stream, sub4->glyphIdArray[j]);
1764 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
1766 char *errfmt = "nameID (%d)";
1767 void *errret = NULL;
1768 OTF_StreamState state;
1773 SAVE_STREAM (stream, state);
1774 SEEK_STREAM (stream, stream->pos + rec->offset);
1778 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
1779 READ_BYTES (stream, str, rec->length);
1780 for (i = 0; i < rec->length; i++)
1784 else if (bytes == 2)
1786 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
1787 for (i = 0; i < rec->length / 2; i++)
1789 READ_USHORT (stream, c);
1795 else if (bytes == 4)
1797 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
1798 for (i = 0; i < rec->length / 4; i++)
1800 READ_ULONG (stream, c);
1807 RESTORE_STREAM (stream, state);
1812 read_name_table (OTF *otf, OTF_Stream *stream)
1814 char *errfmt = "name%s";
1815 void *errret = NULL;
1819 OTF_CALLOC (name, 1, "");
1820 READ_USHORT (stream, name->format);
1821 READ_USHORT (stream, name->count);
1822 READ_USHORT (stream, name->stringOffset);
1823 OTF_MALLOC (name->nameRecord, name->count, "");
1824 for (i = 0; i < name->count; i++)
1826 OTF_NameRecord *rec = name->nameRecord + i;
1828 READ_USHORT (stream, rec->platformID);
1829 READ_USHORT (stream, rec->encodingID);
1830 READ_USHORT (stream, rec->languageID);
1831 READ_USHORT (stream, rec->nameID);
1832 READ_USHORT (stream, rec->length);
1833 READ_USHORT (stream, rec->offset);
1835 for (i = 0; i < name->count; i++)
1837 OTF_NameRecord *rec = name->nameRecord + i;
1838 int nameID = rec->nameID;
1840 if (nameID <= OTF_max_nameID
1841 && ! name->name[nameID])
1843 if (rec->platformID == 0)
1844 name->name[nameID] = read_name (otf, stream, rec,
1845 rec->encodingID <= 3 ? 2 : 4);
1846 else if (rec->platformID == 1
1847 && rec->encodingID == 0)
1848 name->name[nameID] = read_name (otf, stream, rec, 1);
1849 else if (rec->platformID == 3
1850 && (rec->encodingID == 1 || rec->encodingID == 10))
1851 name->name[nameID] = read_name (otf, stream,
1852 rec, rec->encodingID == 1 ? 2 : 4);
1864 otf_tag (char *name)
1866 unsigned char *p = (unsigned char *) name;
1870 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1874 otf_tag_name (OTF_Tag tag, char *name)
1876 name[0] = (char) (tag >> 24);
1877 name[1] = (char) ((tag >> 16) & 0xFF);
1878 name[2] = (char) ((tag >> 8) & 0xFF);
1879 name[3] = (char) (tag & 0xFF);
1884 /* We can't use memory allocation macros in the following functions
1885 because those macros returns from the functions before freeing
1886 memory previously allocated. */
1889 otf_open (char *otf_name)
1892 char *errfmt = "opening otf (%s)";
1893 void *errret = NULL;
1895 OTF_InternalData *internal_data;
1897 fp = fopen (otf_name, "r");
1899 OTF_ERROR (OTF_ERROR_FILE, otf_name);
1900 otf = calloc (1, sizeof (OTF));
1902 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
1903 otf->filename = strdup (otf_name);
1904 if (! otf->filename)
1908 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
1911 internal_data = calloc (1, sizeof (OTF_InternalData));
1912 if (! internal_data)
1913 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
1914 otf->internal_data = internal_data;
1915 if (! allocate_memory_record (otf))
1916 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
1918 /* Here after, all pointers to allocated memory are recorded in
1919 otf->internal_data->memory_record except for what allocated by
1920 the functions allocate_memory_record and make_stream. */
1922 if (read_header_part (otf, fp) < 0)
1935 otf_close (OTF *otf)
1937 OTF_InternalData *internal_data = otf->internal_data;
1942 OTF_MemoryRecord *memrec = internal_data->memory_record;
1944 if (internal_data->header_stream)
1945 free_stream (internal_data->header_stream);
1947 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
1948 if (internal_data->table_info[i].stream)
1949 free_stream (internal_data->table_info[i].stream);
1953 OTF_MemoryRecord *next = memrec->next;
1955 for (i = memrec->used - 1; i >= 0; i--)
1956 free (memrec->memory[i]);
1960 free (internal_data);
1967 otf_get_table (OTF *otf, char *name)
1969 char *errfmt = "OTF Table Read";
1971 OTF_InternalData *internal_data = otf->internal_data;
1972 OTF_TableInfo *table_info;
1973 OTF_Tag tag = otf_tag (name);
1976 OTF_ERROR (OTF_ERROR_TABLE, " (unknown)");
1978 if (tag == otf_tag ("head"))
1979 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
1980 else if (tag == otf_tag ("name"))
1981 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
1982 else if (tag == otf_tag ("cmap"))
1983 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
1984 else if (tag == otf_tag ("GDEF"))
1985 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
1986 else if (tag == otf_tag ("GSUB"))
1987 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
1988 else if (tag == otf_tag ("GPOS"))
1989 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
1991 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported)");
1993 if (*table_info->address)
1995 if (! table_info->stream)
1996 OTF_ERROR (OTF_ERROR_TABLE, " (not found)");
1997 if (! table_info->reader)
1998 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2000 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2001 free_stream (table_info->stream);
2002 table_info->stream = NULL;
2003 if (! *table_info->address)
2005 table_info->reader = NULL;
2006 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");