1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
22 #include "wx/window.h"
25 #include "wx/dialog.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
31 #include "wx/dcprint.h"
32 #include "wx/module.h"
35 #include "wx/graphics.h"
37 #if wxUSE_GRAPHICS_CONTEXT
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 const double RAD2DEG
= 180.0 / M_PI
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 static inline double dmin(double a
, double b
)
57 static inline double dmax(double a
, double b
)
62 static inline double DegToRad(double deg
)
64 return (deg
* M_PI
) / 180.0;
66 static inline double RadToDeg(double deg
)
68 return (deg
* 180.0) / M_PI
;
71 //-----------------------------------------------------------------------------
72 // device context implementation
74 // more and more of the dc functionality should be implemented by calling
75 // the appropricate wxCairoContext, but we will have to do that step by step
76 // also coordinate conversions should be moved to native matrix ops
77 //-----------------------------------------------------------------------------
79 // we always stock two context states, one at entry, to be able to preserve the
80 // state we were called with, the other one after changing to HI Graphics orientation
81 // (this one is used for getting back clippings etc)
83 //-----------------------------------------------------------------------------
84 // wxGraphicsPath implementation
85 //-----------------------------------------------------------------------------
87 // TODO remove this dependency (gdiplus needs the macros)
90 #define max(a,b) (((a) > (b)) ? (a) : (b))
94 #define min(a,b) (((a) < (b)) ? (a) : (b))
100 class WXDLLEXPORT wxCairoPath
: public wxGraphicsPath
102 DECLARE_NO_COPY_CLASS(wxCairoPath
)
109 // These are the path primitives from which everything else can be constructed
112 // begins a new subpath at (x,y)
113 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
115 // adds a straight line from the current point to (x,y)
116 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
118 // adds a cubic Bezier curve from the current point, using two control points and an end point
119 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
122 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
123 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
125 // gets the last point of the current path, (0,0) if not yet set
126 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) ;
128 // closes the current sub-path
129 virtual void CloseSubpath();
132 // These are convenience functions which - if not available natively will be assembled
133 // using the primitives from above
138 // appends a rectangle as a new closed subpath
139 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
140 // appends an ellipsis as a new closed subpath fitting the passed rectangle
141 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
143 // 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)
144 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
147 // returns the native path
148 virtual void * GetNativePath() const ;
150 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
151 virtual void UnGetNativePath(void *p
) ;
154 cairo_t
* m_pathContext
;
157 wxCairoPath::wxCairoPath()
159 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
160 m_pathContext
= cairo_create(surface
);
161 cairo_surface_destroy (surface
);
164 wxCairoPath::~wxCairoPath()
166 cairo_destroy(m_pathContext
);
169 void* wxCairoPath::GetNativePath() const
171 return cairo_copy_path(m_pathContext
) ;
174 void wxCairoPath::UnGetNativePath(void *p
)
176 cairo_path_destroy((cairo_path_t
*)p
);
183 void wxCairoPath::MoveToPoint( wxDouble x
, wxDouble y
)
185 cairo_move_to(m_pathContext
,x
,y
);
188 void wxCairoPath::AddLineToPoint( wxDouble x
, wxDouble y
)
190 cairo_line_to(m_pathContext
,x
,y
);
193 void wxCairoPath::CloseSubpath()
195 cairo_close_path(m_pathContext
);
198 void wxCairoPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
200 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
203 // gets the last point of the current path, (0,0) if not yet set
204 void wxCairoPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
207 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
212 void wxCairoPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
214 // as clockwise means positive in our system (y pointing downwards)
215 // TODO make this interpretation dependent of the
217 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
218 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
220 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
224 void wxCairoPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
226 m_path->AddRectangle(RectF(x,y,w,h));
233 // closes the current subpath
234 void wxCairoPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
236 // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
241 class WXDLLEXPORT wxCairoContext
: public wxGraphicsContext
243 DECLARE_NO_COPY_CLASS(wxCairoContext
)
246 wxCairoContext( const wxWindowDC
& dc
);
248 virtual ~wxCairoContext();
250 virtual void Clip( const wxRegion
®ion
);
252 // clips drawings to the rect
253 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
255 // resets the clipping to original extent
256 virtual void ResetClip();
258 virtual void * GetNativeContext();
260 virtual void StrokePath( const wxGraphicsPath
*p
);
261 virtual void FillPath( const wxGraphicsPath
*p
, int fillStyle
= wxWINDING_RULE
);
263 virtual wxGraphicsPath
* CreatePath();
264 virtual void SetPen( const wxPen
&pen
);
265 virtual void SetBrush( const wxBrush
&brush
);
266 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
) ;
267 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
268 const wxColour
&oColor
, const wxColour
&cColor
);
270 virtual void Translate( wxDouble dx
, wxDouble dy
);
271 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
272 virtual void Rotate( wxDouble angle
);
274 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
275 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
276 virtual void PushState();
277 virtual void PopState();
279 virtual void SetFont( const wxFont
&font
);
280 virtual void SetTextColour( const wxColour
&col
);
281 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
282 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
283 wxDouble
*descent
, wxDouble
*externalLeading
) const;
284 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
288 bool m_penTransparent
;
289 bool m_brushTransparent
;
293 cairo_pattern_t
* m_brushPattern
;
294 wxColour m_textColour
;
300 //-----------------------------------------------------------------------------
301 // wxCairoContext implementation
302 //-----------------------------------------------------------------------------
304 wxCairoContext::wxCairoContext( const wxWindowDC
& dc
)
306 m_context
= gdk_cairo_create( dc
.m_window
) ;
309 m_penTransparent
= true;
310 m_brushTransparent
= true;
311 m_brushPattern
= NULL
;
314 wxCairoContext::~wxCairoContext()
320 cairo_destroy(m_context
);
321 if ( m_brushPattern
)
322 cairo_pattern_destroy(m_brushPattern
);
327 void wxCairoContext::Clip( const wxRegion
& WXUNUSED(region
) )
332 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
337 void wxCairoContext::ResetClip()
343 void wxCairoContext::StrokePath( const wxGraphicsPath
*path
)
345 if ( m_penTransparent
)
348 cairo_path_t
* cp
= (cairo_path_t
*) path
->GetNativePath() ;
349 cairo_append_path(m_context
,cp
);
353 // TODO: * m_dc->m_scaleX
354 double penWidth
= m_pen
.GetWidth();
358 cairo_set_line_width(m_context
,penWidth
);
359 cairo_set_source_rgba(m_context
,m_pen
.GetColour().Red()/255.0,
360 m_pen
.GetColour().Green()/255.0, m_pen
.GetColour().Blue()/255.0,m_pen
.GetColour().Alpha()/255.0);
362 cairo_line_cap_t cap
;
363 switch ( m_pen
.GetCap() )
366 cap
= CAIRO_LINE_CAP_ROUND
;
369 case wxCAP_PROJECTING
:
370 cap
= CAIRO_LINE_CAP_SQUARE
;
374 cap
= CAIRO_LINE_CAP_BUTT
;
378 cap
= CAIRO_LINE_CAP_BUTT
;
381 cairo_set_line_cap(m_context
,cap
);
383 cairo_line_join_t join
;
384 switch ( m_pen
.GetJoin() )
387 join
= CAIRO_LINE_JOIN_BEVEL
;
391 join
= CAIRO_LINE_JOIN_MITER
;
395 join
= CAIRO_LINE_JOIN_ROUND
;
399 join
= CAIRO_LINE_JOIN_MITER
;
402 cairo_set_line_join(m_context
,join
);
405 const double * dashes
= NULL
;
408 const double dashUnit
= penWidth
< 1.0 ? 1.0 : penWidth
;
410 double *userLengths
= NULL
;
411 const double dotted
[] =
413 dashUnit
, dashUnit
+ 2.0
415 const double short_dashed
[] =
419 const double dashed
[] =
423 const double dotted_dashed
[] =
425 9.0 , 6.0 , 3.0 , 3.0
428 switch ( m_pen
.GetStyle() )
435 num_dashes
= WXSIZEOF(dotted
)
441 num_dashes
= WXSIZEOF(dashed
);
446 num_dashes
= WXSIZEOF(short_dashed
);
451 num_dashes
= WXSIZEOF(dotted_dashed
);
457 num_dashes
= m_pen
.GetDashes( &wxdashes
) ;
458 if ((wxdashes
!= NULL
) && (num_dashes
> 0))
460 userLengths
= new double[num_dashes
] ;
461 for ( int i
= 0 ; i
< num_dashes
; ++i
)
463 userLengths
[i
] = wxdashes
[i
] * dashUnit
;
465 if ( i
% 2 == 1 && userLengths
[i
] < dashUnit
+ 2.0 )
466 userLengths
[i
] = dashUnit
+ 2.0 ;
467 else if ( i
% 2 == 0 && userLengths
[i
] < dashUnit
)
468 userLengths
[i
] = dashUnit
;
471 dashes
= userLengths
;
477 wxBitmap* bmp = pen.GetStipple();
478 if ( bmp && bmp->Ok() )
480 wxDELETE( m_penImage );
481 wxDELETE( m_penBrush );
482 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
483 m_penBrush = new TextureBrush(m_penImage);
484 m_pen->SetBrush( m_penBrush );
490 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
493 wxDELETE( m_penBrush );
494 HatchStyle style = HatchStyleHorizontal;
495 switch( pen.GetStyle() )
497 case wxBDIAGONAL_HATCH :
498 style = HatchStyleBackwardDiagonal;
500 case wxCROSSDIAG_HATCH :
501 style = HatchStyleDiagonalCross;
503 case wxFDIAGONAL_HATCH :
504 style = HatchStyleForwardDiagonal;
507 style = HatchStyleCross;
509 case wxHORIZONTAL_HATCH :
510 style = HatchStyleHorizontal;
512 case wxVERTICAL_HATCH :
513 style = HatchStyleVertical;
517 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
518 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
519 m_pen->SetBrush( m_penBrush )
525 cairo_set_dash(m_context
,(double*)dashes
,num_dashes
,offset
);
527 delete[] userLengths
;
528 cairo_stroke(m_context
);
529 wxConstCast(path
, wxGraphicsPath
)->UnGetNativePath(cp
);
532 void wxCairoContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
534 if ( !m_brushTransparent
)
536 cairo_path_t
* cp
= (cairo_path_t
*) path
->GetNativePath() ;
537 cairo_append_path(m_context
,cp
);
539 if ( m_brushPattern
)
541 cairo_set_source(m_context
,m_brushPattern
);
545 cairo_set_source_rgba(m_context
,m_brush
.GetColour().Red()/255.0,
546 m_brush
.GetColour().Green()/255.0,
547 m_brush
.GetColour().Blue()/255.0,
548 m_brush
.GetColour().Alpha()/255.0);
551 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
552 cairo_fill(m_context
);
553 wxConstCast(path
, wxGraphicsPath
)->UnGetNativePath(cp
);
557 wxGraphicsPath
* wxCairoContext::CreatePath()
559 return new wxCairoPath();
562 void wxCairoContext::Rotate( wxDouble angle
)
564 cairo_rotate(m_context
,angle
);
567 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
569 cairo_translate(m_context
,dx
,dy
);
572 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
574 cairo_scale(m_context
,xScale
,yScale
);
576 PointF penWidth( m_pen->GetWidth(), 0);
578 if ( !m_penTransparent )
580 m_context->GetTransform(&matrix);
581 matrix.TransformVectors(&penWidth);
583 m_context->ScaleTransform(xScale,yScale);
584 if ( !m_penTransparent )
586 m_context->GetTransform(&matrix);
588 matrix.TransformVectors(&penWidth) ;
589 m_pen->SetWidth( sqrt( penWidth.X*penWidth.X + penWidth.Y*penWidth.Y));
594 void wxCairoContext::PushState()
596 cairo_save(m_context
);
599 void wxCairoContext::PopState()
601 cairo_restore(m_context
);
604 void wxCairoContext::SetTextColour( const wxColour
&col
)
609 void wxCairoContext::SetPen( const wxPen
&pen
)
612 m_penTransparent
= pen
.GetStyle() == wxTRANSPARENT
;
613 if ( m_penTransparent
)
620 wxBitmap* bmp = pen.GetStipple();
621 if ( bmp && bmp->Ok() )
623 wxDELETE( m_penImage );
624 wxDELETE( m_penBrush );
625 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
626 m_penBrush = new TextureBrush(m_penImage);
627 m_pen->SetBrush( m_penBrush );
633 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
635 wxDELETE( m_penBrush );
636 HatchStyle style = HatchStyleHorizontal;
637 switch( pen.GetStyle() )
639 case wxBDIAGONAL_HATCH :
640 style = HatchStyleBackwardDiagonal;
642 case wxCROSSDIAG_HATCH :
643 style = HatchStyleDiagonalCross;
645 case wxFDIAGONAL_HATCH :
646 style = HatchStyleForwardDiagonal;
649 style = HatchStyleCross;
651 case wxHORIZONTAL_HATCH :
652 style = HatchStyleHorizontal;
654 case wxVERTICAL_HATCH :
655 style = HatchStyleVertical;
659 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
660 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
661 m_pen->SetBrush( m_penBrush );
665 if ( dashStyle != DashStyleSolid )
666 m_pen->SetDashStyle(dashStyle);
670 void wxCairoContext::SetBrush( const wxBrush
&brush
)
675 cairo_pattern_destroy(m_brushPattern
);
676 m_brushPattern
= NULL
;
678 m_brushTransparent
= brush
.GetStyle() == wxTRANSPARENT
;
680 if ( m_brushTransparent
)
685 if ( brush.GetStyle() == wxSOLID)
687 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
688 brush.GetColour().Green() , brush.GetColour().Blue() ) );
690 else if ( brush.IsHatch() )
692 HatchStyle style = HatchStyleHorizontal;
693 switch( brush.GetStyle() )
695 case wxBDIAGONAL_HATCH :
696 style = HatchStyleBackwardDiagonal;
698 case wxCROSSDIAG_HATCH :
699 style = HatchStyleDiagonalCross;
701 case wxFDIAGONAL_HATCH :
702 style = HatchStyleForwardDiagonal;
705 style = HatchStyleCross;
707 case wxHORIZONTAL_HATCH :
708 style = HatchStyleHorizontal;
710 case wxVERTICAL_HATCH :
711 style = HatchStyleVertical;
715 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
716 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
720 wxBitmap* bmp = brush.GetStipple();
721 if ( bmp && bmp->Ok() )
723 wxDELETE( m_brushImage );
724 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
725 m_brush = new TextureBrush(m_brushImage);
731 void wxCairoContext::SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
733 if ( m_brushPattern
)
735 cairo_pattern_destroy(m_brushPattern
);
739 m_brushTransparent
= false;
740 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
741 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
742 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
743 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
744 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
745 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
748 void wxCairoContext::SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
749 const wxColour
&oColor
, const wxColour
&cColor
)
751 if ( m_brushPattern
)
753 cairo_pattern_destroy(m_brushPattern
);
757 m_brushTransparent
= false;
758 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
759 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
760 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
761 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
762 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
763 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
766 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
769 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
770 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
775 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
778 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
779 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
785 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
789 cairo_move_to(m_context
,x
,y
);
790 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
792 cairo_set_source_rgba(m_context
,m_textColour
.Red()/255.0,
793 m_textColour
.Green()/255.0, m_textColour
.Blue()/255.0,m_textColour
.Alpha()/255.0);
794 cairo_show_text(m_context
,buf
);
796 // TODO m_backgroundMode == wxSOLID
799 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
800 wxDouble
*descent
, wxDouble
*externalLeading
) const
803 wxWCharBuffer s = str.wc_str( *wxConvUI );
806 m_font->GetFamily(&ffamily) ;
808 REAL factorY = m_context->GetDpiY() / 72.0 ;
810 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
811 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
812 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
813 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
814 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
815 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
818 *height = rHeight * factorY + 0.5 ;
820 *descent = rDescent * factorY + 0.5 ;
821 if ( externalLeading )
822 *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ;
823 // measuring empty strings is not guaranteed, so do it by hand
831 // MeasureString does return a rectangle that is way too large, so it is
833 RectF layoutRect(0,0, 100000.0f, 100000.0f);
834 StringFormat strFormat;
835 CharacterRange strRange(0,wcslen(s));
836 strFormat.SetMeasurableCharacterRanges(1,&strRange);
838 m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,®ion) ;
840 region.GetBounds(&bbox,m_context);
842 *width = bbox.GetRight()-bbox.GetLeft()+0.5;
847 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
850 widths
.Add(0, text
.length());
855 wxWCharBuffer ws = text.wc_str( *wxConvUI );
856 size_t len = wcslen( ws ) ;
857 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
859 RectF layoutRect(0,0, 100000.0f, 100000.0f);
860 StringFormat strFormat;
862 CharacterRange* ranges = new CharacterRange[len] ;
863 Region* regions = new Region[len];
864 for( int i = 0 ; i < len ; ++i)
866 ranges[i].First = i ;
867 ranges[i].Length = 1 ;
869 strFormat.SetMeasurableCharacterRanges(len,ranges);
870 m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ;
873 for ( int i = 0 ; i < len ; ++i)
875 regions[i].GetBounds(&bbox,m_context);
876 widths[i] = bbox.GetRight()-bbox.GetLeft();
881 void wxCairoContext::SetFont( const wxFont
&font
)
883 cairo_select_font_face(m_context
,font
.GetFaceName().mb_str(wxConvUTF8
),
884 font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
,
885 font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
);
887 cairo_set_font_size(m_context
,font
.GetPointSize());
892 void * wxCairoContext::GetNativeContext()
897 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
899 return new wxCairoContext(dc
);
902 wxGraphicsContext
* wxGraphicsContext::Create( wxWindow
* window
)
907 wxGraphicsContext
* wxGraphicsContext::CreateFromNative( void * context
)
912 #endif // wxUSE_GRAPHICS_CONTEXT