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
344 if ( !m_pen
.IsNull() )
346 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
350 return ( penwidth
% 2 ) == 1;
353 virtual void Clip( const wxRegion
®ion
);
355 cairo_surface_t
* m_mswSurface
;
358 // clips drawings to the rect
359 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
361 // resets the clipping to original extent
362 virtual void ResetClip();
364 virtual void * GetNativeContext();
366 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
368 virtual bool SetCompositionMode(wxCompositionMode op
);
370 virtual void BeginLayer(wxDouble opacity
);
372 virtual void EndLayer();
374 virtual void StrokePath( const wxGraphicsPath
& p
);
375 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
377 virtual void Translate( wxDouble dx
, wxDouble dy
);
378 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
379 virtual void Rotate( wxDouble angle
);
381 // concatenates this transform with the current transform of this context
382 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
384 // sets the transform of this context
385 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
387 // gets the matrix of this context
388 virtual wxGraphicsMatrix
GetTransform() const;
390 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
391 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
392 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
393 virtual void PushState();
394 virtual void PopState();
396 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
397 wxDouble
*descent
, wxDouble
*externalLeading
) const;
398 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
401 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
404 void Init(cairo_t
*context
);
408 wxVector
<float> m_layerOpacities
;
410 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
413 //-----------------------------------------------------------------------------
414 // wxCairoPenData implementation
415 //-----------------------------------------------------------------------------
417 wxCairoPenData::~wxCairoPenData()
419 delete[] m_userLengths
;
422 void wxCairoPenData::Init()
425 m_userLengths
= NULL
;
430 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
431 : wxGraphicsObjectRefData(renderer
)
435 m_width
= m_pen
.GetWidth();
439 m_red
= m_pen
.GetColour().Red()/255.0;
440 m_green
= m_pen
.GetColour().Green()/255.0;
441 m_blue
= m_pen
.GetColour().Blue()/255.0;
442 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
444 switch ( m_pen
.GetCap() )
447 m_cap
= CAIRO_LINE_CAP_ROUND
;
450 case wxCAP_PROJECTING
:
451 m_cap
= CAIRO_LINE_CAP_SQUARE
;
455 m_cap
= CAIRO_LINE_CAP_BUTT
;
459 m_cap
= CAIRO_LINE_CAP_BUTT
;
463 switch ( m_pen
.GetJoin() )
466 m_join
= CAIRO_LINE_JOIN_BEVEL
;
470 m_join
= CAIRO_LINE_JOIN_MITER
;
474 m_join
= CAIRO_LINE_JOIN_ROUND
;
478 m_join
= CAIRO_LINE_JOIN_MITER
;
482 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
483 const double dotted
[] =
485 dashUnit
, dashUnit
+ 2.0
487 static const double short_dashed
[] =
491 static const double dashed
[] =
495 static const double dotted_dashed
[] =
497 9.0 , 6.0 , 3.0 , 3.0
500 switch ( m_pen
.GetStyle() )
502 case wxPENSTYLE_SOLID
:
505 case wxPENSTYLE_DOT
:
506 m_count
= WXSIZEOF(dotted
);
507 m_userLengths
= new double[ m_count
] ;
508 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
509 m_lengths
= m_userLengths
;
512 case wxPENSTYLE_LONG_DASH
:
514 m_count
= WXSIZEOF(dashed
);
517 case wxPENSTYLE_SHORT_DASH
:
518 m_lengths
= short_dashed
;
519 m_count
= WXSIZEOF(short_dashed
);
522 case wxPENSTYLE_DOT_DASH
:
523 m_lengths
= dotted_dashed
;
524 m_count
= WXSIZEOF(dotted_dashed
);
527 case wxPENSTYLE_USER_DASH
:
530 m_count
= m_pen
.GetDashes( &wxdashes
) ;
531 if ((wxdashes
!= NULL
) && (m_count
> 0))
533 m_userLengths
= new double[m_count
] ;
534 for ( int i
= 0 ; i
< m_count
; ++i
)
536 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
538 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
539 m_userLengths
[i
] = dashUnit
+ 2.0 ;
540 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
541 m_userLengths
[i
] = dashUnit
;
544 m_lengths
= m_userLengths
;
547 case wxPENSTYLE_STIPPLE
:
550 wxBitmap* bmp = pen.GetStipple();
551 if ( bmp && bmp->Ok() )
553 wxDELETE( m_penImage );
554 wxDELETE( m_penBrush );
555 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
556 m_penBrush = new TextureBrush(m_penImage);
557 m_pen->SetBrush( m_penBrush );
563 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
564 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
567 wxDELETE( m_penBrush );
568 HatchStyle style = HatchStyleHorizontal;
569 switch( pen.GetStyle() )
571 case wxPENSTYLE_BDIAGONAL_HATCH :
572 style = HatchStyleBackwardDiagonal;
574 case wxPENSTYLE_CROSSDIAG_HATCH :
575 style = HatchStyleDiagonalCross;
577 case wxPENSTYLE_FDIAGONAL_HATCH :
578 style = HatchStyleForwardDiagonal;
580 case wxPENSTYLE_CROSS_HATCH :
581 style = HatchStyleCross;
583 case wxPENSTYLE_HORIZONTAL_HATCH :
584 style = HatchStyleHorizontal;
586 case wxPENSTYLE_VERTICAL_HATCH :
587 style = HatchStyleVertical;
591 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
592 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
593 m_pen->SetBrush( m_penBrush )
600 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
602 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
603 cairo_set_line_width(ctext
,m_width
);
604 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
605 cairo_set_line_cap(ctext
,m_cap
);
606 cairo_set_line_join(ctext
,m_join
);
607 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
610 //-----------------------------------------------------------------------------
611 // wxCairoBrushData implementation
612 //-----------------------------------------------------------------------------
614 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
615 : wxGraphicsObjectRefData( renderer
)
620 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
621 : wxGraphicsObjectRefData(renderer
)
625 m_red
= brush
.GetColour().Red()/255.0;
626 m_green
= brush
.GetColour().Green()/255.0;
627 m_blue
= brush
.GetColour().Blue()/255.0;
628 m_alpha
= brush
.GetColour().Alpha()/255.0;
630 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
632 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
633 brush.GetColour().Green() , brush.GetColour().Blue() ) );
635 else if ( brush.IsHatch() )
637 HatchStyle style = HatchStyleHorizontal;
638 switch( brush.GetStyle() )
640 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
641 style = HatchStyleBackwardDiagonal;
643 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
644 style = HatchStyleDiagonalCross;
646 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
647 style = HatchStyleForwardDiagonal;
649 case wxBRUSHSTYLE_CROSS_HATCH :
650 style = HatchStyleCross;
652 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
653 style = HatchStyleHorizontal;
655 case wxBRUSHSTYLE_VERTICAL_HATCH :
656 style = HatchStyleVertical;
660 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
661 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
665 wxBitmap* bmp = brush.GetStipple();
666 if ( bmp && bmp->Ok() )
668 wxDELETE( m_brushImage );
669 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
670 m_brush = new TextureBrush(m_brushImage);
676 wxCairoBrushData::~wxCairoBrushData ()
679 cairo_pattern_destroy(m_brushPattern
);
682 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
684 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
685 if ( m_brushPattern
)
687 cairo_set_source(ctext
,m_brushPattern
);
691 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
695 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
697 // loop over all the stops, they include the beginning and ending ones
698 const unsigned numStops
= stops
.GetCount();
699 for ( unsigned n
= 0; n
< numStops
; n
++ )
701 const wxGraphicsGradientStop stop
= stops
.Item(n
);
703 const wxColour col
= stop
.GetColour();
705 cairo_pattern_add_color_stop_rgba
716 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
717 wxT("Couldn't create cairo pattern"));
721 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
722 wxDouble x2
, wxDouble y2
,
723 const wxGraphicsGradientStops
& stops
)
725 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
727 AddGradientStops(stops
);
731 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
732 wxDouble xc
, wxDouble yc
,
734 const wxGraphicsGradientStops
& stops
)
736 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
738 AddGradientStops(stops
);
741 void wxCairoBrushData::Init()
743 m_brushPattern
= NULL
;
746 //-----------------------------------------------------------------------------
747 // wxCairoFontData implementation
748 //-----------------------------------------------------------------------------
750 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
751 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
753 m_red
= col
.Red()/255.0;
754 m_green
= col
.Green()/255.0;
755 m_blue
= col
.Blue()/255.0;
756 m_alpha
= col
.Alpha()/255.0;
757 m_size
= font
.GetPointSize();
758 m_underlined
= font
.GetUnderlined();
761 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
762 #elif defined(__WXGTK__)
763 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
765 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
766 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
767 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
771 wxCairoFontData::~wxCairoFontData()
774 cairo_font_face_destroy( m_font
);
775 #elif defined(__WXGTK__)
776 pango_font_description_free( m_font
);
781 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
783 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
784 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
786 // the rest is done using Pango layouts
787 #elif defined(__WXMAC__)
788 cairo_set_font_face(ctext
, m_font
);
789 cairo_set_font_size(ctext
, m_size
);
791 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
792 cairo_set_font_size(ctext
, m_size
);
796 //-----------------------------------------------------------------------------
797 // wxCairoPathData implementation
798 //-----------------------------------------------------------------------------
800 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
801 : wxGraphicsPathData(renderer
)
805 m_pathContext
= pathcontext
;
809 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
810 m_pathContext
= cairo_create(surface
);
811 cairo_surface_destroy (surface
);
815 wxCairoPathData::~wxCairoPathData()
817 cairo_destroy(m_pathContext
);
820 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
822 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
823 cairo_t
* pathcontext
= cairo_create(surface
);
824 cairo_surface_destroy (surface
);
826 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
827 cairo_append_path(pathcontext
, path
);
828 cairo_path_destroy(path
);
829 return new wxCairoPathData( GetRenderer() ,pathcontext
);
833 void* wxCairoPathData::GetNativePath() const
835 return cairo_copy_path(m_pathContext
) ;
838 void wxCairoPathData::UnGetNativePath(void *p
) const
840 cairo_path_destroy((cairo_path_t
*)p
);
847 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
849 cairo_move_to(m_pathContext
,x
,y
);
852 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
854 cairo_line_to(m_pathContext
,x
,y
);
857 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
859 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
860 cairo_append_path(m_pathContext
, p
);
864 void wxCairoPathData::CloseSubpath()
866 cairo_close_path(m_pathContext
);
869 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
871 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
874 // gets the last point of the current path, (0,0) if not yet set
875 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
878 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
885 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
887 // as clockwise means positive in our system (y pointing downwards)
888 // TODO make this interpretation dependent of the
890 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
891 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
893 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
896 // transforms each point of this path by the matrix
897 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
899 // as we don't have a true path object, we have to apply the inverse
900 // matrix to the context
901 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
902 cairo_matrix_invert( &m
);
903 cairo_transform(m_pathContext
,&m
);
906 // gets the bounding box enclosing all points (possibly including control points)
907 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
911 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
935 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
937 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
938 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
941 //-----------------------------------------------------------------------------
942 // wxCairoMatrixData implementation
943 //-----------------------------------------------------------------------------
945 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
946 : wxGraphicsMatrixData(renderer
)
952 wxCairoMatrixData::~wxCairoMatrixData()
957 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
959 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
962 // concatenates the matrix
963 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
965 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
968 // sets the matrix to the respective values
969 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
970 wxDouble tx
, wxDouble ty
)
972 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
975 // gets the component valuess of the matrix
976 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
977 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
979 if (a
) *a
= m_matrix
.xx
;
980 if (b
) *b
= m_matrix
.yx
;
981 if (c
) *c
= m_matrix
.xy
;
982 if (d
) *d
= m_matrix
.yy
;
983 if (tx
) *tx
= m_matrix
.x0
;
984 if (ty
) *ty
= m_matrix
.y0
;
987 // makes this the inverse matrix
988 void wxCairoMatrixData::Invert()
990 cairo_matrix_invert( &m_matrix
);
993 // returns true if the elements of the transformation matrix are equal ?
994 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
996 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
998 m_matrix
.xx
== tm
->xx
&&
999 m_matrix
.yx
== tm
->yx
&&
1000 m_matrix
.xy
== tm
->xy
&&
1001 m_matrix
.yy
== tm
->yy
&&
1002 m_matrix
.x0
== tm
->x0
&&
1003 m_matrix
.y0
== tm
->y0
) ;
1006 // return true if this is the identity matrix
1007 bool wxCairoMatrixData::IsIdentity() const
1009 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1010 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1017 // add the translation to this matrix
1018 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1020 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1023 // add the scale to this matrix
1024 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1026 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1029 // add the rotation to this matrix (radians)
1030 void wxCairoMatrixData::Rotate( wxDouble angle
)
1032 cairo_matrix_rotate( &m_matrix
, angle
) ;
1036 // apply the transforms
1039 // applies that matrix to the point
1040 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1042 double lx
= *x
, ly
= *y
;
1043 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1048 // applies the matrix except for translations
1049 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1051 double lx
= *dx
, ly
= *dy
;
1052 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1057 // returns the native representation
1058 void * wxCairoMatrixData::GetNativeMatrix() const
1060 return (void*) &m_matrix
;
1063 // wxCairoBitmap implementation
1064 //-----------------------------------------------------------------------------
1066 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1067 wxGraphicsObjectRefData( renderer
)
1070 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1073 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1075 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1077 #ifdef wxHAS_RAW_BITMAP
1078 int bw
= m_width
= bmp
.GetWidth();
1079 int bh
= m_height
= bmp
.GetHeight();
1080 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1081 m_buffer
= new unsigned char[bw
*bh
*4];
1082 wxUint32
* data
= (wxUint32
*)m_buffer
;
1084 // Create a surface object and copy the bitmap pixel data to it. if the
1085 // image has alpha (or a mask represented as alpha) then we'll use a
1086 // different format and iterator than if it doesn't...
1087 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1089 m_surface
= cairo_image_surface_create_for_data(
1090 m_buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1091 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1092 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1094 wxAlphaPixelData::Iterator
p(pixData
);
1095 for (int y
=0; y
<bh
; y
++)
1097 wxAlphaPixelData::Iterator rowStart
= p
;
1098 for (int x
=0; x
<bw
; x
++)
1100 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1101 // with alpha in the upper 8 bits, then red, then green, then
1102 // blue. The 32-bit quantities are stored native-endian.
1103 // Pre-multiplied alpha is used.
1104 unsigned char alpha
= p
.Alpha();
1108 *data
= ( alpha
<< 24
1109 | (p
.Red() * alpha
/255) << 16
1110 | (p
.Green() * alpha
/255) << 8
1111 | (p
.Blue() * alpha
/255) );
1116 p
.OffsetY(pixData
, 1);
1121 m_surface
= cairo_image_surface_create_for_data(
1122 m_buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1123 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1124 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1126 wxNativePixelData::Iterator
p(pixData
);
1127 for (int y
=0; y
<bh
; y
++)
1129 wxNativePixelData::Iterator rowStart
= p
;
1130 for (int x
=0; x
<bw
; x
++)
1132 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1133 // the upper 8 bits unused. Red, Green, and Blue are stored in
1134 // the remaining 24 bits in that order. The 32-bit quantities
1135 // are stored native-endian.
1136 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1141 p
.OffsetY(pixData
, 1);
1144 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1145 #endif // wxHAS_RAW_BITMAP
1148 wxCairoBitmapData::~wxCairoBitmapData()
1151 cairo_pattern_destroy(m_pattern
);
1154 cairo_surface_destroy(m_surface
);
1161 //-----------------------------------------------------------------------------
1162 // wxCairoContext implementation
1163 //-----------------------------------------------------------------------------
1165 class wxCairoOffsetHelper
1168 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1173 cairo_translate( m_ctx
, 0.5, 0.5 );
1175 ~wxCairoOffsetHelper( )
1178 cairo_translate( m_ctx
, -0.5, -0.5 );
1185 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1186 : wxGraphicsContext(renderer
)
1189 const wxDCImpl
*impl
= dc
.GetImpl();
1190 Init( (cairo_t
*) impl
->GetCairoContext() );
1192 wxSize sz
= dc
.GetSize();
1196 wxPoint org
= dc
.GetDeviceOrigin();
1197 cairo_translate( m_context
, org
.x
, org
.y
);
1200 dc
.GetUserScale( &sx
, &sy
);
1201 cairo_scale( m_context
, sx
, sy
);
1203 org
= dc
.GetLogicalOrigin();
1204 cairo_translate( m_context
, -org
.x
, -org
.y
);
1208 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1209 : wxGraphicsContext(renderer
)
1212 dc
.GetSize( &width
, &height
);
1217 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1218 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1221 wxGraphicsMatrix matrix
= CreateMatrix();
1223 wxPoint org
= dc
.GetDeviceOrigin();
1224 matrix
.Translate( org
.x
, org
.y
);
1226 org
= dc
.GetLogicalOrigin();
1227 matrix
.Translate( -org
.x
, -org
.y
);
1230 dc
.GetUserScale( &sx
, &sy
);
1231 matrix
.Scale( sx
, sy
);
1233 ConcatTransform( matrix
);
1238 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1239 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1240 Init( cairo_create( surface
) );
1241 cairo_surface_destroy( surface
);
1245 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1246 : wxGraphicsContext(renderer
)
1249 dc
.GetSize( &width
, &height
);
1254 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1255 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1258 wxGraphicsMatrix matrix
= CreateMatrix();
1260 wxPoint org
= dc
.GetDeviceOrigin();
1261 matrix
.Translate( org
.x
, org
.y
);
1263 org
= dc
.GetLogicalOrigin();
1264 matrix
.Translate( -org
.x
, -org
.y
);
1267 dc
.GetUserScale( &sx
, &sy
);
1268 matrix
.Scale( sx
, sy
);
1270 ConcatTransform( matrix
);
1275 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1276 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1277 Init( cairo_create( surface
) );
1278 cairo_surface_destroy( surface
);
1283 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1284 : wxGraphicsContext(renderer
)
1286 Init( gdk_cairo_create( drawable
) );
1289 gdk_drawable_get_size( drawable
, &width
, &height
);
1296 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1297 : wxGraphicsContext(renderer
)
1299 m_mswSurface
= cairo_win32_surface_create(handle
);
1300 Init( cairo_create(m_mswSurface
) );
1307 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1308 : wxGraphicsContext(renderer
)
1315 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1316 : wxGraphicsContext(renderer
)
1319 // something along these lines (copied from dcclient)
1321 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1322 // code should still be able to create wxClientDCs for them, so we will
1323 // use the parent window here then.
1324 if (window
->m_wxwindow
== NULL
)
1326 window
= window
->GetParent();
1329 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1331 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1333 wxSize sz
= window
->GetSize();
1339 wxCairoContext::~wxCairoContext()
1345 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1346 m_context
= cairo_create(m_mswSurface
);
1349 cairo_destroy(m_context
);
1353 cairo_surface_destroy(m_mswSurface
);
1357 void wxCairoContext::Init(cairo_t
*context
)
1359 m_context
= context
;
1365 void wxCairoContext::Clip( const wxRegion
& region
)
1367 // Create a path with all the rectangles in the region
1368 wxGraphicsPath path
= GetRenderer()->CreatePath();
1369 wxRegionIterator
ri(region
);
1372 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1376 // Put it in the context
1377 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1378 cairo_append_path(m_context
, cp
);
1380 // clip to that path
1381 cairo_clip(m_context
);
1382 path
.UnGetNativePath(cp
);
1385 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1387 // Create a path with this rectangle
1388 wxGraphicsPath path
= GetRenderer()->CreatePath();
1389 path
.AddRectangle(x
,y
,w
,h
);
1391 // Put it in the context
1392 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1393 cairo_append_path(m_context
, cp
);
1395 // clip to that path
1396 cairo_clip(m_context
);
1397 path
.UnGetNativePath(cp
);
1400 void wxCairoContext::ResetClip()
1402 cairo_reset_clip(m_context
);
1406 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1408 if ( !m_pen
.IsNull() )
1410 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1411 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1412 cairo_append_path(m_context
,cp
);
1413 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1414 cairo_stroke(m_context
);
1415 path
.UnGetNativePath(cp
);
1419 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1421 if ( !m_brush
.IsNull() )
1423 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1424 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1425 cairo_append_path(m_context
,cp
);
1426 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1427 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1428 cairo_fill(m_context
);
1429 path
.UnGetNativePath(cp
);
1433 void wxCairoContext::Rotate( wxDouble angle
)
1435 cairo_rotate(m_context
,angle
);
1438 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1440 cairo_translate(m_context
,dx
,dy
);
1443 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1445 cairo_scale(m_context
,xScale
,yScale
);
1448 // concatenates this transform with the current transform of this context
1449 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1451 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1454 // sets the transform of this context
1455 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1457 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1460 // gets the matrix of this context
1461 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1463 wxGraphicsMatrix matrix
= CreateMatrix();
1464 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1470 void wxCairoContext::PushState()
1472 cairo_save(m_context
);
1475 void wxCairoContext::PopState()
1477 cairo_restore(m_context
);
1480 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1482 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1483 DrawBitmap(bitmap
, x
, y
, w
, h
);
1487 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1491 // In case we're scaling the image by using a width and height different
1492 // than the bitmap's size create a pattern transformation on the surface and
1493 // draw the transformed pattern.
1494 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1495 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1496 wxSize size
= data
->GetSize();
1498 wxDouble scaleX
= w
/ size
.GetWidth();
1499 wxDouble scaleY
= h
/ size
.GetHeight();
1501 // prepare to draw the image
1502 cairo_translate(m_context
, x
, y
);
1503 cairo_scale(m_context
, scaleX
, scaleY
);
1504 cairo_set_source(m_context
, pattern
);
1505 // use the original size here since the context is scaled already...
1506 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1507 // fill the rectangle using the pattern
1508 cairo_fill(m_context
);
1513 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1515 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1516 // start using the Cairo backend on other platforms then we may need to
1517 // fiddle with this...
1518 DrawBitmap(icon
, x
, y
, w
, h
);
1522 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1524 wxCHECK_RET( !m_font
.IsNull(),
1525 wxT("wxCairoContext::DrawText - no valid font set") );
1530 const wxCharBuffer data
= str
.utf8_str();
1534 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1537 size_t datalen
= strlen(data
);
1539 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1540 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1541 pango_layout_set_font_description( layout
, font_data
->GetFont());
1542 pango_layout_set_text(layout
, data
, datalen
);
1544 if (font_data
->GetUnderlined())
1546 PangoAttrList
*attrs
= pango_attr_list_new();
1547 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1548 pango_attr_list_insert(attrs
, attr
);
1549 pango_layout_set_attributes(layout
, attrs
);
1550 pango_attr_list_unref(attrs
);
1553 cairo_move_to(m_context
, x
, y
);
1554 pango_cairo_show_layout (m_context
, layout
);
1556 g_object_unref (layout
);
1558 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1559 // the position we move to by the ascent.
1560 cairo_font_extents_t fe
;
1561 cairo_font_extents(m_context
, &fe
);
1562 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1564 cairo_show_text(m_context
, data
);
1568 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1569 wxDouble
*descent
, wxDouble
*externalLeading
) const
1571 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1579 if ( externalLeading
)
1580 *externalLeading
= 0;
1588 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1589 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1590 const wxCharBuffer data
= str
.utf8_str();
1595 pango_layout_set_text( layout
, data
, strlen(data
) );
1596 pango_layout_get_pixel_size (layout
, &w
, &h
);
1603 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1604 int baseline
= pango_layout_iter_get_baseline(iter
);
1605 pango_layout_iter_free(iter
);
1606 *descent
= h
- PANGO_PIXELS(baseline
);
1608 g_object_unref (layout
);
1610 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1614 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1615 cairo_text_extents_t te
;
1616 cairo_text_extents(m_context
, buf
, &te
);
1620 if (height
|| descent
|| externalLeading
)
1622 cairo_font_extents_t fe
;
1623 cairo_font_extents(m_context
, &fe
);
1625 // some backends have negative descents
1627 if ( fe
.descent
< 0 )
1628 fe
.descent
= -fe
.descent
;
1630 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1632 // some backends are broken re height ... (eg currently ATSUI)
1633 fe
.height
= fe
.ascent
+ fe
.descent
;
1637 *height
= fe
.height
;
1639 *descent
= fe
.descent
;
1640 if ( externalLeading
)
1641 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1646 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1649 widths
.Add(0, text
.length());
1651 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1659 void * wxCairoContext::GetNativeContext()
1664 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1666 if (m_antialias
== antialias
)
1669 m_antialias
= antialias
;
1671 cairo_antialias_t antialiasMode
;
1674 case wxANTIALIAS_DEFAULT
:
1675 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1677 case wxANTIALIAS_NONE
:
1678 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1683 cairo_set_antialias(m_context
, antialiasMode
);
1687 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1689 if ( m_composition
== op
)
1693 cairo_operator_t cop
;
1696 case wxCOMPOSITION_CLEAR
:
1697 cop
= CAIRO_OPERATOR_CLEAR
;
1699 case wxCOMPOSITION_SOURCE
:
1700 cop
= CAIRO_OPERATOR_SOURCE
;
1702 case wxCOMPOSITION_OVER
:
1703 cop
= CAIRO_OPERATOR_OVER
;
1705 case wxCOMPOSITION_IN
:
1706 cop
= CAIRO_OPERATOR_IN
;
1708 case wxCOMPOSITION_OUT
:
1709 cop
= CAIRO_OPERATOR_OUT
;
1711 case wxCOMPOSITION_ATOP
:
1712 cop
= CAIRO_OPERATOR_ATOP
;
1714 case wxCOMPOSITION_DEST
:
1715 cop
= CAIRO_OPERATOR_DEST
;
1717 case wxCOMPOSITION_DEST_OVER
:
1718 cop
= CAIRO_OPERATOR_DEST_OVER
;
1720 case wxCOMPOSITION_DEST_IN
:
1721 cop
= CAIRO_OPERATOR_DEST_IN
;
1723 case wxCOMPOSITION_DEST_OUT
:
1724 cop
= CAIRO_OPERATOR_DEST_OUT
;
1726 case wxCOMPOSITION_DEST_ATOP
:
1727 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1729 case wxCOMPOSITION_XOR
:
1730 cop
= CAIRO_OPERATOR_XOR
;
1732 case wxCOMPOSITION_ADD
:
1733 cop
= CAIRO_OPERATOR_ADD
;
1738 cairo_set_operator(m_context
, cop
);
1742 void wxCairoContext::BeginLayer(wxDouble opacity
)
1744 m_layerOpacities
.push_back(opacity
);
1745 cairo_push_group(m_context
);
1748 void wxCairoContext::EndLayer()
1750 float opacity
= m_layerOpacities
.back();
1751 m_layerOpacities
.pop_back();
1752 cairo_pop_group_to_source(m_context
);
1753 cairo_paint_with_alpha(m_context
,opacity
);
1756 //-----------------------------------------------------------------------------
1757 // wxCairoRenderer declaration
1758 //-----------------------------------------------------------------------------
1760 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1763 wxCairoRenderer() {}
1765 virtual ~wxCairoRenderer() {}
1769 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1770 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1771 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1773 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1775 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1777 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1779 virtual wxGraphicsContext
* CreateMeasuringContext();
1783 virtual wxGraphicsPath
CreatePath();
1787 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1788 wxDouble tx
=0.0, wxDouble ty
=0.0);
1791 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1793 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1795 virtual wxGraphicsBrush
1796 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1797 wxDouble x2
, wxDouble y2
,
1798 const wxGraphicsGradientStops
& stops
);
1800 virtual wxGraphicsBrush
1801 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1802 wxDouble xc
, wxDouble yc
,
1804 const wxGraphicsGradientStops
& stops
);
1807 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1809 // create a native bitmap representation
1810 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1812 // create a graphics bitmap from a native bitmap
1813 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1815 // create a subimage from a native image representation
1816 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1819 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1822 //-----------------------------------------------------------------------------
1823 // wxCairoRenderer implementation
1824 //-----------------------------------------------------------------------------
1826 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1828 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1829 // temporary hack to allow creating a cairo context on any platform
1830 extern wxGraphicsRenderer
* gCairoRenderer
;
1831 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1833 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1835 return new wxCairoContext(this,dc
);
1838 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1840 return new wxCairoContext(this,dc
);
1843 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1846 const wxDCImpl
*impl
= dc
.GetImpl();
1847 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1849 return new wxCairoContext(this,dc
);
1855 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1858 return new wxCairoContext(this,(HDC
)context
);
1860 return new wxCairoContext(this,(cairo_t
*)context
);
1865 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1868 return new wxCairoContext(this,(GdkDrawable
*)window
);
1874 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1877 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1883 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1885 return new wxCairoContext(this, window
);
1890 wxGraphicsPath
wxCairoRenderer::CreatePath()
1892 wxGraphicsPath path
;
1893 path
.SetRefData( new wxCairoPathData(this) );
1900 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1901 wxDouble tx
, wxDouble ty
)
1905 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1906 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1911 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1913 if ( !pen
.Ok() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1914 return wxNullGraphicsPen
;
1918 p
.SetRefData(new wxCairoPenData( this, pen
));
1923 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1925 if ( !brush
.Ok() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1926 return wxNullGraphicsBrush
;
1930 p
.SetRefData(new wxCairoBrushData( this, brush
));
1936 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1937 wxDouble x2
, wxDouble y2
,
1938 const wxGraphicsGradientStops
& stops
)
1941 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1942 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1948 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1949 wxDouble xc
, wxDouble yc
, wxDouble r
,
1950 const wxGraphicsGradientStops
& stops
)
1953 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1954 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
1960 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1965 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1969 return wxNullGraphicsFont
;
1972 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
1977 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
1981 return wxNullGraphicsBitmap
;
1984 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
1986 if ( bitmap
!= NULL
)
1989 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
1993 return wxNullGraphicsBitmap
;
1997 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
1998 wxDouble
WXUNUSED(x
),
1999 wxDouble
WXUNUSED(y
),
2000 wxDouble
WXUNUSED(w
),
2001 wxDouble
WXUNUSED(h
))
2003 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2004 return wxNullGraphicsBitmap
;
2007 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2009 return &gs_cairoGraphicsRenderer
;
2012 #else // !wxUSE_CAIRO
2014 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2019 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2021 // MSW and OS X have their own native default renderers, but the other ports
2022 // use Cairo by default
2023 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2024 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2026 return GetCairoRenderer();
2028 #endif // !(__WXMSW__ || __WXOSX__)
2030 #endif // wxUSE_GRAPHICS_CONTEXT