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>
34 #include <glib/gi18n.h>
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-document-find.h"
39 #include "ev-document-misc.h"
40 #include "ev-document-links.h"
41 #include "ev-document-images.h"
42 #include "ev-document-fonts.h"
43 #include "ev-document-security.h"
44 #include "ev-document-thumbnails.h"
45 #include "ev-document-transition.h"
46 #include "ev-document-forms.h"
47 #include "ev-selection.h"
48 #include "ev-transition-effect.h"
49 #include "ev-attachment.h"
52 #if (defined (HAVE_POPPLER_PAGE_RENDER) || defined (HAVE_POPPLER_PAGE_RENDER_FOR_PRINTING)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
53 #define HAVE_CAIRO_PRINT
57 PdfDocument *document;
66 EvFileExporterFormat format;
76 #ifdef HAVE_CAIRO_PRINT
79 PopplerPSFile *ps_file;
83 struct _PdfDocumentClass
85 GObjectClass parent_class;
90 GObject parent_instance;
92 PopplerDocument *document;
95 PopplerFontInfo *font_info;
96 PopplerFontsIter *fonts_iter;
97 int fonts_scanned_pages;
99 PdfDocumentSearch *search;
100 PdfPrintContext *print_ctx;
103 static void pdf_document_document_iface_init (EvDocumentIface *iface);
104 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
105 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
106 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
107 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
108 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
109 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
110 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
111 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
112 static void pdf_selection_iface_init (EvSelectionIface *iface);
113 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
114 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
118 static int pdf_document_get_n_pages (EvDocument *document);
120 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
122 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
123 PopplerAction *action);
124 static void pdf_document_search_free (PdfDocumentSearch *search);
125 static void pdf_print_context_free (PdfPrintContext *ctx);
127 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
129 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
130 pdf_document_security_iface_init);
131 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
132 pdf_document_document_thumbnails_iface_init);
133 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
134 pdf_document_document_links_iface_init);
135 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
136 pdf_document_document_images_iface_init);
137 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
138 pdf_document_document_forms_iface_init);
139 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
140 pdf_document_document_fonts_iface_init);
141 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
142 pdf_document_find_iface_init);
143 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
144 pdf_document_file_exporter_iface_init);
145 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
146 pdf_selection_iface_init);
147 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
148 pdf_document_page_transition_iface_init);
152 set_rc_data (PdfDocument *pdf_document,
155 if (rc->data == NULL) {
156 rc->data = poppler_document_get_page (pdf_document->document,
158 rc->destroy = g_object_unref;
160 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
165 pdf_document_search_free (PdfDocumentSearch *search)
167 PdfDocument *pdf_document = search->document;
171 if (search->idle != 0)
172 g_source_remove (search->idle);
174 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
175 for (i = 0; i < n_pages; i++) {
176 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
177 g_list_free (search->pages[i]);
179 g_free (search->pages);
181 g_free (search->text);
186 pdf_document_dispose (GObject *object)
188 PdfDocument *pdf_document = PDF_DOCUMENT(object);
190 if (pdf_document->print_ctx) {
191 pdf_print_context_free (pdf_document->print_ctx);
192 pdf_document->print_ctx = NULL;
195 if (pdf_document->search) {
196 pdf_document_search_free (pdf_document->search);
197 pdf_document->search = NULL;
200 if (pdf_document->document) {
201 g_object_unref (pdf_document->document);
204 if (pdf_document->font_info) {
205 poppler_font_info_free (pdf_document->font_info);
208 if (pdf_document->fonts_iter) {
209 poppler_fonts_iter_free (pdf_document->fonts_iter);
212 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
216 pdf_document_class_init (PdfDocumentClass *klass)
218 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
220 g_object_class->dispose = pdf_document_dispose;
224 pdf_document_init (PdfDocument *pdf_document)
226 pdf_document->password = NULL;
230 convert_error (GError *poppler_error,
233 if (poppler_error == NULL)
236 if (poppler_error->domain == POPPLER_ERROR) {
237 /* convert poppler errors into EvDocument errors */
238 gint code = EV_DOCUMENT_ERROR_INVALID;
239 if (poppler_error->code == POPPLER_ERROR_INVALID)
240 code = EV_DOCUMENT_ERROR_INVALID;
241 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
242 code = EV_DOCUMENT_ERROR_ENCRYPTED;
248 poppler_error->message,
251 g_propagate_error (error, poppler_error);
258 pdf_document_save (EvDocument *document,
263 GError *poppler_error = NULL;
265 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
269 convert_error (poppler_error, error);
275 pdf_document_load (EvDocument *document,
279 GError *poppler_error = NULL;
280 PdfDocument *pdf_document = PDF_DOCUMENT (document);
282 pdf_document->document =
283 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
285 if (pdf_document->document == NULL) {
286 convert_error (poppler_error, error);
294 pdf_document_get_n_pages (EvDocument *document)
296 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
300 pdf_document_get_page_size (EvDocument *document,
305 PdfDocument *pdf_document = PDF_DOCUMENT (document);
306 PopplerPage *poppler_page;
308 poppler_page = poppler_document_get_page (pdf_document->document, page);
309 poppler_page_get_size (poppler_page, width, height);
310 g_object_unref (poppler_page);
314 pdf_document_get_page_label (EvDocument *document,
317 PopplerPage *poppler_page;
320 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
323 g_object_get (G_OBJECT (poppler_page),
326 g_object_unref (poppler_page);
332 pdf_document_has_attachments (EvDocument *document)
334 PdfDocument *pdf_document;
336 pdf_document = PDF_DOCUMENT (document);
338 return poppler_document_has_attachments (pdf_document->document);
341 struct SaveToBufferData {
347 attachment_save_to_buffer_callback (const gchar *buf,
352 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
356 if (sdata->len + count > sdata->max) {
357 new_max = MAX (sdata->max * 2, sdata->len + count);
358 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
360 sdata->buffer = new_buffer;
361 sdata->max = new_max;
364 memcpy (sdata->buffer + sdata->len, buf, count);
371 attachment_save_to_buffer (PopplerAttachment *attachment,
376 static const gint initial_max = 1024;
377 struct SaveToBufferData sdata;
382 sdata.buffer = (gchar *) g_malloc (initial_max);
383 sdata.max = initial_max;
386 if (! poppler_attachment_save_to_callback (attachment,
387 attachment_save_to_buffer_callback,
390 g_free (sdata.buffer);
394 *buffer = sdata.buffer;
395 *buffer_size = sdata.len;
401 pdf_document_get_attachments (EvDocument *document)
403 PdfDocument *pdf_document;
406 GList *retval = NULL;
408 pdf_document = PDF_DOCUMENT (document);
410 if (!pdf_document_has_attachments (document))
413 attachments = poppler_document_get_attachments (pdf_document->document);
415 for (list = attachments; list; list = list->next) {
416 PopplerAttachment *attachment;
417 EvAttachment *ev_attachment;
420 GError *error = NULL;
422 attachment = (PopplerAttachment *) list->data;
424 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
425 ev_attachment = ev_attachment_new (attachment->name,
426 attachment->description,
431 retval = g_list_prepend (retval, ev_attachment);
434 g_warning ("%s", error->message);
435 g_error_free (error);
441 g_object_unref (attachment);
444 return g_list_reverse (retval);
447 static cairo_surface_t *
448 pdf_document_render (EvDocument *document,
451 PdfDocument *pdf_document;
452 cairo_surface_t *surface;
453 double width_points, height_points;
456 pdf_document = PDF_DOCUMENT (document);
458 set_rc_data (pdf_document, rc);
460 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
462 if (rc->rotation == 90 || rc->rotation == 270) {
463 width = (int) ((height_points * rc->scale) + 0.5);
464 height = (int) ((width_points * rc->scale) + 0.5);
466 width = (int) ((width_points * rc->scale) + 0.5);
467 height = (int) ((height_points * rc->scale) + 0.5);
470 #ifdef HAVE_POPPLER_PAGE_RENDER
473 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
475 memset (cairo_image_surface_get_data (surface), 0xff,
476 cairo_image_surface_get_height (surface) *
477 cairo_image_surface_get_stride (surface));
479 cr = cairo_create (surface);
480 switch (rc->rotation) {
482 cairo_translate (cr, width, 0);
485 cairo_translate (cr, width, height);
488 cairo_translate (cr, 0, height);
491 cairo_translate (cr, 0, 0);
493 cairo_scale (cr, rc->scale, rc->scale);
494 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
495 poppler_page_render (POPPLER_PAGE (rc->data), cr);
497 #else /* HAVE_POPPLER_PAGE_RENDER */
500 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
504 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
510 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
511 g_object_unref (pixbuf);
512 #endif /* HAVE_POPPLER_PAGE_RENDER */
517 /* EvDocumentSecurity */
520 pdf_document_has_document_security (EvDocumentSecurity *document_security)
522 /* FIXME: do we really need to have this? */
527 pdf_document_set_password (EvDocumentSecurity *document_security,
528 const char *password)
530 PdfDocument *document = PDF_DOCUMENT (document_security);
532 if (document->password)
533 g_free (document->password);
535 document->password = g_strdup (password);
538 static EvDocumentInfo *
539 pdf_document_get_info (EvDocument *document)
541 EvDocumentInfo *info;
542 PopplerPageLayout layout;
543 PopplerPageMode mode;
544 PopplerViewerPreferences view_prefs;
545 PopplerPermissions permissions;
547 info = g_new0 (EvDocumentInfo, 1);
549 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
550 EV_DOCUMENT_INFO_FORMAT |
551 EV_DOCUMENT_INFO_AUTHOR |
552 EV_DOCUMENT_INFO_SUBJECT |
553 EV_DOCUMENT_INFO_KEYWORDS |
554 EV_DOCUMENT_INFO_LAYOUT |
555 EV_DOCUMENT_INFO_START_MODE |
556 EV_DOCUMENT_INFO_PERMISSIONS |
557 EV_DOCUMENT_INFO_UI_HINTS |
558 EV_DOCUMENT_INFO_CREATOR |
559 EV_DOCUMENT_INFO_PRODUCER |
560 EV_DOCUMENT_INFO_CREATION_DATE |
561 EV_DOCUMENT_INFO_MOD_DATE |
562 EV_DOCUMENT_INFO_LINEARIZED |
563 EV_DOCUMENT_INFO_N_PAGES |
564 EV_DOCUMENT_INFO_SECURITY |
565 EV_DOCUMENT_INFO_PAPER_SIZE;
567 g_object_get (PDF_DOCUMENT (document)->document,
568 "title", &(info->title),
569 "format", &(info->format),
570 "author", &(info->author),
571 "subject", &(info->subject),
572 "keywords", &(info->keywords),
574 "page-layout", &layout,
575 "viewer-preferences", &view_prefs,
576 "permissions", &permissions,
577 "creator", &(info->creator),
578 "producer", &(info->producer),
579 "creation-date", &(info->creation_date),
580 "mod-date", &(info->modified_date),
581 "linearized", &(info->linearized),
584 pdf_document_get_page_size(document, 0,
585 &(info->paper_width),
586 &(info->paper_height));
589 info->paper_width = info->paper_width / 72.0f * 25.4f;
590 info->paper_height = info->paper_height / 72.0f * 25.4f;
593 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
594 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
596 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
597 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
599 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
600 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
602 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
603 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
604 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
605 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
607 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
608 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
615 case POPPLER_PAGE_MODE_NONE:
616 info->mode = EV_DOCUMENT_MODE_NONE;
618 case POPPLER_PAGE_MODE_USE_THUMBS:
619 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
621 case POPPLER_PAGE_MODE_USE_OC:
622 info->mode = EV_DOCUMENT_MODE_USE_OC;
624 case POPPLER_PAGE_MODE_FULL_SCREEN:
625 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
627 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
628 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
634 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
635 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
637 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
638 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
640 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
641 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
643 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
644 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
646 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
647 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
649 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
650 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
652 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
653 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
656 info->permissions = 0;
657 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
658 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
660 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
661 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
663 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
664 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
666 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
667 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
670 info->n_pages = ev_document_get_n_pages (document);
672 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
673 /* translators: this is the document security state */
674 info->security = g_strdup (_("Yes"));
676 /* translators: this is the document security state */
677 info->security = g_strdup (_("No"));
684 pdf_document_document_iface_init (EvDocumentIface *iface)
686 iface->save = pdf_document_save;
687 iface->load = pdf_document_load;
688 iface->get_n_pages = pdf_document_get_n_pages;
689 iface->get_page_size = pdf_document_get_page_size;
690 iface->get_page_label = pdf_document_get_page_label;
691 iface->has_attachments = pdf_document_has_attachments;
692 iface->get_attachments = pdf_document_get_attachments;
693 iface->render = pdf_document_render;
694 iface->get_info = pdf_document_get_info;
698 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
700 iface->has_document_security = pdf_document_has_document_security;
701 iface->set_password = pdf_document_set_password;
705 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
707 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
710 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
712 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
716 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
719 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
722 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
724 if (pdf_document->font_info == NULL) {
725 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
728 if (pdf_document->fonts_iter) {
729 poppler_fonts_iter_free (pdf_document->fonts_iter);
732 pdf_document->fonts_scanned_pages += n_pages;
734 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
735 &pdf_document->fonts_iter);
737 pdf_document->fonts_scanned_pages = 0;
738 poppler_font_info_free (pdf_document->font_info);
739 pdf_document->font_info = NULL;
746 font_type_to_string (PopplerFontType type)
749 case POPPLER_FONT_TYPE_TYPE1:
751 case POPPLER_FONT_TYPE_TYPE1C:
753 case POPPLER_FONT_TYPE_TYPE3:
755 case POPPLER_FONT_TYPE_TRUETYPE:
756 return _("TrueType");
757 case POPPLER_FONT_TYPE_CID_TYPE0:
758 return _("Type 1 (CID)");
759 case POPPLER_FONT_TYPE_CID_TYPE0C:
760 return _("Type 1C (CID)");
761 case POPPLER_FONT_TYPE_CID_TYPE2:
762 return _("TrueType (CID)");
764 return _("Unknown font type");
769 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
772 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
773 PopplerFontsIter *iter = pdf_document->fonts_iter;
775 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
781 GtkTreeIter list_iter;
784 const char *embedded;
787 name = poppler_fonts_iter_get_name (iter);
793 type = font_type_to_string (
794 poppler_fonts_iter_get_font_type (iter));
796 if (poppler_fonts_iter_is_embedded (iter)) {
797 if (poppler_fonts_iter_is_subset (iter))
798 embedded = _("Embedded subset");
800 embedded = _("Embedded");
802 embedded = _("Not embedded");
805 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
807 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
808 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
809 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
810 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
814 } while (poppler_fonts_iter_next (iter));
818 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
820 iface->fill_model = pdf_document_fonts_fill_model;
821 iface->scan = pdf_document_fonts_scan;
822 iface->get_progress = pdf_document_fonts_get_progress;
826 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
828 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
829 PopplerIndexIter *iter;
831 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
833 iter = poppler_index_iter_new (pdf_document->document);
836 poppler_index_iter_free (iter);
842 ev_link_dest_from_dest (PdfDocument *pdf_document,
845 EvLinkDest *ev_dest = NULL;
846 const char *unimplemented_dest = NULL;
848 g_assert (dest != NULL);
850 switch (dest->type) {
851 case POPPLER_DEST_XYZ: {
852 PopplerPage *poppler_page;
855 poppler_page = poppler_document_get_page (pdf_document->document,
856 MAX (0, dest->page_num - 1));
857 poppler_page_get_size (poppler_page, NULL, &height);
858 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
865 g_object_unref (poppler_page);
868 case POPPLER_DEST_FIT:
869 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
871 case POPPLER_DEST_FITH: {
872 PopplerPage *poppler_page;
875 poppler_page = poppler_document_get_page (pdf_document->document,
876 MAX (0, dest->page_num - 1));
877 poppler_page_get_size (poppler_page, NULL, &height);
878 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
881 g_object_unref (poppler_page);
884 case POPPLER_DEST_FITV:
885 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
889 case POPPLER_DEST_FITR: {
890 PopplerPage *poppler_page;
893 poppler_page = poppler_document_get_page (pdf_document->document,
894 MAX (0, dest->page_num - 1));
895 poppler_page_get_size (poppler_page, NULL, &height);
896 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
898 height - dest->bottom,
901 g_object_unref (poppler_page);
904 case POPPLER_DEST_FITB:
905 unimplemented_dest = "POPPLER_DEST_FITB";
907 case POPPLER_DEST_FITBH:
908 unimplemented_dest = "POPPLER_DEST_FITBH";
910 case POPPLER_DEST_FITBV:
911 unimplemented_dest = "POPPLER_DEST_FITBV";
913 case POPPLER_DEST_NAMED:
914 ev_dest = ev_link_dest_new_named (dest->named_dest);
916 case POPPLER_DEST_UNKNOWN:
917 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
921 if (unimplemented_dest) {
922 g_warning ("Unimplemented named action: %s, please post a "
923 "bug report in Evince bugzilla "
924 "(http://bugzilla.gnome.org) with a testcase.",
929 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
935 ev_link_from_action (PdfDocument *pdf_document,
936 PopplerAction *action)
939 EvLinkAction *ev_action = NULL;
940 const char *unimplemented_action = NULL;
942 switch (action->type) {
943 case POPPLER_ACTION_GOTO_DEST: {
946 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
947 ev_action = ev_link_action_new_dest (dest);
950 case POPPLER_ACTION_GOTO_REMOTE: {
953 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
954 ev_action = ev_link_action_new_remote (dest,
955 action->goto_remote.file_name);
959 case POPPLER_ACTION_LAUNCH:
960 ev_action = ev_link_action_new_launch (action->launch.file_name,
961 action->launch.params);
963 case POPPLER_ACTION_URI:
964 ev_action = ev_link_action_new_external_uri (action->uri.uri);
966 case POPPLER_ACTION_NAMED:
967 ev_action = ev_link_action_new_named (action->named.named_dest);
969 case POPPLER_ACTION_MOVIE:
970 unimplemented_action = "POPPLER_ACTION_MOVIE";
972 case POPPLER_ACTION_UNKNOWN:
973 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
976 if (unimplemented_action) {
977 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
978 unimplemented_action);
981 link = ev_link_new (action->any.title, ev_action);
987 build_tree (PdfDocument *pdf_document,
990 PopplerIndexIter *iter)
994 GtkTreeIter tree_iter;
995 PopplerIndexIter *child;
996 PopplerAction *action;
1001 action = poppler_index_iter_get_action (iter);
1002 expand = poppler_index_iter_is_open (iter);
1007 switch (action->type) {
1008 case POPPLER_ACTION_GOTO_DEST: {
1009 /* For bookmarks, solve named destinations */
1010 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1012 EvLinkDest *ev_dest = NULL;
1013 EvLinkAction *ev_action;
1015 dest = poppler_document_find_dest (pdf_document->document,
1016 action->goto_dest.dest->named_dest);
1018 link = ev_link_from_action (pdf_document, action);
1022 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1023 poppler_dest_free (dest);
1025 ev_action = ev_link_action_new_dest (ev_dest);
1026 link = ev_link_new (action->any.title, ev_action);
1028 link = ev_link_from_action (pdf_document, action);
1033 link = ev_link_from_action (pdf_document, action);
1037 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1038 poppler_action_free (action);
1040 g_object_unref (link);
1045 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1046 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1048 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1049 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1050 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1051 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1054 g_free (title_markup);
1055 g_object_unref (link);
1057 child = poppler_index_iter_get_child (iter);
1059 build_tree (pdf_document, model, &tree_iter, child);
1060 poppler_index_iter_free (child);
1061 poppler_action_free (action);
1063 } while (poppler_index_iter_next (iter));
1066 static GtkTreeModel *
1067 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1069 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1070 GtkTreeModel *model = NULL;
1071 PopplerIndexIter *iter;
1073 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1075 iter = poppler_index_iter_new (pdf_document->document);
1076 /* Create the model if we have items*/
1078 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1083 build_tree (pdf_document, model, NULL, iter);
1084 poppler_index_iter_free (iter);
1091 pdf_document_links_get_links (EvDocumentLinks *document_links,
1094 PdfDocument *pdf_document;
1095 PopplerPage *poppler_page;
1096 GList *retval = NULL;
1097 GList *mapping_list;
1101 pdf_document = PDF_DOCUMENT (document_links);
1102 poppler_page = poppler_document_get_page (pdf_document->document,
1104 mapping_list = poppler_page_get_link_mapping (poppler_page);
1105 poppler_page_get_size (poppler_page, NULL, &height);
1107 for (list = mapping_list; list; list = list->next) {
1108 PopplerLinkMapping *link_mapping;
1109 EvLinkMapping *ev_link_mapping;
1111 link_mapping = (PopplerLinkMapping *)list->data;
1112 ev_link_mapping = g_new (EvLinkMapping, 1);
1113 ev_link_mapping->link = ev_link_from_action (pdf_document,
1114 link_mapping->action);
1115 ev_link_mapping->x1 = link_mapping->area.x1;
1116 ev_link_mapping->x2 = link_mapping->area.x2;
1117 /* Invert this for X-style coordinates */
1118 ev_link_mapping->y1 = height - link_mapping->area.y2;
1119 ev_link_mapping->y2 = height - link_mapping->area.y1;
1121 retval = g_list_prepend (retval, ev_link_mapping);
1124 poppler_page_free_link_mapping (mapping_list);
1125 g_object_unref (poppler_page);
1127 return g_list_reverse (retval);
1131 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1132 const gchar *link_name)
1134 PdfDocument *pdf_document;
1136 EvLinkDest *ev_dest = NULL;
1138 pdf_document = PDF_DOCUMENT (document_links);
1139 dest = poppler_document_find_dest (pdf_document->document,
1142 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1143 poppler_dest_free (dest);
1150 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1152 iface->has_document_links = pdf_document_links_has_document_links;
1153 iface->get_links_model = pdf_document_links_get_links_model;
1154 iface->get_links = pdf_document_links_get_links;
1155 iface->find_link_dest = pdf_document_links_find_link_dest;
1159 pdf_document_images_get_images (EvDocumentImages *document_images,
1162 GList *retval = NULL;
1163 PdfDocument *pdf_document;
1164 PopplerPage *poppler_page;
1165 GList *mapping_list;
1168 pdf_document = PDF_DOCUMENT (document_images);
1169 poppler_page = poppler_document_get_page (pdf_document->document, page);
1170 mapping_list = poppler_page_get_image_mapping (poppler_page);
1172 for (list = mapping_list; list; list = list->next) {
1173 PopplerImageMapping *image_mapping;
1174 EvImageMapping *ev_image_mapping;
1176 image_mapping = (PopplerImageMapping *)list->data;
1178 ev_image_mapping = g_new (EvImageMapping, 1);
1180 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1181 ev_image_mapping->x1 = image_mapping->area.x1;
1182 ev_image_mapping->x2 = image_mapping->area.x2;
1183 ev_image_mapping->y1 = image_mapping->area.y1;
1184 ev_image_mapping->y2 = image_mapping->area.y2;
1186 retval = g_list_prepend (retval, ev_image_mapping);
1189 poppler_page_free_image_mapping (mapping_list);
1190 g_object_unref (poppler_page);
1196 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1198 iface->get_images = pdf_document_images_get_images;
1202 make_thumbnail_for_page (PdfDocument *pdf_document,
1203 PopplerPage *poppler_page,
1204 EvRenderContext *rc)
1209 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1210 rc, &width, &height);
1212 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1214 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1216 ev_document_fc_mutex_lock ();
1217 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1219 rc->scale, rc->rotation, pixbuf);
1220 ev_document_fc_mutex_unlock ();
1226 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1227 EvRenderContext *rc,
1230 PdfDocument *pdf_document;
1231 PopplerPage *poppler_page;
1233 GdkPixbuf *border_pixbuf;
1235 pdf_document = PDF_DOCUMENT (document_thumbnails);
1237 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1238 g_return_val_if_fail (poppler_page != NULL, NULL);
1240 pixbuf = poppler_page_get_thumbnail (poppler_page);
1242 /* Rotate provided thumbnail if needed */
1243 GdkPixbuf *rotated_pixbuf;
1245 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1246 (GdkPixbufRotation) (360 - rc->rotation));
1247 g_object_unref (pixbuf);
1248 pixbuf = rotated_pixbuf;
1250 /* There is no provided thumbnail. We need to make one. */
1251 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1255 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1256 g_object_unref (pixbuf);
1257 pixbuf = border_pixbuf;
1260 g_object_unref (poppler_page);
1266 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1267 EvRenderContext *rc,
1271 PdfDocument *pdf_document;
1272 PopplerPage *poppler_page;
1275 pdf_document = PDF_DOCUMENT (document_thumbnails);
1276 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1278 g_return_if_fail (poppler_page != NULL);
1280 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1283 double page_width, page_height;
1285 poppler_page_get_size (poppler_page, &page_width, &page_height);
1287 *width = (gint) MAX (page_width * rc->scale, 1);
1288 *height = (gint) MAX (page_height * rc->scale, 1);
1291 if (rc->rotation == 90 || rc->rotation == 270) {
1299 g_object_unref (poppler_page);
1303 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1305 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1306 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1311 pdf_document_search_idle_callback (void *data)
1313 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1314 PdfDocument *pdf_document = search->document;
1319 page = poppler_document_get_page (search->document->document,
1320 search->search_page);
1322 ev_document_doc_mutex_lock ();
1323 matches = poppler_page_find_text (page, search->text);
1324 ev_document_doc_mutex_unlock ();
1326 g_object_unref (page);
1328 search->pages[search->search_page] = matches;
1329 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1330 search->search_page);
1332 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1333 search->search_page += 1;
1334 if (search->search_page == n_pages) {
1336 search->search_page = 0;
1339 if (search->search_page != search->start_page) {
1344 search->idle = 0; /* will return FALSE to remove */
1349 static PdfDocumentSearch *
1350 pdf_document_search_new (PdfDocument *pdf_document,
1354 PdfDocumentSearch *search;
1358 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1360 search = g_new0 (PdfDocumentSearch, 1);
1362 search->text = g_strdup (text);
1363 search->pages = g_new0 (GList *, n_pages);
1364 search->document = pdf_document;
1366 /* We add at low priority so the progress bar repaints */
1367 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1368 pdf_document_search_idle_callback,
1372 search->start_page = start_page;
1373 search->search_page = start_page;
1379 pdf_document_find_begin (EvDocumentFind *document,
1381 const char *search_string,
1382 gboolean case_sensitive)
1384 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1386 /* FIXME handle case_sensitive (right now XPDF
1387 * code is always case insensitive for ASCII
1388 * and case sensitive for all other languaages)
1391 if (pdf_document->search &&
1392 strcmp (search_string, pdf_document->search->text) == 0)
1395 if (pdf_document->search)
1396 pdf_document_search_free (pdf_document->search);
1398 pdf_document->search = pdf_document_search_new (pdf_document,
1404 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1406 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1409 return g_list_length (search->pages[page]);
1416 pdf_document_find_get_result (EvDocumentFind *document_find,
1419 EvRectangle *rectangle)
1421 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1422 PdfDocumentSearch *search = pdf_document->search;
1423 PopplerPage *poppler_page;
1424 PopplerRectangle *r;
1430 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1435 poppler_page = poppler_document_get_page (pdf_document->document, page);
1436 poppler_page_get_size (poppler_page, NULL, &height);
1437 rectangle->x1 = r->x1;
1438 rectangle->y1 = height - r->y2;
1439 rectangle->x2 = r->x2;
1440 rectangle->y2 = height - r->y1;
1441 g_object_unref (poppler_page);
1447 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1450 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1452 return search && search->pages[page] != NULL;
1456 pdf_document_find_get_progress (EvDocumentFind *document_find)
1458 PdfDocumentSearch *search;
1459 int n_pages, pages_done;
1461 search = PDF_DOCUMENT (document_find)->search;
1463 if (search == NULL) {
1467 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1468 if (search->search_page > search->start_page) {
1469 pages_done = search->search_page - search->start_page + 1;
1470 } else if (search->search_page == search->start_page) {
1471 pages_done = n_pages;
1473 pages_done = n_pages - search->start_page + search->search_page;
1476 return pages_done / (double) n_pages;
1480 pdf_document_find_cancel (EvDocumentFind *document)
1482 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1484 if (pdf_document->search) {
1485 pdf_document_search_free (pdf_document->search);
1486 pdf_document->search = NULL;
1491 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1493 iface->begin = pdf_document_find_begin;
1494 iface->get_n_results = pdf_document_find_get_n_results;
1495 iface->get_result = pdf_document_find_get_result;
1496 iface->page_has_results = pdf_document_find_page_has_results;
1497 iface->get_progress = pdf_document_find_get_progress;
1498 iface->cancel = pdf_document_find_cancel;
1502 pdf_print_context_free (PdfPrintContext *ctx)
1507 #ifdef HAVE_CAIRO_PRINT
1509 cairo_destroy (ctx->cr);
1514 poppler_ps_file_free (ctx->ps_file);
1515 ctx->ps_file = NULL;
1522 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1523 EvFileExporterContext *fc)
1525 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1526 PdfPrintContext *ctx;
1527 #ifdef HAVE_CAIRO_PRINT
1528 gdouble width, height;
1529 cairo_surface_t *surface = NULL;
1532 if (pdf_document->print_ctx)
1533 pdf_print_context_free (pdf_document->print_ctx);
1534 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1535 ctx = pdf_document->print_ctx;
1536 ctx->format = fc->format;
1538 #ifdef HAVE_CAIRO_PRINT
1539 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1541 ctx->paper_width = fc->paper_width;
1542 ctx->paper_height = fc->paper_height;
1544 switch (fc->pages_per_sheet) {
1572 ctx->pages_printed = 0;
1574 switch (fc->format) {
1575 case EV_FILE_FORMAT_PS:
1576 #ifdef HAVE_CAIRO_PS
1577 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1580 case EV_FILE_FORMAT_PDF:
1581 #ifdef HAVE_CAIRO_PDF
1582 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1586 g_assert_not_reached ();
1589 ctx->cr = cairo_create (surface);
1590 cairo_surface_destroy (surface);
1592 #else /* HAVE_CAIRO_PRINT */
1593 if (ctx->format == EV_FILE_FORMAT_PS) {
1594 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1595 fc->filename, fc->first_page,
1596 fc->last_page - fc->first_page + 1);
1597 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1598 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1600 #endif /* HAVE_CAIRO_PRINT */
1604 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1606 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1607 PdfPrintContext *ctx = pdf_document->print_ctx;
1609 g_return_if_fail (pdf_document->print_ctx != NULL);
1611 ctx->pages_printed = 0;
1613 #ifdef HAVE_CAIRO_PRINT
1614 if (ctx->paper_width > ctx->paper_height) {
1615 if (ctx->format == EV_FILE_FORMAT_PS) {
1616 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1619 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1620 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1625 #endif /* HAVE_CAIRO_PRINT */
1629 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1630 EvRenderContext *rc)
1632 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1633 PdfPrintContext *ctx = pdf_document->print_ctx;
1634 PopplerPage *poppler_page;
1635 #ifdef HAVE_CAIRO_PRINT
1636 gdouble page_width, page_height;
1638 gboolean rotate, landscape;
1639 gdouble width, height;
1640 gdouble pwidth, pheight;
1641 gdouble xscale, yscale;
1644 g_return_if_fail (pdf_document->print_ctx != NULL);
1646 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1648 #ifdef HAVE_CAIRO_PRINT
1649 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1650 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1651 poppler_page_get_size (poppler_page, &page_width, &page_height);
1653 if (page_width > page_height && page_width > ctx->paper_width) {
1659 landscape = (ctx->paper_width > ctx->paper_height);
1661 /* Use always portrait mode and rotate when necessary */
1662 if (ctx->paper_width > ctx->paper_height) {
1663 width = ctx->paper_height;
1664 height = ctx->paper_width;
1667 width = ctx->paper_width;
1668 height = ctx->paper_height;
1671 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1684 page_width = page_height;
1688 pwidth = width / ctx->pages_x;
1689 pheight = height / ctx->pages_y;
1691 if ((page_width > pwidth || page_height > pheight) ||
1692 (page_width < pwidth && page_height < pheight)) {
1693 xscale = pwidth / page_width;
1694 yscale = pheight / page_height;
1696 if (yscale < xscale) {
1703 xscale = yscale = 1;
1708 cairo_save (ctx->cr);
1710 cairo_matrix_t matrix;
1712 cairo_translate (ctx->cr, width, 0);
1713 cairo_matrix_init (&matrix,
1717 cairo_transform (ctx->cr, &matrix);
1720 cairo_translate (ctx->cr,
1721 x * (rotate ? pheight : pwidth),
1722 y * (rotate ? pwidth : pheight));
1723 cairo_scale (ctx->cr, xscale, yscale);
1725 #ifdef HAVE_POPPLER_PAGE_RENDER_FOR_PRINTING
1726 poppler_page_render_for_printing (poppler_page, ctx->cr);
1728 #ifdef HAVE_POPPLER_PAGE_RENDER
1729 poppler_page_render (poppler_page, ctx->cr);
1733 ctx->pages_printed++;
1735 cairo_restore (ctx->cr);
1736 #else /* HAVE_CAIRO_PRINT */
1737 if (ctx->format == EV_FILE_FORMAT_PS)
1738 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1739 #endif /* HAVE_CAIRO_PRINT */
1741 g_object_unref (poppler_page);
1745 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1747 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1748 PdfPrintContext *ctx = pdf_document->print_ctx;
1750 g_return_if_fail (pdf_document->print_ctx != NULL);
1752 #ifdef HAVE_CAIRO_PRINT
1753 cairo_show_page (ctx->cr);
1758 pdf_document_file_exporter_end (EvFileExporter *exporter)
1760 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1762 pdf_print_context_free (pdf_document->print_ctx);
1763 pdf_document->print_ctx = NULL;
1766 static EvFileExporterCapabilities
1767 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1769 return (EvFileExporterCapabilities) (
1770 EV_FILE_EXPORTER_CAN_PAGE_SET |
1771 EV_FILE_EXPORTER_CAN_COPIES |
1772 EV_FILE_EXPORTER_CAN_COLLATE |
1773 EV_FILE_EXPORTER_CAN_REVERSE |
1774 EV_FILE_EXPORTER_CAN_SCALE |
1775 #ifdef HAVE_CAIRO_PRINT
1776 #ifdef HAVE_POPPLER_PAGE_RENDER
1777 #if GTK_CHECK_VERSION (2, 11, 1)
1778 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1783 #ifdef HAVE_CAIRO_PDF
1784 #ifdef HAVE_POPPLER_PAGE_RENDER
1785 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1788 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1792 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1794 iface->begin = pdf_document_file_exporter_begin;
1795 iface->begin_page = pdf_document_file_exporter_begin_page;
1796 iface->do_page = pdf_document_file_exporter_do_page;
1797 iface->end_page = pdf_document_file_exporter_end_page;
1798 iface->end = pdf_document_file_exporter_end;
1799 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1803 pdf_selection_render_selection (EvSelection *selection,
1804 EvRenderContext *rc,
1805 cairo_surface_t **surface,
1806 EvRectangle *points,
1807 EvRectangle *old_points,
1808 EvSelectionStyle style,
1812 PdfDocument *pdf_document;
1813 double width_points, height_points;
1816 pdf_document = PDF_DOCUMENT (selection);
1817 set_rc_data (pdf_document, rc);
1819 poppler_page_get_size (POPPLER_PAGE (rc->data),
1820 &width_points, &height_points);
1821 width = (int) ((width_points * rc->scale) + 0.5);
1822 height = (int) ((height_points * rc->scale) + 0.5);
1824 #ifdef HAVE_POPPLER_PAGE_RENDER
1827 if (*surface == NULL) {
1828 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1833 cr = cairo_create (*surface);
1834 cairo_scale (cr, rc->scale, rc->scale);
1835 cairo_surface_set_device_offset (*surface, 0, 0);
1836 memset (cairo_image_surface_get_data (*surface), 0x00,
1837 cairo_image_surface_get_height (*surface) *
1838 cairo_image_surface_get_stride (*surface));
1839 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1841 (PopplerRectangle *)points,
1842 (PopplerRectangle *)old_points,
1843 (PopplerSelectionStyle)style,
1847 #else /* HAVE_POPPLER_PAGE_RENDER */
1850 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1854 poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1855 rc->scale, rc->rotation, pixbuf,
1856 (PopplerRectangle *)points,
1857 (PopplerRectangle *)old_points,
1858 (PopplerSelectionStyle)style,
1862 cairo_surface_destroy (*surface);
1863 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1864 g_object_unref (pixbuf);
1865 #endif /* HAVE_POPPLER_PAGE_RENDER */
1869 pdf_selection_get_selected_text (EvSelection *selection,
1870 EvRenderContext *rc,
1871 EvSelectionStyle style,
1872 EvRectangle *points)
1874 PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1875 PopplerPage *poppler_page;
1880 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1881 g_return_val_if_fail (poppler_page != NULL, NULL);
1883 poppler_page_get_size (poppler_page, NULL, &height);
1885 r.y1 = height - points->y2;
1887 r.y2 = height - points->y1;
1889 retval = poppler_page_get_text (poppler_page,
1890 (PopplerSelectionStyle)style,
1893 g_object_unref (poppler_page);
1899 pdf_selection_get_selection_region (EvSelection *selection,
1900 EvRenderContext *rc,
1901 EvSelectionStyle style,
1902 EvRectangle *points)
1904 PdfDocument *pdf_document;
1907 pdf_document = PDF_DOCUMENT (selection);
1909 set_rc_data (pdf_document, rc);
1911 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data,
1913 (PopplerSelectionStyle)style,
1914 (PopplerRectangle *) points);
1919 pdf_selection_get_selection_map (EvSelection *selection,
1920 EvRenderContext *rc)
1922 PdfDocument *pdf_document;
1923 PopplerPage *poppler_page;
1924 PopplerRectangle points;
1927 pdf_document = PDF_DOCUMENT (selection);
1928 poppler_page = poppler_document_get_page (pdf_document->document,
1933 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1934 retval = poppler_page_get_selection_region (poppler_page, 1.0,
1935 POPPLER_SELECTION_GLYPH,
1937 g_object_unref (poppler_page);
1943 pdf_selection_iface_init (EvSelectionIface *iface)
1945 iface->render_selection = pdf_selection_render_selection;
1946 iface->get_selected_text = pdf_selection_get_selected_text;
1947 iface->get_selection_region = pdf_selection_get_selection_region;
1948 iface->get_selection_map = pdf_selection_get_selection_map;
1951 /* Page Transitions */
1953 pdf_document_get_page_duration (EvDocumentTransition *trans,
1956 PdfDocument *pdf_document;
1957 PopplerPage *poppler_page;
1958 gdouble duration = -1;
1960 pdf_document = PDF_DOCUMENT (trans);
1961 poppler_page = poppler_document_get_page (pdf_document->document, page);
1965 duration = poppler_page_get_duration (poppler_page);
1966 g_object_unref (poppler_page);
1971 static EvTransitionEffect *
1972 pdf_document_get_effect (EvDocumentTransition *trans,
1975 PdfDocument *pdf_document;
1976 PopplerPage *poppler_page;
1977 PopplerPageTransition *page_transition;
1978 EvTransitionEffect *effect;
1980 pdf_document = PDF_DOCUMENT (trans);
1981 poppler_page = poppler_document_get_page (pdf_document->document, page);
1986 page_transition = poppler_page_get_transition (poppler_page);
1988 if (!page_transition) {
1989 g_object_unref (poppler_page);
1993 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1994 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1995 "alignment", page_transition->alignment,
1996 "direction", page_transition->direction,
1997 "duration", page_transition->duration,
1998 "angle", page_transition->angle,
1999 "scale", page_transition->scale,
2000 "rectangular", page_transition->rectangular,
2003 poppler_page_transition_free (page_transition);
2004 g_object_unref (poppler_page);
2010 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2012 iface->get_page_duration = pdf_document_get_page_duration;
2013 iface->get_effect = pdf_document_get_effect;
2018 pdf_document_get_crop_box (EvDocument *document,
2022 PdfDocument *pdf_document;
2023 PopplerPage *poppler_page;
2024 PopplerRectangle poppler_rect;
2026 pdf_document = PDF_DOCUMENT (document);
2027 poppler_page = poppler_document_get_page (pdf_document->document, page);
2028 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2029 rect->x1 = poppler_rect.x1;
2030 rect->x2 = poppler_rect.x2;
2031 rect->y1 = poppler_rect.y1;
2032 rect->y2 = poppler_rect.y2;
2035 static EvFormField *
2036 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2038 EvFormField *ev_field = NULL;
2041 gboolean is_read_only;
2043 id = poppler_form_field_get_id (poppler_field);
2044 font_size = poppler_form_field_get_font_size (poppler_field);
2045 is_read_only = poppler_form_field_is_read_only (poppler_field);
2047 switch (poppler_form_field_get_field_type (poppler_field)) {
2048 case POPPLER_FORM_FIELD_TEXT: {
2049 EvFormFieldText *field_text;
2050 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2052 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2053 case POPPLER_FORM_TEXT_NORMAL:
2054 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2056 case POPPLER_FORM_TEXT_MULTILINE:
2057 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2059 case POPPLER_FORM_TEXT_FILE_SELECT:
2060 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2064 ev_field = ev_form_field_text_new (id, ev_text_type);
2065 field_text = EV_FORM_FIELD_TEXT (ev_field);
2067 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2068 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2069 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2070 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2071 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2072 field_text->text = poppler_form_field_text_get_text (poppler_field);
2076 case POPPLER_FORM_FIELD_BUTTON: {
2077 EvFormFieldButton *field_button;
2078 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2080 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2081 case POPPLER_FORM_BUTTON_PUSH:
2082 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2084 case POPPLER_FORM_BUTTON_CHECK:
2085 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2087 case POPPLER_FORM_BUTTON_RADIO:
2088 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2092 ev_field = ev_form_field_button_new (id, ev_button_type);
2093 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2095 field_button->state = poppler_form_field_button_get_state (poppler_field);
2098 case POPPLER_FORM_FIELD_CHOICE: {
2099 EvFormFieldChoice *field_choice;
2100 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2102 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2103 case POPPLER_FORM_CHOICE_COMBO:
2104 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2106 case EV_FORM_FIELD_CHOICE_LIST:
2107 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2111 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2112 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2114 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2115 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2116 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2117 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2119 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2120 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2121 if (field_choice->is_editable)
2122 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2125 case POPPLER_FORM_FIELD_SIGNATURE:
2127 ev_field = ev_form_field_signature_new (id);
2129 case POPPLER_FORM_FIELD_UNKNOWN:
2133 ev_field->font_size = font_size;
2134 ev_field->is_read_only = is_read_only;
2140 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2143 PdfDocument *pdf_document;
2144 PopplerPage *poppler_page;
2145 GList *retval = NULL;
2150 pdf_document = PDF_DOCUMENT (document);
2151 poppler_page = poppler_document_get_page (pdf_document->document, page);
2152 fields = poppler_page_get_form_field_mapping (poppler_page);
2153 poppler_page_get_size (poppler_page, NULL, &height);
2155 for (list = fields; list; list = list->next) {
2156 PopplerFormFieldMapping *mapping;
2157 EvFormFieldMapping *field_mapping;
2158 EvFormField *ev_field;
2160 mapping = (PopplerFormFieldMapping *)list->data;
2162 ev_field = ev_form_field_from_poppler_field (mapping->field);
2166 field_mapping = g_new0 (EvFormFieldMapping, 1);
2167 field_mapping->x1 = mapping->area.x1;
2168 field_mapping->x2 = mapping->area.x2;
2169 field_mapping->y1 = height - mapping->area.y2;
2170 field_mapping->y2 = height - mapping->area.y1;
2171 field_mapping->field = ev_field;
2172 field_mapping->field->page = page;
2174 retval = g_list_prepend (retval, field_mapping);
2177 poppler_page_free_form_field_mapping (fields);
2178 g_object_unref (poppler_page);
2180 return g_list_reverse (retval);
2184 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2188 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2189 PopplerFormField *poppler_field;
2192 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2196 text = poppler_form_field_text_get_text (poppler_field);
2197 g_object_unref (poppler_field);
2203 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2207 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2208 PopplerFormField *poppler_field;
2210 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2213 poppler_form_field_text_set_text (poppler_field, text);
2214 g_object_unref (poppler_field);
2218 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2222 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2223 PopplerFormField *poppler_field;
2225 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2229 poppler_form_field_button_set_state (poppler_field, state);
2230 g_object_unref (poppler_field);
2234 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2237 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2238 PopplerFormField *poppler_field;
2241 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2245 state = poppler_form_field_button_get_state (poppler_field);
2246 g_object_unref (poppler_field);
2252 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2256 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2257 PopplerFormField *poppler_field;
2260 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2264 text = poppler_form_field_choice_get_item (poppler_field, index);
2265 g_object_unref (poppler_field);
2271 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2274 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2275 PopplerFormField *poppler_field;
2278 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2282 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2283 g_object_unref (poppler_field);
2289 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2293 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2294 PopplerFormField *poppler_field;
2297 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2301 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2302 g_object_unref (poppler_field);
2308 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2312 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2313 PopplerFormField *poppler_field;
2315 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2319 poppler_form_field_choice_select_item (poppler_field, index);
2320 g_object_unref (poppler_field);
2324 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2328 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2329 PopplerFormField *poppler_field;
2331 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2335 poppler_form_field_choice_toggle_item (poppler_field, index);
2336 g_object_unref (poppler_field);
2340 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2343 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2344 PopplerFormField *poppler_field;
2346 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2350 poppler_form_field_choice_unselect_all (poppler_field);
2351 g_object_unref (poppler_field);
2355 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2359 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2360 PopplerFormField *poppler_field;
2362 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2366 poppler_form_field_choice_set_text (poppler_field, text);
2367 g_object_unref (poppler_field);
2371 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2374 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2375 PopplerFormField *poppler_field;
2378 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2382 text = poppler_form_field_choice_get_text (poppler_field);
2383 g_object_unref (poppler_field);
2389 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2391 iface->get_form_fields = pdf_document_forms_get_form_fields;
2392 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2393 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2394 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2395 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2396 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2397 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2398 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2399 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2400 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2401 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2402 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2403 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;