1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program 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
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include <glib-object.h>
23
#include "core-types.h"
26
#include "gimpimage.h"
27
#include "gimpimage-grid.h"
28
#include "gimpimage-guides.h"
29
#include "gimpimage-snap.h"
31
#include "gimp-intl.h"
34
/* public functions */
38
gimp_image_snap_x (GimpImage *gimage,
42
gboolean snap_to_guides,
43
gboolean snap_to_grid)
50
gdouble mindist = G_MAXDOUBLE;
53
gboolean snapped = FALSE;
55
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
56
g_return_val_if_fail (tx != NULL, FALSE);
58
if (! snap_to_guides && ! snap_to_grid)
63
if (x < 0 || x >= gimage->width)
66
if (snap_to_guides && gimage->guides != NULL)
68
for (list = gimage->guides; list; list = g_list_next (list))
70
guide = (GimpGuide *) list->data;
72
if (guide->position < 0)
75
if (guide->orientation == GIMP_ORIENTATION_VERTICAL)
77
dist = ABS (guide->position - x);
79
if (dist < MIN (epsilon_x, mindist))
82
*tx = guide->position;
89
if (snap_to_grid && gimage->grid != NULL)
91
grid = gimp_image_get_grid (gimage);
94
"xspacing", &xspacing,
98
for (i = xoffset; i <= gimage->width; i += xspacing)
105
if (dist < MIN (epsilon_x, mindist))
118
gimp_image_snap_y (GimpImage *gimage,
122
gboolean snap_to_guides,
123
gboolean snap_to_grid)
130
gdouble mindist = G_MAXDOUBLE;
133
gboolean snapped = FALSE;
135
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
136
g_return_val_if_fail (ty != NULL, FALSE);
138
if (! snap_to_guides && ! snap_to_grid)
143
if (y < 0 || y >= gimage->height)
146
if (snap_to_guides && gimage->guides != NULL)
148
for (list = gimage->guides; list; list = g_list_next (list))
150
guide = (GimpGuide *) list->data;
152
if (guide->position < 0)
155
if (guide->orientation == GIMP_ORIENTATION_HORIZONTAL)
157
dist = ABS (guide->position - y);
159
if (dist < MIN (epsilon_y, mindist))
162
*ty = guide->position;
169
if (snap_to_grid && gimage->grid != NULL)
171
grid = gimp_image_get_grid (gimage);
174
"yspacing", &yspacing,
178
for (i = yoffset; i <= gimage->height; i += yspacing)
185
if (dist < MIN (epsilon_y, mindist))
198
gimp_image_snap_point (GimpImage *gimage,
205
gboolean snap_to_guides,
206
gboolean snap_to_grid)
211
gdouble xspacing, yspacing;
212
gdouble xoffset, yoffset;
213
gdouble minxdist, minydist;
216
gboolean snapped = FALSE;
218
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
219
g_return_val_if_fail (tx != NULL, FALSE);
220
g_return_val_if_fail (ty != NULL, FALSE);
222
if (! snap_to_guides && ! snap_to_grid)
228
if (x < 0 || x >= gimage->width ||
229
y < 0 || y >= gimage->height)
234
minxdist = G_MAXDOUBLE;
235
minydist = G_MAXDOUBLE;
237
if (snap_to_guides && gimage->guides != NULL)
239
for (list = gimage->guides; list; list = g_list_next (list))
241
guide = (GimpGuide *) list->data;
243
if (guide->position < 0)
246
switch (guide->orientation)
248
case GIMP_ORIENTATION_HORIZONTAL:
249
dist = ABS (guide->position - y);
251
if (dist < MIN (epsilon_y, minydist))
254
*ty = guide->position;
259
case GIMP_ORIENTATION_VERTICAL:
260
dist = ABS (guide->position - x);
262
if (dist < MIN (epsilon_x, minxdist))
265
*tx = guide->position;
276
if (snap_to_grid && gimage->grid != NULL)
278
grid = gimp_image_get_grid (gimage);
281
"xspacing", &xspacing,
282
"yspacing", &yspacing,
287
for (i = xoffset; i <= gimage->width; i += xspacing)
294
if (dist < MIN (epsilon_x, minxdist))
302
for (i = yoffset; i <= gimage->height; i += yspacing)
309
if (dist < MIN (epsilon_y, minydist))
322
gimp_image_snap_rectangle (GimpImage *gimage,
331
gboolean snap_to_guides,
332
gboolean snap_to_grid)
336
gboolean snap1, snap2, snap3, snap4;
338
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
339
g_return_val_if_fail (tx1 != NULL, FALSE);
340
g_return_val_if_fail (ty1 != NULL, FALSE);
342
if (! snap_to_guides && ! snap_to_grid)
348
snap1 = gimp_image_snap_x (gimage, x1, &nx1,
352
snap2 = gimp_image_snap_x (gimage, x2, &nx2,
356
snap3 = gimp_image_snap_y (gimage, y1, &ny1,
360
snap4 = gimp_image_snap_y (gimage, y2, &ny2,
367
if (ABS (x1 - nx1) > ABS (x2 - nx2))
376
*tx1 = RINT (x1 + (nx2 - x2));
380
if (ABS (y1 - ny1) > ABS (y2 - ny2))
389
*ty1 = RINT (y1 + (ny2 - y2));
391
return (snap1 || snap2 || snap3 || snap4);