~ubuntu-branches/ubuntu/quantal/librsvg/quantal

« back to all changes in this revision

Viewing changes to rsvg-image.c

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2012-02-06 23:06:15 UTC
  • mfrom: (1.1.31)
  • Revision ID: package-import@ubuntu.com-20120206230615-72oj2iu92bt32rl7
Tags: 2.35.2-0ubuntu1
* New upstream version
* debian/control.in:
  - let the dev binary depends on common and bin, some sources 
    build-depends on it to get a working svg loader
* debian/patches/98_revert-abi-break.patch: 
  - dropped the fix is in the new version
* debian/librsvg2-2.symbols:
  - new version update

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include <math.h>
35
35
#include <errno.h>
36
36
#include "rsvg-css.h"
37
 
#include <gio/gio.h>
38
 
 
39
 
static GByteArray *
40
 
rsvg_acquire_base64_resource (const char *data, GError ** error)
41
 
{
42
 
    GByteArray *array = NULL;
43
 
    gsize data_len, written_len;
44
 
    int state = 0;
45
 
    guint save = 0;
46
 
 
47
 
    rsvg_return_val_if_fail (data != NULL, NULL, error);
48
 
 
49
 
    while (*data)
50
 
        if (*data++ == ',')
51
 
            break;
52
 
 
53
 
    data_len = strlen (data);
54
 
    array = g_byte_array_sized_new (data_len / 4 * 3);
55
 
    written_len = g_base64_decode_step (data, data_len, array->data,
56
 
                                        &state, &save);
57
 
    g_byte_array_set_size (array, written_len);
58
 
 
59
 
    return array;
60
 
}
61
 
 
62
 
gchar *
63
 
rsvg_get_file_path (const gchar * filename, const gchar * base_uri)
64
 
{
65
 
    gchar *absolute_filename;
66
 
 
67
 
    if (g_file_test (filename, G_FILE_TEST_EXISTS) || g_path_is_absolute (filename)) {
68
 
        absolute_filename = g_strdup (filename);
69
 
    } else {
70
 
        gchar *tmpcdir;
71
 
        gchar *base_filename;
72
 
 
73
 
        if (base_uri) {
74
 
            base_filename = g_filename_from_uri (base_uri, NULL, NULL);
75
 
            if (base_filename != NULL) {
76
 
                tmpcdir = g_path_get_dirname (base_filename);
77
 
                g_free (base_filename);
78
 
            } else 
79
 
                return NULL;
80
 
        } else
81
 
            tmpcdir = g_get_current_dir ();
82
 
 
83
 
        absolute_filename = g_build_filename (tmpcdir, filename, NULL);
84
 
        g_free (tmpcdir);
85
 
    }
86
 
 
87
 
    return absolute_filename;
88
 
}
89
 
 
90
 
static GByteArray *
91
 
rsvg_acquire_file_resource (const char *filename, const char *base_uri, GError ** error)
92
 
{
93
 
    GByteArray *array;
94
 
    gchar *path;
95
 
    gchar *data = NULL;
96
 
    gsize length;
97
 
 
98
 
    rsvg_return_val_if_fail (filename != NULL, NULL, error);
99
 
 
100
 
    path = rsvg_get_file_path (filename, base_uri);
101
 
    if (path == NULL)
102
 
        return NULL;
103
 
 
104
 
    if (!g_file_get_contents (path, &data, &length, error)) {
105
 
        g_free (path);
106
 
        return NULL;
107
 
    }
108
 
 
109
 
    array = g_byte_array_new ();
110
 
 
111
 
    g_byte_array_append (array, (guint8 *)data, length);
112
 
    g_free (data);
113
 
    g_free (path);
114
 
 
115
 
    return array;
116
 
}
117
 
 
118
 
static GByteArray *
119
 
rsvg_acquire_vfs_resource (const char *filename, const char *base_uri, GError ** error)
120
 
