1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Nickolay V. Shmyrev <nshmyrev@yandex.ru>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "djvu-document.h"
21 #include "djvu-text.h"
22 #include "djvu-links.h"
23 #include "djvu-document-private.h"
24 #include "ev-document-thumbnails.h"
25 #include "ev-file-exporter.h"
26 #include "ev-document-misc.h"
27 #include "ev-document-find.h"
28 #include "ev-document-links.h"
29 #include "ev-selection.h"
31 #include <gdk-pixbuf/gdk-pixbuf-core.h>
32 #include <glib/gi18n.h>
33 #include <glib/gunicode.h>
36 #define SCALE_FACTOR 0.2
43 struct _DjvuDocumentClass
45 GObjectClass parent_class;
48 typedef struct _DjvuDocumentClass DjvuDocumentClass;
50 static void djvu_document_document_iface_init (EvDocumentIface *iface);
51 static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
52 static void djvu_document_file_exporter_iface_init (EvFileExporterIface *iface);
53 static void djvu_document_find_iface_init (EvDocumentFindIface *iface);
54 static void djvu_document_document_links_iface_init (EvDocumentLinksIface *iface);
55 static void djvu_selection_iface_init (EvSelectionIface *iface);
57 G_DEFINE_TYPE_WITH_CODE
58 (DjvuDocument, djvu_document, G_TYPE_OBJECT,
60 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, djvu_document_document_iface_init);
61 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init);
62 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, djvu_document_file_exporter_iface_init);
63 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init);
64 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init);
65 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION, djvu_selection_iface_init);
70 djvu_handle_events (DjvuDocument *djvu_document, int wait)
72 ddjvu_context_t *ctx = djvu_document->d_context;
73 const ddjvu_message_t *msg;
79 msg = ddjvu_message_wait (ctx);
81 while ((msg = ddjvu_message_peek (ctx))) {
82 switch (msg->m_any.tag) {
84 g_warning ("DjvuLibre error: %s",
85 msg->m_error.message);
86 if (msg->m_error.filename)
87 g_warning ("DjvuLibre error: %s:%d",
88 msg->m_error.filename,
94 ddjvu_message_pop (ctx);
99 djvu_document_load (EvDocument *document,
103 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
104 ddjvu_document_t *doc;
106 gboolean missing_files = FALSE;
108 /* FIXME: We could actually load uris */
109 filename = g_filename_from_uri (uri, NULL, error);
113 doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
120 if (djvu_document->d_document)
121 ddjvu_document_release (djvu_document->d_document);
123 djvu_document->d_document = doc;
125 while (!ddjvu_document_decoding_done (djvu_document->d_document))
126 djvu_handle_events (djvu_document, TRUE);
127 g_free (djvu_document->uri);
128 djvu_document->uri = g_strdup (uri);
130 if (ddjvu_document_get_type (djvu_document->d_document) == DDJVU_DOCTYPE_INDIRECT) {
135 base = g_path_get_dirname (filename);
137 n_files = ddjvu_document_get_filenum (djvu_document->d_document);
138 for (i = 0; i < n_files; i++) {
139 struct ddjvu_fileinfo_s fileinfo;
142 ddjvu_document_get_fileinfo (djvu_document->d_document,
145 if (fileinfo.type != 'P')
148 file = g_build_filename (base, fileinfo.id, NULL);
149 if (!g_file_test (file, G_FILE_TEST_EXISTS)) {
150 missing_files = TRUE;
165 _("The document is composed by several files. "
166 "One or more of such files cannot be accessed."));
176 djvu_document_save (EvDocument *document,
180 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
182 return ev_xfer_uri_simple (djvu_document->uri, uri, error);
186 djvu_document_get_n_pages (EvDocument *document)
188 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
190 g_return_val_if_fail (djvu_document->d_document, 0);
192 return ddjvu_document_get_pagenum (djvu_document->d_document);
196 djvu_document_get_page_size (EvDocument *document,
201 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
202 ddjvu_pageinfo_t info;
205 g_return_if_fail (djvu_document->d_document);
207 while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
208 djvu_handle_events(djvu_document, TRUE);
210 if (r >= DDJVU_JOB_FAILED)
211 djvu_handle_events(djvu_document, TRUE);
213 *width = info.width * SCALE_FACTOR;
214 *height = info.height * SCALE_FACTOR;
217 static cairo_surface_t *
218 djvu_document_render (EvDocument *document,
221 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
222 cairo_surface_t *surface;
227 ddjvu_page_t *d_page;
228 ddjvu_page_rotation_t rotation;
229 double page_width, page_height, tmp;
230 static const cairo_user_data_key_t key;
232 d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page);
234 while (!ddjvu_page_decoding_done (d_page))
235 djvu_handle_events(djvu_document, TRUE);
237 page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
238 page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
240 switch (rc->rotation) {
242 rotation = DDJVU_ROTATE_90;
244 page_height = page_width;
249 rotation = DDJVU_ROTATE_180;
253 rotation = DDJVU_ROTATE_270;
255 page_height = page_width;
260 rotation = DDJVU_ROTATE_0;
263 rowstride = page_width * 4;
264 pixels = (gchar *) g_malloc (page_height * rowstride);
265 surface = cairo_image_surface_create_for_data (pixels,
270 cairo_surface_set_user_data (surface, &key,
271 pixels, (cairo_destroy_func_t)g_free);
274 prect.w = page_width;
275 prect.h = page_height;
278 ddjvu_page_set_rotation (d_page, rotation);
280 ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
283 djvu_document->d_format,
291 djvu_document_finalize (GObject *object)
293 DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
295 if (djvu_document->search)
296 djvu_text_free (djvu_document->search);
298 if (djvu_document->d_document)
299 ddjvu_document_release (djvu_document->d_document);
301 if (djvu_document->opts)
302 g_string_free (djvu_document->opts, TRUE);
304 if (djvu_document->ps_filename)
305 g_free (djvu_document->ps_filename);
307 ddjvu_context_release (djvu_document->d_context);
308 ddjvu_format_release (djvu_document->d_format);
309 ddjvu_format_release (djvu_document->thumbs_format);
310 g_free (djvu_document->uri);
312 G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
316 djvu_document_class_init (DjvuDocumentClass *klass)
318 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
320 gobject_class->finalize = djvu_document_finalize;
323 static EvDocumentInfo *
324 djvu_document_get_info (EvDocument *document)
326 EvDocumentInfo *info;
328 info = g_new0 (EvDocumentInfo, 1);
334 djvu_document_document_iface_init (EvDocumentIface *iface)
336 iface->load = djvu_document_load;
337 iface->save = djvu_document_save;
338 iface->get_n_pages = djvu_document_get_n_pages;
339 iface->get_page_size = djvu_document_get_page_size;
340 iface->render = djvu_document_render;
341 iface->get_info = djvu_document_get_info;
345 djvu_selection_get_selected_text (EvSelection *selection,
347 EvSelectionStyle style,
350 DjvuDocument *djvu_document = DJVU_DOCUMENT (selection);
351 double width, height;
352 EvRectangle rectangle;
355 djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
356 rc->page, &width, &height);
357 rectangle.x1 = points->x1 / SCALE_FACTOR;
358 rectangle.y1 = (height - points->y2) / SCALE_FACTOR;
359 rectangle.x2 = points->x2 / SCALE_FACTOR;
360 rectangle.y2 = (height - points->y1) / SCALE_FACTOR;
362 text = djvu_text_copy (djvu_document, rc->page, &rectangle);
365 text = g_strdup ("");
371 djvu_selection_iface_init (EvSelectionIface *iface)
373 iface->get_selected_text = djvu_selection_get_selected_text;
377 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
382 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
383 gdouble page_width, page_height;
385 djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
386 &page_width, &page_height);
388 if (rc->rotation == 90 || rc->rotation == 270) {
389 *width = (gint) (page_height * rc->scale);
390 *height = (gint) (page_width * rc->scale);
392 *width = (gint) (page_width * rc->scale);
393 *height = (gint) (page_height * rc->scale);
398 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
402 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
403 GdkPixbuf *pixbuf, *rotated_pixbuf;
404 gdouble page_width, page_height;
405 gint thumb_width, thumb_height;
408 g_return_val_if_fail (djvu_document->d_document, NULL);
410 djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
411 &page_width, &page_height);
413 thumb_width = (gint) (page_width * rc->scale);
414 thumb_height = (gint) (page_height * rc->scale);
416 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
417 thumb_width, thumb_height);
418 gdk_pixbuf_fill (pixbuf, 0xffffffff);
419 pixels = gdk_pixbuf_get_pixels (pixbuf);
421 while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK)
422 djvu_handle_events(djvu_document, TRUE);
424 ddjvu_thumbnail_render (djvu_document->d_document, rc->page,
425 &thumb_width, &thumb_height,
426 djvu_document->thumbs_format,
427 gdk_pixbuf_get_rowstride (pixbuf),
430 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
431 g_object_unref (pixbuf);
434 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
436 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
437 g_object_unref (tmp_pixbuf);
440 return rotated_pixbuf;
444 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
446 iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
447 iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
450 /* EvFileExporterIface */
452 djvu_document_file_exporter_begin (EvFileExporter *exporter,
453 EvFileExporterContext *fc)
455 DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
457 if (djvu_document->ps_filename)
458 g_free (djvu_document->ps_filename);
459 djvu_document->ps_filename = g_strdup (fc->filename);
461 g_string_assign (djvu_document->opts, "-page=");
465 djvu_document_file_exporter_do_page (EvFileExporter *exporter,
468 DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
470 g_string_append_printf (djvu_document->opts, "%d,", (rc->page) + 1);
474 djvu_document_file_exporter_end (EvFileExporter *exporter)
477 const char *d_optv[d_optc];
479 DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
481 FILE *fn = fopen (djvu_document->ps_filename, "w");
483 g_warning ("Cannot open file ā%sā.", djvu_document->ps_filename);
487 d_optv[0] = djvu_document->opts->str;
489 ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
490 while (!ddjvu_job_done(job)) {
491 djvu_handle_events (djvu_document, TRUE);
497 static EvFileExporterCapabilities
498 djvu_document_file_exporter_get_capabilities (EvFileExporter *exporter)
500 return EV_FILE_EXPORTER_CAN_PAGE_SET |
501 EV_FILE_EXPORTER_CAN_COPIES |
502 EV_FILE_EXPORTER_CAN_COLLATE |
503 EV_FILE_EXPORTER_CAN_REVERSE |
504 EV_FILE_EXPORTER_CAN_GENERATE_PS;
508 djvu_document_file_exporter_iface_init (EvFileExporterIface *iface)
510 iface->begin = djvu_document_file_exporter_begin;
511 iface->do_page = djvu_document_file_exporter_do_page;
512 iface->end = djvu_document_file_exporter_end;
513 iface->get_capabilities = djvu_document_file_exporter_get_capabilities;
517 djvu_document_init (DjvuDocument *djvu_document)
519 guint masks[4] = { 0xff0000, 0xff00, 0xff, 0xff000000 };
521 djvu_document->d_context = ddjvu_context_create ("Evince");
522 djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 4, masks);
523 ddjvu_format_set_row_order (djvu_document->d_format, 1);
525 djvu_document->thumbs_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
526 ddjvu_format_set_row_order (djvu_document->thumbs_format, 1);
528 djvu_document->ps_filename = NULL;
529 djvu_document->opts = g_string_new ("");
531 djvu_document->d_document = NULL;
535 djvu_document_find_begin (EvDocumentFind *document,
537 const char *search_string,
538 gboolean case_sensitive)
540 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
542 if (djvu_document->search &&
543 strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0)
546 if (djvu_document->search)
547 djvu_text_free (djvu_document->search);
549 djvu_document->search = djvu_text_new (djvu_document,
556 djvu_document_find_get_n_results (EvDocumentFind *document_find, int page)
558 DjvuText *search = DJVU_DOCUMENT (document_find)->search;
561 return djvu_text_n_results (search, page);
568 djvu_document_find_get_result (EvDocumentFind *document_find,
571 EvRectangle *rectangle)
573 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find);
574 DjvuText *search = djvu_document->search;
576 double width, height;
581 r = djvu_text_get_result (search, page, n_result);
585 djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
586 page, &width, &height);
587 rectangle->x1 = r->x1 * SCALE_FACTOR;
588 rectangle->y1 = height - r->y2 * SCALE_FACTOR;
589 rectangle->x2 = r->x2 * SCALE_FACTOR;
590 rectangle->y2 = height - r->y1 * SCALE_FACTOR;
596 djvu_document_find_page_has_results (EvDocumentFind *document_find,
599 DjvuText *search = DJVU_DOCUMENT (document_find)->search;
601 return search && djvu_text_has_results (search, page);
605 djvu_document_find_get_progress (EvDocumentFind *document_find)
607 DjvuText *search = DJVU_DOCUMENT (document_find)->search;
609 if (search == NULL) {
613 return djvu_text_get_progress (search);
617 djvu_document_find_cancel (EvDocumentFind *document)
619 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
621 if (djvu_document->search) {
622 djvu_text_free (djvu_document->search);
623 djvu_document->search = NULL;
628 djvu_document_find_iface_init (EvDocumentFindIface *iface)
630 iface->begin = djvu_document_find_begin;
631 iface->get_n_results = djvu_document_find_get_n_results;
632 iface->get_result = djvu_document_find_get_result;
633 iface->page_has_results = djvu_document_find_page_has_results;
634 iface->get_progress = djvu_document_find_get_progress;
635 iface->cancel = djvu_document_find_cancel;
639 djvu_document_links_get_links (EvDocumentLinks *document_links,
642 return djvu_links_get_links (document_links, page, SCALE_FACTOR);
646 djvu_document_document_links_iface_init (EvDocumentLinksIface *iface)
648 iface->has_document_links = djvu_links_has_document_links;
649 iface->get_links_model = djvu_links_get_links_model;
650 iface->get_links = djvu_document_links_get_links;
651 iface->find_link_dest = djvu_links_find_link_dest;