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 wxCairoBitmapData
: public wxGraphicsObjectRefData
292 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
293 ~wxCairoBitmapData();
295 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
296 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
297 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
299 cairo_surface_t
* m_surface
;
300 cairo_pattern_t
* m_pattern
;
303 unsigned char* m_buffer
;
306 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
309 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
310 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
311 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
313 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
315 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
316 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
318 virtual ~wxCairoContext();
320 virtual bool ShouldOffset() const
323 if ( !m_pen
.IsNull() )
325 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
329 return ( penwidth
% 2 ) == 1;
332 virtual void Clip( const wxRegion
®ion
);
334 // clips drawings to the rect
335 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
337 // resets the clipping to original extent
338 virtual void ResetClip();
340 virtual void * GetNativeContext();
342 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
344 virtual bool SetCompositionMode(wxCompositionMode op
);
346 virtual void BeginLayer(wxDouble opacity
);
348 virtual void EndLayer();
350 virtual void StrokePath( const wxGraphicsPath
& p
);
351 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
353 virtual void Translate( wxDouble dx
, wxDouble dy
);
354 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
355 virtual void Rotate( wxDouble angle
);
357 // concatenates this transform with the current transform of this context
358 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
360 // sets the transform of this context
361 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
363 // gets the matrix of this context
364 virtual wxGraphicsMatrix
GetTransform() const;
366 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
367 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
368 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
369 virtual void PushState();
370 virtual void PopState();
372 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
373 wxDouble
*descent
, wxDouble
*externalLeading
) const;
374 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
377 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
380 void Init(cairo_t
*context
);
384 wxVector
<float> m_layerOpacities
;
386 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
389 //-----------------------------------------------------------------------------
390 // wxCairoPenData implementation
391 //-----------------------------------------------------------------------------
393 wxCairoPenData::~wxCairoPenData()
395 delete[] m_userLengths
;
398 void wxCairoPenData::Init()
401 m_userLengths
= NULL
;
406 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
407 : wxGraphicsObjectRefData(renderer
)
411 m_width
= m_pen
.GetWidth();
415 m_red
= m_pen
.GetColour().Red()/255.0;
416 m_green
= m_pen
.GetColour().Green()/255.0;
417 m_blue
= m_pen
.GetColour().Blue()/255.0;
418 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
420 switch ( m_pen
.GetCap() )
423 m_cap
= CAIRO_LINE_CAP_ROUND
;
426 case wxCAP_PROJECTING
:
427 m_cap
= CAIRO_LINE_CAP_SQUARE
;
431 m_cap
= CAIRO_LINE_CAP_BUTT
;
435 m_cap
= CAIRO_LINE_CAP_BUTT
;
439 switch ( m_pen
.GetJoin() )
442 m_join
= CAIRO_LINE_JOIN_BEVEL
;
446 m_join
= CAIRO_LINE_JOIN_MITER
;
450 m_join
= CAIRO_LINE_JOIN_ROUND
;
454 m_join
= CAIRO_LINE_JOIN_MITER
;
458 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
459 const double dotted
[] =
461 dashUnit
, dashUnit
+ 2.0
463 static const double short_dashed
[] =
467 static const double dashed
[] =
471 static const double dotted_dashed
[] =
473 9.0 , 6.0 , 3.0 , 3.0
476 switch ( m_pen
.GetStyle() )
478 case wxPENSTYLE_SOLID
:
481 case wxPENSTYLE_DOT
:
482 m_count
= WXSIZEOF(dotted
);
483 m_userLengths
= new double[ m_count
] ;
484 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
485 m_lengths
= m_userLengths
;
488 case wxPENSTYLE_LONG_DASH
:
490 m_count
= WXSIZEOF(dashed
);
493 case wxPENSTYLE_SHORT_DASH
:
494 m_lengths
= short_dashed
;
495 m_count
= WXSIZEOF(short_dashed
);
498 case wxPENSTYLE_DOT_DASH
:
499 m_lengths
= dotted_dashed
;
500 m_count
= WXSIZEOF(dotted_dashed
);
503 case wxPENSTYLE_USER_DASH
:
506 m_count
= m_pen
.GetDashes( &wxdashes
) ;
507 if ((wxdashes
!= NULL
) && (m_count
> 0))
509 m_userLengths
= new double[m_count
] ;
510 for ( int i
= 0 ; i
< m_count
; ++i
)
512 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
514 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
515 m_userLengths
[i
] = dashUnit
+ 2.0 ;
516 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
517 m_userLengths
[i
] = dashUnit
;
520 m_lengths
= m_userLengths
;
523 case wxPENSTYLE_STIPPLE
:
526 wxBitmap* bmp = pen.GetStipple();
527 if ( bmp && bmp->Ok() )
529 wxDELETE( m_penImage );
530 wxDELETE( m_penBrush );
531 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
532 m_penBrush = new TextureBrush(m_penImage);
533 m_pen->SetBrush( m_penBrush );
539 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
540 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
543 wxDELETE( m_penBrush );
544 HatchStyle style = HatchStyleHorizontal;
545 switch( pen.GetStyle() )
547 case wxPENSTYLE_BDIAGONAL_HATCH :
548 style = HatchStyleBackwardDiagonal;
550 case wxPENSTYLE_CROSSDIAG_HATCH :
551 style = HatchStyleDiagonalCross;
553 case wxPENSTYLE_FDIAGONAL_HATCH :
554 style = HatchStyleForwardDiagonal;
556 case wxPENSTYLE_CROSS_HATCH :
557 style = HatchStyleCross;
559 case wxPENSTYLE_HORIZONTAL_HATCH :
560 style = HatchStyleHorizontal;
562 case wxPENSTYLE_VERTICAL_HATCH :
563 style = HatchStyleVertical;
567 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
568 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
569 m_pen->SetBrush( m_penBrush )
576 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
578 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
579 cairo_set_line_width(ctext
,m_width
);
580 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
581 cairo_set_line_cap(ctext
,m_cap
);
582 cairo_set_line_join(ctext
,m_join
);
583 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
586 //-----------------------------------------------------------------------------
587 // wxCairoBrushData implementation
588 //-----------------------------------------------------------------------------
590 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
591 : wxGraphicsObjectRefData( renderer
)
596 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
597 : wxGraphicsObjectRefData(renderer
)
601 m_red
= brush
.GetColour().Red()/255.0;
602 m_green
= brush
.GetColour().Green()/255.0;
603 m_blue
= brush
.GetColour().Blue()/255.0;
604 m_alpha
= brush
.GetColour().Alpha()/255.0;
606 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
608 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
609 brush.GetColour().Green() , brush.GetColour().Blue() ) );
611 else if ( brush.IsHatch() )
613 HatchStyle style = HatchStyleHorizontal;
614 switch( brush.GetStyle() )
616 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
617 style = HatchStyleBackwardDiagonal;
619 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
620 style = HatchStyleDiagonalCross;
622 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
623 style = HatchStyleForwardDiagonal;
625 case wxBRUSHSTYLE_CROSS_HATCH :
626 style = HatchStyleCross;
628 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
629 style = HatchStyleHorizontal;
631 case wxBRUSHSTYLE_VERTICAL_HATCH :
632 style = HatchStyleVertical;
636 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
637 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
641 wxBitmap* bmp = brush.GetStipple();
642 if ( bmp && bmp->Ok() )
644 wxDELETE( m_brushImage );
645 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
646 m_brush = new TextureBrush(m_brushImage);
652 wxCairoBrushData::~wxCairoBrushData ()
655 cairo_pattern_destroy(m_brushPattern
);
658 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
660 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
661 if ( m_brushPattern
)
663 cairo_set_source(ctext
,m_brushPattern
);
667 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
671 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
672 const wxColour
&c1
, const wxColour
&c2
)
674 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
675 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
676 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
677 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
678 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
679 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
682 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
683 const wxColour
&oColor
, const wxColour
&cColor
)
685 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
686 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
687 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
688 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
689 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
690 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
693 void wxCairoBrushData::Init()
695 m_brushPattern
= NULL
;
698 //-----------------------------------------------------------------------------
699 // wxCairoFontData implementation
700 //-----------------------------------------------------------------------------
702 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
703 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
705 m_red
= col
.Red()/255.0;
706 m_green
= col
.Green()/255.0;
707 m_blue
= col
.Blue()/255.0;
708 m_alpha
= col
.Alpha()/255.0;
709 m_size
= font
.GetPointSize();
710 m_underlined
= font
.GetUnderlined();
713 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
714 #elif defined(__WXGTK__)
715 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
717 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
718 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
719 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
723 wxCairoFontData::~wxCairoFontData()
726 cairo_font_face_destroy( m_font
);
727 #elif defined(__WXGTK__)
728 pango_font_description_free( m_font
);
733 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
735 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
736 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
738 // the rest is done using Pango layouts
739 #elif defined(__WXMAC__)
740 cairo_set_font_face(ctext
, m_font
);
741 cairo_set_font_size(ctext
, m_size
);
743 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
744 cairo_set_font_size(ctext
, m_size
);
748 //-----------------------------------------------------------------------------
749 // wxCairoPathData implementation
750 //-----------------------------------------------------------------------------
752 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
753 : wxGraphicsPathData(renderer
)
757 m_pathContext
= pathcontext
;
761 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
762 m_pathContext
= cairo_create(surface
);
763 cairo_surface_destroy (surface
);
767 wxCairoPathData::~wxCairoPathData()
769 cairo_destroy(m_pathContext
);
772 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
774 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
775 cairo_t
* pathcontext
= cairo_create(surface
);
776 cairo_surface_destroy (surface
);
778 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
779 cairo_append_path(pathcontext
, path
);
780 cairo_path_destroy(path
);
781 return new wxCairoPathData( GetRenderer() ,pathcontext
);
785 void* wxCairoPathData::GetNativePath() const
787 return cairo_copy_path(m_pathContext
) ;
790 void wxCairoPathData::UnGetNativePath(void *p
) const
792 cairo_path_destroy((cairo_path_t
*)p
);
799 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
801 cairo_move_to(m_pathContext
,x
,y
);
804 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
806 cairo_line_to(m_pathContext
,x
,y
);
809 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
811 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
812 cairo_append_path(m_pathContext
, p
);
816 void wxCairoPathData::CloseSubpath()
818 cairo_close_path(m_pathContext
);
821 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
823 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
826 // gets the last point of the current path, (0,0) if not yet set
827 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
830 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
837 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
839 // as clockwise means positive in our system (y pointing downwards)
840 // TODO make this interpretation dependent of the
842 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
843 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
845 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
848 // transforms each point of this path by the matrix
849 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
851 // as we don't have a true path object, we have to apply the inverse
852 // matrix to the context
853 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
854 cairo_matrix_invert( &m
);
855 cairo_transform(m_pathContext
,&m
);
858 // gets the bounding box enclosing all points (possibly including control points)
859 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
863 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
887 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode
WXUNUSED(fillStyle
) ) const
889 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
892 //-----------------------------------------------------------------------------
893 // wxCairoMatrixData implementation
894 //-----------------------------------------------------------------------------
896 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
897 : wxGraphicsMatrixData(renderer
)
903 wxCairoMatrixData::~wxCairoMatrixData()
908 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
910 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
913 // concatenates the matrix
914 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
916 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
919 // sets the matrix to the respective values
920 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
921 wxDouble tx
, wxDouble ty
)
923 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
926 // gets the component valuess of the matrix
927 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
928 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
930 if (a
) *a
= m_matrix
.xx
;
931 if (b
) *b
= m_matrix
.yx
;
932 if (c
) *c
= m_matrix
.xy
;
933 if (d
) *d
= m_matrix
.yy
;
934 if (tx
) *tx
= m_matrix
.x0
;
935 if (ty
) *ty
= m_matrix
.y0
;
938 // makes this the inverse matrix
939 void wxCairoMatrixData::Invert()
941 cairo_matrix_invert( &m_matrix
);
944 // returns true if the elements of the transformation matrix are equal ?
945 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
947 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
949 m_matrix
.xx
== tm
->xx
&&
950 m_matrix
.yx
== tm
->yx
&&
951 m_matrix
.xy
== tm
->xy
&&
952 m_matrix
.yy
== tm
->yy
&&
953 m_matrix
.x0
== tm
->x0
&&
954 m_matrix
.y0
== tm
->y0
) ;
957 // return true if this is the identity matrix
958 bool wxCairoMatrixData::IsIdentity() const
960 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
961 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
968 // add the translation to this matrix
969 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
971 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
974 // add the scale to this matrix
975 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
977 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
980 // add the rotation to this matrix (radians)
981 void wxCairoMatrixData::Rotate( wxDouble angle
)
983 cairo_matrix_rotate( &m_matrix
, angle
) ;
987 // apply the transforms
990 // applies that matrix to the point
991 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
993 double lx
= *x
, ly
= *y
;
994 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
999 // applies the matrix except for translations
1000 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1002 double lx
= *dx
, ly
= *dy
;
1003 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1008 // returns the native representation
1009 void * wxCairoMatrixData::GetNativeMatrix() const
1011 return (void*) &m_matrix
;
1014 // wxCairoBitmap implementation
1015 //-----------------------------------------------------------------------------
1017 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1019 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1021 int bw
= bmp
.GetWidth();
1022 int bh
= bmp
.GetHeight();
1023 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1024 m_buffer
= new unsigned char[bw
*bh
*4];
1025 wxUint32
* data
= (wxUint32
*)m_buffer
;
1027 // Create a surface object and copy the bitmap pixel data to it. if the
1028 // image has alpha (or a mask represented as alpha) then we'll use a
1029 // different format and iterator than if it doesn't...
1030 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1032 m_surface
= cairo_image_surface_create_for_data(
1033 m_buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1034 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1035 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1037 wxAlphaPixelData::Iterator
p(pixData
);
1038 for (int y
=0; y
<bh
; y
++)
1040 wxAlphaPixelData::Iterator rowStart
= p
;
1041 for (int x
=0; x
<bw
; x
++)
1043 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1044 // with alpha in the upper 8 bits, then red, then green, then
1045 // blue. The 32-bit quantities are stored native-endian.
1046 // Pre-multiplied alpha is used.
1047 unsigned char alpha
= p
.Alpha();
1051 *data
= ( alpha
<< 24
1052 | (p
.Red() * alpha
/255) << 16
1053 | (p
.Green() * alpha
/255) << 8
1054 | (p
.Blue() * alpha
/255) );
1059 p
.OffsetY(pixData
, 1);
1064 m_surface
= cairo_image_surface_create_for_data(
1065 m_buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1066 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1067 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1069 wxNativePixelData::Iterator
p(pixData
);
1070 for (int y
=0; y
<bh
; y
++)
1072 wxNativePixelData::Iterator rowStart
= p
;
1073 for (int x
=0; x
<bw
; x
++)
1075 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1076 // the upper 8 bits unused. Red, Green, and Blue are stored in
1077 // the remaining 24 bits in that order. The 32-bit quantities
1078 // are stored native-endian.
1079 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1084 p
.OffsetY(pixData
, 1);
1087 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1090 wxCairoBitmapData::~wxCairoBitmapData()
1093 cairo_pattern_destroy(m_pattern
);
1096 cairo_surface_destroy(m_surface
);
1103 //-----------------------------------------------------------------------------
1104 // wxCairoContext implementation
1105 //-----------------------------------------------------------------------------
1107 class wxCairoOffsetHelper
1110 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1115 cairo_translate( m_ctx
, 0.5, 0.5 );
1117 ~wxCairoOffsetHelper( )
1120 cairo_translate( m_ctx
, -0.5, -0.5 );
1127 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1128 : wxGraphicsContext(renderer
)
1131 const wxDCImpl
*impl
= dc
.GetImpl();
1132 Init( (cairo_t
*) impl
->GetCairoContext() );
1134 wxPoint org
= dc
.GetDeviceOrigin();
1135 cairo_translate( m_context
, org
.x
, org
.y
);
1138 dc
.GetUserScale( &sx
, &sy
);
1139 cairo_scale( m_context
, sx
, sy
);
1141 org
= dc
.GetLogicalOrigin();
1142 cairo_translate( m_context
, -org
.x
, -org
.y
);
1146 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1147 : wxGraphicsContext(renderer
)
1150 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1151 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1154 wxGraphicsMatrix matrix
= CreateMatrix();
1156 wxPoint org
= dc
.GetDeviceOrigin();
1157 matrix
.Translate( org
.x
, org
.y
);
1159 org
= dc
.GetLogicalOrigin();
1160 matrix
.Translate( -org
.x
, -org
.y
);
1163 dc
.GetUserScale( &sx
, &sy
);
1164 matrix
.Scale( sx
, sy
);
1166 ConcatTransform( matrix
);
1172 dc
.GetSize( &width
, &height
);
1173 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1174 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1175 Init( cairo_create( surface
) );
1176 cairo_surface_destroy( surface
);
1180 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1181 : wxGraphicsContext(renderer
)
1184 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1185 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1188 wxGraphicsMatrix matrix
= CreateMatrix();
1190 wxPoint org
= dc
.GetDeviceOrigin();
1191 matrix
.Translate( org
.x
, org
.y
);
1193 org
= dc
.GetLogicalOrigin();
1194 matrix
.Translate( -org
.x
, -org
.y
);
1197 dc
.GetUserScale( &sx
, &sy
);
1198 matrix
.Scale( sx
, sy
);
1200 ConcatTransform( matrix
);
1206 dc
.GetSize( &width
, &height
);
1207 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1208 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1209 Init( cairo_create( surface
) );
1210 cairo_surface_destroy( surface
);
1215 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1216 : wxGraphicsContext(renderer
)
1218 Init( gdk_cairo_create( drawable
) );
1222 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1223 : wxGraphicsContext(renderer
)
1228 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1229 : wxGraphicsContext(renderer
)
1232 // something along these lines (copied from dcclient)
1234 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1235 // code should still be able to create wxClientDCs for them, so we will
1236 // use the parent window here then.
1237 if (window
->m_wxwindow
== NULL
)
1239 window
= window
->GetParent();
1242 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1244 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1248 wxCairoContext::~wxCairoContext()
1254 cairo_destroy(m_context
);
1258 void wxCairoContext::Init(cairo_t
*context
)
1260 m_context
= context
;
1266 void wxCairoContext::Clip( const wxRegion
& region
)
1268 // Create a path with all the rectangles in the region
1269 wxGraphicsPath path
= GetRenderer()->CreatePath();
1270 wxRegionIterator
ri(region
);
1273 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1277 // Put it in the context
1278 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1279 cairo_append_path(m_context
, cp
);
1281 // clip to that path
1282 cairo_clip(m_context
);
1283 path
.UnGetNativePath(cp
);
1286 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1288 // Create a path with this rectangle
1289 wxGraphicsPath path
= GetRenderer()->CreatePath();
1290 path
.AddRectangle(x
,y
,w
,h
);
1292 // Put it in the context
1293 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1294 cairo_append_path(m_context
, cp
);
1296 // clip to that path
1297 cairo_clip(m_context
);
1298 path
.UnGetNativePath(cp
);
1301 void wxCairoContext::ResetClip()
1303 cairo_reset_clip(m_context
);
1307 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1309 if ( !m_pen
.IsNull() )
1311 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1312 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1313 cairo_append_path(m_context
,cp
);
1314 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1315 cairo_stroke(m_context
);
1316 path
.UnGetNativePath(cp
);
1320 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1322 if ( !m_brush
.IsNull() )
1324 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1325 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1326 cairo_append_path(m_context
,cp
);
1327 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1328 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1329 cairo_fill(m_context
);
1330 path
.UnGetNativePath(cp
);
1334 void wxCairoContext::Rotate( wxDouble angle
)
1336 cairo_rotate(m_context
,angle
);
1339 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1341 cairo_translate(m_context
,dx
,dy
);
1344 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1346 cairo_scale(m_context
,xScale
,yScale
);
1349 // concatenates this transform with the current transform of this context
1350 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1352 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1355 // sets the transform of this context
1356 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1358 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1361 // gets the matrix of this context
1362 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1364 wxGraphicsMatrix matrix
= CreateMatrix();
1365 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1371 void wxCairoContext::PushState()
1373 cairo_save(m_context
);
1376 void wxCairoContext::PopState()
1378 cairo_restore(m_context
);
1381 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1383 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1384 DrawBitmap(bitmap
, x
, y
, w
, h
);
1388 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1392 // In case we're scaling the image by using a width and height different
1393 // than the bitmap's size create a pattern transformation on the surface and
1394 // draw the transformed pattern.
1395 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1396 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1397 wxSize size
= data
->GetSize();
1399 wxDouble scaleX
= w
/ size
.GetWidth();
1400 wxDouble scaleY
= h
/ size
.GetHeight();
1401 cairo_scale(m_context
, scaleX
, scaleY
);
1403 // prepare to draw the image
1404 cairo_translate(m_context
, x
, y
);
1405 cairo_set_source(m_context
, pattern
);
1406 // use the original size here since the context is scaled already...
1407 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1408 // fill the rectangle using the pattern
1409 cairo_fill(m_context
);
1414 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1416 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1417 // start using the Cairo backend on other platforms then we may need to
1418 // fiddle with this...
1419 DrawBitmap(icon
, x
, y
, w
, h
);
1423 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1425 wxCHECK_RET( !m_font
.IsNull(),
1426 wxT("wxCairoContext::DrawText - no valid font set") );
1431 const wxCharBuffer data
= str
.utf8_str();
1435 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1438 size_t datalen
= strlen(data
);
1440 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1441 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1442 pango_layout_set_font_description( layout
, font_data
->GetFont());
1443 pango_layout_set_text(layout
, data
, datalen
);
1445 if (font_data
->GetUnderlined())
1447 PangoAttrList
*attrs
= pango_attr_list_new();
1448 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1449 pango_attr_list_insert(attrs
, attr
);
1450 pango_layout_set_attributes(layout
, attrs
);
1451 pango_attr_list_unref(attrs
);
1454 cairo_move_to(m_context
, x
, y
);
1455 pango_cairo_show_layout (m_context
, layout
);
1457 g_object_unref (layout
);
1459 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1460 // the position we move to by the ascent.
1461 cairo_font_extents_t fe
;
1462 cairo_font_extents(m_context
, &fe
);
1463 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1465 cairo_show_text(m_context
, data
);
1469 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1470 wxDouble
*descent
, wxDouble
*externalLeading
) const
1472 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1480 if ( externalLeading
)
1481 *externalLeading
= 0;
1489 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1490 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1491 const wxCharBuffer data
= str
.utf8_str();
1496 pango_layout_set_text( layout
, data
, strlen(data
) );
1497 pango_layout_get_pixel_size (layout
, &w
, &h
);
1504 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1505 int baseline
= pango_layout_iter_get_baseline(iter
);
1506 pango_layout_iter_free(iter
);
1507 *descent
= h
- PANGO_PIXELS(baseline
);
1509 g_object_unref (layout
);
1511 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1515 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1516 cairo_text_extents_t te
;
1517 cairo_text_extents(m_context
, buf
, &te
);
1521 if (height
|| descent
|| externalLeading
)
1523 cairo_font_extents_t fe
;
1524 cairo_font_extents(m_context
, &fe
);
1526 // some backends have negative descents
1528 if ( fe
.descent
< 0 )
1529 fe
.descent
= -fe
.descent
;
1531 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1533 // some backends are broken re height ... (eg currently ATSUI)
1534 fe
.height
= fe
.ascent
+ fe
.descent
;
1538 *height
= fe
.height
;
1540 *descent
= fe
.descent
;
1541 if ( externalLeading
)
1542 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1547 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1550 widths
.Add(0, text
.length());
1552 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1560 void * wxCairoContext::GetNativeContext()
1565 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1567 if (m_antialias
== antialias
)
1570 m_antialias
= antialias
;
1572 cairo_antialias_t antialiasMode
;
1575 case wxANTIALIAS_DEFAULT
:
1576 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1578 case wxANTIALIAS_NONE
:
1579 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1584 cairo_set_antialias(m_context
, antialiasMode
);
1588 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1590 if ( m_composition
== op
)
1594 cairo_operator_t cop
;
1597 case wxCOMPOSITION_CLEAR
:
1598 cop
= CAIRO_OPERATOR_CLEAR
;
1600 case wxCOMPOSITION_SOURCE
:
1601 cop
= CAIRO_OPERATOR_SOURCE
;
1603 case wxCOMPOSITION_OVER
:
1604 cop
= CAIRO_OPERATOR_OVER
;
1606 case wxCOMPOSITION_IN
:
1607 cop
= CAIRO_OPERATOR_IN
;
1609 case wxCOMPOSITION_OUT
:
1610 cop
= CAIRO_OPERATOR_OUT
;
1612 case wxCOMPOSITION_ATOP
:
1613 cop
= CAIRO_OPERATOR_ATOP
;
1615 case wxCOMPOSITION_DEST
:
1616 cop
= CAIRO_OPERATOR_DEST
;
1618 case wxCOMPOSITION_DEST_OVER
:
1619 cop
= CAIRO_OPERATOR_DEST_OVER
;
1621 case wxCOMPOSITION_DEST_IN
:
1622 cop
= CAIRO_OPERATOR_DEST_IN
;
1624 case wxCOMPOSITION_DEST_OUT
:
1625 cop
= CAIRO_OPERATOR_DEST_OUT
;
1627 case wxCOMPOSITION_DEST_ATOP
:
1628 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1630 case wxCOMPOSITION_XOR
:
1631 cop
= CAIRO_OPERATOR_XOR
;
1633 case wxCOMPOSITION_ADD
:
1634 cop
= CAIRO_OPERATOR_ADD
;
1639 cairo_set_operator(m_context
, cop
);
1643 void wxCairoContext::BeginLayer(wxDouble opacity
)
1645 m_layerOpacities
.push_back(opacity
);
1646 cairo_push_group(m_context
);
1649 void wxCairoContext::EndLayer()
1651 float opacity
= m_layerOpacities
.back();
1652 m_layerOpacities
.pop_back();
1653 cairo_pop_group_to_source(m_context
);
1654 cairo_paint_with_alpha(m_context
,opacity
);
1657 //-----------------------------------------------------------------------------
1658 // wxCairoRenderer declaration
1659 //-----------------------------------------------------------------------------
1661 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1664 wxCairoRenderer() {}
1666 virtual ~wxCairoRenderer() {}
1670 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1671 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1672 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1674 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1676 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1678 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1680 virtual wxGraphicsContext
* CreateMeasuringContext();
1684 virtual wxGraphicsPath
CreatePath();
1688 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1689 wxDouble tx
=0.0, wxDouble ty
=0.0);
1692 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1694 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1696 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1697 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1698 const wxColour
&c1
, const wxColour
&c2
) ;
1700 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1701 // with radius r and color cColor
1702 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1703 const wxColour
&oColor
, const wxColour
&cColor
) ;
1706 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1708 // create a native bitmap representation
1709 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1711 // create a subimage from a native image representation
1712 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1715 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1718 //-----------------------------------------------------------------------------
1719 // wxCairoRenderer implementation
1720 //-----------------------------------------------------------------------------
1722 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1724 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1725 // temporary hack to allow creating a cairo context on any platform
1726 extern wxGraphicsRenderer
* gCairoRenderer
;
1727 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1730 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1732 return &gs_cairoGraphicsRenderer
;
1736 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1738 return new wxCairoContext(this,dc
);
1741 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1743 return new wxCairoContext(this,dc
);
1746 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1749 const wxDCImpl
*impl
= dc
.GetImpl();
1750 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1752 return new wxCairoContext(this,dc
);
1758 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1760 return new wxCairoContext(this,(cairo_t
*)context
);
1764 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1767 return new wxCairoContext(this,(GdkDrawable
*)window
);
1773 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1776 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1782 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1784 return new wxCairoContext(this, window
);
1789 wxGraphicsPath
wxCairoRenderer::CreatePath()
1791 wxGraphicsPath path
;
1792 path
.SetRefData( new wxCairoPathData(this) );
1799 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1800 wxDouble tx
, wxDouble ty
)
1804 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1805 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1810 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1812 if ( !pen
.Ok() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1813 return wxNullGraphicsPen
;
1817 p
.SetRefData(new wxCairoPenData( this, pen
));
1822 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1824 if ( !brush
.Ok() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1825 return wxNullGraphicsBrush
;
1829 p
.SetRefData(new wxCairoBrushData( this, brush
));
1834 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1835 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1836 const wxColour
&c1
, const wxColour
&c2
)
1839 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1840 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1845 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1846 // with radius r and color cColor
1847 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1848 const wxColour
&oColor
, const wxColour
&cColor
)
1851 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1852 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1858 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1863 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1867 return wxNullGraphicsFont
;
1870 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
1875 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
1879 return wxNullGraphicsBitmap
;
1883 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
1884 wxDouble
WXUNUSED(x
),
1885 wxDouble
WXUNUSED(y
),
1886 wxDouble
WXUNUSED(w
),
1887 wxDouble
WXUNUSED(h
))
1889 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
1890 return wxNullGraphicsBitmap
;
1893 #endif // wxUSE_GRAPHICS_CONTEXT