summaryrefslogtreecommitdiffstats
path: root/app/display/gimpdisplayshell-utils.c
blob: 8e85e711ef9ce288ad515bb6c1360220c8d5bfab (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
/* GIMP - The GNU 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"

#include "display-types.h"

#include "core/gimp-utils.h"
#include "core/gimpimage.h"
#include "core/gimpunit.h"

#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-utils.h"

#include "gimp-intl.h"

void
gimp_display_shell_get_constrained_line_params (GimpDisplayShell *shell,
                                                gdouble          *offset_angle,
                                                gdouble          *xres,
                                                gdouble          *yres)
{
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
  g_return_if_fail (offset_angle != NULL);
  g_return_if_fail (xres != NULL);
  g_return_if_fail (yres != NULL);

  if (shell->flip_horizontally ^ shell->flip_vertically)
    *offset_angle = +shell->rotate_angle;
  else
    *offset_angle = -shell->rotate_angle;

  *xres = 1.0;
  *yres = 1.0;

  if (! shell->dot_for_dot)
    {
      GimpImage *image = gimp_display_get_image (shell->display);

      if (image)
        gimp_image_get_resolution (image, xres, yres);
    }
}

void
gimp_display_shell_constrain_line (GimpDisplayShell *shell,
                                   gdouble           start_x,
                                   gdouble           start_y,
                                   gdouble          *end_x,
                                   gdouble          *end_y,
                                   gint              n_snap_lines)
{
  gdouble offset_angle;
  gdouble xres, yres;

  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
  g_return_if_fail (end_x != NULL);
  g_return_if_fail (end_y != NULL);

  gimp_display_shell_get_constrained_line_params (shell,
                                                  &offset_angle,
                                                  &xres, &yres);

  gimp_constrain_line (start_x, start_y,
                       end_x,   end_y,
                       n_snap_lines,
                       offset_angle,
                       xres, yres);
}

gdouble
gimp_display_shell_constrain_angle (GimpDisplayShell *shell,
                                    gdouble           angle,
                                    gint              n_snap_lines)
{
  gdouble x, y;

  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), 0.0);

  x = cos (angle);
  y = sin (angle);

  gimp_display_shell_constrain_line (shell,
                                     0.0, 0.0,
                                     &x,  &y,
                                     n_snap_lines);

  return atan2 (y, x);
}

/**
 * gimp_display_shell_get_line_status:
 * @status:    initial status text.
 * @separator: separator text between the line information and @status.
 * @shell:     #GimpDisplayShell this status text will be displayed for.
 * @x1:        abscissa of first point.
 * @y1:        ordinate of first point.
 * @x2:        abscissa of second point.
 * @y2:        ordinate of second point.
 *
 * Utility function to prepend the status message with a distance and
 * angle value. Obviously this is only to be used for tools when it
 * makes sense, and in particular when there is a concept of line. For
 * instance when shift-clicking a painting tool or in the blend tool,
 * etc.
 * This utility prevents code duplication but also ensures a common
 * display for every tool where such a status is needed. It will take
 * into account the shell unit settings and will use the ideal digit
 * precision according to current image resolution.
 *
 * Return value: a newly allocated string containing the enhanced status.
 **/
gchar *
gimp_display_shell_get_line_status (GimpDisplayShell *shell,
                                    const gchar      *status,
                                    const gchar      *separator,
                                    gdouble           x1,
                                    gdouble           y1,
                                    gdouble           x2,
                                    gdouble           y2)
{
  GimpImage *image;
  gchar     *enhanced_status;
  gdouble    xres;
  gdouble    yres;
  gdouble    dx, dy, pixel_dist;
  gdouble    angle;

  image = gimp_display_get_image (shell->display);
  if (! image)
    {
      /* This makes no sense to add line information when no image is
       * attached to the display. */
      return g_strdup (status);
    }

  if (shell->unit == GIMP_UNIT_PIXEL)
    xres = yres = 1.0;
  else
    gimp_image_get_resolution (image, &xres, &yres);

  dx = x2 - x1;
  dy = y2 - y1;
  pixel_dist = sqrt (SQR (dx) + SQR (dy));

  if (dx)
    {
      angle = gimp_rad_to_deg (atan ((dy/yres) / (dx/xres)));
      if (dx > 0)
        {
          if (dy > 0)
            angle = 360.0 - angle;
          else if (dy < 0)
            angle = -angle;
        }
      else
        {
          angle = 180.0 - angle;
        }
    }
  else if (dy)
    {
      angle = dy > 0 ? 270.0 : 90.0;
    }
  else
    {
      angle = 0.0;
    }

  if (shell->unit == GIMP_UNIT_PIXEL)
    {
      enhanced_status = g_strdup_printf ("%.1f %s, %.2f\302\260%s%s",
                                         pixel_dist, _("pixels"), angle,
                                         separator, status);
    }
  else
    {
      gdouble inch_dist;
      gdouble unit_dist;
      gint    digits = 0;

      /* The distance in unit. */
      inch_dist = sqrt (SQR (dx / xres) + SQR (dy / yres));
      unit_dist = gimp_unit_get_factor (shell->unit) * inch_dist;

      /* The ideal digit precision for unit in current resolution. */
      if (inch_dist)
        digits = gimp_unit_get_scaled_digits (shell->unit,
                                              pixel_dist / inch_dist);

      enhanced_status = g_strdup_printf ("%.*f %s, %.2f\302\260%s%s",
                                         digits, unit_dist,
                                         gimp_unit_get_symbol (shell->unit),
                                         angle, separator, status);

    }

  return enhanced_status;
}