]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-document.c
f4caa7f0f36bdc8c594a0dfa5b3c4883468420da
[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       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init);
64       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, djvu_document_file_exporter_iface_init);
65       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init);
66       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init);
67       EV_BACKEND_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 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
265         rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, page_width);
266 #else
267         rowstride = page_width * 4;
268 #endif
269         pixels = (gchar *) g_malloc (page_height * rowstride);
270         surface = cairo_image_surface_create_for_data ((guchar *)pixels,
271                                                        CAIRO_FORMAT_RGB24,
272                                                        page_width,
273                                                        page_height,
274                                                        rowstride);
275         cairo_surface_set_user_data (surface, &key,
276                                      pixels, (cairo_destroy_func_t)g_free);
277         prect.x = 0;
278         prect.y = 0;
279         prect.w = page_width;
280         prect.h = page_height;
281         rrect = prect;
282
283         ddjvu_page_set_rotation (d_page, rotation);
284         
285         ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
286                            &prect,
287                            &rrect,
288                            djvu_document->d_format,
289                            rowstride,
290                            pixels);
291
292         return surface;
293 }
294
295 static void
296 djvu_document_finalize (GObject *object)
297 {
298         DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
299
300         if (djvu_document->search)
301             djvu_text_free (djvu_document->search);
302
303         if (djvu_document->d_document)
304             ddjvu_document_release (djvu_document->d_document);
305             
306         if (djvu_document->opts)
307             g_string_free (djvu_document->opts, TRUE);
308
309         if (djvu_document->ps_filename)
310             g_free (djvu_document->ps_filename);
311             
312         ddjvu_context_release (djvu_document->d_context);
313         ddjvu_format_release (djvu_document->d_format);
314         ddjvu_format_release (djvu_document->thumbs_format);
315         g_free (djvu_document->uri);
316         
317         G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
318 }
319
320 static void
321 djvu_document_class_init (DjvuDocumentClass *klass)
322 {
323         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
324
325         gobject_class->finalize = djvu_document_finalize;
326 }
327
328 static EvDocumentInfo *
329 djvu_document_get_info (EvDocument *document)
330 {
331         EvDocumentInfo *info;
332
333         info = g_new0 (EvDocumentInfo, 1);
334
335         return info;
336 }
337
338 static void
339 djvu_document_document_iface_init (EvDocumentIface *iface)
340 {
341         iface->load = djvu_document_load;
342         iface->save = djvu_document_save;
343         iface->get_n_pages = djvu_document_get_n_pages;
344         iface->get_page_size = djvu_document_get_page_size;
345         iface->render = djvu_document_render;
346         iface->get_info = djvu_document_get_info;
347 }
348
349 static gchar *
350 djvu_selection_get_selected_text (EvSelection     *selection,
351                                   EvRenderContext *rc,
352                                   EvSelectionStyle style,
353                                   EvRectangle     *points)
354 {
355         DjvuDocument *djvu_document = DJVU_DOCUMENT (selection);
356         double width, height;
357         EvRectangle rectangle;
358         gchar *text;
359              
360         djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
361                                      rc->page, &width, &height);                
362         rectangle.x1 = points->x1 / SCALE_FACTOR;
363         rectangle.y1 = (height - points->y2) / SCALE_FACTOR;
364         rectangle.x2 = points->x2 / SCALE_FACTOR;
365         rectangle.y2 = (height - points->y1) / SCALE_FACTOR;
366                 
367         text = djvu_text_copy (djvu_document, rc->page, &rectangle);
368       
369         if (text == NULL)
370                 text = g_strdup ("");
371                 
372         return text;
373 }
374
375 static void
376 djvu_selection_iface_init (EvSelectionIface *iface)
377 {
378         iface->get_selected_text = djvu_selection_get_selected_text;
379 }
380
381 static void
382 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
383                                          EvRenderContext      *rc, 
384                                          gint                 *width,
385                                          gint                 *height)
386 {
387         DjvuDocument *djvu_document = DJVU_DOCUMENT (document); 
388         gdouble page_width, page_height;
389         
390         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
391                                      &page_width, &page_height);
392
393         if (rc->rotation == 90 || rc->rotation == 270) {
394                 *width = (gint) (page_height * rc->scale);
395                 *height = (gint) (page_width * rc->scale);
396         } else {
397                 *width = (gint) (page_width * rc->scale);
398                 *height = (gint) (page_height * rc->scale);
399         }
400 }
401
402 static GdkPixbuf *
403 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
404                                         EvRenderContext      *rc,
405                                         gboolean              border)
406 {
407         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
408         GdkPixbuf *pixbuf, *rotated_pixbuf;
409         gdouble page_width, page_height;
410         gint thumb_width, thumb_height;
411         guchar *pixels;
412         
413         g_return_val_if_fail (djvu_document->d_document, NULL);
414
415         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
416                                      &page_width, &page_height);
417         
418         thumb_width = (gint) (page_width * rc->scale);
419         thumb_height = (gint) (page_height * rc->scale);
420
421         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
422                                  thumb_width, thumb_height);
423         gdk_pixbuf_fill (pixbuf, 0xffffffff);
424         pixels = gdk_pixbuf_get_pixels (pixbuf);
425         
426         while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK)
427                 djvu_handle_events(djvu_document, TRUE);
428                     
429         ddjvu_thumbnail_render (djvu_document->d_document, rc->page, 
430                                 &thumb_width, &thumb_height,
431                                 djvu_document->thumbs_format,
432                                 gdk_pixbuf_get_rowstride (pixbuf), 
433                                 (gchar *)pixels);
434
435         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
436         g_object_unref (pixbuf);
437
438         if (border) {
439               GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
440               
441               rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
442               g_object_unref (tmp_pixbuf);
443         }
444         
445         return rotated_pixbuf;
446 }
447
448 static void
449 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
450 {
451         iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
452         iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
453 }
454
455 /* EvFileExporterIface */
456 static void
457 djvu_document_file_exporter_begin (EvFileExporter        *exporter,
458                                    EvFileExporterContext *fc)
459 {
460         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
461         
462         if (djvu_document->ps_filename)
463                 g_free (djvu_document->ps_filename);    
464         djvu_document->ps_filename = g_strdup (fc->filename);
465
466         g_string_assign (djvu_document->opts, "-page=");
467 }
468
469 static void
470 djvu_document_file_exporter_do_page (EvFileExporter  *exporter,
471                                      EvRenderContext *rc)
472 {
473         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
474         
475         g_string_append_printf (djvu_document->opts, "%d,", (rc->page) + 1); 
476 }
477
478 static void
479 djvu_document_file_exporter_end (EvFileExporter *exporter)
480 {
481         int d_optc = 1; 
482         const char *d_optv[d_optc];
483
484         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
485
486         FILE *fn = fopen (djvu_document->ps_filename, "w");
487         if (fn == NULL) {
488                 g_warning ("Cannot open file ā€œ%sā€.", djvu_document->ps_filename);
489                 return;
490         }
491         
492         d_optv[0] = djvu_document->opts->str; 
493
494         ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
495         while (!ddjvu_job_done(job)) {  
496                 djvu_handle_events (djvu_document, TRUE);
497         }
498
499         fclose(fn); 
500 }
501
502 static EvFileExporterCapabilities
503 djvu_document_file_exporter_get_capabilities (EvFileExporter *exporter)
504 {
505         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
506                 EV_FILE_EXPORTER_CAN_COPIES |
507                 EV_FILE_EXPORTER_CAN_COLLATE |
508                 EV_FILE_EXPORTER_CAN_REVERSE |
509                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
510 }
511
512 static void
513 djvu_document_file_exporter_iface_init (EvFileExporterIface *iface)
514 {
515         iface->begin = djvu_document_file_exporter_begin;
516         iface->do_page = djvu_document_file_exporter_do_page;
517         iface->end = djvu_document_file_exporter_end;
518         iface->get_capabilities = djvu_document_file_exporter_get_capabilities;
519 }
520
521 static void
522 djvu_document_init (DjvuDocument *djvu_document)
523 {
524         guint masks[4] = { 0xff0000, 0xff00, 0xff, 0xff000000 };
525         
526         djvu_document->d_context = ddjvu_context_create ("Evince");
527         djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 4, masks);
528         ddjvu_format_set_row_order (djvu_document->d_format, 1);
529
530         djvu_document->thumbs_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
531         ddjvu_format_set_row_order (djvu_document->thumbs_format, 1);
532
533         djvu_document->ps_filename = NULL;
534         djvu_document->opts = g_string_new ("");
535         
536         djvu_document->d_document = NULL;
537 }
538
539 static void
540 djvu_document_find_begin (EvDocumentFind   *document,
541                           int               page,
542                           const char       *search_string,
543                           gboolean          case_sensitive)
544 {
545         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
546
547         if (djvu_document->search && 
548             strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0)
549                 return;
550
551         if (djvu_document->search)
552                 djvu_text_free (djvu_document->search);
553
554         djvu_document->search = djvu_text_new (djvu_document,
555                                                           page,
556                                                           case_sensitive,
557                                                           search_string);
558 }
559
560 static int
561 djvu_document_find_get_n_results (EvDocumentFind *document_find, int page)
562 {
563         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
564
565         if (search) {
566                 return djvu_text_n_results (search, page);
567         } else {
568                 return 0;
569         }
570 }
571
572 static gboolean
573 djvu_document_find_get_result (EvDocumentFind *document_find,
574                                int             page,
575                                int             n_result,
576                                EvRectangle    *rectangle)
577 {
578         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find);
579         DjvuText *search = djvu_document->search;
580         EvRectangle *r;
581         double width, height;
582
583         if (search == NULL)
584                 return FALSE;
585
586         r = djvu_text_get_result (search, page, n_result);
587         if (r == NULL)
588                 return FALSE;
589
590         djvu_document_get_page_size (EV_DOCUMENT (djvu_document), 
591                 page, &width, &height);
592         rectangle->x1 = r->x1 * SCALE_FACTOR;
593         rectangle->y1 = height - r->y2 * SCALE_FACTOR;
594         rectangle->x2 = r->x2 * SCALE_FACTOR;
595         rectangle->y2 = height - r->y1 * SCALE_FACTOR;
596                 
597         return TRUE;
598 }
599
600 static int
601 djvu_document_find_page_has_results (EvDocumentFind *document_find,
602                                     int             page)
603 {
604         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
605
606         return search && djvu_text_has_results (search, page);
607 }
608
609 static double
610 djvu_document_find_get_progress (EvDocumentFind *document_find)
611 {
612         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
613         
614         if (search == NULL) {
615                 return 0;
616         }
617
618         return djvu_text_get_progress (search);
619 }
620
621 static void
622 djvu_document_find_cancel (EvDocumentFind *document)
623 {
624         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
625
626         if (djvu_document->search) {
627                 djvu_text_free (djvu_document->search);
628                 djvu_document->search = NULL;
629         }
630 }
631
632 static void
633 djvu_document_find_iface_init (EvDocumentFindIface *iface)
634 {
635         iface->begin = djvu_document_find_begin;
636         iface->get_n_results = djvu_document_find_get_n_results;
637         iface->get_result = djvu_document_find_get_result;
638         iface->page_has_results = djvu_document_find_page_has_results;
639         iface->get_progress = djvu_document_find_get_progress;
640         iface->cancel = djvu_document_find_cancel;
641 }
642
643 static GList *
644 djvu_document_links_get_links (EvDocumentLinks *document_links,
645                                gint             page)
646 {
647         return djvu_links_get_links (document_links, page, SCALE_FACTOR);
648 }
649
650 static void
651 djvu_document_document_links_iface_init  (EvDocumentLinksIface *iface)
652 {
653         iface->has_document_links = djvu_links_has_document_links;
654         iface->get_links_model = djvu_links_get_links_model;
655         iface->get_links = djvu_document_links_get_links;
656         iface->find_link_dest = djvu_links_find_link_dest;
657 }