]>
Commit | Line | Data |
---|---|---|
50fae378 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/cairo.cpp | |
3 | // Purpose: Cairo library | |
4 | // Author: Anthony Betaudeau | |
5 | // Created: 2007-08-25 | |
b5b208a1 | 6 | // RCS-ID: $Id$ |
50fae378 RR |
7 | // Copyright: (c) Anthony Bretaudeau |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | // For compilers that support precompilation, includes "wx/wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
932d0768 RD |
19 | // keep cairo.h from defining dllimport as we're defining the symbols inside |
20 | // the wx dll in order to load them dynamically. | |
21 | #define cairo_public | |
22 | ||
50fae378 | 23 | #include "wx/cairo.h" |
932d0768 | 24 | #include "wx/dynlib.h" |
50fae378 RR |
25 | |
26 | #if wxUSE_CAIRO | |
27 | ||
932d0768 RD |
28 | #ifdef __WXMAC__ |
29 | #include "wx/osx/private.h" | |
30 | #include <cairo-quartz.h> | |
31 | #endif | |
32 | ||
50fae378 RR |
33 | #ifndef WX_PRECOMP |
34 | #include "wx/module.h" | |
35 | #include "wx/log.h" | |
36 | #endif | |
37 | ||
932d0768 RD |
38 | #define wxCAIRO_METHOD_TYPE(name) \ |
39 | wxCairo##name##_t | |
40 | ||
41 | #define wxCAIRO_STATIC_METHOD_DEFINE(rettype, name, args, argnames, defret) \ | |
42 | static wxCAIRO_METHOD_TYPE(name) name; | |
43 | ||
44 | #define wxCAIRO_STATIC_VOIDMETHOD_DEFINE(name, args, argnames) \ | |
45 | wxCAIRO_STATIC_METHOD_DEFINE(void, name, args, argnames, NULL) | |
46 | ||
47 | #define wxFOR_ALL_CAIRO_VOIDMETHODS(m) \ | |
48 | m( cairo_append_path, \ | |
49 | (cairo_t *cr, const cairo_path_t *path), (cr, path) ) \ | |
50 | m( cairo_arc, \ | |
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) ) \ | |
54 | m( cairo_clip, \ | |
55 | (cairo_t *cr), (cr) ) \ | |
56 | m( cairo_close_path, \ | |
57 | (cairo_t *cr), (cr) ) \ | |
58 | m( cairo_curve_to, \ | |
59 | (cairo_t *cr, double x1, double y1, double x2, double y2, double x3, double y3), (cr, x1, y1, x2, y2, x3, y3) ) \ | |
60 | m( cairo_destroy, \ | |
61 | (cairo_t *cr), (cr) ) \ | |
62 | m( cairo_fill, \ | |
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) ) \ | |
74 | m( cairo_line_to, \ | |
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) ) \ | |
90 | m( cairo_move_to, \ | |
91 | (cairo_t *cr, double x, double y), (cr, x, y) ) \ | |
92 | m( cairo_new_path, \ | |
93 | (cairo_t *cr), (cr) ) \ | |
94 | m( cairo_paint, \ | |
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) ) \ | |
116 | m( cairo_restore, \ | |
117 | (cairo_t *cr), (cr) ) \ | |
118 | m( cairo_rotate, \ | |
119 | (cairo_t *cr, double angle), (cr, angle) ) \ | |
120 | m( cairo_save, \ | |
121 | (cairo_t *cr), (cr) ) \ | |
122 | m( cairo_scale, \ | |
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) ) \ | |
128 | m( cairo_set_dash, \ | |
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) ) \ | |
152 | m( cairo_stroke, \ | |
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) ) \ | |
166 | ||
167 | #ifdef __WXMAC__ | |
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 ) | |
179 | #else | |
180 | #define wxCAIRO_PLATFORM_METHODS(m) | |
181 | #endif | |
182 | ||
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 | wxCAIRO_PLATFORM_METHODS(m) | |
211 | ||
212 | #if wxUSE_PANGO | |
213 | #define wxFOR_ALL_PANGO_CAIRO_VOIDMETHODS(m) \ | |
214 | m( pango_cairo_update_layout, \ | |
215 | (cairo_t *cr, PangoLayout *layout), (cr, layout) ) \ | |
216 | m( pango_cairo_show_layout, \ | |
217 | (cairo_t *cr, PangoLayout *layout), (cr, layout) ) | |
218 | #endif | |
50fae378 | 219 | |
932d0768 RD |
220 | #define wxCAIRO_DECLARE_TYPE(rettype, name, args, argnames, defret) \ |
221 | typedef rettype (*wxCAIRO_METHOD_TYPE(name)) args ; \ | |
222 | wxCAIRO_METHOD_TYPE(name) wxDL_METHOD_NAME(name); | |
223 | ||
224 | #define wxCAIRO_DECLARE_VOIDTYPE(name, args, argnames) \ | |
225 | wxCAIRO_DECLARE_TYPE(void, name, args, argnames, NULL) | |
226 | ||
227 | wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_DECLARE_VOIDTYPE) | |
228 | wxFOR_ALL_CAIRO_METHODS(wxCAIRO_DECLARE_TYPE) | |
229 | ||
230 | class wxCairo | |
231 | { | |
232 | public: | |
233 | static bool Initialize(); | |
234 | ||
235 | // for internal use only | |
236 | static void CleanUp(); | |
237 | ||
238 | private: | |
239 | // the single wxCairo instance or NULL | |
240 | static wxCairo *ms_lib; | |
241 | ||
242 | wxCairo(); | |
243 | ~wxCairo(); | |
244 | ||
245 | bool IsOk(); | |
246 | ||
247 | wxDynamicLibrary m_libCairo; | |
248 | wxDynamicLibrary m_libPangoCairo; | |
249 | ||
250 | // true if we successfully loaded the libraries and can use them | |
251 | // | |
252 | // note that this field must have this name as it's used by wxDL_XXX macros | |
253 | bool m_ok; | |
254 | ||
255 | public: | |
256 | ||
257 | wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_STATIC_VOIDMETHOD_DEFINE) | |
258 | wxFOR_ALL_CAIRO_METHODS(wxCAIRO_STATIC_METHOD_DEFINE) | |
259 | #if wxUSE_PANGO // untested, uncomment to test compilation. | |
260 | //wxFOR_ALL_PANGO_METHODS(wxDL_STATIC_METHOD_DEFINE) | |
261 | #endif | |
262 | ||
263 | wxDECLARE_NO_COPY_CLASS(wxCairo); | |
264 | }; | |
265 | ||
266 | #define wxINIT_CAIRO_VOIDFUNC(name, params, args) \ | |
267 | wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL; | |
268 | ||
269 | #define wxINIT_CAIRO_FUNC(rettype, name, params, args, defret) \ | |
270 | wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL; | |
271 | ||
272 | wxFOR_ALL_CAIRO_VOIDMETHODS(wxINIT_CAIRO_VOIDFUNC) | |
273 | wxFOR_ALL_CAIRO_METHODS(wxINIT_CAIRO_FUNC) | |
274 | ||
275 | #undef wxINIT_CAIRO_FUNC | |
276 | ||
277 | wxCairo *wxCairo::ms_lib = NULL; | |
50fae378 RR |
278 | |
279 | //---------------------------------------------------------------------------- | |
280 | // wxCairoLibrary | |
281 | //---------------------------------------------------------------------------- | |
282 | ||
932d0768 | 283 | wxCairo::wxCairo() |
50fae378 | 284 | { |
50fae378 | 285 | wxLogNull log; |
03647350 | 286 | |
932d0768 RD |
287 | #ifdef __WXMSW__ |
288 | wxString cairoDllStr("libcairo-2.dll"); | |
289 | #else | |
290 | wxString cairoDllStr("libcairo.so.2"); | |
291 | #endif | |
292 | m_libCairo.Load(cairoDllStr); | |
ced3df77 VZ |
293 | m_ok = m_libCairo.IsLoaded(); |
294 | if ( !m_ok ) | |
295 | return; | |
296 | ||
9f2b6b31 | 297 | #if wxUSE_PANGO |
ced3df77 VZ |
298 | m_libPangoCairo.Load("libpangocairo-1.0.so.0"); |
299 | m_ok = m_libPangoCairo.IsLoaded(); | |
300 | if ( !m_ok ) | |
301 | { | |
302 | m_libCairo.Unload(); | |
303 | return; | |
304 | } | |
9f2b6b31 | 305 | #endif |
50fae378 | 306 | |
932d0768 RD |
307 | |
308 | #define wxDO_LOAD_FUNC(name, nameStr) \ | |
309 | name = (wxCAIRO_METHOD_TYPE(name))m_libCairo.RawGetSymbol(nameStr); \ | |
310 | if ( !name ) \ | |
311 | return; | |
312 | ||
313 | #define wxLOAD_CAIRO_VOIDFUNC(name, params, args) \ | |
314 | wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name)) | |
315 | ||
316 | #define wxLOAD_CAIRO_FUNC(rettype, name, params, args, defret) \ | |
317 | wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name)) | |
318 | ||
319 | wxFOR_ALL_CAIRO_VOIDMETHODS(wxLOAD_CAIRO_VOIDFUNC) | |
320 | wxFOR_ALL_CAIRO_METHODS(wxLOAD_CAIRO_FUNC) | |
321 | ||
322 | #undef wxLOAD_CAIRO_FUNC | |
323 | ||
324 | m_ok = true; | |
50fae378 RR |
325 | } |
326 | ||
932d0768 | 327 | wxCairo::~wxCairo() |
50fae378 | 328 | { |
50fae378 RR |
329 | } |
330 | ||
932d0768 | 331 | /* static */ bool wxCairo::Initialize() |
50fae378 | 332 | { |
ced3df77 VZ |
333 | if ( !ms_lib ) |
334 | { | |
932d0768 | 335 | ms_lib = new wxCairo(); |
ced3df77 VZ |
336 | if ( !ms_lib->IsOk() ) |
337 | { | |
338 | delete ms_lib; | |
339 | ms_lib = NULL; | |
340 | } | |
341 | } | |
03647350 | 342 | |
932d0768 | 343 | return ms_lib != NULL; |
50fae378 RR |
344 | } |
345 | ||
932d0768 | 346 | /* static */ void wxCairo::CleanUp() |
50fae378 | 347 | { |
ced3df77 | 348 | if (ms_lib) |
50fae378 | 349 | { |
ced3df77 VZ |
350 | delete ms_lib; |
351 | ms_lib = NULL; | |
50fae378 RR |
352 | } |
353 | } | |
354 | ||
932d0768 | 355 | bool wxCairo::IsOk() |
50fae378 RR |
356 | { |
357 | return m_ok; | |
358 | } | |
359 | ||
932d0768 RD |
360 | // ============================================================================ |
361 | // implementation of the functions themselves | |
362 | // ============================================================================ | |
363 | ||
364 | extern "C" | |
50fae378 | 365 | { |
ced3df77 | 366 | |
932d0768 RD |
367 | bool wxCairoInit() |
368 | { | |
369 | return wxCairo::Initialize(); | |
370 | } | |
ced3df77 | 371 | |
932d0768 RD |
372 | void wxCairoCleanUp() |
373 | { | |
374 | wxCairo::CleanUp(); | |
50fae378 RR |
375 | } |
376 | ||
932d0768 RD |
377 | #define wxIMPL_CAIRO_FUNC(rettype, name, params, args, defret) \ |
378 | rettype name params \ | |
379 | { \ | |
380 | wxASSERT_MSG(wxCairo::Initialize(), "Cairo not initialized"); \ | |
381 | return wxCairo::name args; \ | |
382 | } | |
383 | ||
384 | #define wxIMPL_CAIRO_VOIDFUNC(name, params, args) \ | |
385 | wxIMPL_CAIRO_FUNC(void, name, params, args, NULL) | |
386 | ||
387 | // we currently link directly to Cairo on GTK since it is usually available there, | |
388 | // so don't use our cairo_xyz wrapper functions until the decision is made to | |
389 | // always load Cairo dynamically there. | |
390 | #ifndef __WXGTK__ | |
391 | wxFOR_ALL_CAIRO_VOIDMETHODS(wxIMPL_CAIRO_VOIDFUNC) | |
392 | wxFOR_ALL_CAIRO_METHODS(wxIMPL_CAIRO_FUNC) | |
393 | #endif | |
394 | ||
395 | } // extern "C" | |
396 | ||
50fae378 RR |
397 | //---------------------------------------------------------------------------- |
398 | // wxCairoModule | |
399 | //---------------------------------------------------------------------------- | |
400 | ||
ced3df77 | 401 | class wxCairoModule : public wxModule |
50fae378 RR |
402 | { |
403 | public: | |
404 | wxCairoModule() { } | |
ced3df77 VZ |
405 | virtual bool OnInit(); |
406 | virtual void OnExit(); | |
50fae378 RR |
407 | |
408 | private: | |
932d0768 | 409 | DECLARE_DYNAMIC_CLASS(wxCairoModule) |
50fae378 RR |
410 | }; |
411 | ||
412 | bool wxCairoModule::OnInit() | |
413 | { | |
414 | return true; | |
415 | } | |
416 | ||
417 | void wxCairoModule::OnExit() | |
418 | { | |
932d0768 | 419 | wxCairo::CleanUp(); |
50fae378 RR |
420 | } |
421 | ||
422 | IMPLEMENT_DYNAMIC_CLASS(wxCairoModule, wxModule) | |
423 | ||
ced3df77 | 424 | #endif // wxUSE_CAIRO |