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 int *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 ev_document_changed (EV_DOCUMENT (pdf_document));
154 /* Update the search results available to the app since
155 * we only provide full results on the current page
157 if (pdf_document->search)
158 pdf_document_search_page_changed (pdf_document->search);
161 return pdf_document->page_valid;
165 pdf_document_load (EvDocument *document,
169 PdfDocument *pdf_document = PDF_DOCUMENT (document);
177 globalParams = new GlobalParams("/etc/xpdfrc");
178 globalParams->setupBaseFontsFc(NULL);
181 if (! pdf_document->umap) {
182 enc = new GString("UTF-8");
183 pdf_document->umap = globalParams->getUnicodeMap(enc);
184 pdf_document->umap->incRefCnt ();
188 filename = g_filename_from_uri (uri, NULL, error);
192 filename_g = new GString (filename);
195 // open the PDF file, assumes ownership of filename_g
196 GString *password = NULL;
197 if (pdf_document->password)
198 password = new GString (pdf_document->password);
199 newDoc = new PDFDoc(filename_g, password, password);
203 if (!newDoc->isOk()) {
204 err = newDoc->getErrorCode();
206 if (err == errEncrypted) {
207 g_set_error (error, EV_DOCUMENT_ERROR,
208 EV_DOCUMENT_ERROR_ENCRYPTED,
209 "Document is encrypted.");
211 g_set_error (error, G_FILE_ERROR,
213 "Failed to load document (error %d) '%s'\n",
221 if (pdf_document->doc)
222 delete pdf_document->doc;
223 pdf_document->doc = newDoc;
225 pdf_document->page = 1;
227 if (pdf_document->out)
228 pdf_document->out->startDoc(pdf_document->doc->getXRef());
230 pdf_document->page_valid = FALSE;
232 g_object_notify (G_OBJECT (pdf_document), "title");
238 pdf_document_save (EvDocument *document,
242 PdfDocument *pdf_document = PDF_DOCUMENT (document);
244 gboolean retval = FALSE;
246 filename = g_filename_from_uri (uri, NULL, error);
247 if (filename != NULL) {
248 GString *fname = new GString (filename);
250 retval = pdf_document->doc->saveAs (fname);
257 pdf_document_get_n_pages (EvDocument *document)
259 PdfDocument *pdf_document = PDF_DOCUMENT (document);
261 if (pdf_document->doc)
262 return pdf_document->doc->getNumPages();
268 pdf_document_set_page (EvDocument *document,
271 PdfDocument *pdf_document = PDF_DOCUMENT (document);
273 page = CLAMP (page, 1, ev_document_get_n_pages (document));
275 if (page != pdf_document->page) {
276 pdf_document->page = page;
277 pdf_document->page_valid = FALSE;
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_find_get_progress (EvDocumentFind *document_find)
405 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
406 int n_pages, pages_done;
408 n_pages = ev_document_get_n_pages (EV_DOCUMENT (document_find));
409 if (search->search_page > search->start_page) {
410 pages_done = search->search_page - search->start_page;
411 } else if (search->search_page == search->start_page) {
412 pages_done = n_pages;
414 pages_done = n_pages - search->start_page + search->search_page;
417 return pages_done / (double) n_pages;
421 pdf_document_find_page_has_results (EvDocumentFind *document_find,
424 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
426 g_return_val_if_fail (search != NULL, FALSE);
428 return search->other_page_flags[page];
432 pdf_document_find_get_n_results (EvDocumentFind *document_find)
434 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
437 return search->current_page_results->len;
444 pdf_document_find_get_result (EvDocumentFind *document_find,
446 GdkRectangle *rectangle)
448 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
449 PdfDocumentSearch *search = pdf_document->search;
452 if (search != NULL) {
453 r = g_array_index (search->current_page_results,
454 GdkRectangle, n_result);
456 rectangle->x = r.x + pdf_document->page_x_offset;
457 rectangle->y = r.y + pdf_document->page_y_offset;
458 rectangle->width = r.width;
459 rectangle->height = r.height;
468 pdf_document_search_page_changed (PdfDocumentSearch *search)
470 PdfDocument *pdf_document = search->document;
473 int xMin, yMin, xMax, yMax;
475 current_page = pdf_document->page;
477 if (!pdf_document->page_valid) {
478 /* we can't do anything until displayPage() */
479 search->current_page = -1;
483 if (search->current_page == current_page)
486 /* We need to create current_page_results for the new current page */
487 g_array_set_size (search->current_page_results, 0);
489 if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
490 gTrue, gTrue, // startAtTop, stopAtBottom
491 gFalse, gFalse, // startAtLast, stopAtLast
492 &xMin, &yMin, &xMax, &yMax)) {
495 result.width = xMax - xMin;
496 result.height = yMax - yMin;
498 g_array_append_val (search->current_page_results, result);
499 /* Now find further results */
501 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
504 &xMin, &yMin, &xMax, &yMax)) {
507 result.width = xMax - xMin;
508 result.height = yMax - yMin;
510 g_array_append_val (search->current_page_results, result);
516 pdf_document_search_idle_callback (void *data)
518 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
519 PdfDocument *pdf_document = search->document;
520 int n_pages, changed_page;
521 double xMin, yMin, xMax, yMax;
523 /* Note that PDF page count is 1 through n_pages INCLUSIVE
524 * like a real book. We are looking to add one result for each
525 * page with a match, because the coordinates are meaningless
526 * with TextOutputDev, so we just want to flag matching pages
527 * and then when the user switches to the current page, we
528 * will emit "found" again with the real results.
530 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
532 if (search->output_dev == 0) {
533 /* First time through here... */
534 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
535 if (!search->output_dev->isOk()) {
540 pdf_document->doc->displayPage (search->output_dev,
542 72, 72, 0, gTrue, gFalse);
544 if (search->output_dev->findText (search->ucs4,
546 gTrue, gTrue, // startAtTop, stopAtBottom
547 gFalse, gFalse, // startAtLast, stopAtLast
548 &xMin, &yMin, &xMax, &yMax)) {
549 /* This page has results */
550 search->other_page_flags[search->search_page] = 1;
552 search->other_page_flags[search->search_page] = 0;
555 changed_page = search->start_page;
557 search->search_page += 1;
558 if (search->search_page > n_pages) {
560 search->search_page = 1;
563 if (search->search_page != search->start_page) {
564 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
571 search->idle = 0; /* will return FALSE to remove */
576 pdf_document_find_begin (EvDocumentFind *document,
577 const char *search_string,
578 gboolean case_sensitive)
580 PdfDocument *pdf_document = PDF_DOCUMENT (document);
581 PdfDocumentSearch *search;
586 /* FIXME handle case_sensitive (right now XPDF
587 * code is always case insensitive for ASCII
588 * and case sensitive for all other languaages)
591 g_assert (sizeof (gunichar) == sizeof (Unicode));
592 ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
595 if (pdf_document->search &&
596 pdf_document->search->ucs4_len == ucs4_len &&
597 memcmp (pdf_document->search->ucs4,
599 sizeof (gunichar) * ucs4_len) == 0) {
600 /* Search is unchanged */
605 if (pdf_document->search) {
606 pdf_document_search_free (pdf_document->search);
607 pdf_document->search = NULL;
610 search = g_new0 (PdfDocumentSearch, 1);
613 search->ucs4_len = ucs4_len;
615 search->current_page_results = g_array_new (FALSE,
617 sizeof (GdkRectangle));
618 n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
620 search->other_page_flags = g_new0 (int, n_pages + 1);
621 for (i = 0; i <= n_pages; i++) {
622 search->other_page_flags[i] = -1;
625 search->document = pdf_document;
627 /* We add at low priority so the progress bar repaints */
628 search->idle = g_idle_add_full (G_PRIORITY_LOW,
629 pdf_document_search_idle_callback,
633 search->output_dev = 0;
635 search->start_page = pdf_document->page;
636 search->search_page = search->start_page;
638 search->current_page = -1;
640 pdf_document->search = search;
642 /* Update for the current page right away */
643 pdf_document_search_page_changed (search);
647 pdf_document_find_cancel (EvDocumentFind *document)
649 PdfDocument *pdf_document = PDF_DOCUMENT (document);
651 if (pdf_document->search) {
652 pdf_document_search_free (pdf_document->search);
653 pdf_document->search = NULL;
658 pdf_document_search_free (PdfDocumentSearch *search)
660 if (search->idle != 0)
661 g_source_remove (search->idle);
663 if (search->output_dev)
664 delete search->output_dev;
666 g_array_free (search->current_page_results, TRUE);
667 g_free (search->other_page_flags);
669 g_free (search->ucs4);
674 pdf_document_has_document_security (EvDocumentSecurity *document_security)
676 /* FIXME: do we really need to have this? */
681 pdf_document_set_password (EvDocumentSecurity *document_security,
682 const char *password)
684 PdfDocument *document = PDF_DOCUMENT (document_security);
686 if (document->password)
687 g_free (document->password);
689 document->password = g_strdup (password);
693 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
695 PdfDocument *document = PDF_DOCUMENT (exporter);
697 if (document->ps_out)
698 delete document->ps_out;
700 document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
701 document->doc->getCatalog(), 1,
702 ev_document_get_n_pages (EV_DOCUMENT (document)),
707 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
709 PdfDocument *document = PDF_DOCUMENT (exporter);
711 document->doc->displayPage (document->ps_out, page,
712 72.0, 72.0, 0, gTrue, gFalse);
716 pdf_document_ps_export_end (EvPSExporter *exporter)
718 PdfDocument *document = PDF_DOCUMENT (exporter);
720 delete document->ps_out;
721 document->ps_out = NULL;
725 /* EvDocumentLinks Implementation */
728 /* goo GList, not glib */
735 unicode_to_char (OutlineItem *outline_item,
739 gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
742 for (i = 0; i < outline_item->getTitleLength(); ++i) {
743 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
747 return g_strdup (gstr.getCString ());
752 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
754 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
757 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
759 outline = pdf_document->doc->getOutline();
760 if (outline->getItems() != NULL &&
761 outline->getItems()->getLength() > 0)
767 static EvDocumentLinksIter *
768 pdf_document_links_begin_read (EvDocumentLinks *document_links)
770 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
775 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
777 outline = pdf_document->doc->getOutline();
778 items = outline->getItems();
782 iter = g_new0 (LinksIter, 1);
787 return (EvDocumentLinksIter *) iter;
791 build_link_from_action (PdfDocument *pdf_document,
792 LinkAction *link_action,
797 if (link_action == NULL) {
798 link = ev_link_new_title (title);
799 } else if (link_action->getKind () == actionGoTo) {
806 link_goto = dynamic_cast <LinkGoTo *> (link_action);
807 link_dest = link_goto->getDest ();
808 named_dest = link_goto->getNamedDest ();
810 /* Wow! This seems excessively slow on large
811 * documents. I need to investigate more... -jrb */
812 if (link_dest != NULL) {
813 link_dest = link_dest->copy ();
814 } else if (named_dest != NULL) {
815 named_dest = named_dest->copy ();
816 link_dest = pdf_document->doc->findDest (named_dest);
819 if (link_dest != NULL) {
820 if (link_dest->isPageRef ()) {
821 page_ref = link_dest->getPageRef ();
822 page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
824 page_num = link_dest->getPageNum ();
829 link = ev_link_new_page (title, page_num);
830 } else if (link_action->getKind () == actionURI) {
833 link_uri = dynamic_cast <LinkURI *> (link_action);
834 link = ev_link_new_external
835 (title, link_uri->getURI()->getCString());
836 } else if (link_action->getKind () == actionNamed) {
843 /* FIXME This returns a new object every time, probably we should cache it
846 pdf_document_links_get_link (EvDocumentLinks *document_links,
847 EvDocumentLinksIter *links_iter)
849 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
850 LinksIter *iter = (LinksIter *)links_iter;
852 LinkAction *link_action;
856 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
857 g_return_val_if_fail (iter != NULL, FALSE);
859 anItem = (OutlineItem *)iter->items->get(iter->index);
860 link_action = anItem->getAction ();
861 link_title = anItem->getTitle ();
862 title = unicode_to_char (anItem, pdf_document->umap);
864 return build_link_from_action (pdf_document, link_action, title);
867 static EvDocumentLinksIter *
868 pdf_document_links_get_child (EvDocumentLinks *document_links,
869 EvDocumentLinksIter *links_iter)
871 LinksIter *iter = (LinksIter *)links_iter;
872 LinksIter *child_iter;
875 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
877 anItem = (OutlineItem *)iter->items->get(iter->index);
879 if (! (anItem->hasKids() && anItem->getKids()) )
882 child_iter = g_new0 (LinksIter, 1);
883 child_iter->index = 0;
884 child_iter->level = iter->level + 1;
885 child_iter->items = anItem->getKids ();
886 g_assert (child_iter->items);
888 return (EvDocumentLinksIter *) child_iter;
892 pdf_document_links_next (EvDocumentLinks *document_links,
893 EvDocumentLinksIter *links_iter)
895 LinksIter *iter = (LinksIter *) links_iter;
897 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
900 if (iter->index >= iter->items->getLength())
907 pdf_document_links_free_iter (EvDocumentLinks *document_links,
908 EvDocumentLinksIter *iter)
910 g_return_if_fail (PDF_IS_DOCUMENT (document_links));
911 g_return_if_fail (iter != NULL);
913 /* FIXME: Should I close all the nodes?? Free them? */
918 pdf_document_finalize (GObject *object)
920 PdfDocument *pdf_document = PDF_DOCUMENT (object);
922 if (pdf_document->links) {
923 delete pdf_document->links;
926 if (pdf_document->umap) {
927 pdf_document->umap->decRefCnt ();
928 pdf_document->umap = NULL;
931 if (pdf_document->search)
932 pdf_document_search_free (pdf_document->search);
934 if (pdf_document->target)
935 g_object_unref (pdf_document->target);
937 if (pdf_document->out)
938 delete pdf_document->out;
939 if (pdf_document->ps_out)
940 delete pdf_document->ps_out;
941 if (pdf_document->doc)
942 delete pdf_document->doc;
947 pdf_document_set_property (GObject *object,
962 has_unicode_marker (GString *string)
964 return ((string->getChar (0) & 0xff) == 0xfe &&
965 (string->getChar (1) & 0xff) == 0xff);
969 pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
974 g_return_val_if_fail (info_dict != NULL, NULL);
975 g_return_val_if_fail (key != NULL, NULL);
977 if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
982 value = obj.getString ();
984 if (has_unicode_marker (value)) {
985 result = g_convert (value->getCString () + 2,
986 value->getLength () - 2,
987 "UTF-8", "UTF-16BE", NULL, NULL, NULL);
989 result = g_strndup (value->getCString (), value->getLength ());
998 pdf_document_get_title (PdfDocument *pdf_document)
1003 if (pdf_document->doc == NULL)
1005 pdf_document->doc->getDocInfo (&info);
1007 if (info.isDict ()) {
1008 title = pdf_info_dict_get_string (info.getDict(), "Title");
1015 pdf_document_get_text (EvDocument *document, GdkRectangle *rect)
1017 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1018 GString *sel_text = new GString;
1022 x1 = rect->x + pdf_document->page_x_offset;
1023 y1 = rect->y + pdf_document->page_y_offset;
1024 x2 = x1 + rect->width + pdf_document->page_x_offset;
1025 y2 = y1 + rect->height + pdf_document->page_y_offset;
1027 sel_text = pdf_document->out->getText (x1, y1, x2, y2);
1028 text = sel_text->getCString ();
1030 return text ? g_strdup (text) : NULL;
1034 pdf_document_get_link (EvDocument *document, int x, int y)
1036 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1038 double link_x, link_y;
1040 if (pdf_document->links == NULL) {
1045 link_x = x - pdf_document->page_x_offset;
1046 link_y = y - pdf_document->page_y_offset;
1049 link_y = pdf_document->out->getBitmapHeight() - link_y;
1052 link_x = link_x / pdf_document->scale;
1053 link_y = link_y / pdf_document->scale;
1055 action = pdf_document->links->find (link_x, link_y);
1058 return build_link_from_action (pdf_document, action, "");
1065 pdf_document_get_property (GObject *object,
1070 PdfDocument *pdf_document = PDF_DOCUMENT (object);
1076 title = pdf_document_get_title (pdf_document);
1077 g_value_set_string (value, title);
1084 pdf_document_class_init (PdfDocumentClass *klass)
1086 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1088 gobject_class->finalize = pdf_document_finalize;
1089 gobject_class->get_property = pdf_document_get_property;
1090 gobject_class->set_property = pdf_document_set_property;
1092 g_object_class_override_property (gobject_class, PROP_TITLE, "title");
1096 pdf_document_document_iface_init (EvDocumentIface *iface)
1098 iface->load = pdf_document_load;
1099 iface->save = pdf_document_save;
1100 iface->get_text = pdf_document_get_text;
1101 iface->get_link = pdf_document_get_link;
1102 iface->get_n_pages = pdf_document_get_n_pages;
1103 iface->set_page = pdf_document_set_page;
1104 iface->get_page = pdf_document_get_page;
1105 iface->set_scale = pdf_document_set_scale;
1106 iface->set_target = pdf_document_set_target;
1107 iface->set_page_offset = pdf_document_set_page_offset;
1108 iface->get_page_size = pdf_document_get_page_size;
1109 iface->render = pdf_document_render;
1113 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1115 iface->begin = pdf_document_ps_export_begin;
1116 iface->do_page = pdf_document_ps_export_do_page;
1117 iface->end = pdf_document_ps_export_end;
1122 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1124 iface->begin = pdf_document_find_begin;
1125 iface->get_n_results = pdf_document_find_get_n_results;
1126 iface->get_result = pdf_document_find_get_result;
1127 iface->page_has_results = pdf_document_find_page_has_results;
1128 iface->cancel = pdf_document_find_cancel;
1132 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
1134 iface->has_document_security = pdf_document_has_document_security;
1135 iface->set_password = pdf_document_set_password;
1139 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1141 iface->has_document_links = pdf_document_links_has_document_links;
1142 iface->begin_read = pdf_document_links_begin_read;
1143 iface->get_link = pdf_document_links_get_link;
1144 iface->get_child = pdf_document_links_get_child;
1145 iface->next = pdf_document_links_next;
1146 iface->free_iter = pdf_document_links_free_iter;
1152 bitmap_to_pixbuf (SplashBitmap *bitmap,
1159 SplashColorPtr dataPtr;
1162 gboolean target_has_alpha;
1163 gint target_rowstride;
1164 guchar *target_data;
1166 width = bitmap->getWidth ();
1167 height = bitmap->getHeight ();
1169 if (width + x_offset > gdk_pixbuf_get_width (target))
1170 width = gdk_pixbuf_get_width (target) - x_offset;
1171 if (height + y_offset > gdk_pixbuf_get_height (target))
1172 height = gdk_pixbuf_get_height (target) - x_offset;
1174 target_has_alpha = gdk_pixbuf_get_has_alpha (target);
1175 target_rowstride = gdk_pixbuf_get_rowstride (target);
1176 target_data = gdk_pixbuf_get_pixels (target);
1178 dataPtr = bitmap->getDataPtr ();
1180 for (y = 0; y < height; y++) {
1185 p = dataPtr.rgb8 + y * width;
1186 q = target_data + ((y + y_offset) * target_rowstride +
1187 x_offset * (target_has_alpha?4:3));
1188 for (x = 0; x < width; x++) {
1191 *q++ = splashRGB8R (rgb);
1192 *q++ = splashRGB8G (rgb);
1193 *q++ = splashRGB8B (rgb);
1195 if (target_has_alpha)
1205 pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
1206 gdouble scale_factor,
1211 SplashOutputDev *output;
1215 color.rgb8 = splashMakeRGB8 (255, 255, 255);
1217 output = new SplashOutputDev (splashModeRGB8, gFalse, color);
1218 output->startDoc (pdf_document->doc->getXRef());
1219 pdf_document->doc->displayPage (output,
1225 pixbuf = ev_document_misc_get_thumbnail_frame (output->getBitmap()->getWidth(),
1226 output->getBitmap()->getHeight(),
1228 bitmap_to_pixbuf (output->getBitmap(), pixbuf, 1, 1);
1235 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1237 gint suggested_width,
1241 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1244 Thumb *thumb = NULL;
1247 /* getPage seems to want page + 1 for some reason; */
1248 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1249 the_page->getThumb (&the_thumb);
1251 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1252 /* Build the thumbnail object */
1253 thumb = new Thumb(pdf_document->doc->getXRef (),
1256 *width = thumb->getWidth ();
1257 *height = thumb->getHeight ();
1259 page_ratio = the_page->getHeight () / the_page->getWidth ();
1260 *width = suggested_width;
1261 *height = (gint) (suggested_width * page_ratio);
1266 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1270 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1271 GdkPixbuf *thumbnail;
1274 Thumb *thumb = NULL;
1275 gboolean have_ethumbs = FALSE;
1279 /* getPage seems to want page + 1 for some reason; */
1280 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1281 the_page->getThumb(&the_thumb);
1283 page_ratio = the_page->getHeight () / the_page->getWidth ();
1284 dest_height = (gint) (width * page_ratio);
1287 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1288 /* Build the thumbnail object */
1289 thumb = new Thumb(pdf_document->doc->getXRef (),
1292 have_ethumbs = thumb->ok();
1297 GdkPixbuf *tmp_pixbuf;
1299 data = thumb->getPixbufData();
1300 tmp_pixbuf = gdk_pixbuf_new_from_data (data,
1305 thumb->getHeight (),
1306 thumb->getWidth () * 3,
1308 /* FIXME: do we want to check that the thumb's size isn't ridiculous?? */
1309 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
1310 g_object_unref (tmp_pixbuf);
1312 gdouble scale_factor;
1314 scale_factor = (gdouble)width / the_page->getWidth ();
1316 thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
1326 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1328 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1329 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1334 pdf_document_init (PdfDocument *pdf_document)
1336 pdf_document->page = 1;
1337 pdf_document->page_x_offset = 0;
1338 pdf_document->page_y_offset = 0;
1339 pdf_document->scale = 1.;
1341 pdf_document->page_valid = FALSE;
1342 pdf_document->password = NULL;