]> git.saurik.com Git - wxWidgets.git/blame - src/common/cairo.cpp
Update version to 2.9.4 in version.bkl too and rebake everything.
[wxWidgets.git] / src / common / cairo.cpp
CommitLineData
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) \
675f6875
RD
210 m( int, cairo_format_stride_for_width, \
211 (cairo_format_t format, int width), (format, width), 0) \
212 m( int, cairo_version, \
213 (), (), 0) \
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) \
932d0768
RD
222 wxCAIRO_PLATFORM_METHODS(m)
223
675f6875 224
932d0768
RD
225#if wxUSE_PANGO
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) )
231#endif
50fae378 232
932d0768
RD
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);
236
237#define wxCAIRO_DECLARE_VOIDTYPE(name, args, argnames) \
238 wxCAIRO_DECLARE_TYPE(void, name, args, argnames, NULL)
239
240wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_DECLARE_VOIDTYPE)
241wxFOR_ALL_CAIRO_METHODS(wxCAIRO_DECLARE_TYPE)
242
675f6875 243
932d0768
RD
244class wxCairo
245{
246public:
247 static bool Initialize();
248
249 // for internal use only
250 static void CleanUp();
251
252private:
253 // the single wxCairo instance or NULL
254 static wxCairo *ms_lib;
255
256 wxCairo();
257 ~wxCairo();
258
259 bool IsOk();
260
261 wxDynamicLibrary m_libCairo;
262 wxDynamicLibrary m_libPangoCairo;
263
264 // true if we successfully loaded the libraries and can use them
265 //
266 // note that this field must have this name as it's used by wxDL_XXX macros
267 bool m_ok;
268
269public:
270
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)
275#endif
276
277 wxDECLARE_NO_COPY_CLASS(wxCairo);
278};
279
280#define wxINIT_CAIRO_VOIDFUNC(name, params, args) \
281 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
282
283#define wxINIT_CAIRO_FUNC(rettype, name, params, args, defret) \
284 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
285
286wxFOR_ALL_CAIRO_VOIDMETHODS(wxINIT_CAIRO_VOIDFUNC)
287wxFOR_ALL_CAIRO_METHODS(wxINIT_CAIRO_FUNC)
288
289#undef wxINIT_CAIRO_FUNC
290
291wxCairo *wxCairo::ms_lib = NULL;
50fae378
RR
292
293//----------------------------------------------------------------------------
294// wxCairoLibrary
295//----------------------------------------------------------------------------
296
932d0768 297wxCairo::wxCairo()
50fae378 298{
50fae378 299 wxLogNull log;
03647350 300
932d0768
RD
301#ifdef __WXMSW__
302 wxString cairoDllStr("libcairo-2.dll");
303#else
304 wxString cairoDllStr("libcairo.so.2");
305#endif
306 m_libCairo.Load(cairoDllStr);
ced3df77
VZ
307 m_ok = m_libCairo.IsLoaded();
308 if ( !m_ok )
309 return;
310
9f2b6b31 311#if wxUSE_PANGO
ced3df77
VZ
312 m_libPangoCairo.Load("libpangocairo-1.0.so.0");
313 m_ok = m_libPangoCairo.IsLoaded();
314 if ( !m_ok )
315 {
316 m_libCairo.Unload();
317 return;
318 }
9f2b6b31 319#endif
50fae378 320
932d0768
RD
321
322#define wxDO_LOAD_FUNC(name, nameStr) \
323 name = (wxCAIRO_METHOD_TYPE(name))m_libCairo.RawGetSymbol(nameStr); \
324 if ( !name ) \
325 return;
326
327#define wxLOAD_CAIRO_VOIDFUNC(name, params, args) \
328 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
329
330#define wxLOAD_CAIRO_FUNC(rettype, name, params, args, defret) \
331 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
332
333wxFOR_ALL_CAIRO_VOIDMETHODS(wxLOAD_CAIRO_VOIDFUNC)
334wxFOR_ALL_CAIRO_METHODS(wxLOAD_CAIRO_FUNC)
335
336#undef wxLOAD_CAIRO_FUNC
337
338 m_ok = true;
50fae378
RR
339}
340
932d0768 341wxCairo::~wxCairo()
50fae378 342{
50fae378
RR
343}
344
932d0768 345/* static */ bool wxCairo::Initialize()
50fae378 346{
ced3df77
VZ
347 if ( !ms_lib )
348 {
932d0768 349 ms_lib = new wxCairo();
ced3df77
VZ
350 if ( !ms_lib->IsOk() )
351 {
352 delete ms_lib;
353 ms_lib = NULL;
354 }
355 }
03647350 356
932d0768 357 return ms_lib != NULL;
50fae378
RR
358}
359
932d0768 360/* static */ void wxCairo::CleanUp()
50fae378 361{
ced3df77 362 if (ms_lib)
50fae378 363 {
ced3df77
VZ
364 delete ms_lib;
365 ms_lib = NULL;
50fae378
RR
366 }
367}
368
932d0768 369bool wxCairo::IsOk()
50fae378
RR
370{
371 return m_ok;
372}
373
932d0768
RD
374// ============================================================================
375// implementation of the functions themselves
376// ============================================================================
377
378extern "C"
50fae378 379{
ced3df77 380
932d0768
RD
381bool wxCairoInit()
382{
383 return wxCairo::Initialize();
384}
ced3df77 385
932d0768
RD
386void wxCairoCleanUp()
387{
388 wxCairo::CleanUp();
50fae378
RR
389}
390
932d0768
RD
391#define wxIMPL_CAIRO_FUNC(rettype, name, params, args, defret) \
392 rettype name params \
393 { \
394 wxASSERT_MSG(wxCairo::Initialize(), "Cairo not initialized"); \
395 return wxCairo::name args; \
396 }
397
398#define wxIMPL_CAIRO_VOIDFUNC(name, params, args) \
399 wxIMPL_CAIRO_FUNC(void, name, params, args, NULL)
400
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.
404#ifndef __WXGTK__
405wxFOR_ALL_CAIRO_VOIDMETHODS(wxIMPL_CAIRO_VOIDFUNC)
406wxFOR_ALL_CAIRO_METHODS(wxIMPL_CAIRO_FUNC)
407#endif
408
409} // extern "C"
410
50fae378
RR
411//----------------------------------------------------------------------------
412// wxCairoModule
413//----------------------------------------------------------------------------
414
ced3df77 415class wxCairoModule : public wxModule
50fae378
RR
416{
417public:
418 wxCairoModule() { }
ced3df77
VZ
419 virtual bool OnInit();
420 virtual void OnExit();
50fae378
RR
421
422private:
932d0768 423 DECLARE_DYNAMIC_CLASS(wxCairoModule)
50fae378
RR
424};
425
426bool wxCairoModule::OnInit()
427{
428 return true;
429}
430
431void wxCairoModule::OnExit()
432{
932d0768 433 wxCairo::CleanUp();
50fae378
RR
434}
435
436IMPLEMENT_DYNAMIC_CLASS(wxCairoModule, wxModule)
437
ced3df77 438#endif // wxUSE_CAIRO