forked from GNOME/librsvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsvg-defs.c
209 lines (169 loc) · 5.36 KB
/
rsvg-defs.c
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=4 ts=4 expandtab: */
/*
rsvg-defs.c: Manage SVG defs and references.
Copyright (C) 2000 Eazel, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library 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.
Author: Raph Levien <[email protected]>
*/
#include "config.h"
#include "rsvg-private.h"
#include "rsvg-defs.h"
#include "rsvg-styles.h"
#include "rsvg-io.h"
#include <glib.h>
struct _RsvgDefs {
GHashTable *hash;
GPtrArray *unnamed;
GHashTable *externs;
GSList *toresolve;
RsvgHandle *ctx;
};
typedef struct _RsvgResolutionPending RsvgResolutionPending;
struct _RsvgResolutionPending {
RsvgNode **tochange;
char *name;
};
RsvgDefs *
rsvg_defs_new (RsvgHandle *handle)
{
RsvgDefs *result = g_new (RsvgDefs, 1);
result->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
result->externs =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
result->unnamed = g_ptr_array_new ();
result->toresolve = NULL;
result->ctx = handle; /* no need to take a ref here */
return result;
}
static int
rsvg_defs_load_extern (const RsvgDefs * defs, const char *name)
{
RsvgHandle *handle;
gchar *filename, *base_uri;
guint8 *data;
gsize data_len;
filename = _rsvg_io_get_file_path (name, rsvg_handle_get_base_uri (defs->ctx));
data = _rsvg_handle_acquire_data (defs->ctx, name, NULL, &data_len, NULL);
if (data) {
handle = rsvg_handle_new ();
base_uri = rsvg_get_base_uri_from_filename (filename);
rsvg_handle_set_base_uri (handle, base_uri);
g_free (base_uri);
if (rsvg_handle_write (handle, data, data_len, NULL) &&
rsvg_handle_close (handle, NULL)) {
g_hash_table_insert (defs->externs, g_strdup (name), handle);
}
g_free (data);
}
g_free (filename);
return 0;
}
static RsvgNode *
rsvg_defs_extern_lookup (const RsvgDefs * defs, const char *filename, const char *name)
{
RsvgHandle *file;
file = (RsvgHandle *) g_hash_table_lookup (defs->externs, filename);
if (file == NULL) {
if (rsvg_defs_load_extern (defs, filename))
return NULL;
file = (RsvgHandle *) g_hash_table_lookup (defs->externs, filename);
}
if (file != NULL)
return (RsvgNode *) g_hash_table_lookup (file->priv->defs->hash, name);
else
return NULL;
}
RsvgNode *
rsvg_defs_lookup (const RsvgDefs * defs, const char *name)
{
char *hashpos;
hashpos = g_strrstr (name, "#");
if (!hashpos) {
return NULL;
}
if (hashpos == name) {
return (RsvgNode *) g_hash_table_lookup (defs->hash, name + 1);
} else {
gchar **splitbits;
RsvgNode *toreturn;
splitbits = g_strsplit (name, "#", 2);
toreturn = rsvg_defs_extern_lookup (defs, splitbits[0], splitbits[1]);
g_strfreev (splitbits);
return toreturn;
}
}
void
rsvg_defs_set (RsvgDefs * defs, const char *name, RsvgNode * val)
{
if (name == NULL);
else if (name[0] == '\0');
else
rsvg_defs_register_name (defs, name, val);
rsvg_defs_register_memory (defs, val);
}
void
rsvg_defs_register_name (RsvgDefs * defs, const char *name, RsvgNode * val)
{
g_hash_table_insert (defs->hash, g_strdup (name), val);
}
void
rsvg_defs_register_memory (RsvgDefs * defs, RsvgNode * val)
{
g_ptr_array_add (defs->unnamed, val);
}
static void
rsvg_defs_free_toresolve (RsvgDefs *defs)
{
GSList *l;
for (l = defs->toresolve; l ; l = l ->next) {
RsvgResolutionPending *data = l->data;
g_free (data->name);
g_free (data);
}
g_slist_free (defs->toresolve);
}
void
rsvg_defs_free (RsvgDefs * defs)
{
guint i;
g_hash_table_destroy (defs->hash);
for (i = 0; i < defs->unnamed->len; i++)
((RsvgNode *) g_ptr_array_index (defs->unnamed, i))->
free (g_ptr_array_index (defs->unnamed, i));
g_ptr_array_free (defs->unnamed, TRUE);
g_hash_table_destroy (defs->externs);
rsvg_defs_free_toresolve (defs);
g_free (defs);
}
void
rsvg_defs_add_resolver (RsvgDefs * defs, RsvgNode ** tochange, const gchar * name)
{
RsvgResolutionPending *data;
data = g_new (RsvgResolutionPending, 1);
data->tochange = tochange;
data->name = g_strdup (name);
defs->toresolve = g_slist_prepend (defs->toresolve, data);
}
void
rsvg_defs_resolve_all (RsvgDefs * defs)
{
GSList *l;
for (l = defs->toresolve; l ; l = l ->next) {
RsvgResolutionPending *data = l->data;
*(data->tochange) = rsvg_defs_lookup (defs, data->name);
}
rsvg_defs_free_toresolve (defs);
defs->toresolve = NULL;
}