~ubuntu-branches/ubuntu/karmic/gimp/karmic-security

« back to all changes in this revision

Viewing changes to plug-ins/common/file-svg.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20081006133041-axco233xt49jobn7
Tags: 2.6.0-1ubuntu1
* Sync on debian and new version (lp: #276839)
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch:
  - updated some strings for ubuntu
* debian/rules:
  - updated translation templates

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
/* SVG loader plug-in
 
20
 * (C) Copyright 2003  Dom Lachowicz <cinamod@hotmail.com>
 
21
 *
 
22
 * Largely rewritten in September 2003 by Sven Neumann <sven@gimp.org>
 
23
 */
 
24
 
 
25
#include "config.h"
 
26
 
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
 
 
30
#include <librsvg/rsvg.h>
 
31
#include <librsvg/librsvg-features.h>  /* for version check */
 
32
 
 
33
#include "libgimp/gimp.h"
 
34
#include "libgimp/gimpui.h"
 
35
 
 
36
#include "libgimp/stdplugins-intl.h"
 
37
 
 
38
 
 
39
#define LOAD_PROC               "file-svg-load"
 
40
#define LOAD_THUMB_PROC         "file-svg-load-thumb"
 
41
#define PLUG_IN_BINARY          "file-svg"
 
42
#define SVG_VERSION             "2.5.0"
 
43
#define SVG_DEFAULT_RESOLUTION  90.0
 
44
#define SVG_DEFAULT_SIZE        500
 
45
#define SVG_PREVIEW_SIZE        128
 
46
 
 
47
 
 
48
typedef struct
 
49
{
 
50
  gdouble    resolution;
 
51
  gint       width;
 
52
  gint       height;
 
53
  gboolean   import;
 
54
  gboolean   merge;
 
55
} SvgLoadVals;
 
56
 
 
57
static SvgLoadVals load_vals =
 
58
{
 
59
  SVG_DEFAULT_RESOLUTION,
 
60
  0,
 
61
  0,
 
62
  FALSE,
 
63
  FALSE
 
64
};
 
65
 
 
66
 
 
67
static void  query (void);
 
68
static void  run   (const gchar      *name,
 
69
                    gint              nparams,
 
70
                    const GimpParam  *param,
 
71
                    gint             *nreturn_vals,
 
72
                    GimpParam       **return_vals);
 
73
 
 
74
static gint32              load_image        (const gchar  *filename,
 
75
                                              GError      **error);
 
76
static GdkPixbuf         * load_rsvg_pixbuf  (const gchar  *filename,
 
77
                                              SvgLoadVals  *vals,
 
78
                                             GError      **error);
 
79
static gboolean            load_rsvg_size    (const gchar  *filename,
 
80
                                              SvgLoadVals  *vals,
 
81
                                              GError      **error);
 
82
static GimpPDBStatusType   load_dialog       (const gchar  *filename,
 
83
                                              GError      **error);
 
84
 
 
85
 
 
86
const GimpPlugInInfo PLUG_IN_INFO =
 
87
{
 
88
  NULL,
 
89
  NULL,
 
90
  query,
 
91
  run,
 
92
};
 
93
 
 
94
MAIN ()
 
95
 
 
96
 
 
97
static void
 
98
query (void)
 
99
{
 
100
  static const GimpParamDef load_args[] =
 
101
  {
 
102
    { GIMP_PDB_INT32,  "run-mode",     "Interactive, non-interactive"        },
 
103
    { GIMP_PDB_STRING, "filename",     "The name of the file to load"        },
 
104
    { GIMP_PDB_STRING, "raw-filename", "The name of the file to load"        },
 
105
    { GIMP_PDB_FLOAT,  "resolution",
 
106
      "Resolution to use for rendering the SVG (defaults to 90 dpi)"         },
 
107
    { GIMP_PDB_INT32,  "width",
 
108
      "Width (in pixels) to load the SVG in. "
 
109
      "(0 for original width, a negative width to specify a maximum width)"  },
 
110
    { GIMP_PDB_INT32,  "height",
 
111
      "Height (in pixels) to load the SVG in. "
 
112
      "(0 for original height, a negative width to specify a maximum height)"},
 
113
    { GIMP_PDB_INT32,  "paths",
 
114
      "Whether to not import paths (0), import paths individually (1) "
 
115
      "or merge all imported paths (2)"                                      }
 
116
  };
 
117
  static const GimpParamDef load_return_vals[] =
 
118
  {
 
119
    { GIMP_PDB_IMAGE,  "image",        "Output image" }
 
120
  };
 
121
 
 
122
  static const GimpParamDef thumb_args[] =
 
123
  {
 
124
    { GIMP_PDB_STRING, "filename",     "The name of the file to load"  },
 
125
    { GIMP_PDB_INT32,  "thumb-size",   "Preferred thumbnail size"      }
 
126
  };
 
127
  static const GimpParamDef thumb_return_vals[] =
 
128
  {
 
129
    { GIMP_PDB_IMAGE,  "image",        "Thumbnail image"               },
 
130
    { GIMP_PDB_INT32,  "image-width",  "Width of full-sized image"     },
 
131
    { GIMP_PDB_INT32,  "image-height", "Height of full-sized image"    }
 
132
  };
 
133
 
 
134
  gimp_install_procedure (LOAD_PROC,
 
135
                          "Loads files in the SVG file format",
 
136
                          "Renders SVG files to raster graphics using librsvg.",
 
137
                          "Dom Lachowicz, Sven Neumann",
 
138
                          "Dom Lachowicz <cinamod@hotmail.com>",
 
139
                          SVG_VERSION,
 
140
                          N_("SVG image"),
 
141
                          NULL,
 
142
                          GIMP_PLUGIN,
 
143
                          G_N_ELEMENTS (load_args),
 
144
                          G_N_ELEMENTS (load_return_vals),
 
145
                          load_args, load_return_vals);
 
146
 
 
147
  gimp_register_file_handler_mime (LOAD_PROC, "image/svg+xml");
 
148
  gimp_register_magic_load_handler (LOAD_PROC,
 
149
                                    "svg", "",
 
150
                                    "0,string,<?xml,0,string,<svg");
 
151
 
 
152
  gimp_install_procedure (LOAD_THUMB_PROC,
 
153
                          "Loads a small preview from an SVG image",
 
154
                          "",
 
155
                          "Dom Lachowicz, Sven Neumann",
 
156
                          "Dom Lachowicz <cinamod@hotmail.com>",
 
157
                          SVG_VERSION,
 
158
                          NULL,
 
159
                          NULL,
 
160
                          GIMP_PLUGIN,
 
161
                          G_N_ELEMENTS (thumb_args),
 
162
                          G_N_ELEMENTS (thumb_return_vals),
 
163
                          thumb_args, thumb_return_vals);
 
164
 
 
165
  gimp_register_thumbnail_loader (LOAD_PROC, LOAD_THUMB_PROC);
 
166
}
 
167
 
 
168
static void
 
169
run (const gchar      *name,
 
170
     gint              nparams,
 
171
     const GimpParam  *param,
 
172
     gint             *nreturn_vals,
 
173
     GimpParam       **return_vals)
 
174
{
 
175
  static GimpParam   values[4];
 
176
  GimpRunMode        run_mode;
 
177
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
 
178
  GError            *error  = NULL;
 
179
 
 
180
  INIT_I18N ();
 
181
 
 
182
  run_mode = param[0].data.d_int32;
 
183
 
 
184
  *nreturn_vals = 1;
 
185
  *return_vals  = values;
 
186
 
 
187
  values[0].type          = GIMP_PDB_STATUS;
 
188
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
 
189
 
 
190
  /* MUST call this before any RSVG funcs */
 
191
  g_type_init ();
 
192
 
 
193
  if (strcmp (name, LOAD_PROC) == 0)
 
194
    {
 
195
      gimp_get_data (LOAD_PROC, &load_vals);
 
196
 
 
197
      switch (run_mode)
 
198
        {
 
199
        case GIMP_RUN_NONINTERACTIVE:
 
200
          if (nparams > 3)  load_vals.resolution = param[3].data.d_float;
 
201
          if (nparams > 4)  load_vals.width      = param[4].data.d_int32;
 
202
          if (nparams > 5)  load_vals.height     = param[5].data.d_int32;
 
203
          if (nparams > 6)
 
204
            {
 
205
              load_vals.import = param[6].data.d_int32 != FALSE;
 
206
              load_vals.merge  = param[6].data.d_int32 > TRUE;
 
207
            }
 
208
          break;
 
209
 
 
210
        case GIMP_RUN_INTERACTIVE:
 
211
          status = load_dialog (param[1].data.d_string, &error);
 
212
          break;
 
213
 
 
214
        case GIMP_RUN_WITH_LAST_VALS:
 
215
          break;
 
216
        }
 
217
 
 
218
      /* Don't clamp this; insane values are probably not meant to be
 
219
       * used as resolution anyway.
 
220
       */
 
221
      if (load_vals.resolution < GIMP_MIN_RESOLUTION ||
 
222
          load_vals.resolution > GIMP_MAX_RESOLUTION)
 
223
        {
 
224
          load_vals.resolution = SVG_DEFAULT_RESOLUTION;
 
225
        }
 
226
 
 
227
      if (status == GIMP_PDB_SUCCESS)
 
228
        {
 
229
          const gchar *filename = param[1].data.d_string;
 
230
          gint32       image_ID = load_image (filename, &error);
 
231
 
 
232
          if (image_ID != -1)
 
233
            {
 
234
              if (load_vals.import)
 
235
                {
 
236
                  gint32 *vectors;
 
237
                  gint    num_vectors;
 
238
 
 
239
                  gimp_vectors_import_from_file (image_ID, filename,
 
240
                                                 load_vals.merge, TRUE,
 
241
                                                 &num_vectors, &vectors);
 
242
                  if (num_vectors > 0)
 
243
                    g_free (vectors);
 
244
                }
 
245
 
 
246
              *nreturn_vals = 2;
 
247
 
 
248
              values[1].type         = GIMP_PDB_IMAGE;
 
249
              values[1].data.d_image = image_ID;
 
250
            }
 
251
          else
 
252
            {
 
253
              status = GIMP_PDB_EXECUTION_ERROR;
 
254
            }
 
255
 
 
256
          gimp_set_data (LOAD_PROC, &load_vals, sizeof (load_vals));
 
257
        }
 
258
     }
 
259
  else if (strcmp (name, LOAD_THUMB_PROC) == 0)
 
260
    {
 
261
      if (nparams < 2)
 
262
        {
 
263
          status = GIMP_PDB_CALLING_ERROR;
 
264
        }
 
265
      else
 
266
        {
 
267
          const gchar *filename = param[0].data.d_string;
 
268
          gint         width    = 0;
 
269
          gint         height   = 0;
 
270
          gint32       image_ID;
 
271
 
 
272
          if (load_rsvg_size (filename, &load_vals, NULL))
 
273
            {
 
274
              width  = load_vals.width;
 
275
              height = load_vals.height;
 
276
            }
 
277
 
 
278
          load_vals.resolution = SVG_DEFAULT_RESOLUTION;
 
279
          load_vals.width      = - param[1].data.d_int32;
 
280
          load_vals.height     = - param[1].data.d_int32;
 
281
 
 
282
          image_ID = load_image (filename, &error);
 
283
 
 
284
          if (image_ID != -1)
 
285
            {
 
286
              *nreturn_vals = 4;
 
287
              values[1].type         = GIMP_PDB_IMAGE;
 
288
              values[1].data.d_image = image_ID;
 
289
              values[2].type         = GIMP_PDB_INT32;
 
290
              values[2].data.d_int32 = width;
 
291
              values[3].type         = GIMP_PDB_INT32;
 
292
              values[3].data.d_int32 = height;
 
293
            }
 
294
          else
 
295
            {
 
296
              status = GIMP_PDB_EXECUTION_ERROR;
 
297
            }
 
298
        }
 
299
    }
 
300
  else
 
301
    {
 
302
      status = GIMP_PDB_CALLING_ERROR;
 
303
    }
 
304
 
 
305
  if (status != GIMP_PDB_SUCCESS && error)
 
306
    {
 
307
      *nreturn_vals = 2;
 
308
      values[1].type          = GIMP_PDB_STRING;
 
309
      values[1].data.d_string = error->message;
 
310
    }
 
311
 
 
312
  values[0].data.d_status = status;
 
313
}
 
314
 
 
315
static gint32
 
316
load_image (const gchar  *filename,
 
317
            GError      **load_error)
 
318
{
 
319
  gint32        image;
 
320
  gint32        layer;
 
321
  GdkPixbuf    *pixbuf;
 
322
  gint          width;
 
323
  gint          height;
 
324
  GError       *error = NULL;
 
325
 
 
326
  pixbuf = load_rsvg_pixbuf (filename, &load_vals, &error);
 
327
 
 
328
  if (! pixbuf)
 
329
    {
 
330
      /*  Do not rely on librsvg setting GError on failure!  */
 
331
      g_set_error (load_error,
 
332
                   error ? error->domain : 0, error ? error->code : 0,
 
333
                   _("Could not open '%s' for reading: %s"),
 
334
                   gimp_filename_to_utf8 (filename),
 
335
                   error ? error->message : _("Unknown reason"));
 
336
      g_clear_error (&error);
 
337
 
 
338
      return -1;
 
339
    }
 
340
 
 
341
  gimp_progress_init (_("Rendering SVG"));
 
342
 
 
343
  width  = gdk_pixbuf_get_width (pixbuf);
 
344
  height = gdk_pixbuf_get_height (pixbuf);
 
345
 
 
346
  image = gimp_image_new (width, height, GIMP_RGB);
 
347
  gimp_image_undo_disable (image);
 
348
 
 
349
  gimp_image_set_filename (image, filename);
 
350
  gimp_image_set_resolution (image,
 
351
                             load_vals.resolution, load_vals.resolution);
 
352
 
 
353
  layer = gimp_layer_new_from_pixbuf (image, _("Rendered SVG"), pixbuf,
 
354
                                      100, GIMP_NORMAL_MODE, 0.0, 1.0);
 
355
  gimp_image_add_layer (image, layer, 0);
 
356
 
 
357
  gimp_image_undo_enable (image);
 
358
 
 
359
  return image;
 
360
}
 
361
 
 
362
/*  This is the callback used from load_rsvg_pixbuf().  */
 
363
static void
 
364
load_set_size_callback (gint     *width,
 
365
                        gint     *height,
 
366
                        gpointer  data)
 
367
{
 
368
  SvgLoadVals *vals = data;
 
369
 
 
370
  if (*width < 1 || *height < 1)
 
371
    {
 
372
      *width  = SVG_DEFAULT_SIZE;
 
373
      *height = SVG_DEFAULT_SIZE;
 
374
    }
 
375
 
 
376
  if (!vals->width || !vals->height)
 
377
    return;
 
378
 
 
379
  /*  either both arguments negative or none  */
 
380
  if ((vals->width * vals->height) < 0)
 
381
    return;
 
382
 
 
383
  if (vals->width > 0)
 
384
    {
 
385
      *width  = vals->width;
 
386
      *height = vals->height;
 
387
    }
 
388
  else
 
389
    {
 
390
      gdouble w      = *width;
 
391
      gdouble h      = *height;
 
392
      gdouble aspect = (gdouble) vals->width / (gdouble) vals->height;
 
393
 
 
394
      if (aspect > (w / h))
 
395
        {
 
396
          *height = abs (vals->height);
 
397
          *width  = (gdouble) abs (vals->width) * (w / h) + 0.5;
 
398
        }
 
399
      else
 
400
        {
 
401
          *width  = abs (vals->width);
 
402
          *height = (gdouble) abs (vals->height) / (w / h) + 0.5;
 
403
        }
 
404
 
 
405
      vals->width  = *width;
 
406
      vals->height = *height;
 
407
    }
 
408
}
 
409
 
 
410
static RsvgHandle *
 
411
load_rsvg_handle_new (gdouble xres,
 
412
                      gdouble yres)
 
413
{
 
414
  RsvgHandle *handle = rsvg_handle_new ();
 
415
 
 
416
#if (((LIBRSVG_MAJOR_VERSION == 2) && (LIBRSVG_MINOR_VERSION == 13) && \
 
417
     ((LIBRSVG_MICRO_VERSION == 0) || \
 
418
      (LIBRSVG_MICRO_VERSION == 1) || \
 
419
      (LIBRSVG_MICRO_VERSION == 2))) || \
 
420
     ((LIBRSVG_MAJOR_VERSION == 2) && (LIBRSVG_MINOR_VERSION == 11) && \
 
421
      (LIBRSVG_MICRO_VERSION == 0)))
 
