1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_GRAPHICS_CONTEXT
20 #include "wx/graphics.h"
24 // keep cairo.h from defining dllimport as we're defining the symbols inside
25 // the wx dll in order to load them dynamically.
31 #include "wx/bitmap.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcmemory.h"
35 #include "wx/dcprint.h"
37 #include "wx/window.h"
41 #include "wx/private/graphics.h"
42 #include "wx/rawbmp.h"
43 #include "wx/vector.h"
47 //-----------------------------------------------------------------------------
48 // device context implementation
50 // more and more of the dc functionality should be implemented by calling
51 // the appropricate wxCairoContext, but we will have to do that step by step
52 // also coordinate conversions should be moved to native matrix ops
53 //-----------------------------------------------------------------------------
55 // we always stock two context states, one at entry, to be able to preserve the
56 // state we were called with, the other one after changing to HI Graphics orientation
57 // (this one is used for getting back clippings etc)
59 //-----------------------------------------------------------------------------
60 // wxGraphicsPath implementation
61 //-----------------------------------------------------------------------------
63 // TODO remove this dependency (gdiplus needs the macros)
66 #define max(a,b) (((a) > (b)) ? (a) : (b))
70 #define min(a,b) (((a) < (b)) ? (a) : (b))
75 #include <cairo-win32.h>
80 #include "wx/fontutil.h"
81 #include "wx/gtk/dc.h"
85 #include <cairo-win32.h>
89 #include "wx/osx/private.h"
90 #include <cairo-quartz.h>
91 #include <cairo-atsui.h>
94 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
97 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
100 virtual wxGraphicsObjectRefData
*Clone() const;
103 // These are the path primitives from which everything else can be constructed
106 // begins a new subpath at (x,y)
107 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
109 // adds a straight line from the current point to (x,y)
110 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
112 // adds a cubic Bezier curve from the current point, using two control points and an end point
113 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
116 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
117 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
119 // gets the last point of the current path, (0,0) if not yet set
120 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
123 virtual void AddPath( const wxGraphicsPathData
* path
);
125 // closes the current sub-path
126 virtual void CloseSubpath();
129 // These are convenience functions which - if not available natively will be assembled
130 // using the primitives from above
135 // appends a rectangle as a new closed subpath
136 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
137 // appends an ellipsis as a new closed subpath fitting the passed rectangle
138 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
140 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
141 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
144 // returns the native path
145 virtual void * GetNativePath() const ;
147 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
148 virtual void UnGetNativePath(void *p
) const;
150 // transforms each point of this path by the matrix
151 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
153 // gets the bounding box enclosing all points (possibly including control points)
154 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
156 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
159 cairo_t
* m_pathContext
;
162 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
165 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
166 virtual ~wxCairoMatrixData() ;
168 virtual wxGraphicsObjectRefData
*Clone() const ;
170 // concatenates the matrix
171 virtual void Concat( const wxGraphicsMatrixData
*t
);
173 // sets the matrix to the respective values
174 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
175 wxDouble tx
=0.0, wxDouble ty
=0.0);
177 // gets the component valuess of the matrix
178 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
179 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
181 // makes this the inverse matrix
182 virtual void Invert();
184 // returns true if the elements of the transformation matrix are equal ?
185 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
187 // return true if this is the identity matrix
188 virtual bool IsIdentity() const;
194 // add the translation to this matrix
195 virtual void Translate( wxDouble dx
, wxDouble dy
);
197 // add the scale to this matrix
198 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
200 // add the rotation to this matrix (radians)
201 virtual void Rotate( wxDouble angle
);
204 // apply the transforms
207 // applies that matrix to the point
208 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
210 // applies the matrix except for translations
211 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
213 // returns the native representation
214 virtual void * GetNativeMatrix() const;
216 cairo_matrix_t m_matrix
;
219 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
222 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
227 virtual void Apply( wxGraphicsContext
* context
);
228 virtual wxDouble
GetWidth() { return m_width
; }
238 cairo_line_cap_t m_cap
;
239 cairo_line_join_t m_join
;
242 const double *m_lengths
;
243 double *m_userLengths
;
247 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
250 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
253 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
254 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
255 ~wxCairoBrushData ();
257 virtual void Apply( wxGraphicsContext
* context
);
259 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
260 wxDouble x2
, wxDouble y2
,
261 const wxGraphicsGradientStops
& stops
);
262 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
263 wxDouble xc
, wxDouble yc
, wxDouble radius
,
264 const wxGraphicsGradientStops
& stops
);
269 // common part of Create{Linear,Radial}GradientBrush()
270 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
278 cairo_pattern_t
* m_brushPattern
;
281 class wxCairoFontData
: public wxGraphicsObjectRefData
284 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
287 virtual void Apply( wxGraphicsContext
* context
);
289 const PangoFontDescription
* GetFont() const { return m_font
; }
290 bool GetUnderlined() const { return m_underlined
; }
300 cairo_font_face_t
*m_font
;
301 #elif defined(__WXGTK__)
302 PangoFontDescription
* m_font
;
304 wxCharBuffer m_fontName
;
305 cairo_font_slant_t m_slant
;
306 cairo_font_weight_t m_weight
;
310 class wxCairoBitmapData
: public wxGraphicsObjectRefData
313 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
314 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
315 ~wxCairoBitmapData();
317 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
318 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
319 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
321 cairo_surface_t
* m_surface
;
322 cairo_pattern_t
* m_pattern
;
325 unsigned char* m_buffer
;
328 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
331 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
332 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
333 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
335 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
338 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
340 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
341 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
343 virtual ~wxCairoContext();
345 virtual bool ShouldOffset() const
347 if ( !m_enableOffset
)
351 if ( !m_pen
.IsNull() )
353 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
357 return ( penwidth
% 2 ) == 1;
360 virtual void Clip( const wxRegion
®ion
);
362 cairo_surface_t
* m_mswSurface
;
365 // clips drawings to the rect
366 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
368 // resets the clipping to original extent
369 virtual void ResetClip();
371 virtual void * GetNativeContext();
373 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
375 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
377 virtual bool SetCompositionMode(wxCompositionMode op
);
379 virtual void BeginLayer(wxDouble opacity
);
381 virtual void EndLayer();
383 virtual void StrokePath( const wxGraphicsPath
& p
);
384 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
386 virtual void Translate( wxDouble dx
, wxDouble dy
);
387 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
388 virtual void Rotate( wxDouble angle
);
390 // concatenates this transform with the current transform of this context
391 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
393 // sets the transform of this context
394 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
396 // gets the matrix of this context
397 virtual wxGraphicsMatrix
GetTransform() const;
399 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
400 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
401 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
402 virtual void PushState();
403 virtual void PopState();
405 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
406 wxDouble
*descent
, wxDouble
*externalLeading
) const;
407 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
410 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
413 void Init(cairo_t
*context
);
417 wxVector
<float> m_layerOpacities
;
419 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
422 //-----------------------------------------------------------------------------
423 // wxCairoPenData implementation
424 //-----------------------------------------------------------------------------
426 wxCairoPenData::~wxCairoPenData()
428 delete[] m_userLengths
;
431 void wxCairoPenData::Init()
434 m_userLengths
= NULL
;
439 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
440 : wxGraphicsObjectRefData(renderer
)
444 m_width
= m_pen
.GetWidth();
448 m_red
= m_pen
.GetColour().Red()/255.0;
449 m_green
= m_pen
.GetColour().Green()/255.0;
450 m_blue
= m_pen
.GetColour().Blue()/255.0;
451 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
453 switch ( m_pen
.GetCap() )
456 m_cap
= CAIRO_LINE_CAP_ROUND
;
459 case wxCAP_PROJECTING
:
460 m_cap
= CAIRO_LINE_CAP_SQUARE
;
464 m_cap
= CAIRO_LINE_CAP_BUTT
;
468 m_cap
= CAIRO_LINE_CAP_BUTT
;
472 switch ( m_pen
.GetJoin() )
475 m_join
= CAIRO_LINE_JOIN_BEVEL
;
479 m_join
= CAIRO_LINE_JOIN_MITER
;
483 m_join
= CAIRO_LINE_JOIN_ROUND
;
487 m_join
= CAIRO_LINE_JOIN_MITER
;
491 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
492 const double dotted
[] =
494 dashUnit
, dashUnit
+ 2.0
496 static const double short_dashed
[] =
500 static const double dashed
[] =
504 static const double dotted_dashed
[] =
506 9.0 , 6.0 , 3.0 , 3.0
509 switch ( m_pen
.GetStyle() )
511 case wxPENSTYLE_SOLID
:
514 case wxPENSTYLE_DOT
:
515 m_count
= WXSIZEOF(dotted
);
516 m_userLengths
= new double[ m_count
] ;
517 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
518 m_lengths
= m_userLengths
;
521 case wxPENSTYLE_LONG_DASH
:
523 m_count
= WXSIZEOF(dashed
);
526 case wxPENSTYLE_SHORT_DASH
:
527 m_lengths
= short_dashed
;
528 m_count
= WXSIZEOF(short_dashed
);
531 case wxPENSTYLE_DOT_DASH
:
532 m_lengths
= dotted_dashed
;
533 m_count
= WXSIZEOF(dotted_dashed
);
536 case wxPENSTYLE_USER_DASH
:
539 m_count
= m_pen
.GetDashes( &wxdashes
) ;
540 if ((wxdashes
!= NULL
) && (m_count
> 0))
542 m_userLengths
= new double[m_count
] ;
543 for ( int i
= 0 ; i
< m_count
; ++i
)
545 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
547 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
548 m_userLengths
[i
] = dashUnit
+ 2.0 ;
549 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
550 m_userLengths
[i
] = dashUnit
;
553 m_lengths
= m_userLengths
;
556 case wxPENSTYLE_STIPPLE
:
559 wxBitmap* bmp = pen.GetStipple();
560 if ( bmp && bmp->IsOk() )
562 wxDELETE( m_penImage );
563 wxDELETE( m_penBrush );
564 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
565 m_penBrush = new TextureBrush(m_penImage);
566 m_pen->SetBrush( m_penBrush );
572 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
573 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
576 wxDELETE( m_penBrush );
577 HatchStyle style = HatchStyleHorizontal;
578 switch( pen.GetStyle() )
580 case wxPENSTYLE_BDIAGONAL_HATCH :
581 style = HatchStyleBackwardDiagonal;
583 case wxPENSTYLE_CROSSDIAG_HATCH :
584 style = HatchStyleDiagonalCross;
586 case wxPENSTYLE_FDIAGONAL_HATCH :
587 style = HatchStyleForwardDiagonal;
589 case wxPENSTYLE_CROSS_HATCH :
590 style = HatchStyleCross;
592 case wxPENSTYLE_HORIZONTAL_HATCH :
593 style = HatchStyleHorizontal;
595 case wxPENSTYLE_VERTICAL_HATCH :
596 style = HatchStyleVertical;
600 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
601 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
602 m_pen->SetBrush( m_penBrush )
609 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
611 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
612 cairo_set_line_width(ctext
,m_width
);
613 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
614 cairo_set_line_cap(ctext
,m_cap
);
615 cairo_set_line_join(ctext
,m_join
);
616 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
619 //-----------------------------------------------------------------------------
620 // wxCairoBrushData implementation
621 //-----------------------------------------------------------------------------
623 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
624 : wxGraphicsObjectRefData( renderer
)
629 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
630 : wxGraphicsObjectRefData(renderer
)
634 m_red
= brush
.GetColour().Red()/255.0;
635 m_green
= brush
.GetColour().Green()/255.0;
636 m_blue
= brush
.GetColour().Blue()/255.0;
637 m_alpha
= brush
.GetColour().Alpha()/255.0;
639 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
641 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
642 brush.GetColour().Green() , brush.GetColour().Blue() ) );
644 else if ( brush.IsHatch() )
646 HatchStyle style = HatchStyleHorizontal;
647 switch( brush.GetStyle() )
649 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
650 style = HatchStyleBackwardDiagonal;
652 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
653 style = HatchStyleDiagonalCross;
655 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
656 style = HatchStyleForwardDiagonal;
658 case wxBRUSHSTYLE_CROSS_HATCH :
659 style = HatchStyleCross;
661 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
662 style = HatchStyleHorizontal;
664 case wxBRUSHSTYLE_VERTICAL_HATCH :
665 style = HatchStyleVertical;
669 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
670 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
674 wxBitmap* bmp = brush.GetStipple();
675 if ( bmp && bmp->IsOk() )
677 wxDELETE( m_brushImage );
678 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
679 m_brush = new TextureBrush(m_brushImage);
685 wxCairoBrushData::~wxCairoBrushData ()
688 cairo_pattern_destroy(m_brushPattern
);
691 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
693 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
694 if ( m_brushPattern
)
696 cairo_set_source(ctext
,m_brushPattern
);
700 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
704 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
706 // loop over all the stops, they include the beginning and ending ones
707 const unsigned numStops
= stops
.GetCount();
708 for ( unsigned n
= 0; n
< numStops
; n
++ )
710 const wxGraphicsGradientStop stop
= stops
.Item(n
);
712 const wxColour col
= stop
.GetColour();
714 cairo_pattern_add_color_stop_rgba
725 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
726 wxT("Couldn't create cairo pattern"));
730 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
731 wxDouble x2
, wxDouble y2
,
732 const wxGraphicsGradientStops
& stops
)
734 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
736 AddGradientStops(stops
);
740 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
741 wxDouble xc
, wxDouble yc
,
743 const wxGraphicsGradientStops
& stops
)
745 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
747 AddGradientStops(stops
);
750 void wxCairoBrushData::Init()
752 m_brushPattern
= NULL
;
755 //-----------------------------------------------------------------------------
756 // wxCairoFontData implementation
757 //-----------------------------------------------------------------------------
759 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
760 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
762 m_red
= col
.Red()/255.0;
763 m_green
= col
.Green()/255.0;
764 m_blue
= col
.Blue()/255.0;
765 m_alpha
= col
.Alpha()/255.0;
766 m_size
= font
.GetPointSize();
767 m_underlined
= font
.GetUnderlined();
770 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
771 #elif defined(__WXGTK__)
772 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
774 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
775 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
776 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
780 wxCairoFontData::~wxCairoFontData()
783 cairo_font_face_destroy( m_font
);
784 #elif defined(__WXGTK__)
785 pango_font_description_free( m_font
);
790 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
792 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
793 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
795 // the rest is done using Pango layouts
796 #elif defined(__WXMAC__)
797 cairo_set_font_face(ctext
, m_font
);
798 cairo_set_font_size(ctext
, m_size
);
800 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
801 cairo_set_font_size(ctext
, m_size
);
805 //-----------------------------------------------------------------------------
806 // wxCairoPathData implementation
807 //-----------------------------------------------------------------------------
809 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
810 : wxGraphicsPathData(renderer
)
814 m_pathContext
= pathcontext
;
818 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
819 m_pathContext
= cairo_create(surface
);
820 cairo_surface_destroy (surface
);
824 wxCairoPathData::~wxCairoPathData()
826 cairo_destroy(m_pathContext
);
829 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
831 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
832 cairo_t
* pathcontext
= cairo_create(surface
);
833 cairo_surface_destroy (surface
);
835 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
836 cairo_append_path(pathcontext
, path
);
837 cairo_path_destroy(path
);
838 return new wxCairoPathData( GetRenderer() ,pathcontext
);
842 void* wxCairoPathData::GetNativePath() const
844 return cairo_copy_path(m_pathContext
) ;
847 void wxCairoPathData::UnGetNativePath(void *p
) const
849 cairo_path_destroy((cairo_path_t
*)p
);
856 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
858 cairo_move_to(m_pathContext
,x
,y
);
861 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
863 cairo_line_to(m_pathContext
,x
,y
);
866 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
868 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
869 cairo_append_path(m_pathContext
, p
);
873 void wxCairoPathData::CloseSubpath()
875 cairo_close_path(m_pathContext
);
878 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
880 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
883 // gets the last point of the current path, (0,0) if not yet set
884 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
887 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
894 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
896 // as clockwise means positive in our system (y pointing downwards)
897 // TODO make this interpretation dependent of the
899 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
900 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
902 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
905 // transforms each point of this path by the matrix
906 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
908 // as we don't have a true path object, we have to apply the inverse
909 // matrix to the context
910 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
911 cairo_matrix_invert( &m
);
912 cairo_transform(m_pathContext
,&m
);
915 // gets the bounding box enclosing all points (possibly including control points)
916 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
920 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
944 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
946 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
947 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
950 //-----------------------------------------------------------------------------
951 // wxCairoMatrixData implementation
952 //-----------------------------------------------------------------------------
954 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
955 : wxGraphicsMatrixData(renderer
)
961 wxCairoMatrixData::~wxCairoMatrixData()
966 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
968 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
971 // concatenates the matrix
972 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
974 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
977 // sets the matrix to the respective values
978 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
979 wxDouble tx
, wxDouble ty
)
981 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
984 // gets the component valuess of the matrix
985 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
986 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
988 if (a
) *a
= m_matrix
.xx
;
989 if (b
) *b
= m_matrix
.yx
;
990 if (c
) *c
= m_matrix
.xy
;
991 if (d
) *d
= m_matrix
.yy
;
992 if (tx
) *tx
= m_matrix
.x0
;
993 if (ty
) *ty
= m_matrix
.y0
;
996 // makes this the inverse matrix
997 void wxCairoMatrixData::Invert()
999 cairo_matrix_invert( &m_matrix
);
1002 // returns true if the elements of the transformation matrix are equal ?
1003 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1005 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1007 m_matrix
.xx
== tm
->xx
&&
1008 m_matrix
.yx
== tm
->yx
&&
1009 m_matrix
.xy
== tm
->xy
&&
1010 m_matrix
.yy
== tm
->yy
&&
1011 m_matrix
.x0
== tm
->x0
&&
1012 m_matrix
.y0
== tm
->y0
) ;
1015 // return true if this is the identity matrix
1016 bool wxCairoMatrixData::IsIdentity() const
1018 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1019 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1026 // add the translation to this matrix
1027 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1029 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1032 // add the scale to this matrix
1033 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1035 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1038 // add the rotation to this matrix (radians)
1039 void wxCairoMatrixData::Rotate( wxDouble angle
)
1041 cairo_matrix_rotate( &m_matrix
, angle
) ;
1045 // apply the transforms
1048 // applies that matrix to the point
1049 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1051 double lx
= *x
, ly
= *y
;
1052 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1057 // applies the matrix except for translations
1058 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1060 double lx
= *dx
, ly
= *dy
;
1061 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1066 // returns the native representation
1067 void * wxCairoMatrixData::GetNativeMatrix() const
1069 return (void*) &m_matrix
;
1072 // wxCairoBitmap implementation
1073 //-----------------------------------------------------------------------------
1075 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1076 wxGraphicsObjectRefData( renderer
)
1079 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1082 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1084 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1086 #ifdef wxHAS_RAW_BITMAP
1087 int bw
= m_width
= bmp
.GetWidth();
1088 int bh
= m_height
= bmp
.GetHeight();
1089 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1090 m_buffer
= new unsigned char[bw
*bh
*4];
1091 wxUint32
* data
= (wxUint32
*)m_buffer
;
1092 cairo_format_t bufferFormat
;
1094 // Create a surface object and copy the bitmap pixel data to it. if the
1095 // image has alpha (or a mask represented as alpha) then we'll use a
1096 // different format and iterator than if it doesn't...
1097 if (bmpSource
.GetDepth() == 32
1099 || bmpSource
.GetMask()
1102 { // use the bitmap's alpha
1103 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1104 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1105 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1107 wxAlphaPixelData::Iterator
p(pixData
);
1108 for (int y
=0; y
<bh
; y
++)
1110 wxAlphaPixelData::Iterator rowStart
= p
;
1111 for (int x
=0; x
<bw
; x
++)
1113 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1114 // with alpha in the upper 8 bits, then red, then green, then
1115 // blue. The 32-bit quantities are stored native-endian.
1116 // Pre-multiplied alpha is used.
1117 unsigned char alpha
= p
.Alpha();
1121 *data
= ( alpha
<< 24
1122 | (p
.Red() * alpha
/255) << 16
1123 | (p
.Green() * alpha
/255) << 8
1124 | (p
.Blue() * alpha
/255) );
1129 p
.OffsetY(pixData
, 1);
1134 bufferFormat
= CAIRO_FORMAT_RGB24
;
1135 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1136 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1138 wxNativePixelData::Iterator
p(pixData
);
1139 for (int y
=0; y
<bh
; y
++)
1141 wxNativePixelData::Iterator rowStart
= p
;
1142 for (int x
=0; x
<bw
; x
++)
1144 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1145 // the upper 8 bits unused. Red, Green, and Blue are stored in
1146 // the remaining 24 bits in that order. The 32-bit quantities
1147 // are stored native-endian.
1148 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1153 p
.OffsetY(pixData
, 1);
1157 // if there is a mask, set the alpha bytes in the target buffer to
1158 // fully transparent or fully opaque
1159 if (bmpSource
.GetMask())
1161 wxBitmap bmpMask
= bmpSource
.GetMaskBitmap();
1162 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1163 data
= (wxUint32
*)m_buffer
;
1164 wxNativePixelData
pixData(bmpMask
, wxPoint(0,0), wxSize(bw
, bh
));
1165 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to mask bitmap data."));
1167 wxNativePixelData::Iterator
p(pixData
);
1168 for (int y
=0; y
<bh
; y
++)
1170 wxNativePixelData::Iterator rowStart
= p
;
1171 for (int x
=0; x
<bw
; x
++)
1173 if (p
.Red()+p
.Green()+p
.Blue() == 0)
1176 *data
= (wxALPHA_OPAQUE
<< 24) | (*data
& 0x00FFFFFF);
1181 p
.OffsetY(pixData
, 1);
1186 m_surface
= cairo_image_surface_create_for_data(
1187 m_buffer
, bufferFormat
, bw
, bh
, bw
*4);
1188 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1189 #endif // wxHAS_RAW_BITMAP
1192 wxCairoBitmapData::~wxCairoBitmapData()
1195 cairo_pattern_destroy(m_pattern
);
1198 cairo_surface_destroy(m_surface
);
1205 //-----------------------------------------------------------------------------
1206 // wxCairoContext implementation
1207 //-----------------------------------------------------------------------------
1209 class wxCairoOffsetHelper
1212 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1217 cairo_translate( m_ctx
, 0.5, 0.5 );
1219 ~wxCairoOffsetHelper( )
1222 cairo_translate( m_ctx
, -0.5, -0.5 );
1229 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1230 : wxGraphicsContext(renderer
)
1233 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1234 // text rendering when printing using Cairo. Switch it to MM_TEXT
1235 // map mode to avoid this problem.
1236 HDC hdc
= (HDC
)dc
.GetHDC();
1237 ::SetMapMode(hdc
, MM_TEXT
);
1238 m_mswSurface
= cairo_win32_printing_surface_create(hdc
);
1239 Init( cairo_create(m_mswSurface
) );
1243 const wxDCImpl
*impl
= dc
.GetImpl();
1244 Init( (cairo_t
*) impl
->GetCairoContext() );
1246 wxSize sz
= dc
.GetSize();
1250 wxPoint org
= dc
.GetDeviceOrigin();
1251 cairo_translate( m_context
, org
.x
, org
.y
);
1254 dc
.GetUserScale( &sx
, &sy
);
1256 // TODO: Determine if these fixes are needed on other platforms too.
1257 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1258 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1262 dc
.GetLogicalScale( &lsx
, &lsy
);
1266 cairo_scale( m_context
, sx
, sy
);
1268 org
= dc
.GetLogicalOrigin();
1269 cairo_translate( m_context
, -org
.x
, -org
.y
);
1272 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1273 : wxGraphicsContext(renderer
)
1276 dc
.GetSize( &width
, &height
);
1280 m_enableOffset
= true;
1283 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1284 Init( cairo_create(m_mswSurface
) );
1288 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1289 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1292 wxGraphicsMatrix matrix
= CreateMatrix();
1294 wxPoint org
= dc
.GetDeviceOrigin();
1295 matrix
.Translate( org
.x
, org
.y
);
1297 org
= dc
.GetLogicalOrigin();
1298 matrix
.Translate( -org
.x
, -org
.y
);
1301 dc
.GetUserScale( &sx
, &sy
);
1302 matrix
.Scale( sx
, sy
);
1304 ConcatTransform( matrix
);
1309 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1310 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1311 Init( cairo_create( surface
) );
1312 cairo_surface_destroy( surface
);
1316 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1317 : wxGraphicsContext(renderer
)
1320 dc
.GetSize( &width
, &height
);
1324 m_enableOffset
= true;
1327 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1328 Init( cairo_create(m_mswSurface
) );
1332 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1333 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1336 wxGraphicsMatrix matrix
= CreateMatrix();
1338 wxPoint org
= dc
.GetDeviceOrigin();
1339 matrix
.Translate( org
.x
, org
.y
);
1341 org
= dc
.GetLogicalOrigin();
1342 matrix
.Translate( -org
.x
, -org
.y
);
1345 dc
.GetUserScale( &sx
, &sy
);
1346 matrix
.Scale( sx
, sy
);
1348 ConcatTransform( matrix
);
1353 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1354 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1355 Init( cairo_create( surface
) );
1356 cairo_surface_destroy( surface
);
1361 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1362 : wxGraphicsContext(renderer
)
1364 Init( gdk_cairo_create( drawable
) );
1367 gdk_drawable_get_size( drawable
, &width
, &height
);
1374 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1375 : wxGraphicsContext(renderer
)
1377 m_mswSurface
= cairo_win32_surface_create(handle
);
1378 Init( cairo_create(m_mswSurface
) );
1385 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1386 : wxGraphicsContext(renderer
)
1393 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1394 : wxGraphicsContext(renderer
)
1396 m_enableOffset
= true;
1398 // something along these lines (copied from dcclient)
1400 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1401 // code should still be able to create wxClientDCs for them, so we will
1402 // use the parent window here then.
1403 if (window
->m_wxwindow
== NULL
)
1405 window
= window
->GetParent();
1408 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1410 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1412 wxSize sz
= window
->GetSize();
1418 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1419 Init(cairo_create(m_mswSurface
));
1424 wxCairoContext::~wxCairoContext()
1430 cairo_destroy(m_context
);
1434 cairo_surface_destroy(m_mswSurface
);
1438 void wxCairoContext::Init(cairo_t
*context
)
1440 m_context
= context
;
1446 void wxCairoContext::Clip( const wxRegion
& region
)
1448 // Create a path with all the rectangles in the region
1449 wxGraphicsPath path
= GetRenderer()->CreatePath();
1450 wxRegionIterator
ri(region
);
1453 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1457 // Put it in the context
1458 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1459 cairo_append_path(m_context
, cp
);
1461 // clip to that path
1462 cairo_clip(m_context
);
1463 path
.UnGetNativePath(cp
);
1466 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1468 // Create a path with this rectangle
1469 wxGraphicsPath path
= GetRenderer()->CreatePath();
1470 path
.AddRectangle(x
,y
,w
,h
);
1472 // Put it in the context
1473 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1474 cairo_append_path(m_context
, cp
);
1476 // clip to that path
1477 cairo_clip(m_context
);
1478 path
.UnGetNativePath(cp
);
1481 void wxCairoContext::ResetClip()
1483 cairo_reset_clip(m_context
);
1487 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1489 if ( !m_pen
.IsNull() )
1491 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1492 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1493 cairo_append_path(m_context
,cp
);
1494 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1495 cairo_stroke(m_context
);
1496 path
.UnGetNativePath(cp
);
1500 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1502 if ( !m_brush
.IsNull() )
1504 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1505 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1506 cairo_append_path(m_context
,cp
);
1507 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1508 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1509 cairo_fill(m_context
);
1510 path
.UnGetNativePath(cp
);
1514 void wxCairoContext::Rotate( wxDouble angle
)
1516 cairo_rotate(m_context
,angle
);
1519 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1521 cairo_translate(m_context
,dx
,dy
);
1524 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1526 cairo_scale(m_context
,xScale
,yScale
);
1529 // concatenates this transform with the current transform of this context
1530 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1532 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1535 // sets the transform of this context
1536 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1538 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1541 // gets the matrix of this context
1542 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1544 wxGraphicsMatrix matrix
= CreateMatrix();
1545 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1551 void wxCairoContext::PushState()
1553 cairo_save(m_context
);
1556 void wxCairoContext::PopState()
1558 cairo_restore(m_context
);
1561 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1563 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1564 DrawBitmap(bitmap
, x
, y
, w
, h
);
1568 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1572 // In case we're scaling the image by using a width and height different
1573 // than the bitmap's size create a pattern transformation on the surface and
1574 // draw the transformed pattern.
1575 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1576 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1577 wxSize size
= data
->GetSize();
1579 wxDouble scaleX
= w
/ size
.GetWidth();
1580 wxDouble scaleY
= h
/ size
.GetHeight();
1582 // prepare to draw the image
1583 cairo_translate(m_context
, x
, y
);
1584 cairo_scale(m_context
, scaleX
, scaleY
);
1585 cairo_set_source(m_context
, pattern
);
1586 // use the original size here since the context is scaled already...
1587 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1588 // fill the rectangle using the pattern
1589 cairo_fill(m_context
);
1594 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1596 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1597 // start using the Cairo backend on other platforms then we may need to
1598 // fiddle with this...
1599 DrawBitmap(icon
, x
, y
, w
, h
);
1603 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1605 wxCHECK_RET( !m_font
.IsNull(),
1606 wxT("wxCairoContext::DrawText - no valid font set") );
1611 const wxCharBuffer data
= str
.utf8_str();
1615 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1618 size_t datalen
= strlen(data
);
1620 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1621 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1622 pango_layout_set_font_description( layout
, font_data
->GetFont());
1623 pango_layout_set_text(layout
, data
, datalen
);
1625 if (font_data
->GetUnderlined())
1627 PangoAttrList
*attrs
= pango_attr_list_new();
1628 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1629 pango_attr_list_insert(attrs
, attr
);
1630 pango_layout_set_attributes(layout
, attrs
);
1631 pango_attr_list_unref(attrs
);
1634 cairo_move_to(m_context
, x
, y
);
1635 pango_cairo_show_layout (m_context
, layout
);
1637 g_object_unref (layout
);
1639 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1640 // the position we move to by the ascent.
1641 cairo_font_extents_t fe
;
1642 cairo_font_extents(m_context
, &fe
);
1643 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1645 cairo_show_text(m_context
, data
);
1649 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1650 wxDouble
*descent
, wxDouble
*externalLeading
) const
1652 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1660 if ( externalLeading
)
1661 *externalLeading
= 0;
1669 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1670 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1671 const wxCharBuffer data
= str
.utf8_str();
1676 pango_layout_set_text( layout
, data
, strlen(data
) );
1677 pango_layout_get_pixel_size (layout
, &w
, &h
);
1684 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1685 int baseline
= pango_layout_iter_get_baseline(iter
);
1686 pango_layout_iter_free(iter
);
1687 *descent
= h
- PANGO_PIXELS(baseline
);
1689 g_object_unref (layout
);
1691 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1695 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1696 cairo_text_extents_t te
;
1697 cairo_text_extents(m_context
, buf
, &te
);
1701 if (height
|| descent
|| externalLeading
)
1703 cairo_font_extents_t fe
;
1704 cairo_font_extents(m_context
, &fe
);
1706 // some backends have negative descents
1708 if ( fe
.descent
< 0 )
1709 fe
.descent
= -fe
.descent
;
1711 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1713 // some backends are broken re height ... (eg currently ATSUI)
1714 fe
.height
= fe
.ascent
+ fe
.descent
;
1718 *height
= fe
.height
;
1720 *descent
= fe
.descent
;
1721 if ( externalLeading
)
1722 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1727 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1730 widths
.Add(0, text
.length());
1732 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1740 void * wxCairoContext::GetNativeContext()
1745 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1747 if (m_antialias
== antialias
)
1750 m_antialias
= antialias
;
1752 cairo_antialias_t antialiasMode
;
1755 case wxANTIALIAS_DEFAULT
:
1756 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1758 case wxANTIALIAS_NONE
:
1759 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1764 cairo_set_antialias(m_context
, antialiasMode
);
1768 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
1774 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1776 if ( m_composition
== op
)
1780 cairo_operator_t cop
;
1783 case wxCOMPOSITION_CLEAR
:
1784 cop
= CAIRO_OPERATOR_CLEAR
;
1786 case wxCOMPOSITION_SOURCE
:
1787 cop
= CAIRO_OPERATOR_SOURCE
;
1789 case wxCOMPOSITION_OVER
:
1790 cop
= CAIRO_OPERATOR_OVER
;
1792 case wxCOMPOSITION_IN
:
1793 cop
= CAIRO_OPERATOR_IN
;
1795 case wxCOMPOSITION_OUT
:
1796 cop
= CAIRO_OPERATOR_OUT
;
1798 case wxCOMPOSITION_ATOP
:
1799 cop
= CAIRO_OPERATOR_ATOP
;
1801 case wxCOMPOSITION_DEST
:
1802 cop
= CAIRO_OPERATOR_DEST
;
1804 case wxCOMPOSITION_DEST_OVER
:
1805 cop
= CAIRO_OPERATOR_DEST_OVER
;
1807 case wxCOMPOSITION_DEST_IN
:
1808 cop
= CAIRO_OPERATOR_DEST_IN
;
1810 case wxCOMPOSITION_DEST_OUT
:
1811 cop
= CAIRO_OPERATOR_DEST_OUT
;
1813 case wxCOMPOSITION_DEST_ATOP
:
1814 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1816 case wxCOMPOSITION_XOR
:
1817 cop
= CAIRO_OPERATOR_XOR
;
1819 case wxCOMPOSITION_ADD
:
1820 cop
= CAIRO_OPERATOR_ADD
;
1825 cairo_set_operator(m_context
, cop
);
1829 void wxCairoContext::BeginLayer(wxDouble opacity
)
1831 m_layerOpacities
.push_back(opacity
);
1832 cairo_push_group(m_context
);
1835 void wxCairoContext::EndLayer()
1837 float opacity
= m_layerOpacities
.back();
1838 m_layerOpacities
.pop_back();
1839 cairo_pop_group_to_source(m_context
);
1840 cairo_paint_with_alpha(m_context
,opacity
);
1843 //-----------------------------------------------------------------------------
1844 // wxCairoRenderer declaration
1845 //-----------------------------------------------------------------------------
1847 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1850 wxCairoRenderer() {}
1852 virtual ~wxCairoRenderer() {}
1856 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1857 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1858 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1860 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1862 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1864 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1866 virtual wxGraphicsContext
* CreateMeasuringContext();
1868 #if wxUSE_ENH_METAFILE
1869 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
1874 virtual wxGraphicsPath
CreatePath();
1878 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1879 wxDouble tx
=0.0, wxDouble ty
=0.0);
1882 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1884 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1886 virtual wxGraphicsBrush
1887 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1888 wxDouble x2
, wxDouble y2
,
1889 const wxGraphicsGradientStops
& stops
);
1891 virtual wxGraphicsBrush
1892 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1893 wxDouble xc
, wxDouble yc
,
1895 const wxGraphicsGradientStops
& stops
);
1898 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1900 // create a native bitmap representation
1901 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1903 // create a graphics bitmap from a native bitmap
1904 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1906 // create a subimage from a native image representation
1907 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1910 bool EnsureIsLoaded();
1913 friend class wxCairoModule
;
1916 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1919 //-----------------------------------------------------------------------------
1920 // wxCairoRenderer implementation
1921 //-----------------------------------------------------------------------------
1923 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1925 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1926 // temporary hack to allow creating a cairo context on any platform
1927 extern wxGraphicsRenderer
* gCairoRenderer
;
1928 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1930 bool wxCairoRenderer::EnsureIsLoaded()
1934 return wxCairoInit();
1940 void wxCairoRenderer::Load()
1945 void wxCairoRenderer::Unload()
1950 // call EnsureIsLoaded() and return returnOnFail value if it fails
1951 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1952 if ( !EnsureIsLoaded() ) \
1953 return (returnOnFail)
1955 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1957 ENSURE_LOADED_OR_RETURN(NULL
);
1958 return new wxCairoContext(this,dc
);
1961 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1963 ENSURE_LOADED_OR_RETURN(NULL
);
1964 return new wxCairoContext(this,dc
);
1967 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1969 ENSURE_LOADED_OR_RETURN(NULL
);
1971 const wxDCImpl
*impl
= dc
.GetImpl();
1972 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1974 return new wxCairoContext(this,dc
);
1977 return new wxCairoContext(this,dc
);
1981 #if wxUSE_ENH_METAFILE
1982 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& WXUNUSED(dc
) )
1984 ENSURE_LOADED_OR_RETURN(NULL
);
1990 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1992 ENSURE_LOADED_OR_RETURN(NULL
);
1994 return new wxCairoContext(this,(HDC
)context
);
1996 return new wxCairoContext(this,(cairo_t
*)context
);
2001 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
2003 ENSURE_LOADED_OR_RETURN(NULL
);
2005 return new wxCairoContext(this,(GdkDrawable
*)window
);
2007 wxUnusedVar(window
);
2012 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
2014 ENSURE_LOADED_OR_RETURN(NULL
);
2016 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2022 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
2024 ENSURE_LOADED_OR_RETURN(NULL
);
2025 return new wxCairoContext(this, window
);
2030 wxGraphicsPath
wxCairoRenderer::CreatePath()
2032 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2033 wxGraphicsPath path
;
2034 path
.SetRefData( new wxCairoPathData(this) );
2041 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2042 wxDouble tx
, wxDouble ty
)
2045 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2047 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
2048 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2053 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
2055 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2056 if ( !pen
.IsOk() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
2057 return wxNullGraphicsPen
;
2061 p
.SetRefData(new wxCairoPenData( this, pen
));
2066 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
2068 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2069 if ( !brush
.IsOk() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
2070 return wxNullGraphicsBrush
;
2074 p
.SetRefData(new wxCairoBrushData( this, brush
));
2080 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2081 wxDouble x2
, wxDouble y2
,
2082 const wxGraphicsGradientStops
& stops
)
2084 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2086 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2087 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2093 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2094 wxDouble xc
, wxDouble yc
, wxDouble r
,
2095 const wxGraphicsGradientStops
& stops
)
2097 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2099 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2100 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
2106 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2108 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2112 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2116 return wxNullGraphicsFont
;
2119 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2121 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2125 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2129 return wxNullGraphicsBitmap
;
2132 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2134 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2135 if ( bitmap
!= NULL
)
2138 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2142 return wxNullGraphicsBitmap
;
2146 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2147 wxDouble
WXUNUSED(x
),
2148 wxDouble
WXUNUSED(y
),
2149 wxDouble
WXUNUSED(w
),
2150 wxDouble
WXUNUSED(h
))
2152 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2153 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2154 return wxNullGraphicsBitmap
;
2157 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2159 return &gs_cairoGraphicsRenderer
;
2162 #else // !wxUSE_CAIRO
2164 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2169 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2171 // MSW and OS X have their own native default renderers, but the other ports
2172 // use Cairo by default
2173 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2174 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2176 return GetCairoRenderer();
2178 #endif // !(__WXMSW__ || __WXOSX__)
2180 #endif // wxUSE_GRAPHICS_CONTEXT