{
121
 
    GByteArray *array;
122
 
 
123
 
    GFile *file;
124
 
    char *data;
125
 
    gsize size;
126
 
    gboolean res = FALSE;
127
 
 
128
 
    rsvg_return_val_if_fail (filename != NULL, NULL, error);
129
 
 
130
 
    file = g_file_new_for_uri (filename);
131
 
 
132
 
    if (!(res = g_file_load_contents (file, NULL, &data, &size, NULL, error))) {
133
 
        if (base_uri != NULL) {
134
 
            GFile *base;
135
 
 
136
 
            g_clear_error (error);
137
 
 
138
 
            g_object_unref (file);
139
 
 
140
 
            base = g_file_new_for_uri (base_uri);
141
 
            file = g_file_resolve_relative_path (base, filename);
142
 
            g_object_unref (base);
143
 
 
144
 
            res = g_file_load_contents (file, NULL, &data, &size, NULL, error);
145
 
        }
146
 
    }
147
 
 
148
 
    g_object_unref (file);
149
 
 
150
 
    if (res) {
151
 
        array = g_byte_array_new ();
152
 
 
153
 
        g_byte_array_append (array, (guint8 *)data, size);
154
 
        g_free (data);
155
 
    } else {
156
 
        return NULL;
157
 
    }
158
 
 
159
 
    return array;
160
 
}
161
 
 
162
 
GByteArray *
163
 
_rsvg_acquire_xlink_href_resource (const char *href, const char *base_uri, GError ** err)
164
 
{
165
 
    GByteArray *arr = NULL;
166
 
 
167
 
    if (!(href && *href))
168
 
        return NULL;
169
 
 
170
 
    if (!strncmp (href, "data:", 5))
171
 
        arr = rsvg_acquire_base64_resource (href, NULL);
172
 
 
173
 
    if (!arr)
174
 
        arr = rsvg_acquire_file_resource (href, base_uri, NULL);
175
 
 
176
 
    if (!arr)
177
 
        arr = rsvg_acquire_vfs_resource (href, base_uri, NULL);
178
 
 
179
 
    return arr;
180
 
}
 
37
#include "rsvg-io.h"
181
38
 
182
39
cairo_surface_t *
183
 
rsvg_cairo_surface_new_from_href (const char *href, 
184
 
                                  const char *base_uri, 
 
40
rsvg_cairo_surface_new_from_href (RsvgHandle *handle,
 
41
                                  const char *href,
185
42
                                  GError **error)
186
43
{
187
 
    GByteArray *arr;
 
44
    guint8 *data;
 
45
    gsize data_len;
 
46
    char *mime_type = NULL;
188
47
    GdkPixbufLoader *loader;
189
48
    GdkPixbuf *pixbuf = NULL;
190
49
    int res;
191
50
    cairo_surface_t *surface;
192
51
 
193
 
    arr = _rsvg_acquire_xlink_href_resource (href, base_uri, error);
194
 
    if (arr == NULL)
195
 
        return NULL;
196
 
 
197
 
    loader = gdk_pixbuf_loader_new ();
198
 
 
199
 
    res = gdk_pixbuf_loader_write (loader, arr->data, arr->len, error);
200
 
    g_byte_array_free (arr, TRUE);
 
52
    data = _rsvg_handle_acquire_data (handle, href, &mime_type, &data_len, error);
 
53
    if (data == NULL)
 
54
        return NULL;
 
55
 
 
56
    if (mime_type) {
 
57
        loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error);
 
58
        g_free (mime_type);
 
59
    } else {
 
60
        loader = gdk_pixbuf_loader_new ();
 
61
    }
 
62
 
 
63
    if (loader == NULL) {
 
64
        g_free (data);
 
65
        return NULL;
 
66
    }
 
67
 
 
68
    res = gdk_pixbuf_loader_write (loader, data, data_len, error);
 
69
    g_free (data);
201
70
 
202
71
    if (!res) {
203
72
        gdk_pixbuf_loader_close (loader, NULL);
332
201
        /* path is used by some older adobe illustrator versions */
333
202
        if ((value = rsvg_property_bag_lookup (atts, "path"))
334
203
            || (value = rsvg_property_bag_lookup (atts, "xlink:href"))) {
335
 
            image->surface = rsvg_cairo_surface_new_from_href (value, 
336
 
                                                               rsvg_handle_get_base_uri (ctx), 
 
204
            image->surface = rsvg_cairo_surface_new_from_href (ctx,
 
205
                                                               value, 
337
206
                                                               NULL);
338
207
 
339
208
            if (!image->surface) {