]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/graphics.cpp
57e63acbbd6bf9cf216f23ca6a382dab23bdf7df
[wxWidgets.git] / src / mac / carbon / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dccg.cpp
3 // Purpose: wxDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_GRAPHICS_CONTEXT && wxMAC_USE_CORE_GRAPHICS
15
16 #include "wx/graphics.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/dcclient.h"
20 #include "wx/log.h"
21 #include "wx/region.h"
22 #endif
23
24 #include "wx/mac/uma.h"
25
26 #ifdef __MSL__
27 #if __MSL__ >= 0x6000
28 #include "math.h"
29 // in case our functions were defined outside std, we make it known all the same
30 namespace std { }
31 using namespace std;
32 #endif
33 #endif
34
35 #include "wx/mac/private.h"
36
37 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
38 typedef float CGFloat;
39 #endif
40
41 //-----------------------------------------------------------------------------
42 // constants
43 //-----------------------------------------------------------------------------
44
45 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
46 #ifndef M_PI
47 const double M_PI = 3.14159265358979;
48 #endif
49 #endif
50
51 static const double RAD2DEG = 180.0 / M_PI;
52
53 //
54 // Graphics Path
55 //
56
57 class WXDLLEXPORT wxMacCoreGraphicsPath : public wxGraphicsPath
58 {
59 public :
60 wxMacCoreGraphicsPath();
61 ~wxMacCoreGraphicsPath();
62
63 // begins a new subpath at (x,y)
64 virtual void MoveToPoint( wxDouble x, wxDouble y );
65
66 // adds a straight line from the current point to (x,y)
67 virtual void AddLineToPoint( wxDouble x, wxDouble y );
68
69 // adds a cubic Bezier curve from the current point, using two control points and an end point
70 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
71
72 // closes the current sub-path
73 virtual void CloseSubpath();
74
75 // gets the last point of the current path, (0,0) if not yet set
76 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y);
77
78 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
79 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise );
80
81 //
82 // These are convenience functions which - if not available natively will be assembled
83 // using the primitives from above
84 //
85
86 // adds a quadratic Bezier curve from the current point, using a control point and an end point
87 virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y );
88
89 // appends a rectangle as a new closed subpath
90 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
91
92 // appends an ellipsis as a new closed subpath fitting the passed rectangle
93 virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r );
94
95 // 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)
96 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r );
97
98 // returns the native path
99 virtual void * GetNativePath() const { return m_path; }
100
101 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
102 virtual void UnGetNativePath(void *p) {}
103
104 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsPath)
105 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsPath)
106 private :
107 CGMutablePathRef m_path;
108 };
109
110 //-----------------------------------------------------------------------------
111 // wxGraphicsPath implementation
112 //-----------------------------------------------------------------------------
113
114 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsPath, wxGraphicsPath)
115
116 wxMacCoreGraphicsPath::wxMacCoreGraphicsPath()
117 {
118 m_path = CGPathCreateMutable();
119 }
120
121 wxMacCoreGraphicsPath::~wxMacCoreGraphicsPath()
122 {
123 CGPathRelease( m_path );
124 }
125
126 // opens (starts) a new subpath
127 void wxMacCoreGraphicsPath::MoveToPoint( wxDouble x1 , wxDouble y1 )
128 {
129 CGPathMoveToPoint( m_path , NULL , x1 , y1 );
130 }
131
132 void wxMacCoreGraphicsPath::AddLineToPoint( wxDouble x1 , wxDouble y1 )
133 {
134 CGPathAddLineToPoint( m_path , NULL , x1 , y1 );
135 }
136
137 void wxMacCoreGraphicsPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
138 {
139 CGPathAddCurveToPoint( m_path , NULL , cx1 , cy1 , cx2, cy2, x , y );
140 }
141
142 void wxMacCoreGraphicsPath::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y )
143 {
144 CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x , y );
145 }
146
147 void wxMacCoreGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
148 {
149 CGRect cgRect = { { x , y } , { w , h } };
150 CGPathAddRect( m_path , NULL , cgRect );
151 }
152
153 void wxMacCoreGraphicsPath::AddCircle( wxDouble x, wxDouble y , wxDouble r )
154 {
155 CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true );
156 }
157
158 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
159 void wxMacCoreGraphicsPath::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise )
160 {
161 // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup
162 CGPathAddArc( m_path, NULL , x, y, r, startAngle, endAngle, !clockwise);
163 }
164
165 void wxMacCoreGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
166 {
167 CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
168 }
169
170 // closes the current subpath
171 void wxMacCoreGraphicsPath::CloseSubpath()
172 {
173 CGPathCloseSubpath( m_path );
174 }
175
176 // gets the last point of the current path, (0,0) if not yet set
177 void wxMacCoreGraphicsPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
178 {
179 CGPoint p = CGPathGetCurrentPoint( m_path );
180 x = p.x;
181 y = p.y;
182 }
183
184 //
185 // Graphics Context
186 //
187
188 class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext
189 {
190 public:
191 wxMacCoreGraphicsContext( CGContextRef cgcontext );
192
193 wxMacCoreGraphicsContext( WindowRef window );
194
195 wxMacCoreGraphicsContext( wxWindow* window );
196
197 wxMacCoreGraphicsContext();
198
199 ~wxMacCoreGraphicsContext();
200
201 void Init();
202
203 // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ...
204 virtual wxGraphicsPath * CreatePath();
205
206 // push the current state of the context, ie the transformation matrix on a stack
207 virtual void PushState();
208
209 // pops a stored state from the stack
210 virtual void PopState();
211
212 // clips drawings to the region
213 virtual void Clip( const wxRegion &region );
214
215 // clips drawings to the rect
216 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
217
218 // resets the clipping to original extent
219 virtual void ResetClip();
220
221 virtual void * GetNativeContext();
222
223 //
224 // transformation
225 //
226
227 // translate
228 virtual void Translate( wxDouble dx , wxDouble dy );
229
230 // scale
231 virtual void Scale( wxDouble xScale , wxDouble yScale );
232
233 // rotate (radians)
234 virtual void Rotate( wxDouble angle );
235
236 //
237 // setting the paint
238 //
239
240 // sets the pan
241 virtual void SetPen( const wxPen &pen );
242
243 // sets the brush for filling
244 virtual void SetBrush( const wxBrush &brush );
245
246 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
247 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
248 const wxColour&c1, const wxColour&c2);
249
250 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
251 // with radius r and color cColor
252 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
253 const wxColour &oColor, const wxColour &cColor);
254
255 // sets the font
256 virtual void SetFont( const wxFont &font );
257
258 // sets the text color
259 virtual void SetTextColor( const wxColour &col );
260
261 // strokes along a path with the current pen
262 virtual void StrokePath( const wxGraphicsPath *path );
263
264 // fills a path with the current brush
265 virtual void FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
266
267 // draws a path by first filling and then stroking
268 virtual void DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE );
269
270 //
271 // text
272 //
273
274 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y );
275
276 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle );
277
278 virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height,
279 wxDouble *descent, wxDouble *externalLeading ) const;
280
281 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
282
283 //
284 // image support
285 //
286
287 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
288
289 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
290
291 void SetNativeContext( CGContextRef cg );
292 CGPathDrawingMode GetDrawingMode() const { return m_mode; }
293
294
295 virtual bool ShouldOffset() const
296 {
297 int penwidth = m_pen.GetWidth();
298 if ( penwidth == 0 )
299 penwidth = 1;
300 if ( m_pen.GetStyle() == wxTRANSPARENT )
301 penwidth = 0;
302 return ( penwidth % 2 ) == 1;
303 }
304
305
306 DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext)
307 DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext)
308
309 private:
310 void EnsureIsValid();
311
312 CGContextRef m_cgContext;
313 WindowRef m_windowRef;
314 int m_originX;
315 int m_originY;
316 wxMacCFRefHolder<HIShapeRef> m_clipRgn;
317 bool m_releaseContext;
318 CGPathDrawingMode m_mode;
319 ATSUStyle m_macATSUIStyle;
320 wxPen m_pen;
321 wxBrush m_brush;
322 wxFont m_font;
323 wxColor m_textForegroundColor;
324 };
325
326 //-----------------------------------------------------------------------------
327 // device context implementation
328 //
329 // more and more of the dc functionality should be implemented by calling
330 // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step
331 // also coordinate conversions should be moved to native matrix ops
332 //-----------------------------------------------------------------------------
333
334 // we always stock two context states, one at entry, to be able to preserve the
335 // state we were called with, the other one after changing to HI Graphics orientation
336 // (this one is used for getting back clippings etc)
337
338 //-----------------------------------------------------------------------------
339 // wxGraphicsContext implementation
340 //-----------------------------------------------------------------------------
341
342 IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext)
343
344 void wxMacCoreGraphicsContext::Init()
345 {
346 m_cgContext = NULL;
347 m_mode = kCGPathFill;
348 m_macATSUIStyle = NULL;
349 m_releaseContext = false;
350 HIRect r = CGRectMake(0,0,0,0);
351 m_clipRgn.Set(HIShapeCreateWithRect(&r));
352 }
353
354 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( CGContextRef cgcontext )
355 {
356 Init();
357 m_cgContext = cgcontext;
358 CGContextSaveGState( m_cgContext );
359 CGContextSaveGState( m_cgContext );
360 }
361
362 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( WindowRef window )
363 {
364 Init();
365 m_windowRef = window;
366 }
367
368 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxWindow* window )
369 {
370 Init();
371 m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef();
372 m_originX = m_originY = 0;
373 window->MacWindowToRootWindow( &m_originX , &m_originY );
374 }
375
376 wxMacCoreGraphicsContext::wxMacCoreGraphicsContext()
377 {
378 Init();
379 }
380
381 wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext()
382 {
383 if ( m_cgContext )
384 {
385 // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext );
386 CGContextRestoreGState( m_cgContext );
387 CGContextRestoreGState( m_cgContext );
388 }
389
390 if ( m_releaseContext )
391 QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext);
392 }
393
394 void wxMacCoreGraphicsContext::EnsureIsValid()
395 {
396 if ( !m_cgContext )
397 {
398 OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext );
399 wxASSERT_MSG( status == noErr , wxT("Cannot nest wxDCs on the same window") );
400 Rect bounds;
401 GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds );
402 CGContextSaveGState( m_cgContext );
403 CGContextTranslateCTM( m_cgContext , 0 , bounds.bottom - bounds.top );
404 CGContextScaleCTM( m_cgContext , 1 , -1 );
405 CGContextTranslateCTM( m_cgContext, m_originX, m_originY );
406 CGContextSaveGState( m_cgContext );
407 m_releaseContext = true;
408 if ( !HIShapeIsEmpty(m_clipRgn) )
409 {
410 HIShapeReplacePathInCGContext( m_clipRgn, m_cgContext );
411 CGContextClip( m_cgContext );
412 }
413 }
414 }
415
416
417 void wxMacCoreGraphicsContext::Clip( const wxRegion &region )
418 {
419 if( m_cgContext )
420 {
421 HIShapeRef shape = HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() );
422 HIShapeReplacePathInCGContext( shape, m_cgContext );
423 CGContextClip( m_cgContext );
424 CFRelease( shape );
425 }
426 else
427 {
428 m_clipRgn.Set(HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() ));
429 }
430 }
431
432 // clips drawings to the rect
433 void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
434 {
435 HIRect r = CGRectMake( x , y , w , h );
436 if ( m_cgContext )
437 {
438 CGContextClipToRect( m_cgContext, r );
439 }
440 else
441 {
442 m_clipRgn.Set(HIShapeCreateWithRect(&r));
443 }
444 }
445
446 // resets the clipping to original extent
447 void wxMacCoreGraphicsContext::ResetClip()
448 {
449 if ( m_cgContext )
450 {
451 CGContextRestoreGState( m_cgContext );
452 CGContextSaveGState( m_cgContext );
453 }
454 else
455 {
456 HIRect r = CGRectMake(0,0,0,0);
457 m_clipRgn.Set(HIShapeCreateWithRect(&r));
458 }
459 }
460
461 void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath *path )
462 {
463 EnsureIsValid();
464
465 bool offset = ShouldOffset();
466
467 if ( offset )
468 CGContextTranslateCTM( m_cgContext, 0.5, 0.5 );
469
470 CGContextAddPath( m_cgContext , (CGPathRef) path->GetNativePath() );
471 CGContextStrokePath( m_cgContext );
472
473 if ( offset )
474 CGContextTranslateCTM( m_cgContext, -0.5, -0.5 );
475 }
476
477 void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath *path , int fillStyle )
478 {
479 EnsureIsValid();
480
481 CGPathDrawingMode mode = m_mode;
482
483 if ( fillStyle == wxODDEVEN_RULE )
484 {
485 if ( mode == kCGPathFill )
486 mode = kCGPathEOFill;
487 else if ( mode == kCGPathFillStroke )
488 mode = kCGPathEOFillStroke;
489 }
490
491 bool offset = ShouldOffset();
492
493 if ( offset )
494 CGContextTranslateCTM( m_cgContext, 0.5, 0.5 );
495
496 CGContextAddPath( m_cgContext , (CGPathRef) path->GetNativePath() );
497 CGContextDrawPath( m_cgContext , mode );
498
499 if ( offset )
500 CGContextTranslateCTM( m_cgContext, -0.5, -0.5 );
501 }
502
503 void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath *path , int fillStyle )
504 {
505 EnsureIsValid();
506
507 CGContextAddPath( m_cgContext , (CGPathRef) path->GetNativePath() );
508 if ( fillStyle == wxODDEVEN_RULE )
509 CGContextEOFillPath( m_cgContext );
510 else
511 CGContextFillPath( m_cgContext );
512 }
513
514 wxGraphicsPath* wxMacCoreGraphicsContext::CreatePath()
515 {
516 return new wxMacCoreGraphicsPath();
517 }
518
519 void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg )
520 {
521 // we allow either setting or clearing but not replacing
522 wxASSERT( m_cgContext == NULL || cg == NULL );
523
524 if ( cg )
525 CGContextSaveGState( cg );
526 m_cgContext = cg;
527 }
528
529 void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy )
530 {
531 EnsureIsValid();
532
533 CGContextTranslateCTM( m_cgContext, dx, dy );
534 }
535
536 void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale )
537 {
538 EnsureIsValid();
539
540 CGContextScaleCTM( m_cgContext , xScale , yScale );
541 }
542
543 void wxMacCoreGraphicsContext::Rotate( wxDouble angle )
544 {
545 EnsureIsValid();
546
547 CGContextRotateCTM( m_cgContext , angle );
548 }
549
550 void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
551 {
552 EnsureIsValid();
553
554 CGImageRef image = (CGImageRef)( bmp.CGImageCreate() );
555 HIRect r = CGRectMake( x , y , w , h );
556 HIViewDrawCGImage( m_cgContext , &r , image );
557 CGImageRelease( image );
558 }
559
560 void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
561 {
562 EnsureIsValid();
563
564 CGRect r = CGRectMake( 00 , 00 , w , h );
565 CGContextSaveGState( m_cgContext );
566 CGContextTranslateCTM( m_cgContext, x , y + h );
567 CGContextScaleCTM( m_cgContext, 1, -1 );
568 PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone ,
569 NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) );
570 CGContextRestoreGState( m_cgContext );
571 }
572
573 void wxMacCoreGraphicsContext::PushState()
574 {
575 EnsureIsValid();
576
577 CGContextSaveGState( m_cgContext );
578 }
579
580 void wxMacCoreGraphicsContext::PopState()
581 {
582 EnsureIsValid();
583
584 CGContextRestoreGState( m_cgContext );
585 }
586
587 void wxMacCoreGraphicsContext::SetTextColor( const wxColour &col )
588 {
589 m_textForegroundColor = col;
590 // to recreate the native font after color change
591 SetFont( m_font );
592 }
593
594 #pragma mark -
595 #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes
596
597 // CGPattern wrapper class: always allocate on heap, never call destructor
598
599 class wxMacCoreGraphicsPattern
600 {
601 public :
602 wxMacCoreGraphicsPattern() {}
603
604 // is guaranteed to be called only with a non-Null CGContextRef
605 virtual void Render( CGContextRef ctxRef ) = 0;
606
607 operator CGPatternRef() const { return m_patternRef; }
608
609 protected :
610 virtual ~wxMacCoreGraphicsPattern()
611 {
612 // as this is called only when the m_patternRef is been released;
613 // don't release it again
614 }
615
616 static void _Render( void *info, CGContextRef ctxRef )
617 {
618 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
619 if ( self && ctxRef )
620 self->Render( ctxRef );
621 }
622
623 static void _Dispose( void *info )
624 {
625 wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info;
626 delete self;
627 }
628
629 CGPatternRef m_patternRef;
630
631 static const CGPatternCallbacks ms_Callbacks;
632 };
633
634 const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose };
635
636 class ImagePattern : public wxMacCoreGraphicsPattern
637 {
638 public :
639 ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform )
640 {
641 wxASSERT( bmp && bmp->Ok() );
642
643 Init( (CGImageRef) bmp->CGImageCreate() , transform );
644 }
645
646 // ImagePattern takes ownership of CGImageRef passed in
647 ImagePattern( CGImageRef image , const CGAffineTransform& transform )
648 {
649 if ( image )
650 CFRetain( image );
651
652 Init( image , transform );
653 }
654
655 virtual void Render( CGContextRef ctxRef )
656 {
657 if (m_image != NULL)
658 HIViewDrawCGImage( ctxRef, &m_imageBounds, m_image );
659 }
660
661 protected :
662 void Init( CGImageRef image, const CGAffineTransform& transform )
663 {
664 m_image = image;
665 if ( m_image )
666 {
667 m_imageBounds = CGRectMake( 0.0, 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) );
668 m_patternRef = CGPatternCreate(
669 this , m_imageBounds, transform ,
670 m_imageBounds.size.width, m_imageBounds.size.height,
671 kCGPatternTilingNoDistortion, true , &wxMacCoreGraphicsPattern::ms_Callbacks );
672 }
673 }
674
675 virtual ~ImagePattern()
676 {
677 if ( m_image )
678 CGImageRelease( m_image );
679 }
680
681 CGImageRef m_image;
682 CGRect m_imageBounds;
683 };
684
685 class HatchPattern : public wxMacCoreGraphicsPattern
686 {
687 public :
688 HatchPattern( int hatchstyle, const CGAffineTransform& transform )
689 {
690 m_hatch = hatchstyle;
691 m_imageBounds = CGRectMake( 0.0, 0.0, 8.0 , 8.0 );
692 m_patternRef = CGPatternCreate(
693 this , m_imageBounds, transform ,
694 m_imageBounds.size.width, m_imageBounds.size.height,
695 kCGPatternTilingNoDistortion, false , &wxMacCoreGraphicsPattern::ms_Callbacks );
696 }
697
698 void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
699 {
700 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
701 if ( UMAGetSystemVersion() >= 0x1040 )
702 {
703 CGContextStrokeLineSegments( ctxRef , pts , count );
704 }
705 else
706 #endif
707 {
708 CGContextBeginPath( ctxRef );
709 for (size_t i = 0; i < count; i += 2)
710 {
711 CGContextMoveToPoint(ctxRef, pts[i].x, pts[i].y);
712 CGContextAddLineToPoint(ctxRef, pts[i+1].x, pts[i+1].y);
713 }
714 CGContextStrokePath(ctxRef);
715 }
716 }
717
718 virtual void Render( CGContextRef ctxRef )
719 {
720 switch ( m_hatch )
721 {
722 case wxBDIAGONAL_HATCH :
723 {
724 CGPoint pts[] =
725 {
726 { 8.0 , 0.0 } , { 0.0 , 8.0 }
727 };
728 StrokeLineSegments( ctxRef , pts , 2 );
729 }
730 break;
731
732 case wxCROSSDIAG_HATCH :
733 {
734 CGPoint pts[] =
735 {
736 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
737 { 8.0 , 0.0 } , { 0.0 , 8.0 }
738 };
739 StrokeLineSegments( ctxRef , pts , 4 );
740 }
741 break;
742
743 case wxFDIAGONAL_HATCH :
744 {
745 CGPoint pts[] =
746 {
747 { 0.0 , 0.0 } , { 8.0 , 8.0 }
748 };
749 StrokeLineSegments( ctxRef , pts , 2 );
750 }
751 break;
752
753 case wxCROSS_HATCH :
754 {
755 CGPoint pts[] =
756 {
757 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
758 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
759 };
760 StrokeLineSegments( ctxRef , pts , 4 );
761 }
762 break;
763
764 case wxHORIZONTAL_HATCH :
765 {
766 CGPoint pts[] =
767 {
768 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
769 };
770 StrokeLineSegments( ctxRef , pts , 2 );
771 }
772 break;
773
774 case wxVERTICAL_HATCH :
775 {
776 CGPoint pts[] =
777 {
778 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
779 };
780 StrokeLineSegments( ctxRef , pts , 2 );
781 }
782 break;
783
784 default:
785 break;
786 }
787 }
788
789 protected :
790 virtual ~HatchPattern() {}
791
792 CGRect m_imageBounds;
793 int m_hatch;
794 };
795
796 #pragma mark -
797
798 void wxMacCoreGraphicsContext::SetPen( const wxPen &pen )
799 {
800 m_pen = pen;
801 if ( m_cgContext == NULL )
802 return;
803
804 bool fill = m_brush.GetStyle() != wxTRANSPARENT;
805 bool stroke = pen.GetStyle() != wxTRANSPARENT;
806
807 #if 0
808 // we can benchmark performance; should go into a setting eventually
809 CGContextSetShouldAntialias( m_cgContext , false );
810 #endif
811
812 if ( fill || stroke )
813 {
814 // set up brushes
815 m_mode = kCGPathFill; // just a default
816
817 if ( stroke )
818 {
819 CGContextSetRGBStrokeColor( m_cgContext , pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 ,
820 pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 );
821
822 // TODO: * m_dc->m_scaleX
823 CGFloat penWidth = pen.GetWidth();
824 if (penWidth <= 0.0)
825 penWidth = 0.1;
826 CGContextSetLineWidth( m_cgContext , penWidth );
827
828 CGLineCap cap;
829 switch ( pen.GetCap() )
830 {
831 case wxCAP_ROUND :
832 cap = kCGLineCapRound;
833 break;
834
835 case wxCAP_PROJECTING :
836 cap = kCGLineCapSquare;
837 break;
838
839 case wxCAP_BUTT :
840 cap = kCGLineCapButt;
841 break;
842
843 default :
844 cap = kCGLineCapButt;
845 break;
846 }
847
848 CGLineJoin join;
849 switch ( pen.GetJoin() )
850 {
851 case wxJOIN_BEVEL :
852 join = kCGLineJoinBevel;
853 break;
854
855 case wxJOIN_MITER :
856 join = kCGLineJoinMiter;
857 break;
858
859 case wxJOIN_ROUND :
860 join = kCGLineJoinRound;
861 break;
862
863 default :
864 join = kCGLineJoinMiter;
865 break;
866 }
867
868 m_mode = kCGPathStroke;
869 int count = 0;
870
871 const CGFloat *lengths = NULL;
872 CGFloat *userLengths = NULL;
873
874 const CGFloat dashUnit = penWidth < 1.0 ? 1.0 : penWidth;
875
876 const CGFloat dotted[] = { dashUnit , dashUnit + 2.0 };
877 const CGFloat short_dashed[] = { 9.0 , 6.0 };
878 const CGFloat dashed[] = { 19.0 , 9.0 };
879 const CGFloat dotted_dashed[] = { 9.0 , 6.0 , 3.0 , 3.0 };
880
881 switch ( pen.GetStyle() )
882 {
883 case wxSOLID :
884 break;
885
886 case wxDOT :
887 lengths = dotted;
888 count = WXSIZEOF(dotted);
889 break;
890
891 case wxLONG_DASH :
892 lengths = dashed;
893 count = WXSIZEOF(dashed);
894 break;
895
896 case wxSHORT_DASH :
897 lengths = short_dashed;
898 count = WXSIZEOF(short_dashed);
899 break;
900
901 case wxDOT_DASH :
902 lengths = dotted_dashed;
903 count = WXSIZEOF(dotted_dashed);
904 break;
905
906 case wxUSER_DASH :
907 wxDash *dashes;
908 count = pen.GetDashes( &dashes );
909 if ((dashes != NULL) && (count > 0))
910 {
911 userLengths = new CGFloat[count];
912 for ( int i = 0; i < count; ++i )
913 {
914 userLengths[i] = dashes[i] * dashUnit;
915
916 if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 )
917 userLengths[i] = dashUnit + 2.0;
918 else if ( i % 2 == 0 && userLengths[i] < dashUnit )
919 userLengths[i] = dashUnit;
920 }
921 }
922 lengths = userLengths;
923 break;
924
925 case wxSTIPPLE :
926 {
927 CGFloat alphaArray[1] = { 1.0 };
928 wxBitmap* bmp = pen.GetStipple();
929 if ( bmp && bmp->Ok() )
930 {
931 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) );
932 CGContextSetStrokeColorSpace( m_cgContext , patternSpace );
933 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
934 CGContextSetStrokePattern( m_cgContext, pattern , alphaArray );
935 }
936 }
937 break;
938
939 default :
940 {
941 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
942 CGContextSetStrokeColorSpace( m_cgContext , patternSpace );
943 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( pen.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
944
945 CGFloat colorArray[4] = { pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 ,
946 pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 };
947
948 CGContextSetStrokePattern( m_cgContext, pattern , colorArray );
949 }
950 break;
951 }
952
953 if ((lengths != NULL) && (count > 0))
954 {
955 CGContextSetLineDash( m_cgContext , 0 , lengths , count );
956 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
957 cap = kCGLineCapButt;
958 }
959 else
960 {
961 CGContextSetLineDash( m_cgContext , 0 , NULL , 0 );
962 }
963
964 CGContextSetLineCap( m_cgContext , cap );
965 CGContextSetLineJoin( m_cgContext , join );
966
967 delete[] userLengths;
968 }
969
970 if ( fill && stroke )
971 m_mode = kCGPathFillStroke;
972 }
973 }
974
975 void wxMacCoreGraphicsContext::SetBrush( const wxBrush &brush )
976 {
977 m_brush = brush;
978 if ( m_cgContext == NULL )
979 return;
980
981 bool fill = brush.GetStyle() != wxTRANSPARENT;
982 bool stroke = m_pen.GetStyle() != wxTRANSPARENT;
983
984 #if 0
985 // we can benchmark performance, should go into a setting later
986 CGContextSetShouldAntialias( m_cgContext , false );
987 #endif
988
989 if ( fill || stroke )
990 {
991 // setup brushes
992 m_mode = kCGPathFill; // just a default
993
994 if ( fill )
995 {
996 if ( brush.GetStyle() == wxSOLID )
997 {
998 CGContextSetRGBFillColor( m_cgContext , brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
999 brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 );
1000 }
1001 else if ( brush.IsHatch() )
1002 {
1003 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) );
1004 CGContextSetFillColorSpace( m_cgContext , patternSpace );
1005 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( brush.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
1006
1007 CGFloat colorArray[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 ,
1008 brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 };
1009
1010 CGContextSetFillPattern( m_cgContext, pattern , colorArray );
1011 }
1012 else
1013 {
1014 // now brush is a bitmap
1015 CGFloat alphaArray[1] = { 1.0 };
1016 wxBitmap* bmp = brush.GetStipple();
1017 if ( bmp && bmp->Ok() )
1018 {
1019 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) );
1020 CGContextSetFillColorSpace( m_cgContext , patternSpace );
1021 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
1022 CGContextSetFillPattern( m_cgContext, pattern , alphaArray );
1023 }
1024 }
1025
1026 m_mode = kCGPathFill;
1027 }
1028
1029 if ( fill && stroke )
1030 m_mode = kCGPathFillStroke;
1031 else if ( stroke )
1032 m_mode = kCGPathStroke;
1033 }
1034 }
1035
1036 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1037 void wxMacCoreGraphicsContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1038 const wxColour&c1, const wxColour&c2)
1039 {
1040 }
1041
1042 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1043 // with radius r and color cColor
1044 void wxMacCoreGraphicsContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1045 const wxColour &oColor, const wxColour &cColor)
1046 {
1047 }
1048
1049
1050 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1051 {
1052 DrawText(str, x, y, 0.0);
1053 }
1054
1055 void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle )
1056 {
1057 EnsureIsValid();
1058
1059 OSStatus status = noErr;
1060 ATSUTextLayout atsuLayout;
1061 UniCharCount chars = str.length();
1062 UniChar* ubuf = NULL;
1063
1064 #if SIZEOF_WCHAR_T == 4
1065 wxMBConvUTF16 converter;
1066 #if wxUSE_UNICODE
1067 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
1068 ubuf = (UniChar*) malloc( unicharlen + 2 );
1069 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
1070 #else
1071 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1072 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1073 ubuf = (UniChar*) malloc( unicharlen + 2 );
1074 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1075 #endif
1076 chars = unicharlen / 2;
1077 #else
1078 #if wxUSE_UNICODE
1079 ubuf = (UniChar*) str.wc_str();
1080 #else
1081 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1082 chars = wxWcslen( wchar.data() );
1083 ubuf = (UniChar*) wchar.data();
1084 #endif
1085 #endif
1086
1087 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1088 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout );
1089
1090 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
1091
1092 status = ::ATSUSetTransientFontMatching( atsuLayout , true );
1093 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
1094
1095 int iAngle = int( angle * RAD2DEG );
1096 if ( abs(iAngle) > 0 )
1097 {
1098 Fixed atsuAngle = IntToFixed( iAngle );
1099 ATSUAttributeTag atsuTags[] =
1100 {
1101 kATSULineRotationTag ,
1102 };
1103 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1104 {
1105 sizeof( Fixed ) ,
1106 };
1107 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1108 {
1109 &atsuAngle ,
1110 };
1111 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1112 atsuTags, atsuSizes, atsuValues );
1113 }
1114
1115 {
1116 ATSUAttributeTag atsuTags[] =
1117 {
1118 kATSUCGContextTag ,
1119 };
1120 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1121 {
1122 sizeof( CGContextRef ) ,
1123 };
1124 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1125 {
1126 &m_cgContext ,
1127 };
1128 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
1129 atsuTags, atsuSizes, atsuValues );
1130 }
1131
1132 ATSUTextMeasurement textBefore, textAfter;
1133 ATSUTextMeasurement ascent, descent;
1134
1135 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1136 &textBefore , &textAfter, &ascent , &descent );
1137
1138 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1139
1140 Rect rect;
1141 /*
1142 // TODO
1143 if ( m_backgroundMode == wxSOLID )
1144 {
1145 wxGraphicsPath* path = m_graphicContext->CreatePath();
1146 path->MoveToPoint( drawX , drawY );
1147 path->AddLineToPoint(
1148 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1149 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) );
1150 path->AddLineToPoint(
1151 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1152 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1153 path->AddLineToPoint(
1154 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1155 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) );
1156
1157 m_graphicContext->FillPath( path , m_textBackgroundColour );
1158 delete path;
1159 }
1160 */
1161 x += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent));
1162 y += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent));
1163
1164 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1165 IntToFixed(x) , IntToFixed(y) , &rect );
1166 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1167
1168 CGContextSaveGState(m_cgContext);
1169 CGContextTranslateCTM(m_cgContext, x, y);
1170 CGContextScaleCTM(m_cgContext, 1, -1);
1171 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1172 IntToFixed(0) , IntToFixed(0) );
1173
1174 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
1175
1176 CGContextRestoreGState(m_cgContext);
1177
1178 ::ATSUDisposeTextLayout(atsuLayout);
1179
1180 #if SIZEOF_WCHAR_T == 4
1181 free( ubuf );
1182 #endif
1183 }
1184
1185 void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1186 wxDouble *descent, wxDouble *externalLeading ) const
1187 {
1188 wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") );
1189
1190 OSStatus status = noErr;
1191
1192 ATSUTextLayout atsuLayout;
1193 UniCharCount chars = str.length();
1194 UniChar* ubuf = NULL;
1195
1196 #if SIZEOF_WCHAR_T == 4
1197 wxMBConvUTF16 converter;
1198 #if wxUSE_UNICODE
1199 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 );
1200 ubuf = (UniChar*) malloc( unicharlen + 2 );
1201 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 );
1202 #else
1203 const wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1204 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1205 ubuf = (UniChar*) malloc( unicharlen + 2 );
1206 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1207 #endif
1208 chars = unicharlen / 2;
1209 #else
1210 #if wxUSE_UNICODE
1211 ubuf = (UniChar*) str.wc_str();
1212 #else
1213 wxWCharBuffer wchar = str.wc_str( wxConvLocal );
1214 chars = wxWcslen( wchar.data() );
1215 ubuf = (UniChar*) wchar.data();
1216 #endif
1217 #endif
1218
1219 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1220 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout );
1221
1222 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
1223
1224 ATSUTextMeasurement textBefore, textAfter;
1225 ATSUTextMeasurement textAscent, textDescent;
1226
1227 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1228 &textBefore , &textAfter, &textAscent , &textDescent );
1229
1230 if ( height )
1231 *height = FixedToInt(textAscent + textDescent);
1232 if ( descent )
1233 *descent = FixedToInt(textDescent);
1234 if ( externalLeading )
1235 *externalLeading = 0;
1236 if ( width )
1237 *width = FixedToInt(textAfter - textBefore);
1238
1239 ::ATSUDisposeTextLayout(atsuLayout);
1240 }
1241
1242 void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1243 {
1244 widths.Empty();
1245 widths.Add(0, text.length());
1246
1247 if (text.empty())
1248 return;
1249
1250 ATSUTextLayout atsuLayout;
1251 UniCharCount chars = text.length();
1252 UniChar* ubuf = NULL;
1253
1254 #if SIZEOF_WCHAR_T == 4
1255 wxMBConvUTF16 converter;
1256 #if wxUSE_UNICODE
1257 size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 );
1258 ubuf = (UniChar*) malloc( unicharlen + 2 );
1259 converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 );
1260 #else
1261 const wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1262 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 );
1263 ubuf = (UniChar*) malloc( unicharlen + 2 );
1264 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 );
1265 #endif
1266 chars = unicharlen / 2;
1267 #else
1268 #if wxUSE_UNICODE
1269 ubuf = (UniChar*) text.wc_str();
1270 #else
1271 wxWCharBuffer wchar = text.wc_str( wxConvLocal );
1272 chars = wxWcslen( wchar.data() );
1273 ubuf = (UniChar*) wchar.data();
1274 #endif
1275 #endif
1276
1277 ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1278 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout );
1279
1280 for ( int pos = 0; pos < (int)chars; pos ++ )
1281 {
1282 unsigned long actualNumberOfBounds = 0;
1283 ATSTrapezoid glyphBounds;
1284
1285 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1286 OSStatus result;
1287 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
1288 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
1289 if (result != noErr || actualNumberOfBounds != 1 )
1290 return;
1291
1292 widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x );
1293 //unsigned char uch = s[i];
1294 }
1295
1296 ::ATSUDisposeTextLayout(atsuLayout);
1297 }
1298
1299 void wxMacCoreGraphicsContext::SetFont( const wxFont &font )
1300 {
1301 if ( m_macATSUIStyle )
1302 {
1303 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
1304 m_macATSUIStyle = NULL;
1305 }
1306
1307 if ( font.Ok() )
1308 {
1309 m_font = font ;
1310 OSStatus status;
1311
1312 status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle );
1313
1314 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") );
1315
1316 // we need the scale here ...
1317
1318 Fixed atsuSize = IntToFixed( int( /*m_scaleY*/ 1 * font.MacGetFontSize()) );
1319 RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColor.GetPixel() );
1320 ATSUAttributeTag atsuTags[] =
1321 {
1322 kATSUSizeTag ,
1323 kATSUColorTag ,
1324 };
1325 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1326 {
1327 sizeof( Fixed ) ,
1328 sizeof( RGBColor ) ,
1329 };
1330 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
1331 {
1332 &atsuSize ,
1333 &atsuColor ,
1334 };
1335
1336 status = ::ATSUSetAttributes(
1337 (ATSUStyle)m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
1338 atsuTags, atsuSizes, atsuValues);
1339
1340 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
1341 }
1342 }
1343
1344 void * wxMacCoreGraphicsContext::GetNativeContext()
1345 {
1346 return m_cgContext;
1347 }
1348
1349 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC &dc )
1350 {
1351 return new wxMacCoreGraphicsContext((CGContextRef)dc.GetWindow()->MacGetCGContextRef() );
1352 }
1353
1354 wxGraphicsContext* wxGraphicsContext::Create( wxWindow * window )
1355 {
1356 return new wxMacCoreGraphicsContext( window );
1357 }
1358
1359 wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context )
1360 {
1361 return new wxMacCoreGraphicsContext((CGContextRef)context);
1362 }
1363
1364 wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow( void * window )
1365 {
1366 return new wxMacCoreGraphicsContext((WindowRef)window);
1367 }
1368
1369 #endif // wxMAC_USE_CORE_GRAPHICS