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