rsvg-gobject.c 11.5 KB
Newer Older
1
/* vim: set sw=4 sts=4: -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
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
/*
   rsvg-gobject.c: GObject support.

   Copyright (C) 2006 Robert Staudinger <robert.staudinger@gmail.com>

   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.
*/

#include "rsvg-private.h"
#include "rsvg-defs.h"

enum {
27 28 29 30 31 32 33 34 35 36 37 38
    PROP_0,
    PROP_DPI_X,
    PROP_DPI_Y,
    PROP_BASE_URI,
    PROP_WIDTH,
    PROP_HEIGHT,
    PROP_EM,
    PROP_EX,
    PROP_TITLE,
    PROP_DESC,
    PROP_METADATA,
    NUM_PROPS
39 40 41 42 43 44 45 46
};

extern double rsvg_internal_dpi_x;
extern double rsvg_internal_dpi_y;

static GObjectClass *rsvg_parent_class = NULL;

static void
47
instance_init (RsvgHandle * self)
48
{
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    self->priv = g_new0 (RsvgHandlePrivate, 1);
    self->priv->defs = rsvg_defs_new ();
    self->priv->handler_nest = 0;
    self->priv->entities = g_hash_table_new (g_str_hash, g_str_equal);
    self->priv->dpi_x = rsvg_internal_dpi_x;
    self->priv->dpi_y = rsvg_internal_dpi_y;

    self->priv->css_props = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

    self->priv->ctxt = NULL;
    self->priv->currentnode = NULL;
    self->priv->treebase = NULL;

    self->priv->finished = 0;
    self->priv->first_write = TRUE;

    self->priv->is_disposed = FALSE;
66
    self->priv->in_loop = FALSE;
67 68 69 70 71
}

static void
rsvg_ctx_free_helper (gpointer key, gpointer value, gpointer user_data)
{
72 73
    xmlEntityPtr entval = (xmlEntityPtr) value;

74
#if LIBXML_VERSION < 20700
75 76
    /* key == entval->name, so it's implicitly freed below */

77 78 79
    xmlFree ((xmlChar *) entval->name);
    xmlFree ((xmlChar *) entval->ExternalID);
    xmlFree ((xmlChar *) entval->SystemID);
80 81
    xmlFree (entval->content);
    xmlFree (entval->orig);
82
    xmlFree (entval);
83 84 85
#else
	xmlFreeNode(entval);
#endif
86 87 88
}

static void
89
instance_dispose (GObject * instance)
90
{
91
    RsvgHandle *self = (RsvgHandle *) instance;
92

93
    self->priv->is_disposed = TRUE;
94 95

#if HAVE_SVGZ
96 97
    if (self->priv->is_gzipped)
        g_object_unref (G_OBJECT (self->priv->gzipped_data));
98 99
#endif

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    g_hash_table_foreach (self->priv->entities, rsvg_ctx_free_helper, NULL);
    g_hash_table_destroy (self->priv->entities);
    rsvg_defs_free (self->priv->defs);
    g_hash_table_destroy (self->priv->css_props);

    if (self->priv->user_data_destroy)
        (*self->priv->user_data_destroy) (self->priv->user_data);

    if (self->priv->title)
        g_string_free (self->priv->title, TRUE);
    if (self->priv->desc)
        g_string_free (self->priv->desc, TRUE);
    if (self->priv->metadata)
        g_string_free (self->priv->metadata, TRUE);
    if (self->priv->base_uri)
        g_free (self->priv->base_uri);

    g_free (self->priv);

    rsvg_parent_class->dispose (G_OBJECT (self));
120 121 122
}

