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
|
#include "svgpaintable.h"
#include <gtk/gtk.h>
#include <librsvg/rsvg.h>
struct _SvgPaintable
{
GObject parent_instance;
GFile *file;
RsvgHandle *handle;
};
struct _SvgPaintableClass
{
GObjectClass parent_class;
};
enum {
PROP_FILE = 1,
NUM_PROPERTIES
};
static void
svg_paintable_snapshot (GdkPaintable *paintable,
GdkSnapshot *snapshot,
double width,
double height)
{
SvgPaintable *self = SVG_PAINTABLE (paintable);
cairo_t *cr;
GError *error = NULL;
cr = gtk_snapshot_append_cairo (GTK_SNAPSHOT (snapshot),
&GRAPHENE_RECT_INIT (0, 0, width, height));
if (!rsvg_handle_render_document (self->handle, cr,
&(RsvgRectangle) {0, 0, width, height},
&error))
{
g_error ("%s", error->message);
}
cairo_destroy (cr);
}
static int
svg_paintable_get_intrinsic_width (GdkPaintable *paintable)
{
SvgPaintable *self = SVG_PAINTABLE (paintable);
RsvgDimensionData data;
rsvg_handle_get_dimensions (self->handle, &data);
return data.width;
}
static int
svg_paintable_get_intrinsic_height (GdkPaintable *paintable)
{
SvgPaintable *self = SVG_PAINTABLE (paintable);
RsvgDimensionData data;
rsvg_handle_get_dimensions (self->handle, &data);
return data.height;
}
static void
svg_paintable_init_interface (GdkPaintableInterface *iface)
{
iface->snapshot = svg_paintable_snapshot;
iface->get_intrinsic_width = svg_paintable_get_intrinsic_width;
iface->get_intrinsic_height = svg_paintable_get_intrinsic_height;
}
G_DEFINE_TYPE_WITH_CODE (SvgPaintable, svg_paintable, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
svg_paintable_init_interface))
static void
svg_paintable_init (SvgPaintable *self)
{
}
static void
svg_paintable_dispose (GObject *object)
{
SvgPaintable *self = SVG_PAINTABLE (object);
g_clear_object (&self->file);
g_clear_object (&self->handle);
G_OBJECT_CLASS (svg_paintable_parent_class)->dispose (object);
}
static void
svg_paintable_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
SvgPaintable *self = SVG_PAINTABLE (object);
switch (prop_id)
{
case PROP_FILE:
{
GFile *file = g_value_get_object (value);
RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file,
RSVG_HANDLE_FLAGS_NONE,
NULL,
NULL);
rsvg_handle_set_dpi (handle, 90);
g_set_object (&self->file, file);
g_set_object (&self->handle, handle);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
svg_paintable_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
SvgPaintable *self = SVG_PAINTABLE (object);
switch (prop_id)
{
case PROP_FILE:
g_value_set_object (value, self->file);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
svg_paintable_class_init (SvgPaintableClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->dispose = svg_paintable_dispose;
object_class->set_property = svg_paintable_set_property;
object_class->get_property = svg_paintable_get_property;
g_object_class_install_property (object_class, PROP_FILE,
g_param_spec_object ("file", "File", "File",
G_TYPE_FILE,
G_PARAM_READWRITE));
}
GdkPaintable *
svg_paintable_new (GFile *file)
{
return g_object_new (SVG_TYPE_PAINTABLE,
"file", file,
NULL);
}
|