1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pdfdocument.h: Implementation of EvDocument for PDF
3 * Copyright (C) 2004, Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <poppler-document.h>
27 #include <poppler-page.h>
29 #include <cairo-pdf.h>
34 #include <glib/gi18n.h>
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-document-find.h"
39 #include "ev-document-misc.h"
40 #include "ev-document-links.h"
41 #include "ev-document-images.h"
42 #include "ev-document-fonts.h"
43 #include "ev-document-security.h"
44 #include "ev-document-thumbnails.h"
45 #include "ev-document-transition.h"
46 #include "ev-document-forms.h"
47 #include "ev-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;
96 PopplerFontInfo *font_info;
97 PopplerFontsIter *fonts_iter;
98 int fonts_scanned_pages;
100 PdfDocumentSearch *search;
101 PdfPrintContext *print_ctx;
106 static void pdf_document_document_iface_init (EvDocumentIface *iface);
107 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
108 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
109 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
110 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
111 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
112 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
113 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
114 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
115 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
116 static void pdf_selection_iface_init (EvSelectionIface *iface);
117 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
118 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
122 static int pdf_document_get_n_pages (EvDocument *document);
124 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
126 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
127 PopplerAction *action);
128 static void pdf_document_search_free (PdfDocumentSearch *search);
129 static void pdf_print_context_free (PdfPrintContext *ctx);
131 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
133 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
134 pdf_document_security_iface_init);
135 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
136 pdf_document_document_thumbnails_iface_init);
137 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
138 pdf_document_document_links_iface_init);
139 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
140 pdf_document_document_images_iface_init);
141 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
142 pdf_document_document_forms_iface_init);
143 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
144 pdf_document_document_fonts_iface_init);
145 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
146 pdf_document_document_layers_iface_init);
147 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
148 pdf_document_find_iface_init);
149 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
150 pdf_document_file_exporter_iface_init);
151 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
152 pdf_selection_iface_init);
153 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
154 pdf_document_page_transition_iface_init);
158 pdf_document_search_free (PdfDocumentSearch *search)
160 PdfDocument *pdf_document = search->document;
164 if (search->idle != 0)
165 g_source_remove (search->idle);
167 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
168 for (i = 0; i < n_pages; i++) {
169 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
170 g_list_free (search->pages[i]);
172 g_free (search->pages);
174 g_free (search->text);
179 pdf_document_dispose (GObject *object)
181 PdfDocument *pdf_document = PDF_DOCUMENT(object);
183 if (pdf_document->print_ctx) {
184 pdf_print_context_free (pdf_document->print_ctx);
185 pdf_document->print_ctx = NULL;
188 if (pdf_document->search) {
189 pdf_document_search_free (pdf_document->search);
190 pdf_document->search = NULL;
193 if (pdf_document->document) {
194 g_object_unref (pdf_document->document);
197 if (pdf_document->font_info) {
198 poppler_font_info_free (pdf_document->font_info);
201 if (pdf_document->fonts_iter) {
202 poppler_fonts_iter_free (pdf_document->fonts_iter);
205 if (pdf_document->layers) {
206 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
207 g_list_free (pdf_document->layers);
210 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
214 pdf_document_class_init (PdfDocumentClass *klass)
216 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
218 g_object_class->dispose = pdf_document_dispose;
222 pdf_document_init (PdfDocument *pdf_document)
224 pdf_document->password = NULL;
228 convert_error (GError *poppler_error,
231 if (poppler_error == NULL)
234 if (poppler_error->domain == POPPLER_ERROR) {
235 /* convert poppler errors into EvDocument errors */
236 gint code = EV_DOCUMENT_ERROR_INVALID;
237 if (poppler_error->code == POPPLER_ERROR_INVALID)
238 code = EV_DOCUMENT_ERROR_INVALID;
239 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
240 code = EV_DOCUMENT_ERROR_ENCRYPTED;
246 poppler_error->message,
249 g_propagate_error (error, poppler_error);
256 pdf_document_save (EvDocument *document,
261 GError *poppler_error = NULL;
263 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
267 convert_error (poppler_error, error);
273 pdf_document_load (EvDocument *document,
277 GError *poppler_error = NULL;
278 PdfDocument *pdf_document = PDF_DOCUMENT (document);
280 pdf_document->document =
281 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
283 if (pdf_document->document == NULL) {
284 convert_error (poppler_error, error);
292 pdf_document_get_n_pages (EvDocument *document)
294 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
298 pdf_document_get_page (EvDocument *document,
301 PdfDocument *pdf_document = PDF_DOCUMENT (document);
302 PopplerPage *poppler_page;
305 poppler_page = poppler_document_get_page (pdf_document->document, index);
306 page = ev_page_new (index);
307 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
308 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
309 g_object_unref (poppler_page);
315 pdf_document_get_page_size (EvDocument *document,
320 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
322 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
326 pdf_document_get_page_label (EvDocument *document,
331 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
333 g_object_get (G_OBJECT (page->backend_page),
340 pdf_document_has_attachments (EvDocument *document)
342 PdfDocument *pdf_document;
344 pdf_document = PDF_DOCUMENT (document);
346 return poppler_document_has_attachments (pdf_document->document);
349 struct SaveToBufferData {
355 attachment_save_to_buffer_callback (const gchar *buf,
360 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
364 if (sdata->len + count > sdata->max) {
365 new_max = MAX (sdata->max * 2, sdata->len + count);
366 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
368 sdata->buffer = new_buffer;
369 sdata->max = new_max;
372 memcpy (sdata->buffer + sdata->len, buf, count);
379 attachment_save_to_buffer (PopplerAttachment *attachment,
384 static const gint initial_max = 1024;
385 struct SaveToBufferData sdata;
390 sdata.buffer = (gchar *) g_malloc (initial_max);
391 sdata.max = initial_max;
394 if (! poppler_attachment_save_to_callback (attachment,
395 attachment_save_to_buffer_callback,
398 g_free (sdata.buffer);
402 *buffer = sdata.buffer;
403 *buffer_size = sdata.len;
409 pdf_document_get_attachments (EvDocument *document)
411 PdfDocument *pdf_document;
414 GList *retval = NULL;
416 pdf_document = PDF_DOCUMENT (document);
418 if (!pdf_document_has_attachments (document))
421 attachments = poppler_document_get_attachments (pdf_document->document);
423 for (list = attachments; list; list = list->next) {
424 PopplerAttachment *attachment;
425 EvAttachment *ev_attachment;
428 GError *error = NULL;
430 attachment = (PopplerAttachment *) list->data;
432 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
433 ev_attachment = ev_attachment_new (attachment->name,
434 attachment->description,
439 retval = g_list_prepend (retval, ev_attachment);
442 g_warning ("%s", error->message);
443 g_error_free (error);
449 g_object_unref (attachment);
452 return g_list_reverse (retval);
455 static cairo_surface_t *
456 pdf_page_render (PopplerPage *page,
461 cairo_surface_t *surface;
463 #ifdef HAVE_POPPLER_PAGE_RENDER
466 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
468 memset (cairo_image_surface_get_data (surface), 0xff,
469 cairo_image_surface_get_height (surface) *
470 cairo_image_surface_get_stride (surface));
472 cr = cairo_create (surface);
473 switch (rc->rotation) {
475 cairo_translate (cr, width, 0);
478 cairo_translate (cr, width, height);
481 cairo_translate (cr, 0, height);
484 cairo_translate (cr, 0, 0);
486 cairo_scale (cr, rc->scale, rc->scale);
487 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
488 poppler_page_render (page, cr);
490 #else /* HAVE_POPPLER_PAGE_RENDER */
493 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
497 poppler_page_render_to_pixbuf (page,
503 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
504 g_object_unref (pixbuf);
505 #endif /* HAVE_POPPLER_PAGE_RENDER */
510 static cairo_surface_t *
511 pdf_document_render (EvDocument *document,
514 PdfDocument *pdf_document;
515 PopplerPage *poppler_page;
516 double width_points, height_points;
519 poppler_page = POPPLER_PAGE (rc->page->backend_page);
521 poppler_page_get_size (poppler_page,
522 &width_points, &height_points);
524 if (rc->rotation == 90 || rc->rotation == 270) {
525 width = (int) ((height_points * rc->scale) + 0.5);
526 height = (int) ((width_points * rc->scale) + 0.5);
528 width = (int) ((width_points * rc->scale) + 0.5);
529 height = (int) ((height_points * rc->scale) + 0.5);
532 return pdf_page_render (poppler_page,
536 /* EvDocumentSecurity */
539 pdf_document_has_document_security (EvDocumentSecurity *document_security)
541 /* FIXME: do we really need to have this? */
546 pdf_document_set_password (EvDocumentSecurity *document_security,
547 const char *password)
549 PdfDocument *document = PDF_DOCUMENT (document_security);
551 if (document->password)
552 g_free (document->password);
554 document->password = g_strdup (password);
557 static EvDocumentInfo *
558 pdf_document_get_info (EvDocument *document)
560 EvDocumentInfo *info;
561 PopplerPageLayout layout;
562 PopplerPageMode mode;
563 PopplerViewerPreferences view_prefs;
564 PopplerPermissions permissions;
567 info = g_new0 (EvDocumentInfo, 1);
569 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
570 EV_DOCUMENT_INFO_FORMAT |
571 EV_DOCUMENT_INFO_AUTHOR |
572 EV_DOCUMENT_INFO_SUBJECT |
573 EV_DOCUMENT_INFO_KEYWORDS |
574 EV_DOCUMENT_INFO_LAYOUT |
575 EV_DOCUMENT_INFO_START_MODE |
576 EV_DOCUMENT_INFO_PERMISSIONS |
577 EV_DOCUMENT_INFO_UI_HINTS |
578 EV_DOCUMENT_INFO_CREATOR |
579 EV_DOCUMENT_INFO_PRODUCER |
580 EV_DOCUMENT_INFO_CREATION_DATE |
581 EV_DOCUMENT_INFO_MOD_DATE |
582 EV_DOCUMENT_INFO_LINEARIZED |
583 EV_DOCUMENT_INFO_N_PAGES |
584 EV_DOCUMENT_INFO_SECURITY |
585 EV_DOCUMENT_INFO_PAPER_SIZE;
587 g_object_get (PDF_DOCUMENT (document)->document,
588 "title", &(info->title),
589 "format", &(info->format),
590 "author", &(info->author),
591 "subject", &(info->subject),
592 "keywords", &(info->keywords),
594 "page-layout", &layout,
595 "viewer-preferences", &view_prefs,
596 "permissions", &permissions,
597 "creator", &(info->creator),
598 "producer", &(info->producer),
599 "creation-date", &(info->creation_date),
600 "mod-date", &(info->modified_date),
601 "linearized", &(info->linearized),
604 info->n_pages = ev_document_get_n_pages (document);
606 if (info->n_pages > 0) {
607 page = ev_document_get_page (document, 0);
608 ev_document_get_page_size (document, page,
609 &(info->paper_width),
610 &(info->paper_height));
611 g_object_unref (page);
615 info->paper_width = info->paper_width / 72.0f * 25.4f;
616 info->paper_height = info->paper_height / 72.0f * 25.4f;
620 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
621 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
623 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
624 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
626 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
627 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
629 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
630 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
631 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
632 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
634 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
635 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
642 case POPPLER_PAGE_MODE_NONE:
643 info->mode = EV_DOCUMENT_MODE_NONE;
645 case POPPLER_PAGE_MODE_USE_THUMBS:
646 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
648 case POPPLER_PAGE_MODE_USE_OC:
649 info->mode = EV_DOCUMENT_MODE_USE_OC;
651 case POPPLER_PAGE_MODE_FULL_SCREEN:
652 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
654 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
655 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
661 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
662 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
664 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
665 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
667 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
668 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
670 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
671 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
673 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
674 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
676 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
677 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
679 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
680 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
683 info->permissions = 0;
684 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
685 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
687 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
688 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
690 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
691 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
693 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
694 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
697 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
698 /* translators: this is the document security state */
699 info->security = g_strdup (_("Yes"));
701 /* translators: this is the document security state */
702 info->security = g_strdup (_("No"));
709 pdf_document_document_iface_init (EvDocumentIface *iface)
711 iface->save = pdf_document_save;
712 iface->load = pdf_document_load;
713 iface->get_n_pages = pdf_document_get_n_pages;
714 iface->get_page = pdf_document_get_page;
715 iface->get_page_size = pdf_document_get_page_size;
716 iface->get_page_label = pdf_document_get_page_label;
717 iface->has_attachments = pdf_document_has_attachments;
718 iface->get_attachments = pdf_document_get_attachments;
719 iface->render = pdf_document_render;
720 iface->get_info = pdf_document_get_info;
724 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
726 iface->has_document_security = pdf_document_has_document_security;
727 iface->set_password = pdf_document_set_password;
731 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
733 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
736 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
738 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
742 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
745 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
748 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
750 if (pdf_document->font_info == NULL) {
751 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
754 if (pdf_document->fonts_iter) {
755 poppler_fonts_iter_free (pdf_document->fonts_iter);
758 pdf_document->fonts_scanned_pages += n_pages;
760 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
761 &pdf_document->fonts_iter);
763 pdf_document->fonts_scanned_pages = 0;
764 poppler_font_info_free (pdf_document->font_info);
765 pdf_document->font_info = NULL;
772 font_type_to_string (PopplerFontType type)
775 case POPPLER_FONT_TYPE_TYPE1:
777 case POPPLER_FONT_TYPE_TYPE1C:
779 case POPPLER_FONT_TYPE_TYPE3:
781 case POPPLER_FONT_TYPE_TRUETYPE:
782 return _("TrueType");
783 case POPPLER_FONT_TYPE_CID_TYPE0:
784 return _("Type 1 (CID)");
785 case POPPLER_FONT_TYPE_CID_TYPE0C:
786 return _("Type 1C (CID)");
787 case POPPLER_FONT_TYPE_CID_TYPE2:
788 return _("TrueType (CID)");
790 return _("Unknown font type");
795 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
798 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
799 PopplerFontsIter *iter = pdf_document->fonts_iter;
801 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
807 GtkTreeIter list_iter;
810 const char *embedded;
813 name = poppler_fonts_iter_get_name (iter);
819 type = font_type_to_string (
820 poppler_fonts_iter_get_font_type (iter));
822 if (poppler_fonts_iter_is_embedded (iter)) {
823 if (poppler_fonts_iter_is_subset (iter))
824 embedded = _("Embedded subset");
826 embedded = _("Embedded");
828 embedded = _("Not embedded");
831 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
833 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
834 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
835 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
836 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
840 } while (poppler_fonts_iter_next (iter));
844 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
846 iface->fill_model = pdf_document_fonts_fill_model;
847 iface->scan = pdf_document_fonts_scan;
848 iface->get_progress = pdf_document_fonts_get_progress;
852 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
854 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
855 PopplerIndexIter *iter;
857 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
859 iter = poppler_index_iter_new (pdf_document->document);
862 poppler_index_iter_free (iter);
868 ev_link_dest_from_dest (PdfDocument *pdf_document,
871 EvLinkDest *ev_dest = NULL;
872 const char *unimplemented_dest = NULL;
874 g_assert (dest != NULL);
876 switch (dest->type) {
877 case POPPLER_DEST_XYZ: {
878 PopplerPage *poppler_page;
881 poppler_page = poppler_document_get_page (pdf_document->document,
882 MAX (0, dest->page_num - 1));
883 poppler_page_get_size (poppler_page, NULL, &height);
884 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
891 g_object_unref (poppler_page);
894 case POPPLER_DEST_FIT:
895 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
897 case POPPLER_DEST_FITH: {
898 PopplerPage *poppler_page;
901 poppler_page = poppler_document_get_page (pdf_document->document,
902 MAX (0, dest->page_num - 1));
903 poppler_page_get_size (poppler_page, NULL, &height);
904 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
907 g_object_unref (poppler_page);
910 case POPPLER_DEST_FITV:
911 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
915 case POPPLER_DEST_FITR: {
916 PopplerPage *poppler_page;
919 poppler_page = poppler_document_get_page (pdf_document->document,
920 MAX (0, dest->page_num - 1));
921 poppler_page_get_size (poppler_page, NULL, &height);
922 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
924 height - dest->bottom,
927 g_object_unref (poppler_page);
930 case POPPLER_DEST_FITB:
931 unimplemented_dest = "POPPLER_DEST_FITB";
933 case POPPLER_DEST_FITBH:
934 unimplemented_dest = "POPPLER_DEST_FITBH";
936 case POPPLER_DEST_FITBV:
937 unimplemented_dest = "POPPLER_DEST_FITBV";
939 case POPPLER_DEST_NAMED:
940 ev_dest = ev_link_dest_new_named (dest->named_dest);
942 case POPPLER_DEST_UNKNOWN:
943 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
947 if (unimplemented_dest) {
948 g_warning ("Unimplemented destination: %s, please post a "
949 "bug report in Evince bugzilla "
950 "(http://bugzilla.gnome.org) with a testcase.",
955 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
961 ev_link_from_action (PdfDocument *pdf_document,
962 PopplerAction *action)
965 EvLinkAction *ev_action = NULL;
966 const char *unimplemented_action = NULL;
968 switch (action->type) {
969 case POPPLER_ACTION_NONE:
971 case POPPLER_ACTION_GOTO_DEST: {
974 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
975 ev_action = ev_link_action_new_dest (dest);
978 case POPPLER_ACTION_GOTO_REMOTE: {
981 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
982 ev_action = ev_link_action_new_remote (dest,
983 action->goto_remote.file_name);
987 case POPPLER_ACTION_LAUNCH:
988 ev_action = ev_link_action_new_launch (action->launch.file_name,
989 action->launch.params);
991 case POPPLER_ACTION_URI:
992 ev_action = ev_link_action_new_external_uri (action->uri.uri);
994 case POPPLER_ACTION_NAMED:
995 ev_action = ev_link_action_new_named (action->named.named_dest);
997 case POPPLER_ACTION_MOVIE:
998 unimplemented_action = "POPPLER_ACTION_MOVIE";
1000 case POPPLER_ACTION_UNKNOWN:
1001 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1004 if (unimplemented_action) {
1005 g_warning ("Unimplemented action: %s, please post a bug report "
1006 "in Evince bugzilla (http://bugzilla.gnome.org) "
1007 "with a testcase.", unimplemented_action);
1010 link = ev_link_new (action->any.title, ev_action);
1016 build_tree (PdfDocument *pdf_document,
1017 GtkTreeModel *model,
1018 GtkTreeIter *parent,
1019 PopplerIndexIter *iter)
1023 GtkTreeIter tree_iter;
1024 PopplerIndexIter *child;
1025 PopplerAction *action;
1026 EvLink *link = NULL;
1030 action = poppler_index_iter_get_action (iter);
1031 expand = poppler_index_iter_is_open (iter);
1036 switch (action->type) {
1037 case POPPLER_ACTION_GOTO_DEST: {
1038 /* For bookmarks, solve named destinations */
1039 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1041 EvLinkDest *ev_dest = NULL;
1042 EvLinkAction *ev_action;
1044 dest = poppler_document_find_dest (pdf_document->document,
1045 action->goto_dest.dest->named_dest);
1047 link = ev_link_from_action (pdf_document, action);
1051 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1052 poppler_dest_free (dest);
1054 ev_action = ev_link_action_new_dest (ev_dest);
1055 link = ev_link_new (action->any.title, ev_action);
1057 link = ev_link_from_action (pdf_document, action);
1062 link = ev_link_from_action (pdf_document, action);
1066 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1067 poppler_action_free (action);
1069 g_object_unref (link);
1074 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1075 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1077 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1078 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1079 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1080 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1083 g_free (title_markup);
1084 g_object_unref (link);
1086 child = poppler_index_iter_get_child (iter);
1088 build_tree (pdf_document, model, &tree_iter, child);
1089 poppler_index_iter_free (child);
1090 poppler_action_free (action);
1092 } while (poppler_index_iter_next (iter));
1095 static GtkTreeModel *
1096 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1098 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1099 GtkTreeModel *model = NULL;
1100 PopplerIndexIter *iter;
1102 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1104 iter = poppler_index_iter_new (pdf_document->document);
1105 /* Create the model if we have items*/
1107 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1112 build_tree (pdf_document, model, NULL, iter);
1113 poppler_index_iter_free (iter);
1120 pdf_document_links_get_links (EvDocumentLinks *document_links,
1123 PdfDocument *pdf_document;
1124 PopplerPage *poppler_page;
1125 GList *retval = NULL;
1126 GList *mapping_list;
1130 pdf_document = PDF_DOCUMENT (document_links);
1131 poppler_page = poppler_document_get_page (pdf_document->document,
1133 mapping_list = poppler_page_get_link_mapping (poppler_page);
1134 poppler_page_get_size (poppler_page, NULL, &height);
1136 for (list = mapping_list; list; list = list->next) {
1137 PopplerLinkMapping *link_mapping;
1138 EvLinkMapping *ev_link_mapping;
1140 link_mapping = (PopplerLinkMapping *)list->data;
1141 ev_link_mapping = g_new (EvLinkMapping, 1);
1142 ev_link_mapping->link = ev_link_from_action (pdf_document,
1143 link_mapping->action);
1144 ev_link_mapping->x1 = link_mapping->area.x1;
1145 ev_link_mapping->x2 = link_mapping->area.x2;
1146 /* Invert this for X-style coordinates */
1147 ev_link_mapping->y1 = height - link_mapping->area.y2;
1148 ev_link_mapping->y2 = height - link_mapping->area.y1;
1150 retval = g_list_prepend (retval, ev_link_mapping);
1153 poppler_page_free_link_mapping (mapping_list);
1154 g_object_unref (poppler_page);
1156 return g_list_reverse (retval);
1160 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1161 const gchar *link_name)
1163 PdfDocument *pdf_document;
1165 EvLinkDest *ev_dest = NULL;
1167 pdf_document = PDF_DOCUMENT (document_links);
1168 dest = poppler_document_find_dest (pdf_document->document,
1171 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1172 poppler_dest_free (dest);
1179 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1181 iface->has_document_links = pdf_document_links_has_document_links;
1182 iface->get_links_model = pdf_document_links_get_links_model;
1183 iface->get_links = pdf_document_links_get_links;
1184 iface->find_link_dest = pdf_document_links_find_link_dest;
1188 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1191 GList *retval = NULL;
1192 PdfDocument *pdf_document;
1193 PopplerPage *poppler_page;
1194 GList *mapping_list;
1197 pdf_document = PDF_DOCUMENT (document_images);
1198 poppler_page = poppler_document_get_page (pdf_document->document, page);
1199 mapping_list = poppler_page_get_image_mapping (poppler_page);
1201 for (list = mapping_list; list; list = list->next) {
1202 PopplerImageMapping *image_mapping;
1203 EvImageMapping *ev_image_mapping;
1205 image_mapping = (PopplerImageMapping *)list->data;
1207 ev_image_mapping = g_new (EvImageMapping, 1);
1209 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1210 ev_image_mapping->x1 = image_mapping->area.x1;
1211 ev_image_mapping->x2 = image_mapping->area.x2;
1212 ev_image_mapping->y1 = image_mapping->area.y1;
1213 ev_image_mapping->y2 = image_mapping->area.y2;
1215 retval = g_list_prepend (retval, ev_image_mapping);
1218 poppler_page_free_image_mapping (mapping_list);
1219 g_object_unref (poppler_page);
1221 return g_list_reverse (retval);
1225 pdf_document_images_get_image (EvDocumentImages *document_images,
1228 PdfDocument *pdf_document;
1229 PopplerPage *poppler_page;
1230 cairo_surface_t *surface;
1231 GdkPixbuf *retval = NULL;
1233 pdf_document = PDF_DOCUMENT (document_images);
1234 poppler_page = poppler_document_get_page (pdf_document->document,
1235 ev_image_get_page (image));
1237 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1239 retval = ev_document_misc_pixbuf_from_surface (surface);
1240 cairo_surface_destroy (surface);
1243 g_object_unref (poppler_page);
1249 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1251 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1252 iface->get_image = pdf_document_images_get_image;
1256 make_thumbnail_for_page (PdfDocument *pdf_document,
1257 PopplerPage *poppler_page,
1258 EvRenderContext *rc)
1263 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1264 rc, &width, &height);
1265 #ifdef POPPLER_WITH_GDK
1266 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1268 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1270 ev_document_fc_mutex_lock ();
1271 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1273 rc->scale, rc->rotation, pixbuf);
1274 ev_document_fc_mutex_unlock ();
1276 cairo_surface_t *surface;
1278 ev_document_fc_mutex_lock ();
1279 surface = pdf_page_render (poppler_page, width, height, rc);
1280 ev_document_fc_mutex_unlock ();
1282 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1283 cairo_surface_destroy (surface);
1284 #endif /* POPPLER_WITH_GDK */
1290 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1291 EvRenderContext *rc,
1294 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1295 PopplerPage *poppler_page;
1296 GdkPixbuf *pixbuf = NULL;
1297 GdkPixbuf *border_pixbuf;
1299 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1301 #ifdef POPPLER_WITH_GDK
1302 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1304 cairo_surface_t *surface;
1306 surface = poppler_page_get_thumbnail (poppler_page);
1308 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1309 cairo_surface_destroy (surface);
1311 #endif /* POPPLER_WITH_GDK */
1314 /* Rotate provided thumbnail if needed */
1315 GdkPixbuf *rotated_pixbuf;
1317 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1318 (GdkPixbufRotation) (360 - rc->rotation));
1319 g_object_unref (pixbuf);
1320 pixbuf = rotated_pixbuf;
1322 /* There is no provided thumbnail. We need to make one. */
1323 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1327 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1328 g_object_unref (pixbuf);
1329 pixbuf = border_pixbuf;
1336 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1337 EvRenderContext *rc,
1341 PopplerPage *poppler_page;
1344 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1346 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1348 if (!has_thumb || *width <= 0 || *height <= 0) {
1349 double page_width, page_height;
1351 poppler_page_get_size (poppler_page, &page_width, &page_height);
1353 *width = (gint) MAX (page_width * rc->scale, 1);
1354 *height = (gint) MAX (page_height * rc->scale, 1);
1357 if (rc->rotation == 90 || rc->rotation == 270) {
1367 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1369 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1370 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1375 pdf_document_find_find_text (EvDocumentFind *document_find,
1378 gboolean case_sensitive)
1381 PopplerPage *poppler_page;
1384 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1385 g_return_val_if_fail (text != NULL, NULL);
1387 poppler_page = POPPLER_PAGE (page->backend_page);
1389 matches = poppler_page_find_text (poppler_page, text);
1393 poppler_page_get_size (poppler_page, NULL, &height);
1394 for (l = matches; l && l->data; l = g_list_next (l)) {
1395 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1399 rect->y1 = height - rect->y2;
1400 rect->y2 = height - tmp;
1407 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1409 iface->find_text = pdf_document_find_find_text;
1413 pdf_print_context_free (PdfPrintContext *ctx)
1418 #ifdef HAVE_CAIRO_PRINT
1420 cairo_destroy (ctx->cr);
1425 poppler_ps_file_free (ctx->ps_file);
1426 ctx->ps_file = NULL;
1433 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1434 EvFileExporterContext *fc)
1436 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1437 PdfPrintContext *ctx;
1438 #ifdef HAVE_CAIRO_PRINT
1439 gdouble width, height;
1440 cairo_surface_t *surface = NULL;
1443 if (pdf_document->print_ctx)
1444 pdf_print_context_free (pdf_document->print_ctx);
1445 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1446 ctx = pdf_document->print_ctx;
1447 ctx->format = fc->format;
1449 #ifdef HAVE_CAIRO_PRINT
1450 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1452 ctx->paper_width = fc->paper_width;
1453 ctx->paper_height = fc->paper_height;
1455 switch (fc->pages_per_sheet) {
1483 ctx->pages_printed = 0;
1485 switch (fc->format) {
1486 case EV_FILE_FORMAT_PS:
1487 #ifdef HAVE_CAIRO_PS
1488 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1491 case EV_FILE_FORMAT_PDF:
1492 #ifdef HAVE_CAIRO_PDF
1493 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1497 g_assert_not_reached ();
1500 ctx->cr = cairo_create (surface);
1501 cairo_surface_destroy (surface);
1503 #else /* HAVE_CAIRO_PRINT */
1504 if (ctx->format == EV_FILE_FORMAT_PS) {
1505 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1506 fc->filename, fc->first_page,
1507 fc->last_page - fc->first_page + 1);
1508 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1509 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1511 #endif /* HAVE_CAIRO_PRINT */
1515 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1517 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1518 PdfPrintContext *ctx = pdf_document->print_ctx;
1520 g_return_if_fail (pdf_document->print_ctx != NULL);
1522 ctx->pages_printed = 0;
1524 #ifdef HAVE_CAIRO_PRINT
1525 if (ctx->paper_width > ctx->paper_height) {
1526 if (ctx->format == EV_FILE_FORMAT_PS) {
1527 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1530 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1531 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1536 #endif /* HAVE_CAIRO_PRINT */
1540 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1541 EvRenderContext *rc)
1543 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1544 PdfPrintContext *ctx = pdf_document->print_ctx;
1545 PopplerPage *poppler_page;
1546 #ifdef HAVE_CAIRO_PRINT
1547 gdouble page_width, page_height;
1550 gdouble width, height;
1551 gdouble pwidth, pheight;
1552 gdouble xscale, yscale;
1555 g_return_if_fail (pdf_document->print_ctx != NULL);
1557 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1559 #ifdef HAVE_CAIRO_PRINT
1560 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1561 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1562 poppler_page_get_size (poppler_page, &page_width, &page_height);
1564 if (page_width > page_height && page_width > ctx->paper_width) {
1570 /* Use always portrait mode and rotate when necessary */
1571 if (ctx->paper_width > ctx->paper_height) {
1572 width = ctx->paper_height;
1573 height = ctx->paper_width;
1576 width = ctx->paper_width;
1577 height = ctx->paper_height;
1580 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1593 page_width = page_height;
1597 pwidth = width / ctx->pages_x;
1598 pheight = height / ctx->pages_y;
1600 if ((page_width > pwidth || page_height > pheight) ||
1601 (page_width < pwidth && page_height < pheight)) {
1602 xscale = pwidth / page_width;
1603 yscale = pheight / page_height;
1605 if (yscale < xscale) {
1612 xscale = yscale = 1;
1617 cairo_save (ctx->cr);
1619 cairo_matrix_t matrix;
1621 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1622 cairo_matrix_init (&matrix,
1626 cairo_transform (ctx->cr, &matrix);
1629 cairo_translate (ctx->cr,
1630 x * (rotate ? pheight : pwidth),
1631 y * (rotate ? pwidth : pheight));
1632 cairo_scale (ctx->cr, xscale, yscale);
1634 poppler_page_render_for_printing (poppler_page, ctx->cr);
1636 ctx->pages_printed++;
1638 cairo_restore (ctx->cr);
1639 #else /* HAVE_CAIRO_PRINT */
1640 if (ctx->format == EV_FILE_FORMAT_PS)
1641 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1642 #endif /* HAVE_CAIRO_PRINT */
1646 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1648 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1649 PdfPrintContext *ctx = pdf_document->print_ctx;
1651 g_return_if_fail (pdf_document->print_ctx != NULL);
1653 #ifdef HAVE_CAIRO_PRINT
1654 cairo_show_page (ctx->cr);
1659 pdf_document_file_exporter_end (EvFileExporter *exporter)
1661 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1663 pdf_print_context_free (pdf_document->print_ctx);
1664 pdf_document->print_ctx = NULL;
1667 static EvFileExporterCapabilities
1668 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1670 return (EvFileExporterCapabilities) (
1671 EV_FILE_EXPORTER_CAN_PAGE_SET |
1672 EV_FILE_EXPORTER_CAN_COPIES |
1673 EV_FILE_EXPORTER_CAN_COLLATE |
1674 EV_FILE_EXPORTER_CAN_REVERSE |
1675 EV_FILE_EXPORTER_CAN_SCALE |
1676 #ifdef HAVE_CAIRO_PRINT
1677 #ifdef HAVE_POPPLER_PAGE_RENDER
1678 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1682 #ifdef HAVE_CAIRO_PDF
1683 #ifdef HAVE_POPPLER_PAGE_RENDER
1684 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1687 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1691 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1693 iface->begin = pdf_document_file_exporter_begin;
1694 iface->begin_page = pdf_document_file_exporter_begin_page;
1695 iface->do_page = pdf_document_file_exporter_do_page;
1696 iface->end_page = pdf_document_file_exporter_end_page;
1697 iface->end = pdf_document_file_exporter_end;
1698 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1702 pdf_selection_render_selection (EvSelection *selection,
1703 EvRenderContext *rc,
1704 cairo_surface_t **surface,
1705 EvRectangle *points,
1706 EvRectangle *old_points,
1707 EvSelectionStyle style,
1711 PopplerPage *poppler_page;
1712 double width_points, height_points;
1715 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1717 poppler_page_get_size (poppler_page,
1718 &width_points, &height_points);
1719 width = (int) ((width_points * rc->scale) + 0.5);
1720 height = (int) ((height_points * rc->scale) + 0.5);
1722 #ifdef HAVE_POPPLER_PAGE_RENDER
1724 PopplerColor text_color, base_color;
1726 text_color.red = text->red;
1727 text_color.green = text->green;
1728 text_color.blue = text->blue;
1730 base_color.red = base->red;
1731 base_color.green = base->green;
1732 base_color.blue = base->blue;
1734 if (*surface == NULL) {
1735 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1740 cr = cairo_create (*surface);
1741 cairo_scale (cr, rc->scale, rc->scale);
1742 cairo_surface_set_device_offset (*surface, 0, 0);
1743 memset (cairo_image_surface_get_data (*surface), 0x00,
1744 cairo_image_surface_get_height (*surface) *
1745 cairo_image_surface_get_stride (*surface));
1746 poppler_page_render_selection (poppler_page,
1748 (PopplerRectangle *)points,
1749 (PopplerRectangle *)old_points,
1750 (PopplerSelectionStyle)style,
1754 #else /* HAVE_POPPLER_PAGE_RENDER */
1757 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1761 poppler_page_render_selection_to_pixbuf (poppler_page,
1762 rc->scale, rc->rotation, pixbuf,
1763 (PopplerRectangle *)points,
1764 (PopplerRectangle *)old_points,
1765 (PopplerSelectionStyle)style,
1769 cairo_surface_destroy (*surface);
1770 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1771 g_object_unref (pixbuf);
1772 #endif /* HAVE_POPPLER_PAGE_RENDER */
1776 pdf_selection_get_selected_text (EvSelection *selection,
1777 EvRenderContext *rc,
1778 EvSelectionStyle style,
1779 EvRectangle *points)
1781 PopplerPage *poppler_page;
1786 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1788 poppler_page_get_size (poppler_page, NULL, &height);
1790 r.y1 = height - points->y2;
1792 r.y2 = height - points->y1;
1794 retval = poppler_page_get_text (poppler_page,
1795 (PopplerSelectionStyle)style,
1802 create_gdk_region_from_poppler_region (GList *region)
1807 retval = gdk_region_new ();
1809 for (l = region; l; l = g_list_next (l)) {
1810 PopplerRectangle *rectangle;
1813 rectangle = (PopplerRectangle *)l->data;
1815 rect.x = (gint) rectangle->x1;
1816 rect.y = (gint) rectangle->y1;
1817 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1818 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1819 gdk_region_union_with_rect (retval, &rect);
1821 poppler_rectangle_free (rectangle);
1828 pdf_selection_get_selection_region (EvSelection *selection,
1829 EvRenderContext *rc,
1830 EvSelectionStyle style,
1831 EvRectangle *points)
1833 PopplerPage *poppler_page;
1837 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1839 region = poppler_page_get_selection_region (poppler_page,
1841 (PopplerSelectionStyle)style,
1842 (PopplerRectangle *) points);
1843 retval = create_gdk_region_from_poppler_region (region);
1844 g_list_free (region);
1850 pdf_selection_get_selection_map (EvSelection *selection,
1851 EvRenderContext *rc)
1853 PopplerPage *poppler_page;
1854 PopplerRectangle points;
1858 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1862 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1864 region = poppler_page_get_selection_region (poppler_page, 1.0,
1865 POPPLER_SELECTION_GLYPH,
1867 retval = create_gdk_region_from_poppler_region (region);
1868 g_list_free (region);
1874 pdf_selection_iface_init (EvSelectionIface *iface)
1876 iface->render_selection = pdf_selection_render_selection;
1877 iface->get_selected_text = pdf_selection_get_selected_text;
1878 iface->get_selection_region = pdf_selection_get_selection_region;
1879 iface->get_selection_map = pdf_selection_get_selection_map;
1882 /* Page Transitions */
1884 pdf_document_get_page_duration (EvDocumentTransition *trans,
1887 PdfDocument *pdf_document;
1888 PopplerPage *poppler_page;
1889 gdouble duration = -1;
1891 pdf_document = PDF_DOCUMENT (trans);
1892 poppler_page = poppler_document_get_page (pdf_document->document, page);
1896 duration = poppler_page_get_duration (poppler_page);
1897 g_object_unref (poppler_page);
1902 static EvTransitionEffect *
1903 pdf_document_get_effect (EvDocumentTransition *trans,
1906 PdfDocument *pdf_document;
1907 PopplerPage *poppler_page;
1908 PopplerPageTransition *page_transition;
1909 EvTransitionEffect *effect;
1911 pdf_document = PDF_DOCUMENT (trans);
1912 poppler_page = poppler_document_get_page (pdf_document->document, page);
1917 page_transition = poppler_page_get_transition (poppler_page);
1919 if (!page_transition) {
1920 g_object_unref (poppler_page);
1924 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1925 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1926 "alignment", page_transition->alignment,
1927 "direction", page_transition->direction,
1928 "duration", page_transition->duration,
1929 "angle", page_transition->angle,
1930 "scale", page_transition->scale,
1931 "rectangular", page_transition->rectangular,
1934 poppler_page_transition_free (page_transition);
1935 g_object_unref (poppler_page);
1941 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1943 iface->get_page_duration = pdf_document_get_page_duration;
1944 iface->get_effect = pdf_document_get_effect;
1949 pdf_document_get_crop_box (EvDocument *document,
1953 PdfDocument *pdf_document;
1954 PopplerPage *poppler_page;
1955 PopplerRectangle poppler_rect;
1957 pdf_document = PDF_DOCUMENT (document);
1958 poppler_page = poppler_document_get_page (pdf_document->document, page);
1959 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1960 rect->x1 = poppler_rect.x1;
1961 rect->x2 = poppler_rect.x2;
1962 rect->y1 = poppler_rect.y1;
1963 rect->y2 = poppler_rect.y2;
1966 static EvFormField *
1967 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1969 EvFormField *ev_field = NULL;
1972 gboolean is_read_only;
1974 id = poppler_form_field_get_id (poppler_field);
1975 font_size = poppler_form_field_get_font_size (poppler_field);
1976 is_read_only = poppler_form_field_is_read_only (poppler_field);
1978 switch (poppler_form_field_get_field_type (poppler_field)) {
1979 case POPPLER_FORM_FIELD_TEXT: {
1980 EvFormFieldText *field_text;
1981 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1983 switch (poppler_form_field_text_get_text_type (poppler_field)) {
1984 case POPPLER_FORM_TEXT_NORMAL:
1985 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1987 case POPPLER_FORM_TEXT_MULTILINE:
1988 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
1990 case POPPLER_FORM_TEXT_FILE_SELECT:
1991 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
1995 ev_field = ev_form_field_text_new (id, ev_text_type);
1996 field_text = EV_FORM_FIELD_TEXT (ev_field);
1998 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
1999 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2000 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2001 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2002 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2003 field_text->text = poppler_form_field_text_get_text (poppler_field);
2007 case POPPLER_FORM_FIELD_BUTTON: {
2008 EvFormFieldButton *field_button;
2009 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2011 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2012 case POPPLER_FORM_BUTTON_PUSH:
2013 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2015 case POPPLER_FORM_BUTTON_CHECK:
2016 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2018 case POPPLER_FORM_BUTTON_RADIO:
2019 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2023 ev_field = ev_form_field_button_new (id, ev_button_type);
2024 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2026 field_button->state = poppler_form_field_button_get_state (poppler_field);
2029 case POPPLER_FORM_FIELD_CHOICE: {
2030 EvFormFieldChoice *field_choice;
2031 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2033 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2034 case POPPLER_FORM_CHOICE_COMBO:
2035 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2037 case EV_FORM_FIELD_CHOICE_LIST:
2038 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2042 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2043 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2045 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2046 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2047 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2048 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2050 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2051 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2052 if (field_choice->is_editable)
2053 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2056 case POPPLER_FORM_FIELD_SIGNATURE:
2058 ev_field = ev_form_field_signature_new (id);
2060 case POPPLER_FORM_FIELD_UNKNOWN:
2064 ev_field->font_size = font_size;
2065 ev_field->is_read_only = is_read_only;
2071 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2074 PopplerPage *poppler_page;
2075 GList *retval = NULL;
2080 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2082 poppler_page = POPPLER_PAGE (page->backend_page);
2083 fields = poppler_page_get_form_field_mapping (poppler_page);
2084 poppler_page_get_size (poppler_page, NULL, &height);
2086 for (list = fields; list; list = list->next) {
2087 PopplerFormFieldMapping *mapping;
2088 EvFormFieldMapping *field_mapping;
2089 EvFormField *ev_field;
2091 mapping = (PopplerFormFieldMapping *)list->data;
2093 ev_field = ev_form_field_from_poppler_field (mapping->field);
2097 field_mapping = g_new0 (EvFormFieldMapping, 1);
2098 field_mapping->x1 = mapping->area.x1;
2099 field_mapping->x2 = mapping->area.x2;
2100 field_mapping->y1 = height - mapping->area.y2;
2101 field_mapping->y2 = height - mapping->area.y1;
2102 field_mapping->field = ev_field;
2103 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2105 g_object_set_data_full (G_OBJECT (ev_field),
2107 g_object_ref (mapping->field),
2108 (GDestroyNotify) g_object_unref);
2110 retval = g_list_prepend (retval, field_mapping);
2113 poppler_page_free_form_field_mapping (fields);
2115 return g_list_reverse (retval);
2119 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2123 PopplerFormField *poppler_field;
2126 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2130 text = poppler_form_field_text_get_text (poppler_field);
2136 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2140 PopplerFormField *poppler_field;
2142 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2145 poppler_form_field_text_set_text (poppler_field, text);
2149 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2153 PopplerFormField *poppler_field;
2155 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2159 poppler_form_field_button_set_state (poppler_field, state);
2163 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2166 PopplerFormField *poppler_field;
2169 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2173 state = poppler_form_field_button_get_state (poppler_field);
2179 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2183 PopplerFormField *poppler_field;
2186 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2190 text = poppler_form_field_choice_get_item (poppler_field, index);
2196 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2199 PopplerFormField *poppler_field;
2202 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2206 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2212 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2216 PopplerFormField *poppler_field;
2219 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2223 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2229 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2233 PopplerFormField *poppler_field;
2235 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2239 poppler_form_field_choice_select_item (poppler_field, index);
2243 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2247 PopplerFormField *poppler_field;
2249 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2253 poppler_form_field_choice_toggle_item (poppler_field, index);
2257 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2260 PopplerFormField *poppler_field;
2262 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2266 poppler_form_field_choice_unselect_all (poppler_field);
2270 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2274 PopplerFormField *poppler_field;
2276 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2280 poppler_form_field_choice_set_text (poppler_field, text);
2284 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2287 PopplerFormField *poppler_field;
2290 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2294 text = poppler_form_field_choice_get_text (poppler_field);
2300 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2302 iface->get_form_fields = pdf_document_forms_get_form_fields;
2303 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2304 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2305 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2306 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2307 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2308 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2309 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2310 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2311 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2312 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2313 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2314 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2319 pdf_document_layers_has_layers (EvDocumentLayers *document)
2321 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2322 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2323 PopplerLayersIter *iter;
2325 iter = poppler_layers_iter_new (pdf_document->document);
2328 poppler_layers_iter_free (iter);
2333 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2336 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2338 build_layers_tree (PdfDocument *pdf_document,
2339 GtkTreeModel *model,
2340 GtkTreeIter *parent,
2341 PopplerLayersIter *iter)
2344 GtkTreeIter tree_iter;
2345 PopplerLayersIter *child;
2346 PopplerLayer *layer;
2347 EvLayer *ev_layer = NULL;
2352 layer = poppler_layers_iter_get_layer (iter);
2354 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2355 visible = poppler_layer_is_visible (layer);
2356 rb_group = poppler_layer_get_radio_button_group_id (layer);
2357 pdf_document->layers = g_list_append (pdf_document->layers,
2358 g_object_ref (layer));
2359 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2360 poppler_layer_is_parent (layer),
2365 title = poppler_layers_iter_get_title (iter);
2366 markup = g_markup_escape_text (title, -1);
2373 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2374 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2375 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2376 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2377 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2378 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2379 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2380 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2383 g_object_unref (ev_layer);
2386 child = poppler_layers_iter_get_child (iter);
2388 build_layers_tree (pdf_document, model, &tree_iter, child);
2389 poppler_layers_iter_free (child);
2390 } while (poppler_layers_iter_next (iter));
2392 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2394 static GtkTreeModel *
2395 pdf_document_layers_get_layers (EvDocumentLayers *document)
2397 GtkTreeModel *model = NULL;
2398 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2399 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2400 PopplerLayersIter *iter;
2402 iter = poppler_layers_iter_new (pdf_document->document);
2404 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2405 G_TYPE_STRING, /* TITLE */
2406 G_TYPE_OBJECT, /* LAYER */
2407 G_TYPE_BOOLEAN, /* VISIBLE */
2408 G_TYPE_BOOLEAN, /* ENABLED */
2409 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2410 G_TYPE_INT); /* RBGROUP */
2411 build_layers_tree (pdf_document, model, NULL, iter);
2412 poppler_layers_iter_free (iter);
2420 pdf_document_layers_show_layer (EvDocumentLayers *document,
2423 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2424 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2425 guint layer_id = ev_layer_get_id (layer);
2427 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2430 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2434 pdf_document_layers_hide_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_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2444 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2448 pdf_document_layers_layer_is_visible (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 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2458 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2462 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2464 iface->has_layers = pdf_document_layers_has_layers;
2465 iface->get_layers = pdf_document_layers_get_layers;
2466 iface->show_layer = pdf_document_layers_show_layer;
2467 iface->hide_layer = pdf_document_layers_hide_layer;
2468 iface->layer_is_visible = pdf_document_layers_layer_is_visible;