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_LookupSubTable *subtable);
511 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
512 long offset, unsigned type,
513 OTF_LookupSubTable *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)");
539 OTF_CALLOC (lookup->SubTable, lookup->SubTableCount,
541 for (j = 0; j < lookup->SubTableCount; j++)
542 READ_OFFSET (stream, lookup->SubTableOffset[j]);
544 for (j = 0; j < lookup->SubTableCount; j++)
547 = offset + lookup->offset + lookup->SubTableOffset[j];
549 if (read_lookup_subtable_gsub (otf, stream, this_offset,
551 lookup->SubTable + j) < 0)
555 for (j = 0; j < lookup->SubTableCount; j++)
558 = offset + lookup->offset + lookup->SubTableOffset[j];
560 if (read_lookup_subtable_gpos (otf, stream, this_offset,
562 lookup->SubTable + j) < 0)
572 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids, int minus)
574 char *errfmt = "GlyphID List%s";
579 READ_UINT16 (stream, count);
582 OTF_MALLOC (*ids, count, "");
583 for (i = 0; i < count + minus; i++)
584 READ_GLYPHID (stream, (*ids)[i]);
589 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
591 char *errfmt = "RangeRecord%s";
596 READ_UINT16 (stream, count);
599 OTF_MALLOC (*record, count, "");
600 for (i = 0; i < count; i++)
602 READ_GLYPHID (stream, (*record)[i].Start);
603 READ_GLYPHID (stream, (*record)[i].End);
604 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
611 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
612 OTF_Coverage *coverage)
614 char *errfmt = "Coverage%s";
616 OTF_StreamState state;
619 READ_OFFSET (stream, coverage->offset);
620 SAVE_STREAM (stream, state);
621 SEEK_STREAM (stream, offset + coverage->offset);
622 READ_UINT16 (stream, coverage->CoverageFormat);
623 if (coverage->CoverageFormat == 1)
624 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0);
625 else if (coverage->CoverageFormat == 2)
626 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
628 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
631 coverage->Count = (unsigned) count;
632 RESTORE_STREAM (stream, state);
637 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
638 OTF_Coverage **coverage)
640 char *errfmt = "Coverage List%s";
645 READ_UINT16 (stream, count);
648 OTF_MALLOC (*coverage, count, "");
649 for (i = 0; i < count; i++)
650 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
657 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
660 char *errfmt = "ClassDef%s";
663 SEEK_STREAM (stream, class->offset);
664 READ_UINT16 (stream, class->ClassFormat);
665 if (class->ClassFormat == 1)
667 READ_GLYPHID (stream, class->f.f1.StartGlyph);
668 class->f.f1.GlyphCount
670 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
671 if (! class->f.f1.GlyphCount)
672 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
674 else if (class->ClassFormat == 2)
676 class->f.f2.ClassRangeCount
677 = (read_range_records
678 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
679 if (! class->f.f2.ClassRangeCount)
680 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
683 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
689 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
691 char *errfmt = "ClassDef%s";
693 OTF_StreamState state;
695 READ_OFFSET (stream, class->offset);
696 SAVE_STREAM (stream, state);
697 SEEK_STREAM (stream, offset + class->offset);
698 READ_UINT16 (stream, class->ClassFormat);
699 if (class->ClassFormat == 1)
701 READ_GLYPHID (stream, class->f.f1.StartGlyph);
702 class->f.f1.GlyphCount
704 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0));
705 if (! class->f.f1.GlyphCount)
708 else if (class->ClassFormat == 2)
710 class->f.f2.ClassRangeCount
711 = (read_range_records
712 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
713 if (! class->f.f2.ClassRangeCount)
717 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
719 RESTORE_STREAM (stream, state);
725 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
726 OTF_DeviceTable *table)
728 char *errfmt = "Device Table%s";
739 SEEK_STREAM (stream, offset + table->offset);
740 READ_UINT16 (stream, table->StartSize);
741 READ_UINT16 (stream, table->EndSize);
742 READ_UINT16 (stream, table->DeltaFormat);
743 num = table->EndSize - table->StartSize + 1;
744 OTF_MALLOC (table->DeltaValue, num, "");
746 if (table->DeltaFormat == 1)
747 for (i = 0; i < num; i++)
750 READ_UINT16 (stream, val);
751 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
752 table->DeltaValue[i] = intval.int2;
754 else if (table->DeltaFormat == 2)
755 for (i = 0; i < num; i++)
758 READ_UINT16 (stream, val);
759 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
760 table->DeltaValue[i] = intval.int4;
762 else if (table->DeltaFormat == 3)
763 for (i = 0; i < num; i++)
767 READ_UINT16 (stream, val);
768 intval.int8 = val >> 8;
769 table->DeltaValue[i] = intval.int8;
773 intval.int8 = val >> 8;
774 table->DeltaValue[i] = intval.int8;
778 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
786 read_gsub_table (OTF *otf, OTF_Stream *stream)
788 char *errfmt = "GSUB%s";
792 OTF_CALLOC (gsub, 1, "");
793 READ_FIXED (stream, gsub->Version);
794 READ_OFFSET (stream, gsub->ScriptList.offset);
795 READ_OFFSET (stream, gsub->FeatureList.offset);
796 READ_OFFSET (stream, gsub->LookupList.offset);
798 if (read_script_list (otf, stream, gsub->ScriptList.offset,
799 &gsub->ScriptList) < 0
800 || read_feature_list (otf, stream, gsub->FeatureList.offset,
801 &gsub->FeatureList) < 0
802 || read_lookup_list (otf, stream, gsub->LookupList.offset,
803 &gsub->LookupList, 1) < 0)
809 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
811 char *errfmt = "Sequence%s";
816 READ_UINT16 (stream, count);
817 OTF_MALLOC (*seq, count, "");
819 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
820 for (i = 0; i < count; i++)
821 READ_OFFSET (stream, (*seq)[i].offset);
822 for (i = 0; i < count; i++)
824 SEEK_STREAM (stream, offset + (*seq)[i].offset);
825 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
826 &(*seq)[i].Substitute, 0);
827 if (! (*seq)[i].GlyphCount)
834 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
835 OTF_Ligature **ligature)
837 char *errfmt = "Ligature%s";
842 READ_UINT16 (stream, count);
845 OTF_MALLOC (*ligature, count, "");
846 for (i = 0; i < count; i++)
847 READ_OFFSET (stream, (*ligature)[i].offset);
848 for (i = 0; i < count; i++)
850 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
851 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
852 (*ligature)[i].CompCount
853 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1);
854 if (! (*ligature)[i].CompCount)
861 read_ligature_set (OTF *otf, OTF_Stream *stream, long offset,
862 OTF_LigatureSet **ligset)
864 char *errfmt = "LigatureSet%s";
869 READ_UINT16 (stream, count);
872 OTF_MALLOC (*ligset, count, "");
873 for (i = 0; i < count; i++)
874 READ_OFFSET (stream, (*ligset)[i].offset);
875 for (i = 0; i < count; i++)
879 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
880 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
881 &(*ligset)[i].Ligature);
884 (*ligset)[i].LigatureCount = (unsigned) lig_count;
890 read_subst_lookup_record (OTF *otf, OTF_Stream *stream,
891 OTF_SubstLookupRecord **record)
893 char *errfmt = "SubstLookupRecord%s";
898 READ_UINT16 (stream, count);
900 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
901 OTF_MALLOC (*record, count, "");
902 for (i = 0; i < count; i++)
904 READ_UINT16 (stream, (*record)[i].SequenceIndex);
905 READ_UINT16 (stream, (*record)[i].LookupListIndex);
911 read_chain_subrule (OTF *otf, OTF_Stream *stream, long offset,
912 OTF_ChainSubRule **rule)
914 char *errfmt = "ChainSubRule%s";
919 READ_UINT16 (stream, count);
921 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
922 OTF_MALLOC (*rule, count, "");
923 for (i = 0; i < count; i++)
924 READ_OFFSET (stream, (*rule)[i].offset);
925 for (i = 0; i < count; i++)
927 SEEK_STREAM (stream, offset + (*rule)[i].offset);
928 (*rule)[i].BacktrackGlyphCount
929 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0);
930 if (! (*rule)[i].BacktrackGlyphCount)
932 (*rule)[i].InputGlyphCount
933 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1);
934 if (! (*rule)[i].InputGlyphCount)
936 (*rule)[i].LookaheadGlyphCount
937 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0);
938 if (! (*rule)[i].LookaheadGlyphCount)
940 (*rule)[i].SubstCount
941 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
942 if (! (*rule)[i].SubstCount)
950 read_chain_subrule_set (OTF *otf, OTF_Stream *stream, long offset,
951 OTF_ChainSubRuleSet **set)
953 char *errfmt = "ChainSubRuleSet%s";
958 READ_UINT16 (stream, count);
960 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
961 OTF_MALLOC (*set, count, "");
962 for (i = 0; i < count; i++)
963 READ_OFFSET (stream, (*set)[i].offset);
964 for (i = 0; i < count; i++)
966 SEEK_STREAM (stream, offset + (*set)[i].offset);
967 (*set)[i].ChainSubRuleCount
968 = read_chain_subrule (otf, stream, offset + (*set)[i].offset,
969 &(*set)[i].ChainSubRule);
970 if (! (*set)[i].ChainSubRuleCount)
977 read_chain_subclass_rule (OTF *otf, OTF_Stream *stream, long offset,
978 OTF_ChainSubClassRule **rule)
980 char *errfmt = "ChainSubClassRule%s";
985 READ_UINT16 (stream, count);
987 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
988 OTF_MALLOC (*rule, count, "");
989 for (i = 0; i < count; i++)
990 READ_OFFSET (stream, (*rule)[i].offset);
991 for (i = 0; i < count; i++)
993 SEEK_STREAM (stream, offset + (*rule)[i].offset);
994 (*rule)[i].BacktrackGlyphCount
995 = read_glyph_ids (otf, stream,
996 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0);
997 if (! (*rule)[i].BacktrackGlyphCount)
999 (*rule)[i].InputGlyphCount
1000 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Input, -1);
1001 if (! (*rule)[i].InputGlyphCount)
1003 (*rule)[i].LookaheadGlyphCount
1004 = read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].LookAhead, 0);
1005 if (! (*rule)[i].LookaheadGlyphCount)
1007 (*rule)[i].SubstCount
1008 = read_subst_lookup_record (otf, stream, &(*rule)[i].SubstLookupRecord);
1009 if (! (*rule)[i].SubstCount)
1016 read_chain_subclass_set (OTF *otf, OTF_Stream *stream, long offset,
1017 OTF_ChainSubClassSet **set)
1019 char *errfmt = "ChainSubClassSet%s";
1020 unsigned errret = 0;
1024 READ_UINT16 (stream, count);
1026 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1027 OTF_MALLOC (*set, count, "");
1028 for (i = 0; i < count; i++)
1029 READ_OFFSET (stream, (*set)[i].offset);
1030 for (i = 0; i < count; i++)
1032 SEEK_STREAM (stream, offset + (*set)[i].offset);
1033 (*set)[i].ChainSubClassRuleCnt
1034 = read_chain_subclass_rule (otf, stream, offset + (*set)[i].offset,
1035 &(*set)[i].ChainSubClassRule);
1036 if (! (*set)[i].ChainSubClassRuleCnt)
1044 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1045 unsigned type, OTF_LookupSubTable *subtable)
1047 char *errfmt = "GSUB LookupSubTable%s";
1051 SEEK_STREAM (stream, offset);
1052 READ_UINT16 (stream, subtable->Format);
1056 if (subtable->Format == 1)
1058 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1060 READ_INT16 (stream, subtable->sub.gsub.single1.DeltaGlyphID);
1062 else if (subtable->Format == 2)
1064 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1066 subtable->sub.gsub.single2.GlyphCount
1067 = read_glyph_ids (otf, stream, &subtable->sub.gsub.single2.Substitute,
1069 if (! subtable->sub.gsub.single2.GlyphCount)
1073 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1077 if (subtable->Format == 1)
1079 read_coverage (otf, stream, offset, &subtable->Coverage);
1080 subtable->sub.gsub.multiple1.SequenceCount
1081 = read_sequence (otf, stream, offset,
1082 &subtable->sub.gsub.multiple1.Sequence);
1085 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1092 if (subtable->Format == 1)
1094 read_coverage (otf, stream, offset, &subtable->Coverage);
1095 count = (read_ligature_set
1096 (otf, stream, offset,
1097 &subtable->sub.gsub.ligature1.LigatureSet));
1100 subtable->sub.gsub.ligature1.LigSetCount = (unsigned) count;
1103 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1107 if (subtable->Format == 1)
1109 read_coverage (otf, stream, offset, &subtable->Coverage);
1110 subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1111 = (read_chain_subrule_set
1112 (otf, stream, offset,
1113 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1115 else if (subtable->Format == 2)
1117 read_coverage (otf, stream, offset, &subtable->Coverage);
1118 read_class_def (otf, stream, offset,
1119 &subtable->sub.gsub.chain_context2.Backtrack);
1120 read_class_def (otf, stream, offset,
1121 &subtable->sub.gsub.chain_context2.Input);
1122 read_class_def (otf, stream, offset,
1123 &subtable->sub.gsub.chain_context2.LookAhead);
1124 subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1125 = (read_chain_subclass_set
1126 (otf, stream, offset,
1127 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1129 else if (subtable->Format == 3)
1131 count = (read_coverage_list
1132 (otf, stream, offset,
1133 &subtable->sub.gsub.chain_context3.Backtrack));
1136 subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1138 count = (read_coverage_list
1139 (otf, stream, offset,
1140 &subtable->sub.gsub.chain_context3.Input));
1143 subtable->sub.gsub.chain_context3.InputGlyphCount
1145 subtable->Coverage = subtable->sub.gsub.chain_context3.Input[0];
1146 count = (read_coverage_list
1147 (otf, stream, offset,
1148 &subtable->sub.gsub.chain_context3.LookAhead));
1149 subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1151 subtable->sub.gsub.chain_context3.SubstCount
1152 = (read_subst_lookup_record
1154 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1157 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1162 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1171 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1172 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1175 OTF_StreamState state;
1180 for (i = 0, size = 0; i < 8; i++)
1184 if (bit & OTF_XPlacement)
1185 READ_INT16 (stream, value_record->XPlacement);
1186 if (bit & OTF_XPlacement)
1187 READ_INT16 (stream, value_record->YPlacement);
1188 if (bit & OTF_XAdvance)
1189 READ_INT16 (stream, value_record->XAdvance);
1190 if (bit & OTF_YAdvance)
1191 READ_INT16 (stream, value_record->YAdvance);
1192 if (bit & OTF_XPlaDevice)
1193 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1194 if (bit & OTF_YPlaDevice)
1195 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1196 if (bit & OTF_XAdvDevice)
1197 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1198 if (bit & OTF_YAdvDevice)
1199 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1200 SAVE_STREAM (stream, state);
1201 if (value_record->XPlaDevice.offset)
1203 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1206 if (value_record->YPlaDevice.offset)
1208 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1211 if (value_record->XAdvDevice.offset)
1213 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1216 if (value_record->YAdvDevice.offset)
1218 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1221 RESTORE_STREAM (stream, state);
1227 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1229 char *errfmt = "Anchor%s";
1232 SEEK_STREAM (stream, offset + anchor->offset);
1233 READ_UINT16 (stream, anchor->AnchorFormat);
1234 READ_INT16 (stream, anchor->XCoordinate);
1235 READ_INT16 (stream, anchor->YCoordinate);
1236 if (anchor->AnchorFormat == 1)
1238 else if (anchor->AnchorFormat == 2)
1240 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1242 else if (anchor->AnchorFormat == 3)
1244 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1245 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1246 if (anchor->f.f2.XDeviceTable.offset)
1248 if (read_device_table (otf, stream, offset + anchor->offset,
1249 &anchor->f.f2.XDeviceTable) < 0)
1252 if (anchor->f.f2.YDeviceTable.offset)
1254 if (read_device_table (otf, stream, offset + anchor->offset,
1255 &anchor->f.f2.YDeviceTable) < 0)
1260 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1266 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1267 OTF_MarkArray *array)
1269 char *errfmt = "MarkArray%s";
1271 OTF_StreamState state;
1274 READ_OFFSET (stream, array->offset);
1275 SAVE_STREAM (stream, state);
1276 SEEK_STREAM (stream, offset + array->offset);
1277 READ_UINT16 (stream, array->MarkCount);
1278 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1279 for (i = 0; i < array->MarkCount; i++)
1281 READ_UINT16 (stream, array->MarkRecord[i].Class);
1282 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1284 for (i = 0; i < array->MarkCount; i++)
1285 if (read_anchor (otf, stream, offset + array->offset,
1286 &array->MarkRecord[i].MarkAnchor) < 0)
1288 RESTORE_STREAM (stream, state);
1293 read_base_array (OTF *otf, OTF_Stream *stream, long offset,
1294 unsigned ClassCount, OTF_BaseArray *array)
1296 char *errfmt = "BaseArray%s";
1298 OTF_StreamState state;
1301 READ_OFFSET (stream, array->offset);
1302 SAVE_STREAM (stream, state);
1303 SEEK_STREAM (stream, offset + array->offset);
1304 READ_UINT16 (stream, array->BaseCount);
1305 OTF_MALLOC (array->BaseRecord, array->BaseCount, "");
1306 for (i = 0; i < array->BaseCount; i++)
1308 OTF_MALLOC (array->BaseRecord[i].BaseAnchor, ClassCount,
1310 for (j = 0; j < ClassCount; j++)
1311 READ_OFFSET (stream, array->BaseRecord[i].BaseAnchor[j].offset);
1313 for (i = 0; i < array->BaseCount; i++)
1314 for (j = 0; j < ClassCount; j++)
1315 if (read_anchor (otf, stream, offset + array->offset,
1316 &array->BaseRecord[i].BaseAnchor[j]) < 0)
1318 RESTORE_STREAM (stream, state);
1323 static OTF_Class1Record *
1324 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1325 unsigned num1, enum OTF_ValueFormat bit1,
1326 unsigned num2, enum OTF_ValueFormat bit2)
1328 char *errfmt = "Class1Record%s";
1329 void *errret = NULL;
1330 OTF_Class1Record *rec;
1333 OTF_MALLOC (rec, num1, "");
1334 for (i = 0; i < num1; i++)
1336 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1337 for (j = 0; j < num2; j++)
1339 if (read_value_record (otf, stream, offset,
1340 bit1, &rec[i].Class2Record[j].Value1) < 0
1341 || read_value_record (otf, stream, offset,
1342 bit2, &rec[i].Class2Record[j].Value2) < 0)
1351 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1352 long offset, unsigned type,
1353 OTF_LookupSubTable *subtable)
1355 char *errfmt = "GPOS LookupSubTable%s";
1358 SEEK_STREAM (stream, offset);
1359 READ_UINT16 (stream, subtable->Format);
1364 if (subtable->Format == 1)
1366 read_coverage (otf, stream, offset, &subtable->Coverage);
1367 subtable->sub.gsub.single1.DeltaGlyphID = READ_INT16 (stream);
1369 else if (subtable->Format == 2)
1371 read_coverage (otf, stream, offset, &subtable->Coverage);
1372 subtable->sub.gsub.single2.GlyphCount
1373 = read_glyph_ids (otf, stream,
1374 &subtable->sub.gsub.single2.Substitute, 0);
1377 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1382 if (subtable->Format == 1)
1384 read_coverage (otf, stream, offset, &subtable->Coverage);
1386 else if (subtable->Format == 2)
1388 SEEK_STREAM (stream, offset + 2);
1389 read_coverage (otf, stream, offset, &subtable->Coverage);
1390 READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat1);
1391 READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat2);
1392 read_class_def (otf, stream, offset,
1393 &subtable->sub.gpos.pair2.ClassDef1);
1394 read_class_def (otf, stream, offset,
1395 &subtable->sub.gpos.pair2.ClassDef2);
1396 READ_UINT16 (stream, subtable->sub.gpos.pair2.Class1Count);
1397 READ_UINT16 (stream, subtable->sub.gpos.pair2.Class2Count);
1398 subtable->sub.gpos.pair2.Class1Record
1399 = read_class1_record_list (otf, stream, offset,
1400 subtable->sub.gpos.pair2.Class1Count,
1401 subtable->sub.gpos.pair2.ValueFormat1,
1402 subtable->sub.gpos.pair2.Class2Count,
1403 subtable->sub.gpos.pair2.ValueFormat2);
1406 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1410 if (subtable->Format == 1)
1412 read_coverage (otf, stream, offset, &subtable->Coverage);
1413 read_coverage (otf, stream, offset,
1414 &subtable->sub.gpos.mark_base1.BaseCoverage);
1415 READ_UINT16 (stream, subtable->sub.gpos.mark_base1.ClassCount);
1416 read_mark_array (otf, stream, offset,
1417 &subtable->sub.gpos.mark_base1.MarkArray);
1418 read_base_array (otf, stream, offset,
1419 subtable->sub.gpos.mark_base1.ClassCount,
1420 &subtable->sub.gpos.mark_base1.BaseArray);
1423 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1428 if (subtable->Format == 1)
1430 read_coverage (otf, stream, offset,
1431 &subtable->sub.gsub.chain_context1.Coverage);
1432 subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
1433 = (read_chain_subrule_set
1434 (otf, stream, offset,
1435 &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
1437 else if (subtable->Format == 2)
1439 read_coverage (otf, stream, offset,
1440 &subtable->sub.gsub.chain_context2.Coverage);
1441 read_class_def (otf, stream, offset,
1442 &subtable->sub.gsub.chain_context2.Backtrack);
1443 read_class_def (otf, stream, offset,
1444 &subtable->sub.gsub.chain_context2.Input);
1445 read_class_def (otf, stream, offset,
1446 &subtable->sub.gsub.chain_context2.LookAhead);
1447 subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
1448 = (read_chain_subclass_set
1449 (otf, stream, offset,
1450 &subtable->sub.gsub.chain_context2.ChainSubClassSet));
1452 else if (subtable->Format == 3)
1454 subtable->sub.gsub.chain_context3.BacktrackGlyphCount
1455 = (read_coverage_list
1456 (otf, stream, offset,
1457 &subtable->sub.gsub.chain_context3.Backtrack));
1458 subtable->sub.gsub.chain_context3.InputGlyphCount
1459 = (read_coverage_list
1460 (otf, stream, offset,
1461 &subtable->sub.gsub.chain_context3.Input));
1462 subtable->sub.gsub.chain_context3.LookaheadGlyphCount
1463 = (read_coverage_list
1464 (otf, stream, offset,
1465 &subtable->sub.gsub.chain_context3.LookAhead));
1466 subtable->sub.gsub.chain_context3.SubstCount
1467 = (read_subst_lookup_record
1469 &subtable->sub.gsub.chain_context3.SubstLookupRecord));
1472 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1476 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1483 read_gpos_table (OTF *otf, OTF_Stream *stream)
1485 char *errfmt = "GPOS%s";
1486 void *errret = NULL;
1489 OTF_CALLOC (gpos, 1, "");
1490 READ_FIXED (stream, gpos->Version);
1491 READ_OFFSET (stream, gpos->ScriptList.offset);
1492 READ_OFFSET (stream, gpos->FeatureList.offset);
1493 READ_OFFSET (stream, gpos->LookupList.offset);
1495 if (read_script_list (otf, stream, gpos->ScriptList.offset,
1496 &gpos->ScriptList) < 0
1497 || read_feature_list (otf, stream, gpos->FeatureList.offset,
1498 &gpos->FeatureList) < 0
1499 || read_lookup_list (otf, stream, gpos->LookupList.offset,
1500 &gpos->LookupList, 0) < 0)
1510 read_base_table (OTF_Stream *stream, long offset)
1514 OTF_MALLOC (base, 1);
1523 read_jstf_table (OTF_Stream *stream, long offset)
1527 OTF_MALLOC (jstf, 1);
1535 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1536 OTF_AttachList *list)
1538 char *errfmt = "AttachList%s";
1542 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1544 READ_UINT16 (stream, list->GlyphCount);
1545 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1546 for (i = 0; i < list->GlyphCount; i++)
1547 READ_OFFSET (stream, list->AttachPoint[i].offset);
1548 for (i = 0; i < list->GlyphCount; i++)
1552 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1553 READ_UINT16 (stream, count);
1554 list->AttachPoint[i].PointCount = count;
1555 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1556 for (j = 0; j < count; j++)
1557 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1563 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1564 OTF_CaretValue *caret)
1566 char *errfmt = "CaretValue%s";
1569 SEEK_STREAM (stream, offset + caret->offset);
1570 READ_UINT16 (stream, caret->CaretValueFormat);
1571 if (caret->CaretValueFormat == 1)
1572 READ_INT16 (stream, caret->f.f1.Coordinate);
1573 else if (caret->CaretValueFormat == 2)
1574 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1575 else if (caret->CaretValueFormat == 3)
1577 READ_INT16 (stream, caret->f.f3.Coordinate);
1578 if (read_device_table (otf, stream, offset + caret->offset,
1579 &caret->f.f3.DeviceTable) < 0)
1583 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1588 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1589 OTF_LigCaretList *list)
1591 char *errfmt = "LigCaretList%s";
1595 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1597 READ_UINT16 (stream, list->LigGlyphCount);
1598 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1599 for (i = 0; i < list->LigGlyphCount; i++)
1600 READ_OFFSET (stream, list->LigGlyph[i].offset);
1601 for (i = 0; i < list->LigGlyphCount; i++)
1605 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1606 READ_UINT16 (stream, count);
1607 list->LigGlyph[i].CaretCount = count;
1608 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1609 for (j = 0; j < count; j++)
1610 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1611 for (j = 0; j < count; j++)
1612 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1613 &list->LigGlyph[i].CaretValue[j]) < 0)
1620 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1624 READ_FIXED (stream, header->Version);
1625 READ_OFFSET (stream, header->GlyphClassDef);
1626 READ_OFFSET (stream, header->AttachList);
1627 READ_OFFSET (stream, header->LigCaretList);
1628 READ_OFFSET (stream, header->MarkAttachClassDef);
1633 read_gdef_table (OTF *otf, OTF_Stream *stream)
1635 char *errfmt = "GDEF%s";
1636 void *errret = NULL;
1639 OTF_CALLOC (gdef, 1, "");
1640 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1641 if (gdef->header.GlyphClassDef)
1643 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1644 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1646 if (gdef->header.AttachList)
1647 read_attach_list (otf, stream, gdef->header.AttachList,
1648 &gdef->attach_list);
1649 if (gdef->header.LigCaretList)
1650 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1651 &gdef->lig_caret_list);
1652 if (gdef->header.MarkAttachClassDef)
1654 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1655 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1666 read_cmap_table (OTF *otf, OTF_Stream *stream)
1668 char *errfmt = "cmap%s";
1669 void *errret = NULL;
1673 OTF_CALLOC (cmap, 1, "");
1674 READ_USHORT (stream, cmap->version);
1675 READ_USHORT (stream, cmap->numTables);
1676 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
1677 for (i = 0; i < cmap->numTables; i++)
1679 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
1680 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
1681 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
1682 if (cmap->EncodingRecord[i].platformID == 3
1683 && cmap->EncodingRecord[i].encodingID == 1)
1684 cmap->Unicode = cmap->EncodingRecord + i;
1686 for (i = 0; i < cmap->numTables; i++)
1690 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
1691 READ_USHORT (stream, format);
1692 cmap->EncodingRecord[i].subtable.format = format;
1693 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
1694 if (format == 8 || format == 10 || format == 12)
1696 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
1697 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
1701 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
1707 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
1708 " (EncodingRecord)");
1710 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
1720 OTF_EncodingSubtable4 *sub4;
1725 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
1726 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
1727 READ_USHORT (stream, sub4->segCountX2);
1728 segCount = sub4->segCountX2 / 2;
1729 READ_USHORT (stream, sub4->searchRange);
1730 READ_USHORT (stream, sub4->entrySelector);
1731 READ_USHORT (stream, sub4->rangeShift);
1732 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
1733 for (j = 0; j < segCount; j++)
1734 READ_USHORT (stream, sub4->segments[j].endCount);
1735 READ_USHORT (stream, dummy);
1736 for (j = 0; j < segCount; j++)
1737 READ_USHORT (stream, sub4->segments[j].startCount);
1738 for (j = 0; j < segCount; j++)
1739 READ_SHORT (stream, sub4->segments[j].idDelta);
1740 for (j = 0; j < segCount; j++)
1743 unsigned rest = 2 * (segCount - j);
1745 READ_USHORT (stream, off);
1747 sub4->segments[j].idRangeOffset = 0xFFFF;
1749 sub4->segments[j].idRangeOffset = (off - rest) / 2;
1751 j = (cmap->EncodingRecord[i].subtable.length
1752 - (14 + 2 * (segCount * 4 + 1)));
1753 sub4->GlyphCount = j / 2;
1754 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
1755 for (j = 0; j < sub4->GlyphCount; j++)
1756 READ_USHORT (stream, sub4->glyphIdArray[j]);
1768 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
1770 char *errfmt = "nameID (%d)";
1771 void *errret = NULL;
1772 OTF_StreamState state;
1777 SAVE_STREAM (stream, state);
1778 SEEK_STREAM (stream, stream->pos + rec->offset);
1782 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
1783 READ_BYTES (stream, str, rec->length);
1784 for (i = 0; i < rec->length; i++)
1788 else if (bytes == 2)
1790 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
1791 for (i = 0; i < rec->length / 2; i++)
1793 READ_USHORT (stream, c);
1799 else if (bytes == 4)
1801 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
1802 for (i = 0; i < rec->length / 4; i++)
1804 READ_ULONG (stream, c);
1811 RESTORE_STREAM (stream, state);
1816 read_name_table (OTF *otf, OTF_Stream *stream)
1818 char *errfmt = "name%s";
1819 void *errret = NULL;
1823 OTF_CALLOC (name, 1, "");
1824 READ_USHORT (stream, name->format);
1825 READ_USHORT (stream, name->count);
1826 READ_USHORT (stream, name->stringOffset);
1827 OTF_MALLOC (name->nameRecord, name->count, "");
1828 for (i = 0; i < name->count; i++)
1830 OTF_NameRecord *rec = name->nameRecord + i;
1832 READ_USHORT (stream, rec->platformID);
1833 READ_USHORT (stream, rec->encodingID);
1834 READ_USHORT (stream, rec->languageID);
1835 READ_USHORT (stream, rec->nameID);
1836 READ_USHORT (stream, rec->length);
1837 READ_USHORT (stream, rec->offset);
1839 for (i = 0; i < name->count; i++)
1841 OTF_NameRecord *rec = name->nameRecord + i;
1842 int nameID = rec->nameID;
1844 if (nameID <= OTF_max_nameID
1845 && ! name->name[nameID])
1847 if (rec->platformID == 0)
1848 name->name[nameID] = read_name (otf, stream, rec,
1849 rec->encodingID <= 3 ? 2 : 4);
1850 else if (rec->platformID == 1
1851 && rec->encodingID == 0)
1852 name->name[nameID] = read_name (otf, stream, rec, 1);
1853 else if (rec->platformID == 3
1854 && (rec->encodingID == 1 || rec->encodingID == 10))
1855 name->name[nameID] = read_name (otf, stream,
1856 rec, rec->encodingID == 1 ? 2 : 4);
1868 otf_tag (char *name)
1870 unsigned char *p = (unsigned char *) name;
1874 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1878 otf_tag_name (OTF_Tag tag, char *name)
1880 name[0] = (char) (tag >> 24);
1881 name[1] = (char) ((tag >> 16) & 0xFF);
1882 name[2] = (char) ((tag >> 8) & 0xFF);
1883 name[3] = (char) (tag & 0xFF);
1888 /* We can't use memory allocation macros in the following functions
1889 because those macros returns from the functions before freeing
1890 memory previously allocated. */
1893 otf_open (char *otf_name)
1896 char *errfmt = "opening otf (%s)";
1897 void *errret = NULL;
1899 OTF_InternalData *internal_data;
1901 fp = fopen (otf_name, "r");
1903 OTF_ERROR (OTF_ERROR_FILE, otf_name);
1904 otf = calloc (1, sizeof (OTF));
1906 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
1907 otf->filename = strdup (otf_name);
1908 if (! otf->filename)
1912 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
1915 internal_data = calloc (1, sizeof (OTF_InternalData));
1916 if (! internal_data)
1917 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
1918 otf->internal_data = internal_data;
1919 if (! allocate_memory_record (otf))
1920 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
1922 /* Here after, all pointers to allocated memory are recorded in
1923 otf->internal_data->memory_record except for what allocated by
1924 the functions allocate_memory_record and make_stream. */
1926 if (read_header_part (otf, fp) < 0)
1939 otf_close (OTF *otf)
1941 OTF_InternalData *internal_data = otf->internal_data;
1944 //if (otf->filename)
1945 //free (otf->filename);
1948 OTF_MemoryRecord *memrec = internal_data->memory_record;
1950 if (internal_data->header_stream)
1951 free_stream (internal_data->header_stream);
1953 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
1954 if (internal_data->table_info[i].stream)
1955 free_stream (internal_data->table_info[i].stream);
1959 OTF_MemoryRecord *next = memrec->next;
1961 for (i = memrec->used - 1; i >= 0; i--)
1962 free (memrec->memory[i]);
1966 free (internal_data);
1972 otf_get_table (OTF *otf, char *name)
1974 char *errfmt = "OTF Table Read";
1976 OTF_InternalData *internal_data = otf->internal_data;
1977 OTF_TableInfo *table_info;
1978 OTF_Tag tag = otf_tag (name);
1981 OTF_ERROR (OTF_ERROR_TABLE, " (unknown)");
1983 if (tag == otf_tag ("head"))
1984 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
1985 else if (tag == otf_tag ("name"))
1986 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
1987 else if (tag == otf_tag ("cmap"))
1988 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
1989 else if (tag == otf_tag ("GDEF"))
1990 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
1991 else if (tag == otf_tag ("GSUB"))
1992 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
1993 else if (tag == otf_tag ("GPOS"))
1994 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
1996 OTF_ERROR (OTF_ERROR_TABLE, " (unsupported)");
1998 if (*table_info->address)
2000 if (! table_info->stream)
2001 OTF_ERROR (OTF_ERROR_TABLE, " (not found)");
2002 if (! table_info->reader)
2003 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2005 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2006 free_stream (table_info->stream);
2007 table_info->stream = NULL;
2008 if (! *table_info->address)
2010 table_info->reader = NULL;
2011 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");