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