1 /*
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This is a plug-in for GIMP.
5  *
6  * Generates images containing vector type drawings.
7  *
8  * Copyright (C) 1997 Andy Thomas  alt@picnic.demon.co.uk
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22  *
23  */
24 
25 #include "config.h"
26 
27 #include <libgimp/gimp.h>
28 #include <libgimp/gimpui.h>
29 
30 #include "gfig.h"
31 #include "gfig-dobject.h"
32 #include "gfig-poly.h"
33 #include "gfig-circle.h"
34 
35 #include "libgimp/stdplugins-intl.h"
36 
37 static gint        calc_radius     (GdkPoint *center,
38                                     GdkPoint *edge);
39 static void        d_draw_circle   (GfigObject *obj,
40                                     cairo_t    *cr);
41 static void        d_paint_circle  (GfigObject *obj);
42 static GfigObject *d_copy_circle   (GfigObject *obj);
43 
44 static void        d_update_circle (GdkPoint   *pnt);
45 
46 static gint
calc_radius(GdkPoint * center,GdkPoint * edge)47 calc_radius (GdkPoint *center, GdkPoint *edge)
48 {
49   gint dx = center->x - edge->x;
50   gint dy = center->y - edge->y;
51 
52   return (gint) sqrt (dx * dx + dy * dy);
53 }
54 
55 static void
d_draw_circle(GfigObject * obj,cairo_t * cr)56 d_draw_circle (GfigObject *obj,
57                cairo_t    *cr)
58 {
59   DobjPoints *center_pnt;
60   DobjPoints *edge_pnt;
61   gint        radius;
62 
63   center_pnt = obj->points;
64 
65   if (!center_pnt)
66     return; /* End-of-line */
67 
68   draw_sqr (&center_pnt->pnt, obj == gfig_context->selected_obj, cr);
69 
70   edge_pnt = center_pnt->next;
71 
72   if (!edge_pnt)
73     return;
74 
75   radius = calc_radius (&center_pnt->pnt, &edge_pnt->pnt);
76 
77   if (obj_creating == obj)
78     draw_circle (&edge_pnt->pnt, TRUE, cr);
79   else
80     draw_sqr (&edge_pnt->pnt, obj == gfig_context->selected_obj, cr);
81 
82   gfig_draw_arc (center_pnt->pnt.x, center_pnt->pnt.y,
83                  radius, radius, 0, 360, cr);
84 }
85 
86 static void
d_paint_circle(GfigObject * obj)87 d_paint_circle (GfigObject *obj)
88 {
89   DobjPoints *center_pnt;
90   DobjPoints *edge_pnt;
91   gint        radius;
92   gdouble     dpnts[4];
93 
94   g_assert (obj != NULL);
95 
96   center_pnt = obj->points;
97 
98   if (!center_pnt)
99     return; /* End-of-line */
100 
101   edge_pnt = center_pnt->next;
102 
103   if (!edge_pnt)
104     {
105       g_error ("Internal error - circle no edge pnt");
106     }
107 
108   radius = calc_radius (&center_pnt->pnt, &edge_pnt->pnt);
109 
110   dpnts[0] = (gdouble) center_pnt->pnt.x - radius;
111   dpnts[1] = (gdouble) center_pnt->pnt.y - radius;
112   dpnts[3] = dpnts[2] = (gdouble) radius * 2;
113 
114   /* Scale before drawing */
115   if (selvals.scaletoimage)
116     scale_to_original_xy (&dpnts[0], 2);
117   else
118     scale_to_xy (&dpnts[0], 2);
119 
120   if (gfig_context_get_current_style ()->fill_type != FILL_NONE)
121     {
122       gimp_context_push ();
123       gimp_context_set_antialias (selopt.antia);
124       gimp_context_set_feather (selopt.feather);
125       gimp_context_set_feather_radius (selopt.feather_radius, selopt.feather_radius);
126       gimp_image_select_ellipse (gfig_context->image_id,
127                                  selopt.type,
128                                  dpnts[0], dpnts[1],
129                                  dpnts[2], dpnts[3]);
130       gimp_context_pop ();
131 
132       paint_layer_fill (center_pnt->pnt.x - radius,
133                         center_pnt->pnt.y - radius,
134                         center_pnt->pnt.x + radius,
135                         center_pnt->pnt.y + radius);
136       gimp_selection_none (gfig_context->image_id);
137     }
138 
139   /* Drawing a circle may be harder than stroking a circular selection,
140    * but we have to do it or we will not be able to draw outside of the
141    * layer. */
142   if (obj->style.paint_type == PAINT_BRUSH_TYPE)
143     {
144       const gdouble r = dpnts[2] / 2;
145       const gdouble cx = dpnts[0] + r, cy = dpnts[1] + r;
146       gdouble       line_pnts[362];
147       gdouble       angle = 0;
148       gint          i = 0;
149 
150       while (i < 362)
151         {
152           static const gdouble step = 2 * G_PI / 180;
153 
154           line_pnts[i++] = cx + r * cos (angle);
155           line_pnts[i++] = cy + r * sin (angle);
156           angle += step;
157         }
158 
159       gfig_paint (selvals.brshtype, gfig_context->drawable_id, i, line_pnts);
160     }
161 }
162 
163 static GfigObject *
d_copy_circle(GfigObject * obj)164 d_copy_circle (GfigObject * obj)
165 {
166   GfigObject *nc;
167 
168   g_assert (obj->type == CIRCLE);
169 
170   nc = d_new_object (CIRCLE, obj->points->pnt.x, obj->points->pnt.y);
171   nc->points->next = d_copy_dobjpoints (obj->points->next);
172 
173   return nc;
174 }
175 
176 void
d_circle_object_class_init(void)177 d_circle_object_class_init (void)
178 {
179   GfigObjectClass *class = &dobj_class[CIRCLE];
180 
181   class->type      = CIRCLE;
182   class->name      = "CIRCLE";
183   class->drawfunc  = d_draw_circle;
184   class->paintfunc = d_paint_circle;
185   class->copyfunc  = d_copy_circle;
186   class->update    = d_update_circle;
187 }
188 
189 static void
d_update_circle(GdkPoint * pnt)190 d_update_circle (GdkPoint *pnt)
191 {
192   DobjPoints *center_pnt, *edge_pnt;
193 
194   center_pnt = obj_creating->points;
195 
196   if (!center_pnt)
197     return; /* No points */
198 
199   if ((edge_pnt = center_pnt->next))
200     {
201       edge_pnt->pnt = *pnt;
202     }
203   else
204     {
205       edge_pnt = new_dobjpoint (pnt->x, pnt->y);
206       center_pnt->next = edge_pnt;
207     }
208 }
209 
210 void
d_circle_start(GdkPoint * pnt,gboolean shift_down)211 d_circle_start (GdkPoint *pnt,
212                 gboolean  shift_down)
213 {
214   obj_creating = d_new_object (CIRCLE, pnt->x, pnt->y);
215 }
216 
217 void
d_circle_end(GdkPoint * pnt,gboolean shift_down)218 d_circle_end (GdkPoint *pnt,
219               gboolean  shift_down)
220 {
221   /* Under control point */
222   if (!obj_creating->points->next)
223     {
224       /* No circle created */
225       free_one_obj (obj_creating);
226     }
227   else
228     {
229       add_to_all_obj (gfig_context->current_obj, obj_creating);
230     }
231 
232   obj_creating = NULL;
233 }
234 
235