rsvg-gobject.c 13.5 KB
Newer Older
1 2
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=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 42 43
#include "rsvg-private.h"
#include "rsvg-defs.h"

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

extern double rsvg_internal_dpi_x;
extern double rsvg_internal_dpi_y;

62
G_DEFINE_TYPE (RsvgHandle, rsvg_handle, G_TYPE_OBJECT)
63 64

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

69
    self->priv->flags = RSVG_HANDLE_FLAGS_NONE;
70
    self->priv->state = RSVG_HANDLE_STATE_START;
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->data_input_stream = NULL;
92
    self->priv->cancellable = NULL;
93 94

    self->priv->is_disposed = FALSE;
95
    self->priv->in_loop = FALSE;
96 97

    self->priv->is_testing = FALSE;
98 99
}

100 101 102 103 104
static void
free_nodes (RsvgHandle *self)
{
    int i;

105 106
    g_assert (self->priv->all_nodes != NULL);

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

        node = g_ptr_array_index (self->priv->all_nodes, i);
111
        node = rsvg_node_unref (node);
112 113 114 115 116 117
    }

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

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

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

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

128 129
    self->priv->ctxt = rsvg_free_xml_parser_and_doc (self->priv->ctxt);

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

    free_nodes (self);

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

137 138
    g_hash_table_destroy (self->priv->css_props);

139 140 141
    self->priv->treebase = rsvg_node_unref (self->priv->treebase);
    self->priv->currentnode = rsvg_node_unref (self->priv->currentnode);

142 143 144 145 146 147 148 149 150 151 152 153
    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);

154 155 156 157
    if (self->priv->base_gfile) {
        g_object_unref (self->priv->base_gfile);
        self->priv->base_gfile = NULL;
    }
158 159 160 161
    if (self->priv->data_input_stream) {
        g_object_unref (self->priv->data_input_stream);
        self->priv->data_input_stream = NULL;
    }
162

163 164
    g_clear_object (&self->priv->cancellable);

165
  chain:
166
    G_OBJECT_CLASS (rsvg_handle_parent_class)->dispose (instance);
167 168 169
}

static void
170
rsvg_handle_set_property (GObject * instance, guint prop_id, GValue const *value, GParamSpec * pspec)
171
{
172 173 174
    RsvgHandle *self = RSVG_HANDLE (instance);

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

static void
193
rsvg_handle_get_property (GObject * instance, guint prop_id, GValue * value, GParamSpec * pspec)
194
{
195 196 197 198
    RsvgHandle *self = RSVG_HANDLE (instance);
    RsvgDimensionData dim;

    switch (prop_id) {
199 200 201
    case PROP_FLAGS:
        g_value_set_flags (value, self->priv->flags);
        break;
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 235 236 237 238
    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);
    }
239 240 241
}

static void
242
rsvg_handle_class_init (RsvgHandleClass * klass)
243
{
244
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
245

246 247 248
    gobject_class->dispose = rsvg_handle_dispose;
    gobject_class->set_property = rsvg_handle_set_property;
    gobject_class->get_property = rsvg_handle_get_property;
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263
    /**
     * 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));

264 265 266
    /**
     * dpi-x:
     */
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 310 311 312 313
    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)));

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

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

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

353 354
    g_type_class_add_private (klass, sizeof (RsvgHandlePrivate));

355 356
    xmlInitParser ();

357
    rsvg_SAX_handler_struct_init ();
358 359
}

360 361 362 363
/**
 * rsvg_handle_free:
 * @handle: An #RsvgHandle
 *
364
 * Frees @handle.
365 366 367
 * Deprecated: Use g_object_unref() instead.
 **/
void
368
rsvg_handle_free (RsvgHandle * handle)
369
{
370
    g_object_unref (handle);
371 372
}

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


/**
 * 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);
}