]> git.saurik.com Git - wxWidgets.git/blame - src/generic/graphicc.cpp
Refactor wxGDIPlusFontData ctor to allow using it without wxGDIPlusContext.
[wxWidgets.git] / src / generic / graphicc.cpp
CommitLineData
184fc6c8
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/graphicc.cpp
3// Purpose: cairo device context class
4// Author: Stefan Csomor
5// Modified by:
e0876d73 6// Created: 2006-10-03
184fc6c8 7// RCS-ID: $Id$
e0876d73 8// Copyright: (c) 2006 Stefan Csomor
184fc6c8
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
184fc6c8 14#ifdef __BORLANDC__
7f0d0111 15 #pragma hdrstop
184fc6c8
SC
16#endif
17
9a67941f 18#if wxUSE_GRAPHICS_CONTEXT
c0e69d72 19
6c99dbd5 20#include "wx/graphics.h"
9a67941f
VZ
21
22#if wxUSE_CAIRO
23
932d0768
RD
24// keep cairo.h from defining dllimport as we're defining the symbols inside
25// the wx dll in order to load them dynamically.
26#define cairo_public
27
6c99dbd5
VZ
28#include "wx/cairo.h"
29
184fc6c8 30#ifndef WX_PRECOMP
7f0d0111
VZ
31 #include "wx/bitmap.h"
32 #include "wx/icon.h"
7f0d0111
VZ
33 #include "wx/dcclient.h"
34 #include "wx/dcmemory.h"
35 #include "wx/dcprint.h"
081d8d96
PC
36 #ifdef __WXGTK__
37 #include "wx/window.h"
38 #endif
184fc6c8
SC
39#endif
40
2f94ab40 41#include "wx/private/graphics.h"
d9485f89 42#include "wx/rawbmp.h"
50de831a 43#include "wx/vector.h"
184fc6c8 44
184fc6c8
SC
45using namespace std;
46
184fc6c8
SC
47//-----------------------------------------------------------------------------
48// device context implementation
49//
50// more and more of the dc functionality should be implemented by calling
51// the appropricate wxCairoContext, but we will have to do that step by step
52// also coordinate conversions should be moved to native matrix ops
53//-----------------------------------------------------------------------------
54
55// we always stock two context states, one at entry, to be able to preserve the
56// state we were called with, the other one after changing to HI Graphics orientation
57// (this one is used for getting back clippings etc)
58
59//-----------------------------------------------------------------------------
60// wxGraphicsPath implementation
61//-----------------------------------------------------------------------------
62
63// TODO remove this dependency (gdiplus needs the macros)
64
65#ifndef max
66#define max(a,b) (((a) > (b)) ? (a) : (b))
67#endif
68
69#ifndef min
70#define min(a,b) (((a) < (b)) ? (a) : (b))
71#endif
72
73#include <cairo.h>
c0e69d72
KO
74#ifdef __WXMSW__
75#include <cairo-win32.h>
76#endif
77
00bd8e72 78#ifdef __WXGTK__
184fc6c8 79#include <gtk/gtk.h>
84f67b11 80#include "wx/fontutil.h"
888dde65 81#include "wx/gtk/dc.h"
00bd8e72 82#endif
184fc6c8 83
e7f9f819
SC
84#ifdef __WXMSW__
85#include <cairo-win32.h>
86#endif
87
88#ifdef __WXMAC__
33ddeaba 89#include "wx/osx/private.h"
e7f9f819
SC
90#include <cairo-quartz.h>
91#include <cairo-atsui.h>
92#endif
93
0db8a70e 94class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData
184fc6c8 95{
184fc6c8 96public :
0db8a70e
RD
97 wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL);
98 ~wxCairoPathData();
184fc6c8 99
0db8a70e 100 virtual wxGraphicsObjectRefData *Clone() const;
184fc6c8
SC
101
102 //
103 // These are the path primitives from which everything else can be constructed
104 //
105
106 // begins a new subpath at (x,y)
107 virtual void MoveToPoint( wxDouble x, wxDouble y );
108
109 // adds a straight line from the current point to (x,y)
110 virtual void AddLineToPoint( wxDouble x, wxDouble y );
111
112 // adds a cubic Bezier curve from the current point, using two control points and an end point
113 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
114
115
116 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
117 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
118
119 // gets the last point of the current path, (0,0) if not yet set
0db8a70e 120 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
184fc6c8 121
00bd8e72 122 // adds another path
0db8a70e 123 virtual void AddPath( const wxGraphicsPathData* path );
00bd8e72 124
184fc6c8
SC
125 // closes the current sub-path
126 virtual void CloseSubpath();
127
128 //
129 // These are convenience functions which - if not available natively will be assembled
130 // using the primitives from above
131 //
132
133 /*
134
49ad157e 135 // appends a rectangle as a new closed subpath
184fc6c8
SC
136 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
137 // appends an ellipsis as a new closed subpath fitting the passed rectangle
138 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
139
140 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
141 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
142 */
143
cd5adaa6
RD
144 // returns the native path
145 virtual void * GetNativePath() const ;
146
147 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
0db8a70e 148 virtual void UnGetNativePath(void *p) const;
f540e5bd 149
00bd8e72 150 // transforms each point of this path by the matrix
0db8a70e 151 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
00bd8e72
SC
152
153 // gets the bounding box enclosing all points (possibly including control points)
0db8a70e 154 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
00bd8e72 155
94a007ec 156 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxWINDING_RULE) const;
00bd8e72 157
184fc6c8
SC
158private :
159 cairo_t* m_pathContext;
160};
161
0db8a70e 162class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData
184fc6c8 163{
00bd8e72 164public :
0db8a70e
RD
165 wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
166 virtual ~wxCairoMatrixData() ;
184fc6c8 167
0db8a70e 168 virtual wxGraphicsObjectRefData *Clone() const ;
184fc6c8 169
00bd8e72 170 // concatenates the matrix
0db8a70e 171 virtual void Concat( const wxGraphicsMatrixData *t );
184fc6c8 172
00bd8e72 173 // sets the matrix to the respective values
49ad157e 174 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
00bd8e72 175 wxDouble tx=0.0, wxDouble ty=0.0);
184fc6c8 176
248802d0
RD
177 // gets the component valuess of the matrix
178 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
179 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
49ad157e 180
00bd8e72
SC
181 // makes this the inverse matrix
182 virtual void Invert();
184fc6c8 183
00bd8e72 184 // returns true if the elements of the transformation matrix are equal ?
0db8a70e 185 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
184fc6c8 186
00bd8e72 187 // return true if this is the identity matrix
0db8a70e 188 virtual bool IsIdentity() const;
184fc6c8 189
00bd8e72
SC
190 //
191 // transformation
192 //
184fc6c8 193
00bd8e72
SC
194 // add the translation to this matrix
195 virtual void Translate( wxDouble dx , wxDouble dy );
196
197 // add the scale to this matrix
198 virtual void Scale( wxDouble xScale , wxDouble yScale );
199
200 // add the rotation to this matrix (radians)
49ad157e 201 virtual void Rotate( wxDouble angle );
00bd8e72
SC
202
203 //
204 // apply the transforms
205 //
206
207 // applies that matrix to the point
0db8a70e 208 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
00bd8e72
SC
209
210 // applies the matrix except for translations
0db8a70e 211 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
00bd8e72
SC
212
213 // returns the native representation
214 virtual void * GetNativeMatrix() const;
215private:
216 cairo_matrix_t m_matrix ;
00bd8e72
SC
217} ;
218
87752530 219class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
184fc6c8 220{
00bd8e72 221public:
87752530
SC
222 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
223 ~wxCairoPenData();
00bd8e72
SC
224
225 void Init();
226
227 virtual void Apply( wxGraphicsContext* context );
228 virtual wxDouble GetWidth() { return m_width; }
229
230private :
231 double m_width;
49ad157e 232
00bd8e72
SC
233 double m_red;
234 double m_green;
235 double m_blue;
236 double m_alpha;
49ad157e 237
00bd8e72
SC
238 cairo_line_cap_t m_cap;
239 cairo_line_join_t m_join;
240
241 int m_count;
242 const double *m_lengths;
243 double *m_userLengths;
184fc6c8 244
00bd8e72 245 wxPen m_pen;
50de831a
PC
246
247 wxDECLARE_NO_COPY_CLASS(wxCairoPenData);
00bd8e72
SC
248};
249
87752530 250class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
184fc6c8 251{
00bd8e72 252public:
87752530
SC
253 wxCairoBrushData( wxGraphicsRenderer* renderer );
254 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
255 ~wxCairoBrushData ();
00bd8e72
SC
256
257 virtual void Apply( wxGraphicsContext* context );
4ee4c7b9
VZ
258
259 void CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
260 wxDouble x2, wxDouble y2,
261 const wxGraphicsGradientStops& stops);
262 void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
263 wxDouble xc, wxDouble yc, wxDouble radius,
264 const wxGraphicsGradientStops& stops);
00bd8e72
SC
265
266protected:
267 virtual void Init();
268
4ee4c7b9
VZ
269 // common part of Create{Linear,Radial}GradientBrush()
270 void AddGradientStops(const wxGraphicsGradientStops& stops);
271
00bd8e72
SC
272private :
273 double m_red;
274 double m_green;
275 double m_blue;
276 double m_alpha;
277
278 cairo_pattern_t* m_brushPattern;
00bd8e72
SC
279};
280
87752530 281class wxCairoFontData : public wxGraphicsObjectRefData
184fc6c8 282{
00bd8e72 283public:
87752530
SC
284 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
285 ~wxCairoFontData();
00bd8e72
SC
286
287 virtual void Apply( wxGraphicsContext* context );
84f67b11
SC
288#ifdef __WXGTK__
289 const PangoFontDescription* GetFont() const { return m_font; }
0d19936d 290 bool GetUnderlined() const { return m_underlined; }
84f67b11 291#endif
00bd8e72 292private :
00bd8e72 293 double m_size;
0d19936d 294 bool m_underlined;
00bd8e72
SC
295 double m_red;
296 double m_green;
297 double m_blue;
298 double m_alpha;
84f67b11 299#ifdef __WXMAC__
e7f9f819 300 cairo_font_face_t *m_font;
84f67b11
SC
301#elif defined(__WXGTK__)
302 PangoFontDescription* m_font;
303#else
304 wxCharBuffer m_fontName;
305 cairo_font_slant_t m_slant;
306 cairo_font_weight_t m_weight;
307#endif
00bd8e72 308};
184fc6c8 309
2fc9c1ea
KO
310class wxCairoBitmapData : public wxGraphicsObjectRefData
311{
312public:
313 wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp );
2986eb86 314 wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap );
2fc9c1ea
KO
315 ~wxCairoBitmapData();
316
317 virtual cairo_surface_t* GetCairoSurface() { return m_surface; }
318 virtual cairo_pattern_t* GetCairoPattern() { return m_pattern; }
319 virtual wxSize GetSize() { return wxSize(m_width, m_height); }
08b2d55f
VZ
320
321#if wxUSE_IMAGE
322 wxImage ConvertToImage() const;
323#endif // wxUSE_IMAGE
324
2fc9c1ea 325private :
5f606e65
VZ
326 // Allocate m_buffer for the bitmap of the given size in the given format.
327 //
328 // Returns the stride used for the buffer.
329 int InitBuffer(int width, int height, cairo_format_t format);
330
331 // Really create the surface using the buffer (which was supposed to be
332 // filled since InitBuffer() call).
333 void InitSurface(cairo_format_t format, int stride);
334
335
2fc9c1ea
KO
336 cairo_surface_t* m_surface;
337 cairo_pattern_t* m_pattern;
338 int m_width;
339 int m_height;
340 unsigned char* m_buffer;
341};
342
00bd8e72 343class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
184fc6c8 344{
184fc6c8 345public:
00bd8e72 346 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
888dde65 347 wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc );
c9008abe 348 wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc );
00bd8e72
SC
349#ifdef __WXGTK__
350 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
9f2b6b31
RD
351#endif
352#ifdef __WXMSW__
353 wxCairoContext( wxGraphicsRenderer* renderer, HDC context );
00bd8e72
SC
354#endif
355 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
356 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
79313208
VZ
357
358 // If this ctor is used, Init() must be called by the derived class later.
359 wxCairoContext( wxGraphicsRenderer* renderer );
360
184fc6c8
SC
361 virtual ~wxCairoContext();
362
e7f9f819 363 virtual bool ShouldOffset() const
49ad157e 364 {
136a1de9
SC
365 if ( !m_enableOffset )
366 return false;
367
e7f9f819
SC
368 int penwidth = 0 ;
369 if ( !m_pen.IsNull() )
370 {
371 penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth();
372 if ( penwidth == 0 )
373 penwidth = 1;
374 }
375 return ( penwidth % 2 ) == 1;
376 }
377
184fc6c8 378 virtual void Clip( const wxRegion &region );
c0e69d72
KO
379#ifdef __WXMSW__
380 cairo_surface_t* m_mswSurface;
381#endif
539e2795
SC
382
383 // clips drawings to the rect
384 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
539e2795 385
cd5adaa6
RD
386 // resets the clipping to original extent
387 virtual void ResetClip();
388
389 virtual void * GetNativeContext();
390
bf02a7f9 391 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
49ad157e 392
133cb28b
SC
393 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation);
394
bf02a7f9
SC
395 virtual bool SetCompositionMode(wxCompositionMode op);
396
397 virtual void BeginLayer(wxDouble opacity);
398
399 virtual void EndLayer();
03647350 400
0db8a70e 401 virtual void StrokePath( const wxGraphicsPath& p );
94a007ec 402 virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxWINDING_RULE );
184fc6c8 403
184fc6c8
SC
404 virtual void Translate( wxDouble dx , wxDouble dy );
405 virtual void Scale( wxDouble xScale , wxDouble yScale );
406 virtual void Rotate( wxDouble angle );
407
00bd8e72 408 // concatenates this transform with the current transform of this context
0db8a70e 409 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
00bd8e72
SC
410
411 // sets the transform of this context
0db8a70e 412 virtual void SetTransform( const wxGraphicsMatrix& matrix );
00bd8e72
SC
413
414 // gets the matrix of this context
0db8a70e 415 virtual wxGraphicsMatrix GetTransform() const;
00bd8e72 416
e77cba1a 417 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
184fc6c8
SC
418 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
419 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
420 virtual void PushState();
421 virtual void PopState();
422
184fc6c8
SC
423 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
424 wxDouble *descent, wxDouble *externalLeading ) const;
425 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
426
50de831a
PC
427protected:
428 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
429
184fc6c8 430private:
e7f9f819 431 void Init(cairo_t *context);
49ad157e 432
184fc6c8 433 cairo_t* m_context;
03647350 434
bf02a7f9 435 wxVector<float> m_layerOpacities;
0b7dce54 436
c0c133e1 437 wxDECLARE_NO_COPY_CLASS(wxCairoContext);
184fc6c8
SC
438};
439
184fc6c8 440//-----------------------------------------------------------------------------
87752530 441// wxCairoPenData implementation
184fc6c8
SC
442//-----------------------------------------------------------------------------
443
87752530 444wxCairoPenData::~wxCairoPenData()
539e2795 445{
00bd8e72 446 delete[] m_userLengths;
539e2795 447}
cd5adaa6 448
87752530 449void wxCairoPenData::Init()
539e2795 450{
00bd8e72
SC
451 m_lengths = NULL;
452 m_userLengths = NULL;
453 m_width = 0;
454 m_count = 0;
539e2795
SC
455}
456
87752530
SC
457wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
458: wxGraphicsObjectRefData(renderer)
49ad157e 459{
00bd8e72 460 Init();
7f8bd9fc
RD
461 m_pen = pen;
462 m_width = m_pen.GetWidth();
00bd8e72
SC
463 if (m_width <= 0.0)
464 m_width = 0.1;
539e2795 465
00bd8e72 466 m_red = m_pen.GetColour().Red()/255.0;
49ad157e 467 m_green = m_pen.GetColour().Green()/255.0;
00bd8e72
SC
468 m_blue = m_pen.GetColour().Blue()/255.0;
469 m_alpha = m_pen.GetColour().Alpha()/255.0;
184fc6c8 470
184fc6c8
SC
471 switch ( m_pen.GetCap() )
472 {
473 case wxCAP_ROUND :
00bd8e72 474 m_cap = CAIRO_LINE_CAP_ROUND;
184fc6c8
SC
475 break;
476
477 case wxCAP_PROJECTING :
00bd8e72 478 m_cap = CAIRO_LINE_CAP_SQUARE;
184fc6c8
SC
479 break;
480
481 case wxCAP_BUTT :
00bd8e72 482 m_cap = CAIRO_LINE_CAP_BUTT;
184fc6c8
SC
483 break;
484
485 default :
00bd8e72 486 m_cap = CAIRO_LINE_CAP_BUTT;
184fc6c8
SC
487 break;
488 }
184fc6c8 489
184fc6c8
SC
490 switch ( m_pen.GetJoin() )
491 {
492 case wxJOIN_BEVEL :
00bd8e72 493 m_join = CAIRO_LINE_JOIN_BEVEL;
184fc6c8
SC
494 break;
495
496 case wxJOIN_MITER :
00bd8e72 497 m_join = CAIRO_LINE_JOIN_MITER;
184fc6c8
SC
498 break;
499
500 case wxJOIN_ROUND :
00bd8e72 501 m_join = CAIRO_LINE_JOIN_ROUND;
184fc6c8
SC
502 break;
503
504 default :
00bd8e72 505 m_join = CAIRO_LINE_JOIN_MITER;
184fc6c8
SC
506 break;
507 }
184fc6c8 508
00bd8e72 509 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
184fc6c8 510 const double dotted[] =
00bd8e72
SC
511 {
512 dashUnit , dashUnit + 2.0
513 };
514 static const double short_dashed[] =
515 {
516 9.0 , 6.0
517 };
518 static const double dashed[] =
519 {
520 19.0 , 9.0
521 };
522 static const double dotted_dashed[] =
523 {
524 9.0 , 6.0 , 3.0 , 3.0
525 };
184fc6c8
SC
526
527 switch ( m_pen.GetStyle() )
528 {
fb040094 529 case wxPENSTYLE_SOLID :
184fc6c8
SC
530 break;
531
fb040094 532 case wxPENSTYLE_DOT :
00bd8e72
SC
533 m_count = WXSIZEOF(dotted);
534 m_userLengths = new double[ m_count ] ;
535 memcpy( m_userLengths, dotted, sizeof(dotted) );
536 m_lengths = m_userLengths;
184fc6c8
SC
537 break;
538
fb040094 539 case wxPENSTYLE_LONG_DASH :
84f67b11 540 m_lengths = dashed ;
00bd8e72 541 m_count = WXSIZEOF(dashed);
184fc6c8
SC
542 break;
543
fb040094 544 case wxPENSTYLE_SHORT_DASH :
84f67b11 545 m_lengths = short_dashed ;
00bd8e72 546 m_count = WXSIZEOF(short_dashed);
184fc6c8
SC
547 break;
548
fb040094 549 case wxPENSTYLE_DOT_DASH :
84f67b11 550 m_lengths = dotted_dashed ;
00bd8e72 551 m_count = WXSIZEOF(dotted_dashed);
184fc6c8
SC
552 break;
553
fb040094 554 case wxPENSTYLE_USER_DASH :
184fc6c8
SC
555 {
556 wxDash *wxdashes ;
00bd8e72
SC
557 m_count = m_pen.GetDashes( &wxdashes ) ;
558 if ((wxdashes != NULL) && (m_count > 0))
184fc6c8 559 {
00bd8e72
SC
560 m_userLengths = new double[m_count] ;
561 for ( int i = 0 ; i < m_count ; ++i )
184fc6c8 562 {
00bd8e72 563 m_userLengths[i] = wxdashes[i] * dashUnit ;
184fc6c8 564
00bd8e72
SC
565 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
566 m_userLengths[i] = dashUnit + 2.0 ;
567 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
568 m_userLengths[i] = dashUnit ;
184fc6c8
SC
569 }
570 }
00bd8e72 571 m_lengths = m_userLengths ;
184fc6c8
SC
572 }
573 break;
fb040094 574 case wxPENSTYLE_STIPPLE :
184fc6c8
SC
575 {
576 /*
00bd8e72 577 wxBitmap* bmp = pen.GetStipple();
a1b806b9 578 if ( bmp && bmp->IsOk() )
00bd8e72
SC
579 {
580 wxDELETE( m_penImage );
581 wxDELETE( m_penBrush );
582 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
583 m_penBrush = new TextureBrush(m_penImage);
584 m_pen->SetBrush( m_penBrush );
585 }
184fc6c8
SC
586 */
587 }
588 break;
589 default :
89efaf2b 590 if ( m_pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH
fb040094 591 && m_pen.GetStyle() <= wxPENSTYLE_LAST_HATCH )
184fc6c8
SC
592 {
593 /*
00bd8e72
SC
594 wxDELETE( m_penBrush );
595 HatchStyle style = HatchStyleHorizontal;
596 switch( pen.GetStyle() )
597 {
fb040094 598 case wxPENSTYLE_BDIAGONAL_HATCH :
00bd8e72
SC
599 style = HatchStyleBackwardDiagonal;
600 break ;
fb040094 601 case wxPENSTYLE_CROSSDIAG_HATCH :
00bd8e72
SC
602 style = HatchStyleDiagonalCross;
603 break ;
fb040094 604 case wxPENSTYLE_FDIAGONAL_HATCH :
00bd8e72
SC
605 style = HatchStyleForwardDiagonal;
606 break ;
fb040094 607 case wxPENSTYLE_CROSS_HATCH :
00bd8e72
SC
608 style = HatchStyleCross;
609 break ;
fb040094 610 case wxPENSTYLE_HORIZONTAL_HATCH :
00bd8e72
SC
611 style = HatchStyleHorizontal;
612 break ;
fb040094 613 case wxPENSTYLE_VERTICAL_HATCH :
00bd8e72
SC
614 style = HatchStyleVertical;
615 break ;
616
617 }
618 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
619 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
620 m_pen->SetBrush( m_penBrush )
184fc6c8
SC
621 */
622 }
623 break;
624 }
00bd8e72 625}
184fc6c8 626
87752530 627void wxCairoPenData::Apply( wxGraphicsContext* context )
00bd8e72
SC
628{
629 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
630 cairo_set_line_width(ctext,m_width);
631 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
632 cairo_set_line_cap(ctext,m_cap);
633 cairo_set_line_join(ctext,m_join);
634 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
184fc6c8
SC
635}
636
00bd8e72 637//-----------------------------------------------------------------------------
87752530 638// wxCairoBrushData implementation
00bd8e72
SC
639//-----------------------------------------------------------------------------
640
7f8bd9fc
RD
641wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
642 : wxGraphicsObjectRefData( renderer )
00bd8e72
SC
643{
644 Init();
645}
184fc6c8 646
87752530 647wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
7f8bd9fc 648 : wxGraphicsObjectRefData(renderer)
00bd8e72 649{
7f8bd9fc 650 Init();
49ad157e 651
00bd8e72 652 m_red = brush.GetColour().Red()/255.0;
49ad157e 653 m_green = brush.GetColour().Green()/255.0;
00bd8e72
SC
654 m_blue = brush.GetColour().Blue()/255.0;
655 m_alpha = brush.GetColour().Alpha()/255.0;
656 /*
fb040094 657 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
00bd8e72
SC
658 {
659 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
660 brush.GetColour().Green() , brush.GetColour().Blue() ) );
661 }
662 else if ( brush.IsHatch() )
663 {
664 HatchStyle style = HatchStyleHorizontal;
665 switch( brush.GetStyle() )
666 {
fb040094 667 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
00bd8e72
SC
668 style = HatchStyleBackwardDiagonal;
669 break ;
fb040094 670 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
00bd8e72
SC
671 style = HatchStyleDiagonalCross;
672 break ;
fb040094 673 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
00bd8e72
SC
674 style = HatchStyleForwardDiagonal;
675 break ;
fb040094 676 case wxBRUSHSTYLE_CROSS_HATCH :
00bd8e72
SC
677 style = HatchStyleCross;
678 break ;
fb040094 679 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
00bd8e72
SC
680 style = HatchStyleHorizontal;
681 break ;
fb040094 682 case wxBRUSHSTYLE_VERTICAL_HATCH :
00bd8e72
SC
683 style = HatchStyleVertical;
684 break ;
685
686 }
687 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
688 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
689 }
49ad157e 690 else
00bd8e72
SC
691 {
692 wxBitmap* bmp = brush.GetStipple();
a1b806b9 693 if ( bmp && bmp->IsOk() )
00bd8e72
SC
694 {
695 wxDELETE( m_brushImage );
696 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
697 m_brush = new TextureBrush(m_brushImage);
698 }
184fc6c8 699 }
00bd8e72 700 */
184fc6c8
SC
701}
702
87752530 703wxCairoBrushData::~wxCairoBrushData ()
184fc6c8 704{
00bd8e72
SC
705 if (m_brushPattern)
706 cairo_pattern_destroy(m_brushPattern);
184fc6c8
SC
707}
708
87752530 709void wxCairoBrushData::Apply( wxGraphicsContext* context )
184fc6c8 710{
00bd8e72
SC
711 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
712 if ( m_brushPattern )
713 {
714 cairo_set_source(ctext,m_brushPattern);
715 }
716 else
717 {
718 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
719 }
184fc6c8
SC
720}
721
4ee4c7b9
VZ
722void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops& stops)
723{
724 // loop over all the stops, they include the beginning and ending ones
725 const unsigned numStops = stops.GetCount();
726 for ( unsigned n = 0; n < numStops; n++ )
727 {
728 const wxGraphicsGradientStop stop = stops.Item(n);
729
730 const wxColour col = stop.GetColour();
731
732 cairo_pattern_add_color_stop_rgba
733 (
734 m_brushPattern,
735 stop.GetPosition(),
736 col.Red()/255.0,
737 col.Green()/255.0,
738 col.Blue()/255.0,
739 col.Alpha()/255.0
740 );
741 }
742
743 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS,
744 wxT("Couldn't create cairo pattern"));
745}
746
747void
748wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
749 wxDouble x2, wxDouble y2,
750 const wxGraphicsGradientStops& stops)
184fc6c8 751{
00bd8e72 752 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
4ee4c7b9
VZ
753
754 AddGradientStops(stops);
184fc6c8
SC
755}
756
4ee4c7b9
VZ
757void
758wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
759 wxDouble xc, wxDouble yc,
760 wxDouble radius,
761 const wxGraphicsGradientStops& stops)
184fc6c8 762{
00bd8e72 763 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
4ee4c7b9
VZ
764
765 AddGradientStops(stops);
184fc6c8
SC
766}
767
87752530 768void wxCairoBrushData::Init()
184fc6c8 769{
00bd8e72 770 m_brushPattern = NULL;
184fc6c8
SC
771}
772
00bd8e72 773//-----------------------------------------------------------------------------
87752530 774// wxCairoFontData implementation
00bd8e72
SC
775//-----------------------------------------------------------------------------
776
49ad157e 777wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
87752530 778 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
184fc6c8 779{
00bd8e72 780 m_red = col.Red()/255.0;
49ad157e 781 m_green = col.Green()/255.0;
00bd8e72
SC
782 m_blue = col.Blue()/255.0;
783 m_alpha = col.Alpha()/255.0;
00bd8e72 784 m_size = font.GetPointSize();
0d19936d 785 m_underlined = font.GetUnderlined();
84f67b11
SC
786
787#ifdef __WXMAC__
aa6208d9 788 m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() );
84f67b11
SC
789#elif defined(__WXGTK__)
790 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
791#else
00bd8e72
SC
792 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
793 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
794 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
e7f9f819 795#endif
184fc6c8
SC
796}
797
87752530 798wxCairoFontData::~wxCairoFontData()
184fc6c8 799{
84f67b11 800#ifdef __WXMAC__
e7f9f819 801 cairo_font_face_destroy( m_font );
84f67b11
SC
802#elif defined(__WXGTK__)
803 pango_font_description_free( m_font );
804#else
805#endif
00bd8e72 806}
184fc6c8 807
87752530 808void wxCairoFontData::Apply( wxGraphicsContext* context )
00bd8e72
SC
809{
810 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
811 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
84f67b11
SC
812#ifdef __WXGTK__
813 // the rest is done using Pango layouts
814#elif defined(__WXMAC__)
815 cairo_set_font_face(ctext, m_font);
3e700936 816 cairo_set_font_size(ctext, m_size );
84f67b11 817#else
711a4812 818 cairo_select_font_face(ctext, m_fontName, m_slant, m_weight );
84f67b11 819 cairo_set_font_size(ctext, m_size );
e7f9f819 820#endif
00bd8e72
SC
821}
822
823//-----------------------------------------------------------------------------
0db8a70e 824// wxCairoPathData implementation
00bd8e72
SC
825//-----------------------------------------------------------------------------
826
0db8a70e
RD
827wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
828 : wxGraphicsPathData(renderer)
184fc6c8 829{
00bd8e72 830 if (pathcontext)
184fc6c8 831 {
00bd8e72
SC
832 m_pathContext = pathcontext;
833 }
834 else
835 {
836 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
837 m_pathContext = cairo_create(surface);
838 cairo_surface_destroy (surface);
184fc6c8 839 }
00bd8e72 840}
184fc6c8 841
0db8a70e 842wxCairoPathData::~wxCairoPathData()
00bd8e72
SC
843{
844 cairo_destroy(m_pathContext);
184fc6c8
SC
845}
846
0db8a70e 847wxGraphicsObjectRefData *wxCairoPathData::Clone() const
184fc6c8 848{
00bd8e72
SC
849 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
850 cairo_t* pathcontext = cairo_create(surface);
851 cairo_surface_destroy (surface);
852
853 cairo_path_t* path = cairo_copy_path(m_pathContext);
854 cairo_append_path(pathcontext, path);
855 cairo_path_destroy(path);
0db8a70e 856 return new wxCairoPathData( GetRenderer() ,pathcontext);
00bd8e72
SC
857}
858
859
0db8a70e 860void* wxCairoPathData::GetNativePath() const
00bd8e72
SC
861{
862 return cairo_copy_path(m_pathContext) ;
863}
864
0db8a70e 865void wxCairoPathData::UnGetNativePath(void *p) const
00bd8e72
SC
866{
867 cairo_path_destroy((cairo_path_t*)p);
868}
869
870//
871// The Primitives
872//
873
0db8a70e 874void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
00bd8e72
SC
875{
876 cairo_move_to(m_pathContext,x,y);
877}
878
0db8a70e 879void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
00bd8e72
SC
880{
881 cairo_line_to(m_pathContext,x,y);
882}
883
0db8a70e 884void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
6b06903d 885{
d9485f89
RD
886 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
887 cairo_append_path(m_pathContext, p);
888 UnGetNativePath(p);
6b06903d
RD
889}
890
0db8a70e 891void wxCairoPathData::CloseSubpath()
00bd8e72
SC
892{
893 cairo_close_path(m_pathContext);
894}
895
0db8a70e 896void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
00bd8e72
SC
897{
898 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
899}
900
901// gets the last point of the current path, (0,0) if not yet set
0db8a70e 902void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
00bd8e72
SC
903{
904 double dx,dy;
905 cairo_get_current_point(m_pathContext,&dx,&dy);
0db8a70e
RD
906 if (x)
907 *x = dx;
908 if (y)
909 *y = dy;
00bd8e72
SC
910}
911
0db8a70e 912void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
00bd8e72 913{
49ad157e 914 // as clockwise means positive in our system (y pointing downwards)
00bd8e72
SC
915 // TODO make this interpretation dependent of the
916 // real device trans
917 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
918 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
919 else
920 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
921}
922
923// transforms each point of this path by the matrix
49ad157e 924void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
00bd8e72
SC
925{
926 // as we don't have a true path object, we have to apply the inverse
927 // matrix to the context
928 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
929 cairo_matrix_invert( &m );
930 cairo_transform(m_pathContext,&m);
49ad157e 931}
00bd8e72
SC
932
933// gets the bounding box enclosing all points (possibly including control points)
0db8a70e 934void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
00bd8e72
SC
935{
936 double x1,y1,x2,y2;
937
938 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
939 if ( x2 < x1 )
184fc6c8 940 {
00bd8e72
SC
941 *x = x2;
942 *w = x1-x2;
943 }
944 else
945 {
946 *x = x1;
947 *w = x2-x1;
184fc6c8 948 }
49ad157e 949
00bd8e72
SC
950 if( y2 < y1 )
951 {
952 *y = y2;
953 *h = y1-y2;
954 }
955 else
956 {
957 *y = y1;
958 *h = y2-y1;
959 }
960}
961
f2b905d7 962bool wxCairoPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
00bd8e72 963{
f2b905d7
SC
964 cairo_set_fill_rule(m_pathContext,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
965 return cairo_in_fill( m_pathContext, x, y) != 0;
00bd8e72
SC
966}
967
968//-----------------------------------------------------------------------------
0db8a70e 969// wxCairoMatrixData implementation
00bd8e72
SC
970//-----------------------------------------------------------------------------
971
0db8a70e
RD
972wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
973 : wxGraphicsMatrixData(renderer)
00bd8e72
SC
974{
975 if ( matrix )
976 m_matrix = *matrix;
977}
978
49ad157e 979wxCairoMatrixData::~wxCairoMatrixData()
00bd8e72
SC
980{
981 // nothing to do
982}
983
49ad157e 984wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
00bd8e72 985{
0db8a70e 986 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
00bd8e72
SC
987}
988
989// concatenates the matrix
49ad157e 990void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
00bd8e72 991{
49ad157e 992 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
00bd8e72
SC
993}
994
00bd8e72 995// sets the matrix to the respective values
49ad157e
VZ
996void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
997 wxDouble tx, wxDouble ty)
00bd8e72
SC
998{
999 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
1000}
1001
248802d0
RD
1002// gets the component valuess of the matrix
1003void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1004 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1005{
1006 if (a) *a = m_matrix.xx;
1007 if (b) *b = m_matrix.yx;
1008 if (c) *c = m_matrix.xy;
1009 if (d) *d = m_matrix.yy;
1010 if (tx) *tx= m_matrix.x0;
1011 if (ty) *ty= m_matrix.y0;
1012}
1013
00bd8e72 1014// makes this the inverse matrix
49ad157e 1015void wxCairoMatrixData::Invert()
00bd8e72
SC
1016{
1017 cairo_matrix_invert( &m_matrix );
1018}
1019
1020// returns true if the elements of the transformation matrix are equal ?
49ad157e 1021bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
00bd8e72
SC
1022{
1023 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
49ad157e
VZ
1024 return (
1025 m_matrix.xx == tm->xx &&
1026 m_matrix.yx == tm->yx &&
1027 m_matrix.xy == tm->xy &&
1028 m_matrix.yy == tm->yy &&
1029 m_matrix.x0 == tm->x0 &&
00bd8e72
SC
1030 m_matrix.y0 == tm->y0 ) ;
1031}
1032
1033// return true if this is the identity matrix
0db8a70e 1034bool wxCairoMatrixData::IsIdentity() const
00bd8e72
SC
1035{
1036 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
1037 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
1038}
1039
1040//
1041// transformation
1042//
1043
1044// add the translation to this matrix
0db8a70e 1045void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
00bd8e72
SC
1046{
1047 cairo_matrix_translate( &m_matrix, dx, dy) ;
1048}
1049
1050// add the scale to this matrix
0db8a70e 1051void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
00bd8e72
SC
1052{
1053 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
1054}
1055
1056// add the rotation to this matrix (radians)
49ad157e 1057void wxCairoMatrixData::Rotate( wxDouble angle )
00bd8e72
SC
1058{
1059 cairo_matrix_rotate( &m_matrix, angle) ;
49ad157e 1060}
00bd8e72
SC
1061
1062//
1063// apply the transforms
1064//
1065
1066// applies that matrix to the point
0db8a70e 1067void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
00bd8e72
SC
1068{
1069 double lx = *x, ly = *y ;
1070 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
1071 *x = lx;
1072 *y = ly;
1073}
1074
1075// applies the matrix except for translations
0db8a70e 1076void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
00bd8e72
SC
1077{
1078 double lx = *dx, ly = *dy ;
1079 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1080 *dx = lx;
1081 *dy = ly;
1082}
1083
1084// returns the native representation
0db8a70e 1085void * wxCairoMatrixData::GetNativeMatrix() const
00bd8e72
SC
1086{
1087 return (void*) &m_matrix;
1088}
1089
5f606e65 1090// ----------------------------------------------------------------------------
2fc9c1ea 1091// wxCairoBitmap implementation
5f606e65
VZ
1092// ----------------------------------------------------------------------------
1093
1094int wxCairoBitmapData::InitBuffer(int width, int height, cairo_format_t format)
1095{
1096 wxUnusedVar(format); // Only really unused with Cairo < 1.6.
1097
1098 // Determine the stride: use cairo_format_stride_for_width() if available
1099 // but fall back to 4*width for the earlier versions as this is what that
1100 // function always returns, even in latest Cairo, anyhow.
1101 int stride;
1102#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1103 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1104 {
1105 stride = cairo_format_stride_for_width(format, width);
1106
1107 // All our code would totally break if stride were not a multiple of 4
1108 // so ensure this is the case.
1109 if ( stride % 4 )
1110 {
1111 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1112
1113 stride += 4 - stride % 4;
1114 }
1115 }
1116 else
1117#endif
1118 stride = 4*width;
1119
1120 m_width = width;
1121 m_height = height;
1122 m_buffer = new unsigned char[height*stride];
1123
1124 return stride;
1125}
1126
1127void wxCairoBitmapData::InitSurface(cairo_format_t format, int stride)
1128{
1129 m_surface = cairo_image_surface_create_for_data(
1130 m_buffer, format, m_width, m_height, stride);
1131 m_pattern = cairo_pattern_create_for_surface(m_surface);
1132}
2fc9c1ea 1133
2986eb86
SC
1134wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ) :
1135 wxGraphicsObjectRefData( renderer )
1136{
1137 m_surface = bitmap;
1138 m_pattern = cairo_pattern_create_for_surface(m_surface);
1139}
1140
2fc9c1ea
KO
1141wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer )
1142{
1143 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1144
711a4812 1145#ifdef wxHAS_RAW_BITMAP
2fc9c1ea
KO
1146 // Create a surface object and copy the bitmap pixel data to it. if the
1147 // image has alpha (or a mask represented as alpha) then we'll use a
1148 // different format and iterator than if it doesn't...
5f606e65 1149 const cairo_format_t bufferFormat = bmp.GetDepth() == 32
75ded4d1 1150#ifdef __WXGTK__
5f606e65 1151 || bmp.GetMask()
932d0768 1152#endif
5f606e65
VZ
1153 ? CAIRO_FORMAT_ARGB32
1154 : CAIRO_FORMAT_RGB24;
1155
1156 int stride = InitBuffer(bmp.GetWidth(), bmp.GetHeight(), bufferFormat);
1157
1158 int bw = m_width;
1159 int bh = m_height;
1160 wxBitmap bmpSource = bmp; // we need a non-const instance
1161 wxUint32* data = (wxUint32*)m_buffer;
1162
1163 if ( bufferFormat == CAIRO_FORMAT_ARGB32 )
1164 {
1165 // use the bitmap's alpha
2fc9c1ea
KO
1166 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1167 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1168
1169 wxAlphaPixelData::Iterator p(pixData);
1170 for (int y=0; y<bh; y++)
1171 {
1172 wxAlphaPixelData::Iterator rowStart = p;
5f606e65 1173 wxUint32* const rowStartDst = data;
2fc9c1ea
KO
1174 for (int x=0; x<bw; x++)
1175 {
1176 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1177 // with alpha in the upper 8 bits, then red, then green, then
1178 // blue. The 32-bit quantities are stored native-endian.
1179 // Pre-multiplied alpha is used.
1180 unsigned char alpha = p.Alpha();
1181 if (alpha == 0)
1182 *data = 0;
1183 else
1184 *data = ( alpha << 24
1185 | (p.Red() * alpha/255) << 16
1186 | (p.Green() * alpha/255) << 8
1187 | (p.Blue() * alpha/255) );
1188 ++data;
1189 ++p;
1190 }
5f606e65
VZ
1191
1192 data = rowStartDst + stride / 4;
2fc9c1ea
KO
1193 p = rowStart;
1194 p.OffsetY(pixData, 1);
1195 }
1196 }
1197 else // no alpha
1198 {
2fc9c1ea
KO
1199 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1200 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1201
1202 wxNativePixelData::Iterator p(pixData);
1203 for (int y=0; y<bh; y++)
1204 {
1205 wxNativePixelData::Iterator rowStart = p;
5f606e65 1206 wxUint32* const rowStartDst = data;
2fc9c1ea
KO
1207 for (int x=0; x<bw; x++)
1208 {
1209 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1210 // the upper 8 bits unused. Red, Green, and Blue are stored in
1211 // the remaining 24 bits in that order. The 32-bit quantities
1212 // are stored native-endian.
1213 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1214 ++data;
1215 ++p;
1216 }
5f606e65
VZ
1217
1218 data = rowStartDst + stride / 4;
2fc9c1ea
KO
1219 p = rowStart;
1220 p.OffsetY(pixData, 1);
1221 }
1222 }
75ded4d1
RD
1223#ifdef __WXMSW__
1224 // if there is a mask, set the alpha bytes in the target buffer to
1225 // fully transparent or fully opaque
1226 if (bmpSource.GetMask())
1227 {
1228 wxBitmap bmpMask = bmpSource.GetMaskBitmap();
1229 bufferFormat = CAIRO_FORMAT_ARGB32;
1230 data = (wxUint32*)m_buffer;
1231 wxNativePixelData pixData(bmpMask, wxPoint(0,0), wxSize(bw, bh));
1232 wxCHECK_RET( pixData, wxT("Failed to gain raw access to mask bitmap data."));
1233
1234 wxNativePixelData::Iterator p(pixData);
1235 for (int y=0; y<bh; y++)
1236 {
1237 wxNativePixelData::Iterator rowStart = p;
5f606e65 1238 wxUint32* const rowStartDst = data;
75ded4d1
RD
1239 for (int x=0; x<bw; x++)
1240 {
1241 if (p.Red()+p.Green()+p.Blue() == 0)
1242 *data = 0;
1243 else
1244 *data = (wxALPHA_OPAQUE << 24) | (*data & 0x00FFFFFF);
1245 ++data;
1246 ++p;
1247 }
5f606e65
VZ
1248
1249 data = rowStartDst + stride / 4;
75ded4d1
RD
1250 p = rowStart;
1251 p.OffsetY(pixData, 1);
1252 }
1253 }
1254#endif
1255
5f606e65 1256 InitSurface(bufferFormat, stride);
711a4812 1257#endif // wxHAS_RAW_BITMAP
2fc9c1ea
KO
1258}
1259
08b2d55f
VZ
1260#if wxUSE_IMAGE
1261
1262// Helper functions for dealing with alpha pre-multiplication.
1263namespace
1264{
1265
1266inline unsigned char Premultiply(unsigned char alpha, unsigned char data)
1267{
1268 return alpha ? (data * alpha)/0xff : data;
1269}
1270
1271inline unsigned char Unpremultiply(unsigned char alpha, unsigned char data)
1272{
1273 return alpha ? (data * 0xff)/alpha : data;
1274}
1275
1276} // anonymous namespace
1277
1278wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer* renderer,
1279 const wxImage& image)
1280 : wxGraphicsObjectRefData(renderer)
1281{
1282 const cairo_format_t bufferFormat = image.HasAlpha()
1283 ? CAIRO_FORMAT_ARGB32
1284 : CAIRO_FORMAT_RGB24;
1285
1286 InitBuffer(image.GetWidth(), image.GetHeight(), bufferFormat);
1287
1288 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1289 // values and not bytes becase Cairo always works with buffers in native
1290 // endianness.
1291 wxUint32* dst = reinterpret_cast<wxUint32*>(m_buffer);
1292 const unsigned char* src = image.GetData();
1293
1294 if ( bufferFormat == CAIRO_FORMAT_ARGB32 )
1295 {
1296 const unsigned char* alpha = image.GetAlpha();
1297
1298 for ( int y = 0; y < m_height; y++ )
1299 {
1300 for ( int x = 0; x < m_width; x++ )
1301 {
1302 const unsigned char a = *alpha++;
1303
1304 *dst++ = a << 24 |
1305 Premultiply(a, src[0]) << 16 |
1306 Premultiply(a, src[1]) << 8 |
1307 Premultiply(a, src[2]);
1308 src += 3;
1309 }
1310 }
1311 }
1312 else // RGB
1313 {
1314 for ( int y = 0; y < m_height; y++ )
1315 {
1316 for ( int x = 0; x < m_width; x++ )
1317 {
1318 *dst++ = src[0] << 16 |
1319 src[1] << 8 |
1320 src[2];
1321 src += 3;
1322 }
1323 }
1324 }
1325
1326 InitSurface(bufferFormat);
1327}
1328
1329wxImage wxCairoBitmapData::ConvertToImage() const
1330{
1331 wxImage image(m_width, m_height, false /* don't clear */);
1332
1333 // Get the surface type and format.
1334 wxCHECK_MSG( cairo_surface_get_type(m_surface) == CAIRO_SURFACE_TYPE_IMAGE,
1335 wxNullImage,
1336 wxS("Can't convert non-image surface to image.") );
1337
1338 switch ( cairo_image_surface_get_format(m_surface) )
1339 {
1340 case CAIRO_FORMAT_ARGB32:
1341 image.SetAlpha();
1342 break;
1343
1344 case CAIRO_FORMAT_RGB24:
1345 // Nothing to do, we don't use alpha by default.
1346 break;
1347
1348 case CAIRO_FORMAT_A8:
1349 case CAIRO_FORMAT_A1:
1350 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1351 return wxNullImage;
1352
1353 default:
1354 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1355 return wxNullImage;
1356 }
1357
1358 // Prepare for copying data.
1359 const wxUint32* src = (wxUint32*)cairo_image_surface_get_data(m_surface);
1360 wxCHECK_MSG( src, wxNullImage, wxS("Failed to get Cairo surface data.") );
1361
1362 int stride = cairo_image_surface_get_stride(m_surface);
1363 wxCHECK_MSG( stride > 0, wxNullImage,
1364 wxS("Failed to get Cairo surface stride.") );
1365
1366 // As we work with wxUint32 pointers and not char ones, we need to adjust
1367 // the stride accordingly. This should be lossless as the stride must be a
1368 // multiple of pixel size.
1369 wxASSERT_MSG( !(stride % sizeof(wxUint32)), wxS("Unexpected stride.") );
1370 stride /= sizeof(wxUint32);
1371
1372 unsigned char* dst = image.GetData();
1373 unsigned char *alpha = image.GetAlpha();
1374 if ( alpha )
1375 {
1376 // We need to also copy alpha and undo the pre-multiplication as Cairo
1377 // stores pre-multiplied values in this format while wxImage does not.
1378 for ( int y = 0; y < m_height; y++ )
1379 {
1380 const wxUint32* const rowStart = src;
1381 for ( int x = 0; x < m_width; x++ )
1382 {
1383 const wxUint32 argb = *src++;
1384
1385 *alpha++ = (argb & 0xff000000) >> 24;
1386
1387 // Copy the RGB data undoing the pre-multiplication.
1388 *dst++ = Unpremultiply(*alpha, (argb & 0x00ff0000) >> 16);
1389 *dst++ = Unpremultiply(*alpha, (argb & 0x0000ff00) >> 8);
1390 *dst++ = Unpremultiply(*alpha, (argb & 0x000000ff));
1391 }
1392
1393 src = rowStart + stride;
1394 }
1395 }
1396 else // RGB
1397 {
1398 // Things are pretty simple in this case, just copy RGB bytes.
1399 for ( int y = 0; y < m_height; y++ )
1400 {
1401 const wxUint32* const rowStart = src;
1402 for ( int x = 0; x < m_width; x++ )
1403 {
1404 const wxUint32 argb = *src++;
1405
1406 *dst++ = (argb & 0x00ff0000) >> 16;
1407 *dst++ = (argb & 0x0000ff00) >> 8;
1408 *dst++ = (argb & 0x000000ff);
1409 }
1410
1411 src = rowStart + stride;
1412 }
1413 }
1414
1415 return image;
1416}
1417
1418#endif // wxUSE_IMAGE
1419
2fc9c1ea
KO
1420wxCairoBitmapData::~wxCairoBitmapData()
1421{
66f917a7
KO
1422 if (m_pattern)
1423 cairo_pattern_destroy(m_pattern);
4ee4c7b9 1424
66f917a7
KO
1425 if (m_surface)
1426 cairo_surface_destroy(m_surface);
4ee4c7b9 1427
2fc9c1ea
KO
1428 delete [] m_buffer;
1429}
1430
08b2d55f
VZ
1431// ----------------------------------------------------------------------------
1432// wxGraphicsBitmap implementation
1433// ----------------------------------------------------------------------------
1434
1435#if wxUSE_IMAGE
1436
1437wxImage wxGraphicsBitmap::ConvertToImage() const
1438{
1439 const wxCairoBitmapData* const
1440 data = static_cast<wxCairoBitmapData*>(GetGraphicsData());
1441
1442 return data ? data->ConvertToImage() : wxNullImage;
1443}
2fc9c1ea 1444
08b2d55f 1445#endif // wxUSE_IMAGE
2fc9c1ea 1446
00bd8e72
SC
1447//-----------------------------------------------------------------------------
1448// wxCairoContext implementation
1449//-----------------------------------------------------------------------------
1450
e7f9f819
SC
1451class wxCairoOffsetHelper
1452{
1453public :
1454 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1455 {
1456 m_ctx = ctx;
1457 m_offset = offset;
1458 if ( m_offset )
1459 cairo_translate( m_ctx, 0.5, 0.5 );
1460 }
1461 ~wxCairoOffsetHelper( )
1462 {
1463 if ( m_offset )
1464 cairo_translate( m_ctx, -0.5, -0.5 );
1465 }
1466public :
1467 cairo_t* m_ctx;
1468 bool m_offset;
1469} ;
1470
c9008abe
RR
1471wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc )
1472: wxGraphicsContext(renderer)
1473{
932d0768
RD
1474#ifdef __WXMSW__
1475 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1476 // text rendering when printing using Cairo. Switch it to MM_TEXT
1477 // map mode to avoid this problem.
1478 HDC hdc = (HDC)dc.GetHDC();
1479 ::SetMapMode(hdc, MM_TEXT);
1480 m_mswSurface = cairo_win32_printing_surface_create(hdc);
1481 Init( cairo_create(m_mswSurface) );
1482#endif
1483
c9008abe
RR
1484#ifdef __WXGTK20__
1485 const wxDCImpl *impl = dc.GetImpl();
1486 Init( (cairo_t*) impl->GetCairoContext() );
932d0768 1487#endif
b129eaa3
VZ
1488 wxSize sz = dc.GetSize();
1489 m_width = sz.x;
1490 m_height = sz.y;
1491
c9008abe 1492 wxPoint org = dc.GetDeviceOrigin();
eaeb9985 1493 cairo_translate( m_context, org.x, org.y );
49ad157e 1494
c9008abe
RR
1495 double sx,sy;
1496 dc.GetUserScale( &sx, &sy );
932d0768
RD
1497
1498// TODO: Determine if these fixes are needed on other platforms too.
1499// On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1500// For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1501// drawing code
1502#ifdef __WXMSW__
1503 double lsx,lsy;
1504 dc.GetLogicalScale( &lsx, &lsy );
1505 sx *= lsx;
1506 sy *= lsy;
1507#endif
eaeb9985 1508 cairo_scale( m_context, sx, sy );
c9008abe 1509
eaeb9985
RR
1510 org = dc.GetLogicalOrigin();
1511 cairo_translate( m_context, -org.x, -org.y );
c9008abe
RR
1512}
1513
00bd8e72
SC
1514wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1515: wxGraphicsContext(renderer)
1516{
b129eaa3
VZ
1517 int width, height;
1518 dc.GetSize( &width, &height );
1519 m_width = width;
1520 m_height = height;
1521
136a1de9
SC
1522 m_enableOffset = true;
1523
932d0768
RD
1524#ifdef __WXMSW__
1525 m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC());
1526 Init( cairo_create(m_mswSurface) );
1527#endif
1528
c9008abe 1529#ifdef __WXGTK20__
888dde65 1530 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
64a226f7 1531 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
49ad157e
VZ
1532
1533#if 0
c9008abe 1534 wxGraphicsMatrix matrix = CreateMatrix();
49ad157e 1535
c9008abe
RR
1536 wxPoint org = dc.GetDeviceOrigin();
1537 matrix.Translate( org.x, org.y );
49ad157e 1538
c9008abe
RR
1539 org = dc.GetLogicalOrigin();
1540 matrix.Translate( -org.x, -org.y );
1541
1542 double sx,sy;
1543 dc.GetUserScale( &sx, &sy );
1544 matrix.Scale( sx, sy );
1545
1546 ConcatTransform( matrix );
1547#endif
e7f9f819 1548#endif
c9008abe 1549
888dde65 1550#ifdef __WXMAC__
888dde65
RR
1551 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1552 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1553 Init( cairo_create( surface ) );
1554 cairo_surface_destroy( surface );
1555#endif
1556}
1557
1558wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
1559: wxGraphicsContext(renderer)
1560{
b129eaa3
VZ
1561 int width, height;
1562 dc.GetSize( &width, &height );
1563 m_width = width;
1564 m_height = height;
1565
136a1de9 1566 m_enableOffset = true;
932d0768
RD
1567
1568#ifdef __WXMSW__
1569 m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC());
1570 Init( cairo_create(m_mswSurface) );
1571#endif
136a1de9 1572
c9008abe 1573#ifdef __WXGTK20__
888dde65
RR
1574 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1575 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
49ad157e
VZ
1576
1577#if 0
c9008abe 1578 wxGraphicsMatrix matrix = CreateMatrix();
49ad157e 1579
c9008abe
RR
1580 wxPoint org = dc.GetDeviceOrigin();
1581 matrix.Translate( org.x, org.y );
49ad157e 1582
c9008abe
RR
1583 org = dc.GetLogicalOrigin();
1584 matrix.Translate( -org.x, -org.y );
1585
1586 double sx,sy;
1587 dc.GetUserScale( &sx, &sy );
1588 matrix.Scale( sx, sy );
1589
1590 ConcatTransform( matrix );
64a226f7 1591#endif
c9008abe
RR
1592#endif
1593
e7f9f819 1594#ifdef __WXMAC__
e7f9f819
SC
1595 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1596 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1597 Init( cairo_create( surface ) );
1598 cairo_surface_destroy( surface );
00bd8e72 1599#endif
00bd8e72
SC
1600}
1601
c9008abe 1602#ifdef __WXGTK20__
00bd8e72
SC
1603wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1604: wxGraphicsContext(renderer)
1605{
e7f9f819 1606 Init( gdk_cairo_create( drawable ) );
b129eaa3
VZ
1607
1608 int width, height;
1609 gdk_drawable_get_size( drawable, &width, &height );
1610 m_width = width;
1611 m_height = height;
00bd8e72
SC
1612}
1613#endif
1614
c0e69d72
KO
1615#ifdef __WXMSW__
1616wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, HDC handle )
1617: wxGraphicsContext(renderer)
1618{
1619 m_mswSurface = cairo_win32_surface_create(handle);
b129eaa3
VZ
1620 Init( cairo_create(m_mswSurface) );
1621 m_width =
1622 m_height = 0;
c0e69d72
KO
1623}
1624#endif
1625
1626
00bd8e72
SC
1627wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1628: wxGraphicsContext(renderer)
1629{
e7f9f819 1630 Init( context );
b129eaa3
VZ
1631 m_width =
1632 m_height = 0;
184fc6c8
SC
1633}
1634
00bd8e72
SC
1635wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1636: wxGraphicsContext(renderer)
184fc6c8 1637{
136a1de9 1638 m_enableOffset = true;
00bd8e72
SC
1639#ifdef __WXGTK__
1640 // something along these lines (copied from dcclient)
1641
00bd8e72
SC
1642 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1643 // code should still be able to create wxClientDCs for them, so we will
1644 // use the parent window here then.
82a234fb 1645 if (window->m_wxwindow == NULL)
184fc6c8 1646 {
00bd8e72 1647 window = window->GetParent();
184fc6c8 1648 }
00bd8e72 1649
82a234fb 1650 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
00bd8e72 1651
82a234fb 1652 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
b129eaa3
VZ
1653
1654 wxSize sz = window->GetSize();
1655 m_width = sz.x;
1656 m_height = sz.y;
00bd8e72 1657#endif
9f2b6b31
RD
1658
1659#ifdef __WXMSW__
1660 m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle());
1661 Init(cairo_create(m_mswSurface));
1662#endif
1663
00bd8e72
SC
1664}
1665
79313208
VZ
1666wxCairoContext::wxCairoContext(wxGraphicsRenderer* renderer) :
1667 wxGraphicsContext(renderer)
1668{
1669 m_context = NULL;
1670}
1671
00bd8e72
SC
1672wxCairoContext::~wxCairoContext()
1673{
00bd8e72
SC
1674 if ( m_context )
1675 {
1676 PopState();
1677 PopState();
1678 cairo_destroy(m_context);
1679 }
c0e69d72
KO
1680#ifdef __WXMSW__
1681 if ( m_mswSurface )
1682 cairo_surface_destroy(m_mswSurface);
1683#endif
00bd8e72
SC
1684}
1685
e7f9f819
SC
1686void wxCairoContext::Init(cairo_t *context)
1687{
1688 m_context = context ;
1689 PushState();
1690 PushState();
1691}
1692
00bd8e72 1693
d9485f89 1694void wxCairoContext::Clip( const wxRegion& region )
00bd8e72 1695{
d9485f89
RD
1696 // Create a path with all the rectangles in the region
1697 wxGraphicsPath path = GetRenderer()->CreatePath();
1698 wxRegionIterator ri(region);
1699 while (ri)
1700 {
1701 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
82a234fb 1702 ++ri;
d9485f89 1703 }
49ad157e 1704
d9485f89
RD
1705 // Put it in the context
1706 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1707 cairo_append_path(m_context, cp);
1708
1709 // clip to that path
1710 cairo_clip(m_context);
49ad157e 1711 path.UnGetNativePath(cp);
00bd8e72
SC
1712}
1713
1714void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1715{
d9485f89
RD
1716 // Create a path with this rectangle
1717 wxGraphicsPath path = GetRenderer()->CreatePath();
1718 path.AddRectangle(x,y,w,h);
1719
1720 // Put it in the context
1721 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1722 cairo_append_path(m_context, cp);
1723
1724 // clip to that path
71696da0 1725 cairo_clip(m_context);
49ad157e 1726 path.UnGetNativePath(cp);
00bd8e72
SC
1727}
1728
1729void wxCairoContext::ResetClip()
1730{
d9485f89 1731 cairo_reset_clip(m_context);
00bd8e72
SC
1732}
1733
1734
0db8a70e 1735void wxCairoContext::StrokePath( const wxGraphicsPath& path )
00bd8e72 1736{
87752530 1737 if ( !m_pen.IsNull() )
49ad157e
VZ
1738 {
1739 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
0db8a70e 1740 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
00bd8e72 1741 cairo_append_path(m_context,cp);
87752530 1742 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
00bd8e72 1743 cairo_stroke(m_context);
0db8a70e 1744 path.UnGetNativePath(cp);
00bd8e72
SC
1745 }
1746}
1747
94a007ec 1748void wxCairoContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
00bd8e72 1749{
87752530 1750 if ( !m_brush.IsNull() )
00bd8e72 1751 {
e7f9f819 1752 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
0db8a70e 1753 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
00bd8e72 1754 cairo_append_path(m_context,cp);
87752530 1755 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
00bd8e72
SC
1756 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1757 cairo_fill(m_context);
0db8a70e 1758 path.UnGetNativePath(cp);
00bd8e72
SC
1759 }
1760}
1761
1762void wxCairoContext::Rotate( wxDouble angle )
1763{
1764 cairo_rotate(m_context,angle);
1765}
1766
1767void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1768{
1769 cairo_translate(m_context,dx,dy);
1770}
1771
1772void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1773{
1774 cairo_scale(m_context,xScale,yScale);
1775}
1776
1777// concatenates this transform with the current transform of this context
0db8a70e 1778void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
00bd8e72 1779{
0db8a70e 1780 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
00bd8e72
SC
1781}
1782
1783// sets the transform of this context
0db8a70e 1784void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
00bd8e72 1785{
0db8a70e 1786 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
00bd8e72
SC
1787}
1788
1789// gets the matrix of this context
0db8a70e 1790wxGraphicsMatrix wxCairoContext::GetTransform() const
00bd8e72 1791{
0db8a70e
RD
1792 wxGraphicsMatrix matrix = CreateMatrix();
1793 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1794 return matrix;
00bd8e72
SC
1795}
1796
1797
1798
1799void wxCairoContext::PushState()
1800{
1801 cairo_save(m_context);
1802}
1803
1804void wxCairoContext::PopState()
1805{
1806 cairo_restore(m_context);
1807}
184fc6c8
SC
1808
1809void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1810{
2fc9c1ea 1811 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
3c10f6db 1812 DrawBitmap(bitmap, x, y, w, h);
d9485f89 1813
2fc9c1ea 1814}
49ad157e 1815
2fc9c1ea
KO
1816void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1817{
d9485f89 1818 PushState();
49ad157e 1819
d9485f89
RD
1820 // In case we're scaling the image by using a width and height different
1821 // than the bitmap's size create a pattern transformation on the surface and
1822 // draw the transformed pattern.
2fc9c1ea
KO
1823 wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData());
1824 cairo_pattern_t* pattern = data->GetCairoPattern();
1825 wxSize size = data->GetSize();
1826
1827 wxDouble scaleX = w / size.GetWidth();
1828 wxDouble scaleY = h / size.GetHeight();
d9485f89
RD
1829
1830 // prepare to draw the image
1831 cairo_translate(m_context, x, y);
ce6b1014 1832 cairo_scale(m_context, scaleX, scaleY);
d9485f89
RD
1833 cairo_set_source(m_context, pattern);
1834 // use the original size here since the context is scaled already...
2fc9c1ea 1835 cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight());
d9485f89
RD
1836 // fill the rectangle using the pattern
1837 cairo_fill(m_context);
1838
d9485f89 1839 PopState();
184fc6c8
SC
1840}
1841
1842void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1843{
d9485f89
RD
1844 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1845 // start using the Cairo backend on other platforms then we may need to
1846 // fiddle with this...
1847 DrawBitmap(icon, x, y, w, h);
184fc6c8
SC
1848}
1849
1850
0b7dce54 1851void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y)
184fc6c8 1852{
0b7dce54
VZ
1853 wxCHECK_RET( !m_font.IsNull(),
1854 wxT("wxCairoContext::DrawText - no valid font set") );
1011fbeb
SC
1855
1856 if ( str.empty())
d9485f89 1857 return;
84f67b11 1858
10c508ef 1859 const wxCharBuffer data = str.utf8_str();
e7f9f819
SC
1860 if ( !data )
1861 return;
0b7dce54 1862
84f67b11
SC
1863 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1864
0b7dce54
VZ
1865#ifdef __WXGTK__
1866 size_t datalen = strlen(data);
1867
e7f9f819 1868 PangoLayout *layout = pango_cairo_create_layout (m_context);
0d19936d
FM
1869 wxCairoFontData* font_data = (wxCairoFontData*) m_font.GetRefData();
1870 pango_layout_set_font_description( layout, font_data->GetFont());
84f67b11 1871 pango_layout_set_text(layout, data, datalen);
03647350
VZ
1872
1873 if (font_data->GetUnderlined())
0d19936d
FM
1874 {
1875 PangoAttrList *attrs = pango_attr_list_new();
1876 PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1877 pango_attr_list_insert(attrs, attr);
1878 pango_layout_set_attributes(layout, attrs);
1879 pango_attr_list_unref(attrs);
1880 }
1881
e7f9f819
SC
1882 cairo_move_to(m_context, x, y);
1883 pango_cairo_show_layout (m_context, layout);
1884
1885 g_object_unref (layout);
1886#else
d9485f89
RD
1887 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1888 // the position we move to by the ascent.
1889 cairo_font_extents_t fe;
1890 cairo_font_extents(m_context, &fe);
1891 cairo_move_to(m_context, x, y+fe.ascent);
49ad157e 1892
0b7dce54 1893 cairo_show_text(m_context, data);
e7f9f819 1894#endif
184fc6c8
SC
1895}
1896
1897void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1898 wxDouble *descent, wxDouble *externalLeading ) const
1899{
1011fbeb
SC
1900 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1901
e7f9f819
SC
1902 if ( width )
1903 *width = 0;
1904 if ( height )
1905 *height = 0;
1906 if ( descent )
1907 *descent = 0;
1908 if ( externalLeading )
1909 *externalLeading = 0;
1910
1011fbeb 1911 if ( str.empty())
d9485f89
RD
1912 return;
1913
e7f9f819
SC
1914#ifdef __WXGTK__
1915 int w, h;
49ad157e 1916
e7f9f819 1917 PangoLayout *layout = pango_cairo_create_layout (m_context);
84f67b11 1918 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
10c508ef 1919 const wxCharBuffer data = str.utf8_str();
84f67b11 1920 if ( !data )
e7f9f819
SC
1921 {
1922 return;
1923 }
84f67b11 1924 pango_layout_set_text( layout, data, strlen(data) );
e7f9f819
SC
1925 pango_layout_get_pixel_size (layout, &w, &h);
1926 if ( width )
1927 *width = w;
1928 if ( height )
1929 *height = h;
1930 if (descent)
1931 {
1932 PangoLayoutIter *iter = pango_layout_get_iter(layout);
1933 int baseline = pango_layout_iter_get_baseline(iter);
1934 pango_layout_iter_free(iter);
1935 *descent = h - PANGO_PIXELS(baseline);
1936 }
1937 g_object_unref (layout);
1938#else
d9485f89
RD
1939 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1940
1941 if (width)
1942 {
1943 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1944 cairo_text_extents_t te;
1945 cairo_text_extents(m_context, buf, &te);
1946 *width = te.width;
1947 }
1948
1949 if (height || descent || externalLeading)
1950 {
1951 cairo_font_extents_t fe;
1952 cairo_font_extents(m_context, &fe);
49ad157e 1953
3e700936 1954 // some backends have negative descents
49ad157e 1955
3e700936
SC
1956 if ( fe.descent < 0 )
1957 fe.descent = -fe.descent;
49ad157e 1958
3e700936
SC
1959 if ( fe.height < (fe.ascent + fe.descent ) )
1960 {
1961 // some backends are broken re height ... (eg currently ATSUI)
1962 fe.height = fe.ascent + fe.descent;
1963 }
49ad157e 1964
d9485f89
RD
1965 if (height)
1966 *height = fe.height;
1967 if ( descent )
1968 *descent = fe.descent;
1969 if ( externalLeading )
1970 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1971 }
e7f9f819 1972#endif
184fc6c8
SC
1973}
1974
1975void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1976{
1977 widths.Empty();
1978 widths.Add(0, text.length());
1979
1011fbeb
SC
1980 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1981
184fc6c8
SC
1982 if (text.empty())
1983 return;
00bd8e72
SC
1984
1985 // TODO
184fc6c8
SC
1986}
1987
49ad157e 1988void * wxCairoContext::GetNativeContext()
184fc6c8 1989{
00bd8e72
SC
1990 return m_context;
1991}
184fc6c8 1992
bf02a7f9 1993bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias)
49ad157e 1994{
bf02a7f9 1995 if (m_antialias == antialias)
49ad157e
VZ
1996 return true;
1997
bf02a7f9 1998 m_antialias = antialias;
03647350 1999
bf02a7f9
SC
2000 cairo_antialias_t antialiasMode;
2001 switch (antialias)
2002 {
2003 case wxANTIALIAS_DEFAULT:
2004 antialiasMode = CAIRO_ANTIALIAS_DEFAULT;
2005 break;
2006 case wxANTIALIAS_NONE:
2007 antialiasMode = CAIRO_ANTIALIAS_NONE;
2008 break;
2009 default:
2010 return false;
2011 }
2012 cairo_set_antialias(m_context, antialiasMode);
2013 return true;
2014}
49ad157e 2015
133cb28b
SC
2016bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality WXUNUSED(interpolation))
2017{
2018 // placeholder
2019 return false;
2020}
2021
bf02a7f9
SC
2022bool wxCairoContext::SetCompositionMode(wxCompositionMode op)
2023{
2024 if ( m_composition == op )
2025 return true;
03647350 2026
bf02a7f9
SC
2027 m_composition = op;
2028 cairo_operator_t cop;
2029 switch (op)
49ad157e 2030 {
50de831a 2031 case wxCOMPOSITION_CLEAR:
bf02a7f9 2032 cop = CAIRO_OPERATOR_CLEAR;
49ad157e 2033 break;
bf02a7f9
SC
2034 case wxCOMPOSITION_SOURCE:
2035 cop = CAIRO_OPERATOR_SOURCE;
49ad157e 2036 break;
bf02a7f9
SC
2037 case wxCOMPOSITION_OVER:
2038 cop = CAIRO_OPERATOR_OVER;
49ad157e 2039 break;
bf02a7f9
SC
2040 case wxCOMPOSITION_IN:
2041 cop = CAIRO_OPERATOR_IN;
2042 break;
2043 case wxCOMPOSITION_OUT:
2044 cop = CAIRO_OPERATOR_OUT;
2045 break;
2046 case wxCOMPOSITION_ATOP:
2047 cop = CAIRO_OPERATOR_ATOP;
2048 break;
2049 case wxCOMPOSITION_DEST:
2050 cop = CAIRO_OPERATOR_DEST;
2051 break;
2052 case wxCOMPOSITION_DEST_OVER:
2053 cop = CAIRO_OPERATOR_DEST_OVER;
2054 break;
2055 case wxCOMPOSITION_DEST_IN:
2056 cop = CAIRO_OPERATOR_DEST_IN;
2057 break;
2058 case wxCOMPOSITION_DEST_OUT:
2059 cop = CAIRO_OPERATOR_DEST_OUT;
2060 break;
2061 case wxCOMPOSITION_DEST_ATOP:
2062 cop = CAIRO_OPERATOR_DEST_ATOP;
2063 break;
2064 case wxCOMPOSITION_XOR:
2065 cop = CAIRO_OPERATOR_XOR;
2066 break;
2067 case wxCOMPOSITION_ADD:
2068 cop = CAIRO_OPERATOR_ADD;
49ad157e 2069 break;
49ad157e
VZ
2070 default:
2071 return false;
2072 }
bf02a7f9 2073 cairo_set_operator(m_context, cop);
49ad157e
VZ
2074 return true;
2075}
2076
bf02a7f9
SC
2077void wxCairoContext::BeginLayer(wxDouble opacity)
2078{
2079 m_layerOpacities.push_back(opacity);
2080 cairo_push_group(m_context);
2081}
49ad157e 2082
bf02a7f9
SC
2083void wxCairoContext::EndLayer()
2084{
2085 float opacity = m_layerOpacities.back();
2086 m_layerOpacities.pop_back();
2087 cairo_pop_group_to_source(m_context);
2088 cairo_paint_with_alpha(m_context,opacity);
2089}
03647350 2090
00bd8e72
SC
2091//-----------------------------------------------------------------------------
2092// wxCairoRenderer declaration
2093//-----------------------------------------------------------------------------
2094
2095class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
2096{
2097public :
2098 wxCairoRenderer() {}
2099
2100 virtual ~wxCairoRenderer() {}
2101
2102 // Context
2103
2104 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
773ccc31 2105 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
0b822969 2106 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
773ccc31 2107
00bd8e72
SC
2108 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2109
2110 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2111
2112 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2113
091ef146 2114 virtual wxGraphicsContext * CreateMeasuringContext();
9f2b6b31
RD
2115#ifdef __WXMSW__
2116#if wxUSE_ENH_METAFILE
2117 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
2118#endif
2119#endif
00bd8e72
SC
2120 // Path
2121
0db8a70e 2122 virtual wxGraphicsPath CreatePath();
00bd8e72
SC
2123
2124 // Matrix
2125
49ad157e 2126 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
00bd8e72
SC
2127 wxDouble tx=0.0, wxDouble ty=0.0);
2128
2129
87752530 2130 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
00bd8e72 2131
87752530 2132 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
00bd8e72 2133
4ee4c7b9
VZ
2134 virtual wxGraphicsBrush
2135 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2136 wxDouble x2, wxDouble y2,
2137 const wxGraphicsGradientStops& stops);
00bd8e72 2138
4ee4c7b9
VZ
2139 virtual wxGraphicsBrush
2140 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2141 wxDouble xc, wxDouble yc,
2142 wxDouble radius,
2143 const wxGraphicsGradientStops& stops);
00bd8e72
SC
2144
2145 // sets the font
87752530 2146 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
00bd8e72 2147
d974a494 2148 // create a native bitmap representation
2fc9c1ea 2149 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
49ad157e 2150
2986eb86
SC
2151 // create a graphics bitmap from a native bitmap
2152 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
2153
d974a494 2154 // create a subimage from a native image representation
2fc9c1ea 2155 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
d974a494 2156
932d0768
RD
2157protected :
2158 bool EnsureIsLoaded();
2159 void Load();
2160 void Unload();
2161 friend class wxCairoModule;
00bd8e72 2162private :
932d0768 2163 int m_loaded;
00bd8e72
SC
2164 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
2165} ;
2166
2167//-----------------------------------------------------------------------------
2168// wxCairoRenderer implementation
2169//-----------------------------------------------------------------------------
2170
2171IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
2172
2173static wxCairoRenderer gs_cairoGraphicsRenderer;
3e700936
SC
2174// temporary hack to allow creating a cairo context on any platform
2175extern wxGraphicsRenderer* gCairoRenderer;
2176wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer;
00bd8e72 2177
932d0768
RD
2178bool wxCairoRenderer::EnsureIsLoaded()
2179{
2180#ifndef __WXGTK__
2181 Load();
2182 return wxCairoInit();
2183#else
2184 return true;
2185#endif
2186}
2187
2188void wxCairoRenderer::Load()
2189{
2190 wxCairoInit();
2191}
2192
2193void wxCairoRenderer::Unload()
2194{
2195 wxCairoCleanUp();
2196}
2197
2198// call EnsureIsLoaded() and return returnOnFail value if it fails
2199#define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2200 if ( !EnsureIsLoaded() ) \
2201 return (returnOnFail)
2202
00bd8e72 2203wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
539e2795 2204{
932d0768 2205 ENSURE_LOADED_OR_RETURN(NULL);
00bd8e72
SC
2206 return new wxCairoContext(this,dc);
2207}
2208
773ccc31
SC
2209wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
2210{
932d0768 2211 ENSURE_LOADED_OR_RETURN(NULL);
888dde65 2212 return new wxCairoContext(this,dc);
773ccc31 2213}
773ccc31 2214
0b822969
RR
2215wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc)
2216{
932d0768 2217 ENSURE_LOADED_OR_RETURN(NULL);
c9008abe 2218#ifdef __WXGTK20__
0b822969
RR
2219 const wxDCImpl *impl = dc.GetImpl();
2220 cairo_t* context = (cairo_t*) impl->GetCairoContext();
2221 if (context)
c9008abe 2222 return new wxCairoContext(this,dc);
0b822969 2223 else
c9008abe 2224#endif
932d0768 2225 return new wxCairoContext(this,dc);
0b822969
RR
2226}
2227
9f2b6b31
RD
2228#ifdef __WXMSW__
2229#if wxUSE_ENH_METAFILE
932d0768 2230wxGraphicsContext * wxCairoRenderer::CreateContext( const wxEnhMetaFileDC& WXUNUSED(dc) )
9f2b6b31 2231{
932d0768 2232 ENSURE_LOADED_OR_RETURN(NULL);
9f2b6b31
RD
2233 return NULL;
2234}
2235#endif
2236#endif
2237
00bd8e72
SC
2238wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
2239{
932d0768 2240 ENSURE_LOADED_OR_RETURN(NULL);
a8f9fe13 2241#ifdef __WXMSW__
c0e69d72 2242 return new wxCairoContext(this,(HDC)context);
711a4812 2243#else
00bd8e72 2244 return new wxCairoContext(this,(cairo_t*)context);
c0e69d72 2245#endif
00bd8e72
SC
2246}
2247
2248
2249wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
2250{
932d0768 2251 ENSURE_LOADED_OR_RETURN(NULL);
00bd8e72 2252#ifdef __WXGTK__
ed1b38a8 2253 return new wxCairoContext(this,(GdkDrawable*)window);
00bd8e72 2254#else
932d0768 2255 wxUnusedVar(window);
00bd8e72
SC
2256 return NULL;
2257#endif
2258}
2259
091ef146
SC
2260wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
2261{
932d0768 2262 ENSURE_LOADED_OR_RETURN(NULL);
5e8d27fe
KO
2263#ifdef __WXGTK__
2264 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2265#endif
091ef146
SC
2266 return NULL;
2267 // TODO
2268}
2269
00bd8e72
SC
2270wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
2271{
932d0768 2272 ENSURE_LOADED_OR_RETURN(NULL);
00bd8e72
SC
2273 return new wxCairoContext(this, window );
2274}
2275
2276// Path
2277
0db8a70e 2278wxGraphicsPath wxCairoRenderer::CreatePath()
00bd8e72 2279{
932d0768 2280 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
0db8a70e
RD
2281 wxGraphicsPath path;
2282 path.SetRefData( new wxCairoPathData(this) );
2283 return path;
539e2795
SC
2284}
2285
00bd8e72
SC
2286
2287// Matrix
2288
49ad157e 2289wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
0db8a70e 2290 wxDouble tx, wxDouble ty)
00bd8e72 2291
184fc6c8 2292{
932d0768 2293 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
0db8a70e
RD
2294 wxGraphicsMatrix m;
2295 wxCairoMatrixData* data = new wxCairoMatrixData( this );
2296 data->Set( a,b,c,d,tx,ty ) ;
2297 m.SetRefData(data);
00bd8e72 2298 return m;
7ba86d93
RD
2299}
2300
49ad157e 2301wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
539e2795 2302{
932d0768 2303 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
a1b806b9 2304 if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
87752530 2305 return wxNullGraphicsPen;
00bd8e72 2306 else
87752530
SC
2307 {
2308 wxGraphicsPen p;
2309 p.SetRefData(new wxCairoPenData( this, pen ));
2310 return p;
2311 }
539e2795
SC
2312}
2313
49ad157e 2314wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
539e2795 2315{
932d0768 2316 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
a1b806b9 2317 if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
87752530 2318 return wxNullGraphicsBrush;
00bd8e72 2319 else
87752530
SC
2320 {
2321 wxGraphicsBrush p;
2322 p.SetRefData(new wxCairoBrushData( this, brush ));
2323 return p;
2324 }
00bd8e72
SC
2325}
2326
4ee4c7b9
VZ
2327wxGraphicsBrush
2328wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2329 wxDouble x2, wxDouble y2,
2330 const wxGraphicsGradientStops& stops)
00bd8e72 2331{
932d0768 2332 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
87752530
SC
2333 wxGraphicsBrush p;
2334 wxCairoBrushData* d = new wxCairoBrushData( this );
4ee4c7b9 2335 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
87752530
SC
2336 p.SetRefData(d);
2337 return p;
00bd8e72
SC
2338}
2339
4ee4c7b9
VZ
2340wxGraphicsBrush
2341wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2342 wxDouble xc, wxDouble yc, wxDouble r,
2343 const wxGraphicsGradientStops& stops)
00bd8e72 2344{
932d0768 2345 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
87752530
SC
2346 wxGraphicsBrush p;
2347 wxCairoBrushData* d = new wxCairoBrushData( this );
4ee4c7b9 2348 d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops);
87752530
SC
2349 p.SetRefData(d);
2350 return p;
00bd8e72
SC
2351}
2352
2353// sets the font
49ad157e 2354wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
00bd8e72 2355{
932d0768 2356 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
a1b806b9 2357 if ( font.IsOk() )
49ad157e 2358 {
87752530
SC
2359 wxGraphicsFont p;
2360 p.SetRefData(new wxCairoFontData( this , font, col ));
2361 return p;
2362 }
00bd8e72 2363 else
87752530 2364 return wxNullGraphicsFont;
539e2795
SC
2365}
2366
f5008769 2367wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp )
2fc9c1ea 2368{
932d0768 2369 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
a1b806b9 2370 if ( bmp.IsOk() )
2fc9c1ea
KO
2371 {
2372 wxGraphicsBitmap p;
2373 p.SetRefData(new wxCairoBitmapData( this , bmp ));
2374 return p;
2375 }
2376 else
2377 return wxNullGraphicsBitmap;
2378}
2379
2986eb86
SC
2380wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
2381{
932d0768 2382 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2986eb86
SC
2383 if ( bitmap != NULL )
2384 {
2385 wxGraphicsBitmap p;
2386 p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap ));
2387 return p;
2388 }
2389 else
2390 return wxNullGraphicsBitmap;
2391}
2392
84e0e526
VZ
2393wxGraphicsBitmap
2394wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap),
2395 wxDouble WXUNUSED(x),
2396 wxDouble WXUNUSED(y),
2397 wxDouble WXUNUSED(w),
2398 wxDouble WXUNUSED(h))
2fc9c1ea 2399{
932d0768 2400 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2fc9c1ea
KO
2401 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2402 return wxNullGraphicsBitmap;
2403}
2404
c0e69d72
KO
2405wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2406{
c0e69d72 2407 return &gs_cairoGraphicsRenderer;
ef40c54c
VZ
2408}
2409
2410#else // !wxUSE_CAIRO
2411
2412wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2413{
c0e69d72 2414 return NULL;
c0e69d72 2415}
9a67941f 2416
ef40c54c
VZ
2417#endif // wxUSE_CAIRO/!wxUSE_CAIRO
2418
2419// MSW and OS X have their own native default renderers, but the other ports
2420// use Cairo by default
2421#if !(defined(__WXMSW__) || defined(__WXOSX__))
2422wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2423{
2424 return GetCairoRenderer();
2425}
2426#endif // !(__WXMSW__ || __WXOSX__)
2427
9a67941f 2428#endif // wxUSE_GRAPHICS_CONTEXT