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