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"
36 #include "wx/window.h"
39 #include "wx/private/graphics.h"
40 #include "wx/rawbmp.h"
41 #include "wx/vector.h"
45 //-----------------------------------------------------------------------------
46 // device context implementation
48 // more and more of the dc functionality should be implemented by calling
49 // the appropricate wxCairoContext, but we will have to do that step by step
50 // also coordinate conversions should be moved to native matrix ops
51 //-----------------------------------------------------------------------------
53 // we always stock two context states, one at entry, to be able to preserve the
54 // state we were called with, the other one after changing to HI Graphics orientation
55 // (this one is used for getting back clippings etc)
57 //-----------------------------------------------------------------------------
58 // wxGraphicsPath implementation
59 //-----------------------------------------------------------------------------
61 // TODO remove this dependency (gdiplus needs the macros)
64 #define max(a,b) (((a) > (b)) ? (a) : (b))
68 #define min(a,b) (((a) < (b)) ? (a) : (b))
73 #include <cairo-win32.h>
74 // Notice that the order is important: cairo-win32.h includes windows.h which
75 // pollutes the global name space with macros so include our own header which
76 // #undefines them after it.
77 #include "wx/msw/private.h"
82 #include "wx/fontutil.h"
83 #include "wx/gtk/dc.h"
87 #include "wx/osx/private.h"
88 #include <cairo-quartz.h>
89 #include <cairo-atsui.h>
92 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
95 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
98 virtual wxGraphicsObjectRefData
*Clone() const;
101 // These are the path primitives from which everything else can be constructed
104 // begins a new subpath at (x,y)
105 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
107 // adds a straight line from the current point to (x,y)
108 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
110 // adds a cubic Bezier curve from the current point, using two control points and an end point
111 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
114 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
115 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
117 // gets the last point of the current path, (0,0) if not yet set
118 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
121 virtual void AddPath( const wxGraphicsPathData
* path
);
123 // closes the current sub-path
124 virtual void CloseSubpath();
127 // These are convenience functions which - if not available natively will be assembled
128 // using the primitives from above
133 // appends a rectangle as a new closed subpath
134 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
135 // appends an ellipsis as a new closed subpath fitting the passed rectangle
136 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
138 // 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)
139 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
142 // returns the native path
143 virtual void * GetNativePath() const ;
145 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
146 virtual void UnGetNativePath(void *p
) const;
148 // transforms each point of this path by the matrix
149 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
151 // gets the bounding box enclosing all points (possibly including control points)
152 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
154 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
157 cairo_t
* m_pathContext
;
160 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
163 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
164 virtual ~wxCairoMatrixData() ;
166 virtual wxGraphicsObjectRefData
*Clone() const ;
168 // concatenates the matrix
169 virtual void Concat( const wxGraphicsMatrixData
*t
);
171 // sets the matrix to the respective values
172 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
173 wxDouble tx
=0.0, wxDouble ty
=0.0);
175 // gets the component valuess of the matrix
176 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
177 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
179 // makes this the inverse matrix
180 virtual void Invert();
182 // returns true if the elements of the transformation matrix are equal ?
183 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
185 // return true if this is the identity matrix
186 virtual bool IsIdentity() const;
192 // add the translation to this matrix
193 virtual void Translate( wxDouble dx
, wxDouble dy
);
195 // add the scale to this matrix
196 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
198 // add the rotation to this matrix (radians)
199 virtual void Rotate( wxDouble angle
);
202 // apply the transforms
205 // applies that matrix to the point
206 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
208 // applies the matrix except for translations
209 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
211 // returns the native representation
212 virtual void * GetNativeMatrix() const;
214 cairo_matrix_t m_matrix
;
217 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
220 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
225 virtual void Apply( wxGraphicsContext
* context
);
226 virtual wxDouble
GetWidth() { return m_width
; }
236 cairo_line_cap_t m_cap
;
237 cairo_line_join_t m_join
;
240 const double *m_lengths
;
241 double *m_userLengths
;
245 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
248 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
251 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
252 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
253 ~wxCairoBrushData ();
255 virtual void Apply( wxGraphicsContext
* context
);
257 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
258 wxDouble x2
, wxDouble y2
,
259 const wxGraphicsGradientStops
& stops
);
260 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
261 wxDouble xc
, wxDouble yc
, wxDouble radius
,
262 const wxGraphicsGradientStops
& stops
);
267 // common part of Create{Linear,Radial}GradientBrush()
268 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
276 cairo_pattern_t
* m_brushPattern
;
279 class wxCairoFontData
: public wxGraphicsObjectRefData
282 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
283 wxCairoFontData(wxGraphicsRenderer
* renderer
,
285 const wxString
& facename
,
287 const wxColour
& col
);
290 virtual bool Apply( wxGraphicsContext
* context
);
292 const wxFont
& GetFont() const { return m_wxfont
; }
295 void InitColour(const wxColour
& col
);
296 void InitFontComponents(const wxString
& facename
,
297 cairo_font_slant_t slant
,
298 cairo_font_weight_t weight
);
306 cairo_font_face_t
*m_font
;
307 #elif defined(__WXGTK__)
311 // These members are used when the font is created from its face name and
312 // flags (and not from wxFont) and also even when creating it from wxFont
313 // on the platforms not covered above.
315 // Notice that we can't use cairo_font_face_t instead of storing those,
316 // even though it would be simpler and need less #ifdefs, because
317 // cairo_toy_font_face_create() that we'd need to create it is only
318 // available in Cairo 1.8 and we require just 1.2 currently. If we do drop
319 // support for < 1.8 versions in the future it would be definitely better
320 // to use cairo_toy_font_face_create() instead.
321 wxCharBuffer m_fontName
;
322 cairo_font_slant_t m_slant
;
323 cairo_font_weight_t m_weight
;
326 class wxCairoBitmapData
: public wxGraphicsObjectRefData
329 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
331 wxCairoBitmapData(wxGraphicsRenderer
* renderer
, const wxImage
& image
);
332 #endif // wxUSE_IMAGE
333 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
334 ~wxCairoBitmapData();
336 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
337 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
338 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
341 wxImage
ConvertToImage() const;
342 #endif // wxUSE_IMAGE
345 // Allocate m_buffer for the bitmap of the given size in the given format.
347 // Returns the stride used for the buffer.
348 int InitBuffer(int width
, int height
, cairo_format_t format
);
350 // Really create the surface using the buffer (which was supposed to be
351 // filled since InitBuffer() call).
352 void InitSurface(cairo_format_t format
, int stride
);
355 cairo_surface_t
* m_surface
;
356 cairo_pattern_t
* m_pattern
;
359 unsigned char* m_buffer
;
362 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
365 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
366 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
367 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
369 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
372 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
374 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
375 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
377 // If this ctor is used, Init() must be called by the derived class later.
378 wxCairoContext( wxGraphicsRenderer
* renderer
);
380 virtual ~wxCairoContext();
382 virtual bool ShouldOffset() const
384 if ( !m_enableOffset
)
388 if ( !m_pen
.IsNull() )
390 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
394 return ( penwidth
% 2 ) == 1;
397 virtual void Clip( const wxRegion
®ion
);
399 cairo_surface_t
* m_mswSurface
;
400 WindowHDC m_mswWindowHDC
;
403 // clips drawings to the rect
404 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
406 // resets the clipping to original extent
407 virtual void ResetClip();
409 virtual void * GetNativeContext();
411 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
413 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
415 virtual bool SetCompositionMode(wxCompositionMode op
);
417 virtual void BeginLayer(wxDouble opacity
);
419 virtual void EndLayer();
421 virtual void StrokePath( const wxGraphicsPath
& p
);
422 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
424 virtual void Translate( wxDouble dx
, wxDouble dy
);
425 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
426 virtual void Rotate( wxDouble angle
);
428 // concatenates this transform with the current transform of this context
429 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
431 // sets the transform of this context
432 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
434 // gets the matrix of this context
435 virtual wxGraphicsMatrix
GetTransform() const;
437 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
438 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
439 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
440 virtual void PushState();
441 virtual void PopState();
443 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
444 wxDouble
*descent
, wxDouble
*externalLeading
) const;
445 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
448 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
450 void Init(cairo_t
*context
);
455 wxVector
<float> m_layerOpacities
;
457 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
461 // ----------------------------------------------------------------------------
462 // wxCairoImageContext: context associated with a wxImage.
463 // ----------------------------------------------------------------------------
465 class wxCairoImageContext
: public wxCairoContext
468 wxCairoImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
469 wxCairoContext(renderer
),
471 m_data(renderer
, image
)
473 Init(cairo_create(m_data
.GetCairoSurface()));
476 virtual ~wxCairoImageContext()
478 m_image
= m_data
.ConvertToImage();
483 wxCairoBitmapData m_data
;
485 wxDECLARE_NO_COPY_CLASS(wxCairoImageContext
);
487 #endif // wxUSE_IMAGE
489 //-----------------------------------------------------------------------------
490 // wxCairoPenData implementation
491 //-----------------------------------------------------------------------------
493 wxCairoPenData::~wxCairoPenData()
495 delete[] m_userLengths
;
498 void wxCairoPenData::Init()
501 m_userLengths
= NULL
;
506 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
507 : wxGraphicsObjectRefData(renderer
)
511 m_width
= m_pen
.GetWidth();
515 m_red
= m_pen
.GetColour().Red()/255.0;
516 m_green
= m_pen
.GetColour().Green()/255.0;
517 m_blue
= m_pen
.GetColour().Blue()/255.0;
518 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
520 switch ( m_pen
.GetCap() )
523 m_cap
= CAIRO_LINE_CAP_ROUND
;
526 case wxCAP_PROJECTING
:
527 m_cap
= CAIRO_LINE_CAP_SQUARE
;
531 m_cap
= CAIRO_LINE_CAP_BUTT
;
535 m_cap
= CAIRO_LINE_CAP_BUTT
;
539 switch ( m_pen
.GetJoin() )
542 m_join
= CAIRO_LINE_JOIN_BEVEL
;
546 m_join
= CAIRO_LINE_JOIN_MITER
;
550 m_join
= CAIRO_LINE_JOIN_ROUND
;
554 m_join
= CAIRO_LINE_JOIN_MITER
;
558 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
559 const double dotted
[] =
561 dashUnit
, dashUnit
+ 2.0
563 static const double short_dashed
[] =
567 static const double dashed
[] =
571 static const double dotted_dashed
[] =
573 9.0 , 6.0 , 3.0 , 3.0
576 switch ( m_pen
.GetStyle() )
578 case wxPENSTYLE_SOLID
:
581 case wxPENSTYLE_DOT
:
582 m_count
= WXSIZEOF(dotted
);
583 m_userLengths
= new double[ m_count
] ;
584 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
585 m_lengths
= m_userLengths
;
588 case wxPENSTYLE_LONG_DASH
:
590 m_count
= WXSIZEOF(dashed
);
593 case wxPENSTYLE_SHORT_DASH
:
594 m_lengths
= short_dashed
;
595 m_count
= WXSIZEOF(short_dashed
);
598 case wxPENSTYLE_DOT_DASH
:
599 m_lengths
= dotted_dashed
;
600 m_count
= WXSIZEOF(dotted_dashed
);
603 case wxPENSTYLE_USER_DASH
:
606 m_count
= m_pen
.GetDashes( &wxdashes
) ;
607 if ((wxdashes
!= NULL
) && (m_count
> 0))
609 m_userLengths
= new double[m_count
] ;
610 for ( int i
= 0 ; i
< m_count
; ++i
)
612 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
614 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
615 m_userLengths
[i
] = dashUnit
+ 2.0 ;
616 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
617 m_userLengths
[i
] = dashUnit
;
620 m_lengths
= m_userLengths
;
623 case wxPENSTYLE_STIPPLE
:
626 wxBitmap* bmp = pen.GetStipple();
627 if ( bmp && bmp->IsOk() )
629 wxDELETE( m_penImage );
630 wxDELETE( m_penBrush );
631 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
632 m_penBrush = new TextureBrush(m_penImage);
633 m_pen->SetBrush( m_penBrush );
639 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
640 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
643 wxDELETE( m_penBrush );
644 HatchStyle style = HatchStyleHorizontal;
645 switch( pen.GetStyle() )
647 case wxPENSTYLE_BDIAGONAL_HATCH :
648 style = HatchStyleBackwardDiagonal;
650 case wxPENSTYLE_CROSSDIAG_HATCH :
651 style = HatchStyleDiagonalCross;
653 case wxPENSTYLE_FDIAGONAL_HATCH :
654 style = HatchStyleForwardDiagonal;
656 case wxPENSTYLE_CROSS_HATCH :
657 style = HatchStyleCross;
659 case wxPENSTYLE_HORIZONTAL_HATCH :
660 style = HatchStyleHorizontal;
662 case wxPENSTYLE_VERTICAL_HATCH :
663 style = HatchStyleVertical;
667 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
668 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
669 m_pen->SetBrush( m_penBrush )
676 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
678 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
679 cairo_set_line_width(ctext
,m_width
);
680 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
681 cairo_set_line_cap(ctext
,m_cap
);
682 cairo_set_line_join(ctext
,m_join
);
683 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
686 //-----------------------------------------------------------------------------
687 // wxCairoBrushData implementation
688 //-----------------------------------------------------------------------------
690 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
691 : wxGraphicsObjectRefData( renderer
)
696 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
697 : wxGraphicsObjectRefData(renderer
)
701 m_red
= brush
.GetColour().Red()/255.0;
702 m_green
= brush
.GetColour().Green()/255.0;
703 m_blue
= brush
.GetColour().Blue()/255.0;
704 m_alpha
= brush
.GetColour().Alpha()/255.0;
706 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
708 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
709 brush.GetColour().Green() , brush.GetColour().Blue() ) );
711 else if ( brush.IsHatch() )
713 HatchStyle style = HatchStyleHorizontal;
714 switch( brush.GetStyle() )
716 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
717 style = HatchStyleBackwardDiagonal;
719 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
720 style = HatchStyleDiagonalCross;
722 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
723 style = HatchStyleForwardDiagonal;
725 case wxBRUSHSTYLE_CROSS_HATCH :
726 style = HatchStyleCross;
728 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
729 style = HatchStyleHorizontal;
731 case wxBRUSHSTYLE_VERTICAL_HATCH :
732 style = HatchStyleVertical;
736 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
737 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
741 wxBitmap* bmp = brush.GetStipple();
742 if ( bmp && bmp->IsOk() )
744 wxDELETE( m_brushImage );
745 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
746 m_brush = new TextureBrush(m_brushImage);
752 wxCairoBrushData::~wxCairoBrushData ()
755 cairo_pattern_destroy(m_brushPattern
);
758 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
760 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
761 if ( m_brushPattern
)
763 cairo_set_source(ctext
,m_brushPattern
);
767 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
771 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
773 // loop over all the stops, they include the beginning and ending ones
774 const unsigned numStops
= stops
.GetCount();
775 for ( unsigned n
= 0; n
< numStops
; n
++ )
777 const wxGraphicsGradientStop stop
= stops
.Item(n
);
779 const wxColour col
= stop
.GetColour();
781 cairo_pattern_add_color_stop_rgba
792 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
793 wxT("Couldn't create cairo pattern"));
797 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
798 wxDouble x2
, wxDouble y2
,
799 const wxGraphicsGradientStops
& stops
)
801 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
803 AddGradientStops(stops
);
807 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
808 wxDouble xc
, wxDouble yc
,
810 const wxGraphicsGradientStops
& stops
)
812 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
814 AddGradientStops(stops
);
817 void wxCairoBrushData::Init()
819 m_brushPattern
= NULL
;
822 //-----------------------------------------------------------------------------
823 // wxCairoFontData implementation
824 //-----------------------------------------------------------------------------
826 void wxCairoFontData::InitColour(const wxColour
& col
)
828 m_red
= col
.Red()/255.0;
829 m_green
= col
.Green()/255.0;
830 m_blue
= col
.Blue()/255.0;
831 m_alpha
= col
.Alpha()/255.0;
835 wxCairoFontData::InitFontComponents(const wxString
& facename
,
836 cairo_font_slant_t slant
,
837 cairo_font_weight_t weight
)
839 m_fontName
= facename
.mb_str(wxConvUTF8
);
844 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
845 const wxColour
& col
)
846 : wxGraphicsObjectRefData(renderer
)
853 m_size
= font
.GetPointSize();
856 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
857 #elif defined(__WXGTK__)
862 font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
863 : CAIRO_FONT_SLANT_NORMAL
,
864 font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
865 : CAIRO_FONT_WEIGHT_NORMAL
870 wxCairoFontData::wxCairoFontData(wxGraphicsRenderer
* renderer
,
872 const wxString
& facename
,
874 const wxColour
& col
) :
875 wxGraphicsObjectRefData(renderer
)
879 // Resolution for Cairo image surfaces is 72 DPI meaning that the sizes in
880 // points and pixels are identical, so we can just pass the size in pixels
881 // directly to cairo_set_font_size().
882 m_size
= sizeInPixels
;
884 #if defined(__WXMAC__)
888 // There is no need to set m_underlined under wxGTK in this case, it can
889 // only be used if m_font != NULL.
894 flags
& wxFONTFLAG_ITALIC
? CAIRO_FONT_SLANT_ITALIC
895 : CAIRO_FONT_SLANT_NORMAL
,
896 flags
& wxFONTFLAG_BOLD
? CAIRO_FONT_WEIGHT_BOLD
897 : CAIRO_FONT_WEIGHT_NORMAL
901 wxCairoFontData::~wxCairoFontData()
905 cairo_font_face_destroy( m_font
);
909 bool wxCairoFontData::Apply( wxGraphicsContext
* context
)
911 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
912 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
916 // Nothing to do, the caller uses Pango layout functions to do
920 #elif defined(__WXMAC__)
923 cairo_set_font_face(ctext
, m_font
);
924 cairo_set_font_size(ctext
, m_size
);
929 // If we get here, we must be on a platform without native font support or
930 // we're using toy Cairo API even under wxGTK/wxMac.
931 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
932 cairo_set_font_size(ctext
, m_size
);
934 // Indicate that we don't use native fonts for the platforms which care
935 // about this (currently only wxGTK).
939 //-----------------------------------------------------------------------------
940 // wxCairoPathData implementation
941 //-----------------------------------------------------------------------------
943 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
944 : wxGraphicsPathData(renderer
)
948 m_pathContext
= pathcontext
;
952 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
953 m_pathContext
= cairo_create(surface
);
954 cairo_surface_destroy (surface
);
958 wxCairoPathData::~wxCairoPathData()
960 cairo_destroy(m_pathContext
);
963 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
965 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
966 cairo_t
* pathcontext
= cairo_create(surface
);
967 cairo_surface_destroy (surface
);
969 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
970 cairo_append_path(pathcontext
, path
);
971 cairo_path_destroy(path
);
972 return new wxCairoPathData( GetRenderer() ,pathcontext
);
976 void* wxCairoPathData::GetNativePath() const
978 return cairo_copy_path(m_pathContext
) ;
981 void wxCairoPathData::UnGetNativePath(void *p
) const
983 cairo_path_destroy((cairo_path_t
*)p
);
990 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
992 cairo_move_to(m_pathContext
,x
,y
);
995 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
997 cairo_line_to(m_pathContext
,x
,y
);
1000 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
1002 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
1003 cairo_append_path(m_pathContext
, p
);
1007 void wxCairoPathData::CloseSubpath()
1009 cairo_close_path(m_pathContext
);
1012 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1014 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
1017 // gets the last point of the current path, (0,0) if not yet set
1018 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1021 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
1028 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1030 // as clockwise means positive in our system (y pointing downwards)
1031 // TODO make this interpretation dependent of the
1032 // real device trans
1033 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
1034 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1036 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1039 // transforms each point of this path by the matrix
1040 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1042 // as we don't have a true path object, we have to apply the inverse
1043 // matrix to the context
1044 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
1045 cairo_matrix_invert( &m
);
1046 cairo_transform(m_pathContext
,&m
);
1049 // gets the bounding box enclosing all points (possibly including control points)
1050 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1054 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
1078 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1080 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1081 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
1084 //-----------------------------------------------------------------------------
1085 // wxCairoMatrixData implementation
1086 //-----------------------------------------------------------------------------
1088 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
1089 : wxGraphicsMatrixData(renderer
)
1095 wxCairoMatrixData::~wxCairoMatrixData()
1100 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
1102 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
1105 // concatenates the matrix
1106 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1108 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
1111 // sets the matrix to the respective values
1112 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1113 wxDouble tx
, wxDouble ty
)
1115 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
1118 // gets the component valuess of the matrix
1119 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1120 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1122 if (a
) *a
= m_matrix
.xx
;
1123 if (b
) *b
= m_matrix
.yx
;
1124 if (c
) *c
= m_matrix
.xy
;
1125 if (d
) *d
= m_matrix
.yy
;
1126 if (tx
) *tx
= m_matrix
.x0
;
1127 if (ty
) *ty
= m_matrix
.y0
;
1130 // makes this the inverse matrix
1131 void wxCairoMatrixData::Invert()
1133 cairo_matrix_invert( &m_matrix
);
1136 // returns true if the elements of the transformation matrix are equal ?
1137 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1139 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1141 m_matrix
.xx
== tm
->xx
&&
1142 m_matrix
.yx
== tm
->yx
&&
1143 m_matrix
.xy
== tm
->xy
&&
1144 m_matrix
.yy
== tm
->yy
&&
1145 m_matrix
.x0
== tm
->x0
&&
1146 m_matrix
.y0
== tm
->y0
) ;
1149 // return true if this is the identity matrix
1150 bool wxCairoMatrixData::IsIdentity() const
1152 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1153 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1160 // add the translation to this matrix
1161 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1163 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1166 // add the scale to this matrix
1167 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1169 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1172 // add the rotation to this matrix (radians)
1173 void wxCairoMatrixData::Rotate( wxDouble angle
)
1175 cairo_matrix_rotate( &m_matrix
, angle
) ;
1179 // apply the transforms
1182 // applies that matrix to the point
1183 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1185 double lx
= *x
, ly
= *y
;
1186 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1191 // applies the matrix except for translations
1192 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1194 double lx
= *dx
, ly
= *dy
;
1195 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1200 // returns the native representation
1201 void * wxCairoMatrixData::GetNativeMatrix() const
1203 return (void*) &m_matrix
;
1206 // ----------------------------------------------------------------------------
1207 // wxCairoBitmap implementation
1208 // ----------------------------------------------------------------------------
1210 int wxCairoBitmapData::InitBuffer(int width
, int height
, cairo_format_t format
)
1212 wxUnusedVar(format
); // Only really unused with Cairo < 1.6.
1214 // Determine the stride: use cairo_format_stride_for_width() if available
1215 // but fall back to 4*width for the earlier versions as this is what that
1216 // function always returns, even in latest Cairo, anyhow.
1218 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1219 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1221 stride
= cairo_format_stride_for_width(format
, width
);
1223 // All our code would totally break if stride were not a multiple of 4
1224 // so ensure this is the case.
1227 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1229 stride
+= 4 - stride
% 4;
1238 m_buffer
= new unsigned char[height
*stride
];
1243 void wxCairoBitmapData::InitSurface(cairo_format_t format
, int stride
)
1245 m_surface
= cairo_image_surface_create_for_data(
1246 m_buffer
, format
, m_width
, m_height
, stride
);
1247 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1250 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1251 wxGraphicsObjectRefData( renderer
)
1254 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1257 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1259 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1261 #ifdef wxHAS_RAW_BITMAP
1262 // Create a surface object and copy the bitmap pixel data to it. if the
1263 // image has alpha (or a mask represented as alpha) then we'll use a
1264 // different format and iterator than if it doesn't...
1265 cairo_format_t bufferFormat
= bmp
.GetDepth() == 32
1269 ? CAIRO_FORMAT_ARGB32
1270 : CAIRO_FORMAT_RGB24
;
1272 int stride
= InitBuffer(bmp
.GetWidth(), bmp
.GetHeight(), bufferFormat
);
1274 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1275 wxUint32
* data
= (wxUint32
*)m_buffer
;
1277 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1279 // use the bitmap's alpha
1281 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1282 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1284 wxAlphaPixelData::Iterator
p(pixData
);
1285 for (int y
=0; y
<m_height
; y
++)
1287 wxAlphaPixelData::Iterator rowStart
= p
;
1288 wxUint32
* const rowStartDst
= data
;
1289 for (int x
=0; x
<m_width
; x
++)
1291 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1292 // with alpha in the upper 8 bits, then red, then green, then
1293 // blue. The 32-bit quantities are stored native-endian.
1294 // Pre-multiplied alpha is used.
1295 unsigned char alpha
= p
.Alpha();
1299 *data
= ( alpha
<< 24
1300 | (p
.Red() * alpha
/255) << 16
1301 | (p
.Green() * alpha
/255) << 8
1302 | (p
.Blue() * alpha
/255) );
1307 data
= rowStartDst
+ stride
/ 4;
1309 p
.OffsetY(pixData
, 1);
1315 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1316 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1318 wxNativePixelData::Iterator
p(pixData
);
1319 for (int y
=0; y
<m_height
; y
++)
1321 wxNativePixelData::Iterator rowStart
= p
;
1322 wxUint32
* const rowStartDst
= data
;
1323 for (int x
=0; x
<m_width
; x
++)
1325 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1326 // the upper 8 bits unused. Red, Green, and Blue are stored in
1327 // the remaining 24 bits in that order. The 32-bit quantities
1328 // are stored native-endian.
1329 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1334 data
= rowStartDst
+ stride
/ 4;
1336 p
.OffsetY(pixData
, 1);
1340 // if there is a mask, set the alpha bytes in the target buffer to
1341 // fully transparent or fully opaque
1342 if (bmpSource
.GetMask())
1344 wxBitmap bmpMask
= bmpSource
.GetMaskBitmap();
1345 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1346 data
= (wxUint32
*)m_buffer
;
1348 pixData(bmpMask
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1349 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to mask data."));
1351 wxNativePixelData::Iterator
p(pixData
);
1352 for (int y
=0; y
<m_height
; y
++)
1354 wxNativePixelData::Iterator rowStart
= p
;
1355 wxUint32
* const rowStartDst
= data
;
1356 for (int x
=0; x
<m_width
; x
++)
1358 if (p
.Red()+p
.Green()+p
.Blue() == 0)
1361 *data
= (wxALPHA_OPAQUE
<< 24) | (*data
& 0x00FFFFFF);
1366 data
= rowStartDst
+ stride
/ 4;
1368 p
.OffsetY(pixData
, 1);
1373 InitSurface(bufferFormat
, stride
);
1374 #endif // wxHAS_RAW_BITMAP
1379 // Helper functions for dealing with alpha pre-multiplication.
1383 inline unsigned char Premultiply(unsigned char alpha
, unsigned char data
)
1385 return alpha
? (data
* alpha
)/0xff : data
;
1388 inline unsigned char Unpremultiply(unsigned char alpha
, unsigned char data
)
1390 return alpha
? (data
* 0xff)/alpha
: data
;
1393 } // anonymous namespace
1395 wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer
* renderer
,
1396 const wxImage
& image
)
1397 : wxGraphicsObjectRefData(renderer
)
1399 const cairo_format_t bufferFormat
= image
.HasAlpha()
1400 ? CAIRO_FORMAT_ARGB32
1401 : CAIRO_FORMAT_RGB24
;
1403 int stride
= InitBuffer(image
.GetWidth(), image
.GetHeight(), bufferFormat
);
1405 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1406 // values and not bytes becase Cairo always works with buffers in native
1408 wxUint32
* dst
= reinterpret_cast<wxUint32
*>(m_buffer
);
1409 const unsigned char* src
= image
.GetData();
1411 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1413 const unsigned char* alpha
= image
.GetAlpha();
1415 for ( int y
= 0; y
< m_height
; y
++ )
1417 wxUint32
* const rowStartDst
= dst
;
1419 for ( int x
= 0; x
< m_width
; x
++ )
1421 const unsigned char a
= *alpha
++;
1424 Premultiply(a
, src
[0]) << 16 |
1425 Premultiply(a
, src
[1]) << 8 |
1426 Premultiply(a
, src
[2]);
1430 dst
= rowStartDst
+ stride
/ 4;
1435 for ( int y
= 0; y
< m_height
; y
++ )
1437 wxUint32
* const rowStartDst
= dst
;
1439 for ( int x
= 0; x
< m_width
; x
++ )
1441 *dst
++ = src
[0] << 16 |
1447 dst
= rowStartDst
+ stride
/ 4;
1451 InitSurface(bufferFormat
, stride
);
1454 wxImage
wxCairoBitmapData::ConvertToImage() const
1456 wxImage
image(m_width
, m_height
, false /* don't clear */);
1458 // Get the surface type and format.
1459 wxCHECK_MSG( cairo_surface_get_type(m_surface
) == CAIRO_SURFACE_TYPE_IMAGE
,
1461 wxS("Can't convert non-image surface to image.") );
1463 switch ( cairo_image_surface_get_format(m_surface
) )
1465 case CAIRO_FORMAT_ARGB32
:
1469 case CAIRO_FORMAT_RGB24
:
1470 // Nothing to do, we don't use alpha by default.
1473 case CAIRO_FORMAT_A8
:
1474 case CAIRO_FORMAT_A1
:
1475 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1479 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1483 // Prepare for copying data.
1484 const wxUint32
* src
= (wxUint32
*)cairo_image_surface_get_data(m_surface
);
1485 wxCHECK_MSG( src
, wxNullImage
, wxS("Failed to get Cairo surface data.") );
1487 int stride
= cairo_image_surface_get_stride(m_surface
);
1488 wxCHECK_MSG( stride
> 0, wxNullImage
,
1489 wxS("Failed to get Cairo surface stride.") );
1491 // As we work with wxUint32 pointers and not char ones, we need to adjust
1492 // the stride accordingly. This should be lossless as the stride must be a
1493 // multiple of pixel size.
1494 wxASSERT_MSG( !(stride
% sizeof(wxUint32
)), wxS("Unexpected stride.") );
1495 stride
/= sizeof(wxUint32
);
1497 unsigned char* dst
= image
.GetData();
1498 unsigned char *alpha
= image
.GetAlpha();
1501 // We need to also copy alpha and undo the pre-multiplication as Cairo
1502 // stores pre-multiplied values in this format while wxImage does not.
1503 for ( int y
= 0; y
< m_height
; y
++ )
1505 const wxUint32
* const rowStart
= src
;
1506 for ( int x
= 0; x
< m_width
; x
++ )
1508 const wxUint32 argb
= *src
++;
1510 *alpha
++ = (argb
& 0xff000000) >> 24;
1512 // Copy the RGB data undoing the pre-multiplication.
1513 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x00ff0000) >> 16);
1514 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x0000ff00) >> 8);
1515 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x000000ff));
1518 src
= rowStart
+ stride
;
1523 // Things are pretty simple in this case, just copy RGB bytes.
1524 for ( int y
= 0; y
< m_height
; y
++ )
1526 const wxUint32
* const rowStart
= src
;
1527 for ( int x
= 0; x
< m_width
; x
++ )
1529 const wxUint32 argb
= *src
++;
1531 *dst
++ = (argb
& 0x00ff0000) >> 16;
1532 *dst
++ = (argb
& 0x0000ff00) >> 8;
1533 *dst
++ = (argb
& 0x000000ff);
1536 src
= rowStart
+ stride
;
1543 #endif // wxUSE_IMAGE
1545 wxCairoBitmapData::~wxCairoBitmapData()
1548 cairo_pattern_destroy(m_pattern
);
1551 cairo_surface_destroy(m_surface
);
1556 //-----------------------------------------------------------------------------
1557 // wxCairoContext implementation
1558 //-----------------------------------------------------------------------------
1560 class wxCairoOffsetHelper
1563 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1568 cairo_translate( m_ctx
, 0.5, 0.5 );
1570 ~wxCairoOffsetHelper( )
1573 cairo_translate( m_ctx
, -0.5, -0.5 );
1580 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1581 : wxGraphicsContext(renderer
)
1584 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1585 // text rendering when printing using Cairo. Switch it to MM_TEXT
1586 // map mode to avoid this problem.
1587 HDC hdc
= (HDC
)dc
.GetHDC();
1588 ::SetMapMode(hdc
, MM_TEXT
);
1589 m_mswSurface
= cairo_win32_printing_surface_create(hdc
);
1590 Init( cairo_create(m_mswSurface
) );
1594 const wxDCImpl
*impl
= dc
.GetImpl();
1595 Init( (cairo_t
*) impl
->GetCairoContext() );
1597 wxSize sz
= dc
.GetSize();
1601 wxPoint org
= dc
.GetDeviceOrigin();
1602 cairo_translate( m_context
, org
.x
, org
.y
);
1605 dc
.GetUserScale( &sx
, &sy
);
1607 // TODO: Determine if these fixes are needed on other platforms too.
1608 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1609 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1613 dc
.GetLogicalScale( &lsx
, &lsy
);
1617 cairo_scale( m_context
, sx
, sy
);
1619 org
= dc
.GetLogicalOrigin();
1620 cairo_translate( m_context
, -org
.x
, -org
.y
);
1623 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1624 : wxGraphicsContext(renderer
)
1627 dc
.GetSize( &width
, &height
);
1631 m_enableOffset
= true;
1634 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1635 Init( cairo_create(m_mswSurface
) );
1639 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1640 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1643 wxGraphicsMatrix matrix
= CreateMatrix();
1645 wxPoint org
= dc
.GetDeviceOrigin();
1646 matrix
.Translate( org
.x
, org
.y
);
1648 org
= dc
.GetLogicalOrigin();
1649 matrix
.Translate( -org
.x
, -org
.y
);
1652 dc
.GetUserScale( &sx
, &sy
);
1653 matrix
.Scale( sx
, sy
);
1655 ConcatTransform( matrix
);
1660 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1661 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1662 Init( cairo_create( surface
) );
1663 cairo_surface_destroy( surface
);
1667 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1668 : wxGraphicsContext(renderer
)
1671 dc
.GetSize( &width
, &height
);
1675 m_enableOffset
= true;
1678 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1679 Init( cairo_create(m_mswSurface
) );
1683 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1684 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1687 wxGraphicsMatrix matrix
= CreateMatrix();
1689 wxPoint org
= dc
.GetDeviceOrigin();
1690 matrix
.Translate( org
.x
, org
.y
);
1692 org
= dc
.GetLogicalOrigin();
1693 matrix
.Translate( -org
.x
, -org
.y
);
1696 dc
.GetUserScale( &sx
, &sy
);
1697 matrix
.Scale( sx
, sy
);
1699 ConcatTransform( matrix
);
1704 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1705 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1706 Init( cairo_create( surface
) );
1707 cairo_surface_destroy( surface
);
1712 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1713 : wxGraphicsContext(renderer
)
1715 Init( gdk_cairo_create( drawable
) );
1718 gdk_drawable_get_size( drawable
, &width
, &height
);
1725 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1726 : wxGraphicsContext(renderer
)
1728 m_mswSurface
= cairo_win32_surface_create(handle
);
1729 Init( cairo_create(m_mswSurface
) );
1736 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1737 : wxGraphicsContext(renderer
)
1744 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1745 : wxGraphicsContext(renderer
)
1747 , m_mswWindowHDC(GetHwndOf(window
))
1750 m_enableOffset
= true;
1752 // something along these lines (copied from dcclient)
1754 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1755 // code should still be able to create wxClientDCs for them, so we will
1756 // use the parent window here then.
1757 if (window
->m_wxwindow
== NULL
)
1759 window
= window
->GetParent();
1762 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1764 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1766 wxSize sz
= window
->GetSize();
1772 m_mswSurface
= cairo_win32_surface_create((HDC
)m_mswWindowHDC
);
1773 Init(cairo_create(m_mswSurface
));
1778 wxCairoContext::wxCairoContext(wxGraphicsRenderer
* renderer
) :
1779 wxGraphicsContext(renderer
)
1784 wxCairoContext::~wxCairoContext()
1790 cairo_destroy(m_context
);
1794 cairo_surface_destroy(m_mswSurface
);
1798 void wxCairoContext::Init(cairo_t
*context
)
1800 m_context
= context
;
1806 void wxCairoContext::Clip( const wxRegion
& region
)
1808 // Create a path with all the rectangles in the region
1809 wxGraphicsPath path
= GetRenderer()->CreatePath();
1810 wxRegionIterator
ri(region
);
1813 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1817 // Put it in the context
1818 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1819 cairo_append_path(m_context
, cp
);
1821 // clip to that path
1822 cairo_clip(m_context
);
1823 path
.UnGetNativePath(cp
);
1826 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1828 // Create a path with this rectangle
1829 wxGraphicsPath path
= GetRenderer()->CreatePath();
1830 path
.AddRectangle(x
,y
,w
,h
);
1832 // Put it in the context
1833 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1834 cairo_append_path(m_context
, cp
);
1836 // clip to that path
1837 cairo_clip(m_context
);
1838 path
.UnGetNativePath(cp
);
1841 void wxCairoContext::ResetClip()
1843 cairo_reset_clip(m_context
);
1847 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1849 if ( !m_pen
.IsNull() )
1851 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1852 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1853 cairo_append_path(m_context
,cp
);
1854 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1855 cairo_stroke(m_context
);
1856 path
.UnGetNativePath(cp
);
1860 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1862 if ( !m_brush
.IsNull() )
1864 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1865 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1866 cairo_append_path(m_context
,cp
);
1867 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1868 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1869 cairo_fill(m_context
);
1870 path
.UnGetNativePath(cp
);
1874 void wxCairoContext::Rotate( wxDouble angle
)
1876 cairo_rotate(m_context
,angle
);
1879 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1881 cairo_translate(m_context
,dx
,dy
);
1884 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1886 cairo_scale(m_context
,xScale
,yScale
);
1889 // concatenates this transform with the current transform of this context
1890 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1892 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1895 // sets the transform of this context
1896 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1898 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1901 // gets the matrix of this context
1902 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1904 wxGraphicsMatrix matrix
= CreateMatrix();
1905 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1911 void wxCairoContext::PushState()
1913 cairo_save(m_context
);
1916 void wxCairoContext::PopState()
1918 cairo_restore(m_context
);
1921 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1923 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1924 DrawBitmap(bitmap
, x
, y
, w
, h
);
1928 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1932 // In case we're scaling the image by using a width and height different
1933 // than the bitmap's size create a pattern transformation on the surface and
1934 // draw the transformed pattern.
1935 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1936 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1937 wxSize size
= data
->GetSize();
1939 wxDouble scaleX
= w
/ size
.GetWidth();
1940 wxDouble scaleY
= h
/ size
.GetHeight();
1942 // prepare to draw the image
1943 cairo_translate(m_context
, x
, y
);
1944 cairo_scale(m_context
, scaleX
, scaleY
);
1945 cairo_set_source(m_context
, pattern
);
1946 // use the original size here since the context is scaled already...
1947 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1948 // fill the rectangle using the pattern
1949 cairo_fill(m_context
);
1954 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1956 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1957 // start using the Cairo backend on other platforms then we may need to
1958 // fiddle with this...
1959 DrawBitmap(icon
, x
, y
, w
, h
);
1963 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1965 wxCHECK_RET( !m_font
.IsNull(),
1966 wxT("wxCairoContext::DrawText - no valid font set") );
1971 const wxCharBuffer data
= str
.utf8_str();
1975 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this) )
1978 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1979 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
1980 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
1981 pango_layout_set_text(layout
, data
, data
.length());
1982 font
.GTKSetPangoAttrs(layout
);
1984 cairo_move_to(m_context
, x
, y
);
1985 pango_cairo_show_layout (m_context
, layout
);
1987 g_object_unref (layout
);
1989 // Don't use Cairo text API, we already did everything.
1994 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1995 // the position we move to by the ascent.
1996 cairo_font_extents_t fe
;
1997 cairo_font_extents(m_context
, &fe
);
1998 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
2000 cairo_show_text(m_context
, data
);
2003 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2004 wxDouble
*descent
, wxDouble
*externalLeading
) const
2006 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
2014 if ( externalLeading
)
2015 *externalLeading
= 0;
2020 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this) )
2025 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2026 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2027 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2028 const wxCharBuffer data
= str
.utf8_str();
2033 pango_layout_set_text(layout
, data
, data
.length());
2034 pango_layout_get_pixel_size (layout
, &w
, &h
);
2041 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
2042 int baseline
= pango_layout_iter_get_baseline(iter
);
2043 pango_layout_iter_free(iter
);
2044 *descent
= h
- PANGO_PIXELS(baseline
);
2046 g_object_unref (layout
);
2053 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
2054 cairo_text_extents_t te
;
2055 cairo_text_extents(m_context
, buf
, &te
);
2059 if (height
|| descent
|| externalLeading
)
2061 cairo_font_extents_t fe
;
2062 cairo_font_extents(m_context
, &fe
);
2064 // some backends have negative descents
2066 if ( fe
.descent
< 0 )
2067 fe
.descent
= -fe
.descent
;
2069 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
2071 // some backends are broken re height ... (eg currently ATSUI)
2072 fe
.height
= fe
.ascent
+ fe
.descent
;
2076 *height
= fe
.height
;
2078 *descent
= fe
.descent
;
2079 if ( externalLeading
)
2080 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
2084 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2087 widths
.Add(0, text
.length());
2089 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
2097 void * wxCairoContext::GetNativeContext()
2102 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
2104 if (m_antialias
== antialias
)
2107 m_antialias
= antialias
;
2109 cairo_antialias_t antialiasMode
;
2112 case wxANTIALIAS_DEFAULT
:
2113 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
2115 case wxANTIALIAS_NONE
:
2116 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
2121 cairo_set_antialias(m_context
, antialiasMode
);
2125 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
2131 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
2133 if ( m_composition
== op
)
2137 cairo_operator_t cop
;
2140 case wxCOMPOSITION_CLEAR
:
2141 cop
= CAIRO_OPERATOR_CLEAR
;
2143 case wxCOMPOSITION_SOURCE
:
2144 cop
= CAIRO_OPERATOR_SOURCE
;
2146 case wxCOMPOSITION_OVER
:
2147 cop
= CAIRO_OPERATOR_OVER
;
2149 case wxCOMPOSITION_IN
:
2150 cop
= CAIRO_OPERATOR_IN
;
2152 case wxCOMPOSITION_OUT
:
2153 cop
= CAIRO_OPERATOR_OUT
;
2155 case wxCOMPOSITION_ATOP
:
2156 cop
= CAIRO_OPERATOR_ATOP
;
2158 case wxCOMPOSITION_DEST
:
2159 cop
= CAIRO_OPERATOR_DEST
;
2161 case wxCOMPOSITION_DEST_OVER
:
2162 cop
= CAIRO_OPERATOR_DEST_OVER
;
2164 case wxCOMPOSITION_DEST_IN
:
2165 cop
= CAIRO_OPERATOR_DEST_IN
;
2167 case wxCOMPOSITION_DEST_OUT
:
2168 cop
= CAIRO_OPERATOR_DEST_OUT
;
2170 case wxCOMPOSITION_DEST_ATOP
:
2171 cop
= CAIRO_OPERATOR_DEST_ATOP
;
2173 case wxCOMPOSITION_XOR
:
2174 cop
= CAIRO_OPERATOR_XOR
;
2176 case wxCOMPOSITION_ADD
:
2177 cop
= CAIRO_OPERATOR_ADD
;
2182 cairo_set_operator(m_context
, cop
);
2186 void wxCairoContext::BeginLayer(wxDouble opacity
)
2188 m_layerOpacities
.push_back(opacity
);
2189 cairo_push_group(m_context
);
2192 void wxCairoContext::EndLayer()
2194 float opacity
= m_layerOpacities
.back();
2195 m_layerOpacities
.pop_back();
2196 cairo_pop_group_to_source(m_context
);
2197 cairo_paint_with_alpha(m_context
,opacity
);
2200 //-----------------------------------------------------------------------------
2201 // wxCairoRenderer declaration
2202 //-----------------------------------------------------------------------------
2204 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
2207 wxCairoRenderer() {}
2209 virtual ~wxCairoRenderer() {}
2213 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2214 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2215 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2217 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2219 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2221 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2222 #endif // wxUSE_IMAGE
2224 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2226 virtual wxGraphicsContext
* CreateMeasuringContext();
2228 #if wxUSE_ENH_METAFILE
2229 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
2234 virtual wxGraphicsPath
CreatePath();
2238 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2239 wxDouble tx
=0.0, wxDouble ty
=0.0);
2242 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2244 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2246 virtual wxGraphicsBrush
2247 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2248 wxDouble x2
, wxDouble y2
,
2249 const wxGraphicsGradientStops
& stops
);
2251 virtual wxGraphicsBrush
2252 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2253 wxDouble xc
, wxDouble yc
,
2255 const wxGraphicsGradientStops
& stops
);
2258 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2259 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2260 const wxString
& facename
,
2261 int flags
= wxFONTFLAG_DEFAULT
,
2262 const wxColour
& col
= *wxBLACK
);
2264 // create a native bitmap representation
2265 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
2267 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2268 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2269 #endif // wxUSE_IMAGE
2271 // create a graphics bitmap from a native bitmap
2272 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2274 // create a subimage from a native image representation
2275 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
2278 bool EnsureIsLoaded();
2281 friend class wxCairoModule
;
2284 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
2287 //-----------------------------------------------------------------------------
2288 // wxCairoRenderer implementation
2289 //-----------------------------------------------------------------------------
2291 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
2293 static wxCairoRenderer gs_cairoGraphicsRenderer
;
2294 // temporary hack to allow creating a cairo context on any platform
2295 extern wxGraphicsRenderer
* gCairoRenderer
;
2296 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
2298 bool wxCairoRenderer::EnsureIsLoaded()
2302 return wxCairoInit();
2308 void wxCairoRenderer::Load()
2313 void wxCairoRenderer::Unload()
2318 // call EnsureIsLoaded() and return returnOnFail value if it fails
2319 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2320 if ( !EnsureIsLoaded() ) \
2321 return (returnOnFail)
2323 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
2325 ENSURE_LOADED_OR_RETURN(NULL
);
2326 return new wxCairoContext(this,dc
);
2329 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
2331 ENSURE_LOADED_OR_RETURN(NULL
);
2332 return new wxCairoContext(this,dc
);
2335 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
2337 ENSURE_LOADED_OR_RETURN(NULL
);
2338 return new wxCairoContext(this, dc
);
2342 #if wxUSE_ENH_METAFILE
2343 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& WXUNUSED(dc
) )
2345 ENSURE_LOADED_OR_RETURN(NULL
);
2351 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
2353 ENSURE_LOADED_OR_RETURN(NULL
);
2355 return new wxCairoContext(this,(HDC
)context
);
2357 return new wxCairoContext(this,(cairo_t
*)context
);
2362 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
2364 ENSURE_LOADED_OR_RETURN(NULL
);
2366 return new wxCairoContext(this,(GdkDrawable
*)window
);
2368 wxUnusedVar(window
);
2374 wxGraphicsContext
* wxCairoRenderer::CreateContextFromImage(wxImage
& image
)
2376 return new wxCairoImageContext(this, image
);
2378 #endif // wxUSE_IMAGE
2380 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
2382 ENSURE_LOADED_OR_RETURN(NULL
);
2384 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2390 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
2392 ENSURE_LOADED_OR_RETURN(NULL
);
2393 return new wxCairoContext(this, window
);
2398 wxGraphicsPath
wxCairoRenderer::CreatePath()
2400 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2401 wxGraphicsPath path
;
2402 path
.SetRefData( new wxCairoPathData(this) );
2409 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2410 wxDouble tx
, wxDouble ty
)
2413 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2415 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
2416 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2421 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
2423 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2424 if ( !pen
.IsOk() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
2425 return wxNullGraphicsPen
;
2429 p
.SetRefData(new wxCairoPenData( this, pen
));
2434 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
2436 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2437 if ( !brush
.IsOk() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
2438 return wxNullGraphicsBrush
;
2442 p
.SetRefData(new wxCairoBrushData( this, brush
));
2448 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2449 wxDouble x2
, wxDouble y2
,
2450 const wxGraphicsGradientStops
& stops
)
2452 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2454 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2455 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2461 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2462 wxDouble xc
, wxDouble yc
, wxDouble r
,
2463 const wxGraphicsGradientStops
& stops
)
2465 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2467 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2468 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
2473 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2475 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2479 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2483 return wxNullGraphicsFont
;
2487 wxCairoRenderer::CreateFont(double sizeInPixels
,
2488 const wxString
& facename
,
2490 const wxColour
& col
)
2492 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2494 wxGraphicsFont font
;
2495 font
.SetRefData(new wxCairoFontData(this, sizeInPixels
, facename
, flags
, col
));
2499 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2501 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2505 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2509 return wxNullGraphicsBitmap
;
2514 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromImage(const wxImage
& image
)
2516 wxGraphicsBitmap bmp
;
2518 ENSURE_LOADED_OR_RETURN(bmp
);
2522 bmp
.SetRefData(new wxCairoBitmapData(this, image
));
2528 wxImage
wxCairoRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
2530 ENSURE_LOADED_OR_RETURN(wxNullImage
);
2532 const wxCairoBitmapData
* const
2533 data
= static_cast<wxCairoBitmapData
*>(bmp
.GetGraphicsData());
2535 return data
? data
->ConvertToImage() : wxNullImage
;
2538 #endif // wxUSE_IMAGE
2541 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2543 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2544 if ( bitmap
!= NULL
)
2547 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2551 return wxNullGraphicsBitmap
;
2555 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2556 wxDouble
WXUNUSED(x
),
2557 wxDouble
WXUNUSED(y
),
2558 wxDouble
WXUNUSED(w
),
2559 wxDouble
WXUNUSED(h
))
2561 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2562 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2563 return wxNullGraphicsBitmap
;
2566 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2568 return &gs_cairoGraphicsRenderer
;
2571 #else // !wxUSE_CAIRO
2573 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2578 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2580 // MSW and OS X have their own native default renderers, but the other ports
2581 // use Cairo by default
2582 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2583 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2585 return GetCairoRenderer();
2587 #endif // !(__WXMSW__ || __WXOSX__)
2589 #endif // wxUSE_GRAPHICS_CONTEXT