1 /* EINA - EFL data type library
2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef EINA_INLINE_RECTANGLE_H__
20 #define EINA_INLINE_RECTANGLE_H__
21
22 static inline Eina_Bool
eina_rectangle_is_valid(const Eina_Rectangle * r)23 eina_rectangle_is_valid(const Eina_Rectangle *r)
24 {
25 if ((r->w > 0) && (r->h > 0)) return EINA_TRUE;
26 return EINA_FALSE;
27 }
28
29 static inline int
eina_spans_intersect(int c1,int l1,int c2,int l2)30 eina_spans_intersect(int c1, int l1, int c2, int l2)
31 {
32 return (!(((c2 + l2) <= c1) || (c2 >= (c1 + l1))));
33 }
34
35 static inline Eina_Bool
eina_rectangle_is_empty(const Eina_Rectangle * r)36 eina_rectangle_is_empty(const Eina_Rectangle *r)
37 {
38 return !eina_rectangle_is_valid(r);
39 }
40
41 static inline void
eina_rectangle_coords_from(Eina_Rectangle * r,int x,int y,int w,int h)42 eina_rectangle_coords_from(Eina_Rectangle *r, int x, int y, int w, int h)
43 {
44 r->x = x;
45 r->y = y;
46 r->w = w;
47 r->h = h;
48 }
49
50 static inline Eina_Bool
eina_rectangle_equal(const Eina_Rectangle * rect1,const Eina_Rectangle * rect2)51 eina_rectangle_equal(const Eina_Rectangle *rect1, const Eina_Rectangle *rect2)
52 {
53 return ((rect1->x == rect2->x) && (rect1->y == rect2->y) &&
54 (rect1->w == rect2->w) && (rect1->h == rect2->h));
55 }
56
57 static inline Eina_Bool
eina_rectangles_intersect(const Eina_Rectangle * r1,const Eina_Rectangle * r2)58 eina_rectangles_intersect(const Eina_Rectangle *r1, const Eina_Rectangle *r2)
59 {
60 return (eina_spans_intersect(r1->x, r1->w, r2->x, r2->w) && eina_spans_intersect(r1->y, r1->h, r2->y, r2->h)) ? EINA_TRUE : EINA_FALSE;
61 }
62
63 static inline Eina_Bool
eina_rectangle_xcoord_inside(const Eina_Rectangle * r,int x)64 eina_rectangle_xcoord_inside(const Eina_Rectangle *r, int x)
65 {
66 return ((x >= r->x) && (x < (r->x + r->w))) ? EINA_TRUE : EINA_FALSE;
67 }
68
69 static inline Eina_Bool
eina_rectangle_ycoord_inside(const Eina_Rectangle * r,int y)70 eina_rectangle_ycoord_inside(const Eina_Rectangle *r, int y)
71 {
72 return ((y >= r->y) && (y < (r->y + r->h))) ? EINA_TRUE : EINA_FALSE;
73 }
74
75 static inline Eina_Bool
eina_rectangle_coords_inside(const Eina_Rectangle * r,int x,int y)76 eina_rectangle_coords_inside(const Eina_Rectangle *r, int x, int y)
77 {
78 return (eina_rectangle_xcoord_inside(r, x) && eina_rectangle_ycoord_inside(r, y)) ? EINA_TRUE : EINA_FALSE;
79 }
80
81 static inline void
eina_rectangle_union(Eina_Rectangle * dst,const Eina_Rectangle * src)82 eina_rectangle_union(Eina_Rectangle *dst, const Eina_Rectangle *src)
83 {
84 /* left */
85 if (dst->x > src->x)
86 {
87 dst->w += dst->x - src->x;
88 dst->x = src->x;
89 }
90 /* right */
91 if ((dst->x + dst->w) < (src->x + src->w))
92 dst->w = src->x + src->w - dst->x;
93 /* top */
94 if (dst->y > src->y)
95 {
96 dst->h += dst->y - src->y;
97 dst->y = src->y;
98 }
99 /* bottom */
100 if ((dst->y + dst->h) < (src->y + src->h))
101 dst->h = src->y + src->h - dst->y;
102 }
103
104 static inline Eina_Bool
eina_rectangle_intersection(Eina_Rectangle * dst,const Eina_Rectangle * src)105 eina_rectangle_intersection(Eina_Rectangle *dst, const Eina_Rectangle *src)
106 {
107 if (eina_rectangle_is_valid(dst) && eina_rectangle_is_valid(src) &&
108 eina_rectangles_intersect(dst, src))
109 {
110 /* left */
111 if (dst->x < src->x)
112 {
113 dst->w += dst->x - src->x;
114 dst->x = src->x;
115 if (dst->w < 0) dst->w = 0;
116 }
117 /* right */
118 if ((dst->x + dst->w) > (src->x + src->w))
119 dst->w = src->x + src->w - dst->x;
120 /* top */
121 if (dst->y < src->y)
122 {
123 dst->h += dst->y - src->y;
124 dst->y = src->y;
125 if (dst->h < 0) dst->h = 0;
126 }
127 /* bottom */
128 if ((dst->y + dst->h) > (src->y + src->h))
129 dst->h = src->y + src->h - dst->y;
130
131 if ((dst->w == 0) || (dst->h == 0)) return EINA_FALSE;
132 return EINA_TRUE;
133 }
134 return EINA_FALSE;
135 }
136
137 static inline void
eina_rectangle_rescale_in(const Eina_Rectangle * out,const Eina_Rectangle * in,Eina_Rectangle * res)138 eina_rectangle_rescale_in(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
139 {
140 res->x = in->x - out->x;
141 res->y = in->y - out->y;
142 res->w = in->w;
143 res->h = in->h;
144 }
145
146 static inline void
eina_rectangle_rescale_out(const Eina_Rectangle * out,const Eina_Rectangle * in,Eina_Rectangle * res)147 eina_rectangle_rescale_out(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
148 {
149 res->x = out->x + in->x;
150 res->y = out->y + in->y;
151 res->w = out->w;
152 res->h = out->h;
153 }
154
155 static inline int
eina_rectangle_max_x(Eina_Rectangle * thiz)156 eina_rectangle_max_x(Eina_Rectangle *thiz)
157 {
158 return thiz->x + thiz->w;
159 }
160
161 static inline int
eina_rectangle_max_y(Eina_Rectangle * thiz)162 eina_rectangle_max_y(Eina_Rectangle *thiz)
163 {
164 return thiz->y + thiz->h;
165 }
166
167 static inline Eina_Bool
eina_rectangle_x_cut(Eina_Rectangle * thiz,Eina_Rectangle * slice,Eina_Rectangle * leftover,int amount)168 eina_rectangle_x_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
169 {
170 Eina_Rectangle tmp1, tmp2;
171 if (amount > thiz->w)
172 return EINA_FALSE;
173 eina_rectangle_coords_from(&tmp1, thiz->x, thiz->y, amount, thiz->h);
174 eina_rectangle_coords_from(&tmp2, thiz->x + amount, thiz->y, thiz->w - amount, thiz->h);
175 if (slice) *slice = tmp1;
176 if (leftover) *leftover = tmp2;
177 return EINA_TRUE;
178 }
179
180 static inline Eina_Bool
eina_rectangle_y_cut(Eina_Rectangle * thiz,Eina_Rectangle * slice,Eina_Rectangle * leftover,int amount)181 eina_rectangle_y_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
182 {
183 Eina_Rectangle tmp1, tmp2;
184 if (amount > thiz->h)
185 return EINA_FALSE;
186 eina_rectangle_coords_from(&tmp1, thiz->x, thiz->y, thiz->w, amount);
187 eina_rectangle_coords_from(&tmp2, thiz->x, thiz->y + amount, thiz->w, thiz->h - amount);
188 if (slice) *slice = tmp1;
189 if (leftover) *leftover = tmp2;
190 return EINA_TRUE;
191 }
192
193 static inline Eina_Bool
eina_rectangle_width_cut(Eina_Rectangle * thiz,Eina_Rectangle * slice,Eina_Rectangle * leftover,int amount)194 eina_rectangle_width_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
195 {
196 Eina_Rectangle tmp1, tmp2;
197 if (thiz->w - amount < 0)
198 return EINA_FALSE;
199 eina_rectangle_coords_from(&tmp1, thiz->x + (thiz->w - amount), thiz->y, amount, thiz->h);
200 eina_rectangle_coords_from(&tmp2, thiz->x, thiz->y, thiz->w - amount, thiz->h);
201 if (slice) *slice = tmp1;
202 if (leftover) *leftover = tmp2;
203 return EINA_TRUE;
204 }
205
206 static inline Eina_Bool
eina_rectangle_height_cut(Eina_Rectangle * thiz,Eina_Rectangle * slice,Eina_Rectangle * leftover,int amount)207 eina_rectangle_height_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
208 {
209 Eina_Rectangle tmp1, tmp2;
210 if (thiz->h - amount < 0)
211 return EINA_FALSE;
212 eina_rectangle_coords_from(&tmp1, thiz->x, thiz->y + (thiz->h - amount), thiz->w, amount);
213 eina_rectangle_coords_from(&tmp2, thiz->x, thiz->y, thiz->w, thiz->h - amount);
214 if (slice) *slice = tmp1;
215 if (leftover) *leftover = tmp2;
216 return EINA_TRUE;
217 }
218
219 static inline Eina_Bool
eina_rectangle_subtract(Eina_Rectangle * thiz,Eina_Rectangle * other,Eina_Rectangle out[4])220 eina_rectangle_subtract(Eina_Rectangle *thiz, Eina_Rectangle *other, Eina_Rectangle out[4])
221 {
222 Eina_Rectangle intersection;
223 Eina_Rectangle leftover = EINA_RECTANGLE_INIT;
224 Eina_Rectangle tmp;
225 int cut = 0;
226
227 if (eina_rectangle_is_valid(thiz))
228 {
229 eina_rectangle_coords_from(&out[0], 0, 0, 0, 0);
230 eina_rectangle_coords_from(&out[1], 0, 0, 0, 0);
231 eina_rectangle_coords_from(&out[2], 0, 0, 0, 0);
232 eina_rectangle_coords_from(&out[3], 0, 0, 0, 0);
233 intersection = *thiz;
234 if (!eina_rectangle_intersection(&intersection, other))
235 {
236 out[0] = *thiz;
237 return EINA_TRUE;
238 }
239
240 /* cut in height */
241 {
242 cut = thiz->h - (intersection.y - thiz->y);
243 if (cut > thiz->h) cut = thiz->h;
244 eina_rectangle_height_cut(thiz, &leftover, &out[0], cut);
245 }
246 /* cut in y */
247 tmp = leftover;
248 if (eina_rectangle_intersection(&tmp, &intersection))
249 {
250 cut = leftover.h - (eina_rectangle_max_y(&leftover) - eina_rectangle_max_y(&tmp));
251 if (cut > leftover.h) cut = leftover.h;
252 eina_rectangle_y_cut(&leftover, &leftover, &out[1], cut);
253 }
254 /* cut in width */
255 tmp = leftover;
256 if (eina_rectangle_intersection(&tmp, &intersection))
257 {
258 cut = leftover.w - (tmp.x - leftover.x);
259 if (cut > leftover.w) cut = leftover.w;
260 eina_rectangle_width_cut(&leftover, &leftover, &out[2], cut);
261 }
262 /* cut in x */
263 tmp = leftover;
264 if (eina_rectangle_intersection(&tmp, &intersection))
265 {
266 cut = leftover.w - (eina_rectangle_max_x(&leftover) - eina_rectangle_max_x(&tmp));
267 if (cut > leftover.w) cut = leftover.w;
268 eina_rectangle_x_cut(&leftover, &leftover, &out[3], cut);
269 }
270 return EINA_TRUE;
271 }
272 return EINA_FALSE;
273 }
274
275 #endif
276