]> www.fi.muni.cz Git - evince.git/blob - djvu/djvu-document.c
aa853a71e68d72c1be4518d733c929ac9bf1195f
[evince.git] / djvu / djvu-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2005, Nickolay V. Shmyrev <nshmyrev@yandex.ru>
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  */
19
20 #include "djvu-document.h"
21 #include "ev-document-thumbnails.h"
22 #include "ev-document-misc.h"
23
24 #include <libdjvu/ddjvuapi.h>
25 #include <gtk/gtk.h>
26 #include <gdk-pixbuf/gdk-pixbuf-core.h>
27
28 #define SCALE_FACTOR 0.2
29
30 enum {
31         PROP_0,
32         PROP_TITLE
33 };
34
35 struct _DjvuDocumentClass
36 {
37         GObjectClass parent_class;
38 };
39
40 struct _DjvuDocument
41 {
42         GObject parent_instance;
43
44         ddjvu_context_t  *d_context;
45         ddjvu_document_t *d_document;
46         ddjvu_format_t   *d_format;
47 };
48
49 typedef struct _DjvuDocumentClass DjvuDocumentClass;
50
51 static void djvu_document_document_iface_init (EvDocumentIface *iface);
52 static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
53
54 G_DEFINE_TYPE_WITH_CODE 
55     (DjvuDocument, djvu_document, G_TYPE_OBJECT, 
56     {
57       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, djvu_document_document_iface_init);    
58       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init)
59      });
60
61 static gboolean
62 djvu_document_load (EvDocument  *document,
63                       const char  *uri,
64                       GError     **error)
65 {
66         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
67         ddjvu_document_t *doc;
68         gchar *filename;
69
70         /* FIXME: We could actually load uris  */
71         filename = g_filename_from_uri (uri, NULL, error);
72         if (!filename)
73                 return FALSE;
74         
75         doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
76
77         if (!doc) return FALSE;
78
79         if (djvu_document->d_document)
80             ddjvu_document_release (djvu_document->d_document);
81
82         djvu_document->d_document = doc;
83
84         while (!ddjvu_document_decoding_done (djvu_document->d_document)) {
85                     ddjvu_message_wait (djvu_document->d_context);
86                     ddjvu_message_pop (djvu_document->d_context);       
87         }
88
89         return TRUE;
90 }
91
92
93 static gboolean
94 djvu_document_save (EvDocument  *document,
95                       const char  *uri,
96                       GError     **error)
97 {
98         g_warning ("djvu_document_save not implemented"); /* FIXME */
99         return TRUE;
100 }
101
102 static int
103 djvu_document_get_n_pages (EvDocument  *document)
104 {
105         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
106         
107         g_return_val_if_fail (djvu_document->d_document, 0);
108         
109         return ddjvu_document_get_pagenum (djvu_document->d_document);
110 }
111
112 static void
113 djvu_document_get_page_size (EvDocument   *document,
114                                int           page,
115                                double       *width,
116                                double       *height)
117 {
118         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
119
120         ddjvu_page_t *d_page;
121         
122         g_return_if_fail (djvu_document->d_document);
123         
124         d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, page);
125
126         while (!ddjvu_page_decoding_done (d_page)) {
127                     ddjvu_message_wait (djvu_document->d_context);
128                     ddjvu_message_pop (djvu_document->d_context);       
129         }
130
131         if (width)
132                 *width = ddjvu_page_get_width (d_page) * SCALE_FACTOR;
133         if (height)
134                 *height = ddjvu_page_get_height (d_page) * SCALE_FACTOR;
135         
136         ddjvu_page_release (d_page);
137 }
138
139 static GdkPixbuf *
140 djvu_document_render_pixbuf (EvDocument  *document, 
141                              int page, gdouble scale)
142 {
143         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
144         GdkPixbuf *pixbuf;
145         
146         ddjvu_rect_t rrect;
147         ddjvu_rect_t prect;
148         ddjvu_page_t *d_page;
149         
150         double page_width, page_height;
151
152         d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, page);
153         
154         while (!ddjvu_page_decoding_done (d_page)) {
155                     ddjvu_message_wait (djvu_document->d_context);
156                     ddjvu_message_pop (djvu_document->d_context);       
157         }
158         
159         page_width = ddjvu_page_get_width (d_page) * scale * SCALE_FACTOR;
160         page_height = ddjvu_page_get_height (d_page) * scale * SCALE_FACTOR;
161
162         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height);
163
164         prect.x = 0; prect.y = 0;
165         prect.w = page_width; prect.h = page_height;
166         rrect = prect;
167
168         ddjvu_page_render(d_page, DDJVU_RENDER_COLOR,
169                           &prect,
170                           &rrect,
171                           djvu_document->d_format,
172                           gdk_pixbuf_get_rowstride (pixbuf),
173                           (gchar *)gdk_pixbuf_get_pixels (pixbuf));
174         
175     
176         return pixbuf;
177 }
178
179 static void
180 djvu_document_finalize (GObject *object)
181 {
182         DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
183
184         if (djvu_document->d_document)
185             ddjvu_document_release (djvu_document->d_document);
186
187         ddjvu_context_release (djvu_document->d_context);
188         ddjvu_format_release (djvu_document->d_format);
189         
190         G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
191 }
192
193 static void
194 djvu_document_class_init (DjvuDocumentClass *klass)
195 {
196         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
197
198         gobject_class->finalize = djvu_document_finalize;
199 }
200
201 static gboolean
202 djvu_document_can_get_text (EvDocument *document)
203 {
204         return FALSE;
205 }
206
207 static EvDocumentInfo *
208 djvu_document_get_info (EvDocument *document)
209 {
210         EvDocumentInfo *info;
211
212         info = g_new0 (EvDocumentInfo, 1);
213
214         return info;
215 }
216
217 static void
218 djvu_document_document_iface_init (EvDocumentIface *iface)
219 {
220         iface->load = djvu_document_load;
221         iface->save = djvu_document_save;
222         iface->can_get_text = djvu_document_can_get_text;
223         iface->get_n_pages = djvu_document_get_n_pages;
224         iface->get_page_size = djvu_document_get_page_size;
225         iface->render_pixbuf = djvu_document_render_pixbuf;
226         iface->get_info = djvu_document_get_info;
227 }
228
229 static void
230 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
231                                            gint                  page,
232                                            gint                  suggested_width,
233                                            gint                  *width,
234                                            gint                  *height)
235 {
236         DjvuDocument *djvu_document = DJVU_DOCUMENT (document); 
237         gdouble p_width, p_height;
238         gdouble page_ratio;
239         
240         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), page, &p_width, &p_height);
241
242         page_ratio = p_height / p_width;
243         *width = suggested_width;
244         *height = (gint) (suggested_width * page_ratio);
245         
246         return;
247 }
248
249 static GdkPixbuf *
250 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
251                                           gint                   page,
252                                           gint                   width,
253                                           gboolean               border)
254 {
255         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
256         GdkPixbuf *pixbuf;
257         gint thumb_width, thumb_height;
258
259         guchar *pixels;
260         
261         g_return_val_if_fail (djvu_document->d_document, NULL);
262         
263         djvu_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
264         
265         if (border) {
266                 pixbuf = ev_document_misc_get_thumbnail_frame (thumb_width, thumb_height, NULL);
267                 pixels = gdk_pixbuf_get_pixels (pixbuf) + gdk_pixbuf_get_rowstride (pixbuf) + 4;
268         } else {
269                 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
270                                          thumb_width, thumb_height);
271                 gdk_pixbuf_fill (pixbuf, 0xffffffff);
272                 pixels = gdk_pixbuf_get_pixels (pixbuf);
273         }
274         
275         while (ddjvu_thumbnail_status (djvu_document->d_document, page, 1) < DDJVU_JOB_OK) {
276                     ddjvu_message_wait (djvu_document->d_context);
277                     ddjvu_message_pop (djvu_document->d_context);       
278         }
279                     
280         ddjvu_thumbnail_render (djvu_document->d_document, page, 
281                                 &thumb_width, &thumb_height,
282                                 djvu_document->d_format,
283                                 gdk_pixbuf_get_rowstride (pixbuf), 
284                                 (gchar *)pixels);
285         
286         return pixbuf;
287 }
288
289 static void
290 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
291 {
292         iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
293         iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
294 }
295
296 static void
297 djvu_document_init (DjvuDocument *djvu_document)
298 {
299         djvu_document->d_context = ddjvu_context_create ("Evince");
300         djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
301         ddjvu_format_set_row_order (djvu_document->d_format,1);
302         
303         djvu_document->d_document = NULL;
304 }
305