1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pdfdocument.h: Implementation of EvDocument for PDF
3 * Copyright (C) 2004, Red Hat, Inc.
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 <glib/gi18n.h>
22 #include "gpdf-g-switch.h"
23 #include "pdf-document.h"
24 #include "ev-ps-exporter.h"
25 #include "ev-document-find.h"
26 #include "gpdf-g-switch.h"
27 #include "ev-document-links.h"
28 #include "ev-document-misc.h"
29 #include "ev-document-security.h"
30 #include "ev-document-thumbnails.h"
32 #include "GlobalParams.h"
33 #include "GDKSplashOutputDev.h"
34 #include "SplashBitmap.h"
37 #include "ErrorCodes.h"
38 #include "UnicodeMap.h"
39 #include "GlobalParams.h"
42 #include "goo/GList.h"
43 #include "PSOutputDev.h"
52 PdfDocument *document;
56 /* full results are only possible for the rendered current page */
58 GArray *current_page_results;
59 guchar *other_page_flags; /* length n_pages + 1, first element ignored */
60 int start_page; /* skip this one as we iterate, since we did it first */
61 int search_page; /* the page we're searching now */
62 TextOutputDev *output_dev;
65 typedef struct _PdfDocumentClass PdfDocumentClass;
67 #define PDF_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDF_TYPE_DOCUMENT, PdfDocumentClass))
68 #define PDF_IS_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDF_TYPE_DOCUMENT))
69 #define PDF_DOCUMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDF_TYPE_DOCUMENT, PdfDocumentClass))
71 struct _PdfDocumentClass
73 GObjectClass parent_class;
78 GObject parent_instance;
86 GDKSplashOutputDev *out;
95 PdfDocumentSearch *search;
98 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
99 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
100 static void pdf_document_document_iface_init (EvDocumentIface *iface);
101 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface);
102 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
103 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
104 static void pdf_document_search_free (PdfDocumentSearch *search);
105 static void pdf_document_search_page_changed (PdfDocumentSearch *search);
108 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
110 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
111 pdf_document_document_iface_init);
112 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
113 pdf_document_document_links_iface_init);
114 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
115 pdf_document_document_thumbnails_iface_init);
116 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
117 pdf_document_ps_exporter_iface_init);
118 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
119 pdf_document_find_iface_init);
120 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
121 pdf_document_security_iface_init);
125 document_init_links (PdfDocument *pdf_document)
130 if (pdf_document->links) {
131 delete pdf_document->links;
133 page = pdf_document->doc->getCatalog ()->getPage (pdf_document->page);
134 pdf_document->links = new Links (page->getAnnots (&obj),
135 pdf_document->doc->getCatalog ()->getBaseURI ());
140 document_validate_page (PdfDocument *pdf_document)
142 if (!pdf_document->page_valid) {
143 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
144 72 * pdf_document->scale,
145 72 * pdf_document->scale,
148 document_init_links (pdf_document);
150 pdf_document->page_valid = TRUE;
152 /* Update the search results available to the app since
153 * we only provide full results on the current page
155 if (pdf_document->search)
156 pdf_document_search_page_changed (pdf_document->search);
159 return pdf_document->page_valid;
163 pdf_document_load (EvDocument *document,
167 PdfDocument *pdf_document = PDF_DOCUMENT (document);
175 globalParams = new GlobalParams("/etc/xpdfrc");
176 globalParams->setupBaseFontsFc(NULL);
179 if (! pdf_document->umap) {
180 enc = new GString("UTF-8");
181 pdf_document->umap = globalParams->getUnicodeMap(enc);
182 pdf_document->umap->incRefCnt ();
186 filename = g_filename_from_uri (uri, NULL, error);
190 filename_g = new GString (filename);
193 // open the PDF file, assumes ownership of filename_g
194 GString *password = NULL;
195 if (pdf_document->password)
196 password = new GString (pdf_document->password);
197 newDoc = new PDFDoc(filename_g, password, password);
201 if (!newDoc->isOk()) {
202 err = newDoc->getErrorCode();
204 if (err == errEncrypted) {
205 g_set_error (error, EV_DOCUMENT_ERROR,
206 EV_DOCUMENT_ERROR_ENCRYPTED,
207 "Document is encrypted.");
209 g_set_error (error, G_FILE_ERROR,
211 "Failed to load document (error %d) '%s'\n",
219 if (pdf_document->doc)
220 delete pdf_document->doc;
221 pdf_document->doc = newDoc;
223 pdf_document->page = 1;
225 if (pdf_document->out)
226 pdf_document->out->startDoc(pdf_document->doc->getXRef());
228 pdf_document->page_valid = FALSE;
230 g_object_notify (G_OBJECT (pdf_document), "title");
236 pdf_document_save (EvDocument *document,
240 PdfDocument *pdf_document = PDF_DOCUMENT (document);
242 gboolean retval = FALSE;
244 filename = g_filename_from_uri (uri, NULL, error);
245 if (filename != NULL) {
246 GString *fname = new GString (filename);
248 retval = pdf_document->doc->saveAs (fname);
255 pdf_document_get_n_pages (EvDocument *document)
257 PdfDocument *pdf_document = PDF_DOCUMENT (document);
259 if (pdf_document->doc)
260 return pdf_document->doc->getNumPages();
266 pdf_document_set_page (EvDocument *document,
269 PdfDocument *pdf_document = PDF_DOCUMENT (document);
271 page = CLAMP (page, 1, ev_document_get_n_pages (document));
273 if (page != pdf_document->page) {
274 pdf_document->page = page;
275 pdf_document->page_valid = FALSE;
278 ev_document_changed (document);
282 pdf_document_get_page (EvDocument *document)
284 PdfDocument *pdf_document = PDF_DOCUMENT (document);
286 return pdf_document->page;
290 redraw_callback (void *data)
292 /* Need to hook up through a EvDocument callback? */
296 pdf_document_set_target (EvDocument *document,
299 PdfDocument *pdf_document = PDF_DOCUMENT (document);
301 if (pdf_document->target != target) {
302 if (pdf_document->target)
303 g_object_unref (pdf_document->target);
305 pdf_document->target = target;
307 if (pdf_document->target)
308 g_object_ref (pdf_document->target);
310 if (pdf_document->out) {
311 delete pdf_document->out;
312 pdf_document->out = NULL;
315 if (pdf_document->target) {
316 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
317 redraw_callback, (void*) document);
319 if (pdf_document->doc)
320 pdf_document->out->startDoc(pdf_document->doc->getXRef());
324 pdf_document->page_valid = FALSE;
329 pdf_document_set_scale (EvDocument *document,
332 PdfDocument *pdf_document = PDF_DOCUMENT (document);
334 if (pdf_document->scale != scale) {
335 pdf_document->scale = scale;
336 pdf_document->page_valid = FALSE;
341 pdf_document_set_page_offset (EvDocument *document,
345 PdfDocument *pdf_document = PDF_DOCUMENT (document);
347 pdf_document->page_x_offset = x;
348 pdf_document->page_y_offset = y;
352 pdf_document_get_page_size (EvDocument *document,
356 PdfDocument *pdf_document = PDF_DOCUMENT (document);
358 if (document_validate_page (pdf_document)) {
360 *width = pdf_document->out->getBitmapWidth();
362 *height = pdf_document->out->getBitmapHeight();
372 pdf_document_render (EvDocument *document,
378 PdfDocument *pdf_document = PDF_DOCUMENT (document);
382 if (!document_validate_page (pdf_document) || !pdf_document->target)
385 page.x = pdf_document->page_x_offset;
386 page.y = pdf_document->page_y_offset;
387 page.width = pdf_document->out->getBitmapWidth();
388 page.height = pdf_document->out->getBitmapHeight();
392 draw.width = clip_width;
393 draw.height = clip_height;
395 if (gdk_rectangle_intersect (&page, &draw, &draw))
396 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
397 pdf_document->target,
399 draw.width, draw.height);
403 pdf_document_search_emit_found (PdfDocumentSearch *search)
405 PdfDocument *pdf_document = search->document;
411 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
412 if (search->search_page > search->start_page) {
413 pages_done = search->search_page - search->start_page;
414 } else if (search->search_page == search->start_page) {
415 pages_done = n_pages;
417 pages_done = n_pages - search->start_page + search->search_page;
420 tmp_results = g_array_new (FALSE, FALSE, sizeof (EvFindResult));
421 g_array_append_vals (tmp_results,
422 search->current_page_results->data,
423 search->current_page_results->len);
425 /* Now append a bogus element for each page that has a result in it,
426 * that is not the current page
429 while (i <= n_pages) {
430 if (i != pdf_document->page &&
431 search->other_page_flags[i]) {
436 /* Use bogus coordinates, again we can't get coordinates
437 * until this is the current page because TextOutputDev
440 result.highlight_area.x = -1;
441 result.highlight_area.y = -1;
442 result.highlight_area.width = 1;
443 result.highlight_area.height = 1;
445 g_array_append_val (tmp_results, result);
451 ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
452 (EvFindResult*) tmp_results->data,
454 pages_done / (double) n_pages);
456 g_array_free (tmp_results, TRUE);
460 pdf_document_search_page_changed (PdfDocumentSearch *search)
462 PdfDocument *pdf_document = search->document;
465 int xMin, yMin, xMax, yMax;
467 current_page = pdf_document->page;
469 if (!pdf_document->page_valid) {
470 /* we can't do anything until displayPage() */
471 search->current_page = -1;
475 if (search->current_page == current_page)
478 /* We need to create current_page_results for the new current page */
479 g_array_set_size (search->current_page_results, 0);
481 if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
482 gTrue, gTrue, // startAtTop, stopAtBottom
483 gFalse, gFalse, // startAtLast, stopAtLast
484 &xMin, &yMin, &xMax, &yMax)) {
485 result.page_num = pdf_document->page;
487 result.highlight_area.x = xMin;
488 result.highlight_area.y = yMin;
489 result.highlight_area.width = xMax - xMin;
490 result.highlight_area.height = yMax - yMin;
492 g_array_append_val (search->current_page_results, result);
493 /* Now find further results */
495 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
498 &xMin, &yMin, &xMax, &yMax)) {
500 result.page_num = pdf_document->page;
502 result.highlight_area.x = xMin;
503 result.highlight_area.y = yMin;
504 result.highlight_area.width = xMax - xMin;
505 result.highlight_area.height = yMax - yMin;
507 g_array_append_val (search->current_page_results, result);
511 /* needed for the initial current page since we don't search
514 search->other_page_flags[current_page] =
515 search->current_page_results->len > 0;
517 pdf_document_search_emit_found (search);
521 pdf_document_search_idle_callback (void *data)
523 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
524 PdfDocument *pdf_document = search->document;
526 double xMin, yMin, xMax, yMax;
528 /* Note that PDF page count is 1 through n_pages INCLUSIVE
529 * like a real book. We are looking to add one result for each
530 * page with a match, because the coordinates are meaningless
531 * with TextOutputDev, so we just want to flag matching pages
532 * and then when the user switches to the current page, we
533 * will emit "found" again with the real results.
535 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
537 if (search->search_page == search->start_page) {
541 if (search->output_dev == 0) {
542 /* First time through here... */
543 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
544 if (!search->output_dev->isOk()) {
549 pdf_document->doc->displayPage (search->output_dev,
551 72, 72, 0, gTrue, gFalse);
553 if (search->output_dev->findText (search->ucs4,
555 gTrue, gTrue, // startAtTop, stopAtBottom
556 gFalse, gFalse, // startAtLast, stopAtLast
557 &xMin, &yMin, &xMax, &yMax)) {
558 /* This page has results */
559 search->other_page_flags[search->search_page] = TRUE;
562 search->search_page += 1;
563 if (search->search_page > n_pages) {
565 search->search_page = 1;
568 /* We do this even if nothing was found, to update the percent complete */
569 pdf_document_search_emit_found (search);
575 search->idle = 0; /* will return FALSE to remove */
580 pdf_document_find_begin (EvDocumentFind *document,
581 const char *search_string,
582 gboolean case_sensitive)
584 PdfDocument *pdf_document = PDF_DOCUMENT (document);
585 PdfDocumentSearch *search;
590 /* FIXME handle case_sensitive (right now XPDF
591 * code is always case insensitive for ASCII
592 * and case sensitive for all other languaages)
595 g_assert (sizeof (gunichar) == sizeof (Unicode));
596 ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
599 if (pdf_document->search &&
600 pdf_document->search->ucs4_len == ucs4_len &&
601 memcmp (pdf_document->search->ucs4,
603 sizeof (gunichar) * ucs4_len) == 0) {
604 /* Search is unchanged */
609 if (pdf_document->search) {
610 pdf_document_search_free (pdf_document->search);
611 pdf_document->search = NULL;
614 search = g_new0 (PdfDocumentSearch, 1);
617 search->ucs4_len = ucs4_len;
619 search->current_page_results = g_array_new (FALSE,
621 sizeof (EvFindResult));
622 n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
624 /* This is an array of bool; with the first value ignored
625 * so we can index by the based-at-1 page numbers
627 search->other_page_flags = g_new0 (guchar, n_pages + 1);
629 search->document = pdf_document;
631 /* We add at low priority so the progress bar repaints */
632 search->idle = g_idle_add_full (G_PRIORITY_LOW,
633 pdf_document_search_idle_callback,
637 search->output_dev = 0;
639 search->start_page = pdf_document->page;
640 search->search_page = search->start_page + 1;
641 if (search->search_page > n_pages)
642 search->search_page = 1;
644 search->current_page = -1;
646 pdf_document->search = search;
648 /* Update for the current page right away */
649 pdf_document_search_page_changed (search);
653 pdf_document_find_cancel (EvDocumentFind *document)
655 PdfDocument *pdf_document = PDF_DOCUMENT (document);
657 if (pdf_document->search) {
658 pdf_document_search_free (pdf_document->search);
659 pdf_document->search = NULL;
664 pdf_document_search_free (PdfDocumentSearch *search)
666 if (search->idle != 0)
667 g_source_remove (search->idle);
669 if (search->output_dev)
670 delete search->output_dev;
672 g_array_free (search->current_page_results, TRUE);
673 g_free (search->other_page_flags);
675 g_free (search->ucs4);
680 pdf_document_has_document_security (EvDocumentSecurity *document_security)
682 /* FIXME: do we really need to have this? */
687 pdf_document_set_password (EvDocumentSecurity *document_security,
688 const char *password)
690 PdfDocument *document = PDF_DOCUMENT (document_security);
692 if (document->password)
693 g_free (document->password);
695 document->password = g_strdup (password);
699 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
701 PdfDocument *document = PDF_DOCUMENT (exporter);
703 if (document->ps_out)
704 delete document->ps_out;
706 document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
707 document->doc->getCatalog(), 1,
708 ev_document_get_n_pages (EV_DOCUMENT (document)),
713 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
715 PdfDocument *document = PDF_DOCUMENT (exporter);
717 document->doc->displayPage (document->ps_out, page,
718 72.0, 72.0, 0, gTrue, gFalse);
722 pdf_document_ps_export_end (EvPSExporter *exporter)
724 PdfDocument *document = PDF_DOCUMENT (exporter);
726 delete document->ps_out;
727 document->ps_out = NULL;
731 /* EvDocumentLinks Implementation */
734 /* goo GList, not glib */
741 unicode_to_char (OutlineItem *outline_item,
745 gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
748 for (i = 0; i < outline_item->getTitleLength(); ++i) {
749 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
753 return g_strdup (gstr.getCString ());
758 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
760 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
763 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
765 outline = pdf_document->doc->getOutline();
766 if (outline->getItems() != NULL &&
767 outline->getItems()->getLength() > 0)
773 static EvDocumentLinksIter *
774 pdf_document_links_begin_read (EvDocumentLinks *document_links)
776 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
781 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
783 outline = pdf_document->doc->getOutline();
784 items = outline->getItems();
788 iter = g_new0 (LinksIter, 1);
793 return (EvDocumentLinksIter *) iter;
797 build_link_from_action (PdfDocument *pdf_document,
798 LinkAction *link_action,
803 if (link_action == NULL) {
804 link = ev_link_new_title (title);
805 } else if (link_action->getKind () == actionGoTo) {
812 link_goto = dynamic_cast <LinkGoTo *> (link_action);
813 link_dest = link_goto->getDest ();
814 named_dest = link_goto->getNamedDest ();
816 /* Wow! This seems excessively slow on large
817 * documents. I need to investigate more... -jrb */
818 if (link_dest != NULL) {
819 link_dest = link_dest->copy ();
820 } else if (named_dest != NULL) {
821 named_dest = named_dest->copy ();
822 link_dest = pdf_document->doc->findDest (named_dest);
825 if (link_dest != NULL) {
826 if (link_dest->isPageRef ()) {
827 page_ref = link_dest->getPageRef ();
828 page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
830 page_num = link_dest->getPageNum ();
835 link = ev_link_new_page (title, page_num);
836 } else if (link_action->getKind () == actionURI) {
839 link_uri = dynamic_cast <LinkURI *> (link_action);
840 link = ev_link_new_external
841 (title, link_uri->getURI()->getCString());
842 } else if (link_action->getKind () == actionNamed) {
849 /* FIXME This returns a new object every time, probably we should cache it
852 pdf_document_links_get_link (EvDocumentLinks *document_links,
853 EvDocumentLinksIter *links_iter)
855 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
856 LinksIter *iter = (LinksIter *)links_iter;
858 LinkAction *link_action;
862 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
863 g_return_val_if_fail (iter != NULL, FALSE);
865 anItem = (OutlineItem *)iter->items->get(iter->index);
866 link_action = anItem->getAction ();
867 link_title = anItem->getTitle ();
868 title = unicode_to_char (anItem, pdf_document->umap);
870 return build_link_from_action (pdf_document, link_action, title);
873 static EvDocumentLinksIter *
874 pdf_document_links_get_child (EvDocumentLinks *document_links,
875 EvDocumentLinksIter *links_iter)
877 LinksIter *iter = (LinksIter *)links_iter;
878 LinksIter *child_iter;
881 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
883 anItem = (OutlineItem *)iter->items->get(iter->index);
885 if (! (anItem->hasKids() && anItem->getKids()) )
888 child_iter = g_new0 (LinksIter, 1);
889 child_iter->index = 0;
890 child_iter->level = iter->level + 1;
891 child_iter->items = anItem->getKids ();
892 g_assert (child_iter->items);
894 return (EvDocumentLinksIter *) child_iter;
898 pdf_document_links_next (EvDocumentLinks *document_links,
899 EvDocumentLinksIter *links_iter)
901 LinksIter *iter = (LinksIter *) links_iter;
903 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
906 if (iter->index >= iter->items->getLength())
913 pdf_document_links_free_iter (EvDocumentLinks *document_links,
914 EvDocumentLinksIter *iter)
916 g_return_if_fail (PDF_IS_DOCUMENT (document_links));
917 g_return_if_fail (iter != NULL);
919 /* FIXME: Should I close all the nodes?? Free them? */
924 pdf_document_finalize (GObject *object)
926 PdfDocument *pdf_document = PDF_DOCUMENT (object);
928 if (pdf_document->links) {
929 delete pdf_document->links;
932 if (pdf_document->umap) {
933 pdf_document->umap->decRefCnt ();
934 pdf_document->umap = NULL;
937 if (pdf_document->search)
938 pdf_document_search_free (pdf_document->search);
940 if (pdf_document->target)
941 g_object_unref (pdf_document->target);
943 if (pdf_document->out)
944 delete pdf_document->out;
945 if (pdf_document->ps_out)
946 delete pdf_document->ps_out;
947 if (pdf_document->doc)
948 delete pdf_document->doc;
953 pdf_document_set_property (GObject *object,
968 has_unicode_marker (GString *string)
970 return ((string->getChar (0) & 0xff) == 0xfe &&
971 (string->getChar (1) & 0xff) == 0xff);
975 pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
980 g_return_val_if_fail (info_dict != NULL, NULL);
981 g_return_val_if_fail (key != NULL, NULL);
983 if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
988 value = obj.getString ();
990 if (has_unicode_marker (value)) {
991 result = g_convert (value->getCString () + 2,
992 value->getLength () - 2,
993 "UTF-8", "UTF-16BE", NULL, NULL, NULL);
995 result = g_strndup (value->getCString (), value->getLength ());
1004 pdf_document_get_title (PdfDocument *pdf_document)
1009 pdf_document->doc->getDocInfo (&info);
1011 if (info.isDict ()) {
1012 title = pdf_info_dict_get_string (info.getDict(), "Title");
1019 pdf_document_get_text (EvDocument *document, GdkRectangle *rect)
1021 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1022 GString *sel_text = new GString;
1028 x2 = x1 + rect->width;
1029 y2 = y1 + rect->height;
1031 sel_text = pdf_document->out->getText (x1, y1, x2, y2);
1032 text = sel_text->getCString ();
1034 return text ? g_strdup (text) : NULL;
1038 pdf_document_get_link (EvDocument *document, int x, int y)
1040 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1043 y = pdf_document->out->getBitmapHeight() - y;
1045 action = pdf_document->links->find ((double)x / pdf_document->scale,
1046 (double)y / pdf_document->scale);
1049 return build_link_from_action (pdf_document, action, "");
1056 pdf_document_get_property (GObject *object,
1061 PdfDocument *pdf_document = PDF_DOCUMENT (object);
1067 title = pdf_document_get_title (pdf_document);
1068 g_value_set_string (value, title);
1075 pdf_document_class_init (PdfDocumentClass *klass)
1077 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1079 gobject_class->finalize = pdf_document_finalize;
1080 gobject_class->get_property = pdf_document_get_property;
1081 gobject_class->set_property = pdf_document_set_property;
1083 g_object_class_override_property (gobject_class, PROP_TITLE, "title");
1087 pdf_document_document_iface_init (EvDocumentIface *iface)
1089 iface->load = pdf_document_load;
1090 iface->save = pdf_document_save;
1091 iface->get_text = pdf_document_get_text;
1092 iface->get_link = pdf_document_get_link;
1093 iface->get_n_pages = pdf_document_get_n_pages;
1094 iface->set_page = pdf_document_set_page;
1095 iface->get_page = pdf_document_get_page;
1096 iface->set_scale = pdf_document_set_scale;
1097 iface->set_target = pdf_document_set_target;
1098 iface->set_page_offset = pdf_document_set_page_offset;
1099 iface->get_page_size = pdf_document_get_page_size;
1100 iface->render = pdf_document_render;
1104 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1106 iface->begin = pdf_document_ps_export_begin;
1107 iface->do_page = pdf_document_ps_export_do_page;
1108 iface->end = pdf_document_ps_export_end;
1113 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1115 iface->begin = pdf_document_find_begin;
1116 iface->cancel = pdf_document_find_cancel;
1120 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
1122 iface->has_document_security = pdf_document_has_document_security;
1123 iface->set_password = pdf_document_set_password;
1127 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1129 iface->has_document_links = pdf_document_links_has_document_links;
1130 iface->begin_read = pdf_document_links_begin_read;
1131 iface->get_link = pdf_document_links_get_link;
1132 iface->get_child = pdf_document_links_get_child;
1133 iface->next = pdf_document_links_next;
1134 iface->free_iter = pdf_document_links_free_iter;
1140 bitmap_to_pixbuf (SplashBitmap *bitmap,
1147 SplashColorPtr dataPtr;
1150 gboolean target_has_alpha;
1151 gint target_rowstride;
1152 guchar *target_data;
1154 width = bitmap->getWidth ();
1155 height = bitmap->getHeight ();
1157 if (width + x_offset > gdk_pixbuf_get_width (target))
1158 width = gdk_pixbuf_get_width (target) - x_offset;
1159 if (height + y_offset > gdk_pixbuf_get_height (target))
1160 height = gdk_pixbuf_get_height (target) - x_offset;
1162 target_has_alpha = gdk_pixbuf_get_has_alpha (target);
1163 target_rowstride = gdk_pixbuf_get_rowstride (target);
1164 target_data = gdk_pixbuf_get_pixels (target);
1166 dataPtr = bitmap->getDataPtr ();
1168 for (y = 0; y < height; y++) {
1173 p = dataPtr.rgb8 + y * width;
1174 q = target_data + ((y + y_offset) * target_rowstride +
1175 x_offset * (target_has_alpha?4:3));
1176 for (x = 0; x < width; x++) {
1179 *q++ = splashRGB8R (rgb);
1180 *q++ = splashRGB8G (rgb);
1181 *q++ = splashRGB8B (rgb);
1183 if (target_has_alpha)
1193 pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
1194 gdouble scale_factor,
1199 SplashOutputDev *output;
1203 color.rgb8 = splashMakeRGB8 (255, 255, 255);
1205 output = new SplashOutputDev (splashModeRGB8, gFalse, color);
1206 output->startDoc (pdf_document->doc->getXRef());
1207 pdf_document->doc->displayPage (output,
1213 pixbuf = ev_document_misc_get_thumbnail_frame (output->getBitmap()->getWidth(),
1214 output->getBitmap()->getHeight(),
1216 bitmap_to_pixbuf (output->getBitmap(), pixbuf, 1, 1);
1223 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1225 gint suggested_width,
1229 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1232 Thumb *thumb = NULL;
1235 /* getPage seems to want page + 1 for some reason; */
1236 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1237 the_page->getThumb (&the_thumb);
1239 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1240 /* Build the thumbnail object */
1241 thumb = new Thumb(pdf_document->doc->getXRef (),
1244 *width = thumb->getWidth ();
1245 *height = thumb->getHeight ();
1247 page_ratio = the_page->getHeight () / the_page->getWidth ();
1248 *width = suggested_width;
1249 *height = (gint) (suggested_width * page_ratio);
1254 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1258 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1259 GdkPixbuf *thumbnail;
1262 Thumb *thumb = NULL;
1263 gboolean have_ethumbs = FALSE;
1267 /* getPage seems to want page + 1 for some reason; */
1268 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1269 the_page->getThumb(&the_thumb);
1271 page_ratio = the_page->getHeight () / the_page->getWidth ();
1272 dest_height = (gint) (width * page_ratio);
1275 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1276 /* Build the thumbnail object */
1277 thumb = new Thumb(pdf_document->doc->getXRef (),
1280 have_ethumbs = thumb->ok();
1285 GdkPixbuf *tmp_pixbuf;
1287 data = thumb->getPixbufData();
1288 tmp_pixbuf = gdk_pixbuf_new_from_data (data,
1293 thumb->getHeight (),
1294 thumb->getWidth () * 3,
1296 /* FIXME: do we want to check that the thumb's size isn't ridiculous?? */
1297 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
1298 g_object_unref (tmp_pixbuf);
1300 gdouble scale_factor;
1302 scale_factor = (gdouble)width / the_page->getWidth ();
1304 thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
1314 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1316 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1317 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1322 pdf_document_init (PdfDocument *pdf_document)
1324 pdf_document->page = 1;
1325 pdf_document->page_x_offset = 0;
1326 pdf_document->page_y_offset = 0;
1327 pdf_document->scale = 1.;
1329 pdf_document->page_valid = FALSE;
1330 pdf_document->password = NULL;