/core/externals/update-engine/externals/google-toolbox-for-mac/iPhone/GTMRoundedRectPath.m

http://macfuse.googlecode.com/ · Objective C · 91 lines · 64 code · 8 blank · 19 comment · 7 complexity · b6cf92a72141039dcf557fab2b60460e MD5 · raw file

  1. //
  2. // GTMRoundedRectPath.m
  3. //
  4. // Copyright 2010 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #include "GTMRoundedRectPath.h"
  19. #include <Foundation/Foundation.h>
  20. void GTMCGContextAddRoundRect(CGContextRef context,
  21. CGRect rect,
  22. CGFloat radius) {
  23. if (!CGRectIsEmpty(rect)) {
  24. if (radius > 0.0) {
  25. // Clamp radius to be no larger than half the rect's width or height.
  26. radius = MIN(radius, 0.5f * MIN(rect.size.width, rect.size.height));
  27. CGPoint topLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
  28. CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
  29. CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect),
  30. CGRectGetMinY(rect));
  31. CGContextMoveToPoint(context, CGRectGetMidX(rect), CGRectGetMaxY(rect));
  32. CGContextAddArcToPoint(context, topLeft.x, topLeft.y, rect.origin.x,
  33. rect.origin.y, radius);
  34. CGContextAddArcToPoint(context, rect.origin.x, rect.origin.y,
  35. bottomRight.x, bottomRight.y, radius);
  36. CGContextAddArcToPoint(context, bottomRight.x, bottomRight.y,
  37. topRight.x, topRight.y, radius);
  38. CGContextAddArcToPoint(context, topRight.x, topRight.y,
  39. topLeft.x, topLeft.y, radius);
  40. CGContextAddLineToPoint(context, CGRectGetMidX(rect), CGRectGetMaxY(rect));
  41. } else {
  42. CGContextAddRect(context, rect);
  43. }
  44. }
  45. }
  46. void GTMCGPathAddRoundRect(CGMutablePathRef path,
  47. const CGAffineTransform *m,
  48. CGRect rect,
  49. CGFloat radius) {
  50. if (!CGRectIsEmpty(rect)) {
  51. if (radius > 0.0) {
  52. // Clamp radius to be no larger than half the rect's width or height.
  53. radius = MIN(radius, 0.5f * MIN(rect.size.width, rect.size.height));
  54. CGPoint topLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
  55. CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
  56. CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect),
  57. CGRectGetMinY(rect));
  58. CGPathMoveToPoint(path, m, CGRectGetMidX(rect), CGRectGetMaxY(rect));
  59. CGPathAddArcToPoint(path, m, topLeft.x, topLeft.y,
  60. rect.origin.x, rect.origin.y, radius);
  61. CGPathAddArcToPoint(path, m, rect.origin.x, rect.origin.y,
  62. bottomRight.x, bottomRight.y, radius);
  63. CGPathAddArcToPoint(path, m, bottomRight.x, bottomRight.y,
  64. topRight.x, topRight.y, radius);
  65. CGPathAddArcToPoint(path, m, topRight.x, topRight.y,
  66. topLeft.x, topLeft.y, radius);
  67. CGPathAddLineToPoint(path, m, CGRectGetMidX(rect), CGRectGetMaxY(rect));
  68. } else {
  69. CGPathAddRect(path, m, rect);
  70. }
  71. }
  72. }
  73. CGPathRef GTMCreateRoundedRectPath(CGRect rect, CGFloat radius) {
  74. CGPathRef immutablePath = NULL;
  75. CGMutablePathRef path = CGPathCreateMutable();
  76. if (path) {
  77. GTMCGPathAddRoundRect(path, NULL, rect, radius);
  78. CGPathCloseSubpath(path);
  79. immutablePath = CGPathCreateCopy(path);
  80. CGPathRelease(path);
  81. }
  82. return immutablePath;
  83. }