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_metric) (void);
123 void (*suitable_p) (void);
124 void (*drive_otf) (void);
129 void get_glyph_id (void) {}
130 void get_metric (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, MFLTGlyph *g)
182 FT_Face face = ((FontInfo *) font)->face;
184 g->code = FT_Get_Char_Index (face, g->code);
186 return (g->code ? 0 : -1);
190 get_metric (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
192 FT_Face face = ((FontInfo *) font)->face;
194 for (; from < to; from++)
196 MFLTGlyph *g = gstring->glyphs + from;
197 FT_Glyph_Metrics *metrics;
199 if (FT_Load_Glyph (face, g->code, FT_LOAD_DEFAULT))
201 metrics = &face->glyph->metrics;
202 g->lbearing = metrics->horiBearingX;
203 g->rbearing = metrics->horiBearingX + metrics->width;
204 g->xadv = metrics->horiAdvance;
205 g->yadv = metrics->vertAdvance;
206 g->ascent = metrics->horiBearingY;
207 g->descent = metrics->height - metrics->horiBearingY;
220 #define DEVICE_DELTA(table, size) \
221 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
222 ? ((table).DeltaValue[(size) - (table).StartSize] << 6) \
226 adjust_anchor (OTF_Anchor *anchor, FT_Face face,
227 unsigned code, unsigned x_ppem, unsigned y_ppem, int *x, int *y)
229 if (anchor->AnchorFormat == 2)
232 int ap = anchor->f.f1.AnchorPoint;
234 FT_Load_Glyph (face, (FT_UInt) code, FT_LOAD_MONOCHROME);
235 outline = &face->glyph->outline;
236 if (ap < outline->n_points)
238 *x = outline->points[ap].x;
239 *y = outline->points[ap].y;
242 else if (anchor->AnchorFormat == 3)
244 if (anchor->f.f2.XDeviceTable.offset)
245 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
246 if (anchor->f.f2.YDeviceTable.offset)
247 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
252 tag_name (char *str, unsigned tag)
255 *str++ = (tag >> 16) & 0xFF;
256 *str++ = (tag >> 8) & 0xFF;
262 encode_features (char *str, int count, unsigned *features)
267 for (i = 0; i < count; i++)
269 unsigned tag = features[i];
279 str = tag_name (str, tag);
292 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
293 MFLTGlyphString *in, int from, int to,
294 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
298 OTF *otf = ((FontInfoOTF *) font)->otf;
299 OTF_GlyphString otf_gstring;
301 char script[4], langsys[4];
302 char *gsub_features = NULL, *gpos_features = NULL;
309 otf_gstring.glyphs = NULL;
310 if (OTF_get_table (otf, "head") < 0)
313 ((FontInfoOTF *) font)->otf = NULL;
317 tag_name (script, spec->script);
318 tag_name (langsys, spec->langsys);
320 if (spec->gsub_count > 0)
322 gsub_features = alloca (6 * spec->gsub_count);
325 if (OTF_check_table (otf, "GSUB") < 0)
326 gsub_features = NULL;
328 encode_features (gsub_features, spec->gsub_count, spec->gsub);
331 if (spec->gpos_count)
333 gpos_features = alloca (6 * spec->gpos_count);
336 if (OTF_check_table (otf, "GPOS") < 0)
337 gpos_features = NULL;
339 encode_features (gpos_features, spec->gpos_count, spec->gpos);
343 otf_gstring.size = otf_gstring.used = len;
344 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
345 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
346 for (i = 0; i < len; i++)
348 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
349 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
352 OTF_drive_gdef (otf, &otf_gstring);
357 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
360 if (out->allocated < out->used + otf_gstring.used)
362 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; i++, otfg++)
364 MFLTGlyph *g = out->glyphs + out->used;
367 *g = in->glyphs[from + otfg->f.index.from];
369 for (j = from + otfg->f.index.from; j <= from + otfg->f.index.to; j++)
370 if (in->glyphs[j].code == otfg->glyph_id)
372 g->c = in->glyphs[j].c;
375 g->code = otfg->glyph_id;
381 if (out->allocated < out->used + len)
383 for (i = 0; i < len; i++)
384 out->glyphs[out->used++] = in->glyphs[from + i];
387 font->get_metric (font, out, gidx, out->used);
392 MFLTGlyph *base = NULL, *mark = NULL, *g;
393 int x_ppem, y_ppem, x_scale, y_scale;
395 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
399 face = ((FontInfo *) font)->face;
400 x_ppem = face->size->metrics.x_ppem;
401 y_ppem = face->size->metrics.y_ppem;
402 x_scale = face->size->metrics.x_scale;
403 y_scale = face->size->metrics.y_scale;
405 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
406 i < otf_gstring.used; i++, otfg++, g++)
410 if (! otfg->glyph_id)
412 switch (otfg->positioning_type)
418 int format = otfg->f.f1.format;
420 if (format & OTF_XPlacement)
422 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
423 if (format & OTF_XPlaDevice)
425 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
426 if (format & OTF_YPlacement)
428 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
429 if (format & OTF_YPlaDevice)
431 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
432 if (format & OTF_XAdvance)
434 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
435 if (format & OTF_XAdvDevice)
437 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
438 if (format & OTF_YAdvance)
440 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
441 if (format & OTF_YAdvDevice)
443 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
444 adjustment[i].set = 1;
448 /* Not yet supported. */
454 goto label_adjust_anchor;
455 default: /* i.e. case 6 */
462 int base_x, base_y, mark_x, mark_y;
464 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
465 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
466 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
467 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
469 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
470 adjust_anchor (otfg->f.f4.base_anchor, face, prev->code,
471 x_ppem, y_ppem, &base_x, &base_y);
472 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
473 adjust_anchor (otfg->f.f4.mark_anchor, face, g->code,
474 x_ppem, y_ppem, &mark_x, &mark_y);
475 adjustment[i].xoff = (base_x - mark_x);
476 adjustment[i].yoff = - (base_y - mark_y);
477 adjustment[i].back = (g - prev);
478 adjustment[i].set = 1;
481 if (otfg->GlyphClass == OTF_GlyphClass0)
483 else if (otfg->GlyphClass == OTF_GlyphClassMark)
489 free (otf_gstring.glyphs);
493 font->get_metric (font, in, from, to);
494 for (i = 0; i < len; i++)
496 MFLTGlyph *g = in->glyphs + (from + i);
498 out->glyphs[out->used++] = *g;
500 if (otf_gstring.glyphs)
501 free (otf_gstring.glyphs);
506 open_font (char *fontname, char **err)
508 FT_Face face = new_face (fontname, err);
509 FontInfoOTF *font_info;
513 font_info = malloc (sizeof (FontInfoOTF));
514 font_info->face = face;
515 font_info->otf = OTF_open_ft_face (face);
516 return ((MFLTFont *) font_info);
520 close_font (MFLTFont *font)
522 FontInfoOTF *font_info = (FontInfoOTF *) font;
524 OTF_close (font_info->otf);
525 FT_Done_Face (font_info->face);
545 HB_StreamRec gdef_stream;
549 MPlist *otf_spec_cache;
553 convertStringToGlyphIndices (HB_Font font, const HB_UChar16 *string,
554 hb_uint32 length, HB_Glyph *glyphs,
555 hb_uint32 *numGlyphs, HB_Bool rightToLeft)
561 getGlyphAdvances (HB_Font font, const HB_Glyph *glyphs, hb_uint32 numGlyphs,
562 HB_Fixed *advances, int flags /*HB_ShaperFlag*/)
566 for (i = 0; i < numGlyphs; i++)
571 canRender (HB_Font font, const HB_UChar16 *string, hb_uint32 length)
577 getPointInOutline (HB_Font font, HB_Glyph glyph, int flags /*HB_ShaperFlag*/,
578 hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos,
581 FT_Face face = font->faceData;
584 if ((error = (HB_Error) FT_Load_Glyph (face, glyph, flags)))
586 if (face->glyph->format != ft_glyph_format_outline)
587 return (HB_Error) HB_Err_Invalid_GPOS_SubTable;
588 *nPoints = face->glyph->outline.n_points;
591 if (point > *nPoints)
592 return (HB_Error) HB_Err_Invalid_GPOS_SubTable;
594 *xpos = face->glyph->outline.points[point].x;
595 *ypos = face->glyph->outline.points[point].y;
601 getGlyphMetrics (HB_Font font, HB_Glyph glyph, HB_GlyphMetrics *metrics)
606 getFontMetric (HB_Font font, HB_FontMetric metric)
611 #define MAKE_TAG(name) ((((FT_ULong) (name)[0]) << 24) \
612 | (((FT_ULong) (name)[1]) << 16) \
613 | (((FT_ULong) (name)[2]) << 8) \
617 new_stream (FT_Face face, char *tagname, HB_Stream stream)
619 FT_ULong tag = MAKE_TAG (tagname);
623 if (! FT_IS_SFNT (face))
624 return HB_Err_Invalid_Argument;
626 if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &len))
627 return HB_Err_Table_Missing;
630 return HB_Err_Out_Of_Memory;
631 if (FT_Load_Sfnt_Table (face, tag, 0, buf, &len))
634 return HB_Err_Table_Missing;
636 stream->base = (HB_Byte *) buf;
639 stream->cursor = NULL;
645 load_gdef (FontInfoHB *font_info)
649 if (! font_info->gdef_stream.base)
651 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GDEF",
652 &font_info->gdef_stream);
653 if (err != HB_Err_Ok)
656 return HB_Load_GDEF_Table (&font_info->gdef_stream, &font_info->gdef);
660 load_gsub (FontInfoHB *font_info)
664 HB_LookupList *lookup_list;
667 if (! font_info->gdef)
669 if ((err = load_gdef (font_info)) != HB_Err_Ok)
672 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GSUB", &stream);
673 if (err != HB_Err_Ok)
675 err = HB_Load_GSUB_Table (&stream, &font_info->gsub,
676 font_info->gdef, &font_info->gdef_stream);
678 lookup_list = &font_info->gsub->LookupList;
679 for (i = 0; i < lookup_list->LookupCount; i++)
680 lookup_list->Properties[i] = HB_GLYPH_PROPERTIES_UNKNOWN;
685 load_gpos (FontInfoHB *font_info)
689 HB_LookupList *lookup_list;
692 if (! font_info->gdef)
694 if ((err = load_gdef (font_info)) != HB_Err_Ok)
697 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GPOS", &stream);
698 if (err != HB_Err_Ok)
700 err = HB_Load_GPOS_Table (&stream, &font_info->gpos,
701 font_info->gdef, &font_info->gdef_stream);
703 lookup_list = &font_info->gpos->LookupList;
704 for (i = 0; i < lookup_list->LookupCount; i++)
705 lookup_list->Properties[i] = HB_GLYPH_PROPERTIES_UNKNOWN;
709 const HB_FontClass hb_fontClass = {
710 convertStringToGlyphIndices, getGlyphAdvances, canRender,
711 getPointInOutline, getGlyphMetrics, getFontMetric
715 setup_features (int gsubp, FontInfoHB *font_info, MFLTOtfSpec *spec,
718 int count, preordered;
719 unsigned int *features;
720 FT_UShort script, langsys;
721 FT_UShort req_feature, index;
722 FT_UInt *feature_list;
727 if (! font_info->gsub)
729 if (load_gsub (font_info) != HB_Err_Ok)
732 if (HB_GSUB_Select_Script (font_info->gsub, spec->script, &script)
737 if (HB_GSUB_Select_Language (font_info->gsub, spec->langsys,
738 script, &langsys, &req_feature)
743 langsys = req_feature = 0xFFFF;
744 count = spec->gsub_count;
745 features = spec->gsub;
749 if (! font_info->gpos)
751 if (load_gpos (font_info) != HB_Err_Ok)
754 if (HB_GPOS_Select_Script (font_info->gpos, spec->script, &script)
759 if (HB_GPOS_Select_Language (font_info->gpos, spec->langsys,
760 script, &langsys, &req_feature)
765 langsys = req_feature = 0xFFFF;
766 count = spec->gpos_count;
767 features = spec->gpos;
770 for (preordered = 0; preordered < count; preordered++)
771 if (features[preordered] == 0)
773 if ((gsubp ? HB_GSUB_Query_Features (font_info->gsub, script, langsys,
775 : HB_GPOS_Query_Features (font_info->gpos, script, langsys,
782 for (i = 0; feature_list[i]; i++);
785 info->indices = malloc (sizeof (FT_UShort) * ((req_feature != 0xFFFF) + i));
787 if (req_feature != 0xFFFF)
788 info->indices[i++] = req_feature;
789 for (j = 0; j < preordered; j++)
790 if ((gsubp ? HB_GSUB_Select_Feature (font_info->gsub, features[j],
791 script, langsys, &index)
792 : HB_GPOS_Select_Feature (font_info->gpos, features[j],
793 script, langsys, &index))
795 info->indices[i++] = index;
797 for (j = 0; feature_list[j]; j++)
799 for (k = preordered + 1; k < count; k++)
800 if (feature_list[j] == features[k])
804 ? HB_GSUB_Select_Feature (font_info->gsub, feature_list[j],
805 script, langsys, &index)
806 : HB_GPOS_Select_Feature (font_info->gpos, feature_list[j],
807 script, langsys, &index))
809 info->indices[i++] = index;
815 setup_otf_spec (MFLTFont *font, MFLTOtfSpec *spec)
817 FontInfoHB *font_info = (FontInfoHB *) font;
820 if (spec->gsub_count + spec->gpos_count == 0)
822 ginfo = mplist_get (font_info->otf_spec_cache, spec->sym);
824 return (ginfo->gsub.count + ginfo->gpos.count > 0 ? ginfo : NULL);
825 ginfo = calloc (1, sizeof (GsubGposInfo));
826 mplist_push (font_info->otf_spec_cache, spec->sym, ginfo);
827 setup_features (1, font_info, spec, &(ginfo->gsub));
828 setup_features (0, font_info, spec, &(ginfo->gpos));
833 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
834 MFLTGlyphString *in, int from, int to,
835 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
837 FontInfoHB *font_info = (FontInfoHB *) font;
842 HB_UShort *apply_order_save;
843 HB_UShort apply_count_save;
844 int gpos_applied = 0;
851 if (hb_buffer_new (&buf) != FT_Err_Ok)
853 for (i = from; i < to; i++)
855 if (hb_buffer_add_glyph (buf, in->glyphs[i].code, 0, i) != FT_Err_Ok)
858 ginfo = setup_otf_spec (font, spec);
861 if (ginfo->gsub.count > 0)
863 if (! font_info->gdef
864 && load_gdef (font_info) != HB_Err_Ok)
866 apply_order_save = font_info->gsub->FeatureList.ApplyOrder;
867 apply_count_save = font_info->gsub->FeatureList.ApplyCount;
868 font_info->gsub->FeatureList.ApplyOrder = ginfo->gsub.indices;
869 font_info->gsub->FeatureList.ApplyCount = ginfo->gsub.count;
870 err = HB_GSUB_Apply_String (font_info->gsub, buf);
871 font_info->gsub->FeatureList.ApplyOrder = apply_order_save;
872 font_info->gsub->FeatureList.ApplyCount = apply_count_save;
873 if (err != HB_Err_Ok && err != HB_Err_Not_Covered)
875 if (out->used + buf->in_length > out->allocated)
879 if (ginfo->gpos.count > 0
880 && (font_info->gpos || load_gpos (font_info) == HB_Err_Ok))
882 apply_order_save = font_info->gpos->FeatureList.ApplyOrder;
883 apply_count_save = font_info->gpos->FeatureList.ApplyCount;
884 font_info->gpos->FeatureList.ApplyOrder = ginfo->gpos.indices;
885 font_info->gpos->FeatureList.ApplyCount = ginfo->gpos.count;
886 err = HB_GPOS_Apply_String (&font_info->hb_font, font_info->gpos,
887 FT_LOAD_DEFAULT, buf, FALSE, FALSE);
888 font_info->gpos->FeatureList.ApplyOrder = apply_order_save;
889 font_info->gpos->FeatureList.ApplyCount = apply_count_save;
890 if (err == HB_Err_Ok)
894 for (i = 0; i < buf->in_length; i++)
896 HB_GlyphItem hg = buf->in_string + i;
897 HB_Position pos = buf->positions + i;
900 out->glyphs[out->used] = in->glyphs[hg->cluster];
901 g = out->glyphs + out->used++;
902 if (g->code != hg->gindex)
903 g->c = 0, g->code = hg->gindex;
904 adjustment[i].set = gpos_applied;
907 adjustment[i].xadv = pos->x_advance;
908 adjustment[i].yadv = pos->y_advance;
909 adjustment[i].xoff = pos->x_pos;
910 adjustment[i].yoff = - pos->y_pos;
911 adjustment[i].back = pos->back;
912 adjustment[i].advance_is_absolute = pos->new_advance;
919 hb_buffer_free (buf);
920 for (i = 0; i < len; i++)
921 out->glyphs[out->used++] = in->glyphs[from + i];
926 open_font (char *fontname, char **err)
928 FT_Face face = new_face (fontname, err);
929 FontInfoHB *font_info;
933 font_info = calloc (1, sizeof (FontInfoHB));
934 font_info->face = face;
935 font_info->hb_font.klass = &hb_fontClass;
936 font_info->hb_font.faceData = face;
937 font_info->hb_font.userData = NULL;
938 font_info->hb_font.x_ppem = face->size->metrics.x_ppem;
939 font_info->hb_font.x_scale = face->size->metrics.x_scale;
940 font_info->hb_font.y_scale = face->size->metrics.y_scale;
941 font_info->hb_font.y_ppem = face->size->metrics.y_ppem;
942 font_info->otf_spec_cache = mplist ();
943 return ((MFLTFont *) font_info);
947 close_font (MFLTFont *font)
949 FontInfoHB *font_info = (FontInfoHB *) font;
954 HB_Done_GDEF_Table (font_info->gdef);
955 free (font_info->gdef_stream.base);
958 HB_Done_GSUB_Table (font_info->gsub);
960 HB_Done_GPOS_Table (font_info->gpos);
961 FT_Done_Face ((FT_Face) (font_info->hb_font.faceData));
962 for (p = font_info->otf_spec_cache; mplist_key (p) != Mnil;
965 GsubGposInfo *ginfo = mplist_value (p);
967 if (ginfo->gsub.count > 0)
968 free (ginfo->gsub.indices);
969 if (ginfo->gpos.count > 0)
970 free (ginfo->gpos.indices);
972 m17n_object_unref (font_info->otf_spec_cache);
978 #else /* (defined (FLT_PANGO) */
983 PangoOTRuleset *ruleset;
993 PangoFontMap *fontmap;
994 PangoContext *context;
995 PangoFcFont *pango_font;
996 MPlist *otf_spec_cache;
1000 get_glyph_id (MFLTFont *font, MFLTGlyph *g)
1002 FontInfoPango *font_info = (FontInfoPango *) font;
1004 g->code = pango_fc_font_get_glyph (font_info->pango_font, g->code);
1005 return (g->code ? 0 : -1);
1008 #define PANGO_SCALE_TO_26_6 (PANGO_SCALE / (1<<6))
1012 get_metric (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
1014 FontInfoPango *font_info = (FontInfoPango *) font;
1017 for (i = from; i < to; i++)
1019 MFLTGlyph *g = gstring->glyphs + from;
1020 PangoRectangle inc, logical;
1022 pango_font_get_glyph_extents (PANGO_FONT (font_info->pango_font),
1023 gstring->glyphs[i].code, &inc, &logical);
1024 g->lbearing = inc.x / PANGO_SCALE_TO_26_6;
1025 g->rbearing = (inc.x + inc.width) / PANGO_SCALE_TO_26_6;
1026 g->xadv = logical.width / PANGO_SCALE_TO_26_6;
1028 g->ascent = - inc.y / PANGO_SCALE_TO_26_6;
1029 g->descent = (inc.height + inc.y) / PANGO_SCALE_TO_26_6;
1034 int (*get_metric) (MFLTFont *font, MFLTGlyphString *gstring, int from, int to) = NULL;
1037 #ifndef PANGO_OT_DEFAULT_LANGUAGE
1038 #define PANGO_OT_DEFAULT_LANGUAGE ((guint) 0xFFFF)
1040 #ifndef PANGO_OT_ALL_GLYPHS
1041 #define PANGO_OT_ALL_GLYPHS ((guint) 0xFFFF)
1045 setup_features (PangoOTTableType type, FontInfoPango *font_info,
1046 MFLTOtfSpec *spec, FeatureInfo *info)
1048 int count, preordered;
1049 unsigned int *features;
1050 guint script, langsys;
1051 guint req_feature, index;
1052 guint *feature_list;
1055 PangoOTInfo *ot_info;
1057 face = pango_fc_font_lock_face (font_info->pango_font);
1058 ot_info = pango_ot_info_get (face);
1059 pango_fc_font_unlock_face (font_info->pango_font);
1061 if (! pango_ot_info_find_script (ot_info, type, spec->script, &script))
1065 if (! pango_ot_info_find_language (ot_info, type, script,
1067 &langsys, &req_feature))
1072 langsys = PANGO_OT_DEFAULT_LANGUAGE;
1073 req_feature = 0xFFFF;
1075 if (type == PANGO_OT_TABLE_GSUB)
1077 count = spec->gsub_count;
1078 features = spec->gsub;
1082 count = spec->gpos_count;
1083 features = spec->gpos;
1085 feature_list = NULL;
1086 for (preordered = 0; preordered < count; preordered++)
1087 if (features[preordered] == 0)
1089 feature_list = pango_ot_info_list_features (ot_info, type, 0,
1096 for (i = 0; feature_list[i]; i++);
1099 info->indices = malloc (sizeof (guint) * ((req_feature != 0xFFFF) + i));
1101 if (req_feature != 0xFFFF)
1102 info->indices[i++] = req_feature;
1103 for (j = 0; j < preordered; j++)
1104 if (pango_ot_info_find_feature (ot_info, type, features[j],
1105 script, langsys, &index))
1106 info->indices[i++] = index;
1108 for (j = 0; feature_list[j]; j++)
1110 for (k = preordered + 1; k < count; k++)
1111 if (feature_list[j] == features[k])
1114 && pango_ot_info_find_feature (ot_info, type, feature_list[j],
1115 script, langsys, &index))
1116 info->indices[i++] = index;
1119 info->ruleset = pango_ot_ruleset_new (ot_info);
1121 for (i = 0; i < info->count; i++)
1122 pango_ot_ruleset_add_feature (info->ruleset, type, info->indices[i],
1123 PANGO_OT_ALL_GLYPHS);
1125 for (i = info->count - 1; i >= 0; i--)
1126 pango_ot_ruleset_add_feature (info->ruleset, type, info->indices[i],
1127 PANGO_OT_ALL_GLYPHS);
1132 setup_otf_spec (FontInfoPango *font_info, MFLTOtfSpec *spec)
1134 GsubGposInfo *ginfo;
1136 if (spec->gsub_count + spec->gpos_count == 0)
1138 ginfo = mplist_get (font_info->otf_spec_cache, spec->sym);
1140 return (ginfo->gsub.count + ginfo->gpos.count > 0 ? ginfo : NULL);
1141 ginfo = calloc (1, sizeof (GsubGposInfo));
1142 mplist_push (font_info->otf_spec_cache, spec->sym, ginfo);
1143 setup_features (PANGO_OT_TABLE_GSUB, font_info, spec, &(ginfo->gsub));
1144 setup_features (PANGO_OT_TABLE_GPOS, font_info, spec, &(ginfo->gpos));
1149 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
1150 MFLTGlyphString *in, int from, int to,
1151 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
1153 FontInfoPango *font_info = (FontInfoPango *) font;
1154 PangoOTBuffer *buffer;
1155 GsubGposInfo *ginfo;
1156 PangoGlyphString *glyphs;
1159 buffer = pango_ot_buffer_new (font_info->pango_font);
1160 for (i = from; i < to; i++)
1161 pango_ot_buffer_add_glyph (buffer, in->glyphs[i].code, 0, i);
1162 ginfo = setup_otf_spec (font_info, spec);
1165 if (ginfo->gsub.count > 0)
1166 pango_ot_ruleset_substitute (ginfo->gsub.ruleset, buffer);
1167 if (ginfo->gpos.count > 0)
1168 pango_ot_ruleset_position (ginfo->gpos.ruleset, buffer);
1169 glyphs = pango_glyph_string_new ();
1170 pango_ot_buffer_output (buffer, glyphs);
1171 for (i = 0; i < glyphs->num_glyphs; i++)
1173 PangoGlyphInfo *glyph_info = glyphs->glyphs + i;
1176 out->glyphs[out->used] = in->glyphs[glyphs->log_clusters[i]];
1177 g = out->glyphs + out->used++;
1178 if (g->code != glyph_info->glyph)
1179 g->c = 0, g->code = glyph_info->glyph;
1180 g->xoff = glyph_info->geometry.x_offset / PANGO_SCALE_TO_26_6;
1181 g->yoff = glyph_info->geometry.y_offset / PANGO_SCALE_TO_26_6;
1182 g->xadv = glyph_info->geometry.width / PANGO_SCALE_TO_26_6;
1184 adjustment[i].set = 0;
1186 pango_ot_buffer_destroy (buffer);
1187 pango_glyph_string_free (glyphs);
1191 pango_ot_buffer_destroy (buffer);
1192 for (i = from; i < to; i++)
1193 out->glyphs[out->used++] = in->glyphs[i];
1199 open_font (char *fontname, char **err)
1201 FontInfoPango *font_info = NULL;
1202 PangoFontMap *fontmap;
1203 PangoContext *context;
1204 PangoFontDescription *desc;
1205 PangoFont *pango_font;
1210 if (parse_font_name (fontname, &family, &size, err) < 0)
1212 desc = pango_font_description_new ();
1213 pango_font_description_set_family (desc, family);
1215 pango_size = size * PANGO_SCALE;
1216 pango_font_description_set_absolute_size (desc, pango_size);
1218 fontmap = pango_ft2_font_map_new ();
1219 context = pango_context_new ();
1220 pango_context_set_font_map (context, fontmap);
1221 pango_font = pango_context_load_font (context, desc);
1222 pango_font_description_free (desc);
1225 font_info = calloc (1, sizeof (FontInfoPango));
1226 font_info->fontmap = fontmap;
1227 font_info->context = context;
1228 font_info->pango_font = PANGO_FC_FONT (pango_font);
1229 font_info->otf_spec_cache = mplist ();
1233 g_object_unref (context);
1234 g_object_unref (fontmap);
1236 return (MFLTFont *) font_info;
1240 close_font (MFLTFont *font)
1242 FontInfoPango *font_info = (FontInfoPango *) font;
1244 g_object_unref (font_info->context);
1245 g_object_unref (font_info->fontmap);
1253 run_flt (MText *mt, int from, int to, MFLTFont *font, char *flt_name)
1255 MDrawControl control;
1257 memset (&control, 0, sizeof (MDrawControl));
1258 control.two_dimensional = 1;
1259 control.enable_bidi = 1;
1260 control.anti_alias = 1;
1261 /*control.disable_caching = 1;*/
1262 mtext_put_prop (mt, from, to, Mfont, font->font);
1263 mdraw_text_extents (frame, mt, from, to, &control, NULL, NULL, NULL);
1270 run_flt (MText *mt, int from, int to, MFLTFont *font, char *flt_name)
1272 static MFLT *flt = NULL;
1273 MCharTable *coverage;
1274 static MFLTGlyphString gstring;
1277 && ! (flt = mflt_get (flt_name)))
1279 fprintf (stderr, "Invalid FLT name: %s\n", flt_name);
1282 coverage = mflt_coverage (flt);
1287 for (; from < to; from++)
1288 if (mchartable_lookup (coverage, mtext_ref_char (mt, from)))
1290 for (i = from + 1; i < to; i++)
1291 if (! mchartable_lookup (coverage, mtext_ref_char (mt, i)))
1298 if (gstring.allocated < len)
1300 gstring.allocated = len * 2;
1301 gstring.glyphs = realloc (gstring.glyphs,
1302 sizeof (MFLTGlyph) * len * 2);
1304 gstring.glyph_size = sizeof (MFLTGlyph);
1305 for (i = 0; i < len; i++)
1306 gstring.glyphs[i].c = mtext_ref_char (mt, from + i);
1308 i = mflt_run (&gstring, 0, len, font, flt);
1320 main (int argc, char **argv)
1322 char *font_name, *flt_name;
1330 fprintf (stderr, "Usage: flt FONT-NAME FLT-NAME < TEXT-FILE\n");
1334 font_name = argv[1];
1337 FT_Init_FreeType (&ft_library);
1339 font = open_font (font_name, &err);
1342 fprintf (stderr, "Font error: %s: %s\n", argv[1], err);
1345 font->get_glyph_id = get_glyph_id;
1346 font->get_metric = get_metric;
1347 font->drive_otf = drive_otf;
1349 mt = mconv_decode_stream (msymbol ("utf-8"), stdin);
1350 len = mtext_len (mt);
1351 for (i = 0, j = mtext_character (mt, i, len, '\n'); i < len;
1352 i = j + 1, j = mtext_character (mt, i, len, '\n'))
1358 to = run_flt (mt, i, j, font, flt_name);
1361 fprintf (stderr, "Error in FLT processing.\n");
1366 m17n_object_unref (mt);