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"
27 #include "wx/bitmap.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcmemory.h"
31 #include "wx/dcprint.h"
33 #include "wx/window.h"
37 #include "wx/private/graphics.h"
38 #include "wx/rawbmp.h"
39 #include "wx/vector.h"
43 //-----------------------------------------------------------------------------
44 // device context implementation
46 // more and more of the dc functionality should be implemented by calling
47 // the appropricate wxCairoContext, but we will have to do that step by step
48 // also coordinate conversions should be moved to native matrix ops
49 //-----------------------------------------------------------------------------
51 // we always stock two context states, one at entry, to be able to preserve the
52 // state we were called with, the other one after changing to HI Graphics orientation
53 // (this one is used for getting back clippings etc)
55 //-----------------------------------------------------------------------------
56 // wxGraphicsPath implementation
57 //-----------------------------------------------------------------------------
59 // TODO remove this dependency (gdiplus needs the macros)
62 #define max(a,b) (((a) > (b)) ? (a) : (b))
66 #define min(a,b) (((a) < (b)) ? (a) : (b))
71 #include <cairo-win32.h>
76 #include "wx/fontutil.h"
77 #include "wx/gtk/dc.h"
81 #include <cairo-win32.h>
85 #include "wx/osx/private.h"
86 #include <cairo-quartz.h>
87 #include <cairo-atsui.h>
90 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
93 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
96 virtual wxGraphicsObjectRefData
*Clone() const;
99 // These are the path primitives from which everything else can be constructed
102 // begins a new subpath at (x,y)
103 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
105 // adds a straight line from the current point to (x,y)
106 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
108 // adds a cubic Bezier curve from the current point, using two control points and an end point
109 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
112 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
113 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
115 // gets the last point of the current path, (0,0) if not yet set
116 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
119 virtual void AddPath( const wxGraphicsPathData
* path
);
121 // closes the current sub-path
122 virtual void CloseSubpath();
125 // These are convenience functions which - if not available natively will be assembled
126 // using the primitives from above
131 // appends a rectangle as a new closed subpath
132 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
133 // appends an ellipsis as a new closed subpath fitting the passed rectangle
134 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
136 // 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)
137 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
140 // returns the native path
141 virtual void * GetNativePath() const ;
143 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
144 virtual void UnGetNativePath(void *p
) const;
146 // transforms each point of this path by the matrix
147 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
149 // gets the bounding box enclosing all points (possibly including control points)
150 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
152 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
155 cairo_t
* m_pathContext
;
158 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
161 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
162 virtual ~wxCairoMatrixData() ;
164 virtual wxGraphicsObjectRefData
*Clone() const ;
166 // concatenates the matrix
167 virtual void Concat( const wxGraphicsMatrixData
*t
);
169 // sets the matrix to the respective values
170 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
171 wxDouble tx
=0.0, wxDouble ty
=0.0);
173 // gets the component valuess of the matrix
174 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
175 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
177 // makes this the inverse matrix
178 virtual void Invert();
180 // returns true if the elements of the transformation matrix are equal ?
181 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
183 // return true if this is the identity matrix
184 virtual bool IsIdentity() const;
190 // add the translation to this matrix
191 virtual void Translate( wxDouble dx
, wxDouble dy
);
193 // add the scale to this matrix
194 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
196 // add the rotation to this matrix (radians)
197 virtual void Rotate( wxDouble angle
);
200 // apply the transforms
203 // applies that matrix to the point
204 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
206 // applies the matrix except for translations
207 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
209 // returns the native representation
210 virtual void * GetNativeMatrix() const;
212 cairo_matrix_t m_matrix
;
215 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
218 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
223 virtual void Apply( wxGraphicsContext
* context
);
224 virtual wxDouble
GetWidth() { return m_width
; }
234 cairo_line_cap_t m_cap
;
235 cairo_line_join_t m_join
;
238 const double *m_lengths
;
239 double *m_userLengths
;
243 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
246 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
249 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
250 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
251 ~wxCairoBrushData ();
253 virtual void Apply( wxGraphicsContext
* context
);
255 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
256 wxDouble x2
, wxDouble y2
,
257 const wxGraphicsGradientStops
& stops
);
258 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
259 wxDouble xc
, wxDouble yc
, wxDouble radius
,
260 const wxGraphicsGradientStops
& stops
);
265 // common part of Create{Linear,Radial}GradientBrush()
266 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
274 cairo_pattern_t
* m_brushPattern
;
277 class wxCairoFontData
: public wxGraphicsObjectRefData
280 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
283 virtual void Apply( wxGraphicsContext
* context
);
285 const PangoFontDescription
* GetFont() const { return m_font
; }
286 bool GetUnderlined() const { return m_underlined
; }
296 cairo_font_face_t
*m_font
;
297 #elif defined(__WXGTK__)
298 PangoFontDescription
* m_font
;
300 wxCharBuffer m_fontName
;
301 cairo_font_slant_t m_slant
;
302 cairo_font_weight_t m_weight
;
306 class wxCairoBitmapData
: public wxGraphicsObjectRefData
309 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
310 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
311 ~wxCairoBitmapData();
313 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
314 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
315 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
317 cairo_surface_t
* m_surface
;
318 cairo_pattern_t
* m_pattern
;
321 unsigned char* m_buffer
;
324 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
327 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
328 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
329 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
331 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
334 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
336 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
337 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
339 virtual ~wxCairoContext();
341 virtual bool ShouldOffset() const
343 if ( !m_enableOffset
)
347 if ( !m_pen
.IsNull() )
349 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
353 return ( penwidth
% 2 ) == 1;
356 virtual void Clip( const wxRegion
®ion
);
358 cairo_surface_t
* m_mswSurface
;
361 // clips drawings to the rect
362 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
364 // resets the clipping to original extent
365 virtual void ResetClip();
367 virtual void * GetNativeContext();
369 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
371 virtual bool SetCompositionMode(wxCompositionMode op
);
373 virtual void BeginLayer(wxDouble opacity
);
375 virtual void EndLayer();
377 virtual void StrokePath( const wxGraphicsPath
& p
);
378 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
380 virtual void Translate( wxDouble dx
, wxDouble dy
);
381 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
382 virtual void Rotate( wxDouble angle
);
384 // concatenates this transform with the current transform of this context
385 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
387 // sets the transform of this context
388 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
390 // gets the matrix of this context
391 virtual wxGraphicsMatrix
GetTransform() const;
393 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
394 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
395 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
396 virtual void PushState();
397 virtual void PopState();
399 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
400 wxDouble
*descent
, wxDouble
*externalLeading
) const;
401 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
404 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
407 void Init(cairo_t
*context
);
411 wxVector
<float> m_layerOpacities
;
413 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
416 //-----------------------------------------------------------------------------
417 // wxCairoPenData implementation
418 //-----------------------------------------------------------------------------
420 wxCairoPenData::~wxCairoPenData()
422 delete[] m_userLengths
;
425 void wxCairoPenData::Init()
428 m_userLengths
= NULL
;
433 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
434 : wxGraphicsObjectRefData(renderer
)
438 m_width
= m_pen
.GetWidth();
442 m_red
= m_pen
.GetColour().Red()/255.0;
443 m_green
= m_pen
.GetColour().Green()/255.0;
444 m_blue
= m_pen
.GetColour().Blue()/255.0;
445 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
447 switch ( m_pen
.GetCap() )
450 m_cap
= CAIRO_LINE_CAP_ROUND
;
453 case wxCAP_PROJECTING
:
454 m_cap
= CAIRO_LINE_CAP_SQUARE
;
458 m_cap
= CAIRO_LINE_CAP_BUTT
;
462 m_cap
= CAIRO_LINE_CAP_BUTT
;
466 switch ( m_pen
.GetJoin() )
469 m_join
= CAIRO_LINE_JOIN_BEVEL
;
473 m_join
= CAIRO_LINE_JOIN_MITER
;
477 m_join
= CAIRO_LINE_JOIN_ROUND
;
481 m_join
= CAIRO_LINE_JOIN_MITER
;
485 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
486 const double dotted
[] =
488 dashUnit
, dashUnit
+ 2.0
490 static const double short_dashed
[] =
494 static const double dashed
[] =
498 static const double dotted_dashed
[] =
500 9.0 , 6.0 , 3.0 , 3.0
503 switch ( m_pen
.GetStyle() )
505 case wxPENSTYLE_SOLID
:
508 case wxPENSTYLE_DOT
:
509 m_count
= WXSIZEOF(dotted
);
510 m_userLengths
= new double[ m_count
] ;
511 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
512 m_lengths
= m_userLengths
;
515 case wxPENSTYLE_LONG_DASH
:
517 m_count
= WXSIZEOF(dashed
);
520 case wxPENSTYLE_SHORT_DASH
:
521 m_lengths
= short_dashed
;
522 m_count
= WXSIZEOF(short_dashed
);
525 case wxPENSTYLE_DOT_DASH
:
526 m_lengths
= dotted_dashed
;
527 m_count
= WXSIZEOF(dotted_dashed
);
530 case wxPENSTYLE_USER_DASH
:
533 m_count
= m_pen
.GetDashes( &wxdashes
) ;
534 if ((wxdashes
!= NULL
) && (m_count
> 0))
536 m_userLengths
= new double[m_count
] ;
537 for ( int i
= 0 ; i
< m_count
; ++i
)
539 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
541 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
542 m_userLengths
[i
] = dashUnit
+ 2.0 ;
543 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
544 m_userLengths
[i
] = dashUnit
;
547 m_lengths
= m_userLengths
;
550 case wxPENSTYLE_STIPPLE
:
553 wxBitmap* bmp = pen.GetStipple();
554 if ( bmp && bmp->IsOk() )
556 wxDELETE( m_penImage );
557 wxDELETE( m_penBrush );
558 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
559 m_penBrush = new TextureBrush(m_penImage);
560 m_pen->SetBrush( m_penBrush );
566 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
567 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
570 wxDELETE( m_penBrush );
571 HatchStyle style = HatchStyleHorizontal;
572 switch( pen.GetStyle() )
574 case wxPENSTYLE_BDIAGONAL_HATCH :
575 style = HatchStyleBackwardDiagonal;
577 case wxPENSTYLE_CROSSDIAG_HATCH :
578 style = HatchStyleDiagonalCross;
580 case wxPENSTYLE_FDIAGONAL_HATCH :
581 style = HatchStyleForwardDiagonal;
583 case wxPENSTYLE_CROSS_HATCH :
584 style = HatchStyleCross;
586 case wxPENSTYLE_HORIZONTAL_HATCH :
587 style = HatchStyleHorizontal;
589 case wxPENSTYLE_VERTICAL_HATCH :
590 style = HatchStyleVertical;
594 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
595 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
596 m_pen->SetBrush( m_penBrush )
603 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
605 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
606 cairo_set_line_width(ctext
,m_width
);
607 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
608 cairo_set_line_cap(ctext
,m_cap
);
609 cairo_set_line_join(ctext
,m_join
);
610 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
613 //-----------------------------------------------------------------------------
614 // wxCairoBrushData implementation
615 //-----------------------------------------------------------------------------
617 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
618 : wxGraphicsObjectRefData( renderer
)
623 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
624 : wxGraphicsObjectRefData(renderer
)
628 m_red
= brush
.GetColour().Red()/255.0;
629 m_green
= brush
.GetColour().Green()/255.0;
630 m_blue
= brush
.GetColour().Blue()/255.0;
631 m_alpha
= brush
.GetColour().Alpha()/255.0;
633 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
635 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
636 brush.GetColour().Green() , brush.GetColour().Blue() ) );
638 else if ( brush.IsHatch() )
640 HatchStyle style = HatchStyleHorizontal;
641 switch( brush.GetStyle() )
643 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
644 style = HatchStyleBackwardDiagonal;
646 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
647 style = HatchStyleDiagonalCross;
649 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
650 style = HatchStyleForwardDiagonal;
652 case wxBRUSHSTYLE_CROSS_HATCH :
653 style = HatchStyleCross;
655 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
656 style = HatchStyleHorizontal;
658 case wxBRUSHSTYLE_VERTICAL_HATCH :
659 style = HatchStyleVertical;
663 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
664 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
668 wxBitmap* bmp = brush.GetStipple();
669 if ( bmp && bmp->IsOk() )
671 wxDELETE( m_brushImage );
672 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
673 m_brush = new TextureBrush(m_brushImage);
679 wxCairoBrushData::~wxCairoBrushData ()
682 cairo_pattern_destroy(m_brushPattern
);
685 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
687 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
688 if ( m_brushPattern
)
690 cairo_set_source(ctext
,m_brushPattern
);
694 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
698 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
700 // loop over all the stops, they include the beginning and ending ones
701 const unsigned numStops
= stops
.GetCount();
702 for ( unsigned n
= 0; n
< numStops
; n
++ )
704 const wxGraphicsGradientStop stop
= stops
.Item(n
);
706 const wxColour col
= stop
.GetColour();
708 cairo_pattern_add_color_stop_rgba
719 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
720 wxT("Couldn't create cairo pattern"));
724 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
725 wxDouble x2
, wxDouble y2
,
726 const wxGraphicsGradientStops
& stops
)
728 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
730 AddGradientStops(stops
);
734 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
735 wxDouble xc
, wxDouble yc
,
737 const wxGraphicsGradientStops
& stops
)
739 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
741 AddGradientStops(stops
);
744 void wxCairoBrushData::Init()
746 m_brushPattern
= NULL
;
749 //-----------------------------------------------------------------------------
750 // wxCairoFontData implementation
751 //-----------------------------------------------------------------------------
753 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
754 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
756 m_red
= col
.Red()/255.0;
757 m_green
= col
.Green()/255.0;
758 m_blue
= col
.Blue()/255.0;
759 m_alpha
= col
.Alpha()/255.0;
760 m_size
= font
.GetPointSize();
761 m_underlined
= font
.GetUnderlined();
764 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
765 #elif defined(__WXGTK__)
766 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
768 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
769 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
770 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
774 wxCairoFontData::~wxCairoFontData()
777 cairo_font_face_destroy( m_font
);
778 #elif defined(__WXGTK__)
779 pango_font_description_free( m_font
);
784 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
786 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
787 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
789 // the rest is done using Pango layouts
790 #elif defined(__WXMAC__)
791 cairo_set_font_face(ctext
, m_font
);
792 cairo_set_font_size(ctext
, m_size
);
794 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
795 cairo_set_font_size(ctext
, m_size
);
799 //-----------------------------------------------------------------------------
800 // wxCairoPathData implementation
801 //-----------------------------------------------------------------------------
803 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
804 : wxGraphicsPathData(renderer
)
808 m_pathContext
= pathcontext
;
812 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
813 m_pathContext
= cairo_create(surface
);
814 cairo_surface_destroy (surface
);
818 wxCairoPathData::~wxCairoPathData()
820 cairo_destroy(m_pathContext
);
823 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
825 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
826 cairo_t
* pathcontext
= cairo_create(surface
);
827 cairo_surface_destroy (surface
);
829 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
830 cairo_append_path(pathcontext
, path
);
831 cairo_path_destroy(path
);
832 return new wxCairoPathData( GetRenderer() ,pathcontext
);
836 void* wxCairoPathData::GetNativePath() const
838 return cairo_copy_path(m_pathContext
) ;
841 void wxCairoPathData::UnGetNativePath(void *p
) const
843 cairo_path_destroy((cairo_path_t
*)p
);
850 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
852 cairo_move_to(m_pathContext
,x
,y
);
855 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
857 cairo_line_to(m_pathContext
,x
,y
);
860 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
862 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
863 cairo_append_path(m_pathContext
, p
);
867 void wxCairoPathData::CloseSubpath()
869 cairo_close_path(m_pathContext
);
872 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
874 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
877 // gets the last point of the current path, (0,0) if not yet set
878 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
881 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
888 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
890 // as clockwise means positive in our system (y pointing downwards)
891 // TODO make this interpretation dependent of the
893 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
894 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
896 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
899 // transforms each point of this path by the matrix
900 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
902 // as we don't have a true path object, we have to apply the inverse
903 // matrix to the context
904 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
905 cairo_matrix_invert( &m
);
906 cairo_transform(m_pathContext
,&m
);
909 // gets the bounding box enclosing all points (possibly including control points)
910 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
914 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
938 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
940 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
941 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
944 //-----------------------------------------------------------------------------
945 // wxCairoMatrixData implementation
946 //-----------------------------------------------------------------------------
948 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
949 : wxGraphicsMatrixData(renderer
)
955 wxCairoMatrixData::~wxCairoMatrixData()
960 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
962 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
965 // concatenates the matrix
966 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
968 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
971 // sets the matrix to the respective values
972 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
973 wxDouble tx
, wxDouble ty
)
975 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
978 // gets the component valuess of the matrix
979 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
980 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
982 if (a
) *a
= m_matrix
.xx
;
983 if (b
) *b
= m_matrix
.yx
;
984 if (c
) *c
= m_matrix
.xy
;
985 if (d
) *d
= m_matrix
.yy
;
986 if (tx
) *tx
= m_matrix
.x0
;
987 if (ty
) *ty
= m_matrix
.y0
;
990 // makes this the inverse matrix
991 void wxCairoMatrixData::Invert()
993 cairo_matrix_invert( &m_matrix
);
996 // returns true if the elements of the transformation matrix are equal ?
997 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
999 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1001 m_matrix
.xx
== tm
->xx
&&
1002 m_matrix
.yx
== tm
->yx
&&
1003 m_matrix
.xy
== tm
->xy
&&
1004 m_matrix
.yy
== tm
->yy
&&
1005 m_matrix
.x0
== tm
->x0
&&
1006 m_matrix
.y0
== tm
->y0
) ;
1009 // return true if this is the identity matrix
1010 bool wxCairoMatrixData::IsIdentity() const
1012 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1013 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1020 // add the translation to this matrix
1021 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1023 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1026 // add the scale to this matrix
1027 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1029 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1032 // add the rotation to this matrix (radians)
1033 void wxCairoMatrixData::Rotate( wxDouble angle
)
1035 cairo_matrix_rotate( &m_matrix
, angle
) ;
1039 // apply the transforms
1042 // applies that matrix to the point
1043 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1045 double lx
= *x
, ly
= *y
;
1046 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1051 // applies the matrix except for translations
1052 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1054 double lx
= *dx
, ly
= *dy
;
1055 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1060 // returns the native representation
1061 void * wxCairoMatrixData::GetNativeMatrix() const
1063 return (void*) &m_matrix
;
1066 // wxCairoBitmap implementation
1067 //-----------------------------------------------------------------------------
1069 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1070 wxGraphicsObjectRefData( renderer
)
1073 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1076 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1078 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1080 #ifdef wxHAS_RAW_BITMAP
1081 int bw
= m_width
= bmp
.GetWidth();
1082 int bh
= m_height
= bmp
.GetHeight();
1083 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1084 m_buffer
= new unsigned char[bw
*bh
*4];
1085 wxUint32
* data
= (wxUint32
*)m_buffer
;
1087 // Create a surface object and copy the bitmap pixel data to it. if the
1088 // image has alpha (or a mask represented as alpha) then we'll use a
1089 // different format and iterator than if it doesn't...
1090 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1092 m_surface
= cairo_image_surface_create_for_data(
1093 m_buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1094 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1095 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1097 wxAlphaPixelData::Iterator
p(pixData
);
1098 for (int y
=0; y
<bh
; y
++)
1100 wxAlphaPixelData::Iterator rowStart
= p
;
1101 for (int x
=0; x
<bw
; x
++)
1103 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1104 // with alpha in the upper 8 bits, then red, then green, then
1105 // blue. The 32-bit quantities are stored native-endian.
1106 // Pre-multiplied alpha is used.
1107 unsigned char alpha
= p
.Alpha();
1111 *data
= ( alpha
<< 24
1112 | (p
.Red() * alpha
/255) << 16
1113 | (p
.Green() * alpha
/255) << 8
1114 | (p
.Blue() * alpha
/255) );
1119 p
.OffsetY(pixData
, 1);
1124 m_surface
= cairo_image_surface_create_for_data(
1125 m_buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1126 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1127 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1129 wxNativePixelData::Iterator
p(pixData
);
1130 for (int y
=0; y
<bh
; y
++)
1132 wxNativePixelData::Iterator rowStart
= p
;
1133 for (int x
=0; x
<bw
; x
++)
1135 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1136 // the upper 8 bits unused. Red, Green, and Blue are stored in
1137 // the remaining 24 bits in that order. The 32-bit quantities
1138 // are stored native-endian.
1139 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1144 p
.OffsetY(pixData
, 1);
1147 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1148 #endif // wxHAS_RAW_BITMAP
1151 wxCairoBitmapData::~wxCairoBitmapData()
1154 cairo_pattern_destroy(m_pattern
);
1157 cairo_surface_destroy(m_surface
);
1164 //-----------------------------------------------------------------------------
1165 // wxCairoContext implementation
1166 //-----------------------------------------------------------------------------
1168 class wxCairoOffsetHelper
1171 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1176 cairo_translate( m_ctx
, 0.5, 0.5 );
1178 ~wxCairoOffsetHelper( )
1181 cairo_translate( m_ctx
, -0.5, -0.5 );
1188 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1189 : wxGraphicsContext(renderer
)
1192 const wxDCImpl
*impl
= dc
.GetImpl();
1193 Init( (cairo_t
*) impl
->GetCairoContext() );
1195 wxSize sz
= dc
.GetSize();
1199 wxPoint org
= dc
.GetDeviceOrigin();
1200 cairo_translate( m_context
, org
.x
, org
.y
);
1203 dc
.GetUserScale( &sx
, &sy
);
1204 cairo_scale( m_context
, sx
, sy
);
1206 org
= dc
.GetLogicalOrigin();
1207 cairo_translate( m_context
, -org
.x
, -org
.y
);
1211 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1212 : wxGraphicsContext(renderer
)
1215 dc
.GetSize( &width
, &height
);
1219 m_enableOffset
= true;
1222 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1223 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1226 wxGraphicsMatrix matrix
= CreateMatrix();
1228 wxPoint org
= dc
.GetDeviceOrigin();
1229 matrix
.Translate( org
.x
, org
.y
);
1231 org
= dc
.GetLogicalOrigin();
1232 matrix
.Translate( -org
.x
, -org
.y
);
1235 dc
.GetUserScale( &sx
, &sy
);
1236 matrix
.Scale( sx
, sy
);
1238 ConcatTransform( matrix
);
1243 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1244 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1245 Init( cairo_create( surface
) );
1246 cairo_surface_destroy( surface
);
1250 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1251 : wxGraphicsContext(renderer
)
1254 dc
.GetSize( &width
, &height
);
1258 m_enableOffset
= true;
1261 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1262 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1265 wxGraphicsMatrix matrix
= CreateMatrix();
1267 wxPoint org
= dc
.GetDeviceOrigin();
1268 matrix
.Translate( org
.x
, org
.y
);
1270 org
= dc
.GetLogicalOrigin();
1271 matrix
.Translate( -org
.x
, -org
.y
);
1274 dc
.GetUserScale( &sx
, &sy
);
1275 matrix
.Scale( sx
, sy
);
1277 ConcatTransform( matrix
);
1282 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1283 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1284 Init( cairo_create( surface
) );
1285 cairo_surface_destroy( surface
);
1290 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1291 : wxGraphicsContext(renderer
)
1293 Init( gdk_cairo_create( drawable
) );
1296 gdk_drawable_get_size( drawable
, &width
, &height
);
1303 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1304 : wxGraphicsContext(renderer
)
1306 m_mswSurface
= cairo_win32_surface_create(handle
);
1307 Init( cairo_create(m_mswSurface
) );
1314 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1315 : wxGraphicsContext(renderer
)
1322 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1323 : wxGraphicsContext(renderer
)
1325 m_enableOffset
= true;
1327 // something along these lines (copied from dcclient)
1329 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1330 // code should still be able to create wxClientDCs for them, so we will
1331 // use the parent window here then.
1332 if (window
->m_wxwindow
== NULL
)
1334 window
= window
->GetParent();
1337 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1339 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1341 wxSize sz
= window
->GetSize();
1347 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1348 Init(cairo_create(m_mswSurface
));
1353 wxCairoContext::~wxCairoContext()
1359 cairo_destroy(m_context
);
1363 cairo_surface_destroy(m_mswSurface
);
1367 void wxCairoContext::Init(cairo_t
*context
)
1369 m_context
= context
;
1375 void wxCairoContext::Clip( const wxRegion
& region
)
1377 // Create a path with all the rectangles in the region
1378 wxGraphicsPath path
= GetRenderer()->CreatePath();
1379 wxRegionIterator
ri(region
);
1382 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1386 // Put it in the context
1387 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1388 cairo_append_path(m_context
, cp
);
1390 // clip to that path
1391 cairo_clip(m_context
);
1392 path
.UnGetNativePath(cp
);
1395 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1397 // Create a path with this rectangle
1398 wxGraphicsPath path
= GetRenderer()->CreatePath();
1399 path
.AddRectangle(x
,y
,w
,h
);
1401 // Put it in the context
1402 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1403 cairo_append_path(m_context
, cp
);
1405 // clip to that path
1406 cairo_clip(m_context
);
1407 path
.UnGetNativePath(cp
);
1410 void wxCairoContext::ResetClip()
1412 cairo_reset_clip(m_context
);
1416 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1418 if ( !m_pen
.IsNull() )
1420 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1421 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1422 cairo_append_path(m_context
,cp
);
1423 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1424 cairo_stroke(m_context
);
1425 path
.UnGetNativePath(cp
);
1429 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1431 if ( !m_brush
.IsNull() )
1433 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1434 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1435 cairo_append_path(m_context
,cp
);
1436 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1437 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1438 cairo_fill(m_context
);
1439 path
.UnGetNativePath(cp
);
1443 void wxCairoContext::Rotate( wxDouble angle
)
1445 cairo_rotate(m_context
,angle
);
1448 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1450 cairo_translate(m_context
,dx
,dy
);
1453 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1455 cairo_scale(m_context
,xScale
,yScale
);
1458 // concatenates this transform with the current transform of this context
1459 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1461 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1464 // sets the transform of this context
1465 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1467 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1470 // gets the matrix of this context
1471 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1473 wxGraphicsMatrix matrix
= CreateMatrix();
1474 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1480 void wxCairoContext::PushState()
1482 cairo_save(m_context
);
1485 void wxCairoContext::PopState()
1487 cairo_restore(m_context
);
1490 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1492 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1493 DrawBitmap(bitmap
, x
, y
, w
, h
);
1497 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1501 // In case we're scaling the image by using a width and height different
1502 // than the bitmap's size create a pattern transformation on the surface and
1503 // draw the transformed pattern.
1504 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1505 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1506 wxSize size
= data
->GetSize();
1508 wxDouble scaleX
= w
/ size
.GetWidth();
1509 wxDouble scaleY
= h
/ size
.GetHeight();
1511 // prepare to draw the image
1512 cairo_translate(m_context
, x
, y
);
1513 cairo_scale(m_context
, scaleX
, scaleY
);
1514 cairo_set_source(m_context
, pattern
);
1515 // use the original size here since the context is scaled already...
1516 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1517 // fill the rectangle using the pattern
1518 cairo_fill(m_context
);
1523 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1525 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1526 // start using the Cairo backend on other platforms then we may need to
1527 // fiddle with this...
1528 DrawBitmap(icon
, x
, y
, w
, h
);
1532 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1534 wxCHECK_RET( !m_font
.IsNull(),
1535 wxT("wxCairoContext::DrawText - no valid font set") );
1540 const wxCharBuffer data
= str
.utf8_str();
1544 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1547 size_t datalen
= strlen(data
);
1549 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1550 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1551 pango_layout_set_font_description( layout
, font_data
->GetFont());
1552 pango_layout_set_text(layout
, data
, datalen
);
1554 if (font_data
->GetUnderlined())
1556 PangoAttrList
*attrs
= pango_attr_list_new();
1557 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1558 pango_attr_list_insert(attrs
, attr
);
1559 pango_layout_set_attributes(layout
, attrs
);
1560 pango_attr_list_unref(attrs
);
1563 cairo_move_to(m_context
, x
, y
);
1564 pango_cairo_show_layout (m_context
, layout
);
1566 g_object_unref (layout
);
1568 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1569 // the position we move to by the ascent.
1570 cairo_font_extents_t fe
;
1571 cairo_font_extents(m_context
, &fe
);
1572 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1574 cairo_show_text(m_context
, data
);
1578 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1579 wxDouble
*descent
, wxDouble
*externalLeading
) const
1581 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1589 if ( externalLeading
)
1590 *externalLeading
= 0;
1598 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1599 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1600 const wxCharBuffer data
= str
.utf8_str();
1605 pango_layout_set_text( layout
, data
, strlen(data
) );
1606 pango_layout_get_pixel_size (layout
, &w
, &h
);
1613 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1614 int baseline
= pango_layout_iter_get_baseline(iter
);
1615 pango_layout_iter_free(iter
);
1616 *descent
= h
- PANGO_PIXELS(baseline
);
1618 g_object_unref (layout
);
1620 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1624 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1625 cairo_text_extents_t te
;
1626 cairo_text_extents(m_context
, buf
, &te
);
1630 if (height
|| descent
|| externalLeading
)
1632 cairo_font_extents_t fe
;
1633 cairo_font_extents(m_context
, &fe
);
1635 // some backends have negative descents
1637 if ( fe
.descent
< 0 )
1638 fe
.descent
= -fe
.descent
;
1640 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1642 // some backends are broken re height ... (eg currently ATSUI)
1643 fe
.height
= fe
.ascent
+ fe
.descent
;
1647 *height
= fe
.height
;
1649 *descent
= fe
.descent
;
1650 if ( externalLeading
)
1651 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1656 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1659 widths
.Add(0, text
.length());
1661 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1669 void * wxCairoContext::GetNativeContext()
1674 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1676 if (m_antialias
== antialias
)
1679 m_antialias
= antialias
;
1681 cairo_antialias_t antialiasMode
;
1684 case wxANTIALIAS_DEFAULT
:
1685 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1687 case wxANTIALIAS_NONE
:
1688 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1693 cairo_set_antialias(m_context
, antialiasMode
);
1697 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1699 if ( m_composition
== op
)
1703 cairo_operator_t cop
;
1706 case wxCOMPOSITION_CLEAR
:
1707 cop
= CAIRO_OPERATOR_CLEAR
;
1709 case wxCOMPOSITION_SOURCE
:
1710 cop
= CAIRO_OPERATOR_SOURCE
;
1712 case wxCOMPOSITION_OVER
:
1713 cop
= CAIRO_OPERATOR_OVER
;
1715 case wxCOMPOSITION_IN
:
1716 cop
= CAIRO_OPERATOR_IN
;
1718 case wxCOMPOSITION_OUT
:
1719 cop
= CAIRO_OPERATOR_OUT
;
1721 case wxCOMPOSITION_ATOP
:
1722 cop
= CAIRO_OPERATOR_ATOP
;
1724 case wxCOMPOSITION_DEST
:
1725 cop
= CAIRO_OPERATOR_DEST
;
1727 case wxCOMPOSITION_DEST_OVER
:
1728 cop
= CAIRO_OPERATOR_DEST_OVER
;
1730 case wxCOMPOSITION_DEST_IN
:
1731 cop
= CAIRO_OPERATOR_DEST_IN
;
1733 case wxCOMPOSITION_DEST_OUT
:
1734 cop
= CAIRO_OPERATOR_DEST_OUT
;
1736 case wxCOMPOSITION_DEST_ATOP
:
1737 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1739 case wxCOMPOSITION_XOR
:
1740 cop
= CAIRO_OPERATOR_XOR
;
1742 case wxCOMPOSITION_ADD
:
1743 cop
= CAIRO_OPERATOR_ADD
;
1748 cairo_set_operator(m_context
, cop
);
1752 void wxCairoContext::BeginLayer(wxDouble opacity
)
1754 m_layerOpacities
.push_back(opacity
);
1755 cairo_push_group(m_context
);
1758 void wxCairoContext::EndLayer()
1760 float opacity
= m_layerOpacities
.back();
1761 m_layerOpacities
.pop_back();
1762 cairo_pop_group_to_source(m_context
);
1763 cairo_paint_with_alpha(m_context
,opacity
);
1766 //-----------------------------------------------------------------------------
1767 // wxCairoRenderer declaration
1768 //-----------------------------------------------------------------------------
1770 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1773 wxCairoRenderer() {}
1775 virtual ~wxCairoRenderer() {}
1779 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1780 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1781 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1783 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1785 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1787 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1789 virtual wxGraphicsContext
* CreateMeasuringContext();
1791 #if wxUSE_ENH_METAFILE
1792 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
1797 virtual wxGraphicsPath
CreatePath();
1801 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1802 wxDouble tx
=0.0, wxDouble ty
=0.0);
1805 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1807 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1809 virtual wxGraphicsBrush
1810 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1811 wxDouble x2
, wxDouble y2
,
1812 const wxGraphicsGradientStops
& stops
);
1814 virtual wxGraphicsBrush
1815 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1816 wxDouble xc
, wxDouble yc
,
1818 const wxGraphicsGradientStops
& stops
);
1821 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1823 // create a native bitmap representation
1824 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1826 // create a graphics bitmap from a native bitmap
1827 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1829 // create a subimage from a native image representation
1830 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1833 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1836 //-----------------------------------------------------------------------------
1837 // wxCairoRenderer implementation
1838 //-----------------------------------------------------------------------------
1840 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1842 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1843 // temporary hack to allow creating a cairo context on any platform
1844 extern wxGraphicsRenderer
* gCairoRenderer
;
1845 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1847 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1849 return new wxCairoContext(this,dc
);
1852 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1854 return new wxCairoContext(this,dc
);
1857 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1860 const wxDCImpl
*impl
= dc
.GetImpl();
1861 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1863 return new wxCairoContext(this,dc
);
1870 #if wxUSE_ENH_METAFILE
1871 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1878 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1881 return new wxCairoContext(this,(HDC
)context
);
1883 return new wxCairoContext(this,(cairo_t
*)context
);
1888 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1891 return new wxCairoContext(this,(GdkDrawable
*)window
);
1897 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1900 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1906 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1908 return new wxCairoContext(this, window
);
1913 wxGraphicsPath
wxCairoRenderer::CreatePath()
1915 wxGraphicsPath path
;
1916 path
.SetRefData( new wxCairoPathData(this) );
1923 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1924 wxDouble tx
, wxDouble ty
)
1928 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1929 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1934 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1936 if ( !pen
.IsOk() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1937 return wxNullGraphicsPen
;
1941 p
.SetRefData(new wxCairoPenData( this, pen
));
1946 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1948 if ( !brush
.IsOk() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1949 return wxNullGraphicsBrush
;
1953 p
.SetRefData(new wxCairoBrushData( this, brush
));
1959 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1960 wxDouble x2
, wxDouble y2
,
1961 const wxGraphicsGradientStops
& stops
)
1964 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1965 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1971 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1972 wxDouble xc
, wxDouble yc
, wxDouble r
,
1973 const wxGraphicsGradientStops
& stops
)
1976 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1977 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
1983 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1988 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1992 return wxNullGraphicsFont
;
1995 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2000 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2004 return wxNullGraphicsBitmap
;
2007 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2009 if ( bitmap
!= NULL
)
2012 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2016 return wxNullGraphicsBitmap
;
2020 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2021 wxDouble
WXUNUSED(x
),
2022 wxDouble
WXUNUSED(y
),
2023 wxDouble
WXUNUSED(w
),
2024 wxDouble
WXUNUSED(h
))
2026 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2027 return wxNullGraphicsBitmap
;
2030 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2032 return &gs_cairoGraphicsRenderer
;
2035 #else // !wxUSE_CAIRO
2037 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2042 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2044 // MSW and OS X have their own native default renderers, but the other ports
2045 // use Cairo by default
2046 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2047 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2049 return GetCairoRenderer();
2051 #endif // !(__WXMSW__ || __WXOSX__)
2053 #endif // wxUSE_GRAPHICS_CONTEXT