This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathregion.cpp
More file actions
269 lines (193 loc) · 6.09 KB
/
region.cpp
File metadata and controls
269 lines (193 loc) · 6.09 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "graphics.h"
#include "graphics-internal.h"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool MCGRegionCreate(MCGRegionRef &r_region)
{
bool t_success;
t_success = true;
MCGRegionRef t_region;
t_region = nil;
if (t_success)
t_success = MCMemoryCreate(t_region);
if (t_success)
r_region = t_region;
else
MCGRegionDestroy(t_region);
return t_success;
}
void MCGRegionDestroy(MCGRegionRef p_region)
{
if (p_region == nil)
return;
MCMemoryDestroy(p_region);
}
bool MCGRegionCopy(MCGRegionRef p_region, MCGRegionRef &r_copy)
{
if (p_region == nil)
return false;
bool t_success;
t_success = true;
MCGRegionRef t_copy;
t_copy = nil;
if (t_success)
t_success = MCGRegionCreate(t_copy) && MCGRegionSetRegion(t_copy, p_region);
if (t_success)
r_copy = t_copy;
else
MCGRegionDestroy(t_copy);
return t_success;
}
//////////
bool MCGRegionIsEmpty(MCGRegionRef p_region)
{
return p_region == nil || p_region->region.isEmpty();
}
//////////
MCGIntegerRectangle MCGRegionGetBounds(MCGRegionRef p_region)
{
if (p_region == nil)
return MCGIntegerRectangleMake(0, 0, 0, 0);
SkIRect t_irect;
t_irect = p_region->region.getBounds();
return MCGIntegerRectangleMake(t_irect.x(), t_irect.y(), t_irect.width(), t_irect.height());
}
//////////
bool MCGRegionSetEmpty(MCGRegionRef p_region)
{
if (p_region == nil)
return false;
p_region->region.setEmpty();
return true;
}
bool MCGRegionSetRect(MCGRegionRef p_region, const MCGIntegerRectangle &p_rect)
{
if (p_region == nil)
return false;
p_region->region.setRect(p_rect.origin.x, p_rect.origin.y, p_rect.origin.x + p_rect.size.width, p_rect.origin.y + p_rect.size.height);
return true;
}
bool MCGRegionSetRegion(MCGRegionRef p_region, MCGRegionRef p_other)
{
if (p_region == nil || p_other == nil)
return false;
p_region->region.setRegion(p_other->region);
return true;
}
//////////
bool MCGRegionAddRect(MCGRegionRef p_region, const MCGIntegerRectangle &p_rect)
{
if (p_region == nil)
return false;
p_region->region.op(p_rect.origin.x, p_rect.origin.y, p_rect.origin.x + p_rect.size.width, p_rect.origin.y + p_rect.size.height, SkRegion::kUnion_Op);
return true;
}
bool MCGRegionAddRegion(MCGRegionRef p_region, MCGRegionRef p_other)
{
if (p_region == nil || p_other == nil)
return false;
p_region->region.op(p_other->region, SkRegion::kUnion_Op);
return true;
}
bool MCGRegionIntersectRect(MCGRegionRef p_region, const MCGIntegerRectangle &p_rect)
{
if (p_region == nil)
return false;
p_region->region.op(p_rect.origin.x, p_rect.origin.y, p_rect.origin.x + p_rect.size.width, p_rect.origin. y + p_rect.size.height, SkRegion::kIntersect_Op);
return true;
}
bool MCGRegionIntersectRegion(MCGRegionRef p_region, MCGRegionRef p_other)
{
if (p_region == nil || p_other == nil)
return false;
p_region->region.op(p_other->region, SkRegion::kIntersect_Op);
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool MCGRegionTranslate(MCGRegionRef p_region, int32_t p_dx, int32_t p_dy)
{
if (p_region == nil)
return false;
p_region->region.translate(p_dx, p_dy);
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool MCGRegionIterate(MCGRegionRef p_region, MCGRegionIterateCallback p_callback, void *p_context)
{
if (p_region == nil)
return false;
bool t_success;
t_success = true;
SkRegion::Iterator t_iterator(p_region->region);
while (t_success && !t_iterator.done())
{
const SkIRect &t_rect = t_iterator.rect();
t_success = p_callback(p_context, MCGIntegerRectangleMake(t_rect.x(), t_rect.y(), t_rect.width(), t_rect.height()));
t_iterator.next();
}
return t_success;
}
////////////////////////////////////////////////////////////////////////////////
struct MCGRegionCopyWithTransformIterateContext
{
MCGRegionRef region;
MCGAffineTransform transform;
};
bool MCGRegionCopyWithTransformIterateCallback(void *p_context, const MCGIntegerRectangle &p_rect)
{
MCGRegionCopyWithTransformIterateContext *t_context;
t_context = static_cast<MCGRegionCopyWithTransformIterateContext*>(p_context);
return MCGRegionAddRect(t_context->region, MCGIntegerRectangleGetTransformedBounds(p_rect, t_context->transform));
}
bool MCGRegionCopyWithTransform(MCGRegionRef p_region, const MCGAffineTransform &p_transform, MCGRegionRef &r_copy)
{
if (p_region == nil)
return false;
MCGRegionRef t_copy;
t_copy = nil;
if (!MCGRegionCreate(t_copy))
return false;
if (MCGAffineTransformIsRectangular(p_transform))
{
MCGRegionCopyWithTransformIterateContext t_context;
t_context.region = t_copy;
t_context.transform = p_transform;
if (MCGRegionIterate(p_region, MCGRegionCopyWithTransformIterateCallback, &t_context))
{
r_copy = t_copy;
return true;
}
else
{
MCGRegionDestroy(t_copy);
return false;
}
}
else
{
SkMatrix t_matrix;
MCGAffineTransformToSkMatrix(p_transform, t_matrix);
SkPath t_path;
p_region->region.getBoundaryPath(&t_path);
t_path.transform(t_matrix);
SkRect t_bounds;
t_bounds = t_path.getBounds();
SkRegion t_clip;
t_clip.setRect(floorf(t_bounds.left()), floorf(t_bounds.top()), ceilf(t_bounds.right()), ceilf(t_bounds.bottom()));
t_copy->region.setPath(t_path, t_clip);
r_copy = t_copy;
return true;
}
}
////////////////////////////////////////////////////////////////////////////////