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