1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/window.h"
28 #include "wx/dialog.h"
30 #include "wx/bitmap.h"
31 #include "wx/dcmemory.h"
34 #include "wx/dcprint.h"
35 #include "wx/module.h"
38 #include "wx/graphics.h"
40 #if wxUSE_GRAPHICS_CONTEXT
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 const double RAD2DEG
= 180.0 / M_PI
;
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 static inline double dmin(double a
, double b
)
60 static inline double dmax(double a
, double b
)
65 static inline double DegToRad(double deg
)
67 return (deg
* M_PI
) / 180.0;
69 static inline double RadToDeg(double deg
)
71 return (deg
* 180.0) / M_PI
;
74 //-----------------------------------------------------------------------------
75 // device context implementation
77 // more and more of the dc functionality should be implemented by calling
78 // the appropricate wxCairoContext, but we will have to do that step by step
79 // also coordinate conversions should be moved to native matrix ops
80 //-----------------------------------------------------------------------------
82 // we always stock two context states, one at entry, to be able to preserve the
83 // state we were called with, the other one after changing to HI Graphics orientation
84 // (this one is used for getting back clippings etc)
86 //-----------------------------------------------------------------------------
87 // wxGraphicsPath implementation
88 //-----------------------------------------------------------------------------
90 // TODO remove this dependency (gdiplus needs the macros)
93 #define max(a,b) (((a) > (b)) ? (a) : (b))
97 #define min(a,b) (((a) < (b)) ? (a) : (b))
103 class WXDLLEXPORT wxCairoPath
: public wxGraphicsPath
105 DECLARE_NO_COPY_CLASS(wxCairoPath
)
112 // These are the path primitives from which everything else can be constructed
115 // begins a new subpath at (x,y)
116 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
118 // adds a straight line from the current point to (x,y)
119 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
121 // adds a cubic Bezier curve from the current point, using two control points and an end point
122 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
125 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
126 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
128 // gets the last point of the current path, (0,0) if not yet set
129 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) ;
131 // closes the current sub-path
132 virtual void CloseSubpath();
135 // These are convenience functions which - if not available natively will be assembled
136 // using the primitives from above
141 // appends a rectangle as a new closed subpath
142 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
143 // appends an ellipsis as a new closed subpath fitting the passed rectangle
144 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
146 // 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)
147 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
150 cairo_path_t
* GetPath() const;
152 cairo_t
* m_pathContext
;
155 wxCairoPath::wxCairoPath()
157 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
158 m_pathContext
= cairo_create(surface
);
159 cairo_surface_destroy (surface
);
162 wxCairoPath::~wxCairoPath()
164 cairo_destroy(m_pathContext
);
167 cairo_path_t
* wxCairoPath::GetPath() const
169 return cairo_copy_path(m_pathContext
) ;
176 void wxCairoPath::MoveToPoint( wxDouble x
, wxDouble y
)
178 cairo_move_to(m_pathContext
,x
,y
);
181 void wxCairoPath::AddLineToPoint( wxDouble x
, wxDouble y
)
183 cairo_line_to(m_pathContext
,x
,y
);
186 void wxCairoPath::CloseSubpath()
188 cairo_close_path(m_pathContext
);
191 void wxCairoPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
193 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
196 // gets the last point of the current path, (0,0) if not yet set
197 void wxCairoPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
200 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
205 void wxCairoPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
207 // as clockwise means positive in our system (y pointing downwards)
208 // TODO make this interpretation dependent of the
210 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
211 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
213 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
217 void wxCairoPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
219 m_path->AddRectangle(RectF(x,y,w,h));
226 // closes the current subpath
227 void wxCairoPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
229 // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
234 class WXDLLEXPORT wxCairoContext
: public wxGraphicsContext
236 DECLARE_NO_COPY_CLASS(wxCairoContext
)
239 wxCairoContext( const wxWindowDC
& dc
);
241 virtual ~wxCairoContext();
243 virtual void Clip( const wxRegion
®ion
);
244 virtual void StrokePath( const wxGraphicsPath
*p
);
245 virtual void FillPath( const wxGraphicsPath
*p
, int fillStyle
= wxWINDING_RULE
);
247 virtual wxGraphicsPath
* CreatePath();
248 virtual void SetPen( const wxPen
&pen
);
249 virtual void SetBrush( const wxBrush
&brush
);
250 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
) ;
251 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
252 const wxColour
&oColor
, const wxColour
&cColor
);
254 virtual void Translate( wxDouble dx
, wxDouble dy
);
255 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
256 virtual void Rotate( wxDouble angle
);
258 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
259 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
260 virtual void PushState();
261 virtual void PopState();
263 virtual void SetFont( const wxFont
&font
);
264 virtual void SetTextColor( const wxColour
&col
);
265 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
266 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
267 wxDouble
*descent
, wxDouble
*externalLeading
) const;
268 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
272 bool m_penTransparent
;
273 bool m_brushTransparent
;
277 cairo_pattern_t
* m_brushPattern
;
278 wxColour m_textColour
;
284 //-----------------------------------------------------------------------------
285 // wxCairoContext implementation
286 //-----------------------------------------------------------------------------
288 wxCairoContext::wxCairoContext( const wxWindowDC
& dc
)
290 m_context
= gdk_cairo_create( dc
.m_window
) ;
293 m_penTransparent
= true;
294 m_brushTransparent
= true;
295 m_brushPattern
= NULL
;
298 wxCairoContext::~wxCairoContext()
304 cairo_destroy(m_context
);
305 if ( m_brushPattern
)
306 cairo_pattern_destroy(m_brushPattern
);
311 void wxCairoContext::Clip( const wxRegion
®ion
)
313 // ClipCGContextToRegion ( m_context, &bounds , (RgnHandle) dc->m_macCurrentClipRgn );
316 void wxCairoContext::StrokePath( const wxGraphicsPath
*p
)
318 if ( m_penTransparent
)
321 const wxCairoPath
* path
= dynamic_cast< const wxCairoPath
*>( p
);
322 cairo_path_t
* cp
= path
->GetPath() ;
323 cairo_append_path(m_context
,cp
);
327 // TODO: * m_dc->m_scaleX
328 double penWidth
= m_pen
.GetWidth();
332 cairo_set_line_width(m_context
,penWidth
);
333 cairo_set_source_rgba(m_context
,m_pen
.GetColour().Red()/255.0,
334 m_pen
.GetColour().Green()/255.0, m_pen
.GetColour().Blue()/255.0,m_pen
.GetColour().Alpha()/255.0);
336 cairo_line_cap_t cap
;
337 switch ( m_pen
.GetCap() )
340 cap
= CAIRO_LINE_CAP_ROUND
;
343 case wxCAP_PROJECTING
:
344 cap
= CAIRO_LINE_CAP_SQUARE
;
348 cap
= CAIRO_LINE_CAP_BUTT
;
352 cap
= CAIRO_LINE_CAP_BUTT
;
355 cairo_set_line_cap(m_context
,cap
);
357 cairo_line_join_t join
;
358 switch ( m_pen
.GetJoin() )
361 join
= CAIRO_LINE_JOIN_BEVEL
;
365 join
= CAIRO_LINE_JOIN_MITER
;
369 join
= CAIRO_LINE_JOIN_ROUND
;
373 join
= CAIRO_LINE_JOIN_MITER
;
376 cairo_set_line_join(m_context
,join
);
379 const double * dashes
= NULL
;
382 const double dashUnit
= penWidth
< 1.0 ? 1.0 : penWidth
;
384 double *userLengths
= NULL
;
385 const double dotted
[] =
387 dashUnit
, dashUnit
+ 2.0
389 const double short_dashed
[] =
393 const double dashed
[] =
397 const double dotted_dashed
[] =
399 9.0 , 6.0 , 3.0 , 3.0
402 switch ( m_pen
.GetStyle() )
409 num_dashes
= WXSIZEOF(dotted
)
415 num_dashes
= WXSIZEOF(dashed
);
420 num_dashes
= WXSIZEOF(short_dashed
);
425 num_dashes
= WXSIZEOF(dotted_dashed
);
431 num_dashes
= m_pen
.GetDashes( &wxdashes
) ;
432 if ((wxdashes
!= NULL
) && (num_dashes
> 0))
434 userLengths
= new double[num_dashes
] ;
435 for ( int i
= 0 ; i
< num_dashes
; ++i
)
437 userLengths
[i
] = wxdashes
[i
] * dashUnit
;
439 if ( i
% 2 == 1 && userLengths
[i
] < dashUnit
+ 2.0 )
440 userLengths
[i
] = dashUnit
+ 2.0 ;
441 else if ( i
% 2 == 0 && userLengths
[i
] < dashUnit
)
442 userLengths
[i
] = dashUnit
;
445 dashes
= userLengths
;
451 wxBitmap* bmp = pen.GetStipple();
452 if ( bmp && bmp->Ok() )
454 wxDELETE( m_penImage );
455 wxDELETE( m_penBrush );
456 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
457 m_penBrush = new TextureBrush(m_penImage);
458 m_pen->SetBrush( m_penBrush );
464 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
467 wxDELETE( m_penBrush );
468 HatchStyle style = HatchStyleHorizontal;
469 switch( pen.GetStyle() )
471 case wxBDIAGONAL_HATCH :
472 style = HatchStyleBackwardDiagonal;
474 case wxCROSSDIAG_HATCH :
475 style = HatchStyleDiagonalCross;
477 case wxFDIAGONAL_HATCH :
478 style = HatchStyleForwardDiagonal;
481 style = HatchStyleCross;
483 case wxHORIZONTAL_HATCH :
484 style = HatchStyleHorizontal;
486 case wxVERTICAL_HATCH :
487 style = HatchStyleVertical;
491 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
492 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
493 m_pen->SetBrush( m_penBrush )
499 cairo_set_dash(m_context
,(double*)dashes
,num_dashes
,offset
);
501 delete[] userLengths
;
502 cairo_stroke(m_context
);
503 cairo_path_destroy(cp
);
506 void wxCairoContext::FillPath( const wxGraphicsPath
*p
, int fillStyle
)
508 if ( !m_brushTransparent
)
510 const wxCairoPath
* path
= dynamic_cast< const wxCairoPath
*>( p
);
511 cairo_path_t
* cp
= path
->GetPath() ;
512 cairo_append_path(m_context
,cp
);
514 if ( m_brushPattern
)
516 cairo_set_source(m_context
,m_brushPattern
);
520 cairo_set_source_rgba(m_context
,m_brush
.GetColour().Red()/255.0,
521 m_brush
.GetColour().Green()/255.0,
522 m_brush
.GetColour().Blue()/255.0,
523 m_brush
.GetColour().Alpha()/255.0);
526 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
527 cairo_fill(m_context
);
528 cairo_path_destroy(cp
);
532 wxGraphicsPath
* wxCairoContext::CreatePath()
534 return new wxCairoPath();
537 void wxCairoContext::Rotate( wxDouble angle
)
539 cairo_rotate(m_context
,angle
);
542 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
544 cairo_translate(m_context
,dx
,dy
);
547 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
549 cairo_scale(m_context
,xScale
,yScale
);
551 PointF penWidth( m_pen->GetWidth(), 0);
553 if ( !m_penTransparent )
555 m_context->GetTransform(&matrix);
556 matrix.TransformVectors(&penWidth);
558 m_context->ScaleTransform(xScale,yScale);
559 if ( !m_penTransparent )
561 m_context->GetTransform(&matrix);
563 matrix.TransformVectors(&penWidth) ;
564 m_pen->SetWidth( sqrt( penWidth.X*penWidth.X + penWidth.Y*penWidth.Y));
569 void wxCairoContext::PushState()
571 cairo_save(m_context
);
574 void wxCairoContext::PopState()
576 cairo_restore(m_context
);
579 void wxCairoContext::SetTextColor( const wxColour
&col
)
584 void wxCairoContext::SetPen( const wxPen
&pen
)
587 m_penTransparent
= pen
.GetStyle() == wxTRANSPARENT
;
588 if ( m_penTransparent
)
595 wxBitmap* bmp = pen.GetStipple();
596 if ( bmp && bmp->Ok() )
598 wxDELETE( m_penImage );
599 wxDELETE( m_penBrush );
600 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
601 m_penBrush = new TextureBrush(m_penImage);
602 m_pen->SetBrush( m_penBrush );
608 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
610 wxDELETE( m_penBrush );
611 HatchStyle style = HatchStyleHorizontal;
612 switch( pen.GetStyle() )
614 case wxBDIAGONAL_HATCH :
615 style = HatchStyleBackwardDiagonal;
617 case wxCROSSDIAG_HATCH :
618 style = HatchStyleDiagonalCross;
620 case wxFDIAGONAL_HATCH :
621 style = HatchStyleForwardDiagonal;
624 style = HatchStyleCross;
626 case wxHORIZONTAL_HATCH :
627 style = HatchStyleHorizontal;
629 case wxVERTICAL_HATCH :
630 style = HatchStyleVertical;
634 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
635 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
636 m_pen->SetBrush( m_penBrush );
640 if ( dashStyle != DashStyleSolid )
641 m_pen->SetDashStyle(dashStyle);
645 void wxCairoContext::SetBrush( const wxBrush
&brush
)
650 cairo_pattern_destroy(m_brushPattern
);
651 m_brushPattern
= NULL
;
653 m_brushTransparent
= brush
.GetStyle() == wxTRANSPARENT
;
655 if ( m_brushTransparent
)
660 if ( brush.GetStyle() == wxSOLID)
662 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
663 brush.GetColour().Green() , brush.GetColour().Blue() ) );
665 else if ( brush.IsHatch() )
667 HatchStyle style = HatchStyleHorizontal;
668 switch( brush.GetStyle() )
670 case wxBDIAGONAL_HATCH :
671 style = HatchStyleBackwardDiagonal;
673 case wxCROSSDIAG_HATCH :
674 style = HatchStyleDiagonalCross;
676 case wxFDIAGONAL_HATCH :
677 style = HatchStyleForwardDiagonal;
680 style = HatchStyleCross;
682 case wxHORIZONTAL_HATCH :
683 style = HatchStyleHorizontal;
685 case wxVERTICAL_HATCH :
686 style = HatchStyleVertical;
690 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
691 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
695 wxBitmap* bmp = brush.GetStipple();
696 if ( bmp && bmp->Ok() )
698 wxDELETE( m_brushImage );
699 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
700 m_brush = new TextureBrush(m_brushImage);
706 void wxCairoContext::SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
708 if ( m_brushPattern
)
710 cairo_pattern_destroy(m_brushPattern
);
714 m_brushTransparent
= false;
715 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
716 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
717 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
718 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
719 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
720 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
723 void wxCairoContext::SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
724 const wxColour
&oColor
, const wxColour
&cColor
)
726 if ( m_brushPattern
)
728 cairo_pattern_destroy(m_brushPattern
);
732 m_brushTransparent
= false;
733 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
734 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
735 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
736 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
737 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
738 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
741 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
744 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
745 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
750 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
753 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
754 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
760 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
764 cairo_move_to(m_context
,x
,y
);
765 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
767 cairo_set_source_rgba(m_context
,m_textColour
.Red()/255.0,
768 m_textColour
.Green()/255.0, m_textColour
.Blue()/255.0,m_textColour
.Alpha()/255.0);
769 cairo_show_text(m_context
,buf
);
771 // TODO m_backgroundMode == wxSOLID
774 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
775 wxDouble
*descent
, wxDouble
*externalLeading
) const
778 wxWCharBuffer s = str.wc_str( *wxConvUI );
781 m_font->GetFamily(&ffamily) ;
783 REAL factorY = m_context->GetDpiY() / 72.0 ;
785 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
786 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
787 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
788 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
789 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
790 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
793 *height = rHeight * factorY + 0.5 ;
795 *descent = rDescent * factorY + 0.5 ;
796 if ( externalLeading )
797 *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ;
798 // measuring empty strings is not guaranteed, so do it by hand
806 // MeasureString does return a rectangle that is way too large, so it is
808 RectF layoutRect(0,0, 100000.0f, 100000.0f);
809 StringFormat strFormat;
810 CharacterRange strRange(0,wcslen(s));
811 strFormat.SetMeasurableCharacterRanges(1,&strRange);
813 m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,®ion) ;
815 region.GetBounds(&bbox,m_context);
817 *width = bbox.GetRight()-bbox.GetLeft()+0.5;
822 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
825 widths
.Add(0, text
.length());
830 wxWCharBuffer ws = text.wc_str( *wxConvUI );
831 size_t len = wcslen( ws ) ;
832 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
834 RectF layoutRect(0,0, 100000.0f, 100000.0f);
835 StringFormat strFormat;
837 CharacterRange* ranges = new CharacterRange[len] ;
838 Region* regions = new Region[len];
839 for( int i = 0 ; i < len ; ++i)
841 ranges[i].First = i ;
842 ranges[i].Length = 1 ;
844 strFormat.SetMeasurableCharacterRanges(len,ranges);
845 m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ;
848 for ( int i = 0 ; i < len ; ++i)
850 regions[i].GetBounds(&bbox,m_context);
851 widths[i] = bbox.GetRight()-bbox.GetLeft();
856 void wxCairoContext::SetFont( const wxFont
&font
)
858 cairo_select_font_face(m_context
,font
.GetFaceName().mb_str(wxConvUTF8
),
859 font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
,
860 font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
);
862 cairo_set_font_size(m_context
,font
.GetPointSize());
867 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
869 return new wxCairoContext(dc
);
872 #endif // wxUSE_GRAPHICS_CONTEXT