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-document-misc.h"
26 #include "ev-document-find.h"
27 #include "ev-document-links.h"
29 #include <gdk-pixbuf/gdk-pixbuf-core.h>
30 #include <glib/gi18n.h>
31 #include <glib/gunicode.h>
34 #define SCALE_FACTOR 0.2
41 struct _DjvuDocumentClass
43 GObjectClass parent_class;
46 typedef struct _DjvuDocumentClass DjvuDocumentClass;
48 static void djvu_document_document_iface_init (EvDocumentIface *iface);
49 static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
50 static void djvu_document_find_iface_init (EvDocumentFindIface *iface);
51 static void djvu_document_document_links_iface_init (EvDocumentLinksIface *iface);
53 G_DEFINE_TYPE_WITH_CODE
54 (DjvuDocument, djvu_document, G_TYPE_OBJECT,
56 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, djvu_document_document_iface_init);
57 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init)
58 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init);
59 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init);
64 djvu_handle_events (DjvuDocument *djvu_document, int wait)
66 ddjvu_context_t *ctx = djvu_document->d_context;
67 const ddjvu_message_t *msg;
73 msg = ddjvu_message_wait (ctx);
75 while ((msg = ddjvu_message_peek (ctx))) {
76 switch (msg->m_any.tag) {
78 g_warning ("DjvuLibre error: %s",
79 msg->m_error.message);
80 if (msg->m_error.filename)
81 g_warning ("DjvuLibre error: %s:%d",
82 msg->m_error.filename,
88 ddjvu_message_pop (ctx);
93 djvu_document_load (EvDocument *document,
97 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
98 ddjvu_document_t *doc;
100 gboolean missing_files = FALSE;
102 /* FIXME: We could actually load uris */
103 filename = g_filename_from_uri (uri, NULL, error);
107 doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
114 if (djvu_document->d_document)
115 ddjvu_document_release (djvu_document->d_document);
117 djvu_document->d_document = doc;
119 while (!ddjvu_document_decoding_done (djvu_document->d_document))
120 djvu_handle_events (djvu_document, TRUE);
121 g_free (djvu_document->uri);
122 djvu_document->uri = g_strdup (uri);
124 if (ddjvu_document_get_type (djvu_document->d_document) == DDJVU_DOCTYPE_INDIRECT) {
129 base = g_path_get_dirname (filename);
131 n_files = ddjvu_document_get_filenum (djvu_document->d_document);
132 for (i = 0; i < n_files; i++) {
133 struct ddjvu_fileinfo_s fileinfo;
136 ddjvu_document_get_fileinfo (djvu_document->d_document,
139 if (fileinfo.type != 'P')
142 file = g_build_filename (base, fileinfo.id, NULL);
143 if (!g_file_test (file, G_FILE_TEST_EXISTS)) {
144 missing_files = TRUE;
159 _("The document is composed by several files. "
160 "One or more of such files cannot be accessed."));
170 djvu_document_save (EvDocument *document,
174 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
176 return ev_xfer_uri_simple (djvu_document->uri, uri, error);
180 djvu_document_get_n_pages (EvDocument *document)
182 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
184 g_return_val_if_fail (djvu_document->d_document, 0);
186 return ddjvu_document_get_pagenum (djvu_document->d_document);
190 djvu_document_get_page_size (EvDocument *document,
195 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
196 ddjvu_pageinfo_t info;
199 g_return_if_fail (djvu_document->d_document);
201 while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
202 djvu_handle_events(djvu_document, TRUE);
204 if (r >= DDJVU_JOB_FAILED)
205 djvu_handle_events(djvu_document, TRUE);
207 *width = info.width * SCALE_FACTOR;
208 *height = info.height * SCALE_FACTOR;
212 djvu_document_render_pixbuf (EvDocument *document,
215 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
217 GdkPixbuf *rotated_pixbuf;
221 ddjvu_page_t *d_page;
223 double page_width, page_height;
225 d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page);
227 while (!ddjvu_page_decoding_done (d_page))
228 djvu_handle_events(djvu_document, TRUE);
230 page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR;
231 page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR;
233 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height);
235 prect.x = 0; prect.y = 0;
236 prect.w = page_width; prect.h = page_height;
239 ddjvu_page_render(d_page, DDJVU_RENDER_COLOR,
242 djvu_document->d_format,
243 gdk_pixbuf_get_rowstride (pixbuf),
244 (gchar *)gdk_pixbuf_get_pixels (pixbuf));
246 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
247 g_object_unref (pixbuf);
249 return rotated_pixbuf;
253 djvu_document_finalize (GObject *object)
255 DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
257 if (djvu_document->d_document)
258 ddjvu_document_release (djvu_document->d_document);
260 ddjvu_context_release (djvu_document->d_context);
261 ddjvu_format_release (djvu_document->d_format);
262 g_free (djvu_document->uri);
264 G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
268 djvu_document_class_init (DjvuDocumentClass *klass)
270 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
272 gobject_class->finalize = djvu_document_finalize;
276 djvu_document_can_get_text (EvDocument *document)
283 djvu_document_get_text (EvDocument *document, int page, EvRectangle *rect)
285 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
286 double width, height;
287 EvRectangle rectangle;
290 djvu_document_get_page_size (document, page, &width, &height);
291 rectangle.x1 = rect->x1 / SCALE_FACTOR;
292 rectangle.y1 = (height - rect->y2) / SCALE_FACTOR;
293 rectangle.x2 = rect->x2 / SCALE_FACTOR;
294 rectangle.y2 = (height - rect->y1) / SCALE_FACTOR;
296 text = djvu_text_copy (djvu_document, page, &rectangle);
299 text = g_strdup ("");
304 static EvDocumentInfo *
305 djvu_document_get_info (EvDocument *document)
307 EvDocumentInfo *info;
309 info = g_new0 (EvDocumentInfo, 1);
315 djvu_document_document_iface_init (EvDocumentIface *iface)
317 iface->load = djvu_document_load;
318 iface->save = djvu_document_save;
319 iface->can_get_text = djvu_document_can_get_text;
320 iface->get_text = djvu_document_get_text;
321 iface->get_n_pages = djvu_document_get_n_pages;
322 iface->get_page_size = djvu_document_get_page_size;
323 iface->render_pixbuf = djvu_document_render_pixbuf;
324 iface->get_info = djvu_document_get_info;
328 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
333 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
334 gdouble page_width, page_height;
336 djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
337 &page_width, &page_height);
339 if (rc->rotation == 90 || rc->rotation == 270) {
340 *width = (gint) (page_height * rc->scale);
341 *height = (gint) (page_width * rc->scale);
343 *width = (gint) (page_width * rc->scale);
344 *height = (gint) (page_height * rc->scale);
349 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
353 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
354 GdkPixbuf *pixbuf, *rotated_pixbuf;
355 gdouble page_width, page_height;
356 gint thumb_width, thumb_height;
359 g_return_val_if_fail (djvu_document->d_document, NULL);
361 djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
362 &page_width, &page_height);
364 thumb_width = (gint) (page_width * rc->scale);
365 thumb_height = (gint) (page_height * rc->scale);
367 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
368 thumb_width, thumb_height);
369 gdk_pixbuf_fill (pixbuf, 0xffffffff);
370 pixels = gdk_pixbuf_get_pixels (pixbuf);
372 while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK)
373 djvu_handle_events(djvu_document, TRUE);
375 ddjvu_thumbnail_render (djvu_document->d_document, rc->page,
376 &thumb_width, &thumb_height,
377 djvu_document->d_format,
378 gdk_pixbuf_get_rowstride (pixbuf),
381 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
382 g_object_unref (pixbuf);
385 GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
387 rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
388 g_object_unref (tmp_pixbuf);
391 return rotated_pixbuf;
395 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
397 iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
398 iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
402 djvu_document_init (DjvuDocument *djvu_document)
404 djvu_document->d_context = ddjvu_context_create ("Evince");
405 djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
406 ddjvu_format_set_row_order (djvu_document->d_format,1);
408 djvu_document->d_document = NULL;
412 djvu_document_find_begin (EvDocumentFind *document,
414 const char *search_string,
415 gboolean case_sensitive)
417 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
419 if (djvu_document->search &&
420 strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0)
423 if (djvu_document->search)
424 djvu_text_free (djvu_document->search);
426 djvu_document->search = djvu_text_new (djvu_document,
433 djvu_document_find_get_n_results (EvDocumentFind *document_find, int page)
435 DjvuText *search = DJVU_DOCUMENT (document_find)->search;
438 return djvu_text_n_results (search, page);
445 djvu_document_find_get_result (EvDocumentFind *document_find,
448 EvRectangle *rectangle)
450 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find);
451 DjvuText *search = djvu_document->search;
453 double width, height;
458 r = djvu_text_get_result (search, page, n_result);
462 djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
463 page, &width, &height);
464 rectangle->x1 = r->x1 * SCALE_FACTOR;
465 rectangle->y1 = height - r->y2 * SCALE_FACTOR;
466 rectangle->x2 = r->x2 * SCALE_FACTOR;
467 rectangle->y2 = height - r->y1 * SCALE_FACTOR;
473 djvu_document_find_page_has_results (EvDocumentFind *document_find,
476 DjvuText *search = DJVU_DOCUMENT (document_find)->search;
478 return search && djvu_text_has_results (search, page);
482 djvu_document_find_get_progress (EvDocumentFind *document_find)
484 DjvuText *search = DJVU_DOCUMENT (document_find)->search;
486 if (search == NULL) {
490 return djvu_text_get_progress (search);
494 djvu_document_find_cancel (EvDocumentFind *document)
496 DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
498 if (djvu_document->search) {
499 djvu_text_free (djvu_document->search);
500 djvu_document->search = NULL;
505 djvu_document_find_iface_init (EvDocumentFindIface *iface)
507 iface->begin = djvu_document_find_begin;
508 iface->get_n_results = djvu_document_find_get_n_results;
509 iface->get_result = djvu_document_find_get_result;
510 iface->page_has_results = djvu_document_find_page_has_results;
511 iface->get_progress = djvu_document_find_get_progress;
512 iface->cancel = djvu_document_find_cancel;
516 djvu_document_links_get_links (EvDocumentLinks *document_links,
519 return djvu_links_get_links (document_links, page, SCALE_FACTOR);
523 djvu_document_document_links_iface_init (EvDocumentLinksIface *iface)
525 iface->has_document_links = djvu_links_has_document_links;
526 iface->get_links_model = djvu_links_get_links_model;
527 iface->get_links = djvu_document_links_get_links;
528 iface->find_link_dest = djvu_links_find_link_dest;