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 "ev-document-misc.h"
27 #include "gpdf-g-switch.h"
28 #include "ev-document-links.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;
94 PdfDocumentSearch *search;
97 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
98 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
99 static void pdf_document_document_iface_init (EvDocumentIface *iface);
100 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface);
101 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
102 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
103 static void pdf_document_search_free (PdfDocumentSearch *search);
104 static void pdf_document_search_page_changed (PdfDocumentSearch *search);
107 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
109 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
110 pdf_document_document_iface_init);
111 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
112 pdf_document_document_links_iface_init);
113 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
114 pdf_document_document_thumbnails_iface_init);
115 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
116 pdf_document_ps_exporter_iface_init);
117 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
118 pdf_document_find_iface_init);
119 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
120 pdf_document_security_iface_init);
124 document_init_links (PdfDocument *pdf_document)
129 if (pdf_document->links) {
130 delete pdf_document->links;
132 page = pdf_document->doc->getCatalog ()->getPage (pdf_document->page);
133 pdf_document->links = new Links (page->getAnnots (&obj),
134 pdf_document->doc->getCatalog ()->getBaseURI ());
139 document_display_page (PdfDocument *pdf_document)
141 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
142 72 * pdf_document->scale,
143 72 * pdf_document->scale,
146 document_init_links (pdf_document);
148 /* Update the search results available to the app since
149 * we only provide full results on the current page
151 if (pdf_document->search)
152 pdf_document_search_page_changed (pdf_document->search);
156 pdf_document_load (EvDocument *document,
160 PdfDocument *pdf_document = PDF_DOCUMENT (document);
168 globalParams = new GlobalParams("/etc/xpdfrc");
169 globalParams->setupBaseFontsFc(NULL);
172 if (! pdf_document->umap) {
173 enc = new GString("UTF-8");
174 pdf_document->umap = globalParams->getUnicodeMap(enc);
175 pdf_document->umap->incRefCnt ();
179 filename = g_filename_from_uri (uri, NULL, error);
183 filename_g = new GString (filename);
186 // open the PDF file, assumes ownership of filename_g
187 GString *password = NULL;
188 if (pdf_document->password)
189 password = new GString (pdf_document->password);
190 newDoc = new PDFDoc(filename_g, password, password);
194 if (!newDoc->isOk()) {
195 err = newDoc->getErrorCode();
197 if (err == errEncrypted) {
198 g_set_error (error, EV_DOCUMENT_ERROR,
199 EV_DOCUMENT_ERROR_ENCRYPTED,
200 "Document is encrypted.");
202 g_set_error (error, G_FILE_ERROR,
204 "Failed to load document (error %d) '%s'\n",
212 if (pdf_document->doc)
213 delete pdf_document->doc;
214 pdf_document->doc = newDoc;
216 pdf_document->page = 1;
218 if (pdf_document->out)
219 pdf_document->out->startDoc(pdf_document->doc->getXRef());
221 g_object_notify (G_OBJECT (pdf_document), "title");
227 pdf_document_save (EvDocument *document,
231 PdfDocument *pdf_document = PDF_DOCUMENT (document);
233 gboolean retval = FALSE;
235 filename = g_filename_from_uri (uri, NULL, error);
236 if (filename != NULL) {
237 GString *fname = new GString (filename);
239 retval = pdf_document->doc->saveAs (fname);
246 pdf_document_get_n_pages (EvDocument *document)
248 PdfDocument *pdf_document = PDF_DOCUMENT (document);
250 if (pdf_document->doc)
251 return pdf_document->doc->getNumPages();
257 pdf_document_set_page (EvDocument *document,
260 PdfDocument *pdf_document = PDF_DOCUMENT (document);
262 page = CLAMP (page, 1, ev_document_get_n_pages (document));
264 if (page != pdf_document->page) {
265 pdf_document->page = page;
266 document_display_page (pdf_document);
267 ev_document_page_changed (EV_DOCUMENT (pdf_document));
272 pdf_document_get_page (EvDocument *document)
274 PdfDocument *pdf_document = PDF_DOCUMENT (document);
276 return pdf_document->page;
280 redraw_callback (void *data)
282 /* Need to hook up through a EvDocument callback? */
286 pdf_document_set_target (EvDocument *document,
289 PdfDocument *pdf_document = PDF_DOCUMENT (document);
291 if (pdf_document->target != target) {
292 if (pdf_document->target)
293 g_object_unref (pdf_document->target);
295 pdf_document->target = target;
297 if (pdf_document->target)
298 g_object_ref (pdf_document->target);
300 if (pdf_document->out) {
301 delete pdf_document->out;
302 pdf_document->out = NULL;
305 if (pdf_document->target) {
306 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
307 redraw_callback, (void*) document);
309 if (pdf_document->doc)
310 pdf_document->out->startDoc(pdf_document->doc->getXRef());
314 /* FIXME we need to regenerate pages */
319 pdf_document_set_scale (EvDocument *document,
322 PdfDocument *pdf_document = PDF_DOCUMENT (document);
324 if (pdf_document->scale != scale) {
325 pdf_document->scale = scale;
326 document_display_page (pdf_document);
327 ev_document_scale_changed (EV_DOCUMENT (pdf_document));
332 pdf_document_set_page_offset (EvDocument *document,
336 PdfDocument *pdf_document = PDF_DOCUMENT (document);
338 pdf_document->page_x_offset = x;
339 pdf_document->page_y_offset = y;
343 pdf_document_get_page_size (EvDocument *document,
348 PdfDocument *pdf_document = PDF_DOCUMENT (document);
351 /* set some default values */
358 page = pdf_document->page;
360 the_page = pdf_document->doc->getCatalog ()->getPage (page);
362 *width = (int) ((the_page->getWidth () * pdf_document->scale) + 0.5);
363 *height = (int) ((the_page->getHeight () * pdf_document->scale) + 0.5);
368 pdf_document_render (EvDocument *document,
374 PdfDocument *pdf_document = PDF_DOCUMENT (document);
378 if (!pdf_document->target)
381 page.x = pdf_document->page_x_offset;
382 page.y = pdf_document->page_y_offset;
383 page.width = pdf_document->out->getBitmapWidth();
384 page.height = pdf_document->out->getBitmapHeight();
388 draw.width = clip_width;
389 draw.height = clip_height;
391 if (gdk_rectangle_intersect (&page, &draw, &draw))
392 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
393 pdf_document->target,
395 draw.width, draw.height);
399 pdf_document_find_get_progress (EvDocumentFind *document_find)
401 PdfDocumentSearch *search;
402 int n_pages, pages_done;
404 search = PDF_DOCUMENT (document_find)->search;
406 if (search == NULL) {
410 n_pages = ev_document_get_n_pages (EV_DOCUMENT (document_find));
411 if (search->search_page > search->start_page) {
412 pages_done = search->search_page - search->start_page + 1;
413 } else if (search->search_page == search->start_page) {
414 pages_done = n_pages;
416 pages_done = n_pages - search->start_page + search->search_page;
419 return pages_done / (double) n_pages;
423 pdf_document_find_page_has_results (EvDocumentFind *document_find,
426 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
428 g_return_val_if_fail (search != NULL, FALSE);
430 return search->other_page_flags[page];
434 pdf_document_find_get_n_results (EvDocumentFind *document_find)
436 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
439 return search->current_page_results->len;
446 pdf_document_find_get_result (EvDocumentFind *document_find,
448 GdkRectangle *rectangle)
450 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
451 PdfDocumentSearch *search = pdf_document->search;
454 if (search != NULL &&
455 n_result < search->current_page_results->len) {
456 r = g_array_index (search->current_page_results,
457 GdkRectangle, n_result);
459 rectangle->x = r.x + pdf_document->page_x_offset;
460 rectangle->y = r.y + pdf_document->page_y_offset;
461 rectangle->width = r.width;
462 rectangle->height = r.height;
471 pdf_document_search_page_changed (PdfDocumentSearch *search)
473 PdfDocument *pdf_document = search->document;
476 int xMin, yMin, xMax, yMax;
478 current_page = pdf_document->page;
480 if (search->current_page == current_page)
483 /* We need to create current_page_results for the new current page */
484 g_array_set_size (search->current_page_results, 0);
486 if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
487 gTrue, gTrue, // startAtTop, stopAtBottom
488 gFalse, gFalse, // startAtLast, stopAtLast
489 &xMin, &yMin, &xMax, &yMax)) {
492 result.width = xMax - xMin;
493 result.height = yMax - yMin;
495 g_array_append_val (search->current_page_results, result);
496 /* Now find further results */
498 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
501 &xMin, &yMin, &xMax, &yMax)) {
504 result.width = xMax - xMin;
505 result.height = yMax - yMin;
507 g_array_append_val (search->current_page_results, result);
513 pdf_document_search_idle_callback (void *data)
515 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
516 PdfDocument *pdf_document = search->document;
517 int n_pages, changed_page;
518 double xMin, yMin, xMax, yMax;
520 /* Note that PDF page count is 1 through n_pages INCLUSIVE
521 * like a real book. We are looking to add one result for each
522 * page with a match, because the coordinates are meaningless
523 * with TextOutputDev, so we just want to flag matching pages
524 * and then when the user switches to the current page, we
525 * will emit "found" again with the real results.
527 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
529 if (search->output_dev == 0) {
530 /* First time through here... */
531 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
532 if (!search->output_dev->isOk()) {
537 pdf_document->doc->displayPage (search->output_dev,
539 72, 72, 0, gTrue, gFalse);
541 if (search->output_dev->findText (search->ucs4,
543 gTrue, gTrue, // startAtTop, stopAtBottom
544 gFalse, gFalse, // startAtLast, stopAtLast
545 &xMin, &yMin, &xMax, &yMax)) {
546 /* This page has results */
547 search->other_page_flags[search->search_page] = 1;
549 search->other_page_flags[search->search_page] = 0;
552 changed_page = search->start_page;
554 search->search_page += 1;
555 if (search->search_page > n_pages) {
557 search->search_page = 1;
560 if (search->search_page != search->start_page) {
561 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
568 search->idle = 0; /* will return FALSE to remove */
573 pdf_document_find_begin (EvDocumentFind *document,
574 const char *search_string,
575 gboolean case_sensitive)
577 PdfDocument *pdf_document = PDF_DOCUMENT (document);
578 PdfDocumentSearch *search;
583 /* FIXME handle case_sensitive (right now XPDF
584 * code is always case insensitive for ASCII
585 * and case sensitive for all other languaages)
588 g_assert (sizeof (gunichar) == sizeof (Unicode));
589 ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
592 if (pdf_document->search &&
593 pdf_document->search->ucs4_len == ucs4_len &&
594 memcmp (pdf_document->search->ucs4,
596 sizeof (gunichar) * ucs4_len) == 0) {
597 /* Search is unchanged */
602 if (pdf_document->search) {
603 pdf_document_search_free (pdf_document->search);
604 pdf_document->search = NULL;
607 search = g_new0 (PdfDocumentSearch, 1);
610 search->ucs4_len = ucs4_len;
612 search->current_page_results = g_array_new (FALSE,
614 sizeof (GdkRectangle));
615 n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
617 search->other_page_flags = g_new0 (int, n_pages + 1);
618 for (i = 0; i <= n_pages; i++) {
619 search->other_page_flags[i] = -1;
622 search->document = pdf_document;
624 /* We add at low priority so the progress bar repaints */
625 search->idle = g_idle_add_full (G_PRIORITY_LOW,
626 pdf_document_search_idle_callback,
630 search->output_dev = 0;
632 search->start_page = pdf_document->page;
633 search->search_page = search->start_page;
635 search->current_page = -1;
637 pdf_document->search = search;
639 /* Update for the current page right away */
640 pdf_document_search_page_changed (search);
644 pdf_document_find_cancel (EvDocumentFind *document)
646 PdfDocument *pdf_document = PDF_DOCUMENT (document);
648 if (pdf_document->search) {
649 pdf_document_search_free (pdf_document->search);
650 pdf_document->search = NULL;
655 pdf_document_search_free (PdfDocumentSearch *search)
657 if (search->idle != 0)
658 g_source_remove (search->idle);
660 if (search->output_dev)
661 delete search->output_dev;
663 g_array_free (search->current_page_results, TRUE);
664 g_free (search->other_page_flags);
666 g_free (search->ucs4);
671 pdf_document_has_document_security (EvDocumentSecurity *document_security)
673 /* FIXME: do we really need to have this? */
678 pdf_document_set_password (EvDocumentSecurity *document_security,
679 const char *password)
681 PdfDocument *document = PDF_DOCUMENT (document_security);
683 if (document->password)
684 g_free (document->password);
686 document->password = g_strdup (password);
690 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
692 PdfDocument *document = PDF_DOCUMENT (exporter);
694 if (document->ps_out)
695 delete document->ps_out;
697 document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
698 document->doc->getCatalog(), 1,
699 ev_document_get_n_pages (EV_DOCUMENT (document)),
704 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
706 PdfDocument *document = PDF_DOCUMENT (exporter);
708 document->doc->displayPage (document->ps_out, page,
709 72.0, 72.0, 0, gTrue, gFalse);
713 pdf_document_ps_export_end (EvPSExporter *exporter)
715 PdfDocument *document = PDF_DOCUMENT (exporter);
717 delete document->ps_out;
718 document->ps_out = NULL;
722 /* EvDocumentLinks Implementation */
725 /* goo GList, not glib */
732 unicode_to_char (OutlineItem *outline_item,
736 gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
739 for (i = 0; i < outline_item->getTitleLength(); ++i) {
740 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
744 return g_strdup (gstr.getCString ());
749 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
751 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
754 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
756 outline = pdf_document->doc->getOutline();
757 if (outline->getItems() != NULL &&
758 outline->getItems()->getLength() > 0)
764 static EvDocumentLinksIter *
765 pdf_document_links_begin_read (EvDocumentLinks *document_links)
767 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
772 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
774 outline = pdf_document->doc->getOutline();
775 items = outline->getItems();
779 iter = g_new0 (LinksIter, 1);
784 return (EvDocumentLinksIter *) iter;
788 build_link_from_action (PdfDocument *pdf_document,
789 LinkAction *link_action,
794 if (link_action == NULL) {
795 link = ev_link_new_title (title);
796 } else if (link_action->getKind () == actionGoToR) {
797 g_warning ("actionGoToR links not implemented");
798 } else if (link_action->getKind () == actionLaunch) {
799 g_warning ("actionLaunch links not implemented");
800 } else if (link_action->getKind () == actionNamed) {
801 g_warning ("actionNamed links not implemented");
802 } else if (link_action->getKind () == actionMovie) {
803 g_warning ("actionMovie links not implemented");
804 } else if (link_action->getKind () == actionGoTo) {
811 link_goto = dynamic_cast <LinkGoTo *> (link_action);
812 link_dest = link_goto->getDest ();
813 named_dest = link_goto->getNamedDest ();
815 /* Wow! This seems excessively slow on large
816 * documents. I need to investigate more... -jrb */
817 if (link_dest != NULL) {
818 link_dest = link_dest->copy ();
819 } else if (named_dest != NULL) {
820 named_dest = named_dest->copy ();
821 link_dest = pdf_document->doc->findDest (named_dest);
824 if (link_dest != NULL) {
825 if (link_dest->isPageRef ()) {
826 page_ref = link_dest->getPageRef ();
827 page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
829 page_num = link_dest->getPageNum ();
834 link = ev_link_new_page (title, page_num);
835 } else if (link_action->getKind () == actionURI) {
838 link_uri = dynamic_cast <LinkURI *> (link_action);
839 link = ev_link_new_external
840 (title, link_uri->getURI()->getCString());
841 } else if (link_action->getKind () == actionUnknown) {
842 LinkUnknown *link_unknown;
844 link_unknown = dynamic_cast <LinkUnknown *> (link_action);
846 g_warning ("Unknown link type %s",
847 link_unknown->getAction()->getCString());
853 /* FIXME This returns a new object every time, probably we should cache it
856 pdf_document_links_get_link (EvDocumentLinks *document_links,
857 EvDocumentLinksIter *links_iter)
859 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
860 LinksIter *iter = (LinksIter *)links_iter;
862 LinkAction *link_action;
866 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
867 g_return_val_if_fail (iter != NULL, FALSE);
869 anItem = (OutlineItem *)iter->items->get(iter->index);
870 link_action = anItem->getAction ();
871 link_title = anItem->getTitle ();
872 title = unicode_to_char (anItem, pdf_document->umap);
874 return build_link_from_action (pdf_document, link_action, title);
877 static EvDocumentLinksIter *
878 pdf_document_links_get_child (EvDocumentLinks *document_links,
879 EvDocumentLinksIter *links_iter)
881 LinksIter *iter = (LinksIter *)links_iter;
882 LinksIter *child_iter;
885 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
887 anItem = (OutlineItem *)iter->items->get(iter->index);
889 if (! (anItem->hasKids() && anItem->getKids()) )
892 child_iter = g_new0 (LinksIter, 1);
893 child_iter->index = 0;
894 child_iter->level = iter->level + 1;
895 child_iter->items = anItem->getKids ();
896 g_assert (child_iter->items);
898 return (EvDocumentLinksIter *) child_iter;
902 pdf_document_links_next (EvDocumentLinks *document_links,
903 EvDocumentLinksIter *links_iter)
905 LinksIter *iter = (LinksIter *) links_iter;
907 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
910 if (iter->index >= iter->items->getLength())
917 pdf_document_links_free_iter (EvDocumentLinks *document_links,
918 EvDocumentLinksIter *iter)
920 g_return_if_fail (PDF_IS_DOCUMENT (document_links));
921 g_return_if_fail (iter != NULL);
923 /* FIXME: Should I close all the nodes?? Free them? */
928 pdf_document_finalize (GObject *object)
930 PdfDocument *pdf_document = PDF_DOCUMENT (object);
932 if (pdf_document->links) {
933 delete pdf_document->links;
936 if (pdf_document->umap) {
937 pdf_document->umap->decRefCnt ();
938 pdf_document->umap = NULL;
941 if (pdf_document->search)
942 pdf_document_search_free (pdf_document->search);
944 if (pdf_document->target)
945 g_object_unref (pdf_document->target);
947 if (pdf_document->out)
948 delete pdf_document->out;
949 if (pdf_document->ps_out)
950 delete pdf_document->ps_out;
951 if (pdf_document->doc)
952 delete pdf_document->doc;
957 pdf_document_set_property (GObject *object,
972 has_unicode_marker (GString *string)
974 return ((string->getChar (0) & 0xff) == 0xfe &&
975 (string->getChar (1) & 0xff) == 0xff);
979 pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
984 g_return_val_if_fail (info_dict != NULL, NULL);
985 g_return_val_if_fail (key != NULL, NULL);
987 if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
992 value = obj.getString ();
994 if (has_unicode_marker (value)) {
995 result = g_convert (value->getCString () + 2,
996 value->getLength () - 2,
997 "UTF-8", "UTF-16BE", NULL, NULL, NULL);
999 result = g_strndup (value->getCString (), value->getLength ());
1008 pdf_document_get_title (PdfDocument *pdf_document)
1013 if (pdf_document->doc == NULL)
1015 pdf_document->doc->getDocInfo (&info);
1017 if (info.isDict ()) {
1018 title = pdf_info_dict_get_string (info.getDict(), "Title");
1025 pdf_document_get_text (EvDocument *document, GdkRectangle *rect)
1027 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1028 GString *sel_text = new GString;
1032 x1 = rect->x - pdf_document->page_x_offset;
1033 y1 = rect->y - pdf_document->page_y_offset;
1034 x2 = x1 + rect->width;
1035 y2 = y1 + rect->height;
1037 sel_text = pdf_document->out->getText (x1, y1, x2, y2);
1038 text = sel_text->getCString ();
1040 return text ? g_strdup (text) : NULL;
1044 pdf_document_get_link (EvDocument *document, int x, int y)
1046 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1048 double link_x, link_y;
1050 if (pdf_document->links == NULL) {
1055 link_x = x - pdf_document->page_x_offset;
1056 link_y = y - pdf_document->page_y_offset;
1059 link_y = pdf_document->out->getBitmapHeight() - link_y;
1062 link_x = link_x / pdf_document->scale;
1063 link_y = link_y / pdf_document->scale;
1065 action = pdf_document->links->find (link_x, link_y);
1068 return build_link_from_action (pdf_document, action, "");
1075 pdf_document_get_property (GObject *object,
1080 PdfDocument *pdf_document = PDF_DOCUMENT (object);
1086 title = pdf_document_get_title (pdf_document);
1087 g_value_set_string (value, title);
1094 pdf_document_class_init (PdfDocumentClass *klass)
1096 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1098 gobject_class->finalize = pdf_document_finalize;
1099 gobject_class->get_property = pdf_document_get_property;
1100 gobject_class->set_property = pdf_document_set_property;
1102 g_object_class_override_property (gobject_class, PROP_TITLE, "title");
1106 pdf_document_document_iface_init (EvDocumentIface *iface)
1108 iface->load = pdf_document_load;
1109 iface->save = pdf_document_save;
1110 iface->get_text = pdf_document_get_text;
1111 iface->get_link = pdf_document_get_link;
1112 iface->get_n_pages = pdf_document_get_n_pages;
1113 iface->set_page = pdf_document_set_page;
1114 iface->get_page = pdf_document_get_page;
1115 iface->set_scale = pdf_document_set_scale;
1116 iface->set_target = pdf_document_set_target;
1117 iface->set_page_offset = pdf_document_set_page_offset;
1118 iface->get_page_size = pdf_document_get_page_size;
1119 iface->render = pdf_document_render;
1123 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1125 iface->begin = pdf_document_ps_export_begin;
1126 iface->do_page = pdf_document_ps_export_do_page;
1127 iface->end = pdf_document_ps_export_end;
1132 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1134 iface->begin = pdf_document_find_begin;
1135 iface->get_n_results = pdf_document_find_get_n_results;
1136 iface->get_result = pdf_document_find_get_result;
1137 iface->page_has_results = pdf_document_find_page_has_results;
1138 iface->get_progress = pdf_document_find_get_progress;
1139 iface->cancel = pdf_document_find_cancel;
1143 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
1145 iface->has_document_security = pdf_document_has_document_security;
1146 iface->set_password = pdf_document_set_password;
1150 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1152 iface->has_document_links = pdf_document_links_has_document_links;
1153 iface->begin_read = pdf_document_links_begin_read;
1154 iface->get_link = pdf_document_links_get_link;
1155 iface->get_child = pdf_document_links_get_child;
1156 iface->next = pdf_document_links_next;
1157 iface->free_iter = pdf_document_links_free_iter;
1163 bitmap_to_pixbuf (SplashBitmap *bitmap,
1170 SplashColorPtr dataPtr;
1173 gboolean target_has_alpha;
1174 gint target_rowstride;
1175 guchar *target_data;
1177 width = bitmap->getWidth ();
1178 height = bitmap->getHeight ();
1180 if (width + x_offset > gdk_pixbuf_get_width (target))
1181 width = gdk_pixbuf_get_width (target) - x_offset;
1182 if (height + y_offset > gdk_pixbuf_get_height (target))
1183 height = gdk_pixbuf_get_height (target) - x_offset;
1185 target_has_alpha = gdk_pixbuf_get_has_alpha (target);
1186 target_rowstride = gdk_pixbuf_get_rowstride (target);
1187 target_data = gdk_pixbuf_get_pixels (target);
1189 dataPtr = bitmap->getDataPtr ();
1191 for (y = 0; y < height; y++) {
1196 p = dataPtr.rgb8 + y * width;
1197 q = target_data + ((y + y_offset) * target_rowstride +
1198 x_offset * (target_has_alpha?4:3));
1199 for (x = 0; x < width; x++) {
1202 *q++ = splashRGB8R (rgb);
1203 *q++ = splashRGB8G (rgb);
1204 *q++ = splashRGB8B (rgb);
1206 if (target_has_alpha)
1216 pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
1217 gdouble scale_factor,
1222 SplashOutputDev *output;
1226 color.rgb8 = splashMakeRGB8 (255, 255, 255);
1228 output = new SplashOutputDev (splashModeRGB8, gFalse, color);
1229 output->startDoc (pdf_document->doc->getXRef());
1230 pdf_document->doc->displayPage (output,
1236 pixbuf = ev_document_misc_get_thumbnail_frame (output->getBitmap()->getWidth(),
1237 output->getBitmap()->getHeight(),
1239 bitmap_to_pixbuf (output->getBitmap(), pixbuf, 1, 1);
1246 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1248 gint suggested_width,
1252 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1255 Thumb *thumb = NULL;
1258 the_page = pdf_document->doc->getCatalog ()->getPage (page);
1259 the_page->getThumb (&the_thumb);
1261 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1262 /* Build the thumbnail object */
1263 thumb = new Thumb(pdf_document->doc->getXRef (),
1266 *width = thumb->getWidth ();
1267 *height = thumb->getHeight ();
1269 page_ratio = the_page->getHeight () / the_page->getWidth ();
1270 *width = suggested_width;
1271 *height = (gint) (suggested_width * page_ratio);
1276 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1280 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1281 GdkPixbuf *thumbnail;
1284 Thumb *thumb = NULL;
1285 gboolean have_ethumbs = FALSE;
1289 /* getPage seems to want page + 1 for some reason; */
1290 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1291 the_page->getThumb(&the_thumb);
1293 page_ratio = the_page->getHeight () / the_page->getWidth ();
1294 dest_height = (gint) (width * page_ratio);
1297 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1298 /* Build the thumbnail object */
1299 thumb = new Thumb(pdf_document->doc->getXRef (),
1302 have_ethumbs = thumb->ok();
1307 GdkPixbuf *tmp_pixbuf;
1309 data = thumb->getPixbufData();
1310 tmp_pixbuf = gdk_pixbuf_new_from_data (data,
1315 thumb->getHeight (),
1316 thumb->getWidth () * 3,
1318 /* FIXME: do we want to check that the thumb's size isn't ridiculous?? */
1319 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
1320 g_object_unref (tmp_pixbuf);
1322 gdouble scale_factor;
1324 scale_factor = (gdouble)width / the_page->getWidth ();
1326 thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
1336 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1338 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1339 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1344 pdf_document_init (PdfDocument *pdf_document)
1346 pdf_document->page = 1;
1347 pdf_document->page_x_offset = 0;
1348 pdf_document->page_y_offset = 0;
1349 pdf_document->scale = 1.;
1351 pdf_document->password = NULL;