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;
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 g_return_if_fail (poppler_error != NULL);
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,
262 GError *poppler_error = NULL;
264 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
268 convert_error (poppler_error, error);
274 pdf_document_load (EvDocument *document,
278 GError *poppler_error = NULL;
279 PdfDocument *pdf_document = PDF_DOCUMENT (document);
281 pdf_document->document =
282 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
284 if (pdf_document->document == NULL) {
285 convert_error (poppler_error, error);
293 pdf_document_get_n_pages (EvDocument *document)
295 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
299 pdf_document_get_page (EvDocument *document,
302 PdfDocument *pdf_document = PDF_DOCUMENT (document);
303 PopplerPage *poppler_page;
306 poppler_page = poppler_document_get_page (pdf_document->document, index);
307 page = ev_page_new (index);
308 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
309 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
310 g_object_unref (poppler_page);
316 pdf_document_get_page_size (EvDocument *document,
321 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
323 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
327 pdf_document_get_page_label (EvDocument *document,
332 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
334 g_object_get (G_OBJECT (page->backend_page),
341 pdf_document_has_attachments (EvDocument *document)
343 PdfDocument *pdf_document;
345 pdf_document = PDF_DOCUMENT (document);
347 return poppler_document_has_attachments (pdf_document->document);
350 struct SaveToBufferData {
356 attachment_save_to_buffer_callback (const gchar *buf,
361 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
365 if (sdata->len + count > sdata->max) {
366 new_max = MAX (sdata->max * 2, sdata->len + count);
367 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
369 sdata->buffer = new_buffer;
370 sdata->max = new_max;
373 memcpy (sdata->buffer + sdata->len, buf, count);
380 attachment_save_to_buffer (PopplerAttachment *attachment,
385 static const gint initial_max = 1024;
386 struct SaveToBufferData sdata;
391 sdata.buffer = (gchar *) g_malloc (initial_max);
392 sdata.max = initial_max;
395 if (! poppler_attachment_save_to_callback (attachment,
396 attachment_save_to_buffer_callback,
399 g_free (sdata.buffer);
403 *buffer = sdata.buffer;
404 *buffer_size = sdata.len;
410 pdf_document_get_attachments (EvDocument *document)
412 PdfDocument *pdf_document;
415 GList *retval = NULL;
417 pdf_document = PDF_DOCUMENT (document);
419 if (!pdf_document_has_attachments (document))
422 attachments = poppler_document_get_attachments (pdf_document->document);
424 for (list = attachments; list; list = list->next) {
425 PopplerAttachment *attachment;
426 EvAttachment *ev_attachment;
429 GError *error = NULL;
431 attachment = (PopplerAttachment *) list->data;
433 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
434 ev_attachment = ev_attachment_new (attachment->name,
435 attachment->description,
440 retval = g_list_prepend (retval, ev_attachment);
443 g_warning ("%s", error->message);
444 g_error_free (error);
450 g_object_unref (attachment);
453 return g_list_reverse (retval);
456 static cairo_surface_t *
457 pdf_page_render (PopplerPage *page,
462 cairo_surface_t *surface;
464 #ifdef HAVE_POPPLER_PAGE_RENDER
467 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
469 memset (cairo_image_surface_get_data (surface), 0xff,
470 cairo_image_surface_get_height (surface) *
471 cairo_image_surface_get_stride (surface));
473 cr = cairo_create (surface);
474 switch (rc->rotation) {
476 cairo_translate (cr, width, 0);
479 cairo_translate (cr, width, height);
482 cairo_translate (cr, 0, height);
485 cairo_translate (cr, 0, 0);
487 cairo_scale (cr, rc->scale, rc->scale);
488 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
489 poppler_page_render (page, cr);
491 #else /* HAVE_POPPLER_PAGE_RENDER */
494 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
498 poppler_page_render_to_pixbuf (page,
504 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
505 g_object_unref (pixbuf);
506 #endif /* HAVE_POPPLER_PAGE_RENDER */
511 static cairo_surface_t *
512 pdf_document_render (EvDocument *document,
515 PdfDocument *pdf_document;
516 PopplerPage *poppler_page;
517 double width_points, height_points;
520 poppler_page = POPPLER_PAGE (rc->page->backend_page);
522 poppler_page_get_size (poppler_page,
523 &width_points, &height_points);
525 if (rc->rotation == 90 || rc->rotation == 270) {
526 width = (int) ((height_points * rc->scale) + 0.5);
527 height = (int) ((width_points * rc->scale) + 0.5);
529 width = (int) ((width_points * rc->scale) + 0.5);
530 height = (int) ((height_points * rc->scale) + 0.5);
533 return pdf_page_render (poppler_page,
537 /* EvDocumentSecurity */
540 pdf_document_has_document_security (EvDocumentSecurity *document_security)
542 /* FIXME: do we really need to have this? */
547 pdf_document_set_password (EvDocumentSecurity *document_security,
548 const char *password)
550 PdfDocument *document = PDF_DOCUMENT (document_security);
552 if (document->password)
553 g_free (document->password);
555 document->password = g_strdup (password);
558 static EvDocumentInfo *
559 pdf_document_get_info (EvDocument *document)
561 EvDocumentInfo *info;
562 PopplerPageLayout layout;
563 PopplerPageMode mode;
564 PopplerViewerPreferences view_prefs;
565 PopplerPermissions permissions;
568 info = g_new0 (EvDocumentInfo, 1);
570 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
571 EV_DOCUMENT_INFO_FORMAT |
572 EV_DOCUMENT_INFO_AUTHOR |
573 EV_DOCUMENT_INFO_SUBJECT |
574 EV_DOCUMENT_INFO_KEYWORDS |
575 EV_DOCUMENT_INFO_LAYOUT |
576 EV_DOCUMENT_INFO_START_MODE |
577 EV_DOCUMENT_INFO_PERMISSIONS |
578 EV_DOCUMENT_INFO_UI_HINTS |
579 EV_DOCUMENT_INFO_CREATOR |
580 EV_DOCUMENT_INFO_PRODUCER |
581 EV_DOCUMENT_INFO_CREATION_DATE |
582 EV_DOCUMENT_INFO_MOD_DATE |
583 EV_DOCUMENT_INFO_LINEARIZED |
584 EV_DOCUMENT_INFO_N_PAGES |
585 EV_DOCUMENT_INFO_SECURITY |
586 EV_DOCUMENT_INFO_PAPER_SIZE;
588 g_object_get (PDF_DOCUMENT (document)->document,
589 "title", &(info->title),
590 "format", &(info->format),
591 "author", &(info->author),
592 "subject", &(info->subject),
593 "keywords", &(info->keywords),
595 "page-layout", &layout,
596 "viewer-preferences", &view_prefs,
597 "permissions", &permissions,
598 "creator", &(info->creator),
599 "producer", &(info->producer),
600 "creation-date", &(info->creation_date),
601 "mod-date", &(info->modified_date),
602 "linearized", &(info->linearized),
605 info->n_pages = ev_document_get_n_pages (document);
607 if (info->n_pages > 0) {
608 page = ev_document_get_page (document, 0);
609 ev_document_get_page_size (document, page,
610 &(info->paper_width),
611 &(info->paper_height));
612 g_object_unref (page);
616 info->paper_width = info->paper_width / 72.0f * 25.4f;
617 info->paper_height = info->paper_height / 72.0f * 25.4f;
621 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
622 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
624 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
625 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
627 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
628 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
630 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
631 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
632 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
633 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
635 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
636 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
643 case POPPLER_PAGE_MODE_NONE:
644 info->mode = EV_DOCUMENT_MODE_NONE;
646 case POPPLER_PAGE_MODE_USE_THUMBS:
647 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
649 case POPPLER_PAGE_MODE_USE_OC:
650 info->mode = EV_DOCUMENT_MODE_USE_OC;
652 case POPPLER_PAGE_MODE_FULL_SCREEN:
653 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
655 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
656 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
662 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
663 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
665 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
666 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
668 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
669 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
671 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
672 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
674 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
675 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
677 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
678 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
680 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
681 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
684 info->permissions = 0;
685 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
686 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
688 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
689 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
691 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
692 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
694 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
695 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
698 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
699 /* translators: this is the document security state */
700 info->security = g_strdup (_("Yes"));
702 /* translators: this is the document security state */
703 info->security = g_strdup (_("No"));
710 pdf_document_document_iface_init (EvDocumentIface *iface)
712 iface->save = pdf_document_save;
713 iface->load = pdf_document_load;
714 iface->get_n_pages = pdf_document_get_n_pages;
715 iface->get_page = pdf_document_get_page;
716 iface->get_page_size = pdf_document_get_page_size;
717 iface->get_page_label = pdf_document_get_page_label;
718 iface->has_attachments = pdf_document_has_attachments;
719 iface->get_attachments = pdf_document_get_attachments;
720 iface->render = pdf_document_render;
721 iface->get_info = pdf_document_get_info;
725 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
727 iface->has_document_security = pdf_document_has_document_security;
728 iface->set_password = pdf_document_set_password;
732 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
734 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
737 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
739 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
743 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
746 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
749 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
751 if (pdf_document->font_info == NULL) {
752 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
755 if (pdf_document->fonts_iter) {
756 poppler_fonts_iter_free (pdf_document->fonts_iter);
759 pdf_document->fonts_scanned_pages += n_pages;
761 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
762 &pdf_document->fonts_iter);
764 pdf_document->fonts_scanned_pages = 0;
765 poppler_font_info_free (pdf_document->font_info);
766 pdf_document->font_info = NULL;
773 font_type_to_string (PopplerFontType type)
776 case POPPLER_FONT_TYPE_TYPE1:
778 case POPPLER_FONT_TYPE_TYPE1C:
780 case POPPLER_FONT_TYPE_TYPE3:
782 case POPPLER_FONT_TYPE_TRUETYPE:
783 return _("TrueType");
784 case POPPLER_FONT_TYPE_CID_TYPE0:
785 return _("Type 1 (CID)");
786 case POPPLER_FONT_TYPE_CID_TYPE0C:
787 return _("Type 1C (CID)");
788 case POPPLER_FONT_TYPE_CID_TYPE2:
789 return _("TrueType (CID)");
791 return _("Unknown font type");
796 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
799 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
800 PopplerFontsIter *iter = pdf_document->fonts_iter;
802 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
808 GtkTreeIter list_iter;
811 const char *embedded;
814 name = poppler_fonts_iter_get_name (iter);
820 type = font_type_to_string (
821 poppler_fonts_iter_get_font_type (iter));
823 if (poppler_fonts_iter_is_embedded (iter)) {
824 if (poppler_fonts_iter_is_subset (iter))
825 embedded = _("Embedded subset");
827 embedded = _("Embedded");
829 embedded = _("Not embedded");
832 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
834 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
835 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
836 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
837 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
841 } while (poppler_fonts_iter_next (iter));
845 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
847 iface->fill_model = pdf_document_fonts_fill_model;
848 iface->scan = pdf_document_fonts_scan;
849 iface->get_progress = pdf_document_fonts_get_progress;
853 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
855 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
856 PopplerIndexIter *iter;
858 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
860 iter = poppler_index_iter_new (pdf_document->document);
863 poppler_index_iter_free (iter);
869 ev_link_dest_from_dest (PdfDocument *pdf_document,
872 EvLinkDest *ev_dest = NULL;
873 const char *unimplemented_dest = NULL;
875 g_assert (dest != NULL);
877 switch (dest->type) {
878 case POPPLER_DEST_XYZ: {
879 PopplerPage *poppler_page;
882 poppler_page = poppler_document_get_page (pdf_document->document,
883 MAX (0, dest->page_num - 1));
884 poppler_page_get_size (poppler_page, NULL, &height);
885 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
887 height - MIN (height, dest->top),
892 g_object_unref (poppler_page);
895 case POPPLER_DEST_FIT:
896 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
898 case POPPLER_DEST_FITH: {
899 PopplerPage *poppler_page;
902 poppler_page = poppler_document_get_page (pdf_document->document,
903 MAX (0, dest->page_num - 1));
904 poppler_page_get_size (poppler_page, NULL, &height);
905 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
906 height - MIN (height, dest->top),
908 g_object_unref (poppler_page);
911 case POPPLER_DEST_FITV:
912 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
916 case POPPLER_DEST_FITR: {
917 PopplerPage *poppler_page;
920 poppler_page = poppler_document_get_page (pdf_document->document,
921 MAX (0, dest->page_num - 1));
922 poppler_page_get_size (poppler_page, NULL, &height);
923 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
925 height - MIN (height, dest->bottom),
927 height - MIN (height, dest->top));
928 g_object_unref (poppler_page);
931 case POPPLER_DEST_FITB:
932 unimplemented_dest = "POPPLER_DEST_FITB";
934 case POPPLER_DEST_FITBH:
935 unimplemented_dest = "POPPLER_DEST_FITBH";
937 case POPPLER_DEST_FITBV:
938 unimplemented_dest = "POPPLER_DEST_FITBV";
940 case POPPLER_DEST_NAMED:
941 ev_dest = ev_link_dest_new_named (dest->named_dest);
943 case POPPLER_DEST_UNKNOWN:
944 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
948 if (unimplemented_dest) {
949 g_warning ("Unimplemented destination: %s, please post a "
950 "bug report in Evince bugzilla "
951 "(http://bugzilla.gnome.org) with a testcase.",
956 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
962 ev_link_from_action (PdfDocument *pdf_document,
963 PopplerAction *action)
966 EvLinkAction *ev_action = NULL;
967 const char *unimplemented_action = NULL;
969 switch (action->type) {
970 case POPPLER_ACTION_NONE:
972 case POPPLER_ACTION_GOTO_DEST: {
975 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
976 ev_action = ev_link_action_new_dest (dest);
979 case POPPLER_ACTION_GOTO_REMOTE: {
982 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
983 ev_action = ev_link_action_new_remote (dest,
984 action->goto_remote.file_name);
988 case POPPLER_ACTION_LAUNCH:
989 ev_action = ev_link_action_new_launch (action->launch.file_name,
990 action->launch.params);
992 case POPPLER_ACTION_URI:
993 ev_action = ev_link_action_new_external_uri (action->uri.uri);
995 case POPPLER_ACTION_NAMED:
996 ev_action = ev_link_action_new_named (action->named.named_dest);
998 case POPPLER_ACTION_MOVIE:
999 unimplemented_action = "POPPLER_ACTION_MOVIE";
1001 case POPPLER_ACTION_UNKNOWN:
1002 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1005 if (unimplemented_action) {
1006 g_warning ("Unimplemented action: %s, please post a bug report "
1007 "in Evince bugzilla (http://bugzilla.gnome.org) "
1008 "with a testcase.", unimplemented_action);
1011 link = ev_link_new (action->any.title, ev_action);
1017 build_tree (PdfDocument *pdf_document,
1018 GtkTreeModel *model,
1019 GtkTreeIter *parent,
1020 PopplerIndexIter *iter)
1024 GtkTreeIter tree_iter;
1025 PopplerIndexIter *child;
1026 PopplerAction *action;
1027 EvLink *link = NULL;
1031 action = poppler_index_iter_get_action (iter);
1032 expand = poppler_index_iter_is_open (iter);
1037 switch (action->type) {
1038 case POPPLER_ACTION_GOTO_DEST: {
1039 /* For bookmarks, solve named destinations */
1040 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1042 EvLinkDest *ev_dest = NULL;
1043 EvLinkAction *ev_action;
1045 dest = poppler_document_find_dest (pdf_document->document,
1046 action->goto_dest.dest->named_dest);
1048 link = ev_link_from_action (pdf_document, action);
1052 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1053 poppler_dest_free (dest);
1055 ev_action = ev_link_action_new_dest (ev_dest);
1056 link = ev_link_new (action->any.title, ev_action);
1058 link = ev_link_from_action (pdf_document, action);
1063 link = ev_link_from_action (pdf_document, action);
1067 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1068 poppler_action_free (action);
1070 g_object_unref (link);
1075 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1076 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1078 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1079 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1080 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1081 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1084 g_free (title_markup);
1085 g_object_unref (link);
1087 child = poppler_index_iter_get_child (iter);
1089 build_tree (pdf_document, model, &tree_iter, child);
1090 poppler_index_iter_free (child);
1091 poppler_action_free (action);
1093 } while (poppler_index_iter_next (iter));
1096 static GtkTreeModel *
1097 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1099 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1100 GtkTreeModel *model = NULL;
1101 PopplerIndexIter *iter;
1103 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1105 iter = poppler_index_iter_new (pdf_document->document);
1106 /* Create the model if we have items*/
1108 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1113 build_tree (pdf_document, model, NULL, iter);
1114 poppler_index_iter_free (iter);
1121 pdf_document_links_get_links (EvDocumentLinks *document_links,
1124 PdfDocument *pdf_document;
1125 PopplerPage *poppler_page;
1126 GList *retval = NULL;
1127 GList *mapping_list;
1131 pdf_document = PDF_DOCUMENT (document_links);
1132 poppler_page = poppler_document_get_page (pdf_document->document,
1134 mapping_list = poppler_page_get_link_mapping (poppler_page);
1135 poppler_page_get_size (poppler_page, NULL, &height);
1137 for (list = mapping_list; list; list = list->next) {
1138 PopplerLinkMapping *link_mapping;
1139 EvLinkMapping *ev_link_mapping;
1141 link_mapping = (PopplerLinkMapping *)list->data;
1142 ev_link_mapping = g_new (EvLinkMapping, 1);
1143 ev_link_mapping->link = ev_link_from_action (pdf_document,
1144 link_mapping->action);
1145 ev_link_mapping->x1 = link_mapping->area.x1;
1146 ev_link_mapping->x2 = link_mapping->area.x2;
1147 /* Invert this for X-style coordinates */
1148 ev_link_mapping->y1 = height - link_mapping->area.y2;
1149 ev_link_mapping->y2 = height - link_mapping->area.y1;
1151 retval = g_list_prepend (retval, ev_link_mapping);
1154 poppler_page_free_link_mapping (mapping_list);
1155 g_object_unref (poppler_page);
1157 return g_list_reverse (retval);
1161 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1162 const gchar *link_name)
1164 PdfDocument *pdf_document;
1166 EvLinkDest *ev_dest = NULL;
1168 pdf_document = PDF_DOCUMENT (document_links);
1169 dest = poppler_document_find_dest (pdf_document->document,
1172 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1173 poppler_dest_free (dest);
1180 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1182 iface->has_document_links = pdf_document_links_has_document_links;
1183 iface->get_links_model = pdf_document_links_get_links_model;
1184 iface->get_links = pdf_document_links_get_links;
1185 iface->find_link_dest = pdf_document_links_find_link_dest;
1189 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1192 GList *retval = NULL;
1193 PdfDocument *pdf_document;
1194 PopplerPage *poppler_page;
1195 GList *mapping_list;
1198 pdf_document = PDF_DOCUMENT (document_images);
1199 poppler_page = poppler_document_get_page (pdf_document->document, page);
1200 mapping_list = poppler_page_get_image_mapping (poppler_page);
1202 for (list = mapping_list; list; list = list->next) {
1203 PopplerImageMapping *image_mapping;
1204 EvImageMapping *ev_image_mapping;
1206 image_mapping = (PopplerImageMapping *)list->data;
1208 ev_image_mapping = g_new (EvImageMapping, 1);
1210 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1211 ev_image_mapping->x1 = image_mapping->area.x1;
1212 ev_image_mapping->x2 = image_mapping->area.x2;
1213 ev_image_mapping->y1 = image_mapping->area.y1;
1214 ev_image_mapping->y2 = image_mapping->area.y2;
1216 retval = g_list_prepend (retval, ev_image_mapping);
1219 poppler_page_free_image_mapping (mapping_list);
1220 g_object_unref (poppler_page);
1222 return g_list_reverse (retval);
1226 pdf_document_images_get_image (EvDocumentImages *document_images,
1229 PdfDocument *pdf_document;
1230 PopplerPage *poppler_page;
1231 cairo_surface_t *surface;
1232 GdkPixbuf *retval = NULL;
1234 pdf_document = PDF_DOCUMENT (document_images);
1235 poppler_page = poppler_document_get_page (pdf_document->document,
1236 ev_image_get_page (image));
1238 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1240 retval = ev_document_misc_pixbuf_from_surface (surface);
1241 cairo_surface_destroy (surface);
1244 g_object_unref (poppler_page);
1250 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1252 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1253 iface->get_image = pdf_document_images_get_image;
1257 make_thumbnail_for_page (PdfDocument *pdf_document,
1258 PopplerPage *poppler_page,
1259 EvRenderContext *rc)
1264 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1265 rc, &width, &height);
1266 #ifdef POPPLER_WITH_GDK
1267 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1269 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1271 ev_document_fc_mutex_lock ();
1272 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1274 rc->scale, rc->rotation, pixbuf);
1275 ev_document_fc_mutex_unlock ();
1277 cairo_surface_t *surface;
1279 ev_document_fc_mutex_lock ();
1280 surface = pdf_page_render (poppler_page, width, height, rc);
1281 ev_document_fc_mutex_unlock ();
1283 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1284 cairo_surface_destroy (surface);
1285 #endif /* POPPLER_WITH_GDK */
1291 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1292 EvRenderContext *rc,
1295 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1296 PopplerPage *poppler_page;
1297 GdkPixbuf *pixbuf = NULL;
1298 GdkPixbuf *border_pixbuf;
1300 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1302 #ifdef POPPLER_WITH_GDK
1303 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1305 cairo_surface_t *surface;
1307 surface = poppler_page_get_thumbnail (poppler_page);
1309 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1310 cairo_surface_destroy (surface);
1312 #endif /* POPPLER_WITH_GDK */
1315 /* Rotate provided thumbnail if needed */
1316 GdkPixbuf *rotated_pixbuf;
1318 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1319 (GdkPixbufRotation) (360 - rc->rotation));
1320 g_object_unref (pixbuf);
1321 pixbuf = rotated_pixbuf;
1323 /* There is no provided thumbnail. We need to make one. */
1324 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1328 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1329 g_object_unref (pixbuf);
1330 pixbuf = border_pixbuf;
1337 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1338 EvRenderContext *rc,
1342 PopplerPage *poppler_page;
1345 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1347 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1349 if (!has_thumb || *width <= 0 || *height <= 0) {
1350 double page_width, page_height;
1352 poppler_page_get_size (poppler_page, &page_width, &page_height);
1354 *width = (gint) MAX (page_width * rc->scale, 1);
1355 *height = (gint) MAX (page_height * rc->scale, 1);
1358 if (rc->rotation == 90 || rc->rotation == 270) {
1368 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1370 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1371 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1376 pdf_document_find_find_text (EvDocumentFind *document_find,
1379 gboolean case_sensitive)
1382 PopplerPage *poppler_page;
1385 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1386 g_return_val_if_fail (text != NULL, NULL);
1388 poppler_page = POPPLER_PAGE (page->backend_page);
1390 matches = poppler_page_find_text (poppler_page, text);
1394 poppler_page_get_size (poppler_page, NULL, &height);
1395 for (l = matches; l && l->data; l = g_list_next (l)) {
1396 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1400 rect->y1 = height - rect->y2;
1401 rect->y2 = height - tmp;
1408 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1410 iface->find_text = pdf_document_find_find_text;
1414 pdf_print_context_free (PdfPrintContext *ctx)
1419 #ifdef HAVE_CAIRO_PRINT
1421 cairo_destroy (ctx->cr);
1426 poppler_ps_file_free (ctx->ps_file);
1427 ctx->ps_file = NULL;
1434 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1435 EvFileExporterContext *fc)
1437 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1438 PdfPrintContext *ctx;
1439 #ifdef HAVE_CAIRO_PRINT
1440 gdouble width, height;
1441 cairo_surface_t *surface = NULL;
1444 if (pdf_document->print_ctx)
1445 pdf_print_context_free (pdf_document->print_ctx);
1446 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1447 ctx = pdf_document->print_ctx;
1448 ctx->format = fc->format;
1450 #ifdef HAVE_CAIRO_PRINT
1451 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1453 ctx->paper_width = fc->paper_width;
1454 ctx->paper_height = fc->paper_height;
1456 switch (fc->pages_per_sheet) {
1484 ctx->pages_printed = 0;
1486 switch (fc->format) {
1487 case EV_FILE_FORMAT_PS:
1488 #ifdef HAVE_CAIRO_PS
1489 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1492 case EV_FILE_FORMAT_PDF:
1493 #ifdef HAVE_CAIRO_PDF
1494 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1498 g_assert_not_reached ();
1501 ctx->cr = cairo_create (surface);
1502 cairo_surface_destroy (surface);
1504 #else /* HAVE_CAIRO_PRINT */
1505 if (ctx->format == EV_FILE_FORMAT_PS) {
1506 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1507 fc->filename, fc->first_page,
1508 fc->last_page - fc->first_page + 1);
1509 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1510 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1512 #endif /* HAVE_CAIRO_PRINT */
1516 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1518 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1519 PdfPrintContext *ctx = pdf_document->print_ctx;
1521 g_return_if_fail (pdf_document->print_ctx != NULL);
1523 ctx->pages_printed = 0;
1525 #ifdef HAVE_CAIRO_PRINT
1526 if (ctx->paper_width > ctx->paper_height) {
1527 if (ctx->format == EV_FILE_FORMAT_PS) {
1528 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1531 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1532 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1537 #endif /* HAVE_CAIRO_PRINT */
1541 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1542 EvRenderContext *rc)
1544 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1545 PdfPrintContext *ctx = pdf_document->print_ctx;
1546 PopplerPage *poppler_page;
1547 #ifdef HAVE_CAIRO_PRINT
1548 gdouble page_width, page_height;
1551 gdouble width, height;
1552 gdouble pwidth, pheight;
1553 gdouble xscale, yscale;
1556 g_return_if_fail (pdf_document->print_ctx != NULL);
1558 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1560 #ifdef HAVE_CAIRO_PRINT
1561 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1562 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1563 poppler_page_get_size (poppler_page, &page_width, &page_height);
1565 if (page_width > page_height && page_width > ctx->paper_width) {
1571 /* Use always portrait mode and rotate when necessary */
1572 if (ctx->paper_width > ctx->paper_height) {
1573 width = ctx->paper_height;
1574 height = ctx->paper_width;
1577 width = ctx->paper_width;
1578 height = ctx->paper_height;
1581 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1594 page_width = page_height;
1598 pwidth = width / ctx->pages_x;
1599 pheight = height / ctx->pages_y;
1601 if ((page_width > pwidth || page_height > pheight) ||
1602 (page_width < pwidth && page_height < pheight)) {
1603 xscale = pwidth / page_width;
1604 yscale = pheight / page_height;
1606 if (yscale < xscale) {
1613 xscale = yscale = 1;
1618 cairo_save (ctx->cr);
1620 cairo_matrix_t matrix;
1622 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1623 cairo_matrix_init (&matrix,
1627 cairo_transform (ctx->cr, &matrix);
1630 cairo_translate (ctx->cr,
1631 x * (rotate ? pheight : pwidth),
1632 y * (rotate ? pwidth : pheight));
1633 cairo_scale (ctx->cr, xscale, yscale);
1635 poppler_page_render_for_printing (poppler_page, ctx->cr);
1637 ctx->pages_printed++;
1639 cairo_restore (ctx->cr);
1640 #else /* HAVE_CAIRO_PRINT */
1641 if (ctx->format == EV_FILE_FORMAT_PS)
1642 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1643 #endif /* HAVE_CAIRO_PRINT */
1647 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1649 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1650 PdfPrintContext *ctx = pdf_document->print_ctx;
1652 g_return_if_fail (pdf_document->print_ctx != NULL);
1654 #ifdef HAVE_CAIRO_PRINT
1655 cairo_show_page (ctx->cr);
1660 pdf_document_file_exporter_end (EvFileExporter *exporter)
1662 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1664 pdf_print_context_free (pdf_document->print_ctx);
1665 pdf_document->print_ctx = NULL;
1668 static EvFileExporterCapabilities
1669 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1671 return (EvFileExporterCapabilities) (
1672 EV_FILE_EXPORTER_CAN_PAGE_SET |
1673 EV_FILE_EXPORTER_CAN_COPIES |
1674 EV_FILE_EXPORTER_CAN_COLLATE |
1675 EV_FILE_EXPORTER_CAN_REVERSE |
1676 EV_FILE_EXPORTER_CAN_SCALE |
1677 #ifdef HAVE_CAIRO_PRINT
1678 #ifdef HAVE_POPPLER_PAGE_RENDER
1679 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1683 #ifdef HAVE_CAIRO_PDF
1684 #ifdef HAVE_POPPLER_PAGE_RENDER
1685 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1688 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1692 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1694 iface->begin = pdf_document_file_exporter_begin;
1695 iface->begin_page = pdf_document_file_exporter_begin_page;
1696 iface->do_page = pdf_document_file_exporter_do_page;
1697 iface->end_page = pdf_document_file_exporter_end_page;
1698 iface->end = pdf_document_file_exporter_end;
1699 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1703 pdf_selection_render_selection (EvSelection *selection,
1704 EvRenderContext *rc,
1705 cairo_surface_t **surface,
1706 EvRectangle *points,
1707 EvRectangle *old_points,
1708 EvSelectionStyle style,
1712 PopplerPage *poppler_page;
1713 double width_points, height_points;
1716 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1718 poppler_page_get_size (poppler_page,
1719 &width_points, &height_points);
1720 width = (int) ((width_points * rc->scale) + 0.5);
1721 height = (int) ((height_points * rc->scale) + 0.5);
1723 #ifdef HAVE_POPPLER_PAGE_RENDER
1725 PopplerColor text_color, base_color;
1727 text_color.red = text->red;
1728 text_color.green = text->green;
1729 text_color.blue = text->blue;
1731 base_color.red = base->red;
1732 base_color.green = base->green;
1733 base_color.blue = base->blue;
1735 if (*surface == NULL) {
1736 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1741 cr = cairo_create (*surface);
1742 cairo_scale (cr, rc->scale, rc->scale);
1743 cairo_surface_set_device_offset (*surface, 0, 0);
1744 memset (cairo_image_surface_get_data (*surface), 0x00,
1745 cairo_image_surface_get_height (*surface) *
1746 cairo_image_surface_get_stride (*surface));
1747 poppler_page_render_selection (poppler_page,
1749 (PopplerRectangle *)points,
1750 (PopplerRectangle *)old_points,
1751 (PopplerSelectionStyle)style,
1755 #else /* HAVE_POPPLER_PAGE_RENDER */
1758 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1762 poppler_page_render_selection_to_pixbuf (poppler_page,
1763 rc->scale, rc->rotation, pixbuf,
1764 (PopplerRectangle *)points,
1765 (PopplerRectangle *)old_points,
1766 (PopplerSelectionStyle)style,
1770 cairo_surface_destroy (*surface);
1771 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1772 g_object_unref (pixbuf);
1773 #endif /* HAVE_POPPLER_PAGE_RENDER */
1777 pdf_selection_get_selected_text (EvSelection *selection,
1778 EvRenderContext *rc,
1779 EvSelectionStyle style,
1780 EvRectangle *points)
1782 PopplerPage *poppler_page;
1787 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1789 poppler_page_get_size (poppler_page, NULL, &height);
1791 r.y1 = height - points->y2;
1793 r.y2 = height - points->y1;
1795 retval = poppler_page_get_text (poppler_page,
1796 (PopplerSelectionStyle)style,
1803 create_gdk_region_from_poppler_region (GList *region)
1808 retval = gdk_region_new ();
1810 for (l = region; l; l = g_list_next (l)) {
1811 PopplerRectangle *rectangle;
1814 rectangle = (PopplerRectangle *)l->data;
1816 rect.x = (gint) rectangle->x1;
1817 rect.y = (gint) rectangle->y1;
1818 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1819 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1820 gdk_region_union_with_rect (retval, &rect);
1822 poppler_rectangle_free (rectangle);
1829 pdf_selection_get_selection_region (EvSelection *selection,
1830 EvRenderContext *rc,
1831 EvSelectionStyle style,
1832 EvRectangle *points)
1834 PopplerPage *poppler_page;
1838 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1840 region = poppler_page_get_selection_region (poppler_page,
1842 (PopplerSelectionStyle)style,
1843 (PopplerRectangle *) points);
1844 retval = create_gdk_region_from_poppler_region (region);
1845 g_list_free (region);
1851 pdf_selection_get_selection_map (EvSelection *selection,
1852 EvRenderContext *rc)
1854 PopplerPage *poppler_page;
1855 PopplerRectangle points;
1859 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1863 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1865 region = poppler_page_get_selection_region (poppler_page, 1.0,
1866 POPPLER_SELECTION_GLYPH,
1868 retval = create_gdk_region_from_poppler_region (region);
1869 g_list_free (region);
1875 pdf_selection_iface_init (EvSelectionIface *iface)
1877 iface->render_selection = pdf_selection_render_selection;
1878 iface->get_selected_text = pdf_selection_get_selected_text;
1879 iface->get_selection_region = pdf_selection_get_selection_region;
1880 iface->get_selection_map = pdf_selection_get_selection_map;
1883 /* Page Transitions */
1885 pdf_document_get_page_duration (EvDocumentTransition *trans,
1888 PdfDocument *pdf_document;
1889 PopplerPage *poppler_page;
1890 gdouble duration = -1;
1892 pdf_document = PDF_DOCUMENT (trans);
1893 poppler_page = poppler_document_get_page (pdf_document->document, page);
1897 duration = poppler_page_get_duration (poppler_page);
1898 g_object_unref (poppler_page);
1903 static EvTransitionEffect *
1904 pdf_document_get_effect (EvDocumentTransition *trans,
1907 PdfDocument *pdf_document;
1908 PopplerPage *poppler_page;
1909 PopplerPageTransition *page_transition;
1910 EvTransitionEffect *effect;
1912 pdf_document = PDF_DOCUMENT (trans);
1913 poppler_page = poppler_document_get_page (pdf_document->document, page);
1918 page_transition = poppler_page_get_transition (poppler_page);
1920 if (!page_transition) {
1921 g_object_unref (poppler_page);
1925 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1926 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1927 "alignment", page_transition->alignment,
1928 "direction", page_transition->direction,
1929 "duration", page_transition->duration,
1930 "angle", page_transition->angle,
1931 "scale", page_transition->scale,
1932 "rectangular", page_transition->rectangular,
1935 poppler_page_transition_free (page_transition);
1936 g_object_unref (poppler_page);
1942 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1944 iface->get_page_duration = pdf_document_get_page_duration;
1945 iface->get_effect = pdf_document_get_effect;
1950 pdf_document_get_crop_box (EvDocument *document,
1954 PdfDocument *pdf_document;
1955 PopplerPage *poppler_page;
1956 PopplerRectangle poppler_rect;
1958 pdf_document = PDF_DOCUMENT (document);
1959 poppler_page = poppler_document_get_page (pdf_document->document, page);
1960 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1961 rect->x1 = poppler_rect.x1;
1962 rect->x2 = poppler_rect.x2;
1963 rect->y1 = poppler_rect.y1;
1964 rect->y2 = poppler_rect.y2;
1967 static EvFormField *
1968 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1970 EvFormField *ev_field = NULL;
1973 gboolean is_read_only;
1975 id = poppler_form_field_get_id (poppler_field);
1976 font_size = poppler_form_field_get_font_size (poppler_field);
1977 is_read_only = poppler_form_field_is_read_only (poppler_field);
1979 switch (poppler_form_field_get_field_type (poppler_field)) {
1980 case POPPLER_FORM_FIELD_TEXT: {
1981 EvFormFieldText *field_text;
1982 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1984 switch (poppler_form_field_text_get_text_type (poppler_field)) {
1985 case POPPLER_FORM_TEXT_NORMAL:
1986 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1988 case POPPLER_FORM_TEXT_MULTILINE:
1989 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
1991 case POPPLER_FORM_TEXT_FILE_SELECT:
1992 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
1996 ev_field = ev_form_field_text_new (id, ev_text_type);
1997 field_text = EV_FORM_FIELD_TEXT (ev_field);
1999 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2000 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2001 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2002 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2003 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2004 field_text->text = poppler_form_field_text_get_text (poppler_field);
2008 case POPPLER_FORM_FIELD_BUTTON: {
2009 EvFormFieldButton *field_button;
2010 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2012 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2013 case POPPLER_FORM_BUTTON_PUSH:
2014 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2016 case POPPLER_FORM_BUTTON_CHECK:
2017 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2019 case POPPLER_FORM_BUTTON_RADIO:
2020 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2024 ev_field = ev_form_field_button_new (id, ev_button_type);
2025 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2027 field_button->state = poppler_form_field_button_get_state (poppler_field);
2030 case POPPLER_FORM_FIELD_CHOICE: {
2031 EvFormFieldChoice *field_choice;
2032 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2034 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2035 case POPPLER_FORM_CHOICE_COMBO:
2036 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2038 case EV_FORM_FIELD_CHOICE_LIST:
2039 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2043 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2044 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2046 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2047 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2048 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2049 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2051 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2052 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2053 if (field_choice->is_editable)
2054 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2057 case POPPLER_FORM_FIELD_SIGNATURE:
2059 ev_field = ev_form_field_signature_new (id);
2061 case POPPLER_FORM_FIELD_UNKNOWN:
2065 ev_field->font_size = font_size;
2066 ev_field->is_read_only = is_read_only;
2072 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2075 PopplerPage *poppler_page;
2076 GList *retval = NULL;
2081 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2083 poppler_page = POPPLER_PAGE (page->backend_page);
2084 fields = poppler_page_get_form_field_mapping (poppler_page);
2085 poppler_page_get_size (poppler_page, NULL, &height);
2087 for (list = fields; list; list = list->next) {
2088 PopplerFormFieldMapping *mapping;
2089 EvFormFieldMapping *field_mapping;
2090 EvFormField *ev_field;
2092 mapping = (PopplerFormFieldMapping *)list->data;
2094 ev_field = ev_form_field_from_poppler_field (mapping->field);
2098 field_mapping = g_new0 (EvFormFieldMapping, 1);
2099 field_mapping->x1 = mapping->area.x1;
2100 field_mapping->x2 = mapping->area.x2;
2101 field_mapping->y1 = height - mapping->area.y2;
2102 field_mapping->y2 = height - mapping->area.y1;
2103 field_mapping->field = ev_field;
2104 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2106 g_object_set_data_full (G_OBJECT (ev_field),
2108 g_object_ref (mapping->field),
2109 (GDestroyNotify) g_object_unref);
2111 retval = g_list_prepend (retval, field_mapping);
2114 poppler_page_free_form_field_mapping (fields);
2116 return g_list_reverse (retval);
2120 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2124 PopplerFormField *poppler_field;
2127 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2131 text = poppler_form_field_text_get_text (poppler_field);
2137 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2141 PopplerFormField *poppler_field;
2143 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2146 poppler_form_field_text_set_text (poppler_field, text);
2150 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2154 PopplerFormField *poppler_field;
2156 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2160 poppler_form_field_button_set_state (poppler_field, state);
2164 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2167 PopplerFormField *poppler_field;
2170 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2174 state = poppler_form_field_button_get_state (poppler_field);
2180 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2184 PopplerFormField *poppler_field;
2187 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2191 text = poppler_form_field_choice_get_item (poppler_field, index);
2197 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2200 PopplerFormField *poppler_field;
2203 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2207 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2213 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2217 PopplerFormField *poppler_field;
2220 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2224 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2230 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2234 PopplerFormField *poppler_field;
2236 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2240 poppler_form_field_choice_select_item (poppler_field, index);
2244 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2248 PopplerFormField *poppler_field;
2250 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2254 poppler_form_field_choice_toggle_item (poppler_field, index);
2258 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2261 PopplerFormField *poppler_field;
2263 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2267 poppler_form_field_choice_unselect_all (poppler_field);
2271 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2275 PopplerFormField *poppler_field;
2277 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2281 poppler_form_field_choice_set_text (poppler_field, text);
2285 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2288 PopplerFormField *poppler_field;
2291 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2295 text = poppler_form_field_choice_get_text (poppler_field);
2301 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2303 iface->get_form_fields = pdf_document_forms_get_form_fields;
2304 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2305 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2306 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2307 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2308 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2309 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2310 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2311 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2312 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2313 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2314 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2315 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2320 pdf_document_layers_has_layers (EvDocumentLayers *document)
2322 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2323 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2324 PopplerLayersIter *iter;
2326 iter = poppler_layers_iter_new (pdf_document->document);
2329 poppler_layers_iter_free (iter);
2334 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2337 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2339 build_layers_tree (PdfDocument *pdf_document,
2340 GtkTreeModel *model,
2341 GtkTreeIter *parent,
2342 PopplerLayersIter *iter)
2345 GtkTreeIter tree_iter;
2346 PopplerLayersIter *child;
2347 PopplerLayer *layer;
2348 EvLayer *ev_layer = NULL;
2353 layer = poppler_layers_iter_get_layer (iter);
2355 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2356 visible = poppler_layer_is_visible (layer);
2357 rb_group = poppler_layer_get_radio_button_group_id (layer);
2358 pdf_document->layers = g_list_append (pdf_document->layers,
2359 g_object_ref (layer));
2360 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2361 poppler_layer_is_parent (layer),
2366 title = poppler_layers_iter_get_title (iter);
2367 markup = g_markup_escape_text (title, -1);
2374 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2375 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2376 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2377 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2378 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2379 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2380 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2381 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2384 g_object_unref (ev_layer);
2387 child = poppler_layers_iter_get_child (iter);
2389 build_layers_tree (pdf_document, model, &tree_iter, child);
2390 poppler_layers_iter_free (child);
2391 } while (poppler_layers_iter_next (iter));
2393 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2395 static GtkTreeModel *
2396 pdf_document_layers_get_layers (EvDocumentLayers *document)
2398 GtkTreeModel *model = NULL;
2399 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2400 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2401 PopplerLayersIter *iter;
2403 iter = poppler_layers_iter_new (pdf_document->document);
2405 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2406 G_TYPE_STRING, /* TITLE */
2407 G_TYPE_OBJECT, /* LAYER */
2408 G_TYPE_BOOLEAN, /* VISIBLE */
2409 G_TYPE_BOOLEAN, /* ENABLED */
2410 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2411 G_TYPE_INT); /* RBGROUP */
2412 build_layers_tree (pdf_document, model, NULL, iter);
2413 poppler_layers_iter_free (iter);
2421 pdf_document_layers_show_layer (EvDocumentLayers *document,
2424 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2425 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2426 guint layer_id = ev_layer_get_id (layer);
2428 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2431 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2435 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2438 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2439 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2440 guint layer_id = ev_layer_get_id (layer);
2442 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2445 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2449 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2452 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2453 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2454 guint layer_id = ev_layer_get_id (layer);
2456 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2459 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2463 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2465 iface->has_layers = pdf_document_layers_has_layers;
2466 iface->get_layers = pdf_document_layers_get_layers;
2467 iface->show_layer = pdf_document_layers_show_layer;
2468 iface->hide_layer = pdf_document_layers_hide_layer;
2469 iface->layer_is_visible = pdf_document_layers_layer_is_visible;