| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ |
| 2 |
/* GdkPixbuf library - SVG image loader |
| 3 |
* |
| 4 |
* Copyright (C) 2002 Matthias Clasen |
| 5 |
* Copyright (C) 2002-2004 Dom Lachowicz |
| 6 |
* |
| 7 |
* Authors: Matthias Clasen <[email protected]> |
| 8 |
* Dom Lachowicz <[email protected]> |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or |
| 11 |
* modify it under the terms of the GNU Lesser General Public |
| 12 |
* License as published by the Free Software Foundation; either |
| 13 |
* version 2.1 of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* This library 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 GNU |
| 18 |
* Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public |
| 21 |
* License along with this library; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 |
*/ |
| 24 |
|
| 25 |
#define RSVG_DISABLE_DEPRECATION_WARNINGS |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
|
| 29 |
#include <librsvg/rsvg.h> |
| 30 |
#include <gdk-pixbuf/gdk-pixbuf.h> |
| 31 |
|
| 32 |
typedef struct { |
| 33 |
RsvgHandle *handle; |
| 34 |
|
| 35 |
GdkPixbufModuleUpdatedFunc updated_func; |
| 36 |
GdkPixbufModulePreparedFunc prepared_func; |
| 37 |
GdkPixbufModuleSizeFunc size_func; |
| 38 |
|
| 39 |
gboolean first_write; |
| 40 |
|
| 41 |
gpointer user_data; |
| 42 |
} SvgContext; |
| 43 |
|
| 44 |
G_MODULE_EXPORT void fill_vtable (GdkPixbufModule *module); |
| 45 |
G_MODULE_EXPORT void fill_info (GdkPixbufFormat *info); |
| 46 |
|
| 47 |
enum { |
| 48 |
ERROR_WRITING = 1, |
| 49 |
ERROR_DISPLAYING_IMAGE |
| 50 |
} RsvgLoaderErrorReasons; |
| 51 |
|
| 52 |
static void |
| 53 |
rsvg_propagate_error (GError ** err, |
| 54 |
const char * reason, |
| 55 |
gint code) |
| 56 |
{ |
| 57 |
if (err) { |
| 58 |
*err = NULL; |
| 59 |
g_set_error (err, rsvg_error_quark (), code, "%s", reason); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static gpointer |
| 64 |
gdk_pixbuf__svg_image_begin_load (GdkPixbufModuleSizeFunc size_func, |
| 65 |
GdkPixbufModulePreparedFunc prepared_func, |
| 66 |
GdkPixbufModuleUpdatedFunc updated_func, |
| 67 |
gpointer user_data, |
| 68 |
GError **error) |
| 69 |
{ |
| 70 |
SvgContext *context = g_new0 (SvgContext, 1); |
| 71 |
|
| 72 |
if (error) |
| 73 |
*error = NULL; |
| 74 |
|
| 75 |
context->first_write = TRUE; |
| 76 |
context->size_func = size_func; |
| 77 |
|
| 78 |
context->prepared_func = prepared_func; |
| 79 |
context->updated_func = updated_func; |
| 80 |
context->user_data = user_data; |
| 81 |
|
| 82 |
return context; |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
emit_updated (SvgContext *context, GdkPixbuf *pixbuf) |
| 87 |
{ |
| 88 |
if (context->updated_func != NULL) |
| 89 |
(* context->updated_func) (pixbuf, |
| 90 |
0, 0, |
| 91 |
gdk_pixbuf_get_width (pixbuf), |
| 92 |
gdk_pixbuf_get_height (pixbuf), |
| 93 |
context->user_data); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
emit_prepared (SvgContext *context, GdkPixbuf *pixbuf) |
| 98 |
{ |
| 99 |
if (context->prepared_func != NULL) |
| 100 |
(* context->prepared_func) (pixbuf, NULL, context->user_data); |
| 101 |
} |
| 102 |
|
| 103 |
static gboolean |
| 104 |
gdk_pixbuf__svg_image_load_increment (gpointer data, |
| 105 |
const guchar *buf, guint size, |
| 106 |
GError **error) |
| 107 |
{ |
| 108 |
SvgContext *context = (SvgContext *)data; |
| 109 |
|
| 110 |
if (error) |
| 111 |
*error = NULL; |
| 112 |
|
| 113 |
if (context->first_write == TRUE) { |
| 114 |
context->first_write = FALSE; |
| 115 |
|
| 116 |
context->handle = rsvg_handle_new (); |
| 117 |
|
| 118 |
if (!context->handle) { |
| 119 |
rsvg_propagate_error (error, "Error displaying image", ERROR_DISPLAYING_IMAGE); |
| 120 |
return FALSE; |
| 121 |
} |
| 122 |
|
| 123 |
rsvg_handle_set_size_callback (context->handle, context->size_func, context->user_data, NULL); |
| 124 |
} |
| 125 |
|
| 126 |
if (!context->handle) { |
| 127 |
rsvg_propagate_error (error, "Error displaying image", ERROR_DISPLAYING_IMAGE); |
| 128 |
return FALSE; |
| 129 |
} |
| 130 |
|
| 131 |
if (!rsvg_handle_write (context->handle, buf, size, error)) { |
| 132 |
g_clear_error(error); |
| 133 |
rsvg_propagate_error (error, "Error writing", ERROR_WRITING); |
| 134 |
return FALSE; |
| 135 |
} |
| 136 |
|
| 137 |
return TRUE; |
| 138 |
} |
| 139 |
|
| 140 |
static gboolean |
| 141 |
gdk_pixbuf__svg_image_stop_load (gpointer data, GError **error) |
| 142 |
{ |
| 143 |
SvgContext *context = (SvgContext *)data; |
| 144 |
GdkPixbuf *pixbuf; |
| 145 |
gboolean result = TRUE; |
| 146 |
|
| 147 |
if (error) |
| 148 |
*error = NULL; |
| 149 |
|
| 150 |
if (!context->handle) { |
| 151 |
rsvg_propagate_error (error, "Error displaying image", ERROR_DISPLAYING_IMAGE); |
| 152 |
return FALSE; |
| 153 |
} |
| 154 |
|
| 155 |
if (!rsvg_handle_close (context->handle, error)) { |
| 156 |
g_object_unref (context->handle); |
| 157 |
g_free (context); |
| 158 |
return FALSE; |
| 159 |
} |
| 160 |
|
| 161 |
pixbuf = rsvg_handle_get_pixbuf (context->handle); |
| 162 |
|
| 163 |
if (pixbuf != NULL) { |
| 164 |
emit_prepared (context, pixbuf); |
| 165 |
emit_updated (context, pixbuf); |
| 166 |
g_object_unref (pixbuf); |
| 167 |
} |
| 168 |
else { |
| 169 |
rsvg_propagate_error (error, "Error displaying image", ERROR_DISPLAYING_IMAGE); |
| 170 |
result = FALSE; |
| 171 |
} |
| 172 |
|
| 173 |
g_object_unref (context->handle); |
| 174 |
g_free (context); |
| 175 |
|
| 176 |
return result; |
| 177 |
} |
| 178 |
|
| 179 |
void |
| 180 |
fill_vtable (GdkPixbufModule *module) |
| 181 |
{ |
| 182 |
module->begin_load = gdk_pixbuf__svg_image_begin_load; |
| 183 |
module->stop_load = gdk_pixbuf__svg_image_stop_load; |
| 184 |
module->load_increment = gdk_pixbuf__svg_image_load_increment; |
| 185 |
} |
| 186 |
|
| 187 |
void |
| 188 |
fill_info (GdkPixbufFormat *info) |
| 189 |
{ |
| 190 |
static const GdkPixbufModulePattern signature[] = { |
| 191 |
{ " <svg", "* ", 100 }, |
| 192 |
{ " <!DOCTYPE svg", "* ", 100 }, |
| 193 |
{ NULL, NULL, 0 } |
| 194 |
}; |
| 195 |
|
| 196 |
static const gchar *mime_types[] = { /* yes folks, i actually have run into all of these in the wild... */ |
| 197 |
"image/svg+xml", |
| 198 |
"image/svg", |
| 199 |
"image/svg-xml", |
| 200 |
"image/vnd.adobe.svg+xml", |
| 201 |
"text/xml-svg", |
| 202 |
"image/svg+xml-compressed", |
| 203 |
NULL |
| 204 |
}; |
| 205 |
static const gchar *extensions[] = { |
| 206 |
"svg", |
| 207 |
"svgz", |
| 208 |
"svg.gz", |
| 209 |
NULL |
| 210 |
}; |
| 211 |
|
| 212 |
info->name = "svg"; |
| 213 |
info->signature = (GdkPixbufModulePattern *) signature; |
| 214 |
info->description = "Scalable Vector Graphics"; |
| 215 |
info->mime_types = (gchar **) mime_types; |
| 216 |
info->extensions = (gchar **) extensions; |
| 217 |
info->flags = GDK_PIXBUF_FORMAT_SCALABLE | GDK_PIXBUF_FORMAT_THREADSAFE; |
| 218 |
info->license = "LGPL"; |
| 219 |
} |