# HG changeset patch # User Thinker K.F. Li # Date 1244592964 -28800 # Node ID b3d5ce48670ac47c2a3015f955874b13c7ef1f37 # Parent 27774b93521ef0a306f8f3a7764105e1d5b5ac67 Use fontconfig to find out a font for a family/slant/weight pattern diff -r 27774b93521e -r b3d5ce48670a src/shape_stext.c --- a/src/shape_stext.c Mon Jun 08 07:04:20 2009 +0800 +++ b/src/shape_stext.c Wed Jun 10 08:16:04 2009 +0800 @@ -1,16 +1,70 @@ +#if 0 + #include #include -#include +#include +#include #include "mb_shapes.h" -#if 0 - #ifndef ASSERT #define ASSERT(x) #endif #define OK 0 #define ERR -1 +static +FcPattern *query_fontconfig(const char *family, int slant, int weight) { + FcPattern *ptn, *p; + FcValue val; + FcConfig *cfg; + FcBool r; + static int slant_map[] = { /* from MB_FONT_SLANT_* to FC_SLANT_* */ + FC_SLANT_ROMAN, + FC_SLANT_ROMAN, + FC_SLANT_ITALIC, + FC_SLANT_OBLIQUE}; + + cfg = FcConfigGetCurrent(); + ptn = FcPatternCreate(); + p = FcPatternCreate(); + if(ptn == NULL || p == NULL) + goto err; + + val.type = FcTypeString; + val.u.s = family; + FcPatternAdd(ptn, "family", &val, FcTrue); + + val.type = FcTypeInteger; + val.u.i = slant_map[slant]; + FcPatternAdd(ptn, "slant", &val, FcTrue); + + val.type = FcTypeInteger; + val.u.i = weight; + FcPatternAdd(ptn, "weight", &val, FcTrue); + + r = FcConfigSubstituteWithPat(cfg, ptn, NULL, FcMatchPattern); + if(!r) + goto err; + + r = FcConfigSubstituteWithPat(cfg, p, ptn, FcMatchFont); + if(!r) + goto err; + + FcDefaultSubstitute(p); + + FcPatternDestroy(ptn); + + return p; + +err: + if(ptn) + FcPatternDestroy(ptn); + if(p) + FcPatternDestroy(p); + return NULL; + +} + /*! \brief Query and return a font face for a specified attribute vector. * * Programmers use mb_font_face_t to specify fonts used to show a @@ -28,29 +82,8 @@ int weight) { cairo_font_face_t *cface; FcPattern *ptn; - FcValue val; - static int slant_map[] = { /* from MB_FONT_SLANT_* to FC_SLANT_* */ - FC_SLANT_ROMAN, - FC_SLANT_ROMAN, - FC_SLANT_ITALIC, - FC_SLANT_OBLIQUE}; - ptn = FcPatternCreate(); - val.type = FcTypeString; - val.s = family; - FcPatternAdd(ptn, "family", val, FcTrue); - - if(family < 0 || family >= MB_FONT_SLANT_MAX) { - FcPatternDestroy(ptn); - return NULL; - } - val.type = FcTypeInteger; - val.i = slant_map[slant]; - FcPatternAdd(ptn, "slant", val, FcTrue); - - val.type = FcTypeInteger; - val.i = weight; - FcPatternAdd(ptn, "weight", val, FcTrue); + ptn = query_fontconfig(family, slant, weight); cface = cairo_ft_font_face_create_for_pattern(ptn); FcPatternDestroy(ptn);