static void
123
set_property (GObject * instance, guint prop_id, GValue const *value, GParamSpec * pspec)
124
{
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    RsvgHandle *self = RSVG_HANDLE (instance);

    switch (prop_id) {
    case PROP_DPI_X:
        rsvg_handle_set_dpi_x_y (self, g_value_get_double (value), self->priv->dpi_y);
        break;
    case PROP_DPI_Y:
        rsvg_handle_set_dpi_x_y (self, self->priv->dpi_x, g_value_get_double (value));
        break;
    case PROP_BASE_URI:
        rsvg_handle_set_base_uri (self, g_value_get_string (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (instance, prop_id, pspec);
    }
140 141 142
}

static void
143
get_property (GObject * instance, guint prop_id, GValue * value, GParamSpec * pspec)
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
    RsvgHandle *self = RSVG_HANDLE (instance);
    RsvgDimensionData dim;

    switch (prop_id) {
    case PROP_DPI_X:
        g_value_set_double (value, self->priv->dpi_x);
        break;
    case PROP_DPI_Y:
        g_value_set_double (value, self->priv->dpi_y);
        break;
    case PROP_BASE_URI:
        g_value_set_string (value, rsvg_handle_get_base_uri (self));
        break;
    case PROP_WIDTH:
        rsvg_handle_get_dimensions (self, &dim);
        g_value_set_int (value, dim.width);
        break;
    case PROP_HEIGHT:
        rsvg_handle_get_dimensions (self, &dim);
        g_value_set_int (value, dim.height);
        break;
    case PROP_EM:
        rsvg_handle_get_dimensions (self, &dim);
        g_value_set_double (value, dim.em);
        break;
    case PROP_EX:
        rsvg_handle_get_dimensions (self, &dim);
        g_value_set_double (value, dim.ex);
        break;
    case PROP_TITLE:
        g_value_set_string (value, rsvg_handle_get_title (self));
        break;
    case PROP_DESC:
        g_value_set_string (value, rsvg_handle_get_desc (self));
        break;
    case PROP_METADATA:
        g_value_set_string (value, rsvg_handle_get_metadata (self));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (instance, prop_id, pspec);
    }
186 187 188
}

static void
189
class_init (RsvgHandleClass * klass)
190
{
191
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
192

193 194
    /* hook gobject vfuncs */
    gobject_class->dispose = instance_dispose;
195

196
    rsvg_parent_class = (GObjectClass *) g_type_class_peek_parent (klass);
197

198 199
    gobject_class->set_property = set_property;
    gobject_class->get_property = get_property;
200

201
        /**
202 203
	 * dpi-x:
	 */
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    g_object_class_install_property (gobject_class,
                                     PROP_DPI_X,
                                     g_param_spec_double ("dpi-x", _("Horizontal resolution"),
                                                          _("Horizontal resolution"), 0.,
                                                          G_MAXDOUBLE, rsvg_internal_dpi_x,
                                                          (GParamFlags) (G_PARAM_READWRITE |
                                                                         G_PARAM_CONSTRUCT)));

    g_object_class_install_property (gobject_class,
                                     PROP_DPI_Y,
                                     g_param_spec_double ("dpi-y", _("Vertical resolution"),
                                                          _("Vertical resolution"), 0., G_MAXDOUBLE,
                                                          rsvg_internal_dpi_y,
                                                          (GParamFlags) (G_PARAM_READWRITE |
                                                                         G_PARAM_CONSTRUCT)));

    g_object_class_install_property (gobject_class,
                                     PROP_BASE_URI,
                                     g_param_spec_string ("base-uri", _("Base URI"),
                                                          _("Base URI"), NULL,
                                                          (GParamFlags) (G_PARAM_READWRITE |
                                                                         G_PARAM_CONSTRUCT)));

    g_object_class_install_property (gobject_class,
                                     PROP_WIDTH,
                                     g_param_spec_int ("width", _("Image width"),
                                                       _("Image width"), 0, G_MAXINT, 0,
                                                       (GParamFlags) (G_PARAM_READABLE)));

    g_object_class_install_property (gobject_class,
                                     PROP_HEIGHT,
                                     g_param_spec_int ("height", _("Image height"),
                                                       _("Image height"), 0, G_MAXINT, 0,
                                                       (GParamFlags) (G_PARAM_READABLE)));

    g_object_class_install_property (gobject_class,
                                     PROP_EM,
                                     g_param_spec_double ("em", _("em"),
                                                          _("em"), 0, G_MAXDOUBLE, 0,
                                                          (GParamFlags) (G_PARAM_READABLE)));

    g_object_class_install_property (gobject_class,
                                     PROP_EX,
                                     g_param_spec_double ("ex", _("ex"),
                                                          _("ex"), 0, G_MAXDOUBLE, 0,
                                                          (GParamFlags) (G_PARAM_READABLE)));

    g_object_class_install_property (gobject_class,
                                     PROP_TITLE,
                                     g_param_spec_string ("title", _("Title"),
                                                          _("SVG file title"), NULL,
                                                          (GParamFlags) (G_PARAM_READABLE)));

    g_object_class_install_property (gobject_class,
                                     PROP_DESC,
                                     g_param_spec_string ("desc", _("Description"),
                                                          _("SVG file description"), NULL,
                                                          (GParamFlags) (G_PARAM_READABLE)));

    g_object_class_install_property (gobject_class,
                                     PROP_METADATA,
                                     g_param_spec_string ("metadata", _("Metadata"),
                                                          _("SVG file metadata"), NULL,
                                                          (GParamFlags) (G_PARAM_READABLE)));

    rsvg_SAX_handler_struct_init ();
270 271
}

272
const GTypeInfo rsvg_type_info = {
273 274 275 276 277 278 279 280 281
    sizeof (RsvgHandleClass),
    NULL,                       /* base_init */
    NULL,                       /* base_finalize */
    (GClassInitFunc) class_init,
    NULL,                       /* class_finalize */
    NULL,                       /* class_data */
    sizeof (RsvgHandle),
    0,                          /* n_preallocs */
    (GInstanceInitFunc) instance_init,
282 283 284 285
};

static GType rsvg_type = 0;

286
/* HACK to get around bugs 357406 and 362217. private API for now. */
287
GType
288
_rsvg_register_types (GTypeModule * module)
289
{
290 291 292 293
    rsvg_type = g_type_module_register_type (module,
                                             G_TYPE_OBJECT,
                                             "RsvgHandle", &rsvg_type_info, (GTypeFlags) 0);
    return rsvg_type;
294 295
}

296 297 298
GType
rsvg_handle_get_type (void)
{
299 300 301 302 303
    if (!rsvg_type) {
        rsvg_type =
            g_type_register_static (G_TYPE_OBJECT, "RsvgHandle", &rsvg_type_info, (GTypeFlags) 0);
    }
    return rsvg_type;
304 305
}

306 307 308 309 310 311 312 313
/**
 * rsvg_handle_free:
 * @handle: An #RsvgHandle
 *
 * Frees #handle.
 * Deprecated: Use g_object_unref() instead.
 **/
void
314
rsvg_handle_free (RsvgHandle * handle)
315
{
316
    g_object_unref (G_OBJECT (handle));
317 318
}

319 320 321
/**
 * rsvg_handle_new:
 *
Dom Lachowicz's avatar
Dom Lachowicz committed
322
 * Returns a new rsvg handle.  Must be freed with @g_object_unref.  This
323
 * handle can be used for dynamically loading an image.  You need to feed it
324 325 326 327
 * data using @rsvg_handle_write, then call @rsvg_handle_close when done.
 * Afterwords, you can render it using Cairo or get a GdkPixbuf from it. When
 * finished, free with g_object_unref(). No more than one image can be loaded 
 * with one handle.
328 329 330 331 332 333
 *
 * Returns: A new #RsvgHandle
 **/
RsvgHandle *
rsvg_handle_new (void)
{
334
    return RSVG_HANDLE (g_object_new (RSVG_TYPE_HANDLE, NULL));
335
}