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 "pdf-document.h"
23 #include "ev-ps-exporter.h"
24 #include "ev-document-find.h"
25 #include "ev-document-misc.h"
26 #include "ev-document-links.h"
27 #include "ev-document-security.h"
28 #include "ev-document-thumbnails.h"
30 #include <goo/GooList.h>
31 #include <splash/SplashBitmap.h>
32 #include <GlobalParams.h>
35 #include <ErrorCodes.h>
36 #include <UnicodeMap.h>
38 #include <PSOutputDev.h>
41 #include "GDKSplashOutputDev.h"
50 PdfDocument *document;
54 /* full results are only possible for the rendered current page */
56 GArray *current_page_results;
57 int *other_page_flags; /* length n_pages + 1, first element ignored */
58 int start_page; /* skip this one as we iterate, since we did it first */
59 int search_page; /* the page we're searching now */
60 TextOutputDev *output_dev;
63 typedef struct _PdfDocumentClass PdfDocumentClass;
65 #define PDF_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDF_TYPE_DOCUMENT, PdfDocumentClass))
66 #define PDF_IS_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDF_TYPE_DOCUMENT))
67 #define PDF_DOCUMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDF_TYPE_DOCUMENT, PdfDocumentClass))
69 struct _PdfDocumentClass
71 GObjectClass parent_class;
76 GObject parent_instance;
84 GDKSplashOutputDev *out;
92 PdfDocumentSearch *search;
95 static EvLink *build_link_from_action (PdfDocument *pdf_document,
96 LinkAction *link_action,
99 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
100 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
101 static void pdf_document_document_iface_init (EvDocumentIface *iface);
102 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface);
103 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
104 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
105 static void pdf_document_search_free (PdfDocumentSearch *search);
106 static void pdf_document_search_page_changed (PdfDocumentSearch *search);
108 static GdkPixbuf *bitmap_to_pixbuf (SplashBitmap *bitmap,
114 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
116 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
117 pdf_document_document_iface_init);
118 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
119 pdf_document_document_links_iface_init);
120 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
121 pdf_document_document_thumbnails_iface_init);
122 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
123 pdf_document_ps_exporter_iface_init);
124 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
125 pdf_document_find_iface_init);
126 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
127 pdf_document_security_iface_init);
131 document_init_links (PdfDocument *pdf_document)
136 if (pdf_document->links) {
137 delete pdf_document->links;
139 page = pdf_document->doc->getCatalog ()->getPage (pdf_document->page);
140 pdf_document->links = new Links (page->getAnnots (&obj),
141 pdf_document->doc->getCatalog ()->getBaseURI ());
146 document_display_page (PdfDocument *pdf_document)
148 if (pdf_document->out != NULL) {
149 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
150 72 * pdf_document->scale,
151 72 * pdf_document->scale,
154 document_init_links (pdf_document);
156 /* Update the search results available to the app since
157 * we only provide full results on the current page
159 if (pdf_document->search)
160 pdf_document_search_page_changed (pdf_document->search);
165 pdf_document_load (EvDocument *document,
169 PdfDocument *pdf_document = PDF_DOCUMENT (document);
173 GooString *filename_g;
177 globalParams = new GlobalParams("/etc/xpdfrc");
178 globalParams->setupBaseFontsFc(NULL);
181 if (! pdf_document->umap) {
182 enc = new GooString("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 GooString (filename);
195 // open the PDF file, assumes ownership of filename_g
196 GooString *password = NULL;
197 if (pdf_document->password)
198 password = new GooString (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 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 GooString *fname = new GooString (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, pdf_document_get_n_pages (document));
273 if (page != pdf_document->page) {
274 pdf_document->page = page;
275 document_display_page (pdf_document);
276 ev_document_page_changed (EV_DOCUMENT (pdf_document));
281 pdf_document_get_page (EvDocument *document)
283 PdfDocument *pdf_document = PDF_DOCUMENT (document);
285 return pdf_document->page;
289 redraw_callback (void *data)
291 /* Need to hook up through a EvDocument callback? */
295 pdf_document_set_target (EvDocument *document,
298 PdfDocument *pdf_document = PDF_DOCUMENT (document);
300 if (pdf_document->target != target) {
301 if (pdf_document->target)
302 g_object_unref (pdf_document->target);
304 pdf_document->target = target;
306 if (pdf_document->target)
307 g_object_ref (pdf_document->target);
309 if (pdf_document->out) {
310 delete pdf_document->out;
311 pdf_document->out = NULL;
314 if (pdf_document->target) {
315 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
316 redraw_callback, (void*) document);
318 if (pdf_document->doc)
319 pdf_document->out->startDoc(pdf_document->doc->getXRef());
321 document_display_page (pdf_document);
327 pdf_document_set_scale (EvDocument *document,
330 PdfDocument *pdf_document = PDF_DOCUMENT (document);
332 if (pdf_document->scale != scale) {
333 pdf_document->scale = scale;
334 document_display_page (pdf_document);
335 ev_document_scale_changed (EV_DOCUMENT (pdf_document));
340 pdf_document_set_page_offset (EvDocument *document,
344 PdfDocument *pdf_document = PDF_DOCUMENT (document);
346 pdf_document->page_x_offset = x;
347 pdf_document->page_y_offset = y;
351 canonical_multiple_of_90 (gint n)
360 return 90 * (gint)((n / 90.0) + .5);
364 pdf_document_get_page_size (EvDocument *document,
369 PdfDocument *pdf_document = PDF_DOCUMENT (document);
371 int page_width = 1, page_height = 1;
372 double scale = pdf_document->scale;
375 page = pdf_document->page;
377 doc_page = pdf_document->doc->getCatalog ()->getPage (page);
382 page_rotate = canonical_multiple_of_90 (doc_page->getRotate ());
383 if (page_rotate == 90 || page_rotate == 270) {
384 page_width = (int) ((doc_page->getHeight () * scale) + 0.5);
385 page_height = (int) ((doc_page->getWidth () * scale) + 0.5);
386 } else /* if (page_rotate == 0 || page_rotate == 180) */ {
387 page_width = (int) ((doc_page->getWidth () * scale) + 0.5);
388 page_height = (int) ((doc_page->getHeight () * scale) + 0.5);
392 if (width) *width = page_width;
393 if (height) *height = page_height;
397 pdf_document_render (EvDocument *document,
403 PdfDocument *pdf_document = PDF_DOCUMENT (document);
407 if (!pdf_document->target)
410 page.x = pdf_document->page_x_offset;
411 page.y = pdf_document->page_y_offset;
412 page.width = pdf_document->out->getBitmapWidth();
413 page.height = pdf_document->out->getBitmapHeight();
417 draw.width = clip_width;
418 draw.height = clip_height;
420 if (gdk_rectangle_intersect (&page, &draw, &draw))
421 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
422 pdf_document->target,
424 draw.width, draw.height);
430 pdf_document_render_pixbuf (EvDocument *document)
432 PdfDocument *pdf_document = PDF_DOCUMENT (document);
433 SplashOutputDev *output;
437 color.rgb8 = splashMakeRGB8 (255, 255, 255);
439 output = new SplashOutputDev (splashModeRGB8, gFalse, color);
440 output->startDoc (pdf_document->doc->getXRef());
442 pdf_document->doc->displayPage (output,
444 72*pdf_document->scale,
445 72*pdf_document->scale,
448 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
450 output->getBitmap()->getWidth(),
451 output->getBitmap()->getHeight());
453 bitmap_to_pixbuf (output->getBitmap(), pixbuf, 0, 0);
460 pdf_document_find_get_progress (EvDocumentFind *document_find)
462 PdfDocumentSearch *search;
463 int n_pages, pages_done;
465 search = PDF_DOCUMENT (document_find)->search;
467 if (search == NULL) {
471 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
472 if (search->search_page > search->start_page) {
473 pages_done = search->search_page - search->start_page + 1;
474 } else if (search->search_page == search->start_page) {
475 pages_done = n_pages;
477 pages_done = n_pages - search->start_page + search->search_page;
480 return pages_done / (double) n_pages;
484 pdf_document_find_page_has_results (EvDocumentFind *document_find,
487 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
489 g_return_val_if_fail (search != NULL, FALSE);
491 return search->other_page_flags[page];
495 pdf_document_find_get_n_results (EvDocumentFind *document_find)
497 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
500 return search->current_page_results->len;
507 pdf_document_find_get_result (EvDocumentFind *document_find,
509 GdkRectangle *rectangle)
511 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
512 PdfDocumentSearch *search = pdf_document->search;
515 if (search != NULL &&
516 n_result < search->current_page_results->len) {
517 r = g_array_index (search->current_page_results,
518 GdkRectangle, n_result);
520 rectangle->x = r.x + pdf_document->page_x_offset;
521 rectangle->y = r.y + pdf_document->page_y_offset;
522 rectangle->width = r.width;
523 rectangle->height = r.height;
532 pdf_document_search_page_changed (PdfDocumentSearch *search)
534 PdfDocument *pdf_document = search->document;
537 int xMin, yMin, xMax, yMax;
539 current_page = pdf_document->page;
541 if (search->current_page == current_page)
544 /* We need to create current_page_results for the new current page */
545 g_array_set_size (search->current_page_results, 0);
547 if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
548 gTrue, gTrue, // startAtTop, stopAtBottom
549 gFalse, gFalse, // startAtLast, stopAtLast
550 &xMin, &yMin, &xMax, &yMax)) {
553 result.width = xMax - xMin;
554 result.height = yMax - yMin;
556 g_array_append_val (search->current_page_results, result);
557 /* Now find further results */
559 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
562 &xMin, &yMin, &xMax, &yMax)) {
565 result.width = xMax - xMin;
566 result.height = yMax - yMin;
568 g_array_append_val (search->current_page_results, result);
574 pdf_document_search_idle_callback (void *data)
576 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
577 PdfDocument *pdf_document = search->document;
578 int n_pages, changed_page;
579 double xMin, yMin, xMax, yMax;
581 /* Note that PDF page count is 1 through n_pages INCLUSIVE
582 * like a real book. We are looking to add one result for each
583 * page with a match, because the coordinates are meaningless
584 * with TextOutputDev, so we just want to flag matching pages
585 * and then when the user switches to the current page, we
586 * will emit "found" again with the real results.
588 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
590 if (search->output_dev == 0) {
591 /* First time through here... */
592 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
593 if (!search->output_dev->isOk()) {
598 pdf_document->doc->displayPage (search->output_dev,
600 72, 72, 0, gTrue, gFalse);
602 if (search->output_dev->findText (search->ucs4,
604 gTrue, gTrue, // startAtTop, stopAtBottom
605 gFalse, gFalse, // startAtLast, stopAtLast
606 &xMin, &yMin, &xMax, &yMax)) {
607 /* This page has results */
608 search->other_page_flags[search->search_page] = 1;
610 search->other_page_flags[search->search_page] = 0;
613 changed_page = search->start_page;
615 search->search_page += 1;
616 if (search->search_page > n_pages) {
618 search->search_page = 1;
621 if (search->search_page != search->start_page) {
622 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
629 search->idle = 0; /* will return FALSE to remove */
634 pdf_document_find_begin (EvDocumentFind *document,
635 const char *search_string,
636 gboolean case_sensitive)
638 PdfDocument *pdf_document = PDF_DOCUMENT (document);
639 PdfDocumentSearch *search;
644 /* FIXME handle case_sensitive (right now XPDF
645 * code is always case insensitive for ASCII
646 * and case sensitive for all other languaages)
649 g_assert (sizeof (gunichar) == sizeof (Unicode));
650 ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
653 if (pdf_document->search &&
654 pdf_document->search->ucs4_len == ucs4_len &&
655 memcmp (pdf_document->search->ucs4,
657 sizeof (gunichar) * ucs4_len) == 0) {
658 /* Search is unchanged */
663 if (pdf_document->search) {
664 pdf_document_search_free (pdf_document->search);
665 pdf_document->search = NULL;
668 search = g_new0 (PdfDocumentSearch, 1);
671 search->ucs4_len = ucs4_len;
673 search->current_page_results = g_array_new (FALSE,
675 sizeof (GdkRectangle));
676 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document));
678 search->other_page_flags = g_new0 (int, n_pages + 1);
679 for (i = 0; i <= n_pages; i++) {
680 search->other_page_flags[i] = -1;
683 search->document = pdf_document;
685 /* We add at low priority so the progress bar repaints */
686 search->idle = g_idle_add_full (G_PRIORITY_LOW,
687 pdf_document_search_idle_callback,
691 search->output_dev = 0;
693 search->start_page = pdf_document->page;
694 search->search_page = search->start_page;
696 search->current_page = -1;
698 pdf_document->search = search;
700 /* Update for the current page right away */
701 pdf_document_search_page_changed (search);
705 pdf_document_find_cancel (EvDocumentFind *document)
707 PdfDocument *pdf_document = PDF_DOCUMENT (document);
709 if (pdf_document->search) {
710 pdf_document_search_free (pdf_document->search);
711 pdf_document->search = NULL;
716 pdf_document_search_free (PdfDocumentSearch *search)
718 if (search->idle != 0)
719 g_source_remove (search->idle);
721 if (search->output_dev)
722 delete search->output_dev;
724 g_array_free (search->current_page_results, TRUE);
725 g_free (search->other_page_flags);
727 g_free (search->ucs4);
732 pdf_document_has_document_security (EvDocumentSecurity *document_security)
734 /* FIXME: do we really need to have this? */
739 pdf_document_set_password (EvDocumentSecurity *document_security,
740 const char *password)
742 PdfDocument *document = PDF_DOCUMENT (document_security);
744 if (document->password)
745 g_free (document->password);
747 document->password = g_strdup (password);
751 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
753 PdfDocument *document = PDF_DOCUMENT (exporter);
755 if (document->ps_out)
756 delete document->ps_out;
758 document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
759 document->doc->getCatalog(), 1,
760 pdf_document_get_n_pages (EV_DOCUMENT (document)),
765 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
767 PdfDocument *document = PDF_DOCUMENT (exporter);
769 document->doc->displayPage (document->ps_out, page,
770 72.0, 72.0, 0, gTrue, gFalse);
774 pdf_document_ps_export_end (EvPSExporter *exporter)
776 PdfDocument *document = PDF_DOCUMENT (exporter);
778 delete document->ps_out;
779 document->ps_out = NULL;
783 /* EvDocumentLinks Implementation */
792 unicode_to_char (OutlineItem *outline_item,
796 gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
799 for (i = 0; i < outline_item->getTitleLength(); ++i) {
800 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
804 return g_strdup (gstr.getCString ());
809 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
811 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
814 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
816 outline = pdf_document->doc->getOutline();
817 if (outline->getItems() != NULL &&
818 outline->getItems()->getLength() > 0)
825 build_tree (PdfDocument *pdf_document,
832 for (i = 0; i < items->getLength (); i++) {
835 LinkAction *link_action;
839 item = (OutlineItem *)items->get (i);
840 gtk_tree_store_append (GTK_TREE_STORE (model), &iter, parent);
842 link_action = item->getAction ();
843 title = unicode_to_char (item, pdf_document->umap);
844 link = build_link_from_action (pdf_document, link_action, title);
846 gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
847 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title,
848 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
852 if (item->hasKids () && item->getKids ()) {
853 build_tree (pdf_document,
861 static GtkTreeModel *
862 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
864 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
865 GtkTreeModel *model = NULL;
869 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
871 outline = pdf_document->doc->getOutline();
872 items = outline->getItems();
874 /* Create the model iff we have items*/
876 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
879 build_tree (pdf_document, model, NULL, items);
886 build_link_from_action (PdfDocument *pdf_document,
887 LinkAction *link_action,
892 if (link_action == NULL) {
893 link = ev_link_new_title (title);
894 } else if (link_action->getKind () == actionGoToR) {
895 g_warning ("actionGoToR links not implemented");
896 } else if (link_action->getKind () == actionLaunch) {
897 g_warning ("actionLaunch links not implemented");
898 } else if (link_action->getKind () == actionNamed) {
899 g_warning ("actionNamed links not implemented");
900 } else if (link_action->getKind () == actionMovie) {
901 g_warning ("actionMovie links not implemented");
902 } else if (link_action->getKind () == actionGoTo) {
907 GooString *named_dest;
909 link_goto = dynamic_cast <LinkGoTo *> (link_action);
910 link_dest = link_goto->getDest ();
911 named_dest = link_goto->getNamedDest ();
913 /* Wow! This seems excessively slow on large
914 * documents. I need to investigate more... -jrb */
915 if (link_dest != NULL) {
916 link_dest = link_dest->copy ();
917 } else if (named_dest != NULL) {
918 named_dest = named_dest->copy ();
919 link_dest = pdf_document->doc->findDest (named_dest);
922 if (link_dest != NULL) {
923 if (link_dest->isPageRef ()) {
924 page_ref = link_dest->getPageRef ();
925 page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
927 page_num = link_dest->getPageNum ();
932 link = ev_link_new_page (title, page_num);
933 } else if (link_action->getKind () == actionURI) {
936 link_uri = dynamic_cast <LinkURI *> (link_action);
937 link = ev_link_new_external
938 (title, link_uri->getURI()->getCString());
939 } else if (link_action->getKind () == actionUnknown) {
940 LinkUnknown *link_unknown;
942 link_unknown = dynamic_cast <LinkUnknown *> (link_action);
944 g_warning ("Unknown link type %s",
945 link_unknown->getAction()->getCString());
949 link = ev_link_new_title (title);
957 static EvDocumentLinksIter *
958 pdf_document_links_begin_read (EvDocumentLinks *document_links)
960 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
965 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
967 outline = pdf_document->doc->getOutline();
968 items = outline->getItems();
972 iter = g_new0 (LinksIter, 1);
977 return (EvDocumentLinksIter *) iter;
980 /* FIXME This returns a new object every time, probably we should cache it
983 pdf_document_links_get_link (EvDocumentLinks *document_links,
984 EvDocumentLinksIter *links_iter)
986 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
987 LinksIter *iter = (LinksIter *)links_iter;
989 LinkAction *link_action;
993 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
994 g_return_val_if_fail (iter != NULL, FALSE);
996 anItem = (OutlineItem *)iter->items->get(iter->index);
997 link_action = anItem->getAction ();
998 link_title = anItem->getTitle ();
999 title = unicode_to_char (anItem, pdf_document->umap);
1001 return build_link_from_action (pdf_document, link_action, title);
1004 static EvDocumentLinksIter *
1005 pdf_document_links_get_child (EvDocumentLinks *document_links,
1006 EvDocumentLinksIter *links_iter)
1008 LinksIter *iter = (LinksIter *)links_iter;
1009 LinksIter *child_iter;
1010 OutlineItem *anItem;
1012 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
1014 anItem = (OutlineItem *)iter->items->get(iter->index);
1016 if (! (anItem->hasKids() && anItem->getKids()) )
1019 child_iter = g_new0 (LinksIter, 1);
1020 child_iter->index = 0;
1021 child_iter->level = iter->level + 1;
1022 child_iter->items = anItem->getKids ();
1023 g_assert (child_iter->items);
1025 return (EvDocumentLinksIter *) child_iter;
1029 pdf_document_links_next (EvDocumentLinks *document_links,
1030 EvDocumentLinksIter *links_iter)
1032 LinksIter *iter = (LinksIter *) links_iter;
1034 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
1037 if (iter->index >= iter->items->getLength())
1044 pdf_document_links_free_iter (EvDocumentLinks *document_links,
1045 EvDocumentLinksIter *iter)
1047 g_return_if_fail (PDF_IS_DOCUMENT (document_links));
1048 g_return_if_fail (iter != NULL);
1050 /* FIXME: Should I close all the nodes?? Free them? */
1056 pdf_document_finalize (GObject *object)
1058 PdfDocument *pdf_document = PDF_DOCUMENT (object);
1060 if (pdf_document->links) {
1061 delete pdf_document->links;
1064 if (pdf_document->umap) {
1065 pdf_document->umap->decRefCnt ();
1066 pdf_document->umap = NULL;
1069 if (pdf_document->search)
1070 pdf_document_search_free (pdf_document->search);
1072 if (pdf_document->target)
1073 g_object_unref (pdf_document->target);
1075 if (pdf_document->out)
1076 delete pdf_document->out;
1077 if (pdf_document->ps_out)
1078 delete pdf_document->ps_out;
1079 if (pdf_document->doc)
1080 delete pdf_document->doc;
1085 pdf_document_set_property (GObject *object,
1087 const GValue *value,
1100 has_unicode_marker (GooString *string)
1102 return ((string->getChar (0) & 0xff) == 0xfe &&
1103 (string->getChar (1) & 0xff) == 0xff);
1107 pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
1112 g_return_val_if_fail (info_dict != NULL, NULL);
1113 g_return_val_if_fail (key != NULL, NULL);
1115 if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
1120 value = obj.getString ();
1122 if (has_unicode_marker (value)) {
1123 result = g_convert (value->getCString () + 2,
1124 value->getLength () - 2,
1125 "UTF-8", "UTF-16BE", NULL, NULL, NULL);
1127 result = g_strndup (value->getCString (), value->getLength ());
1136 pdf_document_get_title (PdfDocument *pdf_document)
1141 if (pdf_document->doc == NULL)
1143 pdf_document->doc->getDocInfo (&info);
1145 if (info.isDict ()) {
1146 title = pdf_info_dict_get_string (info.getDict(), "Title");
1153 pdf_document_get_text (EvDocument *document, GdkRectangle *rect)
1155 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1156 GooString *sel_text = new GooString;
1160 x1 = rect->x - pdf_document->page_x_offset;
1161 y1 = rect->y - pdf_document->page_y_offset;
1162 x2 = x1 + rect->width;
1163 y2 = y1 + rect->height;
1165 sel_text = pdf_document->out->getText (x1, y1, x2, y2);
1166 text = sel_text->getCString ();
1168 return text ? g_strdup (text) : NULL;
1172 pdf_document_get_link (EvDocument *document, int x, int y)
1174 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1176 double link_x, link_y;
1178 if (pdf_document->links == NULL) {
1183 link_x = x - pdf_document->page_x_offset;
1184 link_y = y - pdf_document->page_y_offset;
1187 link_y = pdf_document->out->getBitmapHeight() - link_y;
1190 link_x = link_x / pdf_document->scale;
1191 link_y = link_y / pdf_document->scale;
1193 action = pdf_document->links->find (link_x, link_y);
1196 return build_link_from_action (pdf_document, action, "");
1203 pdf_document_get_property (GObject *object,
1208 PdfDocument *pdf_document = PDF_DOCUMENT (object);
1214 title = pdf_document_get_title (pdf_document);
1215 g_value_set_string (value, title);
1222 pdf_document_class_init (PdfDocumentClass *klass)
1224 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1226 gobject_class->finalize = pdf_document_finalize;
1227 gobject_class->get_property = pdf_document_get_property;
1228 gobject_class->set_property = pdf_document_set_property;
1230 g_object_class_override_property (gobject_class, PROP_TITLE, "title");
1234 pdf_document_document_iface_init (EvDocumentIface *iface)
1236 iface->load = pdf_document_load;
1237 iface->save = pdf_document_save;
1238 iface->get_text = pdf_document_get_text;
1239 iface->get_link = pdf_document_get_link;
1240 iface->get_n_pages = pdf_document_get_n_pages;
1241 iface->set_page = pdf_document_set_page;
1242 iface->get_page = pdf_document_get_page;
1243 iface->set_scale = pdf_document_set_scale;
1244 iface->set_target = pdf_document_set_target;
1245 iface->set_page_offset = pdf_document_set_page_offset;
1246 iface->get_page_size = pdf_document_get_page_size;
1247 iface->render = pdf_document_render;
1248 iface->render_pixbuf = pdf_document_render_pixbuf;
1252 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1254 iface->begin = pdf_document_ps_export_begin;
1255 iface->do_page = pdf_document_ps_export_do_page;
1256 iface->end = pdf_document_ps_export_end;
1261 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1263 iface->begin = pdf_document_find_begin;
1264 iface->get_n_results = pdf_document_find_get_n_results;
1265 iface->get_result = pdf_document_find_get_result;
1266 iface->page_has_results = pdf_document_find_page_has_results;
1267 iface->get_progress = pdf_document_find_get_progress;
1268 iface->cancel = pdf_document_find_cancel;
1272 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
1274 iface->has_document_security = pdf_document_has_document_security;
1275 iface->set_password = pdf_document_set_password;
1279 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1281 iface->has_document_links = pdf_document_links_has_document_links;
1282 iface->get_links_model = pdf_document_links_get_links_model;
1288 bitmap_to_pixbuf (SplashBitmap *bitmap,
1295 SplashColorPtr dataPtr;
1298 gboolean target_has_alpha;
1299 gint target_rowstride;
1300 guchar *target_data;
1302 width = bitmap->getWidth ();
1303 height = bitmap->getHeight ();
1305 if (width + x_offset > gdk_pixbuf_get_width (target))
1306 width = gdk_pixbuf_get_width (target) - x_offset;
1307 if (height + y_offset > gdk_pixbuf_get_height (target))
1308 height = gdk_pixbuf_get_height (target) - x_offset;
1310 target_has_alpha = gdk_pixbuf_get_has_alpha (target);
1311 target_rowstride = gdk_pixbuf_get_rowstride (target);
1312 target_data = gdk_pixbuf_get_pixels (target);
1314 dataPtr = bitmap->getDataPtr ();
1316 for (y = 0; y < height; y++) {
1321 p = dataPtr.rgb8 + y * width;
1322 q = target_data + ((y + y_offset) * target_rowstride +
1323 x_offset * (target_has_alpha?4:3));
1324 for (x = 0; x < width; x++) {
1327 *q++ = splashRGB8R (rgb);
1328 *q++ = splashRGB8G (rgb);
1329 *q++ = splashRGB8B (rgb);
1331 if (target_has_alpha)
1341 pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
1342 gdouble scale_factor,
1348 SplashOutputDev *output;
1352 color.rgb8 = splashMakeRGB8 (255, 255, 255);
1354 output = new SplashOutputDev (splashModeRGB8, gFalse, color);
1355 output->startDoc (pdf_document->doc->getXRef());
1356 pdf_document->doc->displayPage (output,
1362 width = output->getBitmap()->getWidth();
1363 height = output->getBitmap()->getHeight();
1366 pixbuf = ev_document_misc_get_thumbnail_frame (width, height, NULL);
1367 bitmap_to_pixbuf (output->getBitmap(), pixbuf, 1, 1);
1369 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
1371 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1372 bitmap_to_pixbuf (output->getBitmap(), pixbuf, 0, 0);
1381 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1387 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1390 Thumb *thumb = NULL;
1392 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1393 the_page->getThumb (&the_thumb);
1395 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1396 /* Build the thumbnail object */
1397 thumb = new Thumb(pdf_document->doc->getXRef (),
1400 *width = thumb->getWidth ();
1401 *height = thumb->getHeight ();
1403 double page_width, page_height;
1405 page_width = the_page->getWidth ();
1406 page_height = the_page->getHeight ();
1408 if (page_width > page_height) {
1410 *height = (int) (size * page_height / page_width);
1412 *width = (int) (size * page_width / page_height);
1419 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1424 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1425 GdkPixbuf *thumbnail;
1428 Thumb *thumb = NULL;
1429 gboolean have_ethumbs = FALSE;
1431 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1432 the_page->getThumb(&the_thumb);
1434 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1435 /* Build the thumbnail object */
1436 thumb = new Thumb(pdf_document->doc->getXRef (),
1439 have_ethumbs = thumb->ok();
1444 GdkPixbuf *tmp_pixbuf;
1446 data = thumb->getPixbufData();
1447 tmp_pixbuf = gdk_pixbuf_new_from_data (data,
1452 thumb->getHeight (),
1453 thumb->getWidth () * 3,
1455 /* FIXME: do we want to check that the thumb's size isn't ridiculous?? */
1456 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
1457 g_object_unref (tmp_pixbuf);
1460 double scale_factor;
1462 pdf_document_thumbnails_get_dimensions (document_thumbnails, page, size,
1464 scale_factor = width / the_page->getWidth ();
1465 thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
1476 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1478 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1479 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1484 pdf_document_init (PdfDocument *pdf_document)
1486 pdf_document->page = 1;
1487 pdf_document->page_x_offset = 0;
1488 pdf_document->page_y_offset = 0;
1489 pdf_document->scale = 1.;
1491 pdf_document->password = NULL;