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-fonts.h"
39 #include "ev-document-security.h"
40 #include "ev-document-thumbnails.h"
41 #include "ev-selection.h"
42 #include "ev-attachment.h"
45 PdfDocument *document;
54 EvFileExporterFormat format;
55 PopplerPSFile *ps_file;
61 struct _PdfDocumentClass
63 GObjectClass parent_class;
68 GObject parent_instance;
70 PopplerDocument *document;
73 PopplerFontInfo *font_info;
74 PopplerFontsIter *fonts_iter;
75 int fonts_scanned_pages;
77 PdfDocumentSearch *search;
78 PdfPrintContext *print_ctx;
81 static void pdf_document_document_iface_init (EvDocumentIface *iface);
82 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
83 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
84 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
85 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
86 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
87 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
88 static void pdf_selection_iface_init (EvSelectionIface *iface);
89 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
94 static int pdf_document_get_n_pages (EvDocument *document);
96 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
98 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
99 PopplerAction *action);
100 static void pdf_document_search_free (PdfDocumentSearch *search);
101 static void pdf_print_context_free (PdfPrintContext *ctx);
104 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
106 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
107 pdf_document_document_iface_init);
108 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
109 pdf_document_security_iface_init);
110 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
111 pdf_document_document_thumbnails_iface_init);
112 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
113 pdf_document_document_links_iface_init);
114 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
115 pdf_document_document_fonts_iface_init);
116 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
117 pdf_document_find_iface_init);
118 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
119 pdf_document_file_exporter_iface_init);
120 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
121 pdf_selection_iface_init);
126 set_rc_data (PdfDocument *pdf_document,
129 if (rc->data == NULL) {
130 rc->data = poppler_document_get_page (pdf_document->document,
132 rc->destroy = g_object_unref;
134 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
139 pdf_document_search_free (PdfDocumentSearch *search)
141 PdfDocument *pdf_document = search->document;
145 if (search->idle != 0)
146 g_source_remove (search->idle);
148 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
149 for (i = 0; i < n_pages; i++) {
150 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
151 g_list_free (search->pages[i]);
153 g_free (search->pages);
155 g_free (search->text);
160 pdf_document_dispose (GObject *object)
162 PdfDocument *pdf_document = PDF_DOCUMENT(object);
164 if (pdf_document->print_ctx) {
165 pdf_print_context_free (pdf_document->print_ctx);
166 pdf_document->print_ctx = NULL;
169 if (pdf_document->search) {
170 pdf_document_search_free (pdf_document->search);
171 pdf_document->search = NULL;
174 if (pdf_document->document) {
175 g_object_unref (pdf_document->document);
178 if (pdf_document->font_info) {
179 poppler_font_info_free (pdf_document->font_info);
182 if (pdf_document->fonts_iter) {
183 poppler_fonts_iter_free (pdf_document->fonts_iter);
188 pdf_document_class_init (PdfDocumentClass *klass)
190 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
192 g_object_class->dispose = pdf_document_dispose;
196 pdf_document_init (PdfDocument *pdf_document)
198 pdf_document->password = NULL;
202 convert_error (GError *poppler_error,
205 if (poppler_error == NULL)
208 if (poppler_error->domain == POPPLER_ERROR) {
209 /* convert poppler errors into EvDocument errors */
210 gint code = EV_DOCUMENT_ERROR_INVALID;
211 if (poppler_error->code == POPPLER_ERROR_INVALID)
212 code = EV_DOCUMENT_ERROR_INVALID;
213 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
214 code = EV_DOCUMENT_ERROR_ENCRYPTED;
220 poppler_error->message,
223 g_propagate_error (error, poppler_error);
230 pdf_document_save (EvDocument *document,
235 GError *poppler_error = NULL;
237 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
241 convert_error (poppler_error, error);
247 pdf_document_load (EvDocument *document,
251 GError *poppler_error = NULL;
252 PdfDocument *pdf_document = PDF_DOCUMENT (document);
254 pdf_document->document =
255 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
257 if (pdf_document->document == NULL) {
258 convert_error (poppler_error, error);
266 pdf_document_get_n_pages (EvDocument *document)
268 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
272 pdf_document_get_page_size (EvDocument *document,
277 PdfDocument *pdf_document = PDF_DOCUMENT (document);
278 PopplerPage *poppler_page;
280 poppler_page = poppler_document_get_page (pdf_document->document, page);
281 poppler_page_get_size (poppler_page, width, height);
282 g_object_unref (poppler_page);
286 pdf_document_get_page_label (EvDocument *document,
289 PopplerPage *poppler_page;
292 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
295 g_object_get (G_OBJECT (poppler_page),
298 g_object_unref (poppler_page);
304 pdf_document_has_attachments (EvDocument *document)
306 PdfDocument *pdf_document;
308 pdf_document = PDF_DOCUMENT (document);
310 return poppler_document_has_attachments (pdf_document->document);
313 struct SaveToBufferData {
319 attachment_save_to_buffer_callback (const gchar *buf,
324 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
328 if (sdata->len + count > sdata->max) {
329 new_max = MAX (sdata->max * 2, sdata->len + count);
330 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
332 sdata->buffer = new_buffer;
333 sdata->max = new_max;
336 memcpy (sdata->buffer + sdata->len, buf, count);
343 attachment_save_to_buffer (PopplerAttachment *attachment,
348 static const gint initial_max = 1024;
349 struct SaveToBufferData sdata;
354 sdata.buffer = (gchar *) g_malloc (initial_max);
355 sdata.max = initial_max;
358 if (! poppler_attachment_save_to_callback (attachment,
359 attachment_save_to_buffer_callback,
362 g_free (sdata.buffer);
366 *buffer = sdata.buffer;
367 *buffer_size = sdata.len;
373 pdf_document_get_attachments (EvDocument *document)
375 PdfDocument *pdf_document;
378 GList *retval = NULL;
380 pdf_document = PDF_DOCUMENT (document);
382 if (!pdf_document_has_attachments (document))
385 attachments = poppler_document_get_attachments (pdf_document->document);
387 for (list = attachments; list; list = list->next) {
388 PopplerAttachment *attachment;
389 EvAttachment *ev_attachment;
392 GError *error = NULL;
394 attachment = (PopplerAttachment *) list->data;
396 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
397 ev_attachment = ev_attachment_new (attachment->name,
398 attachment->description,
403 retval = g_list_prepend (retval, ev_attachment);
406 g_warning ("%s", error->message);
407 g_error_free (error);
413 g_object_unref (attachment);
416 return g_list_reverse (retval);
420 pdf_document_render_pixbuf (EvDocument *document,
423 PdfDocument *pdf_document;
425 double width_points, height_points;
428 pdf_document = PDF_DOCUMENT (document);
430 set_rc_data (pdf_document, rc);
432 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
434 if (rc->rotation == 90 || rc->rotation == 270) {
435 width = (int) ((height_points * rc->scale) + 0.5);
436 height = (int) ((width_points * rc->scale) + 0.5);
438 width = (int) ((width_points * rc->scale) + 0.5);
439 height = (int) ((height_points * rc->scale) + 0.5);
442 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
446 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
457 /* EvDocumentSecurity */
460 pdf_document_has_document_security (EvDocumentSecurity *document_security)
462 /* FIXME: do we really need to have this? */
467 pdf_document_set_password (EvDocumentSecurity *document_security,
468 const char *password)
470 PdfDocument *document = PDF_DOCUMENT (document_security);
472 if (document->password)
473 g_free (document->password);
475 document->password = g_strdup (password);
479 pdf_document_can_get_text (EvDocument *document)
484 static EvDocumentInfo *
485 pdf_document_get_info (EvDocument *document)
487 EvDocumentInfo *info;
488 PopplerPageLayout layout;
489 PopplerPageMode mode;
490 PopplerViewerPreferences view_prefs;
491 PopplerPermissions permissions;
493 info = g_new0 (EvDocumentInfo, 1);
495 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
496 EV_DOCUMENT_INFO_FORMAT |
497 EV_DOCUMENT_INFO_AUTHOR |
498 EV_DOCUMENT_INFO_SUBJECT |
499 EV_DOCUMENT_INFO_KEYWORDS |
500 EV_DOCUMENT_INFO_LAYOUT |
501 EV_DOCUMENT_INFO_START_MODE |
502 EV_DOCUMENT_INFO_PERMISSIONS |
503 EV_DOCUMENT_INFO_UI_HINTS |
504 EV_DOCUMENT_INFO_CREATOR |
505 EV_DOCUMENT_INFO_PRODUCER |
506 EV_DOCUMENT_INFO_CREATION_DATE |
507 EV_DOCUMENT_INFO_MOD_DATE |
508 EV_DOCUMENT_INFO_LINEARIZED |
509 EV_DOCUMENT_INFO_N_PAGES |
510 EV_DOCUMENT_INFO_SECURITY |
511 EV_DOCUMENT_INFO_PAPER_SIZE;
513 g_object_get (PDF_DOCUMENT (document)->document,
514 "title", &(info->title),
515 "format", &(info->format),
516 "author", &(info->author),
517 "subject", &(info->subject),
518 "keywords", &(info->keywords),
520 "page-layout", &layout,
521 "viewer-preferences", &view_prefs,
522 "permissions", &permissions,
523 "creator", &(info->creator),
524 "producer", &(info->producer),
525 "creation-date", &(info->creation_date),
526 "mod-date", &(info->modified_date),
527 "linearized", &(info->linearized),
530 pdf_document_get_page_size(document, 0,
531 &(info->paper_width),
532 &(info->paper_height));
535 info->paper_width = info->paper_width / 72.0f * 25.4f;
536 info->paper_height = info->paper_height / 72.0f * 25.4f;
539 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
540 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
542 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
543 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
545 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
546 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
548 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
549 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
550 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
551 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
553 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
554 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
561 case POPPLER_PAGE_MODE_NONE:
562 info->mode = EV_DOCUMENT_MODE_NONE;
564 case POPPLER_PAGE_MODE_USE_THUMBS:
565 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
567 case POPPLER_PAGE_MODE_USE_OC:
568 info->mode = EV_DOCUMENT_MODE_USE_OC;
570 case POPPLER_PAGE_MODE_FULL_SCREEN:
571 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
573 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
574 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
580 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
581 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
583 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
584 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
586 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
587 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
589 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
590 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
592 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
593 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
595 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
596 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
598 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
599 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
602 info->permissions = 0;
603 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
604 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
606 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
607 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
609 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
610 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
612 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
613 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
616 info->n_pages = ev_document_get_n_pages (document);
618 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
619 /* translators: this is the document security state */
620 info->security = g_strdup (_("Yes"));
622 /* translators: this is the document security state */
623 info->security = g_strdup (_("No"));
630 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
632 PdfDocument *pdf_document = PDF_DOCUMENT (document);
633 PopplerPage *poppler_page;
638 poppler_page = poppler_document_get_page (pdf_document->document, page);
639 g_return_val_if_fail (poppler_page != NULL, NULL);
641 poppler_page_get_size (poppler_page, NULL, &height);
643 r.y1 = height - rect->y2;
645 r.y2 = height - rect->y1;
647 text = poppler_page_get_text (poppler_page, &r);
649 g_object_unref (poppler_page);
655 pdf_document_document_iface_init (EvDocumentIface *iface)
657 iface->save = pdf_document_save;
658 iface->load = pdf_document_load;
659 iface->get_n_pages = pdf_document_get_n_pages;
660 iface->get_page_size = pdf_document_get_page_size;
661 iface->get_page_label = pdf_document_get_page_label;
662 iface->has_attachments = pdf_document_has_attachments;
663 iface->get_attachments = pdf_document_get_attachments;
664 iface->render_pixbuf = pdf_document_render_pixbuf;
665 iface->get_text = pdf_document_get_text;
666 iface->can_get_text = pdf_document_can_get_text;
667 iface->get_info = pdf_document_get_info;
671 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
673 iface->has_document_security = pdf_document_has_document_security;
674 iface->set_password = pdf_document_set_password;
678 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
680 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
683 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
685 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
689 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
692 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
695 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
697 if (pdf_document->font_info == NULL) {
698 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
701 if (pdf_document->fonts_iter) {
702 poppler_fonts_iter_free (pdf_document->fonts_iter);
705 pdf_document->fonts_scanned_pages += n_pages;
707 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
708 &pdf_document->fonts_iter);
710 pdf_document->fonts_scanned_pages = 0;
711 poppler_font_info_free (pdf_document->font_info);
712 pdf_document->font_info = NULL;
719 font_type_to_string (PopplerFontType type)
722 case POPPLER_FONT_TYPE_TYPE1:
724 case POPPLER_FONT_TYPE_TYPE1C:
726 case POPPLER_FONT_TYPE_TYPE3:
728 case POPPLER_FONT_TYPE_TRUETYPE:
729 return _("TrueType");
730 case POPPLER_FONT_TYPE_CID_TYPE0:
731 return _("Type 1 (CID)");
732 case POPPLER_FONT_TYPE_CID_TYPE0C:
733 return _("Type 1C (CID)");
734 case POPPLER_FONT_TYPE_CID_TYPE2:
735 return _("TrueType (CID)");
737 return _("Unknown font type");
742 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
745 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
746 PopplerFontsIter *iter = pdf_document->fonts_iter;
748 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
754 GtkTreeIter list_iter;
757 const char *embedded;
760 name = poppler_fonts_iter_get_name (iter);
766 type = font_type_to_string (
767 poppler_fonts_iter_get_font_type (iter));
769 if (poppler_fonts_iter_is_embedded (iter)) {
770 if (poppler_fonts_iter_is_subset (iter))
771 embedded = _("Embedded subset");
773 embedded = _("Embedded");
775 embedded = _("Not embedded");
778 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
780 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
781 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
782 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
783 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
787 } while (poppler_fonts_iter_next (iter));
791 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
793 iface->fill_model = pdf_document_fonts_fill_model;
794 iface->scan = pdf_document_fonts_scan;
795 iface->get_progress = pdf_document_fonts_get_progress;
799 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
801 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
802 PopplerIndexIter *iter;
804 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
806 iter = poppler_index_iter_new (pdf_document->document);
809 poppler_index_iter_free (iter);
815 ev_link_dest_from_dest (PdfDocument *pdf_document,
818 EvLinkDest *ev_dest = NULL;
819 const char *unimplemented_dest = NULL;
821 g_assert (dest != NULL);
823 switch (dest->type) {
824 case POPPLER_DEST_XYZ: {
825 PopplerPage *poppler_page;
828 poppler_page = poppler_document_get_page (pdf_document->document,
829 MAX (0, dest->page_num - 1));
830 poppler_page_get_size (poppler_page, NULL, &height);
831 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
835 g_object_unref (poppler_page);
838 case POPPLER_DEST_FIT:
839 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
841 case POPPLER_DEST_FITH: {
842 PopplerPage *poppler_page;
845 poppler_page = poppler_document_get_page (pdf_document->document,
846 MAX (0, dest->page_num - 1));
847 poppler_page_get_size (poppler_page, NULL, &height);
848 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
850 g_object_unref (poppler_page);
853 case POPPLER_DEST_FITV:
854 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
857 case POPPLER_DEST_FITR: {
858 PopplerPage *poppler_page;
861 poppler_page = poppler_document_get_page (pdf_document->document,
862 MAX (0, dest->page_num - 1));
863 poppler_page_get_size (poppler_page, NULL, &height);
864 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
866 height - dest->bottom,
869 g_object_unref (poppler_page);
872 case POPPLER_DEST_FITB:
873 unimplemented_dest = "POPPLER_DEST_FITB";
875 case POPPLER_DEST_FITBH:
876 unimplemented_dest = "POPPLER_DEST_FITBH";
878 case POPPLER_DEST_FITBV:
879 unimplemented_dest = "POPPLER_DEST_FITBV";
881 case POPPLER_DEST_NAMED:
882 ev_dest = ev_link_dest_new_named (dest->named_dest);
884 case POPPLER_DEST_UNKNOWN:
885 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
889 if (unimplemented_dest) {
890 g_warning ("Unimplemented named action: %s, please post a "
891 "bug report in Evince bugzilla "
892 "(http://bugzilla.gnome.org) with a testcase.",
897 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
903 ev_link_from_action (PdfDocument *pdf_document,
904 PopplerAction *action)
907 EvLinkAction *ev_action = NULL;
908 const char *unimplemented_action = NULL;
910 switch (action->type) {
911 case POPPLER_ACTION_GOTO_DEST: {
914 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
915 ev_action = ev_link_action_new_dest (dest);
918 case POPPLER_ACTION_GOTO_REMOTE: {
921 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
922 ev_action = ev_link_action_new_remote (dest,
923 action->goto_remote.file_name);
927 case POPPLER_ACTION_LAUNCH:
928 ev_action = ev_link_action_new_launch (action->launch.file_name,
929 action->launch.params);
931 case POPPLER_ACTION_URI:
932 ev_action = ev_link_action_new_external_uri (action->uri.uri);
934 case POPPLER_ACTION_NAMED:
935 ev_action = ev_link_action_new_named (action->named.named_dest);
937 case POPPLER_ACTION_MOVIE:
938 unimplemented_action = "POPPLER_ACTION_MOVIE";
940 case POPPLER_ACTION_UNKNOWN:
941 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
944 if (unimplemented_action) {
945 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
946 unimplemented_action);
949 link = ev_link_new (action->any.title, ev_action);
955 build_tree (PdfDocument *pdf_document,
958 PopplerIndexIter *iter)
962 GtkTreeIter tree_iter;
963 PopplerIndexIter *child;
964 PopplerAction *action;
969 action = poppler_index_iter_get_action (iter);
970 expand = poppler_index_iter_is_open (iter);
975 switch (action->type) {
976 case POPPLER_ACTION_GOTO_DEST: {
977 /* For bookmarks, solve named destinations */
978 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
980 EvLinkDest *ev_dest = NULL;
981 EvLinkAction *ev_action;
983 dest = poppler_document_find_dest (pdf_document->document,
984 action->goto_dest.dest->named_dest);
986 link = ev_link_from_action (pdf_document, action);
990 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
991 poppler_dest_free (dest);
993 ev_action = ev_link_action_new_dest (ev_dest);
994 link = ev_link_new (action->any.title, ev_action);
996 link = ev_link_from_action (pdf_document, action);
1001 link = ev_link_from_action (pdf_document, action);
1006 poppler_action_free (action);
1010 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1011 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1013 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1014 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1015 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1016 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1019 g_free (title_markup);
1020 g_object_unref (link);
1022 child = poppler_index_iter_get_child (iter);
1024 build_tree (pdf_document, model, &tree_iter, child);
1025 poppler_index_iter_free (child);
1026 poppler_action_free (action);
1028 } while (poppler_index_iter_next (iter));
1031 static GtkTreeModel *
1032 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1034 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1035 GtkTreeModel *model = NULL;
1036 PopplerIndexIter *iter;
1038 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1040 iter = poppler_index_iter_new (pdf_document->document);
1041 /* Create the model if we have items*/
1043 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1047 build_tree (pdf_document, model, NULL, iter);
1048 poppler_index_iter_free (iter);
1055 pdf_document_links_get_links (EvDocumentLinks *document_links,
1058 PdfDocument *pdf_document;
1059 PopplerPage *poppler_page;
1060 GList *retval = NULL;
1061 GList *mapping_list;
1065 pdf_document = PDF_DOCUMENT (document_links);
1066 poppler_page = poppler_document_get_page (pdf_document->document,
1068 mapping_list = poppler_page_get_link_mapping (poppler_page);
1069 poppler_page_get_size (poppler_page, NULL, &height);
1071 for (list = mapping_list; list; list = list->next) {
1072 PopplerLinkMapping *link_mapping;
1073 EvLinkMapping *ev_link_mapping;
1075 link_mapping = (PopplerLinkMapping *)list->data;
1076 ev_link_mapping = g_new (EvLinkMapping, 1);
1077 ev_link_mapping->link = ev_link_from_action (pdf_document,
1078 link_mapping->action);
1079 ev_link_mapping->x1 = link_mapping->area.x1;
1080 ev_link_mapping->x2 = link_mapping->area.x2;
1081 /* Invert this for X-style coordinates */
1082 ev_link_mapping->y1 = height - link_mapping->area.y2;
1083 ev_link_mapping->y2 = height - link_mapping->area.y1;
1085 retval = g_list_prepend (retval, ev_link_mapping);
1088 poppler_page_free_link_mapping (mapping_list);
1089 g_object_unref (poppler_page);
1091 return g_list_reverse (retval);
1095 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1096 const gchar *link_name)
1098 PdfDocument *pdf_document;
1100 EvLinkDest *ev_dest = NULL;
1102 pdf_document = PDF_DOCUMENT (document_links);
1103 dest = poppler_document_find_dest (pdf_document->document,
1106 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1107 poppler_dest_free (dest);
1114 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1116 iface->has_document_links = pdf_document_links_has_document_links;
1117 iface->get_links_model = pdf_document_links_get_links_model;
1118 iface->get_links = pdf_document_links_get_links;
1119 iface->find_link_dest = pdf_document_links_find_link_dest;
1123 make_thumbnail_for_size (PdfDocument *pdf_document,
1128 PopplerPage *poppler_page;
1132 gdouble unscaled_width, unscaled_height;
1134 poppler_page = poppler_document_get_page (pdf_document->document, page);
1135 g_return_val_if_fail (poppler_page != NULL, NULL);
1137 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page,
1138 size, &width, &height);
1139 poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
1140 scale = width / unscaled_width;
1143 if (rotation == 90 || rotation == 270) {
1150 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1152 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1154 ev_document_fc_mutex_lock ();
1155 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1157 scale, rotation, pixbuf);
1158 ev_document_fc_mutex_unlock ();
1161 g_object_unref (poppler_page);
1167 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1173 PdfDocument *pdf_document;
1174 PopplerPage *poppler_page;
1176 GdkPixbuf *border_pixbuf;
1178 pdf_document = PDF_DOCUMENT (document_thumbnails);
1180 poppler_page = poppler_document_get_page (pdf_document->document, page);
1181 g_return_val_if_fail (poppler_page != NULL, NULL);
1183 pixbuf = poppler_page_get_thumbnail (poppler_page);
1185 if (pixbuf == NULL) {
1186 /* There is no provided thumbnail. We need to make one. */
1187 pixbuf = make_thumbnail_for_size (pdf_document, page, rotation, size);
1191 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, rotation, pixbuf);
1192 g_object_unref (pixbuf);
1193 pixbuf = border_pixbuf;
1196 g_object_unref (poppler_page);
1202 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1208 PdfDocument *pdf_document;
1209 PopplerPage *poppler_page;
1212 pdf_document = PDF_DOCUMENT (document_thumbnails);
1213 poppler_page = poppler_document_get_page (pdf_document->document, page);
1215 g_return_if_fail (width != NULL);
1216 g_return_if_fail (height != NULL);
1217 g_return_if_fail (poppler_page != NULL);
1219 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1222 double page_width, page_height;
1224 poppler_page_get_size (poppler_page, &page_width, &page_height);
1226 *height = (int) (size * page_height / page_width);
1228 g_object_unref (poppler_page);
1232 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1234 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1235 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1240 pdf_document_search_idle_callback (void *data)
1242 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1243 PdfDocument *pdf_document = search->document;
1248 page = poppler_document_get_page (search->document->document,
1249 search->search_page);
1251 ev_document_doc_mutex_lock ();
1252 matches = poppler_page_find_text (page, search->text);
1253 ev_document_doc_mutex_unlock ();
1255 g_object_unref (page);
1257 search->pages[search->search_page] = matches;
1258 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1259 search->search_page);
1261 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1262 search->search_page += 1;
1263 if (search->search_page == n_pages) {
1265 search->search_page = 0;
1268 if (search->search_page != search->start_page) {
1273 search->idle = 0; /* will return FALSE to remove */
1278 static PdfDocumentSearch *
1279 pdf_document_search_new (PdfDocument *pdf_document,
1283 PdfDocumentSearch *search;
1287 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1289 search = g_new0 (PdfDocumentSearch, 1);
1291 search->text = g_strdup (text);
1292 search->pages = g_new0 (GList *, n_pages);
1293 search->document = pdf_document;
1295 /* We add at low priority so the progress bar repaints */
1296 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1297 pdf_document_search_idle_callback,
1301 search->start_page = start_page;
1302 search->search_page = start_page;
1308 pdf_document_find_begin (EvDocumentFind *document,
1310 const char *search_string,
1311 gboolean case_sensitive)
1313 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1315 /* FIXME handle case_sensitive (right now XPDF
1316 * code is always case insensitive for ASCII
1317 * and case sensitive for all other languaages)
1320 if (pdf_document->search &&
1321 strcmp (search_string, pdf_document->search->text) == 0)
1324 if (pdf_document->search)
1325 pdf_document_search_free (pdf_document->search);
1327 pdf_document->search = pdf_document_search_new (pdf_document,
1333 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1335 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1338 return g_list_length (search->pages[page]);
1345 pdf_document_find_get_result (EvDocumentFind *document_find,
1348 EvRectangle *rectangle)
1350 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1351 PdfDocumentSearch *search = pdf_document->search;
1352 PopplerPage *poppler_page;
1353 PopplerRectangle *r;
1359 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1364 poppler_page = poppler_document_get_page (pdf_document->document, page);
1365 poppler_page_get_size (poppler_page, NULL, &height);
1366 rectangle->x1 = r->x1;
1367 rectangle->y1 = height - r->y2;
1368 rectangle->x2 = r->x2;
1369 rectangle->y2 = height - r->y1;
1370 g_object_unref (poppler_page);
1376 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1379 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1381 return search && search->pages[page] != NULL;
1385 pdf_document_find_get_progress (EvDocumentFind *document_find)
1387 PdfDocumentSearch *search;
1388 int n_pages, pages_done;
1390 search = PDF_DOCUMENT (document_find)->search;
1392 if (search == NULL) {
1396 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1397 if (search->search_page > search->start_page) {
1398 pages_done = search->search_page - search->start_page + 1;
1399 } else if (search->search_page == search->start_page) {
1400 pages_done = n_pages;
1402 pages_done = n_pages - search->start_page + search->search_page;
1405 return pages_done / (double) n_pages;
1409 pdf_document_find_cancel (EvDocumentFind *document)
1411 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1413 if (pdf_document->search) {
1414 pdf_document_search_free (pdf_document->search);
1415 pdf_document->search = NULL;
1420 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1422 iface->begin = pdf_document_find_begin;
1423 iface->get_n_results = pdf_document_find_get_n_results;
1424 iface->get_result = pdf_document_find_get_result;
1425 iface->page_has_results = pdf_document_find_page_has_results;
1426 iface->get_progress = pdf_document_find_get_progress;
1427 iface->cancel = pdf_document_find_cancel;
1430 static const gboolean supported_formats[] = {
1431 TRUE, /* EV_FILE_FORMAT_PS */
1432 #ifdef HAVE_CAIRO_PDF
1433 #ifdef HAVE_POPPLER_PAGE_RENDER
1434 TRUE, /* EV_FILE_FORMAT_PDF */
1436 FALSE, /* EV_FILE_FORMAT_PDF */
1442 pdf_print_context_free (PdfPrintContext *ctx)
1448 poppler_ps_file_free (ctx->ps_file);
1449 ctx->ps_file = NULL;
1451 #ifdef HAVE_CAIRO_PDF
1452 if (ctx->pdf_cairo) {
1453 cairo_destroy (ctx->pdf_cairo);
1454 ctx->pdf_cairo = NULL;
1461 pdf_document_file_exporter_format_supported (EvFileExporter *exporter,
1462 EvFileExporterFormat format)
1464 return supported_formats[format];
1468 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1469 EvFileExporterFormat format,
1470 const char *filename,
1477 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1478 PdfPrintContext *ctx;
1480 if (pdf_document->print_ctx)
1481 pdf_print_context_free (pdf_document->print_ctx);
1482 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1483 ctx = pdf_document->print_ctx;
1484 ctx->format = format;
1487 case EV_FILE_FORMAT_PS:
1488 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1489 filename, first_page,
1490 last_page - first_page + 1);
1491 poppler_ps_file_set_paper_size (ctx->ps_file, width, height);
1492 poppler_ps_file_set_duplex (ctx->ps_file, duplex);
1495 case EV_FILE_FORMAT_PDF: {
1496 #ifdef HAVE_CAIRO_PDF
1497 cairo_surface_t *surface;
1499 surface = cairo_pdf_surface_create (filename, width, height);
1500 ctx->pdf_cairo = cairo_create (surface);
1501 cairo_surface_destroy (surface);
1506 g_assert_not_reached ();
1511 pdf_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1513 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1514 PdfPrintContext *ctx = pdf_document->print_ctx;
1515 PopplerPage *poppler_page;
1517 g_return_if_fail (pdf_document->print_ctx != NULL);
1519 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1521 switch (ctx->format) {
1522 case EV_FILE_FORMAT_PS:
1523 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1525 case EV_FILE_FORMAT_PDF:
1526 #ifdef HAVE_POPPLER_PAGE_RENDER
1527 poppler_page_render (poppler_page, ctx->pdf_cairo);
1529 #ifdef HAVE_CAIRO_PDF
1530 cairo_show_page (ctx->pdf_cairo);
1534 g_assert_not_reached ();
1537 g_object_unref (poppler_page);
1541 pdf_document_file_exporter_end (EvFileExporter *exporter)
1543 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1545 pdf_print_context_free (pdf_document->print_ctx);
1546 pdf_document->print_ctx = NULL;
1550 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1552 iface->format_supported = pdf_document_file_exporter_format_supported;
1553 iface->begin = pdf_document_file_exporter_begin;
1554 iface->do_page = pdf_document_file_exporter_do_page;
1555 iface->end = pdf_document_file_exporter_end;
1559 pdf_selection_render_selection (EvSelection *selection,
1560 EvRenderContext *rc,
1562 EvRectangle *points,
1563 EvRectangle *old_points,
1567 PdfDocument *pdf_document;
1568 double width_points, height_points;
1571 pdf_document = PDF_DOCUMENT (selection);
1572 set_rc_data (pdf_document, rc);
1574 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
1575 width = (int) ((width_points * rc->scale) + 0.5);
1576 height = (int) ((height_points * rc->scale) + 0.5);
1578 if (*pixbuf == NULL) {
1579 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1584 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1585 rc->scale, rc->rotation, *pixbuf,
1586 (PopplerRectangle *)points,
1587 (PopplerRectangle *)old_points,
1594 pdf_selection_get_selection_region (EvSelection *selection,
1595 EvRenderContext *rc,
1596 EvRectangle *points)
1598 PdfDocument *pdf_document;
1601 pdf_document = PDF_DOCUMENT (selection);
1603 set_rc_data (pdf_document, rc);
1605 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data, rc->scale, (PopplerRectangle *) points);
1611 pdf_selection_get_selection_map (EvSelection *selection,
1612 EvRenderContext *rc)
1614 PdfDocument *pdf_document;
1615 PopplerPage *poppler_page;
1616 PopplerRectangle points;
1619 pdf_document = PDF_DOCUMENT (selection);
1620 poppler_page = poppler_document_get_page (pdf_document->document,
1625 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1626 retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1627 g_object_unref (poppler_page);
1633 pdf_selection_iface_init (EvSelectionIface *iface)
1635 iface->render_selection = pdf_selection_render_selection;
1636 iface->get_selection_region = pdf_selection_get_selection_region;
1637 iface->get_selection_map = pdf_selection_get_selection_map;
1641 pdf_document_new (void)
1643 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));