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
21 #include "wx/bitmap.h"
25 #include "wx/dcclient.h"
26 #include "wx/dcmemory.h"
27 #include "wx/dcprint.h"
30 #include "wx/private/graphics.h"
31 #include "wx/rawbmp.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 const double RAD2DEG
= 180.0 / M_PI
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static inline double dmin(double a
, double b
)
49 static inline double dmax(double a
, double b
)
54 static inline double DegToRad(double deg
)
56 return (deg
* M_PI
) / 180.0;
58 static inline double RadToDeg(double deg
)
60 return (deg
* 180.0) / M_PI
;
63 //-----------------------------------------------------------------------------
64 // device context implementation
66 // more and more of the dc functionality should be implemented by calling
67 // the appropricate wxCairoContext, but we will have to do that step by step
68 // also coordinate conversions should be moved to native matrix ops
69 //-----------------------------------------------------------------------------
71 // we always stock two context states, one at entry, to be able to preserve the
72 // state we were called with, the other one after changing to HI Graphics orientation
73 // (this one is used for getting back clippings etc)
75 //-----------------------------------------------------------------------------
76 // wxGraphicsPath implementation
77 //-----------------------------------------------------------------------------
79 // TODO remove this dependency (gdiplus needs the macros)
82 #define max(a,b) (((a) > (b)) ? (a) : (b))
86 #define min(a,b) (((a) < (b)) ? (a) : (b))
92 #include "wx/fontutil.h"
93 #include "wx/gtk/dc.h"
97 #include <cairo-win32.h>
101 #include "wx/osx/private.h"
102 #include <cairo-quartz.h>
103 #include <cairo-atsui.h>
106 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
109 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
112 virtual wxGraphicsObjectRefData
*Clone() const;
115 // These are the path primitives from which everything else can be constructed
118 // begins a new subpath at (x,y)
119 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
121 // adds a straight line from the current point to (x,y)
122 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
124 // adds a cubic Bezier curve from the current point, using two control points and an end point
125 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
128 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
129 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
131 // gets the last point of the current path, (0,0) if not yet set
132 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
135 virtual void AddPath( const wxGraphicsPathData
* path
);
137 // closes the current sub-path
138 virtual void CloseSubpath();
141 // These are convenience functions which - if not available natively will be assembled
142 // using the primitives from above
147 // appends a rectangle as a new closed subpath
148 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
149 // appends an ellipsis as a new closed subpath fitting the passed rectangle
150 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
152 // 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)
153 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
156 // returns the native path
157 virtual void * GetNativePath() const ;
159 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
160 virtual void UnGetNativePath(void *p
) const;
162 // transforms each point of this path by the matrix
163 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
165 // gets the bounding box enclosing all points (possibly including control points)
166 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
168 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
171 cairo_t
* m_pathContext
;
174 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
177 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
178 virtual ~wxCairoMatrixData() ;
180 virtual wxGraphicsObjectRefData
*Clone() const ;
182 // concatenates the matrix
183 virtual void Concat( const wxGraphicsMatrixData
*t
);
185 // sets the matrix to the respective values
186 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
187 wxDouble tx
=0.0, wxDouble ty
=0.0);
189 // gets the component valuess of the matrix
190 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
191 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
193 // makes this the inverse matrix
194 virtual void Invert();
196 // returns true if the elements of the transformation matrix are equal ?
197 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
199 // return true if this is the identity matrix
200 virtual bool IsIdentity() const;
206 // add the translation to this matrix
207 virtual void Translate( wxDouble dx
, wxDouble dy
);
209 // add the scale to this matrix
210 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
212 // add the rotation to this matrix (radians)
213 virtual void Rotate( wxDouble angle
);
216 // apply the transforms
219 // applies that matrix to the point
220 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
222 // applies the matrix except for translations
223 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
225 // returns the native representation
226 virtual void * GetNativeMatrix() const;
228 cairo_matrix_t m_matrix
;
231 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
234 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
239 virtual void Apply( wxGraphicsContext
* context
);
240 virtual wxDouble
GetWidth() { return m_width
; }
250 cairo_line_cap_t m_cap
;
251 cairo_line_join_t m_join
;
254 const double *m_lengths
;
255 double *m_userLengths
;
260 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
263 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
264 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
265 ~wxCairoBrushData ();
267 virtual void Apply( wxGraphicsContext
* context
);
268 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
269 const wxColour
&c1
, const wxColour
&c2
);
270 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
271 const wxColour
&oColor
, const wxColour
&cColor
);
282 cairo_pattern_t
* m_brushPattern
;
285 class wxCairoFontData
: public wxGraphicsObjectRefData
288 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
291 virtual void Apply( wxGraphicsContext
* context
);
293 const PangoFontDescription
* GetFont() const { return m_font
; }
294 bool GetUnderlined() const { return m_underlined
; }
304 cairo_font_face_t
*m_font
;
305 #elif defined(__WXGTK__)
306 PangoFontDescription
* m_font
;
308 wxCharBuffer m_fontName
;
309 cairo_font_slant_t m_slant
;
310 cairo_font_weight_t m_weight
;
314 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
317 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
318 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
319 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
321 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
323 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
324 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
326 virtual ~wxCairoContext();
328 virtual bool ShouldOffset() const
331 if ( !m_pen
.IsNull() )
333 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
337 return ( penwidth
% 2 ) == 1;
340 virtual void Clip( const wxRegion
®ion
);
342 // clips drawings to the rect
343 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
345 // resets the clipping to original extent
346 virtual void ResetClip();
348 virtual void * GetNativeContext();
350 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
352 virtual bool SetCompositionMode(wxCompositionMode op
);
354 virtual void BeginLayer(wxDouble opacity
);
356 virtual void EndLayer();
358 virtual void StrokePath( const wxGraphicsPath
& p
);
359 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
361 virtual void Translate( wxDouble dx
, wxDouble dy
);
362 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
363 virtual void Rotate( wxDouble angle
);
365 // concatenates this transform with the current transform of this context
366 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
368 // sets the transform of this context
369 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
371 // gets the matrix of this context
372 virtual wxGraphicsMatrix
GetTransform() const;
374 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
375 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
376 virtual void PushState();
377 virtual void PopState();
379 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
380 wxDouble
*descent
, wxDouble
*externalLeading
) const;
381 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
384 void Init(cairo_t
*context
);
386 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
390 wxVector
<float> m_layerOpacities
;
392 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
395 //-----------------------------------------------------------------------------
396 // wxCairoPenData implementation
397 //-----------------------------------------------------------------------------
399 wxCairoPenData::~wxCairoPenData()
401 delete[] m_userLengths
;
404 void wxCairoPenData::Init()
407 m_userLengths
= NULL
;
412 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
413 : wxGraphicsObjectRefData(renderer
)
417 m_width
= m_pen
.GetWidth();
421 m_red
= m_pen
.GetColour().Red()/255.0;
422 m_green
= m_pen
.GetColour().Green()/255.0;
423 m_blue
= m_pen
.GetColour().Blue()/255.0;
424 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
426 switch ( m_pen
.GetCap() )
429 m_cap
= CAIRO_LINE_CAP_ROUND
;
432 case wxCAP_PROJECTING
:
433 m_cap
= CAIRO_LINE_CAP_SQUARE
;
437 m_cap
= CAIRO_LINE_CAP_BUTT
;
441 m_cap
= CAIRO_LINE_CAP_BUTT
;
445 switch ( m_pen
.GetJoin() )
448 m_join
= CAIRO_LINE_JOIN_BEVEL
;
452 m_join
= CAIRO_LINE_JOIN_MITER
;
456 m_join
= CAIRO_LINE_JOIN_ROUND
;
460 m_join
= CAIRO_LINE_JOIN_MITER
;
464 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
465 const double dotted
[] =
467 dashUnit
, dashUnit
+ 2.0
469 static const double short_dashed
[] =
473 static const double dashed
[] =
477 static const double dotted_dashed
[] =
479 9.0 , 6.0 , 3.0 , 3.0
482 switch ( m_pen
.GetStyle() )
484 case wxPENSTYLE_SOLID
:
487 case wxPENSTYLE_DOT
:
488 m_count
= WXSIZEOF(dotted
);
489 m_userLengths
= new double[ m_count
] ;
490 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
491 m_lengths
= m_userLengths
;
494 case wxPENSTYLE_LONG_DASH
:
496 m_count
= WXSIZEOF(dashed
);
499 case wxPENSTYLE_SHORT_DASH
:
500 m_lengths
= short_dashed
;
501 m_count
= WXSIZEOF(short_dashed
);
504 case wxPENSTYLE_DOT_DASH
:
505 m_lengths
= dotted_dashed
;
506 m_count
= WXSIZEOF(dotted_dashed
);
509 case wxPENSTYLE_USER_DASH
:
512 m_count
= m_pen
.GetDashes( &wxdashes
) ;
513 if ((wxdashes
!= NULL
) && (m_count
> 0))
515 m_userLengths
= new double[m_count
] ;
516 for ( int i
= 0 ; i
< m_count
; ++i
)
518 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
520 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
521 m_userLengths
[i
] = dashUnit
+ 2.0 ;
522 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
523 m_userLengths
[i
] = dashUnit
;
526 m_lengths
= m_userLengths
;
529 case wxPENSTYLE_STIPPLE
:
532 wxBitmap* bmp = pen.GetStipple();
533 if ( bmp && bmp->Ok() )
535 wxDELETE( m_penImage );
536 wxDELETE( m_penBrush );
537 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
538 m_penBrush = new TextureBrush(m_penImage);
539 m_pen->SetBrush( m_penBrush );
545 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
546 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
549 wxDELETE( m_penBrush );
550 HatchStyle style = HatchStyleHorizontal;
551 switch( pen.GetStyle() )
553 case wxPENSTYLE_BDIAGONAL_HATCH :
554 style = HatchStyleBackwardDiagonal;
556 case wxPENSTYLE_CROSSDIAG_HATCH :
557 style = HatchStyleDiagonalCross;
559 case wxPENSTYLE_FDIAGONAL_HATCH :
560 style = HatchStyleForwardDiagonal;
562 case wxPENSTYLE_CROSS_HATCH :
563 style = HatchStyleCross;
565 case wxPENSTYLE_HORIZONTAL_HATCH :
566 style = HatchStyleHorizontal;
568 case wxPENSTYLE_VERTICAL_HATCH :
569 style = HatchStyleVertical;
573 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
574 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
575 m_pen->SetBrush( m_penBrush )
582 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
584 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
585 cairo_set_line_width(ctext
,m_width
);
586 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
587 cairo_set_line_cap(ctext
,m_cap
);
588 cairo_set_line_join(ctext
,m_join
);
589 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
592 //-----------------------------------------------------------------------------
593 // wxCairoBrushData implementation
594 //-----------------------------------------------------------------------------
596 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
597 : wxGraphicsObjectRefData( renderer
)
602 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
603 : wxGraphicsObjectRefData(renderer
)
607 m_red
= brush
.GetColour().Red()/255.0;
608 m_green
= brush
.GetColour().Green()/255.0;
609 m_blue
= brush
.GetColour().Blue()/255.0;
610 m_alpha
= brush
.GetColour().Alpha()/255.0;
612 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
614 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
615 brush.GetColour().Green() , brush.GetColour().Blue() ) );
617 else if ( brush.IsHatch() )
619 HatchStyle style = HatchStyleHorizontal;
620 switch( brush.GetStyle() )
622 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
623 style = HatchStyleBackwardDiagonal;
625 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
626 style = HatchStyleDiagonalCross;
628 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
629 style = HatchStyleForwardDiagonal;
631 case wxBRUSHSTYLE_CROSS_HATCH :
632 style = HatchStyleCross;
634 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
635 style = HatchStyleHorizontal;
637 case wxBRUSHSTYLE_VERTICAL_HATCH :
638 style = HatchStyleVertical;
642 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
643 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
647 wxBitmap* bmp = brush.GetStipple();
648 if ( bmp && bmp->Ok() )
650 wxDELETE( m_brushImage );
651 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
652 m_brush = new TextureBrush(m_brushImage);
658 wxCairoBrushData::~wxCairoBrushData ()
661 cairo_pattern_destroy(m_brushPattern
);
664 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
666 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
667 if ( m_brushPattern
)
669 cairo_set_source(ctext
,m_brushPattern
);
673 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
677 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
678 const wxColour
&c1
, const wxColour
&c2
)
680 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
681 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
682 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
683 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
684 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
685 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
688 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
689 const wxColour
&oColor
, const wxColour
&cColor
)
691 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
692 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
693 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
694 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
695 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
696 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
699 void wxCairoBrushData::Init()
701 m_brushPattern
= NULL
;
704 //-----------------------------------------------------------------------------
705 // wxCairoFontData implementation
706 //-----------------------------------------------------------------------------
708 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
709 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
711 m_red
= col
.Red()/255.0;
712 m_green
= col
.Green()/255.0;
713 m_blue
= col
.Blue()/255.0;
714 m_alpha
= col
.Alpha()/255.0;
715 m_size
= font
.GetPointSize();
716 m_underlined
= font
.GetUnderlined();
719 m_font
= cairo_atsui_font_face_create_for_atsu_font_id( font
.MacGetATSUFontID() );
720 #elif defined(__WXGTK__)
721 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
723 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
724 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
725 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
729 wxCairoFontData::~wxCairoFontData()
732 cairo_font_face_destroy( m_font
);
733 #elif defined(__WXGTK__)
734 pango_font_description_free( m_font
);
739 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
741 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
742 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
744 // the rest is done using Pango layouts
745 #elif defined(__WXMAC__)
746 cairo_set_font_face(ctext
, m_font
);
747 cairo_set_font_size(ctext
, m_size
);
749 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
750 cairo_set_font_size(ctext
, m_size
);
754 //-----------------------------------------------------------------------------
755 // wxCairoPathData implementation
756 //-----------------------------------------------------------------------------
758 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
759 : wxGraphicsPathData(renderer
)
763 m_pathContext
= pathcontext
;
767 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
768 m_pathContext
= cairo_create(surface
);
769 cairo_surface_destroy (surface
);
773 wxCairoPathData::~wxCairoPathData()
775 cairo_destroy(m_pathContext
);
778 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
780 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
781 cairo_t
* pathcontext
= cairo_create(surface
);
782 cairo_surface_destroy (surface
);
784 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
785 cairo_append_path(pathcontext
, path
);
786 cairo_path_destroy(path
);
787 return new wxCairoPathData( GetRenderer() ,pathcontext
);
791 void* wxCairoPathData::GetNativePath() const
793 return cairo_copy_path(m_pathContext
) ;
796 void wxCairoPathData::UnGetNativePath(void *p
) const
798 cairo_path_destroy((cairo_path_t
*)p
);
805 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
807 cairo_move_to(m_pathContext
,x
,y
);
810 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
812 cairo_line_to(m_pathContext
,x
,y
);
815 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
817 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
818 cairo_append_path(m_pathContext
, p
);
822 void wxCairoPathData::CloseSubpath()
824 cairo_close_path(m_pathContext
);
827 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
829 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
832 // gets the last point of the current path, (0,0) if not yet set
833 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
836 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
843 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
845 // as clockwise means positive in our system (y pointing downwards)
846 // TODO make this interpretation dependent of the
848 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
849 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
851 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
854 // transforms each point of this path by the matrix
855 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
857 // as we don't have a true path object, we have to apply the inverse
858 // matrix to the context
859 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
860 cairo_matrix_invert( &m
);
861 cairo_transform(m_pathContext
,&m
);
864 // gets the bounding box enclosing all points (possibly including control points)
865 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
869 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
893 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode
WXUNUSED(fillStyle
) ) const
895 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
898 //-----------------------------------------------------------------------------
899 // wxCairoMatrixData implementation
900 //-----------------------------------------------------------------------------
902 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
903 : wxGraphicsMatrixData(renderer
)
909 wxCairoMatrixData::~wxCairoMatrixData()
914 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
916 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
919 // concatenates the matrix
920 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
922 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
925 // sets the matrix to the respective values
926 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
927 wxDouble tx
, wxDouble ty
)
929 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
932 // gets the component valuess of the matrix
933 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
934 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
936 if (a
) *a
= m_matrix
.xx
;
937 if (b
) *b
= m_matrix
.yx
;
938 if (c
) *c
= m_matrix
.xy
;
939 if (d
) *d
= m_matrix
.yy
;
940 if (tx
) *tx
= m_matrix
.x0
;
941 if (ty
) *ty
= m_matrix
.y0
;
944 // makes this the inverse matrix
945 void wxCairoMatrixData::Invert()
947 cairo_matrix_invert( &m_matrix
);
950 // returns true if the elements of the transformation matrix are equal ?
951 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
953 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
955 m_matrix
.xx
== tm
->xx
&&
956 m_matrix
.yx
== tm
->yx
&&
957 m_matrix
.xy
== tm
->xy
&&
958 m_matrix
.yy
== tm
->yy
&&
959 m_matrix
.x0
== tm
->x0
&&
960 m_matrix
.y0
== tm
->y0
) ;
963 // return true if this is the identity matrix
964 bool wxCairoMatrixData::IsIdentity() const
966 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
967 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
974 // add the translation to this matrix
975 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
977 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
980 // add the scale to this matrix
981 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
983 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
986 // add the rotation to this matrix (radians)
987 void wxCairoMatrixData::Rotate( wxDouble angle
)
989 cairo_matrix_rotate( &m_matrix
, angle
) ;
993 // apply the transforms
996 // applies that matrix to the point
997 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
999 double lx
= *x
, ly
= *y
;
1000 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1005 // applies the matrix except for translations
1006 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1008 double lx
= *dx
, ly
= *dy
;
1009 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1014 // returns the native representation
1015 void * wxCairoMatrixData::GetNativeMatrix() const
1017 return (void*) &m_matrix
;
1020 //-----------------------------------------------------------------------------
1021 // wxCairoContext implementation
1022 //-----------------------------------------------------------------------------
1024 class wxCairoOffsetHelper
1027 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1032 cairo_translate( m_ctx
, 0.5, 0.5 );
1034 ~wxCairoOffsetHelper( )
1037 cairo_translate( m_ctx
, -0.5, -0.5 );
1044 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1045 : wxGraphicsContext(renderer
)
1048 const wxDCImpl
*impl
= dc
.GetImpl();
1049 Init( (cairo_t
*) impl
->GetCairoContext() );
1051 wxPoint org
= dc
.GetDeviceOrigin();
1052 cairo_translate( m_context
, org
.x
, org
.y
);
1055 dc
.GetUserScale( &sx
, &sy
);
1056 cairo_scale( m_context
, sx
, sy
);
1058 org
= dc
.GetLogicalOrigin();
1059 cairo_translate( m_context
, -org
.x
, -org
.y
);
1063 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1064 : wxGraphicsContext(renderer
)
1067 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1068 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1071 wxGraphicsMatrix matrix
= CreateMatrix();
1073 wxPoint org
= dc
.GetDeviceOrigin();
1074 matrix
.Translate( org
.x
, org
.y
);
1076 org
= dc
.GetLogicalOrigin();
1077 matrix
.Translate( -org
.x
, -org
.y
);
1080 dc
.GetUserScale( &sx
, &sy
);
1081 matrix
.Scale( sx
, sy
);
1083 ConcatTransform( matrix
);
1089 dc
.GetSize( &width
, &height
);
1090 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1091 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1092 Init( cairo_create( surface
) );
1093 cairo_surface_destroy( surface
);
1097 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1098 : wxGraphicsContext(renderer
)
1101 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1102 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1105 wxGraphicsMatrix matrix
= CreateMatrix();
1107 wxPoint org
= dc
.GetDeviceOrigin();
1108 matrix
.Translate( org
.x
, org
.y
);
1110 org
= dc
.GetLogicalOrigin();
1111 matrix
.Translate( -org
.x
, -org
.y
);
1114 dc
.GetUserScale( &sx
, &sy
);
1115 matrix
.Scale( sx
, sy
);
1117 ConcatTransform( matrix
);
1123 dc
.GetSize( &width
, &height
);
1124 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1125 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1126 Init( cairo_create( surface
) );
1127 cairo_surface_destroy( surface
);
1132 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1133 : wxGraphicsContext(renderer
)
1135 Init( gdk_cairo_create( drawable
) );
1139 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1140 : wxGraphicsContext(renderer
)
1145 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1146 : wxGraphicsContext(renderer
)
1149 // something along these lines (copied from dcclient)
1151 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1152 // code should still be able to create wxClientDCs for them, so we will
1153 // use the parent window here then.
1154 if (window
->m_wxwindow
== NULL
)
1156 window
= window
->GetParent();
1159 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1161 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1165 wxCairoContext::~wxCairoContext()
1171 cairo_destroy(m_context
);
1175 void wxCairoContext::Init(cairo_t
*context
)
1177 m_context
= context
;
1183 void wxCairoContext::Clip( const wxRegion
& region
)
1185 // Create a path with all the rectangles in the region
1186 wxGraphicsPath path
= GetRenderer()->CreatePath();
1187 wxRegionIterator
ri(region
);
1190 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1194 // Put it in the context
1195 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1196 cairo_append_path(m_context
, cp
);
1198 // clip to that path
1199 cairo_clip(m_context
);
1200 path
.UnGetNativePath(cp
);
1203 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1205 // Create a path with this rectangle
1206 wxGraphicsPath path
= GetRenderer()->CreatePath();
1207 path
.AddRectangle(x
,y
,w
,h
);
1209 // Put it in the context
1210 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1211 cairo_append_path(m_context
, cp
);
1213 // clip to that path
1214 cairo_clip(m_context
);
1215 path
.UnGetNativePath(cp
);
1218 void wxCairoContext::ResetClip()
1220 cairo_reset_clip(m_context
);
1224 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1226 if ( !m_pen
.IsNull() )
1228 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1229 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1230 cairo_append_path(m_context
,cp
);
1231 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1232 cairo_stroke(m_context
);
1233 path
.UnGetNativePath(cp
);
1237 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1239 if ( !m_brush
.IsNull() )
1241 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1242 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1243 cairo_append_path(m_context
,cp
);
1244 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1245 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1246 cairo_fill(m_context
);
1247 path
.UnGetNativePath(cp
);
1251 void wxCairoContext::Rotate( wxDouble angle
)
1253 cairo_rotate(m_context
,angle
);
1256 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1258 cairo_translate(m_context
,dx
,dy
);
1261 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1263 cairo_scale(m_context
,xScale
,yScale
);
1266 // concatenates this transform with the current transform of this context
1267 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1269 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1272 // sets the transform of this context
1273 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1275 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1278 // gets the matrix of this context
1279 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1281 wxGraphicsMatrix matrix
= CreateMatrix();
1282 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1288 void wxCairoContext::PushState()
1290 cairo_save(m_context
);
1293 void wxCairoContext::PopState()
1295 cairo_restore(m_context
);
1298 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1300 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1302 cairo_surface_t
* surface
;
1303 int bw
= bmp
.GetWidth();
1304 int bh
= bmp
.GetHeight();
1305 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1306 unsigned char* buffer
= new unsigned char[bw
*bh
*4];
1307 wxUint32
* data
= (wxUint32
*)buffer
;
1309 // Create a surface object and copy the bitmap pixel data to it. if the
1310 // image has alpha (or a mask represented as alpha) then we'll use a
1311 // different format and iterator than if it doesn't...
1312 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1314 surface
= cairo_image_surface_create_for_data(
1315 buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1316 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1317 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1319 wxAlphaPixelData::Iterator
p(pixData
);
1320 for (int y
=0; y
<bh
; y
++)
1322 wxAlphaPixelData::Iterator rowStart
= p
;
1323 for (int x
=0; x
<bw
; x
++)
1325 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1326 // with alpha in the upper 8 bits, then red, then green, then
1327 // blue. The 32-bit quantities are stored native-endian.
1328 // Pre-multiplied alpha is used.
1329 unsigned char alpha
= p
.Alpha();
1333 *data
= ( alpha
<< 24
1334 | (p
.Red() * alpha
/255) << 16
1335 | (p
.Green() * alpha
/255) << 8
1336 | (p
.Blue() * alpha
/255) );
1341 p
.OffsetY(pixData
, 1);
1346 surface
= cairo_image_surface_create_for_data(
1347 buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1348 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1349 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1351 wxNativePixelData::Iterator
p(pixData
);
1352 for (int y
=0; y
<bh
; y
++)
1354 wxNativePixelData::Iterator rowStart
= p
;
1355 for (int x
=0; x
<bw
; x
++)
1357 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1358 // the upper 8 bits unused. Red, Green, and Blue are stored in
1359 // the remaining 24 bits in that order. The 32-bit quantities
1360 // are stored native-endian.
1361 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1366 p
.OffsetY(pixData
, 1);
1373 // In case we're scaling the image by using a width and height different
1374 // than the bitmap's size create a pattern transformation on the surface and
1375 // draw the transformed pattern.
1376 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface(surface
);
1377 wxDouble scaleX
= w
/ bw
;
1378 wxDouble scaleY
= h
/ bh
;
1379 cairo_scale(m_context
, scaleX
, scaleY
);
1381 // prepare to draw the image
1382 cairo_translate(m_context
, x
, y
);
1383 cairo_set_source(m_context
, pattern
);
1384 // use the original size here since the context is scaled already...
1385 cairo_rectangle(m_context
, 0, 0, bw
, bh
);
1386 // fill the rectangle using the pattern
1387 cairo_fill(m_context
);
1390 cairo_pattern_destroy(pattern
);
1391 cairo_surface_destroy(surface
);
1396 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1398 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1399 // start using the Cairo backend on other platforms then we may need to
1400 // fiddle with this...
1401 DrawBitmap(icon
, x
, y
, w
, h
);
1405 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1407 wxCHECK_RET( !m_font
.IsNull(),
1408 wxT("wxCairoContext::DrawText - no valid font set") );
1413 const wxCharBuffer data
= str
.utf8_str();
1417 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1420 size_t datalen
= strlen(data
);
1422 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1423 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1424 pango_layout_set_font_description( layout
, font_data
->GetFont());
1425 pango_layout_set_text(layout
, data
, datalen
);
1427 if (font_data
->GetUnderlined())
1429 PangoAttrList
*attrs
= pango_attr_list_new();
1430 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1431 pango_attr_list_insert(attrs
, attr
);
1432 pango_layout_set_attributes(layout
, attrs
);
1433 pango_attr_list_unref(attrs
);
1436 cairo_move_to(m_context
, x
, y
);
1437 pango_cairo_show_layout (m_context
, layout
);
1439 g_object_unref (layout
);
1441 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1442 // the position we move to by the ascent.
1443 cairo_font_extents_t fe
;
1444 cairo_font_extents(m_context
, &fe
);
1445 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1447 cairo_show_text(m_context
, data
);
1451 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1452 wxDouble
*descent
, wxDouble
*externalLeading
) const
1454 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1462 if ( externalLeading
)
1463 *externalLeading
= 0;
1471 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1472 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1473 const wxCharBuffer data
= str
.utf8_str();
1478 pango_layout_set_text( layout
, data
, strlen(data
) );
1479 pango_layout_get_pixel_size (layout
, &w
, &h
);
1486 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1487 int baseline
= pango_layout_iter_get_baseline(iter
);
1488 pango_layout_iter_free(iter
);
1489 *descent
= h
- PANGO_PIXELS(baseline
);
1491 g_object_unref (layout
);
1493 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1497 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1498 cairo_text_extents_t te
;
1499 cairo_text_extents(m_context
, buf
, &te
);
1503 if (height
|| descent
|| externalLeading
)
1505 cairo_font_extents_t fe
;
1506 cairo_font_extents(m_context
, &fe
);
1508 // some backends have negative descents
1510 if ( fe
.descent
< 0 )
1511 fe
.descent
= -fe
.descent
;
1513 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1515 // some backends are broken re height ... (eg currently ATSUI)
1516 fe
.height
= fe
.ascent
+ fe
.descent
;
1520 *height
= fe
.height
;
1522 *descent
= fe
.descent
;
1523 if ( externalLeading
)
1524 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1529 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1532 widths
.Add(0, text
.length());
1534 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1542 void * wxCairoContext::GetNativeContext()
1547 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1549 if (m_antialias
== antialias
)
1552 m_antialias
= antialias
;
1554 cairo_antialias_t antialiasMode
;
1557 case wxANTIALIAS_DEFAULT
:
1558 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1560 case wxANTIALIAS_NONE
:
1561 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1566 cairo_set_antialias(m_context
, antialiasMode
);
1570 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1572 if ( m_composition
== op
)
1576 cairo_operator_t cop
;
1579 case wxCOMPOSITION__CLEAR
:
1580 cop
= CAIRO_OPERATOR_CLEAR
;
1582 case wxCOMPOSITION_SOURCE
:
1583 cop
= CAIRO_OPERATOR_SOURCE
;
1585 case wxCOMPOSITION_OVER
:
1586 cop
= CAIRO_OPERATOR_OVER
;
1588 case wxCOMPOSITION_IN
:
1589 cop
= CAIRO_OPERATOR_IN
;
1591 case wxCOMPOSITION_OUT
:
1592 cop
= CAIRO_OPERATOR_OUT
;
1594 case wxCOMPOSITION_ATOP
:
1595 cop
= CAIRO_OPERATOR_ATOP
;
1597 case wxCOMPOSITION_DEST
:
1598 cop
= CAIRO_OPERATOR_DEST
;
1600 case wxCOMPOSITION_DEST_OVER
:
1601 cop
= CAIRO_OPERATOR_DEST_OVER
;
1603 case wxCOMPOSITION_DEST_IN
:
1604 cop
= CAIRO_OPERATOR_DEST_IN
;
1606 case wxCOMPOSITION_DEST_OUT
:
1607 cop
= CAIRO_OPERATOR_DEST_OUT
;
1609 case wxCOMPOSITION_DEST_ATOP
:
1610 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1612 case wxCOMPOSITION_XOR
:
1613 cop
= CAIRO_OPERATOR_XOR
;
1615 case wxCOMPOSITION_ADD
:
1616 cop
= CAIRO_OPERATOR_ADD
;
1621 cairo_set_operator(m_context
, cop
);
1625 void wxCairoContext::BeginLayer(wxDouble opacity
)
1627 m_layerOpacities
.push_back(opacity
);
1628 cairo_push_group(m_context
);
1631 void wxCairoContext::EndLayer()
1633 float opacity
= m_layerOpacities
.back();
1634 m_layerOpacities
.pop_back();
1635 cairo_pop_group_to_source(m_context
);
1636 cairo_paint_with_alpha(m_context
,opacity
);
1639 //-----------------------------------------------------------------------------
1640 // wxCairoRenderer declaration
1641 //-----------------------------------------------------------------------------
1643 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1646 wxCairoRenderer() {}
1648 virtual ~wxCairoRenderer() {}
1652 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1653 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1654 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1656 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1658 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1660 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1662 virtual wxGraphicsContext
* CreateMeasuringContext();
1666 virtual wxGraphicsPath
CreatePath();
1670 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1671 wxDouble tx
=0.0, wxDouble ty
=0.0);
1674 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1676 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1678 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1679 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1680 const wxColour
&c1
, const wxColour
&c2
) ;
1682 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1683 // with radius r and color cColor
1684 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1685 const wxColour
&oColor
, const wxColour
&cColor
) ;
1688 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1690 // create a native bitmap representation
1692 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
)
1694 return wxGraphicsBitmap
;
1697 // create a subimage from a native image representation
1698 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1700 return wxGraphicsBitmap
;
1705 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1708 //-----------------------------------------------------------------------------
1709 // wxCairoRenderer implementation
1710 //-----------------------------------------------------------------------------
1712 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1714 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1715 // temporary hack to allow creating a cairo context on any platform
1716 extern wxGraphicsRenderer
* gCairoRenderer
;
1717 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1720 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1722 return &gs_cairoGraphicsRenderer
;
1726 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1728 return new wxCairoContext(this,dc
);
1731 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1733 return new wxCairoContext(this,dc
);
1736 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1739 const wxDCImpl
*impl
= dc
.GetImpl();
1740 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1742 return new wxCairoContext(this,dc
);
1748 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1750 return new wxCairoContext(this,(cairo_t
*)context
);
1754 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1757 return new wxCairoContext(this,(GdkDrawable
*)window
);
1763 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1769 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1771 return new wxCairoContext(this, window
);
1776 wxGraphicsPath
wxCairoRenderer::CreatePath()
1778 wxGraphicsPath path
;
1779 path
.SetRefData( new wxCairoPathData(this) );
1786 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1787 wxDouble tx
, wxDouble ty
)
1791 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1792 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1797 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1799 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1800 return wxNullGraphicsPen
;
1804 p
.SetRefData(new wxCairoPenData( this, pen
));
1809 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1811 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1812 return wxNullGraphicsBrush
;
1816 p
.SetRefData(new wxCairoBrushData( this, brush
));
1821 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1822 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1823 const wxColour
&c1
, const wxColour
&c2
)
1826 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1827 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1832 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1833 // with radius r and color cColor
1834 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1835 const wxColour
&oColor
, const wxColour
&cColor
)
1838 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1839 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1845 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1850 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1854 return wxNullGraphicsFont
;
1857 #endif // wxUSE_GRAPHICS_CONTEXT