gimpimage-snap.c 20 KB
Newer Older
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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <glib-object.h>

#include "core-types.h"

#include "gimp.h"
#include "gimpimage.h"
27
#include "gimpimage-grid.h"
28
#include "gimpimage-guides.h"
29
30
#include "gimpimage-snap.h"

31
32
33
#include "vectors/gimpstroke.h"
#include "vectors/gimpvectors.h"

34
35
36
37
38
39
40
41
42
#include "gimp-intl.h"


/*  public functions  */


gboolean
gimp_image_snap_x (GimpImage *gimage,
                   gdouble    x,
43
                   gdouble   *tx,
44
                   gdouble    epsilon_x,
45
                   gboolean   snap_to_guides,
46
47
                   gboolean   snap_to_grid,
                   gboolean   snap_to_canvas)
48
{
49
50
  gdouble    mindist = G_MAXDOUBLE;
  gdouble    dist;
51
52
53
54
55
  gboolean   snapped = FALSE;

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  g_return_val_if_fail (tx != NULL, FALSE);

56
57
58
59
60
  *tx = x;

  if (! gimage->guides) snap_to_guides = FALSE;
  if (! gimage->grid)   snap_to_grid   = FALSE;

61
  if (! (snap_to_guides || snap_to_grid || snap_to_canvas))
62
63
    return FALSE;

64
  if (x < -epsilon_x || x >= (gimage->width + epsilon_x))
65
66
    return FALSE;

67
  if (snap_to_guides)
68
    {
69
70
      GList *list;

71
72
      for (list = gimage->guides; list; list = g_list_next (list))
        {
73
          GimpGuide *guide = list->data;
74

75
76
          if (guide->position < 0)
            continue;
77

78
79
80
          if (guide->orientation == GIMP_ORIENTATION_VERTICAL)
            {
              dist = ABS (guide->position - x);
81

82
              if (dist < MIN (epsilon_x, mindist))
83
84
85
86
87
88
89
90
91
                {
                  mindist = dist;
                  *tx = guide->position;
                  snapped = TRUE;
                }
            }
        }
    }

92
  if (snap_to_grid)
93
    {
94
95
96
97
      GimpGrid *grid = gimp_image_get_grid (gimage);
      gdouble   xspacing;
      gdouble   xoffset;
      gdouble   i;
98

99
      g_object_get (grid,
100
101
102
                    "xspacing", &xspacing,
                    "xoffset",  &xoffset,
                    NULL);
103

104
105
106
107
      for (i = xoffset; i <= gimage->width; i += xspacing)
        {
          if (i < 0)
            continue;
108

109
          dist = ABS (i - x);
110

111
          if (dist < MIN (epsilon_x, mindist))
112
113
114
115
116
117
118
119
            {
              mindist = dist;
              *tx = i;
              snapped = TRUE;
            }
        }
    }

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  if (snap_to_canvas)
    {
      dist = ABS (x);

      if (dist < MIN (epsilon_x, mindist))
        {
          mindist = dist;
          *tx = 0;
          snapped = TRUE;
        }

      dist = ABS (gimage->width - x);

      if (dist < MIN (epsilon_x, mindist))
        {
          mindist = dist;
          *tx = gimage->width;
          snapped = TRUE;
        }
    }

141
142
143
144
145
146
  return snapped;
}

gboolean
gimp_image_snap_y (GimpImage *gimage,
                   gdouble    y,
147
                   gdouble   *ty,
148
                   gdouble    epsilon_y,
149
                   gboolean   snap_to_guides,
150
151
                   gboolean   snap_to_grid,
                   gboolean   snap_to_canvas)
