-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSGeometry_SKExtensions.h
209 lines (171 loc) · 6.87 KB
/
NSGeometry_SKExtensions.h
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
//
// NSGeometry_SKExtensions.h
// Skim
//
// Created by Christiaan Hofman on 8/22/07.
/*
This software is Copyright (c) 2007
Christiaan Hofman. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
- Neither the name of Christiaan Hofman nor the names of any
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_OPTIONS(NSUInteger, SKRectEdges) {
SKNoEdgeMask = 0,
SKMinXEdgeMask = 1 << NSMinXEdge,
SKMinYEdgeMask = 1 << NSMinYEdge,
SKMaxXEdgeMask = 1 << NSMaxXEdge,
SKMaxYEdgeMask = 1 << NSMaxYEdge,
SKEveryEdgeMask = SKMinXEdgeMask | SKMinYEdgeMask | SKMaxXEdgeMask | SKMaxYEdgeMask,
};
static inline NSPoint SKIntegralPoint(NSPoint point) {
return NSMakePoint(round(point.x), round(point.y));
}
static inline NSPoint SKAddPoints(NSPoint aPoint, NSPoint bPoint) {
return NSMakePoint(aPoint.x + bPoint.x, aPoint.y + bPoint.y);
}
static inline NSPoint SKSubstractPoints(NSPoint aPoint, NSPoint bPoint) {
return NSMakePoint(aPoint.x - bPoint.x, aPoint.y - bPoint.y);
}
static inline NSPoint SKBottomLeftPoint(NSRect rect) {
return NSMakePoint(NSMinX(rect), NSMinY(rect));
}
static inline NSPoint SKBottomRightPoint(NSRect rect) {
return NSMakePoint(NSMaxX(rect), NSMinY(rect));
}
static inline NSPoint SKTopLeftPoint(NSRect rect) {
return NSMakePoint(NSMinX(rect), NSMaxY(rect));
}
static inline NSPoint SKTopRightPoint(NSRect rect) {
return NSMakePoint(NSMaxX(rect), NSMaxY(rect));
}
static inline NSPoint SKCenterPoint(NSRect rect) {
return NSMakePoint(NSMidX(rect), NSMidY(rect));
}
static inline NSSize SKMakeSquareSize(CGFloat width) {
return NSMakeSize(width, width);
}
static inline NSRect SKRectFromPoints(NSPoint aPoint, NSPoint bPoint) {
NSRect rect;
rect.origin.x = fmin(aPoint.x, bPoint.x);
rect.origin.y = fmin(aPoint.y, bPoint.y);
rect.size.width = fmax(aPoint.x, bPoint.x) - NSMinX(rect);
rect.size.height = fmax(aPoint.y, bPoint.y) - NSMinY(rect);
return rect;
}
static inline NSRect SKIntegralRectFromPoints(NSPoint aPoint, NSPoint bPoint) {
NSRect rect;
rect.origin.x = floor(fmin(aPoint.x, bPoint.x));
rect.origin.y = floor(fmin(aPoint.y, bPoint.y));
rect.size.width = ceil(fmax(aPoint.x, bPoint.x) - NSMinX(rect));
rect.size.height = ceil(fmax(aPoint.y, bPoint.y) - NSMinY(rect));
return rect;
}
static inline NSRect SKRectFromCenterAndPoint(NSPoint center, NSPoint point) {
NSRect rect;
rect.size.width = 2.0 * fabs(center.x - point.x);
rect.size.height = 2.0 * fabs(center.y - point.y);
rect.origin.x = center.x - 0.5 * NSWidth(rect);
rect.origin.y = center.y - 0.5 * NSHeight(rect);
return rect;
}
static inline NSRect SKRectFromCenterAndSize(NSPoint center, NSSize size) {
NSRect rect;
rect.origin.x = center.x - 0.5 * size.width;
rect.origin.y = center.y - 0.5 * size.height;
rect.size = size;
return rect;
}
static inline NSRect SKRectFromCenterAndSquareSize(NSPoint center, CGFloat size) {
NSRect rect;
rect.origin.x = center.x - 0.5 * size;
rect.origin.y = center.y - 0.5 * size;
rect.size.width = size;
rect.size.height = size;
return rect;
}
static inline NSRect SKSliceRect(NSRect rect, CGFloat amount, NSRectEdge edge) {
NSRect ignored;
NSDivideRect(rect, &rect, &ignored, amount, edge);
return rect;
}
static inline NSRect SKShrinkRect(NSRect rect, CGFloat amount, NSRectEdge edge) {
NSRect ignored;
NSDivideRect(rect, &ignored, &rect, amount, edge);
return rect;
}
static inline NSRect SKIntegralRect(NSRect rect, CGFloat scale) {
NSRect r;
r.origin.x = round(NSMinX(rect) * scale) / scale;
r.origin.y = round(NSMinY(rect) * scale) / scale;
r.size.width = round(NSMaxX(rect) * scale) / scale - NSMinX(r);
r.size.height = round(NSMaxY(rect) * scale) / scale - NSMinY(r);
return NSWidth(r) > 0.0 && NSHeight(r) > 0.0 ? r : NSZeroRect;
}
static inline NSRect SKTransformRect(NSAffineTransform *transform, NSRect rect) {
return SKRectFromPoints([transform transformPoint:SKBottomLeftPoint(rect)], [transform transformPoint:SKTopRightPoint(rect)]);
}
#pragma mark -
extern NSPoint SKConstrainPointInRect(NSPoint point, NSRect boundary);
extern NSRect SKConstrainRect(NSRect rect, NSRect boundary);
extern NSRect SKIntersectionRect(NSRect rect, NSRect boundary);
extern NSRect SKCenterRectVertically(NSRect rect, CGFloat height, CGFloat offset, BOOL flipped);
#pragma mark -
extern BOOL SKPointNearLineFromPointToPoint(NSPoint point, NSPoint startPoint, NSPoint endPoint, CGFloat delta);
extern SKRectEdges SKResizeHandleForPointFromRect(NSPoint point, NSRect rect, CGFloat delta);
#pragma mark -
static inline
Rect SKQDRectFromNSRect(NSRect nsRect) {
Rect qdRect;
qdRect.left = (short)round(NSMinX(nsRect));
qdRect.bottom = (short)round(NSMinY(nsRect));
qdRect.right = (short)round(NSMaxX(nsRect));
qdRect.top = (short)round(NSMaxY(nsRect));
return qdRect;
}
static inline
NSRect SKNSRectFromQDRect(Rect qdRect) {
NSRect nsRect;
nsRect.origin.x = (CGFloat)qdRect.left;
nsRect.origin.y = (CGFloat)qdRect.bottom;
nsRect.size.width = (CGFloat)(qdRect.right - qdRect.left);
nsRect.size.height = (CGFloat)(qdRect.top - qdRect.bottom);
return nsRect;
}
static inline
Point SKQDPointFromNSPoint(NSPoint nsPoint) {
Point qdPoint;
qdPoint.h = (short)round(nsPoint.x);
qdPoint.v = (short)round(nsPoint.y);
return qdPoint;
}
static inline
NSPoint SKNSPointFromQDPoint(Point qdPoint) {
NSPoint nsPoint;
nsPoint.x = (CGFloat)qdPoint.h;
nsPoint.y = (CGFloat)qdPoint.v;
return nsPoint;
}
NS_ASSUME_NONNULL_END