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