152
{
153
154
  gdouble    mindist = G_MAXDOUBLE;
  gdouble    dist;
155
156
157
158
159
  gboolean   snapped = FALSE;

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  g_return_val_if_fail (ty != NULL, FALSE);

160
161
162
163
164
  *ty = y;

  if (! gimage->guides) snap_to_guides = FALSE;
  if (! gimage->grid)   snap_to_grid   = FALSE;

165
  if (! (snap_to_guides || snap_to_grid || snap_to_canvas))
166
167
    return FALSE;

168
  if (y < -epsilon_y || y >= (gimage->height + epsilon_y))
169
170
    return FALSE;

171
  if (snap_to_guides)
172
    {
173
174
      GList *list;

175
176
      for (list = gimage->guides; list; list = g_list_next (list))
        {
177
          GimpGuide *guide = list->data;
178

179
180
          if (guide->position < 0)
            continue;
181

182
183
184
          if (guide->orientation == GIMP_ORIENTATION_HORIZONTAL)
            {
              dist = ABS (guide->position - y);
185

186
              if (dist < MIN (epsilon_y, mindist))
187
188
189
190
191
192
193
194
195
                {
                  mindist = dist;
                  *ty = guide->position;
                  snapped = TRUE;
                }
            }
        }
    }

196
  if (snap_to_grid)
197
    {
198
199
200
201
      GimpGrid *grid = gimp_image_get_grid (gimage);
      gdouble    yspacing;
      gdouble    yoffset;
      gdouble    i;
202

203
      g_object_get (grid,
204
205
206
                    "yspacing", &yspacing,
                    "yoffset",  &yoffset,
                    NULL);
207

208
209
210
211
      for (i = yoffset; i <= gimage->height; i += yspacing)
        {
          if (i < 0)
            continue;
212

213
          dist = ABS (i - y);
214

215
          if (dist < MIN (epsilon_y, mindist))
216
217
218
219
220
221
222
223
            {
              mindist = dist;
              *ty = i;
              snapped = TRUE;
            }
        }
    }

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  if (snap_to_canvas)
    {
      dist = ABS (y);

      if (dist < MIN (epsilon_y, mindist))
        {
          mindist = dist;
          *ty = 0;
          snapped = TRUE;
        }

      dist = ABS (gimage->height - y);

      if (dist < MIN (epsilon_y, mindist))
        {
          mindist = dist;
          *ty = gimage->height;
          snapped = TRUE;
        }
    }

245
246
247
248
249
250
251
  return snapped;
}

gboolean
gimp_image_snap_point (GimpImage *gimage,
                       gdouble    x,
                       gdouble    y,
252
253
                       gdouble   *tx,
                       gdouble   *ty,
254
255
                       gdouble    epsilon_x,
                       gdouble    epsilon_y,
256
                       gboolean   snap_to_guides,
257
258
259
                       gboolean   snap_to_grid,
                       gboolean   snap_to_canvas,
                       gboolean   snap_to_vectors)
