forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSNSValue.m
237 lines (211 loc) · 7.7 KB
/
FSNSValue.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* FSNSValue.m Copyright (c) 2003-2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSNSValue.h"
#import "FSNSObject.h"
#import "FSNumber.h"
#import "FScriptFunctions.h"
#import "FSBooleanPrivate.h"
@implementation NSValue (FSNSValue)
/////////////////////////// USER METHODS ////////////////////////////
+ (NSRange)rangeWithLocation:(NSUInteger)location length:(NSUInteger)length
{
return NSMakeRange(location,length);
}
+ (NSSize)sizeWithWidth:(CGFloat)width height:(CGFloat)height
{
return NSMakeSize(width, height);
}
- (id)clone { return [[self copy] autorelease];}
- (NSPoint)corner
{
if (strcmp([self objCType],@encode(NSRect)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"corner\" sent to a number");
else
FSExecError(@"message \"corner\" sent to an NSValue that does not contain an NSRect");
}
else
{
NSRect rectValue = [self rectValue];
return NSMakePoint(rectValue.origin.x + rectValue.size.width, rectValue.origin.y + rectValue.size.height);
}
}
- (NSRect)corner:(NSPoint)operand
{
if (strcmp([self objCType],@encode(NSPoint)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"corner:\" sent to a number");
else
FSExecError(@"message \"corner:\" sent to an NSValue that does not contain an NSPoint");
}
else
{
NSPoint pointValue = [self pointValue];
if (operand.x < pointValue.x || operand.y < pointValue.y) FSExecError(@"argument 1 of method \"corner:\" must be a valid corner");
return NSMakeRect(pointValue.x,pointValue.y,operand.x - pointValue.x,operand.y - pointValue.y);
}
}
- (NSPoint)extent
{
if (strcmp([self objCType],@encode(NSRect)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"extent\" sent to a number");
else
FSExecError(@"message \"extent\" sent to an NSValue that does not contain an NSRect");
}
else
{
NSRect rectValue = [self rectValue];
return NSMakePoint(rectValue.size.width,rectValue.size.height);
}
}
- (NSRect)extent:(NSPoint)operand
{
if (strcmp([self objCType],@encode(NSPoint)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"extent:\" sent to a number");
else
FSExecError(@"message \"extent:\" sent to an NSValue that does not contain an NSPoint");
}
else
{
if (operand.x < 0 || operand.y < 0) FSExecError(@"argument 1 of method \"extent:\" must be a point with no negative coordinate");
NSPoint pointValue = [self pointValue];
return NSMakeRect(pointValue.x,pointValue.y,operand.x,operand.y);
}
}
- (CGFloat)height
{
if (strcmp([self objCType],@encode(NSSize)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"height\" sent to a number");
else
FSExecError(@"message \"height\" sent to an NSValue that does not contain an NSSize");
}
else return [self sizeValue].height;
}
- (NSUInteger)length
{
if (strcmp([self objCType],@encode(NSRange)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"length\" sent to a number");
else
FSExecError(@"message \"length\" sent to an NSValue that does not contain an NSRange");
}
else return [self rangeValue].length;
}
- (NSUInteger)location
{
if (strcmp([self objCType],@encode(NSRange)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"location\" sent to a number");
else
FSExecError(@"message \"location\" sent to an NSValue that does not contain an NSRange");
}
else return [self rangeValue].location;
}
- (NSPoint)origin
{
if (strcmp([self objCType],@encode(NSRect)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"origin\" sent to a number");
else
FSExecError(@"message \"origin\" sent to an NSValue that does not contain an NSRect");
}
else return [self rectValue].origin;
}
- (FSBoolean *)operator_equal:(id)operand
{
return ([self isEqual:operand] ? fsTrue : fsFalse);
}
- (FSBoolean *)operator_tilde_equal:(id)operand
{
return (![self isEqual:operand] ? fsTrue : fsFalse);
}
- (NSString *)printString
{
const char *objCType = [self objCType];
if (strcmp(objCType,@encode(NSPoint)) == 0)
{
NSPoint pointValue = [self pointValue];
BOOL yIsNegativeZero = [[[FSNumber numberWithDouble:pointValue.y] printString] isEqualToString:@"-0"];
if (pointValue.y >= 0 && ! yIsNegativeZero)
return [NSString stringWithFormat:@"(%@<>%@)",[FSNumber numberWithDouble:pointValue.x],[FSNumber numberWithDouble:pointValue.y]];
else
return [NSString stringWithFormat:@"(%@ <> %@)",[FSNumber numberWithDouble:pointValue.x],[FSNumber numberWithDouble:pointValue.y]];
}
else if (strcmp(objCType,@encode(NSRange)) == 0)
{
NSRange rangeValue = [self rangeValue];
return [NSString stringWithFormat:@"(Range location = %lu length = %lu)",(unsigned long)(rangeValue.location),(unsigned long)(rangeValue.length)];
}
else if (strcmp(objCType,@encode(NSRect)) == 0)
{
NSRect rectValue = [self rectValue];
CGFloat originX = rectValue.origin.x;
CGFloat originY = rectValue.origin.y;
CGFloat width = rectValue.size.width;
CGFloat height = rectValue.size.height;
BOOL originYIsNegativeZero = [[[FSNumber numberWithDouble:originY] printString] isEqualToString:@"-0"];
BOOL heightIsNegativeZero = [[[FSNumber numberWithDouble:height] printString] isEqualToString:@"-0"];
NSString *formatString;
if (originY >= 0 && !originYIsNegativeZero && height >= 0 && !heightIsNegativeZero) formatString = @"(%@<>%@ extent:%@<>%@)";
else if ((originY < 0 || originYIsNegativeZero) && height >= 0 && !heightIsNegativeZero) formatString = @"(%@ <> %@ extent:%@<>%@)";
else if (originY >= 0 && !originYIsNegativeZero && (height < 0 || heightIsNegativeZero)) formatString = @"(%@<>%@ extent:%@ <> %@)";
else formatString = @"(%@ <> %@ extent:%@ <> %@)";
return [NSString stringWithFormat:formatString, [FSNumber numberWithDouble:originX], [FSNumber numberWithDouble:originY], [FSNumber numberWithDouble:width], [FSNumber numberWithDouble:height]];
}
else if (strcmp(objCType,@encode(NSSize)) == 0)
{
NSSize sizeValue = [self sizeValue];
return [NSString stringWithFormat:@"(Size width = %@ height = %@)", [FSNumber numberWithDouble:sizeValue.width], [FSNumber numberWithDouble:sizeValue.height]];
}
return [super printString];
}
/*- (NSSize)size
{
if (strcmp([self objCType],@encode(NSRect)) != 0) FSExecError(@"Receiver of message \"size\" must be an NSValue containing an NSRect");
return [self rectValue].size;
}*/
- (CGFloat)width
{
if (strcmp([self objCType],@encode(NSSize)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"width\" sent to a number");
else
FSExecError(@"message \"width\" sent to an NSValue that does not contain an NSSize");
}
else return [self sizeValue].width;
}
- (CGFloat)x
{
if (strcmp([self objCType],@encode(NSPoint)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"x\" sent to a number");
else
FSExecError(@"message \"x\" sent to an NSValue that does not contain an NSPoint");
}
else return [self pointValue].x;
}
- (CGFloat)y
{
if (strcmp([self objCType],@encode(NSPoint)) != 0)
{
if ([self isKindOfClass:[NSNumber class]])
FSExecError(@"message \"y\" sent to a number");
else
FSExecError(@"message \"y\" sent to an NSValue that does not contain an NSPoint");
}
else return [self pointValue].y;
}
@end