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.
24 #include <poppler-document.h>
25 #include <poppler-page.h>
26 #include <glib/gi18n.h>
28 #include "ev-poppler.h"
29 #include "ev-ps-exporter.h"
30 #include "ev-document-find.h"
31 #include "ev-document-misc.h"
32 #include "ev-document-links.h"
33 #include "ev-document-fonts.h"
34 #include "ev-document-security.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-selection.h"
37 #include "ev-attachment.h"
40 PdfDocument *document;
48 struct _PdfDocumentClass
50 GObjectClass parent_class;
55 GObject parent_instance;
57 PopplerDocument *document;
58 PopplerPSFile *ps_file;
61 PopplerFontInfo *font_info;
62 PopplerFontsIter *fonts_iter;
63 int fonts_scanned_pages;
65 PdfDocumentSearch *search;
68 static void pdf_document_document_iface_init (EvDocumentIface *iface);
69 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
70 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
71 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
72 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
73 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
74 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface);
75 static void pdf_selection_iface_init (EvSelectionIface *iface);
76 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
81 static int pdf_document_get_n_pages (EvDocument *document);
83 static EvLinkDest *ev_link_dest_from_dest (PopplerDest *dest);
84 static EvLink *ev_link_from_action (PopplerAction *action);
85 static void pdf_document_search_free (PdfDocumentSearch *search);
88 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
90 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
91 pdf_document_document_iface_init);
92 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
93 pdf_document_security_iface_init);
94 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
95 pdf_document_document_thumbnails_iface_init);
96 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
97 pdf_document_document_links_iface_init);
98 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
99 pdf_document_document_fonts_iface_init);
100 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
101 pdf_document_find_iface_init);
102 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
103 pdf_document_ps_exporter_iface_init);
104 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
105 pdf_selection_iface_init);
110 set_rc_data (PdfDocument *pdf_document,
113 if (rc->data == NULL) {
114 rc->data = poppler_document_get_page (pdf_document->document,
116 rc->destroy = g_object_unref;
118 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
123 pdf_document_search_free (PdfDocumentSearch *search)
125 PdfDocument *pdf_document = search->document;
129 if (search->idle != 0)
130 g_source_remove (search->idle);
132 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
133 for (i = 0; i < n_pages; i++) {
134 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
135 g_list_free (search->pages[i]);
138 g_free (search->text);
142 pdf_document_dispose (GObject *object)
144 PdfDocument *pdf_document = PDF_DOCUMENT(object);
146 if (pdf_document->search) {
147 pdf_document_search_free (pdf_document->search);
148 pdf_document->search = NULL;
151 if (pdf_document->document) {
152 g_object_unref (pdf_document->document);
155 if (pdf_document->font_info) {
156 poppler_font_info_free (pdf_document->font_info);
159 if (pdf_document->fonts_iter) {
160 poppler_fonts_iter_free (pdf_document->fonts_iter);
165 pdf_document_class_init (PdfDocumentClass *klass)
167 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
169 g_object_class->dispose = pdf_document_dispose;
173 pdf_document_init (PdfDocument *pdf_document)
175 pdf_document->password = NULL;
179 convert_error (GError *poppler_error,
182 if (poppler_error == NULL)
185 if (poppler_error->domain == POPPLER_ERROR) {
186 /* convert poppler errors into EvDocument errors */
187 gint code = EV_DOCUMENT_ERROR_INVALID;
188 if (poppler_error->code == POPPLER_ERROR_INVALID)
189 code = EV_DOCUMENT_ERROR_INVALID;
190 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
191 code = EV_DOCUMENT_ERROR_ENCRYPTED;
197 poppler_error->message,
200 g_propagate_error (error, poppler_error);
207 pdf_document_save (EvDocument *document,
212 GError *poppler_error = NULL;
214 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
218 convert_error (poppler_error, error);
224 pdf_document_load (EvDocument *document,
228 GError *poppler_error = NULL;
229 PdfDocument *pdf_document = PDF_DOCUMENT (document);
231 pdf_document->document =
232 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
234 if (pdf_document->document == NULL) {
235 convert_error (poppler_error, error);
243 pdf_document_get_n_pages (EvDocument *document)
245 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
249 pdf_document_get_page_size (EvDocument *document,
254 PdfDocument *pdf_document = PDF_DOCUMENT (document);
255 PopplerPage *poppler_page;
257 poppler_page = poppler_document_get_page (pdf_document->document, page);
258 poppler_page_get_size (poppler_page, width, height);
259 g_object_unref (poppler_page);
263 pdf_document_get_page_label (EvDocument *document,
266 PopplerPage *poppler_page;
269 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
272 g_object_get (G_OBJECT (poppler_page),
275 g_object_unref (poppler_page);
281 pdf_document_has_attachments (EvDocument *document)
283 PdfDocument *pdf_document;
285 pdf_document = PDF_DOCUMENT (document);
287 return poppler_document_has_attachments (pdf_document->document);
290 struct SaveToBufferData {
296 attachment_save_to_buffer_callback (const gchar *buf,
301 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
305 if (sdata->len + count > sdata->max) {
306 new_max = MAX (sdata->max * 2, sdata->len + count);
307 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
309 sdata->buffer = new_buffer;
310 sdata->max = new_max;
313 memcpy (sdata->buffer + sdata->len, buf, count);
320 attachment_save_to_buffer (PopplerAttachment *attachment,
325 static const gint initial_max = 1024;
326 struct SaveToBufferData sdata;
331 sdata.buffer = (gchar *) g_malloc (initial_max);
332 sdata.max = initial_max;
335 if (! poppler_attachment_save_to_callback (attachment,
336 attachment_save_to_buffer_callback,
339 g_free (sdata.buffer);
343 *buffer = sdata.buffer;
344 *buffer_size = sdata.len;
350 pdf_document_get_attachments (EvDocument *document)
352 PdfDocument *pdf_document;
355 GList *retval = NULL;
357 pdf_document = PDF_DOCUMENT (document);
359 if (!pdf_document_has_attachments (document))
362 attachments = poppler_document_get_attachments (pdf_document->document);
364 for (list = attachments; list; list = list->next) {
365 PopplerAttachment *attachment;
366 EvAttachment *ev_attachment;
369 GError *error = NULL;
371 attachment = (PopplerAttachment *) list->data;
373 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
374 ev_attachment = ev_attachment_new (attachment->name,
375 attachment->description,
380 retval = g_list_prepend (retval, ev_attachment);
383 g_warning ("%s", error->message);
384 g_error_free (error);
390 g_object_unref (attachment);
393 return g_list_reverse (retval);
397 pdf_document_render_pixbuf (EvDocument *document,
400 PdfDocument *pdf_document;
402 double width_points, height_points;
405 pdf_document = PDF_DOCUMENT (document);
407 set_rc_data (pdf_document, rc);
409 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
411 if (rc->rotation == 90 || rc->rotation == 270) {
412 width = (int) ((height_points * rc->scale) + 0.5);
413 height = (int) ((width_points * rc->scale) + 0.5);
415 width = (int) ((width_points * rc->scale) + 0.5);
416 height = (int) ((height_points * rc->scale) + 0.5);
419 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
423 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
434 /* EvDocumentSecurity */
437 pdf_document_has_document_security (EvDocumentSecurity *document_security)
439 /* FIXME: do we really need to have this? */
444 pdf_document_set_password (EvDocumentSecurity *document_security,
445 const char *password)
447 PdfDocument *document = PDF_DOCUMENT (document_security);
449 if (document->password)
450 g_free (document->password);
452 document->password = g_strdup (password);
456 pdf_document_can_get_text (EvDocument *document)
461 static EvDocumentInfo *
462 pdf_document_get_info (EvDocument *document)
464 EvDocumentInfo *info;
465 PopplerPageLayout layout;
466 PopplerPageMode mode;
467 PopplerViewerPreferences view_prefs;
468 PopplerPermissions permissions;
470 info = g_new0 (EvDocumentInfo, 1);
472 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
473 EV_DOCUMENT_INFO_FORMAT |
474 EV_DOCUMENT_INFO_AUTHOR |
475 EV_DOCUMENT_INFO_SUBJECT |
476 EV_DOCUMENT_INFO_KEYWORDS |
477 EV_DOCUMENT_INFO_LAYOUT |
478 EV_DOCUMENT_INFO_START_MODE |
479 EV_DOCUMENT_INFO_PERMISSIONS |
480 EV_DOCUMENT_INFO_UI_HINTS |
481 EV_DOCUMENT_INFO_CREATOR |
482 EV_DOCUMENT_INFO_PRODUCER |
483 EV_DOCUMENT_INFO_CREATION_DATE |
484 EV_DOCUMENT_INFO_MOD_DATE |
485 EV_DOCUMENT_INFO_LINEARIZED |
486 EV_DOCUMENT_INFO_N_PAGES |
487 EV_DOCUMENT_INFO_SECURITY;
490 g_object_get (PDF_DOCUMENT (document)->document,
491 "title", &(info->title),
492 "format", &(info->format),
493 "author", &(info->author),
494 "subject", &(info->subject),
495 "keywords", &(info->keywords),
497 "page-layout", &layout,
498 "viewer-preferences", &view_prefs,
499 "permissions", &permissions,
500 "creator", &(info->creator),
501 "producer", &(info->producer),
502 "creation-date", &(info->creation_date),
503 "mod-date", &(info->modified_date),
504 "linearized", &(info->linearized),
508 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
509 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
511 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
512 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
514 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
515 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
517 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
518 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
519 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
520 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
522 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
523 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
530 case POPPLER_PAGE_MODE_NONE:
531 info->mode = EV_DOCUMENT_MODE_NONE;
533 case POPPLER_PAGE_MODE_USE_THUMBS:
534 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
536 case POPPLER_PAGE_MODE_USE_OC:
537 info->mode = EV_DOCUMENT_MODE_USE_OC;
539 case POPPLER_PAGE_MODE_FULL_SCREEN:
540 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
542 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
543 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
549 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
550 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
552 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
553 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
555 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
556 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
558 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
559 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
561 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
562 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
564 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
565 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
567 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
568 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
571 info->permissions = 0;
572 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
573 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
575 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
576 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
578 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
579 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
581 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
582 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
585 info->n_pages = ev_document_get_n_pages (document);
587 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
588 /* translators: this is the document security state */
589 info->security = g_strdup (_("Yes"));
591 /* translators: this is the document security state */
592 info->security = g_strdup (_("No"));
599 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
601 PdfDocument *pdf_document = PDF_DOCUMENT (document);
602 PopplerPage *poppler_page;
607 poppler_page = poppler_document_get_page (pdf_document->document, page);
608 g_return_val_if_fail (poppler_page != NULL, NULL);
610 poppler_page_get_size (poppler_page, NULL, &height);
612 r.y1 = height - rect->y2;
614 r.y2 = height - rect->y1;
616 text = poppler_page_get_text (poppler_page, &r);
618 g_object_unref (poppler_page);
624 pdf_document_document_iface_init (EvDocumentIface *iface)
626 iface->save = pdf_document_save;
627 iface->load = pdf_document_load;
628 iface->get_n_pages = pdf_document_get_n_pages;
629 iface->get_page_size = pdf_document_get_page_size;
630 iface->get_page_label = pdf_document_get_page_label;
631 iface->has_attachments = pdf_document_has_attachments;
632 iface->get_attachments = pdf_document_get_attachments;
633 iface->render_pixbuf = pdf_document_render_pixbuf;
634 iface->get_text = pdf_document_get_text;
635 iface->can_get_text = pdf_document_can_get_text;
636 iface->get_info = pdf_document_get_info;
640 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
642 iface->has_document_security = pdf_document_has_document_security;
643 iface->set_password = pdf_document_set_password;
647 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
649 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
652 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
654 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
658 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
661 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
664 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
666 if (pdf_document->font_info == NULL) {
667 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
670 if (pdf_document->fonts_iter) {
671 poppler_fonts_iter_free (pdf_document->fonts_iter);
674 pdf_document->fonts_scanned_pages += n_pages;
676 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
677 &pdf_document->fonts_iter);
679 pdf_document->fonts_scanned_pages = 0;
680 poppler_font_info_free (pdf_document->font_info);
681 pdf_document->font_info = NULL;
688 font_type_to_string (PopplerFontType type)
691 case POPPLER_FONT_TYPE_TYPE1:
693 case POPPLER_FONT_TYPE_TYPE1C:
695 case POPPLER_FONT_TYPE_TYPE3:
697 case POPPLER_FONT_TYPE_TRUETYPE:
698 return _("TrueType");
699 case POPPLER_FONT_TYPE_CID_TYPE0:
700 return _("Type 1 (CID)");
701 case POPPLER_FONT_TYPE_CID_TYPE0C:
702 return _("Type 1C (CID)");
703 case POPPLER_FONT_TYPE_CID_TYPE2:
704 return _("TrueType (CID)");
706 return _("Unknown font type");
711 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
714 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
715 PopplerFontsIter *iter = pdf_document->fonts_iter;
717 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
723 GtkTreeIter list_iter;
726 const char *embedded;
729 name = poppler_fonts_iter_get_name (iter);
735 type = font_type_to_string (
736 poppler_fonts_iter_get_font_type (iter));
738 if (poppler_fonts_iter_is_embedded (iter)) {
739 if (poppler_fonts_iter_is_subset (iter))
740 embedded = _("Embedded subset");
742 embedded = _("Embedded");
744 embedded = _("Not embedded");
747 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
749 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
750 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
751 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
752 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
756 } while (poppler_fonts_iter_next (iter));
760 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
762 iface->fill_model = pdf_document_fonts_fill_model;
763 iface->scan = pdf_document_fonts_scan;
764 iface->get_progress = pdf_document_fonts_get_progress;
768 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
770 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
771 PopplerIndexIter *iter;
773 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
775 iter = poppler_index_iter_new (pdf_document->document);
778 poppler_index_iter_free (iter);
784 ev_link_dest_from_dest (PopplerDest *dest)
786 EvLinkDest *ev_dest = NULL;
787 const char *unimplemented_dest = NULL;
789 g_assert (dest != NULL);
791 switch (dest->type) {
792 case POPPLER_DEST_XYZ:
793 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
798 case POPPLER_DEST_FIT:
799 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
801 case POPPLER_DEST_FITH:
802 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
805 case POPPLER_DEST_FITV:
806 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
809 case POPPLER_DEST_FITR:
810 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
816 case POPPLER_DEST_FITB:
817 unimplemented_dest = "POPPLER_DEST_FITB";
819 case POPPLER_DEST_FITBH:
820 unimplemented_dest = "POPPLER_DEST_FITBH";
822 case POPPLER_DEST_FITBV:
823 unimplemented_dest = "POPPLER_DEST_FITBV";
825 case POPPLER_DEST_NAMED:
826 ev_dest = ev_link_dest_new_named (dest->named_dest);
828 case POPPLER_DEST_UNKNOWN:
829 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
833 if (unimplemented_dest) {
834 g_warning ("Unimplemented destination: %s, please post a bug report with a testcase.",
839 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
845 ev_link_from_action (PopplerAction *action)
848 EvLinkAction *ev_action = NULL;
849 const char *unimplemented_action = NULL;
851 switch (action->type) {
852 case POPPLER_ACTION_GOTO_DEST: {
855 dest = ev_link_dest_from_dest (action->goto_dest.dest);
856 ev_action = ev_link_action_new_dest (dest);
859 case POPPLER_ACTION_GOTO_REMOTE: {
862 dest = ev_link_dest_from_dest (action->goto_remote.dest);
863 ev_action = ev_link_action_new_remote (dest,
864 action->goto_remote.file_name);
868 case POPPLER_ACTION_LAUNCH:
869 ev_action = ev_link_action_new_launch (action->launch.file_name,
870 action->launch.params);
872 case POPPLER_ACTION_URI:
873 ev_action = ev_link_action_new_external_uri (action->uri.uri);
875 case POPPLER_ACTION_NAMED:
876 ev_action = ev_link_action_new_named (action->named.named_dest);
878 case POPPLER_ACTION_MOVIE:
879 unimplemented_action = "POPPLER_ACTION_MOVIE";
881 case POPPLER_ACTION_UNKNOWN:
882 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
885 if (unimplemented_action) {
886 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
887 unimplemented_action);
890 link = ev_link_new (action->any.title, ev_action);
896 build_tree (PdfDocument *pdf_document,
899 PopplerIndexIter *iter)
903 GtkTreeIter tree_iter;
904 PopplerIndexIter *child;
905 PopplerAction *action;
910 action = poppler_index_iter_get_action (iter);
911 expand = poppler_index_iter_is_open (iter);
916 switch (action->type) {
917 case POPPLER_ACTION_GOTO_DEST: {
918 /* For bookmarks, solve named destinations */
919 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
921 EvLinkDest *ev_dest = NULL;
922 EvLinkAction *ev_action;
924 dest = poppler_document_find_dest (pdf_document->document,
925 action->goto_dest.dest->named_dest);
927 link = ev_link_from_action (action);
931 ev_dest = ev_link_dest_from_dest (dest);
932 poppler_dest_free (dest);
934 ev_action = ev_link_action_new_dest (ev_dest);
935 link = ev_link_new (action->any.title, ev_action);
937 link = ev_link_from_action (action);
942 link = ev_link_from_action (action);
947 poppler_action_free (action);
951 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
952 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
954 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
955 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
956 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
957 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
960 g_free (title_markup);
961 g_object_unref (link);
963 child = poppler_index_iter_get_child (iter);
965 build_tree (pdf_document, model, &tree_iter, child);
966 poppler_index_iter_free (child);
968 } while (poppler_index_iter_next (iter));
971 static GtkTreeModel *
972 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
974 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
975 GtkTreeModel *model = NULL;
976 PopplerIndexIter *iter;
978 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
980 iter = poppler_index_iter_new (pdf_document->document);
981 /* Create the model if we have items*/
983 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
987 build_tree (pdf_document, model, NULL, iter);
988 poppler_index_iter_free (iter);
995 pdf_document_links_get_links (EvDocumentLinks *document_links,
998 PdfDocument *pdf_document;
999 PopplerPage *poppler_page;
1000 GList *retval = NULL;
1001 GList *mapping_list;
1005 pdf_document = PDF_DOCUMENT (document_links);
1006 poppler_page = poppler_document_get_page (pdf_document->document,
1008 mapping_list = poppler_page_get_link_mapping (poppler_page);
1009 poppler_page_get_size (poppler_page, NULL, &height);
1011 for (list = mapping_list; list; list = list->next) {
1012 PopplerLinkMapping *link_mapping;
1013 EvLinkMapping *ev_link_mapping;
1015 link_mapping = (PopplerLinkMapping *)list->data;
1016 ev_link_mapping = g_new (EvLinkMapping, 1);
1017 ev_link_mapping->link = ev_link_from_action (link_mapping->action);
1018 ev_link_mapping->x1 = link_mapping->area.x1;
1019 ev_link_mapping->x2 = link_mapping->area.x2;
1020 /* Invert this for X-style coordinates */
1021 ev_link_mapping->y1 = height - link_mapping->area.y2;
1022 ev_link_mapping->y2 = height - link_mapping->area.y1;
1024 retval = g_list_prepend (retval, ev_link_mapping);
1027 poppler_page_free_link_mapping (mapping_list);
1028 g_object_unref (poppler_page);
1030 return g_list_reverse (retval);
1034 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1035 const gchar *link_name)
1037 PdfDocument *pdf_document;
1039 EvLinkDest *ev_dest = NULL;
1041 pdf_document = PDF_DOCUMENT (document_links);
1042 dest = poppler_document_find_dest (pdf_document->document,
1045 ev_dest = ev_link_dest_from_dest (dest);
1046 poppler_dest_free (dest);
1053 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1055 iface->has_document_links = pdf_document_links_has_document_links;
1056 iface->get_links_model = pdf_document_links_get_links_model;
1057 iface->get_links = pdf_document_links_get_links;
1058 iface->find_link_dest = pdf_document_links_find_link_dest;
1062 make_thumbnail_for_size (PdfDocument *pdf_document,
1067 PopplerPage *poppler_page;
1071 gdouble unscaled_width, unscaled_height;
1073 poppler_page = poppler_document_get_page (pdf_document->document, page);
1074 g_return_val_if_fail (poppler_page != NULL, NULL);
1076 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page,
1077 size, &width, &height);
1078 poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
1079 scale = width / unscaled_width;
1082 if (rotation == 90 || rotation == 270) {
1089 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1091 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1093 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1095 scale, rotation, pixbuf);
1098 g_object_unref (poppler_page);
1104 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1110 PdfDocument *pdf_document;
1111 PopplerPage *poppler_page;
1113 GdkPixbuf *border_pixbuf;
1115 pdf_document = PDF_DOCUMENT (document_thumbnails);
1117 poppler_page = poppler_document_get_page (pdf_document->document, page);
1118 g_return_val_if_fail (poppler_page != NULL, NULL);
1120 pixbuf = poppler_page_get_thumbnail (poppler_page);
1122 if (pixbuf == NULL) {
1123 /* There is no provided thumbnail. We need to make one. */
1124 pixbuf = make_thumbnail_for_size (pdf_document, page, rotation, size);
1128 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, rotation, pixbuf);
1129 g_object_unref (pixbuf);
1130 pixbuf = border_pixbuf;
1133 g_object_unref (poppler_page);
1139 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1145 PdfDocument *pdf_document;
1146 PopplerPage *poppler_page;
1149 pdf_document = PDF_DOCUMENT (document_thumbnails);
1150 poppler_page = poppler_document_get_page (pdf_document->document, page);
1152 g_return_if_fail (width != NULL);
1153 g_return_if_fail (height != NULL);
1154 g_return_if_fail (poppler_page != NULL);
1156 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1159 double page_width, page_height;
1161 poppler_page_get_size (poppler_page, &page_width, &page_height);
1163 *height = (int) (size * page_height / page_width);
1165 g_object_unref (poppler_page);
1169 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1171 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1172 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1177 pdf_document_search_idle_callback (void *data)
1179 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1180 PdfDocument *pdf_document = search->document;
1185 page = poppler_document_get_page (search->document->document,
1186 search->search_page);
1188 ev_document_doc_mutex_lock ();
1189 matches = poppler_page_find_text (page, search->text);
1190 ev_document_doc_mutex_unlock ();
1192 g_object_unref (page);
1194 search->pages[search->search_page] = matches;
1195 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1196 search->search_page);
1198 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1199 search->search_page += 1;
1200 if (search->search_page == n_pages) {
1202 search->search_page = 0;
1205 if (search->search_page != search->start_page) {
1210 search->idle = 0; /* will return FALSE to remove */
1215 static PdfDocumentSearch *
1216 pdf_document_search_new (PdfDocument *pdf_document,
1220 PdfDocumentSearch *search;
1224 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1226 search = g_new0 (PdfDocumentSearch, 1);
1228 search->text = g_strdup (text);
1229 search->pages = g_new0 (GList *, n_pages);
1230 for (i = 0; i < n_pages; i++) {
1231 search->pages[i] = NULL;
1234 search->document = pdf_document;
1236 /* We add at low priority so the progress bar repaints */
1237 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1238 pdf_document_search_idle_callback,
1242 search->start_page = start_page;
1243 search->search_page = start_page;
1249 pdf_document_find_begin (EvDocumentFind *document,
1251 const char *search_string,
1252 gboolean case_sensitive)
1254 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1256 /* FIXME handle case_sensitive (right now XPDF
1257 * code is always case insensitive for ASCII
1258 * and case sensitive for all other languaages)
1261 if (pdf_document->search &&
1262 strcmp (search_string, pdf_document->search->text) == 0)
1265 if (pdf_document->search)
1266 pdf_document_search_free (pdf_document->search);
1268 pdf_document->search = pdf_document_search_new (pdf_document,
1274 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1276 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1279 return g_list_length (search->pages[page]);
1286 pdf_document_find_get_result (EvDocumentFind *document_find,
1289 EvRectangle *rectangle)
1291 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1292 PdfDocumentSearch *search = pdf_document->search;
1293 PopplerPage *poppler_page;
1294 PopplerRectangle *r;
1300 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1305 poppler_page = poppler_document_get_page (pdf_document->document, page);
1306 poppler_page_get_size (poppler_page, NULL, &height);
1307 rectangle->x1 = r->x1;
1308 rectangle->y1 = height - r->y2;
1309 rectangle->x2 = r->x2;
1310 rectangle->y2 = height - r->y1;
1311 g_object_unref (poppler_page);
1317 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1320 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1322 return search && search->pages[page] != NULL;
1326 pdf_document_find_get_progress (EvDocumentFind *document_find)
1328 PdfDocumentSearch *search;
1329 int n_pages, pages_done;
1331 search = PDF_DOCUMENT (document_find)->search;
1333 if (search == NULL) {
1337 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1338 if (search->search_page > search->start_page) {
1339 pages_done = search->search_page - search->start_page + 1;
1340 } else if (search->search_page == search->start_page) {
1341 pages_done = n_pages;
1343 pages_done = n_pages - search->start_page + search->search_page;
1346 return pages_done / (double) n_pages;
1350 pdf_document_find_cancel (EvDocumentFind *document)
1352 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1354 if (pdf_document->search) {
1355 pdf_document_search_free (pdf_document->search);
1356 pdf_document->search = NULL;
1361 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1363 iface->begin = pdf_document_find_begin;
1364 iface->get_n_results = pdf_document_find_get_n_results;
1365 iface->get_result = pdf_document_find_get_result;
1366 iface->page_has_results = pdf_document_find_page_has_results;
1367 iface->get_progress = pdf_document_find_get_progress;
1368 iface->cancel = pdf_document_find_cancel;
1372 pdf_document_ps_exporter_begin (EvPSExporter *exporter, const char *filename,
1373 int first_page, int last_page,
1374 double width, double height, gboolean duplex)
1376 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1378 pdf_document->ps_file = poppler_ps_file_new (pdf_document->document, filename,
1380 last_page - first_page + 1);
1381 poppler_ps_file_set_paper_size (pdf_document->ps_file, width, height);
1382 poppler_ps_file_set_duplex (pdf_document->ps_file, duplex);
1386 pdf_document_ps_exporter_do_page (EvPSExporter *exporter, EvRenderContext *rc)
1388 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1389 PopplerPage *poppler_page;
1391 g_return_if_fail (pdf_document->ps_file != NULL);
1393 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1394 poppler_page_render_to_ps (poppler_page, pdf_document->ps_file);
1395 g_object_unref (poppler_page);
1399 pdf_document_ps_exporter_end (EvPSExporter *exporter)
1401 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1403 poppler_ps_file_free (pdf_document->ps_file);
1404 pdf_document->ps_file = NULL;
1408 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1410 iface->begin = pdf_document_ps_exporter_begin;
1411 iface->do_page = pdf_document_ps_exporter_do_page;
1412 iface->end = pdf_document_ps_exporter_end;
1417 pdf_selection_render_selection (EvSelection *selection,
1418 EvRenderContext *rc,
1420 EvRectangle *points,
1421 EvRectangle *old_points,
1425 PdfDocument *pdf_document;
1426 double width_points, height_points;
1429 pdf_document = PDF_DOCUMENT (selection);
1430 set_rc_data (pdf_document, rc);
1432 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
1433 width = (int) ((width_points * rc->scale) + 0.5);
1434 height = (int) ((height_points * rc->scale) + 0.5);
1436 if (*pixbuf == NULL) {
1437 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1442 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1443 rc->scale, rc->rotation, *pixbuf,
1444 (PopplerRectangle *)points,
1445 (PopplerRectangle *)old_points,
1452 pdf_selection_get_selection_region (EvSelection *selection,
1453 EvRenderContext *rc,
1454 EvRectangle *points)
1456 PdfDocument *pdf_document;
1459 pdf_document = PDF_DOCUMENT (selection);
1461 set_rc_data (pdf_document, rc);
1463 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data, rc->scale, (PopplerRectangle *) points);
1469 pdf_selection_get_selection_map (EvSelection *selection,
1470 EvRenderContext *rc)
1472 PdfDocument *pdf_document;
1473 PopplerPage *poppler_page;
1474 PopplerRectangle points;
1477 pdf_document = PDF_DOCUMENT (selection);
1478 poppler_page = poppler_document_get_page (pdf_document->document,
1483 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1484 retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1485 g_object_unref (poppler_page);
1491 pdf_selection_iface_init (EvSelectionIface *iface)
1493 iface->render_selection = pdf_selection_render_selection;
1494 iface->get_selection_region = pdf_selection_get_selection_region;
1495 iface->get_selection_map = pdf_selection_get_selection_map;
1499 pdf_document_new (void)
1501 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));