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