* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
+#include "config.h"
+
+#include <config.h>
#include "djvu-document.h"
#include "djvu-text.h"
#include "djvu-links.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 <gdk-pixbuf/gdk-pixbuf-core.h>
#include <glib/gi18n.h>
static void djvu_document_file_exporter_iface_init (EvFileExporterIface *iface);
static void djvu_document_find_iface_init (EvDocumentFindIface *iface);
static void djvu_document_document_links_iface_init (EvDocumentLinksIface *iface);
+static void djvu_selection_iface_init (EvSelectionIface *iface);
-G_DEFINE_TYPE_WITH_CODE
- (DjvuDocument, djvu_document, G_TYPE_OBJECT,
+EV_BACKEND_REGISTER_WITH_CODE (DjvuDocument, djvu_document,
{
- 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_FILE_EXPORTER, djvu_document_file_exporter_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);
+ 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);
});
-void
-djvu_handle_events (DjvuDocument *djvu_document, int wait)
+#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_static_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 (error, EV_DJVU_ERROR, 0, error_str);
+ } else {
+ g_warning (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;
return;
if (wait)
- msg = ddjvu_message_wait (ctx);
+ 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);
- break;
- default:
- break;
- }
+ 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,
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);
djvu_document->d_document = doc;
- while (!ddjvu_document_decoding_done (djvu_document->d_document))
- djvu_handle_events (djvu_document, TRUE);
+ djvu_wait_for_message (djvu_document, DDJVU_DOCINFO, &djvu_error);
+ if (djvu_error) {
+ g_set_error (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 (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);
}
static void
-djvu_document_get_page_size (EvDocument *document,
- int page,
- double *width,
- double *height)
+document_get_page_size (DjvuDocument *djvu_document,
+ gint page,
+ double *width,
+ double *height)
{
- DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
- ddjvu_pageinfo_t info;
+ 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);
+ djvu_handle_events(djvu_document, TRUE, NULL);
if (r >= DDJVU_JOB_FAILED)
- djvu_handle_events(djvu_document, TRUE);
+ 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, *rotated_surface;
+ cairo_surface_t *surface;
gchar *pixels;
gint rowstride;
ddjvu_rect_t rrect;
ddjvu_rect_t prect;
ddjvu_page_t *d_page;
- double page_width, page_height;
+ ddjvu_page_rotation_t rotation;
+ double page_width, page_height, tmp;
static const cairo_user_data_key_t key;
- d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page);
+ 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);
+ 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;
+ }
+#ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
+ rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, page_width);
+#else
rowstride = page_width * 4;
+#endif
pixels = (gchar *) g_malloc (page_height * rowstride);
- surface = cairo_image_surface_create_for_data (pixels,
- CAIRO_FORMAT_ARGB32,
+ surface = cairo_image_surface_create_for_data ((guchar *)pixels,
+ CAIRO_FORMAT_RGB24,
page_width,
page_height,
rowstride);
prect.h = page_height;
rrect = prect;
+ ddjvu_page_set_rotation (d_page, rotation);
+
ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
&prect,
&rrect,
rowstride,
pixels);
- rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
- page_width,
- page_height,
- rc->rotation);
- cairo_surface_destroy (surface);
-
- return rotated_surface;
+ return surface;
}
static void
{
DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
+ if (djvu_document->search)
+ djvu_text_free (djvu_document->search);
+
if (djvu_document->d_document)
ddjvu_document_release (djvu_document->d_document);
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)
{
{
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 = djvu_document_render;
iface->get_info = djvu_document_get_info;
}
+static gchar *
+djvu_selection_get_selected_text (EvSelection *selection,
+ EvRenderContext *rc,
+ 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),
+ rc->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, rc->page->index, &rectangle);
+
+ if (text == NULL)
+ text = g_strdup ("");
+
+ return text;
+}
+
+static void
+djvu_selection_iface_init (EvSelectionIface *iface)
+{
+ iface->get_selected_text = djvu_selection_get_selected_text;
+}
+
static void
djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
EvRenderContext *rc,
gdk_pixbuf_fill (pixbuf, 0xffffffff);
pixels = gdk_pixbuf_get_pixels (pixbuf);
- while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK)
- djvu_handle_events(djvu_document, TRUE);
+ 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,
+ ddjvu_thumbnail_render (djvu_document->d_document, rc->page->index,
&thumb_width, &thumb_height,
djvu_document->thumbs_format,
gdk_pixbuf_get_rowstride (pixbuf),
}
/* EvFileExporterIface */
-static gboolean
-djvu_document_file_exporter_format_supported (EvFileExporter *exporter,
- EvFileExporterFormat format)
-{
- return (format == EV_FILE_FORMAT_PS); // only exporting to PS is implemented.
-}
-
static void
-djvu_document_file_exporter_begin (EvFileExporter *exporter,
- EvFileExporterFormat format,
- const char *filename, /* for storing the temp ps file */
- int first_page,
- int last_page,
- double width,
- double height,
- gboolean duplex)
+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(filename);
+ djvu_document->ps_filename = g_strdup (fc->filename);
- g_string_assign(djvu_document->opts, "-page=");
+ g_string_assign (djvu_document->opts, "-page=");
}
static void
{
DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
- g_string_append_printf(djvu_document->opts, "%d,", (rc->page) + 1);
+ g_string_append_printf (djvu_document->opts, "%d,", (rc->page->index) + 1);
}
static void
DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
- FILE *fn = fopen(djvu_document->ps_filename, "w");
+ FILE *fn = fopen (djvu_document->ps_filename, "w");
if (fn == NULL) {
- g_warning(_("Cannot open file ā%sā."), djvu_document->ps_filename);
+ 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);
+ 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 (EvFileExporterIface *iface)
{
- iface->format_supported = djvu_document_file_exporter_format_supported;
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
if (r == NULL)
return FALSE;
- djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
- page, &width, &height);
+ document_get_page_size (djvu_document, page, &width, &height);
rectangle->x1 = r->x1 * SCALE_FACTOR;
rectangle->y1 = height - r->y2 * SCALE_FACTOR;
rectangle->x2 = r->x2 * SCALE_FACTOR;