From c53ec5520e39096e0804ce8d89a21378c0904481 Mon Sep 17 00:00:00 2001 From: thomasvl Date: Fri, 13 Jun 2008 19:21:50 +0000 Subject: Landing a log of AppleScript/AppleEvent support code. Landing GTMHTTPServer as a simple server but mainly for use in unittesting. _GTMCompileAssert for doing compile time assertions to GTMDefines.h Lots of improvments for UnitTesting, Dave's gonna put up a wiki page shortly with the full details of what can be done. --- Foundation/GTMGeometryUtils.h | 326 ++++++++++++++++++++++++------------------ 1 file changed, 187 insertions(+), 139 deletions(-) (limited to 'Foundation/GTMGeometryUtils.h') diff --git a/Foundation/GTMGeometryUtils.h b/Foundation/GTMGeometryUtils.h index a58ac13..7d54cf2 100644 --- a/Foundation/GTMGeometryUtils.h +++ b/Foundation/GTMGeometryUtils.h @@ -42,7 +42,125 @@ enum { }; typedef NSUInteger GTMRectAlignment; -#pragma mark Miscellaneous +#pragma mark - +#pragma mark CG - Point On Rect +/// Return middle of min X side of rectangle +// +// Args: +// rect - rectangle +// +// Returns: +// point located in the middle of min X side of rect +CG_INLINE CGPoint GTMCGMidMinX(CGRect rect) { + return CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect)); +} + +/// Return middle of max X side of rectangle +// +// Args: +// rect - rectangle +// +// Returns: +// point located in the middle of max X side of rect +CG_INLINE CGPoint GTMCGMidMaxX(CGRect rect) { + return CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect)); +} + +/// Return middle of max Y side of rectangle +// +// Args: +// rect - rectangle +// +// Returns: +// point located in the middle of max Y side of rect +CG_INLINE CGPoint GTMCGMidMaxY(CGRect rect) { + return CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)); +} + +/// Return middle of min Y side of rectangle +// +// Args: +// rect - rectangle +// +// Returns: +// point located in the middle of min Y side of rect +CG_INLINE CGPoint GTMCGMidMinY(CGRect rect) { + return CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)); +} + +/// Return center of rectangle +// +// Args: +// rect - rectangle +// +// Returns: +// point located in the center of rect +CG_INLINE CGPoint GTMCGCenter(CGRect rect) { + return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); +} + +#pragma mark - +#pragma mark CG - Rect-Size Conversion + +/// Return size of rectangle +// +// Args: +// rect - rectangle +// +// Returns: +// size of rectangle +CG_INLINE CGSize GTMCGRectSize(CGRect rect) { + return CGSizeMake(CGRectGetWidth(rect), CGRectGetHeight(rect)); +} + +/// Return rectangle of size +// +// Args: +// size - size +// +// Returns: +// rectangle of size (origin 0,0) +CG_INLINE CGRect GTMCGRectOfSize(CGSize size) { + return CGRectMake(0.0f, 0.0f, size.width, size.height); +} + +#pragma mark - +#pragma mark CG - Rect Scaling and Alignment + +/// Scales an CGRect +// +// Args: +// inRect: Rect to scale +// xScale: fraction to scale (1.0 is 100%) +// yScale: fraction to scale (1.0 is 100%) +// +// Returns: +// Converted Rect +CG_INLINE CGRect GTMCGRectScale(CGRect inRect, CGFloat xScale, CGFloat yScale) { + return CGRectMake(inRect.origin.x, inRect.origin.y, + inRect.size.width * xScale, inRect.size.height * yScale); +} + + +/// Align rectangles +// +// Args: +// alignee - rect to be aligned +// aligner - rect to be aligned from +// alignment - way to align the rectangles +CGRect GTMCGAlignRectangles(CGRect alignee, CGRect aligner, + GTMRectAlignment alignment); +/// Scale rectangle +// +// Args: +// scalee - rect to be scaled +// size - size to scale to +// scaling - way to scale the rectangle +CGRect GTMCGScaleRectangleToSize(CGRect scalee, CGSize size, + GTMScaling scaling); + +#pragma mark - +#pragma mark CG - Miscellaneous /// Calculate the distance between two points. // @@ -52,7 +170,7 @@ typedef NSUInteger GTMRectAlignment; // // Returns: // Distance -CG_INLINE CGFloat GTMDistanceBetweenPoints(NSPoint pt1, NSPoint pt2) { +CG_INLINE CGFloat GTMCGDistanceBetweenPoints(CGPoint pt1, CGPoint pt2) { CGFloat dX = pt1.x - pt2.x; CGFloat dY = pt1.y - pt2.y; #if CGFLOAT_IS_DOUBLE @@ -62,8 +180,14 @@ CG_INLINE CGFloat GTMDistanceBetweenPoints(NSPoint pt1, NSPoint pt2) { #endif } +#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) +// iPhone does not have NSTypes defined, only CGTypes. So no NSRect, NSPoint etc. + #pragma mark - -#pragma mark Point Conversion +// All of the conversion routines below are basically copied from the +// NSGeometry header in the 10.5 sdk. + +#pragma mark NS <-> CG Point Conversion /// Quickly convert from a CGPoint to a NSPoint. // @@ -76,7 +200,9 @@ CG_INLINE CGFloat GTMDistanceBetweenPoints(NSPoint pt1, NSPoint pt2) { // Returns: // Converted NSPoint CG_INLINE NSPoint GTMCGPointToNSPoint(CGPoint inPoint) { - return NSMakePoint(inPoint.x, inPoint.y); + _GTMCompileAssert(sizeof(NSPoint) == sizeof(CGPoint), NSPoint_and_CGPoint_must_be_the_same_size); + union convertUnion {NSPoint ns; CGPoint cg;}; + return ((union convertUnion *)&inPoint)->ns; } /// Quickly convert from a NSPoint to a CGPoint. @@ -90,11 +216,13 @@ CG_INLINE NSPoint GTMCGPointToNSPoint(CGPoint inPoint) { // Returns: // Converted CGPoint CG_INLINE CGPoint GTMNSPointToCGPoint(NSPoint inPoint) { - return CGPointMake(inPoint.x, inPoint.y); + _GTMCompileAssert(sizeof(NSPoint) == sizeof(CGPoint), NSPoint_and_CGPoint_must_be_the_same_size); + union convertUnion {NSPoint ns; CGPoint cg;}; + return ((union convertUnion *)&inPoint)->cg; } #pragma mark - -#pragma mark Rect Conversion +#pragma mark NS <-> CG Rect Conversion /// Convert from a CGRect to a NSRect. // @@ -107,7 +235,9 @@ CG_INLINE CGPoint GTMNSPointToCGPoint(NSPoint inPoint) { // Returns: // Converted NSRect CG_INLINE NSRect GTMCGRectToNSRect(CGRect inRect) { - return NSMakeRect(inRect.origin.x,inRect.origin.y,inRect.size.width,inRect.size.height); + _GTMCompileAssert(sizeof(NSRect) == sizeof(CGRect), NSRect_and_CGRect_must_be_the_same_size); + union convertUnion {NSRect ns; CGRect cg;}; + return ((union convertUnion *)&inRect)->ns; } /// Convert from a NSRect to a CGRect. @@ -121,11 +251,14 @@ CG_INLINE NSRect GTMCGRectToNSRect(CGRect inRect) { // Returns: // Converted CGRect CG_INLINE CGRect GTMNSRectToCGRect(NSRect inRect) { - return CGRectMake(inRect.origin.x,inRect.origin.y,inRect.size.width,inRect.size.height); + _GTMCompileAssert(sizeof(NSRect) == sizeof(CGRect), NSRect_and_CGRect_must_be_the_same_size); + union convertUnion {NSRect ns; CGRect cg;}; + return ((union convertUnion *)&inRect)->cg; } + #pragma mark - -#pragma mark Size Conversion +#pragma mark NS <-> CG Size Conversion /// Convert from a CGSize to an NSSize. // @@ -135,7 +268,9 @@ CG_INLINE CGRect GTMNSRectToCGRect(NSRect inRect) { // Returns: // Converted NSSize CG_INLINE NSSize GTMCGSizeToNSSize(CGSize inSize) { - return NSMakeSize(inSize.width, inSize.height); + _GTMCompileAssert(sizeof(NSSize) == sizeof(CGSize), NSSize_and_CGSize_must_be_the_same_size); + union convertUnion {NSSize ns; CGSize cg;}; + return ((union convertUnion *)&inSize)->ns; } /// Convert from a NSSize to a CGSize. @@ -146,53 +281,55 @@ CG_INLINE NSSize GTMCGSizeToNSSize(CGSize inSize) { // Returns: // Converted CGSize CG_INLINE CGSize GTMNSSizeToCGSize(NSSize inSize) { - return CGSizeMake(inSize.width, inSize.height); + _GTMCompileAssert(sizeof(NSSize) == sizeof(CGSize), NSSize_and_CGSize_must_be_the_same_size); + union convertUnion {NSSize ns; CGSize cg;}; + return ((union convertUnion *)&inSize)->cg; } #pragma mark - -#pragma mark Point On Rect +#pragma mark NS - Point On Rect -/// Return middle of left side of rectangle +/// Return middle of min X side of rectangle // // Args: // rect - rectangle // // Returns: -// point located in the middle of left side of rect -CG_INLINE NSPoint GTMNSMidLeft(NSRect rect) { +// point located in the middle of min X side of rect +CG_INLINE NSPoint GTMNSMidMinX(NSRect rect) { return NSMakePoint(NSMinX(rect), NSMidY(rect)); } -/// Return middle of right side of rectangle +/// Return middle of max X side of rectangle // // Args: // rect - rectangle // // Returns: -// point located in the middle of right side of rect -CG_INLINE NSPoint GTMNSMidRight(NSRect rect) { +// point located in the middle of max X side of rect +CG_INLINE NSPoint GTMNSMidMaxX(NSRect rect) { return NSMakePoint(NSMaxX(rect), NSMidY(rect)); } -/// Return middle of top side of rectangle +/// Return middle of max Y side of rectangle // // Args: // rect - rectangle // // Returns: -// point located in the middle of top side of rect -CG_INLINE NSPoint GTMNSMidTop(NSRect rect) { +// point located in the middle of max Y side of rect +CG_INLINE NSPoint GTMNSMidMaxY(NSRect rect) { return NSMakePoint(NSMidX(rect), NSMaxY(rect)); } -/// Return middle of bottom side of rectangle +/// Return middle of min Y side of rectangle // // Args: // rect - rectangle // // Returns: -// point located in the middle of bottom side of rect -CG_INLINE NSPoint GTMNSMidBottom(NSRect rect) { +// point located in the middle of min Y side of rect +CG_INLINE NSPoint GTMNSMidMinY(NSRect rect) { return NSMakePoint(NSMidX(rect), NSMinY(rect)); } @@ -207,63 +344,8 @@ CG_INLINE NSPoint GTMNSCenter(NSRect rect) { return NSMakePoint(NSMidX(rect), NSMidY(rect)); } -/// Return middle of left side of rectangle -// -// Args: -// rect - rectangle -// -// Returns: -// point located in the middle of left side of rect -CG_INLINE CGPoint GTMCGMidLeft(CGRect rect) { - return CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect)); -} - -/// Return middle of right side of rectangle -// -// Args: -// rect - rectangle -// -// Returns: -// point located in the middle of right side of rect -CG_INLINE CGPoint GTMCGMidRight(CGRect rect) { - return CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect)); -} - -/// Return middle of top side of rectangle -// -// Args: -// rect - rectangle -// -// Returns: -// point located in the middle of top side of rect -CG_INLINE CGPoint GTMCGMidTop(CGRect rect) { - return CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)); -} - -/// Return middle of bottom side of rectangle -// -// Args: -// rect - rectangle -// -// Returns: -// point located in the middle of bottom side of rect -CG_INLINE CGPoint GTMCGMidBottom(CGRect rect) { - return CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)); -} - -/// Return center of rectangle -// -// Args: -// rect - rectangle -// -// Returns: -// point located in the center of rect -CG_INLINE CGPoint GTMCGCenter(CGRect rect) { - return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); -} - #pragma mark - -#pragma mark Rect-Size Conversion +#pragma mark NS - Rect-Size Conversion /// Return size of rectangle // @@ -276,17 +358,6 @@ CG_INLINE NSSize GTMNSRectSize(NSRect rect) { return NSMakeSize(NSWidth(rect), NSHeight(rect)); } -/// Return size of rectangle -// -// Args: -// rect - rectangle -// -// Returns: -// size of rectangle -CG_INLINE CGSize GTMCGRectSize(CGRect rect) { - return CGSizeMake(CGRectGetWidth(rect), CGRectGetHeight(rect)); -} - /// Return rectangle of size // // Args: @@ -298,19 +369,8 @@ CG_INLINE NSRect GTMNSRectOfSize(NSSize size) { return NSMakeRect(0.0f, 0.0f, size.width, size.height); } -/// Return rectangle of size -// -// Args: -// size - size -// -// Returns: -// rectangle of size (origin 0,0) -CG_INLINE CGRect GTMCGRectOfSize(CGSize size) { - return CGRectMake(0.0f, 0.0f, size.width, size.height); -} - #pragma mark - -#pragma mark Rect Scaling and Alignment +#pragma mark NS - Rect Scaling and Alignment /// Scales an NSRect // @@ -326,40 +386,19 @@ CG_INLINE NSRect GTMNSRectScale(NSRect inRect, CGFloat xScale, CGFloat yScale) { inRect.size.width * xScale, inRect.size.height * yScale); } -/// Scales an CGRect -// -// Args: -// inRect: Rect to scale -// xScale: fraction to scale (1.0 is 100%) -// yScale: fraction to scale (1.0 is 100%) -// -// Returns: -// Converted Rect -CG_INLINE CGRect GTMCGRectScale(CGRect inRect, CGFloat xScale, CGFloat yScale) { - return CGRectMake(inRect.origin.x, inRect.origin.y, - inRect.size.width * xScale, inRect.size.height * yScale); -} -/// Align rectangles -// -// Args: -// alignee - rect to be aligned -// aligner - rect to be aligned from -NSRect GTMAlignRectangles(NSRect alignee, NSRect aligner, - GTMRectAlignment alignment); /// Align rectangles // // Args: // alignee - rect to be aligned // aligner - rect to be aligned from -// alignment - way to align the rectangles -CG_INLINE CGRect GTMCGAlignRectangles(CGRect alignee, CGRect aligner, +CG_INLINE NSRect GTMNSAlignRectangles(NSRect alignee, NSRect aligner, GTMRectAlignment alignment) { - return GTMNSRectToCGRect(GTMAlignRectangles(GTMCGRectToNSRect(alignee), - GTMCGRectToNSRect(aligner), + return GTMCGRectToNSRect(GTMCGAlignRectangles(GTMNSRectToCGRect(alignee), + GTMNSRectToCGRect(aligner), alignment)); -} +} /// Scale rectangle // @@ -367,18 +406,27 @@ CG_INLINE CGRect GTMCGAlignRectangles(CGRect alignee, CGRect aligner, // scalee - rect to be scaled // size - size to scale to // scaling - way to scale the rectangle -NSRect GTMScaleRectangleToSize(NSRect scalee, NSSize size, - GTMScaling scaling); +CG_INLINE NSRect GTMNSScaleRectangleToSize(NSRect scalee, NSSize size, + GTMScaling scaling) { + return GTMCGRectToNSRect(GTMCGScaleRectangleToSize(GTMNSRectToCGRect(scalee), + GTMNSSizeToCGSize(size), + scaling)); +} -/// Scale rectangle +#pragma mark - +#pragma mark NS - Miscellaneous + +/// Calculate the distance between two points. // // Args: -// scalee - rect to be scaled -// size - size to scale to -// scaling - way to scale the rectangle -CG_INLINE CGRect GTMCGScaleRectangleToSize(CGRect scalee, CGSize size, - GTMScaling scaling) { - return GTMNSRectToCGRect(GTMScaleRectangleToSize(GTMCGRectToNSRect(scalee), - GTMCGSizeToNSSize(size), - scaling)); +// pt1 first point +// pt2 second point +// +// Returns: +// Distance +CG_INLINE CGFloat GTMNSDistanceBetweenPoints(NSPoint pt1, NSPoint pt2) { + return GTMCGDistanceBetweenPoints(GTMNSPointToCGPoint(pt1), + GTMNSPointToCGPoint(pt2)); } + +#endif // (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) -- cgit v1.2.3