1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
7 // Copyright: (c) 2006 Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #if wxUSE_GRAPHICS_CONTEXT
19 #include "wx/graphics.h"
23 // keep cairo.h from defining dllimport as we're defining the symbols inside
24 // the wx dll in order to load them dynamically.
32 #include "wx/bitmap.h"
34 #include "wx/dcclient.h"
35 #include "wx/dcmemory.h"
36 #include "wx/dcprint.h"
37 #include "wx/window.h"
40 #include "wx/private/graphics.h"
41 #include "wx/rawbmp.h"
42 #include "wx/vector.h"
46 //-----------------------------------------------------------------------------
47 // device context implementation
49 // more and more of the dc functionality should be implemented by calling
50 // the appropricate wxCairoContext, but we will have to do that step by step
51 // also coordinate conversions should be moved to native matrix ops
52 //-----------------------------------------------------------------------------
54 // we always stock two context states, one at entry, to be able to preserve the
55 // state we were called with, the other one after changing to HI Graphics orientation
56 // (this one is used for getting back clippings etc)
58 //-----------------------------------------------------------------------------
59 // wxGraphicsPath implementation
60 //-----------------------------------------------------------------------------
62 // TODO remove this dependency (gdiplus needs the macros)
65 #define max(a,b) (((a) > (b)) ? (a) : (b))
69 #define min(a,b) (((a) < (b)) ? (a) : (b))
74 #include <cairo-win32.h>
75 // Notice that the order is important: cairo-win32.h includes windows.h which
76 // pollutes the global name space with macros so include our own header which
77 // #undefines them after it.
78 #include "wx/msw/private.h"
83 #include "wx/fontutil.h"
85 #include "wx/gtk/dc.h"
90 #include "wx/osx/private.h"
91 #include <cairo-quartz.h>
92 #include <cairo-atsui.h>
95 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
98 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
101 virtual wxGraphicsObjectRefData
*Clone() const;
104 // These are the path primitives from which everything else can be constructed
107 // begins a new subpath at (x,y)
108 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
110 // adds a straight line from the current point to (x,y)
111 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
113 // adds a cubic Bezier curve from the current point, using two control points and an end point
114 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
117 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
118 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
120 // gets the last point of the current path, (0,0) if not yet set
121 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
124 virtual void AddPath( const wxGraphicsPathData
* path
);
126 // closes the current sub-path
127 virtual void CloseSubpath();
130 // These are convenience functions which - if not available natively will be assembled
131 // using the primitives from above
136 // appends a rectangle as a new closed subpath
137 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
138 // appends an ellipsis as a new closed subpath fitting the passed rectangle
139 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
141 // 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)
142 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
145 // returns the native path
146 virtual void * GetNativePath() const ;
148 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
149 virtual void UnGetNativePath(void *p
) const;
151 // transforms each point of this path by the matrix
152 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
154 // gets the bounding box enclosing all points (possibly including control points)
155 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
157 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
) const;
160 cairo_t
* m_pathContext
;
163 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
166 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
167 virtual ~wxCairoMatrixData() ;
169 virtual wxGraphicsObjectRefData
*Clone() const ;
171 // concatenates the matrix
172 virtual void Concat( const wxGraphicsMatrixData
*t
);
174 // sets the matrix to the respective values
175 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
176 wxDouble tx
=0.0, wxDouble ty
=0.0);
178 // gets the component valuess of the matrix
179 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
180 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
182 // makes this the inverse matrix
183 virtual void Invert();
185 // returns true if the elements of the transformation matrix are equal ?
186 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
188 // return true if this is the identity matrix
189 virtual bool IsIdentity() const;
195 // add the translation to this matrix
196 virtual void Translate( wxDouble dx
, wxDouble dy
);
198 // add the scale to this matrix
199 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
201 // add the rotation to this matrix (radians)
202 virtual void Rotate( wxDouble angle
);
205 // apply the transforms
208 // applies that matrix to the point
209 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
211 // applies the matrix except for translations
212 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
214 // returns the native representation
215 virtual void * GetNativeMatrix() const;
217 cairo_matrix_t m_matrix
;
220 // Common base class for pens and brushes.
221 class wxCairoPenBrushBaseData
: public wxGraphicsObjectRefData
224 wxCairoPenBrushBaseData(wxGraphicsRenderer
* renderer
,
227 virtual ~wxCairoPenBrushBaseData();
229 virtual void Apply( wxGraphicsContext
* context
);
232 // Call this to use the given bitmap as stipple. Bitmap must be non-null
234 void InitStipple(wxBitmap
* bmp
);
236 // Call this to use the given hatch style. Hatch style must be valid.
237 void InitHatch(wxHatchStyle hatchStyle
);
245 cairo_pattern_t
* m_pattern
;
246 class wxCairoBitmapData
* m_bmpdata
;
249 // Called once to allocate m_pattern if needed.
250 void InitHatchPattern(cairo_t
* ctext
);
252 wxHatchStyle m_hatchStyle
;
254 wxDECLARE_NO_COPY_CLASS(wxCairoPenBrushBaseData
);
257 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxCairoPenBrushBaseData
260 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
265 virtual void Apply( wxGraphicsContext
* context
);
266 virtual wxDouble
GetWidth() { return m_width
; }
271 cairo_line_cap_t m_cap
;
272 cairo_line_join_t m_join
;
275 const double *m_lengths
;
276 double *m_userLengths
;
278 wxDECLARE_NO_COPY_CLASS(wxCairoPenData
);
281 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxCairoPenBrushBaseData
284 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
285 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
287 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
288 wxDouble x2
, wxDouble y2
,
289 const wxGraphicsGradientStops
& stops
);
290 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
291 wxDouble xc
, wxDouble yc
, wxDouble radius
,
292 const wxGraphicsGradientStops
& stops
);
297 // common part of Create{Linear,Radial}GradientBrush()
298 void AddGradientStops(const wxGraphicsGradientStops
& stops
);
301 class wxCairoFontData
: public wxGraphicsObjectRefData
304 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
305 wxCairoFontData(wxGraphicsRenderer
* renderer
,
307 const wxString
& facename
,
309 const wxColour
& col
);
312 virtual bool Apply( wxGraphicsContext
* context
);
314 const wxFont
& GetFont() const { return m_wxfont
; }
317 void InitColour(const wxColour
& col
);
318 void InitFontComponents(const wxString
& facename
,
319 cairo_font_slant_t slant
,
320 cairo_font_weight_t weight
);
328 cairo_font_face_t
*m_font
;
329 #elif defined(__WXGTK__)
333 // These members are used when the font is created from its face name and
334 // flags (and not from wxFont) and also even when creating it from wxFont
335 // on the platforms not covered above.
337 // Notice that we can't use cairo_font_face_t instead of storing those,
338 // even though it would be simpler and need less #ifdefs, because
339 // cairo_toy_font_face_create() that we'd need to create it is only
340 // available in Cairo 1.8 and we require just 1.2 currently. If we do drop
341 // support for < 1.8 versions in the future it would be definitely better
342 // to use cairo_toy_font_face_create() instead.
343 wxCharBuffer m_fontName
;
344 cairo_font_slant_t m_slant
;
345 cairo_font_weight_t m_weight
;
348 class wxCairoBitmapData
: public wxGraphicsBitmapData
351 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
);
353 wxCairoBitmapData(wxGraphicsRenderer
* renderer
, const wxImage
& image
);
354 #endif // wxUSE_IMAGE
355 wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
);
356 ~wxCairoBitmapData();
358 virtual cairo_surface_t
* GetCairoSurface() { return m_surface
; }
359 virtual cairo_pattern_t
* GetCairoPattern() { return m_pattern
; }
360 virtual void* GetNativeBitmap() const { return m_surface
; }
361 virtual wxSize
GetSize() { return wxSize(m_width
, m_height
); }
364 wxImage
ConvertToImage() const;
365 #endif // wxUSE_IMAGE
368 // Allocate m_buffer for the bitmap of the given size in the given format.
370 // Returns the stride used for the buffer.
371 int InitBuffer(int width
, int height
, cairo_format_t format
);
373 // Really create the surface using the buffer (which was supposed to be
374 // filled since InitBuffer() call).
375 void InitSurface(cairo_format_t format
, int stride
);
378 cairo_surface_t
* m_surface
;
379 cairo_pattern_t
* m_pattern
;
382 unsigned char* m_buffer
;
385 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
388 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
389 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
390 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
392 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkWindow
*window
);
395 wxCairoContext( wxGraphicsRenderer
* renderer
, HDC context
);
397 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
398 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
400 // If this ctor is used, Init() must be called by the derived class later.
401 wxCairoContext( wxGraphicsRenderer
* renderer
);
403 virtual ~wxCairoContext();
405 virtual bool ShouldOffset() const
407 if ( !m_enableOffset
)
411 if ( !m_pen
.IsNull() )
413 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
417 return ( penwidth
% 2 ) == 1;
420 virtual void Clip( const wxRegion
®ion
);
422 cairo_surface_t
* m_mswSurface
;
423 WindowHDC m_mswWindowHDC
;
426 // clips drawings to the rect
427 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
429 // resets the clipping to original extent
430 virtual void ResetClip();
432 virtual void * GetNativeContext();
434 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
436 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
438 virtual bool SetCompositionMode(wxCompositionMode op
);
440 virtual void BeginLayer(wxDouble opacity
);
442 virtual void EndLayer();
444 virtual void StrokePath( const wxGraphicsPath
& p
);
445 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxWINDING_RULE
);
447 virtual void Translate( wxDouble dx
, wxDouble dy
);
448 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
449 virtual void Rotate( wxDouble angle
);
451 // concatenates this transform with the current transform of this context
452 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
454 // sets the transform of this context
455 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
457 // gets the matrix of this context
458 virtual wxGraphicsMatrix
GetTransform() const;
460 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
461 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
462 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
463 virtual void PushState();
464 virtual void PopState();
466 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
467 wxDouble
*descent
, wxDouble
*externalLeading
) const;
468 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
471 virtual void DoDrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
473 void Init(cairo_t
*context
);
478 wxVector
<float> m_layerOpacities
;
480 wxDECLARE_NO_COPY_CLASS(wxCairoContext
);
484 // ----------------------------------------------------------------------------
485 // wxCairoImageContext: context associated with a wxImage.
486 // ----------------------------------------------------------------------------
488 class wxCairoImageContext
: public wxCairoContext
491 wxCairoImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
492 wxCairoContext(renderer
),
494 m_data(renderer
, image
)
496 Init(cairo_create(m_data
.GetCairoSurface()));
499 virtual ~wxCairoImageContext()
501 m_image
= m_data
.ConvertToImage();
506 wxCairoBitmapData m_data
;
508 wxDECLARE_NO_COPY_CLASS(wxCairoImageContext
);
510 #endif // wxUSE_IMAGE
512 // ----------------------------------------------------------------------------
513 // wxCairoPenBrushBaseData implementation
514 //-----------------------------------------------------------------------------
516 wxCairoPenBrushBaseData::wxCairoPenBrushBaseData(wxGraphicsRenderer
* renderer
,
519 : wxGraphicsObjectRefData(renderer
)
521 m_hatchStyle
= wxHATCHSTYLE_INVALID
;
532 else // non-transparent
534 m_red
= col
.Red()/255.0;
535 m_green
= col
.Green()/255.0;
536 m_blue
= col
.Blue()/255.0;
537 m_alpha
= col
.Alpha()/255.0;
541 wxCairoPenBrushBaseData::~wxCairoPenBrushBaseData()
545 // Deleting the bitmap data also deletes the pattern referenced by
546 // m_pattern, so set it to NULL to avoid deleting it twice.
551 cairo_pattern_destroy(m_pattern
);
554 void wxCairoPenBrushBaseData::InitHatchPattern(cairo_t
* ctext
)
556 cairo_surface_t
* const
557 surface
= cairo_surface_create_similar(
558 cairo_get_target(ctext
), CAIRO_CONTENT_COLOR_ALPHA
, 10, 10
561 cairo_t
* const cr
= cairo_create(surface
);
562 cairo_set_line_cap(cr
, CAIRO_LINE_CAP_SQUARE
);
563 cairo_set_line_width(cr
, 1);
564 cairo_set_line_join(cr
,CAIRO_LINE_JOIN_MITER
);
566 switch ( m_hatchStyle
)
568 case wxHATCHSTYLE_CROSS
:
569 cairo_move_to(cr
, 5, 0);
570 cairo_line_to(cr
, 5, 10);
571 cairo_move_to(cr
, 0, 5);
572 cairo_line_to(cr
, 10, 5);
575 case wxHATCHSTYLE_BDIAGONAL
:
576 cairo_move_to(cr
, 0, 10);
577 cairo_line_to(cr
, 10, 0);
580 case wxHATCHSTYLE_FDIAGONAL
:
581 cairo_move_to(cr
, 0, 0);
582 cairo_line_to(cr
, 10, 10);
585 case wxHATCHSTYLE_CROSSDIAG
:
586 cairo_move_to(cr
, 0, 0);
587 cairo_line_to(cr
, 10, 10);
588 cairo_move_to(cr
, 10, 0);
589 cairo_line_to(cr
, 0, 10);
592 case wxHATCHSTYLE_HORIZONTAL
:
593 cairo_move_to(cr
, 0, 5);
594 cairo_line_to(cr
, 10, 5);
597 case wxHATCHSTYLE_VERTICAL
:
598 cairo_move_to(cr
, 5, 0);
599 cairo_line_to(cr
, 5, 10);
603 wxFAIL_MSG(wxS("Invalid hatch pattern style."));
606 cairo_set_source_rgba(cr
, m_red
, m_green
, m_blue
, m_alpha
);
611 m_pattern
= cairo_pattern_create_for_surface(surface
);
612 cairo_surface_destroy(surface
);
613 cairo_pattern_set_extend(m_pattern
, CAIRO_EXTEND_REPEAT
);
616 void wxCairoPenBrushBaseData::InitStipple(wxBitmap
* bmp
)
618 wxCHECK_RET( bmp
&& bmp
->IsOk(), wxS("Invalid stippled bitmap") );
620 m_bmpdata
= new wxCairoBitmapData(GetRenderer(), *bmp
);
621 m_pattern
= m_bmpdata
->GetCairoPattern();
622 cairo_pattern_set_extend(m_pattern
, CAIRO_EXTEND_REPEAT
);
625 void wxCairoPenBrushBaseData::InitHatch(wxHatchStyle hatchStyle
)
627 // We can't create m_pattern right now as we don't have the Cairo context
628 // needed for it, so just remember that we need to do it.
629 m_hatchStyle
= hatchStyle
;
632 void wxCairoPenBrushBaseData::Apply( wxGraphicsContext
* context
)
634 cairo_t
* const ctext
= (cairo_t
*) context
->GetNativeContext();
636 if ( m_hatchStyle
!= wxHATCHSTYLE_INVALID
&& !m_pattern
)
637 InitHatchPattern(ctext
);
640 cairo_set_source(ctext
, m_pattern
);
642 cairo_set_source_rgba(ctext
, m_red
, m_green
, m_blue
, m_alpha
);
645 //-----------------------------------------------------------------------------
646 // wxCairoPenData implementation
647 //-----------------------------------------------------------------------------
649 wxCairoPenData::~wxCairoPenData()
651 delete[] m_userLengths
;
654 void wxCairoPenData::Init()
657 m_userLengths
= NULL
;
662 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
663 : wxCairoPenBrushBaseData(renderer
, pen
.GetColour(), pen
.IsTransparent())
666 m_width
= pen
.GetWidth();
670 switch ( pen
.GetCap() )
673 m_cap
= CAIRO_LINE_CAP_ROUND
;
676 case wxCAP_PROJECTING
:
677 m_cap
= CAIRO_LINE_CAP_SQUARE
;
681 m_cap
= CAIRO_LINE_CAP_BUTT
;
685 m_cap
= CAIRO_LINE_CAP_BUTT
;
689 switch ( pen
.GetJoin() )
692 m_join
= CAIRO_LINE_JOIN_BEVEL
;
696 m_join
= CAIRO_LINE_JOIN_MITER
;
700 m_join
= CAIRO_LINE_JOIN_ROUND
;
704 m_join
= CAIRO_LINE_JOIN_MITER
;
708 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
709 const double dotted
[] =
711 dashUnit
, dashUnit
+ 2.0
713 static const double short_dashed
[] =
717 static const double dashed
[] =
721 static const double dotted_dashed
[] =
723 9.0 , 6.0 , 3.0 , 3.0
726 switch ( pen
.GetStyle() )
728 case wxPENSTYLE_SOLID
:
731 case wxPENSTYLE_DOT
:
732 m_count
= WXSIZEOF(dotted
);
733 m_userLengths
= new double[ m_count
] ;
734 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
735 m_lengths
= m_userLengths
;
738 case wxPENSTYLE_LONG_DASH
:
740 m_count
= WXSIZEOF(dashed
);
743 case wxPENSTYLE_SHORT_DASH
:
744 m_lengths
= short_dashed
;
745 m_count
= WXSIZEOF(short_dashed
);
748 case wxPENSTYLE_DOT_DASH
:
749 m_lengths
= dotted_dashed
;
750 m_count
= WXSIZEOF(dotted_dashed
);
753 case wxPENSTYLE_USER_DASH
:
756 m_count
= pen
.GetDashes( &wxdashes
) ;
757 if ((wxdashes
!= NULL
) && (m_count
> 0))
759 m_userLengths
= new double[m_count
] ;
760 for ( int i
= 0 ; i
< m_count
; ++i
)
762 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
764 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
765 m_userLengths
[i
] = dashUnit
+ 2.0 ;
766 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
767 m_userLengths
[i
] = dashUnit
;
770 m_lengths
= m_userLengths
;
774 case wxPENSTYLE_STIPPLE
:
775 case wxPENSTYLE_STIPPLE_MASK
:
776 case wxPENSTYLE_STIPPLE_MASK_OPAQUE
:
777 InitStipple(pen
.GetStipple());
781 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
782 && pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
784 InitHatch(static_cast<wxHatchStyle
>(pen
.GetStyle()));
790 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
792 wxCairoPenBrushBaseData::Apply(context
);
794 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
795 cairo_set_line_width(ctext
,m_width
);
796 cairo_set_line_cap(ctext
,m_cap
);
797 cairo_set_line_join(ctext
,m_join
);
798 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
801 //-----------------------------------------------------------------------------
802 // wxCairoBrushData implementation
803 //-----------------------------------------------------------------------------
805 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
806 : wxCairoPenBrushBaseData(renderer
, wxColour(), true /* transparent */)
811 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
,
812 const wxBrush
&brush
)
813 : wxCairoPenBrushBaseData(renderer
, brush
.GetColour(), brush
.IsTransparent())
817 switch ( brush
.GetStyle() )
819 case wxBRUSHSTYLE_STIPPLE
:
820 case wxBRUSHSTYLE_STIPPLE_MASK
:
821 case wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE
:
822 InitStipple(brush
.GetStipple());
826 if ( brush
.IsHatch() )
827 InitHatch(static_cast<wxHatchStyle
>(brush
.GetStyle()));
832 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops
& stops
)
834 // loop over all the stops, they include the beginning and ending ones
835 const unsigned numStops
= stops
.GetCount();
836 for ( unsigned n
= 0; n
< numStops
; n
++ )
838 const wxGraphicsGradientStop stop
= stops
.Item(n
);
840 const wxColour col
= stop
.GetColour();
842 cairo_pattern_add_color_stop_rgba
853 wxASSERT_MSG(cairo_pattern_status(m_pattern
) == CAIRO_STATUS_SUCCESS
,
854 wxT("Couldn't create cairo pattern"));
858 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
859 wxDouble x2
, wxDouble y2
,
860 const wxGraphicsGradientStops
& stops
)
862 m_pattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
864 AddGradientStops(stops
);
868 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
869 wxDouble xc
, wxDouble yc
,
871 const wxGraphicsGradientStops
& stops
)
873 m_pattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
875 AddGradientStops(stops
);
878 void wxCairoBrushData::Init()
884 //-----------------------------------------------------------------------------
885 // wxCairoFontData implementation
886 //-----------------------------------------------------------------------------
888 void wxCairoFontData::InitColour(const wxColour
& col
)
890 m_red
= col
.Red()/255.0;
891 m_green
= col
.Green()/255.0;
892 m_blue
= col
.Blue()/255.0;
893 m_alpha
= col
.Alpha()/255.0;
897 wxCairoFontData::InitFontComponents(const wxString
& facename
,
898 cairo_font_slant_t slant
,
899 cairo_font_weight_t weight
)
901 m_fontName
= facename
.mb_str(wxConvUTF8
);
906 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
907 const wxColour
& col
)
908 : wxGraphicsObjectRefData(renderer
)
915 m_size
= font
.GetPointSize();
918 m_font
= cairo_quartz_font_face_create_for_cgfont( font
.OSXGetCGFont() );
919 #elif defined(__WXGTK__)
924 font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
925 : CAIRO_FONT_SLANT_NORMAL
,
926 font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
927 : CAIRO_FONT_WEIGHT_NORMAL
932 wxCairoFontData::wxCairoFontData(wxGraphicsRenderer
* renderer
,
934 const wxString
& facename
,
936 const wxColour
& col
) :
937 wxGraphicsObjectRefData(renderer
)
941 // Resolution for Cairo image surfaces is 72 DPI meaning that the sizes in
942 // points and pixels are identical, so we can just pass the size in pixels
943 // directly to cairo_set_font_size().
944 m_size
= sizeInPixels
;
946 #if defined(__WXMAC__)
950 // There is no need to set m_underlined under wxGTK in this case, it can
951 // only be used if m_font != NULL.
956 flags
& wxFONTFLAG_ITALIC
? CAIRO_FONT_SLANT_ITALIC
957 : CAIRO_FONT_SLANT_NORMAL
,
958 flags
& wxFONTFLAG_BOLD
? CAIRO_FONT_WEIGHT_BOLD
959 : CAIRO_FONT_WEIGHT_NORMAL
963 wxCairoFontData::~wxCairoFontData()
967 cairo_font_face_destroy( m_font
);
971 bool wxCairoFontData::Apply( wxGraphicsContext
* context
)
973 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
974 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
978 // Nothing to do, the caller uses Pango layout functions to do
982 #elif defined(__WXMAC__)
985 cairo_set_font_face(ctext
, m_font
);
986 cairo_set_font_size(ctext
, m_size
);
991 // If we get here, we must be on a platform without native font support or
992 // we're using toy Cairo API even under wxGTK/wxMac.
993 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weight
);
994 cairo_set_font_size(ctext
, m_size
);
996 // Indicate that we don't use native fonts for the platforms which care
997 // about this (currently only wxGTK).
1001 //-----------------------------------------------------------------------------
1002 // wxCairoPathData implementation
1003 //-----------------------------------------------------------------------------
1005 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
1006 : wxGraphicsPathData(renderer
)
1010 m_pathContext
= pathcontext
;
1014 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
1015 m_pathContext
= cairo_create(surface
);
1016 cairo_surface_destroy (surface
);
1020 wxCairoPathData::~wxCairoPathData()
1022 cairo_destroy(m_pathContext
);
1025 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
1027 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
1028 cairo_t
* pathcontext
= cairo_create(surface
);
1029 cairo_surface_destroy (surface
);
1031 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
1032 cairo_append_path(pathcontext
, path
);
1033 cairo_path_destroy(path
);
1034 return new wxCairoPathData( GetRenderer() ,pathcontext
);
1038 void* wxCairoPathData::GetNativePath() const
1040 return cairo_copy_path(m_pathContext
) ;
1043 void wxCairoPathData::UnGetNativePath(void *p
) const
1045 cairo_path_destroy((cairo_path_t
*)p
);
1052 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1054 cairo_move_to(m_pathContext
,x
,y
);
1057 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1059 cairo_line_to(m_pathContext
,x
,y
);
1062 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
1064 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
1065 cairo_append_path(m_pathContext
, p
);
1069 void wxCairoPathData::CloseSubpath()
1071 cairo_close_path(m_pathContext
);
1074 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1076 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
1079 // gets the last point of the current path, (0,0) if not yet set
1080 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1083 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
1090 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1092 // as clockwise means positive in our system (y pointing downwards)
1093 // TODO make this interpretation dependent of the
1094 // real device trans
1095 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
1096 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1098 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
1101 // transforms each point of this path by the matrix
1102 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1104 // as we don't have a true path object, we have to apply the inverse
1105 // matrix to the context
1106 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
1107 cairo_matrix_invert( &m
);
1108 cairo_transform(m_pathContext
,&m
);
1111 // gets the bounding box enclosing all points (possibly including control points)
1112 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1116 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
1140 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1142 cairo_set_fill_rule(m_pathContext
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1143 return cairo_in_fill( m_pathContext
, x
, y
) != 0;
1146 //-----------------------------------------------------------------------------
1147 // wxCairoMatrixData implementation
1148 //-----------------------------------------------------------------------------
1150 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
1151 : wxGraphicsMatrixData(renderer
)
1157 wxCairoMatrixData::~wxCairoMatrixData()
1162 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
1164 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
1167 // concatenates the matrix
1168 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1170 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
1173 // sets the matrix to the respective values
1174 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1175 wxDouble tx
, wxDouble ty
)
1177 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
1180 // gets the component valuess of the matrix
1181 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1182 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1184 if (a
) *a
= m_matrix
.xx
;
1185 if (b
) *b
= m_matrix
.yx
;
1186 if (c
) *c
= m_matrix
.xy
;
1187 if (d
) *d
= m_matrix
.yy
;
1188 if (tx
) *tx
= m_matrix
.x0
;
1189 if (ty
) *ty
= m_matrix
.y0
;
1192 // makes this the inverse matrix
1193 void wxCairoMatrixData::Invert()
1195 cairo_matrix_invert( &m_matrix
);
1198 // returns true if the elements of the transformation matrix are equal ?
1199 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1201 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
1203 m_matrix
.xx
== tm
->xx
&&
1204 m_matrix
.yx
== tm
->yx
&&
1205 m_matrix
.xy
== tm
->xy
&&
1206 m_matrix
.yy
== tm
->yy
&&
1207 m_matrix
.x0
== tm
->x0
&&
1208 m_matrix
.y0
== tm
->y0
) ;
1211 // return true if this is the identity matrix
1212 bool wxCairoMatrixData::IsIdentity() const
1214 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
1215 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
1222 // add the translation to this matrix
1223 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1225 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
1228 // add the scale to this matrix
1229 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1231 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
1234 // add the rotation to this matrix (radians)
1235 void wxCairoMatrixData::Rotate( wxDouble angle
)
1237 cairo_matrix_rotate( &m_matrix
, angle
) ;
1241 // apply the transforms
1244 // applies that matrix to the point
1245 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1247 double lx
= *x
, ly
= *y
;
1248 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1253 // applies the matrix except for translations
1254 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1256 double lx
= *dx
, ly
= *dy
;
1257 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1262 // returns the native representation
1263 void * wxCairoMatrixData::GetNativeMatrix() const
1265 return (void*) &m_matrix
;
1268 // ----------------------------------------------------------------------------
1269 // wxCairoBitmap implementation
1270 // ----------------------------------------------------------------------------
1272 int wxCairoBitmapData::InitBuffer(int width
, int height
, cairo_format_t format
)
1274 wxUnusedVar(format
); // Only really unused with Cairo < 1.6.
1276 // Determine the stride: use cairo_format_stride_for_width() if available
1277 // but fall back to 4*width for the earlier versions as this is what that
1278 // function always returns, even in latest Cairo, anyhow.
1280 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1281 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1283 stride
= cairo_format_stride_for_width(format
, width
);
1285 // All our code would totally break if stride were not a multiple of 4
1286 // so ensure this is the case.
1289 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1291 stride
+= 4 - stride
% 4;
1300 m_buffer
= new unsigned char[height
*stride
];
1305 void wxCairoBitmapData::InitSurface(cairo_format_t format
, int stride
)
1307 m_surface
= cairo_image_surface_create_for_data(
1308 m_buffer
, format
, m_width
, m_height
, stride
);
1309 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1312 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, cairo_surface_t
* bitmap
) :
1313 wxGraphicsBitmapData( renderer
)
1316 m_pattern
= cairo_pattern_create_for_surface(m_surface
);
1319 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
& bmp
) : wxGraphicsBitmapData( renderer
)
1321 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1323 #ifdef wxHAS_RAW_BITMAP
1324 // Create a surface object and copy the bitmap pixel data to it. if the
1325 // image has alpha (or a mask represented as alpha) then we'll use a
1326 // different format and iterator than if it doesn't...
1327 cairo_format_t bufferFormat
= bmp
.GetDepth() == 32
1328 #if defined(__WXGTK__) && !defined(__WXGTK3__)
1331 ? CAIRO_FORMAT_ARGB32
1332 : CAIRO_FORMAT_RGB24
;
1334 int stride
= InitBuffer(bmp
.GetWidth(), bmp
.GetHeight(), bufferFormat
);
1336 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1337 wxUint32
* data
= (wxUint32
*)m_buffer
;
1339 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1341 // use the bitmap's alpha
1343 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1344 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1346 wxAlphaPixelData::Iterator
p(pixData
);
1347 for (int y
=0; y
<m_height
; y
++)
1349 wxAlphaPixelData::Iterator rowStart
= p
;
1350 wxUint32
* const rowStartDst
= data
;
1351 for (int x
=0; x
<m_width
; x
++)
1353 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1354 // with alpha in the upper 8 bits, then red, then green, then
1355 // blue. The 32-bit quantities are stored native-endian.
1356 // Pre-multiplied alpha is used.
1357 unsigned char alpha
= p
.Alpha();
1361 *data
= ( alpha
<< 24
1362 | (p
.Red() * alpha
/255) << 16
1363 | (p
.Green() * alpha
/255) << 8
1364 | (p
.Blue() * alpha
/255) );
1369 data
= rowStartDst
+ stride
/ 4;
1371 p
.OffsetY(pixData
, 1);
1377 pixData(bmpSource
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1378 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1380 wxNativePixelData::Iterator
p(pixData
);
1381 for (int y
=0; y
<m_height
; y
++)
1383 wxNativePixelData::Iterator rowStart
= p
;
1384 wxUint32
* const rowStartDst
= data
;
1385 for (int x
=0; x
<m_width
; x
++)
1387 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1388 // the upper 8 bits unused. Red, Green, and Blue are stored in
1389 // the remaining 24 bits in that order. The 32-bit quantities
1390 // are stored native-endian.
1391 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1396 data
= rowStartDst
+ stride
/ 4;
1398 p
.OffsetY(pixData
, 1);
1401 #if defined(__WXMSW__) || defined(__WXGTK3__)
1402 // if there is a mask, set the alpha bytes in the target buffer to
1403 // fully transparent or fully opaque
1404 if (bmpSource
.GetMask())
1406 wxBitmap bmpMask
= bmpSource
.GetMask()->GetBitmap();
1407 bufferFormat
= CAIRO_FORMAT_ARGB32
;
1408 data
= (wxUint32
*)m_buffer
;
1410 pixData(bmpMask
, wxPoint(0, 0), wxSize(m_width
, m_height
));
1411 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to mask data."));
1413 wxNativePixelData::Iterator
p(pixData
);
1414 for (int y
=0; y
<m_height
; y
++)
1416 wxNativePixelData::Iterator rowStart
= p
;
1417 wxUint32
* const rowStartDst
= data
;
1418 for (int x
=0; x
<m_width
; x
++)
1420 if (p
.Red()+p
.Green()+p
.Blue() == 0)
1423 *data
= (wxALPHA_OPAQUE
<< 24) | (*data
& 0x00FFFFFF);
1428 data
= rowStartDst
+ stride
/ 4;
1430 p
.OffsetY(pixData
, 1);
1435 InitSurface(bufferFormat
, stride
);
1436 #endif // wxHAS_RAW_BITMAP
1441 // Helper functions for dealing with alpha pre-multiplication.
1445 inline unsigned char Premultiply(unsigned char alpha
, unsigned char data
)
1447 return alpha
? (data
* alpha
)/0xff : data
;
1450 inline unsigned char Unpremultiply(unsigned char alpha
, unsigned char data
)
1452 return alpha
? (data
* 0xff)/alpha
: data
;
1455 } // anonymous namespace
1457 wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer
* renderer
,
1458 const wxImage
& image
)
1459 : wxGraphicsBitmapData(renderer
)
1461 const cairo_format_t bufferFormat
= image
.HasAlpha()
1462 ? CAIRO_FORMAT_ARGB32
1463 : CAIRO_FORMAT_RGB24
;
1465 int stride
= InitBuffer(image
.GetWidth(), image
.GetHeight(), bufferFormat
);
1467 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1468 // values and not bytes becase Cairo always works with buffers in native
1470 wxUint32
* dst
= reinterpret_cast<wxUint32
*>(m_buffer
);
1471 const unsigned char* src
= image
.GetData();
1473 if ( bufferFormat
== CAIRO_FORMAT_ARGB32
)
1475 const unsigned char* alpha
= image
.GetAlpha();
1477 for ( int y
= 0; y
< m_height
; y
++ )
1479 wxUint32
* const rowStartDst
= dst
;
1481 for ( int x
= 0; x
< m_width
; x
++ )
1483 const unsigned char a
= *alpha
++;
1486 Premultiply(a
, src
[0]) << 16 |
1487 Premultiply(a
, src
[1]) << 8 |
1488 Premultiply(a
, src
[2]);
1492 dst
= rowStartDst
+ stride
/ 4;
1497 for ( int y
= 0; y
< m_height
; y
++ )
1499 wxUint32
* const rowStartDst
= dst
;
1501 for ( int x
= 0; x
< m_width
; x
++ )
1503 *dst
++ = src
[0] << 16 |
1509 dst
= rowStartDst
+ stride
/ 4;
1513 InitSurface(bufferFormat
, stride
);
1516 wxImage
wxCairoBitmapData::ConvertToImage() const
1518 wxImage
image(m_width
, m_height
, false /* don't clear */);
1520 // Get the surface type and format.
1521 wxCHECK_MSG( cairo_surface_get_type(m_surface
) == CAIRO_SURFACE_TYPE_IMAGE
,
1523 wxS("Can't convert non-image surface to image.") );
1525 switch ( cairo_image_surface_get_format(m_surface
) )
1527 case CAIRO_FORMAT_ARGB32
:
1531 case CAIRO_FORMAT_RGB24
:
1532 // Nothing to do, we don't use alpha by default.
1535 case CAIRO_FORMAT_A8
:
1536 case CAIRO_FORMAT_A1
:
1537 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1541 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1545 // Prepare for copying data.
1546 const wxUint32
* src
= (wxUint32
*)cairo_image_surface_get_data(m_surface
);
1547 wxCHECK_MSG( src
, wxNullImage
, wxS("Failed to get Cairo surface data.") );
1549 int stride
= cairo_image_surface_get_stride(m_surface
);
1550 wxCHECK_MSG( stride
> 0, wxNullImage
,
1551 wxS("Failed to get Cairo surface stride.") );
1553 // As we work with wxUint32 pointers and not char ones, we need to adjust
1554 // the stride accordingly. This should be lossless as the stride must be a
1555 // multiple of pixel size.
1556 wxASSERT_MSG( !(stride
% sizeof(wxUint32
)), wxS("Unexpected stride.") );
1557 stride
/= sizeof(wxUint32
);
1559 unsigned char* dst
= image
.GetData();
1560 unsigned char *alpha
= image
.GetAlpha();
1563 // We need to also copy alpha and undo the pre-multiplication as Cairo
1564 // stores pre-multiplied values in this format while wxImage does not.
1565 for ( int y
= 0; y
< m_height
; y
++ )
1567 const wxUint32
* const rowStart
= src
;
1568 for ( int x
= 0; x
< m_width
; x
++ )
1570 const wxUint32 argb
= *src
++;
1572 *alpha
++ = (argb
& 0xff000000) >> 24;
1574 // Copy the RGB data undoing the pre-multiplication.
1575 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x00ff0000) >> 16);
1576 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x0000ff00) >> 8);
1577 *dst
++ = Unpremultiply(*alpha
, (argb
& 0x000000ff));
1580 src
= rowStart
+ stride
;
1585 // Things are pretty simple in this case, just copy RGB bytes.
1586 for ( int y
= 0; y
< m_height
; y
++ )
1588 const wxUint32
* const rowStart
= src
;
1589 for ( int x
= 0; x
< m_width
; x
++ )
1591 const wxUint32 argb
= *src
++;
1593 *dst
++ = (argb
& 0x00ff0000) >> 16;
1594 *dst
++ = (argb
& 0x0000ff00) >> 8;
1595 *dst
++ = (argb
& 0x000000ff);
1598 src
= rowStart
+ stride
;
1605 #endif // wxUSE_IMAGE
1607 wxCairoBitmapData::~wxCairoBitmapData()
1610 cairo_pattern_destroy(m_pattern
);
1613 cairo_surface_destroy(m_surface
);
1618 //-----------------------------------------------------------------------------
1619 // wxCairoContext implementation
1620 //-----------------------------------------------------------------------------
1622 class wxCairoOffsetHelper
1625 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1630 cairo_translate( m_ctx
, 0.5, 0.5 );
1632 ~wxCairoOffsetHelper( )
1635 cairo_translate( m_ctx
, -0.5, -0.5 );
1642 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1643 : wxGraphicsContext(renderer
)
1646 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1647 // text rendering when printing using Cairo. Switch it to MM_TEXT
1648 // map mode to avoid this problem.
1649 HDC hdc
= (HDC
)dc
.GetHDC();
1650 ::SetMapMode(hdc
, MM_TEXT
);
1651 m_mswSurface
= cairo_win32_printing_surface_create(hdc
);
1652 Init( cairo_create(m_mswSurface
) );
1656 const wxDCImpl
*impl
= dc
.GetImpl();
1657 Init( (cairo_t
*) impl
->GetCairoContext() );
1659 wxSize sz
= dc
.GetSize();
1663 wxPoint org
= dc
.GetDeviceOrigin();
1664 cairo_translate( m_context
, org
.x
, org
.y
);
1667 dc
.GetUserScale( &sx
, &sy
);
1669 // TODO: Determine if these fixes are needed on other platforms too.
1670 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1671 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1675 dc
.GetLogicalScale( &lsx
, &lsy
);
1679 cairo_scale( m_context
, sx
, sy
);
1681 org
= dc
.GetLogicalOrigin();
1682 cairo_translate( m_context
, -org
.x
, -org
.y
);
1685 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1686 : wxGraphicsContext(renderer
)
1689 dc
.GetSize( &width
, &height
);
1693 m_enableOffset
= true;
1696 m_mswSurface
= cairo_win32_surface_create((HDC
)dc
.GetHDC());
1697 Init( cairo_create(m_mswSurface
) );
1701 cairo_t
* cr
= static_cast<cairo_t
*>(dc
.GetImpl()->GetCairoContext());
1704 #elif defined __WXGTK20__
1705 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1706 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1709 wxGraphicsMatrix matrix
= CreateMatrix();
1711 wxPoint org
= dc
.GetDeviceOrigin();
1712 matrix
.Translate( org
.x
, org
.y
);
1714 org
= dc
.GetLogicalOrigin();
1715 matrix
.Translate( -org
.x
, -org
.y
);
1718 dc
.GetUserScale( &sx
, &sy
);
1719 matrix
.Scale( sx
, sy
);
1721 ConcatTransform( matrix
);
1726 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1727 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1728 Init( cairo_create( surface
) );
1729 cairo_surface_destroy( surface
);
1733 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1734 : wxGraphicsContext(renderer
)
1737 dc
.GetSize( &width
, &height
);
1741 m_enableOffset
= true;
1745 HDC hdc
= (HDC
)dc
.GetHDC();
1747 HBITMAP bitmap
= (HBITMAP
)GetCurrentObject(hdc
, OBJ_BITMAP
);
1750 bool hasBitmap
= false;
1752 // cairo_win32_surface_create creates a 24-bit bitmap,
1753 // so if we have alpha, we need to create a 32-bit surface instead.
1754 if (!GetObject(bitmap
, sizeof(info
), &info
) || info
.bmBitsPixel
< 32)
1755 m_mswSurface
= cairo_win32_surface_create(hdc
);
1758 m_mswSurface
= cairo_image_surface_create_for_data((unsigned char*)info
.bmBits
,
1759 CAIRO_FORMAT_ARGB32
,
1765 Init( cairo_create(m_mswSurface
) );
1766 // If we've created a image surface, we need to flip the Y axis so that
1767 // all drawing will appear right side up.
1769 cairo_matrix_t matrix
;
1770 cairo_matrix_init(&matrix
, 1.0, 0.0, 0.0, -1.0, 0.0, height
);
1771 cairo_set_matrix(m_context
, &matrix
);
1776 cairo_t
* cr
= static_cast<cairo_t
*>(dc
.GetImpl()->GetCairoContext());
1779 #elif defined __WXGTK20__
1780 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1781 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1784 wxGraphicsMatrix matrix
= CreateMatrix();
1786 wxPoint org
= dc
.GetDeviceOrigin();
1787 matrix
.Translate( org
.x
, org
.y
);
1789 org
= dc
.GetLogicalOrigin();
1790 matrix
.Translate( -org
.x
, -org
.y
);
1793 dc
.GetUserScale( &sx
, &sy
);
1794 matrix
.Scale( sx
, sy
);
1796 ConcatTransform( matrix
);
1801 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1802 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1803 Init( cairo_create( surface
) );
1804 cairo_surface_destroy( surface
);
1809 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkWindow
*window
)
1810 : wxGraphicsContext(renderer
)
1812 Init( gdk_cairo_create( window
) );
1815 m_width
= gdk_window_get_width(window
);
1816 m_height
= gdk_window_get_height(window
);
1819 gdk_drawable_get_size(window
, &width
, &height
);
1827 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, HDC handle
)
1828 : wxGraphicsContext(renderer
)
1830 m_mswSurface
= cairo_win32_surface_create(handle
);
1831 Init( cairo_create(m_mswSurface
) );
1838 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1839 : wxGraphicsContext(renderer
)
1846 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1847 : wxGraphicsContext(renderer
)
1849 , m_mswWindowHDC(GetHwndOf(window
))
1852 m_enableOffset
= true;
1854 // something along these lines (copied from dcclient)
1856 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1857 // code should still be able to create wxClientDCs for them, so we will
1858 // use the parent window here then.
1859 if (window
->m_wxwindow
== NULL
)
1861 window
= window
->GetParent();
1864 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1866 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1868 wxSize sz
= window
->GetSize();
1874 m_mswSurface
= cairo_win32_surface_create((HDC
)m_mswWindowHDC
);
1875 Init(cairo_create(m_mswSurface
));
1880 wxCairoContext::wxCairoContext(wxGraphicsRenderer
* renderer
) :
1881 wxGraphicsContext(renderer
)
1886 wxCairoContext::~wxCairoContext()
1892 cairo_destroy(m_context
);
1896 cairo_surface_destroy(m_mswSurface
);
1900 void wxCairoContext::Init(cairo_t
*context
)
1902 m_context
= context
;
1908 void wxCairoContext::Clip( const wxRegion
& region
)
1910 // Create a path with all the rectangles in the region
1911 wxGraphicsPath path
= GetRenderer()->CreatePath();
1912 wxRegionIterator
ri(region
);
1915 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1919 // Put it in the context
1920 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1921 cairo_append_path(m_context
, cp
);
1923 // clip to that path
1924 cairo_clip(m_context
);
1925 path
.UnGetNativePath(cp
);
1928 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1930 // Create a path with this rectangle
1931 wxGraphicsPath path
= GetRenderer()->CreatePath();
1932 path
.AddRectangle(x
,y
,w
,h
);
1934 // Put it in the context
1935 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1936 cairo_append_path(m_context
, cp
);
1938 // clip to that path
1939 cairo_clip(m_context
);
1940 path
.UnGetNativePath(cp
);
1943 void wxCairoContext::ResetClip()
1945 cairo_reset_clip(m_context
);
1949 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1951 if ( !m_pen
.IsNull() )
1953 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1954 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1955 cairo_append_path(m_context
,cp
);
1956 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1957 cairo_stroke(m_context
);
1958 path
.UnGetNativePath(cp
);
1962 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1964 if ( !m_brush
.IsNull() )
1966 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1967 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1968 cairo_append_path(m_context
,cp
);
1969 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1970 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1971 cairo_fill(m_context
);
1972 path
.UnGetNativePath(cp
);
1976 void wxCairoContext::Rotate( wxDouble angle
)
1978 cairo_rotate(m_context
,angle
);
1981 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1983 cairo_translate(m_context
,dx
,dy
);
1986 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1988 cairo_scale(m_context
,xScale
,yScale
);
1991 // concatenates this transform with the current transform of this context
1992 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1994 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1997 // sets the transform of this context
1998 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
2000 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
2003 // gets the matrix of this context
2004 wxGraphicsMatrix
wxCairoContext::GetTransform() const
2006 wxGraphicsMatrix matrix
= CreateMatrix();
2007 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
2013 void wxCairoContext::PushState()
2015 cairo_save(m_context
);
2018 void wxCairoContext::PopState()
2020 cairo_restore(m_context
);
2023 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2025 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
2026 DrawBitmap(bitmap
, x
, y
, w
, h
);
2030 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2034 // In case we're scaling the image by using a width and height different
2035 // than the bitmap's size create a pattern transformation on the surface and
2036 // draw the transformed pattern.
2037 wxCairoBitmapData
* data
= static_cast<wxCairoBitmapData
*>(bmp
.GetRefData());
2038 cairo_pattern_t
* pattern
= data
->GetCairoPattern();
2039 wxSize size
= data
->GetSize();
2041 wxDouble scaleX
= w
/ size
.GetWidth();
2042 wxDouble scaleY
= h
/ size
.GetHeight();
2044 // prepare to draw the image
2045 cairo_translate(m_context
, x
, y
);
2046 cairo_scale(m_context
, scaleX
, scaleY
);
2047 cairo_set_source(m_context
, pattern
);
2048 // use the original size here since the context is scaled already...
2049 cairo_rectangle(m_context
, 0, 0, size
.GetWidth(), size
.GetHeight());
2050 // fill the rectangle using the pattern
2051 cairo_fill(m_context
);
2056 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2058 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
2059 // start using the Cairo backend on other platforms then we may need to
2060 // fiddle with this...
2061 DrawBitmap(icon
, x
, y
, w
, h
);
2065 void wxCairoContext::DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
2067 wxCHECK_RET( !m_font
.IsNull(),
2068 wxT("wxCairoContext::DrawText - no valid font set") );
2073 const wxCharBuffer data
= str
.utf8_str();
2077 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this) )
2080 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2081 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2082 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2083 pango_layout_set_text(layout
, data
, data
.length());
2084 font
.GTKSetPangoAttrs(layout
);
2086 cairo_move_to(m_context
, x
, y
);
2087 pango_cairo_show_layout (m_context
, layout
);
2089 g_object_unref (layout
);
2091 // Don't use Cairo text API, we already did everything.
2096 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
2097 // the position we move to by the ascent.
2098 cairo_font_extents_t fe
;
2099 cairo_font_extents(m_context
, &fe
);
2100 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
2102 cairo_show_text(m_context
, data
);
2105 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
2106 wxDouble
*descent
, wxDouble
*externalLeading
) const
2108 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
2116 if ( externalLeading
)
2117 *externalLeading
= 0;
2122 if ( ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this) )
2127 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
2128 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2129 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2130 const wxCharBuffer data
= str
.utf8_str();
2135 pango_layout_set_text(layout
, data
, data
.length());
2136 pango_layout_get_pixel_size (layout
, &w
, &h
);
2143 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
2144 int baseline
= pango_layout_iter_get_baseline(iter
);
2145 pango_layout_iter_free(iter
);
2146 *descent
= h
- PANGO_PIXELS(baseline
);
2148 g_object_unref (layout
);
2155 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
2156 cairo_text_extents_t te
;
2157 cairo_text_extents(m_context
, buf
, &te
);
2161 if (height
|| descent
|| externalLeading
)
2163 cairo_font_extents_t fe
;
2164 cairo_font_extents(m_context
, &fe
);
2166 // some backends have negative descents
2168 if ( fe
.descent
< 0 )
2169 fe
.descent
= -fe
.descent
;
2171 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
2173 // some backends are broken re height ... (eg currently ATSUI)
2174 fe
.height
= fe
.ascent
+ fe
.descent
;
2178 *height
= fe
.height
;
2180 *descent
= fe
.descent
;
2181 if ( externalLeading
)
2182 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
2186 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
2189 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
2191 const wxCharBuffer data
= text
.utf8_str();
2195 PangoLayout
* layout
= pango_cairo_create_layout(m_context
);
2196 const wxFont
& font
= static_cast<wxCairoFontData
*>(m_font
.GetRefData())->GetFont();
2197 pango_layout_set_font_description(layout
, font
.GetNativeFontInfo()->description
);
2198 pango_layout_set_text(layout
, data
, data
.length());
2199 PangoLayoutIter
* iter
= pango_layout_get_iter(layout
);
2200 PangoRectangle rect
;
2202 pango_layout_iter_get_cluster_extents(iter
, NULL
, &rect
);
2204 widths
.Add(PANGO_PIXELS(w
));
2205 } while (pango_layout_iter_next_cluster(iter
));
2206 pango_layout_iter_free(iter
);
2207 g_object_unref(layout
);
2209 size_t i
= widths
.GetCount();
2210 const size_t len
= text
.length();
2212 widths
.Add(PANGO_PIXELS(w
));
2218 void * wxCairoContext::GetNativeContext()
2223 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias
)
2225 if (m_antialias
== antialias
)
2228 m_antialias
= antialias
;
2230 cairo_antialias_t antialiasMode
;
2233 case wxANTIALIAS_DEFAULT
:
2234 antialiasMode
= CAIRO_ANTIALIAS_DEFAULT
;
2236 case wxANTIALIAS_NONE
:
2237 antialiasMode
= CAIRO_ANTIALIAS_NONE
;
2242 cairo_set_antialias(m_context
, antialiasMode
);
2246 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
2252 bool wxCairoContext::SetCompositionMode(wxCompositionMode op
)
2254 if ( m_composition
== op
)
2258 cairo_operator_t cop
;
2261 case wxCOMPOSITION_CLEAR
:
2262 cop
= CAIRO_OPERATOR_CLEAR
;
2264 case wxCOMPOSITION_SOURCE
:
2265 cop
= CAIRO_OPERATOR_SOURCE
;
2267 case wxCOMPOSITION_OVER
:
2268 cop
= CAIRO_OPERATOR_OVER
;
2270 case wxCOMPOSITION_IN
:
2271 cop
= CAIRO_OPERATOR_IN
;
2273 case wxCOMPOSITION_OUT
:
2274 cop
= CAIRO_OPERATOR_OUT
;
2276 case wxCOMPOSITION_ATOP
:
2277 cop
= CAIRO_OPERATOR_ATOP
;
2279 case wxCOMPOSITION_DEST
:
2280 cop
= CAIRO_OPERATOR_DEST
;
2282 case wxCOMPOSITION_DEST_OVER
:
2283 cop
= CAIRO_OPERATOR_DEST_OVER
;
2285 case wxCOMPOSITION_DEST_IN
:
2286 cop
= CAIRO_OPERATOR_DEST_IN
;
2288 case wxCOMPOSITION_DEST_OUT
:
2289 cop
= CAIRO_OPERATOR_DEST_OUT
;
2291 case wxCOMPOSITION_DEST_ATOP
:
2292 cop
= CAIRO_OPERATOR_DEST_ATOP
;
2294 case wxCOMPOSITION_XOR
:
2295 cop
= CAIRO_OPERATOR_XOR
;
2297 case wxCOMPOSITION_ADD
:
2298 cop
= CAIRO_OPERATOR_ADD
;
2303 cairo_set_operator(m_context
, cop
);
2307 void wxCairoContext::BeginLayer(wxDouble opacity
)
2309 m_layerOpacities
.push_back(opacity
);
2310 cairo_push_group(m_context
);
2313 void wxCairoContext::EndLayer()
2315 float opacity
= m_layerOpacities
.back();
2316 m_layerOpacities
.pop_back();
2317 cairo_pop_group_to_source(m_context
);
2318 cairo_paint_with_alpha(m_context
,opacity
);
2321 //-----------------------------------------------------------------------------
2322 // wxCairoRenderer declaration
2323 //-----------------------------------------------------------------------------
2325 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
2328 wxCairoRenderer() {}
2330 virtual ~wxCairoRenderer() {}
2334 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
2335 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
2336 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
2338 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
2340 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
2342 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
2343 #endif // wxUSE_IMAGE
2345 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
2347 virtual wxGraphicsContext
* CreateMeasuringContext();
2349 #if wxUSE_ENH_METAFILE
2350 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
2355 virtual wxGraphicsPath
CreatePath();
2359 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
2360 wxDouble tx
=0.0, wxDouble ty
=0.0);
2363 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
2365 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
2367 virtual wxGraphicsBrush
2368 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2369 wxDouble x2
, wxDouble y2
,
2370 const wxGraphicsGradientStops
& stops
);
2372 virtual wxGraphicsBrush
2373 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2374 wxDouble xc
, wxDouble yc
,
2376 const wxGraphicsGradientStops
& stops
);
2379 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
2380 virtual wxGraphicsFont
CreateFont(double sizeInPixels
,
2381 const wxString
& facename
,
2382 int flags
= wxFONTFLAG_DEFAULT
,
2383 const wxColour
& col
= *wxBLACK
);
2385 // create a native bitmap representation
2386 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
2388 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
2389 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
2390 #endif // wxUSE_IMAGE
2392 // create a graphics bitmap from a native bitmap
2393 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
2395 // create a subimage from a native image representation
2396 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
2398 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
2401 //-----------------------------------------------------------------------------
2402 // wxCairoRenderer implementation
2403 //-----------------------------------------------------------------------------
2405 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
2407 static wxCairoRenderer gs_cairoGraphicsRenderer
;
2410 #define ENSURE_LOADED_OR_RETURN(returnOnFail)
2412 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2413 if (!wxCairoInit()) \
2417 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
2419 ENSURE_LOADED_OR_RETURN(NULL
);
2420 return new wxCairoContext(this,dc
);
2423 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
2425 ENSURE_LOADED_OR_RETURN(NULL
);
2426 return new wxCairoContext(this,dc
);
2429 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
2431 ENSURE_LOADED_OR_RETURN(NULL
);
2432 return new wxCairoContext(this, dc
);
2436 #if wxUSE_ENH_METAFILE
2437 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxEnhMetaFileDC
& WXUNUSED(dc
) )
2444 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
2446 ENSURE_LOADED_OR_RETURN(NULL
);
2448 return new wxCairoContext(this,(HDC
)context
);
2450 return new wxCairoContext(this,(cairo_t
*)context
);
2455 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
2458 return new wxCairoContext(this, static_cast<GdkWindow
*>(window
));
2460 wxUnusedVar(window
);
2466 wxGraphicsContext
* wxCairoRenderer::CreateContextFromImage(wxImage
& image
)
2468 ENSURE_LOADED_OR_RETURN(NULL
);
2469 return new wxCairoImageContext(this, image
);
2471 #endif // wxUSE_IMAGE
2473 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
2476 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2483 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
2485 ENSURE_LOADED_OR_RETURN(NULL
);
2486 return new wxCairoContext(this, window
);
2491 wxGraphicsPath
wxCairoRenderer::CreatePath()
2493 wxGraphicsPath path
;
2494 ENSURE_LOADED_OR_RETURN(path
);
2495 path
.SetRefData( new wxCairoPathData(this) );
2502 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2503 wxDouble tx
, wxDouble ty
)
2507 ENSURE_LOADED_OR_RETURN(m
);
2508 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
2509 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2514 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
2517 ENSURE_LOADED_OR_RETURN(p
);
2518 if (pen
.IsOk() && pen
.GetStyle() != wxPENSTYLE_TRANSPARENT
)
2520 p
.SetRefData(new wxCairoPenData( this, pen
));
2525 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
2528 ENSURE_LOADED_OR_RETURN(p
);
2529 if (brush
.IsOk() && brush
.GetStyle() != wxBRUSHSTYLE_TRANSPARENT
)
2531 p
.SetRefData(new wxCairoBrushData( this, brush
));
2537 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2538 wxDouble x2
, wxDouble y2
,
2539 const wxGraphicsGradientStops
& stops
)
2542 ENSURE_LOADED_OR_RETURN(p
);
2543 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2544 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2550 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2551 wxDouble xc
, wxDouble yc
, wxDouble r
,
2552 const wxGraphicsGradientStops
& stops
)
2555 ENSURE_LOADED_OR_RETURN(p
);
2556 wxCairoBrushData
* d
= new wxCairoBrushData( this );
2557 d
->CreateRadialGradientBrush(xo
, yo
, xc
, yc
, r
, stops
);
2562 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
2565 ENSURE_LOADED_OR_RETURN(p
);
2568 p
.SetRefData(new wxCairoFontData( this , font
, col
));
2574 wxCairoRenderer::CreateFont(double sizeInPixels
,
2575 const wxString
& facename
,
2577 const wxColour
& col
)
2579 wxGraphicsFont font
;
2580 ENSURE_LOADED_OR_RETURN(font
);
2581 font
.SetRefData(new wxCairoFontData(this, sizeInPixels
, facename
, flags
, col
));
2585 wxGraphicsBitmap
wxCairoRenderer::CreateBitmap( const wxBitmap
& bmp
)
2588 ENSURE_LOADED_OR_RETURN(p
);
2591 p
.SetRefData(new wxCairoBitmapData( this , bmp
));
2598 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromImage(const wxImage
& image
)
2600 wxGraphicsBitmap bmp
;
2602 ENSURE_LOADED_OR_RETURN(bmp
);
2606 bmp
.SetRefData(new wxCairoBitmapData(this, image
));
2612 wxImage
wxCairoRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
2615 ENSURE_LOADED_OR_RETURN(image
);
2617 const wxCairoBitmapData
* const
2618 data
= static_cast<wxCairoBitmapData
*>(bmp
.GetGraphicsData());
2620 image
= data
->ConvertToImage();
2625 #endif // wxUSE_IMAGE
2628 wxGraphicsBitmap
wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap
)
2631 ENSURE_LOADED_OR_RETURN(p
);
2632 if ( bitmap
!= NULL
)
2634 p
.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t
*) bitmap
));
2640 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap
& WXUNUSED(bitmap
),
2641 wxDouble
WXUNUSED(x
),
2642 wxDouble
WXUNUSED(y
),
2643 wxDouble
WXUNUSED(w
),
2644 wxDouble
WXUNUSED(h
))
2647 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2651 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2653 return &gs_cairoGraphicsRenderer
;
2656 #else // !wxUSE_CAIRO
2658 wxGraphicsRenderer
* wxGraphicsRenderer::GetCairoRenderer()
2663 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2665 // MSW and OS X have their own native default renderers, but the other ports
2666 // use Cairo by default
2667 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2668 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
2670 return GetCairoRenderer();
2672 #endif // !(__WXMSW__ || __WXOSX__)
2674 #endif // wxUSE_GRAPHICS_CONTEXT