422
  rsvg_handle_set_dpi (handle, xres, yres);
 
423
#else
 
424
  rsvg_handle_set_dpi_x_y (handle, xres, yres);
 
425
#endif
 
426
 
 
427
  return handle;
 
428
}
 
429
 
 
430
/*  This function renders a pixbuf from an SVG file according to vals.  */
 
431
static GdkPixbuf *
 
432
load_rsvg_pixbuf (const gchar  *filename,
 
433
                  SvgLoadVals  *vals,
 
434
                  GError      **error)
 
435
{
 
436
  GdkPixbuf  *pixbuf  = NULL;
 
437
  RsvgHandle *handle;
 
438
  GIOChannel *io;
 
439
  gchar      *uri;
 
440
  GIOStatus   status  = G_IO_STATUS_NORMAL;
 
441
  gboolean    success = TRUE;
 
442
 
 
443
  io = g_io_channel_new_file (filename, "r", error);
 
444
  if (!io)
 
445
    return NULL;
 
446
 
 
447
  g_io_channel_set_encoding (io, NULL, NULL);
 
448
 
 
449
  handle = load_rsvg_handle_new (vals->resolution, vals->resolution);
 
450
 
 
451
  /*  set the base URI so that librsvg can resolve relative paths  */
 
452
  uri = g_filename_to_uri (filename, NULL, NULL);
 
453
  if (uri)
 
454
    {
 
455
      gchar *p = strrchr (uri, '/');
 
456
 
 
457
      if (p)
 
458
        *p = '\0';
 
459
 
 
460
      rsvg_handle_set_base_uri (handle, uri);
 
461
      g_free (uri);
 
462
    }
 
463
 
 
464
  rsvg_handle_set_size_callback (handle, load_set_size_callback, vals, NULL);
 
465
 
 
466
  while (success && status != G_IO_STATUS_EOF)
 
467
    {
 
468
      gchar  buf[8192];
 
469
      gsize  len;
 
470
 
 
471
      status = g_io_channel_read_chars (io, buf, sizeof (buf), &len, error);
 
472
 
 
473
      switch (status)
 
474
        {
 
475
        case G_IO_STATUS_ERROR:
 
476
          success = FALSE;
 
477
          break;
 
478
        case G_IO_STATUS_EOF:
 
479
          success = rsvg_handle_close (handle, error);
 
480
          break;
 
481
        case G_IO_STATUS_NORMAL:
 
482
          success = rsvg_handle_write (handle,
 
483
                                       (const guchar *) buf, len, error);
 
484
          break;
 
485
        case G_IO_STATUS_AGAIN:
 
486
          break;
 
487
        }
 
488
    }
 
489
 
 
490
  g_io_channel_unref (io);
 
491
 
 
492
  if (success)
 
493
    pixbuf = rsvg_handle_get_pixbuf (handle);
 
494
 
 
495
  rsvg_handle_free (handle);
 
496
 
 
497
  return pixbuf;
 
498
}
 
