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.
22 #ifdef HAVE_POPPLER_FORM_FIELD_BUTTON_GET_BUTTON_TYPE
30 #include <poppler-document.h>
31 #include <poppler-page.h>
33 #include <cairo-pdf.h>
38 #include <glib/gi18n.h>
40 #include "ev-poppler.h"
41 #include "ev-file-exporter.h"
42 #include "ev-document-find.h"
43 #include "ev-document-misc.h"
44 #include "ev-document-links.h"
45 #include "ev-document-images.h"
46 #include "ev-document-fonts.h"
47 #include "ev-document-security.h"
48 #include "ev-document-thumbnails.h"
49 #include "ev-document-transition.h"
50 #include "ev-document-forms.h"
51 #include "ev-selection.h"
52 #include "ev-attachment.h"
55 #if defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS)
56 #define HAVE_CAIRO_PRINT
60 PdfDocument *document;
69 EvFileExporterFormat format;
79 #ifdef HAVE_CAIRO_PRINT
82 PopplerPSFile *ps_file;
86 struct _PdfDocumentClass
88 GObjectClass parent_class;
93 GObject parent_instance;
95 PopplerDocument *document;
98 PopplerFontInfo *font_info;
99 PopplerFontsIter *fonts_iter;
100 int fonts_scanned_pages;
102 PdfDocumentSearch *search;
103 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_find_iface_init (EvDocumentFindIface *iface);
114 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
115 static void pdf_selection_iface_init (EvSelectionIface *iface);
116 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
117 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
121 static int pdf_document_get_n_pages (EvDocument *document);
123 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
125 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
126 PopplerAction *action);
127 static void pdf_document_search_free (PdfDocumentSearch *search);
128 static void pdf_print_context_free (PdfPrintContext *ctx);
131 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
133 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
134 pdf_document_document_iface_init);
135 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
136 pdf_document_security_iface_init);
137 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
138 pdf_document_document_thumbnails_iface_init);
139 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
140 pdf_document_document_links_iface_init);
141 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
142 pdf_document_document_images_iface_init);
143 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
144 pdf_document_document_forms_iface_init);
145 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
146 pdf_document_document_fonts_iface_init);
147 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
148 pdf_document_find_iface_init);
149 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
150 pdf_document_file_exporter_iface_init);
151 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
152 pdf_selection_iface_init);
153 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
154 pdf_document_page_transition_iface_init);
156 #else /* !HAVE_FORMS */
157 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
159 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
160 pdf_document_document_iface_init);
161 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
162 pdf_document_security_iface_init);
163 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
164 pdf_document_document_thumbnails_iface_init);
165 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
166 pdf_document_document_links_iface_init);
167 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
168 pdf_document_document_images_iface_init);
169 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
170 pdf_document_document_fonts_iface_init);
171 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
172 pdf_document_find_iface_init);
173 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
174 pdf_document_file_exporter_iface_init);
175 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
176 pdf_selection_iface_init);
177 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
178 pdf_document_page_transition_iface_init);
180 #endif /* HAVE_FORMS */
183 set_rc_data (PdfDocument *pdf_document,
186 if (rc->data == NULL) {
187 rc->data = poppler_document_get_page (pdf_document->document,
189 rc->destroy = g_object_unref;
191 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
196 pdf_document_search_free (PdfDocumentSearch *search)
198 PdfDocument *pdf_document = search->document;
202 if (search->idle != 0)
203 g_source_remove (search->idle);
205 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
206 for (i = 0; i < n_pages; i++) {
207 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
208 g_list_free (search->pages[i]);
210 g_free (search->pages);
212 g_free (search->text);
217 pdf_document_dispose (GObject *object)
219 PdfDocument *pdf_document = PDF_DOCUMENT(object);
221 if (pdf_document->print_ctx) {
222 pdf_print_context_free (pdf_document->print_ctx);
223 pdf_document->print_ctx = NULL;
226 if (pdf_document->search) {
227 pdf_document_search_free (pdf_document->search);
228 pdf_document->search = NULL;
231 if (pdf_document->document) {
232 g_object_unref (pdf_document->document);
235 if (pdf_document->font_info) {
236 poppler_font_info_free (pdf_document->font_info);
239 if (pdf_document->fonts_iter) {
240 poppler_fonts_iter_free (pdf_document->fonts_iter);
243 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
247 pdf_document_class_init (PdfDocumentClass *klass)
249 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
251 g_object_class->dispose = pdf_document_dispose;
255 pdf_document_init (PdfDocument *pdf_document)
257 pdf_document->password = NULL;
261 convert_error (GError *poppler_error,
264 if (poppler_error == NULL)
267 if (poppler_error->domain == POPPLER_ERROR) {
268 /* convert poppler errors into EvDocument errors */
269 gint code = EV_DOCUMENT_ERROR_INVALID;
270 if (poppler_error->code == POPPLER_ERROR_INVALID)
271 code = EV_DOCUMENT_ERROR_INVALID;
272 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
273 code = EV_DOCUMENT_ERROR_ENCRYPTED;
279 poppler_error->message,
282 g_propagate_error (error, poppler_error);
289 pdf_document_save (EvDocument *document,
294 GError *poppler_error = NULL;
296 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
300 convert_error (poppler_error, error);
306 pdf_document_load (EvDocument *document,
310 GError *poppler_error = NULL;
311 PdfDocument *pdf_document = PDF_DOCUMENT (document);
313 pdf_document->document =
314 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
316 if (pdf_document->document == NULL) {
317 convert_error (poppler_error, error);
325 pdf_document_get_n_pages (EvDocument *document)
327 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
331 pdf_document_get_page_size (EvDocument *document,
336 PdfDocument *pdf_document = PDF_DOCUMENT (document);
337 PopplerPage *poppler_page;
339 poppler_page = poppler_document_get_page (pdf_document->document, page);
340 poppler_page_get_size (poppler_page, width, height);
341 g_object_unref (poppler_page);
345 pdf_document_get_page_label (EvDocument *document,
348 PopplerPage *poppler_page;
351 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
354 g_object_get (G_OBJECT (poppler_page),
357 g_object_unref (poppler_page);
363 pdf_document_has_attachments (EvDocument *document)
365 PdfDocument *pdf_document;
367 pdf_document = PDF_DOCUMENT (document);
369 return poppler_document_has_attachments (pdf_document->document);
372 struct SaveToBufferData {
378 attachment_save_to_buffer_callback (const gchar *buf,
383 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
387 if (sdata->len + count > sdata->max) {
388 new_max = MAX (sdata->max * 2, sdata->len + count);
389 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
391 sdata->buffer = new_buffer;
392 sdata->max = new_max;
395 memcpy (sdata->buffer + sdata->len, buf, count);
402 attachment_save_to_buffer (PopplerAttachment *attachment,
407 static const gint initial_max = 1024;
408 struct SaveToBufferData sdata;
413 sdata.buffer = (gchar *) g_malloc (initial_max);
414 sdata.max = initial_max;
417 if (! poppler_attachment_save_to_callback (attachment,
418 attachment_save_to_buffer_callback,
421 g_free (sdata.buffer);
425 *buffer = sdata.buffer;
426 *buffer_size = sdata.len;
432 pdf_document_get_attachments (EvDocument *document)
434 PdfDocument *pdf_document;
437 GList *retval = NULL;
439 pdf_document = PDF_DOCUMENT (document);
441 if (!pdf_document_has_attachments (document))
444 attachments = poppler_document_get_attachments (pdf_document->document);
446 for (list = attachments; list; list = list->next) {
447 PopplerAttachment *attachment;
448 EvAttachment *ev_attachment;
451 GError *error = NULL;
453 attachment = (PopplerAttachment *) list->data;
455 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
456 ev_attachment = ev_attachment_new (attachment->name,
457 attachment->description,
462 retval = g_list_prepend (retval, ev_attachment);
465 g_warning ("%s", error->message);
466 g_error_free (error);
472 g_object_unref (attachment);
475 return g_list_reverse (retval);
478 static cairo_surface_t *
479 pdf_document_render (EvDocument *document,
482 PdfDocument *pdf_document;
483 cairo_surface_t *surface;
484 double width_points, height_points;
487 pdf_document = PDF_DOCUMENT (document);
489 set_rc_data (pdf_document, rc);
491 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
493 if (rc->rotation == 90 || rc->rotation == 270) {
494 width = (int) ((height_points * rc->scale) + 0.5);
495 height = (int) ((width_points * rc->scale) + 0.5);
497 width = (int) ((width_points * rc->scale) + 0.5);
498 height = (int) ((height_points * rc->scale) + 0.5);
501 #ifdef HAVE_POPPLER_PAGE_RENDER
504 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
506 memset (cairo_image_surface_get_data (surface), 0xff,
507 cairo_image_surface_get_height (surface) *
508 cairo_image_surface_get_stride (surface));
510 cr = cairo_create (surface);
511 switch (rc->rotation) {
513 cairo_translate (cr, width, 0);
516 cairo_translate (cr, width, height);
519 cairo_translate (cr, 0, height);
522 cairo_translate (cr, 0, 0);
524 cairo_scale (cr, rc->scale, rc->scale);
525 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
526 poppler_page_render (POPPLER_PAGE (rc->data), cr);
528 #else /* HAVE_POPPLER_PAGE_RENDER */
531 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
535 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
541 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
542 g_object_unref (pixbuf);
543 #endif /* HAVE_POPPLER_PAGE_RENDER */
548 /* EvDocumentSecurity */
551 pdf_document_has_document_security (EvDocumentSecurity *document_security)
553 /* FIXME: do we really need to have this? */
558 pdf_document_set_password (EvDocumentSecurity *document_security,
559 const char *password)
561 PdfDocument *document = PDF_DOCUMENT (document_security);
563 if (document->password)
564 g_free (document->password);
566 document->password = g_strdup (password);
569 static EvDocumentInfo *
570 pdf_document_get_info (EvDocument *document)
572 EvDocumentInfo *info;
573 PopplerPageLayout layout;
574 PopplerPageMode mode;
575 PopplerViewerPreferences view_prefs;
576 PopplerPermissions permissions;
578 info = g_new0 (EvDocumentInfo, 1);
580 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
581 EV_DOCUMENT_INFO_FORMAT |
582 EV_DOCUMENT_INFO_AUTHOR |
583 EV_DOCUMENT_INFO_SUBJECT |
584 EV_DOCUMENT_INFO_KEYWORDS |
585 EV_DOCUMENT_INFO_LAYOUT |
586 EV_DOCUMENT_INFO_START_MODE |
587 EV_DOCUMENT_INFO_PERMISSIONS |
588 EV_DOCUMENT_INFO_UI_HINTS |
589 EV_DOCUMENT_INFO_CREATOR |
590 EV_DOCUMENT_INFO_PRODUCER |
591 EV_DOCUMENT_INFO_CREATION_DATE |
592 EV_DOCUMENT_INFO_MOD_DATE |
593 EV_DOCUMENT_INFO_LINEARIZED |
594 EV_DOCUMENT_INFO_N_PAGES |
595 EV_DOCUMENT_INFO_SECURITY |
596 EV_DOCUMENT_INFO_PAPER_SIZE;
598 g_object_get (PDF_DOCUMENT (document)->document,
599 "title", &(info->title),
600 "format", &(info->format),
601 "author", &(info->author),
602 "subject", &(info->subject),
603 "keywords", &(info->keywords),
605 "page-layout", &layout,
606 "viewer-preferences", &view_prefs,
607 "permissions", &permissions,
608 "creator", &(info->creator),
609 "producer", &(info->producer),
610 "creation-date", &(info->creation_date),
611 "mod-date", &(info->modified_date),
612 "linearized", &(info->linearized),
615 pdf_document_get_page_size(document, 0,
616 &(info->paper_width),
617 &(info->paper_height));
620 info->paper_width = info->paper_width / 72.0f * 25.4f;
621 info->paper_height = info->paper_height / 72.0f * 25.4f;
624 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
625 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
627 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
628 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
630 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
631 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
633 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
634 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
635 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
636 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
638 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
639 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
646 case POPPLER_PAGE_MODE_NONE:
647 info->mode = EV_DOCUMENT_MODE_NONE;
649 case POPPLER_PAGE_MODE_USE_THUMBS:
650 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
652 case POPPLER_PAGE_MODE_USE_OC:
653 info->mode = EV_DOCUMENT_MODE_USE_OC;
655 case POPPLER_PAGE_MODE_FULL_SCREEN:
656 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
658 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
659 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
665 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
666 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
668 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
669 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
671 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
672 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
674 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
675 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
677 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
678 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
680 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
681 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
683 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
684 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
687 info->permissions = 0;
688 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
689 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
691 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
692 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
694 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
695 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
697 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
698 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
701 info->n_pages = ev_document_get_n_pages (document);
703 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
704 /* translators: this is the document security state */
705 info->security = g_strdup (_("Yes"));
707 /* translators: this is the document security state */
708 info->security = g_strdup (_("No"));
715 pdf_document_document_iface_init (EvDocumentIface *iface)
717 iface->save = pdf_document_save;
718 iface->load = pdf_document_load;
719 iface->get_n_pages = pdf_document_get_n_pages;
720 iface->get_page_size = pdf_document_get_page_size;
721 iface->get_page_label = pdf_document_get_page_label;
722 iface->has_attachments = pdf_document_has_attachments;
723 iface->get_attachments = pdf_document_get_attachments;
724 iface->render = pdf_document_render;
725 iface->get_info = pdf_document_get_info;
729 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
731 iface->has_document_security = pdf_document_has_document_security;
732 iface->set_password = pdf_document_set_password;
736 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
738 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
741 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
743 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
747 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
750 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
753 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
755 if (pdf_document->font_info == NULL) {
756 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
759 if (pdf_document->fonts_iter) {
760 poppler_fonts_iter_free (pdf_document->fonts_iter);
763 pdf_document->fonts_scanned_pages += n_pages;
765 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
766 &pdf_document->fonts_iter);
768 pdf_document->fonts_scanned_pages = 0;
769 poppler_font_info_free (pdf_document->font_info);
770 pdf_document->font_info = NULL;
777 font_type_to_string (PopplerFontType type)
780 case POPPLER_FONT_TYPE_TYPE1:
782 case POPPLER_FONT_TYPE_TYPE1C:
784 case POPPLER_FONT_TYPE_TYPE3:
786 case POPPLER_FONT_TYPE_TRUETYPE:
787 return _("TrueType");
788 case POPPLER_FONT_TYPE_CID_TYPE0:
789 return _("Type 1 (CID)");
790 case POPPLER_FONT_TYPE_CID_TYPE0C:
791 return _("Type 1C (CID)");
792 case POPPLER_FONT_TYPE_CID_TYPE2:
793 return _("TrueType (CID)");
795 return _("Unknown font type");
800 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
803 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
804 PopplerFontsIter *iter = pdf_document->fonts_iter;
806 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
812 GtkTreeIter list_iter;
815 const char *embedded;
818 name = poppler_fonts_iter_get_name (iter);
824 type = font_type_to_string (
825 poppler_fonts_iter_get_font_type (iter));
827 if (poppler_fonts_iter_is_embedded (iter)) {
828 if (poppler_fonts_iter_is_subset (iter))
829 embedded = _("Embedded subset");
831 embedded = _("Embedded");
833 embedded = _("Not embedded");
836 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
838 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
839 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
840 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
841 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
845 } while (poppler_fonts_iter_next (iter));
849 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
851 iface->fill_model = pdf_document_fonts_fill_model;
852 iface->scan = pdf_document_fonts_scan;
853 iface->get_progress = pdf_document_fonts_get_progress;
857 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
859 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
860 PopplerIndexIter *iter;
862 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
864 iter = poppler_index_iter_new (pdf_document->document);
867 poppler_index_iter_free (iter);
873 ev_link_dest_from_dest (PdfDocument *pdf_document,
876 EvLinkDest *ev_dest = NULL;
877 const char *unimplemented_dest = NULL;
879 g_assert (dest != NULL);
881 switch (dest->type) {
882 case POPPLER_DEST_XYZ: {
883 PopplerPage *poppler_page;
886 poppler_page = poppler_document_get_page (pdf_document->document,
887 MAX (0, dest->page_num - 1));
888 poppler_page_get_size (poppler_page, NULL, &height);
889 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
893 g_object_unref (poppler_page);
896 case POPPLER_DEST_FIT:
897 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
899 case POPPLER_DEST_FITH: {
900 PopplerPage *poppler_page;
903 poppler_page = poppler_document_get_page (pdf_document->document,
904 MAX (0, dest->page_num - 1));
905 poppler_page_get_size (poppler_page, NULL, &height);
906 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
908 g_object_unref (poppler_page);
911 case POPPLER_DEST_FITV:
912 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
915 case POPPLER_DEST_FITR: {
916 PopplerPage *poppler_page;
919 poppler_page = poppler_document_get_page (pdf_document->document,
920 MAX (0, dest->page_num - 1));
921 poppler_page_get_size (poppler_page, NULL, &height);
922 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
924 height - dest->bottom,
927 g_object_unref (poppler_page);
930 case POPPLER_DEST_FITB:
931 unimplemented_dest = "POPPLER_DEST_FITB";
933 case POPPLER_DEST_FITBH:
934 unimplemented_dest = "POPPLER_DEST_FITBH";
936 case POPPLER_DEST_FITBV:
937 unimplemented_dest = "POPPLER_DEST_FITBV";
939 case POPPLER_DEST_NAMED:
940 ev_dest = ev_link_dest_new_named (dest->named_dest);
942 case POPPLER_DEST_UNKNOWN:
943 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
947 if (unimplemented_dest) {
948 g_warning ("Unimplemented named action: %s, please post a "
949 "bug report in Evince bugzilla "
950 "(http://bugzilla.gnome.org) with a testcase.",
955 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
961 ev_link_from_action (PdfDocument *pdf_document,
962 PopplerAction *action)
965 EvLinkAction *ev_action = NULL;
966 const char *unimplemented_action = NULL;
968 switch (action->type) {
969 case POPPLER_ACTION_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 with a testcase.",
1004 unimplemented_action);
1007 link = ev_link_new (action->any.title, ev_action);
1013 build_tree (PdfDocument *pdf_document,
1014 GtkTreeModel *model,
1015 GtkTreeIter *parent,
1016 PopplerIndexIter *iter)
1020 GtkTreeIter tree_iter;
1021 PopplerIndexIter *child;
1022 PopplerAction *action;
1023 EvLink *link = NULL;
1027 action = poppler_index_iter_get_action (iter);
1028 expand = poppler_index_iter_is_open (iter);
1033 switch (action->type) {
1034 case POPPLER_ACTION_GOTO_DEST: {
1035 /* For bookmarks, solve named destinations */
1036 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1038 EvLinkDest *ev_dest = NULL;
1039 EvLinkAction *ev_action;
1041 dest = poppler_document_find_dest (pdf_document->document,
1042 action->goto_dest.dest->named_dest);
1044 link = ev_link_from_action (pdf_document, action);
1048 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1049 poppler_dest_free (dest);
1051 ev_action = ev_link_action_new_dest (ev_dest);
1052 link = ev_link_new (action->any.title, ev_action);
1054 link = ev_link_from_action (pdf_document, action);
1059 link = ev_link_from_action (pdf_document, action);
1064 poppler_action_free (action);
1068 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1069 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1071 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1072 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1073 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1074 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1077 g_free (title_markup);
1078 g_object_unref (link);
1080 child = poppler_index_iter_get_child (iter);
1082 build_tree (pdf_document, model, &tree_iter, child);
1083 poppler_index_iter_free (child);
1084 poppler_action_free (action);
1086 } while (poppler_index_iter_next (iter));
1089 static GtkTreeModel *
1090 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1092 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1093 GtkTreeModel *model = NULL;
1094 PopplerIndexIter *iter;
1096 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1098 iter = poppler_index_iter_new (pdf_document->document);
1099 /* Create the model if we have items*/
1101 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1106 build_tree (pdf_document, model, NULL, iter);
1107 poppler_index_iter_free (iter);
1114 pdf_document_links_get_links (EvDocumentLinks *document_links,
1117 PdfDocument *pdf_document;
1118 PopplerPage *poppler_page;
1119 GList *retval = NULL;
1120 GList *mapping_list;
1124 pdf_document = PDF_DOCUMENT (document_links);
1125 poppler_page = poppler_document_get_page (pdf_document->document,
1127 mapping_list = poppler_page_get_link_mapping (poppler_page);
1128 poppler_page_get_size (poppler_page, NULL, &height);
1130 for (list = mapping_list; list; list = list->next) {
1131 PopplerLinkMapping *link_mapping;
1132 EvLinkMapping *ev_link_mapping;
1134 link_mapping = (PopplerLinkMapping *)list->data;
1135 ev_link_mapping = g_new (EvLinkMapping, 1);
1136 ev_link_mapping->link = ev_link_from_action (pdf_document,
1137 link_mapping->action);
1138 ev_link_mapping->x1 = link_mapping->area.x1;
1139 ev_link_mapping->x2 = link_mapping->area.x2;
1140 /* Invert this for X-style coordinates */
1141 ev_link_mapping->y1 = height - link_mapping->area.y2;
1142 ev_link_mapping->y2 = height - link_mapping->area.y1;
1144 retval = g_list_prepend (retval, ev_link_mapping);
1147 poppler_page_free_link_mapping (mapping_list);
1148 g_object_unref (poppler_page);
1150 return g_list_reverse (retval);
1154 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1155 const gchar *link_name)
1157 PdfDocument *pdf_document;
1159 EvLinkDest *ev_dest = NULL;
1161 pdf_document = PDF_DOCUMENT (document_links);
1162 dest = poppler_document_find_dest (pdf_document->document,
1165 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1166 poppler_dest_free (dest);
1173 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1175 iface->has_document_links = pdf_document_links_has_document_links;
1176 iface->get_links_model = pdf_document_links_get_links_model;
1177 iface->get_links = pdf_document_links_get_links;
1178 iface->find_link_dest = pdf_document_links_find_link_dest;
1182 pdf_document_images_get_images (EvDocumentImages *document_images,
1185 GList *retval = NULL;
1186 PdfDocument *pdf_document;
1187 PopplerPage *poppler_page;
1188 GList *mapping_list;
1191 pdf_document = PDF_DOCUMENT (document_images);
1192 poppler_page = poppler_document_get_page (pdf_document->document, page);
1193 mapping_list = poppler_page_get_image_mapping (poppler_page);
1195 for (list = mapping_list; list; list = list->next) {
1196 PopplerImageMapping *image_mapping;
1197 EvImageMapping *ev_image_mapping;
1199 image_mapping = (PopplerImageMapping *)list->data;
1201 ev_image_mapping = g_new (EvImageMapping, 1);
1203 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1204 ev_image_mapping->x1 = image_mapping->area.x1;
1205 ev_image_mapping->x2 = image_mapping->area.x2;
1206 ev_image_mapping->y1 = image_mapping->area.y1;
1207 ev_image_mapping->y2 = image_mapping->area.y2;
1209 retval = g_list_prepend (retval, ev_image_mapping);
1212 poppler_page_free_image_mapping (mapping_list);
1213 g_object_unref (poppler_page);
1219 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1221 iface->get_images = pdf_document_images_get_images;
1225 make_thumbnail_for_page (PdfDocument *pdf_document,
1226 PopplerPage *poppler_page,
1227 EvRenderContext *rc)
1232 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1233 rc, &width, &height);
1235 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1237 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1239 ev_document_fc_mutex_lock ();
1240 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1242 rc->scale, rc->rotation, pixbuf);
1243 ev_document_fc_mutex_unlock ();
1249 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1250 EvRenderContext *rc,
1253 PdfDocument *pdf_document;
1254 PopplerPage *poppler_page;
1256 GdkPixbuf *border_pixbuf;
1258 pdf_document = PDF_DOCUMENT (document_thumbnails);
1260 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1261 g_return_val_if_fail (poppler_page != NULL, NULL);
1263 pixbuf = poppler_page_get_thumbnail (poppler_page);
1265 /* Rotate provided thumbnail if needed */
1266 GdkPixbuf *rotated_pixbuf;
1268 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1269 (GdkPixbufRotation) (360 - rc->rotation));
1270 g_object_unref (pixbuf);
1271 pixbuf = rotated_pixbuf;
1273 /* There is no provided thumbnail. We need to make one. */
1274 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1278 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1279 g_object_unref (pixbuf);
1280 pixbuf = border_pixbuf;
1283 g_object_unref (poppler_page);
1289 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1290 EvRenderContext *rc,
1294 PdfDocument *pdf_document;
1295 PopplerPage *poppler_page;
1298 pdf_document = PDF_DOCUMENT (document_thumbnails);
1299 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1301 g_return_if_fail (poppler_page != NULL);
1303 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1306 double page_width, page_height;
1308 poppler_page_get_size (poppler_page, &page_width, &page_height);
1310 *width = (gint) MAX (page_width * rc->scale, 1);
1311 *height = (gint) MAX (page_height * rc->scale, 1);
1314 if (rc->rotation == 90 || rc->rotation == 270) {
1322 g_object_unref (poppler_page);
1326 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1328 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1329 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1334 pdf_document_search_idle_callback (void *data)
1336 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1337 PdfDocument *pdf_document = search->document;
1342 page = poppler_document_get_page (search->document->document,
1343 search->search_page);
1345 ev_document_doc_mutex_lock ();
1346 matches = poppler_page_find_text (page, search->text);
1347 ev_document_doc_mutex_unlock ();
1349 g_object_unref (page);
1351 search->pages[search->search_page] = matches;
1352 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1353 search->search_page);
1355 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1356 search->search_page += 1;
1357 if (search->search_page == n_pages) {
1359 search->search_page = 0;
1362 if (search->search_page != search->start_page) {
1367 search->idle = 0; /* will return FALSE to remove */
1372 static PdfDocumentSearch *
1373 pdf_document_search_new (PdfDocument *pdf_document,
1377 PdfDocumentSearch *search;
1381 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1383 search = g_new0 (PdfDocumentSearch, 1);
1385 search->text = g_strdup (text);
1386 search->pages = g_new0 (GList *, n_pages);
1387 search->document = pdf_document;
1389 /* We add at low priority so the progress bar repaints */
1390 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1391 pdf_document_search_idle_callback,
1395 search->start_page = start_page;
1396 search->search_page = start_page;
1402 pdf_document_find_begin (EvDocumentFind *document,
1404 const char *search_string,
1405 gboolean case_sensitive)
1407 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1409 /* FIXME handle case_sensitive (right now XPDF
1410 * code is always case insensitive for ASCII
1411 * and case sensitive for all other languaages)
1414 if (pdf_document->search &&
1415 strcmp (search_string, pdf_document->search->text) == 0)
1418 if (pdf_document->search)
1419 pdf_document_search_free (pdf_document->search);
1421 pdf_document->search = pdf_document_search_new (pdf_document,
1427 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1429 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1432 return g_list_length (search->pages[page]);
1439 pdf_document_find_get_result (EvDocumentFind *document_find,
1442 EvRectangle *rectangle)
1444 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1445 PdfDocumentSearch *search = pdf_document->search;
1446 PopplerPage *poppler_page;
1447 PopplerRectangle *r;
1453 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1458 poppler_page = poppler_document_get_page (pdf_document->document, page);
1459 poppler_page_get_size (poppler_page, NULL, &height);
1460 rectangle->x1 = r->x1;
1461 rectangle->y1 = height - r->y2;
1462 rectangle->x2 = r->x2;
1463 rectangle->y2 = height - r->y1;
1464 g_object_unref (poppler_page);
1470 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1473 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1475 return search && search->pages[page] != NULL;
1479 pdf_document_find_get_progress (EvDocumentFind *document_find)
1481 PdfDocumentSearch *search;
1482 int n_pages, pages_done;
1484 search = PDF_DOCUMENT (document_find)->search;
1486 if (search == NULL) {
1490 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1491 if (search->search_page > search->start_page) {
1492 pages_done = search->search_page - search->start_page + 1;
1493 } else if (search->search_page == search->start_page) {
1494 pages_done = n_pages;
1496 pages_done = n_pages - search->start_page + search->search_page;
1499 return pages_done / (double) n_pages;
1503 pdf_document_find_cancel (EvDocumentFind *document)
1505 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1507 if (pdf_document->search) {
1508 pdf_document_search_free (pdf_document->search);
1509 pdf_document->search = NULL;
1514 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1516 iface->begin = pdf_document_find_begin;
1517 iface->get_n_results = pdf_document_find_get_n_results;
1518 iface->get_result = pdf_document_find_get_result;
1519 iface->page_has_results = pdf_document_find_page_has_results;
1520 iface->get_progress = pdf_document_find_get_progress;
1521 iface->cancel = pdf_document_find_cancel;
1525 pdf_print_context_free (PdfPrintContext *ctx)
1530 #ifdef HAVE_CAIRO_PRINT
1532 cairo_destroy (ctx->cr);
1537 poppler_ps_file_free (ctx->ps_file);
1538 ctx->ps_file = NULL;
1545 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1546 EvFileExporterContext *fc)
1548 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1549 PdfPrintContext *ctx;
1550 gdouble width, height;
1551 #ifdef HAVE_CAIRO_PRINT
1552 cairo_surface_t *surface = NULL;
1555 if (pdf_document->print_ctx)
1556 pdf_print_context_free (pdf_document->print_ctx);
1557 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1558 ctx = pdf_document->print_ctx;
1559 ctx->format = fc->format;
1560 ctx->pages_per_sheet = fc->pages_per_sheet;
1562 ctx->paper_width = fc->paper_width;
1563 ctx->paper_height = fc->paper_height;
1565 switch (fc->pages_per_sheet) {
1593 ctx->pages_printed = 0;
1595 switch (fc->format) {
1596 case EV_FILE_FORMAT_PS:
1597 #ifdef HAVE_CAIRO_PS
1598 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1600 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1601 fc->filename, fc->first_page,
1602 fc->last_page - fc->first_page + 1);
1603 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1604 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1605 #endif /* HAVE_CAIRO_PS */
1607 case EV_FILE_FORMAT_PDF:
1608 #ifdef HAVE_CAIRO_PDF
1609 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1613 g_assert_not_reached ();
1616 #ifdef HAVE_CAIRO_PRINT
1617 ctx->cr = cairo_create (surface);
1618 cairo_surface_destroy (surface);
1623 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1624 EvRenderContext *rc)
1626 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1627 PdfPrintContext *ctx = pdf_document->print_ctx;
1628 PopplerPage *poppler_page;
1629 #ifdef HAVE_CAIRO_PRINT
1630 gdouble page_width, page_height;
1632 gboolean rotate, landscape;
1633 gdouble width, height;
1634 gdouble pwidth, pheight;
1635 gdouble xscale, yscale;
1638 g_return_if_fail (pdf_document->print_ctx != NULL);
1640 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1642 #ifdef HAVE_CAIRO_PRINT
1643 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1644 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1645 poppler_page_get_size (poppler_page, &page_width, &page_height);
1647 if (page_width > page_height && page_width > ctx->paper_width) {
1653 landscape = (ctx->paper_width > ctx->paper_height);
1655 /* Use always portrait mode and rotate when necessary */
1656 if (ctx->paper_width > ctx->paper_height) {
1657 width = ctx->paper_height;
1658 height = ctx->paper_width;
1661 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1664 width = ctx->paper_width;
1665 height = ctx->paper_height;
1668 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1681 page_width = page_height;
1686 pwidth = width / ctx->pages_x;
1687 pheight = height / ctx->pages_y;
1689 if ((page_width > pwidth || page_height > pheight) ||
1690 (page_width < pwidth && page_height < pheight)) {
1691 xscale = pwidth / page_width;
1692 yscale = pheight / page_height;
1694 if (yscale < xscale) {
1701 xscale = yscale = 1;
1706 cairo_save (ctx->cr);
1708 cairo_matrix_t matrix;
1710 cairo_translate (ctx->cr, width, 0);
1711 cairo_matrix_init (&matrix,
1715 cairo_transform (ctx->cr, &matrix);
1718 cairo_translate (ctx->cr,
1719 x * (rotate ? pheight : pwidth),
1720 y * (rotate ? pwidth : pheight));
1721 cairo_scale (ctx->cr, xscale, yscale);
1723 #ifdef HAVE_POPPLER_PAGE_RENDER
1724 poppler_page_render (poppler_page, ctx->cr);
1726 ctx->pages_printed++;
1728 if (ctx->pages_printed % ctx->pages_per_sheet == 0) {
1729 cairo_show_page (ctx->cr);
1731 cairo_restore (ctx->cr);
1732 #else /* HAVE_CAIRO_PRINT */
1733 if (ctx->format == EV_FILE_FORMAT_PS)
1734 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1735 #endif /* HAVE_CAIRO_PRINT */
1737 g_object_unref (poppler_page);
1741 pdf_document_file_exporter_end (EvFileExporter *exporter)
1743 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1745 pdf_print_context_free (pdf_document->print_ctx);
1746 pdf_document->print_ctx = NULL;
1749 static EvFileExporterCapabilities
1750 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1752 return (EvFileExporterCapabilities) (
1753 EV_FILE_EXPORTER_CAN_PAGE_SET |
1754 EV_FILE_EXPORTER_CAN_COPIES |
1755 EV_FILE_EXPORTER_CAN_COLLATE |
1756 EV_FILE_EXPORTER_CAN_REVERSE |
1757 EV_FILE_EXPORTER_CAN_SCALE |
1758 #ifdef HAVE_CAIRO_PRINT
1759 #ifdef HAVE_POPPLER_PAGE_RENDER
1760 #if GTK_CHECK_VERSION (2, 11, 1)
1761 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1766 #ifdef HAVE_CAIRO_PDF
1767 #ifdef HAVE_POPPLER_PAGE_RENDER
1768 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1771 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1775 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1777 iface->begin = pdf_document_file_exporter_begin;
1778 iface->do_page = pdf_document_file_exporter_do_page;
1779 iface->end = pdf_document_file_exporter_end;
1780 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1784 pdf_selection_render_selection (EvSelection *selection,
1785 EvRenderContext *rc,
1786 cairo_surface_t **surface,
1787 EvRectangle *points,
1788 EvRectangle *old_points,
1789 EvSelectionStyle style,
1793 PdfDocument *pdf_document;
1794 double width_points, height_points;
1797 pdf_document = PDF_DOCUMENT (selection);
1798 set_rc_data (pdf_document, rc);
1800 poppler_page_get_size (POPPLER_PAGE (rc->data),
1801 &width_points, &height_points);
1802 width = (int) ((width_points * rc->scale) + 0.5);
1803 height = (int) ((height_points * rc->scale) + 0.5);
1805 #ifdef HAVE_POPPLER_PAGE_RENDER
1808 if (*surface == NULL) {
1809 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1814 cr = cairo_create (*surface);
1815 cairo_scale (cr, rc->scale, rc->scale);
1816 cairo_surface_set_device_offset (*surface, 0, 0);
1817 memset (cairo_image_surface_get_data (*surface), 0x00,
1818 cairo_image_surface_get_height (*surface) *
1819 cairo_image_surface_get_stride (*surface));
1820 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1822 (PopplerRectangle *)points,
1823 (PopplerRectangle *)old_points,
1824 (PopplerSelectionStyle)style,
1828 #else /* HAVE_POPPLER_PAGE_RENDER */
1831 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1835 poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1836 rc->scale, rc->rotation, pixbuf,
1837 (PopplerRectangle *)points,
1838 (PopplerRectangle *)old_points,
1839 (PopplerSelectionStyle)style,
1843 cairo_surface_destroy (*surface);
1844 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1845 g_object_unref (pixbuf);
1846 #endif /* HAVE_POPPLER_PAGE_RENDER */
1850 pdf_selection_get_selected_text (EvSelection *selection,
1851 EvRenderContext *rc,
1852 EvSelectionStyle style,
1853 EvRectangle *points)
1855 PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1856 PopplerPage *poppler_page;
1861 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1862 g_return_val_if_fail (poppler_page != NULL, NULL);
1864 poppler_page_get_size (poppler_page, NULL, &height);
1866 r.y1 = height - points->y2;
1868 r.y2 = height - points->y1;
1870 retval = poppler_page_get_text (poppler_page,
1871 (PopplerSelectionStyle)style,
1874 g_object_unref (poppler_page);
1880 pdf_selection_get_selection_region (EvSelection *selection,
1881 EvRenderContext *rc,
1882 EvSelectionStyle style,
1883 EvRectangle *points)
1885 PdfDocument *pdf_document;
1888 pdf_document = PDF_DOCUMENT (selection);
1890 set_rc_data (pdf_document, rc);
1892 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data,
1894 (PopplerSelectionStyle)style,
1895 (PopplerRectangle *) points);
1900 pdf_selection_get_selection_map (EvSelection *selection,
1901 EvRenderContext *rc)
1903 PdfDocument *pdf_document;
1904 PopplerPage *poppler_page;
1905 PopplerRectangle points;
1908 pdf_document = PDF_DOCUMENT (selection);
1909 poppler_page = poppler_document_get_page (pdf_document->document,
1914 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1915 retval = poppler_page_get_selection_region (poppler_page, 1.0,
1916 POPPLER_SELECTION_GLYPH,
1918 g_object_unref (poppler_page);
1924 pdf_selection_iface_init (EvSelectionIface *iface)
1926 iface->render_selection = pdf_selection_render_selection;
1927 iface->get_selected_text = pdf_selection_get_selected_text;
1928 iface->get_selection_region = pdf_selection_get_selection_region;
1929 iface->get_selection_map = pdf_selection_get_selection_map;
1932 /* Page Transitions */
1934 pdf_document_get_page_duration (EvDocumentTransition *trans,
1937 PdfDocument *pdf_document;
1938 PopplerPage *poppler_page;
1939 gdouble duration = -1;
1941 pdf_document = PDF_DOCUMENT (trans);
1942 poppler_page = poppler_document_get_page (pdf_document->document, page);
1946 duration = poppler_page_get_duration (poppler_page);
1947 g_object_unref (poppler_page);
1953 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1955 iface->get_page_duration = pdf_document_get_page_duration;
1959 pdf_document_new (void)
1961 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1966 pdf_document_get_crop_box (EvDocument *document,
1970 PdfDocument *pdf_document;
1971 PopplerPage *poppler_page;
1972 PopplerRectangle poppler_rect;
1974 pdf_document = PDF_DOCUMENT (document);
1975 poppler_page = poppler_document_get_page (pdf_document->document, page);
1976 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1977 rect->x1 = poppler_rect.x1;
1978 rect->x2 = poppler_rect.x2;
1979 rect->y1 = poppler_rect.y1;
1980 rect->y2 = poppler_rect.y2;
1984 static EvFormField *
1985 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1987 EvFormField *ev_field = NULL;
1990 gboolean is_read_only;
1992 id = poppler_form_field_get_id (poppler_field);
1993 font_size = poppler_form_field_get_font_size (poppler_field);
1994 is_read_only = poppler_form_field_is_read_only (poppler_field);
1996 switch (poppler_form_field_get_field_type (poppler_field)) {
1997 case POPPLER_FORM_FIELD_TEXT: {
1998 EvFormFieldText *field_text;
1999 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2001 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2002 case POPPLER_FORM_TEXT_NORMAL:
2003 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2005 case POPPLER_FORM_TEXT_MULTILINE:
2006 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2008 case POPPLER_FORM_TEXT_FILE_SELECT:
2009 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2013 ev_field = ev_form_field_text_new (id, ev_text_type);
2014 field_text = EV_FORM_FIELD_TEXT (ev_field);
2016 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2017 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2018 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2019 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2021 #ifdef HAVE_POPPLER_FORM_FIELD_TEXT_GET_MAX_LEN
2022 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2024 field_text->text = poppler_form_field_text_get_text (poppler_field);
2028 case POPPLER_FORM_FIELD_BUTTON: {
2029 EvFormFieldButton *field_button;
2030 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2032 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2033 case POPPLER_FORM_BUTTON_PUSH:
2034 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2036 case POPPLER_FORM_BUTTON_CHECK:
2037 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2039 case POPPLER_FORM_BUTTON_RADIO:
2040 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2044 ev_field = ev_form_field_button_new (id, ev_button_type);
2045 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2047 field_button->state = poppler_form_field_button_get_state (poppler_field);
2050 case POPPLER_FORM_FIELD_CHOICE: {
2051 EvFormFieldChoice *field_choice;
2052 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2054 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2055 case POPPLER_FORM_CHOICE_COMBO:
2056 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2058 case EV_FORM_FIELD_CHOICE_LIST:
2059 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2063 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2064 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2066 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2067 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2068 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2069 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2071 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2072 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2073 if (field_choice->is_editable)
2074 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2077 case POPPLER_FORM_FIELD_SIGNATURE:
2079 ev_field = ev_form_field_signature_new (id);
2081 case POPPLER_FORM_FIELD_UNKNOWN:
2085 ev_field->font_size = font_size;
2086 ev_field->is_read_only = is_read_only;
2092 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2095 PdfDocument *pdf_document;
2096 PopplerPage *poppler_page;
2097 GList *retval = NULL;
2103 pdf_document = PDF_DOCUMENT (document);
2104 poppler_page = poppler_document_get_page (pdf_document->document, page);
2105 fields = poppler_page_get_form_field_mapping (poppler_page);
2106 poppler_page_get_size (poppler_page, NULL, &height);
2108 for (list = fields; list; list = list->next) {
2109 PopplerFormFieldMapping *mapping;
2110 EvFormFieldMapping *field_mapping;
2112 mapping = (PopplerFormFieldMapping *)list->data;
2114 field_mapping = g_new0 (EvFormFieldMapping, 1);
2115 field_mapping->x1 = mapping->area.x1;
2116 field_mapping->x2 = mapping->area.x2;
2117 field_mapping->y1 = height - mapping->area.y2;
2118 field_mapping->y2 = height - mapping->area.y1;
2119 field_mapping->field = ev_form_field_from_poppler_field (mapping->field);
2120 field_mapping->field->page = page;
2122 retval = g_list_prepend (retval, field_mapping);
2124 poppler_page_free_form_field_mapping (fields);
2125 g_object_unref (poppler_page);
2127 return g_list_reverse (retval);
2131 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2135 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2136 PopplerFormField *poppler_field;
2139 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2143 text = poppler_form_field_text_get_text (poppler_field);
2144 g_object_unref (poppler_field);
2150 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2154 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2155 PopplerFormField *poppler_field;
2157 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2160 poppler_form_field_text_set_text (poppler_field, text);
2161 g_object_unref (poppler_field);
2165 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2169 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2170 PopplerFormField *poppler_field;
2172 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2176 poppler_form_field_button_set_state (poppler_field, state);
2177 g_object_unref (poppler_field);
2181 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2184 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2185 PopplerFormField *poppler_field;
2188 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2192 state = poppler_form_field_button_get_state (poppler_field);
2193 g_object_unref (poppler_field);
2199 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2203 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2204 PopplerFormField *poppler_field;
2207 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2211 text = poppler_form_field_choice_get_item (poppler_field, index);
2212 g_object_unref (poppler_field);
2218 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2221 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2222 PopplerFormField *poppler_field;
2225 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2229 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2230 g_object_unref (poppler_field);
2236 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2240 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2241 PopplerFormField *poppler_field;
2244 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2248 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2249 g_object_unref (poppler_field);
2255 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2259 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2260 PopplerFormField *poppler_field;
2262 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2266 poppler_form_field_choice_select_item (poppler_field, index);
2267 g_object_unref (poppler_field);
2271 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2275 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2276 PopplerFormField *poppler_field;
2278 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2282 poppler_form_field_choice_toggle_item (poppler_field, index);
2283 g_object_unref (poppler_field);
2287 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2290 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2291 PopplerFormField *poppler_field;
2293 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2297 poppler_form_field_choice_unselect_all (poppler_field);
2298 g_object_unref (poppler_field);
2302 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2306 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2307 PopplerFormField *poppler_field;
2309 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2313 poppler_form_field_choice_set_text (poppler_field, text);
2314 g_object_unref (poppler_field);
2318 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2321 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2322 PopplerFormField *poppler_field;
2325 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2329 text = poppler_form_field_choice_get_text (poppler_field);
2330 g_object_unref (poppler_field);
2336 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2338 iface->get_form_fields = pdf_document_forms_get_form_fields;
2339 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2340 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2341 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2342 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2343 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2344 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2345 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2346 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2347 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2348 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2349 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2350 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2352 #endif /* HAVE_FORMS */