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);
1063 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1064 poppler_action_free (action);
1066 g_object_unref (link);
1071 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1072 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1074 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1075 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1076 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1077 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1080 g_free (title_markup);
1081 g_object_unref (link);
1083 child = poppler_index_iter_get_child (iter);
1085 build_tree (pdf_document, model, &tree_iter, child);
1086 poppler_index_iter_free (child);
1087 poppler_action_free (action);
1089 } while (poppler_index_iter_next (iter));
1092 static GtkTreeModel *
1093 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1095 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1096 GtkTreeModel *model = NULL;
1097 PopplerIndexIter *iter;
1099 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1101 iter = poppler_index_iter_new (pdf_document->document);
1102 /* Create the model if we have items*/
1104 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1109 build_tree (pdf_document, model, NULL, iter);
1110 poppler_index_iter_free (iter);
1117 pdf_document_links_get_links (EvDocumentLinks *document_links,
1120 PdfDocument *pdf_document;
1121 PopplerPage *poppler_page;
1122 GList *retval = NULL;
1123 GList *mapping_list;
1127 pdf_document = PDF_DOCUMENT (document_links);
1128 poppler_page = poppler_document_get_page (pdf_document->document,
1130 mapping_list = poppler_page_get_link_mapping (poppler_page);
1131 poppler_page_get_size (poppler_page, NULL, &height);
1133 for (list = mapping_list; list; list = list->next) {
1134 PopplerLinkMapping *link_mapping;
1135 EvLinkMapping *ev_link_mapping;
1137 link_mapping = (PopplerLinkMapping *)list->data;
1138 ev_link_mapping = g_new (EvLinkMapping, 1);
1139 ev_link_mapping->link = ev_link_from_action (pdf_document,
1140 link_mapping->action);
1141 ev_link_mapping->x1 = link_mapping->area.x1;
1142 ev_link_mapping->x2 = link_mapping->area.x2;
1143 /* Invert this for X-style coordinates */
1144 ev_link_mapping->y1 = height - link_mapping->area.y2;
1145 ev_link_mapping->y2 = height - link_mapping->area.y1;
1147 retval = g_list_prepend (retval, ev_link_mapping);
1150 poppler_page_free_link_mapping (mapping_list);
1151 g_object_unref (poppler_page);
1153 return g_list_reverse (retval);
1157 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1158 const gchar *link_name)
1160 PdfDocument *pdf_document;
1162 EvLinkDest *ev_dest = NULL;
1164 pdf_document = PDF_DOCUMENT (document_links);
1165 dest = poppler_document_find_dest (pdf_document->document,
1168 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1169 poppler_dest_free (dest);
1176 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1178 iface->has_document_links = pdf_document_links_has_document_links;
1179 iface->get_links_model = pdf_document_links_get_links_model;
1180 iface->get_links = pdf_document_links_get_links;
1181 iface->find_link_dest = pdf_document_links_find_link_dest;
1185 pdf_document_images_get_images (EvDocumentImages *document_images,
1188 GList *retval = NULL;
1189 PdfDocument *pdf_document;
1190 PopplerPage *poppler_page;
1191 GList *mapping_list;
1194 pdf_document = PDF_DOCUMENT (document_images);
1195 poppler_page = poppler_document_get_page (pdf_document->document, page);
1196 mapping_list = poppler_page_get_image_mapping (poppler_page);
1198 for (list = mapping_list; list; list = list->next) {
1199 PopplerImageMapping *image_mapping;
1200 EvImageMapping *ev_image_mapping;
1202 image_mapping = (PopplerImageMapping *)list->data;
1204 ev_image_mapping = g_new (EvImageMapping, 1);
1206 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1207 ev_image_mapping->x1 = image_mapping->area.x1;
1208 ev_image_mapping->x2 = image_mapping->area.x2;
1209 ev_image_mapping->y1 = image_mapping->area.y1;
1210 ev_image_mapping->y2 = image_mapping->area.y2;
1212 retval = g_list_prepend (retval, ev_image_mapping);
1215 poppler_page_free_image_mapping (mapping_list);
1216 g_object_unref (poppler_page);
1222 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1224 iface->get_images = pdf_document_images_get_images;
1228 make_thumbnail_for_page (PdfDocument *pdf_document,
1229 PopplerPage *poppler_page,
1230 EvRenderContext *rc)
1235 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1236 rc, &width, &height);
1238 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1240 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1242 ev_document_fc_mutex_lock ();
1243 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1245 rc->scale, rc->rotation, pixbuf);
1246 ev_document_fc_mutex_unlock ();
1252 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1253 EvRenderContext *rc,
1256 PdfDocument *pdf_document;
1257 PopplerPage *poppler_page;
1259 GdkPixbuf *border_pixbuf;
1261 pdf_document = PDF_DOCUMENT (document_thumbnails);
1263 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1264 g_return_val_if_fail (poppler_page != NULL, NULL);
1266 pixbuf = poppler_page_get_thumbnail (poppler_page);
1268 /* Rotate provided thumbnail if needed */
1269 GdkPixbuf *rotated_pixbuf;
1271 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1272 (GdkPixbufRotation) (360 - rc->rotation));
1273 g_object_unref (pixbuf);
1274 pixbuf = rotated_pixbuf;
1276 /* There is no provided thumbnail. We need to make one. */
1277 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1281 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1282 g_object_unref (pixbuf);
1283 pixbuf = border_pixbuf;
1286 g_object_unref (poppler_page);
1292 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1293 EvRenderContext *rc,
1297 PdfDocument *pdf_document;
1298 PopplerPage *poppler_page;
1301 pdf_document = PDF_DOCUMENT (document_thumbnails);
1302 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1304 g_return_if_fail (poppler_page != NULL);
1306 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1309 double page_width, page_height;
1311 poppler_page_get_size (poppler_page, &page_width, &page_height);
1313 *width = (gint) MAX (page_width * rc->scale, 1);
1314 *height = (gint) MAX (page_height * rc->scale, 1);
1317 if (rc->rotation == 90 || rc->rotation == 270) {
1325 g_object_unref (poppler_page);
1329 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1331 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1332 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1337 pdf_document_search_idle_callback (void *data)
1339 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1340 PdfDocument *pdf_document = search->document;
1345 page = poppler_document_get_page (search->document->document,
1346 search->search_page);
1348 ev_document_doc_mutex_lock ();
1349 matches = poppler_page_find_text (page, search->text);
1350 ev_document_doc_mutex_unlock ();
1352 g_object_unref (page);
1354 search->pages[search->search_page] = matches;
1355 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1356 search->search_page);
1358 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1359 search->search_page += 1;
1360 if (search->search_page == n_pages) {
1362 search->search_page = 0;
1365 if (search->search_page != search->start_page) {
1370 search->idle = 0; /* will return FALSE to remove */
1375 static PdfDocumentSearch *
1376 pdf_document_search_new (PdfDocument *pdf_document,
1380 PdfDocumentSearch *search;
1384 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1386 search = g_new0 (PdfDocumentSearch, 1);
1388 search->text = g_strdup (text);
1389 search->pages = g_new0 (GList *, n_pages);
1390 search->document = pdf_document;
1392 /* We add at low priority so the progress bar repaints */
1393 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1394 pdf_document_search_idle_callback,
1398 search->start_page = start_page;
1399 search->search_page = start_page;
1405 pdf_document_find_begin (EvDocumentFind *document,
1407 const char *search_string,
1408 gboolean case_sensitive)
1410 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1412 /* FIXME handle case_sensitive (right now XPDF
1413 * code is always case insensitive for ASCII
1414 * and case sensitive for all other languaages)
1417 if (pdf_document->search &&
1418 strcmp (search_string, pdf_document->search->text) == 0)
1421 if (pdf_document->search)
1422 pdf_document_search_free (pdf_document->search);
1424 pdf_document->search = pdf_document_search_new (pdf_document,
1430 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1432 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1435 return g_list_length (search->pages[page]);
1442 pdf_document_find_get_result (EvDocumentFind *document_find,
1445 EvRectangle *rectangle)
1447 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1448 PdfDocumentSearch *search = pdf_document->search;
1449 PopplerPage *poppler_page;
1450 PopplerRectangle *r;
1456 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1461 poppler_page = poppler_document_get_page (pdf_document->document, page);
1462 poppler_page_get_size (poppler_page, NULL, &height);
1463 rectangle->x1 = r->x1;
1464 rectangle->y1 = height - r->y2;
1465 rectangle->x2 = r->x2;
1466 rectangle->y2 = height - r->y1;
1467 g_object_unref (poppler_page);
1473 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1476 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1478 return search && search->pages[page] != NULL;
1482 pdf_document_find_get_progress (EvDocumentFind *document_find)
1484 PdfDocumentSearch *search;
1485 int n_pages, pages_done;
1487 search = PDF_DOCUMENT (document_find)->search;
1489 if (search == NULL) {
1493 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1494 if (search->search_page > search->start_page) {
1495 pages_done = search->search_page - search->start_page + 1;
1496 } else if (search->search_page == search->start_page) {
1497 pages_done = n_pages;
1499 pages_done = n_pages - search->start_page + search->search_page;
1502 return pages_done / (double) n_pages;
1506 pdf_document_find_cancel (EvDocumentFind *document)
1508 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1510 if (pdf_document->search) {
1511 pdf_document_search_free (pdf_document->search);
1512 pdf_document->search = NULL;
1517 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1519 iface->begin = pdf_document_find_begin;
1520 iface->get_n_results = pdf_document_find_get_n_results;
1521 iface->get_result = pdf_document_find_get_result;
1522 iface->page_has_results = pdf_document_find_page_has_results;
1523 iface->get_progress = pdf_document_find_get_progress;
1524 iface->cancel = pdf_document_find_cancel;
1528 pdf_print_context_free (PdfPrintContext *ctx)
1533 #ifdef HAVE_CAIRO_PRINT
1535 cairo_destroy (ctx->cr);
1540 poppler_ps_file_free (ctx->ps_file);
1541 ctx->ps_file = NULL;
1548 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1549 EvFileExporterContext *fc)
1551 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1552 PdfPrintContext *ctx;
1553 gdouble width, height;
1554 #ifdef HAVE_CAIRO_PRINT
1555 cairo_surface_t *surface = NULL;
1558 if (pdf_document->print_ctx)
1559 pdf_print_context_free (pdf_document->print_ctx);
1560 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1561 ctx = pdf_document->print_ctx;
1562 ctx->format = fc->format;
1563 ctx->pages_per_sheet = fc->pages_per_sheet;
1565 ctx->paper_width = fc->paper_width;
1566 ctx->paper_height = fc->paper_height;
1568 switch (fc->pages_per_sheet) {
1596 ctx->pages_printed = 0;
1598 switch (fc->format) {
1599 case EV_FILE_FORMAT_PS:
1600 #ifdef HAVE_CAIRO_PS
1601 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1603 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1604 fc->filename, fc->first_page,
1605 fc->last_page - fc->first_page + 1);
1606 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1607 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1608 #endif /* HAVE_CAIRO_PS */
1610 case EV_FILE_FORMAT_PDF:
1611 #ifdef HAVE_CAIRO_PDF
1612 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1616 g_assert_not_reached ();
1619 #ifdef HAVE_CAIRO_PRINT
1620 ctx->cr = cairo_create (surface);
1621 cairo_surface_destroy (surface);
1626 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1627 EvRenderContext *rc)
1629 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1630 PdfPrintContext *ctx = pdf_document->print_ctx;
1631 PopplerPage *poppler_page;
1632 #ifdef HAVE_CAIRO_PRINT
1633 gdouble page_width, page_height;
1635 gboolean rotate, landscape;
1636 gdouble width, height;
1637 gdouble pwidth, pheight;
1638 gdouble xscale, yscale;
1641 g_return_if_fail (pdf_document->print_ctx != NULL);
1643 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1645 #ifdef HAVE_CAIRO_PRINT
1646 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1647 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1648 poppler_page_get_size (poppler_page, &page_width, &page_height);
1650 if (page_width > page_height && page_width > ctx->paper_width) {
1656 landscape = (ctx->paper_width > ctx->paper_height);
1658 /* Use always portrait mode and rotate when necessary */
1659 if (ctx->paper_width > ctx->paper_height) {
1660 width = ctx->paper_height;
1661 height = ctx->paper_width;
1664 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1667 width = ctx->paper_width;
1668 height = ctx->paper_height;
1671 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1684 page_width = page_height;
1689 pwidth = width / ctx->pages_x;
1690 pheight = height / ctx->pages_y;
1692 if ((page_width > pwidth || page_height > pheight) ||
1693 (page_width < pwidth && page_height < pheight)) {
1694 xscale = pwidth / page_width;
1695 yscale = pheight / page_height;
1697 if (yscale < xscale) {
1704 xscale = yscale = 1;
1709 cairo_save (ctx->cr);
1711 cairo_matrix_t matrix;
1713 cairo_translate (ctx->cr, width, 0);
1714 cairo_matrix_init (&matrix,
1718 cairo_transform (ctx->cr, &matrix);
1721 cairo_translate (ctx->cr,
1722 x * (rotate ? pheight : pwidth),
1723 y * (rotate ? pwidth : pheight));
1724 cairo_scale (ctx->cr, xscale, yscale);
1726 #ifdef HAVE_POPPLER_PAGE_RENDER
1727 poppler_page_render (poppler_page, ctx->cr);
1729 ctx->pages_printed++;
1731 if (ctx->pages_printed % ctx->pages_per_sheet == 0) {
1732 cairo_show_page (ctx->cr);
1734 cairo_restore (ctx->cr);
1735 #else /* HAVE_CAIRO_PRINT */
1736 if (ctx->format == EV_FILE_FORMAT_PS)
1737 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1738 #endif /* HAVE_CAIRO_PRINT */
1740 g_object_unref (poppler_page);
1744 pdf_document_file_exporter_end (EvFileExporter *exporter)
1746 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1748 pdf_print_context_free (pdf_document->print_ctx);
1749 pdf_document->print_ctx = NULL;
1752 static EvFileExporterCapabilities
1753 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1755 return (EvFileExporterCapabilities) (
1756 EV_FILE_EXPORTER_CAN_PAGE_SET |
1757 EV_FILE_EXPORTER_CAN_COPIES |
1758 EV_FILE_EXPORTER_CAN_COLLATE |
1759 EV_FILE_EXPORTER_CAN_REVERSE |
1760 EV_FILE_EXPORTER_CAN_SCALE |
1761 #ifdef HAVE_CAIRO_PRINT
1762 #ifdef HAVE_POPPLER_PAGE_RENDER
1763 #if GTK_CHECK_VERSION (2, 11, 1)
1764 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1769 #ifdef HAVE_CAIRO_PDF
1770 #ifdef HAVE_POPPLER_PAGE_RENDER
1771 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1774 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1778 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1780 iface->begin = pdf_document_file_exporter_begin;
1781 iface->do_page = pdf_document_file_exporter_do_page;
1782 iface->end = pdf_document_file_exporter_end;
1783 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1787 pdf_selection_render_selection (EvSelection *selection,
1788 EvRenderContext *rc,
1789 cairo_surface_t **surface,
1790 EvRectangle *points,
1791 EvRectangle *old_points,
1792 EvSelectionStyle style,
1796 PdfDocument *pdf_document;
1797 double width_points, height_points;
1800 pdf_document = PDF_DOCUMENT (selection);
1801 set_rc_data (pdf_document, rc);
1803 poppler_page_get_size (POPPLER_PAGE (rc->data),
1804 &width_points, &height_points);
1805 width = (int) ((width_points * rc->scale) + 0.5);
1806 height = (int) ((height_points * rc->scale) + 0.5);
1808 #ifdef HAVE_POPPLER_PAGE_RENDER
1811 if (*surface == NULL) {
1812 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1817 cr = cairo_create (*surface);
1818 cairo_scale (cr, rc->scale, rc->scale);
1819 cairo_surface_set_device_offset (*surface, 0, 0);
1820 memset (cairo_image_surface_get_data (*surface), 0x00,
1821 cairo_image_surface_get_height (*surface) *
1822 cairo_image_surface_get_stride (*surface));
1823 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1825 (PopplerRectangle *)points,
1826 (PopplerRectangle *)old_points,
1827 (PopplerSelectionStyle)style,
1831 #else /* HAVE_POPPLER_PAGE_RENDER */
1834 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1838 poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1839 rc->scale, rc->rotation, pixbuf,
1840 (PopplerRectangle *)points,
1841 (PopplerRectangle *)old_points,
1842 (PopplerSelectionStyle)style,
1846 cairo_surface_destroy (*surface);
1847 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1848 g_object_unref (pixbuf);
1849 #endif /* HAVE_POPPLER_PAGE_RENDER */
1853 pdf_selection_get_selected_text (EvSelection *selection,
1854 EvRenderContext *rc,
1855 EvSelectionStyle style,
1856 EvRectangle *points)
1858 PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1859 PopplerPage *poppler_page;
1864 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1865 g_return_val_if_fail (poppler_page != NULL, NULL);
1867 poppler_page_get_size (poppler_page, NULL, &height);
1869 r.y1 = height - points->y2;
1871 r.y2 = height - points->y1;
1873 retval = poppler_page_get_text (poppler_page,
1875 (PopplerSelectionStyle)style,
1879 g_object_unref (poppler_page);
1885 pdf_selection_get_selection_region (EvSelection *selection,
1886 EvRenderContext *rc,
1887 EvSelectionStyle style,
1888 EvRectangle *points)
1890 PdfDocument *pdf_document;
1893 pdf_document = PDF_DOCUMENT (selection);
1895 set_rc_data (pdf_document, rc);
1897 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data,
1900 (PopplerSelectionStyle)style,
1902 (PopplerRectangle *) points);
1907 pdf_selection_get_selection_map (EvSelection *selection,
1908 EvRenderContext *rc)
1910 PdfDocument *pdf_document;
1911 PopplerPage *poppler_page;
1912 PopplerRectangle points;
1915 pdf_document = PDF_DOCUMENT (selection);
1916 poppler_page = poppler_document_get_page (pdf_document->document,
1921 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1922 retval = poppler_page_get_selection_region (poppler_page, 1.0,
1924 POPPLER_SELECTION_GLYPH,
1927 g_object_unref (poppler_page);
1933 pdf_selection_iface_init (EvSelectionIface *iface)
1935 iface->render_selection = pdf_selection_render_selection;
1936 iface->get_selected_text = pdf_selection_get_selected_text;
1937 iface->get_selection_region = pdf_selection_get_selection_region;
1938 iface->get_selection_map = pdf_selection_get_selection_map;
1941 /* Page Transitions */
1943 pdf_document_get_page_duration (EvDocumentTransition *trans,
1946 PdfDocument *pdf_document;
1947 PopplerPage *poppler_page;
1948 gdouble duration = -1;
1950 pdf_document = PDF_DOCUMENT (trans);
1951 poppler_page = poppler_document_get_page (pdf_document->document, page);
1955 duration = poppler_page_get_duration (poppler_page);
1956 g_object_unref (poppler_page);
1962 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1964 iface->get_page_duration = pdf_document_get_page_duration;
1968 pdf_document_new (void)
1970 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1975 pdf_document_get_crop_box (EvDocument *document,
1979 PdfDocument *pdf_document;
1980 PopplerPage *poppler_page;
1981 PopplerRectangle poppler_rect;
1983 pdf_document = PDF_DOCUMENT (document);
1984 poppler_page = poppler_document_get_page (pdf_document->document, page);
1985 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1986 rect->x1 = poppler_rect.x1;
1987 rect->x2 = poppler_rect.x2;
1988 rect->y1 = poppler_rect.y1;
1989 rect->y2 = poppler_rect.y2;
1993 static EvFormField *
1994 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1996 EvFormField *ev_field = NULL;
1999 gboolean is_read_only;
2001 id = poppler_form_field_get_id (poppler_field);
2002 font_size = poppler_form_field_get_font_size (poppler_field);
2003 is_read_only = poppler_form_field_is_read_only (poppler_field);
2005 switch (poppler_form_field_get_field_type (poppler_field)) {
2006 case POPPLER_FORM_FIELD_TEXT: {
2007 EvFormFieldText *field_text;
2008 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2010 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2011 case POPPLER_FORM_TEXT_NORMAL:
2012 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2014 case POPPLER_FORM_TEXT_MULTILINE:
2015 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2017 case POPPLER_FORM_TEXT_FILE_SELECT:
2018 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2022 ev_field = ev_form_field_text_new (id, ev_text_type);
2023 field_text = EV_FORM_FIELD_TEXT (ev_field);
2025 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2026 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2027 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2028 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2030 #ifdef HAVE_POPPLER_FORM_FIELD_TEXT_GET_MAX_LEN
2031 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2033 field_text->text = poppler_form_field_text_get_text (poppler_field);
2037 case POPPLER_FORM_FIELD_BUTTON: {
2038 EvFormFieldButton *field_button;
2039 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2041 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2042 case POPPLER_FORM_BUTTON_PUSH:
2043 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2045 case POPPLER_FORM_BUTTON_CHECK:
2046 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2048 case POPPLER_FORM_BUTTON_RADIO:
2049 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2053 ev_field = ev_form_field_button_new (id, ev_button_type);
2054 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2056 field_button->state = poppler_form_field_button_get_state (poppler_field);
2059 case POPPLER_FORM_FIELD_CHOICE: {
2060 EvFormFieldChoice *field_choice;
2061 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2063 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2064 case POPPLER_FORM_CHOICE_COMBO:
2065 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2067 case EV_FORM_FIELD_CHOICE_LIST:
2068 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2072 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2073 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2075 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2076 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2077 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2078 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2080 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2081 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2082 if (field_choice->is_editable)
2083 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2086 case POPPLER_FORM_FIELD_SIGNATURE:
2088 ev_field = ev_form_field_signature_new (id);
2090 case POPPLER_FORM_FIELD_UNKNOWN:
2094 ev_field->font_size = font_size;
2095 ev_field->is_read_only = is_read_only;
2101 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2104 PdfDocument *pdf_document;
2105 PopplerPage *poppler_page;
2106 GList *retval = NULL;
2112 pdf_document = PDF_DOCUMENT (document);
2113 poppler_page = poppler_document_get_page (pdf_document->document, page);
2114 fields = poppler_page_get_form_field_mapping (poppler_page);
2115 poppler_page_get_size (poppler_page, NULL, &height);
2117 for (list = fields; list; list = list->next) {
2118 PopplerFormFieldMapping *mapping;
2119 EvFormFieldMapping *field_mapping;
2121 mapping = (PopplerFormFieldMapping *)list->data;
2123 field_mapping = g_new0 (EvFormFieldMapping, 1);
2124 field_mapping->x1 = mapping->area.x1;
2125 field_mapping->x2 = mapping->area.x2;
2126 field_mapping->y1 = height - mapping->area.y2;
2127 field_mapping->y2 = height - mapping->area.y1;
2128 field_mapping->field = ev_form_field_from_poppler_field (mapping->field);
2129 field_mapping->field->page = page;
2131 retval = g_list_prepend (retval, field_mapping);
2133 poppler_page_free_form_field_mapping (fields);
2134 g_object_unref (poppler_page);
2136 return g_list_reverse (retval);
2140 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2144 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2145 PopplerFormField *poppler_field;
2148 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2152 text = poppler_form_field_text_get_text (poppler_field);
2153 g_object_unref (poppler_field);
2159 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2163 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2164 PopplerFormField *poppler_field;
2166 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2169 poppler_form_field_text_set_text (poppler_field, text);
2170 g_object_unref (poppler_field);
2174 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2178 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2179 PopplerFormField *poppler_field;
2181 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2185 poppler_form_field_button_set_state (poppler_field, state);
2186 g_object_unref (poppler_field);
2190 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2193 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2194 PopplerFormField *poppler_field;
2197 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2201 state = poppler_form_field_button_get_state (poppler_field);
2202 g_object_unref (poppler_field);
2208 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2212 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2213 PopplerFormField *poppler_field;
2216 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2220 text = poppler_form_field_choice_get_item (poppler_field, index);
2221 g_object_unref (poppler_field);
2227 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2230 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2231 PopplerFormField *poppler_field;
2234 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2238 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2239 g_object_unref (poppler_field);
2245 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2249 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2250 PopplerFormField *poppler_field;
2253 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2257 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2258 g_object_unref (poppler_field);
2264 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2268 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2269 PopplerFormField *poppler_field;
2271 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2275 poppler_form_field_choice_select_item (poppler_field, index);
2276 g_object_unref (poppler_field);
2280 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2284 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2285 PopplerFormField *poppler_field;
2287 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2291 poppler_form_field_choice_toggle_item (poppler_field, index);
2292 g_object_unref (poppler_field);
2296 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2299 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2300 PopplerFormField *poppler_field;
2302 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2306 poppler_form_field_choice_unselect_all (poppler_field);
2307 g_object_unref (poppler_field);
2311 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2315 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2316 PopplerFormField *poppler_field;
2318 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2322 poppler_form_field_choice_set_text (poppler_field, text);
2323 g_object_unref (poppler_field);
2327 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2330 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2331 PopplerFormField *poppler_field;
2334 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2338 text = poppler_form_field_choice_get_text (poppler_field);
2339 g_object_unref (poppler_field);
2345 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2347 iface->get_form_fields = pdf_document_forms_get_form_fields;
2348 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2349 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2350 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2351 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2352 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2353 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2354 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2355 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2356 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2357 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2358 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2359 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2361 #endif /* HAVE_FORMS */