499
 
 
500
static GtkWidget *size_label = NULL;
 
501
 
 
502
/*  This function retrieves the pixel size from an SVG file. Parsing
 
503
 *  stops after the first chunk that provided the parser with enough
 
504
 *  information to determine the size. This is usally the opening
 
505
 *  <svg> element and should thus be in the first chunk (1024 bytes).
 
506
 */
 
507
static gboolean
 
508
load_rsvg_size (const gchar  *filename,
 
509
                SvgLoadVals  *vals,
 
510
                GError      **error)
 
511
{
 
512
  RsvgHandle *handle;
 
513
  GIOChannel *io;
 
514
  GIOStatus   status  = G_IO_STATUS_NORMAL;
 
515
  gboolean    success = TRUE;
 
516
  gboolean    done    = FALSE;
 
517
 
 
518
  io = g_io_channel_new_file (filename, "r", error);
 
519
  if (!io)
 
520
    return FALSE;
 
521
 
 
522
  g_io_channel_set_encoding (io, NULL, NULL);
 
523
 
 
524
  handle = load_rsvg_handle_new (vals->resolution, vals->resolution);
 
525
 
 
526
  vals->width  = SVG_DEFAULT_SIZE;
 
527
  vals->height = SVG_DEFAULT_SIZE;
 
528
 
 
529
  while (success && status != G_IO_STATUS_EOF && (! done))
 
530
    {
 
531
      gchar                 buf[1024];
 
532
      gsize                 len;
 
533
      RsvgDimensionData     dim = { 0, 0, 0.0, 0.0 };
 
534
 
 
535
      status = g_io_channel_read_chars (io, buf, sizeof (buf), &len, error);
 
536
 
 
537
      switch (status)
 
538
        {
 
539
        case G_IO_STATUS_ERROR:
 
540
          success = FALSE;
 
541
          break;
 
542
        case G_IO_STATUS_EOF:
 
543
          success = rsvg_handle_close (handle, error);
 
544
          break;
 
545
        case G_IO_STATUS_NORMAL:
 
546
          success = rsvg_handle_write (handle,
 
547
                                       (const guchar *) buf, len, error);
 
548
          rsvg_handle_get_dimensions (handle, &dim);
 
549
 
 
550
          if (dim.width > 0 && dim.height > 0)
 
551
            {
 
552
              vals->width  = dim.width;
 
553
              vals->height = dim.height;
 
554
 
 
555
              done = TRUE;
 
556
            }
 
557
          break;
 
558
        case G_IO_STATUS_AGAIN:
 
559
          break;
 
560
        }
 
561
    }
 
562
 
 
563
    if (size_label)
 
564
      {
 
565
        if (done)
 
566
          {
 
567
            gchar *text = g_strdup_printf (_("%d × %d"),
 
568
                                           vals->width, vals->height);
 
569
            gtk_label_set_text (GTK_LABEL (size_label), text);
 
570
            g_free (text);
 
571
          }
 
572
        else
 
573
          {
 
574
            gtk_label_set_text (GTK_LABEL (size_label),
 
575
                                _("SVG file does not\nspecify a size!"));
 
576
          }
 
577
      }
 
578
 
 
579
  g_io_channel_unref (io);
 
580
  rsvg_handle_free (handle);
 
581
 
 
582
  if (vals->width  < 1)  vals->width  = 1;
 
583
  if (vals->height < 1)  vals->height = 1;
 
584
 
 
585
  return success;
 
586
}
 
