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
;
305 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
309 class wxCairoBitmapData
: public wxGraphicsObjectRefData
312 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
313 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
314 ~wxCairoBitmapData();
316 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
317 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
318 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
320 cairo_surface_t
* m_surface
;
321 cairo_pattern_t
* m_pattern
;
324 unsigned char* m_buffer
;
327 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
330 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
331 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
332 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
334 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
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->Ok() )
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->Ok() )
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 wxCairoContext::~wxCairoContext()
1353 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1354 m_context
= cairo_create(m_mswSurface
);
1357 cairo_destroy(m_context
);
1361 cairo_surface_destroy(m_mswSurface
);
1365 void wxCairoContext::Init(cairo_t
*context
)
1367 m_context
= context
;
1373 void wxCairoContext::Clip( const wxRegion
& region
)
1375 // Create a path with all the rectangles in the region
1376 wxGraphicsPath path
= GetRenderer()->CreatePath();
1377 wxRegionIterator
ri(region
);
1380 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1384 // Put it in the context
1385 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1386 cairo_append_path(m_context
, cp
);
1388 // clip to that path
1389 cairo_clip(m_context
);
1390 path
.UnGetNativePath(cp
);
1393 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1395 // Create a path with this rectangle
1396 wxGraphicsPath path
= GetRenderer()->CreatePath();
1397 path
.AddRectangle(x
,y
,w
,h
);
1399 // Put it in the context
1400 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1401 cairo_append_path(m_context
, cp
);
1403 // clip to that path
1404 cairo_clip(m_context
);
1405 path
.UnGetNativePath(cp
);
1408 void wxCairoContext::ResetClip()
1410 cairo_reset_clip(m_context
);
1414 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1416 if ( !m_pen
.IsNull() )
1418 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1419 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1420 cairo_append_path(m_context
,cp
);
1421 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1422 cairo_stroke(m_context
);
1423 path
.UnGetNativePath(cp
);
1427 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1429 if ( !m_brush
.IsNull() )
1431 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1432 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1433 cairo_append_path(m_context
,cp
);
1434 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1435 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1436 cairo_fill(m_context
);
1437 path
.UnGetNativePath(cp
);
1441 void wxCairoContext::Rotate( wxDouble angle
)
1443 cairo_rotate(m_context
,angle
);
1446 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1448 cairo_translate(m_context
,dx
,dy
);
1451 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1453 cairo_scale(m_context
,xScale
,yScale
);
1456 // concatenates this transform with the current transform of this context
1457 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1459 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1462 // sets the transform of this context
1463 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1465 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1468 // gets the matrix of this context
1469 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1471 wxGraphicsMatrix matrix
= CreateMatrix();
1472 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1478 void wxCairoContext::PushState()
1480 cairo_save(m_context
);
1483 void wxCairoContext::PopState()
1485 cairo_restore(m_context
);
1488 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1490 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1491 DrawBitmap(bitmap
, x
, y
, w
, h
);
1495 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1499 // In case we're scaling the image by using a width and height different
1500 // than the bitmap's size create a pattern transformation on the surface and
1501 // draw the transformed pattern.
1502 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1503 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1504 wxSize size
= data
->GetSize();
1506 wxDouble scaleX
= w
/ size
.GetWidth();
1507 wxDouble scaleY
= h
/ size
.GetHeight();
1509 // prepare to draw the image
1510 cairo_translate(m_context
, x
, y
);
1511 cairo_scale(m_context
, scaleX
, scaleY
);
1512 cairo_set_source(m_context
, pattern
);
1513 // use the original size here since the context is scaled already...
1514 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1515 // fill the rectangle using the pattern
1516 cairo_fill(m_context
);
1521 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1523 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1524 // start using the Cairo backend on other platforms then we may need to
1525 // fiddle with this...
1526 DrawBitmap(icon
, x
, y
, w
, h
);
1530 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1532 wxCHECK_RET( !m_font
.IsNull(),
1533 wxT("wxCairoContext::DrawText - no valid font set") );
1538 const wxCharBuffer data
= str
.utf8_str();
1542 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1545 size_t datalen
= strlen(data
);
1547 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1548 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1549 pango_layout_set_font_description( layout
, font_data
->GetFont());
1550 pango_layout_set_text(layout
, data
, datalen
);
1552 if (font_data
->GetUnderlined())
1554 PangoAttrList
*attrs
= pango_attr_list_new();
1555 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1556 pango_attr_list_insert(attrs
, attr
);
1557 pango_layout_set_attributes(layout
, attrs
);
1558 pango_attr_list_unref(attrs
);
1561 cairo_move_to(m_context
, x
, y
);
1562 pango_cairo_show_layout (m_context
, layout
);
1564 g_object_unref (layout
);
1566 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1567 // the position we move to by the ascent.
1568 cairo_font_extents_t fe
;
1569 cairo_font_extents(m_context
, &fe
);
1570 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1572 cairo_show_text(m_context
, data
);
1576 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1577 wxDouble
*descent
, wxDouble
*externalLeading
) const
1579 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1587 if ( externalLeading
)
1588 *externalLeading
= 0;
1596 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1597 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1598 const wxCharBuffer data
= str
.utf8_str();
1603 pango_layout_set_text( layout
, data
, strlen(data
) );
1604 pango_layout_get_pixel_size (layout
, &w
, &h
);
1611 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1612 int baseline
= pango_layout_iter_get_baseline(iter
);
1613 pango_layout_iter_free(iter
);
1614 *descent
= h
- PANGO_PIXELS(baseline
);
1616 g_object_unref (layout
);
1618 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1622 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1623 cairo_text_extents_t te
;
1624 cairo_text_extents(m_context
, buf
, &te
);
1628 if (height
|| descent
|| externalLeading
)
1630 cairo_font_extents_t fe
;
1631 cairo_font_extents(m_context
, &fe
);
1633 // some backends have negative descents
1635 if ( fe
.descent
< 0 )
1636 fe
.descent
= -fe
.descent
;
1638 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1640 // some backends are broken re height ... (eg currently ATSUI)
1641 fe
.height
= fe
.ascent
+ fe
.descent
;
1645 *height
= fe
.height
;
1647 *descent
= fe
.descent
;
1648 if ( externalLeading
)
1649 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1654 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1657 widths
.Add(0, text
.length());
1659 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1667 void * wxCairoContext::GetNativeContext()
1672 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1674 if (m_antialias
== antialias
)
1677 m_antialias
= antialias
;
1679 cairo_antialias_t antialiasMode
;
1682 case wxANTIALIAS_DEFAULT
:
1683 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1685 case wxANTIALIAS_NONE
:
1686 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1691 cairo_set_antialias(m_context
, antialiasMode
);
1695 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1697 if ( m_composition
== op
)
1701 cairo_operator_t cop
;
1704 case wxCOMPOSITION_CLEAR
:
1705 cop
= CAIRO_OPERATOR_CLEAR
;
1707 case wxCOMPOSITION_SOURCE
:
1708 cop
= CAIRO_OPERATOR_SOURCE
;
1710 case wxCOMPOSITION_OVER
:
1711 cop
= CAIRO_OPERATOR_OVER
;
1713 case wxCOMPOSITION_IN
:
1714 cop
= CAIRO_OPERATOR_IN
;
1716 case wxCOMPOSITION_OUT
:
1717 cop
= CAIRO_OPERATOR_OUT
;
1719 case wxCOMPOSITION_ATOP
:
1720 cop
= CAIRO_OPERATOR_ATOP
;
1722 case wxCOMPOSITION_DEST
:
1723 cop
= CAIRO_OPERATOR_DEST
;
1725 case wxCOMPOSITION_DEST_OVER
:
1726 cop
= CAIRO_OPERATOR_DEST_OVER
;
1728 case wxCOMPOSITION_DEST_IN
:
1729 cop
= CAIRO_OPERATOR_DEST_IN
;
1731 case wxCOMPOSITION_DEST_OUT
:
1732 cop
= CAIRO_OPERATOR_DEST_OUT
;
1734 case wxCOMPOSITION_DEST_ATOP
:
1735 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1737 case wxCOMPOSITION_XOR
:
1738 cop
= CAIRO_OPERATOR_XOR
;
1740 case wxCOMPOSITION_ADD
:
1741 cop
= CAIRO_OPERATOR_ADD
;
1746 cairo_set_operator(m_context
, cop
);
1750 void wxCairoContext::BeginLayer(wxDouble opacity
)
1752 m_layerOpacities
.push_back(opacity
);
1753 cairo_push_group(m_context
);
1756 void wxCairoContext::EndLayer()
1758 float opacity
= m_layerOpacities
.back();
1759 m_layerOpacities
.pop_back();
1760 cairo_pop_group_to_source(m_context
);
1761 cairo_paint_with_alpha(m_context
,opacity
);
1764 //-----------------------------------------------------------------------------
1765 // wxCairoRenderer declaration
1766 //-----------------------------------------------------------------------------
1768 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1771 wxCairoRenderer() {}
1773 virtual ~wxCairoRenderer() {}
1777 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1778 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1779 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1781 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1783 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1785 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1787 virtual wxGraphicsContext
* CreateMeasuringContext();
1791 virtual wxGraphicsPath
CreatePath();
1795 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1796 wxDouble tx
=0.0, wxDouble ty
=0.0);
1799 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1801 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1803 virtual wxGraphicsBrush
1804 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1805 wxDouble x2
, wxDouble y2
,
1806 const wxGraphicsGradientStops
& stops
);
1808 virtual wxGraphicsBrush
1809 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1810 wxDouble xc
, wxDouble yc
,
1812 const wxGraphicsGradientStops
& stops
);
1815 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1817 // create a native bitmap representation
1818 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1820 // create a graphics bitmap from a native bitmap
1821 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1823 // create a subimage from a native image representation
1824 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1827 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1830 //-----------------------------------------------------------------------------
1831 // wxCairoRenderer implementation
1832 //-----------------------------------------------------------------------------
1834 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1836 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1837 // temporary hack to allow creating a cairo context on any platform
1838 extern wxGraphicsRenderer
* gCairoRenderer
;
1839 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1841 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1843 return new wxCairoContext(this,dc
);
1846 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1848 return new wxCairoContext(this,dc
);
1851 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1854 const wxDCImpl
*impl
= dc
.GetImpl();
1855 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1857 return new wxCairoContext(this,dc
);
1863 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1866 return new wxCairoContext(this,(HDC
)context
);
1868 return new wxCairoContext(this,(cairo_t
*)context
);
1873 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1876 return new wxCairoContext(this,(GdkDrawable
*)window
);
1882 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1885 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1891 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1893 return new wxCairoContext(this, window
);
1898 wxGraphicsPath
wxCairoRenderer::CreatePath()
1900 wxGraphicsPath path
;
1901 path
.SetRefData( new wxCairoPathData(this) );
1908 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1909 wxDouble tx
, wxDouble ty
)
1913 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1914 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1919 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1921 if ( !pen
.Ok() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1922 return wxNullGraphicsPen
;
1926 p
.SetRefData(new wxCairoPenData( this, pen
));
1931 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1933 if ( !brush
.Ok() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1934 return wxNullGraphicsBrush
;
1938 p
.SetRefData(new wxCairoBrushData( this, brush
));
1944 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1945 wxDouble x2
, wxDouble y2
,
1946 const wxGraphicsGradientStops
& stops
)
1949 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1950 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1956 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1957 wxDouble xc
, wxDouble yc
, wxDouble r
,
1958 const wxGraphicsGradientStops
& stops
)
1961 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1962 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
1968 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1973 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1977 return wxNullGraphicsFont
;
1980 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
1985 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
1989 return wxNullGraphicsBitmap
;
1992 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
1994 if ( bitmap
!= NULL
)
1997 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2001 return wxNullGraphicsBitmap
;
2005 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2006 wxDouble
WXUNUSED(x
),
2007 wxDouble
WXUNUSED(y
),
2008 wxDouble
WXUNUSED(w
),
2009 wxDouble
WXUNUSED(h
))
2011 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2012 return wxNullGraphicsBitmap
;
2015 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2017 return &gs_cairoGraphicsRenderer
;
2020 #else // !wxUSE_CAIRO
2022 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2027 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2029 // MSW and OS X have their own native default renderers, but the other ports
2030 // use Cairo by default
2031 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2032 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2034 return GetCairoRenderer();
2036 #endif // !(__WXMSW__ || __WXOSX__)
2038 #endif // wxUSE_GRAPHICS_CONTEXT