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"
23 #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"
32 #include "wx/vector.h"
36 //-----------------------------------------------------------------------------
37 // device context implementation
39 // more and more of the dc functionality should be implemented by calling
40 // the appropricate wxCairoContext, but we will have to do that step by step
41 // also coordinate conversions should be moved to native matrix ops
42 //-----------------------------------------------------------------------------
44 // we always stock two context states, one at entry, to be able to preserve the
45 // state we were called with, the other one after changing to HI Graphics orientation
46 // (this one is used for getting back clippings etc)
48 //-----------------------------------------------------------------------------
49 // wxGraphicsPath implementation
50 //-----------------------------------------------------------------------------
52 // TODO remove this dependency (gdiplus needs the macros)
55 #define max(a,b) (((a) > (b)) ? (a) : (b))
59 #define min(a,b) (((a) < (b)) ? (a) : (b))
65 #include "wx/fontutil.h"
66 #include "wx/gtk/dc.h"
70 #include <cairo-win32.h>
74 #include "wx/osx/private.h"
75 #include <cairo-quartz.h>
76 #include <cairo-atsui.h>
79 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
82 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
85 virtual wxGraphicsObjectRefData
*Clone() const;
88 // These are the path primitives from which everything else can be constructed
91 // begins a new subpath at (x,y)
92 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
94 // adds a straight line from the current point to (x,y)
95 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
97 // adds a cubic Bezier curve from the current point, using two control points and an end point
98 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
101 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
102 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
104 // gets the last point of the current path, (0,0) if not yet set
105 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
108 virtual void AddPath( const wxGraphicsPathData
* path
);
110 // closes the current sub-path
111 virtual void CloseSubpath();
114 // These are convenience functions which - if not available natively will be assembled
115 // using the primitives from above
120 // appends a rectangle as a new closed subpath
121 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
122 // appends an ellipsis as a new closed subpath fitting the passed rectangle
123 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
125 // 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)
126 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
129 // returns the native path
130 virtual void * GetNativePath() const ;
132 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
133 virtual void UnGetNativePath(void *p
) const;
135 // transforms each point of this path by the matrix
136 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
138 // gets the bounding box enclosing all points (possibly including control points)
139 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
141 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
144 cairo_t
* m_pathContext
;
147 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
150 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
151 virtual ~wxCairoMatrixData() ;
153 virtual wxGraphicsObjectRefData
*Clone() const ;
155 // concatenates the matrix
156 virtual void Concat( const wxGraphicsMatrixData
*t
);
158 // sets the matrix to the respective values
159 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
160 wxDouble tx
=0.0, wxDouble ty
=0.0);
162 // gets the component valuess of the matrix
163 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
164 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
166 // makes this the inverse matrix
167 virtual void Invert();
169 // returns true if the elements of the transformation matrix are equal ?
170 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
172 // return true if this is the identity matrix
173 virtual bool IsIdentity() const;
179 // add the translation to this matrix
180 virtual void Translate( wxDouble dx
, wxDouble dy
);
182 // add the scale to this matrix
183 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
185 // add the rotation to this matrix (radians)
186 virtual void Rotate( wxDouble angle
);
189 // apply the transforms
192 // applies that matrix to the point
193 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
195 // applies the matrix except for translations
196 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
198 // returns the native representation
199 virtual void * GetNativeMatrix() const;
201 cairo_matrix_t m_matrix
;
204 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
207 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
212 virtual void Apply( wxGraphicsContext
* context
);
213 virtual wxDouble
GetWidth() { return m_width
; }
223 cairo_line_cap_t m_cap
;
224 cairo_line_join_t m_join
;
227 const double *m_lengths
;
228 double *m_userLengths
;
232 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
235 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
238 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
239 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
240 ~wxCairoBrushData ();
242 virtual void Apply( wxGraphicsContext
* context
);
243 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
244 const wxColour
&c1
, const wxColour
&c2
);
245 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
246 const wxColour
&oColor
, const wxColour
&cColor
);
257 cairo_pattern_t
* m_brushPattern
;
260 class wxCairoFontData
: public wxGraphicsObjectRefData
263 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
266 virtual void Apply( wxGraphicsContext
* context
);
268 const PangoFontDescription
* GetFont() const { return m_font
; }
269 bool GetUnderlined() const { return m_underlined
; }
279 cairo_font_face_t
*m_font
;
280 #elif defined(__WXGTK__)
281 PangoFontDescription
* m_font
;
283 wxCharBuffer m_fontName
;
284 cairo_font_slant_t m_slant
;
285 cairo_font_weight_t m_weight
;
289 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
292 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
293 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
294 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
296 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
298 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
299 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
301 virtual ~wxCairoContext();
303 virtual bool ShouldOffset() const
306 if ( !m_pen
.IsNull() )
308 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
312 return ( penwidth
% 2 ) == 1;
315 virtual void Clip( const wxRegion
®ion
);
317 // clips drawings to the rect
318 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
320 // resets the clipping to original extent
321 virtual void ResetClip();
323 virtual void * GetNativeContext();
325 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
327 virtual bool SetCompositionMode(wxCompositionMode op
);
329 virtual void BeginLayer(wxDouble opacity
);
331 virtual void EndLayer();
333 virtual void StrokePath( const wxGraphicsPath
& p
);
334 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
336 virtual void Translate( wxDouble dx
, wxDouble dy
);
337 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
338 virtual void Rotate( wxDouble angle
);
340 // concatenates this transform with the current transform of this context
341 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
343 // sets the transform of this context
344 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
346 // gets the matrix of this context
347 virtual wxGraphicsMatrix
GetTransform() const;
349 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
350 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
351 virtual void PushState();
352 virtual void PopState();
354 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
355 wxDouble
*descent
, wxDouble
*externalLeading
) const;
356 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
359 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
362 void Init(cairo_t
*context
);
366 wxVector
<float> m_layerOpacities
;
368 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
371 //-----------------------------------------------------------------------------
372 // wxCairoPenData implementation
373 //-----------------------------------------------------------------------------
375 wxCairoPenData::~wxCairoPenData()
377 delete[] m_userLengths
;
380 void wxCairoPenData::Init()
383 m_userLengths
= NULL
;
388 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
389 : wxGraphicsObjectRefData(renderer
)
393 m_width
= m_pen
.GetWidth();
397 m_red
= m_pen
.GetColour().Red()/255.0;
398 m_green
= m_pen
.GetColour().Green()/255.0;
399 m_blue
= m_pen
.GetColour().Blue()/255.0;
400 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
402 switch ( m_pen
.GetCap() )
405 m_cap
= CAIRO_LINE_CAP_ROUND
;
408 case wxCAP_PROJECTING
:
409 m_cap
= CAIRO_LINE_CAP_SQUARE
;
413 m_cap
= CAIRO_LINE_CAP_BUTT
;
417 m_cap
= CAIRO_LINE_CAP_BUTT
;
421 switch ( m_pen
.GetJoin() )
424 m_join
= CAIRO_LINE_JOIN_BEVEL
;
428 m_join
= CAIRO_LINE_JOIN_MITER
;
432 m_join
= CAIRO_LINE_JOIN_ROUND
;
436 m_join
= CAIRO_LINE_JOIN_MITER
;
440 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
441 const double dotted
[] =
443 dashUnit
, dashUnit
+ 2.0
445 static const double short_dashed
[] =
449 static const double dashed
[] =
453 static const double dotted_dashed
[] =
455 9.0 , 6.0 , 3.0 , 3.0
458 switch ( m_pen
.GetStyle() )
460 case wxPENSTYLE_SOLID
:
463 case wxPENSTYLE_DOT
:
464 m_count
= WXSIZEOF(dotted
);
465 m_userLengths
= new double[ m_count
] ;
466 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
467 m_lengths
= m_userLengths
;
470 case wxPENSTYLE_LONG_DASH
:
472 m_count
= WXSIZEOF(dashed
);
475 case wxPENSTYLE_SHORT_DASH
:
476 m_lengths
= short_dashed
;
477 m_count
= WXSIZEOF(short_dashed
);
480 case wxPENSTYLE_DOT_DASH
:
481 m_lengths
= dotted_dashed
;
482 m_count
= WXSIZEOF(dotted_dashed
);
485 case wxPENSTYLE_USER_DASH
:
488 m_count
= m_pen
.GetDashes( &wxdashes
) ;
489 if ((wxdashes
!= NULL
) && (m_count
> 0))
491 m_userLengths
= new double[m_count
] ;
492 for ( int i
= 0 ; i
< m_count
; ++i
)
494 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
496 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
497 m_userLengths
[i
] = dashUnit
+ 2.0 ;
498 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
499 m_userLengths
[i
] = dashUnit
;
502 m_lengths
= m_userLengths
;
505 case wxPENSTYLE_STIPPLE
:
508 wxBitmap* bmp = pen.GetStipple();
509 if ( bmp && bmp->Ok() )
511 wxDELETE( m_penImage );
512 wxDELETE( m_penBrush );
513 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
514 m_penBrush = new TextureBrush(m_penImage);
515 m_pen->SetBrush( m_penBrush );
521 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
522 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
525 wxDELETE( m_penBrush );
526 HatchStyle style = HatchStyleHorizontal;
527 switch( pen.GetStyle() )
529 case wxPENSTYLE_BDIAGONAL_HATCH :
530 style = HatchStyleBackwardDiagonal;
532 case wxPENSTYLE_CROSSDIAG_HATCH :
533 style = HatchStyleDiagonalCross;
535 case wxPENSTYLE_FDIAGONAL_HATCH :
536 style = HatchStyleForwardDiagonal;
538 case wxPENSTYLE_CROSS_HATCH :
539 style = HatchStyleCross;
541 case wxPENSTYLE_HORIZONTAL_HATCH :
542 style = HatchStyleHorizontal;
544 case wxPENSTYLE_VERTICAL_HATCH :
545 style = HatchStyleVertical;
549 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
550 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
551 m_pen->SetBrush( m_penBrush )
558 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
560 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
561 cairo_set_line_width(ctext
,m_width
);
562 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
563 cairo_set_line_cap(ctext
,m_cap
);
564 cairo_set_line_join(ctext
,m_join
);
565 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
568 //-----------------------------------------------------------------------------
569 // wxCairoBrushData implementation
570 //-----------------------------------------------------------------------------
572 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
573 : wxGraphicsObjectRefData( renderer
)
578 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
579 : wxGraphicsObjectRefData(renderer
)
583 m_red
= brush
.GetColour().Red()/255.0;
584 m_green
= brush
.GetColour().Green()/255.0;
585 m_blue
= brush
.GetColour().Blue()/255.0;
586 m_alpha
= brush
.GetColour().Alpha()/255.0;
588 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
590 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
591 brush.GetColour().Green() , brush.GetColour().Blue() ) );
593 else if ( brush.IsHatch() )
595 HatchStyle style = HatchStyleHorizontal;
596 switch( brush.GetStyle() )
598 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
599 style = HatchStyleBackwardDiagonal;
601 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
602 style = HatchStyleDiagonalCross;
604 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
605 style = HatchStyleForwardDiagonal;
607 case wxBRUSHSTYLE_CROSS_HATCH :
608 style = HatchStyleCross;
610 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
611 style = HatchStyleHorizontal;
613 case wxBRUSHSTYLE_VERTICAL_HATCH :
614 style = HatchStyleVertical;
618 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
619 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
623 wxBitmap* bmp = brush.GetStipple();
624 if ( bmp && bmp->Ok() )
626 wxDELETE( m_brushImage );
627 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
628 m_brush = new TextureBrush(m_brushImage);
634 wxCairoBrushData::~wxCairoBrushData ()
637 cairo_pattern_destroy(m_brushPattern
);
640 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
642 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
643 if ( m_brushPattern
)
645 cairo_set_source(ctext
,m_brushPattern
);
649 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
653 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
654 const wxColour
&c1
, const wxColour
&c2
)
656 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
657 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
658 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
659 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
660 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
661 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
664 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
665 const wxColour
&oColor
, const wxColour
&cColor
)
667 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
668 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
669 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
670 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
671 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
672 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
675 void wxCairoBrushData::Init()
677 m_brushPattern
= NULL
;
680 //-----------------------------------------------------------------------------
681 // wxCairoFontData implementation
682 //-----------------------------------------------------------------------------
684 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
685 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
687 m_red
= col
.Red()/255.0;
688 m_green
= col
.Green()/255.0;
689 m_blue
= col
.Blue()/255.0;
690 m_alpha
= col
.Alpha()/255.0;
691 m_size
= font
.GetPointSize();
692 m_underlined
= font
.GetUnderlined();
695 m_font
= cairo_atsui_font_face_create_for_atsu_font_id( font
.MacGetATSUFontID() );
696 #elif defined(__WXGTK__)
697 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
699 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
700 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
701 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
705 wxCairoFontData::~wxCairoFontData()
708 cairo_font_face_destroy( m_font
);
709 #elif defined(__WXGTK__)
710 pango_font_description_free( m_font
);
715 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
717 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
718 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
720 // the rest is done using Pango layouts
721 #elif defined(__WXMAC__)
722 cairo_set_font_face(ctext
, m_font
);
723 cairo_set_font_size(ctext
, m_size
);
725 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
726 cairo_set_font_size(ctext
, m_size
);
730 //-----------------------------------------------------------------------------
731 // wxCairoPathData implementation
732 //-----------------------------------------------------------------------------
734 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
735 : wxGraphicsPathData(renderer
)
739 m_pathContext
= pathcontext
;
743 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
744 m_pathContext
= cairo_create(surface
);
745 cairo_surface_destroy (surface
);
749 wxCairoPathData::~wxCairoPathData()
751 cairo_destroy(m_pathContext
);
754 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
756 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
757 cairo_t
* pathcontext
= cairo_create(surface
);
758 cairo_surface_destroy (surface
);
760 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
761 cairo_append_path(pathcontext
, path
);
762 cairo_path_destroy(path
);
763 return new wxCairoPathData( GetRenderer() ,pathcontext
);
767 void* wxCairoPathData::GetNativePath() const
769 return cairo_copy_path(m_pathContext
) ;
772 void wxCairoPathData::UnGetNativePath(void *p
) const
774 cairo_path_destroy((cairo_path_t
*)p
);
781 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
783 cairo_move_to(m_pathContext
,x
,y
);
786 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
788 cairo_line_to(m_pathContext
,x
,y
);
791 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
793 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
794 cairo_append_path(m_pathContext
, p
);
798 void wxCairoPathData::CloseSubpath()
800 cairo_close_path(m_pathContext
);
803 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
805 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
808 // gets the last point of the current path, (0,0) if not yet set
809 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
812 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
819 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
821 // as clockwise means positive in our system (y pointing downwards)
822 // TODO make this interpretation dependent of the
824 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
825 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
827 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
830 // transforms each point of this path by the matrix
831 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
833 // as we don't have a true path object, we have to apply the inverse
834 // matrix to the context
835 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
836 cairo_matrix_invert( &m
);
837 cairo_transform(m_pathContext
,&m
);
840 // gets the bounding box enclosing all points (possibly including control points)
841 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
845 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
869 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode
WXUNUSED(fillStyle
) ) const
871 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
874 //-----------------------------------------------------------------------------
875 // wxCairoMatrixData implementation
876 //-----------------------------------------------------------------------------
878 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
879 : wxGraphicsMatrixData(renderer
)
885 wxCairoMatrixData::~wxCairoMatrixData()
890 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
892 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
895 // concatenates the matrix
896 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
898 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
901 // sets the matrix to the respective values
902 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
903 wxDouble tx
, wxDouble ty
)
905 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
908 // gets the component valuess of the matrix
909 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
910 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
912 if (a
) *a
= m_matrix
.xx
;
913 if (b
) *b
= m_matrix
.yx
;
914 if (c
) *c
= m_matrix
.xy
;
915 if (d
) *d
= m_matrix
.yy
;
916 if (tx
) *tx
= m_matrix
.x0
;
917 if (ty
) *ty
= m_matrix
.y0
;
920 // makes this the inverse matrix
921 void wxCairoMatrixData::Invert()
923 cairo_matrix_invert( &m_matrix
);
926 // returns true if the elements of the transformation matrix are equal ?
927 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
929 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
931 m_matrix
.xx
== tm
->xx
&&
932 m_matrix
.yx
== tm
->yx
&&
933 m_matrix
.xy
== tm
->xy
&&
934 m_matrix
.yy
== tm
->yy
&&
935 m_matrix
.x0
== tm
->x0
&&
936 m_matrix
.y0
== tm
->y0
) ;
939 // return true if this is the identity matrix
940 bool wxCairoMatrixData::IsIdentity() const
942 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
943 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
950 // add the translation to this matrix
951 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
953 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
956 // add the scale to this matrix
957 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
959 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
962 // add the rotation to this matrix (radians)
963 void wxCairoMatrixData::Rotate( wxDouble angle
)
965 cairo_matrix_rotate( &m_matrix
, angle
) ;
969 // apply the transforms
972 // applies that matrix to the point
973 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
975 double lx
= *x
, ly
= *y
;
976 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
981 // applies the matrix except for translations
982 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
984 double lx
= *dx
, ly
= *dy
;
985 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
990 // returns the native representation
991 void * wxCairoMatrixData::GetNativeMatrix() const
993 return (void*) &m_matrix
;
996 //-----------------------------------------------------------------------------
997 // wxCairoContext implementation
998 //-----------------------------------------------------------------------------
1000 class wxCairoOffsetHelper
1003 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1008 cairo_translate( m_ctx
, 0.5, 0.5 );
1010 ~wxCairoOffsetHelper( )
1013 cairo_translate( m_ctx
, -0.5, -0.5 );
1020 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1021 : wxGraphicsContext(renderer
)
1024 const wxDCImpl
*impl
= dc
.GetImpl();
1025 Init( (cairo_t
*) impl
->GetCairoContext() );
1027 wxPoint org
= dc
.GetDeviceOrigin();
1028 cairo_translate( m_context
, org
.x
, org
.y
);
1031 dc
.GetUserScale( &sx
, &sy
);
1032 cairo_scale( m_context
, sx
, sy
);
1034 org
= dc
.GetLogicalOrigin();
1035 cairo_translate( m_context
, -org
.x
, -org
.y
);
1039 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1040 : wxGraphicsContext(renderer
)
1043 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1044 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1047 wxGraphicsMatrix matrix
= CreateMatrix();
1049 wxPoint org
= dc
.GetDeviceOrigin();
1050 matrix
.Translate( org
.x
, org
.y
);
1052 org
= dc
.GetLogicalOrigin();
1053 matrix
.Translate( -org
.x
, -org
.y
);
1056 dc
.GetUserScale( &sx
, &sy
);
1057 matrix
.Scale( sx
, sy
);
1059 ConcatTransform( matrix
);
1065 dc
.GetSize( &width
, &height
);
1066 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1067 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1068 Init( cairo_create( surface
) );
1069 cairo_surface_destroy( surface
);
1073 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1074 : wxGraphicsContext(renderer
)
1077 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1078 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1081 wxGraphicsMatrix matrix
= CreateMatrix();
1083 wxPoint org
= dc
.GetDeviceOrigin();
1084 matrix
.Translate( org
.x
, org
.y
);
1086 org
= dc
.GetLogicalOrigin();
1087 matrix
.Translate( -org
.x
, -org
.y
);
1090 dc
.GetUserScale( &sx
, &sy
);
1091 matrix
.Scale( sx
, sy
);
1093 ConcatTransform( matrix
);
1099 dc
.GetSize( &width
, &height
);
1100 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1101 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1102 Init( cairo_create( surface
) );
1103 cairo_surface_destroy( surface
);
1108 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1109 : wxGraphicsContext(renderer
)
1111 Init( gdk_cairo_create( drawable
) );
1115 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1116 : wxGraphicsContext(renderer
)
1121 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1122 : wxGraphicsContext(renderer
)
1125 // something along these lines (copied from dcclient)
1127 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1128 // code should still be able to create wxClientDCs for them, so we will
1129 // use the parent window here then.
1130 if (window
->m_wxwindow
== NULL
)
1132 window
= window
->GetParent();
1135 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1137 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1141 wxCairoContext::~wxCairoContext()
1147 cairo_destroy(m_context
);
1151 void wxCairoContext::Init(cairo_t
*context
)
1153 m_context
= context
;
1159 void wxCairoContext::Clip( const wxRegion
& region
)
1161 // Create a path with all the rectangles in the region
1162 wxGraphicsPath path
= GetRenderer()->CreatePath();
1163 wxRegionIterator
ri(region
);
1166 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1170 // Put it in the context
1171 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1172 cairo_append_path(m_context
, cp
);
1174 // clip to that path
1175 cairo_clip(m_context
);
1176 path
.UnGetNativePath(cp
);
1179 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1181 // Create a path with this rectangle
1182 wxGraphicsPath path
= GetRenderer()->CreatePath();
1183 path
.AddRectangle(x
,y
,w
,h
);
1185 // Put it in the context
1186 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1187 cairo_append_path(m_context
, cp
);
1189 // clip to that path
1190 cairo_clip(m_context
);
1191 path
.UnGetNativePath(cp
);
1194 void wxCairoContext::ResetClip()
1196 cairo_reset_clip(m_context
);
1200 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1202 if ( !m_pen
.IsNull() )
1204 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1205 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1206 cairo_append_path(m_context
,cp
);
1207 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1208 cairo_stroke(m_context
);
1209 path
.UnGetNativePath(cp
);
1213 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1215 if ( !m_brush
.IsNull() )
1217 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1218 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1219 cairo_append_path(m_context
,cp
);
1220 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1221 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1222 cairo_fill(m_context
);
1223 path
.UnGetNativePath(cp
);
1227 void wxCairoContext::Rotate( wxDouble angle
)
1229 cairo_rotate(m_context
,angle
);
1232 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1234 cairo_translate(m_context
,dx
,dy
);
1237 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1239 cairo_scale(m_context
,xScale
,yScale
);
1242 // concatenates this transform with the current transform of this context
1243 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1245 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1248 // sets the transform of this context
1249 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1251 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1254 // gets the matrix of this context
1255 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1257 wxGraphicsMatrix matrix
= CreateMatrix();
1258 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1264 void wxCairoContext::PushState()
1266 cairo_save(m_context
);
1269 void wxCairoContext::PopState()
1271 cairo_restore(m_context
);
1274 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1276 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1278 cairo_surface_t
* surface
;
1279 int bw
= bmp
.GetWidth();
1280 int bh
= bmp
.GetHeight();
1281 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1282 unsigned char* buffer
= new unsigned char[bw
*bh
*4];
1283 wxUint32
* data
= (wxUint32
*)buffer
;
1285 // Create a surface object and copy the bitmap pixel data to it. if the
1286 // image has alpha (or a mask represented as alpha) then we'll use a
1287 // different format and iterator than if it doesn't...
1288 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1290 surface
= cairo_image_surface_create_for_data(
1291 buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1292 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1293 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1295 wxAlphaPixelData::Iterator
p(pixData
);
1296 for (int y
=0; y
<bh
; y
++)
1298 wxAlphaPixelData::Iterator rowStart
= p
;
1299 for (int x
=0; x
<bw
; x
++)
1301 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1302 // with alpha in the upper 8 bits, then red, then green, then
1303 // blue. The 32-bit quantities are stored native-endian.
1304 // Pre-multiplied alpha is used.
1305 unsigned char alpha
= p
.Alpha();
1309 *data
= ( alpha
<< 24
1310 | (p
.Red() * alpha
/255) << 16
1311 | (p
.Green() * alpha
/255) << 8
1312 | (p
.Blue() * alpha
/255) );
1317 p
.OffsetY(pixData
, 1);
1322 surface
= cairo_image_surface_create_for_data(
1323 buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1324 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1325 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1327 wxNativePixelData::Iterator
p(pixData
);
1328 for (int y
=0; y
<bh
; y
++)
1330 wxNativePixelData::Iterator rowStart
= p
;
1331 for (int x
=0; x
<bw
; x
++)
1333 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1334 // the upper 8 bits unused. Red, Green, and Blue are stored in
1335 // the remaining 24 bits in that order. The 32-bit quantities
1336 // are stored native-endian.
1337 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1342 p
.OffsetY(pixData
, 1);
1349 // In case we're scaling the image by using a width and height different
1350 // than the bitmap's size create a pattern transformation on the surface and
1351 // draw the transformed pattern.
1352 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface(surface
);
1353 wxDouble scaleX
= w
/ bw
;
1354 wxDouble scaleY
= h
/ bh
;
1355 cairo_scale(m_context
, scaleX
, scaleY
);
1357 // prepare to draw the image
1358 cairo_translate(m_context
, x
, y
);
1359 cairo_set_source(m_context
, pattern
);
1360 // use the original size here since the context is scaled already...
1361 cairo_rectangle(m_context
, 0, 0, bw
, bh
);
1362 // fill the rectangle using the pattern
1363 cairo_fill(m_context
);
1366 cairo_pattern_destroy(pattern
);
1367 cairo_surface_destroy(surface
);
1372 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1374 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1375 // start using the Cairo backend on other platforms then we may need to
1376 // fiddle with this...
1377 DrawBitmap(icon
, x
, y
, w
, h
);
1381 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1383 wxCHECK_RET( !m_font
.IsNull(),
1384 wxT("wxCairoContext::DrawText - no valid font set") );
1389 const wxCharBuffer data
= str
.utf8_str();
1393 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1396 size_t datalen
= strlen(data
);
1398 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1399 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1400 pango_layout_set_font_description( layout
, font_data
->GetFont());
1401 pango_layout_set_text(layout
, data
, datalen
);
1403 if (font_data
->GetUnderlined())
1405 PangoAttrList
*attrs
= pango_attr_list_new();
1406 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1407 pango_attr_list_insert(attrs
, attr
);
1408 pango_layout_set_attributes(layout
, attrs
);
1409 pango_attr_list_unref(attrs
);
1412 cairo_move_to(m_context
, x
, y
);
1413 pango_cairo_show_layout (m_context
, layout
);
1415 g_object_unref (layout
);
1417 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1418 // the position we move to by the ascent.
1419 cairo_font_extents_t fe
;
1420 cairo_font_extents(m_context
, &fe
);
1421 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1423 cairo_show_text(m_context
, data
);
1427 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1428 wxDouble
*descent
, wxDouble
*externalLeading
) const
1430 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1438 if ( externalLeading
)
1439 *externalLeading
= 0;
1447 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1448 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1449 const wxCharBuffer data
= str
.utf8_str();
1454 pango_layout_set_text( layout
, data
, strlen(data
) );
1455 pango_layout_get_pixel_size (layout
, &w
, &h
);
1462 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1463 int baseline
= pango_layout_iter_get_baseline(iter
);
1464 pango_layout_iter_free(iter
);
1465 *descent
= h
- PANGO_PIXELS(baseline
);
1467 g_object_unref (layout
);
1469 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1473 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1474 cairo_text_extents_t te
;
1475 cairo_text_extents(m_context
, buf
, &te
);
1479 if (height
|| descent
|| externalLeading
)
1481 cairo_font_extents_t fe
;
1482 cairo_font_extents(m_context
, &fe
);
1484 // some backends have negative descents
1486 if ( fe
.descent
< 0 )
1487 fe
.descent
= -fe
.descent
;
1489 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1491 // some backends are broken re height ... (eg currently ATSUI)
1492 fe
.height
= fe
.ascent
+ fe
.descent
;
1496 *height
= fe
.height
;
1498 *descent
= fe
.descent
;
1499 if ( externalLeading
)
1500 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1505 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1508 widths
.Add(0, text
.length());
1510 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1518 void * wxCairoContext::GetNativeContext()
1523 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1525 if (m_antialias
== antialias
)
1528 m_antialias
= antialias
;
1530 cairo_antialias_t antialiasMode
;
1533 case wxANTIALIAS_DEFAULT
:
1534 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1536 case wxANTIALIAS_NONE
:
1537 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1542 cairo_set_antialias(m_context
, antialiasMode
);
1546 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1548 if ( m_composition
== op
)
1552 cairo_operator_t cop
;
1555 case wxCOMPOSITION_CLEAR
:
1556 cop
= CAIRO_OPERATOR_CLEAR
;
1558 case wxCOMPOSITION_SOURCE
:
1559 cop
= CAIRO_OPERATOR_SOURCE
;
1561 case wxCOMPOSITION_OVER
:
1562 cop
= CAIRO_OPERATOR_OVER
;
1564 case wxCOMPOSITION_IN
:
1565 cop
= CAIRO_OPERATOR_IN
;
1567 case wxCOMPOSITION_OUT
:
1568 cop
= CAIRO_OPERATOR_OUT
;
1570 case wxCOMPOSITION_ATOP
:
1571 cop
= CAIRO_OPERATOR_ATOP
;
1573 case wxCOMPOSITION_DEST
:
1574 cop
= CAIRO_OPERATOR_DEST
;
1576 case wxCOMPOSITION_DEST_OVER
:
1577 cop
= CAIRO_OPERATOR_DEST_OVER
;
1579 case wxCOMPOSITION_DEST_IN
:
1580 cop
= CAIRO_OPERATOR_DEST_IN
;
1582 case wxCOMPOSITION_DEST_OUT
:
1583 cop
= CAIRO_OPERATOR_DEST_OUT
;
1585 case wxCOMPOSITION_DEST_ATOP
:
1586 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1588 case wxCOMPOSITION_XOR
:
1589 cop
= CAIRO_OPERATOR_XOR
;
1591 case wxCOMPOSITION_ADD
:
1592 cop
= CAIRO_OPERATOR_ADD
;
1597 cairo_set_operator(m_context
, cop
);
1601 void wxCairoContext::BeginLayer(wxDouble opacity
)
1603 m_layerOpacities
.push_back(opacity
);
1604 cairo_push_group(m_context
);
1607 void wxCairoContext::EndLayer()
1609 float opacity
= m_layerOpacities
.back();
1610 m_layerOpacities
.pop_back();
1611 cairo_pop_group_to_source(m_context
);
1612 cairo_paint_with_alpha(m_context
,opacity
);
1615 //-----------------------------------------------------------------------------
1616 // wxCairoRenderer declaration
1617 //-----------------------------------------------------------------------------
1619 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1622 wxCairoRenderer() {}
1624 virtual ~wxCairoRenderer() {}
1628 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1629 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1630 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1632 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1634 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1636 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1638 virtual wxGraphicsContext
* CreateMeasuringContext();
1642 virtual wxGraphicsPath
CreatePath();
1646 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1647 wxDouble tx
=0.0, wxDouble ty
=0.0);
1650 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1652 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1654 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1655 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1656 const wxColour
&c1
, const wxColour
&c2
) ;
1658 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1659 // with radius r and color cColor
1660 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1661 const wxColour
&oColor
, const wxColour
&cColor
) ;
1664 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1666 // create a native bitmap representation
1668 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
)
1670 return wxGraphicsBitmap
;
1673 // create a subimage from a native image representation
1674 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1676 return wxGraphicsBitmap
;
1681 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1684 //-----------------------------------------------------------------------------
1685 // wxCairoRenderer implementation
1686 //-----------------------------------------------------------------------------
1688 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1690 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1691 // temporary hack to allow creating a cairo context on any platform
1692 extern wxGraphicsRenderer
* gCairoRenderer
;
1693 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1696 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1698 return &gs_cairoGraphicsRenderer
;
1702 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1704 return new wxCairoContext(this,dc
);
1707 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1709 return new wxCairoContext(this,dc
);
1712 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1715 const wxDCImpl
*impl
= dc
.GetImpl();
1716 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1718 return new wxCairoContext(this,dc
);
1724 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1726 return new wxCairoContext(this,(cairo_t
*)context
);
1730 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1733 return new wxCairoContext(this,(GdkDrawable
*)window
);
1739 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1745 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1747 return new wxCairoContext(this, window
);
1752 wxGraphicsPath
wxCairoRenderer::CreatePath()
1754 wxGraphicsPath path
;
1755 path
.SetRefData( new wxCairoPathData(this) );
1762 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1763 wxDouble tx
, wxDouble ty
)
1767 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1768 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1773 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1775 if ( !pen
.Ok() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1776 return wxNullGraphicsPen
;
1780 p
.SetRefData(new wxCairoPenData( this, pen
));
1785 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1787 if ( !brush
.Ok() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1788 return wxNullGraphicsBrush
;
1792 p
.SetRefData(new wxCairoBrushData( this, brush
));
1797 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1798 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1799 const wxColour
&c1
, const wxColour
&c2
)
1802 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1803 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1808 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1809 // with radius r and color cColor
1810 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1811 const wxColour
&oColor
, const wxColour
&cColor
)
1814 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1815 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1821 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1826 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1830 return wxNullGraphicsFont
;
1833 #endif // wxUSE_GRAPHICS_CONTEXT