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.
33 #include "wx/bitmap.h"
35 #include "wx/dcclient.h"
36 #include "wx/dcmemory.h"
37 #include "wx/dcprint.h"
38 #include "wx/window.h"
41 #include "wx/private/graphics.h"
42 #include "wx/rawbmp.h"
43 #include "wx/vector.h"
47 //-----------------------------------------------------------------------------
48 // device context implementation
50 // more and more of the dc functionality should be implemented by calling
51 // the appropricate wxCairoContext, but we will have to do that step by step
52 // also coordinate conversions should be moved to native matrix ops
53 //-----------------------------------------------------------------------------
55 // we always stock two context states, one at entry, to be able to preserve the
56 // state we were called with, the other one after changing to HI Graphics orientation
57 // (this one is used for getting back clippings etc)
59 //-----------------------------------------------------------------------------
60 // wxGraphicsPath implementation
61 //-----------------------------------------------------------------------------
63 // TODO remove this dependency (gdiplus needs the macros)
66 #define max(a,b) (((a) > (b)) ? (a) : (b))
70 #define min(a,b) (((a) < (b)) ? (a) : (b))
75 #include <cairo-win32.h>
76 // Notice that the order is important: cairo-win32.h includes windows.h which
77 // pollutes the global name space with macros so include our own header which
78 // #undefines them after it.
79 #include "wx/msw/private.h"
84 #include "wx/fontutil.h"
86 #include "wx/gtk/dc.h"
91 #include "wx/osx/private.h"
92 #include <cairo-quartz.h>
93 #include <cairo-atsui.h>
96 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
99 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
102 virtual wxGraphicsObjectRefData
*Clone() const;
105 // These are the path primitives from which everything else can be constructed
108 // begins a new subpath at (x,y)
109 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
111 // adds a straight line from the current point to (x,y)
112 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
114 // adds a cubic Bezier curve from the current point, using two control points and an end point
115 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
118 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
119 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
121 // gets the last point of the current path, (0,0) if not yet set
122 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
125 virtual void AddPath( const wxGraphicsPathData
* path
);
127 // closes the current sub-path
128 virtual void CloseSubpath();
131 // These are convenience functions which - if not available natively will be assembled
132 // using the primitives from above
137 // appends a rectangle as a new closed subpath
138 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
139 // appends an ellipsis as a new closed subpath fitting the passed rectangle
140 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
142 // 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)
143 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
146 // returns the native path
147 virtual void * GetNativePath() const ;
149 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
150 virtual void UnGetNativePath(void *p
) const;
152 // transforms each point of this path by the matrix
153 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
155 // gets the bounding box enclosing all points (possibly including control points)
156 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
158 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
161 cairo_t
* m_pathContext
;
164 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
167 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
168 virtual ~wxCairoMatrixData() ;
170 virtual wxGraphicsObjectRefData
*Clone() const ;
172 // concatenates the matrix
173 virtual void Concat( const wxGraphicsMatrixData
*t
);
175 // sets the matrix to the respective values
176 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
177 wxDouble tx
=0.0, wxDouble ty
=0.0);
179 // gets the component valuess of the matrix
180 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
181 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
183 // makes this the inverse matrix
184 virtual void Invert();
186 // returns true if the elements of the transformation matrix are equal ?
187 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
189 // return true if this is the identity matrix
190 virtual bool IsIdentity() const;
196 // add the translation to this matrix
197 virtual void Translate( wxDouble dx
, wxDouble dy
);
199 // add the scale to this matrix
200 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
202 // add the rotation to this matrix (radians)
203 virtual void Rotate( wxDouble angle
);
206 // apply the transforms
209 // applies that matrix to the point
210 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
212 // applies the matrix except for translations
213 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
215 // returns the native representation
216 virtual void * GetNativeMatrix() const;
218 cairo_matrix_t m_matrix
;
221 // Common base class for pens and brushes.
222 class wxCairoPenBrushBaseData
: public wxGraphicsObjectRefData
225 wxCairoPenBrushBaseData(wxGraphicsRenderer
* renderer
,
228 virtual ~wxCairoPenBrushBaseData();
230 virtual void Apply( wxGraphicsContext
* context
);
233 // Call this to use the given bitmap as stipple. Bitmap must be non-null
235 void InitStipple(wxBitmap
* bmp
);
237 // Call this to use the given hatch style. Hatch style must be valid.
238 void InitHatch(wxHatchStyle hatchStyle
);
246 cairo_pattern_t
* m_pattern
;
247 class wxCairoBitmapData
* m_bmpdata
;
250 // Called once to allocate m_pattern if needed.
251 void InitHatchPattern(cairo_t
* ctext
);
253 wxHatchStyle m_hatchStyle
;
255 wxDECLARE_NO_COPY_CLASS(wxCairoPenBrushBaseData
);
258 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxCairoPenBrushBaseData
261 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
266 virtual void Apply( wxGraphicsContext
* context
);
267 virtual wxDouble
GetWidth() { return m_width
; }
272 cairo_line_cap_t m_cap
;
273 cairo_line_join_t m_join
;
276 const double *m_lengths
;
277 double *m_userLengths
;
279 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
282 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxCairoPenBrushBaseData
285 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
286 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
288 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
289 wxDouble x2
, wxDouble y2
,
290 const wxGraphicsGradientStops
& stops
);
291 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
292 wxDouble xc
, wxDouble yc
, wxDouble radius
,
293 const wxGraphicsGradientStops
& stops
);
298 // common part of Create{Linear,Radial}GradientBrush()
299 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
302 class wxCairoFontData
: public wxGraphicsObjectRefData
305 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
306 wxCairoFontData(wxGraphicsRenderer
* renderer
,
308 const wxString
& facename
,
310 const wxColour
& col
);
313 virtual bool Apply( wxGraphicsContext
* context
);
315 const wxFont
& GetFont() const { return m_wxfont
; }
318 void InitColour(const wxColour
& col
);
319 void InitFontComponents(const wxString
& facename
,
320 cairo_font_slant_t slant
,
321 cairo_font_weight_t weight
);
329 cairo_font_face_t
*m_font
;
330 #elif defined(__WXGTK__)
334 // These members are used when the font is created from its face name and
335 // flags (and not from wxFont) and also even when creating it from wxFont
336 // on the platforms not covered above.
338 // Notice that we can't use cairo_font_face_t instead of storing those,
339 // even though it would be simpler and need less #ifdefs, because
340 // cairo_toy_font_face_create() that we'd need to create it is only
341 // available in Cairo 1.8 and we require just 1.2 currently. If we do drop
342 // support for < 1.8 versions in the future it would be definitely better
343 // to use cairo_toy_font_face_create() instead.
344 wxCharBuffer m_fontName
;
345 cairo_font_slant_t m_slant
;
346 cairo_font_weight_t m_weight
;
349 class wxCairoBitmapData
: public wxGraphicsBitmapData
352 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
354 wxCairoBitmapData(wxGraphicsRenderer
* renderer
, const wxImage
& image
);
355 #endif // wxUSE_IMAGE
356 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
357 ~wxCairoBitmapData();
359 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
360 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
361 virtual void* GetNativeBitmap() const { return m_surface
; }
362 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
365 wxImage
ConvertToImage() const;
366 #endif // wxUSE_IMAGE
369 // Allocate m_buffer for the bitmap of the given size in the given format.
371 // Returns the stride used for the buffer.
372 int InitBuffer(int width
, int height
, cairo_format_t format
);
374 // Really create the surface using the buffer (which was supposed to be
375 // filled since InitBuffer() call).
376 void InitSurface(cairo_format_t format
, int stride
);
379 cairo_surface_t
* m_surface
;
380 cairo_pattern_t
* m_pattern
;
383 unsigned char* m_buffer
;
386 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
389 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
390 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
391 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
393 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkWindow
*window
);
396 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
398 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
399 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
401 // If this ctor is used, Init() must be called by the derived class later.
402 wxCairoContext( wxGraphicsRenderer
* renderer
);
404 virtual ~wxCairoContext();
406 virtual bool ShouldOffset() const
408 if ( !m_enableOffset
)
412 if ( !m_pen
.IsNull() )
414 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
418 return ( penwidth
% 2 ) == 1;
421 virtual void Clip( const wxRegion
®ion
);
423 cairo_surface_t
* m_mswSurface
;
424 WindowHDC m_mswWindowHDC
;
427 // clips drawings to the rect
428 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
430 // resets the clipping to original extent
431 virtual void ResetClip();
433 virtual void * GetNativeContext();
435 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
437 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
439 virtual bool SetCompositionMode(wxCompositionMode op
);
441 virtual void BeginLayer(wxDouble opacity
);
443 virtual void EndLayer();
445 virtual void StrokePath( const wxGraphicsPath
& p
);
446 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
448 virtual void Translate( wxDouble dx
, wxDouble dy
);
449 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
450 virtual void Rotate( wxDouble angle
);
452 // concatenates this transform with the current transform of this context
453 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
455 // sets the transform of this context
456 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
458 // gets the matrix of this context
459 virtual wxGraphicsMatrix
GetTransform() const;
461 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
462 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
463 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
464 virtual void PushState();
465 virtual void PopState();
467 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
468 wxDouble
*descent
, wxDouble
*externalLeading
) const;
469 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
472 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
474 void Init(cairo_t
*context
);
479 wxVector
<float> m_layerOpacities
;
481 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
485 // ----------------------------------------------------------------------------
486 // wxCairoImageContext: context associated with a wxImage.
487 // ----------------------------------------------------------------------------
489 class wxCairoImageContext
: public wxCairoContext
492 wxCairoImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
493 wxCairoContext(renderer
),
495 m_data(renderer
, image
)
497 Init(cairo_create(m_data
.GetCairoSurface()));
500 virtual ~wxCairoImageContext()
502 m_image
= m_data
.ConvertToImage();
507 wxCairoBitmapData m_data
;
509 wxDECLARE_NO_COPY_CLASS(wxCairoImageContext
);
511 #endif // wxUSE_IMAGE
513 // ----------------------------------------------------------------------------
514 // wxCairoPenBrushBaseData implementation
515 //-----------------------------------------------------------------------------
517 wxCairoPenBrushBaseData::wxCairoPenBrushBaseData(wxGraphicsRenderer
* renderer
,
520 : wxGraphicsObjectRefData(renderer
)
522 m_hatchStyle
= wxHATCHSTYLE_INVALID
;
533 else // non-transparent
535 m_red
= col
.Red()/255.0;
536 m_green
= col
.Green()/255.0;
537 m_blue
= col
.Blue()/255.0;
538 m_alpha
= col
.Alpha()/255.0;
542 wxCairoPenBrushBaseData::~wxCairoPenBrushBaseData()
546 // Deleting the bitmap data also deletes the pattern referenced by
547 // m_pattern, so set it to NULL to avoid deleting it twice.
552 cairo_pattern_destroy(m_pattern
);
555 void wxCairoPenBrushBaseData::InitHatchPattern(cairo_t
* ctext
)
557 cairo_surface_t
* const
558 surface
= cairo_surface_create_similar(
559 cairo_get_target(ctext
), CAIRO_CONTENT_COLOR_ALPHA
, 10, 10
562 cairo_t
* const cr
= cairo_create(surface
);
563 cairo_set_line_cap(cr
, CAIRO_LINE_CAP_SQUARE
);
564 cairo_set_line_width(cr
, 1);
565 cairo_set_line_join(cr
,CAIRO_LINE_JOIN_MITER
);
567 switch ( m_hatchStyle
)
569 case wxHATCHSTYLE_CROSS
:
570 cairo_move_to(cr
, 5, 0);
571 cairo_line_to(cr
, 5, 10);
572 cairo_move_to(cr
, 0, 5);
573 cairo_line_to(cr
, 10, 5);
576 case wxHATCHSTYLE_BDIAGONAL
:
577 cairo_move_to(cr
, 0, 10);
578 cairo_line_to(cr
, 10, 0);
581 case wxHATCHSTYLE_FDIAGONAL
:
582 cairo_move_to(cr
, 0, 0);
583 cairo_line_to(cr
, 10, 10);
586 case wxHATCHSTYLE_CROSSDIAG
:
587 cairo_move_to(cr
, 0, 0);
588 cairo_line_to(cr
, 10, 10);
589 cairo_move_to(cr
, 10, 0);
590 cairo_line_to(cr
, 0, 10);
593 case wxHATCHSTYLE_HORIZONTAL
:
594 cairo_move_to(cr
, 0, 5);
595 cairo_line_to(cr
, 10, 5);
598 case wxHATCHSTYLE_VERTICAL
:
599 cairo_move_to(cr
, 5, 0);
600 cairo_line_to(cr
, 5, 10);
604 wxFAIL_MSG(wxS("Invalid hatch pattern style."));
607 cairo_set_source_rgba(cr
, m_red
, m_green
, m_blue
, m_alpha
);
612 m_pattern
= cairo_pattern_create_for_surface(surface
);
613 cairo_surface_destroy(surface
);
614 cairo_pattern_set_extend(m_pattern
, CAIRO_EXTEND_REPEAT
);
617 void wxCairoPenBrushBaseData::InitStipple(wxBitmap
* bmp
)
619 wxCHECK_RET( bmp
&& bmp
->IsOk(), wxS("Invalid stippled bitmap") );
621 m_bmpdata
= new wxCairoBitmapData(GetRenderer(), *bmp
);
622 m_pattern
= m_bmpdata
->GetCairoPattern();
623 cairo_pattern_set_extend(m_pattern
, CAIRO_EXTEND_REPEAT
);
626 void wxCairoPenBrushBaseData::InitHatch(wxHatchStyle hatchStyle
)
628 // We can't create m_pattern right now as we don't have the Cairo context
629 // needed for it, so just remember that we need to do it.
630 m_hatchStyle
= hatchStyle
;
633 void wxCairoPenBrushBaseData::Apply( wxGraphicsContext
* context
)
635 cairo_t
* const ctext
= (cairo_t
*) context
->GetNativeContext();
637 if ( m_hatchStyle
!= wxHATCHSTYLE_INVALID
&& !m_pattern
)
638 InitHatchPattern(ctext
);
641 cairo_set_source(ctext
, m_pattern
);
643 cairo_set_source_rgba(ctext
, m_red
, m_green
, m_blue
, m_alpha
);
646 //-----------------------------------------------------------------------------
647 // wxCairoPenData implementation
648 //-----------------------------------------------------------------------------
650 wxCairoPenData::~wxCairoPenData()
652 delete[] m_userLengths
;
655 void wxCairoPenData::Init()
658 m_userLengths
= NULL
;
663 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
664 : wxCairoPenBrushBaseData(renderer
, pen
.GetColour(), pen
.IsTransparent())
667 m_width
= pen
.GetWidth();
671 switch ( pen
.GetCap() )
674 m_cap
= CAIRO_LINE_CAP_ROUND
;
677 case wxCAP_PROJECTING
:
678 m_cap
= CAIRO_LINE_CAP_SQUARE
;
682 m_cap
= CAIRO_LINE_CAP_BUTT
;
686 m_cap
= CAIRO_LINE_CAP_BUTT
;
690 switch ( pen
.GetJoin() )
693 m_join
= CAIRO_LINE_JOIN_BEVEL
;
697 m_join
= CAIRO_LINE_JOIN_MITER
;
701 m_join
= CAIRO_LINE_JOIN_ROUND
;
705 m_join
= CAIRO_LINE_JOIN_MITER
;
709 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
710 const double dotted
[] =
712 dashUnit
, dashUnit
+ 2.0
714 static const double short_dashed
[] =
718 static const double dashed
[] =
722 static const double dotted_dashed
[] =
724 9.0 , 6.0 , 3.0 , 3.0
727 switch ( pen
.GetStyle() )
729 case wxPENSTYLE_SOLID
:
732 case wxPENSTYLE_DOT
:
733 m_count
= WXSIZEOF(dotted
);
734 m_userLengths
= new double[ m_count
] ;
735 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
736 m_lengths
= m_userLengths
;
739 case wxPENSTYLE_LONG_DASH
:
741 m_count
= WXSIZEOF(dashed
);
744 case wxPENSTYLE_SHORT_DASH
:
745 m_lengths
= short_dashed
;
746 m_count
= WXSIZEOF(short_dashed
);
749 case wxPENSTYLE_DOT_DASH
:
750 m_lengths
= dotted_dashed
;
751 m_count
= WXSIZEOF(dotted_dashed
);
754 case wxPENSTYLE_USER_DASH
:
757 m_count
= pen
.GetDashes( &wxdashes
) ;
758 if ((wxdashes
!= NULL
) && (m_count
> 0))
760 m_userLengths
= new double[m_count
] ;
761 for ( int i
= 0 ; i
< m_count
; ++i
)
763 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
765 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
766 m_userLengths
[i
] = dashUnit
+ 2.0 ;
767 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
768 m_userLengths
[i
] = dashUnit
;
771 m_lengths
= m_userLengths
;
775 case wxPENSTYLE_STIPPLE
:
776 case wxPENSTYLE_STIPPLE_MASK
:
777 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
778 InitStipple(pen
.GetStipple());
782 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
783 && pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
785 InitHatch(static_cast<wxHatchStyle
>(pen
.GetStyle()));
791 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
793 wxCairoPenBrushBaseData::Apply(context
);
795 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
796 cairo_set_line_width(ctext
,m_width
);
797 cairo_set_line_cap(ctext
,m_cap
);
798 cairo_set_line_join(ctext
,m_join
);
799 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
802 //-----------------------------------------------------------------------------
803 // wxCairoBrushData implementation
804 //-----------------------------------------------------------------------------
806 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
807 : wxCairoPenBrushBaseData(renderer
, wxColour(), true /* transparent */)
812 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
,
813 const wxBrush
&brush
)
814 : wxCairoPenBrushBaseData(renderer
, brush
.GetColour(), brush
.IsTransparent())
818 switch ( brush
.GetStyle() )
820 case wxBRUSHSTYLE_STIPPLE
:
821 case wxBRUSHSTYLE_STIPPLE_MASK
:
822 case wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
:
823 InitStipple(brush
.GetStipple());
827 if ( brush
.IsHatch() )
828 InitHatch(static_cast<wxHatchStyle
>(brush
.GetStyle()));
833 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
835 // loop over all the stops, they include the beginning and ending ones
836 const unsigned numStops
= stops
.GetCount();
837 for ( unsigned n
= 0; n
< numStops
; n
++ )
839 const wxGraphicsGradientStop stop
= stops
.Item(n
);
841 const wxColour col
= stop
.GetColour();
843 cairo_pattern_add_color_stop_rgba
854 wxASSERT_MSG(cairo_pattern_status(m_pattern
) == CAIRO_STATUS_SUCCESS
,
855 wxT("Couldn't create cairo pattern"));
859 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
860 wxDouble x2
, wxDouble y2
,
861 const wxGraphicsGradientStops
& stops
)
863 m_pattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
865 AddGradientStops(stops
);
869 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
870 wxDouble xc
, wxDouble yc
,
872 const wxGraphicsGradientStops
& stops
)
874 m_pattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
876 AddGradientStops(stops
);
879 void wxCairoBrushData::Init()
885 //-----------------------------------------------------------------------------
886 // wxCairoFontData implementation
887 //-----------------------------------------------------------------------------
889 void wxCairoFontData::InitColour(const wxColour
& col
)
891 m_red
= col
.Red()/255.0;
892 m_green
= col
.Green()/255.0;
893 m_blue
= col
.Blue()/255.0;
894 m_alpha
= col
.Alpha()/255.0;
898 wxCairoFontData::InitFontComponents(const wxString
& facename
,
899 cairo_font_slant_t slant
,
900 cairo_font_weight_t weight
)
902 m_fontName
= facename
.mb_str(wxConvUTF8
);
907 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
908 const wxColour
& col
)
909 : wxGraphicsObjectRefData(renderer
)
916 m_size
= font
.GetPointSize();
919 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
920 #elif defined(__WXGTK__)
925 font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
926 : CAIRO_FONT_SLANT_NORMAL
,
927 font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
928 : CAIRO_FONT_WEIGHT_NORMAL
933 wxCairoFontData::wxCairoFontData(wxGraphicsRenderer
* renderer
,
935 const wxString
& facename
,
937 const wxColour
& col
) :
938 wxGraphicsObjectRefData(renderer
)
942 // Resolution for Cairo image surfaces is 72 DPI meaning that the sizes in
943 // points and pixels are identical, so we can just pass the size in pixels
944 // directly to cairo_set_font_size().
945 m_size
= sizeInPixels
;
947 #if defined(__WXMAC__)
951 // There is no need to set m_underlined under wxGTK in this case, it can
952 // only be used if m_font != NULL.
957 flags
& wxFONTFLAG_ITALIC
? CAIRO_FONT_SLANT_ITALIC
958 : CAIRO_FONT_SLANT_NORMAL
,
959 flags
& wxFONTFLAG_BOLD
? CAIRO_FONT_WEIGHT_BOLD
960 : CAIRO_FONT_WEIGHT_NORMAL
964 wxCairoFontData::~wxCairoFontData()
968 cairo_font_face_destroy( m_font
);
972 bool wxCairoFontData::Apply( wxGraphicsContext
* context
)
974 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
975 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
979 // Nothing to do, the caller uses Pango layout functions to do
983 #elif defined(__WXMAC__)
986 cairo_set_font_face(ctext
, m_font
);
987 cairo_set_font_size(ctext
, m_size
);
992 // If we get here, we must be on a platform without native font support or
993 // we're using toy Cairo API even under wxGTK/wxMac.
994 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
995 cairo_set_font_size(ctext
, m_size
);
997 // Indicate that we don't use native fonts for the platforms which care
998 // about this (currently only wxGTK).
1002 //-----------------------------------------------------------------------------
1003 // wxCairoPathData implementation
1004 //-----------------------------------------------------------------------------
1006 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
1007 : wxGraphicsPathData(renderer
)
1011 m_pathContext
= pathcontext
;
1015 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
1016 m_pathContext
= cairo_create(surface
);
1017 cairo_surface_destroy (surface
);
1021 wxCairoPathData::~wxCairoPathData()
1023 cairo_destroy(m_pathContext
);
1026 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
1028 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
1029 cairo_t
* pathcontext
= cairo_create(surface
);
1030 cairo_surface_destroy (surface
);
1032 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
1033 cairo_append_path(pathcontext
, path
);
1034 cairo_path_destroy(path
);
1035 return new wxCairoPathData( GetRenderer() ,pathcontext
);
1039 void* wxCairoPathData::GetNativePath() const
1041 return cairo_copy_path(m_pathContext
) ;
1044 void wxCairoPathData::UnGetNativePath(void *p
) const
1046 cairo_path_destroy((cairo_path_t
*)p
);
1053 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1055 cairo_move_to(m_pathContext
,x
,y
);
1058 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1060 cairo_line_to(m_pathContext
,x
,y
);
1063 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
1065 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
1066 cairo_append_path(m_pathContext
, p
);
1070 void wxCairoPathData::CloseSubpath()
1072 cairo_close_path(m_pathContext
);
1075 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1077 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
1080 // gets the last point of the current path, (0,0) if not yet set
1081 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1084 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
1091 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1093 // as clockwise means positive in our system (y pointing downwards)
1094 // TODO make this interpretation dependent of the
1095 // real device trans
1096 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
1097 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1099 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1102 // transforms each point of this path by the matrix
1103 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1105 // as we don't have a true path object, we have to apply the inverse
1106 // matrix to the context
1107 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
1108 cairo_matrix_invert( &m
);
1109 cairo_transform(m_pathContext
,&m
);
1112 // gets the bounding box enclosing all points (possibly including control points)
1113 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1117 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
1141 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1143 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1144 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
1147 //-----------------------------------------------------------------------------
1148 // wxCairoMatrixData implementation
1149 //-----------------------------------------------------------------------------
1151 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
1152 : wxGraphicsMatrixData(renderer
)
1158 wxCairoMatrixData::~wxCairoMatrixData()
1163 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
1165 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
1168 // concatenates the matrix
1169 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1171 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
1174 // sets the matrix to the respective values
1175 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1176 wxDouble tx
, wxDouble ty
)
1178 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
1181 // gets the component valuess of the matrix
1182 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1183 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1185 if (a
) *a
= m_matrix
.xx
;
1186 if (b
) *b
= m_matrix
.yx
;
1187 if (c
) *c
= m_matrix
.xy
;
1188 if (d
) *d
= m_matrix
.yy
;
1189 if (tx
) *tx
= m_matrix
.x0
;
1190 if (ty
) *ty
= m_matrix
.y0
;
1193 // makes this the inverse matrix
1194 void wxCairoMatrixData::Invert()
1196 cairo_matrix_invert( &m_matrix
);
1199 // returns true if the elements of the transformation matrix are equal ?
1200 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1202 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1204 m_matrix
.xx
== tm
->xx
&&
1205 m_matrix
.yx
== tm
->yx
&&
1206 m_matrix
.xy
== tm
->xy
&&
1207 m_matrix
.yy
== tm
->yy
&&
1208 m_matrix
.x0
== tm
->x0
&&
1209 m_matrix
.y0
== tm
->y0
) ;
1212 // return true if this is the identity matrix
1213 bool wxCairoMatrixData::IsIdentity() const
1215 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1216 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1223 // add the translation to this matrix
1224 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1226 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1229 // add the scale to this matrix
1230 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1232 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1235 // add the rotation to this matrix (radians)
1236 void wxCairoMatrixData::Rotate( wxDouble angle
)
1238 cairo_matrix_rotate( &m_matrix
, angle
) ;
1242 // apply the transforms
1245 // applies that matrix to the point
1246 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1248 double lx
= *x
, ly
= *y
;
1249 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1254 // applies the matrix except for translations
1255 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1257 double lx
= *dx
, ly
= *dy
;
1258 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1263 // returns the native representation
1264 void * wxCairoMatrixData::GetNativeMatrix() const
1266 return (void*) &m_matrix
;
1269 // ----------------------------------------------------------------------------
1270 // wxCairoBitmap implementation
1271 // ----------------------------------------------------------------------------
1273 int wxCairoBitmapData::InitBuffer(int width
, int height
, cairo_format_t format
)
1275 wxUnusedVar(format
); // Only really unused with Cairo < 1.6.
1277 // Determine the stride: use cairo_format_stride_for_width() if available
1278 // but fall back to 4*width for the earlier versions as this is what that
1279 // function always returns, even in latest Cairo, anyhow.
1281 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1282 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1284 stride
= cairo_format_stride_for_width(format
, width
);
1286 // All our code would totally break if stride were not a multiple of 4
1287 // so ensure this is the case.
1290 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1292 stride
+= 4 - stride
% 4;
1301 m_buffer
= new unsigned char[height
*stride
];
1306 void wxCairoBitmapData::InitSurface(cairo_format_t format
, int stride
)
1308 m_surface
= cairo_image_surface_create_for_data(
1309 m_buffer
, format
, m_width
, m_height
, stride
);
1310 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1313 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1314 wxGraphicsBitmapData( renderer
)
1317 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1320 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsBitmapData( renderer
)
1322 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1324 #ifdef wxHAS_RAW_BITMAP
1325 // Create a surface object and copy the bitmap pixel data to it. if the
1326 // image has alpha (or a mask represented as alpha) then we'll use a
1327 // different format and iterator than if it doesn't...
1328 cairo_format_t bufferFormat
= bmp
.GetDepth() == 32
1329 #if defined(__WXGTK__) && !defined(__WXGTK3__)
1332 ? CAIRO_FORMAT_ARGB32
1333 : CAIRO_FORMAT_RGB24
;
1335 int stride
= InitBuffer(bmp
.GetWidth(), bmp
.GetHeight(), bufferFormat
);
1337 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1338 wxUint32
* data
= (wxUint32
*)m_buffer
;
1340 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1342 // use the bitmap's alpha
1344 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1345 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1347 wxAlphaPixelData::Iterator
p(pixData
);
1348 for (int y
=0; y
<m_height
; y
++)
1350 wxAlphaPixelData::Iterator rowStart
= p
;
1351 wxUint32
* const rowStartDst
= data
;
1352 for (int x
=0; x
<m_width
; x
++)
1354 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1355 // with alpha in the upper 8 bits, then red, then green, then
1356 // blue. The 32-bit quantities are stored native-endian.
1357 // Pre-multiplied alpha is used.
1358 unsigned char alpha
= p
.Alpha();
1362 *data
= ( alpha
<< 24
1363 | (p
.Red() * alpha
/255) << 16
1364 | (p
.Green() * alpha
/255) << 8
1365 | (p
.Blue() * alpha
/255) );
1370 data
= rowStartDst
+ stride
/ 4;
1372 p
.OffsetY(pixData
, 1);
1378 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1379 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1381 wxNativePixelData::Iterator
p(pixData
);
1382 for (int y
=0; y
<m_height
; y
++)
1384 wxNativePixelData::Iterator rowStart
= p
;
1385 wxUint32
* const rowStartDst
= data
;
1386 for (int x
=0; x
<m_width
; x
++)
1388 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1389 // the upper 8 bits unused. Red, Green, and Blue are stored in
1390 // the remaining 24 bits in that order. The 32-bit quantities
1391 // are stored native-endian.
1392 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1397 data
= rowStartDst
+ stride
/ 4;
1399 p
.OffsetY(pixData
, 1);
1402 #if defined(__WXMSW__) || defined(__WXGTK3__)
1403 // if there is a mask, set the alpha bytes in the target buffer to
1404 // fully transparent or fully opaque
1405 if (bmpSource
.GetMask())
1407 wxBitmap bmpMask
= bmpSource
.GetMask()->GetBitmap();
1408 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1409 data
= (wxUint32
*)m_buffer
;
1411 pixData(bmpMask
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1412 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to mask data."));
1414 wxNativePixelData::Iterator
p(pixData
);
1415 for (int y
=0; y
<m_height
; y
++)
1417 wxNativePixelData::Iterator rowStart
= p
;
1418 wxUint32
* const rowStartDst
= data
;
1419 for (int x
=0; x
<m_width
; x
++)
1421 if (p
.Red()+p
.Green()+p
.Blue() == 0)
1424 *data
= (wxALPHA_OPAQUE
<< 24) | (*data
& 0x00FFFFFF);
1429 data
= rowStartDst
+ stride
/ 4;
1431 p
.OffsetY(pixData
, 1);
1436 InitSurface(bufferFormat
, stride
);
1437 #endif // wxHAS_RAW_BITMAP
1442 // Helper functions for dealing with alpha pre-multiplication.
1446 inline unsigned char Premultiply(unsigned char alpha
, unsigned char data
)
1448 return alpha
? (data
* alpha
)/0xff : data
;
1451 inline unsigned char Unpremultiply(unsigned char alpha
, unsigned char data
)
1453 return alpha
? (data
* 0xff)/alpha
: data
;
1456 } // anonymous namespace
1458 wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer
* renderer
,
1459 const wxImage
& image
)
1460 : wxGraphicsBitmapData(renderer
)
1462 const cairo_format_t bufferFormat
= image
.HasAlpha()
1463 ? CAIRO_FORMAT_ARGB32
1464 : CAIRO_FORMAT_RGB24
;
1466 int stride
= InitBuffer(image
.GetWidth(), image
.GetHeight(), bufferFormat
);
1468 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1469 // values and not bytes becase Cairo always works with buffers in native
1471 wxUint32
* dst
= reinterpret_cast<wxUint32
*>(m_buffer
);
1472 const unsigned char* src
= image
.GetData();
1474 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1476 const unsigned char* alpha
= image
.GetAlpha();
1478 for ( int y
= 0; y
< m_height
; y
++ )
1480 wxUint32
* const rowStartDst
= dst
;
1482 for ( int x
= 0; x
< m_width
; x
++ )
1484 const unsigned char a
= *alpha
++;
1487 Premultiply(a
, src
[0]) << 16 |
1488 Premultiply(a
, src
[1]) << 8 |
1489 Premultiply(a
, src
[2]);
1493 dst
= rowStartDst
+ stride
/ 4;
1498 for ( int y
= 0; y
< m_height
; y
++ )
1500 wxUint32
* const rowStartDst
= dst
;
1502 for ( int x
= 0; x
< m_width
; x
++ )
1504 *dst
++ = src
[0] << 16 |
1510 dst
= rowStartDst
+ stride
/ 4;
1514 InitSurface(bufferFormat
, stride
);
1517 wxImage
wxCairoBitmapData::ConvertToImage() const
1519 wxImage
image(m_width
, m_height
, false /* don't clear */);
1521 // Get the surface type and format.
1522 wxCHECK_MSG( cairo_surface_get_type(m_surface
) == CAIRO_SURFACE_TYPE_IMAGE
,
1524 wxS("Can't convert non-image surface to image.") );
1526 switch ( cairo_image_surface_get_format(m_surface
) )
1528 case CAIRO_FORMAT_ARGB32
:
1532 case CAIRO_FORMAT_RGB24
:
1533 // Nothing to do, we don't use alpha by default.
1536 case CAIRO_FORMAT_A8
:
1537 case CAIRO_FORMAT_A1
:
1538 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1542 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1546 // Prepare for copying data.
1547 const wxUint32
* src
= (wxUint32
*)cairo_image_surface_get_data(m_surface
);
1548 wxCHECK_MSG( src
, wxNullImage
, wxS("Failed to get Cairo surface data.") );
1550 int stride
= cairo_image_surface_get_stride(m_surface
);
1551 wxCHECK_MSG( stride
> 0, wxNullImage
,
1552 wxS("Failed to get Cairo surface stride.") );
1554 // As we work with wxUint32 pointers and not char ones, we need to adjust
1555 // the stride accordingly. This should be lossless as the stride must be a
1556 // multiple of pixel size.
1557 wxASSERT_MSG( !(stride
% sizeof(wxUint32
)), wxS("Unexpected stride.") );
1558 stride
/= sizeof(wxUint32
);
1560 unsigned char* dst
= image
.GetData();
1561 unsigned char *alpha
= image
.GetAlpha();
1564 // We need to also copy alpha and undo the pre-multiplication as Cairo
1565 // stores pre-multiplied values in this format while wxImage does not.
1566 for ( int y
= 0; y
< m_height
; y
++ )
1568 const wxUint32
* const rowStart
= src
;
1569 for ( int x
= 0; x
< m_width
; x
++ )
1571 const wxUint32 argb
= *src
++;
1573 *alpha
++ = (argb
& 0xff000000) >> 24;
1575 // Copy the RGB data undoing the pre-multiplication.
1576 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x00ff0000) >> 16);
1577 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x0000ff00) >> 8);
1578 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x000000ff));
1581 src
= rowStart
+ stride
;
1586 // Things are pretty simple in this case, just copy RGB bytes.
1587 for ( int y
= 0; y
< m_height
; y
++ )
1589 const wxUint32
* const rowStart
= src
;
1590 for ( int x
= 0; x
< m_width
; x
++ )
1592 const wxUint32 argb
= *src
++;
1594 *dst
++ = (argb
& 0x00ff0000) >> 16;
1595 *dst
++ = (argb
& 0x0000ff00) >> 8;
1596 *dst
++ = (argb
& 0x000000ff);
1599 src
= rowStart
+ stride
;
1606 #endif // wxUSE_IMAGE
1608 wxCairoBitmapData::~wxCairoBitmapData()
1611 cairo_pattern_destroy(m_pattern
);
1614 cairo_surface_destroy(m_surface
);
1619 //-----------------------------------------------------------------------------
1620 // wxCairoContext implementation
1621 //-----------------------------------------------------------------------------
1623 class wxCairoOffsetHelper
1626 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1631 cairo_translate( m_ctx
, 0.5, 0.5 );
1633 ~wxCairoOffsetHelper( )
1636 cairo_translate( m_ctx
, -0.5, -0.5 );
1643 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1644 : wxGraphicsContext(renderer
)
1647 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1648 // text rendering when printing using Cairo. Switch it to MM_TEXT
1649 // map mode to avoid this problem.
1650 HDC hdc
= (HDC
)dc
.GetHDC();
1651 ::SetMapMode(hdc
, MM_TEXT
);
1652 m_mswSurface
= cairo_win32_printing_surface_create(hdc
);
1653 Init( cairo_create(m_mswSurface
) );
1657 const wxDCImpl
*impl
= dc
.GetImpl();
1658 Init( (cairo_t
*) impl
->GetCairoContext() );
1660 wxSize sz
= dc
.GetSize();
1664 wxPoint org
= dc
.GetDeviceOrigin();
1665 cairo_translate( m_context
, org
.x
, org
.y
);
1668 dc
.GetUserScale( &sx
, &sy
);
1670 // TODO: Determine if these fixes are needed on other platforms too.
1671 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1672 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1676 dc
.GetLogicalScale( &lsx
, &lsy
);
1680 cairo_scale( m_context
, sx
, sy
);
1682 org
= dc
.GetLogicalOrigin();
1683 cairo_translate( m_context
, -org
.x
, -org
.y
);
1686 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1687 : wxGraphicsContext(renderer
)
1690 dc
.GetSize( &width
, &height
);
1694 m_enableOffset
= true;
1697 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1698 Init( cairo_create(m_mswSurface
) );
1702 cairo_t
* cr
= static_cast<cairo_t
*>(dc
.GetImpl()->GetCairoContext());
1705 #elif defined __WXGTK20__
1706 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1707 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1710 wxGraphicsMatrix matrix
= CreateMatrix();
1712 wxPoint org
= dc
.GetDeviceOrigin();
1713 matrix
.Translate( org
.x
, org
.y
);
1715 org
= dc
.GetLogicalOrigin();
1716 matrix
.Translate( -org
.x
, -org
.y
);
1719 dc
.GetUserScale( &sx
, &sy
);
1720 matrix
.Scale( sx
, sy
);
1722 ConcatTransform( matrix
);
1727 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1728 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1729 Init( cairo_create( surface
) );
1730 cairo_surface_destroy( surface
);
1734 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1735 : wxGraphicsContext(renderer
)
1738 dc
.GetSize( &width
, &height
);
1742 m_enableOffset
= true;
1746 HDC hdc
= (HDC
)dc
.GetHDC();
1748 HBITMAP bitmap
= (HBITMAP
)GetCurrentObject(hdc
, OBJ_BITMAP
);
1751 bool hasBitmap
= false;
1753 // cairo_win32_surface_create creates a 24-bit bitmap,
1754 // so if we have alpha, we need to create a 32-bit surface instead.
1755 if (!GetObject(bitmap
, sizeof(info
), &info
) || info
.bmBitsPixel
< 32)
1756 m_mswSurface
= cairo_win32_surface_create(hdc
);
1759 m_mswSurface
= cairo_image_surface_create_for_data((unsigned char*)info
.bmBits
,
1760 CAIRO_FORMAT_ARGB32
,
1766 Init( cairo_create(m_mswSurface
) );
1767 // If we've created a image surface, we need to flip the Y axis so that
1768 // all drawing will appear right side up.
1770 cairo_matrix_t matrix
;
1771 cairo_matrix_init(&matrix
, 1.0, 0.0, 0.0, -1.0, 0.0, height
);
1772 cairo_set_matrix(m_context
, &matrix
);
1777 cairo_t
* cr
= static_cast<cairo_t
*>(dc
.GetImpl()->GetCairoContext());
1780 #elif defined __WXGTK20__
1781 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1782 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1785 wxGraphicsMatrix matrix
= CreateMatrix();
1787 wxPoint org
= dc
.GetDeviceOrigin();
1788 matrix
.Translate( org
.x
, org
.y
);
1790 org
= dc
.GetLogicalOrigin();
1791 matrix
.Translate( -org
.x
, -org
.y
);
1794 dc
.GetUserScale( &sx
, &sy
);
1795 matrix
.Scale( sx
, sy
);
1797 ConcatTransform( matrix
);
1802 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1803 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1804 Init( cairo_create( surface
) );
1805 cairo_surface_destroy( surface
);
1810 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkWindow
*window
)
1811 : wxGraphicsContext(renderer
)
1813 Init( gdk_cairo_create( window
) );
1816 m_width
= gdk_window_get_width(window
);
1817 m_height
= gdk_window_get_height(window
);
1820 gdk_drawable_get_size(window
, &width
, &height
);
1828 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1829 : wxGraphicsContext(renderer
)
1831 m_mswSurface
= cairo_win32_surface_create(handle
);
1832 Init( cairo_create(m_mswSurface
) );
1839 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1840 : wxGraphicsContext(renderer
)
1847 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1848 : wxGraphicsContext(renderer
)
1850 , m_mswWindowHDC(GetHwndOf(window
))
1853 m_enableOffset
= true;
1855 // something along these lines (copied from dcclient)
1857 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1858 // code should still be able to create wxClientDCs for them, so we will
1859 // use the parent window here then.
1860 if (window
->m_wxwindow
== NULL
)
1862 window
= window
->GetParent();
1865 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1867 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1869 wxSize sz
= window
->GetSize();
1875 m_mswSurface
= cairo_win32_surface_create((HDC
)m_mswWindowHDC
);
1876 Init(cairo_create(m_mswSurface
));
1881 wxCairoContext::wxCairoContext(wxGraphicsRenderer
* renderer
) :
1882 wxGraphicsContext(renderer
)
1887 wxCairoContext::~wxCairoContext()
1893 cairo_destroy(m_context
);
1897 cairo_surface_destroy(m_mswSurface
);
1901 void wxCairoContext::Init(cairo_t
*context
)
1903 m_context
= context
;
1909 void wxCairoContext::Clip( const wxRegion
& region
)
1911 // Create a path with all the rectangles in the region
1912 wxGraphicsPath path
= GetRenderer()->CreatePath();
1913 wxRegionIterator
ri(region
);
1916 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1920 // Put it in the context
1921 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1922 cairo_append_path(m_context
, cp
);
1924 // clip to that path
1925 cairo_clip(m_context
);
1926 path
.UnGetNativePath(cp
);
1929 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1931 // Create a path with this rectangle
1932 wxGraphicsPath path
= GetRenderer()->CreatePath();
1933 path
.AddRectangle(x
,y
,w
,h
);
1935 // Put it in the context
1936 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1937 cairo_append_path(m_context
, cp
);
1939 // clip to that path
1940 cairo_clip(m_context
);
1941 path
.UnGetNativePath(cp
);
1944 void wxCairoContext::ResetClip()
1946 cairo_reset_clip(m_context
);
1950 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1952 if ( !m_pen
.IsNull() )
1954 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1955 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1956 cairo_append_path(m_context
,cp
);
1957 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1958 cairo_stroke(m_context
);
1959 path
.UnGetNativePath(cp
);
1963 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1965 if ( !m_brush
.IsNull() )
1967 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1968 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1969 cairo_append_path(m_context
,cp
);
1970 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1971 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1972 cairo_fill(m_context
);
1973 path
.UnGetNativePath(cp
);
1977 void wxCairoContext::Rotate( wxDouble angle
)
1979 cairo_rotate(m_context
,angle
);
1982 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1984 cairo_translate(m_context
,dx
,dy
);
1987 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1989 cairo_scale(m_context
,xScale
,yScale
);
1992 // concatenates this transform with the current transform of this context
1993 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1995 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1998 // sets the transform of this context
1999 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2001 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
2004 // gets the matrix of this context
2005 wxGraphicsMatrix
wxCairoContext::GetTransform() const
2007 wxGraphicsMatrix matrix
= CreateMatrix();
2008 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
2014 void wxCairoContext::PushState()
2016 cairo_save(m_context
);
2019 void wxCairoContext::PopState()
2021 cairo_restore(m_context
);
2024 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2026 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2027 DrawBitmap(bitmap
, x
, y
, w
, h
);
2031 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2035 // In case we're scaling the image by using a width and height different
2036 // than the bitmap's size create a pattern transformation on the surface and
2037 // draw the transformed pattern.
2038 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
2039 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
2040 wxSize size
= data
->GetSize();
2042 wxDouble scaleX
= w
/ size
.GetWidth();
2043 wxDouble scaleY
= h
/ size
.GetHeight();
2045 // prepare to draw the image
2046 cairo_translate(m_context
, x
, y
);
2047 cairo_scale(m_context
, scaleX
, scaleY
);
2048 cairo_set_source(m_context
, pattern
);
2049 // use the original size here since the context is scaled already...
2050 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
2051 // fill the rectangle using the pattern
2052 cairo_fill(m_context
);
2057 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2059 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
2060 // start using the Cairo backend on other platforms then we may need to
2061 // fiddle with this...
2062 DrawBitmap(icon
, x
, y
, w
, h
);
2066 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
2068 wxCHECK_RET( !m_font
.IsNull(),
2069 wxT("wxCairoContext::DrawText - no valid font set") );
2074 const wxCharBuffer data
= str
.utf8_str();
2078 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this) )
2081 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2082 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2083 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2084 pango_layout_set_text(layout
, data
, data
.length());
2085 font
.GTKSetPangoAttrs(layout
);
2087 cairo_move_to(m_context
, x
, y
);
2088 pango_cairo_show_layout (m_context
, layout
);
2090 g_object_unref (layout
);
2092 // Don't use Cairo text API, we already did everything.
2097 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
2098 // the position we move to by the ascent.
2099 cairo_font_extents_t fe
;
2100 cairo_font_extents(m_context
, &fe
);
2101 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
2103 cairo_show_text(m_context
, data
);
2106 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2107 wxDouble
*descent
, wxDouble
*externalLeading
) const
2109 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
2117 if ( externalLeading
)
2118 *externalLeading
= 0;
2123 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this) )
2128 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2129 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2130 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2131 const wxCharBuffer data
= str
.utf8_str();
2136 pango_layout_set_text(layout
, data
, data
.length());
2137 pango_layout_get_pixel_size (layout
, &w
, &h
);
2144 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
2145 int baseline
= pango_layout_iter_get_baseline(iter
);
2146 pango_layout_iter_free(iter
);
2147 *descent
= h
- PANGO_PIXELS(baseline
);
2149 g_object_unref (layout
);
2156 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
2157 cairo_text_extents_t te
;
2158 cairo_text_extents(m_context
, buf
, &te
);
2162 if (height
|| descent
|| externalLeading
)
2164 cairo_font_extents_t fe
;
2165 cairo_font_extents(m_context
, &fe
);
2167 // some backends have negative descents
2169 if ( fe
.descent
< 0 )
2170 fe
.descent
= -fe
.descent
;
2172 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
2174 // some backends are broken re height ... (eg currently ATSUI)
2175 fe
.height
= fe
.ascent
+ fe
.descent
;
2179 *height
= fe
.height
;
2181 *descent
= fe
.descent
;
2182 if ( externalLeading
)
2183 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
2187 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2190 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
2192 const wxCharBuffer data
= text
.utf8_str();
2196 PangoLayout
* layout
= pango_cairo_create_layout(m_context
);
2197 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2198 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2199 pango_layout_set_text(layout
, data
, data
.length());
2200 PangoLayoutIter
* iter
= pango_layout_get_iter(layout
);
2201 PangoRectangle rect
;
2203 pango_layout_iter_get_cluster_extents(iter
, NULL
, &rect
);
2205 widths
.Add(PANGO_PIXELS(w
));
2206 } while (pango_layout_iter_next_cluster(iter
));
2207 pango_layout_iter_free(iter
);
2208 g_object_unref(layout
);
2210 size_t i
= widths
.GetCount();
2211 const size_t len
= text
.length();
2213 widths
.Add(PANGO_PIXELS(w
));
2219 void * wxCairoContext::GetNativeContext()
2224 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
2226 if (m_antialias
== antialias
)
2229 m_antialias
= antialias
;
2231 cairo_antialias_t antialiasMode
;
2234 case wxANTIALIAS_DEFAULT
:
2235 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
2237 case wxANTIALIAS_NONE
:
2238 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
2243 cairo_set_antialias(m_context
, antialiasMode
);
2247 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
2253 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
2255 if ( m_composition
== op
)
2259 cairo_operator_t cop
;
2262 case wxCOMPOSITION_CLEAR
:
2263 cop
= CAIRO_OPERATOR_CLEAR
;
2265 case wxCOMPOSITION_SOURCE
:
2266 cop
= CAIRO_OPERATOR_SOURCE
;
2268 case wxCOMPOSITION_OVER
:
2269 cop
= CAIRO_OPERATOR_OVER
;
2271 case wxCOMPOSITION_IN
:
2272 cop
= CAIRO_OPERATOR_IN
;
2274 case wxCOMPOSITION_OUT
:
2275 cop
= CAIRO_OPERATOR_OUT
;
2277 case wxCOMPOSITION_ATOP
:
2278 cop
= CAIRO_OPERATOR_ATOP
;
2280 case wxCOMPOSITION_DEST
:
2281 cop
= CAIRO_OPERATOR_DEST
;
2283 case wxCOMPOSITION_DEST_OVER
:
2284 cop
= CAIRO_OPERATOR_DEST_OVER
;
2286 case wxCOMPOSITION_DEST_IN
:
2287 cop
= CAIRO_OPERATOR_DEST_IN
;
2289 case wxCOMPOSITION_DEST_OUT
:
2290 cop
= CAIRO_OPERATOR_DEST_OUT
;
2292 case wxCOMPOSITION_DEST_ATOP
:
2293 cop
= CAIRO_OPERATOR_DEST_ATOP
;
2295 case wxCOMPOSITION_XOR
:
2296 cop
= CAIRO_OPERATOR_XOR
;
2298 case wxCOMPOSITION_ADD
:
2299 cop
= CAIRO_OPERATOR_ADD
;
2304 cairo_set_operator(m_context
, cop
);
2308 void wxCairoContext::BeginLayer(wxDouble opacity
)
2310 m_layerOpacities
.push_back(opacity
);
2311 cairo_push_group(m_context
);
2314 void wxCairoContext::EndLayer()
2316 float opacity
= m_layerOpacities
.back();
2317 m_layerOpacities
.pop_back();
2318 cairo_pop_group_to_source(m_context
);
2319 cairo_paint_with_alpha(m_context
,opacity
);
2322 //-----------------------------------------------------------------------------
2323 // wxCairoRenderer declaration
2324 //-----------------------------------------------------------------------------
2326 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
2329 wxCairoRenderer() {}
2331 virtual ~wxCairoRenderer() {}
2335 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2336 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2337 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2339 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2341 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2343 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2344 #endif // wxUSE_IMAGE
2346 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2348 virtual wxGraphicsContext
* CreateMeasuringContext();
2350 #if wxUSE_ENH_METAFILE
2351 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
2356 virtual wxGraphicsPath
CreatePath();
2360 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2361 wxDouble tx
=0.0, wxDouble ty
=0.0);
2364 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2366 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2368 virtual wxGraphicsBrush
2369 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2370 wxDouble x2
, wxDouble y2
,
2371 const wxGraphicsGradientStops
& stops
);
2373 virtual wxGraphicsBrush
2374 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2375 wxDouble xc
, wxDouble yc
,
2377 const wxGraphicsGradientStops
& stops
);
2380 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2381 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2382 const wxString
& facename
,
2383 int flags
= wxFONTFLAG_DEFAULT
,
2384 const wxColour
& col
= *wxBLACK
);
2386 // create a native bitmap representation
2387 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
2389 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2390 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2391 #endif // wxUSE_IMAGE
2393 // create a graphics bitmap from a native bitmap
2394 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2396 // create a subimage from a native image representation
2397 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
2399 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
2402 //-----------------------------------------------------------------------------
2403 // wxCairoRenderer implementation
2404 //-----------------------------------------------------------------------------
2406 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
2408 static wxCairoRenderer gs_cairoGraphicsRenderer
;
2411 #define ENSURE_LOADED_OR_RETURN(returnOnFail)
2413 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2414 if (!wxCairoInit()) \
2418 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
2420 ENSURE_LOADED_OR_RETURN(NULL
);
2421 return new wxCairoContext(this,dc
);
2424 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
2426 ENSURE_LOADED_OR_RETURN(NULL
);
2427 return new wxCairoContext(this,dc
);
2430 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
2432 ENSURE_LOADED_OR_RETURN(NULL
);
2433 return new wxCairoContext(this, dc
);
2437 #if wxUSE_ENH_METAFILE
2438 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& WXUNUSED(dc
) )
2445 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
2447 ENSURE_LOADED_OR_RETURN(NULL
);
2449 return new wxCairoContext(this,(HDC
)context
);
2451 return new wxCairoContext(this,(cairo_t
*)context
);
2456 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
2459 return new wxCairoContext(this, static_cast<GdkWindow
*>(window
));
2461 wxUnusedVar(window
);
2467 wxGraphicsContext
* wxCairoRenderer::CreateContextFromImage(wxImage
& image
)
2469 ENSURE_LOADED_OR_RETURN(NULL
);
2470 return new wxCairoImageContext(this, image
);
2472 #endif // wxUSE_IMAGE
2474 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
2477 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2484 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
2486 ENSURE_LOADED_OR_RETURN(NULL
);
2487 return new wxCairoContext(this, window
);
2492 wxGraphicsPath
wxCairoRenderer::CreatePath()
2494 wxGraphicsPath path
;
2495 ENSURE_LOADED_OR_RETURN(path
);
2496 path
.SetRefData( new wxCairoPathData(this) );
2503 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2504 wxDouble tx
, wxDouble ty
)
2508 ENSURE_LOADED_OR_RETURN(m
);
2509 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
2510 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2515 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
2518 ENSURE_LOADED_OR_RETURN(p
);
2519 if (pen
.IsOk() && pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
2521 p
.SetRefData(new wxCairoPenData( this, pen
));
2526 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
2529 ENSURE_LOADED_OR_RETURN(p
);
2530 if (brush
.IsOk() && brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
2532 p
.SetRefData(new wxCairoBrushData( this, brush
));
2538 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2539 wxDouble x2
, wxDouble y2
,
2540 const wxGraphicsGradientStops
& stops
)
2543 ENSURE_LOADED_OR_RETURN(p
);
2544 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2545 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2551 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2552 wxDouble xc
, wxDouble yc
, wxDouble r
,
2553 const wxGraphicsGradientStops
& stops
)
2556 ENSURE_LOADED_OR_RETURN(p
);
2557 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2558 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
2563 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2566 ENSURE_LOADED_OR_RETURN(p
);
2569 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2575 wxCairoRenderer::CreateFont(double sizeInPixels
,
2576 const wxString
& facename
,
2578 const wxColour
& col
)
2580 wxGraphicsFont font
;
2581 ENSURE_LOADED_OR_RETURN(font
);
2582 font
.SetRefData(new wxCairoFontData(this, sizeInPixels
, facename
, flags
, col
));
2586 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2589 ENSURE_LOADED_OR_RETURN(p
);
2592 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2599 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromImage(const wxImage
& image
)
2601 wxGraphicsBitmap bmp
;
2603 ENSURE_LOADED_OR_RETURN(bmp
);
2607 bmp
.SetRefData(new wxCairoBitmapData(this, image
));
2613 wxImage
wxCairoRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
2616 ENSURE_LOADED_OR_RETURN(image
);
2618 const wxCairoBitmapData
* const
2619 data
= static_cast<wxCairoBitmapData
*>(bmp
.GetGraphicsData());
2621 image
= data
->ConvertToImage();
2626 #endif // wxUSE_IMAGE
2629 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2632 ENSURE_LOADED_OR_RETURN(p
);
2633 if ( bitmap
!= NULL
)
2635 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2641 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2642 wxDouble
WXUNUSED(x
),
2643 wxDouble
WXUNUSED(y
),
2644 wxDouble
WXUNUSED(w
),
2645 wxDouble
WXUNUSED(h
))
2648 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2652 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2654 return &gs_cairoGraphicsRenderer
;
2657 #else // !wxUSE_CAIRO
2659 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2664 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2666 // MSW and OS X have their own native default renderers, but the other ports
2667 // use Cairo by default
2668 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2669 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2671 return GetCairoRenderer();
2673 #endif // !(__WXMSW__ || __WXOSX__)
2675 #endif // wxUSE_GRAPHICS_CONTEXT