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