1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Nickolay V. Shmyrev <nshmyrev@yandex.ru>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "djvu-document.h"
21 #include "ev-document-thumbnails.h"
22 #include "ev-document-misc.h"
24 #include <libdjvu/ddjvuapi.h>
26 #include <gdk-pixbuf/gdk-pixbuf-core.h>
28 #include <libgnomevfs/gnome-vfs-uri.h>
29 #include <libgnomevfs/gnome-vfs-utils.h>
30 #include <libgnomevfs/gnome-vfs-ops.h>
31 #include <libgnomevfs/gnome-vfs-xfer.h>
33 #define SCALE_FACTOR 0.2
40 struct _DjvuDocumentClass
42 GObjectClass parent_class;
47 GObject parent_instance;
49 ddjvu_context_t *d_context;
50 ddjvu_document_t *d_document;
51 ddjvu_format_t *d_format;
56 typedef struct _DjvuDocumentClass DjvuDocumentClass;
58 static void djvu_document_document_iface_init (EvDocumentIface *iface);
59 static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
61 G_DEFINE_TYPE_WITH_CODE
62 (DjvuDocument, djvu_document, G_TYPE_OBJECT,
64 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, djvu_document_document_iface_init);
65 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init)
69 djvu_document_load (EvDocument *document,
73 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
74 ddjvu_document_t *doc;
77 /* FIXME: We could actually load uris */
78 filename = g_filename_from_uri (uri, NULL, error);
82 doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
84 if (!doc) return FALSE;
86 if (djvu_document->d_document)
87 ddjvu_document_release (djvu_document->d_document);
89 djvu_document->d_document = doc;
91 while (!ddjvu_document_decoding_done (djvu_document->d_document)) {
92 ddjvu_message_wait (djvu_document->d_context);
93 ddjvu_message_pop (djvu_document->d_context);
95 g_free (djvu_document->uri);
96 djvu_document->uri = g_strdup (uri);
103 djvu_document_save (EvDocument *document,
107 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
108 GnomeVFSResult result;
109 GnomeVFSURI *source_uri;
110 GnomeVFSURI *target_uri;
112 if (!djvu_document->uri)
115 source_uri = gnome_vfs_uri_new (djvu_document->uri);
116 target_uri = gnome_vfs_uri_new (uri);
118 result = gnome_vfs_xfer_uri (source_uri, target_uri,
119 GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
120 GNOME_VFS_XFER_ERROR_MODE_ABORT,
121 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
124 gnome_vfs_uri_unref (target_uri);
125 gnome_vfs_uri_unref (source_uri);
127 if (result != GNOME_VFS_OK)
131 gnome_vfs_result_to_string (result));
132 return (result == GNOME_VFS_OK);
136 djvu_document_get_n_pages (EvDocument *document)
138 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
140 g_return_val_if_fail (djvu_document->d_document, 0);
142 return ddjvu_document_get_pagenum (djvu_document->d_document);
146 djvu_document_get_page_size (EvDocument *document,
151 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
152 ddjvu_pageinfo_t info;
154 g_return_if_fail (djvu_document->d_document);
156 while (ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info) < DDJVU_JOB_OK) {
157 ddjvu_message_wait (djvu_document->d_context);
158 ddjvu_message_pop (djvu_document->d_context);
161 *width = info.width * SCALE_FACTOR;
162 *height = info.height * SCALE_FACTOR;
166 djvu_document_render_pixbuf (EvDocument *document,
169 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
171 GdkPixbuf *rotated_pixbuf;
175 ddjvu_page_t *d_page;
177 double page_width, page_height;
179 d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page);
181 while (!ddjvu_page_decoding_done (d_page)) {
182 ddjvu_message_wait (djvu_document->d_context);
183 ddjvu_message_pop (djvu_document->d_context);
186 page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR;
187 page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR;
189 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height);
191 prect.x = 0; prect.y = 0;
192 prect.w = page_width; prect.h = page_height;
195 ddjvu_page_render(d_page, DDJVU_RENDER_COLOR,
198 djvu_document->d_format,
199 gdk_pixbuf_get_rowstride (pixbuf),
200 (gchar *)gdk_pixbuf_get_pixels (pixbuf));
202 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
203 g_object_unref (pixbuf);
205 return rotated_pixbuf;
209 djvu_document_finalize (GObject *object)
211 DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
213 if (djvu_document->d_document)
214 ddjvu_document_release (djvu_document->d_document);
216 ddjvu_context_release (djvu_document->d_context);
217 ddjvu_format_release (djvu_document->d_format);
218 g_free (djvu_document->uri);
220 G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
224 djvu_document_class_init (DjvuDocumentClass *klass)
226 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
228 gobject_class->finalize = djvu_document_finalize;
232 djvu_document_can_get_text (EvDocument *document)
237 static EvDocumentInfo *
238 djvu_document_get_info (EvDocument *document)
240 EvDocumentInfo *info;
242 info = g_new0 (EvDocumentInfo, 1);
248 djvu_document_document_iface_init (EvDocumentIface *iface)
250 iface->load = djvu_document_load;
251 iface->save = djvu_document_save;
252 iface->can_get_text = djvu_document_can_get_text;
253 iface->get_n_pages = djvu_document_get_n_pages;
254 iface->get_page_size = djvu_document_get_page_size;
255 iface->render_pixbuf = djvu_document_render_pixbuf;
256 iface->get_info = djvu_document_get_info;
260 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
262 gint suggested_width,
266 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
267 gdouble p_width, p_height;
270 djvu_document_get_page_size (EV_DOCUMENT(djvu_document), page, &p_width, &p_height);
272 page_ratio = p_height / p_width;
273 *width = suggested_width;
274 *height = (gint) (suggested_width * page_ratio);
280 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
286 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
287 GdkPixbuf *pixbuf, *rotated_pixbuf;
288 gint thumb_width, thumb_height;
292 g_return_val_if_fail (djvu_document->d_document, NULL);
294 djvu_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
296 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
297 thumb_width, thumb_height);
298 gdk_pixbuf_fill (pixbuf, 0xffffffff);
299 pixels = gdk_pixbuf_get_pixels (pixbuf);
301 while (ddjvu_thumbnail_status (djvu_document->d_document, page, 1) < DDJVU_JOB_OK) {
302 ddjvu_message_wait (djvu_document->d_context);
303 ddjvu_message_pop (djvu_document->d_context);
306 ddjvu_thumbnail_render (djvu_document->d_document, page,
307 &thumb_width, &thumb_height,
308 djvu_document->d_format,
309 gdk_pixbuf_get_rowstride (pixbuf),
312 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation);
313 g_object_unref (pixbuf);
316 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
317 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf);
318 g_object_unref (tmp_pixbuf);
321 return rotated_pixbuf;
325 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
327 iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
328 iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
332 djvu_document_init (DjvuDocument *djvu_document)
334 djvu_document->d_context = ddjvu_context_create ("Evince");
335 djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
336 ddjvu_format_set_row_order (djvu_document->d_format,1);
338 djvu_document->d_document = NULL;