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