Don't declare wxCairoFontData::m_underlined unnecessarily.
[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 double m_red;
305 double m_green;
306 double m_blue;
307 double m_alpha;
308 #ifdef __WXMAC__
309 cairo_font_face_t *m_font;
310 #elif defined(__WXGTK__)
311 PangoFontDescription* m_font;
312 bool m_underlined;
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
854 #ifdef __WXMAC__
855 m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() );
856 #elif defined(__WXGTK__)
857 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
858 m_underlined = font.GetUnderlined();
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 // There is no need to set m_underlined under wxGTK in this case, it can
890 // only be used if m_font != NULL.
891
892 InitFontComponents
893 (
894 facename,
895 flags & wxFONTFLAG_ITALIC ? CAIRO_FONT_SLANT_ITALIC
896 : CAIRO_FONT_SLANT_NORMAL,
897 flags & wxFONTFLAG_BOLD ? CAIRO_FONT_WEIGHT_BOLD
898 : CAIRO_FONT_WEIGHT_NORMAL
899 );
900 }
901
902 wxCairoFontData::~wxCairoFontData()
903 {
904 #ifdef __WXMAC__
905 if ( m_font )
906 cairo_font_face_destroy( m_font );
907 #elif defined(__WXGTK__)
908 if ( m_font )
909 pango_font_description_free( m_font );
910 #else
911 #endif
912 }
913
914 bool wxCairoFontData::Apply( wxGraphicsContext* context )
915 {
916 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
917 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
918 #ifdef __WXGTK__
919 if ( m_font )
920 {
921 // Nothing to do, the caller uses Pango layout functions to do
922 // everything.
923 return true;
924 }
925 #elif defined(__WXMAC__)
926 if ( m_font )
927 {
928 cairo_set_font_face(ctext, m_font);
929 cairo_set_font_size(ctext, m_size );
930 return true;
931 }
932 #endif
933
934 // If we get here, we must be on a platform without native font support or
935 // we're using toy Cairo API even under wxGTK/wxMac.
936 cairo_select_font_face(ctext, m_fontName, m_slant, m_weight );
937 cairo_set_font_size(ctext, m_size );
938
939 // Indicate that we don't use native fonts for the platforms which care
940 // about this (currently only wxGTK).
941 return false;
942 }
943
944 //-----------------------------------------------------------------------------
945 // wxCairoPathData implementation
946 //-----------------------------------------------------------------------------
947
948 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
949 : wxGraphicsPathData(renderer)
950 {
951 if (pathcontext)
952 {
953 m_pathContext = pathcontext;
954 }
955 else
956 {
957 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
958 m_pathContext = cairo_create(surface);
959 cairo_surface_destroy (surface);
960 }
961 }
962
963 wxCairoPathData::~wxCairoPathData()
964 {
965 cairo_destroy(m_pathContext);
966 }
967
968 wxGraphicsObjectRefData *wxCairoPathData::Clone() const
969 {
970 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
971 cairo_t* pathcontext = cairo_create(surface);
972 cairo_surface_destroy (surface);
973
974 cairo_path_t* path = cairo_copy_path(m_pathContext);
975 cairo_append_path(pathcontext, path);
976 cairo_path_destroy(path);
977 return new wxCairoPathData( GetRenderer() ,pathcontext);
978 }
979
980
981 void* wxCairoPathData::GetNativePath() const
982 {
983 return cairo_copy_path(m_pathContext) ;
984 }
985
986 void wxCairoPathData::UnGetNativePath(void *p) const
987 {
988 cairo_path_destroy((cairo_path_t*)p);
989 }
990
991 //
992 // The Primitives
993 //
994
995 void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
996 {
997 cairo_move_to(m_pathContext,x,y);
998 }
999
1000 void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
1001 {
1002 cairo_line_to(m_pathContext,x,y);
1003 }
1004
1005 void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
1006 {
1007 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
1008 cairo_append_path(m_pathContext, p);
1009 UnGetNativePath(p);
1010 }
1011
1012 void wxCairoPathData::CloseSubpath()
1013 {
1014 cairo_close_path(m_pathContext);
1015 }
1016
1017 void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
1018 {
1019 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
1020 }
1021
1022 // gets the last point of the current path, (0,0) if not yet set
1023 void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
1024 {
1025 double dx,dy;
1026 cairo_get_current_point(m_pathContext,&dx,&dy);
1027 if (x)
1028 *x = dx;
1029 if (y)
1030 *y = dy;
1031 }
1032
1033 void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
1034 {
1035 // as clockwise means positive in our system (y pointing downwards)
1036 // TODO make this interpretation dependent of the
1037 // real device trans
1038 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
1039 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
1040 else
1041 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
1042 }
1043
1044 // transforms each point of this path by the matrix
1045 void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
1046 {
1047 // as we don't have a true path object, we have to apply the inverse
1048 // matrix to the context
1049 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
1050 cairo_matrix_invert( &m );
1051 cairo_transform(m_pathContext,&m);
1052 }
1053
1054 // gets the bounding box enclosing all points (possibly including control points)
1055 void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
1056 {
1057 double x1,y1,x2,y2;
1058
1059 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
1060 if ( x2 < x1 )
1061 {
1062 *x = x2;
1063 *w = x1-x2;
1064 }
1065 else
1066 {
1067 *x = x1;
1068 *w = x2-x1;
1069 }
1070
1071 if( y2 < y1 )
1072 {
1073 *y = y2;
1074 *h = y1-y2;
1075 }
1076 else
1077 {
1078 *y = y1;
1079 *h = y2-y1;
1080 }
1081 }
1082
1083 bool wxCairoPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
1084 {
1085 cairo_set_fill_rule(m_pathContext,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1086 return cairo_in_fill( m_pathContext, x, y) != 0;
1087 }
1088
1089 //-----------------------------------------------------------------------------
1090 // wxCairoMatrixData implementation
1091 //-----------------------------------------------------------------------------
1092
1093 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
1094 : wxGraphicsMatrixData(renderer)
1095 {
1096 if ( matrix )
1097 m_matrix = *matrix;
1098 }
1099
1100 wxCairoMatrixData::~wxCairoMatrixData()
1101 {
1102 // nothing to do
1103 }
1104
1105 wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
1106 {
1107 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
1108 }
1109
1110 // concatenates the matrix
1111 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
1112 {
1113 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
1114 }
1115
1116 // sets the matrix to the respective values
1117 void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1118 wxDouble tx, wxDouble ty)
1119 {
1120 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
1121 }
1122
1123 // gets the component valuess of the matrix
1124 void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1125 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1126 {
1127 if (a) *a = m_matrix.xx;
1128 if (b) *b = m_matrix.yx;
1129 if (c) *c = m_matrix.xy;
1130 if (d) *d = m_matrix.yy;
1131 if (tx) *tx= m_matrix.x0;
1132 if (ty) *ty= m_matrix.y0;
1133 }
1134
1135 // makes this the inverse matrix
1136 void wxCairoMatrixData::Invert()
1137 {
1138 cairo_matrix_invert( &m_matrix );
1139 }
1140
1141 // returns true if the elements of the transformation matrix are equal ?
1142 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
1143 {
1144 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
1145 return (
1146 m_matrix.xx == tm->xx &&
1147 m_matrix.yx == tm->yx &&
1148 m_matrix.xy == tm->xy &&
1149 m_matrix.yy == tm->yy &&
1150 m_matrix.x0 == tm->x0 &&
1151 m_matrix.y0 == tm->y0 ) ;
1152 }
1153
1154 // return true if this is the identity matrix
1155 bool wxCairoMatrixData::IsIdentity() const
1156 {
1157 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
1158 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
1159 }
1160
1161 //
1162 // transformation
1163 //
1164
1165 // add the translation to this matrix
1166 void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
1167 {
1168 cairo_matrix_translate( &m_matrix, dx, dy) ;
1169 }
1170
1171 // add the scale to this matrix
1172 void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1173 {
1174 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
1175 }
1176
1177 // add the rotation to this matrix (radians)
1178 void wxCairoMatrixData::Rotate( wxDouble angle )
1179 {
1180 cairo_matrix_rotate( &m_matrix, angle) ;
1181 }
1182
1183 //
1184 // apply the transforms
1185 //
1186
1187 // applies that matrix to the point
1188 void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1189 {
1190 double lx = *x, ly = *y ;
1191 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
1192 *x = lx;
1193 *y = ly;
1194 }
1195
1196 // applies the matrix except for translations
1197 void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1198 {
1199 double lx = *dx, ly = *dy ;
1200 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1201 *dx = lx;
1202 *dy = ly;
1203 }
1204
1205 // returns the native representation
1206 void * wxCairoMatrixData::GetNativeMatrix() const
1207 {
1208 return (void*) &m_matrix;
1209 }
1210
1211 // ----------------------------------------------------------------------------
1212 // wxCairoBitmap implementation
1213 // ----------------------------------------------------------------------------
1214
1215 int wxCairoBitmapData::InitBuffer(int width, int height, cairo_format_t format)
1216 {
1217 wxUnusedVar(format); // Only really unused with Cairo < 1.6.
1218
1219 // Determine the stride: use cairo_format_stride_for_width() if available
1220 // but fall back to 4*width for the earlier versions as this is what that
1221 // function always returns, even in latest Cairo, anyhow.
1222 int stride;
1223 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 6, 0)
1224 if ( cairo_version() >= CAIRO_VERSION_ENCODE(1, 6, 0) )
1225 {
1226 stride = cairo_format_stride_for_width(format, width);
1227
1228 // All our code would totally break if stride were not a multiple of 4
1229 // so ensure this is the case.
1230 if ( stride % 4 )
1231 {
1232 wxFAIL_MSG("Unexpected Cairo image surface stride.");
1233
1234 stride += 4 - stride % 4;
1235 }
1236 }
1237 else
1238 #endif
1239 stride = 4*width;
1240
1241 m_width = width;
1242 m_height = height;
1243 m_buffer = new unsigned char[height*stride];
1244
1245 return stride;
1246 }
1247
1248 void wxCairoBitmapData::InitSurface(cairo_format_t format, int stride)
1249 {
1250 m_surface = cairo_image_surface_create_for_data(
1251 m_buffer, format, m_width, m_height, stride);
1252 m_pattern = cairo_pattern_create_for_surface(m_surface);
1253 }
1254
1255 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ) :
1256 wxGraphicsObjectRefData( renderer )
1257 {
1258 m_surface = bitmap;
1259 m_pattern = cairo_pattern_create_for_surface(m_surface);
1260 }
1261
1262 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer )
1263 {
1264 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1265
1266 #ifdef wxHAS_RAW_BITMAP
1267 // Create a surface object and copy the bitmap pixel data to it. if the
1268 // image has alpha (or a mask represented as alpha) then we'll use a
1269 // different format and iterator than if it doesn't...
1270 const cairo_format_t bufferFormat = bmp.GetDepth() == 32
1271 #ifdef __WXGTK__
1272 || bmp.GetMask()
1273 #endif
1274 ? CAIRO_FORMAT_ARGB32
1275 : CAIRO_FORMAT_RGB24;
1276
1277 int stride = InitBuffer(bmp.GetWidth(), bmp.GetHeight(), bufferFormat);
1278
1279 int bw = m_width;
1280 int bh = m_height;
1281 wxBitmap bmpSource = bmp; // we need a non-const instance
1282 wxUint32* data = (wxUint32*)m_buffer;
1283
1284 if ( bufferFormat == CAIRO_FORMAT_ARGB32 )
1285 {
1286 // use the bitmap's alpha
1287 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1288 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1289
1290 wxAlphaPixelData::Iterator p(pixData);
1291 for (int y=0; y<bh; y++)
1292 {
1293 wxAlphaPixelData::Iterator rowStart = p;
1294 wxUint32* const rowStartDst = data;
1295 for (int x=0; x<bw; x++)
1296 {
1297 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1298 // with alpha in the upper 8 bits, then red, then green, then
1299 // blue. The 32-bit quantities are stored native-endian.
1300 // Pre-multiplied alpha is used.
1301 unsigned char alpha = p.Alpha();
1302 if (alpha == 0)
1303 *data = 0;
1304 else
1305 *data = ( alpha << 24
1306 | (p.Red() * alpha/255) << 16
1307 | (p.Green() * alpha/255) << 8
1308 | (p.Blue() * alpha/255) );
1309 ++data;
1310 ++p;
1311 }
1312
1313 data = rowStartDst + stride / 4;
1314 p = rowStart;
1315 p.OffsetY(pixData, 1);
1316 }
1317 }
1318 else // no alpha
1319 {
1320 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1321 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1322
1323 wxNativePixelData::Iterator p(pixData);
1324 for (int y=0; y<bh; y++)
1325 {
1326 wxNativePixelData::Iterator rowStart = p;
1327 wxUint32* const rowStartDst = data;
1328 for (int x=0; x<bw; x++)
1329 {
1330 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1331 // the upper 8 bits unused. Red, Green, and Blue are stored in
1332 // the remaining 24 bits in that order. The 32-bit quantities
1333 // are stored native-endian.
1334 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1335 ++data;
1336 ++p;
1337 }
1338
1339 data = rowStartDst + stride / 4;
1340 p = rowStart;
1341 p.OffsetY(pixData, 1);
1342 }
1343 }
1344 #ifdef __WXMSW__
1345 // if there is a mask, set the alpha bytes in the target buffer to
1346 // fully transparent or fully opaque
1347 if (bmpSource.GetMask())
1348 {
1349 wxBitmap bmpMask = bmpSource.GetMaskBitmap();
1350 bufferFormat = CAIRO_FORMAT_ARGB32;
1351 data = (wxUint32*)m_buffer;
1352 wxNativePixelData pixData(bmpMask, wxPoint(0,0), wxSize(bw, bh));
1353 wxCHECK_RET( pixData, wxT("Failed to gain raw access to mask bitmap data."));
1354
1355 wxNativePixelData::Iterator p(pixData);
1356 for (int y=0; y<bh; y++)
1357 {
1358 wxNativePixelData::Iterator rowStart = p;
1359 wxUint32* const rowStartDst = data;
1360 for (int x=0; x<bw; x++)
1361 {
1362 if (p.Red()+p.Green()+p.Blue() == 0)
1363 *data = 0;
1364 else
1365 *data = (wxALPHA_OPAQUE << 24) | (*data & 0x00FFFFFF);
1366 ++data;
1367 ++p;
1368 }
1369
1370 data = rowStartDst + stride / 4;
1371 p = rowStart;
1372 p.OffsetY(pixData, 1);
1373 }
1374 }
1375 #endif
1376
1377 InitSurface(bufferFormat, stride);
1378 #endif // wxHAS_RAW_BITMAP
1379 }
1380
1381 #if wxUSE_IMAGE
1382
1383 // Helper functions for dealing with alpha pre-multiplication.
1384 namespace
1385 {
1386
1387 inline unsigned char Premultiply(unsigned char alpha, unsigned char data)
1388 {
1389 return alpha ? (data * alpha)/0xff : data;
1390 }
1391
1392 inline unsigned char Unpremultiply(unsigned char alpha, unsigned char data)
1393 {
1394 return alpha ? (data * 0xff)/alpha : data;
1395 }
1396
1397 } // anonymous namespace
1398
1399 wxCairoBitmapData::wxCairoBitmapData(wxGraphicsRenderer* renderer,
1400 const wxImage& image)
1401 : wxGraphicsObjectRefData(renderer)
1402 {
1403 const cairo_format_t bufferFormat = image.HasAlpha()
1404 ? CAIRO_FORMAT_ARGB32
1405 : CAIRO_FORMAT_RGB24;
1406
1407 int stride = InitBuffer(image.GetWidth(), image.GetHeight(), bufferFormat);
1408
1409 // Copy wxImage data into the buffer. Notice that we work with wxUint32
1410 // values and not bytes becase Cairo always works with buffers in native
1411 // endianness.
1412 wxUint32* dst = reinterpret_cast<wxUint32*>(m_buffer);
1413 const unsigned char* src = image.GetData();
1414
1415 if ( bufferFormat == CAIRO_FORMAT_ARGB32 )
1416 {
1417 const unsigned char* alpha = image.GetAlpha();
1418
1419 for ( int y = 0; y < m_height; y++ )
1420 {
1421 wxUint32* const rowStartDst = dst;
1422
1423 for ( int x = 0; x < m_width; x++ )
1424 {
1425 const unsigned char a = *alpha++;
1426
1427 *dst++ = a << 24 |
1428 Premultiply(a, src[0]) << 16 |
1429 Premultiply(a, src[1]) << 8 |
1430 Premultiply(a, src[2]);
1431 src += 3;
1432 }
1433
1434 dst = rowStartDst + stride / 4;
1435 }
1436 }
1437 else // RGB
1438 {
1439 for ( int y = 0; y < m_height; y++ )
1440 {
1441 wxUint32* const rowStartDst = dst;
1442
1443 for ( int x = 0; x < m_width; x++ )
1444 {
1445 *dst++ = src[0] << 16 |
1446 src[1] << 8 |
1447 src[2];
1448 src += 3;
1449 }
1450
1451 dst = rowStartDst + stride / 4;
1452 }
1453 }
1454
1455 InitSurface(bufferFormat, stride);
1456 }
1457
1458 wxImage wxCairoBitmapData::ConvertToImage() const
1459 {
1460 wxImage image(m_width, m_height, false /* don't clear */);
1461
1462 // Get the surface type and format.
1463 wxCHECK_MSG( cairo_surface_get_type(m_surface) == CAIRO_SURFACE_TYPE_IMAGE,
1464 wxNullImage,
1465 wxS("Can't convert non-image surface to image.") );
1466
1467 switch ( cairo_image_surface_get_format(m_surface) )
1468 {
1469 case CAIRO_FORMAT_ARGB32:
1470 image.SetAlpha();
1471 break;
1472
1473 case CAIRO_FORMAT_RGB24:
1474 // Nothing to do, we don't use alpha by default.
1475 break;
1476
1477 case CAIRO_FORMAT_A8:
1478 case CAIRO_FORMAT_A1:
1479 wxFAIL_MSG(wxS("Unsupported Cairo image surface type."));
1480 return wxNullImage;
1481
1482 default:
1483 wxFAIL_MSG(wxS("Unknown Cairo image surface type."));
1484 return wxNullImage;
1485 }
1486
1487 // Prepare for copying data.
1488 const wxUint32* src = (wxUint32*)cairo_image_surface_get_data(m_surface);
1489 wxCHECK_MSG( src, wxNullImage, wxS("Failed to get Cairo surface data.") );
1490
1491 int stride = cairo_image_surface_get_stride(m_surface);
1492 wxCHECK_MSG( stride > 0, wxNullImage,
1493 wxS("Failed to get Cairo surface stride.") );
1494
1495 // As we work with wxUint32 pointers and not char ones, we need to adjust
1496 // the stride accordingly. This should be lossless as the stride must be a
1497 // multiple of pixel size.
1498 wxASSERT_MSG( !(stride % sizeof(wxUint32)), wxS("Unexpected stride.") );
1499 stride /= sizeof(wxUint32);
1500
1501 unsigned char* dst = image.GetData();
1502 unsigned char *alpha = image.GetAlpha();
1503 if ( alpha )
1504 {
1505 // We need to also copy alpha and undo the pre-multiplication as Cairo
1506 // stores pre-multiplied values in this format while wxImage does not.
1507 for ( int y = 0; y < m_height; y++ )
1508 {
1509 const wxUint32* const rowStart = src;
1510 for ( int x = 0; x < m_width; x++ )
1511 {
1512 const wxUint32 argb = *src++;
1513
1514 *alpha++ = (argb & 0xff000000) >> 24;
1515
1516 // Copy the RGB data undoing the pre-multiplication.
1517 *dst++ = Unpremultiply(*alpha, (argb & 0x00ff0000) >> 16);
1518 *dst++ = Unpremultiply(*alpha, (argb & 0x0000ff00) >> 8);
1519 *dst++ = Unpremultiply(*alpha, (argb & 0x000000ff));
1520 }
1521
1522 src = rowStart + stride;
1523 }
1524 }
1525 else // RGB
1526 {
1527 // Things are pretty simple in this case, just copy RGB bytes.
1528 for ( int y = 0; y < m_height; y++ )
1529 {
1530 const wxUint32* const rowStart = src;
1531 for ( int x = 0; x < m_width; x++ )
1532 {
1533 const wxUint32 argb = *src++;
1534
1535 *dst++ = (argb & 0x00ff0000) >> 16;
1536 *dst++ = (argb & 0x0000ff00) >> 8;
1537 *dst++ = (argb & 0x000000ff);
1538 }
1539
1540 src = rowStart + stride;
1541 }
1542 }
1543
1544 return image;
1545 }
1546
1547 #endif // wxUSE_IMAGE
1548
1549 wxCairoBitmapData::~wxCairoBitmapData()
1550 {
1551 if (m_pattern)
1552 cairo_pattern_destroy(m_pattern);
1553
1554 if (m_surface)
1555 cairo_surface_destroy(m_surface);
1556
1557 delete [] m_buffer;
1558 }
1559
1560 // ----------------------------------------------------------------------------
1561 // wxGraphicsBitmap implementation
1562 // ----------------------------------------------------------------------------
1563
1564 #if wxUSE_IMAGE
1565
1566 wxImage wxGraphicsBitmap::ConvertToImage() const
1567 {
1568 const wxCairoBitmapData* const
1569 data = static_cast<wxCairoBitmapData*>(GetGraphicsData());
1570
1571 return data ? data->ConvertToImage() : wxNullImage;
1572 }
1573
1574 #endif // wxUSE_IMAGE
1575
1576 //-----------------------------------------------------------------------------
1577 // wxCairoContext implementation
1578 //-----------------------------------------------------------------------------
1579
1580 class wxCairoOffsetHelper
1581 {
1582 public :
1583 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1584 {
1585 m_ctx = ctx;
1586 m_offset = offset;
1587 if ( m_offset )
1588 cairo_translate( m_ctx, 0.5, 0.5 );
1589 }
1590 ~wxCairoOffsetHelper( )
1591 {
1592 if ( m_offset )
1593 cairo_translate( m_ctx, -0.5, -0.5 );
1594 }
1595 public :
1596 cairo_t* m_ctx;
1597 bool m_offset;
1598 } ;
1599
1600 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc )
1601 : wxGraphicsContext(renderer)
1602 {
1603 #ifdef __WXMSW__
1604 // wxMSW contexts always use MM_ANISOTROPIC, which messes up
1605 // text rendering when printing using Cairo. Switch it to MM_TEXT
1606 // map mode to avoid this problem.
1607 HDC hdc = (HDC)dc.GetHDC();
1608 ::SetMapMode(hdc, MM_TEXT);
1609 m_mswSurface = cairo_win32_printing_surface_create(hdc);
1610 Init( cairo_create(m_mswSurface) );
1611 #endif
1612
1613 #ifdef __WXGTK20__
1614 const wxDCImpl *impl = dc.GetImpl();
1615 Init( (cairo_t*) impl->GetCairoContext() );
1616 #endif
1617 wxSize sz = dc.GetSize();
1618 m_width = sz.x;
1619 m_height = sz.y;
1620
1621 wxPoint org = dc.GetDeviceOrigin();
1622 cairo_translate( m_context, org.x, org.y );
1623
1624 double sx,sy;
1625 dc.GetUserScale( &sx, &sy );
1626
1627 // TODO: Determine if these fixes are needed on other platforms too.
1628 // On MSW, without this the printer context will not respect wxDC SetMapMode calls.
1629 // For example, using dc.SetMapMode(wxMM_POINTS) can let us share printer and screen
1630 // drawing code
1631 #ifdef __WXMSW__
1632 double lsx,lsy;
1633 dc.GetLogicalScale( &lsx, &lsy );
1634 sx *= lsx;
1635 sy *= lsy;
1636 #endif
1637 cairo_scale( m_context, sx, sy );
1638
1639 org = dc.GetLogicalOrigin();
1640 cairo_translate( m_context, -org.x, -org.y );
1641 }
1642
1643 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1644 : wxGraphicsContext(renderer)
1645 {
1646 int width, height;
1647 dc.GetSize( &width, &height );
1648 m_width = width;
1649 m_height = height;
1650
1651 m_enableOffset = true;
1652
1653 #ifdef __WXMSW__
1654 m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC());
1655 Init( cairo_create(m_mswSurface) );
1656 #endif
1657
1658 #ifdef __WXGTK20__
1659 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1660 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1661
1662 #if 0
1663 wxGraphicsMatrix matrix = CreateMatrix();
1664
1665 wxPoint org = dc.GetDeviceOrigin();
1666 matrix.Translate( org.x, org.y );
1667
1668 org = dc.GetLogicalOrigin();
1669 matrix.Translate( -org.x, -org.y );
1670
1671 double sx,sy;
1672 dc.GetUserScale( &sx, &sy );
1673 matrix.Scale( sx, sy );
1674
1675 ConcatTransform( matrix );
1676 #endif
1677 #endif
1678
1679 #ifdef __WXMAC__
1680 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1681 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1682 Init( cairo_create( surface ) );
1683 cairo_surface_destroy( surface );
1684 #endif
1685 }
1686
1687 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
1688 : wxGraphicsContext(renderer)
1689 {
1690 int width, height;
1691 dc.GetSize( &width, &height );
1692 m_width = width;
1693 m_height = height;
1694
1695 m_enableOffset = true;
1696
1697 #ifdef __WXMSW__
1698 m_mswSurface = cairo_win32_surface_create((HDC)dc.GetHDC());
1699 Init( cairo_create(m_mswSurface) );
1700 #endif
1701
1702 #ifdef __WXGTK20__
1703 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1704 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1705
1706 #if 0
1707 wxGraphicsMatrix matrix = CreateMatrix();
1708
1709 wxPoint org = dc.GetDeviceOrigin();
1710 matrix.Translate( org.x, org.y );
1711
1712 org = dc.GetLogicalOrigin();
1713 matrix.Translate( -org.x, -org.y );
1714
1715 double sx,sy;
1716 dc.GetUserScale( &sx, &sy );
1717 matrix.Scale( sx, sy );
1718
1719 ConcatTransform( matrix );
1720 #endif
1721 #endif
1722
1723 #ifdef __WXMAC__
1724 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1725 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1726 Init( cairo_create( surface ) );
1727 cairo_surface_destroy( surface );
1728 #endif
1729 }
1730
1731 #ifdef __WXGTK20__
1732 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1733 : wxGraphicsContext(renderer)
1734 {
1735 Init( gdk_cairo_create( drawable ) );
1736
1737 int width, height;
1738 gdk_drawable_get_size( drawable, &width, &height );
1739 m_width = width;
1740 m_height = height;
1741 }
1742 #endif
1743
1744 #ifdef __WXMSW__
1745 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, HDC handle )
1746 : wxGraphicsContext(renderer)
1747 {
1748 m_mswSurface = cairo_win32_surface_create(handle);
1749 Init( cairo_create(m_mswSurface) );
1750 m_width =
1751 m_height = 0;
1752 }
1753 #endif
1754
1755
1756 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1757 : wxGraphicsContext(renderer)
1758 {
1759 Init( context );
1760 m_width =
1761 m_height = 0;
1762 }
1763
1764 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1765 : wxGraphicsContext(renderer)
1766 {
1767 m_enableOffset = true;
1768 #ifdef __WXGTK__
1769 // something along these lines (copied from dcclient)
1770
1771 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1772 // code should still be able to create wxClientDCs for them, so we will
1773 // use the parent window here then.
1774 if (window->m_wxwindow == NULL)
1775 {
1776 window = window->GetParent();
1777 }
1778
1779 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
1780
1781 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
1782
1783 wxSize sz = window->GetSize();
1784 m_width = sz.x;
1785 m_height = sz.y;
1786 #endif
1787
1788 #ifdef __WXMSW__
1789 m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle());
1790 Init(cairo_create(m_mswSurface));
1791 #endif
1792
1793 }
1794
1795 wxCairoContext::wxCairoContext(wxGraphicsRenderer* renderer) :
1796 wxGraphicsContext(renderer)
1797 {
1798 m_context = NULL;
1799 }
1800
1801 wxCairoContext::~wxCairoContext()
1802 {
1803 if ( m_context )
1804 {
1805 PopState();
1806 PopState();
1807 cairo_destroy(m_context);
1808 }
1809 #ifdef __WXMSW__
1810 if ( m_mswSurface )
1811 cairo_surface_destroy(m_mswSurface);
1812 #endif
1813 }
1814
1815 void wxCairoContext::Init(cairo_t *context)
1816 {
1817 m_context = context ;
1818 PushState();
1819 PushState();
1820 }
1821
1822
1823 void wxCairoContext::Clip( const wxRegion& region )
1824 {
1825 // Create a path with all the rectangles in the region
1826 wxGraphicsPath path = GetRenderer()->CreatePath();
1827 wxRegionIterator ri(region);
1828 while (ri)
1829 {
1830 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1831 ++ri;
1832 }
1833
1834 // Put it in the context
1835 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1836 cairo_append_path(m_context, cp);
1837
1838 // clip to that path
1839 cairo_clip(m_context);
1840 path.UnGetNativePath(cp);
1841 }
1842
1843 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1844 {
1845 // Create a path with this rectangle
1846 wxGraphicsPath path = GetRenderer()->CreatePath();
1847 path.AddRectangle(x,y,w,h);
1848
1849 // Put it in the context
1850 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1851 cairo_append_path(m_context, cp);
1852
1853 // clip to that path
1854 cairo_clip(m_context);
1855 path.UnGetNativePath(cp);
1856 }
1857
1858 void wxCairoContext::ResetClip()
1859 {
1860 cairo_reset_clip(m_context);
1861 }
1862
1863
1864 void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1865 {
1866 if ( !m_pen.IsNull() )
1867 {
1868 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1869 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1870 cairo_append_path(m_context,cp);
1871 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1872 cairo_stroke(m_context);
1873 path.UnGetNativePath(cp);
1874 }
1875 }
1876
1877 void wxCairoContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
1878 {
1879 if ( !m_brush.IsNull() )
1880 {
1881 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1882 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1883 cairo_append_path(m_context,cp);
1884 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1885 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1886 cairo_fill(m_context);
1887 path.UnGetNativePath(cp);
1888 }
1889 }
1890
1891 void wxCairoContext::Rotate( wxDouble angle )
1892 {
1893 cairo_rotate(m_context,angle);
1894 }
1895
1896 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1897 {
1898 cairo_translate(m_context,dx,dy);
1899 }
1900
1901 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1902 {
1903 cairo_scale(m_context,xScale,yScale);
1904 }
1905
1906 // concatenates this transform with the current transform of this context
1907 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1908 {
1909 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1910 }
1911
1912 // sets the transform of this context
1913 void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1914 {
1915 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1916 }
1917
1918 // gets the matrix of this context
1919 wxGraphicsMatrix wxCairoContext::GetTransform() const
1920 {
1921 wxGraphicsMatrix matrix = CreateMatrix();
1922 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1923 return matrix;
1924 }
1925
1926
1927
1928 void wxCairoContext::PushState()
1929 {
1930 cairo_save(m_context);
1931 }
1932
1933 void wxCairoContext::PopState()
1934 {
1935 cairo_restore(m_context);
1936 }
1937
1938 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1939 {
1940 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1941 DrawBitmap(bitmap, x, y, w, h);
1942
1943 }
1944
1945 void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1946 {
1947 PushState();
1948
1949 // In case we're scaling the image by using a width and height different
1950 // than the bitmap's size create a pattern transformation on the surface and
1951 // draw the transformed pattern.
1952 wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData());
1953 cairo_pattern_t* pattern = data->GetCairoPattern();
1954 wxSize size = data->GetSize();
1955
1956 wxDouble scaleX = w / size.GetWidth();
1957 wxDouble scaleY = h / size.GetHeight();
1958
1959 // prepare to draw the image
1960 cairo_translate(m_context, x, y);
1961 cairo_scale(m_context, scaleX, scaleY);
1962 cairo_set_source(m_context, pattern);
1963 // use the original size here since the context is scaled already...
1964 cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight());
1965 // fill the rectangle using the pattern
1966 cairo_fill(m_context);
1967
1968 PopState();
1969 }
1970
1971 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1972 {
1973 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1974 // start using the Cairo backend on other platforms then we may need to
1975 // fiddle with this...
1976 DrawBitmap(icon, x, y, w, h);
1977 }
1978
1979
1980 void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y)
1981 {
1982 wxCHECK_RET( !m_font.IsNull(),
1983 wxT("wxCairoContext::DrawText - no valid font set") );
1984
1985 if ( str.empty())
1986 return;
1987
1988 const wxCharBuffer data = str.utf8_str();
1989 if ( !data )
1990 return;
1991
1992 if ( ((wxCairoFontData*)m_font.GetRefData())->Apply(this) )
1993 {
1994 #ifdef __WXGTK__
1995 size_t datalen = strlen(data);
1996
1997 PangoLayout *layout = pango_cairo_create_layout (m_context);
1998 wxCairoFontData* font_data = (wxCairoFontData*) m_font.GetRefData();
1999 pango_layout_set_font_description( layout, font_data->GetFont());
2000 pango_layout_set_text(layout, data, datalen);
2001
2002 if (font_data->GetUnderlined())
2003 {
2004 PangoAttrList *attrs = pango_attr_list_new();
2005 PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
2006 pango_attr_list_insert(attrs, attr);
2007 pango_layout_set_attributes(layout, attrs);
2008 pango_attr_list_unref(attrs);
2009 }
2010
2011 cairo_move_to(m_context, x, y);
2012 pango_cairo_show_layout (m_context, layout);
2013
2014 g_object_unref (layout);
2015
2016 // Don't use Cairo text API, we already did everything.
2017 return;
2018 #endif
2019 }
2020
2021 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
2022 // the position we move to by the ascent.
2023 cairo_font_extents_t fe;
2024 cairo_font_extents(m_context, &fe);
2025 cairo_move_to(m_context, x, y+fe.ascent);
2026
2027 cairo_show_text(m_context, data);
2028 }
2029
2030 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
2031 wxDouble *descent, wxDouble *externalLeading ) const
2032 {
2033 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
2034
2035 if ( width )
2036 *width = 0;
2037 if ( height )
2038 *height = 0;
2039 if ( descent )
2040 *descent = 0;
2041 if ( externalLeading )
2042 *externalLeading = 0;
2043
2044 if ( str.empty())
2045 return;
2046
2047 if ( ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this) )
2048 {
2049 #ifdef __WXGTK__
2050 int w, h;
2051
2052 PangoLayout *layout = pango_cairo_create_layout (m_context);
2053 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
2054 const wxCharBuffer data = str.utf8_str();
2055 if ( !data )
2056 {
2057 return;
2058 }
2059 pango_layout_set_text( layout, data, strlen(data) );
2060 pango_layout_get_pixel_size (layout, &w, &h);
2061 if ( width )
2062 *width = w;
2063 if ( height )
2064 *height = h;
2065 if (descent)
2066 {
2067 PangoLayoutIter *iter = pango_layout_get_iter(layout);
2068 int baseline = pango_layout_iter_get_baseline(iter);
2069 pango_layout_iter_free(iter);
2070 *descent = h - PANGO_PIXELS(baseline);
2071 }
2072 g_object_unref (layout);
2073 return;
2074 #endif
2075 }
2076
2077 if (width)
2078 {
2079 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
2080 cairo_text_extents_t te;
2081 cairo_text_extents(m_context, buf, &te);
2082 *width = te.width;
2083 }
2084
2085 if (height || descent || externalLeading)
2086 {
2087 cairo_font_extents_t fe;
2088 cairo_font_extents(m_context, &fe);
2089
2090 // some backends have negative descents
2091
2092 if ( fe.descent < 0 )
2093 fe.descent = -fe.descent;
2094
2095 if ( fe.height < (fe.ascent + fe.descent ) )
2096 {
2097 // some backends are broken re height ... (eg currently ATSUI)
2098 fe.height = fe.ascent + fe.descent;
2099 }
2100
2101 if (height)
2102 *height = fe.height;
2103 if ( descent )
2104 *descent = fe.descent;
2105 if ( externalLeading )
2106 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
2107 }
2108 }
2109
2110 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
2111 {
2112 widths.Empty();
2113 widths.Add(0, text.length());
2114
2115 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
2116
2117 if (text.empty())
2118 return;
2119
2120 // TODO
2121 }
2122
2123 void * wxCairoContext::GetNativeContext()
2124 {
2125 return m_context;
2126 }
2127
2128 bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias)
2129 {
2130 if (m_antialias == antialias)
2131 return true;
2132
2133 m_antialias = antialias;
2134
2135 cairo_antialias_t antialiasMode;
2136 switch (antialias)
2137 {
2138 case wxANTIALIAS_DEFAULT:
2139 antialiasMode = CAIRO_ANTIALIAS_DEFAULT;
2140 break;
2141 case wxANTIALIAS_NONE:
2142 antialiasMode = CAIRO_ANTIALIAS_NONE;
2143 break;
2144 default:
2145 return false;
2146 }
2147 cairo_set_antialias(m_context, antialiasMode);
2148 return true;
2149 }
2150
2151 bool wxCairoContext::SetInterpolationQuality(wxInterpolationQuality WXUNUSED(interpolation))
2152 {
2153 // placeholder
2154 return false;
2155 }
2156
2157 bool wxCairoContext::SetCompositionMode(wxCompositionMode op)
2158 {
2159 if ( m_composition == op )
2160 return true;
2161
2162 m_composition = op;
2163 cairo_operator_t cop;
2164 switch (op)
2165 {
2166 case wxCOMPOSITION_CLEAR:
2167 cop = CAIRO_OPERATOR_CLEAR;
2168 break;
2169 case wxCOMPOSITION_SOURCE:
2170 cop = CAIRO_OPERATOR_SOURCE;
2171 break;
2172 case wxCOMPOSITION_OVER:
2173 cop = CAIRO_OPERATOR_OVER;
2174 break;
2175 case wxCOMPOSITION_IN:
2176 cop = CAIRO_OPERATOR_IN;
2177 break;
2178 case wxCOMPOSITION_OUT:
2179 cop = CAIRO_OPERATOR_OUT;
2180 break;
2181 case wxCOMPOSITION_ATOP:
2182 cop = CAIRO_OPERATOR_ATOP;
2183 break;
2184 case wxCOMPOSITION_DEST:
2185 cop = CAIRO_OPERATOR_DEST;
2186 break;
2187 case wxCOMPOSITION_DEST_OVER:
2188 cop = CAIRO_OPERATOR_DEST_OVER;
2189 break;
2190 case wxCOMPOSITION_DEST_IN:
2191 cop = CAIRO_OPERATOR_DEST_IN;
2192 break;
2193 case wxCOMPOSITION_DEST_OUT:
2194 cop = CAIRO_OPERATOR_DEST_OUT;
2195 break;
2196 case wxCOMPOSITION_DEST_ATOP:
2197 cop = CAIRO_OPERATOR_DEST_ATOP;
2198 break;
2199 case wxCOMPOSITION_XOR:
2200 cop = CAIRO_OPERATOR_XOR;
2201 break;
2202 case wxCOMPOSITION_ADD:
2203 cop = CAIRO_OPERATOR_ADD;
2204 break;
2205 default:
2206 return false;
2207 }
2208 cairo_set_operator(m_context, cop);
2209 return true;
2210 }
2211
2212 void wxCairoContext::BeginLayer(wxDouble opacity)
2213 {
2214 m_layerOpacities.push_back(opacity);
2215 cairo_push_group(m_context);
2216 }
2217
2218 void wxCairoContext::EndLayer()
2219 {
2220 float opacity = m_layerOpacities.back();
2221 m_layerOpacities.pop_back();
2222 cairo_pop_group_to_source(m_context);
2223 cairo_paint_with_alpha(m_context,opacity);
2224 }
2225
2226 //-----------------------------------------------------------------------------
2227 // wxCairoRenderer declaration
2228 //-----------------------------------------------------------------------------
2229
2230 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
2231 {
2232 public :
2233 wxCairoRenderer() {}
2234
2235 virtual ~wxCairoRenderer() {}
2236
2237 // Context
2238
2239 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
2240 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
2241 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
2242
2243 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
2244
2245 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
2246 #if wxUSE_IMAGE
2247 virtual wxGraphicsContext * CreateContextFromImage(wxImage& image);
2248 #endif // wxUSE_IMAGE
2249
2250 virtual wxGraphicsContext * CreateContext( wxWindow* window );
2251
2252 virtual wxGraphicsContext * CreateMeasuringContext();
2253 #ifdef __WXMSW__
2254 #if wxUSE_ENH_METAFILE
2255 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
2256 #endif
2257 #endif
2258 // Path
2259
2260 virtual wxGraphicsPath CreatePath();
2261
2262 // Matrix
2263
2264 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
2265 wxDouble tx=0.0, wxDouble ty=0.0);
2266
2267
2268 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
2269
2270 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
2271
2272 virtual wxGraphicsBrush
2273 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2274 wxDouble x2, wxDouble y2,
2275 const wxGraphicsGradientStops& stops);
2276
2277 virtual wxGraphicsBrush
2278 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2279 wxDouble xc, wxDouble yc,
2280 wxDouble radius,
2281 const wxGraphicsGradientStops& stops);
2282
2283 // sets the font
2284 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
2285 virtual wxGraphicsFont CreateFont(double sizeInPixels,
2286 const wxString& facename,
2287 int flags = wxFONTFLAG_DEFAULT,
2288 const wxColour& col = *wxBLACK);
2289
2290 // create a native bitmap representation
2291 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
2292 #if wxUSE_IMAGE
2293 virtual wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image);
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 #ifdef __WXGTK20__
2364 const wxDCImpl *impl = dc.GetImpl();
2365 cairo_t* context = (cairo_t*) impl->GetCairoContext();
2366 if (context)
2367 return new wxCairoContext(this,dc);
2368 else
2369 #endif
2370 return new wxCairoContext(this,dc);
2371 }
2372
2373 #ifdef __WXMSW__
2374 #if wxUSE_ENH_METAFILE
2375 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxEnhMetaFileDC& WXUNUSED(dc) )
2376 {
2377 ENSURE_LOADED_OR_RETURN(NULL);
2378 return NULL;
2379 }
2380 #endif
2381 #endif
2382
2383 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
2384 {
2385 ENSURE_LOADED_OR_RETURN(NULL);
2386 #ifdef __WXMSW__
2387 return new wxCairoContext(this,(HDC)context);
2388 #else
2389 return new wxCairoContext(this,(cairo_t*)context);
2390 #endif
2391 }
2392
2393
2394 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
2395 {
2396 ENSURE_LOADED_OR_RETURN(NULL);
2397 #ifdef __WXGTK__
2398 return new wxCairoContext(this,(GdkDrawable*)window);
2399 #else
2400 wxUnusedVar(window);
2401 return NULL;
2402 #endif
2403 }
2404
2405 #if wxUSE_IMAGE
2406 wxGraphicsContext * wxCairoRenderer::CreateContextFromImage(wxImage& image)
2407 {
2408 return new wxCairoImageContext(this, image);
2409 }
2410 #endif // wxUSE_IMAGE
2411
2412 wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
2413 {
2414 ENSURE_LOADED_OR_RETURN(NULL);
2415 #ifdef __WXGTK__
2416 return CreateContextFromNativeWindow(gdk_get_default_root_window());
2417 #endif
2418 return NULL;
2419 // TODO
2420 }
2421
2422 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
2423 {
2424 ENSURE_LOADED_OR_RETURN(NULL);
2425 return new wxCairoContext(this, window );
2426 }
2427
2428 // Path
2429
2430 wxGraphicsPath wxCairoRenderer::CreatePath()
2431 {
2432 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
2433 wxGraphicsPath path;
2434 path.SetRefData( new wxCairoPathData(this) );
2435 return path;
2436 }
2437
2438
2439 // Matrix
2440
2441 wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
2442 wxDouble tx, wxDouble ty)
2443
2444 {
2445 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
2446 wxGraphicsMatrix m;
2447 wxCairoMatrixData* data = new wxCairoMatrixData( this );
2448 data->Set( a,b,c,d,tx,ty ) ;
2449 m.SetRefData(data);
2450 return m;
2451 }
2452
2453 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
2454 {
2455 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
2456 if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
2457 return wxNullGraphicsPen;
2458 else
2459 {
2460 wxGraphicsPen p;
2461 p.SetRefData(new wxCairoPenData( this, pen ));
2462 return p;
2463 }
2464 }
2465
2466 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
2467 {
2468 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2469 if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
2470 return wxNullGraphicsBrush;
2471 else
2472 {
2473 wxGraphicsBrush p;
2474 p.SetRefData(new wxCairoBrushData( this, brush ));
2475 return p;
2476 }
2477 }
2478
2479 wxGraphicsBrush
2480 wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2481 wxDouble x2, wxDouble y2,
2482 const wxGraphicsGradientStops& stops)
2483 {
2484 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2485 wxGraphicsBrush p;
2486 wxCairoBrushData* d = new wxCairoBrushData( this );
2487 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
2488 p.SetRefData(d);
2489 return p;
2490 }
2491
2492 wxGraphicsBrush
2493 wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2494 wxDouble xc, wxDouble yc, wxDouble r,
2495 const wxGraphicsGradientStops& stops)
2496 {
2497 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2498 wxGraphicsBrush p;
2499 wxCairoBrushData* d = new wxCairoBrushData( this );
2500 d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops);
2501 p.SetRefData(d);
2502 return p;
2503 }
2504
2505 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
2506 {
2507 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
2508 if ( font.IsOk() )
2509 {
2510 wxGraphicsFont p;
2511 p.SetRefData(new wxCairoFontData( this , font, col ));
2512 return p;
2513 }
2514 else
2515 return wxNullGraphicsFont;
2516 }
2517
2518 wxGraphicsFont
2519 wxCairoRenderer::CreateFont(double sizeInPixels,
2520 const wxString& facename,
2521 int flags,
2522 const wxColour& col)
2523 {
2524 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
2525
2526 wxGraphicsFont font;
2527 font.SetRefData(new wxCairoFontData(this, sizeInPixels, facename, flags, col));
2528 return font;
2529 }
2530
2531 wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp )
2532 {
2533 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2534 if ( bmp.IsOk() )
2535 {
2536 wxGraphicsBitmap p;
2537 p.SetRefData(new wxCairoBitmapData( this , bmp ));
2538 return p;
2539 }
2540 else
2541 return wxNullGraphicsBitmap;
2542 }
2543
2544 #if wxUSE_IMAGE
2545
2546 wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromImage(const wxImage& image)
2547 {
2548 wxGraphicsBitmap bmp;
2549
2550 ENSURE_LOADED_OR_RETURN(bmp);
2551
2552 if ( image.IsOk() )
2553 {
2554 bmp.SetRefData(new wxCairoBitmapData(this, image));
2555 }
2556
2557 return bmp;
2558 }
2559
2560 #endif // wxUSE_IMAGE
2561
2562 wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
2563 {
2564 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2565 if ( bitmap != NULL )
2566 {
2567 wxGraphicsBitmap p;
2568 p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap ));
2569 return p;
2570 }
2571 else
2572 return wxNullGraphicsBitmap;
2573 }
2574
2575 wxGraphicsBitmap
2576 wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap),
2577 wxDouble WXUNUSED(x),
2578 wxDouble WXUNUSED(y),
2579 wxDouble WXUNUSED(w),
2580 wxDouble WXUNUSED(h))
2581 {
2582 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2583 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2584 return wxNullGraphicsBitmap;
2585 }
2586
2587 wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2588 {
2589 return &gs_cairoGraphicsRenderer;
2590 }
2591
2592 #else // !wxUSE_CAIRO
2593
2594 wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2595 {
2596 return NULL;
2597 }
2598
2599 #endif // wxUSE_CAIRO/!wxUSE_CAIRO
2600
2601 // MSW and OS X have their own native default renderers, but the other ports
2602 // use Cairo by default
2603 #if !(defined(__WXMSW__) || defined(__WXOSX__))
2604 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2605 {
2606 return GetCairoRenderer();
2607 }
2608 #endif // !(__WXMSW__ || __WXOSX__)
2609
2610 #endif // wxUSE_GRAPHICS_CONTEXT