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