+#include <stdio.h>
+#include <string.h>
+#include <m17n-gui.h>
+#include <ft2build.h>
+#include FT_FREETYPE_H
+#include <fontconfig/fontconfig.h>
+#include <fontconfig/fcfreetype.h>
+
+static FT_Library ft_library;
+
+MFont *
+open_font (MFrame *frame, char *fontname, char **err)
+{
+ FcPattern *pat;
+ FcChar8 *fam, *file;
+ double pixelsize;
+ FcFontSet *fs;
+ FcObjectSet *os;
+ FT_Face face;
+
+ pat = FcNameParse ((FcChar8 *) fontname);
+ if (! pat)
+ {
+ *err = "invalid name format";
+ return NULL;
+ }
+ if (FcPatternGetString (pat, FC_FAMILY, 0, &fam) != FcResultMatch)
+ {
+ *err = "no family name";
+ return NULL;
+ }
+ if (FcPatternGetDouble (pat, FC_PIXEL_SIZE, 0, &pixelsize) != FcResultMatch)
+ pixelsize = 20;
+ FcPatternAddBool (pat, FC_SCALABLE, FcTrue);
+ FcPatternDel (pat, FC_PIXEL_SIZE);
+ os = FcObjectSetBuild (FC_FILE, NULL);
+ fs = FcFontList (NULL, pat, os);
+ if (fs->nfont == 0)
+ {
+ *err = "no matching font";
+ return NULL;
+ }
+ FcPatternGetString (fs->fonts[0], FC_FILE, 0, &file);
+
+ if (FT_New_Face (ft_library, (char *) file, 0, &face))
+ {
+ FcFontSetDestroy (fs);
+ FcObjectSetDestroy (os);
+ FcPatternDestroy (pat);
+ *err = "font open fail";
+ return NULL;
+ }
+ FcFontSetDestroy (fs);
+ FcObjectSetDestroy (os);
+ FcPatternDestroy (pat);
+ if (FT_Set_Pixel_Sizes (face, pixelsize, pixelsize))
+ {
+ *err = "set pixelsize fail";
+ FT_Done_Face (face);
+ return NULL;
+ }
+ return mfont_encapsulate (frame, Mfreetype, face);
+}
+
+int
+main (int argc, char **argv)
+{
+ char *err;
+ MText *mt;
+ MFrame *frame;
+ MFont *font;
+ int len;
+
+ if (argc < 2)
+ {
+ fprintf (stderr, "Usage: flt-old FONT-NAME < TEXT-FILE\n");
+ exit (1);
+ }
+
+ FT_Init_FreeType (&ft_library);
+ M17N_INIT ();
+ mt = mconv_decode_stream (msymbol ("utf-8"), stdin);
+ len = mtext_len (mt);
+ {
+ MFace *face = mface ();
+ MPlist *plist = mplist ();
+ MFontset *fontset = mfontset ("generic");
+
+ mface_put_prop (face, Mfontset, fontset);
+ mplist_add (plist, Mface, face);
+ mplist_add (plist, Mdevice, Mnil);
+ frame = mframe (plist);
+ m17n_object_unref (plist);
+ m17n_object_unref (face);
+ m17n_object_unref (fontset);
+ }
+ font = open_font (frame, argv[1], &err);
+ mtext_put_prop (mt, 0, len, Mfont, font);
+ if (! font)
+ {
+ fprintf (stderr, "Font error: %s: %s\n", argv[1], err);
+ exit (1);
+ }
+
+ mdraw_text_extents (frame, mt, 0, len, NULL, NULL, NULL, NULL);
+ m17n_object_unref (frame);
+ m17n_object_unref (mt);
+ M17N_FINI ();
+ return 0;
+}