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