260
{
261
262
263
264
  gdouble  mindist_x = G_MAXDOUBLE;
  gdouble  mindist_y = G_MAXDOUBLE;
  gdouble  dist;
  gboolean snapped   = FALSE;
265
266
267
268
269

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  g_return_val_if_fail (tx != NULL, FALSE);
  g_return_val_if_fail (ty != NULL, FALSE);

270
271
  *tx = x;
  *ty = y;
272

273
274
275
276
277
278
279
  if (! gimage->guides)         snap_to_guides  = FALSE;
  if (! gimage->grid)           snap_to_grid    = FALSE;
  if (! gimage->active_vectors) snap_to_vectors = FALSE;

  if (! (snap_to_guides || snap_to_grid || snap_to_canvas || snap_to_vectors))
    return FALSE;

280
281
  if (x < -epsilon_x || x >= (gimage->width  + epsilon_x) ||
      y < -epsilon_y || y >= (gimage->height + epsilon_y))
282
283
284
285
    {
      return FALSE;
    }

286
  if (snap_to_guides)
287
    {
288
289
      GList *list;

290
291
      for (list = gimage->guides; list; list = g_list_next (list))
        {
292
          GimpGuide *guide = list->data;
293

294
295
          if (guide->position < 0)
            continue;
296

297
298
299
300
          switch (guide->orientation)
            {
            case GIMP_ORIENTATION_HORIZONTAL:
              dist = ABS (guide->position - y);
301

302
              if (dist < MIN (epsilon_y, mindist_y))
303
                {
304
                  mindist_y = dist;
305
306
307
308
                  *ty = guide->position;
                  snapped = TRUE;
                }
              break;
309

310
311
            case GIMP_ORIENTATION_VERTICAL:
              dist = ABS (guide->position - x);
312

313
              if (dist < MIN (epsilon_x, mindist_x))
314
                {
315
                  mindist_x = dist;
316
317
318
319
                  *tx = guide->position;
                  snapped = TRUE;
                }
              break;
320

321
322
323
324
325
            default:
              break;
            }
        }
    }
326

327
  if (snap_to_grid)
328
    {
329
330
331
332
      GimpGrid *grid = gimp_image_get_grid (gimage);
      gdouble   xspacing, yspacing;
      gdouble   xoffset, yoffset;
      gdouble   i;
333

334
      g_object_get (grid,
335
336
337
338
339
340
341
342
343
344
345
                    "xspacing", &xspacing,
                    "yspacing", &yspacing,
                    "xoffset",  &xoffset,
                    "yoffset",  &yoffset,
                    NULL);

      for (i = xoffset; i <= gimage->width; i += xspacing)
        {
          if (i < 0)
            continue;

346
          dist = ABS (i - x);
347

348
          if (dist < MIN (epsilon_x, mindist_x))
349
            {
350
              mindist_x = dist;
351
352
353
354
355
356
357
358
359
360
              *tx = i;
              snapped = TRUE;
            }
        }

      for (i = yoffset; i <= gimage->height; i += yspacing)
        {
          if (i < 0)
            continue;

361
          dist = ABS (i - y);
362

363
          if (dist < MIN (epsilon_y, mindist_y))
364
            {
365
              mindist_y = dist;
366
367
368
369
370
371
              *ty = i;
              snapped = TRUE;
            }
        }
    }

372
373
374
375
  if (snap_to_canvas)
    {
      dist = ABS (x);

376
      if (dist < MIN (epsilon_x, mindist_x))
377
        {
378
          mindist_x = dist;
379
380
381
382
383
384
          *tx = 0;
          snapped = TRUE;
        }

      dist = ABS (gimage->width - x);

385
      if (dist < MIN (epsilon_x, mindist_x))
386
        {
387
          mindist_x = dist;
388
389
390
391
392
393
          *tx = gimage->width;
          snapped = TRUE;
        }

      dist = ABS (y);

394
      if (dist < MIN (epsilon_y, mindist_y))
395
        {
396
          mindist_y = dist;
397
398
399
400
401
402
          *ty = 0;
          snapped = TRUE;
        }

      dist = ABS (gimage->height - y);

403
      if (dist < MIN (epsilon_y, mindist_y))
404
        {
405
          mindist_y = dist;
406
407
408
409
410
          *ty = gimage->height;
          snapped = TRUE;
        }
    }

411
  if (snap_to_vectors)
412
413
414
    {
      GimpVectors *vectors = gimp_image_get_active_vectors (gimage);
      GimpStroke  *stroke  = NULL;
415
416
417
418
      GimpCoords   coords  = { 0, 0, 0, 0, 0 };

      coords.x = x;
      coords.y = y;
419
420
421
422
423
424
425
426
427
428
429

      while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke)))
        {
          GimpCoords nearest;

          if (gimp_stroke_nearest_point_get (stroke, &coords, 1.0,
                                             &nearest,
                                             NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.x - x);

430
              if (dist < MIN (epsilon_x, mindist_x))
431
                {
432
                  mindist_x = dist;
433
434
435
436
437
438
                  *tx = nearest.x;
                  snapped = TRUE;
                }

              dist = ABS (nearest.y - y);

439
              if (dist < MIN (epsilon_y, mindist_y))
440
                {
441
                  mindist_y = dist;
442
443
444
445
446
447
448
                  *ty = nearest.y;
                  snapped = TRUE;
                }
            }
        }
    }

