From 13a06349251874bd35d2f03c3fc93217cee749a2 Mon Sep 17 00:00:00 2001 From: Nickolay V. Shmyrev Date: Mon, 08 Jan 2007 12:25:31 +0000 Subject: Reorganize source tree. 2007-01-08 Nickolay V. Shmyrev * Makefile.am: * backend/Makefile.am: * backend/comics/Makefile.am: * backend/djvu/Makefile.am: * backend/dvi/Makefile.am: * backend/ev-async-renderer.c: * backend/ev-async-renderer.h: * backend/ev-attachment.c: * backend/ev-attachment.h: * backend/ev-backend-marshal.c: * backend/ev-document-factory.c: * backend/ev-document-factory.h: * backend/ev-document-find.c: * backend/ev-document-find.h: * backend/ev-document-fonts.c: * backend/ev-document-fonts.h: * backend/ev-document-images.c: * backend/ev-document-images.h: * backend/ev-document-info.h: * backend/ev-document-links.c: * backend/ev-document-links.h: * backend/ev-document-misc.c: * backend/ev-document-misc.h: * backend/ev-document-security.c: * backend/ev-document-security.h: * backend/ev-document-thumbnails.c: * backend/ev-document-thumbnails.h: * backend/ev-document-transition.c: * backend/ev-document-transition.h: * backend/ev-document.c: * backend/ev-document.h: * backend/ev-file-exporter.c: * backend/ev-file-exporter.h: * backend/ev-image.c: * backend/ev-image.h: * backend/ev-link-action.c: * backend/ev-link-action.h: * backend/ev-link-dest.c: * backend/ev-link-dest.h: * backend/ev-link.c: * backend/ev-link.h: * backend/ev-render-context.c: * backend/ev-render-context.h: * backend/ev-selection.c: * backend/ev-selection.h: * backend/impress/Makefile.am: * backend/pdf/Makefile.am: * backend/pixbuf/Makefile.am: * backend/ps/Makefile.am: * backend/ps/ps-document.c: (push_pixbuf), (interpreter_failed), (ps_document_widget_event), (setup_pixmap), (setup_page), (input), (start_interpreter), (stop_interpreter), (document_load), (ps_document_next_page), (render_page): * backend/tiff/Makefile.am: * comics/Makefile.am: * comics/comics-document.c: * comics/comics-document.h: * configure.ac: * cut-n-paste/zoom-control/ephy-zoom-control.c: * djvu/Makefile.am: * djvu/djvu-document-private.h: * djvu/djvu-document.c: * djvu/djvu-document.h: * djvu/djvu-links.c: * djvu/djvu-links.h: * djvu/djvu-text-page.c: * djvu/djvu-text-page.h: * djvu/djvu-text.c: * djvu/djvu-text.h: * dvi/Makefile.am: * dvi/dvi-document.c: * dvi/dvi-document.h: * dvi/fonts.c: * dvi/fonts.h: * dvi/mdvi-lib/Makefile.am: * dvi/mdvi-lib/afmparse.c: * dvi/mdvi-lib/afmparse.h: * dvi/mdvi-lib/bitmap.c: * dvi/mdvi-lib/bitmap.h: * dvi/mdvi-lib/color.c: * dvi/mdvi-lib/color.h: * dvi/mdvi-lib/common.c: * dvi/mdvi-lib/common.h: * dvi/mdvi-lib/defaults.h: * dvi/mdvi-lib/dvimisc.c: * dvi/mdvi-lib/dviopcodes.h: * dvi/mdvi-lib/dviread.c: * dvi/mdvi-lib/files.c: * dvi/mdvi-lib/font.c: * dvi/mdvi-lib/fontmap.c: * dvi/mdvi-lib/fontmap.h: * dvi/mdvi-lib/fontsrch.c: * dvi/mdvi-lib/gf.c: * dvi/mdvi-lib/hash.c: * dvi/mdvi-lib/hash.h: * dvi/mdvi-lib/list.c: * dvi/mdvi-lib/mdvi.h: * dvi/mdvi-lib/pagesel.c: * dvi/mdvi-lib/paper.c: * dvi/mdvi-lib/paper.h: * dvi/mdvi-lib/pk.c: * dvi/mdvi-lib/private.h: * dvi/mdvi-lib/setup.c: * dvi/mdvi-lib/sp-epsf.c: * dvi/mdvi-lib/special.c: * dvi/mdvi-lib/sysdeps.h: * dvi/mdvi-lib/t1.c: * dvi/mdvi-lib/tfm.c: * dvi/mdvi-lib/tfmfile.c: * dvi/mdvi-lib/tt.c: * dvi/mdvi-lib/util.c: * dvi/mdvi-lib/vf.c: * dvi/pixbuf-device.c: * dvi/pixbuf-device.h: * impress/Makefile.am: * impress/common.h: * impress/document.c: * impress/f_oasis.c: * impress/f_oo13.c: * impress/iksemel.c: * impress/iksemel.h: * impress/imposter.h: * impress/impress-document.c: * impress/impress-document.h: * impress/internal.h: * impress/r_back.c: * impress/r_draw.c: * impress/r_geometry.c: * impress/r_gradient.c: * impress/r_style.c: * impress/r_text.c: * impress/render.c: * impress/render.h: * impress/zip.c: * impress/zip.h: * lib/Makefile.am: * lib/ev-debug.c: * lib/ev-debug.h: * lib/ev-file-helpers.c: * lib/ev-file-helpers.h: * lib/ev-gui.c: * lib/ev-gui.h: * lib/ev-tooltip.c: * lib/ev-tooltip.h: * libdocument/Makefile.am: * libdocument/ev-file-helpers.c: * pdf/Makefile.am: * pdf/ev-poppler.cc: * pdf/ev-poppler.h: * pixbuf/Makefile.am: * pixbuf/pixbuf-document.c: * pixbuf/pixbuf-document.h: * properties/Makefile.am: * ps/Makefile.am: * ps/gsdefaults.c: * ps/gsdefaults.h: * ps/gsio.c: * ps/gsio.h: * ps/gstypes.h: * ps/ps-document.c: * ps/ps-document.h: * ps/ps.c: * ps/ps.h: * shell/Makefile.am: * shell/ev-application.h: * shell/ev-sidebar-links.c: * shell/ev-sidebar-links.h: * shell/ev-utils.c: (ev_gui_sanitise_popup_position), (ev_gui_menu_position_tree_selection): * shell/ev-utils.h: * shell/ev-view.c: (ev_view_finalize): * shell/ev-window.c: * shell/main.c: (main): * thumbnailer/Makefile.am: * tiff/Makefile.am: * tiff/tiff-document.c: * tiff/tiff-document.h: * tiff/tiff2ps.c: * tiff/tiff2ps.h: Reorganize source tree. svn path=/trunk/; revision=2197 --- (limited to 'backend/djvu') diff --git a/backend/djvu/Makefile.am b/backend/djvu/Makefile.am new file mode 100644 index 0000000..fa44bbb --- /dev/null +++ b/backend/djvu/Makefile.am @@ -0,0 +1,22 @@ +INCLUDES = \ + -I$(top_srcdir) \ + -I$(top_srcdir)/libdocument \ + -DGNOMEICONDIR=\""${prefix}/${DATADIRNAME}/pixmaps"\" \ + $(LIB_CFLAGS) \ + $(DJVU_CFLAGS) + +noinst_LTLIBRARIES = libgtkdjvu.la + +libgtkdjvu_la_SOURCES = \ + djvu-document.c \ + djvu-document.h \ + djvu-document-private.h \ + djvu-links.c \ + djvu-links.h \ + djvu-text.c \ + djvu-text.h \ + djvu-text-page.c \ + djvu-text-page.h + + + diff --git a/backend/djvu/djvu-document-private.h b/backend/djvu/djvu-document-private.h new file mode 100644 index 0000000..3fa579f --- /dev/null +++ b/backend/djvu/djvu-document-private.h @@ -0,0 +1,45 @@ +/* + * Declarations used throughout the djvu classes + * + * Copyright (C) 2006, Michael Hofmann + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __DJVU_DOCUMENT_INTERNAL_H__ +#define __DJVU_DOCUMENT_INTERNAL_H__ + +#include "djvu-document.h" +#include "djvu-text.h" + +#include + +struct _DjvuDocument { + GObject parent_instance; + + ddjvu_context_t *d_context; + ddjvu_document_t *d_document; + ddjvu_format_t *d_format; + + gchar *uri; + + DjvuText *search; +}; + +int djvu_document_get_n_pages (EvDocument *document); +void djvu_handle_events (DjvuDocument *djvu_document, + int wait); + +#endif /* __DJVU_DOCUMENT_INTERNAL_H__ */ diff --git a/backend/djvu/djvu-document.c b/backend/djvu/djvu-document.c new file mode 100644 index 0000000..ad18555 --- /dev/null +++ b/backend/djvu/djvu-document.c @@ -0,0 +1,478 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ +/* + * Copyright (C) 2005, Nickolay V. Shmyrev + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "djvu-document.h" +#include "djvu-text.h" +#include "djvu-links.h" +#include "djvu-document-private.h" +#include "ev-document-thumbnails.h" +#include "ev-document-misc.h" +#include "ev-document-find.h" +#include "ev-document-links.h" + +#include +#include +#include +#include +#include +#include + +#define SCALE_FACTOR 0.2 + +enum { + PROP_0, + PROP_TITLE +}; + +struct _DjvuDocumentClass +{ + GObjectClass parent_class; +}; + +typedef struct _DjvuDocumentClass DjvuDocumentClass; + +static void djvu_document_document_iface_init (EvDocumentIface *iface); +static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface); +static void djvu_document_find_iface_init (EvDocumentFindIface *iface); +static void djvu_document_document_links_iface_init (EvDocumentLinksIface *iface); + +G_DEFINE_TYPE_WITH_CODE + (DjvuDocument, djvu_document, G_TYPE_OBJECT, + { + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, djvu_document_document_iface_init); + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init) + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init); + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init); + }); + + +void +djvu_handle_events (DjvuDocument *djvu_document, int wait) +{ + ddjvu_context_t *ctx = djvu_document->d_context; + const ddjvu_message_t *msg; + if (!ctx) + return; + if (wait) + msg = ddjvu_message_wait (ctx); + while ((msg = ddjvu_message_peek (ctx))) { + switch (msg->m_any.tag) { + case DDJVU_ERROR: + g_warning ("DjvuLibre error: %s", + msg->m_error.message); + if (msg->m_error.filename) + g_warning ("DjvuLibre error: %s:%d", + msg->m_error.filename, + msg->m_error.lineno); + default: + break; + } + 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; + + /* 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) return FALSE; + + if (djvu_document->d_document) + ddjvu_document_release (djvu_document->d_document); + + djvu_document->d_document = doc; + + while (!ddjvu_document_decoding_done (djvu_document->d_document)) + djvu_handle_events(djvu_document, TRUE); + g_free (djvu_document->uri); + djvu_document->uri = g_strdup (uri); + + 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 +djvu_document_get_page_size (EvDocument *document, + int page, + double *width, + double *height) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document); + ddjvu_pageinfo_t info; + ddjvu_status_t r; + + g_return_if_fail (djvu_document->d_document); + + while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK) + djvu_handle_events(djvu_document, TRUE); + + if (r >= DDJVU_JOB_FAILED) + djvu_handle_events(djvu_document, TRUE); + + *width = info.width * SCALE_FACTOR; + *height = info.height * SCALE_FACTOR; +} + +static GdkPixbuf * +djvu_document_render_pixbuf (EvDocument *document, + EvRenderContext *rc) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document); + GdkPixbuf *pixbuf; + GdkPixbuf *rotated_pixbuf; + + ddjvu_rect_t rrect; + ddjvu_rect_t prect; + ddjvu_page_t *d_page; + + double page_width, page_height; + + d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page); + + while (!ddjvu_page_decoding_done (d_page)) + djvu_handle_events(djvu_document, TRUE); + + page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR; + page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR; + + pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height); + + prect.x = 0; prect.y = 0; + prect.w = page_width; prect.h = page_height; + rrect = prect; + + ddjvu_page_render(d_page, DDJVU_RENDER_COLOR, + &prect, + &rrect, + djvu_document->d_format, + gdk_pixbuf_get_rowstride (pixbuf), + (gchar *)gdk_pixbuf_get_pixels (pixbuf)); + + rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation); + g_object_unref (pixbuf); + + return rotated_pixbuf; +} + +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); + + ddjvu_context_release (djvu_document->d_context); + ddjvu_format_release (djvu_document->d_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); + + gobject_class->finalize = djvu_document_finalize; +} + +static gboolean +djvu_document_can_get_text (EvDocument *document) +{ + return TRUE; +} + + +static char * +djvu_document_get_text (EvDocument *document, int page, EvRectangle *rect) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document); + double width, height; + EvRectangle rectangle; + char* text; + + djvu_document_get_page_size (document, page, &width, &height); + rectangle.x1 = rect->x1 / SCALE_FACTOR; + rectangle.y1 = (height - rect->y2) / SCALE_FACTOR; + rectangle.x2 = rect->x2 / SCALE_FACTOR; + rectangle.y2 = (height - rect->y1) / SCALE_FACTOR; + + text = djvu_text_copy (djvu_document, page, &rectangle); + + if (text == NULL) + text = g_strdup (""); + + return text; +} + +static EvDocumentInfo * +djvu_document_get_info (EvDocument *document) +{ + EvDocumentInfo *info; + + info = g_new0 (EvDocumentInfo, 1); + + return info; +} + +static void +djvu_document_document_iface_init (EvDocumentIface *iface) +{ + iface->load = djvu_document_load; + iface->save = djvu_document_save; + iface->can_get_text = djvu_document_can_get_text; + iface->get_text = djvu_document_get_text; + iface->get_n_pages = djvu_document_get_n_pages; + iface->get_page_size = djvu_document_get_page_size; + iface->render_pixbuf = djvu_document_render_pixbuf; + iface->get_info = djvu_document_get_info; +} + +static void +djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document, + gint page, + gint suggested_width, + gint *width, + gint *height) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document); + gdouble p_width, p_height; + gdouble page_ratio; + + djvu_document_get_page_size (EV_DOCUMENT(djvu_document), page, &p_width, &p_height); + + page_ratio = p_height / p_width; + *width = suggested_width; + *height = (gint) (suggested_width * page_ratio); + + return; +} + +static GdkPixbuf * +djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document, + gint page, + gint rotation, + gint width, + gboolean border) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document); + GdkPixbuf *pixbuf, *rotated_pixbuf; + gint thumb_width, thumb_height; + + guchar *pixels; + + g_return_val_if_fail (djvu_document->d_document, NULL); + + djvu_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_height); + + 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, page, 1) < DDJVU_JOB_OK) + djvu_handle_events(djvu_document, TRUE); + + ddjvu_thumbnail_render (djvu_document->d_document, page, + &thumb_width, &thumb_height, + djvu_document->d_format, + gdk_pixbuf_get_rowstride (pixbuf), + (gchar *)pixels); + + rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation); + g_object_unref (pixbuf); + + if (border) { + GdkPixbuf *tmp_pixbuf = rotated_pixbuf; + rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf); + g_object_unref (tmp_pixbuf); + } + + return rotated_pixbuf; +} + +static void +djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface) +{ + iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail; + iface->get_dimensions = djvu_document_thumbnails_get_dimensions; +} + +static void +djvu_document_init (DjvuDocument *djvu_document) +{ + djvu_document->d_context = ddjvu_context_create ("Evince"); + djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0); + ddjvu_format_set_row_order (djvu_document->d_format,1); + + djvu_document->d_document = NULL; +} + +static void +djvu_document_find_begin (EvDocumentFind *document, + int page, + const char *search_string, + gboolean case_sensitive) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document); + + if (djvu_document->search && + strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0) + return; + + if (djvu_document->search) + djvu_text_free (djvu_document->search); + + djvu_document->search = djvu_text_new (djvu_document, + page, + case_sensitive, + search_string); +} + +static int +djvu_document_find_get_n_results (EvDocumentFind *document_find, int page) +{ + DjvuText *search = DJVU_DOCUMENT (document_find)->search; + + if (search) { + return djvu_text_n_results (search, page); + } else { + return 0; + } +} + +static gboolean +djvu_document_find_get_result (EvDocumentFind *document_find, + int page, + int n_result, + EvRectangle *rectangle) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find); + DjvuText *search = djvu_document->search; + EvRectangle *r; + double width, height; + + if (search == NULL) + return FALSE; + + r = djvu_text_get_result (search, page, n_result); + if (r == NULL) + return FALSE; + + djvu_document_get_page_size (EV_DOCUMENT (djvu_document), + page, &width, &height); + rectangle->x1 = r->x1 * SCALE_FACTOR; + rectangle->y1 = height - r->y2 * SCALE_FACTOR; + rectangle->x2 = r->x2 * SCALE_FACTOR; + rectangle->y2 = height - r->y1 * SCALE_FACTOR; + + return TRUE; +} + +static int +djvu_document_find_page_has_results (EvDocumentFind *document_find, + int page) +{ + DjvuText *search = DJVU_DOCUMENT (document_find)->search; + + return search && djvu_text_has_results (search, page); +} + +static double +djvu_document_find_get_progress (EvDocumentFind *document_find) +{ + DjvuText *search = DJVU_DOCUMENT (document_find)->search; + + if (search == NULL) { + return 0; + } + + return djvu_text_get_progress (search); +} + +static void +djvu_document_find_cancel (EvDocumentFind *document) +{ + DjvuDocument *djvu_document = DJVU_DOCUMENT (document); + + if (djvu_document->search) { + djvu_text_free (djvu_document->search); + djvu_document->search = NULL; + } +} + +static void +djvu_document_find_iface_init (EvDocumentFindIface *iface) +{ + iface->begin = djvu_document_find_begin; + iface->get_n_results = djvu_document_find_get_n_results; + iface->get_result = djvu_document_find_get_result; + iface->page_has_results = djvu_document_find_page_has_results; + iface->get_progress = djvu_document_find_get_progress; + iface->cancel = djvu_document_find_cancel; +} + +static GList * +djvu_document_links_get_links (EvDocumentLinks *document_links, + gint page) +{ + return djvu_links_get_links (document_links, page, SCALE_FACTOR); +} + +static void +djvu_document_document_links_iface_init (EvDocumentLinksIface *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 0000000..402f476 --- /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 + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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; + +DjvuDocument *djvu_document_new (void); + +GType djvu_document_get_type (void) G_GNUC_CONST; + +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 0000000..38fad0d --- /dev/null +++ b/backend/djvu/djvu-links.c @@ -0,0 +1,393 @@ +/* -*- 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 + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "djvu-document.h" +#include "djvu-links.h" +#include "djvu-document-private.h" +#include "ev-document-links.h" + +#include +#include +#include +#include + +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; + gchar *end_ptr; + + /* #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; +} + +/** + * 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 ) { + /* 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; + + 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 (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); + + 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, + EvLinkMapping *ev_link_mapping) +{ + miniexp_t iter; + ddjvu_pageinfo_t info; + + 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->x1 = minx; + ev_link_mapping->x2 = (minx + width); + ev_link_mapping->y1 = (page_info->height - (miny + height)); + ev_link_mapping->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->x1 = minx; + ev_link_mapping->x2 = maxx; + ev_link_mapping->y1 = (page_info->height - maxy); + ev_link_mapping->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 EvLinkMapping * +get_djvu_hyperlink_mapping (DjvuDocument *djvu_document, + int page, + ddjvu_pageinfo_t *page_info, + miniexp_t sexp) +{ + EvLinkMapping *ev_link_mapping = NULL; + EvLinkAction *ev_action = NULL; + miniexp_t iter; + const char *url, *url_target, *comment; + + ev_link_mapping = g_new (EvLinkMapping, 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->link = 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); + + if (outline) { + ddjvu_miniexp_release (djvu_document->d_document, outline); + return TRUE; + } + + return FALSE; +} + +GList * +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; + EvLinkMapping *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); + + while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK) + djvu_handle_events(djvu_document, TRUE); + + 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->x1 *= scale_factor; + ev_link_mapping->x2 *= scale_factor; + ev_link_mapping->y1 *= scale_factor; + ev_link_mapping->y2 *= scale_factor; + retval = g_list_prepend (retval, ev_link_mapping); + } + } + free (hyperlinks); + } + ddjvu_miniexp_release (djvu_document->d_document, page_annotations); + } + + return retval; +} + +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 (document_links), 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); + + if (outline) { + model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS, + G_TYPE_STRING, + G_TYPE_OBJECT, + G_TYPE_BOOLEAN); + 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 0000000..fdbfdeb --- /dev/null +++ b/backend/djvu/djvu-links.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2006 Pauli Virtanen + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __DJVU_LINK_H__ +#define __DJVU_LINK_H__ + +#include "ev-document-links.h" +#include "djvu-document.h" + +#include + +GtkTreeModel *djvu_links_get_links_model (EvDocumentLinks *document_links); +GList *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 0000000..c19d6f6 --- /dev/null +++ b/backend/djvu/djvu-text-page.c @@ -0,0 +1,444 @@ +/* + * Implements search and copy functionality for Djvu files. + * Copyright (C) 2006 Michael Hofmann + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "djvu-text-page.h" + +#include + +/** + * 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) +{ + char *token_text; + + 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 = g_new (EvRectangle, 1); + 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, + 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 0000000..db53326 --- /dev/null +++ b/backend/djvu/djvu-text-page.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2006 Michael Hofmann + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __DJVU_TEXT_PAGE_H__ +#define __DJVU_TEXT_PAGE_H__ + +#include "ev-document.h" + +#include +#include + +#include + +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, + 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/djvu-text.c b/backend/djvu/djvu-text.c new file mode 100644 index 0000000..beaac6b --- /dev/null +++ b/backend/djvu/djvu-text.c @@ -0,0 +1,298 @@ +/* + * Implements search and copy functionality for Djvu files. + * Copyright (C) 2006 Michael Hofmann + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "djvu-document-private.h" +#include "djvu-document.h" +#include "djvu-text.h" +#include "djvu-text-page.h" +#include "ev-document-find.h" +#include "ev-document.h" + +#include +#include + +struct _DjvuText { + DjvuDocument *document; + gboolean case_sensitive; + char *text; + GList **pages; + guint idle; + int start_page; + int search_page; +}; + +/** + * djvu_text_idle_callback: + * @data: #DjvuText instance + * + * Idle callback that processes one page at a time. + * + * Returns: whether there are more pages to be processed + */ +static gboolean +djvu_text_idle_callback (void *data) +{ + DjvuText *djvu_text = (DjvuText *) data; + DjvuDocument *djvu_document = djvu_text->document; + int n_pages; + miniexp_t page_text; + + ev_document_doc_mutex_lock (); + while ((page_text = + ddjvu_document_get_pagetext (djvu_document->d_document, + djvu_text->search_page, + "char")) == miniexp_dummy) + djvu_handle_events (djvu_document, TRUE); + + if (page_text != miniexp_nil) { + DjvuTextPage *page = djvu_text_page_new (page_text); + djvu_text_page_prepare_search (page, djvu_text->case_sensitive); + if (page->links->len > 0) { + djvu_text_page_search (page, djvu_text->text); + djvu_text->pages[djvu_text->search_page] = page->results; + ev_document_find_changed (EV_DOCUMENT_FIND + (djvu_document), + djvu_text->search_page); + } + djvu_text_page_free (page); + ddjvu_miniexp_release (djvu_document->d_document, + page_text); + } + ev_document_doc_mutex_unlock (); + + n_pages = + djvu_document_get_n_pages (EV_DOCUMENT (djvu_text->document)); + djvu_text->search_page += 1; + if (djvu_text->search_page == n_pages) { + /* wrap around */ + djvu_text->search_page = 0; + } + + if (djvu_text->search_page != djvu_text->start_page) + return TRUE; + + /* We're done. */ + djvu_text->idle = 0; + /* will return FALSE to remove */ + return FALSE; +} + +/** + * djvu_text_new: + * @djvu_document: document to search + * @start_page: first page to search + * @case_sensitive: uses g_utf8_case_fold() to enable case-insensitive + * searching + * @text: text to search + * + * Creates a new #DjvuText instance to enable searching. An idle call + * is used to process all pages starting from @start_page. + * + * Returns: newly created instance + */ +DjvuText * +djvu_text_new (DjvuDocument *djvu_document, + int start_page, + gboolean case_sensitive, + const char *text) +{ + DjvuText *djvu_text; + int n_pages; + int i; + + n_pages = djvu_document_get_n_pages (EV_DOCUMENT (djvu_document)); + + djvu_text = g_new0 (DjvuText, 1); + + if (case_sensitive) + djvu_text->text = g_strdup (text); + else + djvu_text->text = g_utf8_casefold (text, -1); + djvu_text->pages = g_new0 (GList *, n_pages); + for (i = 0; i < n_pages; i++) { + djvu_text->pages[i] = NULL; + } + + djvu_text->document = djvu_document; + + /* We add at low priority so the progress bar repaints */ + djvu_text->idle = g_idle_add_full (G_PRIORITY_LOW, + djvu_text_idle_callback, + djvu_text, NULL); + + djvu_text->case_sensitive = case_sensitive; + djvu_text->start_page = start_page; + djvu_text->search_page = start_page; + + return djvu_text; +} + +/** + * djvu_text_copy: + * @djvu_document: document to search + * @page: page to search + * @rectangle: rectangle to copy + * + * Copies and returns the text in the given rectangle. + * + * Returns: newly allocated text or NULL of none is available + */ +char * +djvu_text_copy (DjvuDocument *djvu_document, + int page, + EvRectangle *rectangle) +{ + miniexp_t page_text; + char* text = NULL; + + while ((page_text = + ddjvu_document_get_pagetext (djvu_document->d_document, + page, "char")) == miniexp_dummy) + djvu_handle_events (djvu_document, TRUE); + + 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; +} + +/** + * djvu_text_free: + * @djvu_text: instance to free + * + * Frees the given #DjvuText instance. + */ +void djvu_text_free (DjvuText * djvu_text) +{ + DjvuDocument *djvu_document = djvu_text->document; + int n_pages; + int i; + + if (djvu_text->idle != 0) + g_source_remove (djvu_text->idle); + + n_pages = djvu_document_get_n_pages (EV_DOCUMENT (djvu_document)); + for (i = 0; i < n_pages; i++) { + g_list_foreach (djvu_text->pages[i], (GFunc) g_free, NULL); + g_list_free (djvu_text->pages[i]); + } + + g_free (djvu_text->text); +} + +/** + * djvu_text_get_text: + * @djvu_text: #DjvuText instance + * + * Returns the search text. This is mainly to be able to avoid reinstantiation + * for the same search text. + * + * Returns: the text this instance of #DjvuText is looking for + */ +const char * +djvu_text_get_text (DjvuText *djvu_text) +{ + return djvu_text->text; +} + +/** + * djvu_text_n_results: + * @djvu_text: #DjvuText instance + * @page: page number + * + * Returns the number of search results available for the given page. + * + * Returns: number of search results + */ +int +djvu_text_n_results (DjvuText *djvu_text, + int page) +{ + return g_list_length (djvu_text->pages[page]); +} + +/** + * djvu_text_has_results: + * @djvu_text: #DjvuText instance + * @page: page number + * + * Returns whether there are search results available for the given page. + * This method executes faster than djvu_text_n_results(). + * + * Returns: whether there are search results + */ +int +djvu_text_has_results (DjvuText *djvu_text, + int page) +{ + return djvu_text->pages[page] != NULL; +} + +/** + * djvu_text_get_result: + * @djvu_text: #DjvuText instance + * @page: page number + * @n_result: result number + * + * Returns the n-th search result of a given page. The coordinates are + * Djvu-specific and need to be processed to be compatible with the Evince + * coordinate system. The result may span several lines! + * + * Returns: the rectangle for the search result + */ +EvRectangle * +djvu_text_get_result (DjvuText *djvu_text, + int page, + int n_result) +{ + return (EvRectangle *) g_list_nth_data (djvu_text->pages[page], + n_result); +} + +/** + * djvu_text_get_progress: + * @djvu_text: #DjvuText instance + * + * Returns the percentage of pages done searching. + * + * Returns: the progress as value between 0 and 1 + */ +double +djvu_text_get_progress (DjvuText *djvu_text) +{ + int pages_done; + int n_pages; + + n_pages = + djvu_document_get_n_pages (EV_DOCUMENT (djvu_text->document)); + if (djvu_text->search_page > djvu_text->start_page) { + pages_done = djvu_text->search_page - djvu_text->start_page + 1; + } else if (djvu_text->search_page == djvu_text->start_page) { + pages_done = n_pages; + } else { + pages_done = + n_pages - djvu_text->start_page + djvu_text->search_page; + } + return pages_done / (double) n_pages; +} + diff --git a/backend/djvu/djvu-text.h b/backend/djvu/djvu-text.h new file mode 100644 index 0000000..0f99643 --- /dev/null +++ b/backend/djvu/djvu-text.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2006 Michael Hofmann + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __DJVU_TEXT_H__ +#define __DJVU_TEXT_H__ + +#include "ev-document.h" + +#include +#include +#include + +typedef struct _DjvuText DjvuText; + +DjvuText *djvu_text_new (DjvuDocument *djvu_document, + int start_page, + gboolean case_sensitive, + const char *text); +const char *djvu_text_get_text (DjvuText *djvu_text); +int djvu_text_n_results (DjvuText *djvu_text, + int page); +EvRectangle *djvu_text_get_result (DjvuText *djvu_text, + int page, + int n_result); +int djvu_text_has_results (DjvuText *djvu_text, + int page); +double djvu_text_get_progress (DjvuText *djvu_text); +char *djvu_text_copy (DjvuDocument *djvu_document, + int page, + EvRectangle *rectangle); + +#endif /* __DJVU_TEXT_H__ */ -- cgit v0.9.1