1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cairo.cpp
3 // Purpose: Cairo library
4 // Author: Anthony Betaudeau
7 // Copyright: (c) Anthony Bretaudeau
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 // keep cairo.h from defining dllimport as we're defining the symbols inside
20 // the wx dll in order to load them dynamically.
24 #include "wx/dynlib.h"
29 #include "wx/msw/wrapwin.h"
33 #include "wx/osx/private.h"
34 #include <cairo-quartz.h>
38 #include "wx/module.h"
42 #define wxCAIRO_METHOD_TYPE(name) \
45 #define wxCAIRO_STATIC_METHOD_DEFINE(rettype, name, args, argnames, defret) \
46 static wxCAIRO_METHOD_TYPE(name) name;
48 #define wxCAIRO_STATIC_VOIDMETHOD_DEFINE(name, args, argnames) \
49 wxCAIRO_STATIC_METHOD_DEFINE(void, name, args, argnames, NULL)
51 #define wxFOR_ALL_CAIRO_VOIDMETHODS(m) \
52 m( cairo_append_path, \
53 (cairo_t *cr, const cairo_path_t *path), (cr, path) ) \
55 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
56 m( cairo_arc_negative, \
57 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
59 (cairo_t *cr), (cr) ) \
60 m( cairo_close_path, \
61 (cairo_t *cr), (cr) ) \
63 (cairo_t *cr, double x1, double y1, double x2, double y2, double x3, double y3), (cr, x1, y1, x2, y2, x3, y3) ) \
65 (cairo_t *cr), (cr) ) \
67 (cairo_t *cr), (cr) ) \
68 m( cairo_fill_preserve, \
69 (cairo_t *cr), (cr) ) \
70 m( cairo_font_extents, \
71 (cairo_t *cr, cairo_font_extents_t *extents), (cr, extents) ) \
72 m( cairo_font_face_destroy, \
73 (cairo_font_face_t *font_face), (font_face) ) \
74 m( cairo_get_current_point, \
75 (cairo_t *cr, double *x, double *y), (cr, x, y) ) \
76 m( cairo_get_matrix, \
77 (cairo_t *cr, cairo_matrix_t *matrix), (cr, matrix) ) \
79 (cairo_t *cr, double x, double y), (cr, x, y) ) \
80 m( cairo_matrix_init, \
81 (cairo_matrix_t *matrix, double xx, double yx, double xy, double yy, double x0, double y0), (matrix, xx, yx, xy, yy, x0, y0) ) \
82 m( cairo_matrix_multiply, \
83 (cairo_matrix_t *result, const cairo_matrix_t *a, const cairo_matrix_t *b), (result, a, b) ) \
84 m( cairo_matrix_rotate, \
85 (cairo_matrix_t *matrix, double radians), (matrix, radians) ) \
86 m( cairo_matrix_scale, \
87 (cairo_matrix_t *matrix, double sx, double sy), (matrix, sx, sy) ) \
88 m( cairo_matrix_transform_distance, \
89 (const cairo_matrix_t *matrix, double *dx, double *dy), (matrix, dx, dy) ) \
90 m( cairo_matrix_transform_point, \
91 (const cairo_matrix_t *matrix, double *x, double *y), (matrix, x, y) ) \
92 m( cairo_matrix_translate, \
93 (cairo_matrix_t *matrix, double tx, double ty), (matrix, tx, ty) ) \
95 (cairo_t *cr, double x, double y), (cr, x, y) ) \
97 (cairo_t *cr), (cr) ) \
99 (cairo_t *cr), (cr) ) \
100 m( cairo_paint_with_alpha, \
101 (cairo_t *cr, double alpha), (cr, alpha) ) \
102 m( cairo_path_destroy, \
103 (cairo_path_t *path), (path) ) \
104 m( cairo_pattern_add_color_stop_rgba, \
105 (cairo_pattern_t *pattern, double offset, double red, double green, double blue, double alpha), (pattern, offset, red, green, blue, alpha) ) \
106 m( cairo_pattern_destroy, \
107 (cairo_pattern_t *pattern), (pattern) ) \
108 m( cairo_pattern_set_extend, \
109 (cairo_pattern_t *pattern, cairo_extend_t extend), (pattern, extend) ) \
110 m( cairo_pattern_set_filter, \
111 (cairo_pattern_t *pattern, cairo_filter_t filter), (pattern, filter) ) \
112 m( cairo_pop_group_to_source, \
113 (cairo_t *cr), (cr) ) \
114 m( cairo_push_group, \
115 (cairo_t *cr), (cr) ) \
116 m( cairo_rectangle, \
117 (cairo_t *cr, double x, double y, double width, double height), (cr, x, y, width, height) ) \
118 m( cairo_reset_clip, \
119 (cairo_t *cr), (cr) ) \
121 (cairo_t *cr), (cr) ) \
123 (cairo_t *cr, double angle), (cr, angle) ) \
125 (cairo_t *cr), (cr) ) \
127 (cairo_t *cr, double sx, double sy), (cr, sx, sy) ) \
128 m( cairo_select_font_face, \
129 (cairo_t *cr, const char *family, cairo_font_slant_t slant, cairo_font_weight_t weight), (cr, family, slant, weight) ) \
130 m( cairo_set_antialias, \
131 (cairo_t *cr, cairo_antialias_t antialias), (cr, antialias) ) \
133 (cairo_t *cr, const double *dashes, int num_dashes, double offset), (cr, dashes, num_dashes, offset) ) \
134 m( cairo_set_fill_rule, \
135 (cairo_t *cr, cairo_fill_rule_t fill_rule), (cr, fill_rule) ) \
136 m( cairo_set_font_face, \
137 (cairo_t *cr, cairo_font_face_t *font_face), (cr, font_face) ) \
138 m( cairo_set_font_size, \
139 (cairo_t *cr, double size), (cr, size) ) \
140 m( cairo_set_line_cap, \
141 (cairo_t *cr, cairo_line_cap_t line_cap), (cr, line_cap) ) \
142 m( cairo_set_line_join, \
143 (cairo_t *cr, cairo_line_join_t line_join), (cr, line_join) ) \
144 m( cairo_set_line_width, \
145 (cairo_t *cr, double width), (cr, width) ) \
146 m( cairo_set_matrix, \
147 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
148 m( cairo_set_operator, \
149 (cairo_t *cr, cairo_operator_t op), (cr, op) ) \
150 m( cairo_set_source, \
151 (cairo_t *cr, cairo_pattern_t *source), (cr, source) ) \
152 m( cairo_set_source_rgba, \
153 (cairo_t *cr, double red, double green, double blue, double alpha), (cr, red, green, blue, alpha) ) \
154 m( cairo_show_text, \
155 (cairo_t *cr, const char *utf8), (cr, utf8) ) \
157 (cairo_t *cr), (cr) ) \
158 m( cairo_stroke_extents, \
159 (cairo_t *cr, double *x1, double *y1, double *x2, double *y2), (cr, x1, y1, x2, y2) ) \
160 m( cairo_stroke_preserve, \
161 (cairo_t *cr), (cr) ) \
162 m( cairo_surface_destroy, \
163 (cairo_surface_t *surface), (surface) ) \
164 m( cairo_text_extents, \
165 (cairo_t *cr, const char *utf8, cairo_text_extents_t *extents), (cr, utf8, extents) ) \
166 m( cairo_transform, \
167 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
168 m( cairo_translate, \
169 (cairo_t *cr, double tx, double ty), (cr, tx, ty) ) \
172 #define wxCAIRO_PLATFORM_METHODS(m) \
173 m( cairo_font_face_t*, cairo_quartz_font_face_create_for_cgfont, \
174 (CGFontRef font), (font), NULL ) \
175 m( cairo_surface_t*, cairo_quartz_surface_create_for_cg_context, \
176 (CGContextRef cgContext, unsigned int width, unsigned int height), (cgContext, width, height), NULL )
177 #elif defined(__WXMSW__)
178 #define wxCAIRO_PLATFORM_METHODS(m) \
179 m( cairo_surface_t*, cairo_win32_surface_create, \
180 (HDC hdc), (hdc), NULL ) \
181 m( cairo_surface_t*, cairo_win32_printing_surface_create, \
182 (HDC hdc), (hdc), NULL )
184 #define wxCAIRO_PLATFORM_METHODS(m)
187 #define wxFOR_ALL_CAIRO_METHODS(m) \
188 m( cairo_path_t*, cairo_copy_path, \
189 (cairo_t *cr), (cr), NULL ) \
190 m( cairo_t*, cairo_create, \
191 (cairo_surface_t *target), (target), NULL) \
192 m( cairo_surface_t*, cairo_get_target, \
193 (cairo_t *cr), (cr), NULL) \
194 m( cairo_surface_t*, cairo_image_surface_create, \
195 (cairo_format_t format, int width, int height), (format, width, height), NULL ) \
196 m( cairo_surface_t*, cairo_image_surface_create_for_data, \
197 (unsigned char *data, cairo_format_t format, int width, int height, int stride), (data, format, width, height, stride), NULL) \
198 m( cairo_bool_t, cairo_in_fill, \
199 (cairo_t *cr, double x, double y), (cr, x, y), false ) \
200 m( cairo_status_t, cairo_matrix_invert, \
201 (cairo_matrix_t *matrix), (matrix), NULL) \
202 m( cairo_pattern_t*, cairo_pattern_create_for_surface, \
203 (cairo_surface_t *surface), (surface), NULL) \
204 m( cairo_pattern_t*, cairo_pattern_create_linear, \
205 (double x0, double y0, double x1, double y1), (x0, y0, x1, y1), NULL) \
206 m( cairo_pattern_t*, cairo_pattern_create_radial, \
207 (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1), (cx0, cy0, radius0, cx1, cy1, radius1), NULL) \
208 m( cairo_status_t, cairo_pattern_status, \
209 (cairo_pattern_t *pattern), (pattern), 4) \
210 m( cairo_t*, cairo_reference, \
211 (cairo_t *cr), (cr), NULL ) \
212 m( cairo_surface_t*, cairo_surface_create_similar, \
213 (cairo_surface_t *other, cairo_content_t content, int width, int height), (other, content, width, height), NULL) \
214 m( int, cairo_format_stride_for_width, \
215 (cairo_format_t format, int width), (format, width), 0) \
216 m( int, cairo_version, \
218 m( int, cairo_image_surface_get_stride, \
219 (cairo_surface_t *surface), (surface), 0) \
220 m( unsigned char *, cairo_image_surface_get_data, \
221 (cairo_surface_t *surface), (surface), NULL) \
222 m( cairo_format_t, cairo_image_surface_get_format, \
223 (cairo_surface_t *surface), (surface), CAIRO_FORMAT_INVALID) \
224 m( cairo_surface_type_t, cairo_surface_get_type, \
225 (cairo_surface_t *surface), (surface), -1) \
226 wxCAIRO_PLATFORM_METHODS(m)
230 #define wxFOR_ALL_PANGO_CAIRO_VOIDMETHODS(m) \
231 m( pango_cairo_update_layout, \
232 (cairo_t *cr, PangoLayout *layout), (cr, layout) ) \
233 m( pango_cairo_show_layout, \
234 (cairo_t *cr, PangoLayout *layout), (cr, layout) )
237 #define wxCAIRO_DECLARE_TYPE(rettype, name, args, argnames, defret) \
238 typedef rettype (*wxCAIRO_METHOD_TYPE(name)) args ; \
239 wxCAIRO_METHOD_TYPE(name) wxDL_METHOD_NAME(name);
241 #define wxCAIRO_DECLARE_VOIDTYPE(name, args, argnames) \
242 wxCAIRO_DECLARE_TYPE(void, name, args, argnames, NULL)
244 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_DECLARE_VOIDTYPE
)
245 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_DECLARE_TYPE
)
251 static bool Initialize();
253 // for internal use only
254 static void CleanUp();
257 // the single wxCairo instance or NULL
258 static wxCairo
*ms_lib
;
265 wxDynamicLibrary m_libCairo
;
266 wxDynamicLibrary m_libPangoCairo
;
268 // true if we successfully loaded the libraries and can use them
270 // note that this field must have this name as it's used by wxDL_XXX macros
275 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_STATIC_VOIDMETHOD_DEFINE
)
276 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_STATIC_METHOD_DEFINE
)
277 #if wxUSE_PANGO // untested, uncomment to test compilation.
278 //wxFOR_ALL_PANGO_METHODS(wxDL_STATIC_METHOD_DEFINE)
281 wxDECLARE_NO_COPY_CLASS(wxCairo
);
284 #define wxINIT_CAIRO_VOIDFUNC(name, params, args) \
285 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
287 #define wxINIT_CAIRO_FUNC(rettype, name, params, args, defret) \
288 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
290 wxFOR_ALL_CAIRO_VOIDMETHODS(wxINIT_CAIRO_VOIDFUNC
)
291 wxFOR_ALL_CAIRO_METHODS(wxINIT_CAIRO_FUNC
)
293 #undef wxINIT_CAIRO_FUNC
295 wxCairo
*wxCairo::ms_lib
= NULL
;
297 //----------------------------------------------------------------------------
299 //----------------------------------------------------------------------------
306 wxString
cairoDllStr("libcairo-2.dll");
308 wxString
cairoDllStr("libcairo.so.2");
310 m_libCairo
.Load(cairoDllStr
);
311 m_ok
= m_libCairo
.IsLoaded();
316 m_libPangoCairo
.Load("libpangocairo-1.0.so.0");
317 m_ok
= m_libPangoCairo
.IsLoaded();
326 #define wxDO_LOAD_FUNC(name, nameStr) \
327 name = (wxCAIRO_METHOD_TYPE(name))m_libCairo.RawGetSymbol(nameStr); \
331 #define wxLOAD_CAIRO_VOIDFUNC(name, params, args) \
332 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
334 #define wxLOAD_CAIRO_FUNC(rettype, name, params, args, defret) \
335 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
337 wxFOR_ALL_CAIRO_VOIDMETHODS(wxLOAD_CAIRO_VOIDFUNC
)
338 wxFOR_ALL_CAIRO_METHODS(wxLOAD_CAIRO_FUNC
)
340 #undef wxLOAD_CAIRO_FUNC
349 /* static */ bool wxCairo::Initialize()
353 ms_lib
= new wxCairo();
354 if ( !ms_lib
->IsOk() )
361 return ms_lib
!= NULL
;
364 /* static */ void wxCairo::CleanUp()
378 // ============================================================================
379 // implementation of the functions themselves
380 // ============================================================================
387 return wxCairo::Initialize();
390 void wxCairoCleanUp()
395 #define wxIMPL_CAIRO_FUNC(rettype, name, params, args, defret) \
396 rettype name params \
398 wxASSERT_MSG(wxCairo::Initialize(), "Cairo not initialized"); \
399 return wxCairo::name args; \
402 #define wxIMPL_CAIRO_VOIDFUNC(name, params, args) \
403 wxIMPL_CAIRO_FUNC(void, name, params, args, NULL)
405 // we currently link directly to Cairo on GTK since it is usually available there,
406 // so don't use our cairo_xyz wrapper functions until the decision is made to
407 // always load Cairo dynamically there.
409 wxFOR_ALL_CAIRO_VOIDMETHODS(wxIMPL_CAIRO_VOIDFUNC
)
410 wxFOR_ALL_CAIRO_METHODS(wxIMPL_CAIRO_FUNC
)
415 //----------------------------------------------------------------------------
417 //----------------------------------------------------------------------------
419 class wxCairoModule
: public wxModule
423 virtual bool OnInit();
424 virtual void OnExit();
427 DECLARE_DYNAMIC_CLASS(wxCairoModule
)
430 bool wxCairoModule::OnInit()
435 void wxCairoModule::OnExit()
440 IMPLEMENT_DYNAMIC_CLASS(wxCairoModule
, wxModule
)
442 #endif // wxUSE_CAIRO