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 (PdfDocument *pdf_document,
1264 PopplerPage *poppler_page,
1265 EvRenderContext *rc)
1270 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1271 rc, &width, &height);
1272 #ifdef POPPLER_WITH_GDK
1273 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1275 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1277 ev_document_fc_mutex_lock ();
1278 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1280 rc->scale, rc->rotation, pixbuf);
1281 ev_document_fc_mutex_unlock ();
1283 cairo_surface_t *surface;
1285 ev_document_fc_mutex_lock ();
1286 surface = pdf_page_render (poppler_page, width, height, rc);
1287 ev_document_fc_mutex_unlock ();
1289 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1290 cairo_surface_destroy (surface);
1291 #endif /* POPPLER_WITH_GDK */
1297 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1298 EvRenderContext *rc,
1301 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1302 PopplerPage *poppler_page;
1303 GdkPixbuf *pixbuf = NULL;
1304 GdkPixbuf *border_pixbuf;
1306 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1308 #ifdef POPPLER_WITH_GDK
1309 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1311 cairo_surface_t *surface;
1313 surface = poppler_page_get_thumbnail (poppler_page);
1315 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1316 cairo_surface_destroy (surface);
1318 #endif /* POPPLER_WITH_GDK */
1321 /* Rotate provided thumbnail if needed */
1322 GdkPixbuf *rotated_pixbuf;
1324 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1325 (GdkPixbufRotation) (360 - rc->rotation));
1326 g_object_unref (pixbuf);
1327 pixbuf = rotated_pixbuf;
1329 /* There is no provided thumbnail. We need to make one. */
1330 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1334 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1335 g_object_unref (pixbuf);
1336 pixbuf = border_pixbuf;
1343 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1344 EvRenderContext *rc,
1348 PopplerPage *poppler_page;
1351 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1353 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1355 if (!has_thumb || *width <= 0 || *height <= 0) {
1356 double page_width, page_height;
1358 poppler_page_get_size (poppler_page, &page_width, &page_height);
1360 *width = (gint) MAX (page_width * rc->scale, 1);
1361 *height = (gint) MAX (page_height * rc->scale, 1);
1364 if (rc->rotation == 90 || rc->rotation == 270) {
1374 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1376 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1377 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1382 pdf_document_find_find_text (EvDocumentFind *document_find,
1385 gboolean case_sensitive)
1388 PopplerPage *poppler_page;
1391 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1392 g_return_val_if_fail (text != NULL, NULL);
1394 poppler_page = POPPLER_PAGE (page->backend_page);
1396 matches = poppler_page_find_text (poppler_page, text);
1400 poppler_page_get_size (poppler_page, NULL, &height);
1401 for (l = matches; l && l->data; l = g_list_next (l)) {
1402 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1406 rect->y1 = height - rect->y2;
1407 rect->y2 = height - tmp;
1414 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1416 iface->find_text = pdf_document_find_find_text;
1420 pdf_print_context_free (PdfPrintContext *ctx)
1425 #ifdef HAVE_CAIRO_PRINT
1427 cairo_destroy (ctx->cr);
1432 poppler_ps_file_free (ctx->ps_file);
1433 ctx->ps_file = NULL;
1440 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1441 EvFileExporterContext *fc)
1443 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1444 PdfPrintContext *ctx;
1445 #ifdef HAVE_CAIRO_PRINT
1446 gdouble width, height;
1447 cairo_surface_t *surface = NULL;
1450 if (pdf_document->print_ctx)
1451 pdf_print_context_free (pdf_document->print_ctx);
1452 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1453 ctx = pdf_document->print_ctx;
1454 ctx->format = fc->format;
1456 #ifdef HAVE_CAIRO_PRINT
1457 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1459 ctx->paper_width = fc->paper_width;
1460 ctx->paper_height = fc->paper_height;
1462 switch (fc->pages_per_sheet) {
1490 ctx->pages_printed = 0;
1492 switch (fc->format) {
1493 case EV_FILE_FORMAT_PS:
1494 #ifdef HAVE_CAIRO_PS
1495 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1498 case EV_FILE_FORMAT_PDF:
1499 #ifdef HAVE_CAIRO_PDF
1500 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1504 g_assert_not_reached ();
1507 ctx->cr = cairo_create (surface);
1508 cairo_surface_destroy (surface);
1510 #else /* HAVE_CAIRO_PRINT */
1511 if (ctx->format == EV_FILE_FORMAT_PS) {
1512 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1513 fc->filename, fc->first_page,
1514 fc->last_page - fc->first_page + 1);
1515 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1516 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1518 #endif /* HAVE_CAIRO_PRINT */
1522 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1524 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1525 PdfPrintContext *ctx = pdf_document->print_ctx;
1527 g_return_if_fail (pdf_document->print_ctx != NULL);
1529 ctx->pages_printed = 0;
1531 #ifdef HAVE_CAIRO_PRINT
1532 if (ctx->paper_width > ctx->paper_height) {
1533 if (ctx->format == EV_FILE_FORMAT_PS) {
1534 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1537 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1538 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1543 #endif /* HAVE_CAIRO_PRINT */
1547 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1548 EvRenderContext *rc)
1550 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1551 PdfPrintContext *ctx = pdf_document->print_ctx;
1552 PopplerPage *poppler_page;
1553 #ifdef HAVE_CAIRO_PRINT
1554 gdouble page_width, page_height;
1557 gdouble width, height;
1558 gdouble pwidth, pheight;
1559 gdouble xscale, yscale;
1562 g_return_if_fail (pdf_document->print_ctx != NULL);
1564 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1566 #ifdef HAVE_CAIRO_PRINT
1567 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1568 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1569 poppler_page_get_size (poppler_page, &page_width, &page_height);
1571 if (page_width > page_height && page_width > ctx->paper_width) {
1577 /* Use always portrait mode and rotate when necessary */
1578 if (ctx->paper_width > ctx->paper_height) {
1579 width = ctx->paper_height;
1580 height = ctx->paper_width;
1583 width = ctx->paper_width;
1584 height = ctx->paper_height;
1587 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1600 page_width = page_height;
1604 pwidth = width / ctx->pages_x;
1605 pheight = height / ctx->pages_y;
1607 if ((page_width > pwidth || page_height > pheight) ||
1608 (page_width < pwidth && page_height < pheight)) {
1609 xscale = pwidth / page_width;
1610 yscale = pheight / page_height;
1612 if (yscale < xscale) {
1619 xscale = yscale = 1;
1624 cairo_save (ctx->cr);
1626 cairo_matrix_t matrix;
1628 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1629 cairo_matrix_init (&matrix,
1633 cairo_transform (ctx->cr, &matrix);
1636 cairo_translate (ctx->cr,
1637 x * (rotate ? pheight : pwidth),
1638 y * (rotate ? pwidth : pheight));
1639 cairo_scale (ctx->cr, xscale, yscale);
1641 poppler_page_render_for_printing (poppler_page, ctx->cr);
1643 ctx->pages_printed++;
1645 cairo_restore (ctx->cr);
1646 #else /* HAVE_CAIRO_PRINT */
1647 if (ctx->format == EV_FILE_FORMAT_PS)
1648 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1649 #endif /* HAVE_CAIRO_PRINT */
1653 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1655 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1656 PdfPrintContext *ctx = pdf_document->print_ctx;
1658 g_return_if_fail (pdf_document->print_ctx != NULL);
1660 #ifdef HAVE_CAIRO_PRINT
1661 cairo_show_page (ctx->cr);
1666 pdf_document_file_exporter_end (EvFileExporter *exporter)
1668 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1670 pdf_print_context_free (pdf_document->print_ctx);
1671 pdf_document->print_ctx = NULL;
1674 static EvFileExporterCapabilities
1675 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1677 return (EvFileExporterCapabilities) (
1678 EV_FILE_EXPORTER_CAN_PAGE_SET |
1679 EV_FILE_EXPORTER_CAN_COPIES |
1680 EV_FILE_EXPORTER_CAN_COLLATE |
1681 EV_FILE_EXPORTER_CAN_REVERSE |
1682 EV_FILE_EXPORTER_CAN_SCALE |
1683 #ifdef HAVE_CAIRO_PRINT
1684 #ifdef HAVE_POPPLER_PAGE_RENDER
1685 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1689 #ifdef HAVE_CAIRO_PDF
1690 #ifdef HAVE_POPPLER_PAGE_RENDER
1691 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1694 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1698 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1700 iface->begin = pdf_document_file_exporter_begin;
1701 iface->begin_page = pdf_document_file_exporter_begin_page;
1702 iface->do_page = pdf_document_file_exporter_do_page;
1703 iface->end_page = pdf_document_file_exporter_end_page;
1704 iface->end = pdf_document_file_exporter_end;
1705 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1709 pdf_selection_render_selection (EvSelection *selection,
1710 EvRenderContext *rc,
1711 cairo_surface_t **surface,
1712 EvRectangle *points,
1713 EvRectangle *old_points,
1714 EvSelectionStyle style,
1718 PopplerPage *poppler_page;
1719 double width_points, height_points;
1722 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1724 poppler_page_get_size (poppler_page,
1725 &width_points, &height_points);
1726 width = (int) ((width_points * rc->scale) + 0.5);
1727 height = (int) ((height_points * rc->scale) + 0.5);
1729 #ifdef HAVE_POPPLER_PAGE_RENDER
1731 PopplerColor text_color, base_color;
1733 text_color.red = text->red;
1734 text_color.green = text->green;
1735 text_color.blue = text->blue;
1737 base_color.red = base->red;
1738 base_color.green = base->green;
1739 base_color.blue = base->blue;
1741 if (*surface == NULL) {
1742 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1747 cr = cairo_create (*surface);
1748 cairo_scale (cr, rc->scale, rc->scale);
1749 cairo_surface_set_device_offset (*surface, 0, 0);
1750 memset (cairo_image_surface_get_data (*surface), 0x00,
1751 cairo_image_surface_get_height (*surface) *
1752 cairo_image_surface_get_stride (*surface));
1753 poppler_page_render_selection (poppler_page,
1755 (PopplerRectangle *)points,
1756 (PopplerRectangle *)old_points,
1757 (PopplerSelectionStyle)style,
1761 #else /* HAVE_POPPLER_PAGE_RENDER */
1764 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1768 poppler_page_render_selection_to_pixbuf (poppler_page,
1769 rc->scale, rc->rotation, pixbuf,
1770 (PopplerRectangle *)points,
1771 (PopplerRectangle *)old_points,
1772 (PopplerSelectionStyle)style,
1776 cairo_surface_destroy (*surface);
1777 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1778 g_object_unref (pixbuf);
1779 #endif /* HAVE_POPPLER_PAGE_RENDER */
1783 pdf_selection_get_selected_text (EvSelection *selection,
1784 EvRenderContext *rc,
1785 EvSelectionStyle style,
1786 EvRectangle *points)
1788 PopplerPage *poppler_page;
1793 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1795 poppler_page_get_size (poppler_page, NULL, &height);
1797 r.y1 = height - points->y2;
1799 r.y2 = height - points->y1;
1801 retval = poppler_page_get_text (poppler_page,
1802 (PopplerSelectionStyle)style,
1809 create_gdk_region_from_poppler_region (GList *region)
1814 retval = gdk_region_new ();
1816 for (l = region; l; l = g_list_next (l)) {
1817 PopplerRectangle *rectangle;
1820 rectangle = (PopplerRectangle *)l->data;
1822 rect.x = (gint) rectangle->x1;
1823 rect.y = (gint) rectangle->y1;
1824 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1825 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1826 gdk_region_union_with_rect (retval, &rect);
1828 poppler_rectangle_free (rectangle);
1835 pdf_selection_get_selection_region (EvSelection *selection,
1836 EvRenderContext *rc,
1837 EvSelectionStyle style,
1838 EvRectangle *points)
1840 PopplerPage *poppler_page;
1844 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1846 region = poppler_page_get_selection_region (poppler_page,
1848 (PopplerSelectionStyle)style,
1849 (PopplerRectangle *) points);
1850 retval = create_gdk_region_from_poppler_region (region);
1851 g_list_free (region);
1857 pdf_selection_get_selection_map (EvSelection *selection,
1858 EvRenderContext *rc)
1860 PopplerPage *poppler_page;
1861 PopplerRectangle points;
1865 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1869 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1871 region = poppler_page_get_selection_region (poppler_page, 1.0,
1872 POPPLER_SELECTION_GLYPH,
1874 retval = create_gdk_region_from_poppler_region (region);
1875 g_list_free (region);
1881 pdf_selection_iface_init (EvSelectionIface *iface)
1883 iface->render_selection = pdf_selection_render_selection;
1884 iface->get_selected_text = pdf_selection_get_selected_text;
1885 iface->get_selection_region = pdf_selection_get_selection_region;
1886 iface->get_selection_map = pdf_selection_get_selection_map;
1889 /* Page Transitions */
1891 pdf_document_get_page_duration (EvDocumentTransition *trans,
1894 PdfDocument *pdf_document;
1895 PopplerPage *poppler_page;
1896 gdouble duration = -1;
1898 pdf_document = PDF_DOCUMENT (trans);
1899 poppler_page = poppler_document_get_page (pdf_document->document, page);
1903 duration = poppler_page_get_duration (poppler_page);
1904 g_object_unref (poppler_page);
1909 static EvTransitionEffect *
1910 pdf_document_get_effect (EvDocumentTransition *trans,
1913 PdfDocument *pdf_document;
1914 PopplerPage *poppler_page;
1915 PopplerPageTransition *page_transition;
1916 EvTransitionEffect *effect;
1918 pdf_document = PDF_DOCUMENT (trans);
1919 poppler_page = poppler_document_get_page (pdf_document->document, page);
1924 page_transition = poppler_page_get_transition (poppler_page);
1926 if (!page_transition) {
1927 g_object_unref (poppler_page);
1931 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1932 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1933 "alignment", page_transition->alignment,
1934 "direction", page_transition->direction,
1935 "duration", page_transition->duration,
1936 "angle", page_transition->angle,
1937 "scale", page_transition->scale,
1938 "rectangular", page_transition->rectangular,
1941 poppler_page_transition_free (page_transition);
1942 g_object_unref (poppler_page);
1948 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1950 iface->get_page_duration = pdf_document_get_page_duration;
1951 iface->get_effect = pdf_document_get_effect;
1956 pdf_document_get_crop_box (EvDocument *document,
1960 PdfDocument *pdf_document;
1961 PopplerPage *poppler_page;
1962 PopplerRectangle poppler_rect;
1964 pdf_document = PDF_DOCUMENT (document);
1965 poppler_page = poppler_document_get_page (pdf_document->document, page);
1966 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1967 rect->x1 = poppler_rect.x1;
1968 rect->x2 = poppler_rect.x2;
1969 rect->y1 = poppler_rect.y1;
1970 rect->y2 = poppler_rect.y2;
1973 static EvFormField *
1974 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1976 EvFormField *ev_field = NULL;
1979 gboolean is_read_only;
1981 id = poppler_form_field_get_id (poppler_field);
1982 font_size = poppler_form_field_get_font_size (poppler_field);
1983 is_read_only = poppler_form_field_is_read_only (poppler_field);
1985 switch (poppler_form_field_get_field_type (poppler_field)) {
1986 case POPPLER_FORM_FIELD_TEXT: {
1987 EvFormFieldText *field_text;
1988 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1990 switch (poppler_form_field_text_get_text_type (poppler_field)) {
1991 case POPPLER_FORM_TEXT_NORMAL:
1992 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1994 case POPPLER_FORM_TEXT_MULTILINE:
1995 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
1997 case POPPLER_FORM_TEXT_FILE_SELECT:
1998 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2002 ev_field = ev_form_field_text_new (id, ev_text_type);
2003 field_text = EV_FORM_FIELD_TEXT (ev_field);
2005 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2006 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2007 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2008 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2009 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2010 field_text->text = poppler_form_field_text_get_text (poppler_field);
2014 case POPPLER_FORM_FIELD_BUTTON: {
2015 EvFormFieldButton *field_button;
2016 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2018 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2019 case POPPLER_FORM_BUTTON_PUSH:
2020 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2022 case POPPLER_FORM_BUTTON_CHECK:
2023 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2025 case POPPLER_FORM_BUTTON_RADIO:
2026 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2030 ev_field = ev_form_field_button_new (id, ev_button_type);
2031 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2033 field_button->state = poppler_form_field_button_get_state (poppler_field);
2036 case POPPLER_FORM_FIELD_CHOICE: {
2037 EvFormFieldChoice *field_choice;
2038 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2040 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2041 case POPPLER_FORM_CHOICE_COMBO:
2042 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2044 case EV_FORM_FIELD_CHOICE_LIST:
2045 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2049 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2050 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2052 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2053 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2054 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2055 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2057 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2058 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2059 if (field_choice->is_editable)
2060 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2063 case POPPLER_FORM_FIELD_SIGNATURE:
2065 ev_field = ev_form_field_signature_new (id);
2067 case POPPLER_FORM_FIELD_UNKNOWN:
2071 ev_field->font_size = font_size;
2072 ev_field->is_read_only = is_read_only;
2078 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2081 PopplerPage *poppler_page;
2082 GList *retval = NULL;
2087 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2089 poppler_page = POPPLER_PAGE (page->backend_page);
2090 fields = poppler_page_get_form_field_mapping (poppler_page);
2091 poppler_page_get_size (poppler_page, NULL, &height);
2093 for (list = fields; list; list = list->next) {
2094 PopplerFormFieldMapping *mapping;
2095 EvFormFieldMapping *field_mapping;
2096 EvFormField *ev_field;
2098 mapping = (PopplerFormFieldMapping *)list->data;
2100 ev_field = ev_form_field_from_poppler_field (mapping->field);
2104 field_mapping = g_new0 (EvFormFieldMapping, 1);
2105 field_mapping->x1 = mapping->area.x1;
2106 field_mapping->x2 = mapping->area.x2;
2107 field_mapping->y1 = height - mapping->area.y2;
2108 field_mapping->y2 = height - mapping->area.y1;
2109 field_mapping->field = ev_field;
2110 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2112 g_object_set_data_full (G_OBJECT (ev_field),
2114 g_object_ref (mapping->field),
2115 (GDestroyNotify) g_object_unref);
2117 retval = g_list_prepend (retval, field_mapping);
2120 poppler_page_free_form_field_mapping (fields);
2122 return g_list_reverse (retval);
2126 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2130 PopplerFormField *poppler_field;
2133 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2137 text = poppler_form_field_text_get_text (poppler_field);
2143 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2147 PopplerFormField *poppler_field;
2149 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2153 poppler_form_field_text_set_text (poppler_field, text);
2154 PDF_DOCUMENT (document)->modified = TRUE;
2158 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2162 PopplerFormField *poppler_field;
2164 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2168 poppler_form_field_button_set_state (poppler_field, state);
2169 PDF_DOCUMENT (document)->modified = TRUE;
2173 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2176 PopplerFormField *poppler_field;
2179 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2183 state = poppler_form_field_button_get_state (poppler_field);
2189 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2193 PopplerFormField *poppler_field;
2196 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2200 text = poppler_form_field_choice_get_item (poppler_field, index);
2206 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2209 PopplerFormField *poppler_field;
2212 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2216 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2222 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2226 PopplerFormField *poppler_field;
2229 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2233 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2239 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2243 PopplerFormField *poppler_field;
2245 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2249 poppler_form_field_choice_select_item (poppler_field, index);
2250 PDF_DOCUMENT (document)->modified = TRUE;
2254 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2258 PopplerFormField *poppler_field;
2260 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2264 poppler_form_field_choice_toggle_item (poppler_field, index);
2265 PDF_DOCUMENT (document)->modified = TRUE;
2269 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2272 PopplerFormField *poppler_field;
2274 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2278 poppler_form_field_choice_unselect_all (poppler_field);
2279 PDF_DOCUMENT (document)->modified = TRUE;
2283 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2287 PopplerFormField *poppler_field;
2289 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2293 poppler_form_field_choice_set_text (poppler_field, text);
2294 PDF_DOCUMENT (document)->modified = TRUE;
2298 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2301 PopplerFormField *poppler_field;
2304 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2308 text = poppler_form_field_choice_get_text (poppler_field);
2314 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2316 iface->get_form_fields = pdf_document_forms_get_form_fields;
2317 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2318 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2319 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2320 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2321 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2322 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2323 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2324 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2325 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2326 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2327 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2328 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2333 pdf_document_layers_has_layers (EvDocumentLayers *document)
2335 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2336 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2337 PopplerLayersIter *iter;
2339 iter = poppler_layers_iter_new (pdf_document->document);
2342 poppler_layers_iter_free (iter);
2347 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2350 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2352 build_layers_tree (PdfDocument *pdf_document,
2353 GtkTreeModel *model,
2354 GtkTreeIter *parent,
2355 PopplerLayersIter *iter)
2358 GtkTreeIter tree_iter;
2359 PopplerLayersIter *child;
2360 PopplerLayer *layer;
2361 EvLayer *ev_layer = NULL;
2366 layer = poppler_layers_iter_get_layer (iter);
2368 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2369 visible = poppler_layer_is_visible (layer);
2370 rb_group = poppler_layer_get_radio_button_group_id (layer);
2371 pdf_document->layers = g_list_append (pdf_document->layers,
2372 g_object_ref (layer));
2373 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2374 poppler_layer_is_parent (layer),
2379 title = poppler_layers_iter_get_title (iter);
2380 markup = g_markup_escape_text (title, -1);
2387 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2388 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2389 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2390 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2391 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2392 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2393 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2394 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2397 g_object_unref (ev_layer);
2400 child = poppler_layers_iter_get_child (iter);
2402 build_layers_tree (pdf_document, model, &tree_iter, child);
2403 poppler_layers_iter_free (child);
2404 } while (poppler_layers_iter_next (iter));
2406 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2408 static GtkTreeModel *
2409 pdf_document_layers_get_layers (EvDocumentLayers *document)
2411 GtkTreeModel *model = NULL;
2412 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2413 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2414 PopplerLayersIter *iter;
2416 iter = poppler_layers_iter_new (pdf_document->document);
2418 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2419 G_TYPE_STRING, /* TITLE */
2420 G_TYPE_OBJECT, /* LAYER */
2421 G_TYPE_BOOLEAN, /* VISIBLE */
2422 G_TYPE_BOOLEAN, /* ENABLED */
2423 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2424 G_TYPE_INT); /* RBGROUP */
2425 build_layers_tree (pdf_document, model, NULL, iter);
2426 poppler_layers_iter_free (iter);
2434 pdf_document_layers_show_layer (EvDocumentLayers *document,
2437 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2438 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2439 guint layer_id = ev_layer_get_id (layer);
2441 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2444 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2448 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2451 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2452 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2453 guint layer_id = ev_layer_get_id (layer);
2455 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2458 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2462 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2465 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2466 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2467 guint layer_id = ev_layer_get_id (layer);
2469 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2472 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2476 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2478 iface->has_layers = pdf_document_layers_has_layers;
2479 iface->get_layers = pdf_document_layers_get_layers;
2480 iface->show_layer = pdf_document_layers_show_layer;
2481 iface->hide_layer = pdf_document_layers_hide_layer;
2482 iface->layer_is_visible = pdf_document_layers_layer_is_visible;