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"
84 #include "wx/gtk/dc.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
);
285 wxCairoFontData(wxGraphicsRenderer
* renderer
,
287 const wxString
& facename
,
289 const wxColour
& col
);
292 virtual bool Apply( wxGraphicsContext
* context
);
294 const wxFont
& GetFont() const { return m_wxfont
; }
297 void InitColour(const wxColour
& col
);
298 void InitFontComponents(const wxString
& facename
,
299 cairo_font_slant_t slant
,
300 cairo_font_weight_t weight
);
308 cairo_font_face_t
*m_font
;
309 #elif defined(__WXGTK__)
313 // These members are used when the font is created from its face name and
314 // flags (and not from wxFont) and also even when creating it from wxFont
315 // on the platforms not covered above.
317 // Notice that we can't use cairo_font_face_t instead of storing those,
318 // even though it would be simpler and need less #ifdefs, because
319 // cairo_toy_font_face_create() that we'd need to create it is only
320 // available in Cairo 1.8 and we require just 1.2 currently. If we do drop
321 // support for < 1.8 versions in the future it would be definitely better
322 // to use cairo_toy_font_face_create() instead.
323 wxCharBuffer m_fontName
;
324 cairo_font_slant_t m_slant
;
325 cairo_font_weight_t m_weight
;
328 class wxCairoBitmapData
: public wxGraphicsBitmapData
331 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
333 wxCairoBitmapData(wxGraphicsRenderer
* renderer
, const wxImage
& image
);
334 #endif // wxUSE_IMAGE
335 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
336 ~wxCairoBitmapData();
338 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
339 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
340 virtual void* GetNativeBitmap() const { return m_surface
; }
341 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
344 wxImage
ConvertToImage() const;
345 #endif // wxUSE_IMAGE
348 // Allocate m_buffer for the bitmap of the given size in the given format.
350 // Returns the stride used for the buffer.
351 int InitBuffer(int width
, int height
, cairo_format_t format
);
353 // Really create the surface using the buffer (which was supposed to be
354 // filled since InitBuffer() call).
355 void InitSurface(cairo_format_t format
, int stride
);
358 cairo_surface_t
* m_surface
;
359 cairo_pattern_t
* m_pattern
;
362 unsigned char* m_buffer
;
365 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
368 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
369 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
370 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
372 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkWindow
*window
);
375 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
377 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
378 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
380 // If this ctor is used, Init() must be called by the derived class later.
381 wxCairoContext( wxGraphicsRenderer
* renderer
);
383 virtual ~wxCairoContext();
385 virtual bool ShouldOffset() const
387 if ( !m_enableOffset
)
391 if ( !m_pen
.IsNull() )
393 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
397 return ( penwidth
% 2 ) == 1;
400 virtual void Clip( const wxRegion
®ion
);
402 cairo_surface_t
* m_mswSurface
;
403 WindowHDC m_mswWindowHDC
;
406 // clips drawings to the rect
407 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
409 // resets the clipping to original extent
410 virtual void ResetClip();
412 virtual void * GetNativeContext();
414 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
416 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
418 virtual bool SetCompositionMode(wxCompositionMode op
);
420 virtual void BeginLayer(wxDouble opacity
);
422 virtual void EndLayer();
424 virtual void StrokePath( const wxGraphicsPath
& p
);
425 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
427 virtual void Translate( wxDouble dx
, wxDouble dy
);
428 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
429 virtual void Rotate( wxDouble angle
);
431 // concatenates this transform with the current transform of this context
432 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
434 // sets the transform of this context
435 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
437 // gets the matrix of this context
438 virtual wxGraphicsMatrix
GetTransform() const;
440 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
441 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
442 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
443 virtual void PushState();
444 virtual void PopState();
446 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
447 wxDouble
*descent
, wxDouble
*externalLeading
) const;
448 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
451 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
453 void Init(cairo_t
*context
);
458 wxVector
<float> m_layerOpacities
;
460 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
464 // ----------------------------------------------------------------------------
465 // wxCairoImageContext: context associated with a wxImage.
466 // ----------------------------------------------------------------------------
468 class wxCairoImageContext
: public wxCairoContext
471 wxCairoImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
472 wxCairoContext(renderer
),
474 m_data(renderer
, image
)
476 Init(cairo_create(m_data
.GetCairoSurface()));
479 virtual ~wxCairoImageContext()
481 m_image
= m_data
.ConvertToImage();
486 wxCairoBitmapData m_data
;
488 wxDECLARE_NO_COPY_CLASS(wxCairoImageContext
);
490 #endif // wxUSE_IMAGE
492 //-----------------------------------------------------------------------------
493 // wxCairoPenData implementation
494 //-----------------------------------------------------------------------------
496 wxCairoPenData
::~wxCairoPenData()
498 delete[] m_userLengths
;
501 void wxCairoPenData
::Init()
504 m_userLengths
= NULL
;
509 wxCairoPenData
::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
510 : wxGraphicsObjectRefData(renderer
)
514 m_width
= m_pen
.GetWidth();
518 m_red
= m_pen
.GetColour().Red()/255.0;
519 m_green
= m_pen
.GetColour().Green()/255.0;
520 m_blue
= m_pen
.GetColour().Blue()/255.0;
521 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
523 switch ( m_pen
.GetCap() )
526 m_cap
= CAIRO_LINE_CAP_ROUND
;
529 case wxCAP_PROJECTING
:
530 m_cap
= CAIRO_LINE_CAP_SQUARE
;
534 m_cap
= CAIRO_LINE_CAP_BUTT
;
538 m_cap
= CAIRO_LINE_CAP_BUTT
;
542 switch ( m_pen
.GetJoin() )
545 m_join
= CAIRO_LINE_JOIN_BEVEL
;
549 m_join
= CAIRO_LINE_JOIN_MITER
;
553 m_join
= CAIRO_LINE_JOIN_ROUND
;
557 m_join
= CAIRO_LINE_JOIN_MITER
;
561 const double dashUnit
= m_width
< 1.0 ?
1.0 : m_width
;
562 const double dotted
[] =
564 dashUnit
, dashUnit
+ 2.0
566 static const double short_dashed
[] =
570 static const double dashed
[] =
574 static const double dotted_dashed
[] =
576 9.0 , 6.0 , 3.0 , 3.0
579 switch ( m_pen
.GetStyle() )
581 case wxPENSTYLE_SOLID
:
584 case wxPENSTYLE_DOT
:
585 m_count
= WXSIZEOF(dotted
);
586 m_userLengths
= new double[ m_count
] ;
587 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
588 m_lengths
= m_userLengths
;
591 case wxPENSTYLE_LONG_DASH
:
593 m_count
= WXSIZEOF(dashed
);
596 case wxPENSTYLE_SHORT_DASH
:
597 m_lengths
= short_dashed
;
598 m_count
= WXSIZEOF(short_dashed
);
601 case wxPENSTYLE_DOT_DASH
:
602 m_lengths
= dotted_dashed
;
603 m_count
= WXSIZEOF(dotted_dashed
);
606 case wxPENSTYLE_USER_DASH
:
609 m_count
= m_pen
.GetDashes( &wxdashes
) ;
610 if ((wxdashes
!= NULL
) && (m_count
> 0))
612 m_userLengths
= new double[m_count
] ;
613 for ( int i
= 0 ; i
< m_count
; ++i
)
615 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
617 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
618 m_userLengths
[i
] = dashUnit
+ 2.0 ;
619 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
620 m_userLengths
[i
] = dashUnit
;
623 m_lengths
= m_userLengths
;
626 case wxPENSTYLE_STIPPLE
:
629 wxBitmap* bmp = pen.GetStipple();
630 if ( bmp && bmp->IsOk() )
632 wxDELETE( m_penImage );
633 wxDELETE( m_penBrush );
634 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
635 m_penBrush = new TextureBrush(m_penImage);
636 m_pen->SetBrush( m_penBrush );
642 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
643 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
646 wxDELETE( m_penBrush );
647 HatchStyle style = HatchStyleHorizontal;
648 switch( pen.GetStyle() )
650 case wxPENSTYLE_BDIAGONAL_HATCH :
651 style = HatchStyleBackwardDiagonal;
653 case wxPENSTYLE_CROSSDIAG_HATCH :
654 style = HatchStyleDiagonalCross;
656 case wxPENSTYLE_FDIAGONAL_HATCH :
657 style = HatchStyleForwardDiagonal;
659 case wxPENSTYLE_CROSS_HATCH :
660 style = HatchStyleCross;
662 case wxPENSTYLE_HORIZONTAL_HATCH :
663 style = HatchStyleHorizontal;
665 case wxPENSTYLE_VERTICAL_HATCH :
666 style = HatchStyleVertical;
670 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
671 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
672 m_pen->SetBrush( m_penBrush )
679 void wxCairoPenData
::Apply( wxGraphicsContext
* context
)
681 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
682 cairo_set_line_width(ctext
,m_width
);
683 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
684 cairo_set_line_cap(ctext
,m_cap
);
685 cairo_set_line_join(ctext
,m_join
);
686 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
689 //-----------------------------------------------------------------------------
690 // wxCairoBrushData implementation
691 //-----------------------------------------------------------------------------
693 wxCairoBrushData
::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
694 : wxGraphicsObjectRefData( renderer
)
699 wxCairoBrushData
::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
700 : wxGraphicsObjectRefData(renderer
)
704 m_red
= brush
.GetColour().Red()/255.0;
705 m_green
= brush
.GetColour().Green()/255.0;
706 m_blue
= brush
.GetColour().Blue()/255.0;
707 m_alpha
= brush
.GetColour().Alpha()/255.0;
709 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
711 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
712 brush.GetColour().Green() , brush.GetColour().Blue() ) );
714 else if ( brush.IsHatch() )
716 HatchStyle style = HatchStyleHorizontal;
717 switch( brush.GetStyle() )
719 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
720 style = HatchStyleBackwardDiagonal;
722 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
723 style = HatchStyleDiagonalCross;
725 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
726 style = HatchStyleForwardDiagonal;
728 case wxBRUSHSTYLE_CROSS_HATCH :
729 style = HatchStyleCross;
731 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
732 style = HatchStyleHorizontal;
734 case wxBRUSHSTYLE_VERTICAL_HATCH :
735 style = HatchStyleVertical;
739 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
740 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
744 wxBitmap* bmp = brush.GetStipple();
745 if ( bmp && bmp->IsOk() )
747 wxDELETE( m_brushImage );
748 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
749 m_brush = new TextureBrush(m_brushImage);
755 wxCairoBrushData
::~wxCairoBrushData ()
758 cairo_pattern_destroy(m_brushPattern
);
761 void wxCairoBrushData
::Apply( wxGraphicsContext
* context
)
763 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
764 if ( m_brushPattern
)
766 cairo_set_source(ctext
,m_brushPattern
);
770 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
774 void wxCairoBrushData
::AddGradientStops(const wxGraphicsGradientStops
& stops
)
776 // loop over all the stops, they include the beginning and ending ones
777 const unsigned numStops
= stops
.GetCount();
778 for ( unsigned n
= 0; n
< numStops
; n
++ )
780 const wxGraphicsGradientStop stop
= stops
.Item(n
);
782 const wxColour col
= stop
.GetColour();
784 cairo_pattern_add_color_stop_rgba
795 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
796 wxT("Couldn't create cairo pattern"));
800 wxCairoBrushData
::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
801 wxDouble x2
, wxDouble y2
,
802 const wxGraphicsGradientStops
& stops
)
804 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
806 AddGradientStops(stops
);
810 wxCairoBrushData
::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
811 wxDouble xc
, wxDouble yc
,
813 const wxGraphicsGradientStops
& stops
)
815 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
817 AddGradientStops(stops
);
820 void wxCairoBrushData
::Init()
822 m_brushPattern
= NULL
;
825 //-----------------------------------------------------------------------------
826 // wxCairoFontData implementation
827 //-----------------------------------------------------------------------------
829 void wxCairoFontData
::InitColour(const wxColour
& col
)
831 m_red
= col
.Red()/255.0;
832 m_green
= col
.Green()/255.0;
833 m_blue
= col
.Blue()/255.0;
834 m_alpha
= col
.Alpha()/255.0;
838 wxCairoFontData
::InitFontComponents(const wxString
& facename
,
839 cairo_font_slant_t slant
,
840 cairo_font_weight_t weight
)
842 m_fontName
= facename
.mb_str(wxConvUTF8
);
847 wxCairoFontData
::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
848 const wxColour
& col
)
849 : wxGraphicsObjectRefData(renderer
)
856 m_size
= font
.GetPointSize();
859 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
860 #elif defined(__WXGTK__)
865 font
.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC
866 : CAIRO_FONT_SLANT_NORMAL
,
867 font
.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD
868 : CAIRO_FONT_WEIGHT_NORMAL
873 wxCairoFontData
::wxCairoFontData(wxGraphicsRenderer
* renderer
,
875 const wxString
& facename
,
877 const wxColour
& col
) :
878 wxGraphicsObjectRefData(renderer
)
882 // Resolution for Cairo image surfaces is 72 DPI meaning that the sizes in
883 // points and pixels are identical, so we can just pass the size in pixels
884 // directly to cairo_set_font_size().
885 m_size
= sizeInPixels
;
887 #if defined(__WXMAC__)
891 // There is no need to set m_underlined under wxGTK in this case, it can
892 // only be used if m_font != NULL.
897 flags
& wxFONTFLAG_ITALIC ? CAIRO_FONT_SLANT_ITALIC
898 : CAIRO_FONT_SLANT_NORMAL
,
899 flags
& wxFONTFLAG_BOLD ? CAIRO_FONT_WEIGHT_BOLD
900 : CAIRO_FONT_WEIGHT_NORMAL
904 wxCairoFontData
::~wxCairoFontData()
908 cairo_font_face_destroy( m_font
);
912 bool wxCairoFontData
::Apply( wxGraphicsContext
* context
)
914 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
915 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
919 // Nothing to do, the caller uses Pango layout functions to do
923 #elif defined(__WXMAC__)
926 cairo_set_font_face(ctext
, m_font
);
927 cairo_set_font_size(ctext
, m_size
);
932 // If we get here, we must be on a platform without native font support or
933 // we're using toy Cairo API even under wxGTK/wxMac.
934 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
935 cairo_set_font_size(ctext
, m_size
);
937 // Indicate that we don't use native fonts for the platforms which care
938 // about this (currently only wxGTK).
942 //-----------------------------------------------------------------------------
943 // wxCairoPathData implementation
944 //-----------------------------------------------------------------------------
946 wxCairoPathData
::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
947 : wxGraphicsPathData(renderer
)
951 m_pathContext
= pathcontext
;
955 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
956 m_pathContext
= cairo_create(surface
);
957 cairo_surface_destroy (surface
);
961 wxCairoPathData
::~wxCairoPathData()
963 cairo_destroy(m_pathContext
);
966 wxGraphicsObjectRefData
*wxCairoPathData
::Clone() const
968 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
969 cairo_t
* pathcontext
= cairo_create(surface
);
970 cairo_surface_destroy (surface
);
972 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
973 cairo_append_path(pathcontext
, path
);
974 cairo_path_destroy(path
);
975 return new wxCairoPathData( GetRenderer() ,pathcontext
);
979 void* wxCairoPathData
::GetNativePath() const
981 return cairo_copy_path(m_pathContext
) ;
984 void wxCairoPathData
::UnGetNativePath(void *p
) const
986 cairo_path_destroy((cairo_path_t
*)p
);
993 void wxCairoPathData
::MoveToPoint( wxDouble x
, wxDouble y
)
995 cairo_move_to(m_pathContext
,x
,y
);
998 void wxCairoPathData
::AddLineToPoint( wxDouble x
, wxDouble y
)
1000 cairo_line_to(m_pathContext
,x
,y
);
1003 void wxCairoPathData
::AddPath( const wxGraphicsPathData
* path
)
1005 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
1006 cairo_append_path(m_pathContext
, p
);
1010 void wxCairoPathData
::CloseSubpath()
1012 cairo_close_path(m_pathContext
);
1015 void wxCairoPathData
::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1017 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
1020 // gets the last point of the current path, (0,0) if not yet set
1021 void wxCairoPathData
::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1024 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
1031 void wxCairoPathData
::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1033 // as clockwise means positive in our system (y pointing downwards)
1034 // TODO make this interpretation dependent of the
1035 // real device trans
1036 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
1037 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1039 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1042 // transforms each point of this path by the matrix
1043 void wxCairoPathData
::Transform( const wxGraphicsMatrixData
* matrix
)
1045 // as we don't have a true path object, we have to apply the inverse
1046 // matrix to the context
1047 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
1048 cairo_matrix_invert( &m
);
1049 cairo_transform(m_pathContext
,&m
);
1052 // gets the bounding box enclosing all points (possibly including control points)
1053 void wxCairoPathData
::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1057 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
1081 bool wxCairoPathData
::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1083 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1084 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
1087 //-----------------------------------------------------------------------------
1088 // wxCairoMatrixData implementation
1089 //-----------------------------------------------------------------------------
1091 wxCairoMatrixData
::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
1092 : wxGraphicsMatrixData(renderer
)
1098 wxCairoMatrixData
::~wxCairoMatrixData()
1103 wxGraphicsObjectRefData
*wxCairoMatrixData
::Clone() const
1105 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
1108 // concatenates the matrix
1109 void wxCairoMatrixData
::Concat( const wxGraphicsMatrixData
*t
)
1111 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
1114 // sets the matrix to the respective values
1115 void wxCairoMatrixData
::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1116 wxDouble tx
, wxDouble ty
)
1118 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
1121 // gets the component valuess of the matrix
1122 void wxCairoMatrixData
::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1123 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1125 if (a
) *a
= m_matrix
.xx
;
1126 if (b
) *b
= m_matrix
.yx
;
1127 if (c
) *c
= m_matrix
.xy
;
1128 if (d
) *d
= m_matrix
.yy
;
1129 if (tx
) *tx
= m_matrix
.x0
;
1130 if (ty
) *ty
= m_matrix
.y0
;
1133 // makes this the inverse matrix
1134 void wxCairoMatrixData
::Invert()
1136 cairo_matrix_invert( &m_matrix
);
1139 // returns true if the elements of the transformation matrix are equal ?
1140 bool wxCairoMatrixData
::IsEqual( const wxGraphicsMatrixData
* t
) const
1142 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1144 m_matrix
.xx
== tm
->xx
&&
1145 m_matrix
.yx
== tm
->yx
&&
1146 m_matrix
.xy
== tm
->xy
&&
1147 m_matrix
.yy
== tm
->yy
&&
1148 m_matrix
.x0
== tm
->x0
&&
1149 m_matrix
.y0
== tm
->y0
) ;
1152 // return true if this is the identity matrix
1153 bool wxCairoMatrixData
::IsIdentity() const
1155 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1156 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1163 // add the translation to this matrix
1164 void wxCairoMatrixData
::Translate( wxDouble dx
, wxDouble dy
)
1166 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1169 // add the scale to this matrix
1170 void wxCairoMatrixData
::Scale( wxDouble xScale
, wxDouble yScale
)
1172 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1175 // add the rotation to this matrix (radians)
1176 void wxCairoMatrixData
::Rotate( wxDouble angle
)
1178 cairo_matrix_rotate( &m_matrix
, angle
) ;
1182 // apply the transforms
1185 // applies that matrix to the point
1186 void wxCairoMatrixData
::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1188 double lx
= *x
, ly
= *y
;
1189 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1194 // applies the matrix except for translations
1195 void wxCairoMatrixData
::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1197 double lx
= *dx
, ly
= *dy
;
1198 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1203 // returns the native representation
1204 void * wxCairoMatrixData
::GetNativeMatrix() const
1206 return (void*) &m_matrix
;
1209 // ----------------------------------------------------------------------------
1210 // wxCairoBitmap implementation
1211 // ----------------------------------------------------------------------------
1213 int wxCairoBitmapData
::InitBuffer(int width
, int height
, cairo_format_t format
)
1215 wxUnusedVar(format
); // Only really unused with Cairo < 1.6.
1217 // Determine the stride: use cairo_format_stride_for_width() if available
1218 // but fall back to 4*width for the earlier versions as this is what that
1219 // function always returns, even in latest Cairo, anyhow.
1221 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1222 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1224 stride
= cairo_format_stride_for_width(format
, width
);
1226 // All our code would totally break if stride were not a multiple of 4
1227 // so ensure this is the case.
1230 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1232 stride
+= 4 - stride
% 4;
1241 m_buffer
= new unsigned char[height
*stride
];
1246 void wxCairoBitmapData
::InitSurface(cairo_format_t format
, int stride
)
1248 m_surface
= cairo_image_surface_create_for_data(
1249 m_buffer
, format
, m_width
, m_height
, stride
);
1250 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1253 wxCairoBitmapData
::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1254 wxGraphicsBitmapData( renderer
)
1257 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1260 wxCairoBitmapData
::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsBitmapData( renderer
)
1262 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1264 #ifdef wxHAS_RAW_BITMAP
1265 // Create a surface object and copy the bitmap pixel data to it. if the
1266 // image has alpha (or a mask represented as alpha) then we'll use a
1267 // different format and iterator than if it doesn't...
1268 cairo_format_t bufferFormat
= bmp
.GetDepth() == 32
1269 #if defined(__WXGTK__) && !defined(__WXGTK3__)
1272 ? CAIRO_FORMAT_ARGB32
1273 : CAIRO_FORMAT_RGB24
;
1275 int stride
= InitBuffer(bmp
.GetWidth(), bmp
.GetHeight(), bufferFormat
);
1277 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1278 wxUint32
* data
= (wxUint32
*)m_buffer
;
1280 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1282 // use the bitmap's alpha
1284 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1285 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1287 wxAlphaPixelData
::Iterator
p(pixData
);
1288 for (int y
=0; y
<m_height
; y
++)
1290 wxAlphaPixelData
::Iterator rowStart
= p
;
1291 wxUint32
* const rowStartDst
= data
;
1292 for (int x
=0; x
<m_width
; x
++)
1294 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1295 // with alpha in the upper 8 bits, then red, then green, then
1296 // blue. The 32-bit quantities are stored native-endian.
1297 // Pre-multiplied alpha is used.
1298 unsigned char alpha
= p
.Alpha();
1302 *data
= ( alpha
<< 24
1303 | (p
.Red() * alpha
/255) << 16
1304 | (p
.Green() * alpha
/255) << 8
1305 | (p
.Blue() * alpha
/255) );
1310 data
= rowStartDst
+ stride
/ 4;
1312 p
.OffsetY(pixData
, 1);
1318 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1319 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1321 wxNativePixelData
::Iterator
p(pixData
);
1322 for (int y
=0; y
<m_height
; y
++)
1324 wxNativePixelData
::Iterator rowStart
= p
;
1325 wxUint32
* const rowStartDst
= data
;
1326 for (int x
=0; x
<m_width
; x
++)
1328 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1329 // the upper 8 bits unused. Red, Green, and Blue are stored in
1330 // the remaining 24 bits in that order. The 32-bit quantities
1331 // are stored native-endian.
1332 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1337 data
= rowStartDst
+ stride
/ 4;
1339 p
.OffsetY(pixData
, 1);
1342 #if defined(__WXMSW__) || defined(__WXGTK3__)
1343 // if there is a mask, set the alpha bytes in the target buffer to
1344 // fully transparent or fully opaque
1345 if (bmpSource
.GetMask())
1347 wxBitmap bmpMask
= bmpSource
.GetMaskBitmap();
1348 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1349 data
= (wxUint32
*)m_buffer
;
1351 pixData(bmpMask
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1352 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to mask data."));
1354 wxNativePixelData
::Iterator
p(pixData
);
1355 for (int y
=0; y
<m_height
; y
++)
1357 wxNativePixelData
::Iterator rowStart
= p
;
1358 wxUint32
* const rowStartDst
= data
;
1359 for (int x
=0; x
<m_width
; x
++)
1361 if (p
.Red()+p
.Green()+p
.Blue() == 0)
1364 *data
= (wxALPHA_OPAQUE
<< 24) | (*data
& 0x00FFFFFF);
1369 data
= rowStartDst
+ stride
/ 4;
1371 p
.OffsetY(pixData
, 1);
1376 InitSurface(bufferFormat
, stride
);
1377 #endif // wxHAS_RAW_BITMAP
1382 // Helper functions for dealing with alpha pre-multiplication.
1386 inline unsigned char Premultiply(unsigned char alpha
, unsigned char data
)
1388 return alpha ?
(data
* alpha
)/0xff : data
;
1391 inline unsigned char Unpremultiply(unsigned char alpha
, unsigned char data
)
1393 return alpha ?
(data
* 0xff)/alpha
: data
;
1396 } // anonymous namespace
1398 wxCairoBitmapData
::wxCairoBitmapData(wxGraphicsRenderer
* renderer
,
1399 const wxImage
& image
)
1400 : wxGraphicsBitmapData(renderer
)
1402 const cairo_format_t bufferFormat
= image
.HasAlpha()
1403 ? CAIRO_FORMAT_ARGB32
1404 : CAIRO_FORMAT_RGB24
;
1406 int stride
= InitBuffer(image
.GetWidth(), image
.GetHeight(), bufferFormat
);
1408 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1409 // values and not bytes becase Cairo always works with buffers in native
1411 wxUint32
* dst
= reinterpret_cast<wxUint32
*>(m_buffer
);
1412 const unsigned char* src
= image
.GetData();
1414 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1416 const unsigned char* alpha
= image
.GetAlpha();
1418 for ( int y
= 0; y
< m_height
; y
++ )
1420 wxUint32
* const rowStartDst
= dst
;
1422 for ( int x
= 0; x
< m_width
; x
++ )
1424 const unsigned char a
= *alpha
++;
1427 Premultiply(a
, src
[0]) << 16 |
1428 Premultiply(a
, src
[1]) << 8 |
1429 Premultiply(a
, src
[2]);
1433 dst
= rowStartDst
+ stride
/ 4;
1438 for ( int y
= 0; y
< m_height
; y
++ )
1440 wxUint32
* const rowStartDst
= dst
;
1442 for ( int x
= 0; x
< m_width
; x
++ )
1444 *dst
++ = src
[0] << 16 |
1450 dst
= rowStartDst
+ stride
/ 4;
1454 InitSurface(bufferFormat
, stride
);
1457 wxImage wxCairoBitmapData
::ConvertToImage() const
1459 wxImage
image(m_width
, m_height
, false /* don't clear */);
1461 // Get the surface type and format.
1462 wxCHECK_MSG( cairo_surface_get_type(m_surface
) == CAIRO_SURFACE_TYPE_IMAGE
,
1464 wxS("Can't convert non-image surface to image.") );
1466 switch ( cairo_image_surface_get_format(m_surface
) )
1468 case CAIRO_FORMAT_ARGB32
:
1472 case CAIRO_FORMAT_RGB24
:
1473 // Nothing to do, we don't use alpha by default.
1476 case CAIRO_FORMAT_A8
:
1477 case CAIRO_FORMAT_A1
:
1478 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1482 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1486 // Prepare for copying data.
1487 const wxUint32
* src
= (wxUint32
*)cairo_image_surface_get_data(m_surface
);
1488 wxCHECK_MSG( src
, wxNullImage
, wxS("Failed to get Cairo surface data.") );
1490 int stride
= cairo_image_surface_get_stride(m_surface
);
1491 wxCHECK_MSG( stride
> 0, wxNullImage
,
1492 wxS("Failed to get Cairo surface stride.") );
1494 // As we work with wxUint32 pointers and not char ones, we need to adjust
1495 // the stride accordingly. This should be lossless as the stride must be a
1496 // multiple of pixel size.
1497 wxASSERT_MSG( !(stride
% sizeof(wxUint32
)), wxS("Unexpected stride.") );
1498 stride
/= sizeof(wxUint32
);
1500 unsigned char* dst
= image
.GetData();
1501 unsigned char *alpha
= image
.GetAlpha();
1504 // We need to also copy alpha and undo the pre-multiplication as Cairo
1505 // stores pre-multiplied values in this format while wxImage does not.
1506 for ( int y
= 0; y
< m_height
; y
++ )
1508 const wxUint32
* const rowStart
= src
;
1509 for ( int x
= 0; x
< m_width
; x
++ )
1511 const wxUint32 argb
= *src
++;
1513 *alpha
++ = (argb
& 0xff000000) >> 24;
1515 // Copy the RGB data undoing the pre-multiplication.
1516 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x00ff0000) >> 16);
1517 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x0000ff00) >> 8);
1518 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x000000ff));
1521 src
= rowStart
+ stride
;
1526 // Things are pretty simple in this case, just copy RGB bytes.
1527 for ( int y
= 0; y
< m_height
; y
++ )
1529 const wxUint32
* const rowStart
= src
;
1530 for ( int x
= 0; x
< m_width
; x
++ )
1532 const wxUint32 argb
= *src
++;
1534 *dst
++ = (argb
& 0x00ff0000) >> 16;
1535 *dst
++ = (argb
& 0x0000ff00) >> 8;
1536 *dst
++ = (argb
& 0x000000ff);
1539 src
= rowStart
+ stride
;
1546 #endif // wxUSE_IMAGE
1548 wxCairoBitmapData
::~wxCairoBitmapData()
1551 cairo_pattern_destroy(m_pattern
);
1554 cairo_surface_destroy(m_surface
);
1559 //-----------------------------------------------------------------------------
1560 // wxCairoContext implementation
1561 //-----------------------------------------------------------------------------
1563 class wxCairoOffsetHelper
1566 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1571 cairo_translate( m_ctx
, 0.5, 0.5 );
1573 ~wxCairoOffsetHelper( )
1576 cairo_translate( m_ctx
, -0.5, -0.5 );
1583 wxCairoContext
::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1584 : wxGraphicsContext(renderer
)
1587 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1588 // text rendering when printing using Cairo. Switch it to MM_TEXT
1589 // map mode to avoid this problem.
1590 HDC hdc
= (HDC
)dc
.GetHDC();
1591 ::SetMapMode(hdc
, MM_TEXT
);
1592 m_mswSurface
= cairo_win32_printing_surface_create(hdc
);
1593 Init( cairo_create(m_mswSurface
) );
1597 const wxDCImpl
*impl
= dc
.GetImpl();
1598 Init( (cairo_t
*) impl
->GetCairoContext() );
1600 wxSize sz
= dc
.GetSize();
1604 wxPoint org
= dc
.GetDeviceOrigin();
1605 cairo_translate( m_context
, org
.x
, org
.y
);
1608 dc
.GetUserScale( &sx
, &sy
);
1610 // TODO: Determine if these fixes are needed on other platforms too.
1611 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1612 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1616 dc
.GetLogicalScale( &lsx
, &lsy
);
1620 cairo_scale( m_context
, sx
, sy
);
1622 org
= dc
.GetLogicalOrigin();
1623 cairo_translate( m_context
, -org
.x
, -org
.y
);
1626 wxCairoContext
::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1627 : wxGraphicsContext(renderer
)
1630 dc
.GetSize( &width
, &height
);
1634 m_enableOffset
= true;
1637 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1638 Init( cairo_create(m_mswSurface
) );
1642 cairo_t
* cr
= static_cast<cairo_t
*>(dc
.GetImpl()->GetCairoContext());
1645 #elif defined __WXGTK20__
1646 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1647 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1650 wxGraphicsMatrix matrix
= CreateMatrix();
1652 wxPoint org
= dc
.GetDeviceOrigin();
1653 matrix
.Translate( org
.x
, org
.y
);
1655 org
= dc
.GetLogicalOrigin();
1656 matrix
.Translate( -org
.x
, -org
.y
);
1659 dc
.GetUserScale( &sx
, &sy
);
1660 matrix
.Scale( sx
, sy
);
1662 ConcatTransform( matrix
);
1667 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1668 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1669 Init( cairo_create( surface
) );
1670 cairo_surface_destroy( surface
);
1674 wxCairoContext
::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1675 : wxGraphicsContext(renderer
)
1678 dc
.GetSize( &width
, &height
);
1682 m_enableOffset
= true;
1686 HDC hdc
= (HDC
)dc
.GetHDC();
1688 HBITMAP bitmap
= (HBITMAP
)GetCurrentObject(hdc
, OBJ_BITMAP
);
1691 bool hasBitmap
= false;
1693 // cairo_win32_surface_create creates a 24-bit bitmap,
1694 // so if we have alpha, we need to create a 32-bit surface instead.
1695 if (!GetObject(bitmap
, sizeof(info
), &info
) || info
.bmBitsPixel
< 32)
1696 m_mswSurface
= cairo_win32_surface_create(hdc
);
1699 m_mswSurface
= cairo_image_surface_create_for_data((unsigned char*)info
.bmBits
,
1700 CAIRO_FORMAT_ARGB32
,
1706 Init( cairo_create(m_mswSurface
) );
1707 // If we've created a image surface, we need to flip the Y axis so that
1708 // all drawing will appear right side up.
1710 cairo_matrix_t matrix
;
1711 cairo_matrix_init(&matrix
, 1.0, 0.0, 0.0, -1.0, 0.0, height
);
1712 cairo_set_matrix(m_context
, &matrix
);
1717 cairo_t
* cr
= static_cast<cairo_t
*>(dc
.GetImpl()->GetCairoContext());
1720 #elif defined __WXGTK20__
1721 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1722 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1725 wxGraphicsMatrix matrix
= CreateMatrix();
1727 wxPoint org
= dc
.GetDeviceOrigin();
1728 matrix
.Translate( org
.x
, org
.y
);
1730 org
= dc
.GetLogicalOrigin();
1731 matrix
.Translate( -org
.x
, -org
.y
);
1734 dc
.GetUserScale( &sx
, &sy
);
1735 matrix
.Scale( sx
, sy
);
1737 ConcatTransform( matrix
);
1742 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1743 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1744 Init( cairo_create( surface
) );
1745 cairo_surface_destroy( surface
);
1750 wxCairoContext
::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkWindow
*window
)
1751 : wxGraphicsContext(renderer
)
1753 Init( gdk_cairo_create( window
) );
1756 m_width
= gdk_window_get_width(window
);
1757 m_height
= gdk_window_get_height(window
);
1760 gdk_drawable_get_size(window
, &width
, &height
);
1768 wxCairoContext
::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1769 : wxGraphicsContext(renderer
)
1771 m_mswSurface
= cairo_win32_surface_create(handle
);
1772 Init( cairo_create(m_mswSurface
) );
1779 wxCairoContext
::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1780 : wxGraphicsContext(renderer
)
1787 wxCairoContext
::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1788 : wxGraphicsContext(renderer
)
1790 , m_mswWindowHDC(GetHwndOf(window
))
1793 m_enableOffset
= true;
1795 // something along these lines (copied from dcclient)
1797 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1798 // code should still be able to create wxClientDCs for them, so we will
1799 // use the parent window here then.
1800 if (window
->m_wxwindow
== NULL
)
1802 window
= window
->GetParent();
1805 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1807 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1809 wxSize sz
= window
->GetSize();
1815 m_mswSurface
= cairo_win32_surface_create((HDC
)m_mswWindowHDC
);
1816 Init(cairo_create(m_mswSurface
));
1821 wxCairoContext
::wxCairoContext(wxGraphicsRenderer
* renderer
) :
1822 wxGraphicsContext(renderer
)
1827 wxCairoContext
::~wxCairoContext()
1833 cairo_destroy(m_context
);
1837 cairo_surface_destroy(m_mswSurface
);
1841 void wxCairoContext
::Init(cairo_t
*context
)
1843 m_context
= context
;
1849 void wxCairoContext
::Clip( const wxRegion
& region
)
1851 // Create a path with all the rectangles in the region
1852 wxGraphicsPath path
= GetRenderer()->CreatePath();
1853 wxRegionIterator
ri(region
);
1856 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1860 // Put it in the context
1861 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1862 cairo_append_path(m_context
, cp
);
1864 // clip to that path
1865 cairo_clip(m_context
);
1866 path
.UnGetNativePath(cp
);
1869 void wxCairoContext
::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1871 // Create a path with this rectangle
1872 wxGraphicsPath path
= GetRenderer()->CreatePath();
1873 path
.AddRectangle(x
,y
,w
,h
);
1875 // Put it in the context
1876 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1877 cairo_append_path(m_context
, cp
);
1879 // clip to that path
1880 cairo_clip(m_context
);
1881 path
.UnGetNativePath(cp
);
1884 void wxCairoContext
::ResetClip()
1886 cairo_reset_clip(m_context
);
1890 void wxCairoContext
::StrokePath( const wxGraphicsPath
& path
)
1892 if ( !m_pen
.IsNull() )
1894 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1895 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1896 cairo_append_path(m_context
,cp
);
1897 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1898 cairo_stroke(m_context
);
1899 path
.UnGetNativePath(cp
);
1903 void wxCairoContext
::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1905 if ( !m_brush
.IsNull() )
1907 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1908 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1909 cairo_append_path(m_context
,cp
);
1910 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1911 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1912 cairo_fill(m_context
);
1913 path
.UnGetNativePath(cp
);
1917 void wxCairoContext
::Rotate( wxDouble angle
)
1919 cairo_rotate(m_context
,angle
);
1922 void wxCairoContext
::Translate( wxDouble dx
, wxDouble dy
)
1924 cairo_translate(m_context
,dx
,dy
);
1927 void wxCairoContext
::Scale( wxDouble xScale
, wxDouble yScale
)
1929 cairo_scale(m_context
,xScale
,yScale
);
1932 // concatenates this transform with the current transform of this context
1933 void wxCairoContext
::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1935 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1938 // sets the transform of this context
1939 void wxCairoContext
::SetTransform( const wxGraphicsMatrix
& matrix
)
1941 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1944 // gets the matrix of this context
1945 wxGraphicsMatrix wxCairoContext
::GetTransform() const
1947 wxGraphicsMatrix matrix
= CreateMatrix();
1948 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1954 void wxCairoContext
::PushState()
1956 cairo_save(m_context
);
1959 void wxCairoContext
::PopState()
1961 cairo_restore(m_context
);
1964 void wxCairoContext
::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1966 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1967 DrawBitmap(bitmap
, x
, y
, w
, h
);
1971 void wxCairoContext
::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1975 // In case we're scaling the image by using a width and height different
1976 // than the bitmap's size create a pattern transformation on the surface and
1977 // draw the transformed pattern.
1978 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1979 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1980 wxSize size
= data
->GetSize();
1982 wxDouble scaleX
= w
/ size
.GetWidth();
1983 wxDouble scaleY
= h
/ size
.GetHeight();
1985 // prepare to draw the image
1986 cairo_translate(m_context
, x
, y
);
1987 cairo_scale(m_context
, scaleX
, scaleY
);
1988 cairo_set_source(m_context
, pattern
);
1989 // use the original size here since the context is scaled already...
1990 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1991 // fill the rectangle using the pattern
1992 cairo_fill(m_context
);
1997 void wxCairoContext
::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1999 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
2000 // start using the Cairo backend on other platforms then we may need to
2001 // fiddle with this...
2002 DrawBitmap(icon
, x
, y
, w
, h
);
2006 void wxCairoContext
::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
2008 wxCHECK_RET( !m_font
.IsNull(),
2009 wxT("wxCairoContext::DrawText - no valid font set") );
2014 const wxCharBuffer data
= str
.utf8_str();
2018 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this) )
2021 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2022 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2023 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2024 pango_layout_set_text(layout
, data
, data
.length());
2025 font
.GTKSetPangoAttrs(layout
);
2027 cairo_move_to(m_context
, x
, y
);
2028 pango_cairo_show_layout (m_context
, layout
);
2030 g_object_unref (layout
);
2032 // Don't use Cairo text API, we already did everything.
2037 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
2038 // the position we move to by the ascent.
2039 cairo_font_extents_t fe
;
2040 cairo_font_extents(m_context
, &fe
);
2041 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
2043 cairo_show_text(m_context
, data
);
2046 void wxCairoContext
::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2047 wxDouble
*descent
, wxDouble
*externalLeading
) const
2049 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
2057 if ( externalLeading
)
2058 *externalLeading
= 0;
2063 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this) )
2068 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2069 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2070 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2071 const wxCharBuffer data
= str
.utf8_str();
2076 pango_layout_set_text(layout
, data
, data
.length());
2077 pango_layout_get_pixel_size (layout
, &w
, &h
);
2084 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
2085 int baseline
= pango_layout_iter_get_baseline(iter
);
2086 pango_layout_iter_free(iter
);
2087 *descent
= h
- PANGO_PIXELS(baseline
);
2089 g_object_unref (layout
);
2096 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
2097 cairo_text_extents_t te
;
2098 cairo_text_extents(m_context
, buf
, &te
);
2102 if (height
|| descent
|| externalLeading
)
2104 cairo_font_extents_t fe
;
2105 cairo_font_extents(m_context
, &fe
);
2107 // some backends have negative descents
2109 if ( fe
.descent
< 0 )
2110 fe
.descent
= -fe
.descent
;
2112 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
2114 // some backends are broken re height ... (eg currently ATSUI)
2115 fe
.height
= fe
.ascent
+ fe
.descent
;
2119 *height
= fe
.height
;
2121 *descent
= fe
.descent
;
2122 if ( externalLeading
)
2123 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
2127 void wxCairoContext
::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2130 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
2132 const wxCharBuffer data
= text
.utf8_str();
2136 PangoLayout
* layout
= pango_cairo_create_layout(m_context
);
2137 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2138 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2139 pango_layout_set_text(layout
, data
, data
.length());
2140 PangoLayoutIter
* iter
= pango_layout_get_iter(layout
);
2141 PangoRectangle rect
;
2143 pango_layout_iter_get_cluster_extents(iter
, NULL
, &rect
);
2145 widths
.Add(PANGO_PIXELS(w
));
2146 } while (pango_layout_iter_next_cluster(iter
));
2147 pango_layout_iter_free(iter
);
2148 g_object_unref(layout
);
2150 size_t i
= widths
.GetCount();
2151 const size_t len
= text
.length();
2153 widths
.Add(PANGO_PIXELS(w
));
2159 void * wxCairoContext
::GetNativeContext()
2164 bool wxCairoContext
::SetAntialiasMode(wxAntialiasMode antialias
)
2166 if (m_antialias
== antialias
)
2169 m_antialias
= antialias
;
2171 cairo_antialias_t antialiasMode
;
2174 case wxANTIALIAS_DEFAULT
:
2175 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
2177 case wxANTIALIAS_NONE
:
2178 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
2183 cairo_set_antialias(m_context
, antialiasMode
);
2187 bool wxCairoContext
::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
2193 bool wxCairoContext
::SetCompositionMode(wxCompositionMode op
)
2195 if ( m_composition
== op
)
2199 cairo_operator_t cop
;
2202 case wxCOMPOSITION_CLEAR
:
2203 cop
= CAIRO_OPERATOR_CLEAR
;
2205 case wxCOMPOSITION_SOURCE
:
2206 cop
= CAIRO_OPERATOR_SOURCE
;
2208 case wxCOMPOSITION_OVER
:
2209 cop
= CAIRO_OPERATOR_OVER
;
2211 case wxCOMPOSITION_IN
:
2212 cop
= CAIRO_OPERATOR_IN
;
2214 case wxCOMPOSITION_OUT
:
2215 cop
= CAIRO_OPERATOR_OUT
;
2217 case wxCOMPOSITION_ATOP
:
2218 cop
= CAIRO_OPERATOR_ATOP
;
2220 case wxCOMPOSITION_DEST
:
2221 cop
= CAIRO_OPERATOR_DEST
;
2223 case wxCOMPOSITION_DEST_OVER
:
2224 cop
= CAIRO_OPERATOR_DEST_OVER
;
2226 case wxCOMPOSITION_DEST_IN
:
2227 cop
= CAIRO_OPERATOR_DEST_IN
;
2229 case wxCOMPOSITION_DEST_OUT
:
2230 cop
= CAIRO_OPERATOR_DEST_OUT
;
2232 case wxCOMPOSITION_DEST_ATOP
:
2233 cop
= CAIRO_OPERATOR_DEST_ATOP
;
2235 case wxCOMPOSITION_XOR
:
2236 cop
= CAIRO_OPERATOR_XOR
;
2238 case wxCOMPOSITION_ADD
:
2239 cop
= CAIRO_OPERATOR_ADD
;
2244 cairo_set_operator(m_context
, cop
);
2248 void wxCairoContext
::BeginLayer(wxDouble opacity
)
2250 m_layerOpacities
.push_back(opacity
);
2251 cairo_push_group(m_context
);
2254 void wxCairoContext
::EndLayer()
2256 float opacity
= m_layerOpacities
.back();
2257 m_layerOpacities
.pop_back();
2258 cairo_pop_group_to_source(m_context
);
2259 cairo_paint_with_alpha(m_context
,opacity
);
2262 //-----------------------------------------------------------------------------
2263 // wxCairoRenderer declaration
2264 //-----------------------------------------------------------------------------
2266 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
2269 wxCairoRenderer() {}
2271 virtual ~wxCairoRenderer() {}
2275 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2276 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2277 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2279 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2281 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2283 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2284 #endif // wxUSE_IMAGE
2286 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2288 virtual wxGraphicsContext
* CreateMeasuringContext();
2290 #if wxUSE_ENH_METAFILE
2291 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
2296 virtual wxGraphicsPath
CreatePath();
2300 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2301 wxDouble tx
=0.0, wxDouble ty
=0.0);
2304 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2306 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2308 virtual wxGraphicsBrush
2309 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2310 wxDouble x2
, wxDouble y2
,
2311 const wxGraphicsGradientStops
& stops
);
2313 virtual wxGraphicsBrush
2314 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2315 wxDouble xc
, wxDouble yc
,
2317 const wxGraphicsGradientStops
& stops
);
2320 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2321 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2322 const wxString
& facename
,
2323 int flags
= wxFONTFLAG_DEFAULT
,
2324 const wxColour
& col
= *wxBLACK
);
2326 // create a native bitmap representation
2327 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
2329 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2330 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2331 #endif // wxUSE_IMAGE
2333 // create a graphics bitmap from a native bitmap
2334 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2336 // create a subimage from a native image representation
2337 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
2340 bool EnsureIsLoaded();
2343 friend class wxCairoModule
;
2346 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
2349 //-----------------------------------------------------------------------------
2350 // wxCairoRenderer implementation
2351 //-----------------------------------------------------------------------------
2353 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
2355 static wxCairoRenderer gs_cairoGraphicsRenderer
;
2356 // temporary hack to allow creating a cairo context on any platform
2357 extern wxGraphicsRenderer
* gCairoRenderer
;
2358 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
2360 bool wxCairoRenderer
::EnsureIsLoaded()
2364 return wxCairoInit();
2370 void wxCairoRenderer
::Load()
2375 void wxCairoRenderer
::Unload()
2380 // call EnsureIsLoaded() and return returnOnFail value if it fails
2381 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2382 if ( !EnsureIsLoaded() ) \
2383 return (returnOnFail)
2385 wxGraphicsContext
* wxCairoRenderer
::CreateContext( const wxWindowDC
& dc
)
2387 ENSURE_LOADED_OR_RETURN(NULL
);
2388 return new wxCairoContext(this,dc
);
2391 wxGraphicsContext
* wxCairoRenderer
::CreateContext( const wxMemoryDC
& dc
)
2393 ENSURE_LOADED_OR_RETURN(NULL
);
2394 return new wxCairoContext(this,dc
);
2397 wxGraphicsContext
* wxCairoRenderer
::CreateContext( const wxPrinterDC
& dc
)
2399 ENSURE_LOADED_OR_RETURN(NULL
);
2400 return new wxCairoContext(this, dc
);
2404 #if wxUSE_ENH_METAFILE
2405 wxGraphicsContext
* wxCairoRenderer
::CreateContext( const wxEnhMetaFileDC
& WXUNUSED(dc
) )
2407 ENSURE_LOADED_OR_RETURN(NULL
);
2413 wxGraphicsContext
* wxCairoRenderer
::CreateContextFromNativeContext( void * context
)
2415 ENSURE_LOADED_OR_RETURN(NULL
);
2417 return new wxCairoContext(this,(HDC
)context
);
2419 return new wxCairoContext(this,(cairo_t
*)context
);
2424 wxGraphicsContext
* wxCairoRenderer
::CreateContextFromNativeWindow( void * window
)
2426 ENSURE_LOADED_OR_RETURN(NULL
);
2428 return new wxCairoContext(this, static_cast<GdkWindow
*>(window
));
2430 wxUnusedVar(window
);
2436 wxGraphicsContext
* wxCairoRenderer
::CreateContextFromImage(wxImage
& image
)
2438 return new wxCairoImageContext(this, image
);
2440 #endif // wxUSE_IMAGE
2442 wxGraphicsContext
* wxCairoRenderer
::CreateMeasuringContext()
2444 ENSURE_LOADED_OR_RETURN(NULL
);
2446 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2453 wxGraphicsContext
* wxCairoRenderer
::CreateContext( wxWindow
* window
)
2455 ENSURE_LOADED_OR_RETURN(NULL
);
2456 return new wxCairoContext(this, window
);
2461 wxGraphicsPath wxCairoRenderer
::CreatePath()
2463 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2464 wxGraphicsPath path
;
2465 path
.SetRefData( new wxCairoPathData(this) );
2472 wxGraphicsMatrix wxCairoRenderer
::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2473 wxDouble tx
, wxDouble ty
)
2476 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2478 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
2479 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2484 wxGraphicsPen wxCairoRenderer
::CreatePen(const wxPen
& pen
)
2486 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2487 if ( !pen
.IsOk() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
2488 return wxNullGraphicsPen
;
2492 p
.SetRefData(new wxCairoPenData( this, pen
));
2497 wxGraphicsBrush wxCairoRenderer
::CreateBrush(const wxBrush
& brush
)
2499 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2500 if ( !brush
.IsOk() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
2501 return wxNullGraphicsBrush
;
2505 p
.SetRefData(new wxCairoBrushData( this, brush
));
2511 wxCairoRenderer
::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2512 wxDouble x2
, wxDouble y2
,
2513 const wxGraphicsGradientStops
& stops
)
2515 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2517 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2518 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2524 wxCairoRenderer
::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2525 wxDouble xc
, wxDouble yc
, wxDouble r
,
2526 const wxGraphicsGradientStops
& stops
)
2528 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2530 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2531 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
2536 wxGraphicsFont wxCairoRenderer
::CreateFont( const wxFont
&font
, const wxColour
&col
)
2538 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2542 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2546 return wxNullGraphicsFont
;
2550 wxCairoRenderer
::CreateFont(double sizeInPixels
,
2551 const wxString
& facename
,
2553 const wxColour
& col
)
2555 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2557 wxGraphicsFont font
;
2558 font
.SetRefData(new wxCairoFontData(this, sizeInPixels
, facename
, flags
, col
));
2562 wxGraphicsBitmap wxCairoRenderer
::CreateBitmap( const wxBitmap
& bmp
)
2564 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2568 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2572 return wxNullGraphicsBitmap
;
2577 wxGraphicsBitmap wxCairoRenderer
::CreateBitmapFromImage(const wxImage
& image
)
2579 wxGraphicsBitmap bmp
;
2581 ENSURE_LOADED_OR_RETURN(bmp
);
2585 bmp
.SetRefData(new wxCairoBitmapData(this, image
));
2591 wxImage wxCairoRenderer
::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
2593 ENSURE_LOADED_OR_RETURN(wxNullImage
);
2595 const wxCairoBitmapData
* const
2596 data
= static_cast<wxCairoBitmapData
*>(bmp
.GetGraphicsData());
2598 return data ? data
->ConvertToImage() : wxNullImage
;
2601 #endif // wxUSE_IMAGE
2604 wxGraphicsBitmap wxCairoRenderer
::CreateBitmapFromNativeBitmap( void* bitmap
)
2606 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2607 if ( bitmap
!= NULL
)
2610 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2614 return wxNullGraphicsBitmap
;
2618 wxCairoRenderer
::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2619 wxDouble
WXUNUSED(x
),
2620 wxDouble
WXUNUSED(y
),
2621 wxDouble
WXUNUSED(w
),
2622 wxDouble
WXUNUSED(h
))
2624 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2625 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2626 return wxNullGraphicsBitmap
;
2629 wxGraphicsRenderer
* wxGraphicsRenderer
::GetCairoRenderer()
2631 return &gs_cairoGraphicsRenderer
;
2634 #else // !wxUSE_CAIRO
2636 wxGraphicsRenderer
* wxGraphicsRenderer
::GetCairoRenderer()
2641 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2643 // MSW and OS X have their own native default renderers, but the other ports
2644 // use Cairo by default
2645 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2646 wxGraphicsRenderer
* wxGraphicsRenderer
::GetDefaultRenderer()
2648 return GetCairoRenderer();
2650 #endif // !(__WXMSW__ || __WXOSX__)
2652 #endif // wxUSE_GRAPHICS_CONTEXT