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 /* Start from the secoding coverage_idx because the
275 first one is the same as subtable->Coverage and thus
277 for (j = 1; j < context3->InputGlyphCount; j++)
278 if (get_coverage_index (context3->Input + j - 1,
279 gstring->glyphs[gidx + j].glyph_id)
282 for (j = 0; j < context3->LookaheadGlyphCount; j++)
283 if (get_coverage_index (context3->LookAhead + j,
284 gstring->glyphs[fore_gidx + j].glyph_id)
288 orig_used = gstring->used;
289 for (j = 0; j < context3->SubstCount; j++)
290 lookup_gsub (lookup_list,
291 context3->SubstLookupRecord[j].LookupListIndex,
293 gidx + context3->SubstLookupRecord[j].SequenceIndex);
294 gidx += context3->InputGlyphCount + (gstring->used - orig_used);
302 if (gidx == orig_gidx)
304 //printf ("not applied\n");
309 // printf ("done\n");
318 get_anchor (OTF_Anchor *anchor, OTF_ValueRecord *rec)
320 unsigned value_format = OTF_XPlacement | OTF_YPlacement;
322 rec->XPlacement = anchor->XCoordinate;
323 rec->YPlacement = anchor->YCoordinate;
324 if (anchor->AnchorFormat == 1)
327 else if (anchor->AnchorFormat == 2)
328 /* Not yet implemented */
330 else if (anchor->AnchorFormat == 3)
331 /* Not yet implemented */
338 lookup_gpos (OTF_LookupList *lookup_list, unsigned lookup_list_index,
339 OTF_GlyphString *gstring, int gidx)
341 char *errfmt = "GPOS Looking up%s";
343 OTF_Lookup *lookup = lookup_list->Lookup + lookup_list_index;
344 unsigned int flag = lookup->LookupFlag;
345 int orig_gidx = gidx;
346 OTF_Glyph *g = gstring->glyphs + gidx;
351 && (flag & (1 << g->GlyphClass))))
354 /* Try all subtables until one of them handles the current glyph. */
355 for (i = 0; i < lookup->SubTableCount && gidx == orig_gidx; i++)
357 OTF_LookupSubTableGPOS *subtable = lookup->SubTable.gpos + i;
360 // printf ("subtype:%d ", subtable->Format);
361 if (subtable->Coverage.offset)
363 coverage_idx = get_coverage_index (&subtable->Coverage,
365 if (coverage_idx < 0)
367 // printf ("not covererd ");
372 switch (lookup->LookupType)
375 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
378 if (gidx + 1 >= gstring->used)
380 if (subtable->Format == 1)
381 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
382 else if (subtable->Format == 2)
384 OTF_GPOS_Pair2 *pair2 = &subtable->u.pair2;
385 unsigned class1, class2;
387 printf ("GPOS 2-2: c:0x%x g:0x%x\n", g->c, g->glyph_id);
389 class1 = get_class_def (&pair2->ClassDef1, g->glyph_id);
390 class2 = get_class_def (&pair2->ClassDef2, g[1].glyph_id);
391 g->positioning_type = lookup->LookupType;
392 g->f.f2.format = pair2->ValueFormat1;
394 = &pair2->Class1Record[class1].Class2Record[class2].Value1;
395 if (pair2->ValueFormat2)
398 g->positioning_type = lookup->LookupType;
399 g->f.f2.format = pair2->ValueFormat2;
401 = &pair2->Class1Record[class1].Class2Record[class2].Value2;
407 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
412 if (subtable->Format == 1)
414 OTF_GPOS_MarkBase1 *mark_base1 = &subtable->u.mark_base1;
415 OTF_MarkRecord *mark_record;
416 OTF_BaseRecord *base_record;
417 OTF_Anchor *anchor1, *anchor2;
418 int coverage_idx_base
419 = get_coverage_index (&mark_base1->BaseCoverage,
422 if (coverage_idx_base < 0)
424 printf ("GPOS 4-1: c:0x%x g:0x%x\n", g->c, g->glyph_id);
425 mark_record = mark_base1->MarkArray.MarkRecord + coverage_idx;
427 = mark_base1->BaseArray.BaseRecord + coverage_idx_base;
428 anchor1 = &mark_record->MarkAnchor;
429 anchor2 = &base_record->BaseAnchor[mark_record->Class];
430 g->positioning_type = lookup->LookupType;
431 g->f.f4.mark_anchor = anchor1;
432 g->f.f4.base_anchor = anchor2;
436 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
440 OTF_ERROR (OTF_ERROR_GPOS_DRIVE, " (not yet supported)");
447 if (gidx == orig_gidx)
449 // printf ("not applied\n");
454 // printf ("done\n");
460 lookup_cmap (OTF_cmap *cmap, int c)
464 if (! cmap || ! cmap->Unicode)
467 switch (cmap->Unicode->subtable.format)
474 OTF_EncodingSubtable4 *sub4 = cmap->Unicode->subtable.f.f4;
475 int segCount = sub4->segCountX2 / 2;
477 for (i = 0; i < segCount; i++)
478 if (c <= sub4->segments[i].endCount)
480 if (i == segCount || c < sub4->segments[i].startCount)
482 if (sub4->segments[i].idRangeOffset == 0xFFFF)
483 return c + sub4->segments[i].idDelta;
484 return sub4->glyphIdArray[sub4->segments[i].idRangeOffset
485 + (c - sub4->segments[i].startCount)];
497 otf_drive_cmap (OTF *otf, OTF_GlyphString *gstring)
503 && otf_get_table (otf, "cmap") < 0)
507 for (i = 0; i < gstring->used; i++)
508 gstring->glyphs[i].glyph_id = lookup_cmap (cmap, gstring->glyphs[i].c);
515 otf_drive_gdef (OTF *otf, OTF_GlyphString *gstring)
521 && otf_get_table (otf, "GDEF") < 0)
525 if (gdef->glyph_class_def.offset)
526 for (i = 0; i < gstring->used; i++)
527 gstring->glyphs[i].GlyphClass
528 = get_class_def (&gdef->glyph_class_def,
529 gstring->glyphs[i].glyph_id);
531 if (gdef->mark_attach_class_def.offset)
532 for (i = 0; i < gstring->used; i++)
533 gstring->glyphs[i].MarkAttachClass
534 = get_class_def (&gdef->mark_attach_class_def,
535 gstring->glyphs[i].glyph_id);
542 otf_drive_gsub (OTF *otf, OTF_GlyphString *gstring,
543 char *script, char *language, char *features)
545 char *errfmt = "GSUB driving%s";
548 OTF_LangSys *LangSys;
554 && otf_get_table (otf, "GSUB") < 0)
558 LangSys = get_langsys (&gsub->ScriptList, script, language);
562 feature_index = alloca (sizeof (int) * gsub->FeatureList.FeatureCount);
564 OTF_ERROR (OTF_ERROR_MEMORY, " feature list");
566 nfeatures = get_feature_index (LangSys, &gsub->FeatureList,
567 features, feature_index);
569 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " no feature");
571 for (i = 0; i < nfeatures; i++)
573 OTF_Feature *feature = gsub->FeatureList.Feature + feature_index[i];
575 for (j = 0; j < feature->LookupCount; j++)
579 while (gidx < gstring->used)
580 gidx = lookup_gsub (&gsub->LookupList, feature->LookupListIndex[j],
589 otf_drive_gpos (OTF *otf, OTF_GlyphString *gstring,
590 char *script, char *language, char *features)
592 char *errfmt = "GPOS driving%s";
595 OTF_LangSys *LangSys;
601 && otf_get_table (otf, "GPOS") < 0)
605 LangSys = get_langsys (&gpos->ScriptList, script, language);
609 feature_index = alloca (sizeof (int) * gpos->FeatureList.FeatureCount);
611 OTF_ERROR (OTF_ERROR_MEMORY, " feature list");
613 nfeatures = get_feature_index (LangSys, &gpos->FeatureList,
614 features, feature_index);
616 OTF_ERROR (OTF_ERROR_GSUB_DRIVE, " no feature");
618 for (i = 0; i < nfeatures; i++)
620 OTF_Feature *feature = gpos->FeatureList.Feature + feature_index[i];
622 for (j = 0; j < feature->LookupCount; j++)
626 while (gidx < gstring->used)
627 gidx = lookup_gpos (&gpos->LookupList, feature->LookupListIndex[j],
636 otf_drive_tables (OTF *otf, OTF_GlyphString *gstring,
637 char *script, char *language)
639 if (otf_drive_cmap (otf, gstring) < 0)
641 if (otf_drive_gdef (otf, gstring) < 0)
643 if (otf_drive_gsub (otf, gstring, script, language, NULL) < 0)
645 if (otf_drive_gpos (otf, gstring, script, language, NULL) < 0)