1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_GRAPHICS_CONTEXT
20 #include "wx/graphics.h"
27 #include "wx/bitmap.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcmemory.h"
31 #include "wx/dcprint.h"
33 #include "wx/window.h"
37 #include "wx/private/graphics.h"
38 #include "wx/rawbmp.h"
39 #include "wx/vector.h"
43 //-----------------------------------------------------------------------------
44 // device context implementation
46 // more and more of the dc functionality should be implemented by calling
47 // the appropricate wxCairoContext, but we will have to do that step by step
48 // also coordinate conversions should be moved to native matrix ops
49 //-----------------------------------------------------------------------------
51 // we always stock two context states, one at entry, to be able to preserve the
52 // state we were called with, the other one after changing to HI Graphics orientation
53 // (this one is used for getting back clippings etc)
55 //-----------------------------------------------------------------------------
56 // wxGraphicsPath implementation
57 //-----------------------------------------------------------------------------
59 // TODO remove this dependency (gdiplus needs the macros)
62 #define max(a,b) (((a) > (b)) ? (a) : (b))
66 #define min(a,b) (((a) < (b)) ? (a) : (b))
71 #include <cairo-win32.h>
76 #include "wx/fontutil.h"
77 #include "wx/gtk/dc.h"
81 #include <cairo-win32.h>
85 #include "wx/osx/private.h"
86 #include <cairo-quartz.h>
87 #include <cairo-atsui.h>
90 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
93 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
96 virtual wxGraphicsObjectRefData
*Clone() const;
99 // These are the path primitives from which everything else can be constructed
102 // begins a new subpath at (x,y)
103 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
105 // adds a straight line from the current point to (x,y)
106 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
108 // adds a cubic Bezier curve from the current point, using two control points and an end point
109 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
112 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
113 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
115 // gets the last point of the current path, (0,0) if not yet set
116 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
119 virtual void AddPath( const wxGraphicsPathData
* path
);
121 // closes the current sub-path
122 virtual void CloseSubpath();
125 // These are convenience functions which - if not available natively will be assembled
126 // using the primitives from above
131 // appends a rectangle as a new closed subpath
132 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
133 // appends an ellipsis as a new closed subpath fitting the passed rectangle
134 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
136 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
137 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
140 // returns the native path
141 virtual void * GetNativePath() const ;
143 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
144 virtual void UnGetNativePath(void *p
) const;
146 // transforms each point of this path by the matrix
147 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
149 // gets the bounding box enclosing all points (possibly including control points)
150 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
152 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
155 cairo_t
* m_pathContext
;
158 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
161 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
162 virtual ~wxCairoMatrixData() ;
164 virtual wxGraphicsObjectRefData
*Clone() const ;
166 // concatenates the matrix
167 virtual void Concat( const wxGraphicsMatrixData
*t
);
169 // sets the matrix to the respective values
170 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
171 wxDouble tx
=0.0, wxDouble ty
=0.0);
173 // gets the component valuess of the matrix
174 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
175 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
177 // makes this the inverse matrix
178 virtual void Invert();
180 // returns true if the elements of the transformation matrix are equal ?
181 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
183 // return true if this is the identity matrix
184 virtual bool IsIdentity() const;
190 // add the translation to this matrix
191 virtual void Translate( wxDouble dx
, wxDouble dy
);
193 // add the scale to this matrix
194 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
196 // add the rotation to this matrix (radians)
197 virtual void Rotate( wxDouble angle
);
200 // apply the transforms
203 // applies that matrix to the point
204 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
206 // applies the matrix except for translations
207 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
209 // returns the native representation
210 virtual void * GetNativeMatrix() const;
212 cairo_matrix_t m_matrix
;
215 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
218 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
223 virtual void Apply( wxGraphicsContext
* context
);
224 virtual wxDouble
GetWidth() { return m_width
; }
234 cairo_line_cap_t m_cap
;
235 cairo_line_join_t m_join
;
238 const double *m_lengths
;
239 double *m_userLengths
;
243 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
246 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
249 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
250 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
251 ~wxCairoBrushData ();
253 virtual void Apply( wxGraphicsContext
* context
);
255 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
256 wxDouble x2
, wxDouble y2
,
257 const wxGraphicsGradientStops
& stops
);
258 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
259 wxDouble xc
, wxDouble yc
, wxDouble radius
,
260 const wxGraphicsGradientStops
& stops
);
265 // common part of Create{Linear,Radial}GradientBrush()
266 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
274 cairo_pattern_t
* m_brushPattern
;
277 class wxCairoFontData
: public wxGraphicsObjectRefData
280 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
283 virtual void Apply( wxGraphicsContext
* context
);
285 const PangoFontDescription
* GetFont() const { return m_font
; }
286 bool GetUnderlined() const { return m_underlined
; }
296 cairo_font_face_t
*m_font
;
297 #elif defined(__WXGTK__)
298 PangoFontDescription
* m_font
;
300 wxCharBuffer m_fontName
;
301 cairo_font_slant_t m_slant
;
302 cairo_font_weight_t m_weight
;
306 class wxCairoBitmapData
: public wxGraphicsObjectRefData
309 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
310 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
311 ~wxCairoBitmapData();
313 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
314 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
315 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
317 cairo_surface_t
* m_surface
;
318 cairo_pattern_t
* m_pattern
;
321 unsigned char* m_buffer
;
324 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
327 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
328 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
329 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
331 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
334 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
336 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
337 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
339 virtual ~wxCairoContext();
341 virtual bool ShouldOffset() const
343 if ( !m_enableOffset
)
347 if ( !m_pen
.IsNull() )
349 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
353 return ( penwidth
% 2 ) == 1;
356 virtual void Clip( const wxRegion
®ion
);
358 cairo_surface_t
* m_mswSurface
;
361 // clips drawings to the rect
362 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
364 // resets the clipping to original extent
365 virtual void ResetClip();
367 virtual void * GetNativeContext();
369 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
371 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
373 virtual bool SetCompositionMode(wxCompositionMode op
);
375 virtual void BeginLayer(wxDouble opacity
);
377 virtual void EndLayer();
379 virtual void StrokePath( const wxGraphicsPath
& p
);
380 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
382 virtual void Translate( wxDouble dx
, wxDouble dy
);
383 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
384 virtual void Rotate( wxDouble angle
);
386 // concatenates this transform with the current transform of this context
387 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
389 // sets the transform of this context
390 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
392 // gets the matrix of this context
393 virtual wxGraphicsMatrix
GetTransform() const;
395 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
396 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
397 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
398 virtual void PushState();
399 virtual void PopState();
401 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
402 wxDouble
*descent
, wxDouble
*externalLeading
) const;
403 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
406 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
409 void Init(cairo_t
*context
);
413 wxVector
<float> m_layerOpacities
;
415 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
418 //-----------------------------------------------------------------------------
419 // wxCairoPenData implementation
420 //-----------------------------------------------------------------------------
422 wxCairoPenData::~wxCairoPenData()
424 delete[] m_userLengths
;
427 void wxCairoPenData::Init()
430 m_userLengths
= NULL
;
435 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
436 : wxGraphicsObjectRefData(renderer
)
440 m_width
= m_pen
.GetWidth();
444 m_red
= m_pen
.GetColour().Red()/255.0;
445 m_green
= m_pen
.GetColour().Green()/255.0;
446 m_blue
= m_pen
.GetColour().Blue()/255.0;
447 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
449 switch ( m_pen
.GetCap() )
452 m_cap
= CAIRO_LINE_CAP_ROUND
;
455 case wxCAP_PROJECTING
:
456 m_cap
= CAIRO_LINE_CAP_SQUARE
;
460 m_cap
= CAIRO_LINE_CAP_BUTT
;
464 m_cap
= CAIRO_LINE_CAP_BUTT
;
468 switch ( m_pen
.GetJoin() )
471 m_join
= CAIRO_LINE_JOIN_BEVEL
;
475 m_join
= CAIRO_LINE_JOIN_MITER
;
479 m_join
= CAIRO_LINE_JOIN_ROUND
;
483 m_join
= CAIRO_LINE_JOIN_MITER
;
487 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
488 const double dotted
[] =
490 dashUnit
, dashUnit
+ 2.0
492 static const double short_dashed
[] =
496 static const double dashed
[] =
500 static const double dotted_dashed
[] =
502 9.0 , 6.0 , 3.0 , 3.0
505 switch ( m_pen
.GetStyle() )
507 case wxPENSTYLE_SOLID
:
510 case wxPENSTYLE_DOT
:
511 m_count
= WXSIZEOF(dotted
);
512 m_userLengths
= new double[ m_count
] ;
513 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
514 m_lengths
= m_userLengths
;
517 case wxPENSTYLE_LONG_DASH
:
519 m_count
= WXSIZEOF(dashed
);
522 case wxPENSTYLE_SHORT_DASH
:
523 m_lengths
= short_dashed
;
524 m_count
= WXSIZEOF(short_dashed
);
527 case wxPENSTYLE_DOT_DASH
:
528 m_lengths
= dotted_dashed
;
529 m_count
= WXSIZEOF(dotted_dashed
);
532 case wxPENSTYLE_USER_DASH
:
535 m_count
= m_pen
.GetDashes( &wxdashes
) ;
536 if ((wxdashes
!= NULL
) && (m_count
> 0))
538 m_userLengths
= new double[m_count
] ;
539 for ( int i
= 0 ; i
< m_count
; ++i
)
541 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
543 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
544 m_userLengths
[i
] = dashUnit
+ 2.0 ;
545 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
546 m_userLengths
[i
] = dashUnit
;
549 m_lengths
= m_userLengths
;
552 case wxPENSTYLE_STIPPLE
:
555 wxBitmap* bmp = pen.GetStipple();
556 if ( bmp && bmp->IsOk() )
558 wxDELETE( m_penImage );
559 wxDELETE( m_penBrush );
560 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
561 m_penBrush = new TextureBrush(m_penImage);
562 m_pen->SetBrush( m_penBrush );
568 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
569 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
572 wxDELETE( m_penBrush );
573 HatchStyle style = HatchStyleHorizontal;
574 switch( pen.GetStyle() )
576 case wxPENSTYLE_BDIAGONAL_HATCH :
577 style = HatchStyleBackwardDiagonal;
579 case wxPENSTYLE_CROSSDIAG_HATCH :
580 style = HatchStyleDiagonalCross;
582 case wxPENSTYLE_FDIAGONAL_HATCH :
583 style = HatchStyleForwardDiagonal;
585 case wxPENSTYLE_CROSS_HATCH :
586 style = HatchStyleCross;
588 case wxPENSTYLE_HORIZONTAL_HATCH :
589 style = HatchStyleHorizontal;
591 case wxPENSTYLE_VERTICAL_HATCH :
592 style = HatchStyleVertical;
596 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
597 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
598 m_pen->SetBrush( m_penBrush )
605 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
607 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
608 cairo_set_line_width(ctext
,m_width
);
609 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
610 cairo_set_line_cap(ctext
,m_cap
);
611 cairo_set_line_join(ctext
,m_join
);
612 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
615 //-----------------------------------------------------------------------------
616 // wxCairoBrushData implementation
617 //-----------------------------------------------------------------------------
619 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
620 : wxGraphicsObjectRefData( renderer
)
625 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
626 : wxGraphicsObjectRefData(renderer
)
630 m_red
= brush
.GetColour().Red()/255.0;
631 m_green
= brush
.GetColour().Green()/255.0;
632 m_blue
= brush
.GetColour().Blue()/255.0;
633 m_alpha
= brush
.GetColour().Alpha()/255.0;
635 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
637 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
638 brush.GetColour().Green() , brush.GetColour().Blue() ) );
640 else if ( brush.IsHatch() )
642 HatchStyle style = HatchStyleHorizontal;
643 switch( brush.GetStyle() )
645 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
646 style = HatchStyleBackwardDiagonal;
648 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
649 style = HatchStyleDiagonalCross;
651 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
652 style = HatchStyleForwardDiagonal;
654 case wxBRUSHSTYLE_CROSS_HATCH :
655 style = HatchStyleCross;
657 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
658 style = HatchStyleHorizontal;
660 case wxBRUSHSTYLE_VERTICAL_HATCH :
661 style = HatchStyleVertical;
665 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
666 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
670 wxBitmap* bmp = brush.GetStipple();
671 if ( bmp && bmp->IsOk() )
673 wxDELETE( m_brushImage );
674 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
675 m_brush = new TextureBrush(m_brushImage);
681 wxCairoBrushData::~wxCairoBrushData ()
684 cairo_pattern_destroy(m_brushPattern
);
687 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
689 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
690 if ( m_brushPattern
)
692 cairo_set_source(ctext
,m_brushPattern
);
696 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
700 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
702 // loop over all the stops, they include the beginning and ending ones
703 const unsigned numStops
= stops
.GetCount();
704 for ( unsigned n
= 0; n
< numStops
; n
++ )
706 const wxGraphicsGradientStop stop
= stops
.Item(n
);
708 const wxColour col
= stop
.GetColour();
710 cairo_pattern_add_color_stop_rgba
721 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
722 wxT("Couldn't create cairo pattern"));
726 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
727 wxDouble x2
, wxDouble y2
,
728 const wxGraphicsGradientStops
& stops
)
730 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
732 AddGradientStops(stops
);
736 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
737 wxDouble xc
, wxDouble yc
,
739 const wxGraphicsGradientStops
& stops
)
741 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
743 AddGradientStops(stops
);
746 void wxCairoBrushData::Init()
748 m_brushPattern
= NULL
;
751 //-----------------------------------------------------------------------------
752 // wxCairoFontData implementation
753 //-----------------------------------------------------------------------------
755 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
756 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
758 m_red
= col
.Red()/255.0;
759 m_green
= col
.Green()/255.0;
760 m_blue
= col
.Blue()/255.0;
761 m_alpha
= col
.Alpha()/255.0;
762 m_size
= font
.GetPointSize();
763 m_underlined
= font
.GetUnderlined();
766 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
767 #elif defined(__WXGTK__)
768 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
770 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
771 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
772 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
776 wxCairoFontData::~wxCairoFontData()
779 cairo_font_face_destroy( m_font
);
780 #elif defined(__WXGTK__)
781 pango_font_description_free( m_font
);
786 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
788 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
789 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
791 // the rest is done using Pango layouts
792 #elif defined(__WXMAC__)
793 cairo_set_font_face(ctext
, m_font
);
794 cairo_set_font_size(ctext
, m_size
);
796 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
797 cairo_set_font_size(ctext
, m_size
);
801 //-----------------------------------------------------------------------------
802 // wxCairoPathData implementation
803 //-----------------------------------------------------------------------------
805 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
806 : wxGraphicsPathData(renderer
)
810 m_pathContext
= pathcontext
;
814 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
815 m_pathContext
= cairo_create(surface
);
816 cairo_surface_destroy (surface
);
820 wxCairoPathData::~wxCairoPathData()
822 cairo_destroy(m_pathContext
);
825 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
827 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
828 cairo_t
* pathcontext
= cairo_create(surface
);
829 cairo_surface_destroy (surface
);
831 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
832 cairo_append_path(pathcontext
, path
);
833 cairo_path_destroy(path
);
834 return new wxCairoPathData( GetRenderer() ,pathcontext
);
838 void* wxCairoPathData::GetNativePath() const
840 return cairo_copy_path(m_pathContext
) ;
843 void wxCairoPathData::UnGetNativePath(void *p
) const
845 cairo_path_destroy((cairo_path_t
*)p
);
852 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
854 cairo_move_to(m_pathContext
,x
,y
);
857 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
859 cairo_line_to(m_pathContext
,x
,y
);
862 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
864 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
865 cairo_append_path(m_pathContext
, p
);
869 void wxCairoPathData::CloseSubpath()
871 cairo_close_path(m_pathContext
);
874 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
876 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
879 // gets the last point of the current path, (0,0) if not yet set
880 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
883 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
890 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
892 // as clockwise means positive in our system (y pointing downwards)
893 // TODO make this interpretation dependent of the
895 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
896 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
898 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
901 // transforms each point of this path by the matrix
902 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
904 // as we don't have a true path object, we have to apply the inverse
905 // matrix to the context
906 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
907 cairo_matrix_invert( &m
);
908 cairo_transform(m_pathContext
,&m
);
911 // gets the bounding box enclosing all points (possibly including control points)
912 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
916 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
940 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
942 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
943 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
946 //-----------------------------------------------------------------------------
947 // wxCairoMatrixData implementation
948 //-----------------------------------------------------------------------------
950 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
951 : wxGraphicsMatrixData(renderer
)
957 wxCairoMatrixData::~wxCairoMatrixData()
962 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
964 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
967 // concatenates the matrix
968 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
970 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
973 // sets the matrix to the respective values
974 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
975 wxDouble tx
, wxDouble ty
)
977 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
980 // gets the component valuess of the matrix
981 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
982 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
984 if (a
) *a
= m_matrix
.xx
;
985 if (b
) *b
= m_matrix
.yx
;
986 if (c
) *c
= m_matrix
.xy
;
987 if (d
) *d
= m_matrix
.yy
;
988 if (tx
) *tx
= m_matrix
.x0
;
989 if (ty
) *ty
= m_matrix
.y0
;
992 // makes this the inverse matrix
993 void wxCairoMatrixData::Invert()
995 cairo_matrix_invert( &m_matrix
);
998 // returns true if the elements of the transformation matrix are equal ?
999 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1001 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1003 m_matrix
.xx
== tm
->xx
&&
1004 m_matrix
.yx
== tm
->yx
&&
1005 m_matrix
.xy
== tm
->xy
&&
1006 m_matrix
.yy
== tm
->yy
&&
1007 m_matrix
.x0
== tm
->x0
&&
1008 m_matrix
.y0
== tm
->y0
) ;
1011 // return true if this is the identity matrix
1012 bool wxCairoMatrixData::IsIdentity() const
1014 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1015 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1022 // add the translation to this matrix
1023 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1025 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1028 // add the scale to this matrix
1029 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1031 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1034 // add the rotation to this matrix (radians)
1035 void wxCairoMatrixData::Rotate( wxDouble angle
)
1037 cairo_matrix_rotate( &m_matrix
, angle
) ;
1041 // apply the transforms
1044 // applies that matrix to the point
1045 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1047 double lx
= *x
, ly
= *y
;
1048 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1053 // applies the matrix except for translations
1054 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1056 double lx
= *dx
, ly
= *dy
;
1057 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1062 // returns the native representation
1063 void * wxCairoMatrixData::GetNativeMatrix() const
1065 return (void*) &m_matrix
;
1068 // wxCairoBitmap implementation
1069 //-----------------------------------------------------------------------------
1071 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1072 wxGraphicsObjectRefData( renderer
)
1075 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1078 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1080 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1082 #ifdef wxHAS_RAW_BITMAP
1083 int bw
= m_width
= bmp
.GetWidth();
1084 int bh
= m_height
= bmp
.GetHeight();
1085 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1086 m_buffer
= new unsigned char[bw
*bh
*4];
1087 wxUint32
* data
= (wxUint32
*)m_buffer
;
1089 // Create a surface object and copy the bitmap pixel data to it. if the
1090 // image has alpha (or a mask represented as alpha) then we'll use a
1091 // different format and iterator than if it doesn't...
1092 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1094 m_surface
= cairo_image_surface_create_for_data(
1095 m_buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1096 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1097 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1099 wxAlphaPixelData::Iterator
p(pixData
);
1100 for (int y
=0; y
<bh
; y
++)
1102 wxAlphaPixelData::Iterator rowStart
= p
;
1103 for (int x
=0; x
<bw
; x
++)
1105 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1106 // with alpha in the upper 8 bits, then red, then green, then
1107 // blue. The 32-bit quantities are stored native-endian.
1108 // Pre-multiplied alpha is used.
1109 unsigned char alpha
= p
.Alpha();
1113 *data
= ( alpha
<< 24
1114 | (p
.Red() * alpha
/255) << 16
1115 | (p
.Green() * alpha
/255) << 8
1116 | (p
.Blue() * alpha
/255) );
1121 p
.OffsetY(pixData
, 1);
1126 m_surface
= cairo_image_surface_create_for_data(
1127 m_buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1128 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1129 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1131 wxNativePixelData::Iterator
p(pixData
);
1132 for (int y
=0; y
<bh
; y
++)
1134 wxNativePixelData::Iterator rowStart
= p
;
1135 for (int x
=0; x
<bw
; x
++)
1137 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1138 // the upper 8 bits unused. Red, Green, and Blue are stored in
1139 // the remaining 24 bits in that order. The 32-bit quantities
1140 // are stored native-endian.
1141 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1146 p
.OffsetY(pixData
, 1);
1149 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1150 #endif // wxHAS_RAW_BITMAP
1153 wxCairoBitmapData::~wxCairoBitmapData()
1156 cairo_pattern_destroy(m_pattern
);
1159 cairo_surface_destroy(m_surface
);
1166 //-----------------------------------------------------------------------------
1167 // wxCairoContext implementation
1168 //-----------------------------------------------------------------------------
1170 class wxCairoOffsetHelper
1173 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1178 cairo_translate( m_ctx
, 0.5, 0.5 );
1180 ~wxCairoOffsetHelper( )
1183 cairo_translate( m_ctx
, -0.5, -0.5 );
1190 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1191 : wxGraphicsContext(renderer
)
1194 const wxDCImpl
*impl
= dc
.GetImpl();
1195 Init( (cairo_t
*) impl
->GetCairoContext() );
1197 wxSize sz
= dc
.GetSize();
1201 wxPoint org
= dc
.GetDeviceOrigin();
1202 cairo_translate( m_context
, org
.x
, org
.y
);
1205 dc
.GetUserScale( &sx
, &sy
);
1206 cairo_scale( m_context
, sx
, sy
);
1208 org
= dc
.GetLogicalOrigin();
1209 cairo_translate( m_context
, -org
.x
, -org
.y
);
1213 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1214 : wxGraphicsContext(renderer
)
1217 dc
.GetSize( &width
, &height
);
1221 m_enableOffset
= true;
1224 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1225 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1228 wxGraphicsMatrix matrix
= CreateMatrix();
1230 wxPoint org
= dc
.GetDeviceOrigin();
1231 matrix
.Translate( org
.x
, org
.y
);
1233 org
= dc
.GetLogicalOrigin();
1234 matrix
.Translate( -org
.x
, -org
.y
);
1237 dc
.GetUserScale( &sx
, &sy
);
1238 matrix
.Scale( sx
, sy
);
1240 ConcatTransform( matrix
);
1245 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1246 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1247 Init( cairo_create( surface
) );
1248 cairo_surface_destroy( surface
);
1252 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1253 : wxGraphicsContext(renderer
)
1256 dc
.GetSize( &width
, &height
);
1260 m_enableOffset
= true;
1263 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1264 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1267 wxGraphicsMatrix matrix
= CreateMatrix();
1269 wxPoint org
= dc
.GetDeviceOrigin();
1270 matrix
.Translate( org
.x
, org
.y
);
1272 org
= dc
.GetLogicalOrigin();
1273 matrix
.Translate( -org
.x
, -org
.y
);
1276 dc
.GetUserScale( &sx
, &sy
);
1277 matrix
.Scale( sx
, sy
);
1279 ConcatTransform( matrix
);
1284 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1285 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1286 Init( cairo_create( surface
) );
1287 cairo_surface_destroy( surface
);
1292 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1293 : wxGraphicsContext(renderer
)
1295 Init( gdk_cairo_create( drawable
) );
1298 gdk_drawable_get_size( drawable
, &width
, &height
);
1305 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1306 : wxGraphicsContext(renderer
)
1308 m_mswSurface
= cairo_win32_surface_create(handle
);
1309 Init( cairo_create(m_mswSurface
) );
1316 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1317 : wxGraphicsContext(renderer
)
1324 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1325 : wxGraphicsContext(renderer
)
1327 m_enableOffset
= true;
1329 // something along these lines (copied from dcclient)
1331 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1332 // code should still be able to create wxClientDCs for them, so we will
1333 // use the parent window here then.
1334 if (window
->m_wxwindow
== NULL
)
1336 window
= window
->GetParent();
1339 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1341 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1343 wxSize sz
= window
->GetSize();
1349 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1350 Init(cairo_create(m_mswSurface
));
1355 wxCairoContext::~wxCairoContext()
1361 cairo_destroy(m_context
);
1365 cairo_surface_destroy(m_mswSurface
);
1369 void wxCairoContext::Init(cairo_t
*context
)
1371 m_context
= context
;
1377 void wxCairoContext::Clip( const wxRegion
& region
)
1379 // Create a path with all the rectangles in the region
1380 wxGraphicsPath path
= GetRenderer()->CreatePath();
1381 wxRegionIterator
ri(region
);
1384 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1388 // Put it in the context
1389 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1390 cairo_append_path(m_context
, cp
);
1392 // clip to that path
1393 cairo_clip(m_context
);
1394 path
.UnGetNativePath(cp
);
1397 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1399 // Create a path with this rectangle
1400 wxGraphicsPath path
= GetRenderer()->CreatePath();
1401 path
.AddRectangle(x
,y
,w
,h
);
1403 // Put it in the context
1404 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1405 cairo_append_path(m_context
, cp
);
1407 // clip to that path
1408 cairo_clip(m_context
);
1409 path
.UnGetNativePath(cp
);
1412 void wxCairoContext::ResetClip()
1414 cairo_reset_clip(m_context
);
1418 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1420 if ( !m_pen
.IsNull() )
1422 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1423 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1424 cairo_append_path(m_context
,cp
);
1425 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1426 cairo_stroke(m_context
);
1427 path
.UnGetNativePath(cp
);
1431 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1433 if ( !m_brush
.IsNull() )
1435 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1436 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1437 cairo_append_path(m_context
,cp
);
1438 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1439 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1440 cairo_fill(m_context
);
1441 path
.UnGetNativePath(cp
);
1445 void wxCairoContext::Rotate( wxDouble angle
)
1447 cairo_rotate(m_context
,angle
);
1450 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1452 cairo_translate(m_context
,dx
,dy
);
1455 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1457 cairo_scale(m_context
,xScale
,yScale
);
1460 // concatenates this transform with the current transform of this context
1461 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1463 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1466 // sets the transform of this context
1467 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1469 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1472 // gets the matrix of this context
1473 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1475 wxGraphicsMatrix matrix
= CreateMatrix();
1476 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1482 void wxCairoContext::PushState()
1484 cairo_save(m_context
);
1487 void wxCairoContext::PopState()
1489 cairo_restore(m_context
);
1492 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1494 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1495 DrawBitmap(bitmap
, x
, y
, w
, h
);
1499 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1503 // In case we're scaling the image by using a width and height different
1504 // than the bitmap's size create a pattern transformation on the surface and
1505 // draw the transformed pattern.
1506 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1507 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1508 wxSize size
= data
->GetSize();
1510 wxDouble scaleX
= w
/ size
.GetWidth();
1511 wxDouble scaleY
= h
/ size
.GetHeight();
1513 // prepare to draw the image
1514 cairo_translate(m_context
, x
, y
);
1515 cairo_scale(m_context
, scaleX
, scaleY
);
1516 cairo_set_source(m_context
, pattern
);
1517 // use the original size here since the context is scaled already...
1518 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1519 // fill the rectangle using the pattern
1520 cairo_fill(m_context
);
1525 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1527 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1528 // start using the Cairo backend on other platforms then we may need to
1529 // fiddle with this...
1530 DrawBitmap(icon
, x
, y
, w
, h
);
1534 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1536 wxCHECK_RET( !m_font
.IsNull(),
1537 wxT("wxCairoContext::DrawText - no valid font set") );
1542 const wxCharBuffer data
= str
.utf8_str();
1546 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1549 size_t datalen
= strlen(data
);
1551 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1552 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1553 pango_layout_set_font_description( layout
, font_data
->GetFont());
1554 pango_layout_set_text(layout
, data
, datalen
);
1556 if (font_data
->GetUnderlined())
1558 PangoAttrList
*attrs
= pango_attr_list_new();
1559 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1560 pango_attr_list_insert(attrs
, attr
);
1561 pango_layout_set_attributes(layout
, attrs
);
1562 pango_attr_list_unref(attrs
);
1565 cairo_move_to(m_context
, x
, y
);
1566 pango_cairo_show_layout (m_context
, layout
);
1568 g_object_unref (layout
);
1570 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1571 // the position we move to by the ascent.
1572 cairo_font_extents_t fe
;
1573 cairo_font_extents(m_context
, &fe
);
1574 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1576 cairo_show_text(m_context
, data
);
1580 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1581 wxDouble
*descent
, wxDouble
*externalLeading
) const
1583 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1591 if ( externalLeading
)
1592 *externalLeading
= 0;
1600 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1601 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1602 const wxCharBuffer data
= str
.utf8_str();
1607 pango_layout_set_text( layout
, data
, strlen(data
) );
1608 pango_layout_get_pixel_size (layout
, &w
, &h
);
1615 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1616 int baseline
= pango_layout_iter_get_baseline(iter
);
1617 pango_layout_iter_free(iter
);
1618 *descent
= h
- PANGO_PIXELS(baseline
);
1620 g_object_unref (layout
);
1622 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1626 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1627 cairo_text_extents_t te
;
1628 cairo_text_extents(m_context
, buf
, &te
);
1632 if (height
|| descent
|| externalLeading
)
1634 cairo_font_extents_t fe
;
1635 cairo_font_extents(m_context
, &fe
);
1637 // some backends have negative descents
1639 if ( fe
.descent
< 0 )
1640 fe
.descent
= -fe
.descent
;
1642 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1644 // some backends are broken re height ... (eg currently ATSUI)
1645 fe
.height
= fe
.ascent
+ fe
.descent
;
1649 *height
= fe
.height
;
1651 *descent
= fe
.descent
;
1652 if ( externalLeading
)
1653 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1658 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1661 widths
.Add(0, text
.length());
1663 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1671 void * wxCairoContext::GetNativeContext()
1676 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1678 if (m_antialias
== antialias
)
1681 m_antialias
= antialias
;
1683 cairo_antialias_t antialiasMode
;
1686 case wxANTIALIAS_DEFAULT
:
1687 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1689 case wxANTIALIAS_NONE
:
1690 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1695 cairo_set_antialias(m_context
, antialiasMode
);
1699 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
1705 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1707 if ( m_composition
== op
)
1711 cairo_operator_t cop
;
1714 case wxCOMPOSITION_CLEAR
:
1715 cop
= CAIRO_OPERATOR_CLEAR
;
1717 case wxCOMPOSITION_SOURCE
:
1718 cop
= CAIRO_OPERATOR_SOURCE
;
1720 case wxCOMPOSITION_OVER
:
1721 cop
= CAIRO_OPERATOR_OVER
;
1723 case wxCOMPOSITION_IN
:
1724 cop
= CAIRO_OPERATOR_IN
;
1726 case wxCOMPOSITION_OUT
:
1727 cop
= CAIRO_OPERATOR_OUT
;
1729 case wxCOMPOSITION_ATOP
:
1730 cop
= CAIRO_OPERATOR_ATOP
;
1732 case wxCOMPOSITION_DEST
:
1733 cop
= CAIRO_OPERATOR_DEST
;
1735 case wxCOMPOSITION_DEST_OVER
:
1736 cop
= CAIRO_OPERATOR_DEST_OVER
;
1738 case wxCOMPOSITION_DEST_IN
:
1739 cop
= CAIRO_OPERATOR_DEST_IN
;
1741 case wxCOMPOSITION_DEST_OUT
:
1742 cop
= CAIRO_OPERATOR_DEST_OUT
;
1744 case wxCOMPOSITION_DEST_ATOP
:
1745 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1747 case wxCOMPOSITION_XOR
:
1748 cop
= CAIRO_OPERATOR_XOR
;
1750 case wxCOMPOSITION_ADD
:
1751 cop
= CAIRO_OPERATOR_ADD
;
1756 cairo_set_operator(m_context
, cop
);
1760 void wxCairoContext::BeginLayer(wxDouble opacity
)
1762 m_layerOpacities
.push_back(opacity
);
1763 cairo_push_group(m_context
);
1766 void wxCairoContext::EndLayer()
1768 float opacity
= m_layerOpacities
.back();
1769 m_layerOpacities
.pop_back();
1770 cairo_pop_group_to_source(m_context
);
1771 cairo_paint_with_alpha(m_context
,opacity
);
1774 //-----------------------------------------------------------------------------
1775 // wxCairoRenderer declaration
1776 //-----------------------------------------------------------------------------
1778 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1781 wxCairoRenderer() {}
1783 virtual ~wxCairoRenderer() {}
1787 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1788 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1789 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1791 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1793 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1795 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1797 virtual wxGraphicsContext
* CreateMeasuringContext();
1799 #if wxUSE_ENH_METAFILE
1800 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
1805 virtual wxGraphicsPath
CreatePath();
1809 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1810 wxDouble tx
=0.0, wxDouble ty
=0.0);
1813 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1815 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1817 virtual wxGraphicsBrush
1818 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1819 wxDouble x2
, wxDouble y2
,
1820 const wxGraphicsGradientStops
& stops
);
1822 virtual wxGraphicsBrush
1823 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1824 wxDouble xc
, wxDouble yc
,
1826 const wxGraphicsGradientStops
& stops
);
1829 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1831 // create a native bitmap representation
1832 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1834 // create a graphics bitmap from a native bitmap
1835 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1837 // create a subimage from a native image representation
1838 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1841 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1844 //-----------------------------------------------------------------------------
1845 // wxCairoRenderer implementation
1846 //-----------------------------------------------------------------------------
1848 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1850 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1851 // temporary hack to allow creating a cairo context on any platform
1852 extern wxGraphicsRenderer
* gCairoRenderer
;
1853 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1855 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1857 return new wxCairoContext(this,dc
);
1860 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1862 return new wxCairoContext(this,dc
);
1865 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1868 const wxDCImpl
*impl
= dc
.GetImpl();
1869 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1871 return new wxCairoContext(this,dc
);
1878 #if wxUSE_ENH_METAFILE
1879 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1886 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1889 return new wxCairoContext(this,(HDC
)context
);
1891 return new wxCairoContext(this,(cairo_t
*)context
);
1896 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1899 return new wxCairoContext(this,(GdkDrawable
*)window
);
1905 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1908 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1914 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1916 return new wxCairoContext(this, window
);
1921 wxGraphicsPath
wxCairoRenderer::CreatePath()
1923 wxGraphicsPath path
;
1924 path
.SetRefData( new wxCairoPathData(this) );
1931 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1932 wxDouble tx
, wxDouble ty
)
1936 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1937 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1942 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1944 if ( !pen
.IsOk() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1945 return wxNullGraphicsPen
;
1949 p
.SetRefData(new wxCairoPenData( this, pen
));
1954 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1956 if ( !brush
.IsOk() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1957 return wxNullGraphicsBrush
;
1961 p
.SetRefData(new wxCairoBrushData( this, brush
));
1967 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1968 wxDouble x2
, wxDouble y2
,
1969 const wxGraphicsGradientStops
& stops
)
1972 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1973 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1979 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1980 wxDouble xc
, wxDouble yc
, wxDouble r
,
1981 const wxGraphicsGradientStops
& stops
)
1984 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1985 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
1991 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1996 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2000 return wxNullGraphicsFont
;
2003 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2008 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2012 return wxNullGraphicsBitmap
;
2015 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2017 if ( bitmap
!= NULL
)
2020 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2024 return wxNullGraphicsBitmap
;
2028 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2029 wxDouble
WXUNUSED(x
),
2030 wxDouble
WXUNUSED(y
),
2031 wxDouble
WXUNUSED(w
),
2032 wxDouble
WXUNUSED(h
))
2034 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2035 return wxNullGraphicsBitmap
;
2038 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2040 return &gs_cairoGraphicsRenderer
;
2043 #else // !wxUSE_CAIRO
2045 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2050 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2052 // MSW and OS X have their own native default renderers, but the other ports
2053 // use Cairo by default
2054 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2055 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2057 return GetCairoRenderer();
2059 #endif // !(__WXMSW__ || __WXOSX__)
2061 #endif // wxUSE_GRAPHICS_CONTEXT