]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-document.c
e16ec6ab78f1694ecbb5ecd20f84b28bf1ace48d
[evince.git] / backend / 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 "config.h"
21
22 #include "djvu-document.h"
23 #include "djvu-text.h"
24 #include "djvu-links.h"
25 #include "djvu-document-private.h"
26 #include "ev-document-thumbnails.h"
27 #include "ev-file-exporter.h"
28 #include "ev-document-misc.h"
29 #include "ev-document-find.h"
30 #include "ev-document-links.h"
31 #include "ev-selection.h"
32 #include "ev-file-helpers.h"
33
34 #include <gdk-pixbuf/gdk-pixbuf-core.h>
35 #include <glib/gi18n.h>
36 #include <glib/gunicode.h>
37 #include <string.h>
38
39 #define SCALE_FACTOR 0.2
40
41 enum {
42         PROP_0,
43         PROP_TITLE
44 };
45
46 struct _DjvuDocumentClass
47 {
48         GObjectClass parent_class;
49 };
50
51 typedef struct _DjvuDocumentClass DjvuDocumentClass;
52
53 static void djvu_document_document_iface_init (EvDocumentIface *iface);
54 static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
55 static void djvu_document_file_exporter_iface_init (EvFileExporterIface *iface);
56 static void djvu_document_find_iface_init (EvDocumentFindIface *iface);
57 static void djvu_document_document_links_iface_init  (EvDocumentLinksIface *iface);
58 static void djvu_selection_iface_init (EvSelectionIface *iface);
59
60 EV_BACKEND_REGISTER_WITH_CODE (DjvuDocument, djvu_document,
61     {
62       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init);
63       G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, djvu_document_file_exporter_iface_init);
64       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init);
65       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init);
66       G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION, djvu_selection_iface_init);
67      });
68
69
70 void 
71 djvu_handle_events (DjvuDocument *djvu_document, int wait)
72 {
73         ddjvu_context_t *ctx = djvu_document->d_context;
74         const ddjvu_message_t *msg;
75         
76         if (!ctx)
77                 return;
78
79         if (wait)
80                 msg = ddjvu_message_wait (ctx);
81
82         while ((msg = ddjvu_message_peek (ctx))) {
83                 switch (msg->m_any.tag) {
84                         case DDJVU_ERROR:
85                                 g_warning ("DjvuLibre error: %s", 
86                                            msg->m_error.message);
87                                 if (msg->m_error.filename)
88                                         g_warning ("DjvuLibre error: %s:%d", 
89                                                    msg->m_error.filename,
90                                                    msg->m_error.lineno);
91                                 break;
92                         default:
93                                 break;
94                 }
95                 ddjvu_message_pop (ctx);
96         }
97 }
98
99 static gboolean
100 djvu_document_load (EvDocument  *document,
101                     const char  *uri,
102                     GError     **error)
103 {
104         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
105         ddjvu_document_t *doc;
106         gchar *filename;
107         gboolean missing_files = FALSE;
108
109         /* FIXME: We could actually load uris  */
110         filename = g_filename_from_uri (uri, NULL, error);
111         if (!filename)
112                 return FALSE;
113         
114         doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
115
116         if (!doc) {
117                 g_free (filename);
118                 return FALSE;
119         }
120
121         if (djvu_document->d_document)
122             ddjvu_document_release (djvu_document->d_document);
123
124         djvu_document->d_document = doc;
125
126         while (!ddjvu_document_decoding_done (djvu_document->d_document)) 
127                 djvu_handle_events (djvu_document, TRUE);
128         g_free (djvu_document->uri);
129         djvu_document->uri = g_strdup (uri);
130
131         if (ddjvu_document_get_type (djvu_document->d_document) == DDJVU_DOCTYPE_INDIRECT) {
132                 gint n_files;
133                 gint i;
134                 gchar *base;
135
136                 base = g_path_get_dirname (filename);
137
138                 n_files = ddjvu_document_get_filenum (djvu_document->d_document);
139                 for (i = 0; i < n_files; i++) {
140                         struct ddjvu_fileinfo_s fileinfo;
141                         gchar *file;
142                         
143                         ddjvu_document_get_fileinfo (djvu_document->d_document,
144                                                      i, &fileinfo);
145
146                         if (fileinfo.type != 'P')
147                                 continue;
148
149                         file = g_build_filename (base, fileinfo.id, NULL);
150                         if (!g_file_test (file, G_FILE_TEST_EXISTS)) {
151                                 missing_files = TRUE;
152                                 g_free (file);
153                                 
154                                 break;
155                         }
156                         g_free (file);
157                 }
158                 g_free (base);
159         }
160         g_free (filename);
161
162         if (missing_files) {
163                 g_set_error (error,
164                              G_FILE_ERROR,
165                              G_FILE_ERROR_EXIST,
166                              _("The document is composed by several files. "
167                                "One or more of such files cannot be accessed."));
168
169                 return FALSE;
170         }
171
172         return TRUE;
173 }
174
175
176 static gboolean
177 djvu_document_save (EvDocument  *document,
178                     const char  *uri,
179                     GError     **error)
180 {
181         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
182
183         return ev_xfer_uri_simple (djvu_document->uri, uri, error);
184 }
185
186 int
187 djvu_document_get_n_pages (EvDocument  *document)
188 {
189         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
190         
191         g_return_val_if_fail (djvu_document->d_document, 0);
192         
193         return ddjvu_document_get_pagenum (djvu_document->d_document);
194 }
195
196 static void
197 djvu_document_get_page_size (EvDocument   *document,
198                              int           page,
199                              double       *width,
200                              double       *height)
201 {
202         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
203         ddjvu_pageinfo_t info;
204         ddjvu_status_t r;
205         
206         g_return_if_fail (djvu_document->d_document);
207
208         while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
209                 djvu_handle_events(djvu_document, TRUE);
210         
211         if (r >= DDJVU_JOB_FAILED)
212                 djvu_handle_events(djvu_document, TRUE);
213
214         *width = info.width * SCALE_FACTOR; 
215         *height = info.height * SCALE_FACTOR;
216 }
217
218 static cairo_surface_t *
219 djvu_document_render (EvDocument      *document, 
220                       EvRenderContext *rc)
221 {
222         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
223         cairo_surface_t *surface;
224         gchar *pixels;
225         gint   rowstride;
226         ddjvu_rect_t rrect;
227         ddjvu_rect_t prect;
228         ddjvu_page_t *d_page;
229         ddjvu_page_rotation_t rotation;
230         double page_width, page_height, tmp;
231         static const cairo_user_data_key_t key;
232
233         d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page);
234         
235         while (!ddjvu_page_decoding_done (d_page))
236                 djvu_handle_events(djvu_document, TRUE);
237
238         page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
239         page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
240         
241         switch (rc->rotation) {
242                 case 90:
243                         rotation = DDJVU_ROTATE_90;
244                         tmp = page_height;
245                         page_height = page_width;
246                         page_width = tmp;
247                         
248                         break;
249                 case 180:
250                         rotation = DDJVU_ROTATE_180;
251                         
252                         break;
253                 case 270:
254                         rotation = DDJVU_ROTATE_270;
255                         tmp = page_height;
256                         page_height = page_width;
257                         page_width = tmp;
258                         
259                         break;
260                 default:
261                         rotation = DDJVU_ROTATE_0;
262         }
263
264         rowstride = page_width * 4;
265         pixels = (gchar *) g_malloc (page_height * rowstride);
266         surface = cairo_image_surface_create_for_data ((guchar *)pixels,
267                                                        CAIRO_FORMAT_RGB24,
268                                                        page_width,
269                                                        page_height,
270                                                        rowstride);
271         cairo_surface_set_user_data (surface, &key,
272                                      pixels, (cairo_destroy_func_t)g_free);
273         prect.x = 0;
274         prect.y = 0;
275         prect.w = page_width;
276         prect.h = page_height;
277         rrect = prect;
278
279         ddjvu_page_set_rotation (d_page, rotation);
280         
281         ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
282                            &prect,
283                            &rrect,
284                            djvu_document->d_format,
285                            rowstride,
286                            pixels);
287
288         return surface;
289 }
290
291 static void
292 djvu_document_finalize (GObject *object)
293 {
294         DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
295
296         if (djvu_document->search)
297             djvu_text_free (djvu_document->search);
298
299         if (djvu_document->d_document)
300             ddjvu_document_release (djvu_document->d_document);
301             
302         if (djvu_document->opts)
303             g_string_free (djvu_document->opts, TRUE);
304
305         if (djvu_document->ps_filename)
306             g_free (djvu_document->ps_filename);
307             
308         ddjvu_context_release (djvu_document->d_context);
309         ddjvu_format_release (djvu_document->d_format);
310         ddjvu_format_release (djvu_document->thumbs_format);
311         g_free (djvu_document->uri);
312         
313         G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
314 }
315
316 static void
317 djvu_document_class_init (DjvuDocumentClass *klass)
318 {
319         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
320
321         gobject_class->finalize = djvu_document_finalize;
322 }
323
324 static EvDocumentInfo *
325 djvu_document_get_info (EvDocument *document)
326 {
327         EvDocumentInfo *info;
328
329         info = g_new0 (EvDocumentInfo, 1);
330
331         return info;
332 }
333
334 static void
335 djvu_document_document_iface_init (EvDocumentIface *iface)
336 {
337         iface->load = djvu_document_load;
338         iface->save = djvu_document_save;
339         iface->get_n_pages = djvu_document_get_n_pages;
340         iface->get_page_size = djvu_document_get_page_size;
341         iface->render = djvu_document_render;
342         iface->get_info = djvu_document_get_info;
343 }
344
345 static gchar *
346 djvu_selection_get_selected_text (EvSelection     *selection,
347                                   EvRenderContext *rc,
348                                   EvSelectionStyle style,
349                                   EvRectangle     *points)
350 {
351         DjvuDocument *djvu_document = DJVU_DOCUMENT (selection);
352         double width, height;
353         EvRectangle rectangle;
354         gchar *text;
355              
356         djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
357                                      rc->page, &width, &height);                
358         rectangle.x1 = points->x1 / SCALE_FACTOR;
359         rectangle.y1 = (height - points->y2) / SCALE_FACTOR;
360         rectangle.x2 = points->x2 / SCALE_FACTOR;
361         rectangle.y2 = (height - points->y1) / SCALE_FACTOR;
362                 
363         text = djvu_text_copy (djvu_document, rc->page, &rectangle);
364       
365         if (text == NULL)
366                 text = g_strdup ("");
367                 
368         return text;
369 }
370
371 static void
372 djvu_selection_iface_init (EvSelectionIface *iface)
373 {
374         iface->get_selected_text = djvu_selection_get_selected_text;
375 }
376
377 static void
378 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
379                                          EvRenderContext      *rc, 
380                                          gint                 *width,
381                                          gint                 *height)
382 {
383         DjvuDocument *djvu_document = DJVU_DOCUMENT (document); 
384         gdouble page_width, page_height;
385         
386         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
387                                      &page_width, &page_height);
388
389         if (rc->rotation == 90 || rc->rotation == 270) {
390                 *width = (gint) (page_height * rc->scale);
391                 *height = (gint) (page_width * rc->scale);
392         } else {
393                 *width = (gint) (page_width * rc->scale);
394                 *height = (gint) (page_height * rc->scale);
395         }
396 }
397
398 static GdkPixbuf *
399 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
400                                         EvRenderContext      *rc,
401                                         gboolean              border)
402 {
403         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
404         GdkPixbuf *pixbuf, *rotated_pixbuf;
405         gdouble page_width, page_height;
406         gint thumb_width, thumb_height;
407         guchar *pixels;
408         
409         g_return_val_if_fail (djvu_document->d_document, NULL);
410
411         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
412                                      &page_width, &page_height);
413         
414         thumb_width = (gint) (page_width * rc->scale);
415         thumb_height = (gint) (page_height * rc->scale);
416
417         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
418                                  thumb_width, thumb_height);
419         gdk_pixbuf_fill (pixbuf, 0xffffffff);
420         pixels = gdk_pixbuf_get_pixels (pixbuf);
421         
422         while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK)
423                 djvu_handle_events(djvu_document, TRUE);
424                     
425         ddjvu_thumbnail_render (djvu_document->d_document, rc->page, 
426                                 &thumb_width, &thumb_height,
427                                 djvu_document->thumbs_format,
428                                 gdk_pixbuf_get_rowstride (pixbuf), 
429                                 (gchar *)pixels);
430
431         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
432         g_object_unref (pixbuf);
433
434         if (border) {
435               GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
436               
437               rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
438               g_object_unref (tmp_pixbuf);
439         }
440         
441         return rotated_pixbuf;
442 }
443
444 static void
445 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
446 {
447         iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
448         iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
449 }
450
451 /* EvFileExporterIface */
452 static void
453 djvu_document_file_exporter_begin (EvFileExporter        *exporter,
454                                    EvFileExporterContext *fc)
455 {
456         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
457         
458         if (djvu_document->ps_filename)
459                 g_free (djvu_document->ps_filename);    
460         djvu_document->ps_filename = g_strdup (fc->filename);
461
462         g_string_assign (djvu_document->opts, "-page=");
463 }
464
465 static void
466 djvu_document_file_exporter_do_page (EvFileExporter  *exporter,
467                                      EvRenderContext *rc)
468 {
469         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
470         
471         g_string_append_printf (djvu_document->opts, "%d,", (rc->page) + 1); 
472 }
473
474 static void
475 djvu_document_file_exporter_end (EvFileExporter *exporter)
476 {
477         int d_optc = 1; 
478         const char *d_optv[d_optc];
479
480         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
481
482         FILE *fn = fopen (djvu_document->ps_filename, "w");
483         if (fn == NULL) {
484                 g_warning ("Cannot open file ā€œ%sā€.", djvu_document->ps_filename);
485                 return;
486         }
487         
488         d_optv[0] = djvu_document->opts->str; 
489
490         ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
491         while (!ddjvu_job_done(job)) {  
492                 djvu_handle_events (djvu_document, TRUE);
493         }
494
495         fclose(fn); 
496 }
497
498 static EvFileExporterCapabilities
499 djvu_document_file_exporter_get_capabilities (EvFileExporter *exporter)
500 {
501         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
502                 EV_FILE_EXPORTER_CAN_COPIES |
503                 EV_FILE_EXPORTER_CAN_COLLATE |
504                 EV_FILE_EXPORTER_CAN_REVERSE |
505                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
506 }
507
508 static void
509 djvu_document_file_exporter_iface_init (EvFileExporterIface *iface)
510 {
511         iface->begin = djvu_document_file_exporter_begin;
512         iface->do_page = djvu_document_file_exporter_do_page;
513         iface->end = djvu_document_file_exporter_end;
514         iface->get_capabilities = djvu_document_file_exporter_get_capabilities;
515 }
516
517 static void
518 djvu_document_init (DjvuDocument *djvu_document)
519 {
520         guint masks[4] = { 0xff0000, 0xff00, 0xff, 0xff000000 };
521         
522         djvu_document->d_context = ddjvu_context_create ("Evince");
523         djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 4, masks);
524         ddjvu_format_set_row_order (djvu_document->d_format, 1);
525
526         djvu_document->thumbs_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
527         ddjvu_format_set_row_order (djvu_document->thumbs_format, 1);
528
529         djvu_document->ps_filename = NULL;
530         djvu_document->opts = g_string_new ("");
531         
532         djvu_document->d_document = NULL;
533 }
534
535 static void
536 djvu_document_find_begin (EvDocumentFind   *document,
537                           int               page,
538                           const char       *search_string,
539                           gboolean          case_sensitive)
540 {
541         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
542
543         if (djvu_document->search && 
544             strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0)
545                 return;
546
547         if (djvu_document->search)
548                 djvu_text_free (djvu_document->search);
549
550         djvu_document->search = djvu_text_new (djvu_document,
551                                                           page,
552                                                           case_sensitive,
553                                                           search_string);
554 }
555
556 static int
557 djvu_document_find_get_n_results (EvDocumentFind *document_find, int page)
558 {
559         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
560
561         if (search) {
562                 return djvu_text_n_results (search, page);
563         } else {
564                 return 0;
565         }
566 }
567
568 static gboolean
569 djvu_document_find_get_result (EvDocumentFind *document_find,
570                                int             page,
571                                int             n_result,
572                                EvRectangle    *rectangle)
573 {
574         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find);
575         DjvuText *search = djvu_document->search;
576         EvRectangle *r;
577         double width, height;
578
579         if (search == NULL)
580                 return FALSE;
581
582         r = djvu_text_get_result (search, page, n_result);
583         if (r == NULL)
584                 return FALSE;
585
586         djvu_document_get_page_size (EV_DOCUMENT (djvu_document), 
587                 page, &width, &height);
588         rectangle->x1 = r->x1 * SCALE_FACTOR;
589         rectangle->y1 = height - r->y2 * SCALE_FACTOR;
590         rectangle->x2 = r->x2 * SCALE_FACTOR;
591         rectangle->y2 = height - r->y1 * SCALE_FACTOR;
592                 
593         return TRUE;
594 }
595
596 static int
597 djvu_document_find_page_has_results (EvDocumentFind *document_find,
598                                     int             page)
599 {
600         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
601
602         return search && djvu_text_has_results (search, page);
603 }
604
605 static double
606 djvu_document_find_get_progress (EvDocumentFind *document_find)
607 {
608         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
609         
610         if (search == NULL) {
611                 return 0;
612         }
613
614         return djvu_text_get_progress (search);
615 }
616
617 static void
618 djvu_document_find_cancel (EvDocumentFind *document)
619 {
620         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
621
622         if (djvu_document->search) {
623                 djvu_text_free (djvu_document->search);
624                 djvu_document->search = NULL;
625         }
626 }
627
628 static void
629 djvu_document_find_iface_init (EvDocumentFindIface *iface)
630 {
631         iface->begin = djvu_document_find_begin;
632         iface->get_n_results = djvu_document_find_get_n_results;
633         iface->get_result = djvu_document_find_get_result;
634         iface->page_has_results = djvu_document_find_page_has_results;
635         iface->get_progress = djvu_document_find_get_progress;
636         iface->cancel = djvu_document_find_cancel;
637 }
638
639 static GList *
640 djvu_document_links_get_links (EvDocumentLinks *document_links,
641                                gint             page)
642 {
643         return djvu_links_get_links (document_links, page, SCALE_FACTOR);
644 }
645
646 static void
647 djvu_document_document_links_iface_init  (EvDocumentLinksIface *iface)
648 {
649         iface->has_document_links = djvu_links_has_document_links;
650         iface->get_links_model = djvu_links_get_links_model;
651         iface->get_links = djvu_document_links_get_links;
652         iface->find_link_dest = djvu_links_find_link_dest;
653 }