comparison src/shape_stext.c @ 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
comparison
equal deleted inserted replaced
393:27774b93521e 394:b3d5ce48670a
1 #if 0
2
1 #include <stdio.h> 3 #include <stdio.h>
2 #include <cairo.h> 4 #include <cairo.h>
3 #include <fontconfig.h> 5 #include <cairo-ft.h>
6 #include <fontconfig/fontconfig.h>
4 #include "mb_shapes.h" 7 #include "mb_shapes.h"
5
6 #if 0
7 8
8 #ifndef ASSERT 9 #ifndef ASSERT
9 #define ASSERT(x) 10 #define ASSERT(x)
10 #endif 11 #endif
11 #define OK 0 12 #define OK 0
12 #define ERR -1 13 #define ERR -1
14
15 static
16 FcPattern *query_fontconfig(const char *family, int slant, int weight) {
17 FcPattern *ptn, *p;
18 FcValue val;
19 FcConfig *cfg;
20 FcBool r;
21 static int slant_map[] = { /* from MB_FONT_SLANT_* to FC_SLANT_* */
22 FC_SLANT_ROMAN,
23 FC_SLANT_ROMAN,
24 FC_SLANT_ITALIC,
25 FC_SLANT_OBLIQUE};
26
27 cfg = FcConfigGetCurrent();
28 ptn = FcPatternCreate();
29 p = FcPatternCreate();
30 if(ptn == NULL || p == NULL)
31 goto err;
32
33 val.type = FcTypeString;
34 val.u.s = family;
35 FcPatternAdd(ptn, "family", &val, FcTrue);
36
37 val.type = FcTypeInteger;
38 val.u.i = slant_map[slant];
39 FcPatternAdd(ptn, "slant", &val, FcTrue);
40
41 val.type = FcTypeInteger;
42 val.u.i = weight;
43 FcPatternAdd(ptn, "weight", &val, FcTrue);
44
45 r = FcConfigSubstituteWithPat(cfg, ptn, NULL, FcMatchPattern);
46 if(!r)
47 goto err;
48
49 r = FcConfigSubstituteWithPat(cfg, p, ptn, FcMatchFont);
50 if(!r)
51 goto err;
52
53 FcDefaultSubstitute(p);
54
55 FcPatternDestroy(ptn);
56
57 return p;
58
59 err:
60 if(ptn)
61 FcPatternDestroy(ptn);
62 if(p)
63 FcPatternDestroy(p);
64 return NULL;
65
66 }
13 67
14 /*! \brief Query and return a font face for a specified attribute vector. 68 /*! \brief Query and return a font face for a specified attribute vector.
15 * 69 *
16 * Programmers use mb_font_face_t to specify fonts used to show a 70 * Programmers use mb_font_face_t to specify fonts used to show a
17 * block of text on the output device. They can get mb_font_face_t with 71 * block of text on the output device. They can get mb_font_face_t with
26 const char *family, 80 const char *family,
27 int slant, 81 int slant,
28 int weight) { 82 int weight) {
29 cairo_font_face_t *cface; 83 cairo_font_face_t *cface;
30 FcPattern *ptn; 84 FcPattern *ptn;
31 FcValue val; 85
32 static int slant_map[] = { /* from MB_FONT_SLANT_* to FC_SLANT_* */ 86 ptn = query_fontconfig(family, slant, weight);
33 FC_SLANT_ROMAN,
34 FC_SLANT_ROMAN,
35 FC_SLANT_ITALIC,
36 FC_SLANT_OBLIQUE};
37
38 ptn = FcPatternCreate();
39 val.type = FcTypeString;
40 val.s = family;
41 FcPatternAdd(ptn, "family", val, FcTrue);
42
43 if(family < 0 || family >= MB_FONT_SLANT_MAX) {
44 FcPatternDestroy(ptn);
45 return NULL;
46 }
47 val.type = FcTypeInteger;
48 val.i = slant_map[slant];
49 FcPatternAdd(ptn, "slant", val, FcTrue);
50
51 val.type = FcTypeInteger;
52 val.i = weight;
53 FcPatternAdd(ptn, "weight", val, FcTrue);
54 87
55 cface = cairo_ft_font_face_create_for_pattern(ptn); 88 cface = cairo_ft_font_face_create_for_pattern(ptn);
56 FcPatternDestroy(ptn); 89 FcPatternDestroy(ptn);
57 90
58 return (mb_font_face_t *)cface; 91 return (mb_font_face_t *)cface;