587
 
 
588
 
 
589
/*  User interface  */
 
590
 
 
591
static GimpSizeEntry *size       = NULL;
 
592
static GtkObject     *xadj       = NULL;
 
593
static GtkObject     *yadj       = NULL;
 
594
static GtkWidget     *constrain  = NULL;
 
595
static gdouble        ratio_x    = 1.0;
 
596
static gdouble        ratio_y    = 1.0;
 
597
static gint           svg_width  = 0;
 
598
static gint           svg_height = 0;
 
599
 
 
600
static void  load_dialog_set_ratio (gdouble x,
 
601
                                    gdouble y);
 
602
 
 
603
 
 
604
static void
 
605
load_dialog_size_callback (GtkWidget *widget,
 
606
                           gpointer   data)
 
607
{
 
608
  if (gimp_chain_button_get_active (GIMP_CHAIN_BUTTON (constrain)))
 
609
    {
 
610
      gdouble x = gimp_size_entry_get_refval (size, 0) / (gdouble) svg_width;
 
611
      gdouble y = gimp_size_entry_get_refval (size, 1) / (gdouble) svg_height;
 
612
 
 
613
      if (x != ratio_x)
 
614
        {
 
615
          load_dialog_set_ratio (x, x);
 
616
        }
 
617
      else if (y != ratio_y)
 
618
        {
 
619
          load_dialog_set_ratio (y, y);
 
620
        }
 
621
    }
 
622
}
 
