]> git.saurik.com Git - wxWidgets.git/blob - src/common/cairo.cpp
using common GetContentScaleFactor throughout part 1
[wxWidgets.git] / src / common / cairo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/cairo.cpp
3 // Purpose: Cairo library
4 // Author: Anthony Betaudeau
5 // Created: 2007-08-25
6 // RCS-ID: $Id$
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
19 #if wxUSE_CAIRO
20
21 // keep cairo.h from defining dllimport as we're defining the symbols inside
22 // the wx dll in order to load them dynamically.
23 #define cairo_public
24
25 #include <cairo.h>
26 #include "wx/dynlib.h"
27
28 #ifdef __WXMSW__
29 #include "wx/msw/wrapwin.h"
30 #endif
31
32 #ifdef __WXMAC__
33 #include "wx/osx/private.h"
34 #include <cairo-quartz.h>
35 #endif
36
37 #ifndef WX_PRECOMP
38 #include "wx/module.h"
39 #include "wx/log.h"
40 #endif
41
42 #define wxCAIRO_METHOD_TYPE(name) \
43 wxCairo##name##_t
44
45 #define wxCAIRO_STATIC_METHOD_DEFINE(rettype, name, args, argnames, defret) \
46 static wxCAIRO_METHOD_TYPE(name) name;
47
48 #define wxCAIRO_STATIC_VOIDMETHOD_DEFINE(name, args, argnames) \
49 wxCAIRO_STATIC_METHOD_DEFINE(void, name, args, argnames, NULL)
50
51 #define wxFOR_ALL_CAIRO_VOIDMETHODS(m) \
52 m( cairo_append_path, \
53 (cairo_t *cr, const cairo_path_t *path), (cr, path) ) \
54 m( cairo_arc, \
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) ) \
58 m( cairo_clip, \
59 (cairo_t *cr), (cr) ) \
60 m( cairo_close_path, \
61 (cairo_t *cr), (cr) ) \
62 m( cairo_curve_to, \
63 (cairo_t *cr, double x1, double y1, double x2, double y2, double x3, double y3), (cr, x1, y1, x2, y2, x3, y3) ) \
64 m( cairo_destroy, \
65 (cairo_t *cr), (cr) ) \
66 m( cairo_fill, \
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) ) \
78 m( cairo_line_to, \
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) ) \
94 m( cairo_move_to, \
95 (cairo_t *cr, double x, double y), (cr, x, y) ) \
96 m( cairo_new_path, \
97 (cairo_t *cr), (cr) ) \
98 m( cairo_paint, \
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) ) \
120 m( cairo_restore, \
121 (cairo_t *cr), (cr) ) \
122 m( cairo_rotate, \
123 (cairo_t *cr, double angle), (cr, angle) ) \
124 m( cairo_save, \
125 (cairo_t *cr), (cr) ) \
126 m( cairo_scale, \
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) ) \
132 m( cairo_set_dash, \
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) ) \
156 m( cairo_stroke, \
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) ) \
170
171 #ifdef __WXMAC__
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 )
183 #else
184 #define wxCAIRO_PLATFORM_METHODS(m)
185 #endif
186
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, \
217 (), (), 0) \
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)
227
228
229 #if wxUSE_PANGO
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) )
235 #endif
236
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);
240
241 #define wxCAIRO_DECLARE_VOIDTYPE(name, args, argnames) \
242 wxCAIRO_DECLARE_TYPE(void, name, args, argnames, NULL)
243
244 wxFOR_ALL_CAIRO_VOIDMETHODS(wxCAIRO_DECLARE_VOIDTYPE)
245 wxFOR_ALL_CAIRO_METHODS(wxCAIRO_DECLARE_TYPE)
246
247
248 class wxCairo
249 {
250 public:
251 static bool Initialize();
252
253 // for internal use only
254 static void CleanUp();
255
256 private:
257 // the single wxCairo instance or NULL
258 static wxCairo *ms_lib;
259
260 wxCairo();
261 ~wxCairo();
262
263 bool IsOk();
264
265 wxDynamicLibrary m_libCairo;
266 wxDynamicLibrary m_libPangoCairo;
267
268 // true if we successfully loaded the libraries and can use them
269 //
270 // note that this field must have this name as it's used by wxDL_XXX macros
271 bool m_ok;
272
273 public:
274
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)
279 #endif
280
281 wxDECLARE_NO_COPY_CLASS(wxCairo);
282 };
283
284 #define wxINIT_CAIRO_VOIDFUNC(name, params, args) \
285 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
286
287 #define wxINIT_CAIRO_FUNC(rettype, name, params, args, defret) \
288 wxCAIRO_METHOD_TYPE(name) wxCairo::name = NULL;
289
290 wxFOR_ALL_CAIRO_VOIDMETHODS(wxINIT_CAIRO_VOIDFUNC)
291 wxFOR_ALL_CAIRO_METHODS(wxINIT_CAIRO_FUNC)
292
293 #undef wxINIT_CAIRO_FUNC
294
295 wxCairo *wxCairo::ms_lib = NULL;
296
297 //----------------------------------------------------------------------------
298 // wxCairoLibrary
299 //----------------------------------------------------------------------------
300
301 wxCairo::wxCairo()
302 {
303 wxLogNull log;
304
305 #ifdef __WXMSW__
306 wxString cairoDllStr("libcairo-2.dll");
307 #else
308 wxString cairoDllStr("libcairo.so.2");
309 #endif
310 m_libCairo.Load(cairoDllStr);
311 m_ok = m_libCairo.IsLoaded();
312 if ( !m_ok )
313 return;
314
315 #if wxUSE_PANGO
316 m_libPangoCairo.Load("libpangocairo-1.0.so.0");
317 m_ok = m_libPangoCairo.IsLoaded();
318 if ( !m_ok )
319 {
320 m_libCairo.Unload();
321 return;
322 }
323 #endif
324
325
326 #define wxDO_LOAD_FUNC(name, nameStr) \
327 name = (wxCAIRO_METHOD_TYPE(name))m_libCairo.RawGetSymbol(nameStr); \
328 if ( !name ) \
329 return;
330
331 #define wxLOAD_CAIRO_VOIDFUNC(name, params, args) \
332 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
333
334 #define wxLOAD_CAIRO_FUNC(rettype, name, params, args, defret) \
335 wxDO_LOAD_FUNC(name, wxSTRINGIZE_T(name))
336
337 wxFOR_ALL_CAIRO_VOIDMETHODS(wxLOAD_CAIRO_VOIDFUNC)
338 wxFOR_ALL_CAIRO_METHODS(wxLOAD_CAIRO_FUNC)
339
340 #undef wxLOAD_CAIRO_FUNC
341
342 m_ok = true;
343 }
344
345 wxCairo::~wxCairo()
346 {
347 }
348
349 /* static */ bool wxCairo::Initialize()
350 {
351 if ( !ms_lib )
352 {
353 ms_lib = new wxCairo();
354 if ( !ms_lib->IsOk() )
355 {
356 delete ms_lib;
357 ms_lib = NULL;
358 }
359 }
360
361 return ms_lib != NULL;
362 }
363
364 /* static */ void wxCairo::CleanUp()
365 {
366 if (ms_lib)
367 {
368 delete ms_lib;
369 ms_lib = NULL;
370 }
371 }
372
373 bool wxCairo::IsOk()
374 {
375 return m_ok;
376 }
377
378 // ============================================================================
379 // implementation of the functions themselves
380 // ============================================================================
381
382 bool wxCairoInit()
383 {
384 return wxCairo::Initialize();
385 }
386
387 extern "C"
388 {
389
390 #define wxIMPL_CAIRO_FUNC(rettype, name, params, args, defret) \
391 rettype name params \
392 { \
393 wxASSERT_MSG(wxCairo::Initialize(), "Cairo not initialized"); \
394 return wxCairo::name args; \
395 }
396
397 #define wxIMPL_CAIRO_VOIDFUNC(name, params, args) \
398 wxIMPL_CAIRO_FUNC(void, name, params, args, NULL)
399
400 // we currently link directly to Cairo on GTK since it is usually available there,
401 // so don't use our cairo_xyz wrapper functions until the decision is made to
402 // always load Cairo dynamically there.
403 #ifndef __WXGTK__
404 wxFOR_ALL_CAIRO_VOIDMETHODS(wxIMPL_CAIRO_VOIDFUNC)
405 wxFOR_ALL_CAIRO_METHODS(wxIMPL_CAIRO_FUNC)
406 #endif
407
408 } // extern "C"
409
410 //----------------------------------------------------------------------------
411 // wxCairoModule
412 //----------------------------------------------------------------------------
413
414 class wxCairoModule : public wxModule
415 {
416 public:
417 wxCairoModule() { }
418 virtual bool OnInit();
419 virtual void OnExit();
420
421 private:
422 DECLARE_DYNAMIC_CLASS(wxCairoModule)
423 };
424
425 bool wxCairoModule::OnInit()
426 {
427 return true;
428 }
429
430 void wxCairoModule::OnExit()
431 {
432 wxCairo::CleanUp();
433 }
434
435 IMPLEMENT_DYNAMIC_CLASS(wxCairoModule, wxModule)
436
437 #endif // wxUSE_CAIRO