449
450
451
452
453
454
455
456
457
  return snapped;
}

gboolean
gimp_image_snap_rectangle (GimpImage *gimage,
                           gdouble    x1,
                           gdouble    y1,
                           gdouble    x2,
                           gdouble    y2,
458
459
                           gdouble   *tx1,
                           gdouble   *ty1,
460
461
                           gdouble    epsilon_x,
                           gdouble    epsilon_y,
462
                           gboolean   snap_to_guides,
463
464
465
                           gboolean   snap_to_grid,
                           gboolean   snap_to_canvas,
                           gboolean   snap_to_vectors)
466
{
467
468
469
470
  gdouble  nx, ny;
  gdouble  mindist_x = G_MAXDOUBLE;
  gdouble  mindist_y = G_MAXDOUBLE;
  gboolean snapped   = FALSE;
471
472
473
474
475

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  g_return_val_if_fail (tx1 != NULL, FALSE);
  g_return_val_if_fail (ty1 != NULL, FALSE);

476
477
478
479
480
481
482
  *tx1 = x1;
  *ty1 = y1;

  if (! gimage->guides)         snap_to_guides  = FALSE;
  if (! gimage->grid)           snap_to_grid    = FALSE;
  if (! gimage->active_vectors) snap_to_vectors = FALSE;

483
  if (! (snap_to_guides || snap_to_grid || snap_to_canvas || snap_to_vectors))
484
485
    return FALSE;

486
487
488
489
490
491
492
493
494
495
496
  /*  left edge  */
  if (gimp_image_snap_x (gimage, x1, &nx,
                         MIN (epsilon_x, mindist_x),
                         snap_to_guides,
                         snap_to_grid,
                         snap_to_canvas))
    {
      mindist_x = ABS (nx - x1);
      *tx1 = nx;
      snapped = TRUE;
    }
497

498
499
500
501
502
503
  /*  right edge  */
  if (gimp_image_snap_x (gimage, x2, &nx,
                         MIN (epsilon_x, mindist_x),
                         snap_to_guides,
                         snap_to_grid,
                         snap_to_canvas))
504
    {
505
506
507
      mindist_x = ABS (nx - x2);
      *tx1 = RINT (x1 + (nx - x2));
      snapped = TRUE;
508
509
    }

510
511
512
513
514
515
516
517
518
519
520
  /*  top edge  */
  if (gimp_image_snap_y (gimage, y1, &ny,
                         MIN (epsilon_y, mindist_y),
                         snap_to_guides,
                         snap_to_grid,
                         snap_to_canvas))
    {
      mindist_y = ABS (ny - y1);
      *ty1 = ny;
      snapped = TRUE;
    }
521

522
523
524
525
526
527
  /*  bottom edge  */
  if (gimp_image_snap_y (gimage, y2, &ny,
                         MIN (epsilon_y, mindist_y),
                         snap_to_guides,
                         snap_to_grid,
                         snap_to_canvas))
528
    {
529
530
531
      mindist_y = ABS (ny - y2);
      *ty1 = RINT (y1 + (ny - y2));
      snapped = TRUE;
532
533
    }

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
  if (snap_to_vectors)
    {
      GimpVectors *vectors = gimp_image_get_active_vectors (gimage);
      GimpStroke  *stroke  = NULL;
      GimpCoords   coords1 = { 0, 0, 0, 0, 0 };
      GimpCoords   coords2 = { 0, 0, 0, 0, 0 };

      while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke)))
        {
          GimpCoords nearest;
          gdouble    dist;

#define gimp_stroke_nearest_tangent_get(stroke,coords1,coords2,\
                                        d,e,f,g,h) FALSE