623
 
 
624
static void
 
625
load_dialog_ratio_callback (GtkAdjustment *adj,
 
626
                            gpointer       data)
 
627
{
 
628
  gdouble x = gtk_adjustment_get_value (GTK_ADJUSTMENT (xadj));
 
629
  gdouble y = gtk_adjustment_get_value (GTK_ADJUSTMENT (yadj));
 
630
 
 
631
  if (gimp_chain_button_get_active (GIMP_CHAIN_BUTTON (constrain)))
 
632
    {
 
633
      if (x != ratio_x)
 
634
        y = x;
 
635
      else
 
636
        x = y;
 
637
    }
 
638
 
 
639
  load_dialog_set_ratio (x, y);
 
640
}
 
641
 
 
642
static void
 
643
load_dialog_resolution_callback (GimpSizeEntry *res,
 
644
                                 const gchar   *filename)
 
645
{
 
646
  SvgLoadVals  vals = { 0.0, 0, 0 };
 
647
 
 
648
  load_vals.resolution = vals.resolution = gimp_size_entry_get_refval (res, 0);
 
649
 
 
650
  if (!load_rsvg_size (filename, &vals, NULL))
 
651
    return;
 
652
 
 
653
  svg_width  = vals.width;
 
654
  svg_height = vals.height;
 
655
 
 
656
  load_dialog_set_ratio (ratio_x, ratio_y);
 
657
}
 
