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-lib.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-document-layers.h"
48 #include "ev-selection.h"
49 #include "ev-transition-effect.h"
50 #include "ev-attachment.h"
53 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
54 #define HAVE_CAIRO_PRINT
58 PdfDocument *document;
67 EvFileExporterFormat format;
77 #ifdef HAVE_CAIRO_PRINT
80 PopplerPSFile *ps_file;
84 struct _PdfDocumentClass
86 GObjectClass parent_class;
91 GObject parent_instance;
93 PopplerDocument *document;
97 PopplerFontInfo *font_info;
98 PopplerFontsIter *fonts_iter;
99 int fonts_scanned_pages;
101 PdfDocumentSearch *search;
102 PdfPrintContext *print_ctx;
107 static void pdf_document_document_iface_init (EvDocumentIface *iface);
108 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
109 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
110 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
111 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
112 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
113 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
114 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
115 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
116 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
117 static void pdf_selection_iface_init (EvSelectionIface *iface);
118 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
119 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
123 static int pdf_document_get_n_pages (EvDocument *document);
125 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
127 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
128 PopplerAction *action);
129 static void pdf_document_search_free (PdfDocumentSearch *search);
130 static void pdf_print_context_free (PdfPrintContext *ctx);
132 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
134 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
135 pdf_document_security_iface_init);
136 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
137 pdf_document_document_thumbnails_iface_init);
138 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
139 pdf_document_document_links_iface_init);
140 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
141 pdf_document_document_images_iface_init);
142 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
143 pdf_document_document_forms_iface_init);
144 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
145 pdf_document_document_fonts_iface_init);
146 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
147 pdf_document_document_layers_iface_init);
148 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
149 pdf_document_find_iface_init);
150 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
151 pdf_document_file_exporter_iface_init);
152 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
153 pdf_selection_iface_init);
154 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
155 pdf_document_page_transition_iface_init);
159 pdf_document_search_free (PdfDocumentSearch *search)
161 PdfDocument *pdf_document = search->document;
165 if (search->idle != 0)
166 g_source_remove (search->idle);
168 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
169 for (i = 0; i < n_pages; i++) {
170 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
171 g_list_free (search->pages[i]);
173 g_free (search->pages);
175 g_free (search->text);
180 pdf_document_dispose (GObject *object)
182 PdfDocument *pdf_document = PDF_DOCUMENT(object);
184 if (pdf_document->print_ctx) {
185 pdf_print_context_free (pdf_document->print_ctx);
186 pdf_document->print_ctx = NULL;
189 if (pdf_document->search) {
190 pdf_document_search_free (pdf_document->search);
191 pdf_document->search = NULL;
194 if (pdf_document->document) {
195 g_object_unref (pdf_document->document);
198 if (pdf_document->font_info) {
199 poppler_font_info_free (pdf_document->font_info);
202 if (pdf_document->fonts_iter) {
203 poppler_fonts_iter_free (pdf_document->fonts_iter);
206 if (pdf_document->layers) {
207 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
208 g_list_free (pdf_document->layers);
211 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
215 pdf_document_class_init (PdfDocumentClass *klass)
217 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
219 g_object_class->dispose = pdf_document_dispose;
223 pdf_document_init (PdfDocument *pdf_document)
225 pdf_document->password = NULL;
229 convert_error (GError *poppler_error,
232 if (poppler_error == NULL)
235 if (poppler_error->domain == POPPLER_ERROR) {
236 /* convert poppler errors into EvDocument errors */
237 gint code = EV_DOCUMENT_ERROR_INVALID;
238 if (poppler_error->code == POPPLER_ERROR_INVALID)
239 code = EV_DOCUMENT_ERROR_INVALID;
240 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
241 code = EV_DOCUMENT_ERROR_ENCRYPTED;
243 g_set_error_literal (error,
246 poppler_error->message);
248 g_error_free (poppler_error);
250 g_propagate_error (error, poppler_error);
257 pdf_document_save (EvDocument *document,
261 PdfDocument *pdf_document = PDF_DOCUMENT (document);
263 GError *poppler_error = NULL;
265 if (pdf_document->modified) {
266 retval = poppler_document_save (pdf_document->document,
267 uri, &poppler_error);
269 retval = poppler_document_save_a_copy (pdf_document->document,
270 uri, &poppler_error);
274 convert_error (poppler_error, error);
280 pdf_document_load (EvDocument *document,
284 GError *poppler_error = NULL;
285 PdfDocument *pdf_document = PDF_DOCUMENT (document);
287 pdf_document->document =
288 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
290 if (pdf_document->document == NULL) {
291 convert_error (poppler_error, error);
299 pdf_document_get_n_pages (EvDocument *document)
301 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
305 pdf_document_get_page (EvDocument *document,
308 PdfDocument *pdf_document = PDF_DOCUMENT (document);
309 PopplerPage *poppler_page;
312 poppler_page = poppler_document_get_page (pdf_document->document, index);
313 page = ev_page_new (index);
314 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
315 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
316 g_object_unref (poppler_page);
322 pdf_document_get_page_size (EvDocument *document,
327 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
329 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
333 pdf_document_get_page_label (EvDocument *document,
338 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
340 g_object_get (G_OBJECT (page->backend_page),
347 pdf_document_has_attachments (EvDocument *document)
349 PdfDocument *pdf_document;
351 pdf_document = PDF_DOCUMENT (document);
353 return poppler_document_has_attachments (pdf_document->document);
356 struct SaveToBufferData {
362 attachment_save_to_buffer_callback (const gchar *buf,
367 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
371 if (sdata->len + count > sdata->max) {
372 new_max = MAX (sdata->max * 2, sdata->len + count);
373 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
375 sdata->buffer = new_buffer;
376 sdata->max = new_max;
379 memcpy (sdata->buffer + sdata->len, buf, count);
386 attachment_save_to_buffer (PopplerAttachment *attachment,
391 static const gint initial_max = 1024;
392 struct SaveToBufferData sdata;
397 sdata.buffer = (gchar *) g_malloc (initial_max);
398 sdata.max = initial_max;
401 if (! poppler_attachment_save_to_callback (attachment,
402 attachment_save_to_buffer_callback,
405 g_free (sdata.buffer);
409 *buffer = sdata.buffer;
410 *buffer_size = sdata.len;
416 pdf_document_get_attachments (EvDocument *document)
418 PdfDocument *pdf_document;
421 GList *retval = NULL;
423 pdf_document = PDF_DOCUMENT (document);
425 if (!pdf_document_has_attachments (document))
428 attachments = poppler_document_get_attachments (pdf_document->document);
430 for (list = attachments; list; list = list->next) {
431 PopplerAttachment *attachment;
432 EvAttachment *ev_attachment;
435 GError *error = NULL;
437 attachment = (PopplerAttachment *) list->data;
439 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
440 ev_attachment = ev_attachment_new (attachment->name,
441 attachment->description,
446 retval = g_list_prepend (retval, ev_attachment);
449 g_warning ("%s", error->message);
450 g_error_free (error);
456 g_object_unref (attachment);
459 return g_list_reverse (retval);
462 static cairo_surface_t *
463 pdf_page_render (PopplerPage *page,
468 cairo_surface_t *surface;
470 #ifdef HAVE_POPPLER_PAGE_RENDER
473 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
475 memset (cairo_image_surface_get_data (surface), 0xff,
476 cairo_image_surface_get_height (surface) *
477 cairo_image_surface_get_stride (surface));
479 cr = cairo_create (surface);
480 switch (rc->rotation) {
482 cairo_translate (cr, width, 0);
485 cairo_translate (cr, width, height);
488 cairo_translate (cr, 0, height);
491 cairo_translate (cr, 0, 0);
493 cairo_scale (cr, rc->scale, rc->scale);
494 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
495 poppler_page_render (page, cr);
497 #else /* HAVE_POPPLER_PAGE_RENDER */
500 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
504 poppler_page_render_to_pixbuf (page,
510 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
511 g_object_unref (pixbuf);
512 #endif /* HAVE_POPPLER_PAGE_RENDER */
517 static cairo_surface_t *
518 pdf_document_render (EvDocument *document,
521 PdfDocument *pdf_document;
522 PopplerPage *poppler_page;
523 double width_points, height_points;
526 poppler_page = POPPLER_PAGE (rc->page->backend_page);
528 poppler_page_get_size (poppler_page,
529 &width_points, &height_points);
531 if (rc->rotation == 90 || rc->rotation == 270) {
532 width = (int) ((height_points * rc->scale) + 0.5);
533 height = (int) ((width_points * rc->scale) + 0.5);
535 width = (int) ((width_points * rc->scale) + 0.5);
536 height = (int) ((height_points * rc->scale) + 0.5);
539 return pdf_page_render (poppler_page,
543 /* EvDocumentSecurity */
546 pdf_document_has_document_security (EvDocumentSecurity *document_security)
548 /* FIXME: do we really need to have this? */
553 pdf_document_set_password (EvDocumentSecurity *document_security,
554 const char *password)
556 PdfDocument *document = PDF_DOCUMENT (document_security);
558 if (document->password)
559 g_free (document->password);
561 document->password = g_strdup (password);
564 static EvDocumentInfo *
565 pdf_document_get_info (EvDocument *document)
567 EvDocumentInfo *info;
568 PopplerPageLayout layout;
569 PopplerPageMode mode;
570 PopplerViewerPreferences view_prefs;
571 PopplerPermissions permissions;
574 info = g_new0 (EvDocumentInfo, 1);
576 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
577 EV_DOCUMENT_INFO_FORMAT |
578 EV_DOCUMENT_INFO_AUTHOR |
579 EV_DOCUMENT_INFO_SUBJECT |
580 EV_DOCUMENT_INFO_KEYWORDS |
581 EV_DOCUMENT_INFO_LAYOUT |
582 EV_DOCUMENT_INFO_START_MODE |
583 EV_DOCUMENT_INFO_PERMISSIONS |
584 EV_DOCUMENT_INFO_UI_HINTS |
585 EV_DOCUMENT_INFO_CREATOR |
586 EV_DOCUMENT_INFO_PRODUCER |
587 EV_DOCUMENT_INFO_CREATION_DATE |
588 EV_DOCUMENT_INFO_MOD_DATE |
589 EV_DOCUMENT_INFO_LINEARIZED |
590 EV_DOCUMENT_INFO_N_PAGES |
591 EV_DOCUMENT_INFO_SECURITY |
592 EV_DOCUMENT_INFO_PAPER_SIZE;
594 g_object_get (PDF_DOCUMENT (document)->document,
595 "title", &(info->title),
596 "format", &(info->format),
597 "author", &(info->author),
598 "subject", &(info->subject),
599 "keywords", &(info->keywords),
601 "page-layout", &layout,
602 "viewer-preferences", &view_prefs,
603 "permissions", &permissions,
604 "creator", &(info->creator),
605 "producer", &(info->producer),
606 "creation-date", &(info->creation_date),
607 "mod-date", &(info->modified_date),
608 "linearized", &(info->linearized),
611 info->n_pages = ev_document_get_n_pages (document);
613 if (info->n_pages > 0) {
614 page = ev_document_get_page (document, 0);
615 ev_document_get_page_size (document, page,
616 &(info->paper_width),
617 &(info->paper_height));
618 g_object_unref (page);
622 info->paper_width = info->paper_width / 72.0f * 25.4f;
623 info->paper_height = info->paper_height / 72.0f * 25.4f;
627 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
628 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
630 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
631 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
633 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
634 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
636 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
637 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
638 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
639 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
641 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
642 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
649 case POPPLER_PAGE_MODE_NONE:
650 info->mode = EV_DOCUMENT_MODE_NONE;
652 case POPPLER_PAGE_MODE_USE_THUMBS:
653 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
655 case POPPLER_PAGE_MODE_USE_OC:
656 info->mode = EV_DOCUMENT_MODE_USE_OC;
658 case POPPLER_PAGE_MODE_FULL_SCREEN:
659 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
661 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
662 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
668 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
669 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
671 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
672 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
674 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
675 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
677 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
678 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
680 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
681 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
683 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
684 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
686 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
687 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
690 info->permissions = 0;
691 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
692 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
694 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
695 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
697 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
698 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
700 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
701 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
704 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
705 /* translators: this is the document security state */
706 info->security = g_strdup (_("Yes"));
708 /* translators: this is the document security state */
709 info->security = g_strdup (_("No"));
716 pdf_document_document_iface_init (EvDocumentIface *iface)
718 iface->save = pdf_document_save;
719 iface->load = pdf_document_load;
720 iface->get_n_pages = pdf_document_get_n_pages;
721 iface->get_page = pdf_document_get_page;
722 iface->get_page_size = pdf_document_get_page_size;
723 iface->get_page_label = pdf_document_get_page_label;
724 iface->has_attachments = pdf_document_has_attachments;
725 iface->get_attachments = pdf_document_get_attachments;
726 iface->render = pdf_document_render;
727 iface->get_info = pdf_document_get_info;
731 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
733 iface->has_document_security = pdf_document_has_document_security;
734 iface->set_password = pdf_document_set_password;
738 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
740 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
743 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
745 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
749 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
752 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
755 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
757 if (pdf_document->font_info == NULL) {
758 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
761 if (pdf_document->fonts_iter) {
762 poppler_fonts_iter_free (pdf_document->fonts_iter);
765 pdf_document->fonts_scanned_pages += n_pages;
767 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
768 &pdf_document->fonts_iter);
770 pdf_document->fonts_scanned_pages = 0;
771 poppler_font_info_free (pdf_document->font_info);
772 pdf_document->font_info = NULL;
779 font_type_to_string (PopplerFontType type)
782 case POPPLER_FONT_TYPE_TYPE1:
784 case POPPLER_FONT_TYPE_TYPE1C:
786 case POPPLER_FONT_TYPE_TYPE3:
788 case POPPLER_FONT_TYPE_TRUETYPE:
789 return _("TrueType");
790 case POPPLER_FONT_TYPE_CID_TYPE0:
791 return _("Type 1 (CID)");
792 case POPPLER_FONT_TYPE_CID_TYPE0C:
793 return _("Type 1C (CID)");
794 case POPPLER_FONT_TYPE_CID_TYPE2:
795 return _("TrueType (CID)");
797 return _("Unknown font type");
802 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
805 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
806 PopplerFontsIter *iter = pdf_document->fonts_iter;
808 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
814 GtkTreeIter list_iter;
817 const char *embedded;
820 name = poppler_fonts_iter_get_name (iter);
826 type = font_type_to_string (
827 poppler_fonts_iter_get_font_type (iter));
829 if (poppler_fonts_iter_is_embedded (iter)) {
830 if (poppler_fonts_iter_is_subset (iter))
831 embedded = _("Embedded subset");
833 embedded = _("Embedded");
835 embedded = _("Not embedded");
838 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
840 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
841 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
842 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
843 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
847 } while (poppler_fonts_iter_next (iter));
851 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
853 iface->fill_model = pdf_document_fonts_fill_model;
854 iface->scan = pdf_document_fonts_scan;
855 iface->get_progress = pdf_document_fonts_get_progress;
859 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
861 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
862 PopplerIndexIter *iter;
864 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
866 iter = poppler_index_iter_new (pdf_document->document);
869 poppler_index_iter_free (iter);
875 ev_link_dest_from_dest (PdfDocument *pdf_document,
878 EvLinkDest *ev_dest = NULL;
879 const char *unimplemented_dest = NULL;
881 g_assert (dest != NULL);
883 switch (dest->type) {
884 case POPPLER_DEST_XYZ: {
885 PopplerPage *poppler_page;
888 poppler_page = poppler_document_get_page (pdf_document->document,
889 MAX (0, dest->page_num - 1));
890 poppler_page_get_size (poppler_page, NULL, &height);
891 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
893 height - MIN (height, dest->top),
898 g_object_unref (poppler_page);
901 case POPPLER_DEST_FIT:
902 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
904 case POPPLER_DEST_FITH: {
905 PopplerPage *poppler_page;
908 poppler_page = poppler_document_get_page (pdf_document->document,
909 MAX (0, dest->page_num - 1));
910 poppler_page_get_size (poppler_page, NULL, &height);
911 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
912 height - MIN (height, dest->top),
914 g_object_unref (poppler_page);
917 case POPPLER_DEST_FITV:
918 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
922 case POPPLER_DEST_FITR: {
923 PopplerPage *poppler_page;
926 poppler_page = poppler_document_get_page (pdf_document->document,
927 MAX (0, dest->page_num - 1));
928 poppler_page_get_size (poppler_page, NULL, &height);
929 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
931 height - MIN (height, dest->bottom),
933 height - MIN (height, dest->top));
934 g_object_unref (poppler_page);
937 case POPPLER_DEST_FITB:
938 unimplemented_dest = "POPPLER_DEST_FITB";
940 case POPPLER_DEST_FITBH:
941 unimplemented_dest = "POPPLER_DEST_FITBH";
943 case POPPLER_DEST_FITBV:
944 unimplemented_dest = "POPPLER_DEST_FITBV";
946 case POPPLER_DEST_NAMED:
947 ev_dest = ev_link_dest_new_named (dest->named_dest);
949 case POPPLER_DEST_UNKNOWN:
950 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
954 if (unimplemented_dest) {
955 g_warning ("Unimplemented destination: %s, please post a "
956 "bug report in Evince bugzilla "
957 "(http://bugzilla.gnome.org) with a testcase.",
962 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
968 ev_link_from_action (PdfDocument *pdf_document,
969 PopplerAction *action)
972 EvLinkAction *ev_action = NULL;
973 const char *unimplemented_action = NULL;
975 switch (action->type) {
976 case POPPLER_ACTION_NONE:
978 case POPPLER_ACTION_GOTO_DEST: {
981 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
982 ev_action = ev_link_action_new_dest (dest);
985 case POPPLER_ACTION_GOTO_REMOTE: {
988 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
989 ev_action = ev_link_action_new_remote (dest,
990 action->goto_remote.file_name);
994 case POPPLER_ACTION_LAUNCH:
995 ev_action = ev_link_action_new_launch (action->launch.file_name,
996 action->launch.params);
998 case POPPLER_ACTION_URI:
999 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1001 case POPPLER_ACTION_NAMED:
1002 ev_action = ev_link_action_new_named (action->named.named_dest);
1004 case POPPLER_ACTION_MOVIE:
1005 unimplemented_action = "POPPLER_ACTION_MOVIE";
1007 case POPPLER_ACTION_UNKNOWN:
1008 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1011 if (unimplemented_action) {
1012 g_warning ("Unimplemented action: %s, please post a bug report "
1013 "in Evince bugzilla (http://bugzilla.gnome.org) "
1014 "with a testcase.", unimplemented_action);
1017 link = ev_link_new (action->any.title, ev_action);
1023 build_tree (PdfDocument *pdf_document,
1024 GtkTreeModel *model,
1025 GtkTreeIter *parent,
1026 PopplerIndexIter *iter)
1030 GtkTreeIter tree_iter;
1031 PopplerIndexIter *child;
1032 PopplerAction *action;
1033 EvLink *link = NULL;
1037 action = poppler_index_iter_get_action (iter);
1038 expand = poppler_index_iter_is_open (iter);
1043 switch (action->type) {
1044 case POPPLER_ACTION_GOTO_DEST: {
1045 /* For bookmarks, solve named destinations */
1046 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1048 EvLinkDest *ev_dest = NULL;
1049 EvLinkAction *ev_action;
1051 dest = poppler_document_find_dest (pdf_document->document,
1052 action->goto_dest.dest->named_dest);
1054 link = ev_link_from_action (pdf_document, action);
1058 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1059 poppler_dest_free (dest);
1061 ev_action = ev_link_action_new_dest (ev_dest);
1062 link = ev_link_new (action->any.title, ev_action);
1064 link = ev_link_from_action (pdf_document, action);
1069 link = ev_link_from_action (pdf_document, action);
1073 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1074 poppler_action_free (action);
1076 g_object_unref (link);
1081 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1082 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1084 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1085 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1086 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1087 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1090 g_free (title_markup);
1091 g_object_unref (link);
1093 child = poppler_index_iter_get_child (iter);
1095 build_tree (pdf_document, model, &tree_iter, child);
1096 poppler_index_iter_free (child);
1097 poppler_action_free (action);
1099 } while (poppler_index_iter_next (iter));
1102 static GtkTreeModel *
1103 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1105 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1106 GtkTreeModel *model = NULL;
1107 PopplerIndexIter *iter;
1109 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1111 iter = poppler_index_iter_new (pdf_document->document);
1112 /* Create the model if we have items*/
1114 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1119 build_tree (pdf_document, model, NULL, iter);
1120 poppler_index_iter_free (iter);
1127 pdf_document_links_get_links (EvDocumentLinks *document_links,
1130 PdfDocument *pdf_document;
1131 PopplerPage *poppler_page;
1132 GList *retval = NULL;
1133 GList *mapping_list;
1137 pdf_document = PDF_DOCUMENT (document_links);
1138 poppler_page = poppler_document_get_page (pdf_document->document,
1140 mapping_list = poppler_page_get_link_mapping (poppler_page);
1141 poppler_page_get_size (poppler_page, NULL, &height);
1143 for (list = mapping_list; list; list = list->next) {
1144 PopplerLinkMapping *link_mapping;
1145 EvLinkMapping *ev_link_mapping;
1147 link_mapping = (PopplerLinkMapping *)list->data;
1148 ev_link_mapping = g_new (EvLinkMapping, 1);
1149 ev_link_mapping->link = ev_link_from_action (pdf_document,
1150 link_mapping->action);
1151 ev_link_mapping->x1 = link_mapping->area.x1;
1152 ev_link_mapping->x2 = link_mapping->area.x2;
1153 /* Invert this for X-style coordinates */
1154 ev_link_mapping->y1 = height - link_mapping->area.y2;
1155 ev_link_mapping->y2 = height - link_mapping->area.y1;
1157 retval = g_list_prepend (retval, ev_link_mapping);
1160 poppler_page_free_link_mapping (mapping_list);
1161 g_object_unref (poppler_page);
1163 return g_list_reverse (retval);
1167 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1168 const gchar *link_name)
1170 PdfDocument *pdf_document;
1172 EvLinkDest *ev_dest = NULL;
1174 pdf_document = PDF_DOCUMENT (document_links);
1175 dest = poppler_document_find_dest (pdf_document->document,
1178 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1179 poppler_dest_free (dest);
1186 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1188 iface->has_document_links = pdf_document_links_has_document_links;
1189 iface->get_links_model = pdf_document_links_get_links_model;
1190 iface->get_links = pdf_document_links_get_links;
1191 iface->find_link_dest = pdf_document_links_find_link_dest;
1195 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1198 GList *retval = NULL;
1199 PdfDocument *pdf_document;
1200 PopplerPage *poppler_page;
1201 GList *mapping_list;
1204 pdf_document = PDF_DOCUMENT (document_images);
1205 poppler_page = poppler_document_get_page (pdf_document->document, page);
1206 mapping_list = poppler_page_get_image_mapping (poppler_page);
1208 for (list = mapping_list; list; list = list->next) {
1209 PopplerImageMapping *image_mapping;
1210 EvImageMapping *ev_image_mapping;
1212 image_mapping = (PopplerImageMapping *)list->data;
1214 ev_image_mapping = g_new (EvImageMapping, 1);
1216 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1217 ev_image_mapping->x1 = image_mapping->area.x1;
1218 ev_image_mapping->x2 = image_mapping->area.x2;
1219 ev_image_mapping->y1 = image_mapping->area.y1;
1220 ev_image_mapping->y2 = image_mapping->area.y2;
1222 retval = g_list_prepend (retval, ev_image_mapping);
1225 poppler_page_free_image_mapping (mapping_list);
1226 g_object_unref (poppler_page);
1228 return g_list_reverse (retval);
1232 pdf_document_images_get_image (EvDocumentImages *document_images,
1235 PdfDocument *pdf_document;
1236 PopplerPage *poppler_page;
1237 cairo_surface_t *surface;
1238 GdkPixbuf *retval = NULL;
1240 pdf_document = PDF_DOCUMENT (document_images);
1241 poppler_page = poppler_document_get_page (pdf_document->document,
1242 ev_image_get_page (image));
1244 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1246 retval = ev_document_misc_pixbuf_from_surface (surface);
1247 cairo_surface_destroy (surface);
1250 g_object_unref (poppler_page);
1256 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1258 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1259 iface->get_image = pdf_document_images_get_image;
1263 make_thumbnail_for_page (PopplerPage *poppler_page,
1264 EvRenderContext *rc,
1270 #ifdef POPPLER_WITH_GDK
1271 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1273 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1275 ev_document_fc_mutex_lock ();
1276 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1278 rc->scale, rc->rotation, pixbuf);
1279 ev_document_fc_mutex_unlock ();
1281 cairo_surface_t *surface;
1283 ev_document_fc_mutex_lock ();
1284 surface = pdf_page_render (poppler_page, width, height, rc);
1285 ev_document_fc_mutex_unlock ();
1287 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1288 cairo_surface_destroy (surface);
1289 #endif /* POPPLER_WITH_GDK */
1295 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1296 EvRenderContext *rc,
1299 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1300 PopplerPage *poppler_page;
1301 GdkPixbuf *pixbuf = NULL;
1302 GdkPixbuf *border_pixbuf;
1305 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1307 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1308 rc, &width, &height);
1310 #ifdef POPPLER_WITH_GDK
1311 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1313 cairo_surface_t *surface;
1315 surface = poppler_page_get_thumbnail (poppler_page);
1317 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1318 cairo_surface_destroy (surface);
1320 #endif /* POPPLER_WITH_GDK */
1322 if (pixbuf != NULL) {
1323 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1324 gdk_pixbuf_get_height (pixbuf) :
1325 gdk_pixbuf_get_width (pixbuf);
1327 if (thumb_width == width) {
1328 GdkPixbuf *rotated_pixbuf;
1330 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1331 (GdkPixbufRotation) (360 - rc->rotation));
1332 g_object_unref (pixbuf);
1333 pixbuf = rotated_pixbuf;
1335 /* The provided thumbnail has a different size */
1336 g_object_unref (pixbuf);
1337 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1340 /* There is no provided thumbnail. We need to make one. */
1341 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1345 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1346 g_object_unref (pixbuf);
1347 pixbuf = border_pixbuf;
1354 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1355 EvRenderContext *rc,
1359 double page_width, page_height;
1361 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1362 &page_width, &page_height);
1364 *width = (gint) MAX (page_width * rc->scale, 1);
1365 *height = (gint) MAX (page_height * rc->scale, 1);
1367 if (rc->rotation == 90 || rc->rotation == 270) {
1377 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1379 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1380 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1385 pdf_document_find_find_text (EvDocumentFind *document_find,
1388 gboolean case_sensitive)
1391 PopplerPage *poppler_page;
1394 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1395 g_return_val_if_fail (text != NULL, NULL);
1397 poppler_page = POPPLER_PAGE (page->backend_page);
1399 matches = poppler_page_find_text (poppler_page, text);
1403 poppler_page_get_size (poppler_page, NULL, &height);
1404 for (l = matches; l && l->data; l = g_list_next (l)) {
1405 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1409 rect->y1 = height - rect->y2;
1410 rect->y2 = height - tmp;
1417 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1419 iface->find_text = pdf_document_find_find_text;
1423 pdf_print_context_free (PdfPrintContext *ctx)
1428 #ifdef HAVE_CAIRO_PRINT
1430 cairo_destroy (ctx->cr);
1435 poppler_ps_file_free (ctx->ps_file);
1436 ctx->ps_file = NULL;
1443 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1444 EvFileExporterContext *fc)
1446 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1447 PdfPrintContext *ctx;
1448 #ifdef HAVE_CAIRO_PRINT
1449 gdouble width, height;
1450 cairo_surface_t *surface = NULL;
1453 if (pdf_document->print_ctx)
1454 pdf_print_context_free (pdf_document->print_ctx);
1455 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1456 ctx = pdf_document->print_ctx;
1457 ctx->format = fc->format;
1459 #ifdef HAVE_CAIRO_PRINT
1460 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1462 ctx->paper_width = fc->paper_width;
1463 ctx->paper_height = fc->paper_height;
1465 switch (fc->pages_per_sheet) {
1493 ctx->pages_printed = 0;
1495 switch (fc->format) {
1496 case EV_FILE_FORMAT_PS:
1497 #ifdef HAVE_CAIRO_PS
1498 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1501 case EV_FILE_FORMAT_PDF:
1502 #ifdef HAVE_CAIRO_PDF
1503 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1507 g_assert_not_reached ();
1510 ctx->cr = cairo_create (surface);
1511 cairo_surface_destroy (surface);
1513 #else /* HAVE_CAIRO_PRINT */
1514 if (ctx->format == EV_FILE_FORMAT_PS) {
1515 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1516 fc->filename, fc->first_page,
1517 fc->last_page - fc->first_page + 1);
1518 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1519 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1521 #endif /* HAVE_CAIRO_PRINT */
1525 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1527 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1528 PdfPrintContext *ctx = pdf_document->print_ctx;
1530 g_return_if_fail (pdf_document->print_ctx != NULL);
1532 ctx->pages_printed = 0;
1534 #ifdef HAVE_CAIRO_PRINT
1535 if (ctx->paper_width > ctx->paper_height) {
1536 if (ctx->format == EV_FILE_FORMAT_PS) {
1537 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1540 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1541 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1546 #endif /* HAVE_CAIRO_PRINT */
1550 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1551 EvRenderContext *rc)
1553 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1554 PdfPrintContext *ctx = pdf_document->print_ctx;
1555 PopplerPage *poppler_page;
1556 #ifdef HAVE_CAIRO_PRINT
1557 gdouble page_width, page_height;
1560 gdouble width, height;
1561 gdouble pwidth, pheight;
1562 gdouble xscale, yscale;
1565 g_return_if_fail (pdf_document->print_ctx != NULL);
1567 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1569 #ifdef HAVE_CAIRO_PRINT
1570 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1571 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1572 poppler_page_get_size (poppler_page, &page_width, &page_height);
1574 if (page_width > page_height && page_width > ctx->paper_width) {
1580 /* Use always portrait mode and rotate when necessary */
1581 if (ctx->paper_width > ctx->paper_height) {
1582 width = ctx->paper_height;
1583 height = ctx->paper_width;
1586 width = ctx->paper_width;
1587 height = ctx->paper_height;
1590 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1603 page_width = page_height;
1607 pwidth = width / ctx->pages_x;
1608 pheight = height / ctx->pages_y;
1610 if ((page_width > pwidth || page_height > pheight) ||
1611 (page_width < pwidth && page_height < pheight)) {
1612 xscale = pwidth / page_width;
1613 yscale = pheight / page_height;
1615 if (yscale < xscale) {
1622 xscale = yscale = 1;
1627 cairo_save (ctx->cr);
1629 cairo_matrix_t matrix;
1631 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1632 cairo_matrix_init (&matrix,
1636 cairo_transform (ctx->cr, &matrix);
1639 cairo_translate (ctx->cr,
1640 x * (rotate ? pheight : pwidth),
1641 y * (rotate ? pwidth : pheight));
1642 cairo_scale (ctx->cr, xscale, yscale);
1644 poppler_page_render_for_printing (poppler_page, ctx->cr);
1646 ctx->pages_printed++;
1648 cairo_restore (ctx->cr);
1649 #else /* HAVE_CAIRO_PRINT */
1650 if (ctx->format == EV_FILE_FORMAT_PS)
1651 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1652 #endif /* HAVE_CAIRO_PRINT */
1656 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1658 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1659 PdfPrintContext *ctx = pdf_document->print_ctx;
1661 g_return_if_fail (pdf_document->print_ctx != NULL);
1663 #ifdef HAVE_CAIRO_PRINT
1664 cairo_show_page (ctx->cr);
1669 pdf_document_file_exporter_end (EvFileExporter *exporter)
1671 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1673 pdf_print_context_free (pdf_document->print_ctx);
1674 pdf_document->print_ctx = NULL;
1677 static EvFileExporterCapabilities
1678 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1680 return (EvFileExporterCapabilities) (
1681 EV_FILE_EXPORTER_CAN_PAGE_SET |
1682 EV_FILE_EXPORTER_CAN_COPIES |
1683 EV_FILE_EXPORTER_CAN_COLLATE |
1684 EV_FILE_EXPORTER_CAN_REVERSE |
1685 EV_FILE_EXPORTER_CAN_SCALE |
1686 #ifdef HAVE_CAIRO_PRINT
1687 #ifdef HAVE_POPPLER_PAGE_RENDER
1688 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1692 #ifdef HAVE_CAIRO_PDF
1693 #ifdef HAVE_POPPLER_PAGE_RENDER
1694 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1697 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1701 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1703 iface->begin = pdf_document_file_exporter_begin;
1704 iface->begin_page = pdf_document_file_exporter_begin_page;
1705 iface->do_page = pdf_document_file_exporter_do_page;
1706 iface->end_page = pdf_document_file_exporter_end_page;
1707 iface->end = pdf_document_file_exporter_end;
1708 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1712 pdf_selection_render_selection (EvSelection *selection,
1713 EvRenderContext *rc,
1714 cairo_surface_t **surface,
1715 EvRectangle *points,
1716 EvRectangle *old_points,
1717 EvSelectionStyle style,
1721 PopplerPage *poppler_page;
1722 double width_points, height_points;
1725 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1727 poppler_page_get_size (poppler_page,
1728 &width_points, &height_points);
1729 width = (int) ((width_points * rc->scale) + 0.5);
1730 height = (int) ((height_points * rc->scale) + 0.5);
1732 #ifdef HAVE_POPPLER_PAGE_RENDER
1734 PopplerColor text_color, base_color;
1736 text_color.red = text->red;
1737 text_color.green = text->green;
1738 text_color.blue = text->blue;
1740 base_color.red = base->red;
1741 base_color.green = base->green;
1742 base_color.blue = base->blue;
1744 if (*surface == NULL) {
1745 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1750 cr = cairo_create (*surface);
1751 cairo_scale (cr, rc->scale, rc->scale);
1752 cairo_surface_set_device_offset (*surface, 0, 0);
1753 memset (cairo_image_surface_get_data (*surface), 0x00,
1754 cairo_image_surface_get_height (*surface) *
1755 cairo_image_surface_get_stride (*surface));
1756 poppler_page_render_selection (poppler_page,
1758 (PopplerRectangle *)points,
1759 (PopplerRectangle *)old_points,
1760 (PopplerSelectionStyle)style,
1764 #else /* HAVE_POPPLER_PAGE_RENDER */
1767 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1771 poppler_page_render_selection_to_pixbuf (poppler_page,
1772 rc->scale, rc->rotation, pixbuf,
1773 (PopplerRectangle *)points,
1774 (PopplerRectangle *)old_points,
1775 (PopplerSelectionStyle)style,
1779 cairo_surface_destroy (*surface);
1780 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1781 g_object_unref (pixbuf);
1782 #endif /* HAVE_POPPLER_PAGE_RENDER */
1786 pdf_selection_get_selected_text (EvSelection *selection,
1787 EvRenderContext *rc,
1788 EvSelectionStyle style,
1789 EvRectangle *points)
1791 PopplerPage *poppler_page;
1796 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1798 poppler_page_get_size (poppler_page, NULL, &height);
1800 r.y1 = height - points->y2;
1802 r.y2 = height - points->y1;
1804 retval = poppler_page_get_text (poppler_page,
1805 (PopplerSelectionStyle)style,
1812 create_gdk_region_from_poppler_region (GList *region)
1817 retval = gdk_region_new ();
1819 for (l = region; l; l = g_list_next (l)) {
1820 PopplerRectangle *rectangle;
1823 rectangle = (PopplerRectangle *)l->data;
1825 rect.x = (gint) rectangle->x1;
1826 rect.y = (gint) rectangle->y1;
1827 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1828 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1829 gdk_region_union_with_rect (retval, &rect);
1831 poppler_rectangle_free (rectangle);
1838 pdf_selection_get_selection_region (EvSelection *selection,
1839 EvRenderContext *rc,
1840 EvSelectionStyle style,
1841 EvRectangle *points)
1843 PopplerPage *poppler_page;
1847 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1849 region = poppler_page_get_selection_region (poppler_page,
1851 (PopplerSelectionStyle)style,
1852 (PopplerRectangle *) points);
1853 retval = create_gdk_region_from_poppler_region (region);
1854 g_list_free (region);
1860 pdf_selection_get_selection_map (EvSelection *selection,
1861 EvRenderContext *rc)
1863 PopplerPage *poppler_page;
1864 PopplerRectangle points;
1868 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1872 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1874 region = poppler_page_get_selection_region (poppler_page, 1.0,
1875 POPPLER_SELECTION_GLYPH,
1877 retval = create_gdk_region_from_poppler_region (region);
1878 g_list_free (region);
1884 pdf_selection_iface_init (EvSelectionIface *iface)
1886 iface->render_selection = pdf_selection_render_selection;
1887 iface->get_selected_text = pdf_selection_get_selected_text;
1888 iface->get_selection_region = pdf_selection_get_selection_region;
1889 iface->get_selection_map = pdf_selection_get_selection_map;
1892 /* Page Transitions */
1894 pdf_document_get_page_duration (EvDocumentTransition *trans,
1897 PdfDocument *pdf_document;
1898 PopplerPage *poppler_page;
1899 gdouble duration = -1;
1901 pdf_document = PDF_DOCUMENT (trans);
1902 poppler_page = poppler_document_get_page (pdf_document->document, page);
1906 duration = poppler_page_get_duration (poppler_page);
1907 g_object_unref (poppler_page);
1912 static EvTransitionEffect *
1913 pdf_document_get_effect (EvDocumentTransition *trans,
1916 PdfDocument *pdf_document;
1917 PopplerPage *poppler_page;
1918 PopplerPageTransition *page_transition;
1919 EvTransitionEffect *effect;
1921 pdf_document = PDF_DOCUMENT (trans);
1922 poppler_page = poppler_document_get_page (pdf_document->document, page);
1927 page_transition = poppler_page_get_transition (poppler_page);
1929 if (!page_transition) {
1930 g_object_unref (poppler_page);
1934 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1935 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1936 "alignment", page_transition->alignment,
1937 "direction", page_transition->direction,
1938 "duration", page_transition->duration,
1939 "angle", page_transition->angle,
1940 "scale", page_transition->scale,
1941 "rectangular", page_transition->rectangular,
1944 poppler_page_transition_free (page_transition);
1945 g_object_unref (poppler_page);
1951 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1953 iface->get_page_duration = pdf_document_get_page_duration;
1954 iface->get_effect = pdf_document_get_effect;
1959 pdf_document_get_crop_box (EvDocument *document,
1963 PdfDocument *pdf_document;
1964 PopplerPage *poppler_page;
1965 PopplerRectangle poppler_rect;
1967 pdf_document = PDF_DOCUMENT (document);
1968 poppler_page = poppler_document_get_page (pdf_document->document, page);
1969 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1970 rect->x1 = poppler_rect.x1;
1971 rect->x2 = poppler_rect.x2;
1972 rect->y1 = poppler_rect.y1;
1973 rect->y2 = poppler_rect.y2;
1976 static EvFormField *
1977 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1979 EvFormField *ev_field = NULL;
1982 gboolean is_read_only;
1984 id = poppler_form_field_get_id (poppler_field);
1985 font_size = poppler_form_field_get_font_size (poppler_field);
1986 is_read_only = poppler_form_field_is_read_only (poppler_field);
1988 switch (poppler_form_field_get_field_type (poppler_field)) {
1989 case POPPLER_FORM_FIELD_TEXT: {
1990 EvFormFieldText *field_text;
1991 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1993 switch (poppler_form_field_text_get_text_type (poppler_field)) {
1994 case POPPLER_FORM_TEXT_NORMAL:
1995 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1997 case POPPLER_FORM_TEXT_MULTILINE:
1998 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2000 case POPPLER_FORM_TEXT_FILE_SELECT:
2001 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2005 ev_field = ev_form_field_text_new (id, ev_text_type);
2006 field_text = EV_FORM_FIELD_TEXT (ev_field);
2008 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2009 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2010 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2011 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2012 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2013 field_text->text = poppler_form_field_text_get_text (poppler_field);
2017 case POPPLER_FORM_FIELD_BUTTON: {
2018 EvFormFieldButton *field_button;
2019 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2021 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2022 case POPPLER_FORM_BUTTON_PUSH:
2023 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2025 case POPPLER_FORM_BUTTON_CHECK:
2026 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2028 case POPPLER_FORM_BUTTON_RADIO:
2029 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2033 ev_field = ev_form_field_button_new (id, ev_button_type);
2034 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2036 field_button->state = poppler_form_field_button_get_state (poppler_field);
2039 case POPPLER_FORM_FIELD_CHOICE: {
2040 EvFormFieldChoice *field_choice;
2041 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2043 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2044 case POPPLER_FORM_CHOICE_COMBO:
2045 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2047 case EV_FORM_FIELD_CHOICE_LIST:
2048 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2052 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2053 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2055 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2056 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2057 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2058 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2060 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2061 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2062 if (field_choice->is_editable)
2063 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2066 case POPPLER_FORM_FIELD_SIGNATURE:
2068 ev_field = ev_form_field_signature_new (id);
2070 case POPPLER_FORM_FIELD_UNKNOWN:
2074 ev_field->font_size = font_size;
2075 ev_field->is_read_only = is_read_only;
2081 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2084 PopplerPage *poppler_page;
2085 GList *retval = NULL;
2090 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2092 poppler_page = POPPLER_PAGE (page->backend_page);
2093 fields = poppler_page_get_form_field_mapping (poppler_page);
2094 poppler_page_get_size (poppler_page, NULL, &height);
2096 for (list = fields; list; list = list->next) {
2097 PopplerFormFieldMapping *mapping;
2098 EvFormFieldMapping *field_mapping;
2099 EvFormField *ev_field;
2101 mapping = (PopplerFormFieldMapping *)list->data;
2103 ev_field = ev_form_field_from_poppler_field (mapping->field);
2107 field_mapping = g_new0 (EvFormFieldMapping, 1);
2108 field_mapping->x1 = mapping->area.x1;
2109 field_mapping->x2 = mapping->area.x2;
2110 field_mapping->y1 = height - mapping->area.y2;
2111 field_mapping->y2 = height - mapping->area.y1;
2112 field_mapping->field = ev_field;
2113 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2115 g_object_set_data_full (G_OBJECT (ev_field),
2117 g_object_ref (mapping->field),
2118 (GDestroyNotify) g_object_unref);
2120 retval = g_list_prepend (retval, field_mapping);
2123 poppler_page_free_form_field_mapping (fields);
2125 return g_list_reverse (retval);
2129 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2133 PopplerFormField *poppler_field;
2136 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2140 text = poppler_form_field_text_get_text (poppler_field);
2146 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2150 PopplerFormField *poppler_field;
2152 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2156 poppler_form_field_text_set_text (poppler_field, text);
2157 PDF_DOCUMENT (document)->modified = TRUE;
2161 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2165 PopplerFormField *poppler_field;
2167 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2171 poppler_form_field_button_set_state (poppler_field, state);
2172 PDF_DOCUMENT (document)->modified = TRUE;
2176 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2179 PopplerFormField *poppler_field;
2182 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2186 state = poppler_form_field_button_get_state (poppler_field);
2192 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2196 PopplerFormField *poppler_field;
2199 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2203 text = poppler_form_field_choice_get_item (poppler_field, index);
2209 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2212 PopplerFormField *poppler_field;
2215 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2219 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2225 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2229 PopplerFormField *poppler_field;
2232 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2236 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2242 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2246 PopplerFormField *poppler_field;
2248 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2252 poppler_form_field_choice_select_item (poppler_field, index);
2253 PDF_DOCUMENT (document)->modified = TRUE;
2257 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2261 PopplerFormField *poppler_field;
2263 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2267 poppler_form_field_choice_toggle_item (poppler_field, index);
2268 PDF_DOCUMENT (document)->modified = TRUE;
2272 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2275 PopplerFormField *poppler_field;
2277 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2281 poppler_form_field_choice_unselect_all (poppler_field);
2282 PDF_DOCUMENT (document)->modified = TRUE;
2286 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2290 PopplerFormField *poppler_field;
2292 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2296 poppler_form_field_choice_set_text (poppler_field, text);
2297 PDF_DOCUMENT (document)->modified = TRUE;
2301 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2304 PopplerFormField *poppler_field;
2307 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2311 text = poppler_form_field_choice_get_text (poppler_field);
2317 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2319 iface->get_form_fields = pdf_document_forms_get_form_fields;
2320 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2321 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2322 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2323 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2324 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2325 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2326 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2327 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2328 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2329 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2330 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2331 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2336 pdf_document_layers_has_layers (EvDocumentLayers *document)
2338 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2339 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2340 PopplerLayersIter *iter;
2342 iter = poppler_layers_iter_new (pdf_document->document);
2345 poppler_layers_iter_free (iter);
2350 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2353 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2355 build_layers_tree (PdfDocument *pdf_document,
2356 GtkTreeModel *model,
2357 GtkTreeIter *parent,
2358 PopplerLayersIter *iter)
2361 GtkTreeIter tree_iter;
2362 PopplerLayersIter *child;
2363 PopplerLayer *layer;
2364 EvLayer *ev_layer = NULL;
2369 layer = poppler_layers_iter_get_layer (iter);
2371 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2372 visible = poppler_layer_is_visible (layer);
2373 rb_group = poppler_layer_get_radio_button_group_id (layer);
2374 pdf_document->layers = g_list_append (pdf_document->layers,
2375 g_object_ref (layer));
2376 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2377 poppler_layer_is_parent (layer),
2382 title = poppler_layers_iter_get_title (iter);
2383 markup = g_markup_escape_text (title, -1);
2390 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2391 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2392 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2393 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2394 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2395 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2396 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2397 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2400 g_object_unref (ev_layer);
2403 child = poppler_layers_iter_get_child (iter);
2405 build_layers_tree (pdf_document, model, &tree_iter, child);
2406 poppler_layers_iter_free (child);
2407 } while (poppler_layers_iter_next (iter));
2409 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2411 static GtkTreeModel *
2412 pdf_document_layers_get_layers (EvDocumentLayers *document)
2414 GtkTreeModel *model = NULL;
2415 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2416 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2417 PopplerLayersIter *iter;
2419 iter = poppler_layers_iter_new (pdf_document->document);
2421 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2422 G_TYPE_STRING, /* TITLE */
2423 G_TYPE_OBJECT, /* LAYER */
2424 G_TYPE_BOOLEAN, /* VISIBLE */
2425 G_TYPE_BOOLEAN, /* ENABLED */
2426 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2427 G_TYPE_INT); /* RBGROUP */
2428 build_layers_tree (pdf_document, model, NULL, iter);
2429 poppler_layers_iter_free (iter);
2437 pdf_document_layers_show_layer (EvDocumentLayers *document,
2440 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2441 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2442 guint layer_id = ev_layer_get_id (layer);
2444 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2447 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2451 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2454 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2455 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2456 guint layer_id = ev_layer_get_id (layer);
2458 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2461 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2465 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2468 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2469 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2470 guint layer_id = ev_layer_get_id (layer);
2472 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2475 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2479 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2481 iface->has_layers = pdf_document_layers_has_layers;
2482 iface->get_layers = pdf_document_layers_get_layers;
2483 iface->show_layer = pdf_document_layers_show_layer;
2484 iface->hide_layer = pdf_document_layers_hide_layer;
2485 iface->layer_is_visible = pdf_document_layers_layer_is_visible;