5 #include FT_TRUETYPE_TABLES_H
6 #include <fontconfig/fontconfig.h>
7 #include <fontconfig/fcfreetype.h>
12 #define PROGNAME "flt-gui"
14 #elif defined (FLT_OTF)
18 #define PROGNAME "flt-otf"
20 #elif defined (FLT_HB)
24 #define PROGNAME "flt-hb"
26 #else /* (defined (FLT_PANGO)) */
29 #define PANGO_ENABLE_ENGINE
30 #define PANGO_ENABLE_BACKEND
31 #include <pango/pango.h>
32 #include <pango/pango-ot.h>
33 #include <pango/pangoft2.h>
34 #include <pango/pangofc-font.h>
37 static FT_Library ft_library;
40 parse_font_name (char *fontname, char **family, int *size, char **err)
46 pat = FcNameParse ((FcChar8 *) fontname);
49 *err = "invalid name format";
52 if (FcPatternGetString (pat, FC_FAMILY, 0, &fam) != FcResultMatch)
54 FcPatternDestroy (pat);
55 *err = "no family name";
58 if (FcPatternGetDouble (pat, FC_PIXEL_SIZE, 0, &pixelsize) != FcResultMatch)
60 *family = strdup ((char *) fam);
62 FcPatternDestroy (pat);
67 new_face (char *fontname, char **err)
77 if (parse_font_name (fontname, (char **) &fam, &size, err) < 0)
80 pat = FcPatternBuild (0,
81 FC_FAMILY, FcTypeString, fam,
82 FC_SCALABLE, FcTypeBool, FcTrue,
85 os = FcObjectSetBuild (FC_FILE, NULL);
86 fs = FcFontList (NULL, pat, os);
89 *err = "no matching font";
92 FcPatternGetString (fs->fonts[0], FC_FILE, 0, &file);
94 if (FT_New_Face (ft_library, (char *) file, 0, &face))
96 FcFontSetDestroy (fs);
97 FcObjectSetDestroy (os);
98 FcPatternDestroy (pat);
99 *err = "font open fail";
102 FcFontSetDestroy (fs);
103 FcObjectSetDestroy (os);
104 FcPatternDestroy (pat);
105 if (FT_Set_Pixel_Sizes (face, pixelsize, pixelsize))
107 *err = "set pixelsize fail";
116 typedef struct _MFLTFont MFLTFont;
121 void (*get_glyph_id) (void);
122 void (*get_metrics) (void);
123 void (*suitable_p) (void);
124 void (*drive_otf) (void);
129 void get_glyph_id (void) {}
130 void get_metrics (void) {}
131 void suitable_p (void) {}
132 void drive_otf (void) {}
137 open_font (char *fontname, char **err)
139 FT_Face ft_face = new_face (fontname, err);
149 fontset = mfontset ("generic");
150 mface_put_prop (face, Mfontset, fontset);
151 mplist_add (plist, Mface, face);
152 mplist_add (plist, Mdevice, Mnil);
153 frame = mframe (plist);
154 m17n_object_unref (plist);
155 m17n_object_unref (face);
156 m17n_object_unref (fontset);
158 font = calloc (1, sizeof (MFLTFont));
159 font->font = mfont_encapsulate (frame, Mfreetype, ft_face);
160 font->face = ft_face;
165 close_font (MFLTFont *font)
167 FT_Done_Face (font->face);
169 m17n_object_unref (frame);
172 #elif defined (FLT_OTF) || defined (FLT_HB)
180 get_glyph_id (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
182 FT_Face face = ((FontInfo *) font)->face;
184 for (; from < to; from++)
186 MFLTGlyph *g = gstring->glyphs + from;
189 && ! (g->code = FT_Get_Char_Index (face, g->code)))
197 get_metrics (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
199 FT_Face face = ((FontInfo *) font)->face;
201 for (; from < to; from++)
203 MFLTGlyph *g = gstring->glyphs + from;
207 FT_Glyph_Metrics *metrics;
209 if (FT_Load_Glyph (face, g->code, FT_LOAD_DEFAULT))
211 metrics = &face->glyph->metrics;
212 g->lbearing = metrics->horiBearingX;
213 g->rbearing = metrics->horiBearingX + metrics->width;
214 g->xadv = metrics->horiAdvance;
215 g->yadv = metrics->vertAdvance;
216 g->ascent = metrics->horiBearingY;
217 g->descent = metrics->height - metrics->horiBearingY;
232 #define DEVICE_DELTA(table, size) \
233 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
234 ? ((table).DeltaValue[(size) - (table).StartSize] << 6) \
238 adjust_anchor (OTF_Anchor *anchor, FT_Face face,
239 unsigned code, unsigned x_ppem, unsigned y_ppem, int *x, int *y)
241 if (anchor->AnchorFormat == 2)
244 int ap = anchor->f.f1.AnchorPoint;
246 FT_Load_Glyph (face, (FT_UInt) code, FT_LOAD_MONOCHROME);
247 outline = &face->glyph->outline;
248 if (ap < outline->n_points)
250 *x = outline->points[ap].x;
251 *y = outline->points[ap].y;
254 else if (anchor->AnchorFormat == 3)
256 if (anchor->f.f2.XDeviceTable.offset)
257 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
258 if (anchor->f.f2.YDeviceTable.offset)
259 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
264 tag_name (char *str, unsigned tag)
267 *str++ = (tag >> 16) & 0xFF;
268 *str++ = (tag >> 8) & 0xFF;
274 encode_features (char *str, int count, unsigned *features)
279 for (i = 0; i < count; i++)
281 unsigned tag = features[i];
291 str = tag_name (str, tag);
304 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
305 MFLTGlyphString *in, int from, int to,
306 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
310 OTF *otf = ((FontInfoOTF *) font)->otf;
311 OTF_GlyphString otf_gstring;
313 char script[4], langsys[4];
314 char *gsub_features = NULL, *gpos_features = NULL;
321 otf_gstring.glyphs = NULL;
322 if (OTF_get_table (otf, "head") < 0)
325 ((FontInfoOTF *) font)->otf = NULL;
329 tag_name (script, spec->script);
330 tag_name (langsys, spec->langsys);
332 if (spec->gsub_count > 0)
334 gsub_features = alloca (6 * spec->gsub_count);
337 if (OTF_check_table (otf, "GSUB") < 0)
338 gsub_features = NULL;
340 encode_features (gsub_features, spec->gsub_count, spec->gsub);
343 if (spec->gpos_count)
345 gpos_features = alloca (6 * spec->gpos_count);
348 if (OTF_check_table (otf, "GPOS") < 0)
349 gpos_features = NULL;
351 encode_features (gpos_features, spec->gpos_count, spec->gpos);
355 otf_gstring.size = otf_gstring.used = len;
356 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
357 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
358 for (i = 0; i < len; i++)
360 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
361 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
364 OTF_drive_gdef (otf, &otf_gstring);
369 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
372 if (out->allocated < out->used + otf_gstring.used)
374 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; i++, otfg++)
376 MFLTGlyph *g = out->glyphs + out->used;
379 *g = in->glyphs[from + otfg->f.index.from];
381 for (j = from + otfg->f.index.from; j <= from + otfg->f.index.to; j++)
382 if (in->glyphs[j].code == otfg->glyph_id)
384 g->c = in->glyphs[j].c;
387 if (g->code != otfg->glyph_id)
389 g->code = otfg->glyph_id;
397 if (out->allocated < out->used + len)
399 for (i = 0; i < len; i++)
400 out->glyphs[out->used++] = in->glyphs[from + i];
406 MFLTGlyph *base = NULL, *mark = NULL, *g;
407 int x_ppem, y_ppem, x_scale, y_scale;
409 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
413 face = ((FontInfo *) font)->face;
414 x_ppem = face->size->metrics.x_ppem;
415 y_ppem = face->size->metrics.y_ppem;
416 x_scale = face->size->metrics.x_scale;
417 y_scale = face->size->metrics.y_scale;
419 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
420 i < otf_gstring.used; i++, otfg++, g++)
424 if (! otfg->glyph_id)
426 switch (otfg->positioning_type)
432 int format = otfg->f.f1.format;
434 if (format & OTF_XPlacement)
436 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
437 if (format & OTF_XPlaDevice)
439 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
440 if (format & OTF_YPlacement)
442 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
443 if (format & OTF_YPlaDevice)
445 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
446 if (format & OTF_XAdvance)
448 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
449 if (format & OTF_XAdvDevice)
451 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
452 if (format & OTF_YAdvance)
454 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
455 if (format & OTF_YAdvDevice)
457 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
458 adjustment[i].set = 1;
462 /* Not yet supported. */
468 goto label_adjust_anchor;
469 default: /* i.e. case 6 */
476 int base_x, base_y, mark_x, mark_y;
478 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
479 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
480 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
481 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
483 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
484 adjust_anchor (otfg->f.f4.base_anchor, face, prev->code,
485 x_ppem, y_ppem, &base_x, &base_y);
486 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
487 adjust_anchor (otfg->f.f4.mark_anchor, face, g->code,
488 x_ppem, y_ppem, &mark_x, &mark_y);
489 adjustment[i].xoff = (base_x - mark_x);
490 adjustment[i].yoff = - (base_y - mark_y);
491 adjustment[i].back = (g - prev);
492 adjustment[i].set = 1;
495 if (otfg->GlyphClass == OTF_GlyphClass0)
497 else if (otfg->GlyphClass == OTF_GlyphClassMark)
503 free (otf_gstring.glyphs);
507 font->get_metrics (font, in, from, to);
508 for (i = 0; i < len; i++)
510 MFLTGlyph *g = in->glyphs + (from + i);
512 out->glyphs[out->used++] = *g;
514 if (otf_gstring.glyphs)
515 free (otf_gstring.glyphs);
520 open_font (char *fontname, char **err)
522 FT_Face face = new_face (fontname, err);
523 FontInfoOTF *font_info;
527 font_info = malloc (sizeof (FontInfoOTF));
528 font_info->face = face;
529 font_info->otf = OTF_open_ft_face (face);
530 return ((MFLTFont *) font_info);
534 close_font (MFLTFont *font)
536 FontInfoOTF *font_info = (FontInfoOTF *) font;
538 OTF_close (font_info->otf);
539 FT_Done_Face (font_info->face);
559 HB_StreamRec gdef_stream;
563 MPlist *otf_spec_cache;
567 convertStringToGlyphIndices (HB_Font font, const HB_UChar16 *string,
568 hb_uint32 length, HB_Glyph *glyphs,
569 hb_uint32 *numGlyphs, HB_Bool rightToLeft)
575 getGlyphAdvances (HB_Font font, const HB_Glyph *glyphs, hb_uint32 numGlyphs,
576 HB_Fixed *advances, int flags /*HB_ShaperFlag*/)
580 for (i = 0; i < numGlyphs; i++)
585 canRender (HB_Font font, const HB_UChar16 *string, hb_uint32 length)
591 getPointInOutline (HB_Font font, HB_Glyph glyph, int flags /*HB_ShaperFlag*/,
592 hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos,
595 FT_Face face = font->faceData;
598 if ((error = (HB_Error) FT_Load_Glyph (face, glyph, flags)))
600 if (face->glyph->format != ft_glyph_format_outline)
601 return (HB_Error) HB_Err_Invalid_GPOS_SubTable;
602 *nPoints = face->glyph->outline.n_points;
605 if (point > *nPoints)
606 return (HB_Error) HB_Err_Invalid_GPOS_SubTable;
608 *xpos = face->glyph->outline.points[point].x;
609 *ypos = face->glyph->outline.points[point].y;
615 getGlyphMetrics (HB_Font font, HB_Glyph glyph, HB_GlyphMetrics *metrics)
620 getFontMetric (HB_Font font, HB_FontMetric metric)
625 #define MAKE_TAG(name) ((((FT_ULong) (name)[0]) << 24) \
626 | (((FT_ULong) (name)[1]) << 16) \
627 | (((FT_ULong) (name)[2]) << 8) \
631 new_stream (FT_Face face, char *tagname, HB_Stream stream)
633 FT_ULong tag = MAKE_TAG (tagname);
637 if (! FT_IS_SFNT (face))
638 return HB_Err_Invalid_Argument;
640 if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &len))
641 return HB_Err_Table_Missing;
644 return HB_Err_Out_Of_Memory;
645 if (FT_Load_Sfnt_Table (face, tag, 0, buf, &len))
648 return HB_Err_Table_Missing;
650 stream->base = (HB_Byte *) buf;
653 stream->cursor = NULL;
659 load_gdef (FontInfoHB *font_info)
663 if (! font_info->gdef_stream.base)
665 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GDEF",
666 &font_info->gdef_stream);
667 if (err != HB_Err_Ok)
670 return HB_Load_GDEF_Table (&font_info->gdef_stream, &font_info->gdef);
674 load_gsub (FontInfoHB *font_info)
678 HB_LookupList *lookup_list;
681 if (! font_info->gdef)
683 if ((err = load_gdef (font_info)) != HB_Err_Ok)
686 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GSUB", &stream);
687 if (err != HB_Err_Ok)
689 err = HB_Load_GSUB_Table (&stream, &font_info->gsub,
690 font_info->gdef, &font_info->gdef_stream);
692 lookup_list = &font_info->gsub->LookupList;
693 for (i = 0; i < lookup_list->LookupCount; i++)
694 lookup_list->Properties[i] = HB_GLYPH_PROPERTIES_UNKNOWN;
699 load_gpos (FontInfoHB *font_info)
703 HB_LookupList *lookup_list;
706 if (! font_info->gdef)
708 if ((err = load_gdef (font_info)) != HB_Err_Ok)
711 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GPOS", &stream);
712 if (err != HB_Err_Ok)
714 err = HB_Load_GPOS_Table (&stream, &font_info->gpos,
715 font_info->gdef, &font_info->gdef_stream);
717 lookup_list = &font_info->gpos->LookupList;
718 for (i = 0; i < lookup_list->LookupCount; i++)
719 lookup_list->Properties[i] = HB_GLYPH_PROPERTIES_UNKNOWN;
723 const HB_FontClass hb_fontClass = {
724 convertStringToGlyphIndices, getGlyphAdvances, canRender,
725 getPointInOutline, getGlyphMetrics, getFontMetric
729 setup_features (int gsubp, FontInfoHB *font_info, MFLTOtfSpec *spec,
732 int count, preordered;
733 unsigned int *features;
734 FT_UShort script, langsys;
735 FT_UShort req_feature, index;
736 FT_UInt *feature_list;
741 if (! font_info->gsub)
743 if (load_gsub (font_info) != HB_Err_Ok)
746 if (HB_GSUB_Select_Script (font_info->gsub, spec->script, &script)
751 if (HB_GSUB_Select_Language (font_info->gsub, spec->langsys,
752 script, &langsys, &req_feature)
757 langsys = req_feature = 0xFFFF;
758 count = spec->gsub_count;
759 features = spec->gsub;
763 if (! font_info->gpos)
765 if (load_gpos (font_info) != HB_Err_Ok)
768 if (HB_GPOS_Select_Script (font_info->gpos, spec->script, &script)
773 if (HB_GPOS_Select_Language (font_info->gpos, spec->langsys,
774 script, &langsys, &req_feature)
779 langsys = req_feature = 0xFFFF;
780 count = spec->gpos_count;
781 features = spec->gpos;
784 for (preordered = 0; preordered < count; preordered++)
785 if (features[preordered] == 0)
787 if ((gsubp ? HB_GSUB_Query_Features (font_info->gsub, script, langsys,
789 : HB_GPOS_Query_Features (font_info->gpos, script, langsys,
796 for (i = 0; feature_list[i]; i++);
799 info->indices = malloc (sizeof (FT_UShort) * ((req_feature != 0xFFFF) + i));
801 if (req_feature != 0xFFFF)
802 info->indices[i++] = req_feature;
803 for (j = 0; j < preordered; j++)
804 if ((gsubp ? HB_GSUB_Select_Feature (font_info->gsub, features[j],
805 script, langsys, &index)
806 : HB_GPOS_Select_Feature (font_info->gpos, features[j],
807 script, langsys, &index))
809 info->indices[i++] = index;
811 for (j = 0; feature_list[j]; j++)
813 for (k = preordered + 1; k < count; k++)
814 if (feature_list[j] == features[k])
818 ? HB_GSUB_Select_Feature (font_info->gsub, feature_list[j],
819 script, langsys, &index)
820 : HB_GPOS_Select_Feature (font_info->gpos, feature_list[j],
821 script, langsys, &index))
823 info->indices[i++] = index;
829 setup_otf_spec (MFLTFont *font, MFLTOtfSpec *spec)
831 FontInfoHB *font_info = (FontInfoHB *) font;
834 if (spec->gsub_count + spec->gpos_count == 0)
836 ginfo = mplist_get (font_info->otf_spec_cache, spec->sym);
838 return (ginfo->gsub.count + ginfo->gpos.count > 0 ? ginfo : NULL);
839 ginfo = calloc (1, sizeof (GsubGposInfo));
840 mplist_push (font_info->otf_spec_cache, spec->sym, ginfo);
841 setup_features (1, font_info, spec, &(ginfo->gsub));
842 setup_features (0, font_info, spec, &(ginfo->gpos));
847 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
848 MFLTGlyphString *in, int from, int to,
849 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
851 FontInfoHB *font_info = (FontInfoHB *) font;
856 HB_UShort *apply_order_save;
857 HB_UShort apply_count_save;
858 int gpos_applied = 0;
865 if (hb_buffer_new (&buf) != FT_Err_Ok)
867 for (i = from; i < to; i++)
869 if (hb_buffer_add_glyph (buf, in->glyphs[i].code, 0, i) != FT_Err_Ok)
872 ginfo = setup_otf_spec (font, spec);
875 if (ginfo->gsub.count > 0)
877 if (! font_info->gdef
878 && load_gdef (font_info) != HB_Err_Ok)
880 apply_order_save = font_info->gsub->FeatureList.ApplyOrder;
881 apply_count_save = font_info->gsub->FeatureList.ApplyCount;
882 font_info->gsub->FeatureList.ApplyOrder = ginfo->gsub.indices;
883 font_info->gsub->FeatureList.ApplyCount = ginfo->gsub.count;
884 err = HB_GSUB_Apply_String (font_info->gsub, buf);
885 font_info->gsub->FeatureList.ApplyOrder = apply_order_save;
886 font_info->gsub->FeatureList.ApplyCount = apply_count_save;
887 if (err != HB_Err_Ok && err != HB_Err_Not_Covered)
889 if (out->used + buf->in_length > out->allocated)
893 if (ginfo->gpos.count > 0
894 && (font_info->gpos || load_gpos (font_info) == HB_Err_Ok))
896 apply_order_save = font_info->gpos->FeatureList.ApplyOrder;
897 apply_count_save = font_info->gpos->FeatureList.ApplyCount;
898 font_info->gpos->FeatureList.ApplyOrder = ginfo->gpos.indices;
899 font_info->gpos->FeatureList.ApplyCount = ginfo->gpos.count;
900 err = HB_GPOS_Apply_String (&font_info->hb_font, font_info->gpos,
901 FT_LOAD_DEFAULT, buf, FALSE, FALSE);
902 font_info->gpos->FeatureList.ApplyOrder = apply_order_save;
903 font_info->gpos->FeatureList.ApplyCount = apply_count_save;
904 if (err == HB_Err_Ok)
908 for (i = 0; i < buf->in_length; i++)
910 HB_GlyphItem hg = buf->in_string + i;
911 HB_Position pos = buf->positions + i;
914 out->glyphs[out->used] = in->glyphs[hg->cluster];
915 g = out->glyphs + out->used++;
916 if (g->code != hg->gindex)
919 g->code = hg->gindex;
922 adjustment[i].set = gpos_applied;
925 adjustment[i].xadv = pos->x_advance;
926 adjustment[i].yadv = pos->y_advance;
927 adjustment[i].xoff = pos->x_pos;
928 adjustment[i].yoff = - pos->y_pos;
929 adjustment[i].back = pos->back;
930 adjustment[i].advance_is_absolute = pos->new_advance;
937 hb_buffer_free (buf);
938 for (i = 0; i < len; i++)
939 out->glyphs[out->used++] = in->glyphs[from + i];
944 open_font (char *fontname, char **err)
946 FT_Face face = new_face (fontname, err);
947 FontInfoHB *font_info;
951 font_info = calloc (1, sizeof (FontInfoHB));
952 font_info->face = face;
953 font_info->hb_font.klass = &hb_fontClass;
954 font_info->hb_font.faceData = face;
955 font_info->hb_font.userData = NULL;
956 font_info->hb_font.x_ppem = face->size->metrics.x_ppem;
957 font_info->hb_font.x_scale = face->size->metrics.x_scale;
958 font_info->hb_font.y_scale = face->size->metrics.y_scale;
959 font_info->hb_font.y_ppem = face->size->metrics.y_ppem;
960 font_info->otf_spec_cache = mplist ();
961 return ((MFLTFont *) font_info);
965 close_font (MFLTFont *font)
967 FontInfoHB *font_info = (FontInfoHB *) font;
972 HB_Done_GDEF_Table (font_info->gdef);
973 free (font_info->gdef_stream.base);
976 HB_Done_GSUB_Table (font_info->gsub);
978 HB_Done_GPOS_Table (font_info->gpos);
979 FT_Done_Face ((FT_Face) (font_info->hb_font.faceData));
980 for (p = font_info->otf_spec_cache; mplist_key (p) != Mnil;
983 GsubGposInfo *ginfo = mplist_value (p);
985 if (ginfo->gsub.count > 0)
986 free (ginfo->gsub.indices);
987 if (ginfo->gpos.count > 0)
988 free (ginfo->gpos.indices);
990 m17n_object_unref (font_info->otf_spec_cache);
996 #else /* (defined (FLT_PANGO) */
1001 PangoOTRuleset *ruleset;
1011 PangoFontMap *fontmap;
1012 PangoContext *context;
1013 PangoFcFont *pango_font;
1014 MPlist *otf_spec_cache;
1018 get_glyph_id (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
1020 FontInfoPango *font_info = (FontInfoPango *) font;
1022 for (; from < to; from++)
1024 MFLTGlyph *g = gstring->glyphs + from;
1027 && ! (g->code = pango_fc_font_get_glyph (font_info->pango_font,
1035 #define PANGO_SCALE_TO_26_6 (PANGO_SCALE / (1<<6))
1038 get_metrics (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
1040 FontInfoPango *font_info = (FontInfoPango *) font;
1043 for (i = from; i < to; i++)
1045 MFLTGlyph *g = gstring->glyphs + from;
1049 PangoRectangle inc, logical;
1051 pango_font_get_glyph_extents (PANGO_FONT (font_info->pango_font),
1052 gstring->glyphs[i].code, &inc, &logical);
1053 g->lbearing = inc.x / PANGO_SCALE_TO_26_6;
1054 g->rbearing = (inc.x + inc.width) / PANGO_SCALE_TO_26_6;
1055 g->xadv = logical.width / PANGO_SCALE_TO_26_6;
1057 g->ascent = - inc.y / PANGO_SCALE_TO_26_6;
1058 g->descent = (inc.height + inc.y) / PANGO_SCALE_TO_26_6;
1066 #ifndef PANGO_OT_DEFAULT_LANGUAGE
1067 #define PANGO_OT_DEFAULT_LANGUAGE ((guint) 0xFFFF)
1069 #ifndef PANGO_OT_ALL_GLYPHS
1070 #define PANGO_OT_ALL_GLYPHS ((guint) 0xFFFF)
1074 setup_features (PangoOTTableType type, FontInfoPango *font_info,
1075 MFLTOtfSpec *spec, FeatureInfo *info)
1077 int count, preordered;
1078 unsigned int *features;
1079 guint script, langsys;
1080 guint req_feature, index;
1081 guint *feature_list;
1084 PangoOTInfo *ot_info;
1086 face = pango_fc_font_lock_face (font_info->pango_font);
1087 ot_info = pango_ot_info_get (face);
1088 pango_fc_font_unlock_face (font_info->pango_font);
1090 if (! pango_ot_info_find_script (ot_info, type, spec->script, &script))
1094 if (! pango_ot_info_find_language (ot_info, type, script,
1096 &langsys, &req_feature))
1101 langsys = PANGO_OT_DEFAULT_LANGUAGE;
1102 req_feature = 0xFFFF;
1104 if (type == PANGO_OT_TABLE_GSUB)
1106 count = spec->gsub_count;
1107 features = spec->gsub;
1111 count = spec->gpos_count;
1112 features = spec->gpos;
1114 feature_list = NULL;
1115 for (preordered = 0; preordered < count; preordered++)
1116 if (features[preordered] == 0)
1118 feature_list = pango_ot_info_list_features (ot_info, type, 0,
1125 for (i = 0; feature_list[i]; i++);
1128 info->indices = malloc (sizeof (guint) * ((req_feature != 0xFFFF) + i));
1130 if (req_feature != 0xFFFF)
1131 info->indices[i++] = req_feature;
1132 for (j = 0; j < preordered; j++)
1133 if (pango_ot_info_find_feature (ot_info, type, features[j],
1134 script, langsys, &index))
1135 info->indices[i++] = index;
1137 for (j = 0; feature_list[j]; j++)
1139 for (k = preordered + 1; k < count; k++)
1140 if (feature_list[j] == features[k])
1143 && pango_ot_info_find_feature (ot_info, type, feature_list[j],
1144 script, langsys, &index))
1145 info->indices[i++] = index;
1148 info->ruleset = pango_ot_ruleset_new (ot_info);
1150 for (i = 0; i < info->count; i++)
1151 pango_ot_ruleset_add_feature (info->ruleset, type, info->indices[i],
1152 PANGO_OT_ALL_GLYPHS);
1154 for (i = info->count - 1; i >= 0; i--)
1155 pango_ot_ruleset_add_feature (info->ruleset, type, info->indices[i],
1156 PANGO_OT_ALL_GLYPHS);
1161 setup_otf_spec (FontInfoPango *font_info, MFLTOtfSpec *spec)
1163 GsubGposInfo *ginfo;
1165 if (spec->gsub_count + spec->gpos_count == 0)
1167 ginfo = mplist_get (font_info->otf_spec_cache, spec->sym);
1169 return (ginfo->gsub.count + ginfo->gpos.count > 0 ? ginfo : NULL);
1170 ginfo = calloc (1, sizeof (GsubGposInfo));
1171 mplist_push (font_info->otf_spec_cache, spec->sym, ginfo);
1172 setup_features (PANGO_OT_TABLE_GSUB, font_info, spec, &(ginfo->gsub));
1173 setup_features (PANGO_OT_TABLE_GPOS, font_info, spec, &(ginfo->gpos));
1178 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
1179 MFLTGlyphString *in, int from, int to,
1180 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
1182 FontInfoPango *font_info = (FontInfoPango *) font;
1183 PangoOTBuffer *buffer;
1184 GsubGposInfo *ginfo;
1185 PangoGlyphString *glyphs;
1188 buffer = pango_ot_buffer_new (font_info->pango_font);
1189 for (i = from; i < to; i++)
1190 pango_ot_buffer_add_glyph (buffer, in->glyphs[i].code, 0, i);
1191 ginfo = setup_otf_spec (font_info, spec);
1194 if (ginfo->gsub.count > 0)
1195 pango_ot_ruleset_substitute (ginfo->gsub.ruleset, buffer);
1196 if (ginfo->gpos.count > 0)
1197 pango_ot_ruleset_position (ginfo->gpos.ruleset, buffer);
1198 glyphs = pango_glyph_string_new ();
1199 pango_ot_buffer_output (buffer, glyphs);
1200 for (i = 0; i < glyphs->num_glyphs; i++)
1202 PangoGlyphInfo *glyph_info = glyphs->glyphs + i;
1205 out->glyphs[out->used] = in->glyphs[glyphs->log_clusters[i]];
1206 g = out->glyphs + out->used++;
1207 if (g->code != glyph_info->glyph)
1210 g->code = glyph_info->glyph;
1213 g->xoff = glyph_info->geometry.x_offset / PANGO_SCALE_TO_26_6;
1214 g->yoff = glyph_info->geometry.y_offset / PANGO_SCALE_TO_26_6;
1215 g->xadv = glyph_info->geometry.width / PANGO_SCALE_TO_26_6;
1217 adjustment[i].set = 0;
1219 pango_ot_buffer_destroy (buffer);
1220 pango_glyph_string_free (glyphs);
1224 pango_ot_buffer_destroy (buffer);
1225 for (i = from; i < to; i++)
1226 out->glyphs[out->used++] = in->glyphs[i];
1232 open_font (char *fontname, char **err)
1234 FontInfoPango *font_info = NULL;
1235 PangoFontMap *fontmap;
1236 PangoContext *context;
1237 PangoFontDescription *desc;
1238 PangoFont *pango_font;
1243 if (parse_font_name (fontname, &family, &size, err) < 0)
1245 desc = pango_font_description_new ();
1246 pango_font_description_set_family (desc, family);
1248 pango_size = size * PANGO_SCALE;
1249 pango_font_description_set_absolute_size (desc, pango_size);
1251 fontmap = pango_ft2_font_map_new ();
1252 context = pango_context_new ();
1253 pango_context_set_font_map (context, fontmap);
1254 pango_font = pango_context_load_font (context, desc);
1255 pango_font_description_free (desc);
1258 font_info = calloc (1, sizeof (FontInfoPango));
1259 font_info->fontmap = fontmap;
1260 font_info->context = context;
1261 font_info->pango_font = PANGO_FC_FONT (pango_font);
1262 font_info->otf_spec_cache = mplist ();
1266 g_object_unref (context);
1267 g_object_unref (fontmap);
1269 return (MFLTFont *) font_info;
1273 close_font (MFLTFont *font)
1275 FontInfoPango *font_info = (FontInfoPango *) font;
1277 g_object_unref (font_info->context);
1278 g_object_unref (font_info->fontmap);
1286 run_flt (MText *mt, int from, int to, MFLTFont *font, char *flt_name)
1288 MDrawControl control;
1290 memset (&control, 0, sizeof (MDrawControl));
1291 control.two_dimensional = 1;
1292 control.enable_bidi = 1;
1293 control.anti_alias = 1;
1294 /*control.disable_caching = 1;*/
1295 mtext_put_prop (mt, from, to, Mfont, font->font);
1296 mdraw_text_extents (frame, mt, from, to, &control, NULL, NULL, NULL);
1303 run_flt (MText *mt, int from, int to, MFLTFont *font, char *flt_name)
1305 static MFLT *flt = NULL;
1306 MCharTable *coverage;
1307 static MFLTGlyphString gstring;
1310 && ! (flt = mflt_get (flt_name)))
1312 fprintf (stderr, "Invalid FLT name: %s\n", flt_name);
1315 coverage = mflt_coverage (flt);
1320 for (; from < to; from++)
1321 if (mchartable_lookup (coverage, mtext_ref_char (mt, from)))
1323 for (i = from + 1; i < to; i++)
1324 if (! mchartable_lookup (coverage, mtext_ref_char (mt, i)))
1331 if (gstring.allocated < len)
1333 gstring.allocated = len * 2;
1334 gstring.glyphs = realloc (gstring.glyphs,
1335 sizeof (MFLTGlyph) * len * 2);
1337 gstring.glyph_size = sizeof (MFLTGlyph);
1338 for (i = 0; i < len; i++)
1339 gstring.glyphs[i].c = mtext_ref_char (mt, from + i);
1341 i = mflt_run (&gstring, 0, len, font, flt);
1353 main (int argc, char **argv)
1355 char *font_name, *flt_name;
1363 fprintf (stderr, "Usage: flt FONT-NAME FLT-NAME < TEXT-FILE\n");
1367 font_name = argv[1];
1370 FT_Init_FreeType (&ft_library);
1372 font = open_font (font_name, &err);
1375 fprintf (stderr, "Font error: %s: %s\n", argv[1], err);
1378 font->get_glyph_id = get_glyph_id;
1379 font->get_metrics = get_metrics;
1380 font->drive_otf = drive_otf;
1382 mt = mconv_decode_stream (msymbol ("utf-8"), stdin);
1383 len = mtext_len (mt);
1384 for (i = 0, j = mtext_character (mt, i, len, '\n'); i < len;
1385 i = j + 1, j = mtext_character (mt, i, len, '\n'))
1391 to = run_flt (mt, i, j, font, flt_name);
1394 fprintf (stderr, "Error in FLT processing.\n");
1399 m17n_object_unref (mt);