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