Added wxGraphicsBitmap::ConvertToImage().
[wxWidgets.git] / src / generic / graphicc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2006-10-03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_GRAPHICS_CONTEXT
19
20 #include "wx/graphics.h"
21
22 #if wxUSE_CAIRO
23
24 // keep cairo.h from defining dllimport as we're defining the symbols inside
25 // the wx dll in order to load them dynamically.
26 #define cairo_public
27
28 #include "wx/cairo.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/bitmap.h"
32 #include "wx/icon.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcmemory.h"
35 #include "wx/dcprint.h"
36 #ifdef __WXGTK__
37 #include "wx/window.h"
38 #endif
39 #endif
40
41 #include "wx/private/graphics.h"
42 #include "wx/rawbmp.h"
43 #include "wx/vector.h"
44
45 using namespace std;
46
47 //-----------------------------------------------------------------------------
48 // device context implementation
49 //
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 //-----------------------------------------------------------------------------
54
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)
58
59 //-----------------------------------------------------------------------------
60 // wxGraphicsPath implementation
61 //-----------------------------------------------------------------------------
62
63 // TODO remove this dependency (gdiplus needs the macros)
64
65 #ifndef max
66 #define max(a,b) (((a) > (b)) ? (a) : (b))
67 #endif
68
69 #ifndef min
70 #define min(a,b) (((a) < (b)) ? (a) : (b))
71 #endif
72
73 #include <cairo.h>
74 #ifdef __WXMSW__
75 #include <cairo-win32.h>
76 #endif
77
78 #ifdef __WXGTK__
79 #include <gtk/gtk.h>
80 #include "wx/fontutil.h"
81 #include "wx/gtk/dc.h"
82 #endif
83
84 #ifdef __WXMSW__
85 #include <cairo-win32.h>
86 #endif
87
88 #ifdef __WXMAC__
89 #include "wx/osx/private.h"
90 #include <cairo-quartz.h>
91 #include <cairo-atsui.h>
92 #endif
93
94 class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData
95 {
96 public :
97 wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL);
98 ~wxCairoPathData();
99
100 virtual wxGraphicsObjectRefData *Clone() const;
101
102 //
103 // These are the path primitives from which everything else can be constructed
104 //
105
106 // begins a new subpath at (x,y)
107 virtual void MoveToPoint( wxDouble x, wxDouble y );
108
109 // adds a straight line from the current point to (x,y)
110 virtual void AddLineToPoint( wxDouble x, wxDouble y );
111
112 // adds a cubic Bezier curve from the current point, using two control points and an end point
113 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
114
115
116 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
117 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
118
119 // gets the last point of the current path, (0,0) if not yet set
120 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
121
122 // adds another path
123 virtual void AddPath( const wxGraphicsPathData* path );
124
125 // closes the current sub-path
126 virtual void CloseSubpath();
127
128 //
129 // These are convenience functions which - if not available natively will be assembled
130 // using the primitives from above
131 //
132
133 /*
134
135 // appends a rectangle as a new closed subpath
136 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
137 // appends an ellipsis as a new closed subpath fitting the passed rectangle
138 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
139
140 // 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)
141 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
142 */
143
144 // returns the native path
145 virtual void * GetNativePath() const ;
146
147 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
148 virtual void UnGetNativePath(void *p) const;
149
150 // transforms each point of this path by the matrix
151 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
152
153 // gets the bounding box enclosing all points (possibly including control points)
154 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
155
156 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxWINDING_RULE) const;
157
158 private :
159 cairo_t* m_pathContext;
160 };
161
162 class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData
163 {
164 public :
165 wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
166 virtual ~wxCairoMatrixData() ;
167
168 virtual wxGraphicsObjectRefData *Clone() const ;
169
170 // concatenates the matrix
171 virtual void Concat( const wxGraphicsMatrixData *t );
172
173 // sets the matrix to the respective values
174 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
175 wxDouble tx=0.0, wxDouble ty=0.0);
176
177 // gets the component valuess of the matrix
178 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
179 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
180
181 // makes this the inverse matrix
182 virtual void Invert();
183
184 // returns true if the elements of the transformation matrix are equal ?
185 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
186
187 // return true if this is the identity matrix
188 virtual bool IsIdentity() const;
189
190 //
191 // transformation
192 //
193
194 // add the translation to this matrix
195 virtual void Translate( wxDouble dx , wxDouble dy );
196
197 // add the scale to this matrix
198 virtual void Scale( wxDouble xScale , wxDouble yScale );
199
200 // add the rotation to this matrix (radians)
201 virtual void Rotate( wxDouble angle );
202
203 //
204 // apply the transforms
205 //
206
207 // applies that matrix to the point
208 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
209
210 // applies the matrix except for translations
211 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
212
213 // returns the native representation
214 virtual void * GetNativeMatrix() const;
215 private:
216 cairo_matrix_t m_matrix ;
217 } ;
218
219 class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
220 {
221 public:
222 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
223 ~wxCairoPenData();
224
225 void Init();
226
227 virtual void Apply( wxGraphicsContext* context );
228 virtual wxDouble GetWidth() { return m_width; }
229
230 private :
231 double m_width;
232
233 double m_red;
234 double m_green;
235 double m_blue;
236 double m_alpha;
237
238 cairo_line_cap_t m_cap;
239 cairo_line_join_t m_join;
240
241 int m_count;
242 const double *m_lengths;
243 double *m_userLengths;
244
245 wxPen m_pen;
246
247 wxDECLARE_NO_COPY_CLASS(wxCairoPenData);
248 };
249
250 class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
251 {
252 public:
253 wxCairoBrushData( wxGraphicsRenderer* renderer );
254 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
255 ~wxCairoBrushData ();
256
257 virtual void Apply( wxGraphicsContext* context );
258
259 void CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
260 wxDouble x2, wxDouble y2,
261 const wxGraphicsGradientStops& stops);
262 void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
263 wxDouble xc, wxDouble yc, wxDouble radius,
264 const wxGraphicsGradientStops& stops);
265
266 protected:
267 virtual void Init();
268
269 // common part of Create{Linear,Radial}GradientBrush()
270 void AddGradientStops(const wxGraphicsGradientStops& stops);
271
272 private :
273 double m_red;
274 double m_green;
275 double m_blue;
276 double m_alpha;
277
278 cairo_pattern_t* m_brushPattern;
279 };
280
281 class wxCairoFontData : public wxGraphicsObjectRefData
282 {
283 public:
284 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
285 ~wxCairoFontData();
286
287 virtual void Apply( wxGraphicsContext* context );
288 #ifdef __WXGTK__
289 const PangoFontDescription* GetFont() const { return m_font; }
290 bool GetUnderlined() const { return m_underlined; }
291 #endif
292 private :
293 double m_size;
294 bool m_underlined;
295 double m_red;
296 double m_green;
297 double m_blue;
298 double m_alpha;
299 #ifdef __WXMAC__
300 cairo_font_face_t *m_font;
301 #elif defined(__WXGTK__)
302 PangoFontDescription* m_font;
303 #else
304 wxCharBuffer m_fontName;
305 cairo_font_slant_t m_slant;
306 cairo_font_weight_t m_weight;
307 #endif
308 };
309
310 class wxCairoBitmapData : public wxGraphicsObjectRefData
311 {
312 public:
313 wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp );
314 wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap );
315 ~wxCairoBitmapData();
316
317 virtual cairo_surface_t* GetCairoSurface() { return m_surface; }
318 virtual cairo_pattern_t* GetCairoPattern() { return m_pattern; }
319 virtual wxSize GetSize() { return wxSize(m_width, m_height); }
320
321 #if wxUSE_IMAGE
322 wxImage ConvertToImage() const;
323 #endif // wxUSE_IMAGE
324
325 private :
326 // Allocate m_buffer for the bitmap of the given size in the given format.
327 //
328 // Returns the stride used for the buffer.
329 int InitBuffer(int width, int height, cairo_format_t format);
330
331 // Really create the surface using the buffer (which was supposed to be
332 // filled since InitBuffer() call).
333 void InitSurface(cairo_format_t format, int stride);
334
335
336 cairo_surface_t* m_surface;
337 cairo_pattern_t* m_pattern;
338 int m_width;
339 int m_height;
340 unsigned char* m_buffer;
341 };
342
343 class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
344 {
345 public:
346 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
347 wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc );
348 wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc );
349 #ifdef __WXGTK__
350 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
351 #endif
352 #ifdef __WXMSW__
353 wxCairoContext( wxGraphicsRenderer* renderer, HDC context );
354 #endif
355 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
356 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
357 wxCairoContext();
358 virtual ~wxCairoContext();
359
360 virtual bool ShouldOffset() const
361 {
362 if ( !m_enableOffset )
363 return false;
364
365 int penwidth = 0 ;
366 if ( !m_pen.IsNull() )
367 {
368 penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth();
369 if ( penwidth == 0 )
370 penwidth = 1;
371 }
372 return ( penwidth % 2 ) == 1;
373 }
374
375 virtual void Clip( const wxRegion &region );
376 #ifdef __WXMSW__
377 cairo_surface_t* m_mswSurface;
378 #endif
379
380 // clips drawings to the rect
381 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
382
383 // resets the clipping to original extent
384 virtual void ResetClip();
385
386 virtual void * GetNativeContext();
387
388 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
389
390 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation);
391
392 virtual bool SetCompositionMode(wxCompositionMode op);
393
394 virtual void BeginLayer(wxDouble opacity);
395
396 virtual void EndLayer();
397
398 virtual void StrokePath( const wxGraphicsPath& p );
399 virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxWINDING_RULE );
400
401 virtual void Translate( wxDouble dx , wxDouble dy );
402 virtual void Scale( wxDouble xScale , wxDouble yScale );
403 virtual void Rotate( wxDouble angle );
404
405 // concatenates this transform with the current transform of this context
406 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
407
408 // sets the transform of this context
409 virtual void SetTransform( const wxGraphicsMatrix& matrix );
410
411 // gets the matrix of this context
412 virtual wxGraphicsMatrix GetTransform() const;
413
414 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
415 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
416 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
417 virtual void PushState();
418 virtual void PopState();
419
420 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
421 wxDouble *descent, wxDouble *externalLeading ) const;
422 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
423
424 protected:
425 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
426
427 private:
428 void Init(cairo_t *context);
429
430 cairo_t* m_context;
431
432 wxVector<float> m_layerOpacities;
433
434 wxDECLARE_NO_COPY_CLASS(wxCairoContext);
435 };
436
437 //-----------------------------------------------------------------------------
438 // wxCairoPenData implementation
439 //-----------------------------------------------------------------------------
440
441 wxCairoPenData::~wxCairoPenData()
442 {
443 delete[] m_userLengths;
444 }
445
446 void wxCairoPenData::Init()
447 {
448 m_lengths = NULL;
449 m_userLengths = NULL;
450 m_width = 0;
451 m_count = 0;
452 }
453
454 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
455 : wxGraphicsObjectRefData(renderer)
456 {
457 Init();
458 m_pen = pen;
459 m_width = m_pen.GetWidth();
460 if (m_width <= 0.0)
461 m_width = 0.1;
462
463 m_red = m_pen.GetColour().Red()/255.0;
464 m_green = m_pen.GetColour().Green()/255.0;
465 m_blue = m_pen.GetColour().Blue()/255.0;
466 m_alpha = m_pen.GetColour().Alpha()/255.0;
467
468 switch ( m_pen.GetCap() )
469 {
470 case wxCAP_ROUND :
471 m_cap = CAIRO_LINE_CAP_ROUND;
472 break;
473
474 case wxCAP_PROJECTING :
475 m_cap = CAIRO_LINE_CAP_SQUARE;
476 break;
477
478 case wxCAP_BUTT :
479 m_cap = CAIRO_LINE_CAP_BUTT;
480 break;
481
482 default :
483 m_cap = CAIRO_LINE_CAP_BUTT;
484 break;
485 }
486
487 switch ( m_pen.GetJoin() )
488 {
489 case wxJOIN_BEVEL :
490 m_join = CAIRO_LINE_JOIN_BEVEL;
491 break;
492
493 case wxJOIN_MITER :
494 m_join = CAIRO_LINE_JOIN_MITER;
495 break;
496
497 case wxJOIN_ROUND :
498 m_join = CAIRO_LINE_JOIN_ROUND;
499 break;
500
501 default :
502 m_join = CAIRO_LINE_JOIN_MITER;
503 break;
504 }
505
506 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
507 const double dotted[] =
508 {
509 dashUnit , dashUnit + 2.0
510 };
511 static const double short_dashed[] =
512 {
513 9.0 , 6.0
514 };
515 static const double dashed[] =
516 {
517 19.0 , 9.0
518 };
519 static const double dotted_dashed[] =
520 {
521 9.0 , 6.0 , 3.0 , 3.0
522 };
523
524 switch ( m_pen.GetStyle() )
525 {
526 case wxPENSTYLE_SOLID :
527 break;
528
529 case wxPENSTYLE_DOT :
530 m_count = WXSIZEOF(dotted);
531 m_userLengths = new double[ m_count ] ;
532 memcpy( m_userLengths, dotted, sizeof(dotted) );
533 m_lengths = m_userLengths;
534 break;
535
536 case wxPENSTYLE_LONG_DASH :
537 m_lengths = dashed ;
538 m_count = WXSIZEOF(dashed);
539 break;
540
541 case wxPENSTYLE_SHORT_DASH :
542 m_lengths = short_dashed ;
543 m_count = WXSIZEOF(short_dashed);
544 break;
545
546 case wxPENSTYLE_DOT_DASH :
547 m_lengths = dotted_dashed ;
548 m_count = WXSIZEOF(dotted_dashed);
549 break;
550
551 case wxPENSTYLE_USER_DASH :
552 {
553 wxDash *wxdashes ;
554 m_count = m_pen.GetDashes( &wxdashes ) ;
555 if ((wxdashes != NULL) && (m_count > 0))
556 {
557 m_userLengths = new double[m_count] ;
558 for ( int i = 0 ; i < m_count ; ++i )
559 {
560 m_userLengths[i] = wxdashes[i] * dashUnit ;
561
562 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
563 m_userLengths[i] = dashUnit + 2.0 ;
564 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
565 m_userLengths[i] = dashUnit ;
566 }
567 }
568 m_lengths = m_userLengths ;
569 }
570 break;
571 case wxPENSTYLE_STIPPLE :
572 {
573 /*
574 wxBitmap* bmp = pen.GetStipple();
575 if ( bmp && bmp->IsOk() )
576 {
577 wxDELETE( m_penImage );
578 wxDELETE( m_penBrush );
579 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
580 m_penBrush = new TextureBrush(m_penImage);
581 m_pen->SetBrush( m_penBrush );
582 }
583 */
584 }
585 break;
586 default :
587 if ( m_pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH
588 && m_pen.GetStyle() <= wxPENSTYLE_LAST_HATCH )
589 {
590 /*
591 wxDELETE( m_penBrush );
592 HatchStyle style = HatchStyleHorizontal;
593 switch( pen.GetStyle() )
594 {
595 case wxPENSTYLE_BDIAGONAL_HATCH :
596 style = HatchStyleBackwardDiagonal;
597 break ;
598 case wxPENSTYLE_CROSSDIAG_HATCH :
599 style = HatchStyleDiagonalCross;
600 break ;
601 case wxPENSTYLE_FDIAGONAL_HATCH :
602 style = HatchStyleForwardDiagonal;
603 break ;
604 case wxPENSTYLE_CROSS_HATCH :
605 style = HatchStyleCross;
606 break ;
607 case wxPENSTYLE_HORIZONTAL_HATCH :
608 style = HatchStyleHorizontal;
609 break ;
610 case wxPENSTYLE_VERTICAL_HATCH :
611 style = HatchStyleVertical;
612 break ;
613
614 }
615 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
616 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
617 m_pen->SetBrush( m_penBrush )
618 */
619 }
620 break;
621 }
622 }
623
624 void wxCairoPenData::Apply( wxGraphicsContext* context )
625 {
626 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
627 cairo_set_line_width(ctext,m_width);
628 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
629 cairo_set_line_cap(ctext,m_cap);
630 cairo_set_line_join(ctext,m_join);
631 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
632 }
633
634 //-----------------------------------------------------------------------------
635 // wxCairoBrushData implementation
636 //-----------------------------------------------------------------------------
637
638 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
639 : wxGraphicsObjectRefData( renderer )
640 {
641 Init();
642 }
643
644 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
645 : wxGraphicsObjectRefData(renderer)
646 {
647 Init();
648
649 m_red = brush.GetColour().Red()/255.0;
650 m_green = brush.GetColour().Green()/255.0;
651 m_blue = brush.GetColour().Blue()/255.0;
652 m_alpha = brush.GetColour().Alpha()/255.0;
653 /*
654 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
655 {
656 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
657 brush.GetColour().Green() , brush.GetColour().Blue() ) );
658 }
659 else if ( brush.IsHatch() )
660 {
661 HatchStyle style = HatchStyleHorizontal;
662 switch( brush.GetStyle() )
663 {
664 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
665 style = HatchStyleBackwardDiagonal;
666 break ;
667 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
668 style = HatchStyleDiagonalCross;
669 break ;
670 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
671 style = HatchStyleForwardDiagonal;
672 break ;
673 case wxBRUSHSTYLE_CROSS_HATCH :
674 style = HatchStyleCross;
675 break ;
676 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
677 style = HatchStyleHorizontal;
678 break ;
679 case wxBRUSHSTYLE_VERTICAL_HATCH :
680 style = HatchStyleVertical;
681 break ;
682
683 }
684 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
685 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
686 }
687 else
688 {
689 wxBitmap* bmp = brush.GetStipple();
690 if ( bmp && bmp->IsOk() )
691 {
692 wxDELETE( m_brushImage );
693 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
694 m_brush = new TextureBrush(m_brushImage);
695 }
696 }
697 */
698 }
699
700 wxCairoBrushData::~wxCairoBrushData ()
701 {
702 if (m_brushPattern)
703 cairo_pattern_destroy(m_brushPattern);
704 }
705
706 void wxCairoBrushData::Apply( wxGraphicsContext* context )
707 {
708 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
709 if ( m_brushPattern )
710 {
711 cairo_set_source(ctext,m_brushPattern);
712 }
713 else
714 {
715 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
716 }
717 }
718
719 void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops& stops)
720 {
721 // loop over all the stops, they include the beginning and ending ones
722 const unsigned numStops = stops.GetCount();
723 for ( unsigned n = 0; n < numStops; n++ )
724 {
725 const wxGraphicsGradientStop stop = stops.Item(n);
726
727 const wxColour col = stop.GetColour();
728
729 cairo_pattern_add_color_stop_rgba
730 (
731 m_brushPattern,
732 stop.GetPosition(),
733 col.Red()/255.0,
734 col.Green()/255.0,
735 col.Blue()/255.0,
736 col.Alpha()/255.0
737 );
738 }
739
740 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS,
741 wxT("Couldn't create cairo pattern"));
742 }
743
744 void
745 wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
746 wxDouble x2, wxDouble y2,
747 const wxGraphicsGradientStops& stops)
748 {
749 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
750
751 AddGradientStops(stops);
752 }
753
754 void
755 wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
756 wxDouble xc, wxDouble yc,
757 wxDouble radius,
758 const wxGraphicsGradientStops& stops)
759 {
760 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
761
762 AddGradientStops(stops);
763 }
764
765 void wxCairoBrushData::Init()
766 {
767 m_brushPattern = NULL;
768 }
769
770 //-----------------------------------------------------------------------------
771 // wxCairoFontData implementation
772 //-----------------------------------------------------------------------------
773
774 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
775 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
776 {
777 m_red = col.Red()/255.0;
778 m_green = col.Green()/255.0;
779 m_blue = col.Blue()/255.0;
780 m_alpha = col.Alpha()/255.0;
781 m_size = font.GetPointSize();
782 m_underlined = font.GetUnderlined();
783
784 #ifdef __WXMAC__
785 m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() );
786 #elif defined(__WXGTK__)
787 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
788 #else
789 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
790 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
791 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
792 #endif
793 }
794
795 wxCairoFontData::~wxCairoFontData()
796 {
797 #ifdef __WXMAC__
798 cairo_font_face_destroy( m_font );
799 #elif defined(__WXGTK__)
800 pango_font_description_free( m_font );
801 #else
802 #endif
803 }
804
805 void wxCairoFontData::Apply( wxGraphicsContext* context )
806 {
807 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
808 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
809 #ifdef __WXGTK__
810 // the rest is done using Pango layouts
811 #elif defined(__WXMAC__)
812 cairo_set_font_face(ctext, m_font);
813 cairo_set_font_size(ctext, m_size );
814 #else
815 cairo_select_font_face(ctext, m_fontName, m_slant, m_weight );
816 cairo_set_font_size(ctext, m_size );
817 #endif
818 }
819
820 //-----------------------------------------------------------------------------
821 // wxCairoPathData implementation
822 //-----------------------------------------------------------------------------
823
824 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
825 : wxGraphicsPathData(renderer)
826 {
827 if (pathcontext)
828 {
829 m_pathContext = pathcontext;
830 }
831 else
832 {
833 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
834 m_pathContext = cairo_create(surface);
835 cairo_surface_destroy (surface);
836 }
837 }
838
839 wxCairoPathData::~wxCairoPathData()
840 {
841 cairo_destroy(m_pathContext);
842 }
843
844 wxGraphicsObjectRefData *wxCairoPathData::Clone() const
845 {
846 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
847 cairo_t* pathcontext = cairo_create(surface);
848 cairo_surface_destroy (surface);
849
850 cairo_path_t* path = cairo_copy_path(m_pathContext);
851 cairo_append_path(pathcontext, path);
852 cairo_path_destroy(path);
853 return new wxCairoPathData( GetRenderer() ,pathcontext);
854 }
855
856
857 void* wxCairoPathData::GetNativePath() const
858 {
859 return cairo_copy_path(m_pathContext) ;
860 }
861
862 void wxCairoPathData::UnGetNativePath(void *p) const
863 {
864 cairo_path_destroy((cairo_path_t*)p);
865 }
866
867 //
868 // The Primitives
869 //
870
871 void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
872 {
873 cairo_move_to(m_pathContext,x,y);
874 }
875
876 void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
877 {
878 cairo_line_to(m_pathContext,x,y);
879 }
880
881 void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
882 {
883 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
884 cairo_append_path(m_pathContext, p);
885 UnGetNativePath(p);
886 }
887
888 void wxCairoPathData::CloseSubpath()
889 {
890 cairo_close_path(m_pathContext);
891 }
892
893 void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
894 {
895 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
896 }
897
898 // gets the last point of the current path, (0,0) if not yet set
899 void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
900 {
901 double dx,dy;
902 cairo_get_current_point(m_pathContext,&dx,&dy);
903 if (x)
904 *x = dx;
905 if (y)
906 *y = dy;
907 }
908
909 void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
910 {
911 // as clockwise means positive in our system (y pointing downwards)
912 // TODO make this interpretation dependent of the
913 // real device trans
914 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
915 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
916 else
917 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
918 }
919
920 // transforms each point of this path by the matrix
921 void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
922 {
923 // as we don't have a true path object, we have to apply the inverse
924 // matrix to the context
925 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
926 cairo_matrix_invert( &m );
927 cairo_transform(m_pathContext,&m);
928 }
929
930 // gets the bounding box enclosing all points (possibly including control points)
931 void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
932 {
933 double x1,y1,x2,y2;
934
935 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
936 if ( x2 < x1 )
937 {
938 *x = x2;
939 *w = x1-x2;
940 }
941 else
942 {
943 *x = x1;
944 *w = x2-x1;
945 }
946
947 if( y2 < y1 )
948 {
949 *y = y2;
950 *h = y1-y2;
951 }
952 else
953 {
954 *y = y1;
955 *h = y2-y1;
956 }
957 }
958
959 bool wxCairoPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
960 {
961 cairo_set_fill_rule(m_pathContext,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
962 return cairo_in_fill( m_pathContext, x, y) != 0;
963 }
964
965 //-----------------------------------------------------------------------------
966 // wxCairoMatrixData implementation
967 //-----------------------------------------------------------------------------
968
969 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
970 : wxGraphicsMatrixData(renderer)
971 {
972 if ( matrix )
973 m_matrix = *matrix;
974 }
975
976 wxCairoMatrixData::~wxCairoMatrixData()
977 {
978 // nothing to do
979 }
980
981 wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
982 {
983 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
984 }
985
986 // concatenates the matrix
987 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
988 {
989 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
990 }
991
992 // sets the matrix to the respective values
993 void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
994 wxDouble tx, wxDouble ty)
995 {
996 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
997 }
998
999 // gets the component valuess of the matrix
1000 void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1001 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1002 {
1003 if (a) *a = m_matrix.xx;
1004 if (b) *b = m_matrix.yx;
1005 if (c) *c = m_matrix.xy;
1006 if (d) *d = m_matrix.yy;
1007 if (tx) *tx= m_matrix.x0;
1008 if (ty) *ty= m_matrix.y0;
1009 }
1010
1011 // makes this the inverse matrix
1012 void wxCairoMatrixData::Invert()
1013 {
1014 cairo_matrix_invert( &m_matrix );
1015 }
1016
1017 // returns true if the elements of the transformation matrix are equal ?
1018 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
1019 {
1020 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
1021 return (
1022 m_matrix.xx == tm->xx &&
1023 m_matrix.yx == tm->yx &&
1024 m_matrix.xy == tm->xy &&
1025 m_matrix.yy == tm->yy &&
1026 m_matrix.x0 == tm->x0 &&
1027 m_matrix.y0 == tm->y0 ) ;
1028 }
1029
1030 // return true if this is the identity matrix
1031 bool wxCairoMatrixData::IsIdentity() const
1032 {
1033 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
1034 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
1035 }
1036
1037 //
1038 // transformation
1039 //
1040
1041 // add the translation to this matrix
1042 void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
1043 {
1044 cairo_matrix_translate( &m_matrix, dx, dy) ;
1045 }
1046
1047 // add the scale to this matrix
1048 void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1049 {
1050 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
1051 }
1052
1053 // add the rotation to this matrix (radians)
1054 void wxCairoMatrixData::Rotate( wxDouble angle )
1055 {
1056 cairo_matrix_rotate( &m_matrix, angle) ;
1057 }
1058
1059 //
1060 // apply the transforms
1061 //
1062
1063 // applies that matrix to the point
1064 void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1065 {
1066 double lx = *x, ly = *y ;
1067 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
1068 *x = lx;
1069 *y = ly;
1070 }
1071
1072 // applies the matrix except for translations
1073 void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1074 {
1075 double lx = *dx, ly = *dy ;
1076 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1077 *dx = lx;
1078 *dy = ly;
1079 }
1080
1081 // returns the native representation
1082 void * wxCairoMatrixData::GetNativeMatrix() const
1083 {
1084 return (void*) &m_matrix;
1085 }
1086
1087 // ----------------------------------------------------------------------------
1088 // wxCairoBitmap implementation
1089 // ----------------------------------------------------------------------------
1090
1091 int wxCairoBitmapData::InitBuffer(int width, int height, cairo_format_t format)
1092 {
1093 wxUnusedVar(format); // Only really unused with Cairo < 1.6.
1094
1095 // Determine the stride: use cairo_format_stride_for_width() if available
1096 // but fall back to 4*width for the earlier versions as this is what that
1097 // function always returns, even in latest Cairo, anyhow.
1098 int stride;
1099 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1100 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1101 {
1102 stride = cairo_format_stride_for_width(format, width);
1103
1104 // All our code would totally break if stride were not a multiple of 4
1105 // so ensure this is the case.
1106 if ( stride % 4 )
1107 {
1108 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1109
1110 stride += 4 - stride % 4;
1111 }
1112 }
1113 else
1114 #endif
1115 stride = 4*width;
1116
1117 m_width = width;
1118 m_height = height;
1119 m_buffer = new unsigned char[height*stride];
1120
1121 return stride;
1122 }
1123
1124 void wxCairoBitmapData::InitSurface(cairo_format_t format, int stride)
1125 {
1126 m_surface = cairo_image_surface_create_for_data(
1127 m_buffer, format, m_width, m_height, stride);
1128 m_pattern = cairo_pattern_create_for_surface(m_surface);
1129 }
1130
1131 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ) :
1132 wxGraphicsObjectRefData( renderer )
1133 {
1134 m_surface = bitmap;
1135 m_pattern = cairo_pattern_create_for_surface(m_surface);
1136 }
1137
1138 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer )
1139 {
1140 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1141
1142 #ifdef wxHAS_RAW_BITMAP
1143 // Create a surface object and copy the bitmap pixel data to it. if the
1144 // image has alpha (or a mask represented as alpha) then we'll use a
1145 // different format and iterator than if it doesn't...
1146 const cairo_format_t bufferFormat = bmp.GetDepth() == 32
1147 #ifdef __WXGTK__
1148 || bmp.GetMask()
1149 #endif
1150 ? CAIRO_FORMAT_ARGB32
1151 : CAIRO_FORMAT_RGB24;
1152
1153 int stride = InitBuffer(bmp.GetWidth(), bmp.GetHeight(), bufferFormat);
1154
1155 int bw = m_width;
1156 int bh = m_height;
1157 wxBitmap bmpSource = bmp; // we need a non-const instance
1158 wxUint32* data = (wxUint32*)m_buffer;
1159
1160 if ( bufferFormat == CAIRO_FORMAT_ARGB32 )
1161 {
1162 // use the bitmap's alpha
1163 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1164 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1165
1166 wxAlphaPixelData::Iterator p(pixData);
1167 for (int y=0; y<bh; y++)
1168 {
1169 wxAlphaPixelData::Iterator rowStart = p;
1170 wxUint32* const rowStartDst = data;
1171 for (int x=0; x<bw; x++)
1172 {
1173 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1174 // with alpha in the upper 8 bits, then red, then green, then
1175 // blue. The 32-bit quantities are stored native-endian.
1176 // Pre-multiplied alpha is used.
1177 unsigned char alpha = p.Alpha();
1178 if (alpha == 0)
1179 *data = 0;
1180 else
1181 *data = ( alpha << 24
1182 | (p.Red() * alpha/255) << 16
1183 | (p.Green() * alpha/255) << 8
1184 | (p.Blue() * alpha/255) );
1185 ++data;
1186 ++p;
1187 }
1188
1189 data = rowStartDst + stride / 4;
1190 p = rowStart;
1191 p.OffsetY(pixData, 1);
1192 }
1193 }
1194 else // no alpha
1195 {
1196 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1197 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1198
1199 wxNativePixelData::Iterator p(pixData);
1200 for (int y=0; y<bh; y++)
1201 {
1202 wxNativePixelData::Iterator rowStart = p;
1203 wxUint32* const rowStartDst = data;
1204 for (int x=0; x<bw; x++)
1205 {
1206 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1207 // the upper 8 bits unused. Red, Green, and Blue are stored in
1208 // the remaining 24 bits in that order. The 32-bit quantities
1209 // are stored native-endian.
1210 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1211 ++data;
1212 ++p;
1213 }
1214
1215 data = rowStartDst + stride / 4;
1216 p = rowStart;
1217 p.OffsetY(pixData, 1);
1218 }
1219 }
1220 #ifdef __WXMSW__
1221 // if there is a mask, set the alpha bytes in the target buffer to
1222 // fully transparent or fully opaque
1223 if (bmpSource.GetMask())
1224 {
1225 wxBitmap bmpMask = bmpSource.GetMaskBitmap();
1226 bufferFormat = CAIRO_FORMAT_ARGB32;
1227 data = (wxUint32*)m_buffer;
1228 wxNativePixelData pixData(bmpMask, wxPoint(0,0), wxSize(bw, bh));
1229 wxCHECK_RET( pixData, wxT("Failed to gain raw access to mask bitmap data."));
1230
1231 wxNativePixelData::Iterator p(pixData);
1232 for (int y=0; y<bh; y++)
1233 {
1234 wxNativePixelData::Iterator rowStart = p;
1235 wxUint32* const rowStartDst = data;
1236 for (int x=0; x<bw; x++)
1237 {
1238 if (p.Red()+p.Green()+p.Blue() == 0)
1239 *data = 0;
1240 else
1241 *data = (wxALPHA_OPAQUE << 24) | (*data & 0x00FFFFFF);
1242 ++data;
1243 ++p;
1244 }
1245
1246 data = rowStartDst + stride / 4;
1247 p = rowStart;
1248 p.OffsetY(pixData, 1);
1249 }
1250 }
1251 #endif
1252
1253 InitSurface(bufferFormat, stride);
1254 #endif // wxHAS_RAW_BITMAP
1255 }
1256
1257 #if wxUSE_IMAGE
1258
1259 // Helper functions for dealing with alpha pre-multiplication.
1260 namespace
1261 {
1262
1263 inline unsigned char Premultiply(unsigned char alpha, unsigned char data)
1264 {
1265 return alpha ? (data * alpha)/0xff : data;
1266 }
1267
1268 inline unsigned char Unpremultiply(unsigned char alpha, unsigned char data)
1269 {
1270 return alpha ? (data * 0xff)/alpha : data;
1271 }
1272
1273 } // anonymous namespace
1274
1275 wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer* renderer,
1276 const wxImage& image)
1277 : wxGraphicsObjectRefData(renderer)
1278 {
1279 const cairo_format_t bufferFormat = image.HasAlpha()
1280 ? CAIRO_FORMAT_ARGB32
1281 : CAIRO_FORMAT_RGB24;
1282
1283 InitBuffer(image.GetWidth(), image.GetHeight(), bufferFormat);
1284
1285 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1286 // values and not bytes becase Cairo always works with buffers in native
1287 // endianness.
1288 wxUint32* dst = reinterpret_cast<wxUint32*>(m_buffer);
1289 const unsigned char* src = image.GetData();
1290
1291 if ( bufferFormat == CAIRO_FORMAT_ARGB32 )
1292 {
1293 const unsigned char* alpha = image.GetAlpha();
1294
1295 for ( int y = 0; y < m_height; y++ )
1296 {
1297 for ( int x = 0; x < m_width; x++ )
1298 {
1299 const unsigned char a = *alpha++;
1300
1301 *dst++ = a << 24 |
1302 Premultiply(a, src[0]) << 16 |
1303 Premultiply(a, src[1]) << 8 |
1304 Premultiply(a, src[2]);
1305 src += 3;
1306 }
1307 }
1308 }
1309 else // RGB
1310 {
1311 for ( int y = 0; y < m_height; y++ )
1312 {
1313 for ( int x = 0; x < m_width; x++ )
1314 {
1315 *dst++ = src[0] << 16 |
1316 src[1] << 8 |
1317 src[2];
1318 src += 3;
1319 }
1320 }
1321 }
1322
1323 InitSurface(bufferFormat);
1324 }
1325
1326 wxImage wxCairoBitmapData::ConvertToImage() const
1327 {
1328 wxImage image(m_width, m_height, false /* don't clear */);
1329
1330 // Get the surface type and format.
1331 wxCHECK_MSG( cairo_surface_get_type(m_surface) == CAIRO_SURFACE_TYPE_IMAGE,
1332 wxNullImage,
1333 wxS("Can't convert non-image surface to image.") );
1334
1335 switch ( cairo_image_surface_get_format(m_surface) )
1336 {
1337 case CAIRO_FORMAT_ARGB32:
1338 image.SetAlpha();
1339 break;
1340
1341 case CAIRO_FORMAT_RGB24:
1342 // Nothing to do, we don't use alpha by default.
1343 break;
1344
1345 case CAIRO_FORMAT_A8:
1346 case CAIRO_FORMAT_A1:
1347 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1348 return wxNullImage;
1349
1350 default:
1351 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1352 return wxNullImage;
1353 }
1354
1355 // Prepare for copying data.
1356 const wxUint32* src = (wxUint32*)cairo_image_surface_get_data(m_surface);
1357 wxCHECK_MSG( src, wxNullImage, wxS("Failed to get Cairo surface data.") );
1358
1359 int stride = cairo_image_surface_get_stride(m_surface);
1360 wxCHECK_MSG( stride > 0, wxNullImage,
1361 wxS("Failed to get Cairo surface stride.") );
1362
1363 // As we work with wxUint32 pointers and not char ones, we need to adjust
1364 // the stride accordingly. This should be lossless as the stride must be a
1365 // multiple of pixel size.
1366 wxASSERT_MSG( !(stride % sizeof(wxUint32)), wxS("Unexpected stride.") );
1367 stride /= sizeof(wxUint32);
1368
1369 unsigned char* dst = image.GetData();
1370 unsigned char *alpha = image.GetAlpha();
1371 if ( alpha )
1372 {
1373 // We need to also copy alpha and undo the pre-multiplication as Cairo
1374 // stores pre-multiplied values in this format while wxImage does not.
1375 for ( int y = 0; y < m_height; y++ )
1376 {
1377 const wxUint32* const rowStart = src;
1378 for ( int x = 0; x < m_width; x++ )
1379 {
1380 const wxUint32 argb = *src++;
1381
1382 *alpha++ = (argb & 0xff000000) >> 24;
1383
1384 // Copy the RGB data undoing the pre-multiplication.
1385 *dst++ = Unpremultiply(*alpha, (argb & 0x00ff0000) >> 16);
1386 *dst++ = Unpremultiply(*alpha, (argb & 0x0000ff00) >> 8);
1387 *dst++ = Unpremultiply(*alpha, (argb & 0x000000ff));
1388 }
1389
1390 src = rowStart + stride;
1391 }
1392 }
1393 else // RGB
1394 {
1395 // Things are pretty simple in this case, just copy RGB bytes.
1396 for ( int y = 0; y < m_height; y++ )
1397 {
1398 const wxUint32* const rowStart = src;
1399 for ( int x = 0; x < m_width; x++ )
1400 {
1401 const wxUint32 argb = *src++;
1402
1403 *dst++ = (argb & 0x00ff0000) >> 16;
1404 *dst++ = (argb & 0x0000ff00) >> 8;
1405 *dst++ = (argb & 0x000000ff);
1406 }
1407
1408 src = rowStart + stride;
1409 }
1410 }
1411
1412 return image;
1413 }
1414
1415 #endif // wxUSE_IMAGE
1416
1417 wxCairoBitmapData::~wxCairoBitmapData()
1418 {
1419 if (m_pattern)
1420 cairo_pattern_destroy(m_pattern);
1421
1422 if (m_surface)
1423 cairo_surface_destroy(m_surface);
1424
1425 delete [] m_buffer;
1426 }
1427
1428 // ----------------------------------------------------------------------------
1429 // wxGraphicsBitmap implementation
1430 // ----------------------------------------------------------------------------
1431
1432 #if wxUSE_IMAGE
1433
1434 wxImage wxGraphicsBitmap::ConvertToImage() const
1435 {
1436 const wxCairoBitmapData* const
1437 data = static_cast<wxCairoBitmapData*>(GetGraphicsData());
1438
1439 return data ? data->ConvertToImage() : wxNullImage;
1440 }
1441
1442 #endif // wxUSE_IMAGE
1443
1444 //-----------------------------------------------------------------------------
1445 // wxCairoContext implementation
1446 //-----------------------------------------------------------------------------
1447
1448 class wxCairoOffsetHelper
1449 {
1450 public :
1451 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1452 {
1453 m_ctx = ctx;
1454 m_offset = offset;
1455 if ( m_offset )
1456 cairo_translate( m_ctx, 0.5, 0.5 );
1457 }
1458 ~wxCairoOffsetHelper( )
1459 {
1460 if ( m_offset )
1461 cairo_translate( m_ctx, -0.5, -0.5 );
1462 }
1463 public :
1464 cairo_t* m_ctx;
1465 bool m_offset;
1466 } ;
1467
1468 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc )
1469 : wxGraphicsContext(renderer)
1470 {
1471 #ifdef __WXMSW__
1472 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1473 // text rendering when printing using Cairo. Switch it to MM_TEXT
1474 // map mode to avoid this problem.
1475 HDC hdc = (HDC)dc.GetHDC();
1476 ::SetMapMode(hdc, MM_TEXT);
1477 m_mswSurface = cairo_win32_printing_surface_create(hdc);
1478 Init( cairo_create(m_mswSurface) );
1479 #endif
1480
1481 #ifdef __WXGTK20__
1482 const wxDCImpl *impl = dc.GetImpl();
1483 Init( (cairo_t*) impl->GetCairoContext() );
1484 #endif
1485 wxSize sz = dc.GetSize();
1486 m_width = sz.x;
1487 m_height = sz.y;
1488
1489 wxPoint org = dc.GetDeviceOrigin();
1490 cairo_translate( m_context, org.x, org.y );
1491
1492 double sx,sy;
1493 dc.GetUserScale( &sx, &sy );
1494
1495 // TODO: Determine if these fixes are needed on other platforms too.
1496 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1497 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1498 // drawing code
1499 #ifdef __WXMSW__
1500 double lsx,lsy;
1501 dc.GetLogicalScale( &lsx, &lsy );
1502 sx *= lsx;
1503 sy *= lsy;
1504 #endif
1505 cairo_scale( m_context, sx, sy );
1506
1507 org = dc.GetLogicalOrigin();
1508 cairo_translate( m_context, -org.x, -org.y );
1509 }
1510
1511 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1512 : wxGraphicsContext(renderer)
1513 {
1514 int width, height;
1515 dc.GetSize( &width, &height );
1516 m_width = width;
1517 m_height = height;
1518
1519 m_enableOffset = true;
1520
1521 #ifdef __WXMSW__
1522 m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC());
1523 Init( cairo_create(m_mswSurface) );
1524 #endif
1525
1526 #ifdef __WXGTK20__
1527 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1528 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1529
1530 #if 0
1531 wxGraphicsMatrix matrix = CreateMatrix();
1532
1533 wxPoint org = dc.GetDeviceOrigin();
1534 matrix.Translate( org.x, org.y );
1535
1536 org = dc.GetLogicalOrigin();
1537 matrix.Translate( -org.x, -org.y );
1538
1539 double sx,sy;
1540 dc.GetUserScale( &sx, &sy );
1541 matrix.Scale( sx, sy );
1542
1543 ConcatTransform( matrix );
1544 #endif
1545 #endif
1546
1547 #ifdef __WXMAC__
1548 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1549 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1550 Init( cairo_create( surface ) );
1551 cairo_surface_destroy( surface );
1552 #endif
1553 }
1554
1555 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
1556 : wxGraphicsContext(renderer)
1557 {
1558 int width, height;
1559 dc.GetSize( &width, &height );
1560 m_width = width;
1561 m_height = height;
1562
1563 m_enableOffset = true;
1564
1565 #ifdef __WXMSW__
1566 m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC());
1567 Init( cairo_create(m_mswSurface) );
1568 #endif
1569
1570 #ifdef __WXGTK20__
1571 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1572 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1573
1574 #if 0
1575 wxGraphicsMatrix matrix = CreateMatrix();
1576
1577 wxPoint org = dc.GetDeviceOrigin();
1578 matrix.Translate( org.x, org.y );
1579
1580 org = dc.GetLogicalOrigin();
1581 matrix.Translate( -org.x, -org.y );
1582
1583 double sx,sy;
1584 dc.GetUserScale( &sx, &sy );
1585 matrix.Scale( sx, sy );
1586
1587 ConcatTransform( matrix );
1588 #endif
1589 #endif
1590
1591 #ifdef __WXMAC__
1592 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1593 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1594 Init( cairo_create( surface ) );
1595 cairo_surface_destroy( surface );
1596 #endif
1597 }
1598
1599 #ifdef __WXGTK20__
1600 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1601 : wxGraphicsContext(renderer)
1602 {
1603 Init( gdk_cairo_create( drawable ) );
1604
1605 int width, height;
1606 gdk_drawable_get_size( drawable, &width, &height );
1607 m_width = width;
1608 m_height = height;
1609 }
1610 #endif
1611
1612 #ifdef __WXMSW__
1613 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, HDC handle )
1614 : wxGraphicsContext(renderer)
1615 {
1616 m_mswSurface = cairo_win32_surface_create(handle);
1617 Init( cairo_create(m_mswSurface) );
1618 m_width =
1619 m_height = 0;
1620 }
1621 #endif
1622
1623
1624 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1625 : wxGraphicsContext(renderer)
1626 {
1627 Init( context );
1628 m_width =
1629 m_height = 0;
1630 }
1631
1632 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1633 : wxGraphicsContext(renderer)
1634 {
1635 m_enableOffset = true;
1636 #ifdef __WXGTK__
1637 // something along these lines (copied from dcclient)
1638
1639 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1640 // code should still be able to create wxClientDCs for them, so we will
1641 // use the parent window here then.
1642 if (window->m_wxwindow == NULL)
1643 {
1644 window = window->GetParent();
1645 }
1646
1647 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
1648
1649 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
1650
1651 wxSize sz = window->GetSize();
1652 m_width = sz.x;
1653 m_height = sz.y;
1654 #endif
1655
1656 #ifdef __WXMSW__
1657 m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle());
1658 Init(cairo_create(m_mswSurface));
1659 #endif
1660
1661 }
1662
1663 wxCairoContext::~wxCairoContext()
1664 {
1665 if ( m_context )
1666 {
1667 PopState();
1668 PopState();
1669 cairo_destroy(m_context);
1670 }
1671 #ifdef __WXMSW__
1672 if ( m_mswSurface )
1673 cairo_surface_destroy(m_mswSurface);
1674 #endif
1675 }
1676
1677 void wxCairoContext::Init(cairo_t *context)
1678 {
1679 m_context = context ;
1680 PushState();
1681 PushState();
1682 }
1683
1684
1685 void wxCairoContext::Clip( const wxRegion& region )
1686 {
1687 // Create a path with all the rectangles in the region
1688 wxGraphicsPath path = GetRenderer()->CreatePath();
1689 wxRegionIterator ri(region);
1690 while (ri)
1691 {
1692 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1693 ++ri;
1694 }
1695
1696 // Put it in the context
1697 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1698 cairo_append_path(m_context, cp);
1699
1700 // clip to that path
1701 cairo_clip(m_context);
1702 path.UnGetNativePath(cp);
1703 }
1704
1705 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1706 {
1707 // Create a path with this rectangle
1708 wxGraphicsPath path = GetRenderer()->CreatePath();
1709 path.AddRectangle(x,y,w,h);
1710
1711 // Put it in the context
1712 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1713 cairo_append_path(m_context, cp);
1714
1715 // clip to that path
1716 cairo_clip(m_context);
1717 path.UnGetNativePath(cp);
1718 }
1719
1720 void wxCairoContext::ResetClip()
1721 {
1722 cairo_reset_clip(m_context);
1723 }
1724
1725
1726 void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1727 {
1728 if ( !m_pen.IsNull() )
1729 {
1730 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1731 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1732 cairo_append_path(m_context,cp);
1733 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1734 cairo_stroke(m_context);
1735 path.UnGetNativePath(cp);
1736 }
1737 }
1738
1739 void wxCairoContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
1740 {
1741 if ( !m_brush.IsNull() )
1742 {
1743 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1744 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1745 cairo_append_path(m_context,cp);
1746 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1747 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1748 cairo_fill(m_context);
1749 path.UnGetNativePath(cp);
1750 }
1751 }
1752
1753 void wxCairoContext::Rotate( wxDouble angle )
1754 {
1755 cairo_rotate(m_context,angle);
1756 }
1757
1758 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1759 {
1760 cairo_translate(m_context,dx,dy);
1761 }
1762
1763 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1764 {
1765 cairo_scale(m_context,xScale,yScale);
1766 }
1767
1768 // concatenates this transform with the current transform of this context
1769 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1770 {
1771 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1772 }
1773
1774 // sets the transform of this context
1775 void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1776 {
1777 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1778 }
1779
1780 // gets the matrix of this context
1781 wxGraphicsMatrix wxCairoContext::GetTransform() const
1782 {
1783 wxGraphicsMatrix matrix = CreateMatrix();
1784 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1785 return matrix;
1786 }
1787
1788
1789
1790 void wxCairoContext::PushState()
1791 {
1792 cairo_save(m_context);
1793 }
1794
1795 void wxCairoContext::PopState()
1796 {
1797 cairo_restore(m_context);
1798 }
1799
1800 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1801 {
1802 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1803 DrawBitmap(bitmap, x, y, w, h);
1804
1805 }
1806
1807 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1808 {
1809 PushState();
1810
1811 // In case we're scaling the image by using a width and height different
1812 // than the bitmap's size create a pattern transformation on the surface and
1813 // draw the transformed pattern.
1814 wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData());
1815 cairo_pattern_t* pattern = data->GetCairoPattern();
1816 wxSize size = data->GetSize();
1817
1818 wxDouble scaleX = w / size.GetWidth();
1819 wxDouble scaleY = h / size.GetHeight();
1820
1821 // prepare to draw the image
1822 cairo_translate(m_context, x, y);
1823 cairo_scale(m_context, scaleX, scaleY);
1824 cairo_set_source(m_context, pattern);
1825 // use the original size here since the context is scaled already...
1826 cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight());
1827 // fill the rectangle using the pattern
1828 cairo_fill(m_context);
1829
1830 PopState();
1831 }
1832
1833 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1834 {
1835 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1836 // start using the Cairo backend on other platforms then we may need to
1837 // fiddle with this...
1838 DrawBitmap(icon, x, y, w, h);
1839 }
1840
1841
1842 void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y)
1843 {
1844 wxCHECK_RET( !m_font.IsNull(),
1845 wxT("wxCairoContext::DrawText - no valid font set") );
1846
1847 if ( str.empty())
1848 return;
1849
1850 const wxCharBuffer data = str.utf8_str();
1851 if ( !data )
1852 return;
1853
1854 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1855
1856 #ifdef __WXGTK__
1857 size_t datalen = strlen(data);
1858
1859 PangoLayout *layout = pango_cairo_create_layout (m_context);
1860 wxCairoFontData* font_data = (wxCairoFontData*) m_font.GetRefData();
1861 pango_layout_set_font_description( layout, font_data->GetFont());
1862 pango_layout_set_text(layout, data, datalen);
1863
1864 if (font_data->GetUnderlined())
1865 {
1866 PangoAttrList *attrs = pango_attr_list_new();
1867 PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1868 pango_attr_list_insert(attrs, attr);
1869 pango_layout_set_attributes(layout, attrs);
1870 pango_attr_list_unref(attrs);
1871 }
1872
1873 cairo_move_to(m_context, x, y);
1874 pango_cairo_show_layout (m_context, layout);
1875
1876 g_object_unref (layout);
1877 #else
1878 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1879 // the position we move to by the ascent.
1880 cairo_font_extents_t fe;
1881 cairo_font_extents(m_context, &fe);
1882 cairo_move_to(m_context, x, y+fe.ascent);
1883
1884 cairo_show_text(m_context, data);
1885 #endif
1886 }
1887
1888 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1889 wxDouble *descent, wxDouble *externalLeading ) const
1890 {
1891 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1892
1893 if ( width )
1894 *width = 0;
1895 if ( height )
1896 *height = 0;
1897 if ( descent )
1898 *descent = 0;
1899 if ( externalLeading )
1900 *externalLeading = 0;
1901
1902 if ( str.empty())
1903 return;
1904
1905 #ifdef __WXGTK__
1906 int w, h;
1907
1908 PangoLayout *layout = pango_cairo_create_layout (m_context);
1909 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1910 const wxCharBuffer data = str.utf8_str();
1911 if ( !data )
1912 {
1913 return;
1914 }
1915 pango_layout_set_text( layout, data, strlen(data) );
1916 pango_layout_get_pixel_size (layout, &w, &h);
1917 if ( width )
1918 *width = w;
1919 if ( height )
1920 *height = h;
1921 if (descent)
1922 {
1923 PangoLayoutIter *iter = pango_layout_get_iter(layout);
1924 int baseline = pango_layout_iter_get_baseline(iter);
1925 pango_layout_iter_free(iter);
1926 *descent = h - PANGO_PIXELS(baseline);
1927 }
1928 g_object_unref (layout);
1929 #else
1930 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1931
1932 if (width)
1933 {
1934 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1935 cairo_text_extents_t te;
1936 cairo_text_extents(m_context, buf, &te);
1937 *width = te.width;
1938 }
1939
1940 if (height || descent || externalLeading)
1941 {
1942 cairo_font_extents_t fe;
1943 cairo_font_extents(m_context, &fe);
1944
1945 // some backends have negative descents
1946
1947 if ( fe.descent < 0 )
1948 fe.descent = -fe.descent;
1949
1950 if ( fe.height < (fe.ascent + fe.descent ) )
1951 {
1952 // some backends are broken re height ... (eg currently ATSUI)
1953 fe.height = fe.ascent + fe.descent;
1954 }
1955
1956 if (height)
1957 *height = fe.height;
1958 if ( descent )
1959 *descent = fe.descent;
1960 if ( externalLeading )
1961 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1962 }
1963 #endif
1964 }
1965
1966 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1967 {
1968 widths.Empty();
1969 widths.Add(0, text.length());
1970
1971 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1972
1973 if (text.empty())
1974 return;
1975
1976 // TODO
1977 }
1978
1979 void * wxCairoContext::GetNativeContext()
1980 {
1981 return m_context;
1982 }
1983
1984 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias)
1985 {
1986 if (m_antialias == antialias)
1987 return true;
1988
1989 m_antialias = antialias;
1990
1991 cairo_antialias_t antialiasMode;
1992 switch (antialias)
1993 {
1994 case wxANTIALIAS_DEFAULT:
1995 antialiasMode = CAIRO_ANTIALIAS_DEFAULT;
1996 break;
1997 case wxANTIALIAS_NONE:
1998 antialiasMode = CAIRO_ANTIALIAS_NONE;
1999 break;
2000 default:
2001 return false;
2002 }
2003 cairo_set_antialias(m_context, antialiasMode);
2004 return true;
2005 }
2006
2007 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality WXUNUSED(interpolation))
2008 {
2009 // placeholder
2010 return false;
2011 }
2012
2013 bool wxCairoContext::SetCompositionMode(wxCompositionMode op)
2014 {
2015 if ( m_composition == op )
2016 return true;
2017
2018 m_composition = op;
2019 cairo_operator_t cop;
2020 switch (op)
2021 {
2022 case wxCOMPOSITION_CLEAR:
2023 cop = CAIRO_OPERATOR_CLEAR;
2024 break;
2025 case wxCOMPOSITION_SOURCE:
2026 cop = CAIRO_OPERATOR_SOURCE;
2027 break;
2028 case wxCOMPOSITION_OVER:
2029 cop = CAIRO_OPERATOR_OVER;
2030 break;
2031 case wxCOMPOSITION_IN:
2032 cop = CAIRO_OPERATOR_IN;
2033 break;
2034 case wxCOMPOSITION_OUT:
2035 cop = CAIRO_OPERATOR_OUT;
2036 break;
2037 case wxCOMPOSITION_ATOP:
2038 cop = CAIRO_OPERATOR_ATOP;
2039 break;
2040 case wxCOMPOSITION_DEST:
2041 cop = CAIRO_OPERATOR_DEST;
2042 break;
2043 case wxCOMPOSITION_DEST_OVER:
2044 cop = CAIRO_OPERATOR_DEST_OVER;
2045 break;
2046 case wxCOMPOSITION_DEST_IN:
2047 cop = CAIRO_OPERATOR_DEST_IN;
2048 break;
2049 case wxCOMPOSITION_DEST_OUT:
2050 cop = CAIRO_OPERATOR_DEST_OUT;
2051 break;
2052 case wxCOMPOSITION_DEST_ATOP:
2053 cop = CAIRO_OPERATOR_DEST_ATOP;
2054 break;
2055 case wxCOMPOSITION_XOR:
2056 cop = CAIRO_OPERATOR_XOR;
2057 break;
2058 case wxCOMPOSITION_ADD:
2059 cop = CAIRO_OPERATOR_ADD;
2060 break;
2061 default:
2062 return false;
2063 }
2064 cairo_set_operator(m_context, cop);
2065 return true;
2066 }
2067
2068 void wxCairoContext::BeginLayer(wxDouble opacity)
2069 {
2070 m_layerOpacities.push_back(opacity);
2071 cairo_push_group(m_context);
2072 }
2073
2074 void wxCairoContext::EndLayer()
2075 {
2076 float opacity = m_layerOpacities.back();
2077 m_layerOpacities.pop_back();
2078 cairo_pop_group_to_source(m_context);
2079 cairo_paint_with_alpha(m_context,opacity);
2080 }
2081
2082 //-----------------------------------------------------------------------------
2083 // wxCairoRenderer declaration
2084 //-----------------------------------------------------------------------------
2085
2086 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
2087 {
2088 public :
2089 wxCairoRenderer() {}
2090
2091 virtual ~wxCairoRenderer() {}
2092
2093 // Context
2094
2095 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2096 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
2097 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
2098
2099 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2100
2101 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2102
2103 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2104
2105 virtual wxGraphicsContext * CreateMeasuringContext();
2106 #ifdef __WXMSW__
2107 #if wxUSE_ENH_METAFILE
2108 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
2109 #endif
2110 #endif
2111 // Path
2112
2113 virtual wxGraphicsPath CreatePath();
2114
2115 // Matrix
2116
2117 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2118 wxDouble tx=0.0, wxDouble ty=0.0);
2119
2120
2121 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2122
2123 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2124
2125 virtual wxGraphicsBrush
2126 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2127 wxDouble x2, wxDouble y2,
2128 const wxGraphicsGradientStops& stops);
2129
2130 virtual wxGraphicsBrush
2131 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2132 wxDouble xc, wxDouble yc,
2133 wxDouble radius,
2134 const wxGraphicsGradientStops& stops);
2135
2136 // sets the font
2137 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2138
2139 // create a native bitmap representation
2140 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
2141
2142 // create a graphics bitmap from a native bitmap
2143 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
2144
2145 // create a subimage from a native image representation
2146 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
2147
2148 protected :
2149 bool EnsureIsLoaded();
2150 void Load();
2151 void Unload();
2152 friend class wxCairoModule;
2153 private :
2154 int m_loaded;
2155 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
2156 } ;
2157
2158 //-----------------------------------------------------------------------------
2159 // wxCairoRenderer implementation
2160 //-----------------------------------------------------------------------------
2161
2162 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
2163
2164 static wxCairoRenderer gs_cairoGraphicsRenderer;
2165 // temporary hack to allow creating a cairo context on any platform
2166 extern wxGraphicsRenderer* gCairoRenderer;
2167 wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer;
2168
2169 bool wxCairoRenderer::EnsureIsLoaded()
2170 {
2171 #ifndef __WXGTK__
2172 Load();
2173 return wxCairoInit();
2174 #else
2175 return true;
2176 #endif
2177 }
2178
2179 void wxCairoRenderer::Load()
2180 {
2181 wxCairoInit();
2182 }
2183
2184 void wxCairoRenderer::Unload()
2185 {
2186 wxCairoCleanUp();
2187 }
2188
2189 // call EnsureIsLoaded() and return returnOnFail value if it fails
2190 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
2191 if ( !EnsureIsLoaded() ) \
2192 return (returnOnFail)
2193
2194 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
2195 {
2196 ENSURE_LOADED_OR_RETURN(NULL);
2197 return new wxCairoContext(this,dc);
2198 }
2199
2200 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
2201 {
2202 ENSURE_LOADED_OR_RETURN(NULL);
2203 return new wxCairoContext(this,dc);
2204 }
2205
2206 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc)
2207 {
2208 ENSURE_LOADED_OR_RETURN(NULL);
2209 #ifdef __WXGTK20__
2210 const wxDCImpl *impl = dc.GetImpl();
2211 cairo_t* context = (cairo_t*) impl->GetCairoContext();
2212 if (context)
2213 return new wxCairoContext(this,dc);
2214 else
2215 #endif
2216 return new wxCairoContext(this,dc);
2217 }
2218
2219 #ifdef __WXMSW__
2220 #if wxUSE_ENH_METAFILE
2221 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxEnhMetaFileDC& WXUNUSED(dc) )
2222 {
2223 ENSURE_LOADED_OR_RETURN(NULL);
2224 return NULL;
2225 }
2226 #endif
2227 #endif
2228
2229 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
2230 {
2231 ENSURE_LOADED_OR_RETURN(NULL);
2232 #ifdef __WXMSW__
2233 return new wxCairoContext(this,(HDC)context);
2234 #else
2235 return new wxCairoContext(this,(cairo_t*)context);
2236 #endif
2237 }
2238
2239
2240 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
2241 {
2242 ENSURE_LOADED_OR_RETURN(NULL);
2243 #ifdef __WXGTK__
2244 return new wxCairoContext(this,(GdkDrawable*)window);
2245 #else
2246 wxUnusedVar(window);
2247 return NULL;
2248 #endif
2249 }
2250
2251 wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
2252 {
2253 ENSURE_LOADED_OR_RETURN(NULL);
2254 #ifdef __WXGTK__
2255 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2256 #endif
2257 return NULL;
2258 // TODO
2259 }
2260
2261 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
2262 {
2263 ENSURE_LOADED_OR_RETURN(NULL);
2264 return new wxCairoContext(this, window );
2265 }
2266
2267 // Path
2268
2269 wxGraphicsPath wxCairoRenderer::CreatePath()
2270 {
2271 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
2272 wxGraphicsPath path;
2273 path.SetRefData( new wxCairoPathData(this) );
2274 return path;
2275 }
2276
2277
2278 // Matrix
2279
2280 wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2281 wxDouble tx, wxDouble ty)
2282
2283 {
2284 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
2285 wxGraphicsMatrix m;
2286 wxCairoMatrixData* data = new wxCairoMatrixData( this );
2287 data->Set( a,b,c,d,tx,ty ) ;
2288 m.SetRefData(data);
2289 return m;
2290 }
2291
2292 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
2293 {
2294 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
2295 if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
2296 return wxNullGraphicsPen;
2297 else
2298 {
2299 wxGraphicsPen p;
2300 p.SetRefData(new wxCairoPenData( this, pen ));
2301 return p;
2302 }
2303 }
2304
2305 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
2306 {
2307 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2308 if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
2309 return wxNullGraphicsBrush;
2310 else
2311 {
2312 wxGraphicsBrush p;
2313 p.SetRefData(new wxCairoBrushData( this, brush ));
2314 return p;
2315 }
2316 }
2317
2318 wxGraphicsBrush
2319 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2320 wxDouble x2, wxDouble y2,
2321 const wxGraphicsGradientStops& stops)
2322 {
2323 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2324 wxGraphicsBrush p;
2325 wxCairoBrushData* d = new wxCairoBrushData( this );
2326 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
2327 p.SetRefData(d);
2328 return p;
2329 }
2330
2331 wxGraphicsBrush
2332 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2333 wxDouble xc, wxDouble yc, wxDouble r,
2334 const wxGraphicsGradientStops& stops)
2335 {
2336 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2337 wxGraphicsBrush p;
2338 wxCairoBrushData* d = new wxCairoBrushData( this );
2339 d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops);
2340 p.SetRefData(d);
2341 return p;
2342 }
2343
2344 // sets the font
2345 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
2346 {
2347 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
2348 if ( font.IsOk() )
2349 {
2350 wxGraphicsFont p;
2351 p.SetRefData(new wxCairoFontData( this , font, col ));
2352 return p;
2353 }
2354 else
2355 return wxNullGraphicsFont;
2356 }
2357
2358 wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp )
2359 {
2360 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2361 if ( bmp.IsOk() )
2362 {
2363 wxGraphicsBitmap p;
2364 p.SetRefData(new wxCairoBitmapData( this , bmp ));
2365 return p;
2366 }
2367 else
2368 return wxNullGraphicsBitmap;
2369 }
2370
2371 wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
2372 {
2373 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2374 if ( bitmap != NULL )
2375 {
2376 wxGraphicsBitmap p;
2377 p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap ));
2378 return p;
2379 }
2380 else
2381 return wxNullGraphicsBitmap;
2382 }
2383
2384 wxGraphicsBitmap
2385 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap),
2386 wxDouble WXUNUSED(x),
2387 wxDouble WXUNUSED(y),
2388 wxDouble WXUNUSED(w),
2389 wxDouble WXUNUSED(h))
2390 {
2391 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2392 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2393 return wxNullGraphicsBitmap;
2394 }
2395
2396 wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2397 {
2398 return &gs_cairoGraphicsRenderer;
2399 }
2400
2401 #else // !wxUSE_CAIRO
2402
2403 wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2404 {
2405 return NULL;
2406 }
2407
2408 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2409
2410 // MSW and OS X have their own native default renderers, but the other ports
2411 // use Cairo by default
2412 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2413 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2414 {
2415 return GetCairoRenderer();
2416 }
2417 #endif // !(__WXMSW__ || __WXOSX__)
2418
2419 #endif // wxUSE_GRAPHICS_CONTEXT