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 cairo_path_t
* GetPath() const;
149 cairo_t
* m_pathContext
;
152 wxCairoPath::wxCairoPath()
154 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
155 m_pathContext
= cairo_create(surface
);
156 cairo_surface_destroy (surface
);
159 wxCairoPath::~wxCairoPath()
161 cairo_destroy(m_pathContext
);
164 cairo_path_t
* wxCairoPath::GetPath() const
166 return cairo_copy_path(m_pathContext
) ;
173 void wxCairoPath::MoveToPoint( wxDouble x
, wxDouble y
)
175 cairo_move_to(m_pathContext
,x
,y
);
178 void wxCairoPath::AddLineToPoint( wxDouble x
, wxDouble y
)
180 cairo_line_to(m_pathContext
,x
,y
);
183 void wxCairoPath::CloseSubpath()
185 cairo_close_path(m_pathContext
);
188 void wxCairoPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
190 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
193 // gets the last point of the current path, (0,0) if not yet set
194 void wxCairoPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
197 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
202 void wxCairoPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
204 // as clockwise means positive in our system (y pointing downwards)
205 // TODO make this interpretation dependent of the
207 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
208 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
210 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
214 void wxCairoPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
216 m_path->AddRectangle(RectF(x,y,w,h));
223 // closes the current subpath
224 void wxCairoPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
226 // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
231 class WXDLLEXPORT wxCairoContext
: public wxGraphicsContext
233 DECLARE_NO_COPY_CLASS(wxCairoContext
)
236 wxCairoContext( const wxWindowDC
& dc
);
238 virtual ~wxCairoContext();
240 virtual void Clip( const wxRegion
®ion
);
241 virtual void StrokePath( const wxGraphicsPath
*p
);
242 virtual void FillPath( const wxGraphicsPath
*p
, int fillStyle
= wxWINDING_RULE
);
244 virtual wxGraphicsPath
* CreatePath();
245 virtual void SetPen( const wxPen
&pen
);
246 virtual void SetBrush( const wxBrush
&brush
);
247 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
) ;
248 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
249 const wxColour
&oColor
, const wxColour
&cColor
);
251 virtual void Translate( wxDouble dx
, wxDouble dy
);
252 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
253 virtual void Rotate( wxDouble angle
);
255 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
256 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
257 virtual void PushState();
258 virtual void PopState();
260 virtual void SetFont( const wxFont
&font
);
261 virtual void SetTextColor( const wxColour
&col
);
262 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
263 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
264 wxDouble
*descent
, wxDouble
*externalLeading
) const;
265 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
269 bool m_penTransparent
;
270 bool m_brushTransparent
;
274 cairo_pattern_t
* m_brushPattern
;
275 wxColour m_textColour
;
281 //-----------------------------------------------------------------------------
282 // wxCairoContext implementation
283 //-----------------------------------------------------------------------------
285 wxCairoContext::wxCairoContext( const wxWindowDC
& dc
)
287 m_context
= gdk_cairo_create( dc
.m_window
) ;
290 m_penTransparent
= true;
291 m_brushTransparent
= true;
292 m_brushPattern
= NULL
;
295 wxCairoContext::~wxCairoContext()
301 cairo_destroy(m_context
);
302 if ( m_brushPattern
)
303 cairo_pattern_destroy(m_brushPattern
);
308 void wxCairoContext::Clip( const wxRegion
®ion
)
310 // ClipCGContextToRegion ( m_context, &bounds , (RgnHandle) dc->m_macCurrentClipRgn );
313 void wxCairoContext::StrokePath( const wxGraphicsPath
*p
)
315 if ( m_penTransparent
)
318 const wxCairoPath
* path
= dynamic_cast< const wxCairoPath
*>( p
);
319 cairo_path_t
* cp
= path
->GetPath() ;
320 cairo_append_path(m_context
,cp
);
324 // TODO: * m_dc->m_scaleX
325 double penWidth
= m_pen
.GetWidth();
329 cairo_set_line_width(m_context
,penWidth
);
330 cairo_set_source_rgba(m_context
,m_pen
.GetColour().Red()/255.0,
331 m_pen
.GetColour().Green()/255.0, m_pen
.GetColour().Blue()/255.0,m_pen
.GetColour().Alpha()/255.0);
333 cairo_line_cap_t cap
;
334 switch ( m_pen
.GetCap() )
337 cap
= CAIRO_LINE_CAP_ROUND
;
340 case wxCAP_PROJECTING
:
341 cap
= CAIRO_LINE_CAP_SQUARE
;
345 cap
= CAIRO_LINE_CAP_BUTT
;
349 cap
= CAIRO_LINE_CAP_BUTT
;
352 cairo_set_line_cap(m_context
,cap
);
354 cairo_line_join_t join
;
355 switch ( m_pen
.GetJoin() )
358 join
= CAIRO_LINE_JOIN_BEVEL
;
362 join
= CAIRO_LINE_JOIN_MITER
;
366 join
= CAIRO_LINE_JOIN_ROUND
;
370 join
= CAIRO_LINE_JOIN_MITER
;
373 cairo_set_line_join(m_context
,join
);
376 const double * dashes
= NULL
;
379 const double dashUnit
= penWidth
< 1.0 ? 1.0 : penWidth
;
381 double *userLengths
= NULL
;
382 const double dotted
[] =
384 dashUnit
, dashUnit
+ 2.0
386 const double short_dashed
[] =
390 const double dashed
[] =
394 const double dotted_dashed
[] =
396 9.0 , 6.0 , 3.0 , 3.0
399 switch ( m_pen
.GetStyle() )
406 num_dashes
= WXSIZEOF(dotted
)
412 num_dashes
= WXSIZEOF(dashed
);
417 num_dashes
= WXSIZEOF(short_dashed
);
422 num_dashes
= WXSIZEOF(dotted_dashed
);
428 num_dashes
= m_pen
.GetDashes( &wxdashes
) ;
429 if ((wxdashes
!= NULL
) && (num_dashes
> 0))
431 userLengths
= new double[num_dashes
] ;
432 for ( int i
= 0 ; i
< num_dashes
; ++i
)
434 userLengths
[i
] = wxdashes
[i
] * dashUnit
;
436 if ( i
% 2 == 1 && userLengths
[i
] < dashUnit
+ 2.0 )
437 userLengths
[i
] = dashUnit
+ 2.0 ;
438 else if ( i
% 2 == 0 && userLengths
[i
] < dashUnit
)
439 userLengths
[i
] = dashUnit
;
442 dashes
= userLengths
;
448 wxBitmap* bmp = pen.GetStipple();
449 if ( bmp && bmp->Ok() )
451 wxDELETE( m_penImage );
452 wxDELETE( m_penBrush );
453 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
454 m_penBrush = new TextureBrush(m_penImage);
455 m_pen->SetBrush( m_penBrush );
461 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
464 wxDELETE( m_penBrush );
465 HatchStyle style = HatchStyleHorizontal;
466 switch( pen.GetStyle() )
468 case wxBDIAGONAL_HATCH :
469 style = HatchStyleBackwardDiagonal;
471 case wxCROSSDIAG_HATCH :
472 style = HatchStyleDiagonalCross;
474 case wxFDIAGONAL_HATCH :
475 style = HatchStyleForwardDiagonal;
478 style = HatchStyleCross;
480 case wxHORIZONTAL_HATCH :
481 style = HatchStyleHorizontal;
483 case wxVERTICAL_HATCH :
484 style = HatchStyleVertical;
488 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
489 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
490 m_pen->SetBrush( m_penBrush )
496 cairo_set_dash(m_context
,(double*)dashes
,num_dashes
,offset
);
498 delete[] userLengths
;
499 cairo_stroke(m_context
);
500 cairo_path_destroy(cp
);
503 void wxCairoContext::FillPath( const wxGraphicsPath
*p
, int fillStyle
)
505 if ( !m_brushTransparent
)
507 const wxCairoPath
* path
= dynamic_cast< const wxCairoPath
*>( p
);
508 cairo_path_t
* cp
= path
->GetPath() ;
509 cairo_append_path(m_context
,cp
);
511 if ( m_brushPattern
)
513 cairo_set_source(m_context
,m_brushPattern
);
517 cairo_set_source_rgba(m_context
,m_brush
.GetColour().Red()/255.0,
518 m_brush
.GetColour().Green()/255.0,
519 m_brush
.GetColour().Blue()/255.0,
520 m_brush
.GetColour().Alpha()/255.0);
523 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
524 cairo_fill(m_context
);
525 cairo_path_destroy(cp
);
529 wxGraphicsPath
* wxCairoContext::CreatePath()
531 return new wxCairoPath();
534 void wxCairoContext::Rotate( wxDouble angle
)
536 cairo_rotate(m_context
,angle
);
539 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
541 cairo_translate(m_context
,dx
,dy
);
544 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
546 cairo_scale(m_context
,xScale
,yScale
);
548 PointF penWidth( m_pen->GetWidth(), 0);
550 if ( !m_penTransparent )
552 m_context->GetTransform(&matrix);
553 matrix.TransformVectors(&penWidth);
555 m_context->ScaleTransform(xScale,yScale);
556 if ( !m_penTransparent )
558 m_context->GetTransform(&matrix);
560 matrix.TransformVectors(&penWidth) ;
561 m_pen->SetWidth( sqrt( penWidth.X*penWidth.X + penWidth.Y*penWidth.Y));
566 void wxCairoContext::PushState()
568 cairo_save(m_context
);
571 void wxCairoContext::PopState()
573 cairo_restore(m_context
);
576 void wxCairoContext::SetTextColor( const wxColour
&col
)
581 void wxCairoContext::SetPen( const wxPen
&pen
)
584 m_penTransparent
= pen
.GetStyle() == wxTRANSPARENT
;
585 if ( m_penTransparent
)
592 wxBitmap* bmp = pen.GetStipple();
593 if ( bmp && bmp->Ok() )
595 wxDELETE( m_penImage );
596 wxDELETE( m_penBrush );
597 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
598 m_penBrush = new TextureBrush(m_penImage);
599 m_pen->SetBrush( m_penBrush );
605 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
607 wxDELETE( m_penBrush );
608 HatchStyle style = HatchStyleHorizontal;
609 switch( pen.GetStyle() )
611 case wxBDIAGONAL_HATCH :
612 style = HatchStyleBackwardDiagonal;
614 case wxCROSSDIAG_HATCH :
615 style = HatchStyleDiagonalCross;
617 case wxFDIAGONAL_HATCH :
618 style = HatchStyleForwardDiagonal;
621 style = HatchStyleCross;
623 case wxHORIZONTAL_HATCH :
624 style = HatchStyleHorizontal;
626 case wxVERTICAL_HATCH :
627 style = HatchStyleVertical;
631 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
632 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
633 m_pen->SetBrush( m_penBrush );
637 if ( dashStyle != DashStyleSolid )
638 m_pen->SetDashStyle(dashStyle);
642 void wxCairoContext::SetBrush( const wxBrush
&brush
)
647 cairo_pattern_destroy(m_brushPattern
);
648 m_brushPattern
= NULL
;
650 m_brushTransparent
= brush
.GetStyle() == wxTRANSPARENT
;
652 if ( m_brushTransparent
)
657 if ( brush.GetStyle() == wxSOLID)
659 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
660 brush.GetColour().Green() , brush.GetColour().Blue() ) );
662 else if ( brush.IsHatch() )
664 HatchStyle style = HatchStyleHorizontal;
665 switch( brush.GetStyle() )
667 case wxBDIAGONAL_HATCH :
668 style = HatchStyleBackwardDiagonal;
670 case wxCROSSDIAG_HATCH :
671 style = HatchStyleDiagonalCross;
673 case wxFDIAGONAL_HATCH :
674 style = HatchStyleForwardDiagonal;
677 style = HatchStyleCross;
679 case wxHORIZONTAL_HATCH :
680 style = HatchStyleHorizontal;
682 case wxVERTICAL_HATCH :
683 style = HatchStyleVertical;
687 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
688 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
692 wxBitmap* bmp = brush.GetStipple();
693 if ( bmp && bmp->Ok() )
695 wxDELETE( m_brushImage );
696 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
697 m_brush = new TextureBrush(m_brushImage);
703 void wxCairoContext::SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
705 if ( m_brushPattern
)
707 cairo_pattern_destroy(m_brushPattern
);
711 m_brushTransparent
= false;
712 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
713 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
714 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
715 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
716 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
717 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
720 void wxCairoContext::SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
721 const wxColour
&oColor
, const wxColour
&cColor
)
723 if ( m_brushPattern
)
725 cairo_pattern_destroy(m_brushPattern
);
729 m_brushTransparent
= false;
730 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
731 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
732 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
733 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
734 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
735 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
738 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
741 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
742 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
747 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
750 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
751 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
757 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
761 cairo_move_to(m_context
,x
,y
);
762 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
764 cairo_set_source_rgba(m_context
,m_textColour
.Red()/255.0,
765 m_textColour
.Green()/255.0, m_textColour
.Blue()/255.0,m_textColour
.Alpha()/255.0);
766 cairo_show_text(m_context
,buf
);
768 // TODO m_backgroundMode == wxSOLID
771 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
772 wxDouble
*descent
, wxDouble
*externalLeading
) const
775 wxWCharBuffer s = str.wc_str( *wxConvUI );
778 m_font->GetFamily(&ffamily) ;
780 REAL factorY = m_context->GetDpiY() / 72.0 ;
782 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
783 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
784 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
785 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
786 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
787 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
790 *height = rHeight * factorY + 0.5 ;
792 *descent = rDescent * factorY + 0.5 ;
793 if ( externalLeading )
794 *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ;
795 // measuring empty strings is not guaranteed, so do it by hand
803 // MeasureString does return a rectangle that is way too large, so it is
805 RectF layoutRect(0,0, 100000.0f, 100000.0f);
806 StringFormat strFormat;
807 CharacterRange strRange(0,wcslen(s));
808 strFormat.SetMeasurableCharacterRanges(1,&strRange);
810 m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,®ion) ;
812 region.GetBounds(&bbox,m_context);
814 *width = bbox.GetRight()-bbox.GetLeft()+0.5;
819 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
822 widths
.Add(0, text
.length());
827 wxWCharBuffer ws = text.wc_str( *wxConvUI );
828 size_t len = wcslen( ws ) ;
829 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
831 RectF layoutRect(0,0, 100000.0f, 100000.0f);
832 StringFormat strFormat;
834 CharacterRange* ranges = new CharacterRange[len] ;
835 Region* regions = new Region[len];
836 for( int i = 0 ; i < len ; ++i)
838 ranges[i].First = i ;
839 ranges[i].Length = 1 ;
841 strFormat.SetMeasurableCharacterRanges(len,ranges);
842 m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ;
845 for ( int i = 0 ; i < len ; ++i)
847 regions[i].GetBounds(&bbox,m_context);
848 widths[i] = bbox.GetRight()-bbox.GetLeft();
853 void wxCairoContext::SetFont( const wxFont
&font
)
855 cairo_select_font_face(m_context
,font
.GetFaceName().mb_str(wxConvUTF8
),
856 font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
,
857 font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
);
859 cairo_set_font_size(m_context
,font
.GetPointSize());
864 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
866 return new wxCairoContext(dc
);
869 #endif // wxUSE_GRAPHICS_CONTEXT