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
56 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
57 #define POPPLER_HAS_GDK
59 #ifdef POPPLER_WITH_GDK
60 #define POPPLER_HAS_GDK
65 PdfDocument *document;
74 EvFileExporterFormat format;
84 #ifdef HAVE_CAIRO_PRINT
87 PopplerPSFile *ps_file;
91 struct _PdfDocumentClass
93 GObjectClass parent_class;
98 GObject parent_instance;
100 PopplerDocument *document;
103 PopplerFontInfo *font_info;
104 PopplerFontsIter *fonts_iter;
105 int fonts_scanned_pages;
107 PdfDocumentSearch *search;
108 PdfPrintContext *print_ctx;
111 static void pdf_document_document_iface_init (EvDocumentIface *iface);
112 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
113 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
114 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
115 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
116 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
117 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
118 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
119 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
120 static void pdf_selection_iface_init (EvSelectionIface *iface);
121 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
122 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
126 static int pdf_document_get_n_pages (EvDocument *document);
128 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
130 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
131 PopplerAction *action);
132 static void pdf_document_search_free (PdfDocumentSearch *search);
133 static void pdf_print_context_free (PdfPrintContext *ctx);
135 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
137 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
138 pdf_document_security_iface_init);
139 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
140 pdf_document_document_thumbnails_iface_init);
141 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
142 pdf_document_document_links_iface_init);
143 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
144 pdf_document_document_images_iface_init);
145 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
146 pdf_document_document_forms_iface_init);
147 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
148 pdf_document_document_fonts_iface_init);
149 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
150 pdf_document_find_iface_init);
151 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
152 pdf_document_file_exporter_iface_init);
153 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
154 pdf_selection_iface_init);
155 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
156 pdf_document_page_transition_iface_init);
160 set_rc_data (PdfDocument *pdf_document,
163 if (rc->data == NULL) {
164 rc->data = poppler_document_get_page (pdf_document->document,
166 rc->destroy = g_object_unref;
168 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
173 pdf_document_search_free (PdfDocumentSearch *search)
175 PdfDocument *pdf_document = search->document;
179 if (search->idle != 0)
180 g_source_remove (search->idle);
182 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
183 for (i = 0; i < n_pages; i++) {
184 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
185 g_list_free (search->pages[i]);
187 g_free (search->pages);
189 g_free (search->text);
194 pdf_document_dispose (GObject *object)
196 PdfDocument *pdf_document = PDF_DOCUMENT(object);
198 if (pdf_document->print_ctx) {
199 pdf_print_context_free (pdf_document->print_ctx);
200 pdf_document->print_ctx = NULL;
203 if (pdf_document->search) {
204 pdf_document_search_free (pdf_document->search);
205 pdf_document->search = NULL;
208 if (pdf_document->document) {
209 g_object_unref (pdf_document->document);
212 if (pdf_document->font_info) {
213 poppler_font_info_free (pdf_document->font_info);
216 if (pdf_document->fonts_iter) {
217 poppler_fonts_iter_free (pdf_document->fonts_iter);
220 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
224 pdf_document_class_init (PdfDocumentClass *klass)
226 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
228 g_object_class->dispose = pdf_document_dispose;
232 pdf_document_init (PdfDocument *pdf_document)
234 pdf_document->password = NULL;
238 convert_error (GError *poppler_error,
241 if (poppler_error == NULL)
244 if (poppler_error->domain == POPPLER_ERROR) {
245 /* convert poppler errors into EvDocument errors */
246 gint code = EV_DOCUMENT_ERROR_INVALID;
247 if (poppler_error->code == POPPLER_ERROR_INVALID)
248 code = EV_DOCUMENT_ERROR_INVALID;
249 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
250 code = EV_DOCUMENT_ERROR_ENCRYPTED;
256 poppler_error->message,
259 g_propagate_error (error, poppler_error);
266 pdf_document_save (EvDocument *document,
271 GError *poppler_error = NULL;
273 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
277 convert_error (poppler_error, error);
283 pdf_document_load (EvDocument *document,
287 GError *poppler_error = NULL;
288 PdfDocument *pdf_document = PDF_DOCUMENT (document);
290 pdf_document->document =
291 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
293 if (pdf_document->document == NULL) {
294 convert_error (poppler_error, error);
302 pdf_document_get_n_pages (EvDocument *document)
304 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
308 pdf_document_get_page_size (EvDocument *document,
313 PdfDocument *pdf_document = PDF_DOCUMENT (document);
314 PopplerPage *poppler_page;
316 poppler_page = poppler_document_get_page (pdf_document->document, page);
317 poppler_page_get_size (poppler_page, width, height);
318 g_object_unref (poppler_page);
322 pdf_document_get_page_label (EvDocument *document,
325 PopplerPage *poppler_page;
328 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
331 g_object_get (G_OBJECT (poppler_page),
334 g_object_unref (poppler_page);
340 pdf_document_has_attachments (EvDocument *document)
342 PdfDocument *pdf_document;
344 pdf_document = PDF_DOCUMENT (document);
346 return poppler_document_has_attachments (pdf_document->document);
349 struct SaveToBufferData {
355 attachment_save_to_buffer_callback (const gchar *buf,
360 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
364 if (sdata->len + count > sdata->max) {
365 new_max = MAX (sdata->max * 2, sdata->len + count);
366 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
368 sdata->buffer = new_buffer;
369 sdata->max = new_max;
372 memcpy (sdata->buffer + sdata->len, buf, count);
379 attachment_save_to_buffer (PopplerAttachment *attachment,
384 static const gint initial_max = 1024;
385 struct SaveToBufferData sdata;
390 sdata.buffer = (gchar *) g_malloc (initial_max);
391 sdata.max = initial_max;
394 if (! poppler_attachment_save_to_callback (attachment,
395 attachment_save_to_buffer_callback,
398 g_free (sdata.buffer);
402 *buffer = sdata.buffer;
403 *buffer_size = sdata.len;
409 pdf_document_get_attachments (EvDocument *document)
411 PdfDocument *pdf_document;
414 GList *retval = NULL;
416 pdf_document = PDF_DOCUMENT (document);
418 if (!pdf_document_has_attachments (document))
421 attachments = poppler_document_get_attachments (pdf_document->document);
423 for (list = attachments; list; list = list->next) {
424 PopplerAttachment *attachment;
425 EvAttachment *ev_attachment;
428 GError *error = NULL;
430 attachment = (PopplerAttachment *) list->data;
432 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
433 ev_attachment = ev_attachment_new (attachment->name,
434 attachment->description,
439 retval = g_list_prepend (retval, ev_attachment);
442 g_warning ("%s", error->message);
443 g_error_free (error);
449 g_object_unref (attachment);
452 return g_list_reverse (retval);
455 static cairo_surface_t *
456 pdf_page_render (PopplerPage *page,
461 cairo_surface_t *surface;
463 #ifdef HAVE_POPPLER_PAGE_RENDER
466 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
468 memset (cairo_image_surface_get_data (surface), 0xff,
469 cairo_image_surface_get_height (surface) *
470 cairo_image_surface_get_stride (surface));
472 cr = cairo_create (surface);
473 switch (rc->rotation) {
475 cairo_translate (cr, width, 0);
478 cairo_translate (cr, width, height);
481 cairo_translate (cr, 0, height);
484 cairo_translate (cr, 0, 0);
486 cairo_scale (cr, rc->scale, rc->scale);
487 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
488 poppler_page_render (page, cr);
490 #else /* HAVE_POPPLER_PAGE_RENDER */
493 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
497 poppler_page_render_to_pixbuf (page,
503 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
504 g_object_unref (pixbuf);
505 #endif /* HAVE_POPPLER_PAGE_RENDER */
510 static cairo_surface_t *
511 pdf_document_render (EvDocument *document,
514 PdfDocument *pdf_document;
515 double width_points, height_points;
518 pdf_document = PDF_DOCUMENT (document);
520 set_rc_data (pdf_document, rc);
522 poppler_page_get_size (POPPLER_PAGE (rc->data),
523 &width_points, &height_points);
525 if (rc->rotation == 90 || rc->rotation == 270) {
526 width = (int) ((height_points * rc->scale) + 0.5);
527 height = (int) ((width_points * rc->scale) + 0.5);
529 width = (int) ((width_points * rc->scale) + 0.5);
530 height = (int) ((height_points * rc->scale) + 0.5);
533 return pdf_page_render (POPPLER_PAGE (rc->data),
537 /* EvDocumentSecurity */
540 pdf_document_has_document_security (EvDocumentSecurity *document_security)
542 /* FIXME: do we really need to have this? */
547 pdf_document_set_password (EvDocumentSecurity *document_security,
548 const char *password)
550 PdfDocument *document = PDF_DOCUMENT (document_security);
552 if (document->password)
553 g_free (document->password);
555 document->password = g_strdup (password);
558 static EvDocumentInfo *
559 pdf_document_get_info (EvDocument *document)
561 EvDocumentInfo *info;
562 PopplerPageLayout layout;
563 PopplerPageMode mode;
564 PopplerViewerPreferences view_prefs;
565 PopplerPermissions permissions;
567 info = g_new0 (EvDocumentInfo, 1);
569 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
570 EV_DOCUMENT_INFO_FORMAT |
571 EV_DOCUMENT_INFO_AUTHOR |
572 EV_DOCUMENT_INFO_SUBJECT |
573 EV_DOCUMENT_INFO_KEYWORDS |
574 EV_DOCUMENT_INFO_LAYOUT |
575 EV_DOCUMENT_INFO_START_MODE |
576 EV_DOCUMENT_INFO_PERMISSIONS |
577 EV_DOCUMENT_INFO_UI_HINTS |
578 EV_DOCUMENT_INFO_CREATOR |
579 EV_DOCUMENT_INFO_PRODUCER |
580 EV_DOCUMENT_INFO_CREATION_DATE |
581 EV_DOCUMENT_INFO_MOD_DATE |
582 EV_DOCUMENT_INFO_LINEARIZED |
583 EV_DOCUMENT_INFO_N_PAGES |
584 EV_DOCUMENT_INFO_SECURITY |
585 EV_DOCUMENT_INFO_PAPER_SIZE;
587 g_object_get (PDF_DOCUMENT (document)->document,
588 "title", &(info->title),
589 "format", &(info->format),
590 "author", &(info->author),
591 "subject", &(info->subject),
592 "keywords", &(info->keywords),
594 "page-layout", &layout,
595 "viewer-preferences", &view_prefs,
596 "permissions", &permissions,
597 "creator", &(info->creator),
598 "producer", &(info->producer),
599 "creation-date", &(info->creation_date),
600 "mod-date", &(info->modified_date),
601 "linearized", &(info->linearized),
604 pdf_document_get_page_size(document, 0,
605 &(info->paper_width),
606 &(info->paper_height));
609 info->paper_width = info->paper_width / 72.0f * 25.4f;
610 info->paper_height = info->paper_height / 72.0f * 25.4f;
613 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
614 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
616 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
617 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
619 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
620 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
622 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
623 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
624 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
625 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
627 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
628 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
635 case POPPLER_PAGE_MODE_NONE:
636 info->mode = EV_DOCUMENT_MODE_NONE;
638 case POPPLER_PAGE_MODE_USE_THUMBS:
639 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
641 case POPPLER_PAGE_MODE_USE_OC:
642 info->mode = EV_DOCUMENT_MODE_USE_OC;
644 case POPPLER_PAGE_MODE_FULL_SCREEN:
645 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
647 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
648 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
654 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
655 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
657 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
658 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
660 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
661 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
663 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
664 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
666 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
667 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
669 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
670 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
672 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
673 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
676 info->permissions = 0;
677 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
678 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
680 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
681 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
683 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
684 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
686 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
687 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
690 info->n_pages = ev_document_get_n_pages (document);
692 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
693 /* translators: this is the document security state */
694 info->security = g_strdup (_("Yes"));
696 /* translators: this is the document security state */
697 info->security = g_strdup (_("No"));
704 pdf_document_document_iface_init (EvDocumentIface *iface)
706 iface->save = pdf_document_save;
707 iface->load = pdf_document_load;
708 iface->get_n_pages = pdf_document_get_n_pages;
709 iface->get_page_size = pdf_document_get_page_size;
710 iface->get_page_label = pdf_document_get_page_label;
711 iface->has_attachments = pdf_document_has_attachments;
712 iface->get_attachments = pdf_document_get_attachments;
713 iface->render = pdf_document_render;
714 iface->get_info = pdf_document_get_info;
718 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
720 iface->has_document_security = pdf_document_has_document_security;
721 iface->set_password = pdf_document_set_password;
725 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
727 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
730 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
732 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
736 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
739 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
742 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
744 if (pdf_document->font_info == NULL) {
745 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
748 if (pdf_document->fonts_iter) {
749 poppler_fonts_iter_free (pdf_document->fonts_iter);
752 pdf_document->fonts_scanned_pages += n_pages;
754 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
755 &pdf_document->fonts_iter);
757 pdf_document->fonts_scanned_pages = 0;
758 poppler_font_info_free (pdf_document->font_info);
759 pdf_document->font_info = NULL;
766 font_type_to_string (PopplerFontType type)
769 case POPPLER_FONT_TYPE_TYPE1:
771 case POPPLER_FONT_TYPE_TYPE1C:
773 case POPPLER_FONT_TYPE_TYPE3:
775 case POPPLER_FONT_TYPE_TRUETYPE:
776 return _("TrueType");
777 case POPPLER_FONT_TYPE_CID_TYPE0:
778 return _("Type 1 (CID)");
779 case POPPLER_FONT_TYPE_CID_TYPE0C:
780 return _("Type 1C (CID)");
781 case POPPLER_FONT_TYPE_CID_TYPE2:
782 return _("TrueType (CID)");
784 return _("Unknown font type");
789 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
792 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
793 PopplerFontsIter *iter = pdf_document->fonts_iter;
795 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
801 GtkTreeIter list_iter;
804 const char *embedded;
807 name = poppler_fonts_iter_get_name (iter);
813 type = font_type_to_string (
814 poppler_fonts_iter_get_font_type (iter));
816 if (poppler_fonts_iter_is_embedded (iter)) {
817 if (poppler_fonts_iter_is_subset (iter))
818 embedded = _("Embedded subset");
820 embedded = _("Embedded");
822 embedded = _("Not embedded");
825 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
827 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
828 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
829 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
830 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
834 } while (poppler_fonts_iter_next (iter));
838 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
840 iface->fill_model = pdf_document_fonts_fill_model;
841 iface->scan = pdf_document_fonts_scan;
842 iface->get_progress = pdf_document_fonts_get_progress;
846 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
848 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
849 PopplerIndexIter *iter;
851 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
853 iter = poppler_index_iter_new (pdf_document->document);
856 poppler_index_iter_free (iter);
862 ev_link_dest_from_dest (PdfDocument *pdf_document,
865 EvLinkDest *ev_dest = NULL;
866 const char *unimplemented_dest = NULL;
868 g_assert (dest != NULL);
870 switch (dest->type) {
871 case POPPLER_DEST_XYZ: {
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_xyz (dest->page_num - 1,
885 g_object_unref (poppler_page);
888 case POPPLER_DEST_FIT:
889 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
891 case POPPLER_DEST_FITH: {
892 PopplerPage *poppler_page;
895 poppler_page = poppler_document_get_page (pdf_document->document,
896 MAX (0, dest->page_num - 1));
897 poppler_page_get_size (poppler_page, NULL, &height);
898 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
901 g_object_unref (poppler_page);
904 case POPPLER_DEST_FITV:
905 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
909 case POPPLER_DEST_FITR: {
910 PopplerPage *poppler_page;
913 poppler_page = poppler_document_get_page (pdf_document->document,
914 MAX (0, dest->page_num - 1));
915 poppler_page_get_size (poppler_page, NULL, &height);
916 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
918 height - dest->bottom,
921 g_object_unref (poppler_page);
924 case POPPLER_DEST_FITB:
925 unimplemented_dest = "POPPLER_DEST_FITB";
927 case POPPLER_DEST_FITBH:
928 unimplemented_dest = "POPPLER_DEST_FITBH";
930 case POPPLER_DEST_FITBV:
931 unimplemented_dest = "POPPLER_DEST_FITBV";
933 case POPPLER_DEST_NAMED:
934 ev_dest = ev_link_dest_new_named (dest->named_dest);
936 case POPPLER_DEST_UNKNOWN:
937 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
941 if (unimplemented_dest) {
942 g_warning ("Unimplemented named action: %s, please post a "
943 "bug report in Evince bugzilla "
944 "(http://bugzilla.gnome.org) with a testcase.",
949 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
955 ev_link_from_action (PdfDocument *pdf_document,
956 PopplerAction *action)
959 EvLinkAction *ev_action = NULL;
960 const char *unimplemented_action = NULL;
962 switch (action->type) {
963 case POPPLER_ACTION_GOTO_DEST: {
966 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
967 ev_action = ev_link_action_new_dest (dest);
970 case POPPLER_ACTION_GOTO_REMOTE: {
973 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
974 ev_action = ev_link_action_new_remote (dest,
975 action->goto_remote.file_name);
979 case POPPLER_ACTION_LAUNCH:
980 ev_action = ev_link_action_new_launch (action->launch.file_name,
981 action->launch.params);
983 case POPPLER_ACTION_URI:
984 ev_action = ev_link_action_new_external_uri (action->uri.uri);
986 case POPPLER_ACTION_NAMED:
987 ev_action = ev_link_action_new_named (action->named.named_dest);
989 case POPPLER_ACTION_MOVIE:
990 unimplemented_action = "POPPLER_ACTION_MOVIE";
992 case POPPLER_ACTION_UNKNOWN:
993 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
996 if (unimplemented_action) {
997 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
998 unimplemented_action);
1001 link = ev_link_new (action->any.title, ev_action);
1007 build_tree (PdfDocument *pdf_document,
1008 GtkTreeModel *model,
1009 GtkTreeIter *parent,
1010 PopplerIndexIter *iter)
1014 GtkTreeIter tree_iter;
1015 PopplerIndexIter *child;
1016 PopplerAction *action;
1017 EvLink *link = NULL;
1021 action = poppler_index_iter_get_action (iter);
1022 expand = poppler_index_iter_is_open (iter);
1027 switch (action->type) {
1028 case POPPLER_ACTION_GOTO_DEST: {
1029 /* For bookmarks, solve named destinations */
1030 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1032 EvLinkDest *ev_dest = NULL;
1033 EvLinkAction *ev_action;
1035 dest = poppler_document_find_dest (pdf_document->document,
1036 action->goto_dest.dest->named_dest);
1038 link = ev_link_from_action (pdf_document, action);
1042 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1043 poppler_dest_free (dest);
1045 ev_action = ev_link_action_new_dest (ev_dest);
1046 link = ev_link_new (action->any.title, ev_action);
1048 link = ev_link_from_action (pdf_document, action);
1053 link = ev_link_from_action (pdf_document, action);
1057 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1058 poppler_action_free (action);
1060 g_object_unref (link);
1065 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1066 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1068 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1069 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1070 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1071 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1074 g_free (title_markup);
1075 g_object_unref (link);
1077 child = poppler_index_iter_get_child (iter);
1079 build_tree (pdf_document, model, &tree_iter, child);
1080 poppler_index_iter_free (child);
1081 poppler_action_free (action);
1083 } while (poppler_index_iter_next (iter));
1086 static GtkTreeModel *
1087 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1089 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1090 GtkTreeModel *model = NULL;
1091 PopplerIndexIter *iter;
1093 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1095 iter = poppler_index_iter_new (pdf_document->document);
1096 /* Create the model if we have items*/
1098 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1103 build_tree (pdf_document, model, NULL, iter);
1104 poppler_index_iter_free (iter);
1111 pdf_document_links_get_links (EvDocumentLinks *document_links,
1114 PdfDocument *pdf_document;
1115 PopplerPage *poppler_page;
1116 GList *retval = NULL;
1117 GList *mapping_list;
1121 pdf_document = PDF_DOCUMENT (document_links);
1122 poppler_page = poppler_document_get_page (pdf_document->document,
1124 mapping_list = poppler_page_get_link_mapping (poppler_page);
1125 poppler_page_get_size (poppler_page, NULL, &height);
1127 for (list = mapping_list; list; list = list->next) {
1128 PopplerLinkMapping *link_mapping;
1129 EvLinkMapping *ev_link_mapping;
1131 link_mapping = (PopplerLinkMapping *)list->data;
1132 ev_link_mapping = g_new (EvLinkMapping, 1);
1133 ev_link_mapping->link = ev_link_from_action (pdf_document,
1134 link_mapping->action);
1135 ev_link_mapping->x1 = link_mapping->area.x1;
1136 ev_link_mapping->x2 = link_mapping->area.x2;
1137 /* Invert this for X-style coordinates */
1138 ev_link_mapping->y1 = height - link_mapping->area.y2;
1139 ev_link_mapping->y2 = height - link_mapping->area.y1;
1141 retval = g_list_prepend (retval, ev_link_mapping);
1144 poppler_page_free_link_mapping (mapping_list);
1145 g_object_unref (poppler_page);
1147 return g_list_reverse (retval);
1151 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1152 const gchar *link_name)
1154 PdfDocument *pdf_document;
1156 EvLinkDest *ev_dest = NULL;
1158 pdf_document = PDF_DOCUMENT (document_links);
1159 dest = poppler_document_find_dest (pdf_document->document,
1162 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1163 poppler_dest_free (dest);
1170 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1172 iface->has_document_links = pdf_document_links_has_document_links;
1173 iface->get_links_model = pdf_document_links_get_links_model;
1174 iface->get_links = pdf_document_links_get_links;
1175 iface->find_link_dest = pdf_document_links_find_link_dest;
1179 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1182 GList *retval = NULL;
1183 PdfDocument *pdf_document;
1184 PopplerPage *poppler_page;
1185 GList *mapping_list;
1188 pdf_document = PDF_DOCUMENT (document_images);
1189 poppler_page = poppler_document_get_page (pdf_document->document, page);
1190 mapping_list = poppler_page_get_image_mapping (poppler_page);
1192 for (list = mapping_list; list; list = list->next) {
1193 PopplerImageMapping *image_mapping;
1194 EvImageMapping *ev_image_mapping;
1196 image_mapping = (PopplerImageMapping *)list->data;
1198 ev_image_mapping = g_new (EvImageMapping, 1);
1199 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1200 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1201 #elif POPPLER_HAS_CAIRO
1202 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1204 ev_image_mapping->x1 = image_mapping->area.x1;
1205 ev_image_mapping->x2 = image_mapping->area.x2;
1206 ev_image_mapping->y1 = image_mapping->area.y1;
1207 ev_image_mapping->y2 = image_mapping->area.y2;
1209 retval = g_list_prepend (retval, ev_image_mapping);
1212 poppler_page_free_image_mapping (mapping_list);
1213 g_object_unref (poppler_page);
1215 return g_list_reverse (retval);
1219 pdf_document_images_get_image (EvDocumentImages *document_images,
1222 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1223 PdfDocument *pdf_document;
1224 PopplerPage *poppler_page;
1225 cairo_surface_t *surface;
1226 GdkPixbuf *retval = NULL;
1228 pdf_document = PDF_DOCUMENT (document_images);
1229 poppler_page = poppler_document_get_page (pdf_document->document,
1230 ev_image_get_page (image));
1232 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1234 retval = ev_document_misc_pixbuf_from_surface (surface);
1235 cairo_surface_destroy (surface);
1238 g_object_unref (poppler_page);
1242 return GDK_PIXBUF (g_object_ref (ev_image_get_pixbuf (image)));
1243 #endif /* HAVE_POPPLER_PAGE_GET_IMAGE */
1247 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1249 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1250 iface->get_image = pdf_document_images_get_image;
1254 make_thumbnail_for_page (PdfDocument *pdf_document,
1255 PopplerPage *poppler_page,
1256 EvRenderContext *rc)
1261 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1262 rc, &width, &height);
1263 #ifdef POPPLER_HAS_GDK
1264 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1266 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1268 ev_document_fc_mutex_lock ();
1269 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1271 rc->scale, rc->rotation, pixbuf);
1272 ev_document_fc_mutex_unlock ();
1274 cairo_surface_t *surface;
1276 ev_document_fc_mutex_lock ();
1277 surface = pdf_page_render (poppler_page, width, height, rc);
1278 ev_document_fc_mutex_unlock ();
1280 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1281 cairo_surface_destroy (surface);
1282 #endif /* POPPLER_HAS_GDK */
1288 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1289 EvRenderContext *rc,
1292 PdfDocument *pdf_document;
1293 PopplerPage *poppler_page;
1294 GdkPixbuf *pixbuf = NULL;
1295 GdkPixbuf *border_pixbuf;
1297 pdf_document = PDF_DOCUMENT (document_thumbnails);
1299 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1300 g_return_val_if_fail (poppler_page != NULL, NULL);
1302 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
1303 pixbuf = poppler_page_get_thumbnail (poppler_page);
1305 #ifdef POPPLER_HAS_GDK
1306 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1308 cairo_surface_t *surface;
1310 surface = poppler_page_get_thumbnail (poppler_page);
1312 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1313 cairo_surface_destroy (surface);
1319 /* Rotate provided thumbnail if needed */
1320 GdkPixbuf *rotated_pixbuf;
1322 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1323 (GdkPixbufRotation) (360 - rc->rotation));
1324 g_object_unref (pixbuf);
1325 pixbuf = rotated_pixbuf;
1327 /* There is no provided thumbnail. We need to make one. */
1328 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1332 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1333 g_object_unref (pixbuf);
1334 pixbuf = border_pixbuf;
1337 g_object_unref (poppler_page);
1343 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1344 EvRenderContext *rc,
1348 PdfDocument *pdf_document;
1349 PopplerPage *poppler_page;
1352 pdf_document = PDF_DOCUMENT (document_thumbnails);
1353 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1355 g_return_if_fail (poppler_page != NULL);
1357 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1360 double page_width, page_height;
1362 poppler_page_get_size (poppler_page, &page_width, &page_height);
1364 *width = (gint) MAX (page_width * rc->scale, 1);
1365 *height = (gint) MAX (page_height * rc->scale, 1);
1368 if (rc->rotation == 90 || rc->rotation == 270) {
1376 g_object_unref (poppler_page);
1380 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1382 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1383 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1388 pdf_document_search_idle_callback (void *data)
1390 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1391 PdfDocument *pdf_document = search->document;
1396 page = poppler_document_get_page (search->document->document,
1397 search->search_page);
1399 ev_document_doc_mutex_lock ();
1400 matches = poppler_page_find_text (page, search->text);
1401 ev_document_doc_mutex_unlock ();
1403 g_object_unref (page);
1405 search->pages[search->search_page] = matches;
1406 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1407 search->search_page);
1409 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1410 search->search_page += 1;
1411 if (search->search_page == n_pages) {
1413 search->search_page = 0;
1416 if (search->search_page != search->start_page) {
1421 search->idle = 0; /* will return FALSE to remove */
1426 static PdfDocumentSearch *
1427 pdf_document_search_new (PdfDocument *pdf_document,
1431 PdfDocumentSearch *search;
1435 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1437 search = g_new0 (PdfDocumentSearch, 1);
1439 search->text = g_strdup (text);
1440 search->pages = g_new0 (GList *, n_pages);
1441 search->document = pdf_document;
1443 /* We add at low priority so the progress bar repaints */
1444 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1445 pdf_document_search_idle_callback,
1449 search->start_page = start_page;
1450 search->search_page = start_page;
1456 pdf_document_find_begin (EvDocumentFind *document,
1458 const char *search_string,
1459 gboolean case_sensitive)
1461 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1463 /* FIXME handle case_sensitive (right now XPDF
1464 * code is always case insensitive for ASCII
1465 * and case sensitive for all other languaages)
1468 if (pdf_document->search &&
1469 strcmp (search_string, pdf_document->search->text) == 0)
1472 if (pdf_document->search)
1473 pdf_document_search_free (pdf_document->search);
1475 pdf_document->search = pdf_document_search_new (pdf_document,
1481 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1483 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1486 return g_list_length (search->pages[page]);
1493 pdf_document_find_get_result (EvDocumentFind *document_find,
1496 EvRectangle *rectangle)
1498 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1499 PdfDocumentSearch *search = pdf_document->search;
1500 PopplerPage *poppler_page;
1501 PopplerRectangle *r;
1507 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1512 poppler_page = poppler_document_get_page (pdf_document->document, page);
1513 poppler_page_get_size (poppler_page, NULL, &height);
1514 rectangle->x1 = r->x1;
1515 rectangle->y1 = height - r->y2;
1516 rectangle->x2 = r->x2;
1517 rectangle->y2 = height - r->y1;
1518 g_object_unref (poppler_page);
1524 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1527 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1529 return search && search->pages[page] != NULL;
1533 pdf_document_find_get_progress (EvDocumentFind *document_find)
1535 PdfDocumentSearch *search;
1536 int n_pages, pages_done;
1538 search = PDF_DOCUMENT (document_find)->search;
1540 if (search == NULL) {
1544 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1545 if (search->search_page > search->start_page) {
1546 pages_done = search->search_page - search->start_page + 1;
1547 } else if (search->search_page == search->start_page) {
1548 pages_done = n_pages;
1550 pages_done = n_pages - search->start_page + search->search_page;
1553 return pages_done / (double) n_pages;
1557 pdf_document_find_cancel (EvDocumentFind *document)
1559 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1561 if (pdf_document->search) {
1562 pdf_document_search_free (pdf_document->search);
1563 pdf_document->search = NULL;
1568 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1570 iface->begin = pdf_document_find_begin;
1571 iface->get_n_results = pdf_document_find_get_n_results;
1572 iface->get_result = pdf_document_find_get_result;
1573 iface->page_has_results = pdf_document_find_page_has_results;
1574 iface->get_progress = pdf_document_find_get_progress;
1575 iface->cancel = pdf_document_find_cancel;
1579 pdf_print_context_free (PdfPrintContext *ctx)
1584 #ifdef HAVE_CAIRO_PRINT
1586 cairo_destroy (ctx->cr);
1591 poppler_ps_file_free (ctx->ps_file);
1592 ctx->ps_file = NULL;
1599 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1600 EvFileExporterContext *fc)
1602 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1603 PdfPrintContext *ctx;
1604 #ifdef HAVE_CAIRO_PRINT
1605 gdouble width, height;
1606 cairo_surface_t *surface = NULL;
1609 if (pdf_document->print_ctx)
1610 pdf_print_context_free (pdf_document->print_ctx);
1611 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1612 ctx = pdf_document->print_ctx;
1613 ctx->format = fc->format;
1615 #ifdef HAVE_CAIRO_PRINT
1616 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1618 ctx->paper_width = fc->paper_width;
1619 ctx->paper_height = fc->paper_height;
1621 switch (fc->pages_per_sheet) {
1649 ctx->pages_printed = 0;
1651 switch (fc->format) {
1652 case EV_FILE_FORMAT_PS:
1653 #ifdef HAVE_CAIRO_PS
1654 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1657 case EV_FILE_FORMAT_PDF:
1658 #ifdef HAVE_CAIRO_PDF
1659 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1663 g_assert_not_reached ();
1666 ctx->cr = cairo_create (surface);
1667 cairo_surface_destroy (surface);
1669 #else /* HAVE_CAIRO_PRINT */
1670 if (ctx->format == EV_FILE_FORMAT_PS) {
1671 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1672 fc->filename, fc->first_page,
1673 fc->last_page - fc->first_page + 1);
1674 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1675 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1677 #endif /* HAVE_CAIRO_PRINT */
1681 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1683 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1684 PdfPrintContext *ctx = pdf_document->print_ctx;
1686 g_return_if_fail (pdf_document->print_ctx != NULL);
1688 ctx->pages_printed = 0;
1690 #ifdef HAVE_CAIRO_PRINT
1691 if (ctx->paper_width > ctx->paper_height) {
1692 if (ctx->format == EV_FILE_FORMAT_PS) {
1693 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1696 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1697 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1702 #endif /* HAVE_CAIRO_PRINT */
1706 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1707 EvRenderContext *rc)
1709 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1710 PdfPrintContext *ctx = pdf_document->print_ctx;
1711 PopplerPage *poppler_page;
1712 #ifdef HAVE_CAIRO_PRINT
1713 gdouble page_width, page_height;
1715 gboolean rotate, landscape;
1716 gdouble width, height;
1717 gdouble pwidth, pheight;
1718 gdouble xscale, yscale;
1721 g_return_if_fail (pdf_document->print_ctx != NULL);
1723 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1725 #ifdef HAVE_CAIRO_PRINT
1726 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1727 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1728 poppler_page_get_size (poppler_page, &page_width, &page_height);
1730 if (page_width > page_height && page_width > ctx->paper_width) {
1736 landscape = (ctx->paper_width > ctx->paper_height);
1738 /* Use always portrait mode and rotate when necessary */
1739 if (ctx->paper_width > ctx->paper_height) {
1740 width = ctx->paper_height;
1741 height = ctx->paper_width;
1744 width = ctx->paper_width;
1745 height = ctx->paper_height;
1748 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1761 page_width = page_height;
1765 pwidth = width / ctx->pages_x;
1766 pheight = height / ctx->pages_y;
1768 if ((page_width > pwidth || page_height > pheight) ||
1769 (page_width < pwidth && page_height < pheight)) {
1770 xscale = pwidth / page_width;
1771 yscale = pheight / page_height;
1773 if (yscale < xscale) {
1780 xscale = yscale = 1;
1785 cairo_save (ctx->cr);
1787 cairo_matrix_t matrix;
1789 cairo_translate (ctx->cr, width, 0);
1790 cairo_matrix_init (&matrix,
1794 cairo_transform (ctx->cr, &matrix);
1797 cairo_translate (ctx->cr,
1798 x * (rotate ? pheight : pwidth),
1799 y * (rotate ? pwidth : pheight));
1800 cairo_scale (ctx->cr, xscale, yscale);
1802 #ifdef HAVE_POPPLER_PAGE_RENDER_FOR_PRINTING
1803 poppler_page_render_for_printing (poppler_page, ctx->cr);
1805 #ifdef HAVE_POPPLER_PAGE_RENDER
1806 poppler_page_render (poppler_page, ctx->cr);
1810 ctx->pages_printed++;
1812 cairo_restore (ctx->cr);
1813 #else /* HAVE_CAIRO_PRINT */
1814 if (ctx->format == EV_FILE_FORMAT_PS)
1815 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1816 #endif /* HAVE_CAIRO_PRINT */
1818 g_object_unref (poppler_page);
1822 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1824 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1825 PdfPrintContext *ctx = pdf_document->print_ctx;
1827 g_return_if_fail (pdf_document->print_ctx != NULL);
1829 #ifdef HAVE_CAIRO_PRINT
1830 cairo_show_page (ctx->cr);
1835 pdf_document_file_exporter_end (EvFileExporter *exporter)
1837 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1839 pdf_print_context_free (pdf_document->print_ctx);
1840 pdf_document->print_ctx = NULL;
1843 static EvFileExporterCapabilities
1844 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1846 return (EvFileExporterCapabilities) (
1847 EV_FILE_EXPORTER_CAN_PAGE_SET |
1848 EV_FILE_EXPORTER_CAN_COPIES |
1849 EV_FILE_EXPORTER_CAN_COLLATE |
1850 EV_FILE_EXPORTER_CAN_REVERSE |
1851 EV_FILE_EXPORTER_CAN_SCALE |
1852 #ifdef HAVE_CAIRO_PRINT
1853 #ifdef HAVE_POPPLER_PAGE_RENDER
1854 #if GTK_CHECK_VERSION (2, 11, 1)
1855 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1860 #ifdef HAVE_CAIRO_PDF
1861 #ifdef HAVE_POPPLER_PAGE_RENDER
1862 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1865 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1869 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1871 iface->begin = pdf_document_file_exporter_begin;
1872 iface->begin_page = pdf_document_file_exporter_begin_page;
1873 iface->do_page = pdf_document_file_exporter_do_page;
1874 iface->end_page = pdf_document_file_exporter_end_page;
1875 iface->end = pdf_document_file_exporter_end;
1876 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1880 pdf_selection_render_selection (EvSelection *selection,
1881 EvRenderContext *rc,
1882 cairo_surface_t **surface,
1883 EvRectangle *points,
1884 EvRectangle *old_points,
1885 EvSelectionStyle style,
1889 PdfDocument *pdf_document;
1890 double width_points, height_points;
1893 pdf_document = PDF_DOCUMENT (selection);
1894 set_rc_data (pdf_document, rc);
1896 poppler_page_get_size (POPPLER_PAGE (rc->data),
1897 &width_points, &height_points);
1898 width = (int) ((width_points * rc->scale) + 0.5);
1899 height = (int) ((height_points * rc->scale) + 0.5);
1901 #ifdef HAVE_POPPLER_PAGE_RENDER
1904 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
1905 GdkColor **text_color, **base_color;
1910 PopplerColor text_color, base_color;
1912 text_color.red = text->red;
1913 text_color.green = text->green;
1914 text_color.blue = text->blue;
1916 base_color.red = base->red;
1917 base_color.green = base->green;
1918 base_color.blue = base->blue;
1921 if (*surface == NULL) {
1922 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1927 cr = cairo_create (*surface);
1928 cairo_scale (cr, rc->scale, rc->scale);
1929 cairo_surface_set_device_offset (*surface, 0, 0);
1930 memset (cairo_image_surface_get_data (*surface), 0x00,
1931 cairo_image_surface_get_height (*surface) *
1932 cairo_image_surface_get_stride (*surface));
1933 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1935 (PopplerRectangle *)points,
1936 (PopplerRectangle *)old_points,
1937 (PopplerSelectionStyle)style,
1941 #else /* HAVE_POPPLER_PAGE_RENDER */
1944 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1948 poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1949 rc->scale, rc->rotation, pixbuf,
1950 (PopplerRectangle *)points,
1951 (PopplerRectangle *)old_points,
1952 (PopplerSelectionStyle)style,
1956 cairo_surface_destroy (*surface);
1957 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1958 g_object_unref (pixbuf);
1959 #endif /* HAVE_POPPLER_PAGE_RENDER */
1963 pdf_selection_get_selected_text (EvSelection *selection,
1964 EvRenderContext *rc,
1965 EvSelectionStyle style,
1966 EvRectangle *points)
1968 PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1969 PopplerPage *poppler_page;
1974 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1975 g_return_val_if_fail (poppler_page != NULL, NULL);
1977 poppler_page_get_size (poppler_page, NULL, &height);
1979 r.y1 = height - points->y2;
1981 r.y2 = height - points->y1;
1983 retval = poppler_page_get_text (poppler_page,
1984 (PopplerSelectionStyle)style,
1987 g_object_unref (poppler_page);
1993 create_gdk_region_from_poppler_region (GList *region)
1998 retval = gdk_region_new ();
2000 for (l = region; l; l = g_list_next (l)) {
2001 PopplerRectangle *rectangle;
2004 rectangle = (PopplerRectangle *)l->data;
2006 rect.x = (gint) rectangle->x1;
2007 rect.y = (gint) rectangle->y1;
2008 rect.width = (gint) (rectangle->x2 - rectangle->x1);
2009 rect.height = (gint) (rectangle->y2 - rectangle->y1);
2010 gdk_region_union_with_rect (retval, &rect);
2012 poppler_rectangle_free (rectangle);
2019 pdf_selection_get_selection_region (EvSelection *selection,
2020 EvRenderContext *rc,
2021 EvSelectionStyle style,
2022 EvRectangle *points)
2024 PdfDocument *pdf_document;
2027 pdf_document = PDF_DOCUMENT (selection);
2029 set_rc_data (pdf_document, rc);
2031 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
2032 retval = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2034 (PopplerSelectionStyle)style,
2035 (PopplerRectangle *) points);
2039 region = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2041 (PopplerSelectionStyle)style,
2042 (PopplerRectangle *) points);
2043 retval = create_gdk_region_from_poppler_region (region);
2044 g_list_free (region);
2051 pdf_selection_get_selection_map (EvSelection *selection,
2052 EvRenderContext *rc)
2054 PdfDocument *pdf_document;
2055 PopplerPage *poppler_page;
2056 PopplerRectangle points;
2060 pdf_document = PDF_DOCUMENT (selection);
2061 poppler_page = poppler_document_get_page (pdf_document->document,
2066 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2068 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
2069 retval = poppler_page_get_selection_region (poppler_page, 1.0,
2070 POPPLER_SELECTION_GLYPH,
2073 region = poppler_page_get_selection_region (poppler_page, 1.0,
2074 POPPLER_SELECTION_GLYPH,
2076 retval = create_gdk_region_from_poppler_region (region);
2077 g_list_free (region);
2079 g_object_unref (poppler_page);
2085 pdf_selection_iface_init (EvSelectionIface *iface)
2087 iface->render_selection = pdf_selection_render_selection;
2088 iface->get_selected_text = pdf_selection_get_selected_text;
2089 iface->get_selection_region = pdf_selection_get_selection_region;
2090 iface->get_selection_map = pdf_selection_get_selection_map;
2093 /* Page Transitions */
2095 pdf_document_get_page_duration (EvDocumentTransition *trans,
2098 PdfDocument *pdf_document;
2099 PopplerPage *poppler_page;
2100 gdouble duration = -1;
2102 pdf_document = PDF_DOCUMENT (trans);
2103 poppler_page = poppler_document_get_page (pdf_document->document, page);
2107 duration = poppler_page_get_duration (poppler_page);
2108 g_object_unref (poppler_page);
2113 static EvTransitionEffect *
2114 pdf_document_get_effect (EvDocumentTransition *trans,
2117 PdfDocument *pdf_document;
2118 PopplerPage *poppler_page;
2119 PopplerPageTransition *page_transition;
2120 EvTransitionEffect *effect;
2122 pdf_document = PDF_DOCUMENT (trans);
2123 poppler_page = poppler_document_get_page (pdf_document->document, page);
2128 page_transition = poppler_page_get_transition (poppler_page);
2130 if (!page_transition) {
2131 g_object_unref (poppler_page);
2135 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2136 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2137 "alignment", page_transition->alignment,
2138 "direction", page_transition->direction,
2139 "duration", page_transition->duration,
2140 "angle", page_transition->angle,
2141 "scale", page_transition->scale,
2142 "rectangular", page_transition->rectangular,
2145 poppler_page_transition_free (page_transition);
2146 g_object_unref (poppler_page);
2152 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2154 iface->get_page_duration = pdf_document_get_page_duration;
2155 iface->get_effect = pdf_document_get_effect;
2160 pdf_document_get_crop_box (EvDocument *document,
2164 PdfDocument *pdf_document;
2165 PopplerPage *poppler_page;
2166 PopplerRectangle poppler_rect;
2168 pdf_document = PDF_DOCUMENT (document);
2169 poppler_page = poppler_document_get_page (pdf_document->document, page);
2170 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2171 rect->x1 = poppler_rect.x1;
2172 rect->x2 = poppler_rect.x2;
2173 rect->y1 = poppler_rect.y1;
2174 rect->y2 = poppler_rect.y2;
2177 static EvFormField *
2178 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2180 EvFormField *ev_field = NULL;
2183 gboolean is_read_only;
2185 id = poppler_form_field_get_id (poppler_field);
2186 font_size = poppler_form_field_get_font_size (poppler_field);
2187 is_read_only = poppler_form_field_is_read_only (poppler_field);
2189 switch (poppler_form_field_get_field_type (poppler_field)) {
2190 case POPPLER_FORM_FIELD_TEXT: {
2191 EvFormFieldText *field_text;
2192 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2194 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2195 case POPPLER_FORM_TEXT_NORMAL:
2196 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2198 case POPPLER_FORM_TEXT_MULTILINE:
2199 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2201 case POPPLER_FORM_TEXT_FILE_SELECT:
2202 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2206 ev_field = ev_form_field_text_new (id, ev_text_type);
2207 field_text = EV_FORM_FIELD_TEXT (ev_field);
2209 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2210 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2211 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2212 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2213 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2214 field_text->text = poppler_form_field_text_get_text (poppler_field);
2218 case POPPLER_FORM_FIELD_BUTTON: {
2219 EvFormFieldButton *field_button;
2220 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2222 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2223 case POPPLER_FORM_BUTTON_PUSH:
2224 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2226 case POPPLER_FORM_BUTTON_CHECK:
2227 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2229 case POPPLER_FORM_BUTTON_RADIO:
2230 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2234 ev_field = ev_form_field_button_new (id, ev_button_type);
2235 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2237 field_button->state = poppler_form_field_button_get_state (poppler_field);
2240 case POPPLER_FORM_FIELD_CHOICE: {
2241 EvFormFieldChoice *field_choice;
2242 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2244 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2245 case POPPLER_FORM_CHOICE_COMBO:
2246 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2248 case EV_FORM_FIELD_CHOICE_LIST:
2249 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2253 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2254 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2256 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2257 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2258 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2259 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2261 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2262 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2263 if (field_choice->is_editable)
2264 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2267 case POPPLER_FORM_FIELD_SIGNATURE:
2269 ev_field = ev_form_field_signature_new (id);
2271 case POPPLER_FORM_FIELD_UNKNOWN:
2275 ev_field->font_size = font_size;
2276 ev_field->is_read_only = is_read_only;
2282 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2285 PdfDocument *pdf_document;
2286 PopplerPage *poppler_page;
2287 GList *retval = NULL;
2292 pdf_document = PDF_DOCUMENT (document);
2293 poppler_page = poppler_document_get_page (pdf_document->document, page);
2294 fields = poppler_page_get_form_field_mapping (poppler_page);
2295 poppler_page_get_size (poppler_page, NULL, &height);
2297 for (list = fields; list; list = list->next) {
2298 PopplerFormFieldMapping *mapping;
2299 EvFormFieldMapping *field_mapping;
2300 EvFormField *ev_field;
2302 mapping = (PopplerFormFieldMapping *)list->data;
2304 ev_field = ev_form_field_from_poppler_field (mapping->field);
2308 field_mapping = g_new0 (EvFormFieldMapping, 1);
2309 field_mapping->x1 = mapping->area.x1;
2310 field_mapping->x2 = mapping->area.x2;
2311 field_mapping->y1 = height - mapping->area.y2;
2312 field_mapping->y2 = height - mapping->area.y1;
2313 field_mapping->field = ev_field;
2314 field_mapping->field->page = page;
2316 retval = g_list_prepend (retval, field_mapping);
2319 poppler_page_free_form_field_mapping (fields);
2320 g_object_unref (poppler_page);
2322 return g_list_reverse (retval);
2326 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2330 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2331 PopplerFormField *poppler_field;
2334 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2338 text = poppler_form_field_text_get_text (poppler_field);
2339 g_object_unref (poppler_field);
2345 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2349 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2350 PopplerFormField *poppler_field;
2352 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2355 poppler_form_field_text_set_text (poppler_field, text);
2356 g_object_unref (poppler_field);
2360 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2364 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2365 PopplerFormField *poppler_field;
2367 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2371 poppler_form_field_button_set_state (poppler_field, state);
2372 g_object_unref (poppler_field);
2376 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2379 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2380 PopplerFormField *poppler_field;
2383 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2387 state = poppler_form_field_button_get_state (poppler_field);
2388 g_object_unref (poppler_field);
2394 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2398 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2399 PopplerFormField *poppler_field;
2402 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2406 text = poppler_form_field_choice_get_item (poppler_field, index);
2407 g_object_unref (poppler_field);
2413 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2416 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2417 PopplerFormField *poppler_field;
2420 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2424 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2425 g_object_unref (poppler_field);
2431 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2435 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2436 PopplerFormField *poppler_field;
2439 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2443 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2444 g_object_unref (poppler_field);
2450 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2454 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2455 PopplerFormField *poppler_field;
2457 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2461 poppler_form_field_choice_select_item (poppler_field, index);
2462 g_object_unref (poppler_field);
2466 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2470 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2471 PopplerFormField *poppler_field;
2473 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2477 poppler_form_field_choice_toggle_item (poppler_field, index);
2478 g_object_unref (poppler_field);
2482 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2485 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2486 PopplerFormField *poppler_field;
2488 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2492 poppler_form_field_choice_unselect_all (poppler_field);
2493 g_object_unref (poppler_field);
2497 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2501 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2502 PopplerFormField *poppler_field;
2504 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2508 poppler_form_field_choice_set_text (poppler_field, text);
2509 g_object_unref (poppler_field);
2513 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2516 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2517 PopplerFormField *poppler_field;
2520 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2524 text = poppler_form_field_choice_get_text (poppler_field);
2525 g_object_unref (poppler_field);
2531 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2533 iface->get_form_fields = pdf_document_forms_get_form_fields;
2534 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2535 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2536 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2537 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2538 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2539 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2540 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2541 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2542 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2543 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2544 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2545 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;