svg.c 5.57 KB
Newer Older
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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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.
 */

/* SVG loading and saving file filter for the GIMP
 * -Dom Lachowicz <cinamod@hotmail.com>
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gtk/gtk.h>
#include <libgimp/gimp.h>
31 32 33 34
#include <rsvg.h>

/* TODO: remove me, initialize gimp i18n services */
#define _(String) (String)
35 36 37 38 39 40 41 42 43 44 45

static void     query                     (void);
static void     run                       (gchar            *name,
					   gint              nparams,
					   GimpParam        *param,
					   gint             *nreturn_vals,
					   GimpParam       **return_vals);
static gint32   load_image                (gchar            *filename, 
					   GimpRunMode       runmode, 
					   gboolean          preview);

46 47 48 49 50 51 52
GimpPlugInInfo PLUG_IN_INFO = {
  NULL,                         /* init_proc  */
  NULL,                         /* quit_proc  */
  query,                        /* query_proc */
  run,                          /* run_proc   */
};

53 54
MAIN ()

55 56 57
/*
 * 'query()' - Respond to a plug-in query...
 */
58 59 60 61 62
static void
query (void)
{
  static GimpParamDef load_args[] =
  {
63
    { GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive" },
64 65 66 67 68 69 70 71 72
    { GIMP_PDB_STRING,   "filename",     "The name of the file to load" },
    { GIMP_PDB_STRING,   "raw_filename", "The name of the file to load" }
  };
  static GimpParamDef load_return_vals[] =
  {
    { GIMP_PDB_IMAGE,   "image",         "Output image" }
  };

  gimp_install_procedure ("file_svg_load",
73 74 75 76 77
                          "Loads files in the SVG file format",
                          "Loads files in the SVG file format",
                          "Dom Lachowicz <cinamod@hotmail.com>",
                          "Dom Lachowicz <cinamod@hotmail.com>",
                          "(c) 2003 - " VERSION,
78 79 80 81 82 83 84 85
			  "<Load>/SVG",
			  NULL,
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (load_args),
                          G_N_ELEMENTS (load_return_vals),
                          load_args, load_return_vals);

  gimp_register_magic_load_handler ("file_svg_load",
86
				    "svg", "",
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
				    "0,string,<?xml,0,string,<svg");
}

/*
 * 'run()' - Run the plug-in...
 */
static void
run (gchar      *name,
     gint        nparams,
     GimpParam  *param,
     gint       *nreturn_vals,
     GimpParam **return_vals)
{
  static GimpParam     values[2];
  GimpRunMode          run_mode;
  GimpPDBStatusType    status = GIMP_PDB_SUCCESS;
  gint32               image_ID;

  run_mode = param[0].data.d_int32;

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;

113 114 115
  /* MUST call this before any RSVG funcs */
  g_type_init ();

116 117
  if (strcmp (name, "file_svg_load") == 0)
    {
118
      /* INIT_I18N_UI (); */
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      image_ID = load_image (param[1].data.d_string, run_mode, FALSE);

      if (image_ID != -1)
	{
	  *nreturn_vals = 2;
	  values[1].type         = GIMP_PDB_IMAGE;
	  values[1].data.d_image = image_ID;
	}
      else
	{
	  status = GIMP_PDB_EXECUTION_ERROR;
	}
    }

  values[0].data.d_status = status;
}

/*
137
 * 'load_image()' - Load a SVG image into a new image window.
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
 */
static gint32
load_image (gchar       *filename, 	/* I - File to load */ 
	    GimpRunMode  runmode, 
	    gboolean     preview)
{
  gint32        image;	        /* Image */
  gint32	layer;		/* Layer */
  GimpDrawable *drawable;	/* Drawable for layer */
  GimpPixelRgn	pixel_rgn;	/* Pixel region for layer */
  GdkPixbuf    *pixbuf;        /* SVG, rasterized */
  gchar        *status;

  /* TODO: could possibly use preview abilities */

  pixbuf = rsvg_pixbuf_from_file (filename, NULL);

  if (!pixbuf)
    {
      g_message (_("can't open \"%s\"\n"), filename);
      gimp_quit ();
    }

  if (runmode != GIMP_RUN_NONINTERACTIVE)
    {
      status = g_strdup_printf (_("Loading %s:"), filename);
      gimp_progress_init (status);
      g_free (status);
    }

  image = gimp_image_new (gdk_pixbuf_get_width (pixbuf), 
			  gdk_pixbuf_get_height (pixbuf),
			  GIMP_RGB);
171 172 173 174 175 176
  if (image == -1)
    {
      g_message ("Can't allocate new image\n%s", filename);
      gimp_quit ();
    }

177 178 179 180 181
  gimp_image_set_filename (image, filename);

  layer = gimp_layer_new (image, _("Background"),
			  gdk_pixbuf_get_width (pixbuf), 
			  gdk_pixbuf_get_height (pixbuf),
182
			  GIMP_RGBA_IMAGE, 100, GIMP_NORMAL_MODE);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  
  drawable = gimp_drawable_get (layer);
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
		       drawable->width, drawable->height, TRUE, FALSE);

  gimp_pixel_rgn_set_rect (&pixel_rgn, gdk_pixbuf_get_pixels (pixbuf),
			   0, 0, 
			   gdk_pixbuf_get_width (pixbuf),
			   gdk_pixbuf_get_height (pixbuf));

  g_object_unref (G_OBJECT(pixbuf));

  /* Tell the GIMP to display the image.
   */
  gimp_image_add_layer (image, layer, 0);
  gimp_drawable_flush (drawable);

  return image;
}