1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pdfdocument.h: Implementation of EvDocument for PDF
3 * Copyright (C) 2004, Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <poppler-document.h>
27 #include <poppler-page.h>
29 #include <cairo-pdf.h>
34 #include <glib/gi18n.h>
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-document-find.h"
39 #include "ev-document-misc.h"
40 #include "ev-document-links.h"
41 #include "ev-document-images.h"
42 #include "ev-document-fonts.h"
43 #include "ev-document-security.h"
44 #include "ev-document-thumbnails.h"
45 #include "ev-document-transition.h"
46 #include "ev-document-forms.h"
47 #include "ev-selection.h"
48 #include "ev-transition-effect.h"
49 #include "ev-attachment.h"
52 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
53 #define HAVE_CAIRO_PRINT
57 PdfDocument *document;
66 EvFileExporterFormat format;
76 #ifdef HAVE_CAIRO_PRINT
79 PopplerPSFile *ps_file;
83 struct _PdfDocumentClass
85 GObjectClass parent_class;
90 GObject parent_instance;
92 PopplerDocument *document;
95 PopplerFontInfo *font_info;
96 PopplerFontsIter *fonts_iter;
97 int fonts_scanned_pages;
99 PdfDocumentSearch *search;
100 PdfPrintContext *print_ctx;
103 static void pdf_document_document_iface_init (EvDocumentIface *iface);
104 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
105 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
106 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
107 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
108 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
109 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
110 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
111 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
112 static void pdf_selection_iface_init (EvSelectionIface *iface);
113 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
114 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
118 static int pdf_document_get_n_pages (EvDocument *document);
120 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
122 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
123 PopplerAction *action);
124 static void pdf_document_search_free (PdfDocumentSearch *search);
125 static void pdf_print_context_free (PdfPrintContext *ctx);
127 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
129 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
130 pdf_document_security_iface_init);
131 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
132 pdf_document_document_thumbnails_iface_init);
133 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
134 pdf_document_document_links_iface_init);
135 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
136 pdf_document_document_images_iface_init);
137 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
138 pdf_document_document_forms_iface_init);
139 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
140 pdf_document_document_fonts_iface_init);
141 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
142 pdf_document_find_iface_init);
143 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
144 pdf_document_file_exporter_iface_init);
145 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
146 pdf_selection_iface_init);
147 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
148 pdf_document_page_transition_iface_init);
152 pdf_document_search_free (PdfDocumentSearch *search)
154 PdfDocument *pdf_document = search->document;
158 if (search->idle != 0)
159 g_source_remove (search->idle);
161 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
162 for (i = 0; i < n_pages; i++) {
163 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
164 g_list_free (search->pages[i]);
166 g_free (search->pages);
168 g_free (search->text);
173 pdf_document_dispose (GObject *object)
175 PdfDocument *pdf_document = PDF_DOCUMENT(object);
177 if (pdf_document->print_ctx) {
178 pdf_print_context_free (pdf_document->print_ctx);
179 pdf_document->print_ctx = NULL;
182 if (pdf_document->search) {
183 pdf_document_search_free (pdf_document->search);
184 pdf_document->search = NULL;
187 if (pdf_document->document) {
188 g_object_unref (pdf_document->document);
191 if (pdf_document->font_info) {
192 poppler_font_info_free (pdf_document->font_info);
195 if (pdf_document->fonts_iter) {
196 poppler_fonts_iter_free (pdf_document->fonts_iter);
199 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
203 pdf_document_class_init (PdfDocumentClass *klass)
205 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
207 g_object_class->dispose = pdf_document_dispose;
211 pdf_document_init (PdfDocument *pdf_document)
213 pdf_document->password = NULL;
217 convert_error (GError *poppler_error,
220 if (poppler_error == NULL)
223 if (poppler_error->domain == POPPLER_ERROR) {
224 /* convert poppler errors into EvDocument errors */
225 gint code = EV_DOCUMENT_ERROR_INVALID;
226 if (poppler_error->code == POPPLER_ERROR_INVALID)
227 code = EV_DOCUMENT_ERROR_INVALID;
228 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
229 code = EV_DOCUMENT_ERROR_ENCRYPTED;
235 poppler_error->message,
238 g_propagate_error (error, poppler_error);
245 pdf_document_save (EvDocument *document,
250 GError *poppler_error = NULL;
252 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
256 convert_error (poppler_error, error);
262 pdf_document_load (EvDocument *document,
266 GError *poppler_error = NULL;
267 PdfDocument *pdf_document = PDF_DOCUMENT (document);
269 pdf_document->document =
270 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
272 if (pdf_document->document == NULL) {
273 convert_error (poppler_error, error);
281 pdf_document_get_n_pages (EvDocument *document)
283 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
287 pdf_document_get_page (EvDocument *document,
290 PdfDocument *pdf_document = PDF_DOCUMENT (document);
291 PopplerPage *poppler_page;
294 poppler_page = poppler_document_get_page (pdf_document->document, index);
295 page = ev_page_new (index);
296 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
297 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
298 g_object_unref (poppler_page);
304 pdf_document_get_page_size (EvDocument *document,
309 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
311 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
315 pdf_document_get_page_label (EvDocument *document,
320 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
322 g_object_get (G_OBJECT (page->backend_page),
329 pdf_document_has_attachments (EvDocument *document)
331 PdfDocument *pdf_document;
333 pdf_document = PDF_DOCUMENT (document);
335 return poppler_document_has_attachments (pdf_document->document);
338 struct SaveToBufferData {
344 attachment_save_to_buffer_callback (const gchar *buf,
349 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
353 if (sdata->len + count > sdata->max) {
354 new_max = MAX (sdata->max * 2, sdata->len + count);
355 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
357 sdata->buffer = new_buffer;
358 sdata->max = new_max;
361 memcpy (sdata->buffer + sdata->len, buf, count);
368 attachment_save_to_buffer (PopplerAttachment *attachment,
373 static const gint initial_max = 1024;
374 struct SaveToBufferData sdata;
379 sdata.buffer = (gchar *) g_malloc (initial_max);
380 sdata.max = initial_max;
383 if (! poppler_attachment_save_to_callback (attachment,
384 attachment_save_to_buffer_callback,
387 g_free (sdata.buffer);
391 *buffer = sdata.buffer;
392 *buffer_size = sdata.len;
398 pdf_document_get_attachments (EvDocument *document)
400 PdfDocument *pdf_document;
403 GList *retval = NULL;
405 pdf_document = PDF_DOCUMENT (document);
407 if (!pdf_document_has_attachments (document))
410 attachments = poppler_document_get_attachments (pdf_document->document);
412 for (list = attachments; list; list = list->next) {
413 PopplerAttachment *attachment;
414 EvAttachment *ev_attachment;
417 GError *error = NULL;
419 attachment = (PopplerAttachment *) list->data;
421 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
422 ev_attachment = ev_attachment_new (attachment->name,
423 attachment->description,
428 retval = g_list_prepend (retval, ev_attachment);
431 g_warning ("%s", error->message);
432 g_error_free (error);
438 g_object_unref (attachment);
441 return g_list_reverse (retval);
444 static cairo_surface_t *
445 pdf_page_render (PopplerPage *page,
450 cairo_surface_t *surface;
452 #ifdef HAVE_POPPLER_PAGE_RENDER
455 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
457 memset (cairo_image_surface_get_data (surface), 0xff,
458 cairo_image_surface_get_height (surface) *
459 cairo_image_surface_get_stride (surface));
461 cr = cairo_create (surface);
462 switch (rc->rotation) {
464 cairo_translate (cr, width, 0);
467 cairo_translate (cr, width, height);
470 cairo_translate (cr, 0, height);
473 cairo_translate (cr, 0, 0);
475 cairo_scale (cr, rc->scale, rc->scale);
476 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
477 poppler_page_render (page, cr);
479 #else /* HAVE_POPPLER_PAGE_RENDER */
482 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
486 poppler_page_render_to_pixbuf (page,
492 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
493 g_object_unref (pixbuf);
494 #endif /* HAVE_POPPLER_PAGE_RENDER */
499 static cairo_surface_t *
500 pdf_document_render (EvDocument *document,
503 PdfDocument *pdf_document;
504 PopplerPage *poppler_page;
505 double width_points, height_points;
508 poppler_page = POPPLER_PAGE (rc->page->backend_page);
510 poppler_page_get_size (poppler_page,
511 &width_points, &height_points);
513 if (rc->rotation == 90 || rc->rotation == 270) {
514 width = (int) ((height_points * rc->scale) + 0.5);
515 height = (int) ((width_points * rc->scale) + 0.5);
517 width = (int) ((width_points * rc->scale) + 0.5);
518 height = (int) ((height_points * rc->scale) + 0.5);
521 return pdf_page_render (poppler_page,
525 /* EvDocumentSecurity */
528 pdf_document_has_document_security (EvDocumentSecurity *document_security)
530 /* FIXME: do we really need to have this? */
535 pdf_document_set_password (EvDocumentSecurity *document_security,
536 const char *password)
538 PdfDocument *document = PDF_DOCUMENT (document_security);
540 if (document->password)
541 g_free (document->password);
543 document->password = g_strdup (password);
546 static EvDocumentInfo *
547 pdf_document_get_info (EvDocument *document)
549 EvDocumentInfo *info;
550 PopplerPageLayout layout;
551 PopplerPageMode mode;
552 PopplerViewerPreferences view_prefs;
553 PopplerPermissions permissions;
556 info = g_new0 (EvDocumentInfo, 1);
558 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
559 EV_DOCUMENT_INFO_FORMAT |
560 EV_DOCUMENT_INFO_AUTHOR |
561 EV_DOCUMENT_INFO_SUBJECT |
562 EV_DOCUMENT_INFO_KEYWORDS |
563 EV_DOCUMENT_INFO_LAYOUT |
564 EV_DOCUMENT_INFO_START_MODE |
565 EV_DOCUMENT_INFO_PERMISSIONS |
566 EV_DOCUMENT_INFO_UI_HINTS |
567 EV_DOCUMENT_INFO_CREATOR |
568 EV_DOCUMENT_INFO_PRODUCER |
569 EV_DOCUMENT_INFO_CREATION_DATE |
570 EV_DOCUMENT_INFO_MOD_DATE |
571 EV_DOCUMENT_INFO_LINEARIZED |
572 EV_DOCUMENT_INFO_N_PAGES |
573 EV_DOCUMENT_INFO_SECURITY |
574 EV_DOCUMENT_INFO_PAPER_SIZE;
576 g_object_get (PDF_DOCUMENT (document)->document,
577 "title", &(info->title),
578 "format", &(info->format),
579 "author", &(info->author),
580 "subject", &(info->subject),
581 "keywords", &(info->keywords),
583 "page-layout", &layout,
584 "viewer-preferences", &view_prefs,
585 "permissions", &permissions,
586 "creator", &(info->creator),
587 "producer", &(info->producer),
588 "creation-date", &(info->creation_date),
589 "mod-date", &(info->modified_date),
590 "linearized", &(info->linearized),
593 info->n_pages = ev_document_get_n_pages (document);
595 if (info->n_pages > 0) {
596 page = ev_document_get_page (document, 0);
597 ev_document_get_page_size (document, page,
598 &(info->paper_width),
599 &(info->paper_height));
600 g_object_unref (page);
604 info->paper_width = info->paper_width / 72.0f * 25.4f;
605 info->paper_height = info->paper_height / 72.0f * 25.4f;
609 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
610 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
612 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
613 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
615 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
616 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
618 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
619 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
620 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
621 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
623 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
624 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
631 case POPPLER_PAGE_MODE_NONE:
632 info->mode = EV_DOCUMENT_MODE_NONE;
634 case POPPLER_PAGE_MODE_USE_THUMBS:
635 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
637 case POPPLER_PAGE_MODE_USE_OC:
638 info->mode = EV_DOCUMENT_MODE_USE_OC;
640 case POPPLER_PAGE_MODE_FULL_SCREEN:
641 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
643 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
644 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
650 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
651 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
653 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
654 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
656 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
657 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
659 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
660 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
662 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
663 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
665 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
666 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
668 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
669 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
672 info->permissions = 0;
673 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
674 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
676 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
677 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
679 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
680 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
682 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
683 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
686 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
687 /* translators: this is the document security state */
688 info->security = g_strdup (_("Yes"));
690 /* translators: this is the document security state */
691 info->security = g_strdup (_("No"));
698 pdf_document_document_iface_init (EvDocumentIface *iface)
700 iface->save = pdf_document_save;
701 iface->load = pdf_document_load;
702 iface->get_n_pages = pdf_document_get_n_pages;
703 iface->get_page = pdf_document_get_page;
704 iface->get_page_size = pdf_document_get_page_size;
705 iface->get_page_label = pdf_document_get_page_label;
706 iface->has_attachments = pdf_document_has_attachments;
707 iface->get_attachments = pdf_document_get_attachments;
708 iface->render = pdf_document_render;
709 iface->get_info = pdf_document_get_info;
713 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
715 iface->has_document_security = pdf_document_has_document_security;
716 iface->set_password = pdf_document_set_password;
720 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
722 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
725 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
727 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
731 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
734 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
737 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
739 if (pdf_document->font_info == NULL) {
740 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
743 if (pdf_document->fonts_iter) {
744 poppler_fonts_iter_free (pdf_document->fonts_iter);
747 pdf_document->fonts_scanned_pages += n_pages;
749 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
750 &pdf_document->fonts_iter);
752 pdf_document->fonts_scanned_pages = 0;
753 poppler_font_info_free (pdf_document->font_info);
754 pdf_document->font_info = NULL;
761 font_type_to_string (PopplerFontType type)
764 case POPPLER_FONT_TYPE_TYPE1:
766 case POPPLER_FONT_TYPE_TYPE1C:
768 case POPPLER_FONT_TYPE_TYPE3:
770 case POPPLER_FONT_TYPE_TRUETYPE:
771 return _("TrueType");
772 case POPPLER_FONT_TYPE_CID_TYPE0:
773 return _("Type 1 (CID)");
774 case POPPLER_FONT_TYPE_CID_TYPE0C:
775 return _("Type 1C (CID)");
776 case POPPLER_FONT_TYPE_CID_TYPE2:
777 return _("TrueType (CID)");
779 return _("Unknown font type");
784 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
787 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
788 PopplerFontsIter *iter = pdf_document->fonts_iter;
790 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
796 GtkTreeIter list_iter;
799 const char *embedded;
802 name = poppler_fonts_iter_get_name (iter);
808 type = font_type_to_string (
809 poppler_fonts_iter_get_font_type (iter));
811 if (poppler_fonts_iter_is_embedded (iter)) {
812 if (poppler_fonts_iter_is_subset (iter))
813 embedded = _("Embedded subset");
815 embedded = _("Embedded");
817 embedded = _("Not embedded");
820 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
822 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
823 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
824 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
825 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
829 } while (poppler_fonts_iter_next (iter));
833 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
835 iface->fill_model = pdf_document_fonts_fill_model;
836 iface->scan = pdf_document_fonts_scan;
837 iface->get_progress = pdf_document_fonts_get_progress;
841 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
843 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
844 PopplerIndexIter *iter;
846 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
848 iter = poppler_index_iter_new (pdf_document->document);
851 poppler_index_iter_free (iter);
857 ev_link_dest_from_dest (PdfDocument *pdf_document,
860 EvLinkDest *ev_dest = NULL;
861 const char *unimplemented_dest = NULL;
863 g_assert (dest != NULL);
865 switch (dest->type) {
866 case POPPLER_DEST_XYZ: {
867 PopplerPage *poppler_page;
870 poppler_page = poppler_document_get_page (pdf_document->document,
871 MAX (0, dest->page_num - 1));
872 poppler_page_get_size (poppler_page, NULL, &height);
873 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
880 g_object_unref (poppler_page);
883 case POPPLER_DEST_FIT:
884 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
886 case POPPLER_DEST_FITH: {
887 PopplerPage *poppler_page;
890 poppler_page = poppler_document_get_page (pdf_document->document,
891 MAX (0, dest->page_num - 1));
892 poppler_page_get_size (poppler_page, NULL, &height);
893 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
896 g_object_unref (poppler_page);
899 case POPPLER_DEST_FITV:
900 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
904 case POPPLER_DEST_FITR: {
905 PopplerPage *poppler_page;
908 poppler_page = poppler_document_get_page (pdf_document->document,
909 MAX (0, dest->page_num - 1));
910 poppler_page_get_size (poppler_page, NULL, &height);
911 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
913 height - dest->bottom,
916 g_object_unref (poppler_page);
919 case POPPLER_DEST_FITB:
920 unimplemented_dest = "POPPLER_DEST_FITB";
922 case POPPLER_DEST_FITBH:
923 unimplemented_dest = "POPPLER_DEST_FITBH";
925 case POPPLER_DEST_FITBV:
926 unimplemented_dest = "POPPLER_DEST_FITBV";
928 case POPPLER_DEST_NAMED:
929 ev_dest = ev_link_dest_new_named (dest->named_dest);
931 case POPPLER_DEST_UNKNOWN:
932 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
936 if (unimplemented_dest) {
937 g_warning ("Unimplemented destination: %s, please post a "
938 "bug report in Evince bugzilla "
939 "(http://bugzilla.gnome.org) with a testcase.",
944 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
950 ev_link_from_action (PdfDocument *pdf_document,
951 PopplerAction *action)
954 EvLinkAction *ev_action = NULL;
955 const char *unimplemented_action = NULL;
957 switch (action->type) {
958 case POPPLER_ACTION_NONE:
960 case POPPLER_ACTION_GOTO_DEST: {
963 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
964 ev_action = ev_link_action_new_dest (dest);
967 case POPPLER_ACTION_GOTO_REMOTE: {
970 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
971 ev_action = ev_link_action_new_remote (dest,
972 action->goto_remote.file_name);
976 case POPPLER_ACTION_LAUNCH:
977 ev_action = ev_link_action_new_launch (action->launch.file_name,
978 action->launch.params);
980 case POPPLER_ACTION_URI:
981 ev_action = ev_link_action_new_external_uri (action->uri.uri);
983 case POPPLER_ACTION_NAMED:
984 ev_action = ev_link_action_new_named (action->named.named_dest);
986 case POPPLER_ACTION_MOVIE:
987 unimplemented_action = "POPPLER_ACTION_MOVIE";
989 case POPPLER_ACTION_UNKNOWN:
990 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
993 if (unimplemented_action) {
994 g_warning ("Unimplemented action: %s, please post a bug report "
995 "in Evince bugzilla (http://bugzilla.gnome.org) "
996 "with a testcase.", unimplemented_action);
999 link = ev_link_new (action->any.title, ev_action);
1005 build_tree (PdfDocument *pdf_document,
1006 GtkTreeModel *model,
1007 GtkTreeIter *parent,
1008 PopplerIndexIter *iter)
1012 GtkTreeIter tree_iter;
1013 PopplerIndexIter *child;
1014 PopplerAction *action;
1015 EvLink *link = NULL;
1019 action = poppler_index_iter_get_action (iter);
1020 expand = poppler_index_iter_is_open (iter);
1025 switch (action->type) {
1026 case POPPLER_ACTION_GOTO_DEST: {
1027 /* For bookmarks, solve named destinations */
1028 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1030 EvLinkDest *ev_dest = NULL;
1031 EvLinkAction *ev_action;
1033 dest = poppler_document_find_dest (pdf_document->document,
1034 action->goto_dest.dest->named_dest);
1036 link = ev_link_from_action (pdf_document, action);
1040 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1041 poppler_dest_free (dest);
1043 ev_action = ev_link_action_new_dest (ev_dest);
1044 link = ev_link_new (action->any.title, ev_action);
1046 link = ev_link_from_action (pdf_document, action);
1051 link = ev_link_from_action (pdf_document, action);
1055 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1056 poppler_action_free (action);
1058 g_object_unref (link);
1063 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1064 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1066 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1067 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1068 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1069 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1072 g_free (title_markup);
1073 g_object_unref (link);
1075 child = poppler_index_iter_get_child (iter);
1077 build_tree (pdf_document, model, &tree_iter, child);
1078 poppler_index_iter_free (child);
1079 poppler_action_free (action);
1081 } while (poppler_index_iter_next (iter));
1084 static GtkTreeModel *
1085 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1087 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1088 GtkTreeModel *model = NULL;
1089 PopplerIndexIter *iter;
1091 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1093 iter = poppler_index_iter_new (pdf_document->document);
1094 /* Create the model if we have items*/
1096 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1101 build_tree (pdf_document, model, NULL, iter);
1102 poppler_index_iter_free (iter);
1109 pdf_document_links_get_links (EvDocumentLinks *document_links,
1112 PdfDocument *pdf_document;
1113 PopplerPage *poppler_page;
1114 GList *retval = NULL;
1115 GList *mapping_list;
1119 pdf_document = PDF_DOCUMENT (document_links);
1120 poppler_page = poppler_document_get_page (pdf_document->document,
1122 mapping_list = poppler_page_get_link_mapping (poppler_page);
1123 poppler_page_get_size (poppler_page, NULL, &height);
1125 for (list = mapping_list; list; list = list->next) {
1126 PopplerLinkMapping *link_mapping;
1127 EvLinkMapping *ev_link_mapping;
1129 link_mapping = (PopplerLinkMapping *)list->data;
1130 ev_link_mapping = g_new (EvLinkMapping, 1);
1131 ev_link_mapping->link = ev_link_from_action (pdf_document,
1132 link_mapping->action);
1133 ev_link_mapping->x1 = link_mapping->area.x1;
1134 ev_link_mapping->x2 = link_mapping->area.x2;
1135 /* Invert this for X-style coordinates */
1136 ev_link_mapping->y1 = height - link_mapping->area.y2;
1137 ev_link_mapping->y2 = height - link_mapping->area.y1;
1139 retval = g_list_prepend (retval, ev_link_mapping);
1142 poppler_page_free_link_mapping (mapping_list);
1143 g_object_unref (poppler_page);
1145 return g_list_reverse (retval);
1149 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1150 const gchar *link_name)
1152 PdfDocument *pdf_document;
1154 EvLinkDest *ev_dest = NULL;
1156 pdf_document = PDF_DOCUMENT (document_links);
1157 dest = poppler_document_find_dest (pdf_document->document,
1160 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1161 poppler_dest_free (dest);
1168 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1170 iface->has_document_links = pdf_document_links_has_document_links;
1171 iface->get_links_model = pdf_document_links_get_links_model;
1172 iface->get_links = pdf_document_links_get_links;
1173 iface->find_link_dest = pdf_document_links_find_link_dest;
1177 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1180 GList *retval = NULL;
1181 PdfDocument *pdf_document;
1182 PopplerPage *poppler_page;
1183 GList *mapping_list;
1186 pdf_document = PDF_DOCUMENT (document_images);
1187 poppler_page = poppler_document_get_page (pdf_document->document, page);
1188 mapping_list = poppler_page_get_image_mapping (poppler_page);
1190 for (list = mapping_list; list; list = list->next) {
1191 PopplerImageMapping *image_mapping;
1192 EvImageMapping *ev_image_mapping;
1194 image_mapping = (PopplerImageMapping *)list->data;
1196 ev_image_mapping = g_new (EvImageMapping, 1);
1198 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1199 ev_image_mapping->x1 = image_mapping->area.x1;
1200 ev_image_mapping->x2 = image_mapping->area.x2;
1201 ev_image_mapping->y1 = image_mapping->area.y1;
1202 ev_image_mapping->y2 = image_mapping->area.y2;
1204 retval = g_list_prepend (retval, ev_image_mapping);
1207 poppler_page_free_image_mapping (mapping_list);
1208 g_object_unref (poppler_page);
1210 return g_list_reverse (retval);
1214 pdf_document_images_get_image (EvDocumentImages *document_images,
1217 PdfDocument *pdf_document;
1218 PopplerPage *poppler_page;
1219 cairo_surface_t *surface;
1220 GdkPixbuf *retval = NULL;
1222 pdf_document = PDF_DOCUMENT (document_images);
1223 poppler_page = poppler_document_get_page (pdf_document->document,
1224 ev_image_get_page (image));
1226 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1228 retval = ev_document_misc_pixbuf_from_surface (surface);
1229 cairo_surface_destroy (surface);
1232 g_object_unref (poppler_page);
1238 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1240 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1241 iface->get_image = pdf_document_images_get_image;
1245 make_thumbnail_for_page (PdfDocument *pdf_document,
1246 PopplerPage *poppler_page,
1247 EvRenderContext *rc)
1252 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1253 rc, &width, &height);
1254 #ifdef POPPLER_WITH_GDK
1255 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1257 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1259 ev_document_fc_mutex_lock ();
1260 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1262 rc->scale, rc->rotation, pixbuf);
1263 ev_document_fc_mutex_unlock ();
1265 cairo_surface_t *surface;
1267 ev_document_fc_mutex_lock ();
1268 surface = pdf_page_render (poppler_page, width, height, rc);
1269 ev_document_fc_mutex_unlock ();
1271 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1272 cairo_surface_destroy (surface);
1273 #endif /* POPPLER_WITH_GDK */
1279 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1280 EvRenderContext *rc,
1283 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1284 PopplerPage *poppler_page;
1285 GdkPixbuf *pixbuf = NULL;
1286 GdkPixbuf *border_pixbuf;
1288 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1290 #ifdef POPPLER_WITH_GDK
1291 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1293 cairo_surface_t *surface;
1295 surface = poppler_page_get_thumbnail (poppler_page);
1297 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1298 cairo_surface_destroy (surface);
1300 #endif /* POPPLER_WITH_GDK */
1303 /* Rotate provided thumbnail if needed */
1304 GdkPixbuf *rotated_pixbuf;
1306 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1307 (GdkPixbufRotation) (360 - rc->rotation));
1308 g_object_unref (pixbuf);
1309 pixbuf = rotated_pixbuf;
1311 /* There is no provided thumbnail. We need to make one. */
1312 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1316 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1317 g_object_unref (pixbuf);
1318 pixbuf = border_pixbuf;
1325 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1326 EvRenderContext *rc,
1330 PopplerPage *poppler_page;
1333 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1335 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1337 if (!has_thumb || *width <= 0 || *height <= 0) {
1338 double page_width, page_height;
1340 poppler_page_get_size (poppler_page, &page_width, &page_height);
1342 *width = (gint) MAX (page_width * rc->scale, 1);
1343 *height = (gint) MAX (page_height * rc->scale, 1);
1346 if (rc->rotation == 90 || rc->rotation == 270) {
1356 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1358 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1359 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1364 pdf_document_find_find_text (EvDocumentFind *document_find,
1367 gboolean case_sensitive)
1370 PopplerPage *poppler_page;
1373 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1374 g_return_val_if_fail (text != NULL, NULL);
1376 poppler_page = POPPLER_PAGE (page->backend_page);
1378 matches = poppler_page_find_text (poppler_page, text);
1382 poppler_page_get_size (poppler_page, NULL, &height);
1383 for (l = matches; l && l->data; l = g_list_next (l)) {
1384 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1388 rect->y1 = height - rect->y2;
1389 rect->y2 = height - tmp;
1396 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1398 iface->find_text = pdf_document_find_find_text;
1402 pdf_print_context_free (PdfPrintContext *ctx)
1407 #ifdef HAVE_CAIRO_PRINT
1409 cairo_destroy (ctx->cr);
1414 poppler_ps_file_free (ctx->ps_file);
1415 ctx->ps_file = NULL;
1422 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1423 EvFileExporterContext *fc)
1425 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1426 PdfPrintContext *ctx;
1427 #ifdef HAVE_CAIRO_PRINT
1428 gdouble width, height;
1429 cairo_surface_t *surface = NULL;
1432 if (pdf_document->print_ctx)
1433 pdf_print_context_free (pdf_document->print_ctx);
1434 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1435 ctx = pdf_document->print_ctx;
1436 ctx->format = fc->format;
1438 #ifdef HAVE_CAIRO_PRINT
1439 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1441 ctx->paper_width = fc->paper_width;
1442 ctx->paper_height = fc->paper_height;
1444 switch (fc->pages_per_sheet) {
1472 ctx->pages_printed = 0;
1474 switch (fc->format) {
1475 case EV_FILE_FORMAT_PS:
1476 #ifdef HAVE_CAIRO_PS
1477 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1480 case EV_FILE_FORMAT_PDF:
1481 #ifdef HAVE_CAIRO_PDF
1482 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1486 g_assert_not_reached ();
1489 ctx->cr = cairo_create (surface);
1490 cairo_surface_destroy (surface);
1492 #else /* HAVE_CAIRO_PRINT */
1493 if (ctx->format == EV_FILE_FORMAT_PS) {
1494 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1495 fc->filename, fc->first_page,
1496 fc->last_page - fc->first_page + 1);
1497 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1498 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1500 #endif /* HAVE_CAIRO_PRINT */
1504 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1506 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1507 PdfPrintContext *ctx = pdf_document->print_ctx;
1509 g_return_if_fail (pdf_document->print_ctx != NULL);
1511 ctx->pages_printed = 0;
1513 #ifdef HAVE_CAIRO_PRINT
1514 if (ctx->paper_width > ctx->paper_height) {
1515 if (ctx->format == EV_FILE_FORMAT_PS) {
1516 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1519 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1520 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1525 #endif /* HAVE_CAIRO_PRINT */
1529 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1530 EvRenderContext *rc)
1532 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1533 PdfPrintContext *ctx = pdf_document->print_ctx;
1534 PopplerPage *poppler_page;
1535 #ifdef HAVE_CAIRO_PRINT
1536 gdouble page_width, page_height;
1539 gdouble width, height;
1540 gdouble pwidth, pheight;
1541 gdouble xscale, yscale;
1544 g_return_if_fail (pdf_document->print_ctx != NULL);
1546 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1548 #ifdef HAVE_CAIRO_PRINT
1549 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1550 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1551 poppler_page_get_size (poppler_page, &page_width, &page_height);
1553 if (page_width > page_height && page_width > ctx->paper_width) {
1559 /* Use always portrait mode and rotate when necessary */
1560 if (ctx->paper_width > ctx->paper_height) {
1561 width = ctx->paper_height;
1562 height = ctx->paper_width;
1565 width = ctx->paper_width;
1566 height = ctx->paper_height;
1569 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1582 page_width = page_height;
1586 pwidth = width / ctx->pages_x;
1587 pheight = height / ctx->pages_y;
1589 if ((page_width > pwidth || page_height > pheight) ||
1590 (page_width < pwidth && page_height < pheight)) {
1591 xscale = pwidth / page_width;
1592 yscale = pheight / page_height;
1594 if (yscale < xscale) {
1601 xscale = yscale = 1;
1606 cairo_save (ctx->cr);
1608 cairo_matrix_t matrix;
1610 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1611 cairo_matrix_init (&matrix,
1615 cairo_transform (ctx->cr, &matrix);
1618 cairo_translate (ctx->cr,
1619 x * (rotate ? pheight : pwidth),
1620 y * (rotate ? pwidth : pheight));
1621 cairo_scale (ctx->cr, xscale, yscale);
1623 poppler_page_render_for_printing (poppler_page, ctx->cr);
1625 ctx->pages_printed++;
1627 cairo_restore (ctx->cr);
1628 #else /* HAVE_CAIRO_PRINT */
1629 if (ctx->format == EV_FILE_FORMAT_PS)
1630 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1631 #endif /* HAVE_CAIRO_PRINT */
1635 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1637 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1638 PdfPrintContext *ctx = pdf_document->print_ctx;
1640 g_return_if_fail (pdf_document->print_ctx != NULL);
1642 #ifdef HAVE_CAIRO_PRINT
1643 cairo_show_page (ctx->cr);
1648 pdf_document_file_exporter_end (EvFileExporter *exporter)
1650 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1652 pdf_print_context_free (pdf_document->print_ctx);
1653 pdf_document->print_ctx = NULL;
1656 static EvFileExporterCapabilities
1657 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1659 return (EvFileExporterCapabilities) (
1660 EV_FILE_EXPORTER_CAN_PAGE_SET |
1661 EV_FILE_EXPORTER_CAN_COPIES |
1662 EV_FILE_EXPORTER_CAN_COLLATE |
1663 EV_FILE_EXPORTER_CAN_REVERSE |
1664 EV_FILE_EXPORTER_CAN_SCALE |
1665 #ifdef HAVE_CAIRO_PRINT
1666 #ifdef HAVE_POPPLER_PAGE_RENDER
1667 #if GTK_CHECK_VERSION (2, 11, 1)
1668 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1673 #ifdef HAVE_CAIRO_PDF
1674 #ifdef HAVE_POPPLER_PAGE_RENDER
1675 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1678 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1682 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1684 iface->begin = pdf_document_file_exporter_begin;
1685 iface->begin_page = pdf_document_file_exporter_begin_page;
1686 iface->do_page = pdf_document_file_exporter_do_page;
1687 iface->end_page = pdf_document_file_exporter_end_page;
1688 iface->end = pdf_document_file_exporter_end;
1689 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1693 pdf_selection_render_selection (EvSelection *selection,
1694 EvRenderContext *rc,
1695 cairo_surface_t **surface,
1696 EvRectangle *points,
1697 EvRectangle *old_points,
1698 EvSelectionStyle style,
1702 PopplerPage *poppler_page;
1703 double width_points, height_points;
1706 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1708 poppler_page_get_size (poppler_page,
1709 &width_points, &height_points);
1710 width = (int) ((width_points * rc->scale) + 0.5);
1711 height = (int) ((height_points * rc->scale) + 0.5);
1713 #ifdef HAVE_POPPLER_PAGE_RENDER
1715 PopplerColor text_color, base_color;
1717 text_color.red = text->red;
1718 text_color.green = text->green;
1719 text_color.blue = text->blue;
1721 base_color.red = base->red;
1722 base_color.green = base->green;
1723 base_color.blue = base->blue;
1725 if (*surface == NULL) {
1726 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1731 cr = cairo_create (*surface);
1732 cairo_scale (cr, rc->scale, rc->scale);
1733 cairo_surface_set_device_offset (*surface, 0, 0);
1734 memset (cairo_image_surface_get_data (*surface), 0x00,
1735 cairo_image_surface_get_height (*surface) *
1736 cairo_image_surface_get_stride (*surface));
1737 poppler_page_render_selection (poppler_page,
1739 (PopplerRectangle *)points,
1740 (PopplerRectangle *)old_points,
1741 (PopplerSelectionStyle)style,
1745 #else /* HAVE_POPPLER_PAGE_RENDER */
1748 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1752 poppler_page_render_selection_to_pixbuf (poppler_page,
1753 rc->scale, rc->rotation, pixbuf,
1754 (PopplerRectangle *)points,
1755 (PopplerRectangle *)old_points,
1756 (PopplerSelectionStyle)style,
1760 cairo_surface_destroy (*surface);
1761 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1762 g_object_unref (pixbuf);
1763 #endif /* HAVE_POPPLER_PAGE_RENDER */
1767 pdf_selection_get_selected_text (EvSelection *selection,
1768 EvRenderContext *rc,
1769 EvSelectionStyle style,
1770 EvRectangle *points)
1772 PopplerPage *poppler_page;
1777 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1779 poppler_page_get_size (poppler_page, NULL, &height);
1781 r.y1 = height - points->y2;
1783 r.y2 = height - points->y1;
1785 retval = poppler_page_get_text (poppler_page,
1786 (PopplerSelectionStyle)style,
1793 create_gdk_region_from_poppler_region (GList *region)
1798 retval = gdk_region_new ();
1800 for (l = region; l; l = g_list_next (l)) {
1801 PopplerRectangle *rectangle;
1804 rectangle = (PopplerRectangle *)l->data;
1806 rect.x = (gint) rectangle->x1;
1807 rect.y = (gint) rectangle->y1;
1808 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1809 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1810 gdk_region_union_with_rect (retval, &rect);
1812 poppler_rectangle_free (rectangle);
1819 pdf_selection_get_selection_region (EvSelection *selection,
1820 EvRenderContext *rc,
1821 EvSelectionStyle style,
1822 EvRectangle *points)
1824 PopplerPage *poppler_page;
1828 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1830 region = poppler_page_get_selection_region (poppler_page,
1832 (PopplerSelectionStyle)style,
1833 (PopplerRectangle *) points);
1834 retval = create_gdk_region_from_poppler_region (region);
1835 g_list_free (region);
1841 pdf_selection_get_selection_map (EvSelection *selection,
1842 EvRenderContext *rc)
1844 PopplerPage *poppler_page;
1845 PopplerRectangle points;
1849 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1853 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1855 region = poppler_page_get_selection_region (poppler_page, 1.0,
1856 POPPLER_SELECTION_GLYPH,
1858 retval = create_gdk_region_from_poppler_region (region);
1859 g_list_free (region);
1865 pdf_selection_iface_init (EvSelectionIface *iface)
1867 iface->render_selection = pdf_selection_render_selection;
1868 iface->get_selected_text = pdf_selection_get_selected_text;
1869 iface->get_selection_region = pdf_selection_get_selection_region;
1870 iface->get_selection_map = pdf_selection_get_selection_map;
1873 /* Page Transitions */
1875 pdf_document_get_page_duration (EvDocumentTransition *trans,
1878 PdfDocument *pdf_document;
1879 PopplerPage *poppler_page;
1880 gdouble duration = -1;
1882 pdf_document = PDF_DOCUMENT (trans);
1883 poppler_page = poppler_document_get_page (pdf_document->document, page);
1887 duration = poppler_page_get_duration (poppler_page);
1888 g_object_unref (poppler_page);
1893 static EvTransitionEffect *
1894 pdf_document_get_effect (EvDocumentTransition *trans,
1897 PdfDocument *pdf_document;
1898 PopplerPage *poppler_page;
1899 PopplerPageTransition *page_transition;
1900 EvTransitionEffect *effect;
1902 pdf_document = PDF_DOCUMENT (trans);
1903 poppler_page = poppler_document_get_page (pdf_document->document, page);
1908 page_transition = poppler_page_get_transition (poppler_page);
1910 if (!page_transition) {
1911 g_object_unref (poppler_page);
1915 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1916 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1917 "alignment", page_transition->alignment,
1918 "direction", page_transition->direction,
1919 "duration", page_transition->duration,
1920 "angle", page_transition->angle,
1921 "scale", page_transition->scale,
1922 "rectangular", page_transition->rectangular,
1925 poppler_page_transition_free (page_transition);
1926 g_object_unref (poppler_page);
1932 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1934 iface->get_page_duration = pdf_document_get_page_duration;
1935 iface->get_effect = pdf_document_get_effect;
1940 pdf_document_get_crop_box (EvDocument *document,
1944 PdfDocument *pdf_document;
1945 PopplerPage *poppler_page;
1946 PopplerRectangle poppler_rect;
1948 pdf_document = PDF_DOCUMENT (document);
1949 poppler_page = poppler_document_get_page (pdf_document->document, page);
1950 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1951 rect->x1 = poppler_rect.x1;
1952 rect->x2 = poppler_rect.x2;
1953 rect->y1 = poppler_rect.y1;
1954 rect->y2 = poppler_rect.y2;
1957 static EvFormField *
1958 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1960 EvFormField *ev_field = NULL;
1963 gboolean is_read_only;
1965 id = poppler_form_field_get_id (poppler_field);
1966 font_size = poppler_form_field_get_font_size (poppler_field);
1967 is_read_only = poppler_form_field_is_read_only (poppler_field);
1969 switch (poppler_form_field_get_field_type (poppler_field)) {
1970 case POPPLER_FORM_FIELD_TEXT: {
1971 EvFormFieldText *field_text;
1972 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1974 switch (poppler_form_field_text_get_text_type (poppler_field)) {
1975 case POPPLER_FORM_TEXT_NORMAL:
1976 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1978 case POPPLER_FORM_TEXT_MULTILINE:
1979 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
1981 case POPPLER_FORM_TEXT_FILE_SELECT:
1982 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
1986 ev_field = ev_form_field_text_new (id, ev_text_type);
1987 field_text = EV_FORM_FIELD_TEXT (ev_field);
1989 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
1990 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
1991 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
1992 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
1993 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
1994 field_text->text = poppler_form_field_text_get_text (poppler_field);
1998 case POPPLER_FORM_FIELD_BUTTON: {
1999 EvFormFieldButton *field_button;
2000 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2002 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2003 case POPPLER_FORM_BUTTON_PUSH:
2004 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2006 case POPPLER_FORM_BUTTON_CHECK:
2007 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2009 case POPPLER_FORM_BUTTON_RADIO:
2010 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2014 ev_field = ev_form_field_button_new (id, ev_button_type);
2015 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2017 field_button->state = poppler_form_field_button_get_state (poppler_field);
2020 case POPPLER_FORM_FIELD_CHOICE: {
2021 EvFormFieldChoice *field_choice;
2022 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2024 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2025 case POPPLER_FORM_CHOICE_COMBO:
2026 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2028 case EV_FORM_FIELD_CHOICE_LIST:
2029 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2033 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2034 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2036 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2037 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2038 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2039 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2041 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2042 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2043 if (field_choice->is_editable)
2044 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2047 case POPPLER_FORM_FIELD_SIGNATURE:
2049 ev_field = ev_form_field_signature_new (id);
2051 case POPPLER_FORM_FIELD_UNKNOWN:
2055 ev_field->font_size = font_size;
2056 ev_field->is_read_only = is_read_only;
2062 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2065 PopplerPage *poppler_page;
2066 GList *retval = NULL;
2071 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2073 poppler_page = POPPLER_PAGE (page->backend_page);
2074 fields = poppler_page_get_form_field_mapping (poppler_page);
2075 poppler_page_get_size (poppler_page, NULL, &height);
2077 for (list = fields; list; list = list->next) {
2078 PopplerFormFieldMapping *mapping;
2079 EvFormFieldMapping *field_mapping;
2080 EvFormField *ev_field;
2082 mapping = (PopplerFormFieldMapping *)list->data;
2084 ev_field = ev_form_field_from_poppler_field (mapping->field);
2088 field_mapping = g_new0 (EvFormFieldMapping, 1);
2089 field_mapping->x1 = mapping->area.x1;
2090 field_mapping->x2 = mapping->area.x2;
2091 field_mapping->y1 = height - mapping->area.y2;
2092 field_mapping->y2 = height - mapping->area.y1;
2093 field_mapping->field = ev_field;
2094 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2096 g_object_set_data_full (G_OBJECT (ev_field),
2098 g_object_ref (mapping->field),
2099 (GDestroyNotify) g_object_unref);
2101 retval = g_list_prepend (retval, field_mapping);
2104 poppler_page_free_form_field_mapping (fields);
2106 return g_list_reverse (retval);
2110 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2114 PopplerFormField *poppler_field;
2117 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2121 text = poppler_form_field_text_get_text (poppler_field);
2127 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2131 PopplerFormField *poppler_field;
2133 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2136 poppler_form_field_text_set_text (poppler_field, text);
2140 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2144 PopplerFormField *poppler_field;
2146 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2150 poppler_form_field_button_set_state (poppler_field, state);
2154 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2157 PopplerFormField *poppler_field;
2160 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2164 state = poppler_form_field_button_get_state (poppler_field);
2170 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2174 PopplerFormField *poppler_field;
2177 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2181 text = poppler_form_field_choice_get_item (poppler_field, index);
2187 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2190 PopplerFormField *poppler_field;
2193 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2197 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2203 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2207 PopplerFormField *poppler_field;
2210 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2214 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2220 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2224 PopplerFormField *poppler_field;
2226 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2230 poppler_form_field_choice_select_item (poppler_field, index);
2234 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2238 PopplerFormField *poppler_field;
2240 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2244 poppler_form_field_choice_toggle_item (poppler_field, index);
2248 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2251 PopplerFormField *poppler_field;
2253 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2257 poppler_form_field_choice_unselect_all (poppler_field);
2261 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2265 PopplerFormField *poppler_field;
2267 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2271 poppler_form_field_choice_set_text (poppler_field, text);
2275 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2278 PopplerFormField *poppler_field;
2281 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2285 text = poppler_form_field_choice_get_text (poppler_field);
2291 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2293 iface->get_form_fields = pdf_document_forms_get_form_fields;
2294 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2295 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2296 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2297 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2298 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2299 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2300 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2301 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2302 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2303 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2304 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2305 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;