/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMNSBezierPath+CGPath.m
Objective C | 69 lines | 35 code | 6 blank | 28 comment | 3 complexity | 85db1d887185d97988945cc73db6d733 MD5 | raw file
1// 2// GTMNSBezierPath+CGPath.m 3// 4// Category for extracting a CGPathRef from a NSBezierPath 5// 6// Copyright 2006-2008 Google Inc. 7// 8// Licensed under the Apache License, Version 2.0 (the "License"); you may not 9// use this file except in compliance with the License. You may obtain a copy 10// of the License at 11// 12// http://www.apache.org/licenses/LICENSE-2.0 13// 14// Unless required by applicable law or agreed to in writing, software 15// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 16// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 17// License for the specific language governing permissions and limitations under 18// the License. 19// 20#import "GTMNSBezierPath+CGPath.h" 21#import "GTMDefines.h" 22 23@implementation NSBezierPath (GTMBezierPathCGPathAdditions) 24 25// Extract a CGPathRef from a NSBezierPath. 26// 27// Args: 28// 29// Returns: 30// Converted CGPathRef. 31// nil if failure. 32- (CGPathRef)gtm_CGPath { 33 CGMutablePathRef thePath = CGPathCreateMutable(); 34 if (!thePath) return nil; 35 36 NSInteger elementCount = [self elementCount]; 37 38 // The maximum number of points is 3 for a NSCurveToBezierPathElement. 39 // (controlPoint1, controlPoint2, and endPoint) 40 NSPoint controlPoints[3]; 41 42 for (NSInteger i = 0; i < elementCount; i++) { 43 switch ([self elementAtIndex:i associatedPoints:controlPoints]) { 44 case NSMoveToBezierPathElement: 45 CGPathMoveToPoint(thePath, &CGAffineTransformIdentity, 46 controlPoints[0].x, controlPoints[0].y); 47 break; 48 case NSLineToBezierPathElement: 49 CGPathAddLineToPoint(thePath, &CGAffineTransformIdentity, 50 controlPoints[0].x, controlPoints[0].y); 51 break; 52 case NSCurveToBezierPathElement: 53 CGPathAddCurveToPoint(thePath, &CGAffineTransformIdentity, 54 controlPoints[0].x, controlPoints[0].y, 55 controlPoints[1].x, controlPoints[1].y, 56 controlPoints[2].x, controlPoints[2].y); 57 break; 58 case NSClosePathBezierPathElement: 59 CGPathCloseSubpath(thePath); 60 break; 61 default: // COV_NF_START 62 _GTMDevLog(@"Unknown element at [NSBezierPath (GTMBezierPathCGPathAdditions) cgPath]"); 63 break; // COV_NF_END 64 }; 65 } 66 return (CGPathRef)GTMCFAutorelease(thePath); 67} 68 69@end