1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_GRAPHICS_CONTEXT
20 #include "wx/graphics.h"
24 // keep cairo.h from defining dllimport as we're defining the symbols inside
25 // the wx dll in order to load them dynamically.
31 #include "wx/bitmap.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcmemory.h"
35 #include "wx/dcprint.h"
37 #include "wx/window.h"
41 #include "wx/private/graphics.h"
42 #include "wx/rawbmp.h"
43 #include "wx/vector.h"
47 //-----------------------------------------------------------------------------
48 // device context implementation
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 //-----------------------------------------------------------------------------
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)
59 //-----------------------------------------------------------------------------
60 // wxGraphicsPath implementation
61 //-----------------------------------------------------------------------------
63 // TODO remove this dependency (gdiplus needs the macros)
66 #define max(a,b) (((a) > (b)) ? (a) : (b))
70 #define min(a,b) (((a) < (b)) ? (a) : (b))
75 #include <cairo-win32.h>
80 #include "wx/fontutil.h"
81 #include "wx/gtk/dc.h"
85 #include <cairo-win32.h>
89 #include "wx/osx/private.h"
90 #include <cairo-quartz.h>
91 #include <cairo-atsui.h>
94 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
97 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
100 virtual wxGraphicsObjectRefData
*Clone() const;
103 // These are the path primitives from which everything else can be constructed
106 // begins a new subpath at (x,y)
107 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
109 // adds a straight line from the current point to (x,y)
110 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
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
);
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
) ;
119 // gets the last point of the current path, (0,0) if not yet set
120 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
123 virtual void AddPath( const wxGraphicsPathData
* path
);
125 // closes the current sub-path
126 virtual void CloseSubpath();
129 // These are convenience functions which - if not available natively will be assembled
130 // using the primitives from above
135 // appends a rectangle as a new closed subpath
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 ) ;
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 ) ;
144 // returns the native path
145 virtual void * GetNativePath() const ;
147 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
148 virtual void UnGetNativePath(void *p
) const;
150 // transforms each point of this path by the matrix
151 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
153 // gets the bounding box enclosing all points (possibly including control points)
154 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
156 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
159 cairo_t
* m_pathContext
;
162 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
165 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
166 virtual ~wxCairoMatrixData() ;
168 virtual wxGraphicsObjectRefData
*Clone() const ;
170 // concatenates the matrix
171 virtual void Concat( const wxGraphicsMatrixData
*t
);
173 // sets the matrix to the respective values
174 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
175 wxDouble tx
=0.0, wxDouble ty
=0.0);
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;
181 // makes this the inverse matrix
182 virtual void Invert();
184 // returns true if the elements of the transformation matrix are equal ?
185 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
187 // return true if this is the identity matrix
188 virtual bool IsIdentity() const;
194 // add the translation to this matrix
195 virtual void Translate( wxDouble dx
, wxDouble dy
);
197 // add the scale to this matrix
198 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
200 // add the rotation to this matrix (radians)
201 virtual void Rotate( wxDouble angle
);
204 // apply the transforms
207 // applies that matrix to the point
208 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
210 // applies the matrix except for translations
211 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
213 // returns the native representation
214 virtual void * GetNativeMatrix() const;
216 cairo_matrix_t m_matrix
;
219 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
222 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
227 virtual void Apply( wxGraphicsContext
* context
);
228 virtual wxDouble
GetWidth() { return m_width
; }
238 cairo_line_cap_t m_cap
;
239 cairo_line_join_t m_join
;
242 const double *m_lengths
;
243 double *m_userLengths
;
247 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
250 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
253 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
254 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
255 ~wxCairoBrushData ();
257 virtual void Apply( wxGraphicsContext
* context
);
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
);
269 // common part of Create{Linear,Radial}GradientBrush()
270 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
278 cairo_pattern_t
* m_brushPattern
;
281 class wxCairoFontData
: public wxGraphicsObjectRefData
284 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
287 virtual void Apply( wxGraphicsContext
* context
);
289 const PangoFontDescription
* GetFont() const { return m_font
; }
290 bool GetUnderlined() const { return m_underlined
; }
300 cairo_font_face_t
*m_font
;
301 #elif defined(__WXGTK__)
302 PangoFontDescription
* m_font
;
304 wxCharBuffer m_fontName
;
305 cairo_font_slant_t m_slant
;
306 cairo_font_weight_t m_weight
;
310 class wxCairoBitmapData
: public wxGraphicsObjectRefData
313 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
315 wxCairoBitmapData(wxGraphicsRenderer
* renderer
, const wxImage
& image
);
316 #endif // wxUSE_IMAGE
317 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
318 ~wxCairoBitmapData();
320 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
321 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
322 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
325 wxImage
ConvertToImage() const;
326 #endif // wxUSE_IMAGE
329 // Allocate m_buffer for the bitmap of the given size in the given format.
331 // Returns the stride used for the buffer.
332 int InitBuffer(int width
, int height
, cairo_format_t format
);
334 // Really create the surface using the buffer (which was supposed to be
335 // filled since InitBuffer() call).
336 void InitSurface(cairo_format_t format
, int stride
);
339 cairo_surface_t
* m_surface
;
340 cairo_pattern_t
* m_pattern
;
343 unsigned char* m_buffer
;
346 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
349 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
350 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
351 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
353 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
356 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
358 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
359 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
361 // If this ctor is used, Init() must be called by the derived class later.
362 wxCairoContext( wxGraphicsRenderer
* renderer
);
364 virtual ~wxCairoContext();
366 virtual bool ShouldOffset() const
368 if ( !m_enableOffset
)
372 if ( !m_pen
.IsNull() )
374 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
378 return ( penwidth
% 2 ) == 1;
381 virtual void Clip( const wxRegion
®ion
);
383 cairo_surface_t
* m_mswSurface
;
386 // clips drawings to the rect
387 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
389 // resets the clipping to original extent
390 virtual void ResetClip();
392 virtual void * GetNativeContext();
394 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
396 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
398 virtual bool SetCompositionMode(wxCompositionMode op
);
400 virtual void BeginLayer(wxDouble opacity
);
402 virtual void EndLayer();
404 virtual void StrokePath( const wxGraphicsPath
& p
);
405 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
407 virtual void Translate( wxDouble dx
, wxDouble dy
);
408 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
409 virtual void Rotate( wxDouble angle
);
411 // concatenates this transform with the current transform of this context
412 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
414 // sets the transform of this context
415 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
417 // gets the matrix of this context
418 virtual wxGraphicsMatrix
GetTransform() const;
420 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
421 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
422 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
423 virtual void PushState();
424 virtual void PopState();
426 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
427 wxDouble
*descent
, wxDouble
*externalLeading
) const;
428 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
431 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
433 void Init(cairo_t
*context
);
438 wxVector
<float> m_layerOpacities
;
440 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
444 // ----------------------------------------------------------------------------
445 // wxCairoImageContext: context associated with a wxImage.
446 // ----------------------------------------------------------------------------
448 class wxCairoImageContext
: public wxCairoContext
451 wxCairoImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
452 wxCairoContext(renderer
),
454 m_data(renderer
, image
)
456 Init(cairo_create(m_data
.GetCairoSurface()));
459 virtual ~wxCairoImageContext()
461 m_image
= m_data
.ConvertToImage();
466 wxCairoBitmapData m_data
;
468 wxDECLARE_NO_COPY_CLASS(wxCairoImageContext
);
470 #endif // wxUSE_IMAGE
472 //-----------------------------------------------------------------------------
473 // wxCairoPenData implementation
474 //-----------------------------------------------------------------------------
476 wxCairoPenData::~wxCairoPenData()
478 delete[] m_userLengths
;
481 void wxCairoPenData::Init()
484 m_userLengths
= NULL
;
489 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
490 : wxGraphicsObjectRefData(renderer
)
494 m_width
= m_pen
.GetWidth();
498 m_red
= m_pen
.GetColour().Red()/255.0;
499 m_green
= m_pen
.GetColour().Green()/255.0;
500 m_blue
= m_pen
.GetColour().Blue()/255.0;
501 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
503 switch ( m_pen
.GetCap() )
506 m_cap
= CAIRO_LINE_CAP_ROUND
;
509 case wxCAP_PROJECTING
:
510 m_cap
= CAIRO_LINE_CAP_SQUARE
;
514 m_cap
= CAIRO_LINE_CAP_BUTT
;
518 m_cap
= CAIRO_LINE_CAP_BUTT
;
522 switch ( m_pen
.GetJoin() )
525 m_join
= CAIRO_LINE_JOIN_BEVEL
;
529 m_join
= CAIRO_LINE_JOIN_MITER
;
533 m_join
= CAIRO_LINE_JOIN_ROUND
;
537 m_join
= CAIRO_LINE_JOIN_MITER
;
541 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
542 const double dotted
[] =
544 dashUnit
, dashUnit
+ 2.0
546 static const double short_dashed
[] =
550 static const double dashed
[] =
554 static const double dotted_dashed
[] =
556 9.0 , 6.0 , 3.0 , 3.0
559 switch ( m_pen
.GetStyle() )
561 case wxPENSTYLE_SOLID
:
564 case wxPENSTYLE_DOT
:
565 m_count
= WXSIZEOF(dotted
);
566 m_userLengths
= new double[ m_count
] ;
567 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
568 m_lengths
= m_userLengths
;
571 case wxPENSTYLE_LONG_DASH
:
573 m_count
= WXSIZEOF(dashed
);
576 case wxPENSTYLE_SHORT_DASH
:
577 m_lengths
= short_dashed
;
578 m_count
= WXSIZEOF(short_dashed
);
581 case wxPENSTYLE_DOT_DASH
:
582 m_lengths
= dotted_dashed
;
583 m_count
= WXSIZEOF(dotted_dashed
);
586 case wxPENSTYLE_USER_DASH
:
589 m_count
= m_pen
.GetDashes( &wxdashes
) ;
590 if ((wxdashes
!= NULL
) && (m_count
> 0))
592 m_userLengths
= new double[m_count
] ;
593 for ( int i
= 0 ; i
< m_count
; ++i
)
595 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
597 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
598 m_userLengths
[i
] = dashUnit
+ 2.0 ;
599 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
600 m_userLengths
[i
] = dashUnit
;
603 m_lengths
= m_userLengths
;
606 case wxPENSTYLE_STIPPLE
:
609 wxBitmap* bmp = pen.GetStipple();
610 if ( bmp && bmp->IsOk() )
612 wxDELETE( m_penImage );
613 wxDELETE( m_penBrush );
614 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
615 m_penBrush = new TextureBrush(m_penImage);
616 m_pen->SetBrush( m_penBrush );
622 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
623 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
626 wxDELETE( m_penBrush );
627 HatchStyle style = HatchStyleHorizontal;
628 switch( pen.GetStyle() )
630 case wxPENSTYLE_BDIAGONAL_HATCH :
631 style = HatchStyleBackwardDiagonal;
633 case wxPENSTYLE_CROSSDIAG_HATCH :
634 style = HatchStyleDiagonalCross;
636 case wxPENSTYLE_FDIAGONAL_HATCH :
637 style = HatchStyleForwardDiagonal;
639 case wxPENSTYLE_CROSS_HATCH :
640 style = HatchStyleCross;
642 case wxPENSTYLE_HORIZONTAL_HATCH :
643 style = HatchStyleHorizontal;
645 case wxPENSTYLE_VERTICAL_HATCH :
646 style = HatchStyleVertical;
650 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
651 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
652 m_pen->SetBrush( m_penBrush )
659 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
661 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
662 cairo_set_line_width(ctext
,m_width
);
663 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
664 cairo_set_line_cap(ctext
,m_cap
);
665 cairo_set_line_join(ctext
,m_join
);
666 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
669 //-----------------------------------------------------------------------------
670 // wxCairoBrushData implementation
671 //-----------------------------------------------------------------------------
673 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
674 : wxGraphicsObjectRefData( renderer
)
679 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
680 : wxGraphicsObjectRefData(renderer
)
684 m_red
= brush
.GetColour().Red()/255.0;
685 m_green
= brush
.GetColour().Green()/255.0;
686 m_blue
= brush
.GetColour().Blue()/255.0;
687 m_alpha
= brush
.GetColour().Alpha()/255.0;
689 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
691 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
692 brush.GetColour().Green() , brush.GetColour().Blue() ) );
694 else if ( brush.IsHatch() )
696 HatchStyle style = HatchStyleHorizontal;
697 switch( brush.GetStyle() )
699 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
700 style = HatchStyleBackwardDiagonal;
702 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
703 style = HatchStyleDiagonalCross;
705 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
706 style = HatchStyleForwardDiagonal;
708 case wxBRUSHSTYLE_CROSS_HATCH :
709 style = HatchStyleCross;
711 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
712 style = HatchStyleHorizontal;
714 case wxBRUSHSTYLE_VERTICAL_HATCH :
715 style = HatchStyleVertical;
719 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
720 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
724 wxBitmap* bmp = brush.GetStipple();
725 if ( bmp && bmp->IsOk() )
727 wxDELETE( m_brushImage );
728 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
729 m_brush = new TextureBrush(m_brushImage);
735 wxCairoBrushData::~wxCairoBrushData ()
738 cairo_pattern_destroy(m_brushPattern
);
741 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
743 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
744 if ( m_brushPattern
)
746 cairo_set_source(ctext
,m_brushPattern
);
750 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
754 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
756 // loop over all the stops, they include the beginning and ending ones
757 const unsigned numStops
= stops
.GetCount();
758 for ( unsigned n
= 0; n
< numStops
; n
++ )
760 const wxGraphicsGradientStop stop
= stops
.Item(n
);
762 const wxColour col
= stop
.GetColour();
764 cairo_pattern_add_color_stop_rgba
775 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
776 wxT("Couldn't create cairo pattern"));
780 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
781 wxDouble x2
, wxDouble y2
,
782 const wxGraphicsGradientStops
& stops
)
784 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
786 AddGradientStops(stops
);
790 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
791 wxDouble xc
, wxDouble yc
,
793 const wxGraphicsGradientStops
& stops
)
795 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
797 AddGradientStops(stops
);
800 void wxCairoBrushData::Init()
802 m_brushPattern
= NULL
;
805 //-----------------------------------------------------------------------------
806 // wxCairoFontData implementation
807 //-----------------------------------------------------------------------------
809 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
810 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
812 m_red
= col
.Red()/255.0;
813 m_green
= col
.Green()/255.0;
814 m_blue
= col
.Blue()/255.0;
815 m_alpha
= col
.Alpha()/255.0;
816 m_size
= font
.GetPointSize();
817 m_underlined
= font
.GetUnderlined();
820 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
821 #elif defined(__WXGTK__)
822 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
824 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
825 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
826 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
830 wxCairoFontData::~wxCairoFontData()
833 cairo_font_face_destroy( m_font
);
834 #elif defined(__WXGTK__)
835 pango_font_description_free( m_font
);
840 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
842 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
843 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
845 // the rest is done using Pango layouts
846 #elif defined(__WXMAC__)
847 cairo_set_font_face(ctext
, m_font
);
848 cairo_set_font_size(ctext
, m_size
);
850 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
851 cairo_set_font_size(ctext
, m_size
);
855 //-----------------------------------------------------------------------------
856 // wxCairoPathData implementation
857 //-----------------------------------------------------------------------------
859 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
860 : wxGraphicsPathData(renderer
)
864 m_pathContext
= pathcontext
;
868 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
869 m_pathContext
= cairo_create(surface
);
870 cairo_surface_destroy (surface
);
874 wxCairoPathData::~wxCairoPathData()
876 cairo_destroy(m_pathContext
);
879 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
881 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
882 cairo_t
* pathcontext
= cairo_create(surface
);
883 cairo_surface_destroy (surface
);
885 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
886 cairo_append_path(pathcontext
, path
);
887 cairo_path_destroy(path
);
888 return new wxCairoPathData( GetRenderer() ,pathcontext
);
892 void* wxCairoPathData::GetNativePath() const
894 return cairo_copy_path(m_pathContext
) ;
897 void wxCairoPathData::UnGetNativePath(void *p
) const
899 cairo_path_destroy((cairo_path_t
*)p
);
906 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
908 cairo_move_to(m_pathContext
,x
,y
);
911 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
913 cairo_line_to(m_pathContext
,x
,y
);
916 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
918 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
919 cairo_append_path(m_pathContext
, p
);
923 void wxCairoPathData::CloseSubpath()
925 cairo_close_path(m_pathContext
);
928 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
930 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
933 // gets the last point of the current path, (0,0) if not yet set
934 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
937 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
944 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
946 // as clockwise means positive in our system (y pointing downwards)
947 // TODO make this interpretation dependent of the
949 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
950 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
952 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
955 // transforms each point of this path by the matrix
956 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
958 // as we don't have a true path object, we have to apply the inverse
959 // matrix to the context
960 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
961 cairo_matrix_invert( &m
);
962 cairo_transform(m_pathContext
,&m
);
965 // gets the bounding box enclosing all points (possibly including control points)
966 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
970 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
994 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
996 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
997 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
1000 //-----------------------------------------------------------------------------
1001 // wxCairoMatrixData implementation
1002 //-----------------------------------------------------------------------------
1004 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
1005 : wxGraphicsMatrixData(renderer
)
1011 wxCairoMatrixData::~wxCairoMatrixData()
1016 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
1018 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
1021 // concatenates the matrix
1022 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1024 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
1027 // sets the matrix to the respective values
1028 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1029 wxDouble tx
, wxDouble ty
)
1031 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
1034 // gets the component valuess of the matrix
1035 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1036 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1038 if (a
) *a
= m_matrix
.xx
;
1039 if (b
) *b
= m_matrix
.yx
;
1040 if (c
) *c
= m_matrix
.xy
;
1041 if (d
) *d
= m_matrix
.yy
;
1042 if (tx
) *tx
= m_matrix
.x0
;
1043 if (ty
) *ty
= m_matrix
.y0
;
1046 // makes this the inverse matrix
1047 void wxCairoMatrixData::Invert()
1049 cairo_matrix_invert( &m_matrix
);
1052 // returns true if the elements of the transformation matrix are equal ?
1053 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1055 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1057 m_matrix
.xx
== tm
->xx
&&
1058 m_matrix
.yx
== tm
->yx
&&
1059 m_matrix
.xy
== tm
->xy
&&
1060 m_matrix
.yy
== tm
->yy
&&
1061 m_matrix
.x0
== tm
->x0
&&
1062 m_matrix
.y0
== tm
->y0
) ;
1065 // return true if this is the identity matrix
1066 bool wxCairoMatrixData::IsIdentity() const
1068 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1069 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1076 // add the translation to this matrix
1077 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1079 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1082 // add the scale to this matrix
1083 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1085 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1088 // add the rotation to this matrix (radians)
1089 void wxCairoMatrixData::Rotate( wxDouble angle
)
1091 cairo_matrix_rotate( &m_matrix
, angle
) ;
1095 // apply the transforms
1098 // applies that matrix to the point
1099 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1101 double lx
= *x
, ly
= *y
;
1102 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1107 // applies the matrix except for translations
1108 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1110 double lx
= *dx
, ly
= *dy
;
1111 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1116 // returns the native representation
1117 void * wxCairoMatrixData::GetNativeMatrix() const
1119 return (void*) &m_matrix
;
1122 // ----------------------------------------------------------------------------
1123 // wxCairoBitmap implementation
1124 // ----------------------------------------------------------------------------
1126 int wxCairoBitmapData::InitBuffer(int width
, int height
, cairo_format_t format
)
1128 wxUnusedVar(format
); // Only really unused with Cairo < 1.6.
1130 // Determine the stride: use cairo_format_stride_for_width() if available
1131 // but fall back to 4*width for the earlier versions as this is what that
1132 // function always returns, even in latest Cairo, anyhow.
1134 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1135 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1137 stride
= cairo_format_stride_for_width(format
, width
);
1139 // All our code would totally break if stride were not a multiple of 4
1140 // so ensure this is the case.
1143 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1145 stride
+= 4 - stride
% 4;
1154 m_buffer
= new unsigned char[height
*stride
];
1159 void wxCairoBitmapData::InitSurface(cairo_format_t format
, int stride
)
1161 m_surface
= cairo_image_surface_create_for_data(
1162 m_buffer
, format
, m_width
, m_height
, stride
);
1163 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1166 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1167 wxGraphicsObjectRefData( renderer
)
1170 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1173 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1175 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1177 #ifdef wxHAS_RAW_BITMAP
1178 // Create a surface object and copy the bitmap pixel data to it. if the
1179 // image has alpha (or a mask represented as alpha) then we'll use a
1180 // different format and iterator than if it doesn't...
1181 const cairo_format_t bufferFormat
= bmp
.GetDepth() == 32
1185 ? CAIRO_FORMAT_ARGB32
1186 : CAIRO_FORMAT_RGB24
;
1188 int stride
= InitBuffer(bmp
.GetWidth(), bmp
.GetHeight(), bufferFormat
);
1192 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1193 wxUint32
* data
= (wxUint32
*)m_buffer
;
1195 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1197 // use the bitmap's alpha
1198 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1199 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1201 wxAlphaPixelData::Iterator
p(pixData
);
1202 for (int y
=0; y
<bh
; y
++)
1204 wxAlphaPixelData::Iterator rowStart
= p
;
1205 wxUint32
* const rowStartDst
= data
;
1206 for (int x
=0; x
<bw
; x
++)
1208 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1209 // with alpha in the upper 8 bits, then red, then green, then
1210 // blue. The 32-bit quantities are stored native-endian.
1211 // Pre-multiplied alpha is used.
1212 unsigned char alpha
= p
.Alpha();
1216 *data
= ( alpha
<< 24
1217 | (p
.Red() * alpha
/255) << 16
1218 | (p
.Green() * alpha
/255) << 8
1219 | (p
.Blue() * alpha
/255) );
1224 data
= rowStartDst
+ stride
/ 4;
1226 p
.OffsetY(pixData
, 1);
1231 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1232 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1234 wxNativePixelData::Iterator
p(pixData
);
1235 for (int y
=0; y
<bh
; y
++)
1237 wxNativePixelData::Iterator rowStart
= p
;
1238 wxUint32
* const rowStartDst
= data
;
1239 for (int x
=0; x
<bw
; x
++)
1241 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1242 // the upper 8 bits unused. Red, Green, and Blue are stored in
1243 // the remaining 24 bits in that order. The 32-bit quantities
1244 // are stored native-endian.
1245 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1250 data
= rowStartDst
+ stride
/ 4;
1252 p
.OffsetY(pixData
, 1);
1256 // if there is a mask, set the alpha bytes in the target buffer to
1257 // fully transparent or fully opaque
1258 if (bmpSource
.GetMask())
1260 wxBitmap bmpMask
= bmpSource
.GetMaskBitmap();
1261 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1262 data
= (wxUint32
*)m_buffer
;
1263 wxNativePixelData
pixData(bmpMask
, wxPoint(0,0), wxSize(bw
, bh
));
1264 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to mask bitmap data."));
1266 wxNativePixelData::Iterator
p(pixData
);
1267 for (int y
=0; y
<bh
; y
++)
1269 wxNativePixelData::Iterator rowStart
= p
;
1270 wxUint32
* const rowStartDst
= data
;
1271 for (int x
=0; x
<bw
; x
++)
1273 if (p
.Red()+p
.Green()+p
.Blue() == 0)
1276 *data
= (wxALPHA_OPAQUE
<< 24) | (*data
& 0x00FFFFFF);
1281 data
= rowStartDst
+ stride
/ 4;
1283 p
.OffsetY(pixData
, 1);
1288 InitSurface(bufferFormat
, stride
);
1289 #endif // wxHAS_RAW_BITMAP
1294 // Helper functions for dealing with alpha pre-multiplication.
1298 inline unsigned char Premultiply(unsigned char alpha
, unsigned char data
)
1300 return alpha
? (data
* alpha
)/0xff : data
;
1303 inline unsigned char Unpremultiply(unsigned char alpha
, unsigned char data
)
1305 return alpha
? (data
* 0xff)/alpha
: data
;
1308 } // anonymous namespace
1310 wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer
* renderer
,
1311 const wxImage
& image
)
1312 : wxGraphicsObjectRefData(renderer
)
1314 const cairo_format_t bufferFormat
= image
.HasAlpha()
1315 ? CAIRO_FORMAT_ARGB32
1316 : CAIRO_FORMAT_RGB24
;
1318 int stride
= InitBuffer(image
.GetWidth(), image
.GetHeight(), bufferFormat
);
1320 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1321 // values and not bytes becase Cairo always works with buffers in native
1323 wxUint32
* dst
= reinterpret_cast<wxUint32
*>(m_buffer
);
1324 const unsigned char* src
= image
.GetData();
1326 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1328 const unsigned char* alpha
= image
.GetAlpha();
1330 for ( int y
= 0; y
< m_height
; y
++ )
1332 wxUint32
* const rowStartDst
= dst
;
1334 for ( int x
= 0; x
< m_width
; x
++ )
1336 const unsigned char a
= *alpha
++;
1339 Premultiply(a
, src
[0]) << 16 |
1340 Premultiply(a
, src
[1]) << 8 |
1341 Premultiply(a
, src
[2]);
1345 dst
= rowStartDst
+ stride
/ 4;
1350 for ( int y
= 0; y
< m_height
; y
++ )
1352 wxUint32
* const rowStartDst
= dst
;
1354 for ( int x
= 0; x
< m_width
; x
++ )
1356 *dst
++ = src
[0] << 16 |
1362 dst
= rowStartDst
+ stride
/ 4;
1366 InitSurface(bufferFormat
, stride
);
1369 wxImage
wxCairoBitmapData::ConvertToImage() const
1371 wxImage
image(m_width
, m_height
, false /* don't clear */);
1373 // Get the surface type and format.
1374 wxCHECK_MSG( cairo_surface_get_type(m_surface
) == CAIRO_SURFACE_TYPE_IMAGE
,
1376 wxS("Can't convert non-image surface to image.") );
1378 switch ( cairo_image_surface_get_format(m_surface
) )
1380 case CAIRO_FORMAT_ARGB32
:
1384 case CAIRO_FORMAT_RGB24
:
1385 // Nothing to do, we don't use alpha by default.
1388 case CAIRO_FORMAT_A8
:
1389 case CAIRO_FORMAT_A1
:
1390 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1394 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1398 // Prepare for copying data.
1399 const wxUint32
* src
= (wxUint32
*)cairo_image_surface_get_data(m_surface
);
1400 wxCHECK_MSG( src
, wxNullImage
, wxS("Failed to get Cairo surface data.") );
1402 int stride
= cairo_image_surface_get_stride(m_surface
);
1403 wxCHECK_MSG( stride
> 0, wxNullImage
,
1404 wxS("Failed to get Cairo surface stride.") );
1406 // As we work with wxUint32 pointers and not char ones, we need to adjust
1407 // the stride accordingly. This should be lossless as the stride must be a
1408 // multiple of pixel size.
1409 wxASSERT_MSG( !(stride
% sizeof(wxUint32
)), wxS("Unexpected stride.") );
1410 stride
/= sizeof(wxUint32
);
1412 unsigned char* dst
= image
.GetData();
1413 unsigned char *alpha
= image
.GetAlpha();
1416 // We need to also copy alpha and undo the pre-multiplication as Cairo
1417 // stores pre-multiplied values in this format while wxImage does not.
1418 for ( int y
= 0; y
< m_height
; y
++ )
1420 const wxUint32
* const rowStart
= src
;
1421 for ( int x
= 0; x
< m_width
; x
++ )
1423 const wxUint32 argb
= *src
++;
1425 *alpha
++ = (argb
& 0xff000000) >> 24;
1427 // Copy the RGB data undoing the pre-multiplication.
1428 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x00ff0000) >> 16);
1429 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x0000ff00) >> 8);
1430 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x000000ff));
1433 src
= rowStart
+ stride
;
1438 // Things are pretty simple in this case, just copy RGB bytes.
1439 for ( int y
= 0; y
< m_height
; y
++ )
1441 const wxUint32
* const rowStart
= src
;
1442 for ( int x
= 0; x
< m_width
; x
++ )
1444 const wxUint32 argb
= *src
++;
1446 *dst
++ = (argb
& 0x00ff0000) >> 16;
1447 *dst
++ = (argb
& 0x0000ff00) >> 8;
1448 *dst
++ = (argb
& 0x000000ff);
1451 src
= rowStart
+ stride
;
1458 #endif // wxUSE_IMAGE
1460 wxCairoBitmapData::~wxCairoBitmapData()
1463 cairo_pattern_destroy(m_pattern
);
1466 cairo_surface_destroy(m_surface
);
1471 // ----------------------------------------------------------------------------
1472 // wxGraphicsBitmap implementation
1473 // ----------------------------------------------------------------------------
1477 wxImage
wxGraphicsBitmap::ConvertToImage() const
1479 const wxCairoBitmapData
* const
1480 data
= static_cast<wxCairoBitmapData
*>(GetGraphicsData());
1482 return data
? data
->ConvertToImage() : wxNullImage
;
1485 #endif // wxUSE_IMAGE
1487 //-----------------------------------------------------------------------------
1488 // wxCairoContext implementation
1489 //-----------------------------------------------------------------------------
1491 class wxCairoOffsetHelper
1494 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1499 cairo_translate( m_ctx
, 0.5, 0.5 );
1501 ~wxCairoOffsetHelper( )
1504 cairo_translate( m_ctx
, -0.5, -0.5 );
1511 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1512 : wxGraphicsContext(renderer
)
1515 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1516 // text rendering when printing using Cairo. Switch it to MM_TEXT
1517 // map mode to avoid this problem.
1518 HDC hdc
= (HDC
)dc
.GetHDC();
1519 ::SetMapMode(hdc
, MM_TEXT
);
1520 m_mswSurface
= cairo_win32_printing_surface_create(hdc
);
1521 Init( cairo_create(m_mswSurface
) );
1525 const wxDCImpl
*impl
= dc
.GetImpl();
1526 Init( (cairo_t
*) impl
->GetCairoContext() );
1528 wxSize sz
= dc
.GetSize();
1532 wxPoint org
= dc
.GetDeviceOrigin();
1533 cairo_translate( m_context
, org
.x
, org
.y
);
1536 dc
.GetUserScale( &sx
, &sy
);
1538 // TODO: Determine if these fixes are needed on other platforms too.
1539 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1540 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1544 dc
.GetLogicalScale( &lsx
, &lsy
);
1548 cairo_scale( m_context
, sx
, sy
);
1550 org
= dc
.GetLogicalOrigin();
1551 cairo_translate( m_context
, -org
.x
, -org
.y
);
1554 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1555 : wxGraphicsContext(renderer
)
1558 dc
.GetSize( &width
, &height
);
1562 m_enableOffset
= true;
1565 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1566 Init( cairo_create(m_mswSurface
) );
1570 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1571 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1574 wxGraphicsMatrix matrix
= CreateMatrix();
1576 wxPoint org
= dc
.GetDeviceOrigin();
1577 matrix
.Translate( org
.x
, org
.y
);
1579 org
= dc
.GetLogicalOrigin();
1580 matrix
.Translate( -org
.x
, -org
.y
);
1583 dc
.GetUserScale( &sx
, &sy
);
1584 matrix
.Scale( sx
, sy
);
1586 ConcatTransform( matrix
);
1591 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1592 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1593 Init( cairo_create( surface
) );
1594 cairo_surface_destroy( surface
);
1598 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1599 : wxGraphicsContext(renderer
)
1602 dc
.GetSize( &width
, &height
);
1606 m_enableOffset
= true;
1609 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1610 Init( cairo_create(m_mswSurface
) );
1614 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1615 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1618 wxGraphicsMatrix matrix
= CreateMatrix();
1620 wxPoint org
= dc
.GetDeviceOrigin();
1621 matrix
.Translate( org
.x
, org
.y
);
1623 org
= dc
.GetLogicalOrigin();
1624 matrix
.Translate( -org
.x
, -org
.y
);
1627 dc
.GetUserScale( &sx
, &sy
);
1628 matrix
.Scale( sx
, sy
);
1630 ConcatTransform( matrix
);
1635 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1636 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1637 Init( cairo_create( surface
) );
1638 cairo_surface_destroy( surface
);
1643 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1644 : wxGraphicsContext(renderer
)
1646 Init( gdk_cairo_create( drawable
) );
1649 gdk_drawable_get_size( drawable
, &width
, &height
);
1656 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1657 : wxGraphicsContext(renderer
)
1659 m_mswSurface
= cairo_win32_surface_create(handle
);
1660 Init( cairo_create(m_mswSurface
) );
1667 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1668 : wxGraphicsContext(renderer
)
1675 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1676 : wxGraphicsContext(renderer
)
1678 m_enableOffset
= true;
1680 // something along these lines (copied from dcclient)
1682 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1683 // code should still be able to create wxClientDCs for them, so we will
1684 // use the parent window here then.
1685 if (window
->m_wxwindow
== NULL
)
1687 window
= window
->GetParent();
1690 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1692 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1694 wxSize sz
= window
->GetSize();
1700 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1701 Init(cairo_create(m_mswSurface
));
1706 wxCairoContext::wxCairoContext(wxGraphicsRenderer
* renderer
) :
1707 wxGraphicsContext(renderer
)
1712 wxCairoContext::~wxCairoContext()
1718 cairo_destroy(m_context
);
1722 cairo_surface_destroy(m_mswSurface
);
1726 void wxCairoContext::Init(cairo_t
*context
)
1728 m_context
= context
;
1734 void wxCairoContext::Clip( const wxRegion
& region
)
1736 // Create a path with all the rectangles in the region
1737 wxGraphicsPath path
= GetRenderer()->CreatePath();
1738 wxRegionIterator
ri(region
);
1741 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1745 // Put it in the context
1746 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1747 cairo_append_path(m_context
, cp
);
1749 // clip to that path
1750 cairo_clip(m_context
);
1751 path
.UnGetNativePath(cp
);
1754 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1756 // Create a path with this rectangle
1757 wxGraphicsPath path
= GetRenderer()->CreatePath();
1758 path
.AddRectangle(x
,y
,w
,h
);
1760 // Put it in the context
1761 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1762 cairo_append_path(m_context
, cp
);
1764 // clip to that path
1765 cairo_clip(m_context
);
1766 path
.UnGetNativePath(cp
);
1769 void wxCairoContext::ResetClip()
1771 cairo_reset_clip(m_context
);
1775 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1777 if ( !m_pen
.IsNull() )
1779 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1780 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1781 cairo_append_path(m_context
,cp
);
1782 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1783 cairo_stroke(m_context
);
1784 path
.UnGetNativePath(cp
);
1788 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1790 if ( !m_brush
.IsNull() )
1792 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1793 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1794 cairo_append_path(m_context
,cp
);
1795 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1796 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1797 cairo_fill(m_context
);
1798 path
.UnGetNativePath(cp
);
1802 void wxCairoContext::Rotate( wxDouble angle
)
1804 cairo_rotate(m_context
,angle
);
1807 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1809 cairo_translate(m_context
,dx
,dy
);
1812 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1814 cairo_scale(m_context
,xScale
,yScale
);
1817 // concatenates this transform with the current transform of this context
1818 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1820 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1823 // sets the transform of this context
1824 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1826 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1829 // gets the matrix of this context
1830 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1832 wxGraphicsMatrix matrix
= CreateMatrix();
1833 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1839 void wxCairoContext::PushState()
1841 cairo_save(m_context
);
1844 void wxCairoContext::PopState()
1846 cairo_restore(m_context
);
1849 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1851 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1852 DrawBitmap(bitmap
, x
, y
, w
, h
);
1856 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1860 // In case we're scaling the image by using a width and height different
1861 // than the bitmap's size create a pattern transformation on the surface and
1862 // draw the transformed pattern.
1863 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1864 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1865 wxSize size
= data
->GetSize();
1867 wxDouble scaleX
= w
/ size
.GetWidth();
1868 wxDouble scaleY
= h
/ size
.GetHeight();
1870 // prepare to draw the image
1871 cairo_translate(m_context
, x
, y
);
1872 cairo_scale(m_context
, scaleX
, scaleY
);
1873 cairo_set_source(m_context
, pattern
);
1874 // use the original size here since the context is scaled already...
1875 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1876 // fill the rectangle using the pattern
1877 cairo_fill(m_context
);
1882 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1884 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1885 // start using the Cairo backend on other platforms then we may need to
1886 // fiddle with this...
1887 DrawBitmap(icon
, x
, y
, w
, h
);
1891 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1893 wxCHECK_RET( !m_font
.IsNull(),
1894 wxT("wxCairoContext::DrawText - no valid font set") );
1899 const wxCharBuffer data
= str
.utf8_str();
1903 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1906 size_t datalen
= strlen(data
);
1908 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1909 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1910 pango_layout_set_font_description( layout
, font_data
->GetFont());
1911 pango_layout_set_text(layout
, data
, datalen
);
1913 if (font_data
->GetUnderlined())
1915 PangoAttrList
*attrs
= pango_attr_list_new();
1916 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1917 pango_attr_list_insert(attrs
, attr
);
1918 pango_layout_set_attributes(layout
, attrs
);
1919 pango_attr_list_unref(attrs
);
1922 cairo_move_to(m_context
, x
, y
);
1923 pango_cairo_show_layout (m_context
, layout
);
1925 g_object_unref (layout
);
1927 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1928 // the position we move to by the ascent.
1929 cairo_font_extents_t fe
;
1930 cairo_font_extents(m_context
, &fe
);
1931 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1933 cairo_show_text(m_context
, data
);
1937 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1938 wxDouble
*descent
, wxDouble
*externalLeading
) const
1940 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1948 if ( externalLeading
)
1949 *externalLeading
= 0;
1957 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1958 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1959 const wxCharBuffer data
= str
.utf8_str();
1964 pango_layout_set_text( layout
, data
, strlen(data
) );
1965 pango_layout_get_pixel_size (layout
, &w
, &h
);
1972 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1973 int baseline
= pango_layout_iter_get_baseline(iter
);
1974 pango_layout_iter_free(iter
);
1975 *descent
= h
- PANGO_PIXELS(baseline
);
1977 g_object_unref (layout
);
1979 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1983 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1984 cairo_text_extents_t te
;
1985 cairo_text_extents(m_context
, buf
, &te
);
1989 if (height
|| descent
|| externalLeading
)
1991 cairo_font_extents_t fe
;
1992 cairo_font_extents(m_context
, &fe
);
1994 // some backends have negative descents
1996 if ( fe
.descent
< 0 )
1997 fe
.descent
= -fe
.descent
;
1999 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
2001 // some backends are broken re height ... (eg currently ATSUI)
2002 fe
.height
= fe
.ascent
+ fe
.descent
;
2006 *height
= fe
.height
;
2008 *descent
= fe
.descent
;
2009 if ( externalLeading
)
2010 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
2015 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2018 widths
.Add(0, text
.length());
2020 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
2028 void * wxCairoContext::GetNativeContext()
2033 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
2035 if (m_antialias
== antialias
)
2038 m_antialias
= antialias
;
2040 cairo_antialias_t antialiasMode
;
2043 case wxANTIALIAS_DEFAULT
:
2044 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
2046 case wxANTIALIAS_NONE
:
2047 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
2052 cairo_set_antialias(m_context
, antialiasMode
);
2056 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
2062 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
2064 if ( m_composition
== op
)
2068 cairo_operator_t cop
;
2071 case wxCOMPOSITION_CLEAR
:
2072 cop
= CAIRO_OPERATOR_CLEAR
;
2074 case wxCOMPOSITION_SOURCE
:
2075 cop
= CAIRO_OPERATOR_SOURCE
;
2077 case wxCOMPOSITION_OVER
:
2078 cop
= CAIRO_OPERATOR_OVER
;
2080 case wxCOMPOSITION_IN
:
2081 cop
= CAIRO_OPERATOR_IN
;
2083 case wxCOMPOSITION_OUT
:
2084 cop
= CAIRO_OPERATOR_OUT
;
2086 case wxCOMPOSITION_ATOP
:
2087 cop
= CAIRO_OPERATOR_ATOP
;
2089 case wxCOMPOSITION_DEST
:
2090 cop
= CAIRO_OPERATOR_DEST
;
2092 case wxCOMPOSITION_DEST_OVER
:
2093 cop
= CAIRO_OPERATOR_DEST_OVER
;
2095 case wxCOMPOSITION_DEST_IN
:
2096 cop
= CAIRO_OPERATOR_DEST_IN
;
2098 case wxCOMPOSITION_DEST_OUT
:
2099 cop
= CAIRO_OPERATOR_DEST_OUT
;
2101 case wxCOMPOSITION_DEST_ATOP
:
2102 cop
= CAIRO_OPERATOR_DEST_ATOP
;
2104 case wxCOMPOSITION_XOR
:
2105 cop
= CAIRO_OPERATOR_XOR
;
2107 case wxCOMPOSITION_ADD
:
2108 cop
= CAIRO_OPERATOR_ADD
;
2113 cairo_set_operator(m_context
, cop
);
2117 void wxCairoContext::BeginLayer(wxDouble opacity
)
2119 m_layerOpacities
.push_back(opacity
);
2120 cairo_push_group(m_context
);
2123 void wxCairoContext::EndLayer()
2125 float opacity
= m_layerOpacities
.back();
2126 m_layerOpacities
.pop_back();
2127 cairo_pop_group_to_source(m_context
);
2128 cairo_paint_with_alpha(m_context
,opacity
);
2131 //-----------------------------------------------------------------------------
2132 // wxCairoRenderer declaration
2133 //-----------------------------------------------------------------------------
2135 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
2138 wxCairoRenderer() {}
2140 virtual ~wxCairoRenderer() {}
2144 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2145 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2146 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2148 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2150 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2152 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2153 #endif // wxUSE_IMAGE
2155 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2157 virtual wxGraphicsContext
* CreateMeasuringContext();
2159 #if wxUSE_ENH_METAFILE
2160 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
2165 virtual wxGraphicsPath
CreatePath();
2169 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2170 wxDouble tx
=0.0, wxDouble ty
=0.0);
2173 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2175 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2177 virtual wxGraphicsBrush
2178 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2179 wxDouble x2
, wxDouble y2
,
2180 const wxGraphicsGradientStops
& stops
);
2182 virtual wxGraphicsBrush
2183 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2184 wxDouble xc
, wxDouble yc
,
2186 const wxGraphicsGradientStops
& stops
);
2189 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2191 // create a native bitmap representation
2192 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
2194 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2195 #endif // wxUSE_IMAGE
2197 // create a graphics bitmap from a native bitmap
2198 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2200 // create a subimage from a native image representation
2201 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
2204 bool EnsureIsLoaded();
2207 friend class wxCairoModule
;
2210 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
2213 //-----------------------------------------------------------------------------
2214 // wxCairoRenderer implementation
2215 //-----------------------------------------------------------------------------
2217 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
2219 static wxCairoRenderer gs_cairoGraphicsRenderer
;
2220 // temporary hack to allow creating a cairo context on any platform
2221 extern wxGraphicsRenderer
* gCairoRenderer
;
2222 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
2224 bool wxCairoRenderer::EnsureIsLoaded()
2228 return wxCairoInit();
2234 void wxCairoRenderer::Load()
2239 void wxCairoRenderer::Unload()
2244 // call EnsureIsLoaded() and return returnOnFail value if it fails
2245 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2246 if ( !EnsureIsLoaded() ) \
2247 return (returnOnFail)
2249 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
2251 ENSURE_LOADED_OR_RETURN(NULL
);
2252 return new wxCairoContext(this,dc
);
2255 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
2257 ENSURE_LOADED_OR_RETURN(NULL
);
2258 return new wxCairoContext(this,dc
);
2261 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
2263 ENSURE_LOADED_OR_RETURN(NULL
);
2265 const wxDCImpl
*impl
= dc
.GetImpl();
2266 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
2268 return new wxCairoContext(this,dc
);
2271 return new wxCairoContext(this,dc
);
2275 #if wxUSE_ENH_METAFILE
2276 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& WXUNUSED(dc
) )
2278 ENSURE_LOADED_OR_RETURN(NULL
);
2284 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
2286 ENSURE_LOADED_OR_RETURN(NULL
);
2288 return new wxCairoContext(this,(HDC
)context
);
2290 return new wxCairoContext(this,(cairo_t
*)context
);
2295 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
2297 ENSURE_LOADED_OR_RETURN(NULL
);
2299 return new wxCairoContext(this,(GdkDrawable
*)window
);
2301 wxUnusedVar(window
);
2307 wxGraphicsContext
* wxCairoRenderer::CreateContextFromImage(wxImage
& image
)
2309 return new wxCairoImageContext(this, image
);
2311 #endif // wxUSE_IMAGE
2313 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
2315 ENSURE_LOADED_OR_RETURN(NULL
);
2317 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2323 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
2325 ENSURE_LOADED_OR_RETURN(NULL
);
2326 return new wxCairoContext(this, window
);
2331 wxGraphicsPath
wxCairoRenderer::CreatePath()
2333 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2334 wxGraphicsPath path
;
2335 path
.SetRefData( new wxCairoPathData(this) );
2342 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2343 wxDouble tx
, wxDouble ty
)
2346 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2348 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
2349 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2354 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
2356 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2357 if ( !pen
.IsOk() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
2358 return wxNullGraphicsPen
;
2362 p
.SetRefData(new wxCairoPenData( this, pen
));
2367 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
2369 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2370 if ( !brush
.IsOk() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
2371 return wxNullGraphicsBrush
;
2375 p
.SetRefData(new wxCairoBrushData( this, brush
));
2381 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2382 wxDouble x2
, wxDouble y2
,
2383 const wxGraphicsGradientStops
& stops
)
2385 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2387 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2388 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2394 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2395 wxDouble xc
, wxDouble yc
, wxDouble r
,
2396 const wxGraphicsGradientStops
& stops
)
2398 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2400 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2401 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
2407 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2409 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2413 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2417 return wxNullGraphicsFont
;
2420 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2422 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2426 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2430 return wxNullGraphicsBitmap
;
2435 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromImage(const wxImage
& image
)
2437 wxGraphicsBitmap bmp
;
2439 ENSURE_LOADED_OR_RETURN(bmp
);
2443 bmp
.SetRefData(new wxCairoBitmapData(this, image
));
2449 #endif // wxUSE_IMAGE
2451 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2453 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2454 if ( bitmap
!= NULL
)
2457 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2461 return wxNullGraphicsBitmap
;
2465 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2466 wxDouble
WXUNUSED(x
),
2467 wxDouble
WXUNUSED(y
),
2468 wxDouble
WXUNUSED(w
),
2469 wxDouble
WXUNUSED(h
))
2471 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2472 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2473 return wxNullGraphicsBitmap
;
2476 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2478 return &gs_cairoGraphicsRenderer
;
2481 #else // !wxUSE_CAIRO
2483 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2488 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2490 // MSW and OS X have their own native default renderers, but the other ports
2491 // use Cairo by default
2492 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2493 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2495 return GetCairoRenderer();
2497 #endif // !(__WXMSW__ || __WXOSX__)
2499 #endif // wxUSE_GRAPHICS_CONTEXT