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