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.
26 #include <poppler-document.h>
27 #include <poppler-page.h>
29 #include <cairo-pdf.h>
31 #include <glib/gi18n.h>
33 #include "ev-poppler.h"
34 #include "ev-file-exporter.h"
35 #include "ev-document-find.h"
36 #include "ev-document-misc.h"
37 #include "ev-document-links.h"
38 #include "ev-document-images.h"
39 #include "ev-document-fonts.h"
40 #include "ev-document-security.h"
41 #include "ev-document-thumbnails.h"
42 #include "ev-document-transition.h"
43 #include "ev-selection.h"
44 #include "ev-attachment.h"
48 PdfDocument *document;
57 EvFileExporterFormat format;
58 PopplerPSFile *ps_file;
64 struct _PdfDocumentClass
66 GObjectClass parent_class;
71 GObject parent_instance;
73 PopplerDocument *document;
76 PopplerFontInfo *font_info;
77 PopplerFontsIter *fonts_iter;
78 int fonts_scanned_pages;
80 PdfDocumentSearch *search;
81 PdfPrintContext *print_ctx;
84 static void pdf_document_document_iface_init (EvDocumentIface *iface);
85 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
86 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
87 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
88 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
89 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
90 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
91 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
92 static void pdf_selection_iface_init (EvSelectionIface *iface);
93 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
94 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
99 static int pdf_document_get_n_pages (EvDocument *document);
101 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
103 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
104 PopplerAction *action);
105 static void pdf_document_search_free (PdfDocumentSearch *search);
106 static void pdf_print_context_free (PdfPrintContext *ctx);
109 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
111 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
112 pdf_document_document_iface_init);
113 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
114 pdf_document_security_iface_init);
115 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
116 pdf_document_document_thumbnails_iface_init);
117 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
118 pdf_document_document_links_iface_init);
119 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
120 pdf_document_document_images_iface_init);
121 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
122 pdf_document_document_fonts_iface_init);
123 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
124 pdf_document_find_iface_init);
125 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
126 pdf_document_file_exporter_iface_init);
127 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
128 pdf_selection_iface_init);
129 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
130 pdf_document_page_transition_iface_init);
135 set_rc_data (PdfDocument *pdf_document,
138 if (rc->data == NULL) {
139 rc->data = poppler_document_get_page (pdf_document->document,
141 rc->destroy = g_object_unref;
143 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
148 pdf_document_search_free (PdfDocumentSearch *search)
150 PdfDocument *pdf_document = search->document;
154 if (search->idle != 0)
155 g_source_remove (search->idle);
157 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
158 for (i = 0; i < n_pages; i++) {
159 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
160 g_list_free (search->pages[i]);
162 g_free (search->pages);
164 g_free (search->text);
169 pdf_document_dispose (GObject *object)
171 PdfDocument *pdf_document = PDF_DOCUMENT(object);
173 if (pdf_document->print_ctx) {
174 pdf_print_context_free (pdf_document->print_ctx);
175 pdf_document->print_ctx = NULL;
178 if (pdf_document->search) {
179 pdf_document_search_free (pdf_document->search);
180 pdf_document->search = NULL;
183 if (pdf_document->document) {
184 g_object_unref (pdf_document->document);
187 if (pdf_document->font_info) {
188 poppler_font_info_free (pdf_document->font_info);
191 if (pdf_document->fonts_iter) {
192 poppler_fonts_iter_free (pdf_document->fonts_iter);
197 pdf_document_class_init (PdfDocumentClass *klass)
199 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
201 g_object_class->dispose = pdf_document_dispose;
205 pdf_document_init (PdfDocument *pdf_document)
207 pdf_document->password = NULL;
211 convert_error (GError *poppler_error,
214 if (poppler_error == NULL)
217 if (poppler_error->domain == POPPLER_ERROR) {
218 /* convert poppler errors into EvDocument errors */
219 gint code = EV_DOCUMENT_ERROR_INVALID;
220 if (poppler_error->code == POPPLER_ERROR_INVALID)
221 code = EV_DOCUMENT_ERROR_INVALID;
222 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
223 code = EV_DOCUMENT_ERROR_ENCRYPTED;
229 poppler_error->message,
232 g_propagate_error (error, poppler_error);
239 pdf_document_save (EvDocument *document,
244 GError *poppler_error = NULL;
246 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
250 convert_error (poppler_error, error);
256 pdf_document_load (EvDocument *document,
260 GError *poppler_error = NULL;
261 PdfDocument *pdf_document = PDF_DOCUMENT (document);
263 pdf_document->document =
264 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
266 if (pdf_document->document == NULL) {
267 convert_error (poppler_error, error);
275 pdf_document_get_n_pages (EvDocument *document)
277 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
281 pdf_document_get_page_size (EvDocument *document,
286 PdfDocument *pdf_document = PDF_DOCUMENT (document);
287 PopplerPage *poppler_page;
289 poppler_page = poppler_document_get_page (pdf_document->document, page);
290 poppler_page_get_size (poppler_page, width, height);
291 g_object_unref (poppler_page);
295 pdf_document_get_page_label (EvDocument *document,
298 PopplerPage *poppler_page;
301 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
304 g_object_get (G_OBJECT (poppler_page),
307 g_object_unref (poppler_page);
313 pdf_document_has_attachments (EvDocument *document)
315 PdfDocument *pdf_document;
317 pdf_document = PDF_DOCUMENT (document);
319 return poppler_document_has_attachments (pdf_document->document);
322 struct SaveToBufferData {
328 attachment_save_to_buffer_callback (const gchar *buf,
333 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
337 if (sdata->len + count > sdata->max) {
338 new_max = MAX (sdata->max * 2, sdata->len + count);
339 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
341 sdata->buffer = new_buffer;
342 sdata->max = new_max;
345 memcpy (sdata->buffer + sdata->len, buf, count);
352 attachment_save_to_buffer (PopplerAttachment *attachment,
357 static const gint initial_max = 1024;
358 struct SaveToBufferData sdata;
363 sdata.buffer = (gchar *) g_malloc (initial_max);
364 sdata.max = initial_max;
367 if (! poppler_attachment_save_to_callback (attachment,
368 attachment_save_to_buffer_callback,
371 g_free (sdata.buffer);
375 *buffer = sdata.buffer;
376 *buffer_size = sdata.len;
382 pdf_document_get_attachments (EvDocument *document)
384 PdfDocument *pdf_document;
387 GList *retval = NULL;
389 pdf_document = PDF_DOCUMENT (document);
391 if (!pdf_document_has_attachments (document))
394 attachments = poppler_document_get_attachments (pdf_document->document);
396 for (list = attachments; list; list = list->next) {
397 PopplerAttachment *attachment;
398 EvAttachment *ev_attachment;
401 GError *error = NULL;
403 attachment = (PopplerAttachment *) list->data;
405 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
406 ev_attachment = ev_attachment_new (attachment->name,
407 attachment->description,
412 retval = g_list_prepend (retval, ev_attachment);
415 g_warning ("%s", error->message);
416 g_error_free (error);
422 g_object_unref (attachment);
425 return g_list_reverse (retval);
429 pdf_document_render_pixbuf (EvDocument *document,
432 PdfDocument *pdf_document;
434 double width_points, height_points;
437 pdf_document = PDF_DOCUMENT (document);
439 set_rc_data (pdf_document, rc);
441 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
443 if (rc->rotation == 90 || rc->rotation == 270) {
444 width = (int) ((height_points * rc->scale) + 0.5);
445 height = (int) ((width_points * rc->scale) + 0.5);
447 width = (int) ((width_points * rc->scale) + 0.5);
448 height = (int) ((height_points * rc->scale) + 0.5);
451 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
455 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
466 /* EvDocumentSecurity */
469 pdf_document_has_document_security (EvDocumentSecurity *document_security)
471 /* FIXME: do we really need to have this? */
476 pdf_document_set_password (EvDocumentSecurity *document_security,
477 const char *password)
479 PdfDocument *document = PDF_DOCUMENT (document_security);
481 if (document->password)
482 g_free (document->password);
484 document->password = g_strdup (password);
488 pdf_document_can_get_text (EvDocument *document)
493 static EvDocumentInfo *
494 pdf_document_get_info (EvDocument *document)
496 EvDocumentInfo *info;
497 PopplerPageLayout layout;
498 PopplerPageMode mode;
499 PopplerViewerPreferences view_prefs;
500 PopplerPermissions permissions;
502 info = g_new0 (EvDocumentInfo, 1);
504 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
505 EV_DOCUMENT_INFO_FORMAT |
506 EV_DOCUMENT_INFO_AUTHOR |
507 EV_DOCUMENT_INFO_SUBJECT |
508 EV_DOCUMENT_INFO_KEYWORDS |
509 EV_DOCUMENT_INFO_LAYOUT |
510 EV_DOCUMENT_INFO_START_MODE |
511 EV_DOCUMENT_INFO_PERMISSIONS |
512 EV_DOCUMENT_INFO_UI_HINTS |
513 EV_DOCUMENT_INFO_CREATOR |
514 EV_DOCUMENT_INFO_PRODUCER |
515 EV_DOCUMENT_INFO_CREATION_DATE |
516 EV_DOCUMENT_INFO_MOD_DATE |
517 EV_DOCUMENT_INFO_LINEARIZED |
518 EV_DOCUMENT_INFO_N_PAGES |
519 EV_DOCUMENT_INFO_SECURITY |
520 EV_DOCUMENT_INFO_PAPER_SIZE;
522 g_object_get (PDF_DOCUMENT (document)->document,
523 "title", &(info->title),
524 "format", &(info->format),
525 "author", &(info->author),
526 "subject", &(info->subject),
527 "keywords", &(info->keywords),
529 "page-layout", &layout,
530 "viewer-preferences", &view_prefs,
531 "permissions", &permissions,
532 "creator", &(info->creator),
533 "producer", &(info->producer),
534 "creation-date", &(info->creation_date),
535 "mod-date", &(info->modified_date),
536 "linearized", &(info->linearized),
539 pdf_document_get_page_size(document, 0,
540 &(info->paper_width),
541 &(info->paper_height));
544 info->paper_width = info->paper_width / 72.0f * 25.4f;
545 info->paper_height = info->paper_height / 72.0f * 25.4f;
548 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
549 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
551 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
552 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
554 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
555 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
557 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
558 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
559 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
560 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
562 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
563 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
570 case POPPLER_PAGE_MODE_NONE:
571 info->mode = EV_DOCUMENT_MODE_NONE;
573 case POPPLER_PAGE_MODE_USE_THUMBS:
574 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
576 case POPPLER_PAGE_MODE_USE_OC:
577 info->mode = EV_DOCUMENT_MODE_USE_OC;
579 case POPPLER_PAGE_MODE_FULL_SCREEN:
580 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
582 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
583 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
589 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
590 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
592 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
593 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
595 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
596 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
598 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
599 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
601 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
602 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
604 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
605 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
607 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
608 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
611 info->permissions = 0;
612 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
613 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
615 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
616 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
618 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
619 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
621 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
622 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
625 info->n_pages = ev_document_get_n_pages (document);
627 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
628 /* translators: this is the document security state */
629 info->security = g_strdup (_("Yes"));
631 /* translators: this is the document security state */
632 info->security = g_strdup (_("No"));
639 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
641 PdfDocument *pdf_document = PDF_DOCUMENT (document);
642 PopplerPage *poppler_page;
647 poppler_page = poppler_document_get_page (pdf_document->document, page);
648 g_return_val_if_fail (poppler_page != NULL, NULL);
650 poppler_page_get_size (poppler_page, NULL, &height);
652 r.y1 = height - rect->y2;
654 r.y2 = height - rect->y1;
656 text = poppler_page_get_text (poppler_page, &r);
658 g_object_unref (poppler_page);
664 pdf_document_document_iface_init (EvDocumentIface *iface)
666 iface->save = pdf_document_save;
667 iface->load = pdf_document_load;
668 iface->get_n_pages = pdf_document_get_n_pages;
669 iface->get_page_size = pdf_document_get_page_size;
670 iface->get_page_label = pdf_document_get_page_label;
671 iface->has_attachments = pdf_document_has_attachments;
672 iface->get_attachments = pdf_document_get_attachments;
673 iface->render_pixbuf = pdf_document_render_pixbuf;
674 iface->get_text = pdf_document_get_text;
675 iface->can_get_text = pdf_document_can_get_text;
676 iface->get_info = pdf_document_get_info;
680 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
682 iface->has_document_security = pdf_document_has_document_security;
683 iface->set_password = pdf_document_set_password;
687 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
689 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
692 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
694 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
698 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
701 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
704 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
706 if (pdf_document->font_info == NULL) {
707 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
710 if (pdf_document->fonts_iter) {
711 poppler_fonts_iter_free (pdf_document->fonts_iter);
714 pdf_document->fonts_scanned_pages += n_pages;
716 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
717 &pdf_document->fonts_iter);
719 pdf_document->fonts_scanned_pages = 0;
720 poppler_font_info_free (pdf_document->font_info);
721 pdf_document->font_info = NULL;
728 font_type_to_string (PopplerFontType type)
731 case POPPLER_FONT_TYPE_TYPE1:
733 case POPPLER_FONT_TYPE_TYPE1C:
735 case POPPLER_FONT_TYPE_TYPE3:
737 case POPPLER_FONT_TYPE_TRUETYPE:
738 return _("TrueType");
739 case POPPLER_FONT_TYPE_CID_TYPE0:
740 return _("Type 1 (CID)");
741 case POPPLER_FONT_TYPE_CID_TYPE0C:
742 return _("Type 1C (CID)");
743 case POPPLER_FONT_TYPE_CID_TYPE2:
744 return _("TrueType (CID)");
746 return _("Unknown font type");
751 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
754 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
755 PopplerFontsIter *iter = pdf_document->fonts_iter;
757 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
763 GtkTreeIter list_iter;
766 const char *embedded;
769 name = poppler_fonts_iter_get_name (iter);
775 type = font_type_to_string (
776 poppler_fonts_iter_get_font_type (iter));
778 if (poppler_fonts_iter_is_embedded (iter)) {
779 if (poppler_fonts_iter_is_subset (iter))
780 embedded = _("Embedded subset");
782 embedded = _("Embedded");
784 embedded = _("Not embedded");
787 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
789 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
790 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
791 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
792 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
796 } while (poppler_fonts_iter_next (iter));
800 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
802 iface->fill_model = pdf_document_fonts_fill_model;
803 iface->scan = pdf_document_fonts_scan;
804 iface->get_progress = pdf_document_fonts_get_progress;
808 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
810 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
811 PopplerIndexIter *iter;
813 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
815 iter = poppler_index_iter_new (pdf_document->document);
818 poppler_index_iter_free (iter);
824 ev_link_dest_from_dest (PdfDocument *pdf_document,
827 EvLinkDest *ev_dest = NULL;
828 const char *unimplemented_dest = NULL;
830 g_assert (dest != NULL);
832 switch (dest->type) {
833 case POPPLER_DEST_XYZ: {
834 PopplerPage *poppler_page;
837 poppler_page = poppler_document_get_page (pdf_document->document,
838 MAX (0, dest->page_num - 1));
839 poppler_page_get_size (poppler_page, NULL, &height);
840 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
844 g_object_unref (poppler_page);
847 case POPPLER_DEST_FIT:
848 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
850 case POPPLER_DEST_FITH: {
851 PopplerPage *poppler_page;
854 poppler_page = poppler_document_get_page (pdf_document->document,
855 MAX (0, dest->page_num - 1));
856 poppler_page_get_size (poppler_page, NULL, &height);
857 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
859 g_object_unref (poppler_page);
862 case POPPLER_DEST_FITV:
863 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
866 case POPPLER_DEST_FITR: {
867 PopplerPage *poppler_page;
870 poppler_page = poppler_document_get_page (pdf_document->document,
871 MAX (0, dest->page_num - 1));
872 poppler_page_get_size (poppler_page, NULL, &height);
873 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
875 height - dest->bottom,
878 g_object_unref (poppler_page);
881 case POPPLER_DEST_FITB:
882 unimplemented_dest = "POPPLER_DEST_FITB";
884 case POPPLER_DEST_FITBH:
885 unimplemented_dest = "POPPLER_DEST_FITBH";
887 case POPPLER_DEST_FITBV:
888 unimplemented_dest = "POPPLER_DEST_FITBV";
890 case POPPLER_DEST_NAMED:
891 ev_dest = ev_link_dest_new_named (dest->named_dest);
893 case POPPLER_DEST_UNKNOWN:
894 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
898 if (unimplemented_dest) {
899 g_warning ("Unimplemented named action: %s, please post a "
900 "bug report in Evince bugzilla "
901 "(http://bugzilla.gnome.org) with a testcase.",
906 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
912 ev_link_from_action (PdfDocument *pdf_document,
913 PopplerAction *action)
916 EvLinkAction *ev_action = NULL;
917 const char *unimplemented_action = NULL;
919 switch (action->type) {
920 case POPPLER_ACTION_GOTO_DEST: {
923 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
924 ev_action = ev_link_action_new_dest (dest);
927 case POPPLER_ACTION_GOTO_REMOTE: {
930 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
931 ev_action = ev_link_action_new_remote (dest,
932 action->goto_remote.file_name);
936 case POPPLER_ACTION_LAUNCH:
937 ev_action = ev_link_action_new_launch (action->launch.file_name,
938 action->launch.params);
940 case POPPLER_ACTION_URI:
941 ev_action = ev_link_action_new_external_uri (action->uri.uri);
943 case POPPLER_ACTION_NAMED:
944 ev_action = ev_link_action_new_named (action->named.named_dest);
946 case POPPLER_ACTION_MOVIE:
947 unimplemented_action = "POPPLER_ACTION_MOVIE";
949 case POPPLER_ACTION_UNKNOWN:
950 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
953 if (unimplemented_action) {
954 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
955 unimplemented_action);
958 link = ev_link_new (action->any.title, ev_action);
964 build_tree (PdfDocument *pdf_document,
967 PopplerIndexIter *iter)
971 GtkTreeIter tree_iter;
972 PopplerIndexIter *child;
973 PopplerAction *action;
978 action = poppler_index_iter_get_action (iter);
979 expand = poppler_index_iter_is_open (iter);
984 switch (action->type) {
985 case POPPLER_ACTION_GOTO_DEST: {
986 /* For bookmarks, solve named destinations */
987 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
989 EvLinkDest *ev_dest = NULL;
990 EvLinkAction *ev_action;
992 dest = poppler_document_find_dest (pdf_document->document,
993 action->goto_dest.dest->named_dest);
995 link = ev_link_from_action (pdf_document, action);
999 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1000 poppler_dest_free (dest);
1002 ev_action = ev_link_action_new_dest (ev_dest);
1003 link = ev_link_new (action->any.title, ev_action);
1005 link = ev_link_from_action (pdf_document, action);
1010 link = ev_link_from_action (pdf_document, action);
1015 poppler_action_free (action);
1019 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1020 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1022 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1023 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1024 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1025 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1028 g_free (title_markup);
1029 g_object_unref (link);
1031 child = poppler_index_iter_get_child (iter);
1033 build_tree (pdf_document, model, &tree_iter, child);
1034 poppler_index_iter_free (child);
1035 poppler_action_free (action);
1037 } while (poppler_index_iter_next (iter));
1040 static GtkTreeModel *
1041 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1043 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1044 GtkTreeModel *model = NULL;
1045 PopplerIndexIter *iter;
1047 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1049 iter = poppler_index_iter_new (pdf_document->document);
1050 /* Create the model if we have items*/
1052 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1057 build_tree (pdf_document, model, NULL, iter);
1058 poppler_index_iter_free (iter);
1065 pdf_document_links_get_links (EvDocumentLinks *document_links,
1068 PdfDocument *pdf_document;
1069 PopplerPage *poppler_page;
1070 GList *retval = NULL;
1071 GList *mapping_list;
1075 pdf_document = PDF_DOCUMENT (document_links);
1076 poppler_page = poppler_document_get_page (pdf_document->document,
1078 mapping_list = poppler_page_get_link_mapping (poppler_page);
1079 poppler_page_get_size (poppler_page, NULL, &height);
1081 for (list = mapping_list; list; list = list->next) {
1082 PopplerLinkMapping *link_mapping;
1083 EvLinkMapping *ev_link_mapping;
1085 link_mapping = (PopplerLinkMapping *)list->data;
1086 ev_link_mapping = g_new (EvLinkMapping, 1);
1087 ev_link_mapping->link = ev_link_from_action (pdf_document,
1088 link_mapping->action);
1089 ev_link_mapping->x1 = link_mapping->area.x1;
1090 ev_link_mapping->x2 = link_mapping->area.x2;
1091 /* Invert this for X-style coordinates */
1092 ev_link_mapping->y1 = height - link_mapping->area.y2;
1093 ev_link_mapping->y2 = height - link_mapping->area.y1;
1095 retval = g_list_prepend (retval, ev_link_mapping);
1098 poppler_page_free_link_mapping (mapping_list);
1099 g_object_unref (poppler_page);
1101 return g_list_reverse (retval);
1105 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1106 const gchar *link_name)
1108 PdfDocument *pdf_document;
1110 EvLinkDest *ev_dest = NULL;
1112 pdf_document = PDF_DOCUMENT (document_links);
1113 dest = poppler_document_find_dest (pdf_document->document,
1116 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1117 poppler_dest_free (dest);
1124 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1126 iface->has_document_links = pdf_document_links_has_document_links;
1127 iface->get_links_model = pdf_document_links_get_links_model;
1128 iface->get_links = pdf_document_links_get_links;
1129 iface->find_link_dest = pdf_document_links_find_link_dest;
1133 pdf_document_images_get_images (EvDocumentImages *document_images,
1136 GList *retval = NULL;
1137 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE_MAPPING
1138 PdfDocument *pdf_document;
1139 PopplerPage *poppler_page;
1140 GList *mapping_list;
1143 pdf_document = PDF_DOCUMENT (document_images);
1144 poppler_page = poppler_document_get_page (pdf_document->document, page);
1145 mapping_list = poppler_page_get_image_mapping (poppler_page);
1147 for (list = mapping_list; list; list = list->next) {
1148 PopplerImageMapping *image_mapping;
1149 EvImageMapping *ev_image_mapping;
1151 image_mapping = (PopplerImageMapping *)list->data;
1153 ev_image_mapping = g_new (EvImageMapping, 1);
1155 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1156 ev_image_mapping->x1 = image_mapping->area.x1;
1157 ev_image_mapping->x2 = image_mapping->area.x2;
1158 ev_image_mapping->y1 = image_mapping->area.y1;
1159 ev_image_mapping->y2 = image_mapping->area.y2;
1161 retval = g_list_prepend (retval, ev_image_mapping);
1164 poppler_page_free_image_mapping (mapping_list);
1165 g_object_unref (poppler_page);
1166 #endif /* HAVE_POPPLER_PAGE_GET_IMAGE_MAPPING */
1171 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1173 iface->get_images = pdf_document_images_get_images;
1177 make_thumbnail_for_size (PdfDocument *pdf_document,
1182 PopplerPage *poppler_page;
1186 gdouble unscaled_width, unscaled_height;
1188 poppler_page = poppler_document_get_page (pdf_document->document, page);
1189 g_return_val_if_fail (poppler_page != NULL, NULL);
1191 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page,
1192 size, &width, &height);
1193 poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
1194 scale = width / unscaled_width;
1197 if (rotation == 90 || rotation == 270) {
1204 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1206 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1208 ev_document_fc_mutex_lock ();
1209 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1211 scale, rotation, pixbuf);
1212 ev_document_fc_mutex_unlock ();
1215 g_object_unref (poppler_page);
1221 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1227 PdfDocument *pdf_document;
1228 PopplerPage *poppler_page;
1230 GdkPixbuf *border_pixbuf;
1232 pdf_document = PDF_DOCUMENT (document_thumbnails);
1234 poppler_page = poppler_document_get_page (pdf_document->document, page);
1235 g_return_val_if_fail (poppler_page != NULL, NULL);
1237 pixbuf = poppler_page_get_thumbnail (poppler_page);
1239 if (pixbuf == NULL) {
1240 /* There is no provided thumbnail. We need to make one. */
1241 pixbuf = make_thumbnail_for_size (pdf_document, page, rotation, size);
1245 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, rotation, pixbuf);
1246 g_object_unref (pixbuf);
1247 pixbuf = border_pixbuf;
1250 g_object_unref (poppler_page);
1256 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1262 PdfDocument *pdf_document;
1263 PopplerPage *poppler_page;
1266 pdf_document = PDF_DOCUMENT (document_thumbnails);
1267 poppler_page = poppler_document_get_page (pdf_document->document, page);
1269 g_return_if_fail (width != NULL);
1270 g_return_if_fail (height != NULL);
1271 g_return_if_fail (poppler_page != NULL);
1273 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1276 double page_width, page_height;
1278 poppler_page_get_size (poppler_page, &page_width, &page_height);
1280 *height = (int) (size * page_height / page_width);
1282 g_object_unref (poppler_page);
1286 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1288 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1289 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1294 pdf_document_search_idle_callback (void *data)
1296 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1297 PdfDocument *pdf_document = search->document;
1302 page = poppler_document_get_page (search->document->document,
1303 search->search_page);
1305 ev_document_doc_mutex_lock ();
1306 matches = poppler_page_find_text (page, search->text);
1307 ev_document_doc_mutex_unlock ();
1309 g_object_unref (page);
1311 search->pages[search->search_page] = matches;
1312 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1313 search->search_page);
1315 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1316 search->search_page += 1;
1317 if (search->search_page == n_pages) {
1319 search->search_page = 0;
1322 if (search->search_page != search->start_page) {
1327 search->idle = 0; /* will return FALSE to remove */
1332 static PdfDocumentSearch *
1333 pdf_document_search_new (PdfDocument *pdf_document,
1337 PdfDocumentSearch *search;
1341 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1343 search = g_new0 (PdfDocumentSearch, 1);
1345 search->text = g_strdup (text);
1346 search->pages = g_new0 (GList *, n_pages);
1347 search->document = pdf_document;
1349 /* We add at low priority so the progress bar repaints */
1350 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1351 pdf_document_search_idle_callback,
1355 search->start_page = start_page;
1356 search->search_page = start_page;
1362 pdf_document_find_begin (EvDocumentFind *document,
1364 const char *search_string,
1365 gboolean case_sensitive)
1367 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1369 /* FIXME handle case_sensitive (right now XPDF
1370 * code is always case insensitive for ASCII
1371 * and case sensitive for all other languaages)
1374 if (pdf_document->search &&
1375 strcmp (search_string, pdf_document->search->text) == 0)
1378 if (pdf_document->search)
1379 pdf_document_search_free (pdf_document->search);
1381 pdf_document->search = pdf_document_search_new (pdf_document,
1387 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1389 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1392 return g_list_length (search->pages[page]);
1399 pdf_document_find_get_result (EvDocumentFind *document_find,
1402 EvRectangle *rectangle)
1404 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1405 PdfDocumentSearch *search = pdf_document->search;
1406 PopplerPage *poppler_page;
1407 PopplerRectangle *r;
1413 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1418 poppler_page = poppler_document_get_page (pdf_document->document, page);
1419 poppler_page_get_size (poppler_page, NULL, &height);
1420 rectangle->x1 = r->x1;
1421 rectangle->y1 = height - r->y2;
1422 rectangle->x2 = r->x2;
1423 rectangle->y2 = height - r->y1;
1424 g_object_unref (poppler_page);
1430 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1433 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1435 return search && search->pages[page] != NULL;
1439 pdf_document_find_get_progress (EvDocumentFind *document_find)
1441 PdfDocumentSearch *search;
1442 int n_pages, pages_done;
1444 search = PDF_DOCUMENT (document_find)->search;
1446 if (search == NULL) {
1450 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1451 if (search->search_page > search->start_page) {
1452 pages_done = search->search_page - search->start_page + 1;
1453 } else if (search->search_page == search->start_page) {
1454 pages_done = n_pages;
1456 pages_done = n_pages - search->start_page + search->search_page;
1459 return pages_done / (double) n_pages;
1463 pdf_document_find_cancel (EvDocumentFind *document)
1465 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1467 if (pdf_document->search) {
1468 pdf_document_search_free (pdf_document->search);
1469 pdf_document->search = NULL;
1474 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1476 iface->begin = pdf_document_find_begin;
1477 iface->get_n_results = pdf_document_find_get_n_results;
1478 iface->get_result = pdf_document_find_get_result;
1479 iface->page_has_results = pdf_document_find_page_has_results;
1480 iface->get_progress = pdf_document_find_get_progress;
1481 iface->cancel = pdf_document_find_cancel;
1484 static const gboolean supported_formats[] = {
1485 TRUE, /* EV_FILE_FORMAT_PS */
1486 #ifdef HAVE_CAIRO_PDF
1487 #ifdef HAVE_POPPLER_PAGE_RENDER
1488 TRUE, /* EV_FILE_FORMAT_PDF */
1490 FALSE, /* EV_FILE_FORMAT_PDF */
1496 pdf_print_context_free (PdfPrintContext *ctx)
1502 poppler_ps_file_free (ctx->ps_file);
1503 ctx->ps_file = NULL;
1505 #ifdef HAVE_CAIRO_PDF
1506 if (ctx->pdf_cairo) {
1507 cairo_destroy (ctx->pdf_cairo);
1508 ctx->pdf_cairo = NULL;
1515 pdf_document_file_exporter_format_supported (EvFileExporter *exporter,
1516 EvFileExporterFormat format)
1518 return supported_formats[format];
1522 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1523 EvFileExporterFormat format,
1524 const char *filename,
1531 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1532 PdfPrintContext *ctx;
1534 if (pdf_document->print_ctx)
1535 pdf_print_context_free (pdf_document->print_ctx);
1536 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1537 ctx = pdf_document->print_ctx;
1538 ctx->format = format;
1541 case EV_FILE_FORMAT_PS:
1542 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1543 filename, first_page,
1544 last_page - first_page + 1);
1545 poppler_ps_file_set_paper_size (ctx->ps_file, width, height);
1546 poppler_ps_file_set_duplex (ctx->ps_file, duplex);
1549 case EV_FILE_FORMAT_PDF: {
1550 #ifdef HAVE_CAIRO_PDF
1551 cairo_surface_t *surface;
1553 surface = cairo_pdf_surface_create (filename, width, height);
1554 ctx->pdf_cairo = cairo_create (surface);
1555 cairo_surface_destroy (surface);
1560 g_assert_not_reached ();
1565 pdf_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1567 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1568 PdfPrintContext *ctx = pdf_document->print_ctx;
1569 PopplerPage *poppler_page;
1571 g_return_if_fail (pdf_document->print_ctx != NULL);
1573 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1575 switch (ctx->format) {
1576 case EV_FILE_FORMAT_PS:
1577 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1579 case EV_FILE_FORMAT_PDF:
1580 #ifdef HAVE_POPPLER_PAGE_RENDER
1581 poppler_page_render (poppler_page, ctx->pdf_cairo);
1583 #ifdef HAVE_CAIRO_PDF
1584 cairo_show_page (ctx->pdf_cairo);
1588 g_assert_not_reached ();
1591 g_object_unref (poppler_page);
1595 pdf_document_file_exporter_end (EvFileExporter *exporter)
1597 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1599 pdf_print_context_free (pdf_document->print_ctx);
1600 pdf_document->print_ctx = NULL;
1604 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1606 iface->format_supported = pdf_document_file_exporter_format_supported;
1607 iface->begin = pdf_document_file_exporter_begin;
1608 iface->do_page = pdf_document_file_exporter_do_page;
1609 iface->end = pdf_document_file_exporter_end;
1613 pdf_selection_render_selection (EvSelection *selection,
1614 EvRenderContext *rc,
1616 EvRectangle *points,
1617 EvRectangle *old_points,
1621 PdfDocument *pdf_document;
1622 double width_points, height_points;
1625 pdf_document = PDF_DOCUMENT (selection);
1626 set_rc_data (pdf_document, rc);
1628 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
1629 width = (int) ((width_points * rc->scale) + 0.5);
1630 height = (int) ((height_points * rc->scale) + 0.5);
1632 if (*pixbuf == NULL) {
1633 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1638 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1639 rc->scale, rc->rotation, *pixbuf,
1640 (PopplerRectangle *)points,
1641 (PopplerRectangle *)old_points,
1648 pdf_selection_get_selection_region (EvSelection *selection,
1649 EvRenderContext *rc,
1650 EvRectangle *points)
1652 PdfDocument *pdf_document;
1655 pdf_document = PDF_DOCUMENT (selection);
1657 set_rc_data (pdf_document, rc);
1659 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data, rc->scale, (PopplerRectangle *) points);
1665 pdf_selection_get_selection_map (EvSelection *selection,
1666 EvRenderContext *rc)
1668 PdfDocument *pdf_document;
1669 PopplerPage *poppler_page;
1670 PopplerRectangle points;
1673 pdf_document = PDF_DOCUMENT (selection);
1674 poppler_page = poppler_document_get_page (pdf_document->document,
1679 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1680 retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1681 g_object_unref (poppler_page);
1687 pdf_selection_iface_init (EvSelectionIface *iface)
1689 iface->render_selection = pdf_selection_render_selection;
1690 iface->get_selection_region = pdf_selection_get_selection_region;
1691 iface->get_selection_map = pdf_selection_get_selection_map;
1694 /* Page Transitions */
1696 pdf_document_get_page_duration (EvDocumentTransition *trans,
1699 #ifdef HAVE_POPPLER_PAGE_GET_DURATION
1700 PdfDocument *pdf_document;
1701 PopplerPage *poppler_page;
1702 gdouble duration = -1;
1704 pdf_document = PDF_DOCUMENT (trans);
1705 poppler_page = poppler_document_get_page (pdf_document->document, page);
1709 duration = poppler_page_get_duration (poppler_page);
1710 g_object_unref (poppler_page);
1715 #endif /* HAVE_POPPLER_PAGE_GET_DURATION */
1719 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1721 iface->get_page_duration = pdf_document_get_page_duration;
1725 pdf_document_new (void)
1727 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));