File: fo-image.c

package info (click to toggle)
xmlroff 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 36,456 kB
  • ctags: 25,964
  • sloc: ansic: 178,200; xml: 109,155; sh: 8,973; makefile: 1,331; perl: 30
file content (375 lines) | stat: -rw-r--r-- 8,021 bytes parent folder | download | duplicates (5)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Fo
 * fo-image.c: Object representing an image.
 *
 * Copyright (C) 2003 Sun Microsystems
 * Copyright (C) 2007 Menteith Consulting Ltd
 *
 * See COPYING for the status of this software.
 */

#include "fo-image.h"
#include "fo-pixbuf.h"
#include "fo-utils.h"
#include "datatype/fo-length.h"
#include <libxml/uri.h>

struct _FoImage
{
  FoObject parent_instance;

  GdkPixbuf *pixbuf;
  gchar     *uri;

  FoDatatype *width;
  FoDatatype *height;
};

struct _FoImageClass
{
  FoObjectClass parent_class;
};

static void fo_image_class_init  (FoImageClass  *klass);
static void fo_image_pixbuf_init (FoPixbufIface *iface);
static void fo_image_finalize    (GObject       *object);
static void fo_image_debug_dump  (FoObject      *object,
				  gint           depth);

static void        fo_image_set_uri    (FoImage     *fo_image,
					const gchar *uri);
static GdkPixbuf * fo_image_get_pixbuf (FoImage     *fo_image);
static void        fo_image_set_pixbuf (FoImage     *fo_image,
					GdkPixbuf   *pixbuf);

static gpointer parent_class;

/**
 * fo_image_get_type:
 * 
 * Register the #FoImage object type.
 * 
 * Return value: #GType value of the #FoImage object type.
 **/
GType
fo_image_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (FoImageClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) fo_image_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (FoImage),
        0,              /* n_preallocs */
        NULL,		/* instance_init */
	NULL		/* value_table */
      };
      
      static const GInterfaceInfo fo_pixbuf_info =
      {
	(GInterfaceInitFunc) fo_image_pixbuf_init, /* interface_init */
        NULL,
        NULL
      };

      object_type = g_type_register_static (FO_TYPE_OBJECT,
                                            "FoImage",
                                            &object_info,
					    0);
      g_type_add_interface_static (object_type,
                                   FO_TYPE_PIXBUF,
                                   &fo_pixbuf_info);
    }
  
  return object_type;
}

/**
 * fo_image_class_init:
 * @klass: #FoImageClass object to initialise.
 * 
 * Implements #GClassInitFunc for #FoHashTableClass.
 **/
void
fo_image_class_init (FoImageClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  FoObjectClass *fo_object_class = FO_OBJECT_CLASS (klass);
  
  parent_class = g_type_class_peek_parent (klass);
  
  object_class->finalize = fo_image_finalize;

  fo_object_class->debug_dump = fo_image_debug_dump;
}

/**
 * fo_image_pixbuf_init:
 * @iface: #FoPixbufIFace structure for this class.
 * 
 * Initialize #FoPixbufIface interface for this class.
 **/
void
fo_image_pixbuf_init (FoPixbufIface *iface)
{
  iface->get_pixbuf = fo_image_get_pixbuf;
  iface->set_pixbuf = fo_image_set_pixbuf;
}

/**
 * fo_image_finalize:
 * @object: #FoImage object to finalize.
 * 
 * Implements #GObjectFinalizeFunc for #FoImage.
 **/
