X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;ds=inline;f=backend%2Fdjvu%2Fdjvu-document.c;h=6e1c5f3c378287d4eb9524acb4d6a9643f0e9c16;hb=f51872613abaaa42c716e2fe6a2e4695c529dc4b;hp=bf74c9a209f0bbf6680b306f0161c41fd6f59547;hpb=73dca3f3fdd7cca8a32974b989c5243bd23f7791;p=evince.git diff --git a/backend/djvu/djvu-document.c b/backend/djvu/djvu-document.c index bf74c9a2..6e1c5f3c 100644 --- a/backend/djvu/djvu-document.c +++ b/backend/djvu/djvu-document.c @@ -17,14 +17,20 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include "config.h" + +#include #include "djvu-document.h" #include "djvu-text.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 #include @@ -47,16 +53,18 @@ 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_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_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); + G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION, djvu_selection_iface_init); }); @@ -200,7 +208,7 @@ djvu_document_get_page_size (EvDocument *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); @@ -208,45 +216,80 @@ djvu_document_get_page_size (EvDocument *document, *height = info.height * SCALE_FACTOR; } -static GdkPixbuf * -djvu_document_render_pixbuf (EvDocument *document, - EvRenderContext *rc) +static cairo_surface_t * +djvu_document_render (EvDocument *document, + EvRenderContext *rc) { DjvuDocument *djvu_document = DJVU_DOCUMENT (document); - GdkPixbuf *pixbuf; - GdkPixbuf *rotated_pixbuf; - + 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); 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; + 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 ((guchar *)pixels, + CAIRO_FORMAT_RGB24, + page_width, + page_height, + rowstride); + cairo_surface_set_user_data (surface, &key, + pixels, (cairo_destroy_func_t)g_free); + 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); + ddjvu_page_set_rotation (d_page, rotation); - return rotated_pixbuf; + ddjvu_page_render (d_page, DDJVU_RENDER_COLOR, + &prect, + &rrect, + djvu_document->d_format, + rowstride, + pixels); + + return surface; } static void @@ -254,11 +297,21 @@ djvu_document_finalize (GObject *object) { 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); + + 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); @@ -272,35 +325,6 @@ djvu_document_class_init (DjvuDocumentClass *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) { @@ -316,71 +340,105 @@ 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->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, &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, - gint page, - gint suggested_width, - gint *width, - gint *height) + EvRenderContext *rc, + gint *width, + gint *height) { DjvuDocument *djvu_document = DJVU_DOCUMENT (document); - gdouble p_width, p_height; - gdouble page_ratio; + gdouble page_width, page_height; - djvu_document_get_page_size (EV_DOCUMENT(djvu_document), page, &p_width, &p_height); + djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page, + &page_width, &page_height); - page_ratio = p_height / p_width; - *width = suggested_width; - *height = (gint) (suggested_width * page_ratio); - - return; + 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, - gint page, - gint rotation, - gint width, - gboolean border) +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); - djvu_document_thumbnails_get_dimensions (document, page, width, &thumb_width, &thumb_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, page, 1) < DDJVU_JOB_OK) + while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK) djvu_handle_events(djvu_document, TRUE); - ddjvu_thumbnail_render (djvu_document->d_document, page, + ddjvu_thumbnail_render (djvu_document->d_document, rc->page, &thumb_width, &thumb_height, - djvu_document->d_format, + djvu_document->thumbs_format, gdk_pixbuf_get_rowstride (pixbuf), (gchar *)pixels); - rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation); + 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, 0, tmp_pixbuf); + + rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf); g_object_unref (tmp_pixbuf); } @@ -394,12 +452,86 @@ djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface) 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) + 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); + } + + 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->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_RGB24, 0, 0); - ddjvu_format_set_row_order (djvu_document->d_format,1); + 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; }