]> www.fi.muni.cz Git - evince.git/blob - djvu/djvu-document.c
Hungarian translation updated.
[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         ddjvu_pageinfo_t info;
120         
121         g_return_if_fail (djvu_document->d_document);
122         
123         while (ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info) < DDJVU_JOB_OK) {
124                     ddjvu_message_wait (djvu_document->d_context);
125                     ddjvu_message_pop (djvu_document->d_context);       
126         }
127
128         if (width)
129                 *width = info.width * SCALE_FACTOR;
130         if (height)
131                 *height = info.height * SCALE_FACTOR;
132 }
133
134 static GdkPixbuf *
135 djvu_document_render_pixbuf (EvDocument  *document, 
136                              int page, gdouble scale)
137 {
138         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
139         GdkPixbuf *pixbuf;
140         
141         ddjvu_rect_t rrect;
142         ddjvu_rect_t prect;
143         ddjvu_page_t *d_page;
144         
145         double page_width, page_height;
146
147         d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, page);
148         
149         while (!ddjvu_page_decoding_done (d_page)) {
150                     ddjvu_message_wait (djvu_document->d_context);
151                     ddjvu_message_pop (djvu_document->d_context);       
152         }
153         
154         page_width = ddjvu_page_get_width (d_page) * scale * SCALE_FACTOR;
155         page_height = ddjvu_page_get_height (d_page) * scale * SCALE_FACTOR;
156
157         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height);
158
159         prect.x = 0; prect.y = 0;
160         prect.w = page_width; prect.h = page_height;
161         rrect = prect;
162
163         ddjvu_page_render(d_page, DDJVU_RENDER_COLOR,
164                           &prect,
165                           &rrect,
166                           djvu_document->d_format,
167                           gdk_pixbuf_get_rowstride (pixbuf),
168                           (gchar *)gdk_pixbuf_get_pixels (pixbuf));
169         
170     
171         return pixbuf;
172 }
173
174 static void
175 djvu_document_finalize (GObject *object)
176 {
177         DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
178
179         if (djvu_document->d_document)
180             ddjvu_document_release (djvu_document->d_document);
181
182         ddjvu_context_release (djvu_document->d_context);
183         ddjvu_format_release (djvu_document->d_format);
184         
185         G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
186 }
187
188 static void
189 djvu_document_class_init (DjvuDocumentClass *klass)
190 {
191         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
192
193         gobject_class->finalize = djvu_document_finalize;
194 }
195
196 static gboolean
197 djvu_document_can_get_text (EvDocument *document)
198 {
199         return FALSE;
200 }
201
202 static EvDocumentInfo *
203 djvu_document_get_info (EvDocument *document)
204 {
205         EvDocumentInfo *info;
206
207         info = g_new0 (EvDocumentInfo, 1);
208
209         return info;
210 }
211
212 static void
213 djvu_document_document_iface_init (EvDocumentIface *iface)
214 {
215         iface->load = djvu_document_load;
216         iface->save = djvu_document_save;
217         iface->can_get_text = djvu_document_can_get_text;
218         iface->get_n_pages = djvu_document_get_n_pages;
219         iface->get_page_size = djvu_document_get_page_size;
220         iface->render_pixbuf = djvu_document_render_pixbuf;
221         iface->get_info = djvu_document_get_info;
222 }
223
224 static void
225 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
226                                            gint                  page,
227                                            gint                  suggested_width,
228                                            gint                  *width,
229                                            gint                  *height)
230 {
231         DjvuDocument *djvu_document = DJVU_DOCUMENT (document); 
232         gdouble p_width, p_height;
233         gdouble page_ratio;
234         
235         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), page, &p_width, &p_height);
236
237         page_ratio = p_height / p_width;
238         *width = suggested_width;
239         *height = (gint) (suggested_width * page_ratio);
240         
241         return;
242 }
243
244 static GdkPixbuf *
245 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails   *document,
246                                           gint                   page,
247                                           gint                   width,
248                                           gboolean               border)
249 {
250         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
251         GdkPixbuf *pixbuf;
252         gint thumb_width, thumb_height;
253
254         guchar *pixels;
255         
256         g_return_val_if_fail (djvu_document->d_document, NULL);
257         
258         djvu_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height);
259         
260         if (border) {
261                 pixbuf = ev_document_misc_get_thumbnail_frame (thumb_width, thumb_height, NULL);
262                 pixels = gdk_pixbuf_get_pixels (pixbuf) + gdk_pixbuf_get_rowstride (pixbuf) + 4;
263         } else {
264                 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
265                                          thumb_width, thumb_height);
266                 gdk_pixbuf_fill (pixbuf, 0xffffffff);
267                 pixels = gdk_pixbuf_get_pixels (pixbuf);
268         }
269         
270         while (ddjvu_thumbnail_status (djvu_document->d_document, page, 1) < DDJVU_JOB_OK) {
271                     ddjvu_message_wait (djvu_document->d_context);
272                     ddjvu_message_pop (djvu_document->d_context);       
273         }
274                     
275         ddjvu_thumbnail_render (djvu_document->d_document, page, 
276                                 &thumb_width, &thumb_height,
277                                 djvu_document->d_format,
278                                 gdk_pixbuf_get_rowstride (pixbuf), 
279                                 (gchar *)pixels);
280         
281         return pixbuf;
282 }
283
284 static void
285 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
286 {
287         iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
288         iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
289 }
290
291 static void
292 djvu_document_init (DjvuDocument *djvu_document)
293 {
294         djvu_document->d_context = ddjvu_context_create ("Evince");
295         djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
296         ddjvu_format_set_row_order (djvu_document->d_format,1);
297         
298         djvu_document->d_document = NULL;
299 }
300