Mercurial > MadButterfly
changeset 394:b3d5ce48670a
Use fontconfig to find out a font for a family/slant/weight pattern
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 10 Jun 2009 08:16:04 +0800 |
parents | 27774b93521e |
children | d30b575a4ad4 |
files | src/shape_stext.c |
diffstat | 1 files changed, 58 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- 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 <stdio.h> #include <cairo.h> -#include <fontconfig.h> +#include <cairo-ft.h> +#include <fontconfig/fontconfig.h> #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);