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"
24 // keep cairo.h from defining dllimport as we're defining the symbols inside
25 // the wx dll in order to load them dynamically.
31 #include "wx/bitmap.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcmemory.h"
35 #include "wx/dcprint.h"
36 #include "wx/window.h"
39 #include "wx/private/graphics.h"
40 #include "wx/rawbmp.h"
41 #include "wx/vector.h"
45 //-----------------------------------------------------------------------------
46 // device context implementation
48 // more and more of the dc functionality should be implemented by calling
49 // the appropricate wxCairoContext, but we will have to do that step by step
50 // also coordinate conversions should be moved to native matrix ops
51 //-----------------------------------------------------------------------------
53 // we always stock two context states, one at entry, to be able to preserve the
54 // state we were called with, the other one after changing to HI Graphics orientation
55 // (this one is used for getting back clippings etc)
57 //-----------------------------------------------------------------------------
58 // wxGraphicsPath implementation
59 //-----------------------------------------------------------------------------
61 // TODO remove this dependency (gdiplus needs the macros)
64 #define max(a,b) (((a) > (b)) ? (a) : (b))
68 #define min(a,b) (((a) < (b)) ? (a) : (b))
73 #include <cairo-win32.h>
78 #include "wx/fontutil.h"
79 #include "wx/gtk/dc.h"
83 #include <cairo-win32.h>
87 #include "wx/osx/private.h"
88 #include <cairo-quartz.h>
89 #include <cairo-atsui.h>
92 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
95 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
98 virtual wxGraphicsObjectRefData
*Clone() const;
101 // These are the path primitives from which everything else can be constructed
104 // begins a new subpath at (x,y)
105 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
107 // adds a straight line from the current point to (x,y)
108 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
110 // adds a cubic Bezier curve from the current point, using two control points and an end point
111 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
114 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
115 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
117 // gets the last point of the current path, (0,0) if not yet set
118 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
121 virtual void AddPath( const wxGraphicsPathData
* path
);
123 // closes the current sub-path
124 virtual void CloseSubpath();
127 // These are convenience functions which - if not available natively will be assembled
128 // using the primitives from above
133 // appends a rectangle as a new closed subpath
134 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
135 // appends an ellipsis as a new closed subpath fitting the passed rectangle
136 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
138 // 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)
139 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
142 // returns the native path
143 virtual void * GetNativePath() const ;
145 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
146 virtual void UnGetNativePath(void *p
) const;
148 // transforms each point of this path by the matrix
149 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
151 // gets the bounding box enclosing all points (possibly including control points)
152 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
154 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
157 cairo_t
* m_pathContext
;
160 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
163 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
164 virtual ~wxCairoMatrixData() ;
166 virtual wxGraphicsObjectRefData
*Clone() const ;
168 // concatenates the matrix
169 virtual void Concat( const wxGraphicsMatrixData
*t
);
171 // sets the matrix to the respective values
172 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
173 wxDouble tx
=0.0, wxDouble ty
=0.0);
175 // gets the component valuess of the matrix
176 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
177 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
179 // makes this the inverse matrix
180 virtual void Invert();
182 // returns true if the elements of the transformation matrix are equal ?
183 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
185 // return true if this is the identity matrix
186 virtual bool IsIdentity() const;
192 // add the translation to this matrix
193 virtual void Translate( wxDouble dx
, wxDouble dy
);
195 // add the scale to this matrix
196 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
198 // add the rotation to this matrix (radians)
199 virtual void Rotate( wxDouble angle
);
202 // apply the transforms
205 // applies that matrix to the point
206 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
208 // applies the matrix except for translations
209 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
211 // returns the native representation
212 virtual void * GetNativeMatrix() const;
214 cairo_matrix_t m_matrix
;
217 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
220 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
225 virtual void Apply( wxGraphicsContext
* context
);
226 virtual wxDouble
GetWidth() { return m_width
; }
236 cairo_line_cap_t m_cap
;
237 cairo_line_join_t m_join
;
240 const double *m_lengths
;
241 double *m_userLengths
;
245 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
248 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
251 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
252 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
253 ~wxCairoBrushData ();
255 virtual void Apply( wxGraphicsContext
* context
);
257 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
258 wxDouble x2
, wxDouble y2
,
259 const wxGraphicsGradientStops
& stops
);
260 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
261 wxDouble xc
, wxDouble yc
, wxDouble radius
,
262 const wxGraphicsGradientStops
& stops
);
267 // common part of Create{Linear,Radial}GradientBrush()
268 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
276 cairo_pattern_t
* m_brushPattern
;
279 class wxCairoFontData
: public wxGraphicsObjectRefData
282 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
283 wxCairoFontData(wxGraphicsRenderer
* renderer
,
285 const wxString
& facename
,
287 const wxColour
& col
);
290 virtual bool Apply( wxGraphicsContext
* context
);
292 const wxFont
& GetFont() const { return m_wxfont
; }
295 void InitColour(const wxColour
& col
);
296 void InitFontComponents(const wxString
& facename
,
297 cairo_font_slant_t slant
,
298 cairo_font_weight_t weight
);
306 cairo_font_face_t
*m_font
;
307 #elif defined(__WXGTK__)
311 // These members are used when the font is created from its face name and
312 // flags (and not from wxFont) and also even when creating it from wxFont
313 // on the platforms not covered above.
315 // Notice that we can't use cairo_font_face_t instead of storing those,
316 // even though it would be simpler and need less #ifdefs, because
317 // cairo_toy_font_face_create() that we'd need to create it is only
318 // available in Cairo 1.8 and we require just 1.2 currently. If we do drop
319 // support for < 1.8 versions in the future it would be definitely better
320 // to use cairo_toy_font_face_create() instead.
321 wxCharBuffer m_fontName
;
322 cairo_font_slant_t m_slant
;
323 cairo_font_weight_t m_weight
;
326 class wxCairoBitmapData
: public wxGraphicsObjectRefData
329 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
331 wxCairoBitmapData(wxGraphicsRenderer
* renderer
, const wxImage
& image
);
332 #endif // wxUSE_IMAGE
333 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
334 ~wxCairoBitmapData();
336 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
337 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
338 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
341 wxImage
ConvertToImage() const;
342 #endif // wxUSE_IMAGE
345 // Allocate m_buffer for the bitmap of the given size in the given format.
347 // Returns the stride used for the buffer.
348 int InitBuffer(int width
, int height
, cairo_format_t format
);
350 // Really create the surface using the buffer (which was supposed to be
351 // filled since InitBuffer() call).
352 void InitSurface(cairo_format_t format
, int stride
);
355 cairo_surface_t
* m_surface
;
356 cairo_pattern_t
* m_pattern
;
359 unsigned char* m_buffer
;
362 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
365 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
366 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
367 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
369 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
372 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
374 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
375 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
377 // If this ctor is used, Init() must be called by the derived class later.
378 wxCairoContext( wxGraphicsRenderer
* renderer
);
380 virtual ~wxCairoContext();
382 virtual bool ShouldOffset() const
384 if ( !m_enableOffset
)
388 if ( !m_pen
.IsNull() )
390 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
394 return ( penwidth
% 2 ) == 1;
397 virtual void Clip( const wxRegion
®ion
);
399 cairo_surface_t
* m_mswSurface
;
402 // clips drawings to the rect
403 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
405 // resets the clipping to original extent
406 virtual void ResetClip();
408 virtual void * GetNativeContext();
410 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
412 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
414 virtual bool SetCompositionMode(wxCompositionMode op
);
416 virtual void BeginLayer(wxDouble opacity
);
418 virtual void EndLayer();
420 virtual void StrokePath( const wxGraphicsPath
& p
);
421 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
423 virtual void Translate( wxDouble dx
, wxDouble dy
);
424 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
425 virtual void Rotate( wxDouble angle
);
427 // concatenates this transform with the current transform of this context
428 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
430 // sets the transform of this context
431 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
433 // gets the matrix of this context
434 virtual wxGraphicsMatrix
GetTransform() const;
436 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
437 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
438 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
439 virtual void PushState();
440 virtual void PopState();
442 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
443 wxDouble
*descent
, wxDouble
*externalLeading
) const;
444 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
447 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
449 void Init(cairo_t
*context
);
454 wxVector
<float> m_layerOpacities
;
456 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
460 // ----------------------------------------------------------------------------
461 // wxCairoImageContext: context associated with a wxImage.
462 // ----------------------------------------------------------------------------
464 class wxCairoImageContext
: public wxCairoContext
467 wxCairoImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
468 wxCairoContext(renderer
),
470 m_data(renderer
, image
)
472 Init(cairo_create(m_data
.GetCairoSurface()));
475 virtual ~wxCairoImageContext()
477 m_image
= m_data
.ConvertToImage();
482 wxCairoBitmapData m_data
;
484 wxDECLARE_NO_COPY_CLASS(wxCairoImageContext
);
486 #endif // wxUSE_IMAGE
488 //-----------------------------------------------------------------------------
489 // wxCairoPenData implementation
490 //-----------------------------------------------------------------------------
492 wxCairoPenData::~wxCairoPenData()
494 delete[] m_userLengths
;
497 void wxCairoPenData::Init()
500 m_userLengths
= NULL
;
505 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
506 : wxGraphicsObjectRefData(renderer
)
510 m_width
= m_pen
.GetWidth();
514 m_red
= m_pen
.GetColour().Red()/255.0;
515 m_green
= m_pen
.GetColour().Green()/255.0;
516 m_blue
= m_pen
.GetColour().Blue()/255.0;
517 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
519 switch ( m_pen
.GetCap() )
522 m_cap
= CAIRO_LINE_CAP_ROUND
;
525 case wxCAP_PROJECTING
:
526 m_cap
= CAIRO_LINE_CAP_SQUARE
;
530 m_cap
= CAIRO_LINE_CAP_BUTT
;
534 m_cap
= CAIRO_LINE_CAP_BUTT
;
538 switch ( m_pen
.GetJoin() )
541 m_join
= CAIRO_LINE_JOIN_BEVEL
;
545 m_join
= CAIRO_LINE_JOIN_MITER
;
549 m_join
= CAIRO_LINE_JOIN_ROUND
;
553 m_join
= CAIRO_LINE_JOIN_MITER
;
557 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
558 const double dotted
[] =
560 dashUnit
, dashUnit
+ 2.0
562 static const double short_dashed
[] =
566 static const double dashed
[] =
570 static const double dotted_dashed
[] =
572 9.0 , 6.0 , 3.0 , 3.0
575 switch ( m_pen
.GetStyle() )
577 case wxPENSTYLE_SOLID
:
580 case wxPENSTYLE_DOT
:
581 m_count
= WXSIZEOF(dotted
);
582 m_userLengths
= new double[ m_count
] ;
583 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
584 m_lengths
= m_userLengths
;
587 case wxPENSTYLE_LONG_DASH
:
589 m_count
= WXSIZEOF(dashed
);
592 case wxPENSTYLE_SHORT_DASH
:
593 m_lengths
= short_dashed
;
594 m_count
= WXSIZEOF(short_dashed
);
597 case wxPENSTYLE_DOT_DASH
:
598 m_lengths
= dotted_dashed
;
599 m_count
= WXSIZEOF(dotted_dashed
);
602 case wxPENSTYLE_USER_DASH
:
605 m_count
= m_pen
.GetDashes( &wxdashes
) ;
606 if ((wxdashes
!= NULL
) && (m_count
> 0))
608 m_userLengths
= new double[m_count
] ;
609 for ( int i
= 0 ; i
< m_count
; ++i
)
611 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
613 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
614 m_userLengths
[i
] = dashUnit
+ 2.0 ;
615 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
616 m_userLengths
[i
] = dashUnit
;
619 m_lengths
= m_userLengths
;
622 case wxPENSTYLE_STIPPLE
:
625 wxBitmap* bmp = pen.GetStipple();
626 if ( bmp && bmp->IsOk() )
628 wxDELETE( m_penImage );
629 wxDELETE( m_penBrush );
630 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
631 m_penBrush = new TextureBrush(m_penImage);
632 m_pen->SetBrush( m_penBrush );
638 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
639 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
642 wxDELETE( m_penBrush );
643 HatchStyle style = HatchStyleHorizontal;
644 switch( pen.GetStyle() )
646 case wxPENSTYLE_BDIAGONAL_HATCH :
647 style = HatchStyleBackwardDiagonal;
649 case wxPENSTYLE_CROSSDIAG_HATCH :
650 style = HatchStyleDiagonalCross;
652 case wxPENSTYLE_FDIAGONAL_HATCH :
653 style = HatchStyleForwardDiagonal;
655 case wxPENSTYLE_CROSS_HATCH :
656 style = HatchStyleCross;
658 case wxPENSTYLE_HORIZONTAL_HATCH :
659 style = HatchStyleHorizontal;
661 case wxPENSTYLE_VERTICAL_HATCH :
662 style = HatchStyleVertical;
666 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
667 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
668 m_pen->SetBrush( m_penBrush )
675 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
677 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
678 cairo_set_line_width(ctext
,m_width
);
679 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
680 cairo_set_line_cap(ctext
,m_cap
);
681 cairo_set_line_join(ctext
,m_join
);
682 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
685 //-----------------------------------------------------------------------------
686 // wxCairoBrushData implementation
687 //-----------------------------------------------------------------------------
689 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
690 : wxGraphicsObjectRefData( renderer
)
695 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
696 : wxGraphicsObjectRefData(renderer
)
700 m_red
= brush
.GetColour().Red()/255.0;
701 m_green
= brush
.GetColour().Green()/255.0;
702 m_blue
= brush
.GetColour().Blue()/255.0;
703 m_alpha
= brush
.GetColour().Alpha()/255.0;
705 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
707 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
708 brush.GetColour().Green() , brush.GetColour().Blue() ) );
710 else if ( brush.IsHatch() )
712 HatchStyle style = HatchStyleHorizontal;
713 switch( brush.GetStyle() )
715 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
716 style = HatchStyleBackwardDiagonal;
718 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
719 style = HatchStyleDiagonalCross;
721 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
722 style = HatchStyleForwardDiagonal;
724 case wxBRUSHSTYLE_CROSS_HATCH :
725 style = HatchStyleCross;
727 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
728 style = HatchStyleHorizontal;
730 case wxBRUSHSTYLE_VERTICAL_HATCH :
731 style = HatchStyleVertical;
735 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
736 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
740 wxBitmap* bmp = brush.GetStipple();
741 if ( bmp && bmp->IsOk() )
743 wxDELETE( m_brushImage );
744 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
745 m_brush = new TextureBrush(m_brushImage);
751 wxCairoBrushData::~wxCairoBrushData ()
754 cairo_pattern_destroy(m_brushPattern
);
757 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
759 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
760 if ( m_brushPattern
)
762 cairo_set_source(ctext
,m_brushPattern
);
766 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
770 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
772 // loop over all the stops, they include the beginning and ending ones
773 const unsigned numStops
= stops
.GetCount();
774 for ( unsigned n
= 0; n
< numStops
; n
++ )
776 const wxGraphicsGradientStop stop
= stops
.Item(n
);
778 const wxColour col
= stop
.GetColour();
780 cairo_pattern_add_color_stop_rgba
791 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
,
792 wxT("Couldn't create cairo pattern"));
796 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
797 wxDouble x2
, wxDouble y2
,
798 const wxGraphicsGradientStops
& stops
)
800 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
802 AddGradientStops(stops
);
806 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
807 wxDouble xc
, wxDouble yc
,
809 const wxGraphicsGradientStops
& stops
)
811 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
813 AddGradientStops(stops
);
816 void wxCairoBrushData::Init()
818 m_brushPattern
= NULL
;
821 //-----------------------------------------------------------------------------
822 // wxCairoFontData implementation
823 //-----------------------------------------------------------------------------
825 void wxCairoFontData::InitColour(const wxColour
& col
)
827 m_red
= col
.Red()/255.0;
828 m_green
= col
.Green()/255.0;
829 m_blue
= col
.Blue()/255.0;
830 m_alpha
= col
.Alpha()/255.0;
834 wxCairoFontData::InitFontComponents(const wxString
& facename
,
835 cairo_font_slant_t slant
,
836 cairo_font_weight_t weight
)
838 m_fontName
= facename
.mb_str(wxConvUTF8
);
843 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
844 const wxColour
& col
)
845 : wxGraphicsObjectRefData(renderer
)
852 m_size
= font
.GetPointSize();
855 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
856 #elif defined(__WXGTK__)
861 font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
862 : CAIRO_FONT_SLANT_NORMAL
,
863 font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
864 : CAIRO_FONT_WEIGHT_NORMAL
869 wxCairoFontData::wxCairoFontData(wxGraphicsRenderer
* renderer
,
871 const wxString
& facename
,
873 const wxColour
& col
) :
874 wxGraphicsObjectRefData(renderer
)
878 // Resolution for Cairo image surfaces is 72 DPI meaning that the sizes in
879 // points and pixels are identical, so we can just pass the size in pixels
880 // directly to cairo_set_font_size().
881 m_size
= sizeInPixels
;
883 #if defined(__WXMAC__)
887 // There is no need to set m_underlined under wxGTK in this case, it can
888 // only be used if m_font != NULL.
893 flags
& wxFONTFLAG_ITALIC
? CAIRO_FONT_SLANT_ITALIC
894 : CAIRO_FONT_SLANT_NORMAL
,
895 flags
& wxFONTFLAG_BOLD
? CAIRO_FONT_WEIGHT_BOLD
896 : CAIRO_FONT_WEIGHT_NORMAL
900 wxCairoFontData::~wxCairoFontData()
904 cairo_font_face_destroy( m_font
);
908 bool wxCairoFontData::Apply( wxGraphicsContext
* context
)
910 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
911 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
915 // Nothing to do, the caller uses Pango layout functions to do
919 #elif defined(__WXMAC__)
922 cairo_set_font_face(ctext
, m_font
);
923 cairo_set_font_size(ctext
, m_size
);
928 // If we get here, we must be on a platform without native font support or
929 // we're using toy Cairo API even under wxGTK/wxMac.
930 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
931 cairo_set_font_size(ctext
, m_size
);
933 // Indicate that we don't use native fonts for the platforms which care
934 // about this (currently only wxGTK).
938 //-----------------------------------------------------------------------------
939 // wxCairoPathData implementation
940 //-----------------------------------------------------------------------------
942 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
943 : wxGraphicsPathData(renderer
)
947 m_pathContext
= pathcontext
;
951 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
952 m_pathContext
= cairo_create(surface
);
953 cairo_surface_destroy (surface
);
957 wxCairoPathData::~wxCairoPathData()
959 cairo_destroy(m_pathContext
);
962 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
964 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
965 cairo_t
* pathcontext
= cairo_create(surface
);
966 cairo_surface_destroy (surface
);
968 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
969 cairo_append_path(pathcontext
, path
);
970 cairo_path_destroy(path
);
971 return new wxCairoPathData( GetRenderer() ,pathcontext
);
975 void* wxCairoPathData::GetNativePath() const
977 return cairo_copy_path(m_pathContext
) ;
980 void wxCairoPathData::UnGetNativePath(void *p
) const
982 cairo_path_destroy((cairo_path_t
*)p
);
989 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
991 cairo_move_to(m_pathContext
,x
,y
);
994 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
996 cairo_line_to(m_pathContext
,x
,y
);
999 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
1001 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
1002 cairo_append_path(m_pathContext
, p
);
1006 void wxCairoPathData::CloseSubpath()
1008 cairo_close_path(m_pathContext
);
1011 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1013 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
1016 // gets the last point of the current path, (0,0) if not yet set
1017 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1020 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
1027 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1029 // as clockwise means positive in our system (y pointing downwards)
1030 // TODO make this interpretation dependent of the
1031 // real device trans
1032 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
1033 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1035 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1038 // transforms each point of this path by the matrix
1039 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1041 // as we don't have a true path object, we have to apply the inverse
1042 // matrix to the context
1043 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
1044 cairo_matrix_invert( &m
);
1045 cairo_transform(m_pathContext
,&m
);
1048 // gets the bounding box enclosing all points (possibly including control points)
1049 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1053 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
1077 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1079 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1080 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
1083 //-----------------------------------------------------------------------------
1084 // wxCairoMatrixData implementation
1085 //-----------------------------------------------------------------------------
1087 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
1088 : wxGraphicsMatrixData(renderer
)
1094 wxCairoMatrixData::~wxCairoMatrixData()
1099 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
1101 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
1104 // concatenates the matrix
1105 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1107 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
1110 // sets the matrix to the respective values
1111 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1112 wxDouble tx
, wxDouble ty
)
1114 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
1117 // gets the component valuess of the matrix
1118 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1119 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1121 if (a
) *a
= m_matrix
.xx
;
1122 if (b
) *b
= m_matrix
.yx
;
1123 if (c
) *c
= m_matrix
.xy
;
1124 if (d
) *d
= m_matrix
.yy
;
1125 if (tx
) *tx
= m_matrix
.x0
;
1126 if (ty
) *ty
= m_matrix
.y0
;
1129 // makes this the inverse matrix
1130 void wxCairoMatrixData::Invert()
1132 cairo_matrix_invert( &m_matrix
);
1135 // returns true if the elements of the transformation matrix are equal ?
1136 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1138 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1140 m_matrix
.xx
== tm
->xx
&&
1141 m_matrix
.yx
== tm
->yx
&&
1142 m_matrix
.xy
== tm
->xy
&&
1143 m_matrix
.yy
== tm
->yy
&&
1144 m_matrix
.x0
== tm
->x0
&&
1145 m_matrix
.y0
== tm
->y0
) ;
1148 // return true if this is the identity matrix
1149 bool wxCairoMatrixData::IsIdentity() const
1151 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1152 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1159 // add the translation to this matrix
1160 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1162 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1165 // add the scale to this matrix
1166 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1168 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1171 // add the rotation to this matrix (radians)
1172 void wxCairoMatrixData::Rotate( wxDouble angle
)
1174 cairo_matrix_rotate( &m_matrix
, angle
) ;
1178 // apply the transforms
1181 // applies that matrix to the point
1182 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1184 double lx
= *x
, ly
= *y
;
1185 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1190 // applies the matrix except for translations
1191 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1193 double lx
= *dx
, ly
= *dy
;
1194 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1199 // returns the native representation
1200 void * wxCairoMatrixData::GetNativeMatrix() const
1202 return (void*) &m_matrix
;
1205 // ----------------------------------------------------------------------------
1206 // wxCairoBitmap implementation
1207 // ----------------------------------------------------------------------------
1209 int wxCairoBitmapData::InitBuffer(int width
, int height
, cairo_format_t format
)
1211 wxUnusedVar(format
); // Only really unused with Cairo < 1.6.
1213 // Determine the stride: use cairo_format_stride_for_width() if available
1214 // but fall back to 4*width for the earlier versions as this is what that
1215 // function always returns, even in latest Cairo, anyhow.
1217 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1218 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1220 stride
= cairo_format_stride_for_width(format
, width
);
1222 // All our code would totally break if stride were not a multiple of 4
1223 // so ensure this is the case.
1226 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1228 stride
+= 4 - stride
% 4;
1237 m_buffer
= new unsigned char[height
*stride
];
1242 void wxCairoBitmapData::InitSurface(cairo_format_t format
, int stride
)
1244 m_surface
= cairo_image_surface_create_for_data(
1245 m_buffer
, format
, m_width
, m_height
, stride
);
1246 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1249 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1250 wxGraphicsObjectRefData( renderer
)
1253 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1256 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsObjectRefData( renderer
)
1258 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1260 #ifdef wxHAS_RAW_BITMAP
1261 // Create a surface object and copy the bitmap pixel data to it. if the
1262 // image has alpha (or a mask represented as alpha) then we'll use a
1263 // different format and iterator than if it doesn't...
1264 cairo_format_t bufferFormat
= bmp
.GetDepth() == 32
1268 ? CAIRO_FORMAT_ARGB32
1269 : CAIRO_FORMAT_RGB24
;
1271 int stride
= InitBuffer(bmp
.GetWidth(), bmp
.GetHeight(), bufferFormat
);
1273 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1274 wxUint32
* data
= (wxUint32
*)m_buffer
;
1276 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1278 // use the bitmap's alpha
1280 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1281 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1283 wxAlphaPixelData::Iterator
p(pixData
);
1284 for (int y
=0; y
<m_height
; y
++)
1286 wxAlphaPixelData::Iterator rowStart
= p
;
1287 wxUint32
* const rowStartDst
= data
;
1288 for (int x
=0; x
<m_width
; x
++)
1290 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1291 // with alpha in the upper 8 bits, then red, then green, then
1292 // blue. The 32-bit quantities are stored native-endian.
1293 // Pre-multiplied alpha is used.
1294 unsigned char alpha
= p
.Alpha();
1298 *data
= ( alpha
<< 24
1299 | (p
.Red() * alpha
/255) << 16
1300 | (p
.Green() * alpha
/255) << 8
1301 | (p
.Blue() * alpha
/255) );
1306 data
= rowStartDst
+ stride
/ 4;
1308 p
.OffsetY(pixData
, 1);
1314 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1315 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1317 wxNativePixelData::Iterator
p(pixData
);
1318 for (int y
=0; y
<m_height
; y
++)
1320 wxNativePixelData::Iterator rowStart
= p
;
1321 wxUint32
* const rowStartDst
= data
;
1322 for (int x
=0; x
<m_width
; x
++)
1324 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1325 // the upper 8 bits unused. Red, Green, and Blue are stored in
1326 // the remaining 24 bits in that order. The 32-bit quantities
1327 // are stored native-endian.
1328 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1333 data
= rowStartDst
+ stride
/ 4;
1335 p
.OffsetY(pixData
, 1);
1339 // if there is a mask, set the alpha bytes in the target buffer to
1340 // fully transparent or fully opaque
1341 if (bmpSource
.GetMask())
1343 wxBitmap bmpMask
= bmpSource
.GetMaskBitmap();
1344 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1345 data
= (wxUint32
*)m_buffer
;
1347 pixData(bmpMask
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1348 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to mask data."));
1350 wxNativePixelData::Iterator
p(pixData
);
1351 for (int y
=0; y
<m_height
; y
++)
1353 wxNativePixelData::Iterator rowStart
= p
;
1354 wxUint32
* const rowStartDst
= data
;
1355 for (int x
=0; x
<m_width
; x
++)
1357 if (p
.Red()+p
.Green()+p
.Blue() == 0)
1360 *data
= (wxALPHA_OPAQUE
<< 24) | (*data
& 0x00FFFFFF);
1365 data
= rowStartDst
+ stride
/ 4;
1367 p
.OffsetY(pixData
, 1);
1372 InitSurface(bufferFormat
, stride
);
1373 #endif // wxHAS_RAW_BITMAP
1378 // Helper functions for dealing with alpha pre-multiplication.
1382 inline unsigned char Premultiply(unsigned char alpha
, unsigned char data
)
1384 return alpha
? (data
* alpha
)/0xff : data
;
1387 inline unsigned char Unpremultiply(unsigned char alpha
, unsigned char data
)
1389 return alpha
? (data
* 0xff)/alpha
: data
;
1392 } // anonymous namespace
1394 wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer
* renderer
,
1395 const wxImage
& image
)
1396 : wxGraphicsObjectRefData(renderer
)
1398 const cairo_format_t bufferFormat
= image
.HasAlpha()
1399 ? CAIRO_FORMAT_ARGB32
1400 : CAIRO_FORMAT_RGB24
;
1402 int stride
= InitBuffer(image
.GetWidth(), image
.GetHeight(), bufferFormat
);
1404 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1405 // values and not bytes becase Cairo always works with buffers in native
1407 wxUint32
* dst
= reinterpret_cast<wxUint32
*>(m_buffer
);
1408 const unsigned char* src
= image
.GetData();
1410 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1412 const unsigned char* alpha
= image
.GetAlpha();
1414 for ( int y
= 0; y
< m_height
; y
++ )
1416 wxUint32
* const rowStartDst
= dst
;
1418 for ( int x
= 0; x
< m_width
; x
++ )
1420 const unsigned char a
= *alpha
++;
1423 Premultiply(a
, src
[0]) << 16 |
1424 Premultiply(a
, src
[1]) << 8 |
1425 Premultiply(a
, src
[2]);
1429 dst
= rowStartDst
+ stride
/ 4;
1434 for ( int y
= 0; y
< m_height
; y
++ )
1436 wxUint32
* const rowStartDst
= dst
;
1438 for ( int x
= 0; x
< m_width
; x
++ )
1440 *dst
++ = src
[0] << 16 |
1446 dst
= rowStartDst
+ stride
/ 4;
1450 InitSurface(bufferFormat
, stride
);
1453 wxImage
wxCairoBitmapData::ConvertToImage() const
1455 wxImage
image(m_width
, m_height
, false /* don't clear */);
1457 // Get the surface type and format.
1458 wxCHECK_MSG( cairo_surface_get_type(m_surface
) == CAIRO_SURFACE_TYPE_IMAGE
,
1460 wxS("Can't convert non-image surface to image.") );
1462 switch ( cairo_image_surface_get_format(m_surface
) )
1464 case CAIRO_FORMAT_ARGB32
:
1468 case CAIRO_FORMAT_RGB24
:
1469 // Nothing to do, we don't use alpha by default.
1472 case CAIRO_FORMAT_A8
:
1473 case CAIRO_FORMAT_A1
:
1474 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1478 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1482 // Prepare for copying data.
1483 const wxUint32
* src
= (wxUint32
*)cairo_image_surface_get_data(m_surface
);
1484 wxCHECK_MSG( src
, wxNullImage
, wxS("Failed to get Cairo surface data.") );
1486 int stride
= cairo_image_surface_get_stride(m_surface
);
1487 wxCHECK_MSG( stride
> 0, wxNullImage
,
1488 wxS("Failed to get Cairo surface stride.") );
1490 // As we work with wxUint32 pointers and not char ones, we need to adjust
1491 // the stride accordingly. This should be lossless as the stride must be a
1492 // multiple of pixel size.
1493 wxASSERT_MSG( !(stride
% sizeof(wxUint32
)), wxS("Unexpected stride.") );
1494 stride
/= sizeof(wxUint32
);
1496 unsigned char* dst
= image
.GetData();
1497 unsigned char *alpha
= image
.GetAlpha();
1500 // We need to also copy alpha and undo the pre-multiplication as Cairo
1501 // stores pre-multiplied values in this format while wxImage does not.
1502 for ( int y
= 0; y
< m_height
; y
++ )
1504 const wxUint32
* const rowStart
= src
;
1505 for ( int x
= 0; x
< m_width
; x
++ )
1507 const wxUint32 argb
= *src
++;
1509 *alpha
++ = (argb
& 0xff000000) >> 24;
1511 // Copy the RGB data undoing the pre-multiplication.
1512 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x00ff0000) >> 16);
1513 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x0000ff00) >> 8);
1514 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x000000ff));
1517 src
= rowStart
+ stride
;
1522 // Things are pretty simple in this case, just copy RGB bytes.
1523 for ( int y
= 0; y
< m_height
; y
++ )
1525 const wxUint32
* const rowStart
= src
;
1526 for ( int x
= 0; x
< m_width
; x
++ )
1528 const wxUint32 argb
= *src
++;
1530 *dst
++ = (argb
& 0x00ff0000) >> 16;
1531 *dst
++ = (argb
& 0x0000ff00) >> 8;
1532 *dst
++ = (argb
& 0x000000ff);
1535 src
= rowStart
+ stride
;
1542 #endif // wxUSE_IMAGE
1544 wxCairoBitmapData::~wxCairoBitmapData()
1547 cairo_pattern_destroy(m_pattern
);
1550 cairo_surface_destroy(m_surface
);
1555 //-----------------------------------------------------------------------------
1556 // wxCairoContext implementation
1557 //-----------------------------------------------------------------------------
1559 class wxCairoOffsetHelper
1562 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1567 cairo_translate( m_ctx
, 0.5, 0.5 );
1569 ~wxCairoOffsetHelper( )
1572 cairo_translate( m_ctx
, -0.5, -0.5 );
1579 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1580 : wxGraphicsContext(renderer
)
1583 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1584 // text rendering when printing using Cairo. Switch it to MM_TEXT
1585 // map mode to avoid this problem.
1586 HDC hdc
= (HDC
)dc
.GetHDC();
1587 ::SetMapMode(hdc
, MM_TEXT
);
1588 m_mswSurface
= cairo_win32_printing_surface_create(hdc
);
1589 Init( cairo_create(m_mswSurface
) );
1593 const wxDCImpl
*impl
= dc
.GetImpl();
1594 Init( (cairo_t
*) impl
->GetCairoContext() );
1596 wxSize sz
= dc
.GetSize();
1600 wxPoint org
= dc
.GetDeviceOrigin();
1601 cairo_translate( m_context
, org
.x
, org
.y
);
1604 dc
.GetUserScale( &sx
, &sy
);
1606 // TODO: Determine if these fixes are needed on other platforms too.
1607 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1608 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1612 dc
.GetLogicalScale( &lsx
, &lsy
);
1616 cairo_scale( m_context
, sx
, sy
);
1618 org
= dc
.GetLogicalOrigin();
1619 cairo_translate( m_context
, -org
.x
, -org
.y
);
1622 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1623 : wxGraphicsContext(renderer
)
1626 dc
.GetSize( &width
, &height
);
1630 m_enableOffset
= true;
1633 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1634 Init( cairo_create(m_mswSurface
) );
1638 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1639 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1642 wxGraphicsMatrix matrix
= CreateMatrix();
1644 wxPoint org
= dc
.GetDeviceOrigin();
1645 matrix
.Translate( org
.x
, org
.y
);
1647 org
= dc
.GetLogicalOrigin();
1648 matrix
.Translate( -org
.x
, -org
.y
);
1651 dc
.GetUserScale( &sx
, &sy
);
1652 matrix
.Scale( sx
, sy
);
1654 ConcatTransform( matrix
);
1659 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1660 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1661 Init( cairo_create( surface
) );
1662 cairo_surface_destroy( surface
);
1666 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1667 : wxGraphicsContext(renderer
)
1670 dc
.GetSize( &width
, &height
);
1674 m_enableOffset
= true;
1677 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1678 Init( cairo_create(m_mswSurface
) );
1682 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1683 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1686 wxGraphicsMatrix matrix
= CreateMatrix();
1688 wxPoint org
= dc
.GetDeviceOrigin();
1689 matrix
.Translate( org
.x
, org
.y
);
1691 org
= dc
.GetLogicalOrigin();
1692 matrix
.Translate( -org
.x
, -org
.y
);
1695 dc
.GetUserScale( &sx
, &sy
);
1696 matrix
.Scale( sx
, sy
);
1698 ConcatTransform( matrix
);
1703 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1704 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1705 Init( cairo_create( surface
) );
1706 cairo_surface_destroy( surface
);
1711 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1712 : wxGraphicsContext(renderer
)
1714 Init( gdk_cairo_create( drawable
) );
1717 gdk_drawable_get_size( drawable
, &width
, &height
);
1724 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1725 : wxGraphicsContext(renderer
)
1727 m_mswSurface
= cairo_win32_surface_create(handle
);
1728 Init( cairo_create(m_mswSurface
) );
1735 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1736 : wxGraphicsContext(renderer
)
1743 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1744 : wxGraphicsContext(renderer
)
1746 m_enableOffset
= true;
1748 // something along these lines (copied from dcclient)
1750 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1751 // code should still be able to create wxClientDCs for them, so we will
1752 // use the parent window here then.
1753 if (window
->m_wxwindow
== NULL
)
1755 window
= window
->GetParent();
1758 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1760 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1762 wxSize sz
= window
->GetSize();
1768 m_mswSurface
= cairo_win32_surface_create((HDC
)window
->GetHandle());
1769 Init(cairo_create(m_mswSurface
));
1774 wxCairoContext::wxCairoContext(wxGraphicsRenderer
* renderer
) :
1775 wxGraphicsContext(renderer
)
1780 wxCairoContext::~wxCairoContext()
1786 cairo_destroy(m_context
);
1790 cairo_surface_destroy(m_mswSurface
);
1794 void wxCairoContext::Init(cairo_t
*context
)
1796 m_context
= context
;
1802 void wxCairoContext::Clip( const wxRegion
& region
)
1804 // Create a path with all the rectangles in the region
1805 wxGraphicsPath path
= GetRenderer()->CreatePath();
1806 wxRegionIterator
ri(region
);
1809 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1813 // Put it in the context
1814 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1815 cairo_append_path(m_context
, cp
);
1817 // clip to that path
1818 cairo_clip(m_context
);
1819 path
.UnGetNativePath(cp
);
1822 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1824 // Create a path with this rectangle
1825 wxGraphicsPath path
= GetRenderer()->CreatePath();
1826 path
.AddRectangle(x
,y
,w
,h
);
1828 // Put it in the context
1829 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1830 cairo_append_path(m_context
, cp
);
1832 // clip to that path
1833 cairo_clip(m_context
);
1834 path
.UnGetNativePath(cp
);
1837 void wxCairoContext::ResetClip()
1839 cairo_reset_clip(m_context
);
1843 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1845 if ( !m_pen
.IsNull() )
1847 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1848 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1849 cairo_append_path(m_context
,cp
);
1850 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1851 cairo_stroke(m_context
);
1852 path
.UnGetNativePath(cp
);
1856 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1858 if ( !m_brush
.IsNull() )
1860 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1861 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1862 cairo_append_path(m_context
,cp
);
1863 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1864 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1865 cairo_fill(m_context
);
1866 path
.UnGetNativePath(cp
);
1870 void wxCairoContext::Rotate( wxDouble angle
)
1872 cairo_rotate(m_context
,angle
);
1875 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1877 cairo_translate(m_context
,dx
,dy
);
1880 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1882 cairo_scale(m_context
,xScale
,yScale
);
1885 // concatenates this transform with the current transform of this context
1886 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1888 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1891 // sets the transform of this context
1892 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1894 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1897 // gets the matrix of this context
1898 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1900 wxGraphicsMatrix matrix
= CreateMatrix();
1901 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1907 void wxCairoContext::PushState()
1909 cairo_save(m_context
);
1912 void wxCairoContext::PopState()
1914 cairo_restore(m_context
);
1917 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1919 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1920 DrawBitmap(bitmap
, x
, y
, w
, h
);
1924 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1928 // In case we're scaling the image by using a width and height different
1929 // than the bitmap's size create a pattern transformation on the surface and
1930 // draw the transformed pattern.
1931 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
1932 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
1933 wxSize size
= data
->GetSize();
1935 wxDouble scaleX
= w
/ size
.GetWidth();
1936 wxDouble scaleY
= h
/ size
.GetHeight();
1938 // prepare to draw the image
1939 cairo_translate(m_context
, x
, y
);
1940 cairo_scale(m_context
, scaleX
, scaleY
);
1941 cairo_set_source(m_context
, pattern
);
1942 // use the original size here since the context is scaled already...
1943 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
1944 // fill the rectangle using the pattern
1945 cairo_fill(m_context
);
1950 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1952 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1953 // start using the Cairo backend on other platforms then we may need to
1954 // fiddle with this...
1955 DrawBitmap(icon
, x
, y
, w
, h
);
1959 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
1961 wxCHECK_RET( !m_font
.IsNull(),
1962 wxT("wxCairoContext::DrawText - no valid font set") );
1967 const wxCharBuffer data
= str
.utf8_str();
1971 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this) )
1974 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1975 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
1976 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
1977 pango_layout_set_text(layout
, data
, data
.length());
1978 font
.GTKSetPangoAttrs(layout
);
1980 cairo_move_to(m_context
, x
, y
);
1981 pango_cairo_show_layout (m_context
, layout
);
1983 g_object_unref (layout
);
1985 // Don't use Cairo text API, we already did everything.
1990 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1991 // the position we move to by the ascent.
1992 cairo_font_extents_t fe
;
1993 cairo_font_extents(m_context
, &fe
);
1994 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1996 cairo_show_text(m_context
, data
);
1999 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2000 wxDouble
*descent
, wxDouble
*externalLeading
) const
2002 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
2010 if ( externalLeading
)
2011 *externalLeading
= 0;
2016 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this) )
2021 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2022 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2023 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2024 const wxCharBuffer data
= str
.utf8_str();
2029 pango_layout_set_text(layout
, data
, data
.length());
2030 pango_layout_get_pixel_size (layout
, &w
, &h
);
2037 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
2038 int baseline
= pango_layout_iter_get_baseline(iter
);
2039 pango_layout_iter_free(iter
);
2040 *descent
= h
- PANGO_PIXELS(baseline
);
2042 g_object_unref (layout
);
2049 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
2050 cairo_text_extents_t te
;
2051 cairo_text_extents(m_context
, buf
, &te
);
2055 if (height
|| descent
|| externalLeading
)
2057 cairo_font_extents_t fe
;
2058 cairo_font_extents(m_context
, &fe
);
2060 // some backends have negative descents
2062 if ( fe
.descent
< 0 )
2063 fe
.descent
= -fe
.descent
;
2065 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
2067 // some backends are broken re height ... (eg currently ATSUI)
2068 fe
.height
= fe
.ascent
+ fe
.descent
;
2072 *height
= fe
.height
;
2074 *descent
= fe
.descent
;
2075 if ( externalLeading
)
2076 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
2080 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2083 widths
.Add(0, text
.length());
2085 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
2093 void * wxCairoContext::GetNativeContext()
2098 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
2100 if (m_antialias
== antialias
)
2103 m_antialias
= antialias
;
2105 cairo_antialias_t antialiasMode
;
2108 case wxANTIALIAS_DEFAULT
:
2109 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
2111 case wxANTIALIAS_NONE
:
2112 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
2117 cairo_set_antialias(m_context
, antialiasMode
);
2121 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
2127 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
2129 if ( m_composition
== op
)
2133 cairo_operator_t cop
;
2136 case wxCOMPOSITION_CLEAR
:
2137 cop
= CAIRO_OPERATOR_CLEAR
;
2139 case wxCOMPOSITION_SOURCE
:
2140 cop
= CAIRO_OPERATOR_SOURCE
;
2142 case wxCOMPOSITION_OVER
:
2143 cop
= CAIRO_OPERATOR_OVER
;
2145 case wxCOMPOSITION_IN
:
2146 cop
= CAIRO_OPERATOR_IN
;
2148 case wxCOMPOSITION_OUT
:
2149 cop
= CAIRO_OPERATOR_OUT
;
2151 case wxCOMPOSITION_ATOP
:
2152 cop
= CAIRO_OPERATOR_ATOP
;
2154 case wxCOMPOSITION_DEST
:
2155 cop
= CAIRO_OPERATOR_DEST
;
2157 case wxCOMPOSITION_DEST_OVER
:
2158 cop
= CAIRO_OPERATOR_DEST_OVER
;
2160 case wxCOMPOSITION_DEST_IN
:
2161 cop
= CAIRO_OPERATOR_DEST_IN
;
2163 case wxCOMPOSITION_DEST_OUT
:
2164 cop
= CAIRO_OPERATOR_DEST_OUT
;
2166 case wxCOMPOSITION_DEST_ATOP
:
2167 cop
= CAIRO_OPERATOR_DEST_ATOP
;
2169 case wxCOMPOSITION_XOR
:
2170 cop
= CAIRO_OPERATOR_XOR
;
2172 case wxCOMPOSITION_ADD
:
2173 cop
= CAIRO_OPERATOR_ADD
;
2178 cairo_set_operator(m_context
, cop
);
2182 void wxCairoContext::BeginLayer(wxDouble opacity
)
2184 m_layerOpacities
.push_back(opacity
);
2185 cairo_push_group(m_context
);
2188 void wxCairoContext::EndLayer()
2190 float opacity
= m_layerOpacities
.back();
2191 m_layerOpacities
.pop_back();
2192 cairo_pop_group_to_source(m_context
);
2193 cairo_paint_with_alpha(m_context
,opacity
);
2196 //-----------------------------------------------------------------------------
2197 // wxCairoRenderer declaration
2198 //-----------------------------------------------------------------------------
2200 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
2203 wxCairoRenderer() {}
2205 virtual ~wxCairoRenderer() {}
2209 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2210 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2211 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2213 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2215 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2217 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2218 #endif // wxUSE_IMAGE
2220 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2222 virtual wxGraphicsContext
* CreateMeasuringContext();
2224 #if wxUSE_ENH_METAFILE
2225 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
2230 virtual wxGraphicsPath
CreatePath();
2234 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2235 wxDouble tx
=0.0, wxDouble ty
=0.0);
2238 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2240 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2242 virtual wxGraphicsBrush
2243 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2244 wxDouble x2
, wxDouble y2
,
2245 const wxGraphicsGradientStops
& stops
);
2247 virtual wxGraphicsBrush
2248 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2249 wxDouble xc
, wxDouble yc
,
2251 const wxGraphicsGradientStops
& stops
);
2254 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2255 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2256 const wxString
& facename
,
2257 int flags
= wxFONTFLAG_DEFAULT
,
2258 const wxColour
& col
= *wxBLACK
);
2260 // create a native bitmap representation
2261 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
2263 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2264 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2265 #endif // wxUSE_IMAGE
2267 // create a graphics bitmap from a native bitmap
2268 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2270 // create a subimage from a native image representation
2271 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
2274 bool EnsureIsLoaded();
2277 friend class wxCairoModule
;
2280 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
2283 //-----------------------------------------------------------------------------
2284 // wxCairoRenderer implementation
2285 //-----------------------------------------------------------------------------
2287 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
2289 static wxCairoRenderer gs_cairoGraphicsRenderer
;
2290 // temporary hack to allow creating a cairo context on any platform
2291 extern wxGraphicsRenderer
* gCairoRenderer
;
2292 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
2294 bool wxCairoRenderer::EnsureIsLoaded()
2298 return wxCairoInit();
2304 void wxCairoRenderer::Load()
2309 void wxCairoRenderer::Unload()
2314 // call EnsureIsLoaded() and return returnOnFail value if it fails
2315 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2316 if ( !EnsureIsLoaded() ) \
2317 return (returnOnFail)
2319 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
2321 ENSURE_LOADED_OR_RETURN(NULL
);
2322 return new wxCairoContext(this,dc
);
2325 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
2327 ENSURE_LOADED_OR_RETURN(NULL
);
2328 return new wxCairoContext(this,dc
);
2331 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
2333 ENSURE_LOADED_OR_RETURN(NULL
);
2334 return new wxCairoContext(this, dc
);
2338 #if wxUSE_ENH_METAFILE
2339 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& WXUNUSED(dc
) )
2341 ENSURE_LOADED_OR_RETURN(NULL
);
2347 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
2349 ENSURE_LOADED_OR_RETURN(NULL
);
2351 return new wxCairoContext(this,(HDC
)context
);
2353 return new wxCairoContext(this,(cairo_t
*)context
);
2358 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
2360 ENSURE_LOADED_OR_RETURN(NULL
);
2362 return new wxCairoContext(this,(GdkDrawable
*)window
);
2364 wxUnusedVar(window
);
2370 wxGraphicsContext
* wxCairoRenderer::CreateContextFromImage(wxImage
& image
)
2372 return new wxCairoImageContext(this, image
);
2374 #endif // wxUSE_IMAGE
2376 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
2378 ENSURE_LOADED_OR_RETURN(NULL
);
2380 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2386 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
2388 ENSURE_LOADED_OR_RETURN(NULL
);
2389 return new wxCairoContext(this, window
);
2394 wxGraphicsPath
wxCairoRenderer::CreatePath()
2396 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2397 wxGraphicsPath path
;
2398 path
.SetRefData( new wxCairoPathData(this) );
2405 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2406 wxDouble tx
, wxDouble ty
)
2409 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2411 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
2412 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2417 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
2419 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2420 if ( !pen
.IsOk() || pen
.GetStyle() == wxPENSTYLE_TRANSPARENT
)
2421 return wxNullGraphicsPen
;
2425 p
.SetRefData(new wxCairoPenData( this, pen
));
2430 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
2432 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2433 if ( !brush
.IsOk() || brush
.GetStyle() == wxBRUSHSTYLE_TRANSPARENT
)
2434 return wxNullGraphicsBrush
;
2438 p
.SetRefData(new wxCairoBrushData( this, brush
));
2444 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2445 wxDouble x2
, wxDouble y2
,
2446 const wxGraphicsGradientStops
& stops
)
2448 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2450 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2451 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2457 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2458 wxDouble xc
, wxDouble yc
, wxDouble r
,
2459 const wxGraphicsGradientStops
& stops
)
2461 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2463 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2464 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
2469 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2471 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2475 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2479 return wxNullGraphicsFont
;
2483 wxCairoRenderer::CreateFont(double sizeInPixels
,
2484 const wxString
& facename
,
2486 const wxColour
& col
)
2488 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2490 wxGraphicsFont font
;
2491 font
.SetRefData(new wxCairoFontData(this, sizeInPixels
, facename
, flags
, col
));
2495 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2497 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2501 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2505 return wxNullGraphicsBitmap
;
2510 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromImage(const wxImage
& image
)
2512 wxGraphicsBitmap bmp
;
2514 ENSURE_LOADED_OR_RETURN(bmp
);
2518 bmp
.SetRefData(new wxCairoBitmapData(this, image
));
2524 wxImage
wxCairoRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
2526 ENSURE_LOADED_OR_RETURN(wxNullImage
);
2528 const wxCairoBitmapData
* const
2529 data
= static_cast<wxCairoBitmapData
*>(bmp
.GetGraphicsData());
2531 return data
? data
->ConvertToImage() : wxNullImage
;
2534 #endif // wxUSE_IMAGE
2537 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2539 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2540 if ( bitmap
!= NULL
)
2543 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2547 return wxNullGraphicsBitmap
;
2551 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2552 wxDouble
WXUNUSED(x
),
2553 wxDouble
WXUNUSED(y
),
2554 wxDouble
WXUNUSED(w
),
2555 wxDouble
WXUNUSED(h
))
2557 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2558 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2559 return wxNullGraphicsBitmap
;
2562 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2564 return &gs_cairoGraphicsRenderer
;
2567 #else // !wxUSE_CAIRO
2569 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2574 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2576 // MSW and OS X have their own native default renderers, but the other ports
2577 // use Cairo by default
2578 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2579 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2581 return GetCairoRenderer();
2583 #endif // !(__WXMSW__ || __WXOSX__)
2585 #endif // wxUSE_GRAPHICS_CONTEXT