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-bookmarks.h"
28 #include "ev-document-misc.h"
29 #include "ev-document-thumbnails.h"
31 #include "GlobalParams.h"
32 #include "GDKSplashOutputDev.h"
33 #include "SplashBitmap.h"
36 #include "UnicodeMap.h"
37 #include "GlobalParams.h"
40 #include "goo/GList.h"
41 #include "PSOutputDev.h"
50 PdfDocument *document;
54 /* full results are only possible for the rendered current page */
56 GArray *current_page_results;
57 guchar *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;
91 PdfDocumentSearch *search;
94 static void pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface);
95 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
96 static void pdf_document_document_iface_init (EvDocumentIface *iface);
97 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface);
98 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
99 static void pdf_document_search_free (PdfDocumentSearch *search);
100 static void pdf_document_search_page_changed (PdfDocumentSearch *search);
102 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
104 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
105 pdf_document_document_iface_init);
106 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_BOOKMARKS,
107 pdf_document_document_bookmarks_iface_init);
108 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
109 pdf_document_document_thumbnails_iface_init);
110 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
111 pdf_document_ps_exporter_iface_init);
112 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
113 pdf_document_find_iface_init);
117 document_validate_page (PdfDocument *pdf_document)
119 if (!pdf_document->page_valid) {
120 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
121 72 * pdf_document->scale,
122 72 * pdf_document->scale,
125 pdf_document->page_valid = TRUE;
127 /* Update the search results available to the app since
128 * we only provide full results on the current page
130 if (pdf_document->search)
131 pdf_document_search_page_changed (pdf_document->search);
134 return pdf_document->page_valid;
138 pdf_document_load (EvDocument *document,
142 PdfDocument *pdf_document = PDF_DOCUMENT (document);
150 globalParams = new GlobalParams("/etc/xpdfrc");
151 globalParams->setupBaseFontsFc(NULL);
154 if (! pdf_document->umap) {
155 enc = new GString("UTF-8");
156 pdf_document->umap = globalParams->getUnicodeMap(enc);
157 pdf_document->umap->incRefCnt ();
161 filename = g_filename_from_uri (uri, NULL, error);
165 filename_g = new GString (filename);
168 // open the PDF file, assumes ownership of filename_g
169 newDoc = new PDFDoc(filename_g, 0, 0);
171 if (!newDoc->isOk()) {
172 err = newDoc->getErrorCode();
175 /* FIXME: Add a real error enum to EvDocument */
176 g_set_error (error, G_FILE_ERROR,
178 "Failed to load document (error %d) '%s'\n",
185 if (pdf_document->doc)
186 delete pdf_document->doc;
187 pdf_document->doc = newDoc;
189 pdf_document->page = 1;
191 if (pdf_document->out)
192 pdf_document->out->startDoc(pdf_document->doc->getXRef());
194 pdf_document->page_valid = FALSE;
196 g_object_notify (G_OBJECT (pdf_document), "title");
202 pdf_document_save (EvDocument *document,
206 PdfDocument *pdf_document = PDF_DOCUMENT (document);
208 gboolean retval = FALSE;
210 filename = g_filename_from_uri (uri, NULL, error);
211 if (filename != NULL) {
212 GString *fname = new GString (filename);
214 retval = pdf_document->doc->saveAs (fname);
221 pdf_document_get_n_pages (EvDocument *document)
223 PdfDocument *pdf_document = PDF_DOCUMENT (document);
225 if (pdf_document->doc)
226 return pdf_document->doc->getNumPages();
232 pdf_document_set_page (EvDocument *document,
235 PdfDocument *pdf_document = PDF_DOCUMENT (document);
237 page = CLAMP (page, 1, ev_document_get_n_pages (document));
239 if (page != pdf_document->page) {
240 pdf_document->page = page;
241 pdf_document->page_valid = FALSE;
244 ev_document_changed (document);
248 pdf_document_get_page (EvDocument *document)
250 PdfDocument *pdf_document = PDF_DOCUMENT (document);
252 return pdf_document->page;
256 redraw_callback (void *data)
258 /* Need to hook up through a EvDocument callback? */
262 pdf_document_set_target (EvDocument *document,
265 PdfDocument *pdf_document = PDF_DOCUMENT (document);
267 if (pdf_document->target != target) {
268 if (pdf_document->target)
269 g_object_unref (pdf_document->target);
271 pdf_document->target = target;
273 if (pdf_document->target)
274 g_object_ref (pdf_document->target);
276 if (pdf_document->out) {
277 delete pdf_document->out;
278 pdf_document->out = NULL;
281 if (pdf_document->target) {
282 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
283 redraw_callback, (void*) document);
285 if (pdf_document->doc)
286 pdf_document->out->startDoc(pdf_document->doc->getXRef());
290 pdf_document->page_valid = FALSE;
295 pdf_document_set_scale (EvDocument *document,
298 PdfDocument *pdf_document = PDF_DOCUMENT (document);
300 if (pdf_document->scale != scale) {
301 pdf_document->scale = scale;
302 pdf_document->page_valid = FALSE;
307 pdf_document_set_page_offset (EvDocument *document,
311 PdfDocument *pdf_document = PDF_DOCUMENT (document);
313 pdf_document->page_x_offset = x;
314 pdf_document->page_y_offset = y;
318 pdf_document_get_page_size (EvDocument *document,
322 PdfDocument *pdf_document = PDF_DOCUMENT (document);
324 if (document_validate_page (pdf_document)) {
326 *width = pdf_document->out->getBitmapWidth();
328 *height = pdf_document->out->getBitmapHeight();
338 pdf_document_render (EvDocument *document,
344 PdfDocument *pdf_document = PDF_DOCUMENT (document);
348 if (!document_validate_page (pdf_document) || !pdf_document->target)
351 page.x = pdf_document->page_x_offset;
352 page.y = pdf_document->page_y_offset;
353 page.width = pdf_document->out->getBitmapWidth();
354 page.height = pdf_document->out->getBitmapHeight();
358 draw.width = clip_width;
359 draw.height = clip_height;
361 if (gdk_rectangle_intersect (&page, &draw, &draw))
362 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
363 pdf_document->target,
365 draw.width, draw.height);
369 pdf_document_search_emit_found (PdfDocumentSearch *search)
371 PdfDocument *pdf_document = search->document;
377 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
378 if (search->search_page > search->start_page) {
379 pages_done = search->search_page - search->start_page;
380 } else if (search->search_page == search->start_page) {
381 pages_done = n_pages;
383 pages_done = n_pages - search->start_page + search->search_page;
386 tmp_results = g_array_new (FALSE, FALSE, sizeof (EvFindResult));
387 g_array_append_vals (tmp_results,
388 search->current_page_results->data,
389 search->current_page_results->len);
391 /* Now append a bogus element for each page that has a result in it,
392 * that is not the current page
395 while (i <= n_pages) {
396 if (i != pdf_document->page &&
397 search->other_page_flags[i]) {
402 /* Use bogus coordinates, again we can't get coordinates
403 * until this is the current page because TextOutputDev
406 result.highlight_area.x = -1;
407 result.highlight_area.y = -1;
408 result.highlight_area.width = 1;
409 result.highlight_area.height = 1;
411 g_array_append_val (tmp_results, result);
417 ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
418 (EvFindResult*) tmp_results->data,
420 pages_done / (double) n_pages);
422 g_array_free (tmp_results, TRUE);
426 pdf_document_search_page_changed (PdfDocumentSearch *search)
428 PdfDocument *pdf_document = search->document;
431 int xMin, yMin, xMax, yMax;
433 current_page = pdf_document->page;
435 if (!pdf_document->page_valid) {
436 /* we can't do anything until displayPage() */
437 search->current_page = -1;
441 if (search->current_page == current_page)
444 /* We need to create current_page_results for the new current page */
445 g_array_set_size (search->current_page_results, 0);
447 if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
448 gTrue, gTrue, // startAtTop, stopAtBottom
449 gFalse, gFalse, // startAtLast, stopAtLast
450 &xMin, &yMin, &xMax, &yMax)) {
451 result.page_num = pdf_document->page;
453 result.highlight_area.x = xMin;
454 result.highlight_area.y = yMin;
455 result.highlight_area.width = xMax - xMin;
456 result.highlight_area.height = yMax - yMin;
458 g_array_append_val (search->current_page_results, result);
459 /* Now find further results */
461 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
464 &xMin, &yMin, &xMax, &yMax)) {
466 result.page_num = pdf_document->page;
468 result.highlight_area.x = xMin;
469 result.highlight_area.y = yMin;
470 result.highlight_area.width = xMax - xMin;
471 result.highlight_area.height = yMax - yMin;
473 g_array_append_val (search->current_page_results, result);
477 /* needed for the initial current page since we don't search
480 search->other_page_flags[current_page] =
481 search->current_page_results->len > 0;
483 pdf_document_search_emit_found (search);
487 pdf_document_search_idle_callback (void *data)
489 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
490 PdfDocument *pdf_document = search->document;
492 double xMin, yMin, xMax, yMax;
494 /* Note that PDF page count is 1 through n_pages INCLUSIVE
495 * like a real book. We are looking to add one result for each
496 * page with a match, because the coordinates are meaningless
497 * with TextOutputDev, so we just want to flag matching pages
498 * and then when the user switches to the current page, we
499 * will emit "found" again with the real results.
501 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
503 if (search->search_page == search->start_page) {
507 if (search->output_dev == 0) {
508 /* First time through here... */
509 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
510 if (!search->output_dev->isOk()) {
515 pdf_document->doc->displayPage (search->output_dev,
517 72, 72, 0, gTrue, gFalse);
519 if (search->output_dev->findText (search->ucs4,
521 gTrue, gTrue, // startAtTop, stopAtBottom
522 gFalse, gFalse, // startAtLast, stopAtLast
523 &xMin, &yMin, &xMax, &yMax)) {
524 /* This page has results */
525 search->other_page_flags[search->search_page] = TRUE;
528 search->search_page += 1;
529 if (search->search_page > n_pages) {
531 search->search_page = 1;
534 /* We do this even if nothing was found, to update the percent complete */
535 pdf_document_search_emit_found (search);
541 search->idle = 0; /* will return FALSE to remove */
546 pdf_document_find_begin (EvDocumentFind *document,
547 const char *search_string,
548 gboolean case_sensitive)
550 PdfDocument *pdf_document = PDF_DOCUMENT (document);
551 PdfDocumentSearch *search;
556 /* FIXME handle case_sensitive (right now XPDF
557 * code is always case insensitive for ASCII
558 * and case sensitive for all other languaages)
561 g_assert (sizeof (gunichar) == sizeof (Unicode));
562 ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
565 if (pdf_document->search &&
566 pdf_document->search->ucs4_len == ucs4_len &&
567 memcmp (pdf_document->search->ucs4,
569 sizeof (gunichar) * ucs4_len) == 0) {
570 /* Search is unchanged */
575 if (pdf_document->search) {
576 pdf_document_search_free (pdf_document->search);
577 pdf_document->search = NULL;
580 search = g_new0 (PdfDocumentSearch, 1);
583 search->ucs4_len = ucs4_len;
585 search->current_page_results = g_array_new (FALSE,
587 sizeof (EvFindResult));
588 n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
590 /* This is an array of bool; with the first value ignored
591 * so we can index by the based-at-1 page numbers
593 search->other_page_flags = g_new0 (guchar, n_pages + 1);
595 search->document = pdf_document;
597 /* We add at low priority so the progress bar repaints */
598 search->idle = g_idle_add_full (G_PRIORITY_LOW,
599 pdf_document_search_idle_callback,
603 search->output_dev = 0;
605 search->start_page = pdf_document->page;
606 search->search_page = search->start_page + 1;
607 if (search->search_page > n_pages)
608 search->search_page = 1;
610 search->current_page = -1;
612 pdf_document->search = search;
614 /* Update for the current page right away */
615 pdf_document_search_page_changed (search);
619 pdf_document_find_cancel (EvDocumentFind *document)
621 PdfDocument *pdf_document = PDF_DOCUMENT (document);
623 if (pdf_document->search) {
624 pdf_document_search_free (pdf_document->search);
625 pdf_document->search = NULL;
630 pdf_document_search_free (PdfDocumentSearch *search)
632 if (search->idle != 0)
633 g_source_remove (search->idle);
635 if (search->output_dev)
636 delete search->output_dev;
638 g_array_free (search->current_page_results, TRUE);
639 g_free (search->other_page_flags);
641 g_free (search->ucs4);
646 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
648 PdfDocument *document = PDF_DOCUMENT (exporter);
650 if (document->ps_out)
651 delete document->ps_out;
653 document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
654 document->doc->getCatalog(), 1,
655 ev_document_get_n_pages (EV_DOCUMENT (document)),
660 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
662 PdfDocument *document = PDF_DOCUMENT (exporter);
664 document->doc->displayPage (document->ps_out, page,
665 72.0, 72.0, 0, gTrue, gFalse);
669 pdf_document_ps_export_end (EvPSExporter *exporter)
671 PdfDocument *document = PDF_DOCUMENT (exporter);
673 delete document->ps_out;
674 document->ps_out = NULL;
678 /* EvDocumentBookmarks Implementation */
681 /* goo GList, not glib */
688 unicode_to_char (OutlineItem *outline_item,
692 gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
695 for (i = 0; i < outline_item->getTitleLength(); ++i) {
696 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
700 return g_strdup (gstr.getCString ());
705 pdf_document_bookmarks_has_document_bookmarks (EvDocumentBookmarks *document_bookmarks)
707 PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
710 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
712 outline = pdf_document->doc->getOutline();
713 if (outline->getItems() != NULL &&
714 outline->getItems()->getLength() > 0)
720 static EvDocumentBookmarksIter *
721 pdf_document_bookmarks_begin_read (EvDocumentBookmarks *document_bookmarks)
723 PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
728 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), NULL);
730 outline = pdf_document->doc->getOutline();
731 items = outline->getItems();
735 iter = g_new0 (BookmarksIter, 1);
740 return (EvDocumentBookmarksIter *) iter;
744 pdf_document_bookmarks_get_bookmark (EvDocumentBookmarks *document_bookmarks,
745 EvDocumentBookmarksIter *bookmarks_iter)
747 PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
748 EvBookmark *bookmark = NULL;
749 BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
751 LinkAction *link_action;
755 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
756 g_return_val_if_fail (iter != NULL, FALSE);
758 anItem = (OutlineItem *)iter->items->get(iter->index);
759 link_action = anItem->getAction ();
760 link_title = anItem->getTitle ();
761 title = unicode_to_char (anItem, pdf_document->umap);
763 if (link_action == NULL) {
764 bookmark = ev_bookmark_new_title (title);
765 } else if (link_action->getKind () == actionGoTo) {
772 link_goto = dynamic_cast <LinkGoTo *> (link_action);
773 link_dest = link_goto->getDest ();
774 named_dest = link_goto->getNamedDest ();
776 /* Wow! This seems excessively slow on large
777 * documents. I need to investigate more... -jrb */
778 if (link_dest != NULL) {
779 link_dest = link_dest->copy ();
780 } else if (named_dest != NULL) {
781 named_dest = named_dest->copy ();
782 link_dest = pdf_document->doc->findDest (named_dest);
785 if (link_dest != NULL) {
786 if (link_dest->isPageRef ()) {
787 page_ref = link_dest->getPageRef ();
788 page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
790 page_num = link_dest->getPageNum ();
795 bookmark = ev_bookmark_new_link (title, page_num);
796 } else if (link_action->getKind () == actionURI) {
799 link_uri = dynamic_cast <LinkURI *> (link_action);
800 bookmark = ev_bookmark_new_external
801 (title, link_uri->getURI()->getCString());
802 } else if (link_action->getKind () == actionNamed) {
809 static EvDocumentBookmarksIter *
810 pdf_document_bookmarks_get_child (EvDocumentBookmarks *document_bookmarks,
811 EvDocumentBookmarksIter *bookmarks_iter)
813 BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
814 BookmarksIter *child_iter;
817 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
819 anItem = (OutlineItem *)iter->items->get(iter->index);
821 if (! (anItem->hasKids() && anItem->getKids()) )
824 child_iter = g_new0 (BookmarksIter, 1);
825 child_iter->index = 0;
826 child_iter->level = iter->level + 1;
827 child_iter->items = anItem->getKids ();
828 g_assert (child_iter->items);
830 return (EvDocumentBookmarksIter *) child_iter;
834 pdf_document_bookmarks_next (EvDocumentBookmarks *document_bookmarks,
835 EvDocumentBookmarksIter *bookmarks_iter)
837 BookmarksIter *iter = (BookmarksIter *) bookmarks_iter;
839 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
842 if (iter->index >= iter->items->getLength())
849 pdf_document_bookmarks_free_iter (EvDocumentBookmarks *document_bookmarks,
850 EvDocumentBookmarksIter *iter)
852 g_return_if_fail (PDF_IS_DOCUMENT (document_bookmarks));
853 g_return_if_fail (iter != NULL);
855 /* FIXME: Should I close all the nodes?? Free them? */
860 pdf_document_finalize (GObject *object)
862 PdfDocument *pdf_document = PDF_DOCUMENT (object);
864 if (pdf_document->umap) {
865 pdf_document->umap->decRefCnt ();
866 pdf_document->umap = NULL;
869 if (pdf_document->search)
870 pdf_document_search_free (pdf_document->search);
872 if (pdf_document->target)
873 g_object_unref (pdf_document->target);
875 if (pdf_document->out)
876 delete pdf_document->out;
877 if (pdf_document->ps_out)
878 delete pdf_document->ps_out;
879 if (pdf_document->doc)
880 delete pdf_document->doc;
885 pdf_document_set_property (GObject *object,
900 has_unicode_marker (GString *string)
902 return ((string->getChar (0) & 0xff) == 0xfe &&
903 (string->getChar (1) & 0xff) == 0xff);
907 pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
912 g_return_val_if_fail (info_dict != NULL, NULL);
913 g_return_val_if_fail (key != NULL, NULL);
915 if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
917 return g_strdup (_("Unknown"));
920 value = obj.getString ();
922 if (has_unicode_marker (value)) {
923 result = g_convert (value->getCString () + 2,
924 value->getLength () - 2,
925 "UTF-8", "UTF-16BE", NULL, NULL, NULL);
927 result = g_strndup (value->getCString (), value->getLength ());
936 pdf_document_get_title (PdfDocument *pdf_document)
941 pdf_document->doc->getDocInfo (&info);
943 if (info.isDict ()) {
944 title = pdf_info_dict_get_string (info.getDict(), "Title");
951 pdf_document_get_text (EvDocument *document, GdkRectangle *rect)
953 PdfDocument *pdf_document = PDF_DOCUMENT (document);
954 GString *sel_text = new GString;
960 x2 = x1 + rect->width;
961 y2 = y1 + rect->height;
963 sel_text = pdf_document->out->getText (x1, y1, x2, y2);
964 text = sel_text->getCString ();
966 return text ? g_strdup (text) : NULL;
970 pdf_document_get_property (GObject *object,
975 PdfDocument *pdf_document = PDF_DOCUMENT (object);
981 title = pdf_document_get_title (pdf_document);
982 g_value_set_string (value, title);
989 pdf_document_class_init (PdfDocumentClass *klass)
991 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
993 gobject_class->finalize = pdf_document_finalize;
994 gobject_class->get_property = pdf_document_get_property;
995 gobject_class->set_property = pdf_document_set_property;
997 g_object_class_override_property (gobject_class, PROP_TITLE, "title");
1001 pdf_document_document_iface_init (EvDocumentIface *iface)
1003 iface->load = pdf_document_load;
1004 iface->save = pdf_document_save;
1005 iface->get_text = pdf_document_get_text;
1006 iface->get_n_pages = pdf_document_get_n_pages;
1007 iface->set_page = pdf_document_set_page;
1008 iface->get_page = pdf_document_get_page;
1009 iface->set_scale = pdf_document_set_scale;
1010 iface->set_target = pdf_document_set_target;
1011 iface->set_page_offset = pdf_document_set_page_offset;
1012 iface->get_page_size = pdf_document_get_page_size;
1013 iface->render = pdf_document_render;
1017 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1019 iface->begin = pdf_document_ps_export_begin;
1020 iface->do_page = pdf_document_ps_export_do_page;
1021 iface->end = pdf_document_ps_export_end;
1026 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1028 iface->begin = pdf_document_find_begin;
1029 iface->cancel = pdf_document_find_cancel;
1033 pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface)
1035 iface->has_document_bookmarks = pdf_document_bookmarks_has_document_bookmarks;
1036 iface->begin_read = pdf_document_bookmarks_begin_read;
1037 iface->get_bookmark = pdf_document_bookmarks_get_bookmark;
1038 iface->get_child = pdf_document_bookmarks_get_child;
1039 iface->next = pdf_document_bookmarks_next;
1040 iface->free_iter = pdf_document_bookmarks_free_iter;
1046 bitmap_to_pixbuf (SplashBitmap *bitmap,
1053 SplashColorPtr dataPtr;
1056 gboolean target_has_alpha;
1057 gint target_rowstride;
1058 guchar *target_data;
1060 width = bitmap->getWidth ();
1061 height = bitmap->getHeight ();
1063 if (width + x_offset > gdk_pixbuf_get_width (target))
1064 width = gdk_pixbuf_get_width (target) - x_offset;
1065 if (height + y_offset > gdk_pixbuf_get_height (target))
1066 height = gdk_pixbuf_get_height (target) - x_offset;
1068 target_has_alpha = gdk_pixbuf_get_has_alpha (target);
1069 target_rowstride = gdk_pixbuf_get_rowstride (target);
1070 target_data = gdk_pixbuf_get_pixels (target);
1072 dataPtr = bitmap->getDataPtr ();
1074 for (y = 0; y < height; y++) {
1079 p = dataPtr.rgb8 + y * width;
1080 q = target_data + ((y + y_offset) * target_rowstride +
1081 x_offset * (target_has_alpha?4:3));
1082 for (x = 0; x < width; x++) {
1085 *q++ = splashRGB8R (rgb);
1086 *q++ = splashRGB8G (rgb);
1087 *q++ = splashRGB8B (rgb);
1089 if (target_has_alpha)
1099 pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
1100 gdouble scale_factor,
1105 SplashOutputDev *output;
1109 color.rgb8 = splashMakeRGB8 (255, 255, 255);
1111 output = new SplashOutputDev (splashModeRGB8, gFalse, color);
1112 output->startDoc (pdf_document->doc->getXRef());
1113 pdf_document->doc->displayPage (output,
1119 pixbuf = ev_document_misc_get_thumbnail_frame (output->getBitmap()->getWidth(),
1120 output->getBitmap()->getHeight(),
1122 bitmap_to_pixbuf (output->getBitmap(), pixbuf, 1, 1);
1129 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1131 gint suggested_width,
1135 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1138 Thumb *thumb = NULL;
1141 /* getPage seems to want page + 1 for some reason; */
1142 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1143 the_page->getThumb (&the_thumb);
1147 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1148 /* Build the thumbnail object */
1149 thumb = new Thumb(pdf_document->doc->getXRef (),
1152 *width = thumb->getWidth ();
1153 *height = thumb->getHeight ();
1155 page_ratio = the_page->getHeight () / the_page->getWidth ();
1156 *width = suggested_width;
1157 *height = (gint) (suggested_width * page_ratio);
1162 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1166 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1167 GdkPixbuf *thumbnail;
1170 Thumb *thumb = NULL;
1171 gboolean have_ethumbs = FALSE;
1175 /* getPage seems to want page + 1 for some reason; */
1176 the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1177 the_page->getThumb(&the_thumb);
1179 page_ratio = the_page->getHeight () / the_page->getWidth ();
1180 dest_height = (gint) (width * page_ratio);
1183 if (!(the_thumb.isNull () || the_thumb.isNone())) {
1184 /* Build the thumbnail object */
1185 thumb = new Thumb(pdf_document->doc->getXRef (),
1188 have_ethumbs = thumb->ok();
1193 GdkPixbuf *tmp_pixbuf;
1195 data = thumb->getPixbufData();
1196 tmp_pixbuf = gdk_pixbuf_new_from_data (data,
1201 thumb->getHeight (),
1202 thumb->getWidth () * 3,
1204 /* FIXME: do we want to check that the thumb's size isn't ridiculous?? */
1205 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
1206 g_object_unref (tmp_pixbuf);
1208 gdouble scale_factor;
1210 scale_factor = (gdouble)width / the_page->getWidth ();
1212 thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
1222 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1224 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1225 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1230 pdf_document_init (PdfDocument *pdf_document)
1232 pdf_document->page = 1;
1233 pdf_document->page_x_offset = 0;
1234 pdf_document->page_y_offset = 0;
1235 pdf_document->scale = 1.;
1237 pdf_document->page_valid = FALSE;