]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-document.c
Use cairo image surfaces instead of GDK pixbufs for drawing pages and
[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, *rotated_surface;
220         gchar *pixels;
221         gint   rowstride;
222         ddjvu_rect_t rrect;
223         ddjvu_rect_t prect;
224         ddjvu_page_t *d_page;
225         double page_width, page_height;
226         static const cairo_user_data_key_t key;
227
228         d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page);
229         
230         while (!ddjvu_page_decoding_done (d_page))
231                 djvu_handle_events(djvu_document, TRUE);
232
233         page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
234         page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
235
236         rowstride = page_width * 4;
237         pixels = (gchar *) g_malloc (page_height * rowstride);
238         surface = cairo_image_surface_create_for_data (pixels,
239                                                        CAIRO_FORMAT_ARGB32,
240                                                        page_width,
241                                                        page_height,
242                                                        rowstride);
243         cairo_surface_set_user_data (surface, &key,
244                                      pixels, (cairo_destroy_func_t)g_free);
245         prect.x = 0;
246         prect.y = 0;
247         prect.w = page_width;
248         prect.h = page_height;
249         rrect = prect;
250
251         ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
252                            &prect,
253                            &rrect,
254                            djvu_document->d_format,
255                            rowstride,
256                            pixels);
257
258         rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
259                                                                      page_width,
260                                                                      page_height,
261                                                                      rc->rotation);
262         cairo_surface_destroy (surface);
263
264         return rotated_surface;
265 }
266
267 static void
268 djvu_document_finalize (GObject *object)
269 {
270         DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
271
272         if (djvu_document->d_document)
273             ddjvu_document_release (djvu_document->d_document);
274             
275         if (djvu_document->opts)
276             g_string_free (djvu_document->opts, TRUE);
277
278         if (djvu_document->ps_filename)
279             g_free (djvu_document->ps_filename);
280             
281         ddjvu_context_release (djvu_document->d_context);
282         ddjvu_format_release (djvu_document->d_format);
283         ddjvu_format_release (djvu_document->thumbs_format);
284         g_free (djvu_document->uri);
285         
286         G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
287 }
288
289 static void
290 djvu_document_class_init (DjvuDocumentClass *klass)
291 {
292         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
293
294         gobject_class->finalize = djvu_document_finalize;
295 }
296
297 static gboolean
298 djvu_document_can_get_text (EvDocument *document)
299 {
300         return TRUE;
301 }
302
303
304 static char *
305 djvu_document_get_text (EvDocument *document, int page, EvRectangle *rect)
306 {
307         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
308         double width, height;
309         EvRectangle rectangle;
310         char* text;
311              
312         djvu_document_get_page_size (document, page, &width, &height);          
313         rectangle.x1 = rect->x1 / SCALE_FACTOR;
314         rectangle.y1 = (height - rect->y2) / SCALE_FACTOR;
315         rectangle.x2 = rect->x2 / SCALE_FACTOR;
316         rectangle.y2 = (height - rect->y1) / SCALE_FACTOR;
317                 
318         text = djvu_text_copy (djvu_document, page, &rectangle);
319       
320         if (text == NULL)
321                 text = g_strdup ("");
322                 
323         return text;
324 }
325
326 static EvDocumentInfo *
327 djvu_document_get_info (EvDocument *document)
328 {
329         EvDocumentInfo *info;
330
331         info = g_new0 (EvDocumentInfo, 1);
332
333         return info;
334 }
335
336 static void
337 djvu_document_document_iface_init (EvDocumentIface *iface)
338 {
339         iface->load = djvu_document_load;
340         iface->save = djvu_document_save;
341         iface->can_get_text = djvu_document_can_get_text;
342         iface->get_text = djvu_document_get_text;
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 void
350 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
351                                          EvRenderContext      *rc, 
352                                          gint                 *width,
353                                          gint                 *height)
354 {
355         DjvuDocument *djvu_document = DJVU_DOCUMENT (document); 
356         gdouble page_width, page_height;
357         
358         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
359                                      &page_width, &page_height);
360
361         if (rc->rotation == 90 || rc->rotation == 270) {
362                 *width = (gint) (page_height * rc->scale);
363                 *height = (gint) (page_width * rc->scale);
364         } else {
365                 *width = (gint) (page_width * rc->scale);
366                 *height = (gint) (page_height * rc->scale);
367         }
368 }
369
370 static GdkPixbuf *
371 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
372                                         EvRenderContext      *rc,
373                                         gboolean              border)
374 {
375         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
376         GdkPixbuf *pixbuf, *rotated_pixbuf;
377         gdouble page_width, page_height;
378         gint thumb_width, thumb_height;
379         guchar *pixels;
380         
381         g_return_val_if_fail (djvu_document->d_document, NULL);
382
383         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
384                                      &page_width, &page_height);
385         
386         thumb_width = (gint) (page_width * rc->scale);
387         thumb_height = (gint) (page_height * rc->scale);
388
389         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
390                                  thumb_width, thumb_height);
391         gdk_pixbuf_fill (pixbuf, 0xffffffff);
392         pixels = gdk_pixbuf_get_pixels (pixbuf);
393         
394         while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK)
395                 djvu_handle_events(djvu_document, TRUE);
396                     
397         ddjvu_thumbnail_render (djvu_document->d_document, rc->page, 
398                                 &thumb_width, &thumb_height,
399                                 djvu_document->thumbs_format,
400                                 gdk_pixbuf_get_rowstride (pixbuf), 
401                                 (gchar *)pixels);
402
403         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
404         g_object_unref (pixbuf);
405
406         if (border) {
407               GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
408               
409               rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
410               g_object_unref (tmp_pixbuf);
411         }
412         
413         return rotated_pixbuf;
414 }
415
416 static void
417 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
418 {
419         iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
420         iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
421 }
422
423 /* EvFileExporterIface */
424 static gboolean
425 djvu_document_file_exporter_format_supported (EvFileExporter      *exporter,
426                                               EvFileExporterFormat format)
427 {
428         return (format == EV_FILE_FORMAT_PS); // only exporting to PS is implemented.
429 }
430
431 static void
432 djvu_document_file_exporter_begin (EvFileExporter      *exporter,
433                                    EvFileExporterFormat format,
434                                    const char          *filename, /* for storing the temp ps file */
435                                    int                  first_page,
436                                    int                  last_page,
437                                    double               width,
438                                    double               height,
439                                    gboolean             duplex)
440 {
441         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
442         
443         if (djvu_document->ps_filename)
444                 g_free (djvu_document->ps_filename);    
445         djvu_document->ps_filename = g_strdup(filename);
446
447         g_string_assign(djvu_document->opts, "-page=");
448 }
449
450 static void
451 djvu_document_file_exporter_do_page (EvFileExporter  *exporter,
452                                      EvRenderContext *rc)
453 {
454         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
455         
456         g_string_append_printf(djvu_document->opts, "%d,", (rc->page) + 1); 
457 }
458
459 static void
460 djvu_document_file_exporter_end (EvFileExporter *exporter)
461 {
462         int d_optc = 1; 
463         const char *d_optv[d_optc];
464
465         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
466
467         FILE *fn = fopen(djvu_document->ps_filename, "w");
468         if (fn == NULL) {
469                 g_warning(_("Cannot open file ā€œ%sā€."), djvu_document->ps_filename);
470                 return;
471         }
472         
473         d_optv[0] = djvu_document->opts->str; 
474
475         ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
476         while (!ddjvu_job_done(job) ) { 
477                 djvu_handle_events (djvu_document, TRUE);
478         }
479
480         fclose(fn); 
481 }
482
483 static void
484 djvu_document_file_exporter_iface_init (EvFileExporterIface *iface)
485 {
486         iface->format_supported = djvu_document_file_exporter_format_supported;
487         iface->begin = djvu_document_file_exporter_begin;
488         iface->do_page = djvu_document_file_exporter_do_page;
489         iface->end = djvu_document_file_exporter_end;
490 }
491
492 static void
493 djvu_document_init (DjvuDocument *djvu_document)
494 {
495         guint masks[4] = { 0xff0000, 0xff00, 0xff, 0xff000000 };
496         
497         djvu_document->d_context = ddjvu_context_create ("Evince");
498         djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 4, masks);
499         ddjvu_format_set_row_order (djvu_document->d_format, 1);
500
501         djvu_document->thumbs_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
502         ddjvu_format_set_row_order (djvu_document->thumbs_format, 1);
503
504         djvu_document->ps_filename = NULL;
505         djvu_document->opts = g_string_new ("");
506         
507         djvu_document->d_document = NULL;
508 }
509
510 static void
511 djvu_document_find_begin (EvDocumentFind   *document,
512                           int               page,
513                           const char       *search_string,
514                           gboolean          case_sensitive)
515 {
516         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
517
518         if (djvu_document->search && 
519             strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0)
520                 return;
521
522         if (djvu_document->search)
523                 djvu_text_free (djvu_document->search);
524
525         djvu_document->search = djvu_text_new (djvu_document,
526                                                           page,
527                                                           case_sensitive,
528                                                           search_string);
529 }
530
531 static int
532 djvu_document_find_get_n_results (EvDocumentFind *document_find, int page)
533 {
534         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
535
536         if (search) {
537                 return djvu_text_n_results (search, page);
538         } else {
539                 return 0;
540         }
541 }
542
543 static gboolean
544 djvu_document_find_get_result (EvDocumentFind *document_find,
545                                int             page,
546                                int             n_result,
547                                EvRectangle    *rectangle)
548 {
549         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find);
550         DjvuText *search = djvu_document->search;
551         EvRectangle *r;
552         double width, height;
553
554         if (search == NULL)
555                 return FALSE;
556
557         r = djvu_text_get_result (search, page, n_result);
558         if (r == NULL)
559                 return FALSE;
560
561         djvu_document_get_page_size (EV_DOCUMENT (djvu_document), 
562                 page, &width, &height);
563         rectangle->x1 = r->x1 * SCALE_FACTOR;
564         rectangle->y1 = height - r->y2 * SCALE_FACTOR;
565         rectangle->x2 = r->x2 * SCALE_FACTOR;
566         rectangle->y2 = height - r->y1 * SCALE_FACTOR;
567                 
568         return TRUE;
569 }
570
571 static int
572 djvu_document_find_page_has_results (EvDocumentFind *document_find,
573                                     int             page)
574 {
575         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
576
577         return search && djvu_text_has_results (search, page);
578 }
579
580 static double
581 djvu_document_find_get_progress (EvDocumentFind *document_find)
582 {
583         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
584         
585         if (search == NULL) {
586                 return 0;
587         }
588
589         return djvu_text_get_progress (search);
590 }
591
592 static void
593 djvu_document_find_cancel (EvDocumentFind *document)
594 {
595         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
596
597         if (djvu_document->search) {
598                 djvu_text_free (djvu_document->search);
599                 djvu_document->search = NULL;
600         }
601 }
602
603 static void
604 djvu_document_find_iface_init (EvDocumentFindIface *iface)
605 {
606         iface->begin = djvu_document_find_begin;
607         iface->get_n_results = djvu_document_find_get_n_results;
608         iface->get_result = djvu_document_find_get_result;
609         iface->page_has_results = djvu_document_find_page_has_results;
610         iface->get_progress = djvu_document_find_get_progress;
611         iface->cancel = djvu_document_find_cancel;
612 }
613
614 static GList *
615 djvu_document_links_get_links (EvDocumentLinks *document_links,
616                                gint             page)
617 {
618         return djvu_links_get_links (document_links, page, SCALE_FACTOR);
619 }
620
621 static void
622 djvu_document_document_links_iface_init  (EvDocumentLinksIface *iface)
623 {
624         iface->has_document_links = djvu_links_has_document_links;
625         iface->get_links_model = djvu_links_get_links_model;
626         iface->get_links = djvu_document_links_get_links;
627         iface->find_link_dest = djvu_links_find_link_dest;
628 }