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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* GdkPixbuf library - SVG image loader
*
* Copyright (C) 2002 Matthias Clasen
* Copyright (C) 2002-2004 Dom Lachowicz
*
* Authors: Matthias Clasen <maclas@gmx.de>
* Dom Lachowicz <cinamod@hotmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define RSVG_DISABLE_DEPRECATION_WARNINGS
#include "config.h"
#include <stdlib.h>
#include "include/rsvg.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#define N_(string) (string)
#define _(string) (string)
typedef struct {
RsvgHandle *handle;
GdkPixbufModuleUpdatedFunc updated_func;
GdkPixbufModulePreparedFunc prepared_func;
GdkPixbufModuleSizeFunc size_func;
gboolean first_write;
gpointer user_data;
} SvgContext;
G_MODULE_EXPORT void fill_vtable (GdkPixbufModule *module);
G_MODULE_EXPORT void fill_info (GdkPixbufFormat *info);
enum {
ERROR_WRITING = 1,
ERROR_DISPLAYING_IMAGE
} RsvgLoaderErrorReasons;
static void
rsvg_propagate_error (GError ** err,
const char * reason,
gint code)
{
if (err) {
*err = NULL;
g_set_error (err, rsvg_error_quark (), code, "%s", reason);
}
}
static gpointer
gdk_pixbuf__svg_image_begin_load (GdkPixbufModuleSizeFunc size_func,
GdkPixbufModulePreparedFunc prepared_func,
GdkPixbufModuleUpdatedFunc updated_func,
gpointer user_data,
GError **error)
{
SvgContext *context = g_new0 (SvgContext, 1);
if (error)
*error = NULL;
context->first_write = TRUE;
context->size_func = size_func;
context->prepared_func = prepared_func;
context->updated_func = updated_func;
context->user_data = user_data;
return context;
}
static void
emit_updated (SvgContext *context, GdkPixbuf *pixbuf)
{
if (context->updated_func != NULL)
(* context->updated_func) (pixbuf,
0, 0,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
context->user_data);
}
static void
emit_prepared (SvgContext *context, GdkPixbuf *pixbuf)
{
if (context->prepared_func != NULL)
(* context->prepared_func) (pixbuf, NULL, context->user_data);
}
static gboolean
gdk_pixbuf__svg_image_load_increment (gpointer data,
const guchar *buf, guint size,
GError **error)
{
SvgContext *context = (SvgContext *)data;
if (error)
*error = NULL;
if (context->first_write == TRUE) {
context->first_write = FALSE;
context->handle = rsvg_handle_new ();
if (!context->handle) {
rsvg_propagate_error (error, _("Error displaying image"), ERROR_DISPLAYING_IMAGE);
return FALSE;
}
rsvg_handle_set_size_callback (context->handle, context->size_func, context->user_data, NULL);
}
if (!context->handle) {
rsvg_propagate_error (error, _("Error displaying image"), ERROR_DISPLAYING_IMAGE);
return FALSE;
}
if (!rsvg_handle_write (context->handle, buf, size, error)) {
g_clear_error(error);
rsvg_propagate_error (error, _("Error writing"), ERROR_WRITING);
return FALSE;
}
return TRUE;
}
static gboolean
gdk_pixbuf__svg_image_stop_load (gpointer data, GError **error)
{
SvgContext *context = (SvgContext *)data;
GdkPixbuf *pixbuf;
gboolean result = TRUE;
if (error)
*error = NULL;
if (!context->handle) {
rsvg_propagate_error (error, _("Error displaying image"), ERROR_DISPLAYING_IMAGE);
return FALSE;
}
if (!rsvg_handle_close (context->handle, error)) {
g_object_unref (context->handle);
g_free (context);
return FALSE;
}
pixbuf = rsvg_handle_get_pixbuf (context->handle);
if (pixbuf != NULL) {
emit_prepared (context, pixbuf);
emit_updated (context, pixbuf);
g_object_unref (pixbuf);
}
else {
rsvg_propagate_error (error, _("Error displaying image"), ERROR_DISPLAYING_IMAGE);
result = FALSE;
}
g_object_unref (context->handle);
g_free (context);
return result;
}
void
fill_vtable (GdkPixbufModule *module)
{
module->begin_load = gdk_pixbuf__svg_image_begin_load;
module->stop_load = gdk_pixbuf__svg_image_stop_load;
module->load_increment = gdk_pixbuf__svg_image_load_increment;
}
void
fill_info (GdkPixbufFormat *info)
{
static const GdkPixbufModulePattern signature[] = {
{ " <svg", "* ", 100 },
{ " <!DOCTYPE svg", "* ", 100 },
{ NULL, NULL, 0 }
};
static const gchar *mime_types[] = { /* yes folks, i actually have run into all of these in the wild... */
"image/svg+xml",
"image/svg",
"image/svg-xml",
"image/vnd.adobe.svg+xml",
"text/xml-svg",
"image/svg+xml-compressed",
NULL
};
static const gchar *extensions[] = {
"svg",
"svgz",
"svg.gz",
NULL
};
info->name = "svg";
info->signature = (GdkPixbufModulePattern *) signature;
info->description = _("Scalable Vector Graphics");
info->mime_types = (gchar **) mime_types;
info->extensions = (gchar **) extensions;
info->flags = GDK_PIXBUF_FORMAT_SCALABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
info->license = "LGPL";
}
|