rsvg-gobject.c 13.4 KB
Newer Older
1 2
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=4 ts=4 expandtab: */
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
   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.
*/

Christian Persch's avatar
Christian Persch committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * SECTION: rsvg-handle
 * @short_description: Create and manipulate SVG objects
 *
 * librsvg is a component used within software applications to enable
 * support for SVG-format scalable graphics. In contrast to raster
 * formats, scalable vector graphics provide users and artists a way
 * to create, view, and provide imagery that is not limited to the
 * pixel or dot density that an output device is capable of.
 *
 * Many software developers use the librsvg library to render
 * SVG graphics. It is lightweight and portable.
 */

38 39
#include "config.h"

40 41
#include "rsvg-private.h"
#include "rsvg-defs.h"
42
#include "rsvg.h"
43 44

enum {
45
    PROP_0,
46
    PROP_FLAGS,
47 48 49 50 51 52 53 54 55 56 57
    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
58 59 60 61 62
};

extern double rsvg_internal_dpi_x;
extern double rsvg_internal_dpi_y;

63
G_DEFINE_TYPE (RsvgHandle, rsvg_handle, G_TYPE_OBJECT)
64 65

static void
66
rsvg_handle_init (RsvgHandle * self)
67
{
68 69
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, RSVG_TYPE_HANDLE, RsvgHandlePrivate);

70
    self->priv->flags = RSVG_HANDLE_FLAGS_NONE;
71
    self->priv->all_nodes = g_ptr_array_new ();
72
    self->priv->defs = rsvg_defs_new (self);
73
    self->priv->handler_nest = 0;
Christian Persch's avatar
Christian Persch committed
74 75 76
    self->priv->entities = g_hash_table_new_full (g_str_hash, 
                                                  g_str_equal,
                                                  g_free,
77
                                                  (GDestroyNotify) xmlFreeNode);
78 79 80
    self->priv->dpi_x = rsvg_internal_dpi_x;
    self->priv->dpi_y = rsvg_internal_dpi_y;

81 82 83 84
    self->priv->css_props = g_hash_table_new_full (g_str_hash,
                                                   g_str_equal,
                                                   g_free,
                                                   (GDestroyNotify) g_hash_table_destroy);
85 86 87 88

    self->priv->ctxt = NULL;
    self->priv->currentnode = NULL;
    self->priv->treebase = NULL;
89
    self->priv->element_name_stack = NULL;
90 91

    self->priv->finished = 0;
92
    self->priv->data_input_stream = NULL;
93
    self->priv->first_write = TRUE;
94
    self->priv->cancellable = NULL;
95 96

    self->priv->is_disposed = FALSE;
97
    self->priv->in_loop = FALSE;
98 99

    self->priv->is_testing = FALSE;
100 101
}

102 103 104 105 106 107 108 109 110
static void
free_nodes (RsvgHandle *self)
{
    int i;

    for (i = 0; i < self->priv->all_nodes->len; i++) {
        RsvgNode *node;

        node = g_ptr_array_index (self->priv->all_nodes, i);
111 112
        g_assert (node->vtable->free != NULL);
        node->vtable->free (node);
113 114 115 116 117 118
    }

    g_ptr_array_free (self->priv->all_nodes, TRUE);
    self->priv->all_nodes = NULL;
}

119
static void
120
rsvg_handle_dispose (GObject *instance)
121
{
122
    RsvgHandle *self = (RsvgHandle *) instance;
123

124 125 126
    if (self->priv->is_disposed)
      goto chain;

127
    self->priv->is_disposed = TRUE;
128

129
    g_hash_table_destroy (self->priv->entities);
130 131 132

    free_nodes (self);

133
    rsvg_defs_free (self->priv->defs);
134 135
    self->priv->defs = NULL;

136 137 138 139 140 141 142 143 144 145 146 147 148 149
    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);

150 151 152 153
    if (self->priv->base_gfile) {
        g_object_unref (self->priv->base_gfile);
        self->priv->base_gfile = NULL;
    }
154 155 156 157
    if (self->priv->data_input_stream) {
        g_object_unref (self->priv->data_input_stream);
        self->priv->data_input_stream = NULL;
    }
158

159 160
    g_clear_object (&self->priv->cancellable);

161
  chain:
162
    G_OBJECT_CLASS (rsvg_handle_parent_class)->dispose (instance);
163 164 165
}