#define gimp_stroke_nearest_intersection_get(stroke,coords,direction,\
                                             d,e,f,g,h) FALSE

          /*  top edge  */

          coords1.x = x1;
          coords1.y = y1;
          coords2.x = x2;
          coords2.y = y1;

          if (gimp_stroke_nearest_tangent_get (stroke, &coords1, &coords2,
                                               1.0, &nearest,
                                               NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.y - y1);

              if (dist < MIN (epsilon_y, mindist_y))
                {
                  mindist_y = dist;
                  *ty1 = nearest.y;
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords1, &coords2,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.x - x1);

              if (dist < MIN (epsilon_x, mindist_x))
                {
                  mindist_x = dist;
                  *tx1 = nearest.x;
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.x - x2);

              if (dist < MIN (epsilon_x, mindist_x))
                {
                  mindist_x = dist;
                  *tx1 = RINT (x1 + (nearest.x - x2));
                  snapped = TRUE;
                }
            }

          /*  bottom edge  */

          coords1.x = x1;
          coords1.y = y2;
          coords2.x = x2;
          coords2.y = y2;

          if (gimp_stroke_nearest_tangent_get (stroke, &coords1, &coords2,
                                               1.0, &nearest,
                                               NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.y - y2);
612

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
              if (dist < MIN (epsilon_y, mindist_y))
                {
                  mindist_y = dist;
                  *ty1 = RINT (y1 + (nearest.y - y2));
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords1, &coords2,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.x - x1);

              if (dist < MIN (epsilon_x, mindist_x))
                {
                  mindist_x = dist;
                  *tx1 = nearest.x;
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.x - x2);

              if (dist < MIN (epsilon_x, mindist_x))
                {
                  mindist_x = dist;
                  *tx1 = RINT (x1 + (nearest.x - x2));
                  snapped = TRUE;
                }
            }

          /*  left edge  */

          coords1.x = x1;
          coords1.y = y1;
          coords2.x = x1;
          coords2.y = y2;

          if (gimp_stroke_nearest_tangent_get (stroke, &coords1, &coords2,
                                               1.0, &nearest,
                                               NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.x - x1);

              if (dist < MIN (epsilon_x, mindist_x))
                {
                  mindist_x = dist;
                  *tx1 = nearest.x;
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords1, &coords2,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.y - y1);

              if (dist < MIN (epsilon_y, mindist_y))
                {
                  mindist_y = dist;
                  *ty1 = nearest.y;
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.y - y2);

              if (dist < MIN (epsilon_y, mindist_y))
                {
                  mindist_y = dist;
                  *ty1 = RINT (y1 + (nearest.y - y2));
                  snapped = TRUE;
                }
            }

          /*  right edge  */

          coords1.x = x2;
          coords1.y = y1;
          coords2.x = x2;
          coords2.y = y2;

          if (gimp_stroke_nearest_tangent_get (stroke, &coords1, &coords2,
                                               1.0, &nearest,
                                               NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.x - x2);

              if (dist < MIN (epsilon_x, mindist_x))
                {
                  mindist_x = dist;
                  *tx1 = RINT (x1 + (nearest.x - x2));
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords1, &coords2,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.y - y1);

              if (dist < MIN (epsilon_y, mindist_y))
                {
                  mindist_y = dist;
                  *ty1 = nearest.y;
                  snapped = TRUE;
                }
            }

          if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
                                                    1.0, &nearest,
                                                    NULL, NULL, NULL) >= 0)
            {
              dist = ABS (nearest.y - y2);

              if (dist < MIN (epsilon_y, mindist_y))
                {
                  mindist_y = dist;
                  *ty1 = RINT (y1 + (nearest.y - y2));
                  snapped = TRUE;
                }
            }
        }
    }

  return snapped;
750
}