summaryrefslogtreecommitdiff
path: root/eel/eel-art-extensions.c
blob: 13bdefb85b47cab45d3d9efe00c893b4d4db258a (plain)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* eel-art-extensions.c - implementation of libart extension functions.

   Copyright (C) 2000 Eazel, Inc.

   The Mate Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Mate Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Mate Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
   Boston, MA 02110-1301, USA.

   Authors: Darin Adler <darin@eazel.com>
            Ramiro Estrugo <ramiro@eazel.com>
*/

#include <config.h>

#include "eel-art-extensions.h"
#include "eel-lib-self-check-functions.h"
#include <math.h>

const EelDRect eel_drect_empty = { 0.0, 0.0, 0.0, 0.0 };
const EelIRect eel_irect_empty = { 0, 0, 0, 0 };
const EelIPoint eel_ipoint_max = { G_MAXINT, G_MAXINT };
const EelIPoint eel_ipoint_min = { G_MININT, G_MININT };
const EelIPoint eel_ipoint_zero = { 0, 0 };
const EelDimensions eel_dimensions_empty = { 0, 0 };

void
eel_irect_copy (EelIRect *dest, const EelIRect *src)
{
    dest->x0 = src->x0;
    dest->y0 = src->y0;
    dest->x1 = src->x1;
    dest->y1 = src->y1;
}

void
eel_irect_union (EelIRect *dest,
                 const EelIRect *src1,
                 const EelIRect *src2)
{
    if (eel_irect_is_empty (src1))
    {
        eel_irect_copy (dest, src2);
    }
    else if (eel_irect_is_empty (src2))
    {
        eel_irect_copy (dest, src1);
    }
    else
    {
        dest->x0 = MIN (src1->x0, src2->x0);
        dest->y0 = MIN (src1->y0, src2->y0);
        dest->x1 = MAX (src1->x1, src2->x1);
        dest->y1 = MAX (src1->y1, src2->y1);
    }
}

void
eel_irect_intersect (EelIRect *dest,
                     const EelIRect *src1,
                     const EelIRect *src2)
{
    dest->x0 = MAX (src1->x0, src2->x0);
    dest->y0 = MAX (src1->y0, src2->y0);
    dest->x1 = MIN (src1->x1, src2->x1);
    dest->y1 = MIN (src1->y1, src2->y1);
}

gboolean
eel_irect_is_empty (const EelIRect *src)
{
    return (src->x1 <= src->x0 ||
            src->y1 <= src->y0);
}

EelIRect
eel_irect_assign (int x,
                  int y,
                  int width,
                  int height)
{
    EelIRect rectangle;

    rectangle.x0 = x;
    rectangle.y0 = y;
    rectangle.x1 = rectangle.x0 + width;
    rectangle.y1 = rectangle.y0 + height;

    return rectangle;
}

/**
 * eel_irect_assign_dimensions:
 *
 * @x: X coodinate for resulting rectangle.
 * @y: Y coodinate for resulting rectangle.
 * @dimensions: A EelDimensions structure for the rect's width and height.
 *
 * Returns: An EelIRect with the given coordinates and dimensions.
 */
EelIRect
eel_irect_assign_dimensions (int x,
                             int y,
                             EelDimensions dimensions)
{
    EelIRect rectangle;

    rectangle.x0 = x;
    rectangle.y0 = y;
    rectangle.x1 = rectangle.x0 + dimensions.width;
    rectangle.y1 = rectangle.y0 + dimensions.height;

    return rectangle;
}

/**
 * eel_irect_get_width:
 *
 * @rectangle: An EelIRect.
 *
 * Returns: The width of the rectangle.
 *
 */
int
eel_irect_get_width (EelIRect rectangle)
{
    return rectangle.x1 - rectangle.x0;
}

/**
 * eel_irect_get_height:
 *
 * @rectangle: An EelIRect.
 *
 * Returns: The height of the rectangle.
 *
 */
int
eel_irect_get_height (EelIRect rectangle)
{
    return rectangle.y1 - rectangle.y0;
}

static void
eel_drect_copy (EelDRect *dest,
                const EelDRect *src)
{
    dest->x0 = src->x0;
    dest->y0 = src->y0;
    dest->x1 = src->x1;
    dest->y1 = src->y1;
}

