+ stylename = p1;
+ }
+ while (*stylename && ! isalpha (*stylename))
+ stylename++;
+ }
+ return ft_info;
+}
+
+#ifdef HAVE_FONTCONFIG
+
+typedef struct
+{
+ int fc_value;
+ char *m17n_value;
+ MSymbol sym;
+} FC_vs_M17N_font_prop;
+
+static FC_vs_M17N_font_prop fc_weight_table[] =
+ { { FC_WEIGHT_THIN, "thin" },
+ { FC_WEIGHT_ULTRALIGHT, "extralight" },
+ { FC_WEIGHT_LIGHT, "light" },
+#ifdef FC_WEIGHT_BOOK
+ { FC_WEIGHT_BOOK, "book" },
+#endif /* FC_WEIGHT_BOOK */
+ { FC_WEIGHT_REGULAR, "normal" },
+ { FC_WEIGHT_NORMAL, "normal" },
+ { FC_WEIGHT_MEDIUM, "medium" },
+ { FC_WEIGHT_DEMIBOLD, "demibold" },
+ { FC_WEIGHT_BOLD, "bold" },
+ { FC_WEIGHT_EXTRABOLD, "extrabold" },
+ { FC_WEIGHT_BLACK, "black" },
+ { FC_WEIGHT_HEAVY, "heavy" },
+ { FC_WEIGHT_MEDIUM, NULL } };
+int fc_weight_table_size =
+ sizeof fc_weight_table / sizeof (FC_vs_M17N_font_prop);
+
+static FC_vs_M17N_font_prop fc_slant_table[] =
+ { { FC_SLANT_ROMAN, "r" },
+ { FC_SLANT_ITALIC, "i" },
+ { FC_SLANT_OBLIQUE, "o" },
+ { FC_SLANT_ROMAN, NULL } };
+int fc_slant_table_size =
+ sizeof fc_slant_table / sizeof (FC_vs_M17N_font_prop);
+
+static FC_vs_M17N_font_prop fc_width_table[] =
+ { { FC_WIDTH_ULTRACONDENSED, "ultracondensed" },
+ { FC_WIDTH_EXTRACONDENSED, "extracondensed" },
+ { FC_WIDTH_CONDENSED, "condensed" },
+ { FC_WIDTH_SEMICONDENSED, "semicondensed" },
+ { FC_WIDTH_NORMAL, "normal" },
+ { FC_WIDTH_SEMIEXPANDED, "semiexpanded" },
+ { FC_WIDTH_EXPANDED, "expanded" },
+ { FC_WIDTH_EXTRAEXPANDED, "extraexpanded" },
+ { FC_WIDTH_ULTRAEXPANDED, "ultraexpanded" },
+ { FC_WIDTH_NORMAL, NULL } };
+int fc_width_table_size =
+ sizeof fc_width_table / sizeof (FC_vs_M17N_font_prop);
+
+
+static FC_vs_M17N_font_prop *fc_all_table[] =
+ { fc_weight_table, fc_slant_table, fc_width_table };
+
+static MSymbol
+fc_decode_prop (int val, FC_vs_M17N_font_prop *table, int size)
+{
+ int i = size / 2;
+
+ if (val < table[i].fc_value)
+ {
+ for (i--; i >= 0; i--)
+ if (val > table[i].fc_value)
+ break;
+ i++;
+ }
+ else
+ {
+ for (; i < size; i++)
+ if (val <= table[i].fc_value)
+ break;
+ }
+ return table[i].sym;
+}
+
+static int
+fc_encode_prop (MSymbol sym, FC_vs_M17N_font_prop *table)
+{
+ int i;
+
+ for (i = 0; table[i].m17n_value; i++)
+ if (table[i].sym == sym)
+ break;
+ return table[i].fc_value;
+}
+
+FcPattern *
+fc_get_pattern (MFont *font)
+{
+ FcPattern *pat = FcPatternCreate ();
+ MSymbol sym, weight, style, stretch;
+
+
+ if ((sym = (MSymbol) FONT_PROPERTY (font, MFONT_FOUNDRY)) != Mnil)
+ FcPatternAddString (pat, FC_FOUNDRY, (FcChar8 *) MSYMBOL_NAME (sym));
+ if ((sym = (MSymbol) FONT_PROPERTY (font, MFONT_FAMILY)) != Mnil)
+ FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) MSYMBOL_NAME (sym));
+ if ((weight = (MSymbol) FONT_PROPERTY (font, MFONT_WEIGHT)) != Mnil)
+ FcPatternAddInteger (pat, FC_WEIGHT,
+ fc_encode_prop (weight, fc_weight_table));
+ if ((style = (MSymbol) FONT_PROPERTY (font, MFONT_STYLE)) != Mnil)
+ FcPatternAddInteger (pat, FC_SLANT,
+ fc_encode_prop (style, fc_slant_table));
+ if ((stretch = (MSymbol) FONT_PROPERTY (font, MFONT_STRETCH)) != Mnil)
+ FcPatternAddInteger (pat, FC_WIDTH,
+ fc_encode_prop (stretch, fc_width_table));
+ if (font->size > 0)
+ {
+ double size = font->size;
+ FcPatternAddDouble (pat, FC_PIXEL_SIZE, size / 10);
+ }
+ else if (font->size < 0)
+ {
+ double size = - font->size;
+ FcPatternAddDouble (pat, FC_SIZE, size / 10);
+ }
+ return pat;
+}
+
+static void
+fc_parse_pattern (FcPattern *pat, char *family, MFontFT *ft_info)
+{
+ FcChar8 *str;
+ int val;
+ double size;
+ char *buf;
+ int bufsize = 0;
+ MSymbol sym;
+ FcLangSet *ls;
+ FcCharSet *cs;
+ MFont *font = &ft_info->font;
+
+ MFONT_INIT (font);
+ if (FcPatternGetString (pat, FC_FOUNDRY, 0, &str) == FcResultMatch)
+ {
+ STRDUP_LOWER (buf, bufsize, (char *) str);
+ mfont__set_property (font, MFONT_FOUNDRY, msymbol (buf));
+ }
+ if (family)
+ mfont__set_property (font, MFONT_FAMILY, msymbol (family));
+ else if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
+ {
+ STRDUP_LOWER (buf, bufsize, (char *) str);
+ mfont__set_property (font, MFONT_FAMILY, msymbol (buf));
+ }
+ if (FcPatternGetInteger (pat, FC_WEIGHT, 0, &val) == FcResultMatch)
+ {
+ sym = fc_decode_prop (val, fc_weight_table, fc_weight_table_size);
+ mfont__set_property (font, MFONT_WEIGHT, sym);
+ }
+ if (FcPatternGetInteger (pat, FC_SLANT, 0, &val) == FcResultMatch)
+ {
+ sym = fc_decode_prop (val, fc_slant_table, fc_slant_table_size);
+ mfont__set_property (font, MFONT_STYLE, sym);
+ }
+ if (FcPatternGetInteger (pat, FC_WIDTH, 0, &val) == FcResultMatch)
+ {
+ sym = fc_decode_prop (val, fc_width_table, fc_width_table_size);
+ mfont__set_property (font, MFONT_STRETCH, sym);
+ }
+ if (FcPatternGetLangSet (pat, FC_LANG, 0, &ls) == FcResultMatch)
+ {
+ if (FcLangSetHasLang (ls, (FcChar8 *) "ja") != FcLangDifferentLang
+ || FcLangSetHasLang (ls, (FcChar8 *) "zh") != FcLangDifferentLang
+ || FcLangSetHasLang (ls, (FcChar8 *) "ko") != FcLangDifferentLang)
+ font->for_full_width = 1;
+ ft_info->langset = FcLangSetCopy (ls);
+ }
+ if (FcPatternGetCharSet (pat, FC_CHARSET, 0, &cs) == FcResultMatch)
+ ft_info->charset = FcCharSetCopy (cs);
+
+ mfont__set_property (font, MFONT_REGISTRY, Municode_bmp);
+ font->type = MFONT_TYPE_SPEC;
+ font->source = MFONT_SOURCE_FT;
+ if (FcPatternGetDouble (pat, FC_PIXEL_SIZE, 0, &size) == FcResultMatch)
+ font->size = size * 10;
+ if (FcPatternGetString (pat, FC_FILE, 0, &str) == FcResultMatch)
+ font->file = msymbol ((char *) str);
+}
+
+
+static MFontFT *
+fc_gen_font (FcPattern *pat, char *family)
+{
+ MFontFT *ft_info;
+
+ MSTRUCT_CALLOC (ft_info, MERROR_FONT_FT);
+ fc_parse_pattern (pat, family, ft_info);
+ ft_info->font.type = MFONT_TYPE_OBJECT;
+ return ft_info;
+}
+
+static void
+fc_init_font_list (void)
+{
+ FcPattern *pattern = FcPatternCreate ();
+ FcObjectSet *os = FcObjectSetBuild (FC_FAMILY, NULL);
+ FcFontSet *fs = FcFontList (fc_config, pattern, os);
+ MPlist *plist = mplist ();
+ char *buf;
+ int bufsize = 0;
+ int i;
+
+ ft_font_list = plist;
+ for (i = 0; i < fs->nfont; i++)
+ {
+ char *fam;
+
+ if (FcPatternGetString (fs->fonts[i], FC_FAMILY, 0,
+ (FcChar8 **) &fam) != FcResultMatch)
+ continue;
+ STRDUP_LOWER (buf, bufsize, fam);
+ plist = mplist_add (plist, msymbol (buf), NULL);
+ }
+ FcFontSetDestroy (fs);
+ FcObjectSetDestroy (os);
+ FcPatternDestroy (pattern);
+}
+
+static MPlist *
+fc_list_pattern (FcPattern *pattern)
+{
+ FcObjectSet *os = NULL;
+ FcFontSet *fs = NULL;
+ MSymbol last_family = Mnil;
+ MPlist *plist = NULL, *pl = NULL;
+ char *buf;
+ int bufsize = 0;
+ int i;
+
+ if (! (os = FcObjectSetBuild (FC_FAMILY, FC_FILE, NULL)))
+ goto err;
+ if (! (fs = FcFontList (fc_config, pattern, os)))
+ goto err;
+
+ for (i = 0; i < fs->nfont; i++)
+ {
+ MSymbol family, file;
+ char *fam, *filename;
+ MFontFT *ft_info;
+
+ if (FcPatternGetString (fs->fonts[i], FC_FAMILY, 0,
+ (FcChar8 **) &fam) != FcResultMatch)
+ continue;
+ if (FcPatternGetString (fs->fonts[i], FC_FILE, 0,
+ (FcChar8 **) &filename) != FcResultMatch)
+ continue;
+ STRDUP_LOWER (buf, bufsize, fam);
+ family = msymbol (buf);
+ file = msymbol (filename);
+ if (family != last_family)
+ {
+ pl = MPLIST_PLIST (ft_list_family (family, 0));
+ last_family = family;
+ }
+ ft_info = mplist_get (pl, file);
+ if (ft_info)
+ {
+ if (! plist)
+ plist = mplist ();
+ mplist_add (plist, family, ft_info);
+ }
+ }
+
+ err:
+ if (fs) FcFontSetDestroy (fs);
+ if (os) FcObjectSetDestroy (os);
+ return plist;
+}
+
+/* Return FcCharSet object built from CHAR_LIST or MT. In the latter
+ case, it is assured that the M-text contains at least one
+ character. */
+
+static FcCharSet *
+fc_build_charset (MPlist *char_list, MText *mt)
+{
+ FcCharSet *cs = FcCharSetCreate ();
+
+ if (! cs)
+ return NULL;
+ if (char_list)
+ {
+ for (; ! MPLIST_TAIL_P (char_list); char_list = MPLIST_NEXT (char_list))
+ if (! FcCharSetAddChar (cs, (FcChar32) MPLIST_INTEGER (char_list)))
+ {
+ FcCharSetDestroy (cs);
+ return NULL;
+ }
+ }
+ else
+ {
+ int i;
+
+ for (i = mtext_nchars (mt) - 1; i >= 0; i--)
+ if (! FcCharSetAddChar (cs, (FcChar32) mtext_ref_char (mt, i)))
+ {
+ FcCharSetDestroy (cs);
+ return NULL;
+ }
+ if (mtext_nchars (mt) > 0
+ && (mt = mtext_get_prop (mt, 0, Mtext)))
+ for (i = mtext_nchars (mt) - 1; i >= 0; i--)
+ if (! FcCharSetAddChar (cs, (FcChar32) mtext_ref_char (mt, i)))