Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from indragiek/master
Browse files Browse the repository at this point in the history
MEDEdgeInsets
  • Loading branch information
jspahrsummers committed Aug 7, 2013
2 parents f4572a0 + 0723f02 commit 8c8b5c8
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 5 deletions.
18 changes: 18 additions & 0 deletions Archimedes.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
0378ABA717B16F980056C690 /* MEDEdgeInsets.h in Headers */ = {isa = PBXBuildFile; fileRef = 0378ABA517B16F980056C690 /* MEDEdgeInsets.h */; settings = {ATTRIBUTES = (Public, ); }; };
0378ABA817B16F980056C690 /* MEDEdgeInsets.m in Sources */ = {isa = PBXBuildFile; fileRef = 0378ABA617B16F980056C690 /* MEDEdgeInsets.m */; };
0378ABA917B16F9A0056C690 /* MEDEdgeInsets.m in Sources */ = {isa = PBXBuildFile; fileRef = 0378ABA617B16F980056C690 /* MEDEdgeInsets.m */; };
0378ABAA17B16FA10056C690 /* MEDEdgeInsets.h in Headers */ = {isa = PBXBuildFile; fileRef = 0378ABA517B16F980056C690 /* MEDEdgeInsets.h */; settings = {ATTRIBUTES = (Public, ); }; };
0378ABC417B17A160056C690 /* MEDEdgeInsetsSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 0378ABC317B17A160056C690 /* MEDEdgeInsetsSpec.m */; };
0378ABC517B17A160056C690 /* MEDEdgeInsetsSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 0378ABC317B17A160056C690 /* MEDEdgeInsetsSpec.m */; };
D005A1A9169A384400A9D2DB /* Archimedes.h in Headers */ = {isa = PBXBuildFile; fileRef = D02FAEE51635DDC0008D42D9 /* Archimedes.h */; settings = {ATTRIBUTES = (Public, ); }; };
D005A1AB169A384C00A9D2DB /* CGGeometry+MEDConvenienceAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = D02FAFDC1635E1E9008D42D9 /* CGGeometry+MEDConvenienceAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
D005A1AD169A385300A9D2DB /* NSValue+MEDGeometryAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0626A9B1679506E00E951A5 /* NSValue+MEDGeometryAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -144,6 +150,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0378ABA517B16F980056C690 /* MEDEdgeInsets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MEDEdgeInsets.h; sourceTree = "<group>"; };
0378ABA617B16F980056C690 /* MEDEdgeInsets.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MEDEdgeInsets.m; sourceTree = "<group>"; };
0378ABC317B17A160056C690 /* MEDEdgeInsetsSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MEDEdgeInsetsSpec.m; sourceTree = "<group>"; };
D02FAEE11635DDC0008D42D9 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D02FAEE41635DDC0008D42D9 /* Archimedes-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Archimedes-Prefix.pch"; sourceTree = "<group>"; };
D02FAEE51635DDC0008D42D9 /* Archimedes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Archimedes.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -400,6 +409,7 @@
children = (
D02FAFD31635E1A8008D42D9 /* MEDCGGeometryAdditionsSpec.m */,
D0626AA0167950D200E951A5 /* MEDNSValueAdditionsSpec.m */,
0378ABC317B17A160056C690 /* MEDEdgeInsetsSpec.m */,
);
name = Specs;
sourceTree = "<group>";
Expand All @@ -411,6 +421,8 @@
D02FAFDD1635E1E9008D42D9 /* CGGeometry+MEDConvenienceAdditions.m */,
D0626A9B1679506E00E951A5 /* NSValue+MEDGeometryAdditions.h */,
D0626A9C1679506E00E951A5 /* NSValue+MEDGeometryAdditions.m */,
0378ABA517B16F980056C690 /* MEDEdgeInsets.h */,
0378ABA617B16F980056C690 /* MEDEdgeInsets.m */,
);
name = Extensions;
sourceTree = "<group>";
Expand All @@ -424,6 +436,7 @@
files = (
D005A1A9169A384400A9D2DB /* Archimedes.h in Headers */,
D005A1AB169A384C00A9D2DB /* CGGeometry+MEDConvenienceAdditions.h in Headers */,
0378ABAA17B16FA10056C690 /* MEDEdgeInsets.h in Headers */,
D005A1AD169A385300A9D2DB /* NSValue+MEDGeometryAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -434,6 +447,7 @@
files = (
D0D6292517790BF2008EF295 /* Archimedes.h in Headers */,
D0D6292617790BF4008EF295 /* NSValue+MEDGeometryAdditions.h in Headers */,
0378ABA717B16F980056C690 /* MEDEdgeInsets.h in Headers */,
D0D6292717790BF6008EF295 /* CGGeometry+MEDConvenienceAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -674,6 +688,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0378ABA917B16F9A0056C690 /* MEDEdgeInsets.m in Sources */,
D02FAFE01635E1EA008D42D9 /* CGGeometry+MEDConvenienceAdditions.m in Sources */,
D0626A9F1679506E00E951A5 /* NSValue+MEDGeometryAdditions.m in Sources */,
);
Expand All @@ -685,6 +700,7 @@
files = (
D02FAFD51635E1A8008D42D9 /* MEDCGGeometryAdditionsSpec.m in Sources */,
D02FAFDA1635E1BF008D42D9 /* MEDGeometryTestObject.m in Sources */,
0378ABC517B17A160056C690 /* MEDEdgeInsetsSpec.m in Sources */,
D0626AA2167950D200E951A5 /* MEDNSValueAdditionsSpec.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -693,6 +709,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0378ABA817B16F980056C690 /* MEDEdgeInsets.m in Sources */,
D0D6292317790BED008EF295 /* CGGeometry+MEDConvenienceAdditions.m in Sources */,
D0D6292417790BED008EF295 /* NSValue+MEDGeometryAdditions.m in Sources */,
);
Expand All @@ -704,6 +721,7 @@
files = (
D0D6292E17790C2B008EF295 /* MEDNSValueAdditionsSpec.m in Sources */,
D0D6292D17790C29008EF295 /* MEDCGGeometryAdditionsSpec.m in Sources */,
0378ABC417B17A160056C690 /* MEDEdgeInsetsSpec.m in Sources */,
D0D6292F17790C2C008EF295 /* MEDGeometryTestObject.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions Archimedes/Archimedes.h
Expand Up @@ -8,3 +8,4 @@

#import <Archimedes/CGGeometry+MEDConvenienceAdditions.h>
#import <Archimedes/NSValue+MEDGeometryAdditions.h>
#import <Archimedes/MEDEdgeInsets.h>
49 changes: 49 additions & 0 deletions Archimedes/MEDEdgeInsets.h
@@ -0,0 +1,49 @@
//
// MEDEdgeInsets.h
// Archimedes
//
// Created by Indragie Karunaratne on 8/6/2013.
// Copyright (c) 2013 GitHub. All rights reserved.
//

#import <Foundation/Foundation.h>

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#import <UIKit/UIKit.h>
typedef UIEdgeInsets MEDEdgeInsets;
#elif TARGET_OS_MAC
#import <AppKit/AppKit.h>
typedef NSEdgeInsets MEDEdgeInsets;
#endif

// `MEDEdgeInsets` structure with all members set to 0.
#define MEDEdgeInsetsZero (MEDEdgeInsets){ .top = 0, .left = 0, .bottom = 0, .right = 0 }

// Returns an MEDEgeInsets struct with the given edge insets.
MEDEdgeInsets MEDEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);

// Returns whether the two given `MEDEdgeInsets` are equal.
BOOL MEDEdgeInsetsEqualToEdgeInsets(MEDEdgeInsets insets1, MEDEdgeInsets insets2);

// The top inset will affect the min Y coordinate on iOS, and max Y coordinate on
// OS X, and vice-versa for bottom due to the default flippedness of drawing contexts
// on each platform.
//
// Returns a rectangle adjusted by incrementing the origin and decrementing the size
// of the given rect by applying the given insets.
CGRect MEDEdgeInsetsInsetRect(CGRect rect, MEDEdgeInsets insets);

// Returns a string formatted to contain data from an `MEDEdgeInsets` structure.
//
// This string can be passed into `MEDEdgeInsetsFromString()` to recreate the original
// `MEDEdgeInsets` structure.
NSString * NSStringFromMEDEdgeInsets(MEDEdgeInsets insets);

// Returns an `MEDEdgeInsets` structure corresponding to data in the given string
// or `MEDEdgeInsetsZero` if the string is not formatted appropriately.
//
// The string format is “{top, left, bottom, right}”, where each member of the
// `MEDEdgeInsets` structure is separated with a comma. This function should generally
// only be used to convert strings that were previously created using the
// `NSStringFromMEDEdgeInsets()` function.
MEDEdgeInsets MEDEdgeInsetsFromString(NSString *string);
47 changes: 47 additions & 0 deletions Archimedes/MEDEdgeInsets.m
@@ -0,0 +1,47 @@
//
// MEDEdgeInsets.m
// Archimedes
//
// Created by Indragie Karunaratne on 8/6/2013.
// Copyright (c) 2013 GitHub. All rights reserved.
//

#import "MEDEdgeInsets.h"

MEDEdgeInsets MEDEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
return (MEDEdgeInsets){ .top = top, .left = left, .bottom = bottom, .right = right };
}

BOOL MEDEdgeInsetsEqualToEdgeInsets(MEDEdgeInsets insets1, MEDEdgeInsets insets2) {
return (fabs(insets1.top - insets2.top) < 0.1) &&
(fabs(insets1.left - insets2.left) < 0.1) &&
(fabs(insets1.bottom - insets2.bottom) < 0.1) &&
(fabs(insets1.right - insets2.right) < 0.1);
}

CGRect MEDEdgeInsetsInsetRect(CGRect rect, MEDEdgeInsets insets) {
if (((insets.top + insets.bottom) > rect.size.height) || ((insets.left + insets.right) > rect.size.width)) {
return CGRectNull;
} else {
rect.origin.x += insets.left;
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
rect.origin.y += insets.top;
#elif TARGET_OS_MAC
rect.origin.y += insets.bottom;
#endif
rect.size.height -= (insets.top + insets.bottom);
rect.size.width -= (insets.left + insets.right);
return rect;
}
}

NSString * NSStringFromMEDEdgeInsets(MEDEdgeInsets insets) {
return [NSString stringWithFormat:@"{%g, %g, %g, %g}", insets.top, insets.left, insets.bottom, insets.right];
}

MEDEdgeInsets MEDEdgeInsetsFromString(NSString *string) {
NSCParameterAssert(string != nil);
double top = 0, left = 0, bottom = 0, right = 0;
sscanf(string.UTF8String, "{%lg, %lg, %lg, %lg}", &top, &left, &bottom, &right);
return MEDEdgeInsetsMake((CGFloat)top, (CGFloat)left, (CGFloat)bottom, (CGFloat)right);
}
19 changes: 14 additions & 5 deletions Archimedes/NSValue+MEDGeometryAdditions.h
Expand Up @@ -7,6 +7,7 @@
//

#import <CoreGraphics/CoreGraphics.h>
#import "MEDEdgeInsets.h"

// Boxes a geometry structure.
//
Expand All @@ -21,15 +22,17 @@

// Indicates the type of geometry structure that an NSValue contains.
//
// MEDGeometryStructTypeUnknown - The NSValue contains a value of unknown type.
// MEDGeometryStructTypeRect - The NSValue contains a CGRect.
// MEDGeometryStructTypePoint - The NSValue contains a CGPoint.
// MEDGeometryStructTypeSize - The NSValue contains a CGSize.
// MEDGeometryStructTypeUnknown - The NSValue contains a value of unknown type.
// MEDGeometryStructTypeRect - The NSValue contains a CGRect.
// MEDGeometryStructTypePoint - The NSValue contains a CGPoint.
// MEDGeometryStructTypeSize - The NSValue contains a CGSize.
// MEDGeometryStructTypeEdgeInsets - The NSValue contains an MEDEdgeInsets.
typedef enum : NSUInteger {
MEDGeometryStructTypeUnknown,
MEDGeometryStructTypeRect,
MEDGeometryStructTypePoint,
MEDGeometryStructTypeSize
MEDGeometryStructTypeSize,
MEDGeometryStructTypeEdgeInsets
} MEDGeometryStructType;

// Implements a cross-platform interface for manipulating geometry structures
Expand All @@ -45,6 +48,9 @@ typedef enum : NSUInteger {
// Returns an NSValue wrapping the given size.
+ (NSValue *)med_valueWithSize:(CGSize)size;

// Returns an NSValue wrapping the given edge insets.
+ (NSValue *)med_valueWithEdgeInsets:(MEDEdgeInsets)insets;

// Returns the type of geometry structure stored in the receiver, or
// MEDGeometryStructTypeUnknown if the type can't be identified.
@property (nonatomic, assign, readonly) MEDGeometryStructType med_geometryStructType;
Expand All @@ -58,4 +64,7 @@ typedef enum : NSUInteger {
// Returns the CGSize value in the receiver.
@property (nonatomic, assign, readonly) CGSize med_sizeValue;

// Returns the MEDEdgeInsets value in the receiver.
@property (nonatomic, assign, readonly) MEDEdgeInsets med_edgeInsetsValue;

@end
21 changes: 21 additions & 0 deletions Archimedes/NSValue+MEDGeometryAdditions.m
Expand Up @@ -30,6 +30,10 @@ + (NSValue *)med_valueWithSize:(CGSize)size {
return [self valueWithCGSize:size];
}

+ (NSValue *)med_valueWithEdgeInsets:(MEDEdgeInsets)insets {
return [self valueWithUIEdgeInsets:insets];
}

- (CGRect)med_rectValue {
NSAssert(self.med_geometryStructType == MEDGeometryStructTypeRect, @"Value is not a CGRect: %@", self);
return self.CGRectValue;
Expand All @@ -45,6 +49,11 @@ - (CGSize)med_sizeValue {
return self.CGSizeValue;
}

- (MEDEdgeInsets)med_edgeInsetsValue {
NSAssert(self.med_geometryStructType == MEDGeometryStructTypeEdgeInsets, @"Value is not an MEDEdgeInsets: %@", self);
return self.UIEdgeInsetsValue;
}

#elif TARGET_OS_MAC

+ (NSValue *)med_valueWithRect:(CGRect)rect {
Expand All @@ -59,6 +68,10 @@ + (NSValue *)med_valueWithSize:(CGSize)size {
return [self valueWithSize:size];
}

+ (NSValue *)med_valueWithEdgeInsets:(MEDEdgeInsets)insets {
return [self valueWithBytes:&insets objCType:@encode(MEDEdgeInsets)];
}

- (CGRect)med_rectValue {
NSAssert(self.med_geometryStructType == MEDGeometryStructTypeRect, @"Value is not a CGRect: %@", self);
return self.rectValue;
Expand All @@ -74,6 +87,12 @@ - (CGSize)med_sizeValue {
return self.sizeValue;
}

- (MEDEdgeInsets)med_edgeInsetsValue {
NSAssert(self.med_geometryStructType == MEDGeometryStructTypeEdgeInsets, @"Value is not an MEDEdgeInsets: %@", self);
MEDEdgeInsets insets;
[self getValue:&insets];
return insets;
}
#endif

- (MEDGeometryStructType)med_geometryStructType {
Expand All @@ -85,6 +104,8 @@ - (MEDGeometryStructType)med_geometryStructType {
return MEDGeometryStructTypePoint;
} else if (strcmp(type, @encode(CGSize)) == 0) {
return MEDGeometryStructTypeSize;
} else if (strcmp(type, @encode(MEDEdgeInsets)) == 0) {
return MEDGeometryStructTypeEdgeInsets;
} else {
return MEDGeometryStructTypeUnknown;
}
Expand Down
47 changes: 47 additions & 0 deletions ArchimedesTests/MEDEdgeInsetsSpec.m
@@ -0,0 +1,47 @@
//
// MEDEdgeInsetsSpec.m
// Archimedes
//
// Created by Indragie Karunaratne on 8/6/2013.
// Copyright (c) 2013 GitHub. All rights reserved.
//

#import "MEDEdgeInsets.h"

static const MEDEdgeInsets insets = (MEDEdgeInsets){ .top = 1, .left = 2, .bottom = 3, .right = 4 };
static const MEDEdgeInsets insets2 = (MEDEdgeInsets){ .top = 1.05f, .left = 2.05f, .bottom = 3.05f, .right = 4.05f };

SpecBegin(MEDEdgeInsets)

it(@"should check equality between MEDEdgeInsets", ^{
MEDEdgeInsets insets3 = MEDEdgeInsetsMake(5, 6, 7, 8);
expect(MEDEdgeInsetsEqualToEdgeInsets(insets, insets)).to.beTruthy();
expect(MEDEdgeInsetsEqualToEdgeInsets(insets, insets2)).to.beTruthy();
expect(MEDEdgeInsetsEqualToEdgeInsets(insets, insets3)).to.beFalsy();
});

it(@"should make an MEDEdgeInsets", ^{
expect(MEDEdgeInsetsMake(1, 2, 3, 4)).to.equal(insets);
});

it(@"should inset a CGRect", ^{
CGRect rect = CGRectMake(10, 10, 10, 10);
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
CGRect insetRect = CGRectMake(12, 11, 4, 6);
#elif TARGET_OS_MAC
CGRect insetRect = CGRectMake(12, 13, 4, 6);
#endif
expect(MEDEdgeInsetsInsetRect(rect, insets)).to.equal(insetRect);
});

it(@"should create a string from an MEDEdgeInsets", ^{
expect(NSStringFromMEDEdgeInsets(insets)).to.equal(@"{1, 2, 3, 4}");
expect(NSStringFromMEDEdgeInsets(insets2)).to.equal(@"{1.05, 2.05, 3.05, 4.05}");
});

it(@"should create an MEDEdgeInsets from a string", ^{
expect(MEDEdgeInsetsFromString(@"{1, 2, 3, 4}")).to.equal(insets);
expect(MEDEdgeInsetsEqualToEdgeInsets(MEDEdgeInsetsFromString(@"{1.05, 2.05, 3.05, 4.05}"), insets2)).to.beTruthy();
});

SpecEnd

0 comments on commit 8c8b5c8

Please sign in to comment.