comparison src/graph_engine_cairo.c @ 1067:7b4e80ab671a openvg

merge from default branch
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 01 Dec 2010 12:25:56 +0800
parents ac2e6468a22a 74e3ba4d3fa1
children d09f603438d8
comparison
equal deleted inserted replaced
630:bd18951b51d5 1067:7b4e80ab671a
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
2 // vim: sw=4:ts=8:sts=4
1 #include <fontconfig/fontconfig.h> 3 #include <fontconfig/fontconfig.h>
2 #include <cairo-ft.h> 4 #include <cairo-ft.h>
3 #include "mb_graph_engine_cairo.h" 5 #include "mb_graph_engine_cairo.h"
4 #include "mb_shapes.h" 6 #include "mb_shapes.h"
5 7
15 * \param slant make font prune if it it non-zero. 17 * \param slant make font prune if it it non-zero.
16 * \param weight make font normal if it is 100. 18 * \param weight make font normal if it is 100.
17 */ 19 */
18 static 20 static
19 FcPattern *query_font_pattern(const char *family, int slant, int weight) { 21 FcPattern *query_font_pattern(const char *family, int slant, int weight) {
20 FcPattern *ptn, *p, *fn_ptn; 22 FcPattern *ptn, *fn_ptn;
21 FcValue val; 23 FcValue val;
22 FcConfig *cfg; 24 FcConfig *cfg;
23 FcBool r; 25 FcBool r;
24 FcResult result; 26 FcResult result;
25 static int slant_map[] = { /* from MB_FONT_SLANT_* to FC_SLANT_* */ 27 static int slant_map[] = { /* from MB_FONT_SLANT_* to FC_SLANT_* */
28 FC_SLANT_ITALIC, 30 FC_SLANT_ITALIC,
29 FC_SLANT_OBLIQUE}; 31 FC_SLANT_OBLIQUE};
30 32
31 cfg = FcConfigGetCurrent(); 33 cfg = FcConfigGetCurrent();
32 ptn = FcPatternCreate(); 34 ptn = FcPatternCreate();
33 p = FcPatternCreate(); 35 if(ptn == NULL)
34 if(ptn == NULL || p == NULL)
35 goto err; 36 goto err;
36 37
37 val.type = FcTypeString; 38 val.type = FcTypeString;
38 val.u.s = family; 39 val.u.s = family;
39 FcPatternAdd(ptn, "family", val, FcTrue); 40 FcPatternAdd(ptn, "family", val, FcTrue);
40 41
41 val.type = FcTypeInteger; 42 val.type = FcTypeInteger;
42 val.u.i = slant_map[slant]; 43 val.u.i = slant_map[slant];
43 FcPatternAdd(ptn, "slant", val, FcTrue); 44 FcPatternAdd(ptn, "slant", val, FcTrue);
44 45
45 val.type = FcTypeInteger; 46 val.type = FcTypeInteger;
46 val.u.i = weight; 47 val.u.i = weight;
47 FcPatternAdd(ptn, "weight", val, FcTrue); 48 FcPatternAdd(ptn, "weight", val, FcTrue);
48 49
49 r = FcConfigSubstituteWithPat(cfg, ptn, NULL, FcMatchPattern); 50 FcDefaultSubstitute(ptn);
50 if(!r) 51
51 goto err; 52 r = FcConfigSubstituteWithPat(cfg, ptn, ptn, FcMatchPattern);
52
53 r = FcConfigSubstituteWithPat(cfg, p, ptn, FcMatchFont);
54 if(!r) 53 if(!r)
55 goto err; 54 goto err;
56 55
57 FcDefaultSubstitute(p); 56 r = FcConfigSubstituteWithPat(cfg, ptn, ptn, FcMatchFont);
57 if(!r)
58 goto err;
58 59
59 fn_ptn = FcFontMatch(cfg, p, &result); 60 fn_ptn = FcFontMatch(cfg, ptn, &result);
60 61
61 /* It is supposed to return FcResultMatch. But, it is no, now. 62 /* It is supposed to return FcResultMatch. But, it is no, now.
62 * I don't know why. Someone should figure out the issue. 63 * I don't know why. Someone should figure out the issue.
63 */ 64 */
64 #if 0 65 #if 0
69 #endif 70 #endif
70 if(fn_ptn == NULL) 71 if(fn_ptn == NULL)
71 goto err; 72 goto err;
72 73
73 FcPatternDestroy(ptn); 74 FcPatternDestroy(ptn);
74 FcPatternDestroy(p); 75
75
76 return fn_ptn; 76 return fn_ptn;
77 77
78 err: 78 err:
79 if(ptn) 79 if(ptn)
80 FcPatternDestroy(ptn); 80 FcPatternDestroy(ptn);
81 if(p)
82 FcPatternDestroy(p);
83 return NULL; 81 return NULL;
84 82
85 } 83 }
86 84
87 /*! \brief Find out a font face for a pattern specified. 85 /*! \brief Find out a font face for a pattern specified.
92 */ 90 */
93 mbe_font_face_t * 91 mbe_font_face_t *
94 mbe_query_font_face(const char *family, int slant, int weight) { 92 mbe_query_font_face(const char *family, int slant, int weight) {
95 mbe_font_face_t *cface; 93 mbe_font_face_t *cface;
96 FcPattern *ptn; 94 FcPattern *ptn;
97 95
98 ptn = query_font_pattern(family, slant, weight); 96 ptn = query_font_pattern(family, slant, weight);
99 cface = cairo_ft_font_face_create_for_pattern(ptn); 97 cface = cairo_ft_font_face_create_for_pattern(ptn);
100 FcPatternDestroy(ptn); 98 FcPatternDestroy(ptn);
101 99
102 return cface; 100 return cface;
103 } 101 }
104 102
105 void 103 void
106 mbe_free_font_face(mbe_font_face_t *face) { 104 mbe_free_font_face(mbe_font_face_t *face) {
119 117
120 ptn = cairo_pattern_create_radial(cx0, cy0, radius0, 118 ptn = cairo_pattern_create_radial(cx0, cy0, radius0,
121 cx1, cy1, radius1); 119 cx1, cy1, radius1);
122 if(ptn == NULL) 120 if(ptn == NULL)
123 return NULL; 121 return NULL;
124 122
125 stop = stops; 123 stop = stops;
126 for(i = 0; i < stop_cnt; i++) { 124 for(i = 0; i < stop_cnt; i++) {
127 cairo_pattern_add_color_stop_rgba(ptn, stop->offset, 125 cairo_pattern_add_color_stop_rgba(ptn, stop->offset,
128 stop->r, stop->g, stop->b, stop->a); 126 stop->r, stop->g, stop->b, stop->a);
129 stop++; 127 stop++;
150 stop++; 148 stop++;
151 } 149 }
152 150
153 return ptn; 151 return ptn;
154 } 152 }
155
156 mbe_pattern_t *
157 mbe_pattern_create_image(mb_img_data_t *img) {
158 cairo_surface_t *surf;
159 cairo_pattern_t *ptn;
160 cairo_format_t fmt;
161
162 switch(img->fmt) {
163 case MB_IFMT_ARGB32:
164 fmt = CAIRO_FORMAT_ARGB32;
165 break;
166
167 case MB_IFMT_RGB24:
168 fmt = CAIRO_FORMAT_RGB24;
169 break;
170
171 case MB_IFMT_A8:
172 fmt = CAIRO_FORMAT_A8;
173 break;
174
175 case MB_IFMT_A1:
176 fmt = CAIRO_FORMAT_A1;
177 break;
178
179 case MB_IFMT_RGB16_565:
180 fmt = CAIRO_FORMAT_RGB16_565;
181 break;
182
183 default:
184 return NULL;
185 }
186
187 surf = cairo_image_surface_create_for_data(img->content, fmt,
188 img->w, img->h, img->stride);
189 ptn = cairo_pattern_create_for_surface(surf);
190 cairo_surface_destroy(surf);
191
192 return ptn;
193 }
194
195 void
196 mbe_scissoring(mbe_t *canvas, int n_areas, area_t **areas) {
197 area_t *area;
198 int i;
199
200 cairo_new_path(canvas);
201
202 for(i = 0; i < n_areas; i++) {
203 area = areas[i];
204 cairo_rectangle(canvas, area->x, area->y, area->w, area->h);
205 }
206
207 cairo_clip(canvas);
208 }