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>
35 #include <glib/gi18n.h>
37 #include "ev-poppler.h"
38 #include "ev-file-exporter.h"
39 #include "ev-document-find.h"
40 #include "ev-document-misc.h"
41 #include "ev-document-links.h"
42 #include "ev-document-images.h"
43 #include "ev-document-fonts.h"
44 #include "ev-document-security.h"
45 #include "ev-document-thumbnails.h"
46 #include "ev-document-transition.h"
47 #include "ev-document-forms.h"
48 #include "ev-selection.h"
49 #include "ev-attachment.h"
53 PdfDocument *document;
62 EvFileExporterFormat format;
63 PopplerPSFile *ps_file;
69 struct _PdfDocumentClass
71 GObjectClass parent_class;
76 GObject parent_instance;
78 PopplerDocument *document;
81 PopplerFontInfo *font_info;
82 PopplerFontsIter *fonts_iter;
83 int fonts_scanned_pages;
85 PdfDocumentSearch *search;
86 PdfPrintContext *print_ctx;
89 static void pdf_document_document_iface_init (EvDocumentIface *iface);
90 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
91 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
92 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
93 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
94 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
95 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
96 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
97 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
98 static void pdf_selection_iface_init (EvSelectionIface *iface);
99 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
100 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
104 static int pdf_document_get_n_pages (EvDocument *document);
106 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
108 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
109 PopplerAction *action);
110 static void pdf_document_search_free (PdfDocumentSearch *search);
111 static void pdf_print_context_free (PdfPrintContext *ctx);
113 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
115 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
116 pdf_document_document_iface_init);
117 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
118 pdf_document_security_iface_init);
119 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
120 pdf_document_document_thumbnails_iface_init);
121 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
122 pdf_document_document_links_iface_init);
123 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
124 pdf_document_document_images_iface_init);
126 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
127 pdf_document_document_forms_iface_init);
128 #endif /* HAVE_FORMS */
129 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
130 pdf_document_document_fonts_iface_init);
131 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
132 pdf_document_find_iface_init);
133 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
134 pdf_document_file_exporter_iface_init);
135 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
136 pdf_selection_iface_init);
137 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
138 pdf_document_page_transition_iface_init);
143 set_rc_data (PdfDocument *pdf_document,
146 if (rc->data == NULL) {
147 rc->data = poppler_document_get_page (pdf_document->document,
149 rc->destroy = g_object_unref;
151 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
156 pdf_document_search_free (PdfDocumentSearch *search)
158 PdfDocument *pdf_document = search->document;
162 if (search->idle != 0)
163 g_source_remove (search->idle);
165 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
166 for (i = 0; i < n_pages; i++) {
167 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
168 g_list_free (search->pages[i]);
170 g_free (search->pages);
172 g_free (search->text);
177 pdf_document_dispose (GObject *object)
179 PdfDocument *pdf_document = PDF_DOCUMENT(object);
181 if (pdf_document->print_ctx) {
182 pdf_print_context_free (pdf_document->print_ctx);
183 pdf_document->print_ctx = NULL;
186 if (pdf_document->search) {
187 pdf_document_search_free (pdf_document->search);
188 pdf_document->search = NULL;
191 if (pdf_document->document) {
192 g_object_unref (pdf_document->document);
195 if (pdf_document->font_info) {
196 poppler_font_info_free (pdf_document->font_info);
199 if (pdf_document->fonts_iter) {
200 poppler_fonts_iter_free (pdf_document->fonts_iter);
203 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
207 pdf_document_class_init (PdfDocumentClass *klass)
209 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
211 g_object_class->dispose = pdf_document_dispose;
215 pdf_document_init (PdfDocument *pdf_document)
217 pdf_document->password = NULL;
221 convert_error (GError *poppler_error,
224 if (poppler_error == NULL)
227 if (poppler_error->domain == POPPLER_ERROR) {
228 /* convert poppler errors into EvDocument errors */
229 gint code = EV_DOCUMENT_ERROR_INVALID;
230 if (poppler_error->code == POPPLER_ERROR_INVALID)
231 code = EV_DOCUMENT_ERROR_INVALID;
232 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
233 code = EV_DOCUMENT_ERROR_ENCRYPTED;
239 poppler_error->message,
242 g_propagate_error (error, poppler_error);
249 pdf_document_save (EvDocument *document,
254 GError *poppler_error = NULL;
256 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
260 convert_error (poppler_error, error);
266 pdf_document_load (EvDocument *document,
270 GError *poppler_error = NULL;
271 PdfDocument *pdf_document = PDF_DOCUMENT (document);
273 pdf_document->document =
274 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
276 if (pdf_document->document == NULL) {
277 convert_error (poppler_error, error);
285 pdf_document_get_n_pages (EvDocument *document)
287 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
291 pdf_document_get_page_size (EvDocument *document,
296 PdfDocument *pdf_document = PDF_DOCUMENT (document);
297 PopplerPage *poppler_page;
299 poppler_page = poppler_document_get_page (pdf_document->document, page);
300 poppler_page_get_size (poppler_page, width, height);
301 g_object_unref (poppler_page);
305 pdf_document_get_page_label (EvDocument *document,
308 PopplerPage *poppler_page;
311 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
314 g_object_get (G_OBJECT (poppler_page),
317 g_object_unref (poppler_page);
323 pdf_document_has_attachments (EvDocument *document)
325 PdfDocument *pdf_document;
327 pdf_document = PDF_DOCUMENT (document);
329 return poppler_document_has_attachments (pdf_document->document);
332 struct SaveToBufferData {
338 attachment_save_to_buffer_callback (const gchar *buf,
343 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
347 if (sdata->len + count > sdata->max) {
348 new_max = MAX (sdata->max * 2, sdata->len + count);
349 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
351 sdata->buffer = new_buffer;
352 sdata->max = new_max;
355 memcpy (sdata->buffer + sdata->len, buf, count);
362 attachment_save_to_buffer (PopplerAttachment *attachment,
367 static const gint initial_max = 1024;
368 struct SaveToBufferData sdata;
373 sdata.buffer = (gchar *) g_malloc (initial_max);
374 sdata.max = initial_max;
377 if (! poppler_attachment_save_to_callback (attachment,
378 attachment_save_to_buffer_callback,
381 g_free (sdata.buffer);
385 *buffer = sdata.buffer;
386 *buffer_size = sdata.len;
392 pdf_document_get_attachments (EvDocument *document)
394 PdfDocument *pdf_document;
397 GList *retval = NULL;
399 pdf_document = PDF_DOCUMENT (document);
401 if (!pdf_document_has_attachments (document))
404 attachments = poppler_document_get_attachments (pdf_document->document);
406 for (list = attachments; list; list = list->next) {
407 PopplerAttachment *attachment;
408 EvAttachment *ev_attachment;
411 GError *error = NULL;
413 attachment = (PopplerAttachment *) list->data;
415 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
416 ev_attachment = ev_attachment_new (attachment->name,
417 attachment->description,
422 retval = g_list_prepend (retval, ev_attachment);
425 g_warning ("%s", error->message);
426 g_error_free (error);
432 g_object_unref (attachment);
435 return g_list_reverse (retval);
438 static cairo_surface_t *
439 pdf_document_render (EvDocument *document,
442 PdfDocument *pdf_document;
443 cairo_surface_t *surface;
444 double width_points, height_points;
447 pdf_document = PDF_DOCUMENT (document);
449 set_rc_data (pdf_document, rc);
451 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
453 if (rc->rotation == 90 || rc->rotation == 270) {
454 width = (int) ((height_points * rc->scale) + 0.5);
455 height = (int) ((width_points * rc->scale) + 0.5);
457 width = (int) ((width_points * rc->scale) + 0.5);
458 height = (int) ((height_points * rc->scale) + 0.5);
461 #ifdef HAVE_POPPLER_PAGE_RENDER
464 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
466 memset (cairo_image_surface_get_data (surface), 0xff,
467 cairo_image_surface_get_height (surface) *
468 cairo_image_surface_get_stride (surface));
470 cr = cairo_create (surface);
471 switch (rc->rotation) {
473 cairo_translate (cr, width, 0);
476 cairo_translate (cr, width, height);
479 cairo_translate (cr, 0, height);
482 cairo_translate (cr, 0, 0);
484 cairo_scale (cr, rc->scale, rc->scale);
485 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
486 poppler_page_render (POPPLER_PAGE (rc->data), cr);
488 #else /* HAVE_POPPLER_PAGE_RENDER */
491 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
495 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
501 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
502 g_object_unref (pixbuf);
503 #endif /* HAVE_POPPLER_PAGE_RENDER */
508 /* EvDocumentSecurity */
511 pdf_document_has_document_security (EvDocumentSecurity *document_security)
513 /* FIXME: do we really need to have this? */
518 pdf_document_set_password (EvDocumentSecurity *document_security,
519 const char *password)
521 PdfDocument *document = PDF_DOCUMENT (document_security);
523 if (document->password)
524 g_free (document->password);
526 document->password = g_strdup (password);
530 pdf_document_can_get_text (EvDocument *document)
535 static EvDocumentInfo *
536 pdf_document_get_info (EvDocument *document)
538 EvDocumentInfo *info;
539 PopplerPageLayout layout;
540 PopplerPageMode mode;
541 PopplerViewerPreferences view_prefs;
542 PopplerPermissions permissions;
544 info = g_new0 (EvDocumentInfo, 1);
546 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
547 EV_DOCUMENT_INFO_FORMAT |
548 EV_DOCUMENT_INFO_AUTHOR |
549 EV_DOCUMENT_INFO_SUBJECT |
550 EV_DOCUMENT_INFO_KEYWORDS |
551 EV_DOCUMENT_INFO_LAYOUT |
552 EV_DOCUMENT_INFO_START_MODE |
553 EV_DOCUMENT_INFO_PERMISSIONS |
554 EV_DOCUMENT_INFO_UI_HINTS |
555 EV_DOCUMENT_INFO_CREATOR |
556 EV_DOCUMENT_INFO_PRODUCER |
557 EV_DOCUMENT_INFO_CREATION_DATE |
558 EV_DOCUMENT_INFO_MOD_DATE |
559 EV_DOCUMENT_INFO_LINEARIZED |
560 EV_DOCUMENT_INFO_N_PAGES |
561 EV_DOCUMENT_INFO_SECURITY |
562 EV_DOCUMENT_INFO_PAPER_SIZE;
564 g_object_get (PDF_DOCUMENT (document)->document,
565 "title", &(info->title),
566 "format", &(info->format),
567 "author", &(info->author),
568 "subject", &(info->subject),
569 "keywords", &(info->keywords),
571 "page-layout", &layout,
572 "viewer-preferences", &view_prefs,
573 "permissions", &permissions,
574 "creator", &(info->creator),
575 "producer", &(info->producer),
576 "creation-date", &(info->creation_date),
577 "mod-date", &(info->modified_date),
578 "linearized", &(info->linearized),
581 pdf_document_get_page_size(document, 0,
582 &(info->paper_width),
583 &(info->paper_height));
586 info->paper_width = info->paper_width / 72.0f * 25.4f;
587 info->paper_height = info->paper_height / 72.0f * 25.4f;
590 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
591 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
593 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
594 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
596 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
597 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
599 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
600 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
601 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
602 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
604 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
605 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
612 case POPPLER_PAGE_MODE_NONE:
613 info->mode = EV_DOCUMENT_MODE_NONE;
615 case POPPLER_PAGE_MODE_USE_THUMBS:
616 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
618 case POPPLER_PAGE_MODE_USE_OC:
619 info->mode = EV_DOCUMENT_MODE_USE_OC;
621 case POPPLER_PAGE_MODE_FULL_SCREEN:
622 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
624 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
625 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
631 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
632 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
634 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
635 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
637 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
638 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
640 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
641 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
643 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
644 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
646 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
647 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
649 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
650 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
653 info->permissions = 0;
654 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
655 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
657 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
658 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
660 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
661 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
663 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
664 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
667 info->n_pages = ev_document_get_n_pages (document);
669 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
670 /* translators: this is the document security state */
671 info->security = g_strdup (_("Yes"));
673 /* translators: this is the document security state */
674 info->security = g_strdup (_("No"));
681 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
683 PdfDocument *pdf_document = PDF_DOCUMENT (document);
684 PopplerPage *poppler_page;
689 poppler_page = poppler_document_get_page (pdf_document->document, page);
690 g_return_val_if_fail (poppler_page != NULL, NULL);
692 poppler_page_get_size (poppler_page, NULL, &height);
694 r.y1 = height - rect->y2;
696 r.y2 = height - rect->y1;
698 text = poppler_page_get_text (poppler_page, &r);
700 g_object_unref (poppler_page);
706 pdf_document_document_iface_init (EvDocumentIface *iface)
708 iface->save = pdf_document_save;
709 iface->load = pdf_document_load;
710 iface->get_n_pages = pdf_document_get_n_pages;
711 iface->get_page_size = pdf_document_get_page_size;
712 iface->get_page_label = pdf_document_get_page_label;
713 iface->has_attachments = pdf_document_has_attachments;
714 iface->get_attachments = pdf_document_get_attachments;
715 iface->render = pdf_document_render;
716 iface->get_text = pdf_document_get_text;
717 iface->can_get_text = pdf_document_can_get_text;
718 iface->get_info = pdf_document_get_info;
722 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
724 iface->has_document_security = pdf_document_has_document_security;
725 iface->set_password = pdf_document_set_password;
729 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
731 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
734 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
736 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
740 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
743 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
746 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
748 if (pdf_document->font_info == NULL) {
749 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
752 if (pdf_document->fonts_iter) {
753 poppler_fonts_iter_free (pdf_document->fonts_iter);
756 pdf_document->fonts_scanned_pages += n_pages;
758 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
759 &pdf_document->fonts_iter);
761 pdf_document->fonts_scanned_pages = 0;
762 poppler_font_info_free (pdf_document->font_info);
763 pdf_document->font_info = NULL;
770 font_type_to_string (PopplerFontType type)
773 case POPPLER_FONT_TYPE_TYPE1:
775 case POPPLER_FONT_TYPE_TYPE1C:
777 case POPPLER_FONT_TYPE_TYPE3:
779 case POPPLER_FONT_TYPE_TRUETYPE:
780 return _("TrueType");
781 case POPPLER_FONT_TYPE_CID_TYPE0:
782 return _("Type 1 (CID)");
783 case POPPLER_FONT_TYPE_CID_TYPE0C:
784 return _("Type 1C (CID)");
785 case POPPLER_FONT_TYPE_CID_TYPE2:
786 return _("TrueType (CID)");
788 return _("Unknown font type");
793 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
796 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
797 PopplerFontsIter *iter = pdf_document->fonts_iter;
799 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
805 GtkTreeIter list_iter;
808 const char *embedded;
811 name = poppler_fonts_iter_get_name (iter);
817 type = font_type_to_string (
818 poppler_fonts_iter_get_font_type (iter));
820 if (poppler_fonts_iter_is_embedded (iter)) {
821 if (poppler_fonts_iter_is_subset (iter))
822 embedded = _("Embedded subset");
824 embedded = _("Embedded");
826 embedded = _("Not embedded");
829 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
831 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
832 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
833 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
834 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
838 } while (poppler_fonts_iter_next (iter));
842 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
844 iface->fill_model = pdf_document_fonts_fill_model;
845 iface->scan = pdf_document_fonts_scan;
846 iface->get_progress = pdf_document_fonts_get_progress;
850 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
852 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
853 PopplerIndexIter *iter;
855 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
857 iter = poppler_index_iter_new (pdf_document->document);
860 poppler_index_iter_free (iter);
866 ev_link_dest_from_dest (PdfDocument *pdf_document,
869 EvLinkDest *ev_dest = NULL;
870 const char *unimplemented_dest = NULL;
872 g_assert (dest != NULL);
874 switch (dest->type) {
875 case POPPLER_DEST_XYZ: {
876 PopplerPage *poppler_page;
879 poppler_page = poppler_document_get_page (pdf_document->document,
880 MAX (0, dest->page_num - 1));
881 poppler_page_get_size (poppler_page, NULL, &height);
882 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
886 g_object_unref (poppler_page);
889 case POPPLER_DEST_FIT:
890 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
892 case POPPLER_DEST_FITH: {
893 PopplerPage *poppler_page;
896 poppler_page = poppler_document_get_page (pdf_document->document,
897 MAX (0, dest->page_num - 1));
898 poppler_page_get_size (poppler_page, NULL, &height);
899 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
901 g_object_unref (poppler_page);
904 case POPPLER_DEST_FITV:
905 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
908 case POPPLER_DEST_FITR: {
909 PopplerPage *poppler_page;
912 poppler_page = poppler_document_get_page (pdf_document->document,
913 MAX (0, dest->page_num - 1));
914 poppler_page_get_size (poppler_page, NULL, &height);
915 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
917 height - dest->bottom,
920 g_object_unref (poppler_page);
923 case POPPLER_DEST_FITB:
924 unimplemented_dest = "POPPLER_DEST_FITB";
926 case POPPLER_DEST_FITBH:
927 unimplemented_dest = "POPPLER_DEST_FITBH";
929 case POPPLER_DEST_FITBV:
930 unimplemented_dest = "POPPLER_DEST_FITBV";
932 case POPPLER_DEST_NAMED:
933 ev_dest = ev_link_dest_new_named (dest->named_dest);
935 case POPPLER_DEST_UNKNOWN:
936 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
940 if (unimplemented_dest) {
941 g_warning ("Unimplemented named action: %s, please post a "
942 "bug report in Evince bugzilla "
943 "(http://bugzilla.gnome.org) with a testcase.",
948 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
954 ev_link_from_action (PdfDocument *pdf_document,
955 PopplerAction *action)
958 EvLinkAction *ev_action = NULL;
959 const char *unimplemented_action = NULL;
961 switch (action->type) {
962 case POPPLER_ACTION_GOTO_DEST: {
965 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
966 ev_action = ev_link_action_new_dest (dest);
969 case POPPLER_ACTION_GOTO_REMOTE: {
972 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
973 ev_action = ev_link_action_new_remote (dest,
974 action->goto_remote.file_name);
978 case POPPLER_ACTION_LAUNCH:
979 ev_action = ev_link_action_new_launch (action->launch.file_name,
980 action->launch.params);
982 case POPPLER_ACTION_URI:
983 ev_action = ev_link_action_new_external_uri (action->uri.uri);
985 case POPPLER_ACTION_NAMED:
986 ev_action = ev_link_action_new_named (action->named.named_dest);
988 case POPPLER_ACTION_MOVIE:
989 unimplemented_action = "POPPLER_ACTION_MOVIE";
991 case POPPLER_ACTION_UNKNOWN:
992 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
995 if (unimplemented_action) {
996 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
997 unimplemented_action);
1000 link = ev_link_new (action->any.title, ev_action);
1006 build_tree (PdfDocument *pdf_document,
1007 GtkTreeModel *model,
1008 GtkTreeIter *parent,
1009 PopplerIndexIter *iter)
1013 GtkTreeIter tree_iter;
1014 PopplerIndexIter *child;
1015 PopplerAction *action;
1016 EvLink *link = NULL;
1020 action = poppler_index_iter_get_action (iter);
1021 expand = poppler_index_iter_is_open (iter);
1026 switch (action->type) {
1027 case POPPLER_ACTION_GOTO_DEST: {
1028 /* For bookmarks, solve named destinations */
1029 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1031 EvLinkDest *ev_dest = NULL;
1032 EvLinkAction *ev_action;
1034 dest = poppler_document_find_dest (pdf_document->document,
1035 action->goto_dest.dest->named_dest);
1037 link = ev_link_from_action (pdf_document, action);
1041 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1042 poppler_dest_free (dest);
1044 ev_action = ev_link_action_new_dest (ev_dest);
1045 link = ev_link_new (action->any.title, ev_action);
1047 link = ev_link_from_action (pdf_document, action);
1052 link = ev_link_from_action (pdf_document, action);
1057 poppler_action_free (action);
1061 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1062 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1064 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1065 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1066 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1067 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1070 g_free (title_markup);
1071 g_object_unref (link);
1073 child = poppler_index_iter_get_child (iter);
1075 build_tree (pdf_document, model, &tree_iter, child);
1076 poppler_index_iter_free (child);
1077 poppler_action_free (action);
1079 } while (poppler_index_iter_next (iter));
1082 static GtkTreeModel *
1083 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1085 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1086 GtkTreeModel *model = NULL;
1087 PopplerIndexIter *iter;
1089 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1091 iter = poppler_index_iter_new (pdf_document->document);
1092 /* Create the model if we have items*/
1094 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1099 build_tree (pdf_document, model, NULL, iter);
1100 poppler_index_iter_free (iter);
1107 pdf_document_links_get_links (EvDocumentLinks *document_links,
1110 PdfDocument *pdf_document;
1111 PopplerPage *poppler_page;
1112 GList *retval = NULL;
1113 GList *mapping_list;
1117 pdf_document = PDF_DOCUMENT (document_links);
1118 poppler_page = poppler_document_get_page (pdf_document->document,
1120 mapping_list = poppler_page_get_link_mapping (poppler_page);
1121 poppler_page_get_size (poppler_page, NULL, &height);
1123 for (list = mapping_list; list; list = list->next) {
1124 PopplerLinkMapping *link_mapping;
1125 EvLinkMapping *ev_link_mapping;
1127 link_mapping = (PopplerLinkMapping *)list->data;
1128 ev_link_mapping = g_new (EvLinkMapping, 1);
1129 ev_link_mapping->link = ev_link_from_action (pdf_document,
1130 link_mapping->action);
1131 ev_link_mapping->x1 = link_mapping->area.x1;
1132 ev_link_mapping->x2 = link_mapping->area.x2;
1133 /* Invert this for X-style coordinates */
1134 ev_link_mapping->y1 = height - link_mapping->area.y2;
1135 ev_link_mapping->y2 = height - link_mapping->area.y1;
1137 retval = g_list_prepend (retval, ev_link_mapping);
1140 poppler_page_free_link_mapping (mapping_list);
1141 g_object_unref (poppler_page);
1143 return g_list_reverse (retval);
1147 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1148 const gchar *link_name)
1150 PdfDocument *pdf_document;
1152 EvLinkDest *ev_dest = NULL;
1154 pdf_document = PDF_DOCUMENT (document_links);
1155 dest = poppler_document_find_dest (pdf_document->document,
1158 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1159 poppler_dest_free (dest);
1166 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1168 iface->has_document_links = pdf_document_links_has_document_links;
1169 iface->get_links_model = pdf_document_links_get_links_model;
1170 iface->get_links = pdf_document_links_get_links;
1171 iface->find_link_dest = pdf_document_links_find_link_dest;
1175 pdf_document_images_get_images (EvDocumentImages *document_images,
1178 GList *retval = NULL;
1179 PdfDocument *pdf_document;
1180 PopplerPage *poppler_page;
1181 GList *mapping_list;
1184 pdf_document = PDF_DOCUMENT (document_images);
1185 poppler_page = poppler_document_get_page (pdf_document->document, page);
1186 mapping_list = poppler_page_get_image_mapping (poppler_page);
1188 for (list = mapping_list; list; list = list->next) {
1189 PopplerImageMapping *image_mapping;
1190 EvImageMapping *ev_image_mapping;
1192 image_mapping = (PopplerImageMapping *)list->data;
1194 ev_image_mapping = g_new (EvImageMapping, 1);
1196 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1197 ev_image_mapping->x1 = image_mapping->area.x1;
1198 ev_image_mapping->x2 = image_mapping->area.x2;
1199 ev_image_mapping->y1 = image_mapping->area.y1;
1200 ev_image_mapping->y2 = image_mapping->area.y2;
1202 retval = g_list_prepend (retval, ev_image_mapping);
1205 poppler_page_free_image_mapping (mapping_list);
1206 g_object_unref (poppler_page);
1212 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1214 iface->get_images = pdf_document_images_get_images;
1218 make_thumbnail_for_page (PdfDocument *pdf_document,
1219 PopplerPage *poppler_page,
1220 EvRenderContext *rc)
1225 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1226 rc, &width, &height);
1228 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1230 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1232 ev_document_fc_mutex_lock ();
1233 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1235 rc->scale, rc->rotation, pixbuf);
1236 ev_document_fc_mutex_unlock ();
1242 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1243 EvRenderContext *rc,
1246 PdfDocument *pdf_document;
1247 PopplerPage *poppler_page;
1249 GdkPixbuf *border_pixbuf;
1251 pdf_document = PDF_DOCUMENT (document_thumbnails);
1253 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1254 g_return_val_if_fail (poppler_page != NULL, NULL);
1256 pixbuf = poppler_page_get_thumbnail (poppler_page);
1258 /* There is no provided thumbnail. We need to make one. */
1259 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1263 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1264 g_object_unref (pixbuf);
1265 pixbuf = border_pixbuf;
1268 g_object_unref (poppler_page);
1274 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1275 EvRenderContext *rc,
1279 PdfDocument *pdf_document;
1280 PopplerPage *poppler_page;
1283 pdf_document = PDF_DOCUMENT (document_thumbnails);
1284 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1286 g_return_if_fail (poppler_page != NULL);
1288 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1291 double page_width, page_height;
1293 poppler_page_get_size (poppler_page, &page_width, &page_height);
1295 *width = (gint) MAX (page_width * rc->scale, 1);
1296 *height = (gint) MAX (page_height * rc->scale, 1);
1299 if (rc->rotation == 90 || rc->rotation == 270) {
1307 g_object_unref (poppler_page);
1311 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1313 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1314 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1319 pdf_document_search_idle_callback (void *data)
1321 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1322 PdfDocument *pdf_document = search->document;
1327 page = poppler_document_get_page (search->document->document,
1328 search->search_page);
1330 ev_document_doc_mutex_lock ();
1331 matches = poppler_page_find_text (page, search->text);
1332 ev_document_doc_mutex_unlock ();
1334 g_object_unref (page);
1336 search->pages[search->search_page] = matches;
1337 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1338 search->search_page);
1340 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1341 search->search_page += 1;
1342 if (search->search_page == n_pages) {
1344 search->search_page = 0;
1347 if (search->search_page != search->start_page) {
1352 search->idle = 0; /* will return FALSE to remove */
1357 static PdfDocumentSearch *
1358 pdf_document_search_new (PdfDocument *pdf_document,
1362 PdfDocumentSearch *search;
1366 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1368 search = g_new0 (PdfDocumentSearch, 1);
1370 search->text = g_strdup (text);
1371 search->pages = g_new0 (GList *, n_pages);
1372 search->document = pdf_document;
1374 /* We add at low priority so the progress bar repaints */
1375 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1376 pdf_document_search_idle_callback,
1380 search->start_page = start_page;
1381 search->search_page = start_page;
1387 pdf_document_find_begin (EvDocumentFind *document,
1389 const char *search_string,
1390 gboolean case_sensitive)
1392 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1394 /* FIXME handle case_sensitive (right now XPDF
1395 * code is always case insensitive for ASCII
1396 * and case sensitive for all other languaages)
1399 if (pdf_document->search &&
1400 strcmp (search_string, pdf_document->search->text) == 0)
1403 if (pdf_document->search)
1404 pdf_document_search_free (pdf_document->search);
1406 pdf_document->search = pdf_document_search_new (pdf_document,
1412 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1414 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1417 return g_list_length (search->pages[page]);
1424 pdf_document_find_get_result (EvDocumentFind *document_find,
1427 EvRectangle *rectangle)
1429 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1430 PdfDocumentSearch *search = pdf_document->search;
1431 PopplerPage *poppler_page;
1432 PopplerRectangle *r;
1438 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1443 poppler_page = poppler_document_get_page (pdf_document->document, page);
1444 poppler_page_get_size (poppler_page, NULL, &height);
1445 rectangle->x1 = r->x1;
1446 rectangle->y1 = height - r->y2;
1447 rectangle->x2 = r->x2;
1448 rectangle->y2 = height - r->y1;
1449 g_object_unref (poppler_page);
1455 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1458 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1460 return search && search->pages[page] != NULL;
1464 pdf_document_find_get_progress (EvDocumentFind *document_find)
1466 PdfDocumentSearch *search;
1467 int n_pages, pages_done;
1469 search = PDF_DOCUMENT (document_find)->search;
1471 if (search == NULL) {
1475 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1476 if (search->search_page > search->start_page) {
1477 pages_done = search->search_page - search->start_page + 1;
1478 } else if (search->search_page == search->start_page) {
1479 pages_done = n_pages;
1481 pages_done = n_pages - search->start_page + search->search_page;
1484 return pages_done / (double) n_pages;
1488 pdf_document_find_cancel (EvDocumentFind *document)
1490 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1492 if (pdf_document->search) {
1493 pdf_document_search_free (pdf_document->search);
1494 pdf_document->search = NULL;
1499 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1501 iface->begin = pdf_document_find_begin;
1502 iface->get_n_results = pdf_document_find_get_n_results;
1503 iface->get_result = pdf_document_find_get_result;
1504 iface->page_has_results = pdf_document_find_page_has_results;
1505 iface->get_progress = pdf_document_find_get_progress;
1506 iface->cancel = pdf_document_find_cancel;
1509 static const gboolean supported_formats[] = {
1510 TRUE, /* EV_FILE_FORMAT_PS */
1511 #ifdef HAVE_CAIRO_PDF
1512 #ifdef HAVE_POPPLER_PAGE_RENDER
1513 TRUE, /* EV_FILE_FORMAT_PDF */
1515 FALSE, /* EV_FILE_FORMAT_PDF */
1521 pdf_print_context_free (PdfPrintContext *ctx)
1527 poppler_ps_file_free (ctx->ps_file);
1528 ctx->ps_file = NULL;
1530 #ifdef HAVE_CAIRO_PDF
1531 if (ctx->pdf_cairo) {
1532 cairo_destroy (ctx->pdf_cairo);
1533 ctx->pdf_cairo = NULL;
1540 pdf_document_file_exporter_format_supported (EvFileExporter *exporter,
1541 EvFileExporterFormat format)
1543 return supported_formats[format];
1547 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1548 EvFileExporterFormat format,
1549 const char *filename,
1556 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1557 PdfPrintContext *ctx;
1559 if (pdf_document->print_ctx)
1560 pdf_print_context_free (pdf_document->print_ctx);
1561 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1562 ctx = pdf_document->print_ctx;
1563 ctx->format = format;
1566 case EV_FILE_FORMAT_PS:
1567 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1568 filename, first_page,
1569 last_page - first_page + 1);
1570 poppler_ps_file_set_paper_size (ctx->ps_file, width, height);
1571 poppler_ps_file_set_duplex (ctx->ps_file, duplex);
1574 case EV_FILE_FORMAT_PDF: {
1575 #ifdef HAVE_CAIRO_PDF
1576 cairo_surface_t *surface;
1578 surface = cairo_pdf_surface_create (filename, width, height);
1579 ctx->pdf_cairo = cairo_create (surface);
1580 cairo_surface_destroy (surface);
1585 g_assert_not_reached ();
1590 pdf_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1592 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1593 PdfPrintContext *ctx = pdf_document->print_ctx;
1594 PopplerPage *poppler_page;
1596 g_return_if_fail (pdf_document->print_ctx != NULL);
1598 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1600 switch (ctx->format) {
1601 case EV_FILE_FORMAT_PS:
1602 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1604 case EV_FILE_FORMAT_PDF:
1605 #ifdef HAVE_CAIRO_PDF
1606 cairo_save (ctx->pdf_cairo);
1608 #ifdef HAVE_POPPLER_PAGE_RENDER
1609 poppler_page_render (poppler_page, ctx->pdf_cairo);
1611 #ifdef HAVE_CAIRO_PDF
1612 cairo_show_page (ctx->pdf_cairo);
1613 cairo_restore (ctx->pdf_cairo);
1617 g_assert_not_reached ();
1620 g_object_unref (poppler_page);
1624 pdf_document_file_exporter_end (EvFileExporter *exporter)
1626 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1628 pdf_print_context_free (pdf_document->print_ctx);
1629 pdf_document->print_ctx = NULL;
1633 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1635 iface->format_supported = pdf_document_file_exporter_format_supported;
1636 iface->begin = pdf_document_file_exporter_begin;
1637 iface->do_page = pdf_document_file_exporter_do_page;
1638 iface->end = pdf_document_file_exporter_end;
1642 pdf_selection_render_selection (EvSelection *selection,
1643 EvRenderContext *rc,
1644 cairo_surface_t **surface,
1645 EvRectangle *points,
1646 EvRectangle *old_points,
1650 PdfDocument *pdf_document;
1651 double width_points, height_points;
1654 pdf_document = PDF_DOCUMENT (selection);
1655 set_rc_data (pdf_document, rc);
1657 poppler_page_get_size (POPPLER_PAGE (rc->data),
1658 &width_points, &height_points);
1659 width = (int) ((width_points * rc->scale) + 0.5);
1660 height = (int) ((height_points * rc->scale) + 0.5);
1663 #ifdef HAVE_POPPLER_PAGE_RENDER
1666 if (*surface == NULL) {
1667 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1672 cr = cairo_create (*surface);
1673 cairo_scale (cr, rc->scale, rc->scale);
1674 cairo_surface_set_device_offset (*surface, 0, 0);
1675 memset (cairo_image_surface_get_data (*surface), 0x00,
1676 cairo_image_surface_get_height (*surface) *
1677 cairo_image_surface_get_stride (*surface));
1678 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1680 (PopplerRectangle *)points,
1681 (PopplerRectangle *)old_points,
1682 POPPLER_SELECTION_NORMAL, /* SelectionStyle */
1686 #else /* HAVE_POPPLER_PAGE_RENDER */
1689 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1693 poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1694 rc->scale, rc->rotation, pixbuf,
1695 (PopplerRectangle *)points,
1696 (PopplerRectangle *)old_points,
1697 POPPLER_SELECTION_NORMAL, /* SelectionStyle */
1701 cairo_surface_destroy (*surface);
1702 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1703 g_object_unref (pixbuf);
1704 #endif /* HAVE_POPPLER_PAGE_RENDER */
1709 pdf_selection_get_selection_region (EvSelection *selection,
1710 EvRenderContext *rc,
1711 EvRectangle *points)
1713 PdfDocument *pdf_document;
1716 pdf_document = PDF_DOCUMENT (selection);
1718 set_rc_data (pdf_document, rc);
1720 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data,
1722 (PopplerRectangle *) points);
1727 pdf_selection_get_selection_map (EvSelection *selection,
1728 EvRenderContext *rc)
1730 PdfDocument *pdf_document;
1731 PopplerPage *poppler_page;
1732 PopplerRectangle points;
1735 pdf_document = PDF_DOCUMENT (selection);
1736 poppler_page = poppler_document_get_page (pdf_document->document,
1741 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1742 retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1743 g_object_unref (poppler_page);
1749 pdf_selection_iface_init (EvSelectionIface *iface)
1751 iface->render_selection = pdf_selection_render_selection;
1752 iface->get_selection_region = pdf_selection_get_selection_region;
1753 iface->get_selection_map = pdf_selection_get_selection_map;
1756 /* Page Transitions */
1758 pdf_document_get_page_duration (EvDocumentTransition *trans,
1761 PdfDocument *pdf_document;
1762 PopplerPage *poppler_page;
1763 gdouble duration = -1;
1765 pdf_document = PDF_DOCUMENT (trans);
1766 poppler_page = poppler_document_get_page (pdf_document->document, page);
1770 duration = poppler_page_get_duration (poppler_page);
1771 g_object_unref (poppler_page);
1777 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1779 iface->get_page_duration = pdf_document_get_page_duration;
1783 pdf_document_new (void)
1785 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1790 pdf_document_get_crop_box (EvDocument *document,
1794 PdfDocument *pdf_document;
1795 PopplerPage *poppler_page;
1796 PopplerRectangle poppler_rect;
1798 pdf_document = PDF_DOCUMENT (document);
1799 poppler_page = poppler_document_get_page (pdf_document->document, page);
1800 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1801 rect->x1 = poppler_rect.x1;
1802 rect->x2 = poppler_rect.x2;
1803 rect->y1 = poppler_rect.y1;
1804 rect->y2 = poppler_rect.y2;
1808 static EvFormField *
1809 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1811 EvFormField *ev_field = NULL;
1814 gboolean is_read_only;
1816 id = poppler_form_field_get_id (poppler_field);
1817 font_size = poppler_form_field_get_font_size (poppler_field);
1818 is_read_only = poppler_form_field_is_read_only (poppler_field);
1820 switch (poppler_form_field_get_field_type (poppler_field)) {
1821 case POPPLER_FORM_FIELD_TEXT: {
1822 EvFormFieldText *field_text;
1823 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1825 switch (poppler_form_field_text_get_text_type (poppler_field)) {
1826 case POPPLER_FORM_TEXT_NORMAL:
1827 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1829 case POPPLER_FORM_TEXT_MULTILINE:
1830 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
1832 case POPPLER_FORM_TEXT_PASSWORD:
1833 ev_text_type = EV_FORM_FIELD_TEXT_PASSWORD;
1835 case POPPLER_FORM_TEXT_FILE_SELECT:
1836 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
1840 ev_field = ev_form_field_text_new (id, ev_text_type);
1841 field_text = EV_FORM_FIELD_TEXT (ev_field);
1843 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
1844 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
1845 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
1847 field_text->text = poppler_form_field_text_get_text (poppler_field);
1851 case POPPLER_FORM_FIELD_BUTTON: {
1852 EvFormFieldButton *field_button;
1853 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
1855 switch (poppler_form_field_button_get_button_type (poppler_field)) {
1856 case POPPLER_FORM_BUTTON_PUSH:
1857 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
1859 case POPPLER_FORM_BUTTON_CHECK:
1860 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
1862 case POPPLER_FORM_BUTTON_RADIO:
1863 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
1867 ev_field = ev_form_field_button_new (id, ev_button_type);
1868 field_button = EV_FORM_FIELD_BUTTON (ev_field);
1870 field_button->state = poppler_form_field_button_get_state (poppler_field);
1873 case POPPLER_FORM_FIELD_CHOICE: {
1874 EvFormFieldChoice *field_choice;
1875 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
1877 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
1878 case POPPLER_FORM_CHOICE_COMBO:
1879 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
1881 case EV_FORM_FIELD_CHOICE_LIST:
1882 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
1886 ev_field = ev_form_field_choice_new (id, ev_choice_type);
1887 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
1889 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
1890 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
1891 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
1892 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
1894 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
1895 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
1896 if (field_choice->is_editable)
1897 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
1900 case POPPLER_FORM_FIELD_SIGNATURE:
1902 ev_field = ev_form_field_signature_new (id);
1904 case POPPLER_FORM_FIELD_UNKNOWN:
1908 ev_field->font_size = font_size;
1909 ev_field->is_read_only = is_read_only;
1915 pdf_document_forms_get_form_fields (EvDocumentForms *document,
1918 PdfDocument *pdf_document;
1919 PopplerPage *poppler_page;
1920 GList *retval = NULL;
1926 pdf_document = PDF_DOCUMENT (document);
1927 poppler_page = poppler_document_get_page (pdf_document->document, page);
1928 fields = poppler_page_get_form_field_mapping (poppler_page);
1929 poppler_page_get_size (poppler_page, NULL, &height);
1931 for (list = fields; list; list = list->next) {
1932 PopplerFormFieldMapping *mapping;
1933 EvFormFieldMapping *field_mapping;
1935 mapping = (PopplerFormFieldMapping *)list->data;
1937 field_mapping = g_new0 (EvFormFieldMapping, 1);
1938 field_mapping->x1 = mapping->area.x1;
1939 field_mapping->x2 = mapping->area.x2;
1940 field_mapping->y1 = height - mapping->area.y2;
1941 field_mapping->y2 = height - mapping->area.y1;
1942 field_mapping->field = ev_form_field_from_poppler_field (mapping->field);
1943 field_mapping->field->page = page;
1945 retval = g_list_prepend (retval, field_mapping);
1947 poppler_page_free_form_field_mapping (fields);
1948 g_object_unref (poppler_page);
1950 return g_list_reverse (retval);
1954 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
1958 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1959 PopplerFormField *poppler_field;
1962 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
1966 text = poppler_form_field_text_get_text (poppler_field);
1967 g_object_unref (poppler_field);
1973 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
1977 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1978 PopplerFormField *poppler_field;
1980 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
1983 poppler_form_field_text_set_text (poppler_field, text);
1984 g_object_unref (poppler_field);
1988 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
1992 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1993 PopplerFormField *poppler_field;
1995 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
1999 poppler_form_field_button_set_state (poppler_field, state);
2000 g_object_unref (poppler_field);
2004 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2007 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2008 PopplerFormField *poppler_field;
2011 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2015 state = poppler_form_field_button_get_state (poppler_field);
2016 g_object_unref (poppler_field);
2022 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2026 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2027 PopplerFormField *poppler_field;
2030 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2034 text = poppler_form_field_choice_get_item (poppler_field, index);
2035 g_object_unref (poppler_field);
2041 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2044 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2045 PopplerFormField *poppler_field;
2048 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2052 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2053 g_object_unref (poppler_field);
2059 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2063 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2064 PopplerFormField *poppler_field;
2067 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2071 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2072 g_object_unref (poppler_field);
2078 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2082 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2083 PopplerFormField *poppler_field;
2085 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2089 poppler_form_field_choice_select_item (poppler_field, index);
2090 g_object_unref (poppler_field);
2094 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2098 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2099 PopplerFormField *poppler_field;
2101 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2105 poppler_form_field_choice_toggle_item (poppler_field, index);
2106 g_object_unref (poppler_field);
2110 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2113 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2114 PopplerFormField *poppler_field;
2116 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2120 poppler_form_field_choice_unselect_all (poppler_field);
2121 g_object_unref (poppler_field);
2125 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2129 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2130 PopplerFormField *poppler_field;
2132 poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2136 poppler_form_field_choice_set_text (poppler_field, text);
2137 g_object_unref (poppler_field);
2141 pdf_document_forms_form_field_choice_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_choice_get_text (poppler_field);
2153 g_object_unref (poppler_field);
2159 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2161 iface->get_form_fields = pdf_document_forms_get_form_fields;
2162 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2163 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2164 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2165 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2166 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2167 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2168 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2169 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2170 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2171 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2172 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2173 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2175 #endif /* HAVE_FORMS */