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 "gpdf-g-switch.h"
21 #include "pdf-document.h"
22 #include "ev-ps-exporter.h"
23 #include "ev-document-find.h"
24 #include "gpdf-g-switch.h"
26 #include "GlobalParams.h"
27 #include "GDKSplashOutputDev.h"
30 #include "UnicodeMap.h"
31 #include "GlobalParams.h"
32 #include "goo/GList.h"
33 #include "PSOutputDev.h"
37 PdfDocument *document;
41 /* full results are only possible for the rendered current page */
43 GArray *current_page_results;
44 guchar *other_page_flags; /* length n_pages + 1, first element ignored */
45 int start_page; /* skip this one as we iterate, since we did it first */
46 int search_page; /* the page we're searching now */
47 TextOutputDev *output_dev;
50 typedef struct _PdfDocumentClass PdfDocumentClass;
52 #define PDF_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDF_TYPE_DOCUMENT, PdfDocumentClass))
53 #define PDF_IS_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDF_TYPE_DOCUMENT))
54 #define PDF_DOCUMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDF_TYPE_DOCUMENT, PdfDocumentClass))
56 struct _PdfDocumentClass
58 GObjectClass parent_class;
63 GObject parent_instance;
71 GDKSplashOutputDev *out;
78 PdfDocumentSearch *search;
81 static void pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface);
82 static void pdf_document_document_iface_init (EvDocumentIface *iface);
83 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface);
84 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
85 static void pdf_document_search_free (PdfDocumentSearch *search);
86 static void pdf_document_search_page_changed (PdfDocumentSearch *search);
88 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
90 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
91 pdf_document_document_iface_init);
92 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_BOOKMARKS,
93 pdf_document_document_bookmarks_iface_init);
94 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
95 pdf_document_ps_exporter_iface_init);
96 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
97 pdf_document_find_iface_init);
101 document_validate_page (PdfDocument *pdf_document)
103 if (!pdf_document->page_valid) {
104 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
105 72 * pdf_document->scale,
106 72 * pdf_document->scale,
109 pdf_document->page_valid = TRUE;
111 /* Update the search results available to the app since
112 * we only provide full results on the current page
114 if (pdf_document->search)
115 pdf_document_search_page_changed (pdf_document->search);
118 return pdf_document->page_valid;
122 pdf_document_load (EvDocument *document,
126 PdfDocument *pdf_document = PDF_DOCUMENT (document);
134 globalParams = new GlobalParams("/etc/xpdfrc");
135 globalParams->setupBaseFontsFc(NULL);
138 if (! pdf_document->umap) {
139 enc = new GString("UTF-8");
140 pdf_document->umap = globalParams->getUnicodeMap(enc);
141 pdf_document->umap->incRefCnt ();
145 filename = g_filename_from_uri (uri, NULL, error);
149 filename_g = new GString (filename);
152 // open the PDF file, assumes ownership of filename_g
153 newDoc = new PDFDoc(filename_g, 0, 0);
155 if (!newDoc->isOk()) {
156 err = newDoc->getErrorCode();
159 /* FIXME: Add a real error enum to EvDocument */
160 g_set_error (error, G_FILE_ERROR,
162 "Failed to load document (error %d) '%s'\n",
169 if (pdf_document->doc)
170 delete pdf_document->doc;
171 pdf_document->doc = newDoc;
173 pdf_document->page = 1;
175 if (pdf_document->out)
176 pdf_document->out->startDoc(pdf_document->doc->getXRef());
178 pdf_document->page_valid = FALSE;
184 pdf_document_get_n_pages (EvDocument *document)
186 PdfDocument *pdf_document = PDF_DOCUMENT (document);
188 if (pdf_document->doc)
189 return pdf_document->doc->getNumPages();
195 pdf_document_set_page (EvDocument *document,
198 PdfDocument *pdf_document = PDF_DOCUMENT (document);
200 page = CLAMP (page, 1, ev_document_get_n_pages (document));
202 if (page != pdf_document->page) {
203 pdf_document->page = page;
204 pdf_document->page_valid = FALSE;
207 ev_document_changed (document);
211 pdf_document_get_page (EvDocument *document)
213 PdfDocument *pdf_document = PDF_DOCUMENT (document);
215 return pdf_document->page;
219 redraw_callback (void *data)
221 /* Need to hook up through a EvDocument callback? */
225 pdf_document_set_target (EvDocument *document,
228 PdfDocument *pdf_document = PDF_DOCUMENT (document);
230 if (pdf_document->target != target) {
231 if (pdf_document->target)
232 g_object_unref (pdf_document->target);
234 pdf_document->target = target;
236 if (pdf_document->target)
237 g_object_ref (pdf_document->target);
239 if (pdf_document->out) {
240 delete pdf_document->out;
241 pdf_document->out = NULL;
244 if (pdf_document->target) {
245 pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
246 redraw_callback, (void*) document);
248 if (pdf_document->doc)
249 pdf_document->out->startDoc(pdf_document->doc->getXRef());
253 pdf_document->page_valid = FALSE;
258 pdf_document_set_scale (EvDocument *document,
261 PdfDocument *pdf_document = PDF_DOCUMENT (document);
263 if (pdf_document->scale != scale) {
264 pdf_document->scale = scale;
265 pdf_document->page_valid = FALSE;
270 pdf_document_set_page_offset (EvDocument *document,
274 PdfDocument *pdf_document = PDF_DOCUMENT (document);
276 pdf_document->page_x_offset = x;
277 pdf_document->page_y_offset = y;
281 pdf_document_get_page_size (EvDocument *document,
285 PdfDocument *pdf_document = PDF_DOCUMENT (document);
287 if (document_validate_page (pdf_document)) {
289 *width = pdf_document->out->getBitmapWidth();
291 *height = pdf_document->out->getBitmapHeight();
301 pdf_document_render (EvDocument *document,
307 PdfDocument *pdf_document = PDF_DOCUMENT (document);
311 if (!document_validate_page (pdf_document) || !pdf_document->target)
314 page.x = pdf_document->page_x_offset;
315 page.y = pdf_document->page_y_offset;
316 page.width = pdf_document->out->getBitmapWidth();
317 page.height = pdf_document->out->getBitmapHeight();
321 draw.width = clip_width;
322 draw.height = clip_height;
324 if (gdk_rectangle_intersect (&page, &draw, &draw))
325 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
326 pdf_document->target,
328 draw.width, draw.height);
332 pdf_document_search_emit_found (PdfDocumentSearch *search)
334 PdfDocument *pdf_document = search->document;
340 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
341 if (search->search_page > search->start_page) {
342 pages_done = search->search_page - search->start_page;
343 } else if (search->search_page == search->start_page) {
344 pages_done = n_pages;
346 pages_done = n_pages - search->start_page + search->search_page;
349 tmp_results = g_array_new (FALSE, FALSE, sizeof (EvFindResult));
350 g_array_append_vals (tmp_results,
351 search->current_page_results->data,
352 search->current_page_results->len);
354 /* Now append a bogus element for each page that has a result in it,
355 * that is not the current page
358 while (i <= n_pages) {
359 if (i != pdf_document->page &&
360 search->other_page_flags[i]) {
365 /* Use bogus coordinates, again we can't get coordinates
366 * until this is the current page because TextOutputDev
369 result.highlight_area.x = -1;
370 result.highlight_area.y = -1;
371 result.highlight_area.width = 1;
372 result.highlight_area.height = 1;
374 g_array_append_val (tmp_results, result);
380 ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
381 (EvFindResult*) tmp_results->data,
383 pages_done / (double) n_pages);
385 g_array_free (tmp_results, TRUE);
389 pdf_document_search_page_changed (PdfDocumentSearch *search)
391 PdfDocument *pdf_document = search->document;
394 int xMin, yMin, xMax, yMax;
396 current_page = pdf_document->page;
398 if (!pdf_document->page_valid) {
399 /* we can't do anything until displayPage() */
400 search->current_page = -1;
404 if (search->current_page == current_page)
407 /* We need to create current_page_results for the new current page */
408 g_array_set_size (search->current_page_results, 0);
410 if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
411 gTrue, gTrue, // startAtTop, stopAtBottom
412 gFalse, gFalse, // startAtLast, stopAtLast
413 &xMin, &yMin, &xMax, &yMax)) {
414 result.page_num = pdf_document->page;
416 result.highlight_area.x = xMin;
417 result.highlight_area.y = yMin;
418 result.highlight_area.width = xMax - xMin;
419 result.highlight_area.height = yMax - yMin;
421 g_array_append_val (search->current_page_results, result);
422 /* Now find further results */
424 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
427 &xMin, &yMin, &xMax, &yMax)) {
429 result.page_num = pdf_document->page;
431 result.highlight_area.x = xMin;
432 result.highlight_area.y = yMin;
433 result.highlight_area.width = xMax - xMin;
434 result.highlight_area.height = yMax - yMin;
436 g_array_append_val (search->current_page_results, result);
440 /* needed for the initial current page since we don't search
443 search->other_page_flags[current_page] =
444 search->current_page_results->len > 0;
446 pdf_document_search_emit_found (search);
450 pdf_document_search_idle_callback (void *data)
452 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
453 PdfDocument *pdf_document = search->document;
455 double xMin, yMin, xMax, yMax;
457 /* Note that PDF page count is 1 through n_pages INCLUSIVE
458 * like a real book. We are looking to add one result for each
459 * page with a match, because the coordinates are meaningless
460 * with TextOutputDev, so we just want to flag matching pages
461 * and then when the user switches to the current page, we
462 * will emit "found" again with the real results.
464 n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
466 if (search->search_page == search->start_page) {
470 if (search->output_dev == 0) {
471 /* First time through here... */
472 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
473 if (!search->output_dev->isOk()) {
478 pdf_document->doc->displayPage (search->output_dev,
480 72, 72, 0, gTrue, gFalse);
482 if (search->output_dev->findText (search->ucs4,
484 gTrue, gTrue, // startAtTop, stopAtBottom
485 gFalse, gFalse, // startAtLast, stopAtLast
486 &xMin, &yMin, &xMax, &yMax)) {
487 /* This page has results */
488 search->other_page_flags[search->search_page] = TRUE;
491 search->search_page += 1;
492 if (search->search_page > n_pages) {
494 search->search_page = 1;
497 /* We do this even if nothing was found, to update the percent complete */
498 pdf_document_search_emit_found (search);
504 search->idle = 0; /* will return FALSE to remove */
509 pdf_document_find_begin (EvDocumentFind *document,
510 const char *search_string,
511 gboolean case_sensitive)
513 PdfDocument *pdf_document = PDF_DOCUMENT (document);
514 PdfDocumentSearch *search;
519 /* FIXME handle case_sensitive (right now XPDF
520 * code is always case insensitive for ASCII
521 * and case sensitive for all other languaages)
524 g_assert (sizeof (gunichar) == sizeof (Unicode));
525 ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
528 if (pdf_document->search &&
529 pdf_document->search->ucs4_len == ucs4_len &&
530 memcmp (pdf_document->search->ucs4,
532 sizeof (gunichar) * ucs4_len) == 0) {
533 /* Search is unchanged */
538 if (pdf_document->search) {
539 pdf_document_search_free (pdf_document->search);
540 pdf_document->search = NULL;
543 search = g_new0 (PdfDocumentSearch, 1);
546 search->ucs4_len = ucs4_len;
548 search->current_page_results = g_array_new (FALSE,
550 sizeof (EvFindResult));
551 n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
553 /* This is an array of bool; with the first value ignored
554 * so we can index by the based-at-1 page numbers
556 search->other_page_flags = g_new0 (guchar, n_pages + 1);
558 search->document = pdf_document;
560 /* We add at low priority so the progress bar repaints */
561 search->idle = g_idle_add_full (G_PRIORITY_LOW,
562 pdf_document_search_idle_callback,
566 search->output_dev = 0;
568 search->start_page = pdf_document->page;
569 search->search_page = search->start_page + 1;
570 if (search->search_page > n_pages)
571 search->search_page = 1;
573 search->current_page = -1;
575 pdf_document->search = search;
577 /* Update for the current page right away */
578 pdf_document_search_page_changed (search);
582 pdf_document_find_cancel (EvDocumentFind *document)
584 PdfDocument *pdf_document = PDF_DOCUMENT (document);
586 if (pdf_document->search) {
587 pdf_document_search_free (pdf_document->search);
588 pdf_document->search = NULL;
593 pdf_document_search_free (PdfDocumentSearch *search)
595 if (search->idle != 0)
596 g_source_remove (search->idle);
598 if (search->output_dev)
599 delete search->output_dev;
601 g_array_free (search->current_page_results, TRUE);
602 g_free (search->other_page_flags);
604 g_free (search->ucs4);
609 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
611 PdfDocument *document = PDF_DOCUMENT (exporter);
613 if (document->ps_out)
614 delete document->ps_out;
616 document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
617 document->doc->getCatalog(), 1,
618 ev_document_get_n_pages (EV_DOCUMENT (document)),
623 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
625 PdfDocument *document = PDF_DOCUMENT (exporter);
627 document->doc->displayPage (document->ps_out, page,
628 72.0, 72.0, 0, gTrue, gFalse);
632 pdf_document_ps_export_end (EvPSExporter *exporter)
634 PdfDocument *document = PDF_DOCUMENT (exporter);
636 delete document->ps_out;
637 document->ps_out = NULL;
641 /* EvDocumentBookmarks Implementation */
644 /* goo GList, not glib */
651 unicode_to_char (OutlineItem *outline_item,
655 gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
658 for (i = 0; i < outline_item->getTitleLength(); ++i) {
659 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
663 return g_strdup (gstr.getCString ());
668 pdf_document_bookmarks_has_document_bookmarks (EvDocumentBookmarks *document_bookmarks)
670 PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
673 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
675 outline = pdf_document->doc->getOutline();
676 if (outline->getItems() != NULL &&
677 outline->getItems()->getLength() > 0)
683 static EvDocumentBookmarksIter *
684 pdf_document_bookmarks_begin_read (EvDocumentBookmarks *document_bookmarks)
686 PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
691 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), NULL);
693 outline = pdf_document->doc->getOutline();
694 items = outline->getItems();
698 iter = g_new0 (BookmarksIter, 1);
703 return (EvDocumentBookmarksIter *) iter;
707 pdf_document_bookmarks_get_values (EvDocumentBookmarks *document_bookmarks,
708 EvDocumentBookmarksIter *bookmarks_iter,
710 EvDocumentBookmarksType *type,
713 PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
714 BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
716 LinkAction *link_action;
717 LinkDest *link_dest = NULL;
718 LinkURI *link_uri = NULL;
719 LinkGoTo *link_goto = NULL;
725 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
726 g_return_val_if_fail (iter != NULL, FALSE);
727 g_return_val_if_fail (title != NULL, FALSE);
728 g_return_val_if_fail (type != NULL, FALSE);
729 g_return_val_if_fail (page != NULL, FALSE);
731 anItem = (OutlineItem *)iter->items->get(iter->index);
732 link_action = anItem->getAction ();
733 link_title = anItem->getTitle ();
736 switch (link_action->getKind ()) {
739 link_goto = dynamic_cast <LinkGoTo *> (link_action);
740 link_dest = link_goto->getDest ();
741 named_dest = link_goto->getNamedDest ();
743 /* Wow! This seems excessively slow on large
744 * documents. I need to investigate more... -jrb */
745 if (link_dest != NULL) {
746 link_dest = link_dest->copy ();
747 } else if (named_dest != NULL) {
748 named_dest = named_dest->copy ();
749 link_dest = pdf_document->doc->findDest (named_dest);
752 if (link_dest != NULL) {
753 if (link_dest->isPageRef ()) {
754 page_ref = link_dest->getPageRef ();
755 page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
757 page_num = link_dest->getPageNum ();
765 link_uri = dynamic_cast <LinkURI *> (link_action);
771 g_warning ("Unknown link action type: %d", link_action->getKind ());
774 *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
775 } else if (link_title) {
776 *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
779 *type = EV_DOCUMENT_BOOKMARKS_TYPE_LINK;
785 static EvDocumentBookmarksIter *
786 pdf_document_bookmarks_get_child (EvDocumentBookmarks *document_bookmarks,
787 EvDocumentBookmarksIter *bookmarks_iter)
789 BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
790 BookmarksIter *child_iter;
793 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
795 anItem = (OutlineItem *)iter->items->get(iter->index);
797 if (! (anItem->hasKids() && anItem->getKids()) )
800 child_iter = g_new0 (BookmarksIter, 1);
801 child_iter->index = 0;
802 child_iter->level = iter->level + 1;
803 child_iter->items = anItem->getKids ();
804 g_assert (child_iter->items);
806 return (EvDocumentBookmarksIter *) child_iter;
810 pdf_document_bookmarks_next (EvDocumentBookmarks *document_bookmarks,
811 EvDocumentBookmarksIter *bookmarks_iter)
813 BookmarksIter *iter = (BookmarksIter *) bookmarks_iter;
815 g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
818 if (iter->index >= iter->items->getLength())
825 pdf_document_bookmarks_free_iter (EvDocumentBookmarks *document_bookmarks,
826 EvDocumentBookmarksIter *iter)
828 g_return_if_fail (PDF_IS_DOCUMENT (document_bookmarks));
829 g_return_if_fail (iter != NULL);
831 /* FIXME: Should I close all the nodes?? Free them? */
836 pdf_document_finalize (GObject *object)
838 PdfDocument *pdf_document = PDF_DOCUMENT (object);
840 if (pdf_document->umap) {
841 pdf_document->umap->decRefCnt ();
842 pdf_document->umap = NULL;
845 if (pdf_document->search)
846 pdf_document_search_free (pdf_document->search);
848 if (pdf_document->target)
849 g_object_unref (pdf_document->target);
851 if (pdf_document->out)
852 delete pdf_document->out;
853 if (pdf_document->ps_out)
854 delete pdf_document->ps_out;
855 if (pdf_document->doc)
856 delete pdf_document->doc;
861 pdf_document_class_init (PdfDocumentClass *klass)
863 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
865 gobject_class->finalize = pdf_document_finalize;
869 pdf_document_document_iface_init (EvDocumentIface *iface)
871 iface->load = pdf_document_load;
872 iface->get_n_pages = pdf_document_get_n_pages;
873 iface->set_page = pdf_document_set_page;
874 iface->get_page = pdf_document_get_page;
875 iface->set_scale = pdf_document_set_scale;
876 iface->set_target = pdf_document_set_target;
877 iface->set_page_offset = pdf_document_set_page_offset;
878 iface->get_page_size = pdf_document_get_page_size;
879 iface->render = pdf_document_render;
883 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
885 iface->begin = pdf_document_ps_export_begin;
886 iface->do_page = pdf_document_ps_export_do_page;
887 iface->end = pdf_document_ps_export_end;
892 pdf_document_find_iface_init (EvDocumentFindIface *iface)
894 iface->begin = pdf_document_find_begin;
895 iface->cancel = pdf_document_find_cancel;
899 pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface)
901 iface->has_document_bookmarks = pdf_document_bookmarks_has_document_bookmarks;
902 iface->begin_read = pdf_document_bookmarks_begin_read;
903 iface->get_values = pdf_document_bookmarks_get_values;
904 iface->get_child = pdf_document_bookmarks_get_child;
905 iface->next = pdf_document_bookmarks_next;
906 iface->free_iter = pdf_document_bookmarks_free_iter;
911 pdf_document_init (PdfDocument *pdf_document)
913 pdf_document->page = 1;
914 pdf_document->page_x_offset = 0;
915 pdf_document->page_y_offset = 0;
916 pdf_document->scale = 1.;
918 pdf_document->page_valid = FALSE;