static gboolean
eel_drect_is_empty (const EelDRect *src)
{
    return (src->x1 <= src->x0 || src->y1 <= src->y0);
}

void
eel_drect_union (EelDRect *dest,
                 const EelDRect *src1,
                 const EelDRect *src2)
{
    if (eel_drect_is_empty (src1))
    {
        eel_drect_copy (dest, src2);
    }
    else if (eel_drect_is_empty (src2))
    {
        eel_drect_copy (dest, src1);
    }
    else
    {
        dest->x0 = MIN (src1->x0, src2->x0);
        dest->y0 = MIN (src1->y0, src2->y0);
        dest->x1 = MAX (src1->x1, src2->x1);
        dest->y1 = MAX (src1->y1, src2->y1);
    }
}

/**
 * eel_irect_contains_point:
 *
 * @rectangle: An EelIRect.
 * @x: X coordinate to test.
 * @y: Y coordinate to test.
 *
 * Returns: A boolean value indicating whether the rectangle
 *          contains the x,y coordinate.
 *
 */
gboolean
eel_irect_contains_point (EelIRect rectangle,
                          int x,
                          int y)
{
    return x >= rectangle.x0
           && x <= rectangle.x1
           && y >= rectangle.y0
           && y <= rectangle.y1;
}

gboolean
eel_irect_hits_irect (EelIRect rectangle_a,
                      EelIRect rectangle_b)
{
    EelIRect intersection;
    eel_irect_intersect (&intersection, &rectangle_a, &rectangle_b);
    return !eel_irect_is_empty (&intersection);
}

gboolean
eel_irect_equal (EelIRect rectangle_a,
                 EelIRect rectangle_b)
{
    return rectangle_a.x0 == rectangle_b.x0
           && rectangle_a.y0 == rectangle_b.y0
           && rectangle_a.x1 == rectangle_b.x1
           && rectangle_a.y1 == rectangle_b.y1;
}

/**
 * eel_irect_align:
 *
 * @container: The rectangle that is to contain the aligned rectangle.
 * @aligned_width: Width of rectangle being algined.
 * @aligned_height: Height of rectangle being algined.
 * @x_alignment: X alignment.
 * @y_alignment: Y alignment.
 *
 * Returns: A rectangle aligned within a container rectangle
 *          using the given alignment parameters.
 */
EelIRect
eel_irect_align (EelIRect container,
                 int aligned_width,
                 int aligned_height,
                 float x_alignment,
                 float y_alignment)
{
    EelIRect aligned;
    int available_width;
    int available_height;

    if (eel_irect_is_empty (&container))
    {
        return eel_irect_empty;
    }

    if (aligned_width == 0 || aligned_height == 0)
    {
        return eel_irect_empty;
    }

    /* Make sure the aligment parameters are within range */
    x_alignment = MAX (0, x_alignment);
    x_alignment = MIN (1.0, x_alignment);
    y_alignment = MAX (0, y_alignment);
    y_alignment = MIN (1.0, y_alignment);

    available_width = eel_irect_get_width (container) - aligned_width;
    available_height = eel_irect_get_height (container) - aligned_height;

    aligned.x0 = floor (container.x0 + (available_width * x_alignment) + 0.5);
    aligned.y0 = floor (container.y0 + (available_height * y_alignment) + 0.5);
    aligned.x1 = aligned.x0 + aligned_width;
    aligned.y1 = aligned.y0 + aligned_height;

    return aligned;
}

/**
 * eel_dimensions_are_empty:
 *
 * @dimensions: A EelDimensions structure.
 *
 * Returns: Whether the dimensions are empty.
 */
gboolean
eel_dimensions_are_empty (EelDimensions dimensions)
{
    return dimensions.width <= 0 || dimensions.height <= 0;
}

EelIRect
eel_irect_offset_by (EelIRect rectangle, int x, int y)
{
    rectangle.x0 += x;
    rectangle.x1 += x;
    rectangle.y0 += y;
    rectangle.y1 += y;

    return rectangle;
}

EelIRect
eel_irect_scale_by (EelIRect rectangle, double scale)
{
    rectangle.x0 *= scale;
    rectangle.x1 *= scale;
    rectangle.y0 *= scale;
    rectangle.y1 *= scale;

    return rectangle;
}