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 = calloc (1, 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)
567 /* Read Glyph-IDs from STREAM. Allocate memory for IDS, and store the
568 Glyph-IDs there. If COUNT is negative, read the number of
569 Glyphs-IDs at first. MINUS if nozero is how few the actual
570 Glyph-IDs are in STREAM than COUNT. */
573 read_glyph_ids (OTF *otf, OTF_Stream *stream, OTF_GlyphID **ids,
574 int minus, int count)
576 char *errfmt = "GlyphID List%s";
581 READ_UINT16 (stream, count);
584 OTF_MALLOC (*ids, count, "");
585 for (i = 0; i < count + minus; i++)
586 READ_GLYPHID (stream, (*ids)[i]);
591 read_range_records (OTF *otf, OTF_Stream *stream, OTF_RangeRecord **record)
593 char *errfmt = "RangeRecord%s";
598 READ_UINT16 (stream, count);
601 OTF_MALLOC (*record, count, "");
602 for (i = 0; i < count; i++)
604 READ_GLYPHID (stream, (*record)[i].Start);
605 READ_GLYPHID (stream, (*record)[i].End);
606 READ_UINT16 (stream, (*record)[i].StartCoverageIndex);
613 read_coverage (OTF *otf, OTF_Stream *stream, long offset,
614 OTF_Coverage *coverage)
616 char *errfmt = "Coverage%s";
618 OTF_StreamState state;
621 READ_OFFSET (stream, coverage->offset);
622 SAVE_STREAM (stream, state);
623 SEEK_STREAM (stream, offset + coverage->offset);
624 READ_UINT16 (stream, coverage->CoverageFormat);
625 if (coverage->CoverageFormat == 1)
626 count = read_glyph_ids (otf, stream, &coverage->table.GlyphArray, 0, -1);
627 else if (coverage->CoverageFormat == 2)
628 count = read_range_records (otf, stream, &coverage->table.RangeRecord);
630 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid Format)");
633 coverage->Count = (unsigned) count;
634 RESTORE_STREAM (stream, state);
638 /* Read list of Coverages from STREAM. Allocate memory for COVERAGE,
639 and store the Coverages there. If COUNT is negative, read the
640 number of Coverages at first. */
643 read_coverage_list (OTF *otf, OTF_Stream *stream, long offset,
644 OTF_Coverage **coverage, int count)
646 char *errfmt = "Coverage List%s";
651 READ_UINT16 (stream, count);
654 OTF_MALLOC (*coverage, count, "");
655 for (i = 0; i < count; i++)
656 if (read_coverage (otf, stream, offset, (*coverage) + i) < 0)
663 read_class_def_without_offset (OTF *otf, OTF_Stream *stream,
666 char *errfmt = "ClassDef%s";
669 SEEK_STREAM (stream, class->offset);
670 READ_UINT16 (stream, class->ClassFormat);
671 if (class->ClassFormat == 1)
673 READ_GLYPHID (stream, class->f.f1.StartGlyph);
674 class->f.f1.GlyphCount
676 (otf, stream, (OTF_GlyphID **) &class->f.f1.ClassValueArray, 0, -1));
677 if (! class->f.f1.GlyphCount)
678 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
680 else if (class->ClassFormat == 2)
682 class->f.f2.ClassRangeCount
683 = (read_range_records
684 (otf, stream, (OTF_RangeRecord **) &class->f.f2.ClassRangeRecord));
685 if (! class->f.f2.ClassRangeCount)
686 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
689 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
695 read_class_def (OTF *otf, OTF_Stream *stream, long offset, OTF_ClassDef *class)
697 char *errfmt = "ClassDef%s";
699 OTF_StreamState state;
701 READ_OFFSET (stream, class->offset);
704 SAVE_STREAM (stream, state);
705 SEEK_STREAM (stream, offset + class->offset);
706 READ_UINT16 (stream, class->ClassFormat);
707 if (class->ClassFormat == 1)
709 READ_GLYPHID (stream, class->f.f1.StartGlyph);
710 class->f.f1.GlyphCount
711 = read_glyph_ids (otf, stream,
712 (OTF_GlyphID **) &class->f.f1.ClassValueArray,
714 if (! class->f.f1.GlyphCount)
717 else if (class->ClassFormat == 2)
719 class->f.f2.ClassRangeCount
720 = read_range_records (otf, stream,
722 &class->f.f2.ClassRangeRecord);
723 if (! class->f.f2.ClassRangeCount)
727 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
729 RESTORE_STREAM (stream, state);
735 read_device_table (OTF *otf, OTF_Stream *stream, long offset,
736 OTF_DeviceTable *table)
738 char *errfmt = "Device Table%s";
749 SEEK_STREAM (stream, offset + table->offset);
750 READ_UINT16 (stream, table->StartSize);
751 READ_UINT16 (stream, table->EndSize);
752 READ_UINT16 (stream, table->DeltaFormat);
753 num = table->EndSize - table->StartSize + 1;
754 OTF_MALLOC (table->DeltaValue, num, "");
756 if (table->DeltaFormat == 1)
757 for (i = 0; i < num; i++)
760 READ_UINT16 (stream, val);
761 intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
762 table->DeltaValue[i] = intval.int2;
764 else if (table->DeltaFormat == 2)
765 for (i = 0; i < num; i++)
768 READ_UINT16 (stream, val);
769 intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
770 table->DeltaValue[i] = intval.int4;
772 else if (table->DeltaFormat == 3)
773 for (i = 0; i < num; i++)
777 READ_UINT16 (stream, val);
778 intval.int8 = val >> 8;
779 table->DeltaValue[i] = intval.int8;
783 intval.int8 = val >> 8;
784 table->DeltaValue[i] = intval.int8;
788 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
793 read_lookup_record_list (OTF *otf, OTF_Stream *stream,
794 OTF_LookupRecord **record, int count)
796 char *errfmt = "LookupRecord%s";
801 READ_UINT16 (stream, count);
803 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
804 OTF_MALLOC (*record, count, "");
805 for (i = 0; i < count; i++)
807 READ_UINT16 (stream, (*record)[i].SequenceIndex);
808 READ_UINT16 (stream, (*record)[i].LookupListIndex);
814 read_rule_list (OTF *otf, OTF_Stream *stream, long offset, OTF_Rule **rule)
816 char *errfmt = "List of Rule%s";
818 OTF_StreamState state;
822 READ_UINT16 (stream, count);
824 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
825 OTF_MALLOC (*rule, count, "");
826 for (i = 0; i < count; i++)
828 READ_OFFSET (stream, (*rule)[i].offset);
829 if (! (*rule)[i].offset)
830 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
832 SAVE_STREAM (stream, state);
833 for (i = 0; i < count; i++)
835 SEEK_STREAM (stream, offset + (*rule)[i].offset);
836 READ_UINT16 (stream, (*rule)[i].GlyphCount);
837 if ((*rule)[i].GlyphCount == 0)
838 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
839 READ_UINT16 (stream, (*rule)[i].LookupCount);
840 if (read_glyph_ids (otf, stream, &(*rule)[i].Input, 0,
841 (*rule)[i].GlyphCount) < 0)
843 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
844 (*rule)[i].LookupCount) == 0)
847 RESTORE_STREAM (stream, state);
853 read_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
856 char *errfmt = "List of RuleSet%s";
858 OTF_StreamState state;
862 READ_UINT16 (stream, count);
864 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
865 OTF_MALLOC (*set, count, "");
866 for (i = 0; i < count; i++)
868 READ_OFFSET (stream, (*set)[i].offset);
869 if (! (*set)[i].offset)
870 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
872 SAVE_STREAM (stream, state);
873 for (i = 0; i < count; i++)
875 SEEK_STREAM (stream, offset + (*set)[i].offset);
877 = read_rule_list (otf, stream, offset + (*set)[i].offset,
879 if (! (*set)[i].RuleCount)
882 RESTORE_STREAM (stream, state);
887 read_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
888 OTF_ClassRule **rule)
890 char *errfmt = "ClassRule%s";
892 OTF_StreamState state;
896 READ_UINT16 (stream, count);
898 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
899 OTF_MALLOC (*rule, count, "");
900 for (i = 0; i < count; i++)
902 READ_OFFSET (stream, (*rule)[i].offset);
903 if (! (*rule)[i].offset)
904 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
906 SAVE_STREAM (stream, state);
907 for (i = 0; i < count; i++)
909 SEEK_STREAM (stream, offset + (*rule)[i].offset);
910 READ_USHORT (stream, (*rule)[i].GlyphCount);
911 if (! (*rule)[i].GlyphCount)
912 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
913 READ_USHORT (stream, (*rule)[i].LookupCount);
914 if (read_glyph_ids (otf, stream, (OTF_GlyphID **) &(*rule)[i].Class,
915 0, (*rule)[i].GlyphCount - 1) < 0)
917 if (read_lookup_record_list (otf, stream, &(*rule)[i].LookupRecord,
918 (*rule)[i].LookupCount) == 0)
921 RESTORE_STREAM (stream, state);
926 read_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
929 char *errfmt = "ClassSet%s";
931 OTF_StreamState state;
935 READ_UINT16 (stream, count);
937 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
938 OTF_MALLOC (*set, count, "");
939 for (i = 0; i < count; i++)
940 /* Offset can be zero. */
941 READ_OFFSET (stream, (*set)[i].offset);
942 SAVE_STREAM (stream, state);
943 for (i = 0; i < count; i++)
944 if ((*set)[i].offset)
946 SEEK_STREAM (stream, offset + (*set)[i].offset);
947 (*set)[i].ClassRuleCnt
948 = read_class_rule_list (otf, stream, offset + (*set)[i].offset,
949 &(*set)[i].ClassRule);
950 if (! (*set)[i].ClassRuleCnt)
953 RESTORE_STREAM (stream, state);
958 read_chain_rule_list (OTF *otf, OTF_Stream *stream, long offset,
959 OTF_ChainRule **rule)
961 char *errfmt = "ChainRule%s";
966 READ_UINT16 (stream, count);
968 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
969 OTF_MALLOC (*rule, count, "");
970 for (i = 0; i < count; i++)
971 READ_OFFSET (stream, (*rule)[i].offset);
972 for (i = 0; i < count; i++)
974 SEEK_STREAM (stream, offset + (*rule)[i].offset);
975 (*rule)[i].BacktrackGlyphCount
976 = read_glyph_ids (otf, stream, &(*rule)[i].Backtrack, 0, -1);
977 (*rule)[i].InputGlyphCount
978 = read_glyph_ids (otf, stream, &(*rule)[i].Input, -1, -1);
979 if (! (*rule)[i].InputGlyphCount)
980 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
981 (*rule)[i].LookaheadGlyphCount
982 = read_glyph_ids (otf, stream, &(*rule)[i].LookAhead, 0, -1);
983 (*rule)[i].LookupCount
984 = read_lookup_record_list (otf, stream,
985 &(*rule)[i].LookupRecord, -1);
986 if (! (*rule)[i].LookupCount)
994 read_chain_rule_set_list (OTF *otf, OTF_Stream *stream, long offset,
995 OTF_ChainRuleSet **set)
997 char *errfmt = "ChainRuleSet%s";
999 OTF_StreamState state;
1003 READ_UINT16 (stream, count);
1005 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1006 OTF_MALLOC (*set, count, "");
1007 for (i = 0; i < count; i++)
1009 READ_OFFSET (stream, (*set)[i].offset);
1010 if (! (*set)[i].offset)
1011 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1013 SAVE_STREAM (stream, state);
1014 for (i = 0; i < count; i++)
1016 SEEK_STREAM (stream, offset + (*set)[i].offset);
1017 (*set)[i].ChainRuleCount
1018 = read_chain_rule_list (otf, stream, offset + (*set)[i].offset,
1019 &(*set)[i].ChainRule);
1020 if (! (*set)[i].ChainRuleCount)
1023 RESTORE_STREAM (stream, state);
1028 read_chain_class_rule_list (OTF *otf, OTF_Stream *stream, long offset,
1029 OTF_ChainClassRule **rule)
1031 char *errfmt = "ChainClassRule%s";
1032 unsigned errret = 0;
1036 READ_UINT16 (stream, count);
1038 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1039 OTF_MALLOC (*rule, count, "");
1040 for (i = 0; i < count; i++)
1042 READ_OFFSET (stream, (*rule)[i].offset);
1043 if (! (*rule)[i].offset)
1044 OTF_ERROR (OTF_ERROR_TABLE, " (zero offset)");
1046 for (i = 0; i < count; i++)
1048 SEEK_STREAM (stream, offset + (*rule)[i].offset);
1049 (*rule)[i].BacktrackGlyphCount
1050 = read_glyph_ids (otf, stream,
1051 (OTF_GlyphID **) &(*rule)[i].Backtrack, 0, -1);
1052 (*rule)[i].InputGlyphCount
1053 = read_glyph_ids (otf, stream,
1054 (OTF_GlyphID **) &(*rule)[i].Input, -1, -1);
1055 if (! (*rule)[i].InputGlyphCount)
1056 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1057 (*rule)[i].LookaheadGlyphCount
1058 = read_glyph_ids (otf, stream,
1059 (OTF_GlyphID **) &(*rule)[i].LookAhead, 0, -1);
1060 (*rule)[i].LookupCount
1061 = read_lookup_record_list (otf, stream,
1062 &(*rule)[i].LookupRecord, -1);
1063 if (! (*rule)[i].LookupCount)
1070 read_chain_class_set_list (OTF *otf, OTF_Stream *stream, long offset,
1071 OTF_ChainClassSet **set)
1073 char *errfmt = "ChainClassSet%s";
1074 unsigned errret = 0;
1075 OTF_StreamState state;
1079 READ_UINT16 (stream, count);
1081 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1082 OTF_MALLOC (*set, count, "");
1083 for (i = 0; i < count; i++)
1084 /* Offset may be zero. */
1085 READ_OFFSET (stream, (*set)[i].offset);
1086 SAVE_STREAM (stream, state);
1087 for (i = 0; i < count; i++)
1088 if ((*set)[i].offset)
1090 SEEK_STREAM (stream, offset + (*set)[i].offset);
1091 (*set)[i].ChainClassRuleCnt
1092 = read_chain_class_rule_list (otf, stream, offset + (*set)[i].offset,
1093 &(*set)[i].ChainClassRule);
1094 if (! (*set)[i].ChainClassRuleCnt)
1097 RESTORE_STREAM (stream, state);
1105 read_gsub_table (OTF *otf, OTF_Stream *stream)
1107 char *errfmt = "GSUB%s";
1108 void *errret = NULL;
1111 OTF_CALLOC (gsub, 1, "");
1112 READ_FIXED (stream, gsub->Version);
1113 READ_OFFSET (stream, gsub->ScriptList.offset);
1114 READ_OFFSET (stream, gsub->FeatureList.offset);
1115 READ_OFFSET (stream, gsub->LookupList.offset);
1117 if (read_script_list (otf, stream, gsub->ScriptList.offset,
1118 &gsub->ScriptList) < 0
1119 || read_feature_list (otf, stream, gsub->FeatureList.offset,
1120 &gsub->FeatureList) < 0
1121 || read_lookup_list (otf, stream, gsub->LookupList.offset,
1122 &gsub->LookupList, 1) < 0)
1128 read_sequence (OTF *otf, OTF_Stream *stream, long offset, OTF_Sequence **seq)
1130 char *errfmt = "Sequence%s";
1131 unsigned errret = 0;
1135 READ_UINT16 (stream, count);
1137 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1138 OTF_MALLOC (*seq, count, "");
1139 for (i = 0; i < count; i++)
1140 READ_OFFSET (stream, (*seq)[i].offset);
1141 for (i = 0; i < count; i++)
1143 SEEK_STREAM (stream, offset + (*seq)[i].offset);
1144 (*seq)[i].GlyphCount = read_glyph_ids (otf, stream,
1145 &(*seq)[i].Substitute, 0, -1);
1146 if (! (*seq)[i].GlyphCount)
1153 read_ligature (OTF *otf, OTF_Stream *stream, long offset,
1154 OTF_Ligature **ligature)
1156 char *errfmt = "Ligature%s";
1161 READ_UINT16 (stream, count);
1164 OTF_MALLOC (*ligature, count, "");
1165 for (i = 0; i < count; i++)
1166 READ_OFFSET (stream, (*ligature)[i].offset);
1167 for (i = 0; i < count; i++)
1169 SEEK_STREAM (stream, offset + (*ligature)[i].offset);
1170 READ_GLYPHID (stream, (*ligature)[i].LigGlyph);
1171 (*ligature)[i].CompCount
1172 = read_glyph_ids (otf, stream, &(*ligature)[i].Component, -1, -1);
1173 if (! (*ligature)[i].CompCount)
1180 read_ligature_set_list (OTF *otf, OTF_Stream *stream, long offset,
1181 OTF_LigatureSet **ligset)
1183 char *errfmt = "LigatureSet%s";
1188 READ_UINT16 (stream, count);
1191 OTF_MALLOC (*ligset, count, "");
1192 for (i = 0; i < count; i++)
1193 READ_OFFSET (stream, (*ligset)[i].offset);
1194 for (i = 0; i < count; i++)
1198 SEEK_STREAM (stream, offset + (*ligset)[i].offset);
1199 lig_count = read_ligature (otf, stream, offset + (*ligset)[i].offset,
1200 &(*ligset)[i].Ligature);
1203 (*ligset)[i].LigatureCount = (unsigned) lig_count;
1209 read_alternate_set_list (OTF *otf, OTF_Stream *stream, long offset,
1210 OTF_AlternateSet **altset)
1212 char *errfmt = "AlternateSet%s";
1217 READ_UINT16 (stream, count);
1219 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1220 OTF_MALLOC (*altset, count, "");
1221 for (i = 0; i < count; i++)
1222 READ_OFFSET (stream, (*altset)[i].offset);
1223 for (i = 0; i < count; i++)
1227 SEEK_STREAM (stream, offset + (*altset)[i].offset);
1228 alt_count = read_glyph_ids (otf, stream, &(*altset)[i].Alternate, 0, -1);
1231 (*altset)[i].GlyphCount = (unsigned) alt_count;
1237 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
1238 unsigned type, OTF_LookupSubTableGSUB *subtable)
1244 SEEK_STREAM (stream, offset);
1245 READ_UINT16 (stream, subtable->Format);
1246 sprintf (errfmt, "GSUB Lookup %d-%d%%s", type, subtable->Format);
1250 if (subtable->Format == 1)
1252 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1254 READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
1256 else if (subtable->Format == 2)
1258 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1260 subtable->u.single2.GlyphCount
1261 = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
1263 if (! subtable->u.single2.GlyphCount)
1267 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1271 if (subtable->Format == 1)
1273 read_coverage (otf, stream, offset, &subtable->Coverage);
1274 subtable->u.multiple1.SequenceCount
1275 = read_sequence (otf, stream, offset,
1276 &subtable->u.multiple1.Sequence);
1279 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1283 if (subtable->Format == 1)
1285 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1287 subtable->u.alternate1.AlternateSetCount
1288 = read_alternate_set_list (otf, stream, offset,
1289 &subtable->u.alternate1.AlternateSet);
1290 if (! subtable->u.alternate1.AlternateSetCount)
1294 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1298 if (subtable->Format == 1)
1300 if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
1302 subtable->u.ligature1.LigSetCount
1303 = read_ligature_set_list (otf, stream, offset,
1304 &subtable->u.ligature1.LigatureSet);
1305 if (! subtable->u.ligature1.LigSetCount)
1309 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1313 if (subtable->Format == 1)
1315 OTF_GSUB_Context1 *context1 = &subtable->u.context1;
1317 read_coverage (otf, stream, offset, &subtable->Coverage);
1318 context1->SubRuleSetCount
1319 = read_rule_set_list (otf, stream, offset, &context1->SubRuleSet);
1321 else if (subtable->Format == 2)
1323 OTF_GSUB_Context2 *context2 = &subtable->u.context2;
1325 read_coverage (otf, stream, offset, &subtable->Coverage);
1326 read_class_def (otf, stream, offset, &context2->ClassDef);
1327 context2->SubClassSetCnt
1328 = read_class_set_list (otf, stream, offset, &context2->SubClassSet);
1330 else if (subtable->Format == 3)
1332 OTF_GSUB_Context3 *context3 = &subtable->u.context3;
1334 READ_USHORT (stream, context3->GlyphCount);
1335 if (context3->GlyphCount < 0)
1336 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1337 READ_USHORT (stream, context3->SubstCount);
1338 if (read_coverage_list (otf, stream, offset,
1339 &context3->Coverage,
1340 context3->GlyphCount) < 0)
1342 if (read_lookup_record_list (otf, stream,
1343 &context3->LookupRecord,
1344 context3->SubstCount) < 0)
1348 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1352 if (subtable->Format == 1)
1354 OTF_GSUB_ChainContext1 *chain_context1 = &subtable->u.chain_context1;
1356 read_coverage (otf, stream, offset, &subtable->Coverage);
1357 chain_context1->ChainSubRuleSetCount
1358 = read_chain_rule_set_list (otf, stream, offset,
1359 &chain_context1->ChainSubRuleSet);
1361 else if (subtable->Format == 2)
1363 OTF_GSUB_ChainContext2 *chain_context2 = &subtable->u.chain_context2;
1365 read_coverage (otf, stream, offset, &subtable->Coverage);
1366 read_class_def (otf, stream, offset, &chain_context2->Backtrack);
1367 read_class_def (otf, stream, offset, &chain_context2->Input);
1368 read_class_def (otf, stream, offset, &chain_context2->LookAhead);
1369 chain_context2->ChainSubClassSetCnt
1370 = read_chain_class_set_list (otf, stream, offset,
1371 &chain_context2->ChainSubClassSet);
1373 else if (subtable->Format == 3)
1375 OTF_GSUB_ChainContext3 *chain_context3 = &subtable->u.chain_context3;
1377 count = read_coverage_list (otf, stream, offset,
1378 &chain_context3->Backtrack, -1);
1381 chain_context3->BacktrackGlyphCount = (unsigned) count;
1382 count = read_coverage_list (otf, stream, offset,
1383 &chain_context3->Input, -1);
1386 chain_context3->InputGlyphCount = (unsigned) count;
1387 subtable->Coverage = chain_context3->Input[0];
1388 count = read_coverage_list (otf, stream, offset,
1389 &chain_context3->LookAhead, -1);
1390 chain_context3->LookaheadGlyphCount = (unsigned) count;
1391 chain_context3->SubstCount
1392 = read_lookup_record_list (otf, stream,
1393 &chain_context3->LookupRecord, -1);
1396 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1401 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1405 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1414 read_value_record (OTF *otf, OTF_Stream *stream, long offset,
1415 enum OTF_ValueFormat bit, OTF_ValueRecord *value_record)
1418 OTF_StreamState state;
1423 for (i = 0, size = 0; i < 8; i++)
1427 if (bit & OTF_XPlacement)
1428 READ_INT16 (stream, value_record->XPlacement);
1429 if (bit & OTF_XPlacement)
1430 READ_INT16 (stream, value_record->YPlacement);
1431 if (bit & OTF_XAdvance)
1432 READ_INT16 (stream, value_record->XAdvance);
1433 if (bit & OTF_YAdvance)
1434 READ_INT16 (stream, value_record->YAdvance);
1435 if (bit & OTF_XPlaDevice)
1436 READ_OFFSET (stream, value_record->XPlaDevice.offset);
1437 if (bit & OTF_YPlaDevice)
1438 READ_OFFSET (stream, value_record->YPlaDevice.offset);
1439 if (bit & OTF_XAdvDevice)
1440 READ_OFFSET (stream, value_record->XAdvDevice.offset);
1441 if (bit & OTF_YAdvDevice)
1442 READ_OFFSET (stream, value_record->YAdvDevice.offset);
1443 SAVE_STREAM (stream, state);
1444 if (value_record->XPlaDevice.offset)
1446 if (read_device_table (otf, stream, offset, &value_record->XPlaDevice) < 0)
1449 if (value_record->YPlaDevice.offset)
1451 if (read_device_table (otf, stream, offset, &value_record->YPlaDevice) < 0)
1454 if (value_record->XAdvDevice.offset)
1456 if (read_device_table (otf, stream, offset, &value_record->XAdvDevice) < 0)
1459 if (value_record->YAdvDevice.offset)
1461 if (read_device_table (otf, stream, offset, &value_record->YAdvDevice) < 0)
1464 RESTORE_STREAM (stream, state);
1470 read_anchor (OTF *otf, OTF_Stream *stream, long offset, OTF_Anchor *anchor)
1472 char *errfmt = "Anchor%s";
1475 SEEK_STREAM (stream, offset + anchor->offset);
1476 READ_UINT16 (stream, anchor->AnchorFormat);
1477 READ_INT16 (stream, anchor->XCoordinate);
1478 READ_INT16 (stream, anchor->YCoordinate);
1479 if (anchor->AnchorFormat == 1)
1481 else if (anchor->AnchorFormat == 2)
1483 READ_UINT16 (stream, anchor->f.f1.AnchorPoint);
1485 else if (anchor->AnchorFormat == 3)
1487 READ_OFFSET (stream, anchor->f.f2.XDeviceTable.offset);
1488 READ_OFFSET (stream, anchor->f.f2.YDeviceTable.offset);
1489 if (anchor->f.f2.XDeviceTable.offset)
1491 if (read_device_table (otf, stream, offset + anchor->offset,
1492 &anchor->f.f2.XDeviceTable) < 0)
1495 if (anchor->f.f2.YDeviceTable.offset)
1497 if (read_device_table (otf, stream, offset + anchor->offset,
1498 &anchor->f.f2.YDeviceTable) < 0)
1503 OTF_ERROR (OTF_ERROR_TABLE, " (invalid format)");
1509 read_mark_array (OTF *otf, OTF_Stream *stream, long offset,
1510 OTF_MarkArray *array)
1512 char *errfmt = "MarkArray%s";
1514 OTF_StreamState state;
1517 READ_OFFSET (stream, array->offset);
1518 SAVE_STREAM (stream, state);
1519 SEEK_STREAM (stream, offset + array->offset);
1520 READ_UINT16 (stream, array->MarkCount);
1521 OTF_MALLOC (array->MarkRecord, array->MarkCount, "");
1522 for (i = 0; i < array->MarkCount; i++)
1524 READ_UINT16 (stream, array->MarkRecord[i].Class);
1525 READ_OFFSET (stream, array->MarkRecord[i].MarkAnchor.offset);
1527 for (i = 0; i < array->MarkCount; i++)
1528 if (read_anchor (otf, stream, offset + array->offset,
1529 &array->MarkRecord[i].MarkAnchor) < 0)
1531 RESTORE_STREAM (stream, state);
1536 read_anchor_array (OTF *otf, OTF_Stream *stream, long offset,
1537 unsigned ClassCount, OTF_AnchorArray *array)
1539 char *errfmt = "AnchorArray%s";
1541 OTF_StreamState state;
1544 READ_OFFSET (stream, array->offset);
1545 SAVE_STREAM (stream, state);
1546 SEEK_STREAM (stream, offset + array->offset);
1547 READ_UINT16 (stream, array->Count);
1548 OTF_MALLOC (array->AnchorRecord, array->Count, "");
1549 for (i = 0; i < array->Count; i++)
1551 OTF_MALLOC (array->AnchorRecord[i].Anchor, ClassCount,
1553 for (j = 0; j < ClassCount; j++)
1554 READ_OFFSET (stream, array->AnchorRecord[i].Anchor[j].offset);
1556 for (i = 0; i < array->Count; i++)
1557 for (j = 0; j < ClassCount; j++)
1558 if (read_anchor (otf, stream, offset + array->offset,
1559 &array->AnchorRecord[i].Anchor[j]) < 0)
1561 RESTORE_STREAM (stream, state);
1566 static OTF_Class1Record *
1567 read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
1568 unsigned num1, enum OTF_ValueFormat bit1,
1569 unsigned num2, enum OTF_ValueFormat bit2)
1571 char *errfmt = "Class1Record%s";
1572 void *errret = NULL;
1573 OTF_Class1Record *rec;
1576 OTF_MALLOC (rec, num1, "");
1577 for (i = 0; i < num1; i++)
1579 OTF_CALLOC (rec[i].Class2Record, num2, " (Class2Record)");
1580 for (j = 0; j < num2; j++)
1582 if (read_value_record (otf, stream, offset,
1583 bit1, &rec[i].Class2Record[j].Value1) < 0
1584 || read_value_record (otf, stream, offset,
1585 bit2, &rec[i].Class2Record[j].Value2) < 0)
1593 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
1594 long offset, unsigned type,
1595 OTF_LookupSubTableGPOS *subtable)
1601 SEEK_STREAM (stream, offset);
1602 READ_UINT16 (stream, subtable->Format);
1603 sprintf (errfmt, "GPOS Lookup %d-%d%%s", type, subtable->Format);
1607 if (subtable->Format == 1)
1609 READ_UINT16 (stream, subtable->u.single1.ValueFormat);
1610 read_value_record (otf, stream, offset,
1611 subtable->u.single1.ValueFormat,
1612 &subtable->u.single1.Value);
1614 else if (subtable->Format == 2)
1616 OTF_GPOS_Single2 *single2 = &subtable->u.single2;
1619 READ_UINT16 (stream, single2->ValueFormat);
1620 READ_UINT16 (stream, single2->ValueCount);
1621 OTF_CALLOC (single2->Value, single2->ValueCount," (ValueRecord)");
1622 for (i = 0; i < single2->ValueCount; i++)
1623 read_value_record (otf, stream, offset, single2->ValueFormat,
1624 single2->Value + i);
1627 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1631 if (subtable->Format == 1)
1633 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1635 else if (subtable->Format == 2)
1637 SEEK_STREAM (stream, offset + 2);
1638 read_coverage (otf, stream, offset, &subtable->Coverage);
1639 READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
1640 READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
1641 read_class_def (otf, stream, offset,
1642 &subtable->u.pair2.ClassDef1);
1643 read_class_def (otf, stream, offset,
1644 &subtable->u.pair2.ClassDef2);
1645 READ_UINT16 (stream, subtable->u.pair2.Class1Count);
1646 READ_UINT16 (stream, subtable->u.pair2.Class2Count);
1647 subtable->u.pair2.Class1Record
1648 = read_class1_record_list (otf, stream, offset,
1649 subtable->u.pair2.Class1Count,
1650 subtable->u.pair2.ValueFormat1,
1651 subtable->u.pair2.Class2Count,
1652 subtable->u.pair2.ValueFormat2);
1655 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1659 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1663 if (subtable->Format == 1)
1665 read_coverage (otf, stream, offset, &subtable->Coverage);
1666 read_coverage (otf, stream, offset,
1667 &subtable->u.mark_base1.BaseCoverage);
1668 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
1669 read_mark_array (otf, stream, offset,
1670 &subtable->u.mark_base1.MarkArray);
1671 read_anchor_array (otf, stream, offset,
1672 subtable->u.mark_base1.ClassCount,
1673 &subtable->u.mark_base1.BaseArray);
1676 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1680 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1684 if (subtable->Format == 1)
1686 read_coverage (otf, stream, offset, &subtable->Coverage);
1687 read_coverage (otf, stream, offset,
1688 &subtable->u.mark_mark1.Mark2Coverage);
1689 READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
1690 read_mark_array (otf, stream, offset,
1691 &subtable->u.mark_mark1.Mark1Array);
1692 read_anchor_array (otf, stream, offset,
1693 subtable->u.mark_mark1.ClassCount,
1694 &subtable->u.mark_mark1.Mark2Array);
1697 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1701 if (subtable->Format == 1)
1703 OTF_GPOS_Context1 *context1 = &subtable->u.context1;
1705 read_coverage (otf, stream, offset, &subtable->Coverage);
1706 context1->PosRuleSetCount
1707 = read_rule_set_list (otf, stream, offset, &context1->PosRuleSet);
1709 else if (subtable->Format == 2)
1711 OTF_GPOS_Context2 *context2 = &subtable->u.context2;
1713 read_coverage (otf, stream, offset, &subtable->Coverage);
1714 read_class_def (otf, stream, offset, &context2->ClassDef);
1715 context2->PosClassSetCnt
1716 = read_class_set_list (otf, stream, offset, &context2->PosClassSet);
1719 else if (subtable->Format == 3)
1721 OTF_GPOS_Context3 *context3 = &subtable->u.context3;
1723 READ_USHORT (stream, context3->GlyphCount);
1724 if (context3->GlyphCount < 0)
1725 OTF_ERROR (OTF_ERROR_TABLE, " (zero count)");
1726 READ_USHORT (stream, context3->PosCount);
1727 if (read_coverage_list (otf, stream, offset,
1728 &context3->Coverage,
1729 context3->GlyphCount) < 0)
1731 if (read_lookup_record_list (otf, stream,
1732 &context3->LookupRecord,
1733 context3->PosCount) < 0)
1737 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1741 if (subtable->Format == 1)
1743 OTF_GPOS_ChainContext1 *chain_context1 = &subtable->u.chain_context1;
1745 read_coverage (otf, stream, offset, &subtable->Coverage);
1746 chain_context1->ChainPosRuleSetCount
1747 = read_chain_rule_set_list (otf, stream, offset,
1748 &chain_context1->ChainPosRuleSet);
1750 else if (subtable->Format == 2)
1752 OTF_GPOS_ChainContext2 *chain_context2 = &subtable->u.chain_context2;
1754 read_coverage (otf, stream, offset, &subtable->Coverage);
1755 read_class_def (otf, stream, offset, &chain_context2->Backtrack);
1756 read_class_def (otf, stream, offset, &chain_context2->Input);
1757 read_class_def (otf, stream, offset, &chain_context2->LookAhead);
1758 chain_context2->ChainPosClassSetCnt
1759 = read_chain_class_set_list (otf, stream, offset,
1760 &chain_context2->ChainPosClassSet);
1762 else if (subtable->Format == 3)
1764 OTF_GPOS_ChainContext3 *chain_context3 = &subtable->u.chain_context3;
1766 count = read_coverage_list (otf, stream, offset,
1767 &chain_context3->Backtrack, -1);
1770 chain_context3->BacktrackGlyphCount = (unsigned) count;
1771 count = read_coverage_list (otf, stream, offset,
1772 &chain_context3->Input, -1);
1775 chain_context3->InputGlyphCount = (unsigned) count;
1776 subtable->Coverage = chain_context3->Input[0];
1777 count = read_coverage_list (otf, stream, offset,
1778 &chain_context3->LookAhead, -1);
1779 chain_context3->LookaheadGlyphCount = (unsigned) count;
1780 chain_context3->PosCount
1781 = read_lookup_record_list (otf, stream,
1782 &chain_context3->LookupRecord, -1);
1785 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
1789 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported)");
1793 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid LookupType)");
1800 read_gpos_table (OTF *otf, OTF_Stream *stream)
1802 char *errfmt = "GPOS%s";
1803 void *errret = NULL;
1806 OTF_CALLOC (gpos, 1, "");
1807 READ_FIXED (stream, gpos->Version);
1808 READ_OFFSET (stream, gpos->ScriptList.offset);
1809 READ_OFFSET (stream, gpos->FeatureList.offset);
1810 READ_OFFSET (stream, gpos->LookupList.offset);
1812 if (read_script_list (otf, stream, gpos->ScriptList.offset,
1813 &gpos->ScriptList) < 0
1814 || read_feature_list (otf, stream, gpos->FeatureList.offset,
1815 &gpos->FeatureList) < 0
1816 || read_lookup_list (otf, stream, gpos->LookupList.offset,
1817 &gpos->LookupList, 0) < 0)
1827 read_base_table (OTF_Stream *stream, long offset)
1831 OTF_MALLOC (base, 1);
1840 read_jstf_table (OTF_Stream *stream, long offset)
1844 OTF_MALLOC (jstf, 1);
1852 read_attach_list (OTF *otf, OTF_Stream *stream, long offset,
1853 OTF_AttachList *list)
1855 char *errfmt = "AttachList%s";
1859 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1861 READ_UINT16 (stream, list->GlyphCount);
1862 OTF_MALLOC (list->AttachPoint, list->GlyphCount, "");
1863 for (i = 0; i < list->GlyphCount; i++)
1864 READ_OFFSET (stream, list->AttachPoint[i].offset);
1865 for (i = 0; i < list->GlyphCount; i++)
1869 SEEK_STREAM (stream, offset + list->AttachPoint[i].offset);
1870 READ_UINT16 (stream, count);
1871 list->AttachPoint[i].PointCount = count;
1872 OTF_MALLOC (list->AttachPoint[i].PointIndex, count, " (PointIndex)");
1873 for (j = 0; j < count; j++)
1874 READ_UINT16 (stream, list->AttachPoint[i].PointIndex[j]);
1880 read_caret_value (OTF *otf, OTF_Stream *stream, long offset,
1881 OTF_CaretValue *caret)
1883 char *errfmt = "CaretValue%s";
1886 SEEK_STREAM (stream, offset + caret->offset);
1887 READ_UINT16 (stream, caret->CaretValueFormat);
1888 if (caret->CaretValueFormat == 1)
1889 READ_INT16 (stream, caret->f.f1.Coordinate);
1890 else if (caret->CaretValueFormat == 2)
1891 READ_UINT16 (stream, caret->f.f2.CaretValuePoint);
1892 else if (caret->CaretValueFormat == 3)
1894 READ_INT16 (stream, caret->f.f3.Coordinate);
1895 if (read_device_table (otf, stream, offset + caret->offset,
1896 &caret->f.f3.DeviceTable) < 0)
1900 OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
1905 read_lig_caret_list (OTF *otf, OTF_Stream *stream, long offset,
1906 OTF_LigCaretList *list)
1908 char *errfmt = "LigCaretList%s";
1912 if (read_coverage (otf, stream, offset, &list->Coverage) < 0)
1914 READ_UINT16 (stream, list->LigGlyphCount);
1915 OTF_MALLOC (list->LigGlyph, list->LigGlyphCount, "");
1916 for (i = 0; i < list->LigGlyphCount; i++)
1917 READ_OFFSET (stream, list->LigGlyph[i].offset);
1918 for (i = 0; i < list->LigGlyphCount; i++)
1922 SEEK_STREAM (stream, offset + list->LigGlyph[i].offset);
1923 READ_UINT16 (stream, count);
1924 list->LigGlyph[i].CaretCount = count;
1925 OTF_MALLOC (list->LigGlyph[i].CaretValue, count, " (CaretValue)");
1926 for (j = 0; j < count; j++)
1927 READ_OFFSET (stream, list->LigGlyph[i].CaretValue[j].offset);
1928 for (j = 0; j < count; j++)
1929 if (read_caret_value (otf, stream, offset + list->LigGlyph[i].offset,
1930 &list->LigGlyph[i].CaretValue[j]) < 0)
1937 read_gdef_header (OTF_Stream *stream, OTF_GDEFHeader *header)
1941 READ_FIXED (stream, header->Version);
1942 READ_OFFSET (stream, header->GlyphClassDef);
1943 READ_OFFSET (stream, header->AttachList);
1944 READ_OFFSET (stream, header->LigCaretList);
1945 READ_OFFSET (stream, header->MarkAttachClassDef);
1950 read_gdef_table (OTF *otf, OTF_Stream *stream)
1952 char *errfmt = "GDEF%s";
1953 void *errret = NULL;
1956 OTF_CALLOC (gdef, 1, "");
1957 read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
1958 if (gdef->header.GlyphClassDef)
1960 gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
1961 read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
1963 if (gdef->header.AttachList)
1964 read_attach_list (otf, stream, gdef->header.AttachList,
1965 &gdef->attach_list);
1966 if (gdef->header.LigCaretList)
1967 read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
1968 &gdef->lig_caret_list);
1969 if (gdef->header.MarkAttachClassDef)
1971 gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
1972 read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
1983 read_cmap_table (OTF *otf, OTF_Stream *stream)
1985 char *errfmt = "cmap%s";
1986 void *errret = NULL;
1990 OTF_CALLOC (cmap, 1, "");
1991 READ_USHORT (stream, cmap->version);
1992 READ_USHORT (stream, cmap->numTables);
1993 OTF_MALLOC (cmap->EncodingRecord, cmap->numTables, "");
1994 for (i = 0; i < cmap->numTables; i++)
1996 READ_USHORT (stream, cmap->EncodingRecord[i].platformID);
1997 READ_USHORT (stream, cmap->EncodingRecord[i].encodingID);
1998 READ_ULONG (stream, cmap->EncodingRecord[i].offset);
1999 if (cmap->EncodingRecord[i].platformID == 3
2000 && cmap->EncodingRecord[i].encodingID == 1)
2001 cmap->Unicode = cmap->EncodingRecord + i;
2003 for (i = 0; i < cmap->numTables; i++)
2007 SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
2008 READ_USHORT (stream, format);
2009 cmap->EncodingRecord[i].subtable.format = format;
2010 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
2011 if (format == 8 || format == 10 || format == 12)
2013 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
2014 READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
2018 READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
2024 OTF_MALLOC (cmap->EncodingRecord[i].subtable.f.f0, 1,
2025 " (EncodingRecord)");
2027 cmap->EncodingRecord[i].subtable.f.f0->glyphIdArray,
2037 OTF_EncodingSubtable4 *sub4;
2042 OTF_MALLOC (sub4, 1, " (EncodingSubtable4)");
2043 cmap->EncodingRecord[i].subtable.f.f4 = sub4;
2044 READ_USHORT (stream, sub4->segCountX2);
2045 segCount = sub4->segCountX2 / 2;
2046 READ_USHORT (stream, sub4->searchRange);
2047 READ_USHORT (stream, sub4->entrySelector);
2048 READ_USHORT (stream, sub4->rangeShift);
2049 OTF_MALLOC (sub4->segments, segCount, " (segCount)");
2050 for (j = 0; j < segCount; j++)
2051 READ_USHORT (stream, sub4->segments[j].endCount);
2052 READ_USHORT (stream, dummy);
2053 for (j = 0; j < segCount; j++)
2054 READ_USHORT (stream, sub4->segments[j].startCount);
2055 for (j = 0; j < segCount; j++)
2056 READ_SHORT (stream, sub4->segments[j].idDelta);
2057 for (j = 0; j < segCount; j++)
2060 unsigned rest = 2 * (segCount - j);
2062 READ_USHORT (stream, off);
2064 sub4->segments[j].idRangeOffset = 0xFFFF;
2066 sub4->segments[j].idRangeOffset = (off - rest) / 2;
2068 j = (cmap->EncodingRecord[i].subtable.length
2069 - (14 + 2 * (segCount * 4 + 1)));
2070 sub4->GlyphCount = j / 2;
2071 OTF_MALLOC (sub4->glyphIdArray, sub4->GlyphCount, " (GlyphCount)");
2072 for (j = 0; j < sub4->GlyphCount; j++)
2073 READ_USHORT (stream, sub4->glyphIdArray[j]);
2085 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec, int bytes)
2087 char *errfmt = "nameID (%d)";
2088 void *errret = NULL;
2089 OTF_StreamState state;
2094 SAVE_STREAM (stream, state);
2095 SEEK_STREAM (stream, stream->pos + rec->offset);
2099 OTF_MALLOC (str, rec->length + 1, (void *) rec->nameID);
2100 READ_BYTES (stream, str, rec->length);
2101 for (i = 0; i < rec->length; i++)
2105 else if (bytes == 2)
2107 OTF_MALLOC (str, rec->length / 2 + 1, (void *) rec->nameID);
2108 for (i = 0; i < rec->length / 2; i++)
2110 READ_USHORT (stream, c);
2116 else if (bytes == 4)
2118 OTF_MALLOC (str, rec->length / 4 + 1, (void *) rec->nameID);
2119 for (i = 0; i < rec->length / 4; i++)
2121 READ_ULONG (stream, c);
2128 RESTORE_STREAM (stream, state);
2133 read_name_table (OTF *otf, OTF_Stream *stream)
2135 char *errfmt = "name%s";
2136 void *errret = NULL;
2140 OTF_CALLOC (name, 1, "");
2141 READ_USHORT (stream, name->format);
2142 READ_USHORT (stream, name->count);
2143 READ_USHORT (stream, name->stringOffset);
2144 OTF_MALLOC (name->nameRecord, name->count, "");
2145 for (i = 0; i < name->count; i++)
2147 OTF_NameRecord *rec = name->nameRecord + i;
2149 READ_USHORT (stream, rec->platformID);
2150 READ_USHORT (stream, rec->encodingID);
2151 READ_USHORT (stream, rec->languageID);
2152 READ_USHORT (stream, rec->nameID);
2153 READ_USHORT (stream, rec->length);
2154 READ_USHORT (stream, rec->offset);
2156 for (i = 0; i < name->count; i++)
2158 OTF_NameRecord *rec = name->nameRecord + i;
2159 int nameID = rec->nameID;
2161 if (nameID <= OTF_max_nameID
2162 && ! name->name[nameID])
2164 if (rec->platformID == 0)
2165 name->name[nameID] = read_name (otf, stream, rec,
2166 rec->encodingID <= 3 ? 2 : 4);
2167 else if (rec->platformID == 1
2168 && rec->encodingID == 0)
2169 name->name[nameID] = read_name (otf, stream, rec, 1);
2170 else if (rec->platformID == 3
2171 && (rec->encodingID == 1 || rec->encodingID == 10))
2172 name->name[nameID] = read_name (otf, stream,
2173 rec, rec->encodingID == 1 ? 2 : 4);
2185 OTF_tag (char *name)
2187 unsigned char *p = (unsigned char *) name;
2191 return (OTF_Tag) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
2195 OTF_tag_name (OTF_Tag tag, char *name)
2197 name[0] = (char) (tag >> 24);
2198 name[1] = (char) ((tag >> 16) & 0xFF);
2199 name[2] = (char) ((tag >> 8) & 0xFF);
2200 name[3] = (char) (tag & 0xFF);
2205 /* We can't use memory allocation macros in the following functions
2206 because those macros returns from the functions before freeing
2207 memory previously allocated. */
2210 OTF_open (char *otf_name)
2213 char *errfmt = "opening otf (%s)";
2214 void *errret = NULL;
2216 OTF_InternalData *internal_data;
2218 fp = fopen (otf_name, "r");
2220 OTF_ERROR (OTF_ERROR_FILE, otf_name);
2221 otf = calloc (1, sizeof (OTF));
2223 OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
2224 otf->filename = strdup (otf_name);
2225 if (! otf->filename)
2229 OTF_ERROR (OTF_ERROR_MEMORY, "filename allocation");
2232 internal_data = calloc (1, sizeof (OTF_InternalData));
2233 if (! internal_data)
2234 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
2235 otf->internal_data = internal_data;
2236 if (! allocate_memory_record (otf))
2237 OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData)");
2239 /* Here after, all pointers to allocated memory are recorded in
2240 otf->internal_data->memory_record except for what allocated by
2241 the functions allocate_memory_record and make_stream. */
2243 if (read_header_part (otf, fp) < 0)
2256 OTF_close (OTF *otf)
2258 OTF_InternalData *internal_data = otf->internal_data;
2263 OTF_MemoryRecord *memrec = internal_data->memory_record;
2265 if (internal_data->header_stream)
2266 free_stream (internal_data->header_stream);
2268 for (i = 0; i < OTF_TABLE_TYPE_MAX; i++)
2269 if (internal_data->table_info[i].stream)
2270 free_stream (internal_data->table_info[i].stream);
2274 OTF_MemoryRecord *next = memrec->next;
2276 for (i = memrec->used - 1; i >= 0; i--)
2277 free (memrec->memory[i]);
2281 free (internal_data);
2288 OTF_get_table (OTF *otf, char *name)
2290 char *errfmt = "OTF Table Read%s";
2292 OTF_InternalData *internal_data = otf->internal_data;
2293 OTF_TableInfo *table_info;
2294 OTF_Tag tag = OTF_tag (name);
2297 OTF_ERROR (OTF_ERROR_TABLE, " (invalid table name)");
2299 if (tag == OTF_tag ("head"))
2300 table_info = internal_data->table_info + OTF_TABLE_TYPE_HEAD;
2301 else if (tag == OTF_tag ("name"))
2302 table_info = internal_data->table_info + OTF_TABLE_TYPE_NAME;
2303 else if (tag == OTF_tag ("cmap"))
2304 table_info = internal_data->table_info + OTF_TABLE_TYPE_CMAP;
2305 else if (tag == OTF_tag ("GDEF"))
2306 table_info = internal_data->table_info + OTF_TABLE_TYPE_GDEF;
2307 else if (tag == OTF_tag ("GSUB"))
2308 table_info = internal_data->table_info + OTF_TABLE_TYPE_GSUB;
2309 else if (tag == OTF_tag ("GPOS"))
2310 table_info = internal_data->table_info + OTF_TABLE_TYPE_GPOS;
2312 OTF_ERROR (OTF_ERROR_TABLE, " (not yet supported table name)");
2314 if (*table_info->address)
2317 if (! table_info->stream)
2318 OTF_ERROR (OTF_ERROR_TABLE, " (table not found)");
2319 if (! table_info->reader)
2320 OTF_ERROR (OTF_ERROR_TABLE, " (invalid contents)");
2322 *table_info->address = (*table_info->reader) (otf, table_info->stream);
2323 free_stream (table_info->stream);
2324 table_info->stream = NULL;
2325 if (! *table_info->address)
2327 table_info->reader = NULL;