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-line.h"
32 #include "gfig-dobject.h"
33 #include "gfig-star.h"
34 #include "gfig-dialog.h"
35
36 #include "libgimp/stdplugins-intl.h"
37
38 static gint star_num_sides = 3; /* Default to three sided object */
39
40 static void d_draw_star (GfigObject *obj,
41 cairo_t *cr);
42 static void d_paint_star (GfigObject *obj);
43 static GfigObject *d_copy_star (GfigObject *obj);
44
45 static void d_update_star (GdkPoint *pnt);
46
47 void
tool_options_star(GtkWidget * notebook)48 tool_options_star (GtkWidget *notebook)
49 {
50 GtkWidget *sides;
51
52 sides = num_sides_widget (_("Star Number of Points"),
53 &star_num_sides, NULL, 3, 200);
54 gtk_notebook_append_page (GTK_NOTEBOOK (notebook), sides, NULL);
55 }
56
57 static void
d_draw_star(GfigObject * obj,cairo_t * cr)58 d_draw_star (GfigObject *obj,
59 cairo_t *cr)
60 {
61 DobjPoints *center_pnt;
62 DobjPoints *outer_radius_pnt;
63 DobjPoints *inner_radius_pnt;
64 gint16 shift_x;
65 gint16 shift_y;
66 gdouble ang_grid;
67 gdouble ang_loop;
68 gdouble outer_radius;
69 gdouble inner_radius;
70 gdouble offset_angle;
71 gint loop;
72 GdkPoint start_pnt = { 0, 0 };
73 GdkPoint first_pnt = { 0, 0 };
74 gboolean do_line = FALSE;
75
76 center_pnt = obj->points;
77
78 if (!center_pnt)
79 return; /* End-of-line */
80
81 /* First point is the center */
82 /* Just draw a control point around it */
83
84 draw_sqr (¢er_pnt->pnt, obj == gfig_context->selected_obj, cr);
85
86 /* Next point defines the radius */
87 outer_radius_pnt = center_pnt->next; /* this defines the vertices */
88
89 if (!outer_radius_pnt)
90 {
91 return;
92 }
93
94 inner_radius_pnt = outer_radius_pnt->next; /* this defines the vertices */
95
96 if (!inner_radius_pnt)
97 {
98 return;
99 }
100
101 /* Other control points */
102 if (obj == obj_creating)
103 {
104 draw_circle (&outer_radius_pnt->pnt, TRUE, cr);
105 draw_circle (&inner_radius_pnt->pnt, TRUE, cr);
106 }
107 else
108 {
109 draw_sqr (&outer_radius_pnt->pnt, obj == gfig_context->selected_obj, cr);
110 draw_sqr (&inner_radius_pnt->pnt, obj == gfig_context->selected_obj, cr);
111 }
112
113 /* Have center and radius - draw star */
114
115 shift_x = outer_radius_pnt->pnt.x - center_pnt->pnt.x;
116 shift_y = outer_radius_pnt->pnt.y - center_pnt->pnt.y;
117
118 outer_radius = sqrt ((shift_x*shift_x) + (shift_y*shift_y));
119
120 /* Lines */
121 ang_grid = 2.0 * G_PI / (2.0 * (gdouble) obj->type_data);
122 offset_angle = atan2 (shift_y, shift_x);
123
124 shift_x = inner_radius_pnt->pnt.x - center_pnt->pnt.x;
125 shift_y = inner_radius_pnt->pnt.y - center_pnt->pnt.y;
126
127 inner_radius = sqrt ((shift_x*shift_x) + (shift_y*shift_y));
128
129 for (loop = 0 ; loop < 2 * obj->type_data ; loop++)
130 {
131 gdouble lx, ly;
132 GdkPoint calc_pnt;
133
134 ang_loop = (gdouble)loop * ang_grid + offset_angle;
135
136 if (loop % 2)
137 {
138 lx = inner_radius * cos (ang_loop);
139 ly = inner_radius * sin (ang_loop);
140 }
141 else
142 {
143 lx = outer_radius * cos (ang_loop);
144 ly = outer_radius * sin (ang_loop);
145 }
146
147 calc_pnt.x = RINT (lx + center_pnt->pnt.x);
148 calc_pnt.y = RINT (ly + center_pnt->pnt.y);
149
150 if (do_line)
151 {
152
153 /* Miss out points that come to the same location */
154 if (calc_pnt.x == start_pnt.x && calc_pnt.y == start_pnt.y)
155 continue;
156
157 gfig_draw_line (calc_pnt.x, calc_pnt.y, start_pnt.x, start_pnt.y, cr);
158 }
159 else
160 {
161 do_line = TRUE;
162 first_pnt = calc_pnt;
163 }
164 start_pnt = calc_pnt;
165 }
166
167 gfig_draw_line (first_pnt.x, first_pnt.y, start_pnt.x, start_pnt.y, cr);
168 }
169
170 static void
d_paint_star(GfigObject * obj)171 d_paint_star (GfigObject *obj)
172 {
173 /* first point center */
174 /* Next point is radius */
175 gdouble *line_pnts;
176 gint seg_count = 0;
177 gint i = 0;
178 DobjPoints *center_pnt;
179 DobjPoints *outer_radius_pnt;
180 DobjPoints *inner_radius_pnt;
181 gint16 shift_x;
182 gint16 shift_y;
183 gdouble ang_grid;
184 gdouble ang_loop;
185 gdouble outer_radius;
186 gdouble inner_radius;
187 gdouble offset_angle;
188 gint loop;
189 GdkPoint first_pnt = { 0, 0 };
190 GdkPoint last_pnt = { 0, 0 };
191 gboolean first = TRUE;
192 gdouble *min_max;
193
194 g_assert (obj != NULL);
195
196 /* count - add one to close polygon */
197 seg_count = 2 * obj->type_data + 1;
198
199 center_pnt = obj->points;
200
201 if (!center_pnt || !seg_count)
202 return; /* no-line */
203
204 line_pnts = g_new0 (gdouble, 2 * seg_count + 1);
205 min_max = g_new (gdouble, 4);
206
207 /* Go around all the points drawing a line from one to the next */
208 /* Next point defines the radius */
209 outer_radius_pnt = center_pnt->next; /* this defines the vetices */
210
211 if (!outer_radius_pnt)
212 {
213 #ifdef DEBUG
214 g_warning ("Internal error in star - no outer vertice point \n");
215 #endif /* DEBUG */
216 g_free (line_pnts);
217 g_free (min_max);
218 return;
219 }
220
221 inner_radius_pnt = outer_radius_pnt->next; /* this defines the vetices */
222
223 if (!inner_radius_pnt)
224 {
225 #ifdef DEBUG
226 g_warning ("Internal error in star - no inner vertice point \n");
227 #endif /* DEBUG */
228 g_free (line_pnts);
229 g_free (min_max);
230 return;
231 }
232
233 shift_x = outer_radius_pnt->pnt.x - center_pnt->pnt.x;
234 shift_y = outer_radius_pnt->pnt.y - center_pnt->pnt.y;
235
236 outer_radius = sqrt ((shift_x*shift_x) + (shift_y*shift_y));
237
238 /* Lines */
239 ang_grid = 2.0 * G_PI / (2.0 * (gdouble) obj->type_data);
240 offset_angle = atan2 (shift_y, shift_x);
241
242 shift_x = inner_radius_pnt->pnt.x - center_pnt->pnt.x;
243 shift_y = inner_radius_pnt->pnt.y - center_pnt->pnt.y;
244
245 inner_radius = sqrt ((shift_x*shift_x) + (shift_y*shift_y));
246
247 for (loop = 0 ; loop < 2 * obj->type_data ; loop++)
248 {
249 gdouble lx, ly;
250 GdkPoint calc_pnt;
251
252 ang_loop = (gdouble)loop * ang_grid + offset_angle;
253
254 if (loop % 2)
255 {
256 lx = inner_radius * cos (ang_loop);
257 ly = inner_radius * sin (ang_loop);
258 }
259 else
260 {
261 lx = outer_radius * cos (ang_loop);
262 ly = outer_radius * sin (ang_loop);
263 }
264
265 calc_pnt.x = RINT (lx + center_pnt->pnt.x);
266 calc_pnt.y = RINT (ly + center_pnt->pnt.y);
267
268 /* Miss out duped pnts */
269 if (!first)
270 {
271 if (calc_pnt.x == last_pnt.x && calc_pnt.y == last_pnt.y)
272 {
273 continue;
274 }
275 }
276
277 line_pnts[i++] = calc_pnt.x;
278 line_pnts[i++] = calc_pnt.y;
279 last_pnt = calc_pnt;
280
281 if (first)
282 {
283 first_pnt = calc_pnt;
284 first = FALSE;
285 min_max[0] = min_max[2] = calc_pnt.x;
286 min_max[1] = min_max[3] = calc_pnt.y;
287 }
288 else
289 {
290 min_max[0] = MIN (min_max[0], calc_pnt.x);
291 min_max[1] = MIN (min_max[1], calc_pnt.y);
292 min_max[2] = MAX (min_max[2], calc_pnt.x);
293 min_max[3] = MAX (min_max[3], calc_pnt.y);
294 }
295 }
296
297 line_pnts[i++] = first_pnt.x;
298 line_pnts[i++] = first_pnt.y;
299
300 /* Scale before drawing */
301 if (selvals.scaletoimage)
302 {
303 scale_to_original_xy (&line_pnts[0], i / 2);
304 scale_to_original_xy (min_max, 2);
305 }
306 else
307 {
308 scale_to_xy (&line_pnts[0], i / 2);
309 scale_to_xy (min_max, 2);
310 }
311
312 if (gfig_context_get_current_style ()->fill_type != FILL_NONE)
313 {
314 gimp_context_push ();
315 gimp_context_set_antialias (selopt.antia);
316 gimp_context_set_feather (selopt.feather);
317 gimp_context_set_feather_radius (selopt.feather_radius, selopt.feather_radius);
318 gimp_image_select_polygon (gfig_context->image_id,
319 selopt.type,
320 i, line_pnts);
321 gimp_context_pop ();
322
323 paint_layer_fill (min_max[0], min_max[1], min_max[2], min_max[3]);
324 gimp_selection_none (gfig_context->image_id);
325 }
326
327 if (obj->style.paint_type == PAINT_BRUSH_TYPE)
328 gfig_paint (selvals.brshtype, gfig_context->drawable_id, i, line_pnts);
329
330 g_free (line_pnts);
331 g_free (min_max);
332 }
333
334 static GfigObject *
d_copy_star(GfigObject * obj)335 d_copy_star (GfigObject *obj)
336 {
337 GfigObject *np;
338
339 g_assert (obj->type == STAR);
340
341 np = d_new_object (STAR, obj->points->pnt.x, obj->points->pnt.y);
342 np->points->next = d_copy_dobjpoints (obj->points->next);
343 np->type_data = obj->type_data;
344
345 return np;
346 }
347
348 void
d_star_object_class_init(void)349 d_star_object_class_init (void)
350 {
351 GfigObjectClass *class = &dobj_class[STAR];
352
353 class->type = STAR;
354 class->name = "STAR";
355 class->drawfunc = d_draw_star;
356 class->paintfunc = d_paint_star;
357 class->copyfunc = d_copy_star;
358 class->update = d_update_star;
359 }
360
361 static void
d_update_star(GdkPoint * pnt)362 d_update_star (GdkPoint *pnt)
363 {
364 DobjPoints *center_pnt, *inner_pnt, *outer_pnt;
365
366 center_pnt = obj_creating->points;
367
368 if (!center_pnt)
369 return; /* No points */
370
371 if ((outer_pnt = center_pnt->next))
372 {
373 inner_pnt = outer_pnt->next;
374 outer_pnt->pnt = *pnt;
375 inner_pnt->pnt.x = pnt->x + (2 * (center_pnt->pnt.x - pnt->x)) / 3;
376 inner_pnt->pnt.y = pnt->y + (2 * (center_pnt->pnt.y - pnt->y)) / 3;
377 }
378 else
379 {
380 /* Radius is a few pixels away */
381 /* First edge point */
382 d_pnt_add_line (obj_creating, pnt->x, pnt->y,-1);
383 /* Inner radius */
384 d_pnt_add_line (obj_creating,
385 pnt->x + (2 * (center_pnt->pnt.x - pnt->x)) / 3,
386 pnt->y + (2 * (center_pnt->pnt.y - pnt->y)) / 3,
387 -1);
388 }
389 }
390
391 void
d_star_start(GdkPoint * pnt,gboolean shift_down)392 d_star_start (GdkPoint *pnt,
393 gboolean shift_down)
394 {
395 obj_creating = d_new_object (STAR, pnt->x, pnt->y);
396 obj_creating->type_data = star_num_sides;
397 }
398
399 void
d_star_end(GdkPoint * pnt,gboolean shift_down)400 d_star_end (GdkPoint *pnt,
401 gboolean shift_down)
402 {
403 add_to_all_obj (gfig_context->current_obj, obj_creating);
404 obj_creating = NULL;
405 }
406
407