/core/externals/update-engine/externals/google-toolbox-for-mac/AppKit/GTMNSBezierPath+CGPath.m

http://macfuse.googlecode.com/ · 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. @implementation NSBezierPath (GTMBezierPathCGPathAdditions)
  23. // Extract a CGPathRef from a NSBezierPath.
  24. //
  25. // Args:
  26. //
  27. // Returns:
  28. // Converted CGPathRef.
  29. // nil if failure.
  30. - (CGPathRef)gtm_CGPath {
  31. CGMutablePathRef thePath = CGPathCreateMutable();
  32. if (!thePath) return nil;
  33. NSInteger elementCount = [self elementCount];
  34. // The maximum number of points is 3 for a NSCurveToBezierPathElement.
  35. // (controlPoint1, controlPoint2, and endPoint)
  36. NSPoint controlPoints[3];
  37. for (NSInteger i = 0; i < elementCount; i++) {
  38. switch ([self elementAtIndex:i associatedPoints:controlPoints]) {
  39. case NSMoveToBezierPathElement:
  40. CGPathMoveToPoint(thePath, &CGAffineTransformIdentity,
  41. controlPoints[0].x, controlPoints[0].y);
  42. break;
  43. case NSLineToBezierPathElement:
  44. CGPathAddLineToPoint(thePath, &CGAffineTransformIdentity,
  45. controlPoints[0].x, controlPoints[0].y);
  46. break;
  47. case NSCurveToBezierPathElement:
  48. CGPathAddCurveToPoint(thePath, &CGAffineTransformIdentity,
  49. controlPoints[0].x, controlPoints[0].y,
  50. controlPoints[1].x, controlPoints[1].y,
  51. controlPoints[2].x, controlPoints[2].y);
  52. break;
  53. case NSClosePathBezierPathElement:
  54. CGPathCloseSubpath(thePath);
  55. break;
  56. default: // COV_NF_START
  57. _GTMDevLog(@"Unknown element at [NSBezierPath (GTMBezierPathCGPathAdditions) cgPath]");
  58. break; // COV_NF_END
  59. };
  60. }
  61. return (CGPathRef)GTMCFAutorelease(thePath);
  62. }
  63. @end