PageRenderTime 36ms CodeModel.GetById 11ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/core/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
20#include <Foundation/Foundation.h>
21
22void GTMCGContextAddRoundRect(CGContextRef context, 
23                              CGRect rect, 
24                              CGFloat radius) {
25  if (!CGRectIsEmpty(rect)) {
26    if (radius > 0.0) {
27      // Clamp radius to be no larger than half the rect's width or height.
28      radius = MIN(radius, 0.5f * MIN(rect.size.width, rect.size.height));
29
30      CGPoint topLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
31      CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
32      CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect), 
33                                        CGRectGetMinY(rect));
34      
35      CGContextMoveToPoint(context, CGRectGetMidX(rect), CGRectGetMaxY(rect));
36      CGContextAddArcToPoint(context, topLeft.x, topLeft.y, rect.origin.x, 
37                             rect.origin.y, radius);
38      CGContextAddArcToPoint(context, rect.origin.x, rect.origin.y, 
39                             bottomRight.x, bottomRight.y, radius);
40      CGContextAddArcToPoint(context, bottomRight.x, bottomRight.y, 
41                             topRight.x, topRight.y, radius);
42      CGContextAddArcToPoint(context, topRight.x, topRight.y, 
43                             topLeft.x, topLeft.y, radius);
44      CGContextAddLineToPoint(context, CGRectGetMidX(rect), CGRectGetMaxY(rect));
45    } else {
46      CGContextAddRect(context, rect);
47    }
48  }
49}
50
51void GTMCGPathAddRoundRect(CGMutablePathRef path, 
52                           const CGAffineTransform *m, 
53                           CGRect rect,
54                           CGFloat radius) {
55  if (!CGRectIsEmpty(rect)) {
56    if (radius > 0.0) {
57      // Clamp radius to be no larger than half the rect's width or height.
58      radius = MIN(radius, 0.5f * MIN(rect.size.width, rect.size.height));
59
60      CGPoint topLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
61      CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
62      CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect), 
63                                        CGRectGetMinY(rect));
64      
65      CGPathMoveToPoint(path, m, CGRectGetMidX(rect), CGRectGetMaxY(rect));
66      CGPathAddArcToPoint(path, m, topLeft.x, topLeft.y, 
67                          rect.origin.x, rect.origin.y, radius);
68      CGPathAddArcToPoint(path, m, rect.origin.x, rect.origin.y, 
69                          bottomRight.x, bottomRight.y, radius);
70      CGPathAddArcToPoint(path, m, bottomRight.x, bottomRight.y, 
71                          topRight.x, topRight.y, radius);
72      CGPathAddArcToPoint(path, m, topRight.x, topRight.y, 
73                          topLeft.x, topLeft.y, radius);
74      CGPathAddLineToPoint(path, m, CGRectGetMidX(rect), CGRectGetMaxY(rect));
75    } else {
76      CGPathAddRect(path, m, rect);
77    }
78  }
79}
80
81CGPathRef GTMCreateRoundedRectPath(CGRect rect, CGFloat radius) {
82  CGPathRef immutablePath = NULL;
83  CGMutablePathRef path = CGPathCreateMutable();
84  if (path) {
85    GTMCGPathAddRoundRect(path, NULL, rect, radius);
86    CGPathCloseSubpath(path);
87    immutablePath = CGPathCreateCopy(path);
88    CGPathRelease(path);
89  }
90  return immutablePath;
91}