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 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;
242 g_set_error_literal (error,
245 poppler_error->message);
247 g_propagate_error (error, poppler_error);
254 pdf_document_save (EvDocument *document,
259 GError *poppler_error = NULL;
261 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
265 convert_error (poppler_error, error);
271 pdf_document_load (EvDocument *document,
275 GError *poppler_error = NULL;
276 PdfDocument *pdf_document = PDF_DOCUMENT (document);
278 pdf_document->document =
279 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
281 if (pdf_document->document == NULL) {
282 convert_error (poppler_error, error);
290 pdf_document_get_n_pages (EvDocument *document)
292 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
296 pdf_document_get_page (EvDocument *document,
299 PdfDocument *pdf_document = PDF_DOCUMENT (document);
300 PopplerPage *poppler_page;
303 poppler_page = poppler_document_get_page (pdf_document->document, index);
304 page = ev_page_new (index);
305 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
306 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
307 g_object_unref (poppler_page);
313 pdf_document_get_page_size (EvDocument *document,
318 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
320 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
324 pdf_document_get_page_label (EvDocument *document,
329 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
331 g_object_get (G_OBJECT (page->backend_page),
338 pdf_document_has_attachments (EvDocument *document)
340 PdfDocument *pdf_document;
342 pdf_document = PDF_DOCUMENT (document);
344 return poppler_document_has_attachments (pdf_document->document);
347 struct SaveToBufferData {
353 attachment_save_to_buffer_callback (const gchar *buf,
358 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
362 if (sdata->len + count > sdata->max) {
363 new_max = MAX (sdata->max * 2, sdata->len + count);
364 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
366 sdata->buffer = new_buffer;
367 sdata->max = new_max;
370 memcpy (sdata->buffer + sdata->len, buf, count);
377 attachment_save_to_buffer (PopplerAttachment *attachment,
382 static const gint initial_max = 1024;
383 struct SaveToBufferData sdata;
388 sdata.buffer = (gchar *) g_malloc (initial_max);
389 sdata.max = initial_max;
392 if (! poppler_attachment_save_to_callback (attachment,
393 attachment_save_to_buffer_callback,
396 g_free (sdata.buffer);
400 *buffer = sdata.buffer;
401 *buffer_size = sdata.len;
407 pdf_document_get_attachments (EvDocument *document)
409 PdfDocument *pdf_document;
412 GList *retval = NULL;
414 pdf_document = PDF_DOCUMENT (document);
416 if (!pdf_document_has_attachments (document))
419 attachments = poppler_document_get_attachments (pdf_document->document);
421 for (list = attachments; list; list = list->next) {
422 PopplerAttachment *attachment;
423 EvAttachment *ev_attachment;
426 GError *error = NULL;
428 attachment = (PopplerAttachment *) list->data;
430 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
431 ev_attachment = ev_attachment_new (attachment->name,
432 attachment->description,
437 retval = g_list_prepend (retval, ev_attachment);
440 g_warning ("%s", error->message);
441 g_error_free (error);
447 g_object_unref (attachment);
450 return g_list_reverse (retval);
453 static cairo_surface_t *
454 pdf_page_render (PopplerPage *page,
459 cairo_surface_t *surface;
461 #ifdef HAVE_POPPLER_PAGE_RENDER
464 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
466 memset (cairo_image_surface_get_data (surface), 0xff,
467 cairo_image_surface_get_height (surface) *
468 cairo_image_surface_get_stride (surface));
470 cr = cairo_create (surface);
471 switch (rc->rotation) {
473 cairo_translate (cr, width, 0);
476 cairo_translate (cr, width, height);
479 cairo_translate (cr, 0, height);
482 cairo_translate (cr, 0, 0);
484 cairo_scale (cr, rc->scale, rc->scale);
485 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
486 poppler_page_render (page, cr);
488 #else /* HAVE_POPPLER_PAGE_RENDER */
491 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
495 poppler_page_render_to_pixbuf (page,
501 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
502 g_object_unref (pixbuf);
503 #endif /* HAVE_POPPLER_PAGE_RENDER */
508 static cairo_surface_t *
509 pdf_document_render (EvDocument *document,
512 PdfDocument *pdf_document;
513 PopplerPage *poppler_page;
514 double width_points, height_points;
517 poppler_page = POPPLER_PAGE (rc->page->backend_page);
519 poppler_page_get_size (poppler_page,
520 &width_points, &height_points);
522 if (rc->rotation == 90 || rc->rotation == 270) {
523 width = (int) ((height_points * rc->scale) + 0.5);
524 height = (int) ((width_points * rc->scale) + 0.5);
526 width = (int) ((width_points * rc->scale) + 0.5);
527 height = (int) ((height_points * rc->scale) + 0.5);
530 return pdf_page_render (poppler_page,
534 /* EvDocumentSecurity */
537 pdf_document_has_document_security (EvDocumentSecurity *document_security)
539 /* FIXME: do we really need to have this? */
544 pdf_document_set_password (EvDocumentSecurity *document_security,
545 const char *password)
547 PdfDocument *document = PDF_DOCUMENT (document_security);
549 if (document->password)
550 g_free (document->password);
552 document->password = g_strdup (password);
555 static EvDocumentInfo *
556 pdf_document_get_info (EvDocument *document)
558 EvDocumentInfo *info;
559 PopplerPageLayout layout;
560 PopplerPageMode mode;
561 PopplerViewerPreferences view_prefs;
562 PopplerPermissions permissions;
565 info = g_new0 (EvDocumentInfo, 1);
567 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
568 EV_DOCUMENT_INFO_FORMAT |
569 EV_DOCUMENT_INFO_AUTHOR |
570 EV_DOCUMENT_INFO_SUBJECT |
571 EV_DOCUMENT_INFO_KEYWORDS |
572 EV_DOCUMENT_INFO_LAYOUT |
573 EV_DOCUMENT_INFO_START_MODE |
574 EV_DOCUMENT_INFO_PERMISSIONS |
575 EV_DOCUMENT_INFO_UI_HINTS |
576 EV_DOCUMENT_INFO_CREATOR |
577 EV_DOCUMENT_INFO_PRODUCER |
578 EV_DOCUMENT_INFO_CREATION_DATE |
579 EV_DOCUMENT_INFO_MOD_DATE |
580 EV_DOCUMENT_INFO_LINEARIZED |
581 EV_DOCUMENT_INFO_N_PAGES |
582 EV_DOCUMENT_INFO_SECURITY |
583 EV_DOCUMENT_INFO_PAPER_SIZE;
585 g_object_get (PDF_DOCUMENT (document)->document,
586 "title", &(info->title),
587 "format", &(info->format),
588 "author", &(info->author),
589 "subject", &(info->subject),
590 "keywords", &(info->keywords),
592 "page-layout", &layout,
593 "viewer-preferences", &view_prefs,
594 "permissions", &permissions,
595 "creator", &(info->creator),
596 "producer", &(info->producer),
597 "creation-date", &(info->creation_date),
598 "mod-date", &(info->modified_date),
599 "linearized", &(info->linearized),
602 info->n_pages = ev_document_get_n_pages (document);
604 if (info->n_pages > 0) {
605 page = ev_document_get_page (document, 0);
606 ev_document_get_page_size (document, page,
607 &(info->paper_width),
608 &(info->paper_height));
609 g_object_unref (page);
613 info->paper_width = info->paper_width / 72.0f * 25.4f;
614 info->paper_height = info->paper_height / 72.0f * 25.4f;
618 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
619 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
621 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
622 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
624 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
625 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
627 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
628 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
629 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
630 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
632 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
633 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
640 case POPPLER_PAGE_MODE_NONE:
641 info->mode = EV_DOCUMENT_MODE_NONE;
643 case POPPLER_PAGE_MODE_USE_THUMBS:
644 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
646 case POPPLER_PAGE_MODE_USE_OC:
647 info->mode = EV_DOCUMENT_MODE_USE_OC;
649 case POPPLER_PAGE_MODE_FULL_SCREEN:
650 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
652 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
653 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
659 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
660 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
662 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
663 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
665 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
666 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
668 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
669 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
671 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
672 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
674 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
675 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
677 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
678 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
681 info->permissions = 0;
682 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
683 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
685 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
686 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
688 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
689 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
691 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
692 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
695 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
696 /* translators: this is the document security state */
697 info->security = g_strdup (_("Yes"));
699 /* translators: this is the document security state */
700 info->security = g_strdup (_("No"));
707 pdf_document_document_iface_init (EvDocumentIface *iface)
709 iface->save = pdf_document_save;
710 iface->load = pdf_document_load;
711 iface->get_n_pages = pdf_document_get_n_pages;
712 iface->get_page = pdf_document_get_page;
713 iface->get_page_size = pdf_document_get_page_size;
714 iface->get_page_label = pdf_document_get_page_label;
715 iface->has_attachments = pdf_document_has_attachments;
716 iface->get_attachments = pdf_document_get_attachments;
717 iface->render = pdf_document_render;
718 iface->get_info = pdf_document_get_info;
722 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
724 iface->has_document_security = pdf_document_has_document_security;
725 iface->set_password = pdf_document_set_password;
729 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
731 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
734 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
736 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
740 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
743 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
746 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
748 if (pdf_document->font_info == NULL) {
749 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
752 if (pdf_document->fonts_iter) {
753 poppler_fonts_iter_free (pdf_document->fonts_iter);
756 pdf_document->fonts_scanned_pages += n_pages;
758 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
759 &pdf_document->fonts_iter);
761 pdf_document->fonts_scanned_pages = 0;
762 poppler_font_info_free (pdf_document->font_info);
763 pdf_document->font_info = NULL;
770 font_type_to_string (PopplerFontType type)
773 case POPPLER_FONT_TYPE_TYPE1:
775 case POPPLER_FONT_TYPE_TYPE1C:
777 case POPPLER_FONT_TYPE_TYPE3:
779 case POPPLER_FONT_TYPE_TRUETYPE:
780 return _("TrueType");
781 case POPPLER_FONT_TYPE_CID_TYPE0:
782 return _("Type 1 (CID)");
783 case POPPLER_FONT_TYPE_CID_TYPE0C:
784 return _("Type 1C (CID)");
785 case POPPLER_FONT_TYPE_CID_TYPE2:
786 return _("TrueType (CID)");
788 return _("Unknown font type");
793 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
796 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
797 PopplerFontsIter *iter = pdf_document->fonts_iter;
799 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
805 GtkTreeIter list_iter;
808 const char *embedded;
811 name = poppler_fonts_iter_get_name (iter);
817 type = font_type_to_string (
818 poppler_fonts_iter_get_font_type (iter));
820 if (poppler_fonts_iter_is_embedded (iter)) {
821 if (poppler_fonts_iter_is_subset (iter))
822 embedded = _("Embedded subset");
824 embedded = _("Embedded");
826 embedded = _("Not embedded");
829 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
831 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
832 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
833 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
834 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
838 } while (poppler_fonts_iter_next (iter));
842 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
844 iface->fill_model = pdf_document_fonts_fill_model;
845 iface->scan = pdf_document_fonts_scan;
846 iface->get_progress = pdf_document_fonts_get_progress;
850 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
852 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
853 PopplerIndexIter *iter;
855 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
857 iter = poppler_index_iter_new (pdf_document->document);
860 poppler_index_iter_free (iter);
866 ev_link_dest_from_dest (PdfDocument *pdf_document,
869 EvLinkDest *ev_dest = NULL;
870 const char *unimplemented_dest = NULL;
872 g_assert (dest != NULL);
874 switch (dest->type) {
875 case POPPLER_DEST_XYZ: {
876 PopplerPage *poppler_page;
879 poppler_page = poppler_document_get_page (pdf_document->document,
880 MAX (0, dest->page_num - 1));
881 poppler_page_get_size (poppler_page, NULL, &height);
882 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
884 height - MIN (height, dest->top),
889 g_object_unref (poppler_page);
892 case POPPLER_DEST_FIT:
893 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
895 case POPPLER_DEST_FITH: {
896 PopplerPage *poppler_page;
899 poppler_page = poppler_document_get_page (pdf_document->document,
900 MAX (0, dest->page_num - 1));
901 poppler_page_get_size (poppler_page, NULL, &height);
902 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
903 height - MIN (height, dest->top),
905 g_object_unref (poppler_page);
908 case POPPLER_DEST_FITV:
909 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
913 case POPPLER_DEST_FITR: {
914 PopplerPage *poppler_page;
917 poppler_page = poppler_document_get_page (pdf_document->document,
918 MAX (0, dest->page_num - 1));
919 poppler_page_get_size (poppler_page, NULL, &height);
920 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
922 height - MIN (height, dest->bottom),
924 height - MIN (height, dest->top));
925 g_object_unref (poppler_page);
928 case POPPLER_DEST_FITB:
929 unimplemented_dest = "POPPLER_DEST_FITB";
931 case POPPLER_DEST_FITBH:
932 unimplemented_dest = "POPPLER_DEST_FITBH";
934 case POPPLER_DEST_FITBV:
935 unimplemented_dest = "POPPLER_DEST_FITBV";
937 case POPPLER_DEST_NAMED:
938 ev_dest = ev_link_dest_new_named (dest->named_dest);
940 case POPPLER_DEST_UNKNOWN:
941 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
945 if (unimplemented_dest) {
946 g_warning ("Unimplemented destination: %s, please post a "
947 "bug report in Evince bugzilla "
948 "(http://bugzilla.gnome.org) with a testcase.",
953 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
959 ev_link_from_action (PdfDocument *pdf_document,
960 PopplerAction *action)
963 EvLinkAction *ev_action = NULL;
964 const char *unimplemented_action = NULL;
966 switch (action->type) {
967 case POPPLER_ACTION_NONE:
969 case POPPLER_ACTION_GOTO_DEST: {
972 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
973 ev_action = ev_link_action_new_dest (dest);
976 case POPPLER_ACTION_GOTO_REMOTE: {
979 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
980 ev_action = ev_link_action_new_remote (dest,
981 action->goto_remote.file_name);
985 case POPPLER_ACTION_LAUNCH:
986 ev_action = ev_link_action_new_launch (action->launch.file_name,
987 action->launch.params);
989 case POPPLER_ACTION_URI:
990 ev_action = ev_link_action_new_external_uri (action->uri.uri);
992 case POPPLER_ACTION_NAMED:
993 ev_action = ev_link_action_new_named (action->named.named_dest);
995 case POPPLER_ACTION_MOVIE:
996 unimplemented_action = "POPPLER_ACTION_MOVIE";
998 case POPPLER_ACTION_UNKNOWN:
999 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1002 if (unimplemented_action) {
1003 g_warning ("Unimplemented action: %s, please post a bug report "
1004 "in Evince bugzilla (http://bugzilla.gnome.org) "
1005 "with a testcase.", unimplemented_action);
1008 link = ev_link_new (action->any.title, ev_action);
1014 build_tree (PdfDocument *pdf_document,
1015 GtkTreeModel *model,
1016 GtkTreeIter *parent,
1017 PopplerIndexIter *iter)
1021 GtkTreeIter tree_iter;
1022 PopplerIndexIter *child;
1023 PopplerAction *action;
1024 EvLink *link = NULL;
1028 action = poppler_index_iter_get_action (iter);
1029 expand = poppler_index_iter_is_open (iter);
1034 switch (action->type) {
1035 case POPPLER_ACTION_GOTO_DEST: {
1036 /* For bookmarks, solve named destinations */
1037 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1039 EvLinkDest *ev_dest = NULL;
1040 EvLinkAction *ev_action;
1042 dest = poppler_document_find_dest (pdf_document->document,
1043 action->goto_dest.dest->named_dest);
1045 link = ev_link_from_action (pdf_document, action);
1049 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1050 poppler_dest_free (dest);
1052 ev_action = ev_link_action_new_dest (ev_dest);
1053 link = ev_link_new (action->any.title, ev_action);
1055 link = ev_link_from_action (pdf_document, action);
1060 link = ev_link_from_action (pdf_document, action);
1064 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1065 poppler_action_free (action);
1067 g_object_unref (link);
1072 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1073 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1075 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1076 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1077 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1078 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1081 g_free (title_markup);
1082 g_object_unref (link);
1084 child = poppler_index_iter_get_child (iter);
1086 build_tree (pdf_document, model, &tree_iter, child);
1087 poppler_index_iter_free (child);
1088 poppler_action_free (action);
1090 } while (poppler_index_iter_next (iter));
1093 static GtkTreeModel *
1094 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1096 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1097 GtkTreeModel *model = NULL;
1098 PopplerIndexIter *iter;
1100 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1102 iter = poppler_index_iter_new (pdf_document->document);
1103 /* Create the model if we have items*/
1105 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1110 build_tree (pdf_document, model, NULL, iter);
1111 poppler_index_iter_free (iter);
1118 pdf_document_links_get_links (EvDocumentLinks *document_links,
1121 PdfDocument *pdf_document;
1122 PopplerPage *poppler_page;
1123 GList *retval = NULL;
1124 GList *mapping_list;
1128 pdf_document = PDF_DOCUMENT (document_links);
1129 poppler_page = poppler_document_get_page (pdf_document->document,
1131 mapping_list = poppler_page_get_link_mapping (poppler_page);
1132 poppler_page_get_size (poppler_page, NULL, &height);
1134 for (list = mapping_list; list; list = list->next) {
1135 PopplerLinkMapping *link_mapping;
1136 EvLinkMapping *ev_link_mapping;
1138 link_mapping = (PopplerLinkMapping *)list->data;
1139 ev_link_mapping = g_new (EvLinkMapping, 1);
1140 ev_link_mapping->link = ev_link_from_action (pdf_document,
1141 link_mapping->action);
1142 ev_link_mapping->x1 = link_mapping->area.x1;
1143 ev_link_mapping->x2 = link_mapping->area.x2;
1144 /* Invert this for X-style coordinates */
1145 ev_link_mapping->y1 = height - link_mapping->area.y2;
1146 ev_link_mapping->y2 = height - link_mapping->area.y1;
1148 retval = g_list_prepend (retval, ev_link_mapping);
1151 poppler_page_free_link_mapping (mapping_list);
1152 g_object_unref (poppler_page);
1154 return g_list_reverse (retval);
1158 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1159 const gchar *link_name)
1161 PdfDocument *pdf_document;
1163 EvLinkDest *ev_dest = NULL;
1165 pdf_document = PDF_DOCUMENT (document_links);
1166 dest = poppler_document_find_dest (pdf_document->document,
1169 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1170 poppler_dest_free (dest);
1177 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1179 iface->has_document_links = pdf_document_links_has_document_links;
1180 iface->get_links_model = pdf_document_links_get_links_model;
1181 iface->get_links = pdf_document_links_get_links;
1182 iface->find_link_dest = pdf_document_links_find_link_dest;
1186 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1189 GList *retval = NULL;
1190 PdfDocument *pdf_document;
1191 PopplerPage *poppler_page;
1192 GList *mapping_list;
1195 pdf_document = PDF_DOCUMENT (document_images);
1196 poppler_page = poppler_document_get_page (pdf_document->document, page);
1197 mapping_list = poppler_page_get_image_mapping (poppler_page);
1199 for (list = mapping_list; list; list = list->next) {
1200 PopplerImageMapping *image_mapping;
1201 EvImageMapping *ev_image_mapping;
1203 image_mapping = (PopplerImageMapping *)list->data;
1205 ev_image_mapping = g_new (EvImageMapping, 1);
1207 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1208 ev_image_mapping->x1 = image_mapping->area.x1;
1209 ev_image_mapping->x2 = image_mapping->area.x2;
1210 ev_image_mapping->y1 = image_mapping->area.y1;
1211 ev_image_mapping->y2 = image_mapping->area.y2;
1213 retval = g_list_prepend (retval, ev_image_mapping);
1216 poppler_page_free_image_mapping (mapping_list);
1217 g_object_unref (poppler_page);
1219 return g_list_reverse (retval);
1223 pdf_document_images_get_image (EvDocumentImages *document_images,
1226 PdfDocument *pdf_document;
1227 PopplerPage *poppler_page;
1228 cairo_surface_t *surface;
1229 GdkPixbuf *retval = NULL;
1231 pdf_document = PDF_DOCUMENT (document_images);
1232 poppler_page = poppler_document_get_page (pdf_document->document,
1233 ev_image_get_page (image));
1235 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1237 retval = ev_document_misc_pixbuf_from_surface (surface);
1238 cairo_surface_destroy (surface);
1241 g_object_unref (poppler_page);
1247 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1249 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1250 iface->get_image = pdf_document_images_get_image;
1254 make_thumbnail_for_page (PdfDocument *pdf_document,
1255 PopplerPage *poppler_page,
1256 EvRenderContext *rc)
1261 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1262 rc, &width, &height);
1263 #ifdef POPPLER_WITH_GDK
1264 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1266 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1268 ev_document_fc_mutex_lock ();
1269 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1271 rc->scale, rc->rotation, pixbuf);
1272 ev_document_fc_mutex_unlock ();
1274 cairo_surface_t *surface;
1276 ev_document_fc_mutex_lock ();
1277 surface = pdf_page_render (poppler_page, width, height, rc);
1278 ev_document_fc_mutex_unlock ();
1280 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1281 cairo_surface_destroy (surface);
1282 #endif /* POPPLER_WITH_GDK */
1288 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1289 EvRenderContext *rc,
1292 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1293 PopplerPage *poppler_page;
1294 GdkPixbuf *pixbuf = NULL;
1295 GdkPixbuf *border_pixbuf;
1297 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1299 #ifdef POPPLER_WITH_GDK
1300 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1302 cairo_surface_t *surface;
1304 surface = poppler_page_get_thumbnail (poppler_page);
1306 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1307 cairo_surface_destroy (surface);
1309 #endif /* POPPLER_WITH_GDK */
1312 /* Rotate provided thumbnail if needed */
1313 GdkPixbuf *rotated_pixbuf;
1315 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1316 (GdkPixbufRotation) (360 - rc->rotation));
1317 g_object_unref (pixbuf);
1318 pixbuf = rotated_pixbuf;
1320 /* There is no provided thumbnail. We need to make one. */
1321 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1325 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1326 g_object_unref (pixbuf);
1327 pixbuf = border_pixbuf;
1334 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1335 EvRenderContext *rc,
1339 PopplerPage *poppler_page;
1342 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1344 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1346 if (!has_thumb || *width <= 0 || *height <= 0) {
1347 double page_width, page_height;
1349 poppler_page_get_size (poppler_page, &page_width, &page_height);
1351 *width = (gint) MAX (page_width * rc->scale, 1);
1352 *height = (gint) MAX (page_height * rc->scale, 1);
1355 if (rc->rotation == 90 || rc->rotation == 270) {
1365 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1367 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1368 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1373 pdf_document_find_find_text (EvDocumentFind *document_find,
1376 gboolean case_sensitive)
1379 PopplerPage *poppler_page;
1382 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1383 g_return_val_if_fail (text != NULL, NULL);
1385 poppler_page = POPPLER_PAGE (page->backend_page);
1387 matches = poppler_page_find_text (poppler_page, text);
1391 poppler_page_get_size (poppler_page, NULL, &height);
1392 for (l = matches; l && l->data; l = g_list_next (l)) {
1393 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1397 rect->y1 = height - rect->y2;
1398 rect->y2 = height - tmp;
1405 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1407 iface->find_text = pdf_document_find_find_text;
1411 pdf_print_context_free (PdfPrintContext *ctx)
1416 #ifdef HAVE_CAIRO_PRINT
1418 cairo_destroy (ctx->cr);
1423 poppler_ps_file_free (ctx->ps_file);
1424 ctx->ps_file = NULL;
1431 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1432 EvFileExporterContext *fc)
1434 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1435 PdfPrintContext *ctx;
1436 #ifdef HAVE_CAIRO_PRINT
1437 gdouble width, height;
1438 cairo_surface_t *surface = NULL;
1441 if (pdf_document->print_ctx)
1442 pdf_print_context_free (pdf_document->print_ctx);
1443 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1444 ctx = pdf_document->print_ctx;
1445 ctx->format = fc->format;
1447 #ifdef HAVE_CAIRO_PRINT
1448 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1450 ctx->paper_width = fc->paper_width;
1451 ctx->paper_height = fc->paper_height;
1453 switch (fc->pages_per_sheet) {
1481 ctx->pages_printed = 0;
1483 switch (fc->format) {
1484 case EV_FILE_FORMAT_PS:
1485 #ifdef HAVE_CAIRO_PS
1486 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1489 case EV_FILE_FORMAT_PDF:
1490 #ifdef HAVE_CAIRO_PDF
1491 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1495 g_assert_not_reached ();
1498 ctx->cr = cairo_create (surface);
1499 cairo_surface_destroy (surface);
1501 #else /* HAVE_CAIRO_PRINT */
1502 if (ctx->format == EV_FILE_FORMAT_PS) {
1503 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1504 fc->filename, fc->first_page,
1505 fc->last_page - fc->first_page + 1);
1506 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1507 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1509 #endif /* HAVE_CAIRO_PRINT */
1513 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1515 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1516 PdfPrintContext *ctx = pdf_document->print_ctx;
1518 g_return_if_fail (pdf_document->print_ctx != NULL);
1520 ctx->pages_printed = 0;
1522 #ifdef HAVE_CAIRO_PRINT
1523 if (ctx->paper_width > ctx->paper_height) {
1524 if (ctx->format == EV_FILE_FORMAT_PS) {
1525 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1528 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1529 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1534 #endif /* HAVE_CAIRO_PRINT */
1538 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1539 EvRenderContext *rc)
1541 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1542 PdfPrintContext *ctx = pdf_document->print_ctx;
1543 PopplerPage *poppler_page;
1544 #ifdef HAVE_CAIRO_PRINT
1545 gdouble page_width, page_height;
1548 gdouble width, height;
1549 gdouble pwidth, pheight;
1550 gdouble xscale, yscale;
1553 g_return_if_fail (pdf_document->print_ctx != NULL);
1555 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1557 #ifdef HAVE_CAIRO_PRINT
1558 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1559 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1560 poppler_page_get_size (poppler_page, &page_width, &page_height);
1562 if (page_width > page_height && page_width > ctx->paper_width) {
1568 /* Use always portrait mode and rotate when necessary */
1569 if (ctx->paper_width > ctx->paper_height) {
1570 width = ctx->paper_height;
1571 height = ctx->paper_width;
1574 width = ctx->paper_width;
1575 height = ctx->paper_height;
1578 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1591 page_width = page_height;
1595 pwidth = width / ctx->pages_x;
1596 pheight = height / ctx->pages_y;
1598 if ((page_width > pwidth || page_height > pheight) ||
1599 (page_width < pwidth && page_height < pheight)) {
1600 xscale = pwidth / page_width;
1601 yscale = pheight / page_height;
1603 if (yscale < xscale) {
1610 xscale = yscale = 1;
1615 cairo_save (ctx->cr);
1617 cairo_matrix_t matrix;
1619 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1620 cairo_matrix_init (&matrix,
1624 cairo_transform (ctx->cr, &matrix);
1627 cairo_translate (ctx->cr,
1628 x * (rotate ? pheight : pwidth),
1629 y * (rotate ? pwidth : pheight));
1630 cairo_scale (ctx->cr, xscale, yscale);
1632 poppler_page_render_for_printing (poppler_page, ctx->cr);
1634 ctx->pages_printed++;
1636 cairo_restore (ctx->cr);
1637 #else /* HAVE_CAIRO_PRINT */
1638 if (ctx->format == EV_FILE_FORMAT_PS)
1639 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1640 #endif /* HAVE_CAIRO_PRINT */
1644 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1646 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1647 PdfPrintContext *ctx = pdf_document->print_ctx;
1649 g_return_if_fail (pdf_document->print_ctx != NULL);
1651 #ifdef HAVE_CAIRO_PRINT
1652 cairo_show_page (ctx->cr);
1657 pdf_document_file_exporter_end (EvFileExporter *exporter)
1659 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1661 pdf_print_context_free (pdf_document->print_ctx);
1662 pdf_document->print_ctx = NULL;
1665 static EvFileExporterCapabilities
1666 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1668 return (EvFileExporterCapabilities) (
1669 EV_FILE_EXPORTER_CAN_PAGE_SET |
1670 EV_FILE_EXPORTER_CAN_COPIES |
1671 EV_FILE_EXPORTER_CAN_COLLATE |
1672 EV_FILE_EXPORTER_CAN_REVERSE |
1673 EV_FILE_EXPORTER_CAN_SCALE |
1674 #ifdef HAVE_CAIRO_PRINT
1675 #ifdef HAVE_POPPLER_PAGE_RENDER
1676 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1680 #ifdef HAVE_CAIRO_PDF
1681 #ifdef HAVE_POPPLER_PAGE_RENDER
1682 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1685 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1689 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1691 iface->begin = pdf_document_file_exporter_begin;
1692 iface->begin_page = pdf_document_file_exporter_begin_page;
1693 iface->do_page = pdf_document_file_exporter_do_page;
1694 iface->end_page = pdf_document_file_exporter_end_page;
1695 iface->end = pdf_document_file_exporter_end;
1696 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1700 pdf_selection_render_selection (EvSelection *selection,
1701 EvRenderContext *rc,
1702 cairo_surface_t **surface,
1703 EvRectangle *points,
1704 EvRectangle *old_points,
1705 EvSelectionStyle style,
1709 PopplerPage *poppler_page;
1710 double width_points, height_points;
1713 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1715 poppler_page_get_size (poppler_page,
1716 &width_points, &height_points);
1717 width = (int) ((width_points * rc->scale) + 0.5);
1718 height = (int) ((height_points * rc->scale) + 0.5);
1720 #ifdef HAVE_POPPLER_PAGE_RENDER
1722 PopplerColor text_color, base_color;
1724 text_color.red = text->red;
1725 text_color.green = text->green;
1726 text_color.blue = text->blue;
1728 base_color.red = base->red;
1729 base_color.green = base->green;
1730 base_color.blue = base->blue;
1732 if (*surface == NULL) {
1733 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1738 cr = cairo_create (*surface);
1739 cairo_scale (cr, rc->scale, rc->scale);
1740 cairo_surface_set_device_offset (*surface, 0, 0);
1741 memset (cairo_image_surface_get_data (*surface), 0x00,
1742 cairo_image_surface_get_height (*surface) *
1743 cairo_image_surface_get_stride (*surface));
1744 poppler_page_render_selection (poppler_page,
1746 (PopplerRectangle *)points,
1747 (PopplerRectangle *)old_points,
1748 (PopplerSelectionStyle)style,
1752 #else /* HAVE_POPPLER_PAGE_RENDER */
1755 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1759 poppler_page_render_selection_to_pixbuf (poppler_page,
1760 rc->scale, rc->rotation, pixbuf,
1761 (PopplerRectangle *)points,
1762 (PopplerRectangle *)old_points,
1763 (PopplerSelectionStyle)style,
1767 cairo_surface_destroy (*surface);
1768 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1769 g_object_unref (pixbuf);
1770 #endif /* HAVE_POPPLER_PAGE_RENDER */
1774 pdf_selection_get_selected_text (EvSelection *selection,
1775 EvRenderContext *rc,
1776 EvSelectionStyle style,
1777 EvRectangle *points)
1779 PopplerPage *poppler_page;
1784 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1786 poppler_page_get_size (poppler_page, NULL, &height);
1788 r.y1 = height - points->y2;
1790 r.y2 = height - points->y1;
1792 retval = poppler_page_get_text (poppler_page,
1793 (PopplerSelectionStyle)style,
1800 create_gdk_region_from_poppler_region (GList *region)
1805 retval = gdk_region_new ();
1807 for (l = region; l; l = g_list_next (l)) {
1808 PopplerRectangle *rectangle;
1811 rectangle = (PopplerRectangle *)l->data;
1813 rect.x = (gint) rectangle->x1;
1814 rect.y = (gint) rectangle->y1;
1815 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1816 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1817 gdk_region_union_with_rect (retval, &rect);
1819 poppler_rectangle_free (rectangle);
1826 pdf_selection_get_selection_region (EvSelection *selection,
1827 EvRenderContext *rc,
1828 EvSelectionStyle style,
1829 EvRectangle *points)
1831 PopplerPage *poppler_page;
1835 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1837 region = poppler_page_get_selection_region (poppler_page,
1839 (PopplerSelectionStyle)style,
1840 (PopplerRectangle *) points);
1841 retval = create_gdk_region_from_poppler_region (region);
1842 g_list_free (region);
1848 pdf_selection_get_selection_map (EvSelection *selection,
1849 EvRenderContext *rc)
1851 PopplerPage *poppler_page;
1852 PopplerRectangle points;
1856 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1860 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1862 region = poppler_page_get_selection_region (poppler_page, 1.0,
1863 POPPLER_SELECTION_GLYPH,
1865 retval = create_gdk_region_from_poppler_region (region);
1866 g_list_free (region);
1872 pdf_selection_iface_init (EvSelectionIface *iface)
1874 iface->render_selection = pdf_selection_render_selection;
1875 iface->get_selected_text = pdf_selection_get_selected_text;
1876 iface->get_selection_region = pdf_selection_get_selection_region;
1877 iface->get_selection_map = pdf_selection_get_selection_map;
1880 /* Page Transitions */
1882 pdf_document_get_page_duration (EvDocumentTransition *trans,
1885 PdfDocument *pdf_document;
1886 PopplerPage *poppler_page;
1887 gdouble duration = -1;
1889 pdf_document = PDF_DOCUMENT (trans);
1890 poppler_page = poppler_document_get_page (pdf_document->document, page);
1894 duration = poppler_page_get_duration (poppler_page);
1895 g_object_unref (poppler_page);
1900 static EvTransitionEffect *
1901 pdf_document_get_effect (EvDocumentTransition *trans,
1904 PdfDocument *pdf_document;
1905 PopplerPage *poppler_page;
1906 PopplerPageTransition *page_transition;
1907 EvTransitionEffect *effect;
1909 pdf_document = PDF_DOCUMENT (trans);
1910 poppler_page = poppler_document_get_page (pdf_document->document, page);
1915 page_transition = poppler_page_get_transition (poppler_page);
1917 if (!page_transition) {
1918 g_object_unref (poppler_page);
1922 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1923 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1924 "alignment", page_transition->alignment,
1925 "direction", page_transition->direction,
1926 "duration", page_transition->duration,
1927 "angle", page_transition->angle,
1928 "scale", page_transition->scale,
1929 "rectangular", page_transition->rectangular,
1932 poppler_page_transition_free (page_transition);
1933 g_object_unref (poppler_page);
1939 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1941 iface->get_page_duration = pdf_document_get_page_duration;
1942 iface->get_effect = pdf_document_get_effect;
1947 pdf_document_get_crop_box (EvDocument *document,
1951 PdfDocument *pdf_document;
1952 PopplerPage *poppler_page;
1953 PopplerRectangle poppler_rect;
1955 pdf_document = PDF_DOCUMENT (document);
1956 poppler_page = poppler_document_get_page (pdf_document->document, page);
1957 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1958 rect->x1 = poppler_rect.x1;
1959 rect->x2 = poppler_rect.x2;
1960 rect->y1 = poppler_rect.y1;
1961 rect->y2 = poppler_rect.y2;
1964 static EvFormField *
1965 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1967 EvFormField *ev_field = NULL;
1970 gboolean is_read_only;
1972 id = poppler_form_field_get_id (poppler_field);
1973 font_size = poppler_form_field_get_font_size (poppler_field);
1974 is_read_only = poppler_form_field_is_read_only (poppler_field);
1976 switch (poppler_form_field_get_field_type (poppler_field)) {
1977 case POPPLER_FORM_FIELD_TEXT: {
1978 EvFormFieldText *field_text;
1979 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1981 switch (poppler_form_field_text_get_text_type (poppler_field)) {
1982 case POPPLER_FORM_TEXT_NORMAL:
1983 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1985 case POPPLER_FORM_TEXT_MULTILINE:
1986 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
1988 case POPPLER_FORM_TEXT_FILE_SELECT:
1989 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
1993 ev_field = ev_form_field_text_new (id, ev_text_type);
1994 field_text = EV_FORM_FIELD_TEXT (ev_field);
1996 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
1997 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
1998 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
1999 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2000 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2001 field_text->text = poppler_form_field_text_get_text (poppler_field);
2005 case POPPLER_FORM_FIELD_BUTTON: {
2006 EvFormFieldButton *field_button;
2007 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2009 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2010 case POPPLER_FORM_BUTTON_PUSH:
2011 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2013 case POPPLER_FORM_BUTTON_CHECK:
2014 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2016 case POPPLER_FORM_BUTTON_RADIO:
2017 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2021 ev_field = ev_form_field_button_new (id, ev_button_type);
2022 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2024 field_button->state = poppler_form_field_button_get_state (poppler_field);
2027 case POPPLER_FORM_FIELD_CHOICE: {
2028 EvFormFieldChoice *field_choice;
2029 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2031 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2032 case POPPLER_FORM_CHOICE_COMBO:
2033 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2035 case EV_FORM_FIELD_CHOICE_LIST:
2036 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2040 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2041 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2043 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2044 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2045 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2046 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2048 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2049 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2050 if (field_choice->is_editable)
2051 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2054 case POPPLER_FORM_FIELD_SIGNATURE:
2056 ev_field = ev_form_field_signature_new (id);
2058 case POPPLER_FORM_FIELD_UNKNOWN:
2062 ev_field->font_size = font_size;
2063 ev_field->is_read_only = is_read_only;
2069 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2072 PopplerPage *poppler_page;
2073 GList *retval = NULL;
2078 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2080 poppler_page = POPPLER_PAGE (page->backend_page);
2081 fields = poppler_page_get_form_field_mapping (poppler_page);
2082 poppler_page_get_size (poppler_page, NULL, &height);
2084 for (list = fields; list; list = list->next) {
2085 PopplerFormFieldMapping *mapping;
2086 EvFormFieldMapping *field_mapping;
2087 EvFormField *ev_field;
2089 mapping = (PopplerFormFieldMapping *)list->data;
2091 ev_field = ev_form_field_from_poppler_field (mapping->field);
2095 field_mapping = g_new0 (EvFormFieldMapping, 1);
2096 field_mapping->x1 = mapping->area.x1;
2097 field_mapping->x2 = mapping->area.x2;
2098 field_mapping->y1 = height - mapping->area.y2;
2099 field_mapping->y2 = height - mapping->area.y1;
2100 field_mapping->field = ev_field;
2101 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2103 g_object_set_data_full (G_OBJECT (ev_field),
2105 g_object_ref (mapping->field),
2106 (GDestroyNotify) g_object_unref);
2108 retval = g_list_prepend (retval, field_mapping);
2111 poppler_page_free_form_field_mapping (fields);
2113 return g_list_reverse (retval);
2117 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2121 PopplerFormField *poppler_field;
2124 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2128 text = poppler_form_field_text_get_text (poppler_field);
2134 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2138 PopplerFormField *poppler_field;
2140 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2143 poppler_form_field_text_set_text (poppler_field, text);
2147 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2151 PopplerFormField *poppler_field;
2153 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2157 poppler_form_field_button_set_state (poppler_field, state);
2161 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2164 PopplerFormField *poppler_field;
2167 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2171 state = poppler_form_field_button_get_state (poppler_field);
2177 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2181 PopplerFormField *poppler_field;
2184 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2188 text = poppler_form_field_choice_get_item (poppler_field, index);
2194 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2197 PopplerFormField *poppler_field;
2200 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2204 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2210 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2214 PopplerFormField *poppler_field;
2217 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2221 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2227 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2231 PopplerFormField *poppler_field;
2233 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2237 poppler_form_field_choice_select_item (poppler_field, index);
2241 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2245 PopplerFormField *poppler_field;
2247 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2251 poppler_form_field_choice_toggle_item (poppler_field, index);
2255 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2258 PopplerFormField *poppler_field;
2260 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2264 poppler_form_field_choice_unselect_all (poppler_field);
2268 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2272 PopplerFormField *poppler_field;
2274 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2278 poppler_form_field_choice_set_text (poppler_field, text);
2282 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2285 PopplerFormField *poppler_field;
2288 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2292 text = poppler_form_field_choice_get_text (poppler_field);
2298 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2300 iface->get_form_fields = pdf_document_forms_get_form_fields;
2301 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2302 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2303 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2304 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2305 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2306 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2307 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2308 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2309 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2310 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2311 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2312 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2317 pdf_document_layers_has_layers (EvDocumentLayers *document)
2319 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2320 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2321 PopplerLayersIter *iter;
2323 iter = poppler_layers_iter_new (pdf_document->document);
2326 poppler_layers_iter_free (iter);
2331 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2334 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2336 build_layers_tree (PdfDocument *pdf_document,
2337 GtkTreeModel *model,
2338 GtkTreeIter *parent,
2339 PopplerLayersIter *iter)
2342 GtkTreeIter tree_iter;
2343 PopplerLayersIter *child;
2344 PopplerLayer *layer;
2345 EvLayer *ev_layer = NULL;
2350 layer = poppler_layers_iter_get_layer (iter);
2352 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2353 visible = poppler_layer_is_visible (layer);
2354 rb_group = poppler_layer_get_radio_button_group_id (layer);
2355 pdf_document->layers = g_list_append (pdf_document->layers,
2356 g_object_ref (layer));
2357 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2358 poppler_layer_is_parent (layer),
2363 title = poppler_layers_iter_get_title (iter);
2364 markup = g_markup_escape_text (title, -1);
2371 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2372 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2373 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2374 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2375 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2376 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2377 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2378 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2381 g_object_unref (ev_layer);
2384 child = poppler_layers_iter_get_child (iter);
2386 build_layers_tree (pdf_document, model, &tree_iter, child);
2387 poppler_layers_iter_free (child);
2388 } while (poppler_layers_iter_next (iter));
2390 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2392 static GtkTreeModel *
2393 pdf_document_layers_get_layers (EvDocumentLayers *document)
2395 GtkTreeModel *model = NULL;
2396 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2397 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2398 PopplerLayersIter *iter;
2400 iter = poppler_layers_iter_new (pdf_document->document);
2402 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2403 G_TYPE_STRING, /* TITLE */
2404 G_TYPE_OBJECT, /* LAYER */
2405 G_TYPE_BOOLEAN, /* VISIBLE */
2406 G_TYPE_BOOLEAN, /* ENABLED */
2407 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2408 G_TYPE_INT); /* RBGROUP */
2409 build_layers_tree (pdf_document, model, NULL, iter);
2410 poppler_layers_iter_free (iter);
2418 pdf_document_layers_show_layer (EvDocumentLayers *document,
2421 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2422 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2423 guint layer_id = ev_layer_get_id (layer);
2425 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2428 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2432 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2435 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2436 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2437 guint layer_id = ev_layer_get_id (layer);
2439 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2442 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2446 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2449 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2450 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2451 guint layer_id = ev_layer_get_id (layer);
2453 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2456 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2460 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2462 iface->has_layers = pdf_document_layers_has_layers;
2463 iface->get_layers = pdf_document_layers_get_layers;
2464 iface->show_layer = pdf_document_layers_show_layer;
2465 iface->hide_layer = pdf_document_layers_hide_layer;
2466 iface->layer_is_visible = pdf_document_layers_layer_is_visible;