wxCairoContext doesn't need to know about wxPizza
[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/dc.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/image.h"
24 #include "wx/window.h"
25 #include "wx/dc.h"
26 #include "wx/utils.h"
27 #include "wx/dialog.h"
28 #include "wx/app.h"
29 #include "wx/bitmap.h"
30 #include "wx/dcmemory.h"
31 #include "wx/log.h"
32 #include "wx/icon.h"
33 #include "wx/dcprint.h"
34 #include "wx/module.h"
35 #endif
36
37 #include "wx/graphics.h"
38 #include "wx/rawbmp.h"
39
40 #include <vector>
41
42 using namespace std;
43
44 //-----------------------------------------------------------------------------
45 // constants
46 //-----------------------------------------------------------------------------
47
48 const double RAD2DEG = 180.0 / M_PI;
49
50 //-----------------------------------------------------------------------------
51 // Local functions
52 //-----------------------------------------------------------------------------
53
54 static inline double dmin(double a, double b)
55 {
56 return a < b ? a : b;
57 }
58 static inline double dmax(double a, double b)
59 {
60 return a > b ? a : b;
61 }
62
63 static inline double DegToRad(double deg)
64 {
65 return (deg * M_PI) / 180.0;
66 }
67 static inline double RadToDeg(double deg)
68 {
69 return (deg * 180.0) / M_PI;
70 }
71
72 //-----------------------------------------------------------------------------
73 // device context implementation
74 //
75 // more and more of the dc functionality should be implemented by calling
76 // the appropricate wxCairoContext, but we will have to do that step by step
77 // also coordinate conversions should be moved to native matrix ops
78 //-----------------------------------------------------------------------------
79
80 // we always stock two context states, one at entry, to be able to preserve the
81 // state we were called with, the other one after changing to HI Graphics orientation
82 // (this one is used for getting back clippings etc)
83
84 //-----------------------------------------------------------------------------
85 // wxGraphicsPath implementation
86 //-----------------------------------------------------------------------------
87
88 // TODO remove this dependency (gdiplus needs the macros)
89
90 #ifndef max
91 #define max(a,b) (((a) > (b)) ? (a) : (b))
92 #endif
93
94 #ifndef min
95 #define min(a,b) (((a) < (b)) ? (a) : (b))
96 #endif
97
98 #include <cairo.h>
99 #ifdef __WXGTK__
100 #include <gtk/gtk.h>
101 #include "wx/fontutil.h"
102 #endif
103
104 #ifdef __WXMSW__
105 #include <cairo-win32.h>
106 #endif
107
108 #ifdef __WXMAC__
109 #include "wx/mac/private.h"
110 #include <cairo-quartz.h>
111 #include <cairo-atsui.h>
112 #endif
113
114 class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData
115 {
116 public :
117 wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL);
118 ~wxCairoPathData();
119
120 virtual wxGraphicsObjectRefData *Clone() const;
121
122 //
123 // These are the path primitives from which everything else can be constructed
124 //
125
126 // begins a new subpath at (x,y)
127 virtual void MoveToPoint( wxDouble x, wxDouble y );
128
129 // adds a straight line from the current point to (x,y)
130 virtual void AddLineToPoint( wxDouble x, wxDouble y );
131
132 // adds a cubic Bezier curve from the current point, using two control points and an end point
133 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
134
135
136 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
137 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
138
139 // gets the last point of the current path, (0,0) if not yet set
140 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
141
142 // adds another path
143 virtual void AddPath( const wxGraphicsPathData* path );
144
145 // closes the current sub-path
146 virtual void CloseSubpath();
147
148 //
149 // These are convenience functions which - if not available natively will be assembled
150 // using the primitives from above
151 //
152
153 /*
154
155 // appends a rectangle as a new closed subpath
156 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
157 // appends an ellipsis as a new closed subpath fitting the passed rectangle
158 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
159
160 // 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)
161 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
162 */
163
164 // returns the native path
165 virtual void * GetNativePath() const ;
166
167 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
168 virtual void UnGetNativePath(void *p) const;
169
170 // transforms each point of this path by the matrix
171 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
172
173 // gets the bounding box enclosing all points (possibly including control points)
174 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
175
176 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxWINDING_RULE) const;
177
178 private :
179 cairo_t* m_pathContext;
180 };
181
182 class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData
183 {
184 public :
185 wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
186 virtual ~wxCairoMatrixData() ;
187
188 virtual wxGraphicsObjectRefData *Clone() const ;
189
190 // concatenates the matrix
191 virtual void Concat( const wxGraphicsMatrixData *t );
192
193 // sets the matrix to the respective values
194 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
195 wxDouble tx=0.0, wxDouble ty=0.0);
196
197 // gets the component valuess of the matrix
198 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
199 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
200
201 // makes this the inverse matrix
202 virtual void Invert();
203
204 // returns true if the elements of the transformation matrix are equal ?
205 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
206
207 // return true if this is the identity matrix
208 virtual bool IsIdentity() const;
209
210 //
211 // transformation
212 //
213
214 // add the translation to this matrix
215 virtual void Translate( wxDouble dx , wxDouble dy );
216
217 // add the scale to this matrix
218 virtual void Scale( wxDouble xScale , wxDouble yScale );
219
220 // add the rotation to this matrix (radians)
221 virtual void Rotate( wxDouble angle );
222
223 //
224 // apply the transforms
225 //
226
227 // applies that matrix to the point
228 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
229
230 // applies the matrix except for translations
231 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
232
233 // returns the native representation
234 virtual void * GetNativeMatrix() const;
235 private:
236 cairo_matrix_t m_matrix ;
237 } ;
238
239 class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
240 {
241 public:
242 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
243 ~wxCairoPenData();
244
245 void Init();
246
247 virtual void Apply( wxGraphicsContext* context );
248 virtual wxDouble GetWidth() { return m_width; }
249
250 private :
251 double m_width;
252
253 double m_red;
254 double m_green;
255 double m_blue;
256 double m_alpha;
257
258 cairo_line_cap_t m_cap;
259 cairo_line_join_t m_join;
260
261 int m_count;
262 const double *m_lengths;
263 double *m_userLengths;
264
265 wxPen m_pen;
266 };
267
268 class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
269 {
270 public:
271 wxCairoBrushData( wxGraphicsRenderer* renderer );
272 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
273 ~wxCairoBrushData ();
274
275 virtual void Apply( wxGraphicsContext* context );
276 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
277 const wxColour&c1, const wxColour&c2 );
278 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
279 const wxColour &oColor, const wxColour &cColor );
280
281 protected:
282 virtual void Init();
283
284 private :
285 double m_red;
286 double m_green;
287 double m_blue;
288 double m_alpha;
289
290 cairo_pattern_t* m_brushPattern;
291 };
292
293 class wxCairoFontData : public wxGraphicsObjectRefData
294 {
295 public:
296 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
297 ~wxCairoFontData();
298
299 virtual void Apply( wxGraphicsContext* context );
300 #ifdef __WXGTK__
301 const PangoFontDescription* GetFont() const { return m_font; }
302 #endif
303 private :
304 double m_size;
305 double m_red;
306 double m_green;
307 double m_blue;
308 double m_alpha;
309 #ifdef __WXMAC__
310 cairo_font_face_t *m_font;
311 #elif defined(__WXGTK__)
312 PangoFontDescription* m_font;
313 #else
314 wxCharBuffer m_fontName;
315 cairo_font_slant_t m_slant;
316 cairo_font_weight_t m_weight;
317 #endif
318 };
319
320 class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
321 {
322 DECLARE_NO_COPY_CLASS(wxCairoContext)
323
324 public:
325 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
326 #ifdef __WXGTK__
327 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
328 #endif
329 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
330 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
331 wxCairoContext();
332 virtual ~wxCairoContext();
333
334 virtual bool ShouldOffset() const
335 {
336 int penwidth = 0 ;
337 if ( !m_pen.IsNull() )
338 {
339 penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth();
340 if ( penwidth == 0 )
341 penwidth = 1;
342 }
343 return ( penwidth % 2 ) == 1;
344 }
345
346 virtual void Clip( const wxRegion &region );
347
348 // clips drawings to the rect
349 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
350
351 // resets the clipping to original extent
352 virtual void ResetClip();
353
354 virtual void * GetNativeContext();
355
356 virtual void StrokePath( const wxGraphicsPath& p );
357 virtual void FillPath( const wxGraphicsPath& p , int fillStyle = wxWINDING_RULE );
358
359 virtual void Translate( wxDouble dx , wxDouble dy );
360 virtual void Scale( wxDouble xScale , wxDouble yScale );
361 virtual void Rotate( wxDouble angle );
362
363 // concatenates this transform with the current transform of this context
364 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
365
366 // sets the transform of this context
367 virtual void SetTransform( const wxGraphicsMatrix& matrix );
368
369 // gets the matrix of this context
370 virtual wxGraphicsMatrix GetTransform() const;
371
372 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
373 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
374 virtual void PushState();
375 virtual void PopState();
376
377 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
378 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
379 wxDouble *descent, wxDouble *externalLeading ) const;
380 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
381
382 private:
383 void Init(cairo_t *context);
384
385 cairo_t* m_context;
386 };
387
388 //-----------------------------------------------------------------------------
389 // wxCairoPenData implementation
390 //-----------------------------------------------------------------------------
391
392 wxCairoPenData::~wxCairoPenData()
393 {
394 delete[] m_userLengths;
395 }
396
397 void wxCairoPenData::Init()
398 {
399 m_lengths = NULL;
400 m_userLengths = NULL;
401 m_width = 0;
402 m_count = 0;
403 }
404
405 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
406 : wxGraphicsObjectRefData(renderer)
407 {
408 Init();
409 m_pen = pen;
410 m_width = m_pen.GetWidth();
411 if (m_width <= 0.0)
412 m_width = 0.1;
413
414 m_red = m_pen.GetColour().Red()/255.0;
415 m_green = m_pen.GetColour().Green()/255.0;
416 m_blue = m_pen.GetColour().Blue()/255.0;
417 m_alpha = m_pen.GetColour().Alpha()/255.0;
418
419 switch ( m_pen.GetCap() )
420 {
421 case wxCAP_ROUND :
422 m_cap = CAIRO_LINE_CAP_ROUND;
423 break;
424
425 case wxCAP_PROJECTING :
426 m_cap = CAIRO_LINE_CAP_SQUARE;
427 break;
428
429 case wxCAP_BUTT :
430 m_cap = CAIRO_LINE_CAP_BUTT;
431 break;
432
433 default :
434 m_cap = CAIRO_LINE_CAP_BUTT;
435 break;
436 }
437
438 switch ( m_pen.GetJoin() )
439 {
440 case wxJOIN_BEVEL :
441 m_join = CAIRO_LINE_JOIN_BEVEL;
442 break;
443
444 case wxJOIN_MITER :
445 m_join = CAIRO_LINE_JOIN_MITER;
446 break;
447
448 case wxJOIN_ROUND :
449 m_join = CAIRO_LINE_JOIN_ROUND;
450 break;
451
452 default :
453 m_join = CAIRO_LINE_JOIN_MITER;
454 break;
455 }
456
457 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
458 const double dotted[] =
459 {
460 dashUnit , dashUnit + 2.0
461 };
462 static const double short_dashed[] =
463 {
464 9.0 , 6.0
465 };
466 static const double dashed[] =
467 {
468 19.0 , 9.0
469 };
470 static const double dotted_dashed[] =
471 {
472 9.0 , 6.0 , 3.0 , 3.0
473 };
474
475 switch ( m_pen.GetStyle() )
476 {
477 case wxSOLID :
478 break;
479
480 case wxDOT :
481 m_count = WXSIZEOF(dotted);
482 m_userLengths = new double[ m_count ] ;
483 memcpy( m_userLengths, dotted, sizeof(dotted) );
484 m_lengths = m_userLengths;
485 break;
486
487 case wxLONG_DASH :
488 m_lengths = dashed ;
489 m_count = WXSIZEOF(dashed);
490 break;
491
492 case wxSHORT_DASH :
493 m_lengths = short_dashed ;
494 m_count = WXSIZEOF(short_dashed);
495 break;
496
497 case wxDOT_DASH :
498 m_lengths = dotted_dashed ;
499 m_count = WXSIZEOF(dotted_dashed);
500 break;
501
502 case wxUSER_DASH :
503 {
504 wxDash *wxdashes ;
505 m_count = m_pen.GetDashes( &wxdashes ) ;
506 if ((wxdashes != NULL) && (m_count > 0))
507 {
508 m_userLengths = new double[m_count] ;
509 for ( int i = 0 ; i < m_count ; ++i )
510 {
511 m_userLengths[i] = wxdashes[i] * dashUnit ;
512
513 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
514 m_userLengths[i] = dashUnit + 2.0 ;
515 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
516 m_userLengths[i] = dashUnit ;
517 }
518 }
519 m_lengths = m_userLengths ;
520 }
521 break;
522 case wxSTIPPLE :
523 {
524 /*
525 wxBitmap* bmp = pen.GetStipple();
526 if ( bmp && bmp->Ok() )
527 {
528 wxDELETE( m_penImage );
529 wxDELETE( m_penBrush );
530 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
531 m_penBrush = new TextureBrush(m_penImage);
532 m_pen->SetBrush( m_penBrush );
533 }
534 */
535 }
536 break;
537 default :
538 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
539 {
540 /*
541 wxDELETE( m_penBrush );
542 HatchStyle style = HatchStyleHorizontal;
543 switch( pen.GetStyle() )
544 {
545 case wxBDIAGONAL_HATCH :
546 style = HatchStyleBackwardDiagonal;
547 break ;
548 case wxCROSSDIAG_HATCH :
549 style = HatchStyleDiagonalCross;
550 break ;
551 case wxFDIAGONAL_HATCH :
552 style = HatchStyleForwardDiagonal;
553 break ;
554 case wxCROSS_HATCH :
555 style = HatchStyleCross;
556 break ;
557 case wxHORIZONTAL_HATCH :
558 style = HatchStyleHorizontal;
559 break ;
560 case wxVERTICAL_HATCH :
561 style = HatchStyleVertical;
562 break ;
563
564 }
565 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
566 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
567 m_pen->SetBrush( m_penBrush )
568 */
569 }
570 break;
571 }
572 }
573
574 void wxCairoPenData::Apply( wxGraphicsContext* context )
575 {
576 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
577 cairo_set_line_width(ctext,m_width);
578 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
579 cairo_set_line_cap(ctext,m_cap);
580 cairo_set_line_join(ctext,m_join);
581 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
582 }
583
584 //-----------------------------------------------------------------------------
585 // wxCairoBrushData implementation
586 //-----------------------------------------------------------------------------
587
588 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
589 : wxGraphicsObjectRefData( renderer )
590 {
591 Init();
592 }
593
594 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
595 : wxGraphicsObjectRefData(renderer)
596 {
597 Init();
598
599 m_red = brush.GetColour().Red()/255.0;
600 m_green = brush.GetColour().Green()/255.0;
601 m_blue = brush.GetColour().Blue()/255.0;
602 m_alpha = brush.GetColour().Alpha()/255.0;
603 /*
604 if ( brush.GetStyle() == wxSOLID)
605 {
606 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
607 brush.GetColour().Green() , brush.GetColour().Blue() ) );
608 }
609 else if ( brush.IsHatch() )
610 {
611 HatchStyle style = HatchStyleHorizontal;
612 switch( brush.GetStyle() )
613 {
614 case wxBDIAGONAL_HATCH :
615 style = HatchStyleBackwardDiagonal;
616 break ;
617 case wxCROSSDIAG_HATCH :
618 style = HatchStyleDiagonalCross;
619 break ;
620 case wxFDIAGONAL_HATCH :
621 style = HatchStyleForwardDiagonal;
622 break ;
623 case wxCROSS_HATCH :
624 style = HatchStyleCross;
625 break ;
626 case wxHORIZONTAL_HATCH :
627 style = HatchStyleHorizontal;
628 break ;
629 case wxVERTICAL_HATCH :
630 style = HatchStyleVertical;
631 break ;
632
633 }
634 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
635 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
636 }
637 else
638 {
639 wxBitmap* bmp = brush.GetStipple();
640 if ( bmp && bmp->Ok() )
641 {
642 wxDELETE( m_brushImage );
643 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
644 m_brush = new TextureBrush(m_brushImage);
645 }
646 }
647 */
648 }
649
650 wxCairoBrushData::~wxCairoBrushData ()
651 {
652 if (m_brushPattern)
653 cairo_pattern_destroy(m_brushPattern);
654 }
655
656 void wxCairoBrushData::Apply( wxGraphicsContext* context )
657 {
658 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
659 if ( m_brushPattern )
660 {
661 cairo_set_source(ctext,m_brushPattern);
662 }
663 else
664 {
665 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
666 }
667 }
668
669 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
670 const wxColour&c1, const wxColour&c2 )
671 {
672 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
673 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
674 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
675 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
676 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
677 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
678 }
679
680 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
681 const wxColour &oColor, const wxColour &cColor )
682 {
683 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
684 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
685 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
686 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
687 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
688 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
689 }
690
691 void wxCairoBrushData::Init()
692 {
693 m_brushPattern = NULL;
694 }
695
696 //-----------------------------------------------------------------------------
697 // wxCairoFontData implementation
698 //-----------------------------------------------------------------------------
699
700 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
701 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
702 {
703 m_red = col.Red()/255.0;
704 m_green = col.Green()/255.0;
705 m_blue = col.Blue()/255.0;
706 m_alpha = col.Alpha()/255.0;
707 m_size = font.GetPointSize();
708
709 #ifdef __WXMAC__
710 m_font = cairo_atsui_font_face_create_for_atsu_font_id( font.MacGetATSUFontID() );
711 #elif defined(__WXGTK__)
712 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
713 #else
714 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
715 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
716 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
717 #endif
718 }
719
720 wxCairoFontData::~wxCairoFontData()
721 {
722 #ifdef __WXMAC__
723 cairo_font_face_destroy( m_font );
724 #elif defined(__WXGTK__)
725 pango_font_description_free( m_font );
726 #else
727 #endif
728 }
729
730 void wxCairoFontData::Apply( wxGraphicsContext* context )
731 {
732 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
733 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
734 #ifdef __WXGTK__
735 // the rest is done using Pango layouts
736 #elif defined(__WXMAC__)
737 cairo_set_font_face(ctext, m_font);
738 #else
739 cairo_select_font_face(ctext, m_fontName, m_slant, m_weights );
740 cairo_set_font_size(ctext, m_size );
741 #endif
742 }
743
744 //-----------------------------------------------------------------------------
745 // wxCairoPathData implementation
746 //-----------------------------------------------------------------------------
747
748 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
749 : wxGraphicsPathData(renderer)
750 {
751 if (pathcontext)
752 {
753 m_pathContext = pathcontext;
754 }
755 else
756 {
757 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
758 m_pathContext = cairo_create(surface);
759 cairo_surface_destroy (surface);
760 }
761 }
762
763 wxCairoPathData::~wxCairoPathData()
764 {
765 cairo_destroy(m_pathContext);
766 }
767
768 wxGraphicsObjectRefData *wxCairoPathData::Clone() const
769 {
770 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
771 cairo_t* pathcontext = cairo_create(surface);
772 cairo_surface_destroy (surface);
773
774 cairo_path_t* path = cairo_copy_path(m_pathContext);
775 cairo_append_path(pathcontext, path);
776 cairo_path_destroy(path);
777 return new wxCairoPathData( GetRenderer() ,pathcontext);
778 }
779
780
781 void* wxCairoPathData::GetNativePath() const
782 {
783 return cairo_copy_path(m_pathContext) ;
784 }
785
786 void wxCairoPathData::UnGetNativePath(void *p) const
787 {
788 cairo_path_destroy((cairo_path_t*)p);
789 }
790
791 //
792 // The Primitives
793 //
794
795 void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
796 {
797 cairo_move_to(m_pathContext,x,y);
798 }
799
800 void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
801 {
802 cairo_line_to(m_pathContext,x,y);
803 }
804
805 void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
806 {
807 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
808 cairo_append_path(m_pathContext, p);
809 UnGetNativePath(p);
810 }
811
812 void wxCairoPathData::CloseSubpath()
813 {
814 cairo_close_path(m_pathContext);
815 }
816
817 void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
818 {
819 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
820 }
821
822 // gets the last point of the current path, (0,0) if not yet set
823 void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
824 {
825 double dx,dy;
826 cairo_get_current_point(m_pathContext,&dx,&dy);
827 if (x)
828 *x = dx;
829 if (y)
830 *y = dy;
831 }
832
833 void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
834 {
835 // as clockwise means positive in our system (y pointing downwards)
836 // TODO make this interpretation dependent of the
837 // real device trans
838 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
839 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
840 else
841 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
842 }
843
844 // transforms each point of this path by the matrix
845 void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
846 {
847 // as we don't have a true path object, we have to apply the inverse
848 // matrix to the context
849 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
850 cairo_matrix_invert( &m );
851 cairo_transform(m_pathContext,&m);
852 }
853
854 // gets the bounding box enclosing all points (possibly including control points)
855 void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
856 {
857 double x1,y1,x2,y2;
858
859 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
860 if ( x2 < x1 )
861 {
862 *x = x2;
863 *w = x1-x2;
864 }
865 else
866 {
867 *x = x1;
868 *w = x2-x1;
869 }
870
871 if( y2 < y1 )
872 {
873 *y = y2;
874 *h = y1-y2;
875 }
876 else
877 {
878 *y = y1;
879 *h = y2-y1;
880 }
881 }
882
883 bool wxCairoPathData::Contains( wxDouble x, wxDouble y, int WXUNUSED(fillStyle) ) const
884 {
885 return cairo_in_stroke( m_pathContext, x, y) != 0;
886 }
887
888 //-----------------------------------------------------------------------------
889 // wxCairoMatrixData implementation
890 //-----------------------------------------------------------------------------
891
892 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
893 : wxGraphicsMatrixData(renderer)
894 {
895 if ( matrix )
896 m_matrix = *matrix;
897 }
898
899 wxCairoMatrixData::~wxCairoMatrixData()
900 {
901 // nothing to do
902 }
903
904 wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
905 {
906 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
907 }
908
909 // concatenates the matrix
910 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
911 {
912 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
913 }
914
915 // sets the matrix to the respective values
916 void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
917 wxDouble tx, wxDouble ty)
918 {
919 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
920 }
921
922 // gets the component valuess of the matrix
923 void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
924 wxDouble* d, wxDouble* tx, wxDouble* ty) const
925 {
926 if (a) *a = m_matrix.xx;
927 if (b) *b = m_matrix.yx;
928 if (c) *c = m_matrix.xy;
929 if (d) *d = m_matrix.yy;
930 if (tx) *tx= m_matrix.x0;
931 if (ty) *ty= m_matrix.y0;
932 }
933
934 // makes this the inverse matrix
935 void wxCairoMatrixData::Invert()
936 {
937 cairo_matrix_invert( &m_matrix );
938 }
939
940 // returns true if the elements of the transformation matrix are equal ?
941 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
942 {
943 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
944 return (
945 m_matrix.xx == tm->xx &&
946 m_matrix.yx == tm->yx &&
947 m_matrix.xy == tm->xy &&
948 m_matrix.yy == tm->yy &&
949 m_matrix.x0 == tm->x0 &&
950 m_matrix.y0 == tm->y0 ) ;
951 }
952
953 // return true if this is the identity matrix
954 bool wxCairoMatrixData::IsIdentity() const
955 {
956 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
957 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
958 }
959
960 //
961 // transformation
962 //
963
964 // add the translation to this matrix
965 void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
966 {
967 cairo_matrix_translate( &m_matrix, dx, dy) ;
968 }
969
970 // add the scale to this matrix
971 void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
972 {
973 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
974 }
975
976 // add the rotation to this matrix (radians)
977 void wxCairoMatrixData::Rotate( wxDouble angle )
978 {
979 cairo_matrix_rotate( &m_matrix, angle) ;
980 }
981
982 //
983 // apply the transforms
984 //
985
986 // applies that matrix to the point
987 void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
988 {
989 double lx = *x, ly = *y ;
990 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
991 *x = lx;
992 *y = ly;
993 }
994
995 // applies the matrix except for translations
996 void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
997 {
998 double lx = *dx, ly = *dy ;
999 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1000 *dx = lx;
1001 *dy = ly;
1002 }
1003
1004 // returns the native representation
1005 void * wxCairoMatrixData::GetNativeMatrix() const
1006 {
1007 return (void*) &m_matrix;
1008 }
1009
1010 //-----------------------------------------------------------------------------
1011 // wxCairoContext implementation
1012 //-----------------------------------------------------------------------------
1013
1014 class wxCairoOffsetHelper
1015 {
1016 public :
1017 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1018 {
1019 m_ctx = ctx;
1020 m_offset = offset;
1021 if ( m_offset )
1022 cairo_translate( m_ctx, 0.5, 0.5 );
1023 }
1024 ~wxCairoOffsetHelper( )
1025 {
1026 if ( m_offset )
1027 cairo_translate( m_ctx, -0.5, -0.5 );
1028 }
1029 public :
1030 cairo_t* m_ctx;
1031 bool m_offset;
1032 } ;
1033
1034 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1035 : wxGraphicsContext(renderer)
1036 {
1037 #ifdef __WXGTK__
1038 Init( gdk_cairo_create( dc.m_window ) );
1039 #endif
1040 #ifdef __WXMAC__
1041 int width, height;
1042 dc.GetSize( &width, &height );
1043 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1044 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1045 Init( cairo_create( surface ) );
1046 cairo_surface_destroy( surface );
1047 #endif
1048 }
1049
1050 #ifdef __WXGTK__
1051 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1052 : wxGraphicsContext(renderer)
1053 {
1054 Init( gdk_cairo_create( drawable ) );
1055 }
1056 #endif
1057
1058 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1059 : wxGraphicsContext(renderer)
1060 {
1061 Init( context );
1062 }
1063
1064 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1065 : wxGraphicsContext(renderer)
1066 {
1067 #ifdef __WXGTK__
1068 // something along these lines (copied from dcclient)
1069
1070 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1071 // code should still be able to create wxClientDCs for them, so we will
1072 // use the parent window here then.
1073 if (window->m_wxwindow == NULL)
1074 {
1075 window = window->GetParent();
1076 }
1077
1078 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
1079
1080 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
1081 #endif
1082 }
1083
1084 wxCairoContext::~wxCairoContext()
1085 {
1086 if ( m_context )
1087 {
1088 PopState();
1089 PopState();
1090 cairo_destroy(m_context);
1091 }
1092 }
1093
1094 void wxCairoContext::Init(cairo_t *context)
1095 {
1096 m_context = context ;
1097 PushState();
1098 PushState();
1099 }
1100
1101
1102 void wxCairoContext::Clip( const wxRegion& region )
1103 {
1104 // Create a path with all the rectangles in the region
1105 wxGraphicsPath path = GetRenderer()->CreatePath();
1106 wxRegionIterator ri(region);
1107 while (ri)
1108 {
1109 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1110 ++ri;
1111 }
1112
1113 // Put it in the context
1114 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1115 cairo_append_path(m_context, cp);
1116
1117 // clip to that path
1118 cairo_clip(m_context);
1119 path.UnGetNativePath(cp);
1120 }
1121
1122 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1123 {
1124 // Create a path with this rectangle
1125 wxGraphicsPath path = GetRenderer()->CreatePath();
1126 path.AddRectangle(x,y,w,h);
1127
1128 // Put it in the context
1129 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1130 cairo_append_path(m_context, cp);
1131
1132 // clip to that path
1133 cairo_clip(m_context);
1134 path.UnGetNativePath(cp);
1135 }
1136
1137 void wxCairoContext::ResetClip()
1138 {
1139 cairo_reset_clip(m_context);
1140 }
1141
1142
1143 void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1144 {
1145 if ( !m_pen.IsNull() )
1146 {
1147 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1148 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1149 cairo_append_path(m_context,cp);
1150 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1151 cairo_stroke(m_context);
1152 path.UnGetNativePath(cp);
1153 }
1154 }
1155
1156 void wxCairoContext::FillPath( const wxGraphicsPath& path , int fillStyle )
1157 {
1158 if ( !m_brush.IsNull() )
1159 {
1160 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1161 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1162 cairo_append_path(m_context,cp);
1163 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1164 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1165 cairo_fill(m_context);
1166 path.UnGetNativePath(cp);
1167 }
1168 }
1169
1170 void wxCairoContext::Rotate( wxDouble angle )
1171 {
1172 cairo_rotate(m_context,angle);
1173 }
1174
1175 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1176 {
1177 cairo_translate(m_context,dx,dy);
1178 }
1179
1180 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1181 {
1182 cairo_scale(m_context,xScale,yScale);
1183 }
1184
1185 // concatenates this transform with the current transform of this context
1186 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1187 {
1188 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1189 }
1190
1191 // sets the transform of this context
1192 void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1193 {
1194 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1195 }
1196
1197 // gets the matrix of this context
1198 wxGraphicsMatrix wxCairoContext::GetTransform() const
1199 {
1200 wxGraphicsMatrix matrix = CreateMatrix();
1201 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1202 return matrix;
1203 }
1204
1205
1206
1207 void wxCairoContext::PushState()
1208 {
1209 cairo_save(m_context);
1210 }
1211
1212 void wxCairoContext::PopState()
1213 {
1214 cairo_restore(m_context);
1215 }
1216
1217 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1218 {
1219 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1220
1221 cairo_surface_t* surface;
1222 int bw = bmp.GetWidth();
1223 int bh = bmp.GetHeight();
1224 wxBitmap bmpSource = bmp; // we need a non-const instance
1225 unsigned char* buffer = new unsigned char[bw*bh*4];
1226 wxUint32* data = (wxUint32*)buffer;
1227
1228 // Create a surface object and copy the bitmap pixel data to it. if the
1229 // image has alpha (or a mask represented as alpha) then we'll use a
1230 // different format and iterator than if it doesn't...
1231 if (bmpSource.HasAlpha() || bmpSource.GetMask())
1232 {
1233 surface = cairo_image_surface_create_for_data(
1234 buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1235 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1236 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1237
1238 wxAlphaPixelData::Iterator p(pixData);
1239 for (int y=0; y<bh; y++)
1240 {
1241 wxAlphaPixelData::Iterator rowStart = p;
1242 for (int x=0; x<bw; x++)
1243 {
1244 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1245 // with alpha in the upper 8 bits, then red, then green, then
1246 // blue. The 32-bit quantities are stored native-endian.
1247 // Pre-multiplied alpha is used.
1248 unsigned char alpha = p.Alpha();
1249 if (alpha == 0)
1250 *data = 0;
1251 else
1252 *data = ( alpha << 24
1253 | (p.Red() * alpha/255) << 16
1254 | (p.Green() * alpha/255) << 8
1255 | (p.Blue() * alpha/255) );
1256 ++data;
1257 ++p;
1258 }
1259 p = rowStart;
1260 p.OffsetY(pixData, 1);
1261 }
1262 }
1263 else // no alpha
1264 {
1265 surface = cairo_image_surface_create_for_data(
1266 buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1267 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1268 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1269
1270 wxNativePixelData::Iterator p(pixData);
1271 for (int y=0; y<bh; y++)
1272 {
1273 wxNativePixelData::Iterator rowStart = p;
1274 for (int x=0; x<bw; x++)
1275 {
1276 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1277 // the upper 8 bits unused. Red, Green, and Blue are stored in
1278 // the remaining 24 bits in that order. The 32-bit quantities
1279 // are stored native-endian.
1280 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1281 ++data;
1282 ++p;
1283 }
1284 p = rowStart;
1285 p.OffsetY(pixData, 1);
1286 }
1287 }
1288
1289
1290 PushState();
1291
1292 // In case we're scaling the image by using a width and height different
1293 // than the bitmap's size create a pattern transformation on the surface and
1294 // draw the transformed pattern.
1295 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
1296 wxDouble scaleX = w / bw;
1297 wxDouble scaleY = h / bh;
1298 cairo_scale(m_context, scaleX, scaleY);
1299
1300 // prepare to draw the image
1301 cairo_translate(m_context, x, y);
1302 cairo_set_source(m_context, pattern);
1303 // use the original size here since the context is scaled already...
1304 cairo_rectangle(m_context, 0, 0, bw, bh);
1305 // fill the rectangle using the pattern
1306 cairo_fill(m_context);
1307
1308 // clean up
1309 cairo_pattern_destroy(pattern);
1310 cairo_surface_destroy(surface);
1311 delete [] buffer;
1312 PopState();
1313 }
1314
1315 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1316 {
1317 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1318 // start using the Cairo backend on other platforms then we may need to
1319 // fiddle with this...
1320 DrawBitmap(icon, x, y, w, h);
1321 }
1322
1323
1324 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1325 {
1326 if ( m_font.IsNull() || str.empty())
1327 return;
1328
1329 #ifdef __WXGTK__
1330 const wxCharBuffer data = wxConvUTF8.cWC2MB( str );
1331 if ( !data )
1332 return;
1333 size_t datalen = strlen(data);
1334 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1335
1336 PangoLayout *layout = pango_cairo_create_layout (m_context);
1337 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1338 pango_layout_set_text(layout, data, datalen);
1339 cairo_move_to(m_context, x, y);
1340 pango_cairo_show_layout (m_context, layout);
1341
1342 g_object_unref (layout);
1343 #else
1344 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1345 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1346 // the position we move to by the ascent.
1347 cairo_font_extents_t fe;
1348 cairo_font_extents(m_context, &fe);
1349 cairo_move_to(m_context, x, y+fe.ascent);
1350
1351 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1352 cairo_show_text(m_context,buf);
1353 #endif
1354 }
1355
1356 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1357 wxDouble *descent, wxDouble *externalLeading ) const
1358 {
1359 if ( width )
1360 *width = 0;
1361 if ( height )
1362 *height = 0;
1363 if ( descent )
1364 *descent = 0;
1365 if ( externalLeading )
1366 *externalLeading = 0;
1367
1368 if ( m_font.IsNull() || str.empty())
1369 return;
1370
1371 #ifdef __WXGTK__
1372 int w, h;
1373
1374 PangoLayout *layout = pango_cairo_create_layout (m_context);
1375 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1376 const wxCharBuffer data = wxConvUTF8.cWC2MB( str );
1377 if ( !data )
1378 {
1379 return;
1380 }
1381 pango_layout_set_text( layout, data, strlen(data) );
1382 pango_layout_get_pixel_size (layout, &w, &h);
1383 if ( width )
1384 *width = w;
1385 if ( height )
1386 *height = h;
1387 if (descent)
1388 {
1389 PangoLayoutIter *iter = pango_layout_get_iter(layout);
1390 int baseline = pango_layout_iter_get_baseline(iter);
1391 pango_layout_iter_free(iter);
1392 *descent = h - PANGO_PIXELS(baseline);
1393 }
1394 g_object_unref (layout);
1395 #else
1396 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1397
1398 if (width)
1399 {
1400 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1401 cairo_text_extents_t te;
1402 cairo_text_extents(m_context, buf, &te);
1403 *width = te.width;
1404 }
1405
1406 if (height || descent || externalLeading)
1407 {
1408 cairo_font_extents_t fe;
1409 cairo_font_extents(m_context, &fe);
1410
1411 if (height)
1412 *height = fe.height;
1413 if ( descent )
1414 *descent = fe.descent;
1415 if ( externalLeading )
1416 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1417 }
1418 #endif
1419 }
1420
1421 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1422 {
1423 widths.Empty();
1424 widths.Add(0, text.length());
1425
1426 if (text.empty())
1427 return;
1428
1429 // TODO
1430 }
1431
1432 void * wxCairoContext::GetNativeContext()
1433 {
1434 return m_context;
1435 }
1436
1437 //-----------------------------------------------------------------------------
1438 // wxCairoRenderer declaration
1439 //-----------------------------------------------------------------------------
1440
1441 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1442 {
1443 public :
1444 wxCairoRenderer() {}
1445
1446 virtual ~wxCairoRenderer() {}
1447
1448 // Context
1449
1450 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1451
1452 #ifdef __WXMSW__
1453 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1454 #endif
1455
1456 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1457
1458 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1459
1460 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1461
1462 virtual wxGraphicsContext * CreateMeasuringContext();
1463
1464 // Path
1465
1466 virtual wxGraphicsPath CreatePath();
1467
1468 // Matrix
1469
1470 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1471 wxDouble tx=0.0, wxDouble ty=0.0);
1472
1473
1474 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1475
1476 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1477
1478 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1479 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1480 const wxColour&c1, const wxColour&c2) ;
1481
1482 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1483 // with radius r and color cColor
1484 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1485 const wxColour &oColor, const wxColour &cColor) ;
1486
1487 // sets the font
1488 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1489
1490 private :
1491 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1492 } ;
1493
1494 //-----------------------------------------------------------------------------
1495 // wxCairoRenderer implementation
1496 //-----------------------------------------------------------------------------
1497
1498 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1499
1500 static wxCairoRenderer gs_cairoGraphicsRenderer;
1501
1502 #ifdef __WXGTK__
1503 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1504 {
1505 return &gs_cairoGraphicsRenderer;
1506 }
1507 #endif
1508
1509 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1510 {
1511 return new wxCairoContext(this,dc);
1512 }
1513
1514 #ifdef __WXMSW__
1515 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
1516 {
1517 return NULL;
1518 }
1519 #endif
1520
1521 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1522 {
1523 return new wxCairoContext(this,(cairo_t*)context);
1524 }
1525
1526
1527 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1528 {
1529 #ifdef __WXGTK__
1530 return new wxCairoContext(this,(GdkDrawable*)window);
1531 #else
1532 return NULL;
1533 #endif
1534 }
1535
1536 wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
1537 {
1538 return NULL;
1539 // TODO
1540 }
1541
1542 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1543 {
1544 return new wxCairoContext(this, window );
1545 }
1546
1547 // Path
1548
1549 wxGraphicsPath wxCairoRenderer::CreatePath()
1550 {
1551 wxGraphicsPath path;
1552 path.SetRefData( new wxCairoPathData(this) );
1553 return path;
1554 }
1555
1556
1557 // Matrix
1558
1559 wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1560 wxDouble tx, wxDouble ty)
1561
1562 {
1563 wxGraphicsMatrix m;
1564 wxCairoMatrixData* data = new wxCairoMatrixData( this );
1565 data->Set( a,b,c,d,tx,ty ) ;
1566 m.SetRefData(data);
1567 return m;
1568 }
1569
1570 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1571 {
1572 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1573 return wxNullGraphicsPen;
1574 else
1575 {
1576 wxGraphicsPen p;
1577 p.SetRefData(new wxCairoPenData( this, pen ));
1578 return p;
1579 }
1580 }
1581
1582 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1583 {
1584 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1585 return wxNullGraphicsBrush;
1586 else
1587 {
1588 wxGraphicsBrush p;
1589 p.SetRefData(new wxCairoBrushData( this, brush ));
1590 return p;
1591 }
1592 }
1593
1594 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1595 wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1596 const wxColour&c1, const wxColour&c2)
1597 {
1598 wxGraphicsBrush p;
1599 wxCairoBrushData* d = new wxCairoBrushData( this );
1600 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1601 p.SetRefData(d);
1602 return p;
1603 }
1604
1605 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1606 // with radius r and color cColor
1607 wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1608 const wxColour &oColor, const wxColour &cColor)
1609 {
1610 wxGraphicsBrush p;
1611 wxCairoBrushData* d = new wxCairoBrushData( this );
1612 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1613 p.SetRefData(d);
1614 return p;
1615 }
1616
1617 // sets the font
1618 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1619 {
1620 if ( font.Ok() )
1621 {
1622 wxGraphicsFont p;
1623 p.SetRefData(new wxCairoFontData( this , font, col ));
1624 return p;
1625 }
1626 else
1627 return wxNullGraphicsFont;
1628 }
1629
1630 #endif // wxUSE_GRAPHICS_CONTEXT