2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This is a plug-in for the GIMP.
6
* Generates images containing vector type drawings.
8
* Copyright (C) 1997 Andy Thomas alt@picnic.demon.co.uk
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 2 of the License, or
13
* (at your option) any later version.
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.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38
#include <libgimp/gimp.h>
39
#include <libgimp/gimpui.h>
41
#include "libgimp/stdplugins-intl.h"
44
#include "gfig-style.h"
45
#include "gfig-dialog.h"
47
#include "gfig-bezier.h"
48
#include "gfig-circle.h"
49
#include "gfig-dobject.h"
50
#include "gfig-ellipse.h"
51
#include "gfig-grid.h"
52
#include "gfig-line.h"
53
#include "gfig-poly.h"
54
#include "gfig-preview.h"
55
#include "gfig-spiral.h"
56
#include "gfig-star.h"
57
#include "gfig-stock.h"
59
/***** Magic numbers *****/
61
#define GFIG_HEADER "GFIG Version 0.2\n"
63
static void query (void);
64
static void run (const gchar *name,
66
const GimpParam *param,
68
GimpParam **return_vals);
71
GimpPlugInInfo PLUG_IN_INFO =
75
query, /* query_proc */
83
gint obj_show_single = -1; /* -1 all >= 0 object number */
85
/* Structures etc for the objects */
86
/* Points used to draw the object */
89
GfigObject *obj_creating; /* Object we are creating */
90
GfigObject *tmp_line; /* Needed when drawing lines */
92
gboolean need_to_scale;
94
static gint load_options (GFigObj *gfig,
100
/* Stuff for the preview bit */
101
static gint sel_x1, sel_y1, sel_x2, sel_y2;
102
static gint sel_width, sel_height;
103
gint preview_width, preview_height;
104
gdouble scale_x_factor, scale_y_factor;
105
GdkPixbuf *back_pixbuf = NULL;
112
static GimpParamDef args[] =
114
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
115
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
116
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
117
{ GIMP_PDB_INT32, "dummy", "dummy" }
120
gimp_install_procedure ("plug_in_gfig",
121
"Create Geometrical shapes with the Gimp",
122
"Draw Vector Graphics and paint them onto your images. "
123
"Gfig allows you to draw many types of objects "
124
"inlcuding Lines, Circles, Ellipses, Curves, Polygons, "
125
"pointed stars, Bezier curves, and Spirals. "
126
"Objects can be painted using Brushes or other tools"
127
"or filled using colours or patterns. "
128
"Gfig objects can also be used to create selections. ",
135
G_N_ELEMENTS (args), 0,
138
gimp_plugin_menu_register ("plug_in_gfig", "<Image>/Filters/Render");
142
run (const gchar *name,
144
const GimpParam *param,
146
GimpParam **return_vals)
148
GimpParam *values = g_new (GimpParam, 1);
149
GimpDrawable *drawable;
150
GimpRunMode run_mode;
151
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
152
gint pwidth, pheight;
155
gfig_context = g_new (GFigContext, 1);
156
gfig_context->show_background = TRUE;
157
gfig_context->selected_obj = NULL;
159
run_mode = param[0].data.d_int32;
161
gfig_context->image_id = param[1].data.d_image;
162
gfig_context->drawable_id = param[2].data.d_drawable;
165
*return_vals = values;
167
values[0].type = GIMP_PDB_STATUS;
168
values[0].data.d_status = status;
170
gimp_image_undo_group_start (gfig_context->image_id);
172
gimp_context_push ();
174
drawable = gimp_drawable_get (param[2].data.d_drawable);
176
/* TMP Hack - clear any selections */
177
if (! gimp_selection_is_empty (gfig_context->image_id))
178
gimp_selection_none (gfig_context->image_id);
180
gimp_drawable_mask_bounds (drawable->drawable_id,
181
&sel_x1, &sel_y1, &sel_x2, &sel_y2);
183
sel_width = sel_x2 - sel_x1;
184
sel_height = sel_y2 - sel_y1;
187
/* Calculate preview size */
189
if (sel_width > sel_height)
191
pwidth = MIN (sel_width, PREVIEW_SIZE);
192
pheight = sel_height * pwidth / sel_width;
196
pheight = MIN (sel_height, PREVIEW_SIZE);
197
pwidth = sel_width * pheight / sel_height;
201
preview_width = MAX (pwidth, 2); /* Min size is 2 */
202
preview_height = MAX (pheight, 2);
204
org_scale_x_factor = scale_x_factor =
205
(gdouble) sel_width / (gdouble) preview_width;
206
org_scale_y_factor = scale_y_factor =
207
(gdouble) sel_height / (gdouble) preview_height;
209
gimp_tile_cache_ntiles ((drawable->width + gimp_tile_width () - 1) /
212
gimp_drawable_detach (drawable);
215
gfig_init_object_classes ();
219
case GIMP_RUN_INTERACTIVE:
220
case GIMP_RUN_WITH_LAST_VALS:
221
/*gimp_get_data ("plug_in_gfig", &selvals);*/
222
if (! gfig_dialog ())
224
gimp_drawable_detach (gfig_drawable);
225
gimp_image_undo_group_end (gfig_context->image_id);
231
case GIMP_RUN_NONINTERACTIVE:
232
status = GIMP_PDB_CALLING_ERROR;
241
gimp_image_undo_group_end (gfig_context->image_id);
243
if (run_mode != GIMP_RUN_NONINTERACTIVE)
244
gimp_displays_flush ();
247
if (run_mode == GIMP_RUN_INTERACTIVE)
248
gimp_set_data ("plug_in_gfig", &selvals, sizeof (SelectItVals));
252
status = GIMP_PDB_EXECUTION_ERROR;
255
values[0].data.d_status = status;
257
/* gimp_drawable_detach (drawable) already done above, don't do it twice */
261
Translate SPACE to "\\040", etc.
262
Taken from gflare plugin
265
gfig_name_encode (gchar *dest,
268
gint cnt = MAX_LOAD_LINE - 1;
270
while (*src && cnt--)
272
if (g_ascii_iscntrl (*src) || g_ascii_isspace (*src) || *src == '\\')
274
sprintf (dest, "\\%03o", *src++);
284
Translate "\\040" to SPACE, etc.
287
gfig_name_decode (gchar *dest,
290
gint cnt = MAX_LOAD_LINE - 1;
293
while (*src && cnt--)
295
if (*src == '\\' && *(src+1) && *(src+2) && *(src+3))
297
sscanf (src+1, "%3o", &tmp);
309
* Load all gfig, which are founded in gfig-path-list, into gfig_list.
310
* gfig-path-list must be initialized first. (plug_in_parse_gfig_path ())
311
* based on code from Gflare.
315
gfig_list_pos (GFigObj *gfig)
323
for (tmp = gfig_list; tmp; tmp = g_list_next (tmp))
327
if (strcmp (gfig->draw_name, g->draw_name) <= 0)
336
* Insert gfigs in alphabetical order
340
gfig_list_insert (GFigObj *gfig)
344
n = gfig_list_pos (gfig);
346
gfig_list = g_list_insert (gfig_list, gfig, n);
352
gfig_free (GFigObj *gfig)
354
g_assert (gfig != NULL);
356
free_all_objs (gfig->obj_list);
359
g_free (gfig->filename);
360
g_free (gfig->draw_name);
368
return g_new0 (GFigObj, 1);
372
gfig_load_objs (GFigObj *gfig,
377
gchar load_buf[MAX_LOAD_LINE];
382
while (load_count-- > 0)
385
get_line (load_buf, MAX_LOAD_LINE, fp, 0);
389
gfig_skip_style (&style, fp);
391
obj = d_load_object (load_buf, fp);
395
add_to_all_obj (gfig, obj);
396
offset2 = ftell (fp);
397
fseek (fp, offset, SEEK_SET);
398
gfig_load_style (&obj->style, fp);
399
fseek (fp, offset2, SEEK_SET);
403
g_message ("Failed to load object, load count = %d", load_count);
409
gfig_load (const gchar *filename,
414
gchar load_buf[MAX_LOAD_LINE];
415
gchar str_buf[MAX_LOAD_LINE];
422
g_assert (filename != NULL);
425
printf ("Loading %s (%s)\n", filename, name);
428
fp = fopen (filename, "r");
431
g_message (_("Could not open '%s' for reading: %s"),
432
gimp_filename_to_utf8 (filename), g_strerror (errno));
438
gfig->name = g_strdup (name);
439
gfig->filename = g_strdup (filename);
448
get_line (load_buf, MAX_LOAD_LINE, fp, 1);
450
sscanf (load_buf, "%10s %10s %lf", magic1, magic2, &version);
452
if (strcmp (magic1, "GFIG") || strcmp (magic2, "Version"))
454
g_message ("File '%s' is not a gfig file",
455
gimp_filename_to_utf8 (gfig->filename));
459
get_line (load_buf, MAX_LOAD_LINE, fp, 0);
460
sscanf (load_buf, "Name: %100s", str_buf);
461
gfig_name_decode (load_buf, str_buf);
462
gfig->draw_name = g_strdup (load_buf);
464
get_line (load_buf, MAX_LOAD_LINE, fp, 0);
465
if (strncmp (load_buf, "Version: ", 9) == 0)
466
gfig->version = g_ascii_strtod (load_buf + 9, NULL);
468
get_line (load_buf, MAX_LOAD_LINE, fp, 0);
469
sscanf (load_buf, "ObjCount: %d", &load_count);
471
if (load_options (gfig, fp))
473
g_message ("File '%s' corrupt file - Line %d Option section incorrect",
474
gimp_filename_to_utf8 (filename), line_no);
478
if (gfig_load_styles (gfig, fp))
480
g_message ("File '%s' corrupt file - Line %d Option section incorrect",
481
gimp_filename_to_utf8 (filename), line_no);
487
gfig_load_objs (gfig, load_count, fp);
491
chk_count = g_list_length (gfig->obj_list);
493
if (chk_count != load_count)
495
g_message ("File '%s' corrupt file - Line %d Object count to small",
496
gimp_filename_to_utf8 (filename), line_no);
502
if (!gfig_context->current_obj)
503
gfig_context->current_obj = gfig;
505
gfig->obj_status = GFIG_OK;
511
save_options (GString *string)
514
g_string_append_printf (string, "<OPTIONS>\n");
515
g_string_append_printf (string, "GridSpacing: %d\n",
516
selvals.opts.gridspacing);
517
if (selvals.opts.gridtype == RECT_GRID)
519
g_string_append_printf (string, "GridType: RECT_GRID\n");
521
else if (selvals.opts.gridtype == POLAR_GRID)
523
g_string_append_printf (string, "GridType: POLAR_GRID\n");
525
else if (selvals.opts.gridtype == ISO_GRID)
527
g_string_append_printf (string, "GridType: ISO_GRID\n");
531
/* default to RECT_GRID */
532
g_string_append_printf (string, "GridType: RECT_GRID\n");
535
g_string_append_printf (string, "DrawGrid: %s\n",
536
(selvals.opts.drawgrid) ? "TRUE" : "FALSE");
537
g_string_append_printf (string, "Snap2Grid: %s\n",
538
(selvals.opts.snap2grid) ? "TRUE" : "FALSE");
539
g_string_append_printf (string, "LockOnGrid: %s\n",
540
(selvals.opts.lockongrid) ? "TRUE" : "FALSE");
541
g_string_append_printf (string, "ShowControl: %s\n",
542
(selvals.opts.showcontrol) ? "TRUE" : "FALSE");
543
g_string_append_printf (string, "</OPTIONS>\n");
547
gfig_save_obj_start (GfigObject *obj,
550
g_string_append_printf (string, "<%s ", obj->class->name);
551
gfig_style_save_as_attributes (&obj->style, string);
552
g_string_append_printf (string, ">\n");
556
gfig_save_obj_end (GfigObject *obj,
559
g_string_append_printf (string, "</%s>\n",obj->class->name);
563
load_bool (gchar *opt_buf,
566
if (!strcmp (opt_buf, "TRUE"))
568
else if (!strcmp (opt_buf, "FALSE"))
577
load_options (GFigObj *gfig,
580
gchar load_buf[MAX_LOAD_LINE];
581
gchar str_buf[MAX_LOAD_LINE];
582
gchar opt_buf[MAX_LOAD_LINE];
584
get_line (load_buf, MAX_LOAD_LINE, fp, 0);
587
printf ("load '%s'\n", load_buf);
590
if (strcmp (load_buf, "<OPTIONS>"))
593
get_line (load_buf, MAX_LOAD_LINE, fp, 0);
596
printf ("opt line '%s'\n", load_buf);
599
while (strcmp (load_buf, "</OPTIONS>"))
601
/* Get option name */
603
printf ("num = %d\n", sscanf (load_buf, "%s %s", str_buf, opt_buf));
605
printf ("option %s val %s\n", str_buf, opt_buf);
607
sscanf (load_buf, "%s %s", str_buf, opt_buf);
610
if (!strcmp (str_buf, "GridSpacing:"))
612
/* Value is decimal */
617
gfig->opts.gridspacing = sp;
619
else if (!strcmp (str_buf, "DrawGrid:"))
622
if (load_bool (opt_buf, &gfig->opts.drawgrid))
625
else if (!strcmp (str_buf, "Snap2Grid:"))
628
if (load_bool (opt_buf, &gfig->opts.snap2grid))
631
else if (!strcmp (str_buf, "LockOnGrid:"))
634
if (load_bool (opt_buf, &gfig->opts.lockongrid))
637
else if (!strcmp (str_buf, "ShowControl:"))
640
if (load_bool (opt_buf, &gfig->opts.showcontrol))
643
else if (!strcmp (str_buf, "GridType:"))
645
/* Value is string */
646
if (!strcmp (opt_buf, "RECT_GRID"))
647
gfig->opts.gridtype = RECT_GRID;
648
else if (!strcmp (opt_buf, "POLAR_GRID"))
649
gfig->opts.gridtype = POLAR_GRID;
650
else if (!strcmp (opt_buf, "ISO_GRID"))
651
gfig->opts.gridtype = ISO_GRID;
656
get_line (load_buf, MAX_LOAD_LINE, fp, 0);
659
printf ("opt line '%s'\n", load_buf);
666
gfig_save_as_string (void)
670
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
671
gchar conv_buf[MAX_LOAD_LINE * 3 + 1];
674
string = g_string_new (GFIG_HEADER);
676
gfig_name_encode (conv_buf, gfig_context->current_obj->draw_name);
677
g_string_append_printf (string, "Name: %s\n", conv_buf);
678
g_string_append_printf (string, "Version: %s\n",
679
g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
680
gfig_context->current_obj->version));
681
objs = gfig_context->current_obj->obj_list;
683
count = g_list_length (objs);
685
g_string_append_printf (string, "ObjCount: %d\n", count);
687
save_options (string);
689
gfig_save_styles (string);
691
for (objs = gfig_context->current_obj->obj_list;
693
objs = g_list_next (objs))
695
GfigObject *object = objs->data;
697
gfig_save_obj_start (object, string);
699
gfig_save_style (&object->style, string);
702
d_save_object (object, string);
704
gfig_save_obj_end (object, string);
712
gfig_save_as_parasite (void)
714
GimpParasite *parasite;
717
string = gfig_save_as_string ();
719
parasite = gimp_parasite_new ("gfig", GIMP_PARASITE_PERSISTENT | GIMP_PARASITE_UNDOABLE,
720
string->len, string->str);
722
g_string_free (string, TRUE);
724
if (!gimp_drawable_parasite_attach (gfig_context->drawable_id, parasite))
726
g_message (_("Error trying to save figure as a parasite: can't attach parasite to drawable.\n"));
727
gimp_parasite_free (parasite);
731
gimp_parasite_free (parasite);
736
gfig_load_from_parasite (void)
740
GimpParasite *parasite;
743
parasite = gimp_drawable_parasite_find (gfig_context->drawable_id, "gfig");
747
fname = gimp_temp_name ("gfigtmp");
749
fp = fopen (fname, "w");
752
g_message (_("Error trying to open temporary file '%s' "
753
"for parasite loading: %s"),
754
gimp_filename_to_utf8 (fname), g_strerror (errno));
758
fwrite (gimp_parasite_data (parasite),
760
gimp_parasite_data_size (parasite),
764
gimp_parasite_free (parasite);
766
gfig = gfig_load (fname, "(none)");
776
gfig_save_callbk (void)
782
savename = gfig_context->current_obj->filename;
784
fp = fopen (savename, "w+");
788
g_message (_("Could not open '%s' for writing: %s"),
789
gimp_filename_to_utf8 (savename), g_strerror (errno));
793
string = gfig_save_as_string ();
795
fwrite (string->str, string->len, 1, fp);
798
g_message ("Failed to write file\n");
801
gfig_context->current_obj->obj_status &= ~(GFIG_MODIFIED | GFIG_READONLY);