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_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 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
130 pdf_document_security_iface_init);
131 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
132 pdf_document_document_thumbnails_iface_init);
133 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
134 pdf_document_document_links_iface_init);
135 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
136 pdf_document_document_images_iface_init);
137 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
138 pdf_document_document_forms_iface_init);
139 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
140 pdf_document_document_fonts_iface_init);
141 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
142 pdf_document_find_iface_init);
143 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
144 pdf_document_file_exporter_iface_init);
145 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
146 pdf_selection_iface_init);
147 EV_BACKEND_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_page_render (PopplerPage *page,
453 cairo_surface_t *surface;
455 #ifdef HAVE_POPPLER_PAGE_RENDER
458 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
460 memset (cairo_image_surface_get_data (surface), 0xff,
461 cairo_image_surface_get_height (surface) *
462 cairo_image_surface_get_stride (surface));
464 cr = cairo_create (surface);
465 switch (rc->rotation) {
467 cairo_translate (cr, width, 0);
470 cairo_translate (cr, width, height);
473 cairo_translate (cr, 0, height);
476 cairo_translate (cr, 0, 0);
478 cairo_scale (cr, rc->scale, rc->scale);
479 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
480 poppler_page_render (page, cr);
482 #else /* HAVE_POPPLER_PAGE_RENDER */
485 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
489 poppler_page_render_to_pixbuf (page,
495 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
496 g_object_unref (pixbuf);
497 #endif /* HAVE_POPPLER_PAGE_RENDER */
502 static cairo_surface_t *
503 pdf_document_render (EvDocument *document,
506 PdfDocument *pdf_document;
507 double width_points, height_points;
510 pdf_document = PDF_DOCUMENT (document);
512 set_rc_data (pdf_document, rc);
514 poppler_page_get_size (POPPLER_PAGE (rc->data),
515 &width_points, &height_points);
517 if (rc->rotation == 90 || rc->rotation == 270) {
518 width = (int) ((height_points * rc->scale) + 0.5);
519 height = (int) ((width_points * rc->scale) + 0.5);
521 width = (int) ((width_points * rc->scale) + 0.5);
522 height = (int) ((height_points * rc->scale) + 0.5);
525 return pdf_page_render (POPPLER_PAGE (rc->data),
529 /* EvDocumentSecurity */
532 pdf_document_has_document_security (EvDocumentSecurity *document_security)
534 /* FIXME: do we really need to have this? */
539 pdf_document_set_password (EvDocumentSecurity *document_security,
540 const char *password)
542 PdfDocument *document = PDF_DOCUMENT (document_security);
544 if (document->password)
545 g_free (document->password);
547 document->password = g_strdup (password);
550 static EvDocumentInfo *
551 pdf_document_get_info (EvDocument *document)
553 EvDocumentInfo *info;
554 PopplerPageLayout layout;
555 PopplerPageMode mode;
556 PopplerViewerPreferences view_prefs;
557 PopplerPermissions permissions;
559 info = g_new0 (EvDocumentInfo, 1);
561 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
562 EV_DOCUMENT_INFO_FORMAT |
563 EV_DOCUMENT_INFO_AUTHOR |
564 EV_DOCUMENT_INFO_SUBJECT |
565 EV_DOCUMENT_INFO_KEYWORDS |
566 EV_DOCUMENT_INFO_LAYOUT |
567 EV_DOCUMENT_INFO_START_MODE |
568 EV_DOCUMENT_INFO_PERMISSIONS |
569 EV_DOCUMENT_INFO_UI_HINTS |
570 EV_DOCUMENT_INFO_CREATOR |
571 EV_DOCUMENT_INFO_PRODUCER |
572 EV_DOCUMENT_INFO_CREATION_DATE |
573 EV_DOCUMENT_INFO_MOD_DATE |
574 EV_DOCUMENT_INFO_LINEARIZED |
575 EV_DOCUMENT_INFO_N_PAGES |
576 EV_DOCUMENT_INFO_SECURITY |
577 EV_DOCUMENT_INFO_PAPER_SIZE;
579 g_object_get (PDF_DOCUMENT (document)->document,
580 "title", &(info->title),
581 "format", &(info->format),
582 "author", &(info->author),
583 "subject", &(info->subject),
584 "keywords", &(info->keywords),
586 "page-layout", &layout,
587 "viewer-preferences", &view_prefs,
588 "permissions", &permissions,
589 "creator", &(info->creator),
590 "producer", &(info->producer),
591 "creation-date", &(info->creation_date),
592 "mod-date", &(info->modified_date),
593 "linearized", &(info->linearized),
596 pdf_document_get_page_size(document, 0,
597 &(info->paper_width),
598 &(info->paper_height));
601 info->paper_width = info->paper_width / 72.0f * 25.4f;
602 info->paper_height = info->paper_height / 72.0f * 25.4f;
605 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
606 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
608 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
609 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
611 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
612 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
614 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
615 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
616 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
617 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
619 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
620 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
627 case POPPLER_PAGE_MODE_NONE:
628 info->mode = EV_DOCUMENT_MODE_NONE;
630 case POPPLER_PAGE_MODE_USE_THUMBS:
631 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
633 case POPPLER_PAGE_MODE_USE_OC:
634 info->mode = EV_DOCUMENT_MODE_USE_OC;
636 case POPPLER_PAGE_MODE_FULL_SCREEN:
637 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
639 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
640 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
646 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
647 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
649 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
650 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
652 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
653 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
655 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
656 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
658 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
659 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
661 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
662 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
664 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
665 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
668 info->permissions = 0;
669 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
670 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
672 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
673 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
675 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
676 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
678 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
679 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
682 info->n_pages = ev_document_get_n_pages (document);
684 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
685 /* translators: this is the document security state */
686 info->security = g_strdup (_("Yes"));
688 /* translators: this is the document security state */
689 info->security = g_strdup (_("No"));
696 pdf_document_document_iface_init (EvDocumentIface *iface)
698 iface->save = pdf_document_save;
699 iface->load = pdf_document_load;
700 iface->get_n_pages = pdf_document_get_n_pages;
701 iface->get_page_size = pdf_document_get_page_size;
702 iface->get_page_label = pdf_document_get_page_label;
703 iface->has_attachments = pdf_document_has_attachments;
704 iface->get_attachments = pdf_document_get_attachments;
705 iface->render = pdf_document_render;
706 iface->get_info = pdf_document_get_info;
710 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
712 iface->has_document_security = pdf_document_has_document_security;
713 iface->set_password = pdf_document_set_password;
717 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
719 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
722 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
724 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
728 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
731 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
734 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
736 if (pdf_document->font_info == NULL) {
737 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
740 if (pdf_document->fonts_iter) {
741 poppler_fonts_iter_free (pdf_document->fonts_iter);
744 pdf_document->fonts_scanned_pages += n_pages;
746 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
747 &pdf_document->fonts_iter);
749 pdf_document->fonts_scanned_pages = 0;
750 poppler_font_info_free (pdf_document->font_info);
751 pdf_document->font_info = NULL;
758 font_type_to_string (PopplerFontType type)
761 case POPPLER_FONT_TYPE_TYPE1:
763 case POPPLER_FONT_TYPE_TYPE1C:
765 case POPPLER_FONT_TYPE_TYPE3:
767 case POPPLER_FONT_TYPE_TRUETYPE:
768 return _("TrueType");
769 case POPPLER_FONT_TYPE_CID_TYPE0:
770 return _("Type 1 (CID)");
771 case POPPLER_FONT_TYPE_CID_TYPE0C:
772 return _("Type 1C (CID)");
773 case POPPLER_FONT_TYPE_CID_TYPE2:
774 return _("TrueType (CID)");
776 return _("Unknown font type");
781 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
784 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
785 PopplerFontsIter *iter = pdf_document->fonts_iter;
787 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
793 GtkTreeIter list_iter;
796 const char *embedded;
799 name = poppler_fonts_iter_get_name (iter);
805 type = font_type_to_string (
806 poppler_fonts_iter_get_font_type (iter));
808 if (poppler_fonts_iter_is_embedded (iter)) {
809 if (poppler_fonts_iter_is_subset (iter))
810 embedded = _("Embedded subset");
812 embedded = _("Embedded");
814 embedded = _("Not embedded");
817 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
819 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
820 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
821 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
822 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
826 } while (poppler_fonts_iter_next (iter));
830 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
832 iface->fill_model = pdf_document_fonts_fill_model;
833 iface->scan = pdf_document_fonts_scan;
834 iface->get_progress = pdf_document_fonts_get_progress;
838 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
840 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
841 PopplerIndexIter *iter;
843 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
845 iter = poppler_index_iter_new (pdf_document->document);
848 poppler_index_iter_free (iter);
854 ev_link_dest_from_dest (PdfDocument *pdf_document,
857 EvLinkDest *ev_dest = NULL;
858 const char *unimplemented_dest = NULL;
860 g_assert (dest != NULL);
862 switch (dest->type) {
863 case POPPLER_DEST_XYZ: {
864 PopplerPage *poppler_page;
867 poppler_page = poppler_document_get_page (pdf_document->document,
868 MAX (0, dest->page_num - 1));
869 poppler_page_get_size (poppler_page, NULL, &height);
870 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
877 g_object_unref (poppler_page);
880 case POPPLER_DEST_FIT:
881 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
883 case POPPLER_DEST_FITH: {
884 PopplerPage *poppler_page;
887 poppler_page = poppler_document_get_page (pdf_document->document,
888 MAX (0, dest->page_num - 1));
889 poppler_page_get_size (poppler_page, NULL, &height);
890 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
893 g_object_unref (poppler_page);
896 case POPPLER_DEST_FITV:
897 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
901 case POPPLER_DEST_FITR: {
902 PopplerPage *poppler_page;
905 poppler_page = poppler_document_get_page (pdf_document->document,
906 MAX (0, dest->page_num - 1));
907 poppler_page_get_size (poppler_page, NULL, &height);
908 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
910 height - dest->bottom,
913 g_object_unref (poppler_page);
916 case POPPLER_DEST_FITB:
917 unimplemented_dest = "POPPLER_DEST_FITB";
919 case POPPLER_DEST_FITBH:
920 unimplemented_dest = "POPPLER_DEST_FITBH";
922 case POPPLER_DEST_FITBV:
923 unimplemented_dest = "POPPLER_DEST_FITBV";
925 case POPPLER_DEST_NAMED:
926 ev_dest = ev_link_dest_new_named (dest->named_dest);
928 case POPPLER_DEST_UNKNOWN:
929 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
933 if (unimplemented_dest) {
934 g_warning ("Unimplemented destination: %s, please post a "
935 "bug report in Evince bugzilla "
936 "(http://bugzilla.gnome.org) with a testcase.",
941 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
947 ev_link_from_action (PdfDocument *pdf_document,
948 PopplerAction *action)
951 EvLinkAction *ev_action = NULL;
952 const char *unimplemented_action = NULL;
954 switch (action->type) {
955 case POPPLER_ACTION_NONE:
957 case POPPLER_ACTION_GOTO_DEST: {
960 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
961 ev_action = ev_link_action_new_dest (dest);
964 case POPPLER_ACTION_GOTO_REMOTE: {
967 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
968 ev_action = ev_link_action_new_remote (dest,
969 action->goto_remote.file_name);
973 case POPPLER_ACTION_LAUNCH:
974 ev_action = ev_link_action_new_launch (action->launch.file_name,
975 action->launch.params);
977 case POPPLER_ACTION_URI:
978 ev_action = ev_link_action_new_external_uri (action->uri.uri);
980 case POPPLER_ACTION_NAMED:
981 ev_action = ev_link_action_new_named (action->named.named_dest);
983 case POPPLER_ACTION_MOVIE:
984 unimplemented_action = "POPPLER_ACTION_MOVIE";
986 case POPPLER_ACTION_UNKNOWN:
987 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
990 if (unimplemented_action) {
991 g_warning ("Unimplemented action: %s, please post a bug report "
992 "in Evince bugzilla (http://bugzilla.gnome.org) "
993 "with a testcase.", unimplemented_action);
996 link = ev_link_new (action->any.title, ev_action);
1002 build_tree (PdfDocument *pdf_document,
1003 GtkTreeModel *model,
1004 GtkTreeIter *parent,
1005 PopplerIndexIter *iter)
1009 GtkTreeIter tree_iter;
1010 PopplerIndexIter *child;
1011 PopplerAction *action;
1012 EvLink *link = NULL;
1016 action = poppler_index_iter_get_action (iter);
1017 expand = poppler_index_iter_is_open (iter);
1022 switch (action->type) {
1023 case POPPLER_ACTION_GOTO_DEST: {
1024 /* For bookmarks, solve named destinations */
1025 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1027 EvLinkDest *ev_dest = NULL;
1028 EvLinkAction *ev_action;
1030 dest = poppler_document_find_dest (pdf_document->document,
1031 action->goto_dest.dest->named_dest);
1033 link = ev_link_from_action (pdf_document, action);
1037 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1038 poppler_dest_free (dest);
1040 ev_action = ev_link_action_new_dest (ev_dest);
1041 link = ev_link_new (action->any.title, ev_action);
1043 link = ev_link_from_action (pdf_document, action);
1048 link = ev_link_from_action (pdf_document, action);
1052 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1053 poppler_action_free (action);
1055 g_object_unref (link);
1060 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1061 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1063 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1064 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1065 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1066 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1069 g_free (title_markup);
1070 g_object_unref (link);
1072 child = poppler_index_iter_get_child (iter);
1074 build_tree (pdf_document, model, &tree_iter, child);
1075 poppler_index_iter_free (child);
1076 poppler_action_free (action);
1078 } while (poppler_index_iter_next (iter));
1081 static GtkTreeModel *
1082 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1084 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1085 GtkTreeModel *model = NULL;
1086 PopplerIndexIter *iter;
1088 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1090 iter = poppler_index_iter_new (pdf_document->document);
1091 /* Create the model if we have items*/
1093 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1098 build_tree (pdf_document, model, NULL, iter);
1099 poppler_index_iter_free (iter);
1106 pdf_document_links_get_links (EvDocumentLinks *document_links,
1109 PdfDocument *pdf_document;
1110 PopplerPage *poppler_page;
1111 GList *retval = NULL;
1112 GList *mapping_list;
1116 pdf_document = PDF_DOCUMENT (document_links);
1117 poppler_page = poppler_document_get_page (pdf_document->document,
1119 mapping_list = poppler_page_get_link_mapping (poppler_page);
1120 poppler_page_get_size (poppler_page, NULL, &height);
1122 for (list = mapping_list; list; list = list->next) {
1123 PopplerLinkMapping *link_mapping;
1124 EvLinkMapping *ev_link_mapping;
1126 link_mapping = (PopplerLinkMapping *)list->data;
1127 ev_link_mapping = g_new (EvLinkMapping, 1);
1128 ev_link_mapping->link = ev_link_from_action (pdf_document,
1129 link_mapping->action);
1130 ev_link_mapping->x1 = link_mapping->area.x1;
1131 ev_link_mapping->x2 = link_mapping->area.x2;
1132 /* Invert this for X-style coordinates */
1133 ev_link_mapping->y1 = height - link_mapping->area.y2;
1134 ev_link_mapping->y2 = height - link_mapping->area.y1;
1136 retval = g_list_prepend (retval, ev_link_mapping);
1139 poppler_page_free_link_mapping (mapping_list);
1140 g_object_unref (poppler_page);
1142 return g_list_reverse (retval);
1146 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1147 const gchar *link_name)
1149 PdfDocument *pdf_document;
1151 EvLinkDest *ev_dest = NULL;
1153 pdf_document = PDF_DOCUMENT (document_links);
1154 dest = poppler_document_find_dest (pdf_document->document,
1157 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1158 poppler_dest_free (dest);
1165 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1167 iface->has_document_links = pdf_document_links_has_document_links;
1168 iface->get_links_model = pdf_document_links_get_links_model;
1169 iface->get_links = pdf_document_links_get_links;
1170 iface->find_link_dest = pdf_document_links_find_link_dest;
1174 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1177 GList *retval = NULL;
1178 PdfDocument *pdf_document;
1179 PopplerPage *poppler_page;
1180 GList *mapping_list;
1183 pdf_document = PDF_DOCUMENT (document_images);
1184 poppler_page = poppler_document_get_page (pdf_document->document, page);
1185 mapping_list = poppler_page_get_image_mapping (poppler_page);
1187 for (list = mapping_list; list; list = list->next) {
1188 PopplerImageMapping *image_mapping;
1189 EvImageMapping *ev_image_mapping;
1191 image_mapping = (PopplerImageMapping *)list->data;
1193 ev_image_mapping = g_new (EvImageMapping, 1);
1195 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1196 ev_image_mapping->x1 = image_mapping->area.x1;
1197 ev_image_mapping->x2 = image_mapping->area.x2;
1198 ev_image_mapping->y1 = image_mapping->area.y1;
1199 ev_image_mapping->y2 = image_mapping->area.y2;
1201 retval = g_list_prepend (retval, ev_image_mapping);
1204 poppler_page_free_image_mapping (mapping_list);
1205 g_object_unref (poppler_page);
1207 return g_list_reverse (retval);
1211 pdf_document_images_get_image (EvDocumentImages *document_images,
1214 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1215 PdfDocument *pdf_document;
1216 PopplerPage *poppler_page;
1217 cairo_surface_t *surface;
1218 GdkPixbuf *retval = NULL;
1220 pdf_document = PDF_DOCUMENT (document_images);
1221 poppler_page = poppler_document_get_page (pdf_document->document,
1222 ev_image_get_page (image));
1224 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1226 retval = ev_document_misc_pixbuf_from_surface (surface);
1227 cairo_surface_destroy (surface);
1230 g_object_unref (poppler_page);
1234 return GDK_PIXBUF (g_object_ref (ev_image_get_pixbuf (image)));
1235 #endif /* HAVE_POPPLER_PAGE_GET_IMAGE */
1239 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1241 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1242 iface->get_image = pdf_document_images_get_image;
1246 make_thumbnail_for_page (PdfDocument *pdf_document,
1247 PopplerPage *poppler_page,
1248 EvRenderContext *rc)
1253 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1254 rc, &width, &height);
1255 #ifdef POPPLER_WITH_GDK
1256 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1258 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1260 ev_document_fc_mutex_lock ();
1261 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1263 rc->scale, rc->rotation, pixbuf);
1264 ev_document_fc_mutex_unlock ();
1266 cairo_surface_t *surface;
1268 ev_document_fc_mutex_lock ();
1269 surface = pdf_page_render (poppler_page, width, height, rc);
1270 ev_document_fc_mutex_unlock ();
1272 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1273 cairo_surface_destroy (surface);
1274 #endif /* POPPLER_WITH_GDK */
1280 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1281 EvRenderContext *rc,
1284 PdfDocument *pdf_document;
1285 PopplerPage *poppler_page;
1286 GdkPixbuf *pixbuf = NULL;
1287 GdkPixbuf *border_pixbuf;
1289 pdf_document = PDF_DOCUMENT (document_thumbnails);
1291 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1292 g_return_val_if_fail (poppler_page != NULL, NULL);
1294 #ifdef POPPLER_WITH_GDK
1295 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1297 cairo_surface_t *surface;
1299 surface = poppler_page_get_thumbnail (poppler_page);
1301 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1302 cairo_surface_destroy (surface);
1304 #endif /* POPPLER_WITH_GDK */
1308 /* Rotate provided thumbnail if needed */
1309 GdkPixbuf *rotated_pixbuf;
1311 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1312 (GdkPixbufRotation) (360 - rc->rotation));
1313 g_object_unref (pixbuf);
1314 pixbuf = rotated_pixbuf;
1316 /* There is no provided thumbnail. We need to make one. */
1317 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1321 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1322 g_object_unref (pixbuf);
1323 pixbuf = border_pixbuf;
1326 g_object_unref (poppler_page);
1332 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1333 EvRenderContext *rc,
1337 PdfDocument *pdf_document;
1338 PopplerPage *poppler_page;
1341 pdf_document = PDF_DOCUMENT (document_thumbnails);
1342 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1344 g_return_if_fail (poppler_page != NULL);
1346 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1349 double page_width, page_height;
1351 poppler_page_get_size (poppler_page, &page_width, &page_height);
1353 *width = (gint) MAX (page_width * rc->scale, 1);
1354 *height = (gint) MAX (page_height * rc->scale, 1);
1357 if (rc->rotation == 90 || rc->rotation == 270) {
1365 g_object_unref (poppler_page);
1369 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1371 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1372 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1377 pdf_document_search_idle_callback (void *data)
1379 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1380 PdfDocument *pdf_document = search->document;
1385 page = poppler_document_get_page (search->document->document,
1386 search->search_page);
1388 ev_document_doc_mutex_lock ();
1389 matches = poppler_page_find_text (page, search->text);
1390 ev_document_doc_mutex_unlock ();
1392 g_object_unref (page);
1394 search->pages[search->search_page] = matches;
1395 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1396 search->search_page);
1398 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1399 search->search_page += 1;
1400 if (search->search_page == n_pages) {
1402 search->search_page = 0;
1405 if (search->search_page != search->start_page) {
1410 search->idle = 0; /* will return FALSE to remove */
1415 static PdfDocumentSearch *
1416 pdf_document_search_new (PdfDocument *pdf_document,
1420 PdfDocumentSearch *search;
1424 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1426 search = g_new0 (PdfDocumentSearch, 1);
1428 search->text = g_strdup (text);
1429 search->pages = g_new0 (GList *, n_pages);
1430 search->document = pdf_document;
1432 /* We add at low priority so the progress bar repaints */
1433 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1434 pdf_document_search_idle_callback,
1438 search->start_page = start_page;
1439 search->search_page = start_page;
1445 pdf_document_find_begin (EvDocumentFind *document,
1447 const char *search_string,
1448 gboolean case_sensitive)
1450 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1452 /* FIXME handle case_sensitive (right now XPDF
1453 * code is always case insensitive for ASCII
1454 * and case sensitive for all other languaages)
1457 if (pdf_document->search &&
1458 strcmp (search_string, pdf_document->search->text) == 0)
1461 if (pdf_document->search)
1462 pdf_document_search_free (pdf_document->search);
1464 pdf_document->search = pdf_document_search_new (pdf_document,
1470 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1472 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1475 return g_list_length (search->pages[page]);
1482 pdf_document_find_get_result (EvDocumentFind *document_find,
1485 EvRectangle *rectangle)
1487 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1488 PdfDocumentSearch *search = pdf_document->search;
1489 PopplerPage *poppler_page;
1490 PopplerRectangle *r;
1496 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1501 poppler_page = poppler_document_get_page (pdf_document->document, page);
1502 poppler_page_get_size (poppler_page, NULL, &height);
1503 rectangle->x1 = r->x1;
1504 rectangle->y1 = height - r->y2;
1505 rectangle->x2 = r->x2;
1506 rectangle->y2 = height - r->y1;
1507 g_object_unref (poppler_page);
1513 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1516 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1518 return search && search->pages[page] != NULL;
1522 pdf_document_find_get_progress (EvDocumentFind *document_find)
1524 PdfDocumentSearch *search;
1525 int n_pages, pages_done;
1527 search = PDF_DOCUMENT (document_find)->search;
1529 if (search == NULL) {
1533 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1534 if (search->search_page > search->start_page) {
1535 pages_done = search->search_page - search->start_page + 1;
1536 } else if (search->search_page == search->start_page) {
1537 pages_done = n_pages;
1539 pages_done = n_pages - search->start_page + search->search_page;
1542 return pages_done / (double) n_pages;
1546 pdf_document_find_cancel (EvDocumentFind *document)
1548 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1550 if (pdf_document->search) {
1551 pdf_document_search_free (pdf_document->search);
1552 pdf_document->search = NULL;
1557 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1559 iface->begin = pdf_document_find_begin;
1560 iface->get_n_results = pdf_document_find_get_n_results;
1561 iface->get_result = pdf_document_find_get_result;
1562 iface->page_has_results = pdf_document_find_page_has_results;
1563 iface->get_progress = pdf_document_find_get_progress;
1564 iface->cancel = pdf_document_find_cancel;
1568 pdf_print_context_free (PdfPrintContext *ctx)
1573 #ifdef HAVE_CAIRO_PRINT
1575 cairo_destroy (ctx->cr);
1580 poppler_ps_file_free (ctx->ps_file);
1581 ctx->ps_file = NULL;
1588 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1589 EvFileExporterContext *fc)
1591 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1592 PdfPrintContext *ctx;
1593 #ifdef HAVE_CAIRO_PRINT
1594 gdouble width, height;
1595 cairo_surface_t *surface = NULL;
1598 if (pdf_document->print_ctx)
1599 pdf_print_context_free (pdf_document->print_ctx);
1600 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1601 ctx = pdf_document->print_ctx;
1602 ctx->format = fc->format;
1604 #ifdef HAVE_CAIRO_PRINT
1605 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1607 ctx->paper_width = fc->paper_width;
1608 ctx->paper_height = fc->paper_height;
1610 switch (fc->pages_per_sheet) {
1638 ctx->pages_printed = 0;
1640 switch (fc->format) {
1641 case EV_FILE_FORMAT_PS:
1642 #ifdef HAVE_CAIRO_PS
1643 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1646 case EV_FILE_FORMAT_PDF:
1647 #ifdef HAVE_CAIRO_PDF
1648 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1652 g_assert_not_reached ();
1655 ctx->cr = cairo_create (surface);
1656 cairo_surface_destroy (surface);
1658 #else /* HAVE_CAIRO_PRINT */
1659 if (ctx->format == EV_FILE_FORMAT_PS) {
1660 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1661 fc->filename, fc->first_page,
1662 fc->last_page - fc->first_page + 1);
1663 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1664 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1666 #endif /* HAVE_CAIRO_PRINT */
1670 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1672 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1673 PdfPrintContext *ctx = pdf_document->print_ctx;
1675 g_return_if_fail (pdf_document->print_ctx != NULL);
1677 ctx->pages_printed = 0;
1679 #ifdef HAVE_CAIRO_PRINT
1680 if (ctx->paper_width > ctx->paper_height) {
1681 if (ctx->format == EV_FILE_FORMAT_PS) {
1682 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1685 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1686 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1691 #endif /* HAVE_CAIRO_PRINT */
1695 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1696 EvRenderContext *rc)
1698 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1699 PdfPrintContext *ctx = pdf_document->print_ctx;
1700 PopplerPage *poppler_page;
1701 #ifdef HAVE_CAIRO_PRINT
1702 gdouble page_width, page_height;
1705 gdouble width, height;
1706 gdouble pwidth, pheight;
1707 gdouble xscale, yscale;
1710 g_return_if_fail (pdf_document->print_ctx != NULL);
1712 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1714 #ifdef HAVE_CAIRO_PRINT
1715 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1716 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1717 poppler_page_get_size (poppler_page, &page_width, &page_height);
1719 if (page_width > page_height && page_width > ctx->paper_width) {
1725 /* Use always portrait mode and rotate when necessary */
1726 if (ctx->paper_width > ctx->paper_height) {
1727 width = ctx->paper_height;
1728 height = ctx->paper_width;
1731 width = ctx->paper_width;
1732 height = ctx->paper_height;
1735 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1748 page_width = page_height;
1752 pwidth = width / ctx->pages_x;
1753 pheight = height / ctx->pages_y;
1755 if ((page_width > pwidth || page_height > pheight) ||
1756 (page_width < pwidth && page_height < pheight)) {
1757 xscale = pwidth / page_width;
1758 yscale = pheight / page_height;
1760 if (yscale < xscale) {
1767 xscale = yscale = 1;
1772 cairo_save (ctx->cr);
1774 cairo_matrix_t matrix;
1776 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1777 cairo_matrix_init (&matrix,
1781 cairo_transform (ctx->cr, &matrix);
1784 cairo_translate (ctx->cr,
1785 x * (rotate ? pheight : pwidth),
1786 y * (rotate ? pwidth : pheight));
1787 cairo_scale (ctx->cr, xscale, yscale);
1789 poppler_page_render_for_printing (poppler_page, ctx->cr);
1791 ctx->pages_printed++;
1793 cairo_restore (ctx->cr);
1794 #else /* HAVE_CAIRO_PRINT */
1795 if (ctx->format == EV_FILE_FORMAT_PS)
1796 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1797 #endif /* HAVE_CAIRO_PRINT */
1799 g_object_unref (poppler_page);
1803 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1805 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1806 PdfPrintContext *ctx = pdf_document->print_ctx;
1808 g_return_if_fail (pdf_document->print_ctx != NULL);
1810 #ifdef HAVE_CAIRO_PRINT
1811 cairo_show_page (ctx->cr);
1816 pdf_document_file_exporter_end (EvFileExporter *exporter)
1818 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1820 pdf_print_context_free (pdf_document->print_ctx);
1821 pdf_document->print_ctx = NULL;
1824 static EvFileExporterCapabilities
1825 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1827 return (EvFileExporterCapabilities) (
1828 EV_FILE_EXPORTER_CAN_PAGE_SET |
1829 EV_FILE_EXPORTER_CAN_COPIES |
1830 EV_FILE_EXPORTER_CAN_COLLATE |
1831 EV_FILE_EXPORTER_CAN_REVERSE |
1832 EV_FILE_EXPORTER_CAN_SCALE |
1833 #ifdef HAVE_CAIRO_PRINT
1834 #ifdef HAVE_POPPLER_PAGE_RENDER
1835 #if GTK_CHECK_VERSION (2, 11, 1)
1836 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1841 #ifdef HAVE_CAIRO_PDF
1842 #ifdef HAVE_POPPLER_PAGE_RENDER
1843 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1846 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1850 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1852 iface->begin = pdf_document_file_exporter_begin;
1853 iface->begin_page = pdf_document_file_exporter_begin_page;
1854 iface->do_page = pdf_document_file_exporter_do_page;
1855 iface->end_page = pdf_document_file_exporter_end_page;
1856 iface->end = pdf_document_file_exporter_end;
1857 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1861 pdf_selection_render_selection (EvSelection *selection,
1862 EvRenderContext *rc,
1863 cairo_surface_t **surface,
1864 EvRectangle *points,
1865 EvRectangle *old_points,
1866 EvSelectionStyle style,
1870 PdfDocument *pdf_document;
1871 double width_points, height_points;
1874 pdf_document = PDF_DOCUMENT (selection);
1875 set_rc_data (pdf_document, rc);
1877 poppler_page_get_size (POPPLER_PAGE (rc->data),
1878 &width_points, &height_points);
1879 width = (int) ((width_points * rc->scale) + 0.5);
1880 height = (int) ((height_points * rc->scale) + 0.5);
1882 #ifdef HAVE_POPPLER_PAGE_RENDER
1884 PopplerColor text_color, base_color;
1886 text_color.red = text->red;
1887 text_color.green = text->green;
1888 text_color.blue = text->blue;
1890 base_color.red = base->red;
1891 base_color.green = base->green;
1892 base_color.blue = base->blue;
1894 if (*surface == NULL) {
1895 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1900 cr = cairo_create (*surface);
1901 cairo_scale (cr, rc->scale, rc->scale);
1902 cairo_surface_set_device_offset (*surface, 0, 0);
1903 memset (cairo_image_surface_get_data (*surface), 0x00,
1904 cairo_image_surface_get_height (*surface) *
1905 cairo_image_surface_get_stride (*surface));
1906 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1908 (PopplerRectangle *)points,
1909 (PopplerRectangle *)old_points,
1910 (PopplerSelectionStyle)style,
1914 #else /* HAVE_POPPLER_PAGE_RENDER */
1917 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1921 poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1922 rc->scale, rc->rotation, pixbuf,
1923 (PopplerRectangle *)points,
1924 (PopplerRectangle *)old_points,
1925 (PopplerSelectionStyle)style,
1929 cairo_surface_destroy (*surface);
1930 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1931 g_object_unref (pixbuf);
1932 #endif /* HAVE_POPPLER_PAGE_RENDER */
1936 pdf_selection_get_selected_text (EvSelection *selection,
1937 EvRenderContext *rc,
1938 EvSelectionStyle style,
1939 EvRectangle *points)
1941 PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1942 PopplerPage *poppler_page;
1947 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1948 g_return_val_if_fail (poppler_page != NULL, NULL);
1950 poppler_page_get_size (poppler_page, NULL, &height);
1952 r.y1 = height - points->y2;
1954 r.y2 = height - points->y1;
1956 retval = poppler_page_get_text (poppler_page,
1957 (PopplerSelectionStyle)style,
1960 g_object_unref (poppler_page);
1966 create_gdk_region_from_poppler_region (GList *region)
1971 retval = gdk_region_new ();
1973 for (l = region; l; l = g_list_next (l)) {
1974 PopplerRectangle *rectangle;
1977 rectangle = (PopplerRectangle *)l->data;
1979 rect.x = (gint) rectangle->x1;
1980 rect.y = (gint) rectangle->y1;
1981 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1982 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1983 gdk_region_union_with_rect (retval, &rect);
1985 poppler_rectangle_free (rectangle);
1992 pdf_selection_get_selection_region (EvSelection *selection,
1993 EvRenderContext *rc,
1994 EvSelectionStyle style,
1995 EvRectangle *points)
1997 PdfDocument *pdf_document;
2001 pdf_document = PDF_DOCUMENT (selection);
2003 set_rc_data (pdf_document, rc);
2005 region = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2007 (PopplerSelectionStyle)style,
2008 (PopplerRectangle *) points);
2009 retval = create_gdk_region_from_poppler_region (region);
2010 g_list_free (region);
2016 pdf_selection_get_selection_map (EvSelection *selection,
2017 EvRenderContext *rc)
2019 PdfDocument *pdf_document;
2020 PopplerPage *poppler_page;
2021 PopplerRectangle points;
2025 pdf_document = PDF_DOCUMENT (selection);
2026 poppler_page = poppler_document_get_page (pdf_document->document,
2031 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2033 region = poppler_page_get_selection_region (poppler_page, 1.0,
2034 POPPLER_SELECTION_GLYPH,
2036 retval = create_gdk_region_from_poppler_region (region);
2037 g_list_free (region);
2038 g_object_unref (poppler_page);
2044 pdf_selection_iface_init (EvSelectionIface *iface)
2046 iface->render_selection = pdf_selection_render_selection;
2047 iface->get_selected_text = pdf_selection_get_selected_text;
2048 iface->get_selection_region = pdf_selection_get_selection_region;
2049 iface->get_selection_map = pdf_selection_get_selection_map;
2052 /* Page Transitions */
2054 pdf_document_get_page_duration (EvDocumentTransition *trans,
2057 PdfDocument *pdf_document;
2058 PopplerPage *poppler_page;
2059 gdouble duration = -1;
2061 pdf_document = PDF_DOCUMENT (trans);
2062 poppler_page = poppler_document_get_page (pdf_document->document, page);
2066 duration = poppler_page_get_duration (poppler_page);
2067 g_object_unref (poppler_page);
2072 static EvTransitionEffect *
2073 pdf_document_get_effect (EvDocumentTransition *trans,
2076 PdfDocument *pdf_document;
2077 PopplerPage *poppler_page;
2078 PopplerPageTransition *page_transition;
2079 EvTransitionEffect *effect;
2081 pdf_document = PDF_DOCUMENT (trans);
2082 poppler_page = poppler_document_get_page (pdf_document->document, page);
2087 page_transition = poppler_page_get_transition (poppler_page);
2089 if (!page_transition) {
2090 g_object_unref (poppler_page);
2094 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2095 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2096 "alignment", page_transition->alignment,
2097 "direction", page_transition->direction,
2098 "duration", page_transition->duration,
2099 "angle", page_transition->angle,
2100 "scale", page_transition->scale,
2101 "rectangular", page_transition->rectangular,
2104 poppler_page_transition_free (page_transition);
2105 g_object_unref (poppler_page);
2111 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2113 iface->get_page_duration = pdf_document_get_page_duration;
2114 iface->get_effect = pdf_document_get_effect;
2119 pdf_document_get_crop_box (EvDocument *document,
2123 PdfDocument *pdf_document;
2124 PopplerPage *poppler_page;
2125 PopplerRectangle poppler_rect;
2127 pdf_document = PDF_DOCUMENT (document);
2128 poppler_page = poppler_document_get_page (pdf_document->document, page);
2129 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2130 rect->x1 = poppler_rect.x1;
2131 rect->x2 = poppler_rect.x2;
2132 rect->y1 = poppler_rect.y1;
2133 rect->y2 = poppler_rect.y2;
2136 static EvFormField *
2137 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2139 EvFormField *ev_field = NULL;
2142 gboolean is_read_only;
2144 id = poppler_form_field_get_id (poppler_field);
2145 font_size = poppler_form_field_get_font_size (poppler_field);
2146 is_read_only = poppler_form_field_is_read_only (poppler_field);
2148 switch (poppler_form_field_get_field_type (poppler_field)) {
2149 case POPPLER_FORM_FIELD_TEXT: {
2150 EvFormFieldText *field_text;
2151 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2153 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2154 case POPPLER_FORM_TEXT_NORMAL:
2155 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2157 case POPPLER_FORM_TEXT_MULTILINE:
2158 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2160 case POPPLER_FORM_TEXT_FILE_SELECT:
2161 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2165 ev_field = ev_form_field_text_new (id, ev_text_type);
2166 field_text = EV_FORM_FIELD_TEXT (ev_field);
2168 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2169 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2170 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2171 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2172 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2173 field_text->text = poppler_form_field_text_get_text (poppler_field);
2177 case POPPLER_FORM_FIELD_BUTTON: {
2178 EvFormFieldButton *field_button;
2179 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2181 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2182 case POPPLER_FORM_BUTTON_PUSH:
2183 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2185 case POPPLER_FORM_BUTTON_CHECK:
2186 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2188 case POPPLER_FORM_BUTTON_RADIO:
2189 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2193 ev_field = ev_form_field_button_new (id, ev_button_type);
2194 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2196 field_button->state = poppler_form_field_button_get_state (poppler_field);
2199 case POPPLER_FORM_FIELD_CHOICE: {
2200 EvFormFieldChoice *field_choice;
2201 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2203 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2204 case POPPLER_FORM_CHOICE_COMBO:
2205 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2207 case EV_FORM_FIELD_CHOICE_LIST:
2208 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2212 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2213 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2215 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2216 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2217 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2218 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2220 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2221 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2222 if (field_choice->is_editable)
2223 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2226 case POPPLER_FORM_FIELD_SIGNATURE:
2228 ev_field = ev_form_field_signature_new (id);
2230 case POPPLER_FORM_FIELD_UNKNOWN:
2234 ev_field->font_size = font_size;
2235 ev_field->is_read_only = is_read_only;
2241 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2244 PdfDocument *pdf_document;
2245 PopplerPage *poppler_page;
2246 GList *retval = NULL;
2251 pdf_document = PDF_DOCUMENT (document);
2252 poppler_page = poppler_document_get_page (pdf_document->document, page);
2253 fields = poppler_page_get_form_field_mapping (poppler_page);
2254 poppler_page_get_size (poppler_page, NULL, &height);
2256 for (list = fields; list; list = list->next) {
2257 PopplerFormFieldMapping *mapping;
2258 EvFormFieldMapping *field_mapping;
2259 EvFormField *ev_field;
2261 mapping = (PopplerFormFieldMapping *)list->data;
2263 ev_field = ev_form_field_from_poppler_field (mapping->field);
2267 field_mapping = g_new0 (EvFormFieldMapping, 1);
2268 field_mapping->x1 = mapping->area.x1;
2269 field_mapping->x2 = mapping->area.x2;
2270 field_mapping->y1 = height - mapping->area.y2;
2271 field_mapping->y2 = height - mapping->area.y1;
2272 field_mapping->field = ev_field;
2273 field_mapping->field->page = page;
2275 retval = g_list_prepend (retval, field_mapping);
2278 poppler_page_free_form_field_mapping (fields);
2279 g_object_unref (poppler_page);
2281 return g_list_reverse (retval);
2285 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2289 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2290 PopplerFormField *poppler_field;
2293 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2297 text = poppler_form_field_text_get_text (poppler_field);
2298 g_object_unref (poppler_field);
2304 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2308 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2309 PopplerFormField *poppler_field;
2311 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2314 poppler_form_field_text_set_text (poppler_field, text);
2315 g_object_unref (poppler_field);
2319 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2323 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2324 PopplerFormField *poppler_field;
2326 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2330 poppler_form_field_button_set_state (poppler_field, state);
2331 g_object_unref (poppler_field);
2335 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2338 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2339 PopplerFormField *poppler_field;
2342 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2346 state = poppler_form_field_button_get_state (poppler_field);
2347 g_object_unref (poppler_field);
2353 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2357 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2358 PopplerFormField *poppler_field;
2361 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2365 text = poppler_form_field_choice_get_item (poppler_field, index);
2366 g_object_unref (poppler_field);
2372 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2375 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2376 PopplerFormField *poppler_field;
2379 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2383 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2384 g_object_unref (poppler_field);
2390 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2394 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2395 PopplerFormField *poppler_field;
2398 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2402 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2403 g_object_unref (poppler_field);
2409 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2413 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2414 PopplerFormField *poppler_field;
2416 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2420 poppler_form_field_choice_select_item (poppler_field, index);
2421 g_object_unref (poppler_field);
2425 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2429 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2430 PopplerFormField *poppler_field;
2432 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2436 poppler_form_field_choice_toggle_item (poppler_field, index);
2437 g_object_unref (poppler_field);
2441 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2444 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2445 PopplerFormField *poppler_field;
2447 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2451 poppler_form_field_choice_unselect_all (poppler_field);
2452 g_object_unref (poppler_field);
2456 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2460 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2461 PopplerFormField *poppler_field;
2463 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2467 poppler_form_field_choice_set_text (poppler_field, text);
2468 g_object_unref (poppler_field);
2472 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2475 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2476 PopplerFormField *poppler_field;
2479 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2483 text = poppler_form_field_choice_get_text (poppler_field);
2484 g_object_unref (poppler_field);
2490 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2492 iface->get_form_fields = pdf_document_forms_get_form_fields;
2493 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2494 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2495 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2496 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2497 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2498 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2499 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2500 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2501 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2502 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2503 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2504 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;