1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_GRAPHICS_CONTEXT
20 #include "wx/graphics.h"
27 #include "wx/bitmap.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcmemory.h"
31 #include "wx/dcprint.h"
34 #include "wx/private/graphics.h"
35 #include "wx/rawbmp.h"
36 #include "wx/vector.h"
40 //-----------------------------------------------------------------------------
41 // device context implementation
43 // more and more of the dc functionality should be implemented by calling
44 // the appropricate wxCairoContext, but we will have to do that step by step
45 // also coordinate conversions should be moved to native matrix ops
46 //-----------------------------------------------------------------------------
48 // we always stock two context states, one at entry, to be able to preserve the
49 // state we were called with, the other one after changing to HI Graphics orientation
50 // (this one is used for getting back clippings etc)
52 //-----------------------------------------------------------------------------
53 // wxGraphicsPath implementation
54 //-----------------------------------------------------------------------------
56 // TODO remove this dependency (gdiplus needs the macros)
59 #define max(a,b) (((a) > (b)) ? (a) : (b))
63 #define min(a,b) (((a) < (b)) ? (a) : (b))
68 #include <cairo-win32.h>
73 #include "wx/fontutil.h"
74 #include "wx/gtk/dc.h"
78 #include <cairo-win32.h>
82 #include "wx/osx/private.h"
83 #include <cairo-quartz.h>
84 #include <cairo-atsui.h>
87 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
90 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
93 virtual wxGraphicsObjectRefData
*Clone() const;
96 // These are the path primitives from which everything else can be constructed
99 // begins a new subpath at (x,y)
100 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
102 // adds a straight line from the current point to (x,y)
103 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
105 // adds a cubic Bezier curve from the current point, using two control points and an end point
106 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
109 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
110 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
112 // gets the last point of the current path, (0,0) if not yet set
113 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
116 virtual void AddPath( const wxGraphicsPathData
* path
);
118 // closes the current sub-path
119 virtual void CloseSubpath();
122 // These are convenience functions which - if not available natively will be assembled
123 // using the primitives from above
128 // appends a rectangle as a new closed subpath
129 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
130 // appends an ellipsis as a new closed subpath fitting the passed rectangle
131 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
133 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
134 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
137 // returns the native path
138 virtual void * GetNativePath() const ;
140 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
141 virtual void UnGetNativePath(void *p
) const;
143 // transforms each point of this path by the matrix
144 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
146 // gets the bounding box enclosing all points (possibly including control points)
147 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
149 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
152 cairo_t
* m_pathContext
;
155 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
158 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
159 virtual ~wxCairoMatrixData() ;
161 virtual wxGraphicsObjectRefData
*Clone() const ;
163 // concatenates the matrix
164 virtual void Concat( const wxGraphicsMatrixData
*t
);
166 // sets the matrix to the respective values
167 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
168 wxDouble tx
=0.0, wxDouble ty
=0.0);
170 // gets the component valuess of the matrix
171 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
172 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
174 // makes this the inverse matrix
175 virtual void Invert();
177 // returns true if the elements of the transformation matrix are equal ?
178 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
180 // return true if this is the identity matrix
181 virtual bool IsIdentity() const;
187 // add the translation to this matrix
188 virtual void Translate( wxDouble dx
, wxDouble dy
);
190 // add the scale to this matrix
191 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
193 // add the rotation to this matrix (radians)
194 virtual void Rotate( wxDouble angle
);
197 // apply the transforms
200 // applies that matrix to the point
201 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
203 // applies the matrix except for translations
204 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
206 // returns the native representation
207 virtual void * GetNativeMatrix() const;
209 cairo_matrix_t m_matrix
;
212 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
215 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
220 virtual void Apply( wxGraphicsContext
* context
);
221 virtual wxDouble
GetWidth() { return m_width
; }
231 cairo_line_cap_t m_cap
;
232 cairo_line_join_t m_join
;
235 const double *m_lengths
;
236 double *m_userLengths
;
240 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
243 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
246 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
247 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
248 ~wxCairoBrushData ();
250 virtual void Apply( wxGraphicsContext
* context
);
251 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
252 const wxColour
&c1
, const wxColour
&c2
);
253 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
254 const wxColour
&oColor
, const wxColour
&cColor
);
265 cairo_pattern_t
* m_brushPattern
;
268 class wxCairoFontData
: public wxGraphicsObjectRefData
271 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
274 virtual void Apply( wxGraphicsContext
* context
);
276 const PangoFontDescription
* GetFont() const { return m_font
; }
277 bool GetUnderlined() const { return m_underlined
; }
287 cairo_font_face_t
*m_font
;
288 #elif defined(__WXGTK__)
289 PangoFontDescription
* m_font
;
291 wxCharBuffer m_fontName
;
292 cairo_font_slant_t m_slant
;
293 cairo_font_weight_t m_weight
;
296 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
300 class wxCairoBitmapData
: public wxGraphicsObjectRefData
303 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
304 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
305 ~wxCairoBitmapData();
307 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
308 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
309 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
311 cairo_surface_t
* m_surface
;
312 cairo_pattern_t
* m_pattern
;
315 unsigned char* m_buffer
;
318 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
321 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
322 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
323 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
325 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
327 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
328 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
330 virtual ~wxCairoContext();
332 virtual bool ShouldOffset() const
335 if ( !m_pen
.IsNull() )
337 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
341 return ( penwidth
% 2 ) == 1;
344 virtual void Clip( const wxRegion
®ion
);
346 cairo_surface_t
* m_mswSurface
;
349 // clips drawings to the rect
350 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
352 // resets the clipping to original extent
353 virtual void ResetClip();
355 virtual void * GetNativeContext();
357 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
359 virtual bool SetCompositionMode(wxCompositionMode op
);
361 virtual void BeginLayer(wxDouble opacity
);
363 virtual void EndLayer();
365 virtual void StrokePath( const wxGraphicsPath
& p
);
366 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
368 virtual void Translate( wxDouble dx
, wxDouble dy
);
369 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
370 virtual void Rotate( wxDouble angle
);
372 // concatenates this transform with the current transform of this context
373 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
375 // sets the transform of this context
376 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
378 // gets the matrix of this context
379 virtual wxGraphicsMatrix
GetTransform() const;
381 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
382 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
383 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
384 virtual void PushState();
385 virtual void PopState();
387 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
388 wxDouble
*descent
, wxDouble
*externalLeading
) const;
389 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
392 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
395 void Init(cairo_t
*context
);
399 wxVector
<float> m_layerOpacities
;
401 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
404 //-----------------------------------------------------------------------------
405 // wxCairoPenData implementation
406 //-----------------------------------------------------------------------------
408 wxCairoPenData::~wxCairoPenData()
410 delete[] m_userLengths
;
413 void wxCairoPenData::Init()
416 m_userLengths
= NULL
;
421 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
422 : wxGraphicsObjectRefData(renderer
)
426 m_width
= m_pen
.GetWidth();
430 m_red
= m_pen
.GetColour().Red()/255.0;
431 m_green
= m_pen
.GetColour().Green()/255.0;
432 m_blue
= m_pen
.GetColour().Blue()/255.0;
433 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
435 switch ( m_pen
.GetCap() )
438 m_cap
= CAIRO_LINE_CAP_ROUND
;
441 case wxCAP_PROJECTING
:
442 m_cap
= CAIRO_LINE_CAP_SQUARE
;
446 m_cap
= CAIRO_LINE_CAP_BUTT
;
450 m_cap
= CAIRO_LINE_CAP_BUTT
;
454 switch ( m_pen
.GetJoin() )
457 m_join
= CAIRO_LINE_JOIN_BEVEL
;
461 m_join
= CAIRO_LINE_JOIN_MITER
;
465 m_join
= CAIRO_LINE_JOIN_ROUND
;
469 m_join
= CAIRO_LINE_JOIN_MITER
;
473 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
474 const double dotted
[] =
476 dashUnit
, dashUnit
+ 2.0
478 static const double short_dashed
[] =
482 static const double dashed
[] =
486 static const double dotted_dashed
[] =
488 9.0 , 6.0 , 3.0 , 3.0
491 switch ( m_pen
.GetStyle() )
493 case wxPENSTYLE_SOLID
:
496 case wxPENSTYLE_DOT
:
497 m_count
= WXSIZEOF(dotted
);
498 m_userLengths
= new double[ m_count
] ;
499 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
500 m_lengths
= m_userLengths
;
503 case wxPENSTYLE_LONG_DASH
:
505 m_count
= WXSIZEOF(dashed
);
508 case wxPENSTYLE_SHORT_DASH
:
509 m_lengths
= short_dashed
;
510 m_count
= WXSIZEOF(short_dashed
);
513 case wxPENSTYLE_DOT_DASH
:
514 m_lengths
= dotted_dashed
;
515 m_count
= WXSIZEOF(dotted_dashed
);
518 case wxPENSTYLE_USER_DASH
:
521 m_count
= m_pen
.GetDashes( &wxdashes
) ;
522 if ((wxdashes
!= NULL
) && (m_count
> 0))
524 m_userLengths
= new double[m_count
] ;
525 for ( int i
= 0 ; i
< m_count
; ++i
)
527 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
529 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
530 m_userLengths
[i
] = dashUnit
+ 2.0 ;
531 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
532 m_userLengths
[i
] = dashUnit
;
535 m_lengths
= m_userLengths
;
538 case wxPENSTYLE_STIPPLE
:
541 wxBitmap* bmp = pen.GetStipple();
542 if ( bmp && bmp->Ok() )
544 wxDELETE( m_penImage );
545 wxDELETE( m_penBrush );
546 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
547 m_penBrush = new TextureBrush(m_penImage);
548 m_pen->SetBrush( m_penBrush );
554 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
555 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
558 wxDELETE( m_penBrush );
559 HatchStyle style = HatchStyleHorizontal;
560 switch( pen.GetStyle() )
562 case wxPENSTYLE_BDIAGONAL_HATCH :
563 style = HatchStyleBackwardDiagonal;
565 case wxPENSTYLE_CROSSDIAG_HATCH :
566 style = HatchStyleDiagonalCross;
568 case wxPENSTYLE_FDIAGONAL_HATCH :
569 style = HatchStyleForwardDiagonal;
571 case wxPENSTYLE_CROSS_HATCH :
572 style = HatchStyleCross;
574 case wxPENSTYLE_HORIZONTAL_HATCH :
575 style = HatchStyleHorizontal;
577 case wxPENSTYLE_VERTICAL_HATCH :
578 style = HatchStyleVertical;
582 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
583 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
584 m_pen->SetBrush( m_penBrush )
591 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
593 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
594 cairo_set_line_width(ctext
,m_width
);
595 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
596 cairo_set_line_cap(ctext
,m_cap
);
597 cairo_set_line_join(ctext
,m_join
);
598 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
601 //-----------------------------------------------------------------------------
602 // wxCairoBrushData implementation
603 //-----------------------------------------------------------------------------
605 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
606 : wxGraphicsObjectRefData( renderer
)
611 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
612 : wxGraphicsObjectRefData(renderer
)
616 m_red
= brush
.GetColour().Red()/255.0;
617 m_green
= brush
.GetColour().Green()/255.0;
618 m_blue
= brush
.GetColour().Blue()/255.0;
619 m_alpha
= brush
.GetColour().Alpha()/255.0;
621 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
623 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
624 brush.GetColour().Green() , brush.GetColour().Blue() ) );
626 else if ( brush.IsHatch() )
628 HatchStyle style = HatchStyleHorizontal;
629 switch( brush.GetStyle() )
631 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
632 style = HatchStyleBackwardDiagonal;
634 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
635 style = HatchStyleDiagonalCross;
637 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
638 style = HatchStyleForwardDiagonal;
640 case wxBRUSHSTYLE_CROSS_HATCH :
641 style = HatchStyleCross;
643 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
644 style = HatchStyleHorizontal;
646 case wxBRUSHSTYLE_VERTICAL_HATCH :
647 style = HatchStyleVertical;
651 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
652 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
656 wxBitmap* bmp = brush.GetStipple();
657 if ( bmp && bmp->Ok() )
659 wxDELETE( m_brushImage );
660 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
661 m_brush = new TextureBrush(m_brushImage);
667 wxCairoBrushData::~wxCairoBrushData ()
670 cairo_pattern_destroy(m_brushPattern
);
673 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
675 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
676 if ( m_brushPattern
)
678 cairo_set_source(ctext
,m_brushPattern
);
682 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
686 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
687 const wxColour
&c1
, const wxColour
&c2
)
689 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
690 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
691 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
692 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
693 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
694 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
697 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
698 const wxColour
&oColor
, const wxColour
&cColor
)
700 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
701 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
702 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
703 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
704 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
705 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
708 void wxCairoBrushData::Init()
710 m_brushPattern
= NULL
;
713 //-----------------------------------------------------------------------------
714 // wxCairoFontData implementation
715 //-----------------------------------------------------------------------------
717 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
718 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
720 m_red
= col
.Red()/255.0;
721 m_green
= col
.Green()/255.0;
722 m_blue
= col
.Blue()/255.0;
723 m_alpha
= col
.Alpha()/255.0;
724 m_size
= font
.GetPointSize();
725 m_underlined
= font
.GetUnderlined();
728 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
729 #elif defined(__WXGTK__)
730 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
732 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
733 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
734 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
738 wxCairoFontData::~wxCairoFontData()
741 cairo_font_face_destroy( m_font
);
742 #elif defined(__WXGTK__)
743 pango_font_description_free( m_font
);
748 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
750 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
751 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
753 // the rest is done using Pango layouts
754 #elif defined(__WXMAC__)
755 cairo_set_font_face(ctext
, m_font
);
756 cairo_set_font_size(ctext
, m_size
);
758 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
759 cairo_set_font_size(ctext
, m_size
);
763 //-----------------------------------------------------------------------------
764 // wxCairoPathData implementation
765 //-----------------------------------------------------------------------------
767 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
768 : wxGraphicsPathData(renderer
)
772 m_pathContext
= pathcontext
;
776 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
777 m_pathContext
= cairo_create(surface
);
778 cairo_surface_destroy (surface
);
782 wxCairoPathData::~wxCairoPathData()
784 cairo_destroy(m_pathContext
);
787 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
789 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
790 cairo_t
* pathcontext
= cairo_create(surface
);
791 cairo_surface_destroy (surface
);
793 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
794 cairo_append_path(pathcontext
, path
);
795 cairo_path_destroy(path
);
796 return new wxCairoPathData( GetRenderer() ,pathcontext
);
800 void* wxCairoPathData::GetNativePath() const
802 return cairo_copy_path(m_pathContext
) ;
805 void wxCairoPathData::UnGetNativePath(void *p
) const
807 cairo_path_destroy((cairo_path_t
*)p
);
814 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
816 cairo_move_to(m_pathContext
,x
,y
);
819 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
821 cairo_line_to(m_pathContext
,x
,y
);
824 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
826 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
827 cairo_append_path(m_pathContext
, p
);
831 void wxCairoPathData::CloseSubpath()
833 cairo_close_path(m_pathContext
);
836 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
838 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
841 // gets the last point of the current path, (0,0) if not yet set
842 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
845 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
852 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
854 // as clockwise means positive in our system (y pointing downwards)
855 // TODO make this interpretation dependent of the
857 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
858 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
860 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
863 // transforms each point of this path by the matrix
864 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
866 // as we don't have a true path object, we have to apply the inverse
867 // matrix to the context
868 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
869 cairo_matrix_invert( &m
);
870 cairo_transform(m_pathContext
,&m
);
873 // gets the bounding box enclosing all points (possibly including control points)
874 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
878 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
902 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode
WXUNUSED(fillStyle
) ) const
904 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
907 //-----------------------------------------------------------------------------
908 // wxCairoMatrixData implementation
909 //-----------------------------------------------------------------------------
911 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
912 : wxGraphicsMatrixData(renderer
)
918 wxCairoMatrixData::~wxCairoMatrixData()
923 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
925 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
928 // concatenates the matrix
929 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
931 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
934 // sets the matrix to the respective values
935 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
936 wxDouble tx
, wxDouble ty
)
938 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
941 // gets the component valuess of the matrix
942 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
943 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
945 if (a
) *a
= m_matrix
.xx
;
946 if (b
) *b
= m_matrix
.yx
;
947 if (c
) *c
= m_matrix
.xy
;
948 if (d
) *d
= m_matrix
.yy
;
949 if (tx
) *tx
= m_matrix
.x0
;
950 if (ty
) *ty
= m_matrix
.y0
;
953 // makes this the inverse matrix
954 void wxCairoMatrixData::Invert()
956 cairo_matrix_invert( &m_matrix
);
959 // returns true if the elements of the transformation matrix are equal ?
960 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
962 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
964 m_matrix
.xx
== tm
->xx
&&
965 m_matrix
.yx
== tm
->yx
&&
966 m_matrix
.xy
== tm
->xy
&&
967 m_matrix
.yy
== tm
->yy
&&
968 m_matrix
.x0
== tm
->x0
&&
969 m_matrix
.y0
== tm
->y0
) ;
972 // return true if this is the identity matrix
973 bool wxCairoMatrixData::IsIdentity() const
975 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
976 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
983 // add the translation to this matrix
984 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
986 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
989 // add the scale to this matrix
990 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
992 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
995 // add the rotation to this matrix (radians)
996 void wxCairoMatrixData::Rotate( wxDouble angle
)
998 cairo_matrix_rotate( &m_matrix
, angle
) ;
1002 // apply the transforms
1005 // applies that matrix to the point
1006 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1008 double lx
= *x
, ly
= *y
;
1009 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1014 // applies the matrix except for translations
1015 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1017 double lx
= *dx
, ly
= *dy
;
1018 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1023 // returns the native representation
1024 void * wxCairoMatrixData::GetNativeMatrix() const
1026 return (void*) &m_matrix
;
1029 // wxCairoBitmap implementation
1030 //-----------------------------------------------------------------------------
1032 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1033 wxGraphicsObjectRefData( renderer
)
1036 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1039 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1041 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1043 int bw
= bmp
.GetWidth();
1044 int bh
= bmp
.GetHeight();
1045 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1046 m_buffer
= new unsigned char[bw
*bh
*4];
1047 wxUint32
* data
= (wxUint32
*)m_buffer
;
1049 // Create a surface object and copy the bitmap pixel data to it. if the
1050 // image has alpha (or a mask represented as alpha) then we'll use a
1051 // different format and iterator than if it doesn't...
1052 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1054 m_surface
= cairo_image_surface_create_for_data(
1055 m_buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1056 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1057 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1059 wxAlphaPixelData::Iterator
p(pixData
);
1060 for (int y
=0; y
<bh
; y
++)
1062 wxAlphaPixelData::Iterator rowStart
= p
;
1063 for (int x
=0; x
<bw
; x
++)
1065 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1066 // with alpha in the upper 8 bits, then red, then green, then
1067 // blue. The 32-bit quantities are stored native-endian.
1068 // Pre-multiplied alpha is used.
1069 unsigned char alpha
= p
.Alpha();
1073 *data
= ( alpha
<< 24
1074 | (p
.Red() * alpha
/255) << 16
1075 | (p
.Green() * alpha
/255) << 8
1076 | (p
.Blue() * alpha
/255) );
1081 p
.OffsetY(pixData
, 1);
1086 m_surface
= cairo_image_surface_create_for_data(
1087 m_buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1088 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1089 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1091 wxNativePixelData::Iterator
p(pixData
);
1092 for (int y
=0; y
<bh
; y
++)
1094 wxNativePixelData::Iterator rowStart
= p
;
1095 for (int x
=0; x
<bw
; x
++)
1097 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1098 // the upper 8 bits unused. Red, Green, and Blue are stored in
1099 // the remaining 24 bits in that order. The 32-bit quantities
1100 // are stored native-endian.
1101 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1106 p
.OffsetY(pixData
, 1);
1109 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1112 wxCairoBitmapData::~wxCairoBitmapData()
1115 cairo_pattern_destroy(m_pattern
);
1118 cairo_surface_destroy(m_surface
);
1125 //-----------------------------------------------------------------------------
1126 // wxCairoContext implementation
1127 //-----------------------------------------------------------------------------
1129 class wxCairoOffsetHelper
1132 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1137 cairo_translate( m_ctx
, 0.5, 0.5 );
1139 ~wxCairoOffsetHelper( )
1142 cairo_translate( m_ctx
, -0.5, -0.5 );
1149 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1150 : wxGraphicsContext(renderer
)
1153 const wxDCImpl
*impl
= dc
.GetImpl();
1154 Init( (cairo_t
*) impl
->GetCairoContext() );
1156 wxPoint org
= dc
.GetDeviceOrigin();
1157 cairo_translate( m_context
, org
.x
, org
.y
);
1160 dc
.GetUserScale( &sx
, &sy
);
1161 cairo_scale( m_context
, sx
, sy
);
1163 org
= dc
.GetLogicalOrigin();
1164 cairo_translate( m_context
, -org
.x
, -org
.y
);
1168 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1169 : wxGraphicsContext(renderer
)
1172 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1173 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1176 wxGraphicsMatrix matrix
= CreateMatrix();
1178 wxPoint org
= dc
.GetDeviceOrigin();
1179 matrix
.Translate( org
.x
, org
.y
);
1181 org
= dc
.GetLogicalOrigin();
1182 matrix
.Translate( -org
.x
, -org
.y
);
1185 dc
.GetUserScale( &sx
, &sy
);
1186 matrix
.Scale( sx
, sy
);
1188 ConcatTransform( matrix
);
1194 dc
.GetSize( &width
, &height
);
1195 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1196 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1197 Init( cairo_create( surface
) );
1198 cairo_surface_destroy( surface
);
1202 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1203 : wxGraphicsContext(renderer
)
1206 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1207 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1210 wxGraphicsMatrix matrix
= CreateMatrix();
1212 wxPoint org
= dc
.GetDeviceOrigin();
1213 matrix
.Translate( org
.x
, org
.y
);
1215 org
= dc
.GetLogicalOrigin();
1216 matrix
.Translate( -org
.x
, -org
.y
);
1219 dc
.GetUserScale( &sx
, &sy
);
1220 matrix
.Scale( sx
, sy
);
1222 ConcatTransform( matrix
);
1228 dc
.GetSize( &width
, &height
);
1229 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1230 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1231 Init( cairo_create( surface
) );
1232 cairo_surface_destroy( surface
);
1237 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1238 : wxGraphicsContext(renderer
)
1240 Init( gdk_cairo_create( drawable
) );
1245 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1246 : wxGraphicsContext(renderer
)
1248 m_mswSurface
= cairo_win32_surface_create(handle
);
1249 m_context
= cairo_create(m_mswSurface
);
1256 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1257 : wxGraphicsContext(renderer
)
1262 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1263 : wxGraphicsContext(renderer
)
1266 // something along these lines (copied from dcclient)
1268 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1269 // code should still be able to create wxClientDCs for them, so we will
1270 // use the parent window here then.
1271 if (window
->m_wxwindow
== NULL
)
1273 window
= window
->GetParent();
1276 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1278 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1282 wxCairoContext::~wxCairoContext()
1288 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1289 m_context
= cairo_create(m_mswSurface
);
1292 cairo_destroy(m_context
);
1296 cairo_surface_destroy(m_mswSurface
);
1300 void wxCairoContext::Init(cairo_t
*context
)
1302 m_context
= context
;
1308 void wxCairoContext::Clip( const wxRegion
& region
)
1310 // Create a path with all the rectangles in the region
1311 wxGraphicsPath path
= GetRenderer()->CreatePath();
1312 wxRegionIterator
ri(region
);
1315 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1319 // Put it in the context
1320 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1321 cairo_append_path(m_context
, cp
);
1323 // clip to that path
1324 cairo_clip(m_context
);
1325 path
.UnGetNativePath(cp
);
1328 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1330 // Create a path with this rectangle
1331 wxGraphicsPath path
= GetRenderer()->CreatePath();
1332 path
.AddRectangle(x
,y
,w
,h
);
1334 // Put it in the context
1335 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1336 cairo_append_path(m_context
, cp
);
1338 // clip to that path
1339 cairo_clip(m_context
);
1340 path
.UnGetNativePath(cp
);
1343 void wxCairoContext::ResetClip()
1345 cairo_reset_clip(m_context
);
1349 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1351 if ( !m_pen
.IsNull() )
1353 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1354 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1355 cairo_append_path(m_context
,cp
);
1356 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1357 cairo_stroke(m_context
);
1358 path
.UnGetNativePath(cp
);
1362 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1364 if ( !m_brush
.IsNull() )
1366 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1367 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1368 cairo_append_path(m_context
,cp
);
1369 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1370 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1371 cairo_fill(m_context
);
1372 path
.UnGetNativePath(cp
);
1376 void wxCairoContext::Rotate( wxDouble angle
)
1378 cairo_rotate(m_context
,angle
);
1381 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1383 cairo_translate(m_context
,dx
,dy
);
1386 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1388 cairo_scale(m_context
,xScale
,yScale
);
1391 // concatenates this transform with the current transform of this context
1392 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1394 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1397 // sets the transform of this context
1398 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1400 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1403 // gets the matrix of this context
1404 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1406 wxGraphicsMatrix matrix
= CreateMatrix();
1407 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1413 void wxCairoContext::PushState()
1415 cairo_save(m_context
);
1418 void wxCairoContext::PopState()
1420 cairo_restore(m_context
);
1423 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1425 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1426 DrawBitmap(bitmap
, x
, y
, w
, h
);
1430 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1434 // In case we're scaling the image by using a width and height different
1435 // than the bitmap's size create a pattern transformation on the surface and
1436 // draw the transformed pattern.
1437 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1438 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1439 wxSize size
= data
->GetSize();
1441 wxDouble scaleX
= w
/ size
.GetWidth();
1442 wxDouble scaleY
= h
/ size
.GetHeight();
1443 cairo_scale(m_context
, scaleX
, scaleY
);
1445 // prepare to draw the image
1446 cairo_translate(m_context
, x
, y
);
1447 cairo_set_source(m_context
, pattern
);
1448 // use the original size here since the context is scaled already...
1449 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1450 // fill the rectangle using the pattern
1451 cairo_fill(m_context
);
1456 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1458 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1459 // start using the Cairo backend on other platforms then we may need to
1460 // fiddle with this...
1461 DrawBitmap(icon
, x
, y
, w
, h
);
1465 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1467 wxCHECK_RET( !m_font
.IsNull(),
1468 wxT("wxCairoContext::DrawText - no valid font set") );
1473 const wxCharBuffer data
= str
.utf8_str();
1477 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1480 size_t datalen
= strlen(data
);
1482 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1483 wxCairoFontData
* font_data
= (wxCairoFontData
*) m_font
.GetRefData();
1484 pango_layout_set_font_description( layout
, font_data
->GetFont());
1485 pango_layout_set_text(layout
, data
, datalen
);
1487 if (font_data
->GetUnderlined())
1489 PangoAttrList
*attrs
= pango_attr_list_new();
1490 PangoAttribute
*attr
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
1491 pango_attr_list_insert(attrs
, attr
);
1492 pango_layout_set_attributes(layout
, attrs
);
1493 pango_attr_list_unref(attrs
);
1496 cairo_move_to(m_context
, x
, y
);
1497 pango_cairo_show_layout (m_context
, layout
);
1499 g_object_unref (layout
);
1501 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1502 // the position we move to by the ascent.
1503 cairo_font_extents_t fe
;
1504 cairo_font_extents(m_context
, &fe
);
1505 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1507 cairo_show_text(m_context
, data
);
1511 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1512 wxDouble
*descent
, wxDouble
*externalLeading
) const
1514 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1522 if ( externalLeading
)
1523 *externalLeading
= 0;
1531 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1532 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1533 const wxCharBuffer data
= str
.utf8_str();
1538 pango_layout_set_text( layout
, data
, strlen(data
) );
1539 pango_layout_get_pixel_size (layout
, &w
, &h
);
1546 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1547 int baseline
= pango_layout_iter_get_baseline(iter
);
1548 pango_layout_iter_free(iter
);
1549 *descent
= h
- PANGO_PIXELS(baseline
);
1551 g_object_unref (layout
);
1553 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1557 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1558 cairo_text_extents_t te
;
1559 cairo_text_extents(m_context
, buf
, &te
);
1563 if (height
|| descent
|| externalLeading
)
1565 cairo_font_extents_t fe
;
1566 cairo_font_extents(m_context
, &fe
);
1568 // some backends have negative descents
1570 if ( fe
.descent
< 0 )
1571 fe
.descent
= -fe
.descent
;
1573 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1575 // some backends are broken re height ... (eg currently ATSUI)
1576 fe
.height
= fe
.ascent
+ fe
.descent
;
1580 *height
= fe
.height
;
1582 *descent
= fe
.descent
;
1583 if ( externalLeading
)
1584 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1589 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1592 widths
.Add(0, text
.length());
1594 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1602 void * wxCairoContext::GetNativeContext()
1607 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
1609 if (m_antialias
== antialias
)
1612 m_antialias
= antialias
;
1614 cairo_antialias_t antialiasMode
;
1617 case wxANTIALIAS_DEFAULT
:
1618 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
1620 case wxANTIALIAS_NONE
:
1621 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
1626 cairo_set_antialias(m_context
, antialiasMode
);
1630 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
1632 if ( m_composition
== op
)
1636 cairo_operator_t cop
;
1639 case wxCOMPOSITION_CLEAR
:
1640 cop
= CAIRO_OPERATOR_CLEAR
;
1642 case wxCOMPOSITION_SOURCE
:
1643 cop
= CAIRO_OPERATOR_SOURCE
;
1645 case wxCOMPOSITION_OVER
:
1646 cop
= CAIRO_OPERATOR_OVER
;
1648 case wxCOMPOSITION_IN
:
1649 cop
= CAIRO_OPERATOR_IN
;
1651 case wxCOMPOSITION_OUT
:
1652 cop
= CAIRO_OPERATOR_OUT
;
1654 case wxCOMPOSITION_ATOP
:
1655 cop
= CAIRO_OPERATOR_ATOP
;
1657 case wxCOMPOSITION_DEST
:
1658 cop
= CAIRO_OPERATOR_DEST
;
1660 case wxCOMPOSITION_DEST_OVER
:
1661 cop
= CAIRO_OPERATOR_DEST_OVER
;
1663 case wxCOMPOSITION_DEST_IN
:
1664 cop
= CAIRO_OPERATOR_DEST_IN
;
1666 case wxCOMPOSITION_DEST_OUT
:
1667 cop
= CAIRO_OPERATOR_DEST_OUT
;
1669 case wxCOMPOSITION_DEST_ATOP
:
1670 cop
= CAIRO_OPERATOR_DEST_ATOP
;
1672 case wxCOMPOSITION_XOR
:
1673 cop
= CAIRO_OPERATOR_XOR
;
1675 case wxCOMPOSITION_ADD
:
1676 cop
= CAIRO_OPERATOR_ADD
;
1681 cairo_set_operator(m_context
, cop
);
1685 void wxCairoContext::BeginLayer(wxDouble opacity
)
1687 m_layerOpacities
.push_back(opacity
);
1688 cairo_push_group(m_context
);
1691 void wxCairoContext::EndLayer()
1693 float opacity
= m_layerOpacities
.back();
1694 m_layerOpacities
.pop_back();
1695 cairo_pop_group_to_source(m_context
);
1696 cairo_paint_with_alpha(m_context
,opacity
);
1699 //-----------------------------------------------------------------------------
1700 // wxCairoRenderer declaration
1701 //-----------------------------------------------------------------------------
1703 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1706 wxCairoRenderer() {}
1708 virtual ~wxCairoRenderer() {}
1712 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1713 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1714 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1716 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1718 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1720 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1722 virtual wxGraphicsContext
* CreateMeasuringContext();
1726 virtual wxGraphicsPath
CreatePath();
1730 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1731 wxDouble tx
=0.0, wxDouble ty
=0.0);
1734 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1736 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1738 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1739 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1740 const wxColour
&c1
, const wxColour
&c2
) ;
1742 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1743 // with radius r and color cColor
1744 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1745 const wxColour
&oColor
, const wxColour
&cColor
) ;
1748 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1750 // create a native bitmap representation
1751 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1753 // create a graphics bitmap from a native bitmap
1754 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1756 // create a subimage from a native image representation
1757 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1760 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1763 //-----------------------------------------------------------------------------
1764 // wxCairoRenderer implementation
1765 //-----------------------------------------------------------------------------
1767 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1769 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1770 // temporary hack to allow creating a cairo context on any platform
1771 extern wxGraphicsRenderer
* gCairoRenderer
;
1772 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1775 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1777 return &gs_cairoGraphicsRenderer
;
1781 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1783 return new wxCairoContext(this,dc
);
1786 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1788 return new wxCairoContext(this,dc
);
1791 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1794 const wxDCImpl
*impl
= dc
.GetImpl();
1795 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1797 return new wxCairoContext(this,dc
);
1803 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1806 return new wxCairoContext(this,(HDC
)context
);
1809 return new wxCairoContext(this,(cairo_t
*)context
);
1814 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1817 return new wxCairoContext(this,(GdkDrawable
*)window
);
1823 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1826 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1832 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1834 return new wxCairoContext(this, window
);
1839 wxGraphicsPath
wxCairoRenderer::CreatePath()
1841 wxGraphicsPath path
;
1842 path
.SetRefData( new wxCairoPathData(this) );
1849 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1850 wxDouble tx
, wxDouble ty
)
1854 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1855 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1860 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1862 if ( !pen
.Ok() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
1863 return wxNullGraphicsPen
;
1867 p
.SetRefData(new wxCairoPenData( this, pen
));
1872 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1874 if ( !brush
.Ok() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
1875 return wxNullGraphicsBrush
;
1879 p
.SetRefData(new wxCairoBrushData( this, brush
));
1884 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1885 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1886 const wxColour
&c1
, const wxColour
&c2
)
1889 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1890 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1895 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1896 // with radius r and color cColor
1897 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1898 const wxColour
&oColor
, const wxColour
&cColor
)
1901 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1902 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1908 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1913 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1917 return wxNullGraphicsFont
;
1920 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
1925 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
1929 return wxNullGraphicsBitmap
;
1932 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
1934 if ( bitmap
!= NULL
)
1937 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
1941 return wxNullGraphicsBitmap
;
1945 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
1946 wxDouble
WXUNUSED(x
),
1947 wxDouble
WXUNUSED(y
),
1948 wxDouble
WXUNUSED(w
),
1949 wxDouble
WXUNUSED(h
))
1951 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
1952 return wxNullGraphicsBitmap
;
1955 #endif // wxUSE_CAIRO
1957 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
1960 return &gs_cairoGraphicsRenderer
;
1966 #endif // wxUSE_GRAPHICS_CONTEXT