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/osx/private.h"
30 #include <cairo-quartz.h>
34 #include "wx/module.h"
38 #define wxCAIRO_METHOD_TYPE(name) \
41 #define wxCAIRO_STATIC_METHOD_DEFINE(rettype, name, args, argnames, defret) \
42 static wxCAIRO_METHOD_TYPE(name) name;
44 #define wxCAIRO_STATIC_VOIDMETHOD_DEFINE(name, args, argnames) \
45 wxCAIRO_STATIC_METHOD_DEFINE(void, name, args, argnames, NULL)
47 #define wxFOR_ALL_CAIRO_VOIDMETHODS(m) \
48 m( cairo_append_path, \
49 (cairo_t *cr, const cairo_path_t *path), (cr, path) ) \
51 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
52 m( cairo_arc_negative, \
53 (cairo_t *cr, double xc, double yc, double radius, double angle1, double angle2), (cr, xc, yc, radius, angle1, angle2) ) \
55 (cairo_t *cr), (cr) ) \
56 m( cairo_close_path, \
57 (cairo_t *cr), (cr) ) \
59 (cairo_t *cr, double x1, double y1, double x2, double y2, double x3, double y3), (cr, x1, y1, x2, y2, x3, y3) ) \
61 (cairo_t *cr), (cr) ) \
63 (cairo_t *cr), (cr) ) \
64 m( cairo_fill_preserve, \
65 (cairo_t *cr), (cr) ) \
66 m( cairo_font_extents, \
67 (cairo_t *cr, cairo_font_extents_t *extents), (cr, extents) ) \
68 m( cairo_font_face_destroy, \
69 (cairo_font_face_t *font_face), (font_face) ) \
70 m( cairo_get_current_point, \
71 (cairo_t *cr, double *x, double *y), (cr, x, y) ) \
72 m( cairo_get_matrix, \
73 (cairo_t *cr, cairo_matrix_t *matrix), (cr, matrix) ) \
75 (cairo_t *cr, double x, double y), (cr, x, y) ) \
76 m( cairo_matrix_init, \
77 (cairo_matrix_t *matrix, double xx, double yx, double xy, double yy, double x0, double y0), (matrix, xx, yx, xy, yy, x0, y0) ) \
78 m( cairo_matrix_multiply, \
79 (cairo_matrix_t *result, const cairo_matrix_t *a, const cairo_matrix_t *b), (result, a, b) ) \
80 m( cairo_matrix_rotate, \
81 (cairo_matrix_t *matrix, double radians), (matrix, radians) ) \
82 m( cairo_matrix_scale, \
83 (cairo_matrix_t *matrix, double sx, double sy), (matrix, sx, sy) ) \
84 m( cairo_matrix_transform_distance, \
85 (const cairo_matrix_t *matrix, double *dx, double *dy), (matrix, dx, dy) ) \
86 m( cairo_matrix_transform_point, \
87 (const cairo_matrix_t *matrix, double *x, double *y), (matrix, x, y) ) \
88 m( cairo_matrix_translate, \
89 (cairo_matrix_t *matrix, double tx, double ty), (matrix, tx, ty) ) \
91 (cairo_t *cr, double x, double y), (cr, x, y) ) \
93 (cairo_t *cr), (cr) ) \
95 (cairo_t *cr), (cr) ) \
96 m( cairo_paint_with_alpha, \
97 (cairo_t *cr, double alpha), (cr, alpha) ) \
98 m( cairo_path_destroy, \
99 (cairo_path_t *path), (path) ) \
100 m( cairo_pattern_add_color_stop_rgba, \
101 (cairo_pattern_t *pattern, double offset, double red, double green, double blue, double alpha), (pattern, offset, red, green, blue, alpha) ) \
102 m( cairo_pattern_destroy, \
103 (cairo_pattern_t *pattern), (pattern) ) \
104 m( cairo_pattern_set_extend, \
105 (cairo_pattern_t *pattern, cairo_extend_t extend), (pattern, extend) ) \
106 m( cairo_pattern_set_filter, \
107 (cairo_pattern_t *pattern, cairo_filter_t filter), (pattern, filter) ) \
108 m( cairo_pop_group_to_source, \
109 (cairo_t *cr), (cr) ) \
110 m( cairo_push_group, \
111 (cairo_t *cr), (cr) ) \
112 m( cairo_rectangle, \
113 (cairo_t *cr, double x, double y, double width, double height), (cr, x, y, width, height) ) \
114 m( cairo_reset_clip, \
115 (cairo_t *cr), (cr) ) \
117 (cairo_t *cr), (cr) ) \
119 (cairo_t *cr, double angle), (cr, angle) ) \
121 (cairo_t *cr), (cr) ) \
123 (cairo_t *cr, double sx, double sy), (cr, sx, sy) ) \
124 m( cairo_select_font_face, \
125 (cairo_t *cr, const char *family, cairo_font_slant_t slant, cairo_font_weight_t weight), (cr, family, slant, weight) ) \
126 m( cairo_set_antialias, \
127 (cairo_t *cr, cairo_antialias_t antialias), (cr, antialias) ) \
129 (cairo_t *cr, const double *dashes, int num_dashes, double offset), (cr, dashes, num_dashes, offset) ) \
130 m( cairo_set_fill_rule, \
131 (cairo_t *cr, cairo_fill_rule_t fill_rule), (cr, fill_rule) ) \
132 m( cairo_set_font_face, \
133 (cairo_t *cr, cairo_font_face_t *font_face), (cr, font_face) ) \
134 m( cairo_set_font_size, \
135 (cairo_t *cr, double size), (cr, size) ) \
136 m( cairo_set_line_cap, \
137 (cairo_t *cr, cairo_line_cap_t line_cap), (cr, line_cap) ) \
138 m( cairo_set_line_join, \
139 (cairo_t *cr, cairo_line_join_t line_join), (cr, line_join) ) \
140 m( cairo_set_line_width, \
141 (cairo_t *cr, double width), (cr, width) ) \
142 m( cairo_set_matrix, \
143 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
144 m( cairo_set_operator, \
145 (cairo_t *cr, cairo_operator_t op), (cr, op) ) \
146 m( cairo_set_source, \
147 (cairo_t *cr, cairo_pattern_t *source), (cr, source) ) \
148 m( cairo_set_source_rgba, \
149 (cairo_t *cr, double red, double green, double blue, double alpha), (cr, red, green, blue, alpha) ) \
150 m( cairo_show_text, \
151 (cairo_t *cr, const char *utf8), (cr, utf8) ) \
153 (cairo_t *cr), (cr) ) \
154 m( cairo_stroke_extents, \
155 (cairo_t *cr, double *x1, double *y1, double *x2, double *y2), (cr, x1, y1, x2, y2) ) \
156 m( cairo_stroke_preserve, \
157 (cairo_t *cr), (cr) ) \
158 m( cairo_surface_destroy, \
159 (cairo_surface_t *surface), (surface) ) \
160 m( cairo_text_extents, \
161 (cairo_t *cr, const char *utf8, cairo_text_extents_t *extents), (cr, utf8, extents) ) \
162 m( cairo_transform, \
163 (cairo_t *cr, const cairo_matrix_t *matrix), (cr, matrix) ) \
164 m( cairo_translate, \
165 (cairo_t *cr, double tx, double ty), (cr, tx, ty) ) \
168 #define wxCAIRO_PLATFORM_METHODS(m) \
169 m( cairo_font_face_t*, cairo_quartz_font_face_create_for_cgfont, \
170 (CGFontRef font), (font), NULL ) \
171 m( cairo_surface_t*, cairo_quartz_surface_create_for_cg_context, \
172 (CGContextRef cgContext, unsigned int width, unsigned int height), (cgContext, width, height), NULL )
173 #elif defined(__WXMSW__)
174 #define wxCAIRO_PLATFORM_METHODS(m) \
175 m( cairo_surface_t*, cairo_win32_surface_create, \
176 (HDC hdc), (hdc), NULL ) \
177 m( cairo_surface_t*, cairo_win32_printing_surface_create, \
178 (HDC hdc), (hdc), NULL )
180 #define wxCAIRO_PLATFORM_METHODS(m)
183 #define wxFOR_ALL_CAIRO_METHODS(m) \
184 m( cairo_path_t*, cairo_copy_path, \
185 (cairo_t *cr), (cr), NULL ) \
186 m( cairo_t*, cairo_create, \
187 (cairo_surface_t *target), (target), NULL) \
188 m( cairo_surface_t*, cairo_get_target, \
189 (cairo_t *cr), (cr), NULL) \
190 m( cairo_surface_t*, cairo_image_surface_create, \
191 (cairo_format_t format, int width, int height), (format, width, height), NULL ) \
192 m( cairo_surface_t*, cairo_image_surface_create_for_data, \
193 (unsigned char *data, cairo_format_t format, int width, int height, int stride), (data, format, width, height, stride), NULL) \
194 m( cairo_bool_t, cairo_in_fill, \
195 (cairo_t *cr, double x, double y), (cr, x, y), false ) \
196 m( cairo_status_t, cairo_matrix_invert, \
197 (cairo_matrix_t *matrix), (matrix), NULL) \
198 m( cairo_pattern_t*, cairo_pattern_create_for_surface, \
199 (cairo_surface_t *surface), (surface), NULL) \
200 m( cairo_pattern_t*, cairo_pattern_create_linear, \
201 (double x0, double y0, double x1, double y1), (x0, y0, x1, y1), NULL) \
202 m( cairo_pattern_t*, cairo_pattern_create_radial, \
203 (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1), (cx0, cy0, radius0, cx1, cy1, radius1), NULL) \
204 m( cairo_status_t, cairo_pattern_status, \
205 (cairo_pattern_t *pattern), (pattern), 4) \
206 m( cairo_t*, cairo_reference, \
207 (cairo_t *cr), (cr), NULL ) \
208 m( cairo_surface_t*, cairo_surface_create_similar, \
209 (cairo_surface_t *other, cairo_content_t content, int width, int height), (other, content, width, height), NULL) \
210 m( int, cairo_format_stride_for_width, \
211 (cairo_format_t format, int width), (format, width), 0) \
212 m( int, cairo_version, \
214 m( int, cairo_image_surface_get_stride, \
215 (cairo_surface_t *surface), (surface), 0) \
216 m( unsigned char *, cairo_image_surface_get_data, \
217 (cairo_surface_t *surface), (surface), NULL) \
218 m( cairo_format_t, cairo_image_surface_get_format, \
219 (cairo_surface_t *surface), (surface), CAIRO_FORMAT_INVALID) \
220 m( cairo_surface_type_t, cairo_surface_get_type, \
221 (cairo_surface_t *surface), (surface), -1) \
222 wxCAIRO_PLATFORM_METHODS(m)
226 #define wxFOR_ALL_PANGO_CAIRO_VOIDMETHODS(m) \
227 m( pango_cairo_update_layout, \
228 (cairo_t *cr, PangoLayout *layout), (cr, layout) ) \
229 m( pango_cairo_show_layout, \
230 (cairo_t *cr, PangoLayout *layout), (cr, layout) )
233 #define wxCAIRO_DECLARE_TYPE(rettype, name, args, argnames, defret) \
234 typedef rettype (*wxCAIRO_METHOD_TYPE(name)) args ; \
235 wxCAIRO_METHOD_TYPE(name) wxDL_METHOD_NAME(name);
237 #define wxCAIRO_DECLARE_VOIDTYPE(name, args, argnames) \
238 wxCAIRO_DECLARE_TYPE(void, name, args, argnames, NULL)
240 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_DECLARE_VOIDTYPE
)
241 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_DECLARE_TYPE
)
247 static bool Initialize();
249 // for internal use only
250 static void CleanUp();
253 // the single wxCairo instance or NULL
254 static wxCairo
*ms_lib
;
261 wxDynamicLibrary m_libCairo
;
262 wxDynamicLibrary m_libPangoCairo
;
264 // true if we successfully loaded the libraries and can use them
266 // note that this field must have this name as it's used by wxDL_XXX macros
271 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_STATIC_VOIDMETHOD_DEFINE
)
272 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_STATIC_METHOD_DEFINE
)
273 #if wxUSE_PANGO // untested, uncomment to test compilation.
274 //wxFOR_ALL_PANGO_METHODS(wxDL_STATIC_METHOD_DEFINE)
277 wxDECLARE_NO_COPY_CLASS(wxCairo
);
280 #define wxINIT_CAIRO_VOIDFUNC(name, params, args) \
281 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
283 #define wxINIT_CAIRO_FUNC(rettype, name, params, args, defret) \
284 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
286 wxFOR_ALL_CAIRO_VOIDMETHODS(wxINIT_CAIRO_VOIDFUNC
)
287 wxFOR_ALL_CAIRO_METHODS(wxINIT_CAIRO_FUNC
)
289 #undef wxINIT_CAIRO_FUNC
291 wxCairo
*wxCairo::ms_lib
= NULL
;
293 //----------------------------------------------------------------------------
295 //----------------------------------------------------------------------------
302 wxString
cairoDllStr("libcairo-2.dll");
304 wxString
cairoDllStr("libcairo.so.2");
306 m_libCairo
.Load(cairoDllStr
);
307 m_ok
= m_libCairo
.IsLoaded();
312 m_libPangoCairo
.Load("libpangocairo-1.0.so.0");
313 m_ok
= m_libPangoCairo
.IsLoaded();
322 #define wxDO_LOAD_FUNC(name, nameStr) \
323 name = (wxCAIRO_METHOD_TYPE(name))m_libCairo.RawGetSymbol(nameStr); \
327 #define wxLOAD_CAIRO_VOIDFUNC(name, params, args) \
328 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
330 #define wxLOAD_CAIRO_FUNC(rettype, name, params, args, defret) \
331 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
333 wxFOR_ALL_CAIRO_VOIDMETHODS(wxLOAD_CAIRO_VOIDFUNC
)
334 wxFOR_ALL_CAIRO_METHODS(wxLOAD_CAIRO_FUNC
)
336 #undef wxLOAD_CAIRO_FUNC
345 /* static */ bool wxCairo::Initialize()
349 ms_lib
= new wxCairo();
350 if ( !ms_lib
->IsOk() )
357 return ms_lib
!= NULL
;
360 /* static */ void wxCairo::CleanUp()
374 // ============================================================================
375 // implementation of the functions themselves
376 // ============================================================================
383 return wxCairo::Initialize();
386 void wxCairoCleanUp()
391 #define wxIMPL_CAIRO_FUNC(rettype, name, params, args, defret) \
392 rettype name params \
394 wxASSERT_MSG(wxCairo::Initialize(), "Cairo not initialized"); \
395 return wxCairo::name args; \
398 #define wxIMPL_CAIRO_VOIDFUNC(name, params, args) \
399 wxIMPL_CAIRO_FUNC(void, name, params, args, NULL)
401 // we currently link directly to Cairo on GTK since it is usually available there,
402 // so don't use our cairo_xyz wrapper functions until the decision is made to
403 // always load Cairo dynamically there.
405 wxFOR_ALL_CAIRO_VOIDMETHODS(wxIMPL_CAIRO_VOIDFUNC
)
406 wxFOR_ALL_CAIRO_METHODS(wxIMPL_CAIRO_FUNC
)
411 //----------------------------------------------------------------------------
413 //----------------------------------------------------------------------------
415 class wxCairoModule
: public wxModule
419 virtual bool OnInit();
420 virtual void OnExit();
423 DECLARE_DYNAMIC_CLASS(wxCairoModule
)
426 bool wxCairoModule::OnInit()
431 void wxCairoModule::OnExit()
436 IMPLEMENT_DYNAMIC_CLASS(wxCairoModule
, wxModule
)
438 #endif // wxUSE_CAIRO