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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
/*
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This is a plug-in for the GIMP.
*
* Generates images containing vector type drawings.
*
* Copyright (C) 1997 Andy Thomas alt@picnic.demon.co.uk
*
* 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 <math.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "gfig.h"
#include "gfig-line.h"
#include "gfig-dobject.h"
#include "gfig-dialog.h"
#include "libgimp/stdplugins-intl.h"
#define FP_PNT_MAX 10
typedef gdouble (*fp_pnt)[2];
static gboolean bezier_closed = FALSE;
static gboolean bezier_line_frame = FALSE;
static int fp_pnt_cnt = 0;
static int fp_pnt_chunk = 0;
static gdouble *fp_pnt_pnts = NULL;
GfigObject *tmp_bezier; /* Needed when drawing bezier curves */
static void fp_pnt_start (void);
static void fp_pnt_add (gdouble p1,
gdouble p2,
gdouble p3,
gdouble p4);
static gdouble *d_bz_get_array (gint *sz);
static void d_bz_line (void);
static void DrawBezier (fp_pnt points,
gint np,
gdouble mid,
gint depth);
static void d_paint_bezier (GfigObject *obj);
static GfigObject *d_copy_bezier (GfigObject *obj);
static void d_update_bezier (GdkPoint *pnt);
static void
fp_pnt_start (void)
{
fp_pnt_cnt = 0;
}
/* Add a line segment to collection array */
static void
fp_pnt_add (gdouble p1,
gdouble p2,
gdouble p3,
gdouble p4)
{
if (!fp_pnt_pnts)
{
fp_pnt_pnts = g_new0 (gdouble, FP_PNT_MAX);
fp_pnt_chunk = 1;
}
if (((fp_pnt_cnt + 4) / FP_PNT_MAX) >= fp_pnt_chunk)
{
/* more space pls */
fp_pnt_chunk++;
fp_pnt_pnts = g_renew (gdouble, fp_pnt_pnts, fp_pnt_chunk * FP_PNT_MAX);
}
fp_pnt_pnts[fp_pnt_cnt++] = p1;
fp_pnt_pnts[fp_pnt_cnt++] = p2;
fp_pnt_pnts[fp_pnt_cnt++] = p3;
fp_pnt_pnts[fp_pnt_cnt++] = p4;
}
static gdouble *
d_bz_get_array (gint *sz)
{
*sz = fp_pnt_cnt;
return fp_pnt_pnts;
}
static void
d_bz_line (void)
{
gint i, x0, y0, x1, y1;
g_assert ((fp_pnt_cnt % 4) == 0);
for (i = 0 ; i < fp_pnt_cnt; i += 4)
{
x0 = fp_pnt_pnts[i];
y0 = fp_pnt_pnts[i + 1];
x1 = fp_pnt_pnts[i + 2];
y1 = fp_pnt_pnts[i + 3];
gfig_draw_line (x0, y0, x1, y1);
}
}
/* Return points to plot */
/* Terminate by point with DBL_MAX, DBL_MAX */
static void
DrawBezier (fp_pnt points,
gint np,
gdouble mid,
gint depth)
{
gint i, j, x0 = 0, y0 = 0, x1, y1;
fp_pnt left;
fp_pnt right;
if (depth == 0) /* draw polyline */
{
for (i = 0; i < np; i++)
{
x1 = (int) points[i][0];
y1 = (int) points[i][1];
if (i > 0 && (x1 != x0 || y1 != y0))
{
/* Add pnts up */
fp_pnt_add ((gdouble) x0, (gdouble) y0,
(gdouble) x1, (gdouble) y1);
}
x0 = x1;
y0 = y1;
}
}
else /* subdivide control points at mid */
{
left = (fp_pnt)g_new (gdouble, np * 2);
right = (fp_pnt)g_new (gdouble, np * 2);
for (i = 0; i < np; i++)
{
right[i][0] = points[i][0];
right[i][1] = points[i][1];
}
left[0][0] = right[0][0];
left[0][1] = right[0][1];
for (j = np - 1; j >= 1; j--)
{
for (i = 0; i < j; i++)
{
right[i][0] = (1 - mid) * right[i][0] + mid * right[i + 1][0];
right[i][1] = (1 - mid) * right[i][1] + mid * right[i + 1][1];
}
left[np - j][0] = right[0][0];
left[np - j][1] = right[0][1];
}
if (depth > 0)
{
DrawBezier (left, np, mid, depth - 1);
DrawBezier (right, np, mid, depth - 1);
g_free (left);
g_free (right);
}
}
}
void
d_draw_bezier (GfigObject *obj)
{
DobjPoints *spnt;
gint seg_count = 0;
gint i = 0;
gdouble (*line_pnts)[2];
spnt = obj->points;
/* First count the number of points */
for (spnt = obj->points; spnt; spnt = spnt->next)
seg_count++;
if (!seg_count)
return; /* no-line */
line_pnts = (fp_pnt) g_new0 (gdouble, 2 * seg_count + 1);
/* Go around all the points drawing a line from one to the next */
for (spnt = obj->points; spnt; spnt = spnt->next)
{
draw_sqr (&spnt->pnt, obj == gfig_context->selected_obj);
line_pnts[i][0] = spnt->pnt.x;
line_pnts[i][1] = spnt->pnt.y;
i++;
}
/* Generate an array of doubles which are the control points */
if (bezier_line_frame && tmp_bezier)
{
fp_pnt_start ();
DrawBezier (line_pnts, seg_count, 0.5, 0);
d_bz_line ();
}
fp_pnt_start ();
DrawBezier (line_pnts, seg_count, 0.5, 3);
d_bz_line ();
/*bezier4 (line_pnts, seg_count, 20);*/
g_free (line_pnts);
}
static void
d_paint_bezier (GfigObject *obj)
{
gdouble *line_pnts;
gdouble (*bz_line_pnts)[2];
DobjPoints *spnt;
gint seg_count = 0;
gint i = 0;
/* First count the number of points */
for (spnt = obj->points; spnt; spnt = spnt->next)
seg_count++;
if (!seg_count)
return; /* no-line */
bz_line_pnts = (fp_pnt) g_new0 (gdouble, 2 * seg_count + 1);
/* Go around all the points drawing a line from one to the next */
for (spnt = obj->points; spnt; spnt = spnt->next)
{
bz_line_pnts[i][0] = spnt->pnt.x;
bz_line_pnts[i][1] = spnt->pnt.y;
i++;
}
fp_pnt_start ();
DrawBezier (bz_line_pnts, seg_count, 0.5, 5);
line_pnts = d_bz_get_array (&i);
/* Scale before drawing */
if (selvals.scaletoimage)
scale_to_original_xy (&line_pnts[0], i / 2);
else
scale_to_xy (&line_pnts[0], i / 2);
/* One go */
if (obj->style.paint_type == PAINT_BRUSH_TYPE)
{
gfig_paint (selvals.brshtype,
gfig_context->drawable_id,
i, line_pnts);
}
g_free (bz_line_pnts);
/* Don't free line_pnts - may need again */
}
static GfigObject *
d_copy_bezier (GfigObject *obj)
{
GfigObject *np;
g_assert (obj->type == BEZIER);
np = d_new_object (BEZIER, obj->points->pnt.x, obj->points->pnt.y);
np->points->next = d_copy_dobjpoints (obj->points->next);
np->type_data = obj->type_data;
return np;
}
void
d_bezier_object_class_init (void)
{
GfigObjectClass *class = &dobj_class[BEZIER];
class->type = BEZIER;
class->name = "BEZIER";
class->drawfunc = d_draw_bezier;
class->paintfunc = d_paint_bezier;
class->copyfunc = d_copy_bezier;
class->update = d_update_bezier;
}
static void
d_update_bezier (GdkPoint *pnt)
{
DobjPoints *s_pnt, *l_pnt;
gint saved_cnt_pnt = selvals.opts.showcontrol;
g_assert (tmp_bezier != NULL);
/* Undraw last one then draw new one */
s_pnt = tmp_bezier->points;
if (!s_pnt)
return; /* No points */
/* Hack - turn off cnt points in draw routine
*/
if ((l_pnt = s_pnt->next))
{
/* Undraw */
while (l_pnt->next)
{
l_pnt = l_pnt->next;
}
draw_circle (&l_pnt->pnt, TRUE);
selvals.opts.showcontrol = 0;
d_draw_bezier (tmp_bezier);
l_pnt->pnt = *pnt;
}
else
{
/* Radius is a few pixels away */
/* First edge point */
d_pnt_add_line (tmp_bezier, pnt->x, pnt->y,-1);
l_pnt = s_pnt->next;
}
/* draw it */
selvals.opts.showcontrol = 0;
d_draw_bezier (tmp_bezier);
selvals.opts.showcontrol = saved_cnt_pnt;
/* Realy draw the control points */
draw_circle (&l_pnt->pnt, TRUE);
}
void
d_bezier_start (GdkPoint *pnt,
gboolean shift_down)
{
if (!tmp_bezier)
{
/* New curve */
tmp_bezier = obj_creating = d_new_object (BEZIER, pnt->x, pnt->y);
}
}
void
d_bezier_end (GdkPoint *pnt,
gboolean shift_down)
{
DobjPoints *l_pnt;
if (!tmp_bezier)
{
tmp_bezier = obj_creating;
}
l_pnt = tmp_bezier->points->next;
if (!l_pnt)
return;
if (shift_down)
{
/* Undraw circle on last pnt */
while (l_pnt->next)
{
l_pnt = l_pnt->next;
}
if (l_pnt)
{
draw_circle (&l_pnt->pnt, TRUE);
draw_sqr (&l_pnt->pnt, TRUE);
if (bezier_closed)
{
gboolean tmp_frame = bezier_line_frame;
/* if closed then add first point */
d_draw_bezier (tmp_bezier);
d_pnt_add_line (tmp_bezier,
tmp_bezier->points->pnt.x,
tmp_bezier->points->pnt.y, -1);
/* Final has no frame */
bezier_line_frame = FALSE;
d_draw_bezier (tmp_bezier);
bezier_line_frame = tmp_frame; /* What is was */
}
else if (bezier_line_frame)
{
gboolean tmp_frame = bezier_line_frame;
d_draw_bezier (tmp_bezier);
bezier_line_frame = FALSE;
d_draw_bezier (tmp_bezier);
bezier_line_frame = tmp_frame; /* What is was */
}
add_to_all_obj (gfig_context->current_obj, obj_creating);
}
/* small mem leak if !l_pnt ? */
tmp_bezier = NULL;
obj_creating = NULL;
}
else
{
if (!tmp_bezier->points->next)
{
draw_circle (&tmp_bezier->points->pnt, TRUE);
draw_sqr (&tmp_bezier->points->pnt, TRUE);
}
d_draw_bezier (tmp_bezier);
d_pnt_add_line (tmp_bezier, pnt->x, pnt->y,-1);
d_draw_bezier (tmp_bezier);
}
}
void
tool_options_bezier (GtkWidget *notebook)
{
GtkWidget *vbox;
GtkWidget *toggle;
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), vbox, NULL);
gtk_widget_show (vbox);
toggle = gtk_check_button_new_with_label (_("Closed"));
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&bezier_closed);
gimp_help_set_help_data (toggle,
_("Close curve on completion"), NULL);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), bezier_closed);
gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
gtk_widget_show (toggle);
toggle = gtk_check_button_new_with_label (_("Show Line Frame"));
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&bezier_line_frame);
gimp_help_set_help_data (toggle,
_("Draws lines between the control points. "
"Only during curve creation"), NULL);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), bezier_line_frame);
gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
gtk_widget_show (toggle);
}
|