8 #define GSTRING_DELETE(gstring, from, len) \
10 memmove (gstring->glyphs + from, gstring->glyphs + from + len, \
11 sizeof (OTF_Glyph) * (gstring->used - from - len)); \
12 gstring->used -= len; \
16 #define GSTRING_INSERT(gstring, pos, len) \
18 if (gstring->used + len > gstring->size) \
20 char *errfmt = "GSTRING%s"; \
22 gstring->size = gstring->used + len; \
23 gstring->glyphs = (OTF_Glyph *) realloc (gstring->glyphs, \
25 if (! gstring->glyphs) \
26 OTF_ERROR (OTF_ERROR_MEMORY, ""); \
28 memmove (gstring->glyphs + pos + len, gstring->glyphs + pos, \
29 sizeof (OTF_Glyph) * (gstring->used - pos)); \
30 gstring->used += len; \
35 gstring_subst (OTF_GlyphString *gstring, int from, int to,
36 OTF_GlyphID *ids, int num)
43 GSTRING_INSERT (gstring, from, (num - len));
45 GSTRING_DELETE (gstring, from, (len - num));
46 for (i = 0; i < num; i++)
47 gstring->glyphs[from + i].glyph_id = ids[i];
53 get_coverage_index (OTF_Coverage *coverage, OTF_GlyphID id)
57 if (coverage->CoverageFormat == 1)
59 for (i = 0; i < coverage->Count; i++)
60 if (coverage->table.GlyphArray[i] == id)
65 for (i = 0; i < coverage->Count; i++)
66 if (coverage->table.RangeRecord[i].Start <= id
67 && coverage->table.RangeRecord[i].End >= id)
68 return (coverage->table.RangeRecord[i].StartCoverageIndex
69 + (id - coverage->table.RangeRecord[i].Start));
75 get_class_def (OTF_ClassDef *class_def, OTF_GlyphID glyph_id)
77 if (class_def->ClassFormat == 1)
79 int idx = (int) glyph_id - (int) class_def->f.f1.StartGlyph;
81 if (idx >= 0 && idx < class_def->f.f1.GlyphCount)
82 return class_def->f.f1.ClassValueArray[idx];
88 for (i = 0; i < class_def->f.f2.ClassRangeCount; i++)
89 if (glyph_id >= class_def->f.f2.ClassRangeRecord[i].Start
90 && glyph_id <= class_def->f.f2.ClassRangeRecord[i].End)
91 return class_def->f.f2.ClassRangeRecord[i].Class;
97 get_langsys (OTF_ScriptList *script_list, char *script, char *language)
100 OTF_Tag script_tag = OTF_tag (script);
101 OTF_Tag langsys_tag = OTF_tag (language);
104 for (i = 0; i < script_list->ScriptCount; i++)
105 if (script_list->Script[i].ScriptTag == script_tag)
107 OTF_Script *script = script_list->Script + i;
110 return &script->DefaultLangSys;
111 for (j = 0; j < script->LangSysCount; j++)
112 if (script->LangSysRecord[j].LangSysTag == langsys_tag)
113 return script->LangSys + j;
114 return &script->DefaultLangSys;
121 get_feature_index (OTF_LangSys *LangSys, OTF_FeatureList *FeatureList,
122 char *features, int *feature_index)
129 int len = strlen (features) + 1;
132 for (p1 = p0; *p1; p1++)
138 int this_len = strlen (p0) + 1;
139 OTF_Tag tag = OTF_tag (p0);
145 for (i = 0; i < FeatureList->FeatureCount; i++)
146 if (tag == FeatureList->Feature[i].FeatureTag)
148 feature_index[nfeatures++] = i;
149 if (nfeatures == FeatureList->FeatureCount)
159 for (; nfeatures < LangSys->FeatureCount; nfeatures++)
160 feature_index[nfeatures] = LangSys->FeatureIndex[nfeatures];
167 lookup_gsub (OTF_LookupList *lookup_list, unsigned lookup_list_index,
168 OTF_GlyphString *gstring, int gidx)
170 char *errfmt = "GSUB Looking up%s";
172 OTF_Lookup *lookup = lookup_list->Lookup + lookup_list_index;
173 unsigned int flag = lookup->LookupFlag;
174 int orig_gidx = gidx;
175 OTF_Glyph *g = gstring->glyphs + gidx;
180 && (flag & (1 << g->GlyphClass))))
183 /* Try all subtables until one of them handles the current glyph. */
184 for (i = 0; i < lookup->SubTableCount && gidx == orig_gidx; i++)
186 OTF_LookupSubTableGSUB *subtable = lookup->SubTable.gsub + i;
189 if (subtable->Coverage.offset)
191 coverage_idx = get_coverage_index (&subtable->Coverage,
193 if (coverage_idx < 0)
197 switch (lookup->LookupType)
200 if (subtable->Format == 1)
201 g->glyph_id += subtable->u.single1.DeltaGlyphID;
203 g->glyph_id = subtable->u.single2.Substitute[coverage_idx];
209 OTF_GSUB_Multiple1 *multiple1 = &subtable->u.multiple1;
210 OTF_Sequence *seq = multiple1->Sequence + coverage_idx;
212 gstring_subst (gstring, gidx, gidx + 1,
213 seq->Substitute, seq->GlyphCount);
214 gidx += seq->GlyphCount;
219 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (LookupType not yet supported)");
222 if (subtable->Format == 1)
224 OTF_GSUB_Ligature1 *lig1 = &subtable->u.ligature1;
225 OTF_LigatureSet *ligset = lig1->LigatureSet + coverage_idx;
228 for (j = 0; j < ligset->LigatureCount; j++)
230 OTF_Ligature *lig = ligset->Ligature + j;
233 if (gstring->used - gidx < lig->CompCount)
235 for (k = 1; k < lig->CompCount; k++)
236 if (gstring->glyphs[gidx + k].glyph_id
237 != lig->Component[k - 1])
239 if (k < lig->CompCount)
241 gstring_subst (gstring, gidx, gidx + lig->CompCount,
248 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (invalid SubFormat)");
252 if (subtable->Format == 1)
253 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (not yet supported)");
254 else if (subtable->Format == 2)
255 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " (not yet supported)");
258 OTF_GSUB_ChainContext3 *context3
259 = &subtable->u.chain_context3;
260 int back_gidx = gidx - context3->BacktrackGlyphCount;
261 int fore_gidx = gidx + context3->InputGlyphCount;
266 || fore_gidx + context3->LookaheadGlyphCount > gstring->used)
269 for (j = 0; j < context3->BacktrackGlyphCount; j++)
270 if (get_coverage_index (context3->Backtrack + j,
271 gstring->glyphs[back_gidx + j].glyph_id)
274 if (j < context3->BacktrackGlyphCount)
277 /* Start from the secoding coverage_idx because the
278 first one is the same as subtable->Coverage and thus
280 for (j = 1; j < context3->InputGlyphCount; j++)
281 if (get_coverage_index (context3->Input + j - 1,
282 gstring->glyphs[gidx + j].glyph_id)
285 if (j < context3->InputGlyphCount)
288 for (j = 0; j < context3->LookaheadGlyphCount; j++)
289 if (get_coverage_index (context3->LookAhead + j,
290 gstring->glyphs[fore_gidx + j].glyph_id)
293 if (j < context3->LookaheadGlyphCount)
296 orig_used = gstring->used;
297 for (j = 0; j < context3->SubstCount; j++)
298 lookup_gsub (lookup_list,
299 context3->SubstLookupRecord[j].LookupListIndex,
301 gidx + context3->SubstLookupRecord[j].SequenceIndex);
302 gidx += context3->InputGlyphCount + (gstring->used - orig_used);
310 if (gidx == orig_gidx)
312 //printf ("not applied\n");
317 // printf ("done\n");
326 get_anchor (OTF_Anchor *anchor, OTF_ValueRecord *rec)
328 unsigned value_format = OTF_XPlacement | OTF_YPlacement;
330 rec->XPlacement = anchor->XCoordinate;
331 rec->YPlacement = anchor->YCoordinate;
332 if (anchor->AnchorFormat == 1)
335 else if (anchor->AnchorFormat == 2)
336 /* Not yet implemented */
338 else if (anchor->AnchorFormat == 3)
339 /* Not yet implemented */
346 lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
347 OTF_GlyphString *gstring, int gidx)
349 char *errfmt = "GPOS Looking up%s";
351 OTF_Lookup *lookup = lookup_list->Lookup + lookup_list_index;
352 unsigned int flag = lookup->LookupFlag;
353 int orig_gidx = gidx;
354 OTF_Glyph *g = gstring->glyphs + gidx;
359 && (flag & (1 << g->GlyphClass))))
362 /* Try all subtables until one of them handles the current glyph. */
363 for (i = 0; i < lookup->SubTableCount && gidx == orig_gidx; i++)
365 OTF_LookupSubTableGPOS *subtable = lookup->SubTable.gpos + i;
368 // printf ("subtype:%d ", subtable->Format);
369 if (subtable->Coverage.offset)
371 coverage_idx = get_coverage_index (&subtable->Coverage,
373 if (coverage_idx < 0)
375 // printf ("not covererd ");
380 switch (lookup->LookupType)
383 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
386 if (gidx + 1 >= gstring->used)
388 if (subtable->Format == 1)
389 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
390 else if (subtable->Format == 2)
392 OTF_GPOS_Pair2 *pair2 = &subtable->u.pair2;
393 unsigned class1, class2;
395 printf ("GPOS 2-2: c:0x%x g:0x%x\n", g->c, g->glyph_id);
397 class1 = get_class_def (&pair2->ClassDef1, g->glyph_id);
398 class2 = get_class_def (&pair2->ClassDef2, g[1].glyph_id);
399 g->positioning_type = lookup->LookupType;
400 g->f.f2.format = pair2->ValueFormat1;
402 = &pair2->Class1Record[class1].Class2Record[class2].Value1;
403 if (pair2->ValueFormat2)
406 g->positioning_type = lookup->LookupType;
407 g->f.f2.format = pair2->ValueFormat2;
409 = &pair2->Class1Record[class1].Class2Record[class2].Value2;
415 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
420 if (subtable->Format == 1)
422 OTF_GPOS_MarkBase1 *mark_base1 = &subtable->u.mark_base1;
423 OTF_MarkRecord *mark_record;
424 OTF_BaseRecord *base_record;
425 int coverage_idx_base
426 = get_coverage_index (&mark_base1->BaseCoverage,
429 if (coverage_idx_base < 0)
431 printf ("GPOS 4-1: c:0x%x g:0x%x\n", g->c, g->glyph_id);
432 mark_record = mark_base1->MarkArray.MarkRecord + coverage_idx;
434 = mark_base1->BaseArray.BaseRecord + coverage_idx_base;
435 g->f.f4.mark_anchor = &mark_record->MarkAnchor;
437 = &base_record->BaseAnchor[mark_record->Class];
438 g->positioning_type = lookup->LookupType;
442 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
446 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
453 if (gidx == orig_gidx)
455 // printf ("not applied\n");
460 // printf ("done\n");
466 lookup_cmap (OTF_cmap *cmap, int c)
470 if (! cmap || ! cmap->Unicode)
473 switch (cmap->Unicode->subtable.format)
480 OTF_EncodingSubtable4 *sub4 = cmap->Unicode->subtable.f.f4;
481 int segCount = sub4->segCountX2 / 2;
483 for (i = 0; i < segCount; i++)
484 if (c <= sub4->segments[i].endCount)
486 if (i == segCount || c < sub4->segments[i].startCount)
488 if (sub4->segments[i].idRangeOffset == 0xFFFF)
489 return c + sub4->segments[i].idDelta;
490 return sub4->glyphIdArray[sub4->segments[i].idRangeOffset
491 + (c - sub4->segments[i].startCount)];
503 OTF_drive_cmap (OTF *otf, OTF_GlyphString *gstring)
509 && OTF_get_table (otf, "cmap") < 0)
513 for (i = 0; i < gstring->used; i++)
514 gstring->glyphs[i].glyph_id = lookup_cmap (cmap, gstring->glyphs[i].c);
521 OTF_drive_gdef (OTF *otf, OTF_GlyphString *gstring)
527 && OTF_get_table (otf, "GDEF") < 0)
531 if (gdef->glyph_class_def.offset)
532 for (i = 0; i < gstring->used; i++)
533 gstring->glyphs[i].GlyphClass
534 = get_class_def (&gdef->glyph_class_def,
535 gstring->glyphs[i].glyph_id);
537 if (gdef->mark_attach_class_def.offset)
538 for (i = 0; i < gstring->used; i++)
539 gstring->glyphs[i].MarkAttachClass
540 = get_class_def (&gdef->mark_attach_class_def,
541 gstring->glyphs[i].glyph_id);
548 OTF_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
549 char *script, char *language, char *features)
551 char *errfmt = "GSUB driving%s";
554 OTF_LangSys *LangSys;
560 && OTF_get_table (otf, "GSUB") < 0)
564 LangSys = get_langsys (&gsub->ScriptList, script, language);
568 feature_index = alloca (sizeof (int) * gsub->FeatureList.FeatureCount);
570 OTF_ERROR (OTF_ERROR_MEMORY, " feature list");
572 nfeatures = get_feature_index (LangSys, &gsub->FeatureList,
573 features, feature_index);
575 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " no feature");
577 for (i = 0; i < nfeatures; i++)
579 OTF_Feature *feature = gsub->FeatureList.Feature + feature_index[i];
581 for (j = 0; j < feature->LookupCount; j++)
585 while (gidx < gstring->used)
586 gidx = lookup_gsub (&gsub->LookupList, feature->LookupListIndex[j],
595 OTF_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
596 char *script, char *language, char *features)
598 char *errfmt = "GPOS driving%s";
601 OTF_LangSys *LangSys;
607 && OTF_get_table (otf, "GPOS") < 0)
611 LangSys = get_langsys (&gpos->ScriptList, script, language);
615 feature_index = alloca (sizeof (int) * gpos->FeatureList.FeatureCount);
617 OTF_ERROR (OTF_ERROR_MEMORY, " feature list");
619 nfeatures = get_feature_index (LangSys, &gpos->FeatureList,
620 features, feature_index);
622 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " no feature");
624 for (i = 0; i < nfeatures; i++)
626 OTF_Feature *feature = gpos->FeatureList.Feature + feature_index[i];
628 for (j = 0; j < feature->LookupCount; j++)
632 while (gidx < gstring->used)
633 gidx = lookup_gpos (&gpos->LookupList, feature->LookupListIndex[j],
642 OTF_drive_tables (OTF *otf, OTF_GlyphString *gstring,
643 char *script, char *language,
644 char *gsub_features, char *gpos_features)
646 if (OTF_drive_cmap (otf, gstring) < 0)
648 if (OTF_drive_gdef (otf, gstring) < 0)
650 if ((! gsub_features || gsub_features[0])
651 && OTF_drive_gsub (otf, gstring, script, language, gsub_features) < 0)
653 if ((! gpos_features || gpos_features[0])
654 && OTF_drive_gpos (otf, gstring, script, language, gpos_features) < 0)