From 69491c3dd52dffcb3fdbaffeffb63483fbd15088 Mon Sep 17 00:00:00 2001 From: "gtm.daemon" Date: Tue, 25 May 2010 21:05:40 +0000 Subject: [Author: dmaclach] Makes the rounded rect calls fit the CG calling conventions. R=thomasvl --- iPhone/GTMRoundedRectPath.h | 26 +++++++++++++ iPhone/GTMRoundedRectPath.m | 93 ++++++++++++++++++++++++++++++++------------- 2 files changed, 92 insertions(+), 27 deletions(-) (limited to 'iPhone') diff --git a/iPhone/GTMRoundedRectPath.h b/iPhone/GTMRoundedRectPath.h index 1bc8a08..2b20fec 100644 --- a/iPhone/GTMRoundedRectPath.h +++ b/iPhone/GTMRoundedRectPath.h @@ -18,5 +18,31 @@ #import +// Inscribe a round rectangle inside of rectangle |rect| with a corner radius +// of |radius| +// +// Args: +// rect: outer rectangle to inscribe into +// radius: radius of the corners. |radius| is clamped internally +// to be no larger than the smaller of half |rect|'s width or height +void GTMCGContextAddRoundRect(CGContextRef context, + CGRect rect, + CGFloat radius); + +// Adds a path which is a round rectangle inscribed inside of rectangle |rect| +// with a corner radius of |radius| +// +// Args: +// path: path to add the rounded rectangle to +// m: matrix modifying the round rect +// rect: outer rectangle to inscribe into +// radius: radius of the corners. |radius| is clamped internally +// to be no larger than the smaller of half |rect|'s width or height +void GTMCGPathAddRoundRect(CGMutablePathRef path, + const CGAffineTransform *m, + CGRect rect, + CGFloat radius); + // Allocates a new rounded corner rectangle path. +// DEPRECATED. Please use one of the above. CGPathRef GTMCreateRoundedRectPath(CGRect rect, CGFloat radius); diff --git a/iPhone/GTMRoundedRectPath.m b/iPhone/GTMRoundedRectPath.m index 194f53e..bb2c42c 100644 --- a/iPhone/GTMRoundedRectPath.m +++ b/iPhone/GTMRoundedRectPath.m @@ -17,34 +17,73 @@ // #include "GTMRoundedRectPath.h" -CGPathRef GTMCreateRoundedRectPath(CGRect rect, CGFloat radius) { - CGMutablePathRef path = CGPathCreateMutable(); - - CGPoint topLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect)); - CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect)); - CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect)); - CGPoint bottomLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect)); +void GTMCGContextAddRoundRect(CGContextRef context, + CGRect rect, + CGFloat radius) { + if (!CGRectIsEmpty(rect)) { + if (radius > 0.0) { + // Clamp radius to be no larger than half the rect's width or height. + radius = MIN(radius, 0.5 * MIN(rect.size.width, rect.size.height)); + + CGPoint topLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect)); + CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect)); + CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect), + CGRectGetMinY(rect)); + + CGContextMoveToPoint(context, CGRectGetMidX(rect), CGRectGetMaxY(rect)); + CGContextAddArcToPoint(context, topLeft.x, topLeft.y, rect.origin.x, + rect.origin.y, radius); + CGContextAddArcToPoint(context, rect.origin.x, rect.origin.y, + bottomRight.x, bottomRight.y, radius); + CGContextAddArcToPoint(context, bottomRight.x, bottomRight.y, + topRight.x, topRight.y, radius); + CGContextAddArcToPoint(context, topRight.x, topRight.y, + topLeft.x, topLeft.y, radius); + CGContextAddLineToPoint(context, CGRectGetMidX(rect), CGRectGetMaxY(rect)); + } else { + CGContextAddRect(context, rect); + } + } +} - CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect)); - CGPathAddArcToPoint(path, NULL, - topLeft.x, topLeft.y, - bottomLeft.x, bottomLeft.y, - radius); - CGPathAddArcToPoint(path, NULL, - bottomLeft.x, bottomLeft.y, - bottomRight.x, bottomRight.y, - radius); - CGPathAddArcToPoint(path, NULL, - bottomRight.x, bottomRight.y, - topRight.x, topRight.y, - radius); - CGPathAddArcToPoint(path, NULL, - topRight.x, topRight.y, - topLeft.x, topLeft.y, - radius); - CGPathCloseSubpath(path); +void GTMCGPathAddRoundRect(CGMutablePathRef path, + const CGAffineTransform *m, + CGRect rect, + CGFloat radius) { + if (!CGRectIsEmpty(rect)) { + if (radius > 0.0) { + // Clamp radius to be no larger than half the rect's width or height. + radius = MIN(radius, 0.5 * MIN(rect.size.width, rect.size.height)); + + CGPoint topLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect)); + CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect)); + CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect), + CGRectGetMinY(rect)); + + CGPathMoveToPoint(path, m, CGRectGetMidX(rect), CGRectGetMaxY(rect)); + CGPathAddArcToPoint(path, m, topLeft.x, topLeft.y, + rect.origin.x, rect.origin.y, radius); + CGPathAddArcToPoint(path, m, rect.origin.x, rect.origin.y, + bottomRight.x, bottomRight.y, radius); + CGPathAddArcToPoint(path, m, bottomRight.x, bottomRight.y, + topRight.x, topRight.y, radius); + CGPathAddArcToPoint(path, m, topRight.x, topRight.y, + topLeft.x, topLeft.y, radius); + CGPathAddLineToPoint(path, m, CGRectGetMidX(rect), CGRectGetMaxY(rect)); + } else { + CGPathAddRect(path, m, rect); + } + } +} - CGPathRef immutablePath = CGPathCreateCopy(path); - CGPathRelease(path); +CGPathRef GTMCreateRoundedRectPath(CGRect rect, CGFloat radius) { + CGPathRef immutablePath = NULL; + CGMutablePathRef path = CGPathCreateMutable(); + if (path) { + GTMCGPathAddRoundRect(path, NULL, rect, radius); + CGPathCloseSubpath(path); + immutablePath = CGPathCreateCopy(path); + CGPathRelease(path); + } return immutablePath; } -- cgit v1.2.3