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"
19 #include "wx/graphics.h"
21 #if wxUSE_GRAPHICS_CONTEXT && wxUSE_CAIRO
24 #include "wx/bitmap.h"
26 #include "wx/dcclient.h"
27 #include "wx/dcmemory.h"
28 #include "wx/dcprint.h"
31 #include "wx/private/graphics.h"
32 #include "wx/rawbmp.h"
33 #include "wx/vector.h"
37 //-----------------------------------------------------------------------------
38 // device context implementation
40 // more and more of the dc functionality should be implemented by calling
41 // the appropricate wxCairoContext, but we will have to do that step by step
42 // also coordinate conversions should be moved to native matrix ops
43 //-----------------------------------------------------------------------------
45 // we always stock two context states, one at entry, to be able to preserve the
46 // state we were called with, the other one after changing to HI Graphics orientation
47 // (this one is used for getting back clippings etc)
49 //-----------------------------------------------------------------------------
50 // wxGraphicsPath implementation
51 //-----------------------------------------------------------------------------
53 // TODO remove this dependency (gdiplus needs the macros)
56 #define max(a,b) (((a) > (b)) ? (a) : (b))
60 #define min(a,b) (((a) < (b)) ? (a) : (b))
65 #include <cairo-win32.h>
70 #include "wx/fontutil.h"
71 #include "wx/gtk/dc.h"
75 #include <cairo-win32.h>
79 #include "wx/osx/private.h"
80 #include <cairo-quartz.h>
81 #include <cairo-atsui.h>
84 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
87 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
90 virtual wxGraphicsObjectRefData
*Clone() const;
93 // These are the path primitives from which everything else can be constructed
96 // begins a new subpath at (x,y)
97 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
99 // adds a straight line from the current point to (x,y)
100 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
102 // adds a cubic Bezier curve from the current point, using two control points and an end point
103 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
106 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
107 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
109 // gets the last point of the current path, (0,0) if not yet set
110 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
113 virtual void AddPath( const wxGraphicsPathData
* path
);
115 // closes the current sub-path
116 virtual void CloseSubpath();
119 // These are convenience functions which - if not available natively will be assembled
120 // using the primitives from above
125 // appends a rectangle as a new closed subpath
126 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
127 // appends an ellipsis as a new closed subpath fitting the passed rectangle
128 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
130 // 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)
131 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
134 // returns the native path
135 virtual void * GetNativePath() const ;
137 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
138 virtual void UnGetNativePath(void *p
) const;
140 // transforms each point of this path by the matrix
141 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
143 // gets the bounding box enclosing all points (possibly including control points)
144 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
146 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
149 cairo_t
* m_pathContext
;
152 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
155 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
156 virtual ~wxCairoMatrixData() ;
158 virtual wxGraphicsObjectRefData
*Clone() const ;
160 // concatenates the matrix
161 virtual void Concat( const wxGraphicsMatrixData
*t
);
163 // sets the matrix to the respective values
164 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
165 wxDouble tx
=0.0, wxDouble ty
=0.0);
167 // gets the component valuess of the matrix
168 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
169 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
171 // makes this the inverse matrix
172 virtual void Invert();
174 // returns true if the elements of the transformation matrix are equal ?
175 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
177 // return true if this is the identity matrix
178 virtual bool IsIdentity() const;
184 // add the translation to this matrix
185 virtual void Translate( wxDouble dx
, wxDouble dy
);
187 // add the scale to this matrix
188 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
190 // add the rotation to this matrix (radians)
191 virtual void Rotate( wxDouble angle
);
194 // apply the transforms
197 // applies that matrix to the point
198 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
200 // applies the matrix except for translations
201 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
203 // returns the native representation
204 virtual void * GetNativeMatrix() const;
206 cairo_matrix_t m_matrix
;
209 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
212 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
217 virtual void Apply( wxGraphicsContext
* context
);
218 virtual wxDouble
GetWidth() { return m_width
; }
228 cairo_line_cap_t m_cap
;
229 cairo_line_join_t m_join
;
232 const double *m_lengths
;
233 double *m_userLengths
;
237 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
240 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
243 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
244 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
245 ~wxCairoBrushData ();
247 virtual void Apply( wxGraphicsContext
* context
);
248 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
249 const wxColour
&c1
, const wxColour
&c2
);
250 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
251 const wxColour
&oColor
, const wxColour
&cColor
);
262 cairo_pattern_t
* m_brushPattern
;
265 class wxCairoFontData
: public wxGraphicsObjectRefData
268 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
271 virtual void Apply( wxGraphicsContext
* context
);
273 const PangoFontDescription
* GetFont() const { return m_font
; }
274 bool GetUnderlined() const { return m_underlined
; }
284 cairo_font_face_t
*m_font
;
285 #elif defined(__WXGTK__)
286 PangoFontDescription
* m_font
;
288 wxCharBuffer m_fontName
;
289 cairo_font_slant_t m_slant
;
290 cairo_font_weight_t m_weight
;
293 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
297 class wxCairoBitmapData
: public wxGraphicsObjectRefData
300 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
301 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
302 ~wxCairoBitmapData();
304 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
305 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
306 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
308 cairo_surface_t
* m_surface
;
309 cairo_pattern_t
* m_pattern
;
312 unsigned char* m_buffer
;
315 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
318 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
319 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
320 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
322 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
324 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
325 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
327 virtual ~wxCairoContext();
329 virtual bool ShouldOffset() const
332 if ( !m_pen
.IsNull() )
334 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
338 return ( penwidth
% 2 ) == 1;
341 virtual void Clip( const wxRegion
®ion
);
343 cairo_surface_t
* m_mswSurface
;
346 // clips drawings to the rect
347 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
349 // resets the clipping to original extent
350 virtual void ResetClip();
352 virtual void * GetNativeContext();
354 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
356 virtual bool SetCompositionMode(wxCompositionMode op
);
358 virtual void BeginLayer(wxDouble opacity
);
360 virtual void EndLayer();
362 virtual void StrokePath( const wxGraphicsPath
& p
);
363 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
365 virtual void Translate( wxDouble dx
, wxDouble dy
);
366 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
367 virtual void Rotate( wxDouble angle
);
369 // concatenates this transform with the current transform of this context
370 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
372 // sets the transform of this context
373 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
375 // gets the matrix of this context
376 virtual wxGraphicsMatrix
GetTransform() const;
378 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
379 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
380 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
381 virtual void PushState();
382 virtual void PopState();
384 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
385 wxDouble
*descent
, wxDouble
*externalLeading
) const;
386 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
389 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
392 void Init(cairo_t
*context
);
396 wxVector
<float> m_layerOpacities
;
398 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
401 //-----------------------------------------------------------------------------
402 // wxCairoPenData implementation
403 //-----------------------------------------------------------------------------
405 wxCairoPenData::~wxCairoPenData()
407 delete[] m_userLengths
;
410 void wxCairoPenData::Init()
413 m_userLengths
= NULL
;
418 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
419 : wxGraphicsObjectRefData(renderer
)
423 m_width
= m_pen
.GetWidth();
427 m_red
= m_pen
.GetColour().Red()/255.0;
428 m_green
= m_pen
.GetColour().Green()/255.0;
429 m_blue
= m_pen
.GetColour().Blue()/255.0;
430 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
432 switch ( m_pen
.GetCap() )
435 m_cap
= CAIRO_LINE_CAP_ROUND
;
438 case wxCAP_PROJECTING
:
439 m_cap
= CAIRO_LINE_CAP_SQUARE
;
443 m_cap
= CAIRO_LINE_CAP_BUTT
;
447 m_cap
= CAIRO_LINE_CAP_BUTT
;
451 switch ( m_pen
.GetJoin() )
454 m_join
= CAIRO_LINE_JOIN_BEVEL
;
458 m_join
= CAIRO_LINE_JOIN_MITER
;
462 m_join
= CAIRO_LINE_JOIN_ROUND
;
466 m_join
= CAIRO_LINE_JOIN_MITER
;
470 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
471 const double dotted
[] =
473 dashUnit
, dashUnit
+ 2.0
475 static const double short_dashed
[] =
479 static const double dashed
[] =
483 static const double dotted_dashed
[] =
485 9.0 , 6.0 , 3.0 , 3.0
488 switch ( m_pen
.GetStyle() )
490 case wxPENSTYLE_SOLID
:
493 case wxPENSTYLE_DOT
:
494 m_count
= WXSIZEOF(dotted
);
495 m_userLengths
= new double[ m_count
] ;
496 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
497 m_lengths
= m_userLengths
;
500 case wxPENSTYLE_LONG_DASH
:
502 m_count
= WXSIZEOF(dashed
);
505 case wxPENSTYLE_SHORT_DASH
:
506 m_lengths
= short_dashed
;
507 m_count
= WXSIZEOF(short_dashed
);
510 case wxPENSTYLE_DOT_DASH
:
511 m_lengths
= dotted_dashed
;
512 m_count
= WXSIZEOF(dotted_dashed
);
515 case wxPENSTYLE_USER_DASH
:
518 m_count
= m_pen
.GetDashes( &wxdashes
) ;
519 if ((wxdashes
!= NULL
) && (m_count
> 0))
521 m_userLengths
= new double[m_count
] ;
522 for ( int i
= 0 ; i
< m_count
; ++i
)
524 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
526 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
527 m_userLengths
[i
] = dashUnit
+ 2.0 ;
528 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
529 m_userLengths
[i
] = dashUnit
;
532 m_lengths
= m_userLengths
;
535 case wxPENSTYLE_STIPPLE
:
538 wxBitmap* bmp = pen.GetStipple();
539 if ( bmp && bmp->Ok() )
541 wxDELETE( m_penImage );
542 wxDELETE( m_penBrush );
543 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
544 m_penBrush = new TextureBrush(m_penImage);
545 m_pen->SetBrush( m_penBrush );
551 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
552 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
555 wxDELETE( m_penBrush );
556 HatchStyle style = HatchStyleHorizontal;
557 switch( pen.GetStyle() )
559 case wxPENSTYLE_BDIAGONAL_HATCH :
560 style = HatchStyleBackwardDiagonal;
562 case wxPENSTYLE_CROSSDIAG_HATCH :
563 style = HatchStyleDiagonalCross;
565 case wxPENSTYLE_FDIAGONAL_HATCH :
566 style = HatchStyleForwardDiagonal;
568 case wxPENSTYLE_CROSS_HATCH :
569 style = HatchStyleCross;
571 case wxPENSTYLE_HORIZONTAL_HATCH :
572 style = HatchStyleHorizontal;
574 case wxPENSTYLE_VERTICAL_HATCH :
575 style = HatchStyleVertical;
579 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
580 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
581 m_pen->SetBrush( m_penBrush )
588 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
590 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
591 cairo_set_line_width(ctext
,m_width
);
592 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
593 cairo_set_line_cap(ctext
,m_cap
);
594 cairo_set_line_join(ctext
,m_join
);
595 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
598 //-----------------------------------------------------------------------------
599 // wxCairoBrushData implementation
600 //-----------------------------------------------------------------------------
602 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
603 : wxGraphicsObjectRefData( renderer
)
608 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
609 : wxGraphicsObjectRefData(renderer
)
613 m_red
= brush
.GetColour().Red()/255.0;
614 m_green
= brush
.GetColour().Green()/255.0;
615 m_blue
= brush
.GetColour().Blue()/255.0;
616 m_alpha
= brush
.GetColour().Alpha()/255.0;
618 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
620 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
621 brush.GetColour().Green() , brush.GetColour().Blue() ) );
623 else if ( brush.IsHatch() )
625 HatchStyle style = HatchStyleHorizontal;
626 switch( brush.GetStyle() )
628 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
629 style = HatchStyleBackwardDiagonal;
631 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
632 style = HatchStyleDiagonalCross;
634 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
635 style = HatchStyleForwardDiagonal;
637 case wxBRUSHSTYLE_CROSS_HATCH :
638 style = HatchStyleCross;
640 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
641 style = HatchStyleHorizontal;
643 case wxBRUSHSTYLE_VERTICAL_HATCH :
644 style = HatchStyleVertical;
648 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
649 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
653 wxBitmap* bmp = brush.GetStipple();
654 if ( bmp && bmp->Ok() )
656 wxDELETE( m_brushImage );
657 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
658 m_brush = new TextureBrush(m_brushImage);
664 wxCairoBrushData::~wxCairoBrushData ()
667 cairo_pattern_destroy(m_brushPattern
);
670 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
672 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
673 if ( m_brushPattern
)
675 cairo_set_source(ctext
,m_brushPattern
);
679 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
683 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
684 const wxColour
&c1
, const wxColour
&c2
)
686 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
687 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
688 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
689 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
690 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
691 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
694 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
695 const wxColour
&oColor
, const wxColour
&cColor
)
697 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
698 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
699 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
700 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
701 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
702 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
705 void wxCairoBrushData::Init()
707 m_brushPattern
= NULL
;
710 //-----------------------------------------------------------------------------
711 // wxCairoFontData implementation
712 //-----------------------------------------------------------------------------
714 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
715 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
717 m_red
= col
.Red()/255.0;
718 m_green
= col
.Green()/255.0;
719 m_blue
= col
.Blue()/255.0;
720 m_alpha
= col
.Alpha()/255.0;
721 m_size
= font
.GetPointSize();
722 m_underlined
= font
.GetUnderlined();
725 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
726 #elif defined(__WXGTK__)
727 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
729 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
730 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
731 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
735 wxCairoFontData::~wxCairoFontData()
738 cairo_font_face_destroy( m_font
);
739 #elif defined(__WXGTK__)
740 pango_font_description_free( m_font
);
745 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
747 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
748 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
750 // the rest is done using Pango layouts
751 #elif defined(__WXMAC__)
752 cairo_set_font_face(ctext
, m_font
);
753 cairo_set_font_size(ctext
, m_size
);
755 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
756 cairo_set_font_size(ctext
, m_size
);
760 //-----------------------------------------------------------------------------
761 // wxCairoPathData implementation
762 //-----------------------------------------------------------------------------
764 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
765 : wxGraphicsPathData(renderer
)
769 m_pathContext
= pathcontext
;
773 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
774 m_pathContext
= cairo_create(surface
);
775 cairo_surface_destroy (surface
);
779 wxCairoPathData::~wxCairoPathData()
781 cairo_destroy(m_pathContext
);
784 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
786 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
787 cairo_t
* pathcontext
= cairo_create(surface
);
788 cairo_surface_destroy (surface
);
790 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
791 cairo_append_path(pathcontext
, path
);
792 cairo_path_destroy(path
);
793 return new wxCairoPathData( GetRenderer() ,pathcontext
);
797 void* wxCairoPathData::GetNativePath() const
799 return cairo_copy_path(m_pathContext
) ;
802 void wxCairoPathData::UnGetNativePath(void *p
) const
804 cairo_path_destroy((cairo_path_t
*)p
);
811 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
813 cairo_move_to(m_pathContext
,x
,y
);
816 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
818 cairo_line_to(m_pathContext
,x
,y
);
821 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
823 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
824 cairo_append_path(m_pathContext
, p
);
828 void wxCairoPathData::CloseSubpath()
830 cairo_close_path(m_pathContext
);
833 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
835 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
838 // gets the last point of the current path, (0,0) if not yet set
839 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
842 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
849 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
851 // as clockwise means positive in our system (y pointing downwards)
852 // TODO make this interpretation dependent of the
854 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
855 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
857 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
860 // transforms each point of this path by the matrix
861 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
863 // as we don't have a true path object, we have to apply the inverse
864 // matrix to the context
865 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
866 cairo_matrix_invert( &m
);
867 cairo_transform(m_pathContext
,&m
);
870 // gets the bounding box enclosing all points (possibly including control points)
871 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
875 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
899 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode
WXUNUSED(fillStyle
) ) const
901 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
904 //-----------------------------------------------------------------------------
905 // wxCairoMatrixData implementation
906 //-----------------------------------------------------------------------------
908 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
909 : wxGraphicsMatrixData(renderer
)
915 wxCairoMatrixData::~wxCairoMatrixData()
920 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
922 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
925 // concatenates the matrix
926 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
928 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
931 // sets the matrix to the respective values
932 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
933 wxDouble tx
, wxDouble ty
)
935 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
938 // gets the component valuess of the matrix
939 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
940 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
942 if (a
) *a
= m_matrix
.xx
;
943 if (b
) *b
= m_matrix
.yx
;
944 if (c
) *c
= m_matrix
.xy
;
945 if (d
) *d
= m_matrix
.yy
;
946 if (tx
) *tx
= m_matrix
.x0
;
947 if (ty
) *ty
= m_matrix
.y0
;
950 // makes this the inverse matrix
951 void wxCairoMatrixData::Invert()
953 cairo_matrix_invert( &m_matrix
);
956 // returns true if the elements of the transformation matrix are equal ?
957 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
959 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
961 m_matrix
.xx
== tm
->xx
&&
962 m_matrix
.yx
== tm
->yx
&&
963 m_matrix
.xy
== tm
->xy
&&
964 m_matrix
.yy
== tm
->yy
&&
965 m_matrix
.x0
== tm
->x0
&&
966 m_matrix
.y0
== tm
->y0
) ;
969 // return true if this is the identity matrix
970 bool wxCairoMatrixData::IsIdentity() const
972 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
973 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
980 // add the translation to this matrix
981 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
983 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
986 // add the scale to this matrix
987 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
989 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
992 // add the rotation to this matrix (radians)
993 void wxCairoMatrixData::Rotate( wxDouble angle
)
995 cairo_matrix_rotate( &m_matrix
, angle
) ;
999 // apply the transforms
1002 // applies that matrix to the point
1003 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1005 double lx
= *x
, ly
= *y
;
1006 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1011 // applies the matrix except for translations
1012 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1014 double lx
= *dx
, ly
= *dy
;
1015 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1020 // returns the native representation
1021 void * wxCairoMatrixData::GetNativeMatrix() const
1023 return (void*) &m_matrix
;
1026 // wxCairoBitmap implementation
1027 //-----------------------------------------------------------------------------
1029 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1030 wxGraphicsObjectRefData( renderer
)
1033 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1036 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1038 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1040 int bw
= bmp
.GetWidth();
1041 int bh
= bmp
.GetHeight();
1042 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1043 m_buffer
= new unsigned char[bw
*bh
*4];
1044 wxUint32
* data
= (wxUint32
*)m_buffer
;
1046 // Create a surface object and copy the bitmap pixel data to it. if the
1047 // image has alpha (or a mask represented as alpha) then we'll use a
1048 // different format and iterator than if it doesn't...
1049 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1051 m_surface
= cairo_image_surface_create_for_data(
1052 m_buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1053 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1054 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1056 wxAlphaPixelData::Iterator
p(pixData
);
1057 for (int y
=0; y
<bh
; y
++)
1059 wxAlphaPixelData::Iterator rowStart
= p
;
1060 for (int x
=0; x
<bw
; x
++)
1062 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1063 // with alpha in the upper 8 bits, then red, then green, then
1064 // blue. The 32-bit quantities are stored native-endian.
1065 // Pre-multiplied alpha is used.
1066 unsigned char alpha
= p
.Alpha();
1070 *data
= ( alpha
<< 24
1071 | (p
.Red() * alpha
/255) << 16
1072 | (p
.Green() * alpha
/255) << 8
1073 | (p
.Blue() * alpha
/255) );
1078 p
.OffsetY(pixData
, 1);
1083 m_surface
= cairo_image_surface_create_for_data(
1084 m_buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1085 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1086 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1088 wxNativePixelData::Iterator
p(pixData
);
1089 for (int y
=0; y
<bh
; y
++)
1091 wxNativePixelData::Iterator rowStart
= p
;
1092 for (int x
=0; x
<bw
; x
++)
1094 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1095 // the upper 8 bits unused. Red, Green, and Blue are stored in
1096 // the remaining 24 bits in that order. The 32-bit quantities
1097 // are stored native-endian.
1098 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1103 p
.OffsetY(pixData
, 1);
1106 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1109 wxCairoBitmapData::~wxCairoBitmapData()
1112 cairo_pattern_destroy(m_pattern
);
1115 cairo_surface_destroy(m_surface
);
1122 //-----------------------------------------------------------------------------
1123 // wxCairoContext implementation
1124 //-----------------------------------------------------------------------------
1126 class wxCairoOffsetHelper
1129 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1134 cairo_translate( m_ctx
, 0.5, 0.5 );
1136 ~wxCairoOffsetHelper( )
1139 cairo_translate( m_ctx
, -0.5, -0.5 );
1146 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1147 : wxGraphicsContext(renderer
)
1150 const wxDCImpl
*impl
= dc
.GetImpl();
1151 Init( (cairo_t
*) impl
->GetCairoContext() );
1153 wxPoint org
= dc
.GetDeviceOrigin();
1154 cairo_translate( m_context
, org
.x
, org
.y
);
1157 dc
.GetUserScale( &sx
, &sy
);
1158 cairo_scale( m_context
, sx
, sy
);
1160 org
= dc
.GetLogicalOrigin();
1161 cairo_translate( m_context
, -org
.x
, -org
.y
);
1165 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1166 : wxGraphicsContext(renderer
)
1169 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1170 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1173 wxGraphicsMatrix matrix
= CreateMatrix();
1175 wxPoint org
= dc
.GetDeviceOrigin();
1176 matrix
.Translate( org
.x
, org
.y
);
1178 org
= dc
.GetLogicalOrigin();
1179 matrix
.Translate( -org
.x
, -org
.y
);
1182 dc
.GetUserScale( &sx
, &sy
);
1183 matrix
.Scale( sx
, sy
);
1185 ConcatTransform( matrix
);
1191 dc
.GetSize( &width
, &height
);
1192 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1193 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1194 Init( cairo_create( surface
) );
1195 cairo_surface_destroy( surface
);
1199 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1200 : wxGraphicsContext(renderer
)
1203 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1204 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1207 wxGraphicsMatrix matrix
= CreateMatrix();
1209 wxPoint org
= dc
.GetDeviceOrigin();
1210 matrix
.Translate( org
.x
, org
.y
);
1212 org
= dc
.GetLogicalOrigin();
1213 matrix
.Translate( -org
.x
, -org
.y
);
1216 dc
.GetUserScale( &sx
, &sy
);
1217 matrix
.Scale( sx
, sy
);
1219 ConcatTransform( matrix
);
1225 dc
.GetSize( &width
, &height
);
1226 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1227 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1228 Init( cairo_create( surface
) );
1229 cairo_surface_destroy( surface
);
1234 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1235 : wxGraphicsContext(renderer
)
1237 Init( gdk_cairo_create( drawable
) );
1242 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1243 : wxGraphicsContext(renderer
)
1245 m_mswSurface
= cairo_win32_surface_create(handle
);
1246 m_context
= cairo_create(m_mswSurface
);
1253 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1254 : wxGraphicsContext(renderer
)
1259 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1260 : wxGraphicsContext(renderer
)
1263 // something along these lines (copied from dcclient)
1265 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1266 // code should still be able to create wxClientDCs for them, so we will
1267 // use the parent window here then.
1268 if (window
->m_wxwindow
== NULL
)
1270 window
= window
->GetParent();
1273 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1275 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1279 wxCairoContext::~wxCairoContext()
1285 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1286 m_context
= cairo_create(m_mswSurface
);
1289 cairo_destroy(m_context
);
1293 cairo_surface_destroy(m_mswSurface
);
1297 void wxCairoContext::Init(cairo_t
*context
)
1299 m_context
= context
;
1305 void wxCairoContext::Clip( const wxRegion
& region
)
1307 // Create a path with all the rectangles in the region
1308 wxGraphicsPath path
= GetRenderer()->CreatePath();
1309 wxRegionIterator
ri(region
);
1312 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1316 // Put it in the context
1317 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1318 cairo_append_path(m_context
, cp
);
1320 // clip to that path
1321 cairo_clip(m_context
);
1322 path
.UnGetNativePath(cp
);
1325 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1327 // Create a path with this rectangle
1328 wxGraphicsPath path
= GetRenderer()->CreatePath();
1329 path
.AddRectangle(x
,y
,w
,h
);
1331 // Put it in the context
1332 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1333 cairo_append_path(m_context
, cp
);
1335 // clip to that path
1336 cairo_clip(m_context
);
1337 path
.UnGetNativePath(cp
);
1340 void wxCairoContext::ResetClip()
1342 cairo_reset_clip(m_context
);
1346 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1348 if ( !m_pen
.IsNull() )
1350 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1351 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1352 cairo_append_path(m_context
,cp
);
1353 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1354 cairo_stroke(m_context
);
1355 path
.UnGetNativePath(cp
);
1359 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1361 if ( !m_brush
.IsNull() )
1363 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1364 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1365 cairo_append_path(m_context
,cp
);
1366 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1367 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1368 cairo_fill(m_context
);
1369 path
.UnGetNativePath(cp
);
1373 void wxCairoContext::Rotate( wxDouble angle
)
1375 cairo_rotate(m_context
,angle
);
1378 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1380 cairo_translate(m_context
,dx
,dy
);
1383 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1385 cairo_scale(m_context
,xScale
,yScale
);
1388 // concatenates this transform with the current transform of this context
1389 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1391 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1394 // sets the transform of this context
1395 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1397 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1400 // gets the matrix of this context
1401 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1403 wxGraphicsMatrix matrix
= CreateMatrix();
1404 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1410 void wxCairoContext::PushState()
1412 cairo_save(m_context
);
1415 void wxCairoContext::PopState()
1417 cairo_restore(m_context
);
1420 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1422 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1423 DrawBitmap(bitmap
, x
, y
, w
, h
);
1427 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1431 // In case we're scaling the image by using a width and height different
1432 // than the bitmap's size create a pattern transformation on the surface and
1433 // draw the transformed pattern.
1434 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1435 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1436 wxSize size
= data
->GetSize();
1438 wxDouble scaleX
= w
/ size
.GetWidth();
1439 wxDouble scaleY
= h
/ size
.GetHeight();
1440 cairo_scale(m_context
, scaleX
, scaleY
);
1442 // prepare to draw the image
1443 cairo_translate(m_context
, x
, y
);
1444 cairo_set_source(m_context
, pattern
);
1445 // use the original size here since the context is scaled already...
1446 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1447 // fill the rectangle using the pattern
1448 cairo_fill(m_context
);
1453 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1455 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1456 // start using the Cairo backend on other platforms then we may need to
1457 // fiddle with this...
1458 DrawBitmap(icon
, x
, y
, w
, h
);
1462 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1464 wxCHECK_RET( !m_font
.IsNull(),
1465 wxT("wxCairoContext::DrawText - no valid font set") );
1470 const wxCharBuffer data
= str
.utf8_str();
1474 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1477 size_t datalen
= strlen(data
);
1479 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1480 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1481 pango_layout_set_font_description( layout
, font_data
->GetFont());
1482 pango_layout_set_text(layout
, data
, datalen
);
1484 if (font_data
->GetUnderlined())
1486 PangoAttrList
*attrs
= pango_attr_list_new();
1487 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1488 pango_attr_list_insert(attrs
, attr
);
1489 pango_layout_set_attributes(layout
, attrs
);
1490 pango_attr_list_unref(attrs
);
1493 cairo_move_to(m_context
, x
, y
);
1494 pango_cairo_show_layout (m_context
, layout
);
1496 g_object_unref (layout
);
1498 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1499 // the position we move to by the ascent.
1500 cairo_font_extents_t fe
;
1501 cairo_font_extents(m_context
, &fe
);
1502 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1504 cairo_show_text(m_context
, data
);
1508 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1509 wxDouble
*descent
, wxDouble
*externalLeading
) const
1511 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1519 if ( externalLeading
)
1520 *externalLeading
= 0;
1528 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1529 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1530 const wxCharBuffer data
= str
.utf8_str();
1535 pango_layout_set_text( layout
, data
, strlen(data
) );
1536 pango_layout_get_pixel_size (layout
, &w
, &h
);
1543 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1544 int baseline
= pango_layout_iter_get_baseline(iter
);
1545 pango_layout_iter_free(iter
);
1546 *descent
= h
- PANGO_PIXELS(baseline
);
1548 g_object_unref (layout
);
1550 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1554 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1555 cairo_text_extents_t te
;
1556 cairo_text_extents(m_context
, buf
, &te
);
1560 if (height
|| descent
|| externalLeading
)
1562 cairo_font_extents_t fe
;
1563 cairo_font_extents(m_context
, &fe
);
1565 // some backends have negative descents
1567 if ( fe
.descent
< 0 )
1568 fe
.descent
= -fe
.descent
;
1570 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1572 // some backends are broken re height ... (eg currently ATSUI)
1573 fe
.height
= fe
.ascent
+ fe
.descent
;
1577 *height
= fe
.height
;
1579 *descent
= fe
.descent
;
1580 if ( externalLeading
)
1581 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1586 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1589 widths
.Add(0, text
.length());
1591 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1599 void * wxCairoContext::GetNativeContext()
1604 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1606 if (m_antialias
== antialias
)
1609 m_antialias
= antialias
;
1611 cairo_antialias_t antialiasMode
;
1614 case wxANTIALIAS_DEFAULT
:
1615 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1617 case wxANTIALIAS_NONE
:
1618 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1623 cairo_set_antialias(m_context
, antialiasMode
);
1627 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1629 if ( m_composition
== op
)
1633 cairo_operator_t cop
;
1636 case wxCOMPOSITION_CLEAR
:
1637 cop
= CAIRO_OPERATOR_CLEAR
;
1639 case wxCOMPOSITION_SOURCE
:
1640 cop
= CAIRO_OPERATOR_SOURCE
;
1642 case wxCOMPOSITION_OVER
:
1643 cop
= CAIRO_OPERATOR_OVER
;
1645 case wxCOMPOSITION_IN
:
1646 cop
= CAIRO_OPERATOR_IN
;
1648 case wxCOMPOSITION_OUT
:
1649 cop
= CAIRO_OPERATOR_OUT
;
1651 case wxCOMPOSITION_ATOP
:
1652 cop
= CAIRO_OPERATOR_ATOP
;
1654 case wxCOMPOSITION_DEST
:
1655 cop
= CAIRO_OPERATOR_DEST
;
1657 case wxCOMPOSITION_DEST_OVER
:
1658 cop
= CAIRO_OPERATOR_DEST_OVER
;
1660 case wxCOMPOSITION_DEST_IN
:
1661 cop
= CAIRO_OPERATOR_DEST_IN
;
1663 case wxCOMPOSITION_DEST_OUT
:
1664 cop
= CAIRO_OPERATOR_DEST_OUT
;
1666 case wxCOMPOSITION_DEST_ATOP
:
1667 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1669 case wxCOMPOSITION_XOR
:
1670 cop
= CAIRO_OPERATOR_XOR
;
1672 case wxCOMPOSITION_ADD
:
1673 cop
= CAIRO_OPERATOR_ADD
;
1678 cairo_set_operator(m_context
, cop
);
1682 void wxCairoContext::BeginLayer(wxDouble opacity
)
1684 m_layerOpacities
.push_back(opacity
);
1685 cairo_push_group(m_context
);
1688 void wxCairoContext::EndLayer()
1690 float opacity
= m_layerOpacities
.back();
1691 m_layerOpacities
.pop_back();
1692 cairo_pop_group_to_source(m_context
);
1693 cairo_paint_with_alpha(m_context
,opacity
);
1696 //-----------------------------------------------------------------------------
1697 // wxCairoRenderer declaration
1698 //-----------------------------------------------------------------------------
1700 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1703 wxCairoRenderer() {}
1705 virtual ~wxCairoRenderer() {}
1709 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1710 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1711 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1713 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1715 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1717 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1719 virtual wxGraphicsContext
* CreateMeasuringContext();
1723 virtual wxGraphicsPath
CreatePath();
1727 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1728 wxDouble tx
=0.0, wxDouble ty
=0.0);
1731 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1733 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1735 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1736 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1737 const wxColour
&c1
, const wxColour
&c2
) ;
1739 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1740 // with radius r and color cColor
1741 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1742 const wxColour
&oColor
, const wxColour
&cColor
) ;
1745 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1747 // create a native bitmap representation
1748 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1750 // create a graphics bitmap from a native bitmap
1751 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1753 // create a subimage from a native image representation
1754 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1757 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1760 //-----------------------------------------------------------------------------
1761 // wxCairoRenderer implementation
1762 //-----------------------------------------------------------------------------
1764 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1766 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1767 // temporary hack to allow creating a cairo context on any platform
1768 extern wxGraphicsRenderer
* gCairoRenderer
;
1769 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1772 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1774 return &gs_cairoGraphicsRenderer
;
1778 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1780 return new wxCairoContext(this,dc
);
1783 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1785 return new wxCairoContext(this,dc
);
1788 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1791 const wxDCImpl
*impl
= dc
.GetImpl();
1792 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1794 return new wxCairoContext(this,dc
);
1800 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1803 return new wxCairoContext(this,(HDC
)context
);
1806 return new wxCairoContext(this,(cairo_t
*)context
);
1811 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1814 return new wxCairoContext(this,(GdkDrawable
*)window
);
1820 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1823 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1829 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1831 return new wxCairoContext(this, window
);
1836 wxGraphicsPath
wxCairoRenderer::CreatePath()
1838 wxGraphicsPath path
;
1839 path
.SetRefData( new wxCairoPathData(this) );
1846 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1847 wxDouble tx
, wxDouble ty
)
1851 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1852 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1857 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1859 if ( !pen
.Ok() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1860 return wxNullGraphicsPen
;
1864 p
.SetRefData(new wxCairoPenData( this, pen
));
1869 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1871 if ( !brush
.Ok() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1872 return wxNullGraphicsBrush
;
1876 p
.SetRefData(new wxCairoBrushData( this, brush
));
1881 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1882 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1883 const wxColour
&c1
, const wxColour
&c2
)
1886 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1887 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1892 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1893 // with radius r and color cColor
1894 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1895 const wxColour
&oColor
, const wxColour
&cColor
)
1898 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1899 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1905 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1910 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1914 return wxNullGraphicsFont
;
1917 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
1922 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
1926 return wxNullGraphicsBitmap
;
1929 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
1931 if ( bitmap
!= NULL
)
1934 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
1938 return wxNullGraphicsBitmap
;
1942 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
1943 wxDouble
WXUNUSED(x
),
1944 wxDouble
WXUNUSED(y
),
1945 wxDouble
WXUNUSED(w
),
1946 wxDouble
WXUNUSED(h
))
1948 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
1949 return wxNullGraphicsBitmap
;
1952 #endif // wxUSE_GRAPHICS_CONTEXT && wxUSE_CAIRO
1954 #if wxUSE_GRAPHICS_CONTEXT
1955 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
1958 return &gs_cairoGraphicsRenderer
;