summaryrefslogtreecommitdiff
path: root/backend/djvu
diff options
context:
space:
mode:
authorPerberos <[email protected]>2011-11-09 18:17:43 -0300
committerPerberos <[email protected]>2011-11-09 18:17:43 -0300
commitf6ce926719943751cf65cacde7fae050593eb2d6 (patch)
tree9224d1751678cf2d1fbd0431f128b711311c0287 /backend/djvu
downloadatril-f6ce926719943751cf65cacde7fae050593eb2d6.tar.bz2
atril-f6ce926719943751cf65cacde7fae050593eb2d6.tar.xz
inicial
Diffstat (limited to 'backend/djvu')
-rw-r--r--backend/djvu/Makefile.am38
-rw-r--r--backend/djvu/djvu-document-private.h48
-rw-r--r--backend/djvu/djvu-document.c706
-rw-r--r--backend/djvu/djvu-document.h38
-rw-r--r--backend/djvu/djvu-links.c434
-rw-r--r--backend/djvu/djvu-links.h35
-rw-r--r--backend/djvu/djvu-text-page.c445
-rw-r--r--backend/djvu/djvu-text-page.h59
-rw-r--r--backend/djvu/djvudocument.evince-backend.in4
9 files changed, 1807 insertions, 0 deletions
diff --git a/backend/djvu/Makefile.am b/backend/djvu/Makefile.am
new file mode 100644
index 00000000..6bc9d06e
--- /dev/null
+++ b/backend/djvu/Makefile.am
@@ -0,0 +1,38 @@
+INCLUDES = \
+ -I$(top_srcdir) \
+ -I$(top_srcdir)/libdocument \
+ -DMATEICONDIR=\""${prefix}/${DATADIRNAME}/pixmaps"\" \
+ -DMATELOCALEDIR=\"$(datadir)/locale\" \
+ -DEVINCE_COMPILATION \
+ $(BACKEND_CFLAGS) \
+ $(DJVU_CFLAGS) \
+ $(WARN_CFLAGS) \
+ $(DISABLE_DEPRECATED)
+
+backend_LTLIBRARIES = libdjvudocument.la
+
+libdjvudocument_la_SOURCES = \
+ djvu-document.c \
+ djvu-document.h \
+ djvu-document-private.h \
+ djvu-links.c \
+ djvu-links.h \
+ djvu-text-page.c \
+ djvu-text-page.h
+
+libdjvudocument_la_LDFLAGS = $(BACKEND_LIBTOOL_FLAGS)
+libdjvudocument_la_LIBADD = \
+ $(top_builddir)/libdocument/libevdocument.la \
+ $(BACKEND_LIBS) \
+ $(DJVU_LIBS)
+
+backend_in_files = djvudocument.evince-backend.in
+backend_DATA = $(backend_in_files:.evince-backend.in=.evince-backend)
+
+EXTRA_DIST = $(backend_in_files)
+
+CLEANFILES = $(backend_DATA)
+
+@EV_INTLTOOL_EVINCE_BACKEND_RULE@
+
+-include $(top_srcdir)/git.mk
diff --git a/backend/djvu/djvu-document-private.h b/backend/djvu/djvu-document-private.h
new file mode 100644
index 00000000..2ec9b67a
--- /dev/null
+++ b/backend/djvu/djvu-document-private.h
@@ -0,0 +1,48 @@
+/*
+ * Declarations used throughout the djvu classes
+ *
+ * Copyright (C) 2006, Michael Hofmann <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __DJVU_DOCUMENT_INTERNAL_H__
+#define __DJVU_DOCUMENT_INTERNAL_H__
+
+#include "djvu-document.h"
+
+#include <libdjvu/ddjvuapi.h>
+
+struct _DjvuDocument {
+ EvDocument parent_instance;
+
+ ddjvu_context_t *d_context;
+ ddjvu_document_t *d_document;
+ ddjvu_format_t *d_format;
+ ddjvu_format_t *thumbs_format;
+
+ gchar *uri;
+
+ /* PS exporter */
+ gchar *ps_filename;
+ GString *opts;
+};
+
+int djvu_document_get_n_pages (EvDocument *document);
+void djvu_handle_events (DjvuDocument *djvu_document,
+ int wait,
+ GError **error);
+
+#endif /* __DJVU_DOCUMENT_INTERNAL_H__ */
diff --git a/backend/djvu/djvu-document.c b/backend/djvu/djvu-document.c
new file mode 100644
index 00000000..aa0e595d
--- /dev/null
+++ b/backend/djvu/djvu-document.c
@@ -0,0 +1,706 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
+/*
+ * Copyright (C) 2005, Nickolay V. Shmyrev <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include <config.h>
+#include "djvu-document.h"
+#include "djvu-text-page.h"
+#include "djvu-links.h"
+#include "djvu-document-private.h"
+#include "ev-document-thumbnails.h"
+#include "ev-file-exporter.h"
+#include "ev-document-misc.h"
+#include "ev-document-find.h"
+#include "ev-document-links.h"
+#include "ev-selection.h"
+#include "ev-file-helpers.h"
+
+#include <glib.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+#include <glib/gi18n-lib.h>
+#include <string.h>
+
+#define SCALE_FACTOR 0.2
+
+enum {
+ PROP_0,
+ PROP_TITLE
+};
+
+struct _DjvuDocumentClass
+{
+ EvDocumentClass parent_class;
+};
+
+typedef struct _DjvuDocumentClass DjvuDocumentClass;
+
+static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface);
+static void djvu_document_file_exporter_iface_init (EvFileExporterInterface *iface);
+static void djvu_document_find_iface_init (EvDocumentFindInterface *iface);
+static void djvu_document_document_links_iface_init (EvDocumentLinksInterface *iface);
+static void djvu_selection_iface_init (EvSelectionInterface *iface);
+
+EV_BACKEND_REGISTER_WITH_CODE (DjvuDocument, djvu_document,
+ {
+ EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init);
+ EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, djvu_document_file_exporter_iface_init);
+ EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init);
+ EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init);
+ EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION, djvu_selection_iface_init);
+ });
+
+
+#define EV_DJVU_ERROR ev_djvu_error_quark ()
+
+static GQuark
+ev_djvu_error_quark (void)
+{
+ static GQuark q = 0;
+ if (q == 0)
+ q = g_quark_from_string ("ev-djvu-quark");
+
+ return q;
+}
+
+static void
+handle_message (const ddjvu_message_t *msg, GError **error)
+{
+ switch (msg->m_any.tag) {
+ case DDJVU_ERROR: {
+ gchar *error_str;
+
+ if (msg->m_error.filename) {
+ error_str = g_strdup_printf ("DjvuLibre error: %s:%d",
+ msg->m_error.filename,
+ msg->m_error.lineno);
+ } else {
+ error_str = g_strdup_printf ("DjvuLibre error: %s",
+ msg->m_error.message);
+ }
+
+ if (error) {
+ g_set_error_literal (error, EV_DJVU_ERROR, 0, error_str);
+ } else {
+ g_warning ("%s", error_str);
+ }
+
+ g_free (error_str);
+ return;
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+void
+djvu_handle_events (DjvuDocument *djvu_document, int wait, GError **error)
+{
+ ddjvu_context_t *ctx = djvu_document->d_context;
+ const ddjvu_message_t *msg;
+
+ if (!ctx)
+ return;
+
+ if (wait)
+ ddjvu_message_wait (ctx);
+
+ while ((msg = ddjvu_message_peek (ctx))) {
+ handle_message (msg, error);
+ ddjvu_message_pop (ctx);
+ if (error && *error)
+ return;
+ }
+}
+
+static void
+djvu_wait_for_message (DjvuDocument *djvu_document, ddjvu_message_tag_t message, GError **error)
+{
+ ddjvu_context_t *ctx = djvu_document->d_context;
+ const ddjvu_message_t *msg;
+
+ ddjvu_message_wait (ctx);
+ while ((msg = ddjvu_message_peek (ctx)) && (msg->m_any.tag != message)) {
+ handle_message (msg, error);
+ ddjvu_message_pop (ctx);
+ if (error && *error)
+ return;
+ }
+ if (msg && msg->m_any.tag == message)
+ ddjvu_message_pop (ctx);
+}
+
+static gboolean
+djvu_document_load (EvDocument *document,
+ const char *uri,
+ GError **error)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+ ddjvu_document_t *doc;
+ gchar *filename;
+ gboolean missing_files = FALSE;
+ GError *djvu_error = NULL;
+
+ /* FIXME: We could actually load uris */
+ filename = g_filename_from_uri (uri, NULL, error);
+ if (!filename)
+ return FALSE;
+
+ doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
+
+ if (!doc) {
+ g_free (filename);
+ g_set_error_literal (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ _("DjVu document has incorrect format"));
+ return FALSE;
+ }
+
+ if (djvu_document->d_document)
+ ddjvu_document_release (djvu_document->d_document);
+
+ djvu_document->d_document = doc;
+
+ djvu_wait_for_message (djvu_document, DDJVU_DOCINFO, &djvu_error);
+ if (djvu_error) {
+ g_set_error_literal (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ djvu_error->message);
+ g_error_free (djvu_error);
+ g_free (filename);
+ ddjvu_document_release (djvu_document->d_document);
+ djvu_document->d_document = NULL;
+
+ return FALSE;
+ }
+
+ if (ddjvu_document_decoding_error (djvu_document->d_document))
+ djvu_handle_events (djvu_document, TRUE, &djvu_error);
+
+ if (djvu_error) {
+ g_set_error_literal (error,
+ EV_DOCUMENT_ERROR,
+ EV_DOCUMENT_ERROR_INVALID,
+ djvu_error->message);
+ g_error_free (djvu_error);
+ g_free (filename);
+ ddjvu_document_release (djvu_document->d_document);
+ djvu_document->d_document = NULL;
+
+ return FALSE;
+ }
+
+ g_free (djvu_document->uri);
+ djvu_document->uri = g_strdup (uri);
+
+ if (ddjvu_document_get_type (djvu_document->d_document) == DDJVU_DOCTYPE_INDIRECT) {
+ gint n_files;
+ gint i;
+ gchar *base;
+
+ base = g_path_get_dirname (filename);
+
+ n_files = ddjvu_document_get_filenum (djvu_document->d_document);
+ for (i = 0; i < n_files; i++) {
+ struct ddjvu_fileinfo_s fileinfo;
+ gchar *file;
+
+ ddjvu_document_get_fileinfo (djvu_document->d_document,
+ i, &fileinfo);
+
+ if (fileinfo.type != 'P')
+ continue;
+
+ file = g_build_filename (base, fileinfo.id, NULL);
+ if (!g_file_test (file, G_FILE_TEST_EXISTS)) {
+ missing_files = TRUE;
+ g_free (file);
+
+ break;
+ }
+ g_free (file);
+ }
+ g_free (base);
+ }
+ g_free (filename);
+
+ if (missing_files) {
+ g_set_error_literal (error,
+ G_FILE_ERROR,
+ G_FILE_ERROR_EXIST,
+ _("The document is composed of several files. "
+ "One or more of these files cannot be accessed."));
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+
+static gboolean
+djvu_document_save (EvDocument *document,
+ const char *uri,
+ GError **error)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+
+ return ev_xfer_uri_simple (djvu_document->uri, uri, error);
+}
+
+int
+djvu_document_get_n_pages (EvDocument *document)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+
+ g_return_val_if_fail (djvu_document->d_document, 0);
+
+ return ddjvu_document_get_pagenum (djvu_document->d_document);
+}
+
+static void
+document_get_page_size (DjvuDocument *djvu_document,
+ gint page,
+ double *width,
+ double *height)
+{
+ ddjvu_pageinfo_t info;
+ ddjvu_status_t r;
+
+ while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
+ djvu_handle_events(djvu_document, TRUE, NULL);
+
+ if (r >= DDJVU_JOB_FAILED)
+ djvu_handle_events(djvu_document, TRUE, NULL);
+
+ *width = info.width * SCALE_FACTOR;
+ *height = info.height * SCALE_FACTOR;
+}
+
+static void
+djvu_document_get_page_size (EvDocument *document,
+ EvPage *page,
+ double *width,
+ double *height)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+
+ g_return_if_fail (djvu_document->d_document);
+
+ document_get_page_size (djvu_document, page->index,
+ width, height);
+}
+
+static cairo_surface_t *
+djvu_document_render (EvDocument *document,
+ EvRenderContext *rc)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+ cairo_surface_t *surface;
+ gchar *pixels;
+ gint rowstride;
+ ddjvu_rect_t rrect;
+ ddjvu_rect_t prect;
+ ddjvu_page_t *d_page;
+ ddjvu_page_rotation_t rotation;
+ double page_width, page_height, tmp;
+
+ d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page->index);
+
+ while (!ddjvu_page_decoding_done (d_page))
+ djvu_handle_events(djvu_document, TRUE, NULL);
+
+ page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
+ page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
+
+ switch (rc->rotation) {
+ case 90:
+ rotation = DDJVU_ROTATE_90;
+ tmp = page_height;
+ page_height = page_width;
+ page_width = tmp;
+
+ break;
+ case 180:
+ rotation = DDJVU_ROTATE_180;
+
+ break;
+ case 270:
+ rotation = DDJVU_ROTATE_270;
+ tmp = page_height;
+ page_height = page_width;
+ page_width = tmp;
+
+ break;
+ default:
+ rotation = DDJVU_ROTATE_0;
+ }
+
+ surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
+ page_width, page_height);
+ rowstride = cairo_image_surface_get_stride (surface);
+ pixels = (gchar *)cairo_image_surface_get_data (surface);
+
+ prect.x = 0;
+ prect.y = 0;
+ prect.w = page_width;
+ prect.h = page_height;
+ rrect = prect;
+
+ ddjvu_page_set_rotation (d_page, rotation);
+
+ ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
+ &prect,
+ &rrect,
+ djvu_document->d_format,
+ rowstride,
+ pixels);
+
+ cairo_surface_mark_dirty (surface);
+
+ return surface;
+}
+
+static void
+djvu_document_finalize (GObject *object)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
+
+ if (djvu_document->d_document)
+ ddjvu_document_release (djvu_document->d_document);
+
+ if (djvu_document->opts)
+ g_string_free (djvu_document->opts, TRUE);
+
+ if (djvu_document->ps_filename)
+ g_free (djvu_document->ps_filename);
+
+ ddjvu_context_release (djvu_document->d_context);
+ ddjvu_format_release (djvu_document->d_format);
+ ddjvu_format_release (djvu_document->thumbs_format);
+ g_free (djvu_document->uri);
+
+ G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
+}
+
+static void
+djvu_document_class_init (DjvuDocumentClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+ EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
+
+ gobject_class->finalize = djvu_document_finalize;
+
+ ev_document_class->load = djvu_document_load;
+ ev_document_class->save = djvu_document_save;
+ ev_document_class->get_n_pages = djvu_document_get_n_pages;
+ ev_document_class->get_page_size = djvu_document_get_page_size;
+ ev_document_class->render = djvu_document_render;
+}
+
+static gchar *
+djvu_text_copy (DjvuDocument *djvu_document,
+ gint page,
+ EvRectangle *rectangle)
+{
+ miniexp_t page_text;
+ gchar *text = NULL;
+
+ while ((page_text =
+ ddjvu_document_get_pagetext (djvu_document->d_document,
+ page, "char")) == miniexp_dummy)
+ djvu_handle_events (djvu_document, TRUE, NULL);
+
+ if (page_text != miniexp_nil) {
+ DjvuTextPage *page = djvu_text_page_new (page_text);
+
+ text = djvu_text_page_copy (page, rectangle);
+ djvu_text_page_free (page);
+ ddjvu_miniexp_release (djvu_document->d_document, page_text);
+ }
+
+ return text;
+}
+
+static gchar *
+djvu_selection_get_selected_text (EvSelection *selection,
+ EvPage *page,
+ EvSelectionStyle style,
+ EvRectangle *points)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (selection);
+ double width, height;
+ EvRectangle rectangle;
+ gchar *text;
+
+ djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
+ page, &width, &height);
+ rectangle.x1 = points->x1 / SCALE_FACTOR;
+ rectangle.y1 = (height - points->y2) / SCALE_FACTOR;
+ rectangle.x2 = points->x2 / SCALE_FACTOR;
+ rectangle.y2 = (height - points->y1) / SCALE_FACTOR;
+
+ text = djvu_text_copy (djvu_document, page->index, &rectangle);
+
+ if (text == NULL)
+ text = g_strdup ("");
+
+ return text;
+}
+
+static void
+djvu_selection_iface_init (EvSelectionInterface *iface)
+{
+ iface->get_selected_text = djvu_selection_get_selected_text;
+}
+
+static void
+djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
+ EvRenderContext *rc,
+ gint *width,
+ gint *height)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+ gdouble page_width, page_height;
+
+ djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
+ &page_width, &page_height);
+
+ if (rc->rotation == 90 || rc->rotation == 270) {
+ *width = (gint) (page_height * rc->scale);
+ *height = (gint) (page_width * rc->scale);
+ } else {
+ *width = (gint) (page_width * rc->scale);
+ *height = (gint) (page_height * rc->scale);
+ }
+}
+
+static GdkPixbuf *
+djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
+ EvRenderContext *rc,
+ gboolean border)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+ GdkPixbuf *pixbuf, *rotated_pixbuf;
+ gdouble page_width, page_height;
+ gint thumb_width, thumb_height;
+ guchar *pixels;
+
+ g_return_val_if_fail (djvu_document->d_document, NULL);
+
+ djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
+ &page_width, &page_height);
+
+ thumb_width = (gint) (page_width * rc->scale);
+ thumb_height = (gint) (page_height * rc->scale);
+
+ pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
+ thumb_width, thumb_height);
+ gdk_pixbuf_fill (pixbuf, 0xffffffff);
+ pixels = gdk_pixbuf_get_pixels (pixbuf);
+
+ while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page->index, 1) < DDJVU_JOB_OK)
+ djvu_handle_events(djvu_document, TRUE, NULL);
+
+ ddjvu_thumbnail_render (djvu_document->d_document, rc->page->index,
+ &thumb_width, &thumb_height,
+ djvu_document->thumbs_format,
+ gdk_pixbuf_get_rowstride (pixbuf),
+ (gchar *)pixels);
+
+ rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
+ g_object_unref (pixbuf);
+
+ if (border) {
+ GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
+
+ rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
+ g_object_unref (tmp_pixbuf);
+ }
+
+ return rotated_pixbuf;
+}
+
+static void
+djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
+{
+ iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
+ iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
+}
+
+/* EvFileExporterIface */
+static void
+djvu_document_file_exporter_begin (EvFileExporter *exporter,
+ EvFileExporterContext *fc)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
+
+ if (djvu_document->ps_filename)
+ g_free (djvu_document->ps_filename);
+ djvu_document->ps_filename = g_strdup (fc->filename);
+
+ g_string_assign (djvu_document->opts, "-page=");
+}
+
+static void
+djvu_document_file_exporter_do_page (EvFileExporter *exporter,
+ EvRenderContext *rc)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
+
+ g_string_append_printf (djvu_document->opts, "%d,", (rc->page->index) + 1);
+}
+
+static void
+djvu_document_file_exporter_end (EvFileExporter *exporter)
+{
+ int d_optc = 1;
+ const char *d_optv[d_optc];
+
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
+
+ FILE *fn = fopen (djvu_document->ps_filename, "w");
+ if (fn == NULL) {
+ g_warning ("Cannot open file ā€œ%sā€.", djvu_document->ps_filename);
+ return;
+ }
+
+ d_optv[0] = djvu_document->opts->str;
+
+ ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
+ while (!ddjvu_job_done(job)) {
+ djvu_handle_events (djvu_document, TRUE, NULL);
+ }
+
+ fclose(fn);
+}
+
+static EvFileExporterCapabilities
+djvu_document_file_exporter_get_capabilities (EvFileExporter *exporter)
+{
+ return EV_FILE_EXPORTER_CAN_PAGE_SET |
+ EV_FILE_EXPORTER_CAN_COPIES |
+ EV_FILE_EXPORTER_CAN_COLLATE |
+ EV_FILE_EXPORTER_CAN_REVERSE |
+ EV_FILE_EXPORTER_CAN_GENERATE_PS;
+}
+
+static void
+djvu_document_file_exporter_iface_init (EvFileExporterInterface *iface)
+{
+ iface->begin = djvu_document_file_exporter_begin;
+ iface->do_page = djvu_document_file_exporter_do_page;
+ iface->end = djvu_document_file_exporter_end;
+ iface->get_capabilities = djvu_document_file_exporter_get_capabilities;
+}
+
+static void
+djvu_document_init (DjvuDocument *djvu_document)
+{
+ guint masks[4] = { 0xff0000, 0xff00, 0xff, 0xff000000 };
+
+ djvu_document->d_context = ddjvu_context_create ("Evince");
+ djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 4, masks);
+ ddjvu_format_set_row_order (djvu_document->d_format, 1);
+
+ djvu_document->thumbs_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
+ ddjvu_format_set_row_order (djvu_document->thumbs_format, 1);
+
+ djvu_document->ps_filename = NULL;
+ djvu_document->opts = g_string_new ("");
+
+ djvu_document->d_document = NULL;
+}
+
+static GList *
+djvu_document_find_find_text (EvDocumentFind *document,
+ EvPage *page,
+ const char *text,
+ gboolean case_sensitive)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
+ miniexp_t page_text;
+ gdouble width, height;
+ GList *matches = NULL, *l;
+
+ g_return_val_if_fail (text != NULL, NULL);
+
+ while ((page_text = ddjvu_document_get_pagetext (djvu_document->d_document,
+ page->index,
+ "char")) == miniexp_dummy)
+ djvu_handle_events (djvu_document, TRUE, NULL);
+
+ if (page_text != miniexp_nil) {
+ DjvuTextPage *tpage = djvu_text_page_new (page_text);
+
+ djvu_text_page_prepare_search (tpage, case_sensitive);
+ if (tpage->links->len > 0) {
+ djvu_text_page_search (tpage, text);
+ matches = tpage->results;
+ }
+ djvu_text_page_free (tpage);
+ ddjvu_miniexp_release (djvu_document->d_document, page_text);
+ }
+
+ if (!matches)
+ return NULL;
+
+ document_get_page_size (djvu_document, page->index, &width, &height);
+ for (l = matches; l && l->data; l = g_list_next (l)) {
+ EvRectangle *r = (EvRectangle *)l->data;
+ gdouble tmp;
+
+ tmp = r->y1;
+
+ r->x1 *= SCALE_FACTOR;
+ r->x2 *= SCALE_FACTOR;
+
+ tmp = r->y1;
+ r->y1 = height - r->y2 * SCALE_FACTOR;
+ r->y2 = height - tmp * SCALE_FACTOR;
+ }
+
+
+ return matches;
+}
+
+static void
+djvu_document_find_iface_init (EvDocumentFindInterface *iface)
+{
+ iface->find_text = djvu_document_find_find_text;
+}
+
+static EvMappingList *
+djvu_document_links_get_links (EvDocumentLinks *document_links,
+ EvPage *page)
+{
+ return djvu_links_get_links (document_links, page->index, SCALE_FACTOR);
+}
+
+static void
+djvu_document_document_links_iface_init (EvDocumentLinksInterface *iface)
+{
+ iface->has_document_links = djvu_links_has_document_links;
+ iface->get_links_model = djvu_links_get_links_model;
+ iface->get_links = djvu_document_links_get_links;
+ iface->find_link_dest = djvu_links_find_link_dest;
+}
diff --git a/backend/djvu/djvu-document.h b/backend/djvu/djvu-document.h
new file mode 100644
index 00000000..bcb916af
--- /dev/null
+++ b/backend/djvu/djvu-document.h
@@ -0,0 +1,38 @@
+/* djvu-document.h: Implementation of EvDocument for djvu documents
+ * Copyright (C) 2005, Nickolay V. Shmyrev <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __DJVU_DOCUMENT_H__
+#define __DJVU_DOCUMENT_H__
+
+#include "ev-document.h"
+
+G_BEGIN_DECLS
+
+#define DJVU_TYPE_DOCUMENT (djvu_document_get_type ())
+#define DJVU_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DJVU_TYPE_DOCUMENT, DjvuDocument))
+#define DJVU_IS_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DJVU_TYPE_DOCUMENT))
+
+typedef struct _DjvuDocument DjvuDocument;
+
+GType djvu_document_get_type (void) G_GNUC_CONST;
+
+G_MODULE_EXPORT GType register_evince_backend (GTypeModule *module);
+
+G_END_DECLS
+
+#endif /* __DJVU_DOCUMENT_H__ */
diff --git a/backend/djvu/djvu-links.c b/backend/djvu/djvu-links.c
new file mode 100644
index 00000000..d13af0be
--- /dev/null
+++ b/backend/djvu/djvu-links.c
@@ -0,0 +1,434 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
+/*
+ * Implements hyperlink functionality for Djvu files.
+ * Copyright (C) 2006 Pauli Virtanen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <config.h>
+#include <string.h>
+#include <glib.h>
+#include <libdjvu/miniexp.h>
+#include "djvu-document.h"
+#include "djvu-links.h"
+#include "djvu-document-private.h"
+#include "ev-document-links.h"
+#include "ev-mapping-list.h"
+
+static gboolean number_from_miniexp(miniexp_t sexp, int *number)
+{
+ if (miniexp_numberp (sexp)) {
+ *number = miniexp_to_int (sexp);
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
+
+static gboolean string_from_miniexp(miniexp_t sexp, const char **str)
+{
+ if (miniexp_stringp (sexp)) {
+ *str = miniexp_to_str (sexp);
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
+
+static gboolean number_from_string_10(const gchar *str, guint64 *number)
+{
+ gchar *end_ptr;
+
+ *number = g_ascii_strtoull(str, &end_ptr, 10);
+ if (*end_ptr == '\0') {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
+
+static EvLinkDest *
+get_djvu_link_dest (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
+{
+ guint64 page_num = 0;
+
+ /* #pagenum, #+pageoffset, #-pageoffset */
+ if (g_str_has_prefix (link_name, "#")) {
+ if (base_page > 0 && g_str_has_prefix (link_name+1, "+")) {
+ if (number_from_string_10 (link_name + 2, &page_num)) {
+ return ev_link_dest_new_page (base_page + page_num);
+ }
+ } else if (base_page > 0 && g_str_has_prefix (link_name+1, "-")) {
+ if (number_from_string_10 (link_name + 2, &page_num)) {
+ return ev_link_dest_new_page (base_page - page_num);
+ }
+ } else {
+ if (number_from_string_10 (link_name + 1, &page_num)) {
+ return ev_link_dest_new_page (page_num - 1);
+ }
+ }
+ } else {
+ /* FIXME: component file identifiers */
+ }
+
+ return NULL;
+}
+
+static EvLinkAction *
+get_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
+{
+ EvLinkDest *ev_dest = NULL;
+ EvLinkAction *ev_action = NULL;
+
+ ev_dest = get_djvu_link_dest (djvu_document, link_name, base_page);
+
+ if (ev_dest) {
+ ev_action = ev_link_action_new_dest (ev_dest);
+ } else if (strstr(link_name, "://") != NULL) {
+ /* It's probably an URI */
+ ev_action = ev_link_action_new_external_uri (link_name);
+ } else {
+ /* FIXME: component file identifiers */
+ }
+
+ return ev_action;
+}
+
+static gchar *
+str_to_utf8 (const gchar *text)
+{
+ static const gchar *encodings_to_try[2];
+ static gint n_encodings_to_try = 0;
+ gchar *utf8_text = NULL;
+ gint i;
+
+ if (n_encodings_to_try == 0) {
+ const gchar *charset;
+ gboolean charset_is_utf8;
+
+ charset_is_utf8 = g_get_charset (&charset);
+ if (!charset_is_utf8) {
+ encodings_to_try[n_encodings_to_try++] = charset;
+ }
+
+ if (g_ascii_strcasecmp (charset, "ISO-8859-1") != 0) {
+ encodings_to_try[n_encodings_to_try++] = "ISO-8859-1";
+ }
+ }
+
+ for (i = 0; i < n_encodings_to_try; i++) {
+ utf8_text = g_convert (text, -1, "UTF-8",
+ encodings_to_try[i],
+ NULL, NULL, NULL);
+ if (utf8_text)
+ break;
+ }
+
+ return utf8_text;
+}
+
+/**
+ * Builds the index GtkTreeModel from DjVu s-expr
+ *
+ * (bookmarks
+ * ("title1" "dest1"
+ * ("title12" "dest12"
+ * ... )
+ * ... )
+ * ("title2" "dest2"
+ * ... )
+ * ... )
+ */
+static void
+build_tree (const DjvuDocument *djvu_document,
+ GtkTreeModel *model,
+ GtkTreeIter *parent,
+ miniexp_t iter)
+{
+ const char *title, *link_dest;
+ char *title_markup;
+
+ EvLinkAction *ev_action = NULL;
+ EvLink *ev_link = NULL;
+ GtkTreeIter tree_iter;
+
+ if (miniexp_car (iter) == miniexp_symbol ("bookmarks")) {
+ /* The (bookmarks) cons */
+ iter = miniexp_cdr (iter);
+ } else if ( miniexp_length (iter) >= 2 ) {
+ gchar *utf8_title = NULL;
+
+ /* An entry */
+ if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
+ if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
+
+
+ if (!g_utf8_validate (title, -1, NULL)) {
+ utf8_title = str_to_utf8 (title);
+ title_markup = g_markup_escape_text (utf8_title, -1);
+ } else {
+ title_markup = g_markup_escape_text (title, -1);
+ }
+
+ ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
+
+ if (g_str_has_suffix (link_dest, ".djvu")) {
+ /* FIXME: component file identifiers */
+ } else if (ev_action) {
+ ev_link = ev_link_new (utf8_title ? utf8_title : title, ev_action);
+ gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
+ gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
+ EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
+ EV_DOCUMENT_LINKS_COLUMN_LINK, ev_link,
+ EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
+ -1);
+ g_object_unref (ev_link);
+ } else {
+ gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
+ gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
+ EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
+ EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
+ -1);
+ }
+
+ g_free (title_markup);
+ g_free (utf8_title);
+ iter = miniexp_cddr (iter);
+ parent = &tree_iter;
+ } else {
+ goto unknown_entry;
+ }
+
+ for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
+ build_tree (djvu_document, model, parent, miniexp_car (iter));
+ }
+ return;
+
+ unknown_entry:
+ g_warning ("DjvuLibre error: Unknown entry in bookmarks");
+ return;
+}
+
+static gboolean
+get_djvu_hyperlink_area (ddjvu_pageinfo_t *page_info,
+ miniexp_t sexp,
+ EvMapping *ev_link_mapping)
+{
+ miniexp_t iter;
+
+ iter = sexp;
+
+ if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
+ && miniexp_length (iter) == 5) {
+ /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
+ int minx, miny, width, height;
+
+ iter = miniexp_cdr (iter);
+ if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
+ iter = miniexp_cdr (iter);
+ if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
+ iter = miniexp_cdr (iter);
+ if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
+ iter = miniexp_cdr (iter);
+ if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
+
+ ev_link_mapping->area.x1 = minx;
+ ev_link_mapping->area.x2 = (minx + width);
+ ev_link_mapping->area.y1 = (page_info->height - (miny + height));
+ ev_link_mapping->area.y2 = (page_info->height - miny);
+ } else if (miniexp_car (iter) == miniexp_symbol ("poly")
+ && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
+
+ /* FIXME: get bounding box since Evince doesn't support shaped links */
+ int minx = G_MAXINT, miny = G_MAXINT;
+ int maxx = G_MININT, maxy = G_MININT;
+
+ iter = miniexp_cdr(iter);
+ while (iter != miniexp_nil) {
+ int x, y;
+
+ if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
+ iter = miniexp_cdr (iter);
+ if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
+ iter = miniexp_cdr (iter);
+
+ minx = MIN (minx, x);
+ miny = MIN (miny, y);
+ maxx = MAX (maxx, x);
+ maxy = MAX (maxy, y);
+ }
+
+ ev_link_mapping->area.x1 = minx;
+ ev_link_mapping->area.x2 = maxx;
+ ev_link_mapping->area.y1 = (page_info->height - maxy);
+ ev_link_mapping->area.y2 = (page_info->height - miny);
+ } else {
+ /* unknown */
+ goto unknown_link;
+ }
+
+ return TRUE;
+
+ unknown_link:
+ g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
+ return FALSE;
+}
+
+static EvMapping *
+get_djvu_hyperlink_mapping (DjvuDocument *djvu_document,
+ int page,
+ ddjvu_pageinfo_t *page_info,
+ miniexp_t sexp)
+{
+ EvMapping *ev_link_mapping = NULL;
+ EvLinkAction *ev_action = NULL;
+ miniexp_t iter;
+ const char *url, *url_target, *comment;
+
+ ev_link_mapping = g_new (EvMapping, 1);
+
+ iter = sexp;
+
+ if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
+
+ iter = miniexp_cdr(iter);
+
+ if (miniexp_caar(iter) == miniexp_symbol("url")) {
+ if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
+ if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
+ } else {
+ if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
+ url_target = NULL;
+ }
+
+ iter = miniexp_cdr (iter);
+ if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
+
+ iter = miniexp_cdr (iter);
+ if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
+
+ iter = miniexp_cdr (iter);
+ /* FIXME: DjVu hyperlink attributes are ignored */
+
+ ev_action = get_djvu_link_action (djvu_document, url, page);
+ if (!ev_action) goto unknown_mapping;
+
+ ev_link_mapping->data = ev_link_new (comment, ev_action);
+
+ return ev_link_mapping;
+
+ unknown_mapping:
+ if (ev_link_mapping) g_free(ev_link_mapping);
+ g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
+ return NULL;
+}
+
+
+gboolean
+djvu_links_has_document_links (EvDocumentLinks *document_links)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
+ miniexp_t outline;
+
+ while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
+ djvu_handle_events (djvu_document, TRUE, NULL);
+
+ if (outline) {
+ ddjvu_miniexp_release (djvu_document->d_document, outline);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+EvMappingList *
+djvu_links_get_links (EvDocumentLinks *document_links,
+ gint page,
+ double scale_factor)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
+ GList *retval = NULL;
+ miniexp_t page_annotations = miniexp_nil;
+ miniexp_t *hyperlinks = NULL, *iter = NULL;
+ EvMapping *ev_link_mapping;
+ ddjvu_pageinfo_t page_info;
+
+ while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
+ djvu_handle_events (djvu_document, TRUE, NULL);
+
+ while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
+ djvu_handle_events(djvu_document, TRUE, NULL);
+
+ if (page_annotations) {
+ hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
+ if (hyperlinks) {
+ for (iter = hyperlinks; *iter; ++iter) {
+ ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
+ if (ev_link_mapping) {
+ ev_link_mapping->area.x1 *= scale_factor;
+ ev_link_mapping->area.x2 *= scale_factor;
+ ev_link_mapping->area.y1 *= scale_factor;
+ ev_link_mapping->area.y2 *= scale_factor;
+ retval = g_list_prepend (retval, ev_link_mapping);
+ }
+ }
+ free (hyperlinks);
+ }
+ ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
+ }
+
+ return ev_mapping_list_new (page, retval, (GDestroyNotify)g_object_unref);
+}
+
+EvLinkDest *
+djvu_links_find_link_dest (EvDocumentLinks *document_links,
+ const gchar *link_name)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
+ EvLinkDest *ev_dest = NULL;
+
+ ev_dest = get_djvu_link_dest (djvu_document, link_name, -1);
+
+ if (!ev_dest) {
+ g_warning ("DjvuLibre error: unknown link destination %s", link_name);
+ }
+
+ return ev_dest;
+}
+
+GtkTreeModel *
+djvu_links_get_links_model (EvDocumentLinks *document_links)
+{
+ DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
+ GtkTreeModel *model = NULL;
+ miniexp_t outline = miniexp_nil;
+
+ while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
+ djvu_handle_events (djvu_document, TRUE, NULL);
+
+ if (outline) {
+ model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
+ G_TYPE_STRING,
+ G_TYPE_OBJECT,
+ G_TYPE_BOOLEAN,
+ G_TYPE_STRING);
+ build_tree (djvu_document, model, NULL, outline);
+
+ ddjvu_miniexp_release (djvu_document->d_document, outline);
+ }
+
+ return model;
+}
diff --git a/backend/djvu/djvu-links.h b/backend/djvu/djvu-links.h
new file mode 100644
index 00000000..76d9072c
--- /dev/null
+++ b/backend/djvu/djvu-links.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2006 Pauli Virtanen <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __DJVU_LINK_H__
+#define __DJVU_LINK_H__
+
+#include "ev-document-links.h"
+#include "djvu-document.h"
+
+#include <glib.h>
+
+GtkTreeModel *djvu_links_get_links_model (EvDocumentLinks *document_links);
+EvMappingList *djvu_links_get_links (EvDocumentLinks *document_links,
+ gint page,
+ double scale_factor);
+EvLinkDest *djvu_links_find_link_dest (EvDocumentLinks *document_links,
+ const gchar *link_name);
+gboolean djvu_links_has_document_links (EvDocumentLinks *document_links);
+
+#endif /* __DJVU_LINK_H__ */
diff --git a/backend/djvu/djvu-text-page.c b/backend/djvu/djvu-text-page.c
new file mode 100644
index 00000000..3f171d1e
--- /dev/null
+++ b/backend/djvu/djvu-text-page.c
@@ -0,0 +1,445 @@
+/*
+ * Implements search and copy functionality for Djvu files.
+ * Copyright (C) 2006 Michael Hofmann <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <config.h>
+#include <string.h>
+#include <glib.h>
+#include <libdjvu/miniexp.h>
+#include "djvu-text-page.h"
+
+
+/**
+ * djvu_text_page_selection_process:
+ * @page: #DjvuTextPage instance
+ * @p: s-expression to append
+ * @delimit: character/word/... delimiter
+ *
+ * Appends the string in @p to the page text.
+ *
+ * Returns: whether the end was not reached in this s-expression
+ */
+static gboolean
+djvu_text_page_selection_process (DjvuTextPage *page,
+ miniexp_t p,
+ int delimit)
+{
+ if (page->text || p == page->start) {
+ char *token_text = (char *) miniexp_to_str (miniexp_nth (5, p));
+ if (page->text) {
+ char *new_text =
+ g_strjoin (delimit & 2 ? "\n" :
+ delimit & 1 ? " " : NULL,
+ page->text, token_text,
+ NULL);
+ g_free (page->text);
+ page->text = new_text;
+ } else
+ page->text = g_strdup (token_text);
+ if (p == page->end)
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/**
+ * djvu_text_page_selection:
+ * @page: #DjvuTextPage instance
+ * @p: tree to append
+ * @delimit: character/word/... delimiter
+ *
+ * Walks the tree in @p and appends the text with
+ * djvu_text_page_selection_process() for all s-expressions
+ * between the start and end fields.
+ *
+ * Returns: whether the end was not reached in this subtree
+ */
+static gboolean
+djvu_text_page_selection (DjvuTextPage *page,
+ miniexp_t p,
+ int delimit)
+{
+ g_return_val_if_fail (miniexp_consp (p) && miniexp_symbolp
+ (miniexp_car (p)), FALSE);
+
+ if (miniexp_car (p) != page->char_symbol)
+ delimit |= miniexp_car (p) == page->word_symbol ? 1 : 2;
+
+ miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
+ while (deeper != miniexp_nil) {
+ miniexp_t str = miniexp_car (deeper);
+ if (miniexp_stringp (str)) {
+ if (!djvu_text_page_selection_process
+ (page, p, delimit))
+ return FALSE;
+ } else {
+ if (!djvu_text_page_selection
+ (page, str, delimit))
+ return FALSE;
+ }
+ delimit = 0;
+ deeper = miniexp_cdr (deeper);
+ }
+ return TRUE;
+}
+
+static void
+djvu_text_page_limits_process (DjvuTextPage *page,
+ miniexp_t p,
+ EvRectangle *rect)
+{
+ EvRectangle current;
+
+ current.x1 = miniexp_to_int (miniexp_nth (1, p));
+ current.y1 = miniexp_to_int (miniexp_nth (2, p));
+ current.x2 = miniexp_to_int (miniexp_nth (3, p));
+ current.y2 = miniexp_to_int (miniexp_nth (4, p));
+ if (current.x2 >= rect->x1 && current.y1 <= rect->y2 &&
+ current.x1 <= rect->x2 && current.y2 >= rect->y1) {
+ if (page->start == miniexp_nil)
+ page->start = p;
+ page->end = p;
+ }
+}
+
+
+static void
+djvu_text_page_limits (DjvuTextPage *page,
+ miniexp_t p,
+ EvRectangle *rect)
+{
+ g_return_if_fail (miniexp_consp (p) &&
+ miniexp_symbolp (miniexp_car (p)));
+
+ miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
+ while (deeper != miniexp_nil) {
+ miniexp_t str = miniexp_car (deeper);
+ if (miniexp_stringp (str))
+ djvu_text_page_limits_process (page, p, rect);
+ else
+ djvu_text_page_limits (page, str, rect);
+
+ deeper = miniexp_cdr (deeper);
+ }
+}
+
+char *
+djvu_text_page_copy (DjvuTextPage *page,
+ EvRectangle *rectangle)
+{
+ char* text;
+
+ page->start = miniexp_nil;
+ page->end = miniexp_nil;
+ djvu_text_page_limits (page, page->text_structure, rectangle);
+ djvu_text_page_selection (page, page->text_structure, 0);
+
+ /* Do not free the string */
+ text = page->text;
+ page->text = NULL;
+
+ return text;
+}
+
+/**
+ * djvu_text_page_position:
+ * @page: #DjvuTextPage instance
+ * @position: index in the page text
+ *
+ * Returns the closest s-expression that contains the given position in
+ * the page text.
+ *
+ * Returns: closest s-expression
+ */
+static miniexp_t
+djvu_text_page_position (DjvuTextPage *page,
+ int position)
+{
+ GArray *links = page->links;
+ int low = 0;
+ int hi = links->len - 1;
+ int mid = 0;
+
+ g_return_val_if_fail (hi >= 0, miniexp_nil);
+
+ /* Shamelessly copied from GNU classpath */
+ while (low <= hi) {
+ mid = (low + hi) >> 1;
+ DjvuTextLink *link =
+ &g_array_index (links, DjvuTextLink, mid);
+ if (link->position == position)
+ break;
+ else if (link->position > position)
+ hi = --mid;
+ else
+ low = mid + 1;
+ }
+
+ return g_array_index (page->links, DjvuTextLink, mid).pair;
+}
+
+/**
+ * djvu_text_page_union:
+ * @target: first rectangle and result
+ * @source: second rectangle
+ *
+ * Calculates the bounding box of two rectangles and stores the reuslt
+ * in the first.
+ */
+static void
+djvu_text_page_union (EvRectangle *target,
+ EvRectangle *source)
+{
+ if (source->x1 < target->x1)
+ target->x1 = source->x1;
+ if (source->x2 > target->x2)
+ target->x2 = source->x2;
+ if (source->y1 < target->y1)
+ target->y1 = source->y1;
+ if (source->y2 > target->y2)
+ target->y2 = source->y2;
+}
+
+/**
+ * djvu_text_page_sexpr_process:
+ * @page: #DjvuTextPage instance
+ * @p: s-expression to append
+ * @start: first s-expression in the selection
+ * @end: last s-expression in the selection
+ *
+ * Appends the rectangle defined by @p to the internal bounding box rectangle.
+ *
+ * Returns: whether the end was not reached in this s-expression
+ */
+static gboolean
+djvu_text_page_sexpr_process (DjvuTextPage *page,
+ miniexp_t p,
+ miniexp_t start,
+ miniexp_t end)
+{
+ if (page->bounding_box || p == start) {
+ EvRectangle *new_rectangle = ev_rectangle_new ();
+ new_rectangle->x1 = miniexp_to_int (miniexp_nth (1, p));
+ new_rectangle->y1 = miniexp_to_int (miniexp_nth (2, p));
+ new_rectangle->x2 = miniexp_to_int (miniexp_nth (3, p));
+ new_rectangle->y2 = miniexp_to_int (miniexp_nth (4, p));
+ if (page->bounding_box) {
+ djvu_text_page_union (page->bounding_box,
+ new_rectangle);
+ g_free (new_rectangle);
+ } else
+ page->bounding_box = new_rectangle;
+ if (p == end)
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/**
+ * djvu_text_page_sexpr:
+ * @page: #DjvuTextPage instance
+ * @p: tree to append
+ * @start: first s-expression in the selection
+ * @end: last s-expression in the selection
+ *
+ * Walks the tree in @p and extends the rectangle with
+ * djvu_text_page_process() for all s-expressions between @start and @end.
+ *
+ * Returns: whether the end was not reached in this subtree
+ */
+static gboolean
+djvu_text_page_sexpr (DjvuTextPage *page,
+ miniexp_t p,
+ miniexp_t start,
+ miniexp_t end)
+{
+ g_return_val_if_fail (miniexp_consp (p) && miniexp_symbolp
+ (miniexp_car (p)), FALSE);
+
+ miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
+ while (deeper != miniexp_nil) {
+ miniexp_t str = miniexp_car (deeper);
+ if (miniexp_stringp (str)) {
+ if (!djvu_text_page_sexpr_process
+ (page, p, start, end))
+ return FALSE;
+ } else {
+ if (!djvu_text_page_sexpr
+ (page, str, start, end))
+ return FALSE;
+ }
+ deeper = miniexp_cdr (deeper);
+ }
+ return TRUE;
+}
+
+/**
+ * djvu_text_page_box:
+ * @page: #DjvuTextPage instance
+ * @start: first s-expression in the selection
+ * @end: last s-expression in the selection
+ *
+ * Builds a rectangle that contains all s-expressions in the given range.
+ */
+static EvRectangle *
+djvu_text_page_box (DjvuTextPage *page,
+ miniexp_t start,
+ miniexp_t end)
+{
+ page->bounding_box = NULL;
+ djvu_text_page_sexpr (page, page->text_structure, start, end);
+ return page->bounding_box;
+}
+
+/**
+ * djvu_text_page_append_search:
+ * @page: #DjvuTextPage instance
+ * @p: tree to append
+ * @case_sensitive: do not ignore case
+ * @delimit: insert spaces because of higher (sentence/paragraph/...) break
+ *
+ * Appends the tree in @p to the internal text string.
+ */
+static void
+djvu_text_page_append_text (DjvuTextPage *page,
+ miniexp_t p,
+ gboolean case_sensitive,
+ gboolean delimit)
+{
+ char *token_text;
+
+ g_return_if_fail (miniexp_consp (p) &&
+ miniexp_symbolp (miniexp_car (p)));
+
+ delimit |= page->char_symbol != miniexp_car (p);
+
+ miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
+ while (deeper != miniexp_nil) {
+ miniexp_t data = miniexp_car (deeper);
+ if (miniexp_stringp (data)) {
+ DjvuTextLink link;
+ link.position = page->text == NULL ? 0 :
+ strlen (page->text);
+ link.pair = p;
+ g_array_append_val (page->links, link);
+
+ token_text = (char *) miniexp_to_str (data);
+ if (!case_sensitive)
+ token_text = g_utf8_casefold (token_text, -1);
+ if (page->text == NULL)
+ page->text = g_strdup (token_text);
+ else {
+ char *new_text =
+ g_strjoin (delimit ? " " : NULL,
+ page->text, token_text,
+ NULL);
+ g_free (page->text);
+ page->text = new_text;
+ }
+ if (!case_sensitive)
+ g_free (token_text);
+ } else
+ djvu_text_page_append_text (page, data,
+ case_sensitive, delimit);
+ delimit = FALSE;
+ deeper = miniexp_cdr (deeper);
+ }
+}
+
+/**
+ * djvu_text_page_search:
+ * @page: #DjvuTextPage instance
+ * @text: text to search
+ *
+ * Searches the page for the given text. The results list has to be
+ * externally freed afterwards.
+ */
+void
+djvu_text_page_search (DjvuTextPage *page,
+ const char *text)
+{
+ char *haystack = page->text;
+ int search_len;
+ EvRectangle *result;
+ if (page->links->len == 0)
+ return;
+
+ search_len = strlen (text);
+ while ((haystack = strstr (haystack, text)) != NULL) {
+ int start_p = haystack - page->text;
+ miniexp_t start = djvu_text_page_position (page, start_p);
+ int end_p = start_p + search_len - 1;
+ miniexp_t end = djvu_text_page_position (page, end_p);
+ result = djvu_text_page_box (page, start, end);
+ g_assert (result);
+ page->results = g_list_prepend (page->results, result);
+ haystack = haystack + search_len;
+ }
+ page->results = g_list_reverse (page->results);
+}
+
+
+/**
+ * djvu_text_page_prepare_search:
+ * @page: #DjvuTextPage instance
+ * @case_sensitive: do not ignore case
+ *
+ * Indexes the page text and prepares the page for subsequent searches.
+ */
+void
+djvu_text_page_prepare_search (DjvuTextPage *page,
+ gboolean case_sensitive)
+{
+ djvu_text_page_append_text (page, page->text_structure,
+ case_sensitive, FALSE);
+}
+
+/**
+ * djvu_text_page_new:
+ * @text: S-expression of the page text
+ *
+ * Creates a new page to search.
+ *
+ * Returns: new #DjvuTextPage instance
+ */
+DjvuTextPage *
+djvu_text_page_new (miniexp_t text)
+{
+ DjvuTextPage *page;
+
+ page = g_new0 (DjvuTextPage, 1);
+ page->links = g_array_new (FALSE, FALSE, sizeof (DjvuTextLink));
+ page->char_symbol = miniexp_symbol ("char");
+ page->word_symbol = miniexp_symbol ("word");
+ page->text_structure = text;
+ return page;
+}
+
+/**
+ * djvu_text_page_free:
+ * @page: #DjvuTextPage instance
+ *
+ * Frees the given #DjvuTextPage instance.
+ */
+void
+djvu_text_page_free (DjvuTextPage *page)
+{
+ g_free (page->text);
+ g_array_free (page->links, TRUE);
+ g_free (page);
+}
diff --git a/backend/djvu/djvu-text-page.h b/backend/djvu/djvu-text-page.h
new file mode 100644
index 00000000..6e16f259
--- /dev/null
+++ b/backend/djvu/djvu-text-page.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2006 Michael Hofmann <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __DJVU_TEXT_PAGE_H__
+#define __DJVU_TEXT_PAGE_H__
+
+#include "ev-document.h"
+
+#include <string.h>
+#include <glib.h>
+#include <libdjvu/miniexp.h>
+
+
+typedef struct _DjvuTextPage DjvuTextPage;
+typedef struct _DjvuTextLink DjvuTextLink;
+
+struct _DjvuTextPage {
+ char *text;
+ GArray *links;
+ GList *results;
+ miniexp_t char_symbol;
+ miniexp_t word_symbol;
+ EvRectangle *bounding_box;
+ miniexp_t text_structure;
+ miniexp_t start;
+ miniexp_t end;
+};
+
+struct _DjvuTextLink {
+ int position;
+ miniexp_t pair;
+};
+
+char * djvu_text_page_copy (DjvuTextPage *page,
+ EvRectangle *rectangle);
+void djvu_text_page_prepare_search (DjvuTextPage *page,
+ gboolean case_sensitive);
+void djvu_text_page_search (DjvuTextPage *page,
+ const char *text);
+DjvuTextPage* djvu_text_page_new (miniexp_t text);
+void djvu_text_page_free (DjvuTextPage *page);
+
+#endif /* __DJVU_TEXT_PAGE_H__ */
+
diff --git a/backend/djvu/djvudocument.evince-backend.in b/backend/djvu/djvudocument.evince-backend.in
new file mode 100644
index 00000000..485af118
--- /dev/null
+++ b/backend/djvu/djvudocument.evince-backend.in
@@ -0,0 +1,4 @@
+[Evince Backend]
+Module=djvudocument
+_TypeDescription=DjVu Documents
+MimeType=image/vnd.djvu