658
 
 
659
static void
 
660
load_dialog_set_ratio (gdouble x,
 
661
                       gdouble y)
 
662
{
 
663
  ratio_x = x;
 
664
  ratio_y = y;
 
665
 
 
666
  g_signal_handlers_block_by_func (size, load_dialog_size_callback, NULL);
 
667
 
 
668
  gimp_size_entry_set_refval (size, 0, svg_width  * x);
 
669
  gimp_size_entry_set_refval (size, 1, svg_height * y);
 
670
 
 
671
  g_signal_handlers_unblock_by_func (size, load_dialog_size_callback, NULL);
 
672
 
 
673
  g_signal_handlers_block_by_func (xadj, load_dialog_ratio_callback, NULL);
 
674
  g_signal_handlers_block_by_func (yadj, load_dialog_ratio_callback, NULL);
 
675
 
 
676
  gtk_adjustment_set_value (GTK_ADJUSTMENT (xadj), x);
 
677
  gtk_adjustment_set_value (GTK_ADJUSTMENT (yadj), y);
 
678
 
 
679
  g_signal_handlers_unblock_by_func (xadj, load_dialog_ratio_callback, NULL);
 
680
  g_signal_handlers_unblock_by_func (yadj, load_dialog_ratio_callback, NULL);
 
681
}
 
682
 
 
683
static GimpPDBStatusType
 
684
load_dialog (const gchar  *filename,
 
685
             GError      **load_error)
 
686
{
 
687
  GtkWidget   *dialog;
 
688
  GtkWidget   *frame;
 
689
  GtkWidget   *hbox;
 
690
  GtkWidget   *vbox;
 
691
  GtkWidget   *image;
 
692
  GdkPixbuf   *preview;
 
693
  GtkWidget   *table;
 
694
  GtkWidget   *table2;
 
695
  GtkWidget   *abox;
 
696
  GtkWidget   *res;
 
697
  GtkWidget   *label;
 
698
  GtkWidget   *spinbutton;
 
699
  GtkWidget   *toggle;
 
700
  GtkWidget   *toggle2;
 
701
  GtkObject   *adj;
 
702
  gboolean     run;
 
703
  GError      *error = NULL;
 
704
  SvgLoadVals  vals  =
 
705
  {
 
706
    SVG_DEFAULT_RESOLUTION,
 
707
    - SVG_PREVIEW_SIZE,
 
708
    - SVG_PREVIEW_SIZE
 
709
  };
 
710
 
 
711
  preview = load_rsvg_pixbuf (filename, &vals, &error);
 
712
 
 
713
  if (! preview)
 
714
    {
 
715
      /*  Do not rely on librsvg setting GError on failure!  */
 
716
      g_set_error (load_error,
 
717
                   error ? error->domain : 0, error ? error->code : 0,
 
718
                   _("Could not open '%s' for reading: %s"),
 
719
                   gimp_filename_to_utf8 (filename),
 
720
                   error ? error->message : _("Unknown reason"));
 
721
      g_clear_error (&error);
 
722
 
 
723
      return GIMP_PDB_EXECUTION_ERROR;
 
724
    }
 
725
 
 
726
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
 
727
 
 
728
  /* Scalable Vector Graphics is SVG, should perhaps not be translated */
 
729
  dialog = gimp_dialog_new (_("Render Scalable Vector Graphics"),
 
730
                            PLUG_IN_BINARY,
 
731
                            NULL, 0,
 
732
                            gimp_standard_help_func, LOAD_PROC,
 
733
 
 
734
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
735
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
 
736
 
 
737
                            NULL);
 
738
 
 
739
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
 
740
                                           GTK_RESPONSE_OK,
 
741
                                           GTK_RESPONSE_CANCEL,
 
742
                                           -1);
 
743
 
 
744
  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
 
745
 
 
746
  gimp_window_set_transient (GTK_WINDOW (dialog));
 
747
 
 
748
  hbox = gtk_hbox_new (FALSE, 12);
 
749
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
 
750
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
 
751
                      TRUE, TRUE, 0);
 
752
  gtk_widget_show (hbox);
 
753
 
 
754
  /*  The SVG preview  */
 
755
  vbox = gtk_vbox_new (FALSE, 6);
 
756
  gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
 
757
  gtk_widget_show (vbox);
 
758
 
 
759
  abox = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
 
760
  gtk_box_pack_start (GTK_BOX (vbox), abox, FALSE, FALSE, 0);
 
761
  gtk_widget_show (abox);
 
762
 
 
763
  frame = gtk_frame_new (NULL);
 
764
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
 
765
  gtk_container_add (GTK_CONTAINER (abox), frame);
 
766
  gtk_widget_show (frame);
 
767
 
 
768
  image = gtk_image_new_from_pixbuf (preview);
 
769
  gtk_container_add (GTK_CONTAINER (frame), image);
 
770
  gtk_widget_show (image);
 
771
 
 
772
  size_label = gtk_label_new (NULL);
 
773
  gtk_label_set_justify (GTK_LABEL (size_label), GTK_JUSTIFY_CENTER);
 
774
  gtk_misc_set_alignment (GTK_MISC (size_label), 0.5, 0.0);
 