void
fo_image_finalize (GObject *object)
{
  FoImage *fo_image;

  fo_image = FO_IMAGE (object);

  g_free (fo_image->uri);

  if (fo_image->pixbuf != NULL)
    {
      g_object_unref (fo_image->pixbuf);
      fo_image->pixbuf = NULL;
    }

  if (fo_image->width != NULL)
    {
      g_object_unref (fo_image->width);
      fo_image->width = NULL;
    }

  if (fo_image->height != NULL)
    {
      g_object_unref (fo_image->height);
      fo_image->height = NULL;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/**
 * fo_image_new:
 * 
 * Creates a new #FoImage.
 * 
 * Return value: the newly created #FoImage.
 **/
FoImage *
fo_image_new (void)
{
  FoImage *fo_image;

  fo_image = FO_IMAGE (g_object_new (fo_image_get_type (),
				     NULL));

  return fo_image;
}

/**
 * fo_image_new_from_uri:
 * @uri:  URI of image.
 * @base: Base URI for resolving relative URI @uri values.
 * 
 * Creates a new #FoImage from the resource at @uri.
 *
 * If @uri is a relative URI, it is resolved relative to @base.
 * 
 * Return value: the newly created #FoImageo or #NULL.
 **/
FoImage *
fo_image_new_from_uri (const gchar *uri,
		       const gchar *base)
{
  FoImage *fo_image;
  gchar *resolved_uri;

  fo_image = FO_IMAGE (g_object_new (fo_image_get_type (),
				     NULL));

  resolved_uri = (gchar *) xmlBuildURI ((const xmlChar *) uri,
					(const xmlChar *) base);

  fo_image_set_uri (fo_image, resolved_uri);

  xmlFree ((xmlChar *) resolved_uri);

  return fo_image_get_pixbuf (fo_image) != NULL ? fo_image : NULL;
}

/**
 * fo_image_get_pixbuf:
 * @fo_image: #FoImage
 * 
 * Get the #GdkPixbuf in @fo_image.
 * 
 * Return value: #GdkPixbuf.
 **/
GdkPixbuf *
fo_image_get_pixbuf (FoImage *fo_image)
{
  g_return_val_if_fail (fo_image != NULL, NULL);

  return fo_image->pixbuf;
}

/**
 * fo_image_set_pixbuf:
 * @fo_image: #FoImage.
 * @pixbuf:   #GdkPixbuf.
 * 
 * Set the output #GdkPixbuf in @fo_image.
 **/
void
fo_image_set_pixbuf (FoImage   *fo_image,
		     GdkPixbuf *pixbuf)
{
  g_return_if_fail (fo_image != NULL);

  fo_image->pixbuf = pixbuf;
}

/**
 * fo_image_get_uri:
 * @fo_image: #FoImage
 * 
 * Get the URI in @fo_image.
 * 
 * Return value: URI of image to make @fo_image.
 **/
const gchar *
fo_image_get_uri (FoImage *fo_image)
{
  g_return_val_if_fail (fo_image != NULL, NULL);

  return fo_image->uri;
}

/**
 * fo_image_set_uri:
 * @fo_image: #FoImage
 * @uri:      URI of image.
 * 
 * Set the URI of image in @fo_image.
 **/
void
fo_image_set_uri (FoImage     *fo_image,
		  const gchar *uri)
{
  GError *error;

  g_return_if_fail (fo_image != NULL);

  fo_image->uri = g_strdup (uri);

  /* Load the image into a pixbuf */
  error = NULL;
  fo_image->pixbuf = gdk_pixbuf_new_from_file (uri, &error);
  if (error != NULL)
    {
      fo_object_log_error (FO_OBJECT (fo_image),
			   &error);
    }
  if (fo_image->pixbuf != NULL)
    {
      fo_image->width =
	g_object_ref (fo_length_new_from_pixels (gdk_pixbuf_get_width (fo_image->pixbuf)));
      fo_image->height =
	g_object_ref (fo_length_new_from_pixels (gdk_pixbuf_get_height (fo_image->pixbuf)));
    }
  else
    {
      g_print ("Could not load image.\n");
    }
}

/**
 * fo_image_get_width:
 * @fo_image: #FoImage
 * 
 * Gets the intrinsic width of @fo_image.
 * 
 * Return value: The intrinsic width of @fo_image.
 **/
FoDatatype *
fo_image_get_width  (const FoImage *fo_image)
{
  g_return_val_if_fail (fo_image != NULL, 0);
  g_return_val_if_fail (FO_IS_IMAGE (fo_image), 0);
  g_return_val_if_fail (fo_image->pixbuf != NULL, 0);

  return fo_image->width;
}

/**
 * fo_image_get_height:
 * @fo_image: #FoImage.
 * 
 * Gets the intrinsic height of @fo_image.
 * 
 * Return value: The intrinsic height of @fo_image.
 **/
FoDatatype *
fo_image_get_height (const FoImage *fo_image)
{
  g_return_val_if_fail (fo_image != NULL, 0);
  g_return_val_if_fail (FO_IS_IMAGE (fo_image), 0);
  g_return_val_if_fail (fo_image->pixbuf != NULL, 0);

  return fo_image->height;
}

/**
 * fo_image_debug_dump:
 * @fo: The #FoFo object
 * @depth: Indent level to add to the output
 * 
 * Calls #fo_object_debug_dump on each property of @fo then calls
 * debug_dump_properties method of parent class
 **/
void
fo_image_debug_dump (FoObject *object,
		     gint      depth)
{
  FoImage *fo_image;
  gchar *indent = g_strnfill (depth * 2, ' ');

  g_return_if_fail (object != NULL);
  g_return_if_fail (FO_IS_IMAGE (object));

  fo_image = FO_IMAGE (object);

  g_log (G_LOG_DOMAIN,
	 G_LOG_LEVEL_DEBUG,
	 "%suri : %s",
	 indent,
	 fo_image->uri);
  g_log (G_LOG_DOMAIN,
	 G_LOG_LEVEL_DEBUG,
	 "%spixbuf : %s",
	 indent,
	 fo_object_sprintf (fo_image->pixbuf));
  g_log (G_LOG_DOMAIN,
	 G_LOG_LEVEL_DEBUG,
	 "%swidth :",
	 indent);
  fo_object_debug_dump (fo_image->width, depth + 1);

  g_log (G_LOG_DOMAIN,
	 G_LOG_LEVEL_DEBUG,
	 "%sheight :",
	 indent);
  fo_object_debug_dump (fo_image->height, depth + 1);

  g_free (indent);
}