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"
34 #include "wx/private/graphics.h"
35 #include "wx/rawbmp.h"
36 #include "wx/vector.h"
40 //-----------------------------------------------------------------------------
41 // device context implementation
43 // more and more of the dc functionality should be implemented by calling
44 // the appropricate wxCairoContext, but we will have to do that step by step
45 // also coordinate conversions should be moved to native matrix ops
46 //-----------------------------------------------------------------------------
48 // we always stock two context states, one at entry, to be able to preserve the
49 // state we were called with, the other one after changing to HI Graphics orientation
50 // (this one is used for getting back clippings etc)
52 //-----------------------------------------------------------------------------
53 // wxGraphicsPath implementation
54 //-----------------------------------------------------------------------------
56 // TODO remove this dependency (gdiplus needs the macros)
59 #define max(a,b) (((a) > (b)) ? (a) : (b))
63 #define min(a,b) (((a) < (b)) ? (a) : (b))
68 #include <cairo-win32.h>
73 #include "wx/fontutil.h"
74 #include "wx/gtk/dc.h"
78 #include <cairo-win32.h>
82 #include "wx/osx/private.h"
83 #include <cairo-quartz.h>
84 #include <cairo-atsui.h>
87 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
90 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
93 virtual wxGraphicsObjectRefData
*Clone() const;
96 // These are the path primitives from which everything else can be constructed
99 // begins a new subpath at (x,y)
100 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
102 // adds a straight line from the current point to (x,y)
103 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
105 // adds a cubic Bezier curve from the current point, using two control points and an end point
106 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
109 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
110 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
112 // gets the last point of the current path, (0,0) if not yet set
113 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
116 virtual void AddPath( const wxGraphicsPathData
* path
);
118 // closes the current sub-path
119 virtual void CloseSubpath();
122 // These are convenience functions which - if not available natively will be assembled
123 // using the primitives from above
128 // appends a rectangle as a new closed subpath
129 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
130 // appends an ellipsis as a new closed subpath fitting the passed rectangle
131 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
133 // 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)
134 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
137 // returns the native path
138 virtual void * GetNativePath() const ;
140 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
141 virtual void UnGetNativePath(void *p
) const;
143 // transforms each point of this path by the matrix
144 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
146 // gets the bounding box enclosing all points (possibly including control points)
147 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
149 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
152 cairo_t
* m_pathContext
;
155 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
158 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
159 virtual ~wxCairoMatrixData() ;
161 virtual wxGraphicsObjectRefData
*Clone() const ;
163 // concatenates the matrix
164 virtual void Concat( const wxGraphicsMatrixData
*t
);
166 // sets the matrix to the respective values
167 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
168 wxDouble tx
=0.0, wxDouble ty
=0.0);
170 // gets the component valuess of the matrix
171 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
172 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
174 // makes this the inverse matrix
175 virtual void Invert();
177 // returns true if the elements of the transformation matrix are equal ?
178 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
180 // return true if this is the identity matrix
181 virtual bool IsIdentity() const;
187 // add the translation to this matrix
188 virtual void Translate( wxDouble dx
, wxDouble dy
);
190 // add the scale to this matrix
191 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
193 // add the rotation to this matrix (radians)
194 virtual void Rotate( wxDouble angle
);
197 // apply the transforms
200 // applies that matrix to the point
201 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
203 // applies the matrix except for translations
204 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
206 // returns the native representation
207 virtual void * GetNativeMatrix() const;
209 cairo_matrix_t m_matrix
;
212 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
215 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
220 virtual void Apply( wxGraphicsContext
* context
);
221 virtual wxDouble
GetWidth() { return m_width
; }
231 cairo_line_cap_t m_cap
;
232 cairo_line_join_t m_join
;
235 const double *m_lengths
;
236 double *m_userLengths
;
240 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
243 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
246 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
247 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
248 ~wxCairoBrushData ();
250 virtual void Apply( wxGraphicsContext
* context
);
252 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
253 wxDouble x2
, wxDouble y2
,
254 const wxGraphicsGradientStops
& stops
);
255 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
256 wxDouble xc
, wxDouble yc
, wxDouble radius
,
257 const wxGraphicsGradientStops
& stops
);
262 // common part of Create{Linear,Radial}GradientBrush()
263 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
271 cairo_pattern_t
* m_brushPattern
;
274 class wxCairoFontData
: public wxGraphicsObjectRefData
277 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
280 virtual void Apply( wxGraphicsContext
* context
);
282 const PangoFontDescription
* GetFont() const { return m_font
; }
283 bool GetUnderlined() const { return m_underlined
; }
293 cairo_font_face_t
*m_font
;
294 #elif defined(__WXGTK__)
295 PangoFontDescription
* m_font
;
297 wxCharBuffer m_fontName
;
298 cairo_font_slant_t m_slant
;
299 cairo_font_weight_t m_weight
;
302 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
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
);
333 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
334 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
336 virtual ~wxCairoContext();
338 virtual bool ShouldOffset() const
341 if ( !m_pen
.IsNull() )
343 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
347 return ( penwidth
% 2 ) == 1;
350 virtual void Clip( const wxRegion
®ion
);
352 cairo_surface_t
* m_mswSurface
;
355 // clips drawings to the rect
356 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
358 // resets the clipping to original extent
359 virtual void ResetClip();
361 virtual void * GetNativeContext();
363 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
365 virtual bool SetCompositionMode(wxCompositionMode op
);
367 virtual void BeginLayer(wxDouble opacity
);
369 virtual void EndLayer();
371 virtual void StrokePath( const wxGraphicsPath
& p
);
372 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
374 virtual void Translate( wxDouble dx
, wxDouble dy
);
375 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
376 virtual void Rotate( wxDouble angle
);
378 // concatenates this transform with the current transform of this context
379 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
381 // sets the transform of this context
382 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
384 // gets the matrix of this context
385 virtual wxGraphicsMatrix
GetTransform() const;
387 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
388 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
389 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
390 virtual void PushState();
391 virtual void PopState();
393 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
394 wxDouble
*descent
, wxDouble
*externalLeading
) const;
395 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
398 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
401 void Init(cairo_t
*context
);
405 wxVector
<float> m_layerOpacities
;
407 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
410 //-----------------------------------------------------------------------------
411 // wxCairoPenData implementation
412 //-----------------------------------------------------------------------------
414 wxCairoPenData::~wxCairoPenData()
416 delete[] m_userLengths
;
419 void wxCairoPenData::Init()
422 m_userLengths
= NULL
;
427 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
428 : wxGraphicsObjectRefData(renderer
)
432 m_width
= m_pen
.GetWidth();
436 m_red
= m_pen
.GetColour().Red()/255.0;
437 m_green
= m_pen
.GetColour().Green()/255.0;
438 m_blue
= m_pen
.GetColour().Blue()/255.0;
439 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
441 switch ( m_pen
.GetCap() )
444 m_cap
= CAIRO_LINE_CAP_ROUND
;
447 case wxCAP_PROJECTING
:
448 m_cap
= CAIRO_LINE_CAP_SQUARE
;
452 m_cap
= CAIRO_LINE_CAP_BUTT
;
456 m_cap
= CAIRO_LINE_CAP_BUTT
;
460 switch ( m_pen
.GetJoin() )
463 m_join
= CAIRO_LINE_JOIN_BEVEL
;
467 m_join
= CAIRO_LINE_JOIN_MITER
;
471 m_join
= CAIRO_LINE_JOIN_ROUND
;
475 m_join
= CAIRO_LINE_JOIN_MITER
;
479 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
480 const double dotted
[] =
482 dashUnit
, dashUnit
+ 2.0
484 static const double short_dashed
[] =
488 static const double dashed
[] =
492 static const double dotted_dashed
[] =
494 9.0 , 6.0 , 3.0 , 3.0
497 switch ( m_pen
.GetStyle() )
499 case wxPENSTYLE_SOLID
:
502 case wxPENSTYLE_DOT
:
503 m_count
= WXSIZEOF(dotted
);
504 m_userLengths
= new double[ m_count
] ;
505 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
506 m_lengths
= m_userLengths
;
509 case wxPENSTYLE_LONG_DASH
:
511 m_count
= WXSIZEOF(dashed
);
514 case wxPENSTYLE_SHORT_DASH
:
515 m_lengths
= short_dashed
;
516 m_count
= WXSIZEOF(short_dashed
);
519 case wxPENSTYLE_DOT_DASH
:
520 m_lengths
= dotted_dashed
;
521 m_count
= WXSIZEOF(dotted_dashed
);
524 case wxPENSTYLE_USER_DASH
:
527 m_count
= m_pen
.GetDashes( &wxdashes
) ;
528 if ((wxdashes
!= NULL
) && (m_count
> 0))
530 m_userLengths
= new double[m_count
] ;
531 for ( int i
= 0 ; i
< m_count
; ++i
)
533 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
535 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
536 m_userLengths
[i
] = dashUnit
+ 2.0 ;
537 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
538 m_userLengths
[i
] = dashUnit
;
541 m_lengths
= m_userLengths
;
544 case wxPENSTYLE_STIPPLE
:
547 wxBitmap* bmp = pen.GetStipple();
548 if ( bmp && bmp->Ok() )
550 wxDELETE( m_penImage );
551 wxDELETE( m_penBrush );
552 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
553 m_penBrush = new TextureBrush(m_penImage);
554 m_pen->SetBrush( m_penBrush );
560 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
561 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
564 wxDELETE( m_penBrush );
565 HatchStyle style = HatchStyleHorizontal;
566 switch( pen.GetStyle() )
568 case wxPENSTYLE_BDIAGONAL_HATCH :
569 style = HatchStyleBackwardDiagonal;
571 case wxPENSTYLE_CROSSDIAG_HATCH :
572 style = HatchStyleDiagonalCross;
574 case wxPENSTYLE_FDIAGONAL_HATCH :
575 style = HatchStyleForwardDiagonal;
577 case wxPENSTYLE_CROSS_HATCH :
578 style = HatchStyleCross;
580 case wxPENSTYLE_HORIZONTAL_HATCH :
581 style = HatchStyleHorizontal;
583 case wxPENSTYLE_VERTICAL_HATCH :
584 style = HatchStyleVertical;
588 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
589 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
590 m_pen->SetBrush( m_penBrush )
597 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
599 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
600 cairo_set_line_width(ctext
,m_width
);
601 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
602 cairo_set_line_cap(ctext
,m_cap
);
603 cairo_set_line_join(ctext
,m_join
);
604 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
607 //-----------------------------------------------------------------------------
608 // wxCairoBrushData implementation
609 //-----------------------------------------------------------------------------
611 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
612 : wxGraphicsObjectRefData( renderer
)
617 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
618 : wxGraphicsObjectRefData(renderer
)
622 m_red
= brush
.GetColour().Red()/255.0;
623 m_green
= brush
.GetColour().Green()/255.0;
624 m_blue
= brush
.GetColour().Blue()/255.0;
625 m_alpha
= brush
.GetColour().Alpha()/255.0;
627 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
629 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
630 brush.GetColour().Green() , brush.GetColour().Blue() ) );
632 else if ( brush.IsHatch() )
634 HatchStyle style = HatchStyleHorizontal;
635 switch( brush.GetStyle() )
637 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
638 style = HatchStyleBackwardDiagonal;
640 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
641 style = HatchStyleDiagonalCross;
643 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
644 style = HatchStyleForwardDiagonal;
646 case wxBRUSHSTYLE_CROSS_HATCH :
647 style = HatchStyleCross;
649 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
650 style = HatchStyleHorizontal;
652 case wxBRUSHSTYLE_VERTICAL_HATCH :
653 style = HatchStyleVertical;
657 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
658 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
662 wxBitmap* bmp = brush.GetStipple();
663 if ( bmp && bmp->Ok() )
665 wxDELETE( m_brushImage );
666 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
667 m_brush = new TextureBrush(m_brushImage);
673 wxCairoBrushData::~wxCairoBrushData ()
676 cairo_pattern_destroy(m_brushPattern
);
679 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
681 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
682 if ( m_brushPattern
)
684 cairo_set_source(ctext
,m_brushPattern
);
688 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
692 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
694 // loop over all the stops, they include the beginning and ending ones
695 const unsigned numStops
= stops
.GetCount();
696 for ( unsigned n
= 0; n
< numStops
; n
++ )
698 const wxGraphicsGradientStop stop
= stops
.Item(n
);
700 const wxColour col
= stop
.GetColour();
702 cairo_pattern_add_color_stop_rgba
713 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
714 wxT("Couldn't create cairo pattern"));
718 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
719 wxDouble x2
, wxDouble y2
,
720 const wxGraphicsGradientStops
& stops
)
722 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
724 AddGradientStops(stops
);
728 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
729 wxDouble xc
, wxDouble yc
,
731 const wxGraphicsGradientStops
& stops
)
733 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
735 AddGradientStops(stops
);
738 void wxCairoBrushData::Init()
740 m_brushPattern
= NULL
;
743 //-----------------------------------------------------------------------------
744 // wxCairoFontData implementation
745 //-----------------------------------------------------------------------------
747 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
748 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
750 m_red
= col
.Red()/255.0;
751 m_green
= col
.Green()/255.0;
752 m_blue
= col
.Blue()/255.0;
753 m_alpha
= col
.Alpha()/255.0;
754 m_size
= font
.GetPointSize();
755 m_underlined
= font
.GetUnderlined();
758 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
759 #elif defined(__WXGTK__)
760 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
762 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
763 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
764 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
768 wxCairoFontData::~wxCairoFontData()
771 cairo_font_face_destroy( m_font
);
772 #elif defined(__WXGTK__)
773 pango_font_description_free( m_font
);
778 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
780 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
781 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
783 // the rest is done using Pango layouts
784 #elif defined(__WXMAC__)
785 cairo_set_font_face(ctext
, m_font
);
786 cairo_set_font_size(ctext
, m_size
);
788 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
789 cairo_set_font_size(ctext
, m_size
);
793 //-----------------------------------------------------------------------------
794 // wxCairoPathData implementation
795 //-----------------------------------------------------------------------------
797 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
798 : wxGraphicsPathData(renderer
)
802 m_pathContext
= pathcontext
;
806 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
807 m_pathContext
= cairo_create(surface
);
808 cairo_surface_destroy (surface
);
812 wxCairoPathData::~wxCairoPathData()
814 cairo_destroy(m_pathContext
);
817 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
819 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
820 cairo_t
* pathcontext
= cairo_create(surface
);
821 cairo_surface_destroy (surface
);
823 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
824 cairo_append_path(pathcontext
, path
);
825 cairo_path_destroy(path
);
826 return new wxCairoPathData( GetRenderer() ,pathcontext
);
830 void* wxCairoPathData::GetNativePath() const
832 return cairo_copy_path(m_pathContext
) ;
835 void wxCairoPathData::UnGetNativePath(void *p
) const
837 cairo_path_destroy((cairo_path_t
*)p
);
844 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
846 cairo_move_to(m_pathContext
,x
,y
);
849 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
851 cairo_line_to(m_pathContext
,x
,y
);
854 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
856 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
857 cairo_append_path(m_pathContext
, p
);
861 void wxCairoPathData::CloseSubpath()
863 cairo_close_path(m_pathContext
);
866 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
868 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
871 // gets the last point of the current path, (0,0) if not yet set
872 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
875 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
882 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
884 // as clockwise means positive in our system (y pointing downwards)
885 // TODO make this interpretation dependent of the
887 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
888 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
890 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
893 // transforms each point of this path by the matrix
894 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
896 // as we don't have a true path object, we have to apply the inverse
897 // matrix to the context
898 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
899 cairo_matrix_invert( &m
);
900 cairo_transform(m_pathContext
,&m
);
903 // gets the bounding box enclosing all points (possibly including control points)
904 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
908 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
932 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
934 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
935 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
938 //-----------------------------------------------------------------------------
939 // wxCairoMatrixData implementation
940 //-----------------------------------------------------------------------------
942 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
943 : wxGraphicsMatrixData(renderer
)
949 wxCairoMatrixData::~wxCairoMatrixData()
954 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
956 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
959 // concatenates the matrix
960 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
962 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
965 // sets the matrix to the respective values
966 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
967 wxDouble tx
, wxDouble ty
)
969 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
972 // gets the component valuess of the matrix
973 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
974 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
976 if (a
) *a
= m_matrix
.xx
;
977 if (b
) *b
= m_matrix
.yx
;
978 if (c
) *c
= m_matrix
.xy
;
979 if (d
) *d
= m_matrix
.yy
;
980 if (tx
) *tx
= m_matrix
.x0
;
981 if (ty
) *ty
= m_matrix
.y0
;
984 // makes this the inverse matrix
985 void wxCairoMatrixData::Invert()
987 cairo_matrix_invert( &m_matrix
);
990 // returns true if the elements of the transformation matrix are equal ?
991 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
993 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
995 m_matrix
.xx
== tm
->xx
&&
996 m_matrix
.yx
== tm
->yx
&&
997 m_matrix
.xy
== tm
->xy
&&
998 m_matrix
.yy
== tm
->yy
&&
999 m_matrix
.x0
== tm
->x0
&&
1000 m_matrix
.y0
== tm
->y0
) ;
1003 // return true if this is the identity matrix
1004 bool wxCairoMatrixData::IsIdentity() const
1006 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1007 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1014 // add the translation to this matrix
1015 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1017 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1020 // add the scale to this matrix
1021 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1023 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1026 // add the rotation to this matrix (radians)
1027 void wxCairoMatrixData::Rotate( wxDouble angle
)
1029 cairo_matrix_rotate( &m_matrix
, angle
) ;
1033 // apply the transforms
1036 // applies that matrix to the point
1037 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1039 double lx
= *x
, ly
= *y
;
1040 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1045 // applies the matrix except for translations
1046 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1048 double lx
= *dx
, ly
= *dy
;
1049 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1054 // returns the native representation
1055 void * wxCairoMatrixData::GetNativeMatrix() const
1057 return (void*) &m_matrix
;
1060 // wxCairoBitmap implementation
1061 //-----------------------------------------------------------------------------
1063 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1064 wxGraphicsObjectRefData( renderer
)
1067 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1070 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1072 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1074 #ifdef wxHAS_RAW_BITMAP
1075 int bw
= m_width
= bmp
.GetWidth();
1076 int bh
= m_height
= bmp
.GetHeight();
1077 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1078 m_buffer
= new unsigned char[bw
*bh
*4];
1079 wxUint32
* data
= (wxUint32
*)m_buffer
;
1081 // Create a surface object and copy the bitmap pixel data to it. if the
1082 // image has alpha (or a mask represented as alpha) then we'll use a
1083 // different format and iterator than if it doesn't...
1084 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1086 m_surface
= cairo_image_surface_create_for_data(
1087 m_buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1088 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1089 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1091 wxAlphaPixelData::Iterator
p(pixData
);
1092 for (int y
=0; y
<bh
; y
++)
1094 wxAlphaPixelData::Iterator rowStart
= p
;
1095 for (int x
=0; x
<bw
; x
++)
1097 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1098 // with alpha in the upper 8 bits, then red, then green, then
1099 // blue. The 32-bit quantities are stored native-endian.
1100 // Pre-multiplied alpha is used.
1101 unsigned char alpha
= p
.Alpha();
1105 *data
= ( alpha
<< 24
1106 | (p
.Red() * alpha
/255) << 16
1107 | (p
.Green() * alpha
/255) << 8
1108 | (p
.Blue() * alpha
/255) );
1113 p
.OffsetY(pixData
, 1);
1118 m_surface
= cairo_image_surface_create_for_data(
1119 m_buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1120 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1121 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1123 wxNativePixelData::Iterator
p(pixData
);
1124 for (int y
=0; y
<bh
; y
++)
1126 wxNativePixelData::Iterator rowStart
= p
;
1127 for (int x
=0; x
<bw
; x
++)
1129 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1130 // the upper 8 bits unused. Red, Green, and Blue are stored in
1131 // the remaining 24 bits in that order. The 32-bit quantities
1132 // are stored native-endian.
1133 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1138 p
.OffsetY(pixData
, 1);
1141 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1142 #endif // wxHAS_RAW_BITMAP
1145 wxCairoBitmapData::~wxCairoBitmapData()
1148 cairo_pattern_destroy(m_pattern
);
1151 cairo_surface_destroy(m_surface
);
1158 //-----------------------------------------------------------------------------
1159 // wxCairoContext implementation
1160 //-----------------------------------------------------------------------------
1162 class wxCairoOffsetHelper
1165 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1170 cairo_translate( m_ctx
, 0.5, 0.5 );
1172 ~wxCairoOffsetHelper( )
1175 cairo_translate( m_ctx
, -0.5, -0.5 );
1182 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1183 : wxGraphicsContext(renderer
)
1186 const wxDCImpl
*impl
= dc
.GetImpl();
1187 Init( (cairo_t
*) impl
->GetCairoContext() );
1189 wxPoint org
= dc
.GetDeviceOrigin();
1190 cairo_translate( m_context
, org
.x
, org
.y
);
1193 dc
.GetUserScale( &sx
, &sy
);
1194 cairo_scale( m_context
, sx
, sy
);
1196 org
= dc
.GetLogicalOrigin();
1197 cairo_translate( m_context
, -org
.x
, -org
.y
);
1201 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1202 : wxGraphicsContext(renderer
)
1205 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1206 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1209 wxGraphicsMatrix matrix
= CreateMatrix();
1211 wxPoint org
= dc
.GetDeviceOrigin();
1212 matrix
.Translate( org
.x
, org
.y
);
1214 org
= dc
.GetLogicalOrigin();
1215 matrix
.Translate( -org
.x
, -org
.y
);
1218 dc
.GetUserScale( &sx
, &sy
);
1219 matrix
.Scale( sx
, sy
);
1221 ConcatTransform( matrix
);
1227 dc
.GetSize( &width
, &height
);
1228 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1229 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1230 Init( cairo_create( surface
) );
1231 cairo_surface_destroy( surface
);
1235 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1236 : wxGraphicsContext(renderer
)
1239 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1240 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1243 wxGraphicsMatrix matrix
= CreateMatrix();
1245 wxPoint org
= dc
.GetDeviceOrigin();
1246 matrix
.Translate( org
.x
, org
.y
);
1248 org
= dc
.GetLogicalOrigin();
1249 matrix
.Translate( -org
.x
, -org
.y
);
1252 dc
.GetUserScale( &sx
, &sy
);
1253 matrix
.Scale( sx
, sy
);
1255 ConcatTransform( matrix
);
1261 dc
.GetSize( &width
, &height
);
1262 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1263 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1264 Init( cairo_create( surface
) );
1265 cairo_surface_destroy( surface
);
1270 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1271 : wxGraphicsContext(renderer
)
1273 Init( gdk_cairo_create( drawable
) );
1278 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1279 : wxGraphicsContext(renderer
)
1281 m_mswSurface
= cairo_win32_surface_create(handle
);
1282 m_context
= cairo_create(m_mswSurface
);
1289 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1290 : wxGraphicsContext(renderer
)
1295 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1296 : wxGraphicsContext(renderer
)
1299 // something along these lines (copied from dcclient)
1301 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1302 // code should still be able to create wxClientDCs for them, so we will
1303 // use the parent window here then.
1304 if (window
->m_wxwindow
== NULL
)
1306 window
= window
->GetParent();
1309 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1311 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1315 wxCairoContext::~wxCairoContext()
1321 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1322 m_context
= cairo_create(m_mswSurface
);
1325 cairo_destroy(m_context
);
1329 cairo_surface_destroy(m_mswSurface
);
1333 void wxCairoContext::Init(cairo_t
*context
)
1335 m_context
= context
;
1341 void wxCairoContext::Clip( const wxRegion
& region
)
1343 // Create a path with all the rectangles in the region
1344 wxGraphicsPath path
= GetRenderer()->CreatePath();
1345 wxRegionIterator
ri(region
);
1348 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1352 // Put it in the context
1353 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1354 cairo_append_path(m_context
, cp
);
1356 // clip to that path
1357 cairo_clip(m_context
);
1358 path
.UnGetNativePath(cp
);
1361 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1363 // Create a path with this rectangle
1364 wxGraphicsPath path
= GetRenderer()->CreatePath();
1365 path
.AddRectangle(x
,y
,w
,h
);
1367 // Put it in the context
1368 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1369 cairo_append_path(m_context
, cp
);
1371 // clip to that path
1372 cairo_clip(m_context
);
1373 path
.UnGetNativePath(cp
);
1376 void wxCairoContext::ResetClip()
1378 cairo_reset_clip(m_context
);
1382 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1384 if ( !m_pen
.IsNull() )
1386 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1387 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1388 cairo_append_path(m_context
,cp
);
1389 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1390 cairo_stroke(m_context
);
1391 path
.UnGetNativePath(cp
);
1395 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1397 if ( !m_brush
.IsNull() )
1399 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1400 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1401 cairo_append_path(m_context
,cp
);
1402 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1403 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1404 cairo_fill(m_context
);
1405 path
.UnGetNativePath(cp
);
1409 void wxCairoContext::Rotate( wxDouble angle
)
1411 cairo_rotate(m_context
,angle
);
1414 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1416 cairo_translate(m_context
,dx
,dy
);
1419 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1421 cairo_scale(m_context
,xScale
,yScale
);
1424 // concatenates this transform with the current transform of this context
1425 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1427 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1430 // sets the transform of this context
1431 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1433 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1436 // gets the matrix of this context
1437 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1439 wxGraphicsMatrix matrix
= CreateMatrix();
1440 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1446 void wxCairoContext::PushState()
1448 cairo_save(m_context
);
1451 void wxCairoContext::PopState()
1453 cairo_restore(m_context
);
1456 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1458 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1459 DrawBitmap(bitmap
, x
, y
, w
, h
);
1463 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1467 // In case we're scaling the image by using a width and height different
1468 // than the bitmap's size create a pattern transformation on the surface and
1469 // draw the transformed pattern.
1470 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1471 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1472 wxSize size
= data
->GetSize();
1474 wxDouble scaleX
= w
/ size
.GetWidth();
1475 wxDouble scaleY
= h
/ size
.GetHeight();
1477 // prepare to draw the image
1478 cairo_translate(m_context
, x
, y
);
1479 cairo_scale(m_context
, scaleX
, scaleY
);
1480 cairo_set_source(m_context
, pattern
);
1481 // use the original size here since the context is scaled already...
1482 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1483 // fill the rectangle using the pattern
1484 cairo_fill(m_context
);
1489 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1491 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1492 // start using the Cairo backend on other platforms then we may need to
1493 // fiddle with this...
1494 DrawBitmap(icon
, x
, y
, w
, h
);
1498 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1500 wxCHECK_RET( !m_font
.IsNull(),
1501 wxT("wxCairoContext::DrawText - no valid font set") );
1506 const wxCharBuffer data
= str
.utf8_str();
1510 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1513 size_t datalen
= strlen(data
);
1515 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1516 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1517 pango_layout_set_font_description( layout
, font_data
->GetFont());
1518 pango_layout_set_text(layout
, data
, datalen
);
1520 if (font_data
->GetUnderlined())
1522 PangoAttrList
*attrs
= pango_attr_list_new();
1523 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1524 pango_attr_list_insert(attrs
, attr
);
1525 pango_layout_set_attributes(layout
, attrs
);
1526 pango_attr_list_unref(attrs
);
1529 cairo_move_to(m_context
, x
, y
);
1530 pango_cairo_show_layout (m_context
, layout
);
1532 g_object_unref (layout
);
1534 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1535 // the position we move to by the ascent.
1536 cairo_font_extents_t fe
;
1537 cairo_font_extents(m_context
, &fe
);
1538 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1540 cairo_show_text(m_context
, data
);
1544 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1545 wxDouble
*descent
, wxDouble
*externalLeading
) const
1547 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1555 if ( externalLeading
)
1556 *externalLeading
= 0;
1564 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1565 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1566 const wxCharBuffer data
= str
.utf8_str();
1571 pango_layout_set_text( layout
, data
, strlen(data
) );
1572 pango_layout_get_pixel_size (layout
, &w
, &h
);
1579 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1580 int baseline
= pango_layout_iter_get_baseline(iter
);
1581 pango_layout_iter_free(iter
);
1582 *descent
= h
- PANGO_PIXELS(baseline
);
1584 g_object_unref (layout
);
1586 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1590 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1591 cairo_text_extents_t te
;
1592 cairo_text_extents(m_context
, buf
, &te
);
1596 if (height
|| descent
|| externalLeading
)
1598 cairo_font_extents_t fe
;
1599 cairo_font_extents(m_context
, &fe
);
1601 // some backends have negative descents
1603 if ( fe
.descent
< 0 )
1604 fe
.descent
= -fe
.descent
;
1606 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1608 // some backends are broken re height ... (eg currently ATSUI)
1609 fe
.height
= fe
.ascent
+ fe
.descent
;
1613 *height
= fe
.height
;
1615 *descent
= fe
.descent
;
1616 if ( externalLeading
)
1617 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1622 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1625 widths
.Add(0, text
.length());
1627 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1635 void * wxCairoContext::GetNativeContext()
1640 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1642 if (m_antialias
== antialias
)
1645 m_antialias
= antialias
;
1647 cairo_antialias_t antialiasMode
;
1650 case wxANTIALIAS_DEFAULT
:
1651 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1653 case wxANTIALIAS_NONE
:
1654 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1659 cairo_set_antialias(m_context
, antialiasMode
);
1663 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1665 if ( m_composition
== op
)
1669 cairo_operator_t cop
;
1672 case wxCOMPOSITION_CLEAR
:
1673 cop
= CAIRO_OPERATOR_CLEAR
;
1675 case wxCOMPOSITION_SOURCE
:
1676 cop
= CAIRO_OPERATOR_SOURCE
;
1678 case wxCOMPOSITION_OVER
:
1679 cop
= CAIRO_OPERATOR_OVER
;
1681 case wxCOMPOSITION_IN
:
1682 cop
= CAIRO_OPERATOR_IN
;
1684 case wxCOMPOSITION_OUT
:
1685 cop
= CAIRO_OPERATOR_OUT
;
1687 case wxCOMPOSITION_ATOP
:
1688 cop
= CAIRO_OPERATOR_ATOP
;
1690 case wxCOMPOSITION_DEST
:
1691 cop
= CAIRO_OPERATOR_DEST
;
1693 case wxCOMPOSITION_DEST_OVER
:
1694 cop
= CAIRO_OPERATOR_DEST_OVER
;
1696 case wxCOMPOSITION_DEST_IN
:
1697 cop
= CAIRO_OPERATOR_DEST_IN
;
1699 case wxCOMPOSITION_DEST_OUT
:
1700 cop
= CAIRO_OPERATOR_DEST_OUT
;
1702 case wxCOMPOSITION_DEST_ATOP
:
1703 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1705 case wxCOMPOSITION_XOR
:
1706 cop
= CAIRO_OPERATOR_XOR
;
1708 case wxCOMPOSITION_ADD
:
1709 cop
= CAIRO_OPERATOR_ADD
;
1714 cairo_set_operator(m_context
, cop
);
1718 void wxCairoContext::BeginLayer(wxDouble opacity
)
1720 m_layerOpacities
.push_back(opacity
);
1721 cairo_push_group(m_context
);
1724 void wxCairoContext::EndLayer()
1726 float opacity
= m_layerOpacities
.back();
1727 m_layerOpacities
.pop_back();
1728 cairo_pop_group_to_source(m_context
);
1729 cairo_paint_with_alpha(m_context
,opacity
);
1732 //-----------------------------------------------------------------------------
1733 // wxCairoRenderer declaration
1734 //-----------------------------------------------------------------------------
1736 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1739 wxCairoRenderer() {}
1741 virtual ~wxCairoRenderer() {}
1745 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1746 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1747 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1749 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1751 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1753 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1755 virtual wxGraphicsContext
* CreateMeasuringContext();
1759 virtual wxGraphicsPath
CreatePath();
1763 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1764 wxDouble tx
=0.0, wxDouble ty
=0.0);
1767 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1769 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1771 virtual wxGraphicsBrush
1772 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1773 wxDouble x2
, wxDouble y2
,
1774 const wxGraphicsGradientStops
& stops
);
1776 virtual wxGraphicsBrush
1777 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1778 wxDouble xc
, wxDouble yc
,
1780 const wxGraphicsGradientStops
& stops
);
1783 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1785 // create a native bitmap representation
1786 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1788 // create a graphics bitmap from a native bitmap
1789 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1791 // create a subimage from a native image representation
1792 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1795 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1798 //-----------------------------------------------------------------------------
1799 // wxCairoRenderer implementation
1800 //-----------------------------------------------------------------------------
1802 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1804 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1805 // temporary hack to allow creating a cairo context on any platform
1806 extern wxGraphicsRenderer
* gCairoRenderer
;
1807 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1809 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1811 return new wxCairoContext(this,dc
);
1814 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1816 return new wxCairoContext(this,dc
);
1819 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1822 const wxDCImpl
*impl
= dc
.GetImpl();
1823 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1825 return new wxCairoContext(this,dc
);
1831 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1834 return new wxCairoContext(this,(HDC
)context
);
1836 return new wxCairoContext(this,(cairo_t
*)context
);
1841 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1844 return new wxCairoContext(this,(GdkDrawable
*)window
);
1850 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1853 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1859 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1861 return new wxCairoContext(this, window
);
1866 wxGraphicsPath
wxCairoRenderer::CreatePath()
1868 wxGraphicsPath path
;
1869 path
.SetRefData( new wxCairoPathData(this) );
1876 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1877 wxDouble tx
, wxDouble ty
)
1881 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1882 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1887 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1889 if ( !pen
.Ok() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1890 return wxNullGraphicsPen
;
1894 p
.SetRefData(new wxCairoPenData( this, pen
));
1899 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1901 if ( !brush
.Ok() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1902 return wxNullGraphicsBrush
;
1906 p
.SetRefData(new wxCairoBrushData( this, brush
));
1912 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1913 wxDouble x2
, wxDouble y2
,
1914 const wxGraphicsGradientStops
& stops
)
1917 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1918 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1924 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1925 wxDouble xc
, wxDouble yc
, wxDouble r
,
1926 const wxGraphicsGradientStops
& stops
)
1929 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1930 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
1936 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1941 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1945 return wxNullGraphicsFont
;
1948 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
1953 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
1957 return wxNullGraphicsBitmap
;
1960 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
1962 if ( bitmap
!= NULL
)
1965 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
1969 return wxNullGraphicsBitmap
;
1973 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
1974 wxDouble
WXUNUSED(x
),
1975 wxDouble
WXUNUSED(y
),
1976 wxDouble
WXUNUSED(w
),
1977 wxDouble
WXUNUSED(h
))
1979 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
1980 return wxNullGraphicsBitmap
;
1983 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
1985 return &gs_cairoGraphicsRenderer
;
1988 #else // !wxUSE_CAIRO
1990 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
1995 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
1997 // MSW and OS X have their own native default renderers, but the other ports
1998 // use Cairo by default
1999 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2000 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2002 return GetCairoRenderer();
2004 #endif // !(__WXMSW__ || __WXOSX__)
2006 #endif // wxUSE_GRAPHICS_CONTEXT