Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar3/rsvg-wrapper.c
blob: ec3a4cd7b611a0c5665f5746c068ad311fb0ab5f (plain)
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
/* rsvg-wrapper.c
 * Copyright (C) 2011 Raul Gutierrez Segales
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 */


/* Wrapper around rsvg while it gets introspection support.
 *
 * See: https://bugzilla.gnome.org/show_bug.cgi?id=663049
 */

#include "rsvg-wrapper.h"
#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>


G_DEFINE_TYPE (SugarRsvgWrapper, sugar_rsvg_wrapper, G_TYPE_OBJECT)

#define RSVG_WRAPPER_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), SUGAR_TYPE_RSVG_WRAPPER, SugarRsvgWrapperPrivate))

struct _SugarRsvgWrapperPrivate
{
  RsvgHandle *handle;
};

static void
sugar_rsvg_wrapper_dispose (GObject *object)
{
  SugarRsvgWrapper *self = SUGAR_RSVG_WRAPPER (object);
  SugarRsvgWrapperPrivate *priv = self->priv;

  if (priv->handle)
    rsvg_handle_free (priv->handle);

  G_OBJECT_CLASS (sugar_rsvg_wrapper_parent_class)->dispose (object);
}

static void
sugar_rsvg_wrapper_class_init (SugarRsvgWrapperClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (SugarRsvgWrapperPrivate));

  object_class->dispose = sugar_rsvg_wrapper_dispose;
}

static void
sugar_rsvg_wrapper_init (SugarRsvgWrapper *wrapper)
{
  SugarRsvgWrapperPrivate *priv;

  priv = wrapper->priv = RSVG_WRAPPER_PRIVATE (wrapper);
  priv->handle = NULL;
}


/**
 * sugar_rsvg_wrapper_new:
 * @data: (transfer none) (array length=len): the image data
 * @len: the length of @data
 *
 * Creates a new wrapper object
 *
 * Returns: (transfer full): new #SugarRsvgWrapper
 **/
SugarRsvgWrapper*
sugar_rsvg_wrapper_new (const guint8 *data,
			gsize len)
{
  SugarRsvgWrapper* wrapper = g_object_new (SUGAR_TYPE_RSVG_WRAPPER, NULL);
  SugarRsvgWrapperPrivate *priv;
  GError *error;

  priv = RSVG_WRAPPER_PRIVATE (wrapper);

  /* My code never fails, hence I don't bother checking
   * the error after the call - rgs
   */
  priv->handle = rsvg_handle_new_from_data (data, len, &error);

  return wrapper;
}

/**
 * sugar_rsvg_wrapper_get_width:
 * @wrapper: an #SugarRsvgWrapper
 *
 * Gets the width of the associated RsvgHandle.
 *
 * Returns: The width of the wrapped RsvgHandle
 **/
int sugar_rsvg_wrapper_get_width(SugarRsvgWrapper *wrapper)
{
  SugarRsvgWrapperPrivate *priv = RSVG_WRAPPER_PRIVATE (wrapper);
  RsvgDimensionData dim;

  rsvg_handle_get_dimensions (priv->handle, &dim);
  return dim.width;
}

/**
 * sugar_rsvg_wrapper_get_height:
 * @wrapper: an #SugarRsvgWrapper
 *
 * Gets the height of the associated RsvgHandle.
 *
 * Returns: The height of the wrapped RsvgHandle
 **/
int sugar_rsvg_wrapper_get_height(SugarRsvgWrapper *wrapper)
{
  SugarRsvgWrapperPrivate *priv = RSVG_WRAPPER_PRIVATE (wrapper);
  RsvgDimensionData dim;

  rsvg_handle_get_dimensions (priv->handle, &dim);
  return dim.height;
}

/**
 * sugar_rsvg_wrapper_render_cairo:
 * @wrapper: an #SugarRsvgWrapper
 * @cr: the cairo region
 *
 **/
void sugar_rsvg_wrapper_render_cairo(SugarRsvgWrapper *wrapper, cairo_t * cr)
{
  SugarRsvgWrapperPrivate *priv = RSVG_WRAPPER_PRIVATE (wrapper);
  rsvg_handle_render_cairo (priv->handle, cr);
}

/**
 * sugar_rsvg_wrapper_get_pixbuf:
 * @wrapper: an #SugarRsvgWrapper
 *
 * Returns: (transfer full): the #GdkPixbuf
 **/
GdkPixbuf *sugar_rsvg_wrapper_get_pixbuf(SugarRsvgWrapper *wrapper)
{
  SugarRsvgWrapperPrivate *priv = RSVG_WRAPPER_PRIVATE (wrapper);
  return rsvg_handle_get_pixbuf (priv->handle);
}