static void
166
rsvg_handle_set_property (GObject * instance, guint prop_id, GValue const *value, GParamSpec * pspec)
167
{
168 169 170
    RsvgHandle *self = RSVG_HANDLE (instance);

    switch (prop_id) {
171 172 173
    case PROP_FLAGS:
        self->priv->flags = g_value_get_flags (value);
        break;
174 175 176 177 178 179 180 181 182 183 184 185
    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);
    }
186 187 188
}

static void
189
rsvg_handle_get_property (GObject * instance, guint prop_id, GValue * value, GParamSpec * pspec)
190
{
191 192 193 194
    RsvgHandle *self = RSVG_HANDLE (instance);
    RsvgDimensionData dim;

    switch (prop_id) {
195 196 197
    case PROP_FLAGS:
        g_value_set_flags (value, self->priv->flags);
        break;
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 225 226 227 228 229 230 231 232 233 234
    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);
    }
235 236 237
}

static void
238
rsvg_handle_class_init (RsvgHandleClass * klass)
239
{
240
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
241

242 243 244
    gobject_class->dispose = rsvg_handle_dispose;
    gobject_class->set_property = rsvg_handle_set_property;
    gobject_class->get_property = rsvg_handle_get_property;
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259
    /**
     * RsvgHandle:flags:
     * 
     * Flags from #RsvgHandleFlags.
     * 
     * Since: 2.36
     */
    g_object_class_install_property (gobject_class,
                                     PROP_FLAGS,
                                     g_param_spec_flags ("flags", NULL, NULL,
                                                         RSVG_TYPE_HANDLE_FLAGS,
                                                         RSVG_HANDLE_FLAGS_NONE,
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

260 261 262
    /**
     * dpi-x:
     */
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    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)));

310 311 312 313 314 315 316
    /**
     * RsvgHandle:title:
     *
     * SVG's description
     *
     * Deprecated: 2.36
     */
317 318 319 320 321 322
    g_object_class_install_property (gobject_class,
                                     PROP_TITLE,
                                     g_param_spec_string ("title", _("Title"),
                                                          _("SVG file title"), NULL,
                                                          (GParamFlags) (G_PARAM_READABLE)));

323 324 325 326 327 328 329
    /**
     * RsvgHandle:desc:
     *
     * SVG's description
     *
     * Deprecated: 2.36
     */
330 331 332 333 334 335
    g_object_class_install_property (gobject_class,
                                     PROP_DESC,
                                     g_param_spec_string ("desc", _("Description"),
                                                          _("SVG file description"), NULL,
                                                          (GParamFlags) (G_PARAM_READABLE)));

336 337 338 339 340 341 342
    /**
     * RsvgHandle:metadata:
     *
     * SVG's description
     *
     * Deprecated: 2.36
     */
343 344 345 346 347 348
    g_object_class_install_property (gobject_class,
                                     PROP_METADATA,
                                     g_param_spec_string ("metadata", _("Metadata"),
                                                          _("SVG file metadata"), NULL,
                                                          (GParamFlags) (G_PARAM_READABLE)));

349 350
    g_type_class_add_private (klass, sizeof (RsvgHandlePrivate));

351 352
    xmlInitParser ();

353
    rsvg_SAX_handler_struct_init ();
354 355
}

356 357 358 359
/**
 * rsvg_handle_free:
 * @handle: An #RsvgHandle
 *
360
 * Frees @handle.
361 362 363
 * Deprecated: Use g_object_unref() instead.
 **/
void
364
rsvg_handle_free (RsvgHandle * handle)
365
{
366
    g_object_unref (handle);
367 368
}

369 370 371
/**
 * rsvg_handle_new:
 *
Dom Lachowicz's avatar
Dom Lachowicz committed
372
 * Returns a new rsvg handle.  Must be freed with @g_object_unref.  This
373
 * handle can be used for dynamically loading an image.  You need to feed it
374 375 376 377
 * 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.
378 379 380 381 382 383
 *
 * Returns: A new #RsvgHandle
 **/
RsvgHandle *
rsvg_handle_new (void)
{
384
    return RSVG_HANDLE (g_object_new (RSVG_TYPE_HANDLE, NULL));
385
}
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404


/**
 * rsvg_handle_new_with_flags:
 * @flags: flags from #RsvgHandleFlags
 *
 * Creates a new #RsvgHandle with flags @flags.
 *
 * Returns: (transfer full): a new #RsvgHandle
 * 
 * Since: 2.36
 **/
RsvgHandle *
rsvg_handle_new_with_flags (RsvgHandleFlags flags)
{
    return g_object_new (RSVG_TYPE_HANDLE, 
                         "flags", flags,
                         NULL);
}