775
  gtk_box_pack_start (GTK_BOX (vbox), size_label, TRUE, TRUE, 4);
 
776
  gtk_widget_show (size_label);
 
777
 
 
778
  /*  query the initial size after the size label is created  */
 
779
  vals.resolution = load_vals.resolution;
 
780
 
 
781
  load_rsvg_size (filename, &vals, NULL);
 
782
 
 
783
  svg_width  = vals.width;
 
784
  svg_height = vals.height;
 
785
 
 
786
  table = gtk_table_new (7, 3, FALSE);
 
787
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
 
788
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
 
789
  gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
 
790
  gtk_table_set_row_spacing (GTK_TABLE (table), 2, 2);
 
791
  gtk_box_pack_start (GTK_BOX (hbox), table, TRUE, TRUE, 0);
 
792
  gtk_widget_show (table);
 
793
 
 
794
  /*  Width and Height  */
 
795
  label = gtk_label_new (_("Width:"));
 
796
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
797
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
 
798
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
799
  gtk_widget_show (label);
 
800
 
 
801
  label = gtk_label_new (_("Height:"));
 
802
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
803
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
 
804
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
805
  gtk_widget_show (label);
 
806
 
 
807
  hbox = gtk_hbox_new (FALSE, 0);
 
808
  gtk_table_attach (GTK_TABLE (table), hbox, 1, 2, 0, 1,
 
809
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
810
  gtk_widget_show (hbox);
 
811
 
 
812
  spinbutton = gimp_spin_button_new (&adj, 1, 1, 1, 1, 10, 0, 1, 2);
 
813
  gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), 10);
 
814
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
 
815
  gtk_widget_show (spinbutton);
 
816
 
 
817
  hbox = gtk_hbox_new (FALSE, 0);
 
818
  gtk_table_attach (GTK_TABLE (table), hbox, 1, 2, 1, 2,
 
819
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
820
  gtk_widget_show (hbox);
 
821
 
 
822
  size = GIMP_SIZE_ENTRY (gimp_size_entry_new (1, GIMP_UNIT_PIXEL, "%a",
 
823
                                               TRUE, FALSE, FALSE, 10,
 
824
                                               GIMP_SIZE_ENTRY_UPDATE_SIZE));
 
825
  gtk_table_set_col_spacing (GTK_TABLE (size), 1, 6);
 
826
 
 
827
  gimp_size_entry_add_field (size, GTK_SPIN_BUTTON (spinbutton), NULL);
 
828
 
 
829
  gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (size), FALSE, FALSE, 0);
 
830
  gtk_widget_show (GTK_WIDGET (size));
 
831
 
 
832
  gimp_size_entry_set_refval_boundaries (size, 0,
 
833
                                         GIMP_MIN_IMAGE_SIZE,
 
834
                                         GIMP_MAX_IMAGE_SIZE);
 
835
  gimp_size_entry_set_refval_boundaries (size, 1,
 
836
                                         GIMP_MIN_IMAGE_SIZE,
 
837
                                         GIMP_MAX_IMAGE_SIZE);
 
838
 
 
839
  gimp_size_entry_set_refval (size, 0, svg_width);
 
840
  gimp_size_entry_set_refval (size, 1, svg_height);
 
841
 
 
842
  gimp_size_entry_set_resolution (size, 0, load_vals.resolution, FALSE);
 
843
  gimp_size_entry_set_resolution (size, 1, load_vals.resolution, FALSE);
 
844
 
 
845
  g_signal_connect (size, "value-changed",
 
846
                    G_CALLBACK (load_dialog_size_callback),
 
847
                    NULL);
 
848
 
 
849
  /*  Scale ratio  */
 
850
  hbox = gtk_hbox_new (FALSE, 0);
 
851
  gtk_table_attach (GTK_TABLE (table), hbox, 1, 2, 2, 4,
 
852
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
853
  gtk_widget_show (hbox);
 
854
 
 
855
  table2 = gtk_table_new (2, 2, FALSE);
 
856
  gtk_table_set_col_spacing (GTK_TABLE (table2), 0, 2);
 
857
  gtk_table_set_row_spacing (GTK_TABLE (table2), 0, 4);
 
858
  gtk_box_pack_start (GTK_BOX (hbox), table2, FALSE, FALSE, 0);
 
859
 
 
860
  spinbutton =
 
861
    gimp_spin_button_new (&xadj,
 
862
                          ratio_x,
 
863
                          (gdouble) GIMP_MIN_IMAGE_SIZE / (gdouble) svg_width,
 
864
                          (gdouble) GIMP_MAX_IMAGE_SIZE / (gdouble) svg_width,
 
865
                          0.01, 0.1, 0,
 
866
                          0.01, 4);
 
867
  gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), 10);
 
868
  gtk_table_attach_defaults (GTK_TABLE (table2), spinbutton, 0, 1, 0, 1);
 
869
  gtk_widget_show (spinbutton);
 
870
 
 
871
  g_signal_connect (xadj, "value-changed",
 
872
                    G_CALLBACK (load_dialog_ratio_callback),
 
873
                    NULL);
 
874
 
 
875
  label = gtk_label_new_with_mnemonic (_("_X ratio:"));
 
