1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cairo.cpp
3 // Purpose: Cairo library
4 // Author: Anthony Betaudeau
6 // Copyright: (c) Anthony Bretaudeau
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
20 // keep cairo.h from defining dllimport as we're defining the symbols inside
21 // the wx dll in order to load them dynamically.
25 #include "wx/dynlib.h"
28 #include "wx/msw/wrapwin.h"
32 #include "wx/osx/private.h"
33 #include <cairo-quartz.h>
37 #include "wx/module.h"
41 #define wxCAIRO_METHOD_TYPE(name) \
44 #define wxCAIRO_STATIC_METHOD_DEFINE(rettype, name, args, argnames, defret) \
45 static wxCAIRO_METHOD_TYPE(name) name;
47 #define wxCAIRO_STATIC_VOIDMETHOD_DEFINE(name, args, argnames) \
48 wxCAIRO_STATIC_METHOD_DEFINE(void, name, args, argnames, NULL)
50 #define wxFOR_ALL_CAIRO_VOIDMETHODS(m) \
51 m( cairo_append_path, \
52 (cairo_t *cr, const cairo_path_t *path), (cr, path) ) \
54 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
55 m( cairo_arc_negative, \
56 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
58 (cairo_t *cr), (cr) ) \
59 m( cairo_close_path, \
60 (cairo_t *cr), (cr) ) \
62 (cairo_t *cr, double x1, double y1, double x2, double y2, double x3, double y3), (cr, x1, y1, x2, y2, x3, y3) ) \
64 (cairo_t *cr), (cr) ) \
66 (cairo_t *cr), (cr) ) \
67 m( cairo_fill_preserve, \
68 (cairo_t *cr), (cr) ) \
69 m( cairo_font_extents, \
70 (cairo_t *cr, cairo_font_extents_t *extents), (cr, extents) ) \
71 m( cairo_font_face_destroy, \
72 (cairo_font_face_t *font_face), (font_face) ) \
73 m( cairo_get_current_point, \
74 (cairo_t *cr, double *x, double *y), (cr, x, y) ) \
75 m( cairo_get_matrix, \
76 (cairo_t *cr, cairo_matrix_t *matrix), (cr, matrix) ) \
78 (cairo_t *cr, double x, double y), (cr, x, y) ) \
79 m( cairo_matrix_init, \
80 (cairo_matrix_t *matrix, double xx, double yx, double xy, double yy, double x0, double y0), (matrix, xx, yx, xy, yy, x0, y0) ) \
81 m( cairo_matrix_multiply, \
82 (cairo_matrix_t *result, const cairo_matrix_t *a, const cairo_matrix_t *b), (result, a, b) ) \
83 m( cairo_matrix_rotate, \
84 (cairo_matrix_t *matrix, double radians), (matrix, radians) ) \
85 m( cairo_matrix_scale, \
86 (cairo_matrix_t *matrix, double sx, double sy), (matrix, sx, sy) ) \
87 m( cairo_matrix_transform_distance, \
88 (const cairo_matrix_t *matrix, double *dx, double *dy), (matrix, dx, dy) ) \
89 m( cairo_matrix_transform_point, \
90 (const cairo_matrix_t *matrix, double *x, double *y), (matrix, x, y) ) \
91 m( cairo_matrix_translate, \
92 (cairo_matrix_t *matrix, double tx, double ty), (matrix, tx, ty) ) \
94 (cairo_t *cr, double x, double y), (cr, x, y) ) \
96 (cairo_t *cr), (cr) ) \
98 (cairo_t *cr), (cr) ) \
99 m( cairo_paint_with_alpha, \
100 (cairo_t *cr, double alpha), (cr, alpha) ) \
101 m( cairo_path_destroy, \
102 (cairo_path_t *path), (path) ) \
103 m( cairo_pattern_add_color_stop_rgba, \
104 (cairo_pattern_t *pattern, double offset, double red, double green, double blue, double alpha), (pattern, offset, red, green, blue, alpha) ) \
105 m( cairo_pattern_destroy, \
106 (cairo_pattern_t *pattern), (pattern) ) \
107 m( cairo_pattern_set_extend, \
108 (cairo_pattern_t *pattern, cairo_extend_t extend), (pattern, extend) ) \
109 m( cairo_pattern_set_filter, \
110 (cairo_pattern_t *pattern, cairo_filter_t filter), (pattern, filter) ) \
111 m( cairo_pop_group_to_source, \
112 (cairo_t *cr), (cr) ) \
113 m( cairo_push_group, \
114 (cairo_t *cr), (cr) ) \
115 m( cairo_rectangle, \
116 (cairo_t *cr, double x, double y, double width, double height), (cr, x, y, width, height) ) \
117 m( cairo_reset_clip, \
118 (cairo_t *cr), (cr) ) \
120 (cairo_t *cr), (cr) ) \
122 (cairo_t *cr, double angle), (cr, angle) ) \
124 (cairo_t *cr), (cr) ) \
126 (cairo_t *cr, double sx, double sy), (cr, sx, sy) ) \
127 m( cairo_select_font_face, \
128 (cairo_t *cr, const char *family, cairo_font_slant_t slant, cairo_font_weight_t weight), (cr, family, slant, weight) ) \
129 m( cairo_set_antialias, \
130 (cairo_t *cr, cairo_antialias_t antialias), (cr, antialias) ) \
132 (cairo_t *cr, const double *dashes, int num_dashes, double offset), (cr, dashes, num_dashes, offset) ) \
133 m( cairo_set_fill_rule, \
134 (cairo_t *cr, cairo_fill_rule_t fill_rule), (cr, fill_rule) ) \
135 m( cairo_set_font_face, \
136 (cairo_t *cr, cairo_font_face_t *font_face), (cr, font_face) ) \
137 m( cairo_set_font_size, \
138 (cairo_t *cr, double size), (cr, size) ) \
139 m( cairo_set_line_cap, \
140 (cairo_t *cr, cairo_line_cap_t line_cap), (cr, line_cap) ) \
141 m( cairo_set_line_join, \
142 (cairo_t *cr, cairo_line_join_t line_join), (cr, line_join) ) \
143 m( cairo_set_line_width, \
144 (cairo_t *cr, double width), (cr, width) ) \
145 m( cairo_set_matrix, \
146 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
147 m( cairo_set_operator, \
148 (cairo_t *cr, cairo_operator_t op), (cr, op) ) \
149 m( cairo_set_source, \
150 (cairo_t *cr, cairo_pattern_t *source), (cr, source) ) \
151 m( cairo_set_source_rgba, \
152 (cairo_t *cr, double red, double green, double blue, double alpha), (cr, red, green, blue, alpha) ) \
153 m( cairo_show_text, \
154 (cairo_t *cr, const char *utf8), (cr, utf8) ) \
156 (cairo_t *cr), (cr) ) \
157 m( cairo_stroke_extents, \
158 (cairo_t *cr, double *x1, double *y1, double *x2, double *y2), (cr, x1, y1, x2, y2) ) \
159 m( cairo_stroke_preserve, \
160 (cairo_t *cr), (cr) ) \
161 m( cairo_surface_destroy, \
162 (cairo_surface_t *surface), (surface) ) \
163 m( cairo_text_extents, \
164 (cairo_t *cr, const char *utf8, cairo_text_extents_t *extents), (cr, utf8, extents) ) \
165 m( cairo_transform, \
166 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
167 m( cairo_translate, \
168 (cairo_t *cr, double tx, double ty), (cr, tx, ty) ) \
171 #define wxCAIRO_PLATFORM_METHODS(m) \
172 m( cairo_font_face_t*, cairo_quartz_font_face_create_for_cgfont, \
173 (CGFontRef font), (font), NULL ) \
174 m( cairo_surface_t*, cairo_quartz_surface_create_for_cg_context, \
175 (CGContextRef cgContext, unsigned int width, unsigned int height), (cgContext, width, height), NULL )
176 #elif defined(__WXMSW__)
177 #define wxCAIRO_PLATFORM_METHODS(m) \
178 m( cairo_surface_t*, cairo_win32_surface_create, \
179 (HDC hdc), (hdc), NULL ) \
180 m( cairo_surface_t*, cairo_win32_printing_surface_create, \
181 (HDC hdc), (hdc), NULL )
183 #define wxCAIRO_PLATFORM_METHODS(m)
186 #define wxFOR_ALL_CAIRO_METHODS(m) \
187 m( cairo_path_t*, cairo_copy_path, \
188 (cairo_t *cr), (cr), NULL ) \
189 m( cairo_t*, cairo_create, \
190 (cairo_surface_t *target), (target), NULL) \
191 m( cairo_surface_t*, cairo_get_target, \
192 (cairo_t *cr), (cr), NULL) \
193 m( cairo_surface_t*, cairo_image_surface_create, \
194 (cairo_format_t format, int width, int height), (format, width, height), NULL ) \
195 m( cairo_surface_t*, cairo_image_surface_create_for_data, \
196 (unsigned char *data, cairo_format_t format, int width, int height, int stride), (data, format, width, height, stride), NULL) \
197 m( cairo_bool_t, cairo_in_fill, \
198 (cairo_t *cr, double x, double y), (cr, x, y), false ) \
199 m( cairo_status_t, cairo_matrix_invert, \
200 (cairo_matrix_t *matrix), (matrix), NULL) \
201 m( cairo_pattern_t*, cairo_pattern_create_for_surface, \
202 (cairo_surface_t *surface), (surface), NULL) \
203 m( cairo_pattern_t*, cairo_pattern_create_linear, \
204 (double x0, double y0, double x1, double y1), (x0, y0, x1, y1), NULL) \
205 m( cairo_pattern_t*, cairo_pattern_create_radial, \
206 (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1), (cx0, cy0, radius0, cx1, cy1, radius1), NULL) \
207 m( cairo_status_t, cairo_pattern_status, \
208 (cairo_pattern_t *pattern), (pattern), 4) \
209 m( cairo_t*, cairo_reference, \
210 (cairo_t *cr), (cr), NULL ) \
211 m( cairo_surface_t*, cairo_surface_create_similar, \
212 (cairo_surface_t *other, cairo_content_t content, int width, int height), (other, content, width, height), NULL) \
213 m( int, cairo_format_stride_for_width, \
214 (cairo_format_t format, int width), (format, width), 0) \
215 m( int, cairo_version, \
217 m( int, cairo_image_surface_get_stride, \
218 (cairo_surface_t *surface), (surface), 0) \
219 m( unsigned char *, cairo_image_surface_get_data, \
220 (cairo_surface_t *surface), (surface), NULL) \
221 m( cairo_format_t, cairo_image_surface_get_format, \
222 (cairo_surface_t *surface), (surface), CAIRO_FORMAT_INVALID) \
223 m( cairo_surface_type_t, cairo_surface_get_type, \
224 (cairo_surface_t *surface), (surface), -1) \
225 wxCAIRO_PLATFORM_METHODS(m)
229 #define wxFOR_ALL_PANGO_CAIRO_VOIDMETHODS(m) \
230 m( pango_cairo_update_layout, \
231 (cairo_t *cr, PangoLayout *layout), (cr, layout) ) \
232 m( pango_cairo_show_layout, \
233 (cairo_t *cr, PangoLayout *layout), (cr, layout) )
236 #define wxCAIRO_DECLARE_TYPE(rettype, name, args, argnames, defret) \
237 typedef rettype (*wxCAIRO_METHOD_TYPE(name)) args ; \
238 wxCAIRO_METHOD_TYPE(name) wxDL_METHOD_NAME(name);
240 #define wxCAIRO_DECLARE_VOIDTYPE(name, args, argnames) \
241 wxCAIRO_DECLARE_TYPE(void, name, args, argnames, NULL)
243 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_DECLARE_VOIDTYPE
)
244 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_DECLARE_TYPE
)
250 static bool Initialize();
252 // for internal use only
253 static void CleanUp();
256 // the single wxCairo instance or NULL
257 static wxCairo
*ms_lib
;
264 wxDynamicLibrary m_libCairo
;
265 wxDynamicLibrary m_libPangoCairo
;
267 // true if we successfully loaded the libraries and can use them
269 // note that this field must have this name as it's used by wxDL_XXX macros
274 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_STATIC_VOIDMETHOD_DEFINE
)
275 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_STATIC_METHOD_DEFINE
)
276 #if wxUSE_PANGO // untested, uncomment to test compilation.
277 //wxFOR_ALL_PANGO_METHODS(wxDL_STATIC_METHOD_DEFINE)
280 wxDECLARE_NO_COPY_CLASS(wxCairo
);
283 #define wxINIT_CAIRO_VOIDFUNC(name, params, args) \
284 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
286 #define wxINIT_CAIRO_FUNC(rettype, name, params, args, defret) \
287 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
289 wxFOR_ALL_CAIRO_VOIDMETHODS(wxINIT_CAIRO_VOIDFUNC
)
290 wxFOR_ALL_CAIRO_METHODS(wxINIT_CAIRO_FUNC
)
292 #undef wxINIT_CAIRO_FUNC
294 wxCairo
*wxCairo::ms_lib
= NULL
;
296 //----------------------------------------------------------------------------
298 //----------------------------------------------------------------------------
305 wxString
cairoDllStr("libcairo-2.dll");
307 wxString
cairoDllStr("libcairo.so.2");
309 m_libCairo
.Load(cairoDllStr
);
310 m_ok
= m_libCairo
.IsLoaded();
315 m_libPangoCairo
.Load("libpangocairo-1.0.so.0");
316 m_ok
= m_libPangoCairo
.IsLoaded();
325 #define wxDO_LOAD_FUNC(name, nameStr) \
326 name = (wxCAIRO_METHOD_TYPE(name))m_libCairo.RawGetSymbol(nameStr); \
330 #define wxLOAD_CAIRO_VOIDFUNC(name, params, args) \
331 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
333 #define wxLOAD_CAIRO_FUNC(rettype, name, params, args, defret) \
334 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
336 wxFOR_ALL_CAIRO_VOIDMETHODS(wxLOAD_CAIRO_VOIDFUNC
)
337 wxFOR_ALL_CAIRO_METHODS(wxLOAD_CAIRO_FUNC
)
339 #undef wxLOAD_CAIRO_FUNC
348 /* static */ bool wxCairo::Initialize()
352 ms_lib
= new wxCairo();
353 if ( !ms_lib
->IsOk() )
360 return ms_lib
!= NULL
;
363 /* static */ void wxCairo::CleanUp()
377 // ============================================================================
378 // implementation of the functions themselves
379 // ============================================================================
383 return wxCairo::Initialize();
389 #define wxIMPL_CAIRO_FUNC(rettype, name, params, args, defret) \
390 rettype name params \
392 wxASSERT_MSG(wxCairo::Initialize(), "Cairo not initialized"); \
393 return wxCairo::name args; \
396 #define wxIMPL_CAIRO_VOIDFUNC(name, params, args) \
397 wxIMPL_CAIRO_FUNC(void, name, params, args, NULL)
399 // we currently link directly to Cairo on GTK since it is usually available there,
400 // so don't use our cairo_xyz wrapper functions until the decision is made to
401 // always load Cairo dynamically there.
403 wxFOR_ALL_CAIRO_VOIDMETHODS(wxIMPL_CAIRO_VOIDFUNC
)
404 wxFOR_ALL_CAIRO_METHODS(wxIMPL_CAIRO_FUNC
)
409 //----------------------------------------------------------------------------
411 //----------------------------------------------------------------------------
413 class wxCairoModule
: public wxModule
417 virtual bool OnInit();
418 virtual void OnExit();
421 DECLARE_DYNAMIC_CLASS(wxCairoModule
)
424 bool wxCairoModule::OnInit()
429 void wxCairoModule::OnExit()
434 IMPLEMENT_DYNAMIC_CLASS(wxCairoModule
, wxModule
)
436 #endif // wxUSE_CAIRO