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>
28 #include <cairo-pdf.h>
29 #include <glib/gi18n.h>
31 #include "ev-poppler.h"
32 #include "ev-file-exporter.h"
33 #include "ev-document-find.h"
34 #include "ev-document-misc.h"
35 #include "ev-document-links.h"
36 #include "ev-document-fonts.h"
37 #include "ev-document-security.h"
38 #include "ev-document-thumbnails.h"
39 #include "ev-selection.h"
40 #include "ev-attachment.h"
43 PdfDocument *document;
52 EvFileExporterFormat format;
53 PopplerPSFile *ps_file;
57 struct _PdfDocumentClass
59 GObjectClass parent_class;
64 GObject parent_instance;
66 PopplerDocument *document;
69 PopplerFontInfo *font_info;
70 PopplerFontsIter *fonts_iter;
71 int fonts_scanned_pages;
73 PdfDocumentSearch *search;
74 PdfPrintContext *print_ctx;
77 static void pdf_document_document_iface_init (EvDocumentIface *iface);
78 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
79 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
80 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
81 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
82 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
83 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
84 static void pdf_selection_iface_init (EvSelectionIface *iface);
85 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
90 static int pdf_document_get_n_pages (EvDocument *document);
92 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
94 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
95 PopplerAction *action);
96 static void pdf_document_search_free (PdfDocumentSearch *search);
97 static void pdf_print_context_free (PdfPrintContext *ctx);
100 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
102 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
103 pdf_document_document_iface_init);
104 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
105 pdf_document_security_iface_init);
106 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
107 pdf_document_document_thumbnails_iface_init);
108 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
109 pdf_document_document_links_iface_init);
110 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
111 pdf_document_document_fonts_iface_init);
112 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
113 pdf_document_find_iface_init);
114 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
115 pdf_document_file_exporter_iface_init);
116 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
117 pdf_selection_iface_init);
122 set_rc_data (PdfDocument *pdf_document,
125 if (rc->data == NULL) {
126 rc->data = poppler_document_get_page (pdf_document->document,
128 rc->destroy = g_object_unref;
130 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
135 pdf_document_search_free (PdfDocumentSearch *search)
137 PdfDocument *pdf_document = search->document;
141 if (search->idle != 0)
142 g_source_remove (search->idle);
144 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
145 for (i = 0; i < n_pages; i++) {
146 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
147 g_list_free (search->pages[i]);
149 g_free (search->pages);
151 g_free (search->text);
156 pdf_document_dispose (GObject *object)
158 PdfDocument *pdf_document = PDF_DOCUMENT(object);
160 if (pdf_document->print_ctx) {
161 pdf_print_context_free (pdf_document->print_ctx);
162 pdf_document->print_ctx = NULL;
165 if (pdf_document->search) {
166 pdf_document_search_free (pdf_document->search);
167 pdf_document->search = NULL;
170 if (pdf_document->document) {
171 g_object_unref (pdf_document->document);
174 if (pdf_document->font_info) {
175 poppler_font_info_free (pdf_document->font_info);
178 if (pdf_document->fonts_iter) {
179 poppler_fonts_iter_free (pdf_document->fonts_iter);
184 pdf_document_class_init (PdfDocumentClass *klass)
186 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
188 g_object_class->dispose = pdf_document_dispose;
192 pdf_document_init (PdfDocument *pdf_document)
194 pdf_document->password = NULL;
198 convert_error (GError *poppler_error,
201 if (poppler_error == NULL)
204 if (poppler_error->domain == POPPLER_ERROR) {
205 /* convert poppler errors into EvDocument errors */
206 gint code = EV_DOCUMENT_ERROR_INVALID;
207 if (poppler_error->code == POPPLER_ERROR_INVALID)
208 code = EV_DOCUMENT_ERROR_INVALID;
209 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
210 code = EV_DOCUMENT_ERROR_ENCRYPTED;
216 poppler_error->message,
219 g_propagate_error (error, poppler_error);
226 pdf_document_save (EvDocument *document,
231 GError *poppler_error = NULL;
233 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
237 convert_error (poppler_error, error);
243 pdf_document_load (EvDocument *document,
247 GError *poppler_error = NULL;
248 PdfDocument *pdf_document = PDF_DOCUMENT (document);
250 pdf_document->document =
251 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
253 if (pdf_document->document == NULL) {
254 convert_error (poppler_error, error);
262 pdf_document_get_n_pages (EvDocument *document)
264 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
268 pdf_document_get_page_size (EvDocument *document,
273 PdfDocument *pdf_document = PDF_DOCUMENT (document);
274 PopplerPage *poppler_page;
276 poppler_page = poppler_document_get_page (pdf_document->document, page);
277 poppler_page_get_size (poppler_page, width, height);
278 g_object_unref (poppler_page);
282 pdf_document_get_page_label (EvDocument *document,
285 PopplerPage *poppler_page;
288 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
291 g_object_get (G_OBJECT (poppler_page),
294 g_object_unref (poppler_page);
300 pdf_document_has_attachments (EvDocument *document)
302 PdfDocument *pdf_document;
304 pdf_document = PDF_DOCUMENT (document);
306 return poppler_document_has_attachments (pdf_document->document);
309 struct SaveToBufferData {
315 attachment_save_to_buffer_callback (const gchar *buf,
320 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
324 if (sdata->len + count > sdata->max) {
325 new_max = MAX (sdata->max * 2, sdata->len + count);
326 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
328 sdata->buffer = new_buffer;
329 sdata->max = new_max;
332 memcpy (sdata->buffer + sdata->len, buf, count);
339 attachment_save_to_buffer (PopplerAttachment *attachment,
344 static const gint initial_max = 1024;
345 struct SaveToBufferData sdata;
350 sdata.buffer = (gchar *) g_malloc (initial_max);
351 sdata.max = initial_max;
354 if (! poppler_attachment_save_to_callback (attachment,
355 attachment_save_to_buffer_callback,
358 g_free (sdata.buffer);
362 *buffer = sdata.buffer;
363 *buffer_size = sdata.len;
369 pdf_document_get_attachments (EvDocument *document)
371 PdfDocument *pdf_document;
374 GList *retval = NULL;
376 pdf_document = PDF_DOCUMENT (document);
378 if (!pdf_document_has_attachments (document))
381 attachments = poppler_document_get_attachments (pdf_document->document);
383 for (list = attachments; list; list = list->next) {
384 PopplerAttachment *attachment;
385 EvAttachment *ev_attachment;
388 GError *error = NULL;
390 attachment = (PopplerAttachment *) list->data;
392 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
393 ev_attachment = ev_attachment_new (attachment->name,
394 attachment->description,
399 retval = g_list_prepend (retval, ev_attachment);
402 g_warning ("%s", error->message);
403 g_error_free (error);
409 g_object_unref (attachment);
412 return g_list_reverse (retval);
416 pdf_document_render_pixbuf (EvDocument *document,
419 PdfDocument *pdf_document;
421 double width_points, height_points;
424 pdf_document = PDF_DOCUMENT (document);
426 set_rc_data (pdf_document, rc);
428 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
430 if (rc->rotation == 90 || rc->rotation == 270) {
431 width = (int) ((height_points * rc->scale) + 0.5);
432 height = (int) ((width_points * rc->scale) + 0.5);
434 width = (int) ((width_points * rc->scale) + 0.5);
435 height = (int) ((height_points * rc->scale) + 0.5);
438 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
442 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
453 /* EvDocumentSecurity */
456 pdf_document_has_document_security (EvDocumentSecurity *document_security)
458 /* FIXME: do we really need to have this? */
463 pdf_document_set_password (EvDocumentSecurity *document_security,
464 const char *password)
466 PdfDocument *document = PDF_DOCUMENT (document_security);
468 if (document->password)
469 g_free (document->password);
471 document->password = g_strdup (password);
475 pdf_document_can_get_text (EvDocument *document)
480 static EvDocumentInfo *
481 pdf_document_get_info (EvDocument *document)
483 EvDocumentInfo *info;
484 PopplerPageLayout layout;
485 PopplerPageMode mode;
486 PopplerViewerPreferences view_prefs;
487 PopplerPermissions permissions;
489 info = g_new0 (EvDocumentInfo, 1);
491 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
492 EV_DOCUMENT_INFO_FORMAT |
493 EV_DOCUMENT_INFO_AUTHOR |
494 EV_DOCUMENT_INFO_SUBJECT |
495 EV_DOCUMENT_INFO_KEYWORDS |
496 EV_DOCUMENT_INFO_LAYOUT |
497 EV_DOCUMENT_INFO_START_MODE |
498 EV_DOCUMENT_INFO_PERMISSIONS |
499 EV_DOCUMENT_INFO_UI_HINTS |
500 EV_DOCUMENT_INFO_CREATOR |
501 EV_DOCUMENT_INFO_PRODUCER |
502 EV_DOCUMENT_INFO_CREATION_DATE |
503 EV_DOCUMENT_INFO_MOD_DATE |
504 EV_DOCUMENT_INFO_LINEARIZED |
505 EV_DOCUMENT_INFO_N_PAGES |
506 EV_DOCUMENT_INFO_SECURITY |
507 EV_DOCUMENT_INFO_PAPER_SIZE;
509 g_object_get (PDF_DOCUMENT (document)->document,
510 "title", &(info->title),
511 "format", &(info->format),
512 "author", &(info->author),
513 "subject", &(info->subject),
514 "keywords", &(info->keywords),
516 "page-layout", &layout,
517 "viewer-preferences", &view_prefs,
518 "permissions", &permissions,
519 "creator", &(info->creator),
520 "producer", &(info->producer),
521 "creation-date", &(info->creation_date),
522 "mod-date", &(info->modified_date),
523 "linearized", &(info->linearized),
526 pdf_document_get_page_size(document, 0,
527 &(info->paper_width),
528 &(info->paper_height));
531 info->paper_width = info->paper_width / 72.0f * 25.4f;
532 info->paper_height = info->paper_height / 72.0f * 25.4f;
535 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
536 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
538 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
539 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
541 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
542 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
544 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
545 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
546 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
547 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
549 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
550 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
557 case POPPLER_PAGE_MODE_NONE:
558 info->mode = EV_DOCUMENT_MODE_NONE;
560 case POPPLER_PAGE_MODE_USE_THUMBS:
561 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
563 case POPPLER_PAGE_MODE_USE_OC:
564 info->mode = EV_DOCUMENT_MODE_USE_OC;
566 case POPPLER_PAGE_MODE_FULL_SCREEN:
567 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
569 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
570 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
576 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
577 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
579 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
580 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
582 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
583 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
585 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
586 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
588 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
589 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
591 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
592 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
594 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
595 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
598 info->permissions = 0;
599 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
600 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
602 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
603 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
605 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
606 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
608 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
609 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
612 info->n_pages = ev_document_get_n_pages (document);
614 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
615 /* translators: this is the document security state */
616 info->security = g_strdup (_("Yes"));
618 /* translators: this is the document security state */
619 info->security = g_strdup (_("No"));
626 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
628 PdfDocument *pdf_document = PDF_DOCUMENT (document);
629 PopplerPage *poppler_page;
634 poppler_page = poppler_document_get_page (pdf_document->document, page);
635 g_return_val_if_fail (poppler_page != NULL, NULL);
637 poppler_page_get_size (poppler_page, NULL, &height);
639 r.y1 = height - rect->y2;
641 r.y2 = height - rect->y1;
643 text = poppler_page_get_text (poppler_page, &r);
645 g_object_unref (poppler_page);
651 pdf_document_document_iface_init (EvDocumentIface *iface)
653 iface->save = pdf_document_save;
654 iface->load = pdf_document_load;
655 iface->get_n_pages = pdf_document_get_n_pages;
656 iface->get_page_size = pdf_document_get_page_size;
657 iface->get_page_label = pdf_document_get_page_label;
658 iface->has_attachments = pdf_document_has_attachments;
659 iface->get_attachments = pdf_document_get_attachments;
660 iface->render_pixbuf = pdf_document_render_pixbuf;
661 iface->get_text = pdf_document_get_text;
662 iface->can_get_text = pdf_document_can_get_text;
663 iface->get_info = pdf_document_get_info;
667 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
669 iface->has_document_security = pdf_document_has_document_security;
670 iface->set_password = pdf_document_set_password;
674 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
676 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
679 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
681 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
685 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
688 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
691 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
693 if (pdf_document->font_info == NULL) {
694 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
697 if (pdf_document->fonts_iter) {
698 poppler_fonts_iter_free (pdf_document->fonts_iter);
701 pdf_document->fonts_scanned_pages += n_pages;
703 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
704 &pdf_document->fonts_iter);
706 pdf_document->fonts_scanned_pages = 0;
707 poppler_font_info_free (pdf_document->font_info);
708 pdf_document->font_info = NULL;
715 font_type_to_string (PopplerFontType type)
718 case POPPLER_FONT_TYPE_TYPE1:
720 case POPPLER_FONT_TYPE_TYPE1C:
722 case POPPLER_FONT_TYPE_TYPE3:
724 case POPPLER_FONT_TYPE_TRUETYPE:
725 return _("TrueType");
726 case POPPLER_FONT_TYPE_CID_TYPE0:
727 return _("Type 1 (CID)");
728 case POPPLER_FONT_TYPE_CID_TYPE0C:
729 return _("Type 1C (CID)");
730 case POPPLER_FONT_TYPE_CID_TYPE2:
731 return _("TrueType (CID)");
733 return _("Unknown font type");
738 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
741 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
742 PopplerFontsIter *iter = pdf_document->fonts_iter;
744 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
750 GtkTreeIter list_iter;
753 const char *embedded;
756 name = poppler_fonts_iter_get_name (iter);
762 type = font_type_to_string (
763 poppler_fonts_iter_get_font_type (iter));
765 if (poppler_fonts_iter_is_embedded (iter)) {
766 if (poppler_fonts_iter_is_subset (iter))
767 embedded = _("Embedded subset");
769 embedded = _("Embedded");
771 embedded = _("Not embedded");
774 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
776 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
777 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
778 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
779 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
783 } while (poppler_fonts_iter_next (iter));
787 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
789 iface->fill_model = pdf_document_fonts_fill_model;
790 iface->scan = pdf_document_fonts_scan;
791 iface->get_progress = pdf_document_fonts_get_progress;
795 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
797 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
798 PopplerIndexIter *iter;
800 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
802 iter = poppler_index_iter_new (pdf_document->document);
805 poppler_index_iter_free (iter);
811 ev_link_dest_from_dest (PdfDocument *pdf_document,
814 EvLinkDest *ev_dest = NULL;
815 const char *unimplemented_dest = NULL;
817 g_assert (dest != NULL);
819 switch (dest->type) {
820 case POPPLER_DEST_XYZ: {
821 PopplerPage *poppler_page;
824 poppler_page = poppler_document_get_page (pdf_document->document,
825 MAX (0, dest->page_num - 1));
826 poppler_page_get_size (poppler_page, NULL, &height);
827 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
831 g_object_unref (poppler_page);
834 case POPPLER_DEST_FIT:
835 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
837 case POPPLER_DEST_FITH: {
838 PopplerPage *poppler_page;
841 poppler_page = poppler_document_get_page (pdf_document->document,
842 MAX (0, dest->page_num - 1));
843 poppler_page_get_size (poppler_page, NULL, &height);
844 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
846 g_object_unref (poppler_page);
849 case POPPLER_DEST_FITV:
850 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
853 case POPPLER_DEST_FITR: {
854 PopplerPage *poppler_page;
857 poppler_page = poppler_document_get_page (pdf_document->document,
858 MAX (0, dest->page_num - 1));
859 poppler_page_get_size (poppler_page, NULL, &height);
860 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
862 height - dest->bottom,
865 g_object_unref (poppler_page);
868 case POPPLER_DEST_FITB:
869 unimplemented_dest = "POPPLER_DEST_FITB";
871 case POPPLER_DEST_FITBH:
872 unimplemented_dest = "POPPLER_DEST_FITBH";
874 case POPPLER_DEST_FITBV:
875 unimplemented_dest = "POPPLER_DEST_FITBV";
877 case POPPLER_DEST_NAMED:
878 ev_dest = ev_link_dest_new_named (dest->named_dest);
880 case POPPLER_DEST_UNKNOWN:
881 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
885 if (unimplemented_dest) {
886 g_warning ("Unimplemented named action: %s, please post a "
887 "bug report in Evince bugzilla "
888 "(http://bugzilla.gnome.org) with a testcase.",
893 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
899 ev_link_from_action (PdfDocument *pdf_document,
900 PopplerAction *action)
903 EvLinkAction *ev_action = NULL;
904 const char *unimplemented_action = NULL;
906 switch (action->type) {
907 case POPPLER_ACTION_GOTO_DEST: {
910 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
911 ev_action = ev_link_action_new_dest (dest);
914 case POPPLER_ACTION_GOTO_REMOTE: {
917 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
918 ev_action = ev_link_action_new_remote (dest,
919 action->goto_remote.file_name);
923 case POPPLER_ACTION_LAUNCH:
924 ev_action = ev_link_action_new_launch (action->launch.file_name,
925 action->launch.params);
927 case POPPLER_ACTION_URI:
928 ev_action = ev_link_action_new_external_uri (action->uri.uri);
930 case POPPLER_ACTION_NAMED:
931 ev_action = ev_link_action_new_named (action->named.named_dest);
933 case POPPLER_ACTION_MOVIE:
934 unimplemented_action = "POPPLER_ACTION_MOVIE";
936 case POPPLER_ACTION_UNKNOWN:
937 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
940 if (unimplemented_action) {
941 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
942 unimplemented_action);
945 link = ev_link_new (action->any.title, ev_action);
951 build_tree (PdfDocument *pdf_document,
954 PopplerIndexIter *iter)
958 GtkTreeIter tree_iter;
959 PopplerIndexIter *child;
960 PopplerAction *action;
965 action = poppler_index_iter_get_action (iter);
966 expand = poppler_index_iter_is_open (iter);
971 switch (action->type) {
972 case POPPLER_ACTION_GOTO_DEST: {
973 /* For bookmarks, solve named destinations */
974 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
976 EvLinkDest *ev_dest = NULL;
977 EvLinkAction *ev_action;
979 dest = poppler_document_find_dest (pdf_document->document,
980 action->goto_dest.dest->named_dest);
982 link = ev_link_from_action (pdf_document, action);
986 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
987 poppler_dest_free (dest);
989 ev_action = ev_link_action_new_dest (ev_dest);
990 link = ev_link_new (action->any.title, ev_action);
992 link = ev_link_from_action (pdf_document, action);
997 link = ev_link_from_action (pdf_document, action);
1002 poppler_action_free (action);
1006 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1007 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1009 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1010 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1011 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1012 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1015 g_free (title_markup);
1016 g_object_unref (link);
1018 child = poppler_index_iter_get_child (iter);
1020 build_tree (pdf_document, model, &tree_iter, child);
1021 poppler_index_iter_free (child);
1022 poppler_action_free (action);
1024 } while (poppler_index_iter_next (iter));
1027 static GtkTreeModel *
1028 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1030 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1031 GtkTreeModel *model = NULL;
1032 PopplerIndexIter *iter;
1034 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1036 iter = poppler_index_iter_new (pdf_document->document);
1037 /* Create the model if we have items*/
1039 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1043 build_tree (pdf_document, model, NULL, iter);
1044 poppler_index_iter_free (iter);
1051 pdf_document_links_get_links (EvDocumentLinks *document_links,
1054 PdfDocument *pdf_document;
1055 PopplerPage *poppler_page;
1056 GList *retval = NULL;
1057 GList *mapping_list;
1061 pdf_document = PDF_DOCUMENT (document_links);
1062 poppler_page = poppler_document_get_page (pdf_document->document,
1064 mapping_list = poppler_page_get_link_mapping (poppler_page);
1065 poppler_page_get_size (poppler_page, NULL, &height);
1067 for (list = mapping_list; list; list = list->next) {
1068 PopplerLinkMapping *link_mapping;
1069 EvLinkMapping *ev_link_mapping;
1071 link_mapping = (PopplerLinkMapping *)list->data;
1072 ev_link_mapping = g_new (EvLinkMapping, 1);
1073 ev_link_mapping->link = ev_link_from_action (pdf_document,
1074 link_mapping->action);
1075 ev_link_mapping->x1 = link_mapping->area.x1;
1076 ev_link_mapping->x2 = link_mapping->area.x2;
1077 /* Invert this for X-style coordinates */
1078 ev_link_mapping->y1 = height - link_mapping->area.y2;
1079 ev_link_mapping->y2 = height - link_mapping->area.y1;
1081 retval = g_list_prepend (retval, ev_link_mapping);
1084 poppler_page_free_link_mapping (mapping_list);
1085 g_object_unref (poppler_page);
1087 return g_list_reverse (retval);
1091 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1092 const gchar *link_name)
1094 PdfDocument *pdf_document;
1096 EvLinkDest *ev_dest = NULL;
1098 pdf_document = PDF_DOCUMENT (document_links);
1099 dest = poppler_document_find_dest (pdf_document->document,
1102 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1103 poppler_dest_free (dest);
1110 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1112 iface->has_document_links = pdf_document_links_has_document_links;
1113 iface->get_links_model = pdf_document_links_get_links_model;
1114 iface->get_links = pdf_document_links_get_links;
1115 iface->find_link_dest = pdf_document_links_find_link_dest;
1119 make_thumbnail_for_size (PdfDocument *pdf_document,
1124 PopplerPage *poppler_page;
1128 gdouble unscaled_width, unscaled_height;
1130 poppler_page = poppler_document_get_page (pdf_document->document, page);
1131 g_return_val_if_fail (poppler_page != NULL, NULL);
1133 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page,
1134 size, &width, &height);
1135 poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
1136 scale = width / unscaled_width;
1139 if (rotation == 90 || rotation == 270) {
1146 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1148 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1150 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1152 scale, rotation, pixbuf);
1155 g_object_unref (poppler_page);
1161 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1167 PdfDocument *pdf_document;
1168 PopplerPage *poppler_page;
1170 GdkPixbuf *border_pixbuf;
1172 pdf_document = PDF_DOCUMENT (document_thumbnails);
1174 poppler_page = poppler_document_get_page (pdf_document->document, page);
1175 g_return_val_if_fail (poppler_page != NULL, NULL);
1177 pixbuf = poppler_page_get_thumbnail (poppler_page);
1179 if (pixbuf == NULL) {
1180 /* There is no provided thumbnail. We need to make one. */
1181 pixbuf = make_thumbnail_for_size (pdf_document, page, rotation, size);
1185 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, rotation, pixbuf);
1186 g_object_unref (pixbuf);
1187 pixbuf = border_pixbuf;
1190 g_object_unref (poppler_page);
1196 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1202 PdfDocument *pdf_document;
1203 PopplerPage *poppler_page;
1206 pdf_document = PDF_DOCUMENT (document_thumbnails);
1207 poppler_page = poppler_document_get_page (pdf_document->document, page);
1209 g_return_if_fail (width != NULL);
1210 g_return_if_fail (height != NULL);
1211 g_return_if_fail (poppler_page != NULL);
1213 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1216 double page_width, page_height;
1218 poppler_page_get_size (poppler_page, &page_width, &page_height);
1220 *height = (int) (size * page_height / page_width);
1222 g_object_unref (poppler_page);
1226 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1228 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1229 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1234 pdf_document_search_idle_callback (void *data)
1236 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1237 PdfDocument *pdf_document = search->document;
1242 page = poppler_document_get_page (search->document->document,
1243 search->search_page);
1245 ev_document_doc_mutex_lock ();
1246 matches = poppler_page_find_text (page, search->text);
1247 ev_document_doc_mutex_unlock ();
1249 g_object_unref (page);
1251 search->pages[search->search_page] = matches;
1252 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1253 search->search_page);
1255 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1256 search->search_page += 1;
1257 if (search->search_page == n_pages) {
1259 search->search_page = 0;
1262 if (search->search_page != search->start_page) {
1267 search->idle = 0; /* will return FALSE to remove */
1272 static PdfDocumentSearch *
1273 pdf_document_search_new (PdfDocument *pdf_document,
1277 PdfDocumentSearch *search;
1281 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1283 search = g_new0 (PdfDocumentSearch, 1);
1285 search->text = g_strdup (text);
1286 search->pages = g_new0 (GList *, n_pages);
1287 search->document = pdf_document;
1289 /* We add at low priority so the progress bar repaints */
1290 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1291 pdf_document_search_idle_callback,
1295 search->start_page = start_page;
1296 search->search_page = start_page;
1302 pdf_document_find_begin (EvDocumentFind *document,
1304 const char *search_string,
1305 gboolean case_sensitive)
1307 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1309 /* FIXME handle case_sensitive (right now XPDF
1310 * code is always case insensitive for ASCII
1311 * and case sensitive for all other languaages)
1314 if (pdf_document->search &&
1315 strcmp (search_string, pdf_document->search->text) == 0)
1318 if (pdf_document->search)
1319 pdf_document_search_free (pdf_document->search);
1321 pdf_document->search = pdf_document_search_new (pdf_document,
1327 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1329 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1332 return g_list_length (search->pages[page]);
1339 pdf_document_find_get_result (EvDocumentFind *document_find,
1342 EvRectangle *rectangle)
1344 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1345 PdfDocumentSearch *search = pdf_document->search;
1346 PopplerPage *poppler_page;
1347 PopplerRectangle *r;
1353 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1358 poppler_page = poppler_document_get_page (pdf_document->document, page);
1359 poppler_page_get_size (poppler_page, NULL, &height);
1360 rectangle->x1 = r->x1;
1361 rectangle->y1 = height - r->y2;
1362 rectangle->x2 = r->x2;
1363 rectangle->y2 = height - r->y1;
1364 g_object_unref (poppler_page);
1370 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1373 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1375 return search && search->pages[page] != NULL;
1379 pdf_document_find_get_progress (EvDocumentFind *document_find)
1381 PdfDocumentSearch *search;
1382 int n_pages, pages_done;
1384 search = PDF_DOCUMENT (document_find)->search;
1386 if (search == NULL) {
1390 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1391 if (search->search_page > search->start_page) {
1392 pages_done = search->search_page - search->start_page + 1;
1393 } else if (search->search_page == search->start_page) {
1394 pages_done = n_pages;
1396 pages_done = n_pages - search->start_page + search->search_page;
1399 return pages_done / (double) n_pages;
1403 pdf_document_find_cancel (EvDocumentFind *document)
1405 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1407 if (pdf_document->search) {
1408 pdf_document_search_free (pdf_document->search);
1409 pdf_document->search = NULL;
1414 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1416 iface->begin = pdf_document_find_begin;
1417 iface->get_n_results = pdf_document_find_get_n_results;
1418 iface->get_result = pdf_document_find_get_result;
1419 iface->page_has_results = pdf_document_find_page_has_results;
1420 iface->get_progress = pdf_document_find_get_progress;
1421 iface->cancel = pdf_document_find_cancel;
1424 static const gboolean supported_formats[] = {
1425 TRUE, /* EV_FILE_FORMAT_PS */
1426 #ifdef HAVE_POPPLER_PAGE_RENDER
1427 TRUE, /* EV_FILE_FORMAT_PDF */
1429 FALSE, /* EV_FILE_FORMAT_PDF */
1434 pdf_print_context_free (PdfPrintContext *ctx)
1440 poppler_ps_file_free (ctx->ps_file);
1441 ctx->ps_file = NULL;
1444 if (ctx->pdf_cairo) {
1445 cairo_destroy (ctx->pdf_cairo);
1446 ctx->pdf_cairo = NULL;
1453 pdf_document_file_exporter_format_supported (EvFileExporter *exporter,
1454 EvFileExporterFormat format)
1456 return supported_formats[format];
1460 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1461 EvFileExporterFormat format,
1462 const char *filename,
1469 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1470 PdfPrintContext *ctx;
1472 if (pdf_document->print_ctx)
1473 pdf_print_context_free (pdf_document->print_ctx);
1474 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1475 ctx = pdf_document->print_ctx;
1476 ctx->format = format;
1479 case EV_FILE_FORMAT_PS:
1480 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1481 filename, first_page,
1482 last_page - first_page + 1);
1483 poppler_ps_file_set_paper_size (ctx->ps_file, width, height);
1484 poppler_ps_file_set_duplex (ctx->ps_file, duplex);
1487 case EV_FILE_FORMAT_PDF: {
1488 cairo_surface_t *surface;
1490 surface = cairo_pdf_surface_create (filename, width, height);
1491 ctx->pdf_cairo = cairo_create (surface);
1492 cairo_surface_destroy (surface);
1496 g_assert_not_reached ();
1501 pdf_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1503 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1504 PdfPrintContext *ctx = pdf_document->print_ctx;
1505 PopplerPage *poppler_page;
1507 g_return_if_fail (pdf_document->print_ctx != NULL);
1509 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1511 switch (ctx->format) {
1512 case EV_FILE_FORMAT_PS:
1513 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1515 case EV_FILE_FORMAT_PDF:
1516 #ifdef HAVE_POPPLER_PAGE_RENDER
1517 poppler_page_render (poppler_page, ctx->pdf_cairo);
1519 cairo_show_page (ctx->pdf_cairo);
1522 g_assert_not_reached ();
1525 g_object_unref (poppler_page);
1529 pdf_document_file_exporter_end (EvFileExporter *exporter)
1531 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1533 pdf_print_context_free (pdf_document->print_ctx);
1534 pdf_document->print_ctx = NULL;
1538 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1540 iface->format_supported = pdf_document_file_exporter_format_supported;
1541 iface->begin = pdf_document_file_exporter_begin;
1542 iface->do_page = pdf_document_file_exporter_do_page;
1543 iface->end = pdf_document_file_exporter_end;
1547 pdf_selection_render_selection (EvSelection *selection,
1548 EvRenderContext *rc,
1550 EvRectangle *points,
1551 EvRectangle *old_points,
1555 PdfDocument *pdf_document;
1556 double width_points, height_points;
1559 pdf_document = PDF_DOCUMENT (selection);
1560 set_rc_data (pdf_document, rc);
1562 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
1563 width = (int) ((width_points * rc->scale) + 0.5);
1564 height = (int) ((height_points * rc->scale) + 0.5);
1566 if (*pixbuf == NULL) {
1567 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1572 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1573 rc->scale, rc->rotation, *pixbuf,
1574 (PopplerRectangle *)points,
1575 (PopplerRectangle *)old_points,
1582 pdf_selection_get_selection_region (EvSelection *selection,
1583 EvRenderContext *rc,
1584 EvRectangle *points)
1586 PdfDocument *pdf_document;
1589 pdf_document = PDF_DOCUMENT (selection);
1591 set_rc_data (pdf_document, rc);
1593 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data, rc->scale, (PopplerRectangle *) points);
1599 pdf_selection_get_selection_map (EvSelection *selection,
1600 EvRenderContext *rc)
1602 PdfDocument *pdf_document;
1603 PopplerPage *poppler_page;
1604 PopplerRectangle points;
1607 pdf_document = PDF_DOCUMENT (selection);
1608 poppler_page = poppler_document_get_page (pdf_document->document,
1613 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1614 retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1615 g_object_unref (poppler_page);
1621 pdf_selection_iface_init (EvSelectionIface *iface)
1623 iface->render_selection = pdf_selection_render_selection;
1624 iface->get_selection_region = pdf_selection_get_selection_region;
1625 iface->get_selection_map = pdf_selection_get_selection_map;
1629 pdf_document_new (void)
1631 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));