876
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinbutton);
 
877
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
878
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
 
879
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
880
  gtk_widget_show (label);
 
881
 
 
882
  spinbutton =
 
883
    gimp_spin_button_new (&yadj,
 
884
                          ratio_y,
 
885
                          (gdouble) GIMP_MIN_IMAGE_SIZE / (gdouble) svg_height,
 
886
                          (gdouble) GIMP_MAX_IMAGE_SIZE / (gdouble) svg_height,
 
887
                          0.01, 0.1, 0,
 
888
                          0.01, 4);
 
889
  gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), 10);
 
890
  gtk_table_attach_defaults (GTK_TABLE (table2), spinbutton, 0, 1, 1, 2);
 
891
  gtk_widget_show (spinbutton);
 
892
 
 
893
  g_signal_connect (yadj, "value-changed",
 
894
                    G_CALLBACK (load_dialog_ratio_callback),
 
895
                    NULL);
 
896
 
 
897
  label = gtk_label_new_with_mnemonic (_("_Y ratio:"));
 
898
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinbutton);
 
899
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
900
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 3, 4,
 
901
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
902
  gtk_widget_show (label);
 
903
 
 
904
  /*  the constrain ratio chainbutton  */
 
905
  constrain = gimp_chain_button_new (GIMP_CHAIN_RIGHT);
 
906
  gimp_chain_button_set_active (GIMP_CHAIN_BUTTON (constrain), TRUE);
 
907
  gtk_table_attach_defaults (GTK_TABLE (table2), constrain, 1, 2, 0, 2);
 
908
  gtk_widget_show (constrain);
 
909
 
 
910
  gimp_help_set_help_data (GIMP_CHAIN_BUTTON (constrain)->button,
 
911
                           _("Constrain aspect ratio"), NULL);
 
912
 
 
913
  gtk_widget_show (table2);
 
914
 
 
915
  /*  Resolution   */
 
916
  label = gtk_label_new (_("Resolution:"));
 
917
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
918
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 4, 5,
 
919
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
920
  gtk_widget_show (label);
 
921
 
 
922
  res = gimp_size_entry_new (1, GIMP_UNIT_INCH, _("pixels/%a"),
 
923
                             FALSE, FALSE, FALSE, 10,
 
924
                             GIMP_SIZE_ENTRY_UPDATE_RESOLUTION);
 
925
  gtk_table_set_col_spacing (GTK_TABLE (res), 1, 6);
 
926
 
 
927
  gtk_table_attach (GTK_TABLE (table), res, 1, 2, 4, 5,
 
928
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
929
  gtk_widget_show (res);
 
930
 
 
931
  /* don't let the resolution become too small, librsvg tends to
 
932
     crash with very small resolutions */
 
933
  gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (res), 0,
 
934
                                         5.0, GIMP_MAX_RESOLUTION);
 
935
  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (res), 0, load_vals.resolution);
 
936
 
 
937
  g_signal_connect (res, "value-changed",
 
938
                    G_CALLBACK (load_dialog_resolution_callback),
 
939
                    (gpointer) filename);
 
940
 
 
941
  /*  Path Import  */
 
942
  toggle = gtk_check_button_new_with_mnemonic (_("Import _paths"));
 
943
  gtk_table_attach (GTK_TABLE (table), toggle, 0, 2, 5, 6,
 
944
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
945
  gtk_widget_show (toggle);
 
946
 
 
947
  gimp_help_set_help_data (toggle,
 
948
                           _("Import path elements of the SVG so they "
 
949
                             "can be used with the GIMP path tool"),
 
950
                           NULL);
 
951
 
 
952
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), load_vals.import);
 
953
  g_signal_connect (toggle, "toggled",
 
954
                    G_CALLBACK (gimp_toggle_button_update),
 
955
                    &load_vals.import);
 
956
 
 
957
  g_signal_connect (toggle, "toggled",
 
958
                    G_CALLBACK (gimp_toggle_button_sensitive_update),
 
959
                    NULL);
 
960
 
 
961
  toggle2 = gtk_check_button_new_with_mnemonic (_("Merge imported paths"));
 
962
  gtk_table_attach (GTK_TABLE (table), toggle2, 0, 2, 6, 7,
 
963
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
964
  gtk_widget_set_sensitive (toggle2, load_vals.import);
 
965
  gtk_widget_show (toggle2);
 
966
 
 
967
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle2), load_vals.merge);
 
968
  g_signal_connect (toggle2, "toggled",
 
969
                    G_CALLBACK (gimp_toggle_button_update),
 
970
                    &load_vals.merge);
 
971
 
 
972
  g_object_set_data (G_OBJECT (toggle), "set_sensitive", toggle2);
 
973
 
 
974
 
 
975
  gtk_widget_show (dialog);
 
976
 
 
977
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
 
978
 
 
979
  if (run)
 
980
    {
 
981
      load_vals.width  = ROUND (gimp_size_entry_get_refval (size, 0));
 
982
      load_vals.height = ROUND (gimp_size_entry_get_refval (size, 1));
 
983
    }
 
984
 
 
985
  gtk_widget_destroy (dialog);
 
986
 
 
987
  return run ? GIMP_PDB_SUCCESS : GIMP_PDB_CANCEL;
 
988
}