1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Nickolay V. Shmyrev <nshmyrev@yandex.ru>
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)
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "djvu-document.h"
24 #include "djvu-text-page.h"
25 #include "djvu-links.h"
26 #include "djvu-document-private.h"
27 #include "ev-file-exporter.h"
28 #include "ev-document-misc.h"
29 #include "ev-document-find.h"
30 #include "ev-document-links.h"
31 #include "ev-selection.h"
32 #include "ev-file-helpers.h"
35 #include <gdk-pixbuf/gdk-pixbuf.h>
36 #include <glib/gi18n-lib.h>
39 #define SCALE_FACTOR 0.2
46 struct _DjvuDocumentClass
48 EvDocumentClass parent_class;
51 typedef struct _DjvuDocumentClass DjvuDocumentClass;
53 static void djvu_document_file_exporter_iface_init (EvFileExporterInterface *iface);
54 static void djvu_document_find_iface_init (EvDocumentFindInterface *iface);
55 static void djvu_document_document_links_iface_init (EvDocumentLinksInterface *iface);
56 static void djvu_selection_iface_init (EvSelectionInterface *iface);
58 EV_BACKEND_REGISTER_WITH_CODE (DjvuDocument, djvu_document,
60 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, djvu_document_file_exporter_iface_init);
61 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init);
62 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init);
63 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION, djvu_selection_iface_init);
67 #define EV_DJVU_ERROR ev_djvu_error_quark ()
70 ev_djvu_error_quark (void)
74 q = g_quark_from_string ("ev-djvu-quark");
80 handle_message (const ddjvu_message_t *msg, GError **error)
82 switch (msg->m_any.tag) {
86 if (msg->m_error.filename) {
87 error_str = g_strdup_printf ("DjvuLibre error: %s:%d",
88 msg->m_error.filename,
91 error_str = g_strdup_printf ("DjvuLibre error: %s",
92 msg->m_error.message);
96 g_set_error_literal (error, EV_DJVU_ERROR, 0, error_str);
98 g_warning ("%s", error_str);
111 djvu_handle_events (DjvuDocument *djvu_document, int wait, GError **error)
113 ddjvu_context_t *ctx = djvu_document->d_context;
114 const ddjvu_message_t *msg;
120 ddjvu_message_wait (ctx);
122 while ((msg = ddjvu_message_peek (ctx))) {
123 handle_message (msg, error);
124 ddjvu_message_pop (ctx);
131 djvu_wait_for_message (DjvuDocument *djvu_document, ddjvu_message_tag_t message, GError **error)
133 ddjvu_context_t *ctx = djvu_document->d_context;
134 const ddjvu_message_t *msg;
136 ddjvu_message_wait (ctx);
137 while ((msg = ddjvu_message_peek (ctx)) && (msg->m_any.tag != message)) {
138 handle_message (msg, error);
139 ddjvu_message_pop (ctx);
143 if (msg && msg->m_any.tag == message)
144 ddjvu_message_pop (ctx);
148 djvu_document_load (EvDocument *document,
152 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
153 ddjvu_document_t *doc;
155 gboolean missing_files = FALSE;
156 GError *djvu_error = NULL;
158 /* FIXME: We could actually load uris */
159 filename = g_filename_from_uri (uri, NULL, error);
163 doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
167 g_set_error_literal (error,
169 EV_DOCUMENT_ERROR_INVALID,
170 _("DjVu document has incorrect format"));
174 if (djvu_document->d_document)
175 ddjvu_document_release (djvu_document->d_document);
177 djvu_document->d_document = doc;
179 djvu_wait_for_message (djvu_document, DDJVU_DOCINFO, &djvu_error);
181 g_set_error_literal (error,
183 EV_DOCUMENT_ERROR_INVALID,
184 djvu_error->message);
185 g_error_free (djvu_error);
187 ddjvu_document_release (djvu_document->d_document);
188 djvu_document->d_document = NULL;
193 if (ddjvu_document_decoding_error (djvu_document->d_document))
194 djvu_handle_events (djvu_document, TRUE, &djvu_error);
197 g_set_error_literal (error,
199 EV_DOCUMENT_ERROR_INVALID,
200 djvu_error->message);
201 g_error_free (djvu_error);
203 ddjvu_document_release (djvu_document->d_document);
204 djvu_document->d_document = NULL;
209 g_free (djvu_document->uri);
210 djvu_document->uri = g_strdup (uri);
212 if (ddjvu_document_get_type (djvu_document->d_document) == DDJVU_DOCTYPE_INDIRECT) {
217 base = g_path_get_dirname (filename);
219 n_files = ddjvu_document_get_filenum (djvu_document->d_document);
220 for (i = 0; i < n_files; i++) {
221 struct ddjvu_fileinfo_s fileinfo;
224 ddjvu_document_get_fileinfo (djvu_document->d_document,
227 if (fileinfo.type != 'P')
230 file = g_build_filename (base, fileinfo.id, NULL);
231 if (!g_file_test (file, G_FILE_TEST_EXISTS)) {
232 missing_files = TRUE;
244 g_set_error_literal (error,
247 _("The document is composed of several files. "
248 "One or more of these files cannot be accessed."));
258 djvu_document_save (EvDocument *document,
262 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
264 return ev_xfer_uri_simple (djvu_document->uri, uri, error);
268 djvu_document_get_n_pages (EvDocument *document)
270 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
272 g_return_val_if_fail (djvu_document->d_document, 0);
274 return ddjvu_document_get_pagenum (djvu_document->d_document);
278 document_get_page_size (DjvuDocument *djvu_document,
283 ddjvu_pageinfo_t info;
286 while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
287 djvu_handle_events(djvu_document, TRUE, NULL);
289 if (r >= DDJVU_JOB_FAILED)
290 djvu_handle_events(djvu_document, TRUE, NULL);
292 *width = info.width * SCALE_FACTOR;
293 *height = info.height * SCALE_FACTOR;
297 djvu_document_get_page_size (EvDocument *document,
302 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
304 g_return_if_fail (djvu_document->d_document);
306 document_get_page_size (djvu_document, page->index,
310 static cairo_surface_t *
311 djvu_document_render (EvDocument *document,
314 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
315 cairo_surface_t *surface;
320 ddjvu_page_t *d_page;
321 ddjvu_page_rotation_t rotation;
322 double page_width, page_height, tmp;
324 d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page->index);
326 while (!ddjvu_page_decoding_done (d_page))
327 djvu_handle_events(djvu_document, TRUE, NULL);
329 page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
330 page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
332 switch (rc->rotation) {
334 rotation = DDJVU_ROTATE_90;
336 page_height = page_width;
341 rotation = DDJVU_ROTATE_180;
345 rotation = DDJVU_ROTATE_270;
347 page_height = page_width;
352 rotation = DDJVU_ROTATE_0;
355 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
356 page_width, page_height);
357 rowstride = cairo_image_surface_get_stride (surface);
358 pixels = (gchar *)cairo_image_surface_get_data (surface);
362 prect.w = page_width;
363 prect.h = page_height;
366 ddjvu_page_set_rotation (d_page, rotation);
368 ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
371 djvu_document->d_format,
375 cairo_surface_mark_dirty (surface);
381 djvu_document_get_thumbnail (EvDocument *document,
384 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
385 GdkPixbuf *pixbuf, *rotated_pixbuf;
386 gdouble page_width, page_height;
387 gint thumb_width, thumb_height;
390 g_return_val_if_fail (djvu_document->d_document, NULL);
392 djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
393 &page_width, &page_height);
395 thumb_width = (gint) (page_width * rc->scale);
396 thumb_height = (gint) (page_height * rc->scale);
398 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
399 thumb_width, thumb_height);
400 gdk_pixbuf_fill (pixbuf, 0xffffffff);
401 pixels = gdk_pixbuf_get_pixels (pixbuf);
403 while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page->index, 1) < DDJVU_JOB_OK)
404 djvu_handle_events(djvu_document, TRUE, NULL);
406 ddjvu_thumbnail_render (djvu_document->d_document, rc->page->index,
407 &thumb_width, &thumb_height,
408 djvu_document->thumbs_format,
409 gdk_pixbuf_get_rowstride (pixbuf),
412 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
413 g_object_unref (pixbuf);
415 return rotated_pixbuf;
419 djvu_document_finalize (GObject *object)
421 DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
423 if (djvu_document->d_document)
424 ddjvu_document_release (djvu_document->d_document);
426 if (djvu_document->opts)
427 g_string_free (djvu_document->opts, TRUE);
429 if (djvu_document->ps_filename)
430 g_free (djvu_document->ps_filename);
432 ddjvu_context_release (djvu_document->d_context);
433 ddjvu_format_release (djvu_document->d_format);
434 ddjvu_format_release (djvu_document->thumbs_format);
435 g_free (djvu_document->uri);
437 G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
441 djvu_document_class_init (DjvuDocumentClass *klass)
443 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
444 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
446 gobject_class->finalize = djvu_document_finalize;
448 ev_document_class->load = djvu_document_load;
449 ev_document_class->save = djvu_document_save;
450 ev_document_class->get_n_pages = djvu_document_get_n_pages;
451 ev_document_class->get_page_size = djvu_document_get_page_size;
452 ev_document_class->render = djvu_document_render;
453 ev_document_class->get_thumbnail = djvu_document_get_thumbnail;
457 djvu_text_copy (DjvuDocument *djvu_document,
459 EvRectangle *rectangle)
465 ddjvu_document_get_pagetext (djvu_document->d_document,
466 page, "char")) == miniexp_dummy)
467 djvu_handle_events (djvu_document, TRUE, NULL);
469 if (page_text != miniexp_nil) {
470 DjvuTextPage *page = djvu_text_page_new (page_text);
472 text = djvu_text_page_copy (page, rectangle);
473 djvu_text_page_free (page);
474 ddjvu_miniexp_release (djvu_document->d_document, page_text);
481 djvu_selection_get_selected_text (EvSelection *selection,
483 EvSelectionStyle style,
486 DjvuDocument *djvu_document = DJVU_DOCUMENT (selection);
487 double width, height;
488 EvRectangle rectangle;
491 djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
492 page, &width, &height);
493 rectangle.x1 = points->x1 / SCALE_FACTOR;
494 rectangle.y1 = (height - points->y2) / SCALE_FACTOR;
495 rectangle.x2 = points->x2 / SCALE_FACTOR;
496 rectangle.y2 = (height - points->y1) / SCALE_FACTOR;
498 text = djvu_text_copy (djvu_document, page->index, &rectangle);
501 text = g_strdup ("");
507 djvu_selection_iface_init (EvSelectionInterface *iface)
509 iface->get_selected_text = djvu_selection_get_selected_text;
512 /* EvFileExporterIface */
514 djvu_document_file_exporter_begin (EvFileExporter *exporter,
515 EvFileExporterContext *fc)
517 DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
519 if (djvu_document->ps_filename)
520 g_free (djvu_document->ps_filename);
521 djvu_document->ps_filename = g_strdup (fc->filename);
523 g_string_assign (djvu_document->opts, "-page=");
527 djvu_document_file_exporter_do_page (EvFileExporter *exporter,
530 DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
532 g_string_append_printf (djvu_document->opts, "%d,", (rc->page->index) + 1);
536 djvu_document_file_exporter_end (EvFileExporter *exporter)
539 const char *d_optv[d_optc];
541 DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
543 FILE *fn = fopen (djvu_document->ps_filename, "w");
545 g_warning ("Cannot open file ā%sā.", djvu_document->ps_filename);
549 d_optv[0] = djvu_document->opts->str;
551 ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
552 while (!ddjvu_job_done(job)) {
553 djvu_handle_events (djvu_document, TRUE, NULL);
559 static EvFileExporterCapabilities
560 djvu_document_file_exporter_get_capabilities (EvFileExporter *exporter)
562 return EV_FILE_EXPORTER_CAN_PAGE_SET |
563 EV_FILE_EXPORTER_CAN_COPIES |
564 EV_FILE_EXPORTER_CAN_COLLATE |
565 EV_FILE_EXPORTER_CAN_REVERSE |
566 EV_FILE_EXPORTER_CAN_GENERATE_PS;
570 djvu_document_file_exporter_iface_init (EvFileExporterInterface *iface)
572 iface->begin = djvu_document_file_exporter_begin;
573 iface->do_page = djvu_document_file_exporter_do_page;
574 iface->end = djvu_document_file_exporter_end;
575 iface->get_capabilities = djvu_document_file_exporter_get_capabilities;
579 djvu_document_init (DjvuDocument *djvu_document)
581 guint masks[4] = { 0xff0000, 0xff00, 0xff, 0xff000000 };
583 djvu_document->d_context = ddjvu_context_create ("Evince");
584 djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 4, masks);
585 ddjvu_format_set_row_order (djvu_document->d_format, 1);
587 djvu_document->thumbs_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
588 ddjvu_format_set_row_order (djvu_document->thumbs_format, 1);
590 djvu_document->ps_filename = NULL;
591 djvu_document->opts = g_string_new ("");
593 djvu_document->d_document = NULL;
597 djvu_document_find_find_text (EvDocumentFind *document,
600 gboolean case_sensitive)
602 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
604 gdouble width, height;
605 GList *matches = NULL, *l;
607 g_return_val_if_fail (text != NULL, NULL);
609 while ((page_text = ddjvu_document_get_pagetext (djvu_document->d_document,
611 "char")) == miniexp_dummy)
612 djvu_handle_events (djvu_document, TRUE, NULL);
614 if (page_text != miniexp_nil) {
615 DjvuTextPage *tpage = djvu_text_page_new (page_text);
617 djvu_text_page_prepare_search (tpage, case_sensitive);
618 if (tpage->links->len > 0) {
619 djvu_text_page_search (tpage, text);
620 matches = tpage->results;
622 djvu_text_page_free (tpage);
623 ddjvu_miniexp_release (djvu_document->d_document, page_text);
629 document_get_page_size (djvu_document, page->index, &width, &height);
630 for (l = matches; l && l->data; l = g_list_next (l)) {
631 EvRectangle *r = (EvRectangle *)l->data;
636 r->x1 *= SCALE_FACTOR;
637 r->x2 *= SCALE_FACTOR;
640 r->y1 = height - r->y2 * SCALE_FACTOR;
641 r->y2 = height - tmp * SCALE_FACTOR;
649 djvu_document_find_iface_init (EvDocumentFindInterface *iface)
651 iface->find_text = djvu_document_find_find_text;
654 static EvMappingList *
655 djvu_document_links_get_links (EvDocumentLinks *document_links,
658 return djvu_links_get_links (document_links, page->index, SCALE_FACTOR);
662 djvu_document_document_links_iface_init (EvDocumentLinksInterface *iface)
664 iface->has_document_links = djvu_links_has_document_links;
665 iface->get_links_model = djvu_links_get_links_model;
666 iface->get_links = djvu_document_links_get_links;
667 iface->find_link_dest = djvu_links_find_link_dest;
668 iface->find_link_page = djvu_links_find_link_page;