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"
39 PdfDocument *document;
47 struct _PdfDocumentClass
49 GObjectClass parent_class;
54 GObject parent_instance;
56 PopplerDocument *document;
57 PopplerPSFile *ps_file;
60 PopplerFontInfo *font_info;
61 PopplerFontsIter *fonts_iter;
62 int fonts_scanned_pages;
64 PdfDocumentSearch *search;
67 static void pdf_document_document_iface_init (EvDocumentIface *iface);
68 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
69 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
70 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
71 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
72 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
73 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface);
74 static void pdf_selection_iface_init (EvSelectionIface *iface);
75 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
80 static int pdf_document_get_n_pages (EvDocument *document);
82 static EvLink * ev_link_from_action (PopplerAction *action);
83 static void pdf_document_search_free (PdfDocumentSearch *search);
86 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
88 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
89 pdf_document_document_iface_init);
90 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
91 pdf_document_security_iface_init);
92 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
93 pdf_document_document_thumbnails_iface_init);
94 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
95 pdf_document_document_links_iface_init);
96 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
97 pdf_document_document_fonts_iface_init);
98 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
99 pdf_document_find_iface_init);
100 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
101 pdf_document_ps_exporter_iface_init);
102 G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
103 pdf_selection_iface_init);
108 set_rc_data (PdfDocument *pdf_document,
111 if (rc->data == NULL) {
112 rc->data = poppler_document_get_page (pdf_document->document,
114 rc->destroy = g_object_unref;
116 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
121 pdf_document_search_free (PdfDocumentSearch *search)
123 PdfDocument *pdf_document = search->document;
127 if (search->idle != 0)
128 g_source_remove (search->idle);
130 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
131 for (i = 0; i < n_pages; i++) {
132 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
133 g_list_free (search->pages[i]);
136 g_free (search->text);
140 pdf_document_dispose (GObject *object)
142 PdfDocument *pdf_document = PDF_DOCUMENT(object);
144 if (pdf_document->search) {
145 pdf_document_search_free (pdf_document->search);
146 pdf_document->search = NULL;
149 if (pdf_document->document) {
150 g_object_unref (pdf_document->document);
153 if (pdf_document->font_info) {
154 poppler_font_info_free (pdf_document->font_info);
157 if (pdf_document->fonts_iter) {
158 poppler_fonts_iter_free (pdf_document->fonts_iter);
163 pdf_document_class_init (PdfDocumentClass *klass)
165 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
167 g_object_class->dispose = pdf_document_dispose;
171 pdf_document_init (PdfDocument *pdf_document)
173 pdf_document->password = NULL;
177 convert_error (GError *poppler_error,
180 if (poppler_error == NULL)
183 if (poppler_error->domain == POPPLER_ERROR) {
184 /* convert poppler errors into EvDocument errors */
185 gint code = EV_DOCUMENT_ERROR_INVALID;
186 if (poppler_error->code == POPPLER_ERROR_INVALID)
187 code = EV_DOCUMENT_ERROR_INVALID;
188 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
189 code = EV_DOCUMENT_ERROR_ENCRYPTED;
195 poppler_error->message,
198 g_propagate_error (error, poppler_error);
205 pdf_document_save (EvDocument *document,
210 GError *poppler_error = NULL;
212 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
216 convert_error (poppler_error, error);
222 pdf_document_load (EvDocument *document,
226 GError *poppler_error = NULL;
227 PdfDocument *pdf_document = PDF_DOCUMENT (document);
229 pdf_document->document =
230 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
232 if (pdf_document->document == NULL) {
233 convert_error (poppler_error, error);
241 pdf_document_get_n_pages (EvDocument *document)
243 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
247 pdf_document_get_page_size (EvDocument *document,
252 PdfDocument *pdf_document = PDF_DOCUMENT (document);
253 PopplerPage *poppler_page;
255 poppler_page = poppler_document_get_page (pdf_document->document, page);
256 poppler_page_get_size (poppler_page, width, height);
257 g_object_unref (poppler_page);
261 pdf_document_get_page_label (EvDocument *document,
264 PopplerPage *poppler_page;
267 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
270 g_object_get (G_OBJECT (poppler_page),
273 g_object_unref (poppler_page);
279 pdf_document_get_links (EvDocument *document,
282 PdfDocument *pdf_document;
283 PopplerPage *poppler_page;
284 GList *retval = NULL;
289 pdf_document = PDF_DOCUMENT (document);
290 poppler_page = poppler_document_get_page (pdf_document->document,
292 mapping_list = poppler_page_get_link_mapping (poppler_page);
293 poppler_page_get_size (poppler_page, NULL, &height);
295 for (list = mapping_list; list; list = list->next) {
296 PopplerLinkMapping *link_mapping;
297 EvLinkMapping *ev_link_mapping;
299 link_mapping = (PopplerLinkMapping *)list->data;
300 ev_link_mapping = g_new (EvLinkMapping, 1);
301 ev_link_mapping->link = ev_link_from_action (link_mapping->action);
302 ev_link_mapping->x1 = link_mapping->area.x1;
303 ev_link_mapping->x2 = link_mapping->area.x2;
304 /* Invert this for X-style coordinates */
305 ev_link_mapping->y1 = height - link_mapping->area.y2;
306 ev_link_mapping->y2 = height - link_mapping->area.y1;
308 retval = g_list_prepend (retval, ev_link_mapping);
311 poppler_page_free_link_mapping (mapping_list);
312 g_object_unref (poppler_page);
314 return g_list_reverse (retval);
319 pdf_document_render_pixbuf (EvDocument *document,
322 PdfDocument *pdf_document;
324 double width_points, height_points;
327 pdf_document = PDF_DOCUMENT (document);
329 set_rc_data (pdf_document, rc);
331 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
333 if (rc->rotation == 90 || rc->rotation == 270) {
334 width = (int) ((height_points * rc->scale) + 0.5);
335 height = (int) ((width_points * rc->scale) + 0.5);
337 width = (int) ((width_points * rc->scale) + 0.5);
338 height = (int) ((height_points * rc->scale) + 0.5);
341 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
345 poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
356 /* EvDocumentSecurity */
359 pdf_document_has_document_security (EvDocumentSecurity *document_security)
361 /* FIXME: do we really need to have this? */
366 pdf_document_set_password (EvDocumentSecurity *document_security,
367 const char *password)
369 PdfDocument *document = PDF_DOCUMENT (document_security);
371 if (document->password)
372 g_free (document->password);
374 document->password = g_strdup (password);
378 pdf_document_can_get_text (EvDocument *document)
383 static EvDocumentInfo *
384 pdf_document_get_info (EvDocument *document)
386 EvDocumentInfo *info;
387 PopplerPageLayout layout;
388 PopplerPageMode mode;
389 PopplerViewerPreferences view_prefs;
390 PopplerPermissions permissions;
392 info = g_new0 (EvDocumentInfo, 1);
394 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
395 EV_DOCUMENT_INFO_FORMAT |
396 EV_DOCUMENT_INFO_AUTHOR |
397 EV_DOCUMENT_INFO_SUBJECT |
398 EV_DOCUMENT_INFO_KEYWORDS |
399 EV_DOCUMENT_INFO_LAYOUT |
400 EV_DOCUMENT_INFO_START_MODE |
401 EV_DOCUMENT_INFO_PERMISSIONS |
402 EV_DOCUMENT_INFO_UI_HINTS |
403 EV_DOCUMENT_INFO_CREATOR |
404 EV_DOCUMENT_INFO_PRODUCER |
405 EV_DOCUMENT_INFO_CREATION_DATE |
406 EV_DOCUMENT_INFO_MOD_DATE |
407 EV_DOCUMENT_INFO_LINEARIZED |
408 EV_DOCUMENT_INFO_N_PAGES |
409 EV_DOCUMENT_INFO_SECURITY;
412 g_object_get (PDF_DOCUMENT (document)->document,
413 "title", &(info->title),
414 "format", &(info->format),
415 "author", &(info->author),
416 "subject", &(info->subject),
417 "keywords", &(info->keywords),
419 "page-layout", &layout,
420 "viewer-preferences", &view_prefs,
421 "permissions", &permissions,
422 "creator", &(info->creator),
423 "producer", &(info->producer),
424 "creation-date", &(info->creation_date),
425 "mod-date", &(info->modified_date),
426 "linearized", &(info->linearized),
430 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
431 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
433 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
434 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
436 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
437 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
439 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
440 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
441 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
442 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
444 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
445 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
452 case POPPLER_PAGE_MODE_NONE:
453 info->mode = EV_DOCUMENT_MODE_NONE;
455 case POPPLER_PAGE_MODE_USE_THUMBS:
456 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
458 case POPPLER_PAGE_MODE_USE_OC:
459 info->mode = EV_DOCUMENT_MODE_USE_OC;
461 case POPPLER_PAGE_MODE_FULL_SCREEN:
462 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
464 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
465 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
471 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
472 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
474 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
475 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
477 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
478 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
480 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
481 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
483 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
484 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
486 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
487 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
489 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
490 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
493 info->permissions = 0;
494 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
495 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
497 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
498 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
500 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
501 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
503 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
504 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
507 info->n_pages = ev_document_get_n_pages (document);
509 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
510 /* translators: this is the document security state */
511 info->security = g_strdup (_("Yes"));
513 /* translators: this is the document security state */
514 info->security = g_strdup (_("No"));
521 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
523 PdfDocument *pdf_document = PDF_DOCUMENT (document);
524 PopplerPage *poppler_page;
529 poppler_page = poppler_document_get_page (pdf_document->document, page);
530 g_return_val_if_fail (poppler_page != NULL, NULL);
532 poppler_page_get_size (poppler_page, NULL, &height);
534 r.y1 = height - rect->y2;
536 r.y2 = height - rect->y1;
538 text = poppler_page_get_text (poppler_page, &r);
540 g_object_unref (poppler_page);
546 pdf_document_document_iface_init (EvDocumentIface *iface)
548 iface->save = pdf_document_save;
549 iface->load = pdf_document_load;
550 iface->get_n_pages = pdf_document_get_n_pages;
551 iface->get_page_size = pdf_document_get_page_size;
552 iface->get_page_label = pdf_document_get_page_label;
553 iface->get_links = pdf_document_get_links;
554 iface->render_pixbuf = pdf_document_render_pixbuf;
555 iface->get_text = pdf_document_get_text;
556 iface->can_get_text = pdf_document_can_get_text;
557 iface->get_info = pdf_document_get_info;
561 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
563 iface->has_document_security = pdf_document_has_document_security;
564 iface->set_password = pdf_document_set_password;
568 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
570 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
573 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
575 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
579 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
582 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
585 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
587 if (pdf_document->font_info == NULL) {
588 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
591 if (pdf_document->fonts_iter) {
592 poppler_fonts_iter_free (pdf_document->fonts_iter);
595 pdf_document->fonts_scanned_pages += n_pages;
597 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
598 &pdf_document->fonts_iter);
600 pdf_document->fonts_scanned_pages = 0;
601 poppler_font_info_free (pdf_document->font_info);
602 pdf_document->font_info = NULL;
609 font_type_to_string (PopplerFontType type)
613 case POPPLER_FONT_TYPE_TYPE1:
615 case POPPLER_FONT_TYPE_TYPE1C:
617 case POPPLER_FONT_TYPE_TYPE3:
619 case POPPLER_FONT_TYPE_TRUETYPE:
620 return _("TrueType");
621 case POPPLER_FONT_TYPE_CID_TYPE0:
622 return _("Type 1 (CID)");
623 case POPPLER_FONT_TYPE_CID_TYPE0C:
624 return _("Type 1C (CID)");
625 case POPPLER_FONT_TYPE_CID_TYPE2:
626 return _("TrueType (CID)");
628 return _("Unknown font type");
633 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
636 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
637 PopplerFontsIter *iter = pdf_document->fonts_iter;
639 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
645 GtkTreeIter list_iter;
648 const char *embedded;
651 name = poppler_fonts_iter_get_name (iter);
657 type = font_type_to_string (
658 poppler_fonts_iter_get_font_type (iter));
660 if (poppler_fonts_iter_is_embedded (iter)) {
661 if (poppler_fonts_iter_is_subset (iter))
662 embedded = _("Embedded subset");
664 embedded = _("Embedded");
666 embedded = _("Not embedded");
669 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
671 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
672 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
673 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
674 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
678 } while (poppler_fonts_iter_next (iter));
682 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
684 iface->fill_model = pdf_document_fonts_fill_model;
685 iface->scan = pdf_document_fonts_scan;
686 iface->get_progress = pdf_document_fonts_get_progress;
690 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
692 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
693 PopplerIndexIter *iter;
695 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
697 iter = poppler_index_iter_new (pdf_document->document);
700 poppler_index_iter_free (iter);
706 ev_link_from_dest (PopplerAction *action)
709 const char *unimplemented_dest = NULL;
711 switch (action->goto_dest.dest->type) {
712 case POPPLER_DEST_UNKNOWN:
713 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
715 case POPPLER_DEST_XYZ:
716 link = ev_link_new_page_xyz (action->any.title,
717 action->goto_dest.dest->page_num - 1,
718 action->goto_dest.dest->left,
719 action->goto_dest.dest->top,
720 action->goto_dest.dest->zoom);
722 case POPPLER_DEST_FIT:
723 unimplemented_dest = "POPPLER_DEST_FIT";
725 case POPPLER_DEST_FITH:
726 unimplemented_dest = "POPPLER_DEST_FITH";
728 case POPPLER_DEST_FITV:
729 unimplemented_dest = "POPPLER_DEST_FITV";
731 case POPPLER_DEST_FITR:
732 unimplemented_dest = "POPPLER_DEST_FITR";
734 case POPPLER_DEST_FITB:
735 unimplemented_dest = "POPPLER_DEST_FITB";
737 case POPPLER_DEST_FITBH:
738 unimplemented_dest = "POPPLER_DEST_FITBH";
740 case POPPLER_DEST_FITBV:
741 unimplemented_dest = "POPPLER_DEST_FITBV";
745 if (unimplemented_dest) {
746 g_warning ("Unimplemented destination: %s, please post a bug report with a testcase.",
751 link = ev_link_new_page (action->any.title, action->goto_dest.dest->page_num - 1);
758 ev_link_from_action (PopplerAction *action)
762 const char *unimplemented_action = NULL;
764 title = action->any.title;
766 switch (action->type) {
767 case POPPLER_ACTION_UNKNOWN:
768 g_warning ("Unknown action");
770 case POPPLER_ACTION_GOTO_DEST:
771 link = ev_link_from_dest (action);
773 case POPPLER_ACTION_GOTO_REMOTE:
774 unimplemented_action = "POPPLER_ACTION_GOTO_REMOTE";
776 case POPPLER_ACTION_LAUNCH:
777 unimplemented_action = "POPPLER_ACTION_LAUNCH";
779 case POPPLER_ACTION_URI:
780 link = ev_link_new_external (title, action->uri.uri);
782 case POPPLER_ACTION_NAMED:
783 unimplemented_action = "POPPLER_ACTION_NAMED";
785 case POPPLER_ACTION_MOVIE:
786 unimplemented_action = "POPPLER_ACTION_MOVIE";
790 if (unimplemented_action) {
791 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
792 unimplemented_action);
796 link = ev_link_new_title (title);
803 build_tree (PdfDocument *pdf_document,
806 PopplerIndexIter *iter)
810 GtkTreeIter tree_iter;
811 PopplerIndexIter *child;
812 PopplerAction *action;
816 action = poppler_index_iter_get_action (iter);
817 expand = poppler_index_iter_is_open (iter);
819 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
820 link = ev_link_from_action (action);
821 poppler_action_free (action);
823 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
824 EV_DOCUMENT_LINKS_COLUMN_MARKUP, ev_link_get_title (link),
825 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
826 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
828 g_object_unref (link);
829 child = poppler_index_iter_get_child (iter);
831 build_tree (pdf_document, model, &tree_iter, child);
832 poppler_index_iter_free (child);
834 } while (poppler_index_iter_next (iter));
838 static GtkTreeModel *
839 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
841 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
842 GtkTreeModel *model = NULL;
843 PopplerIndexIter *iter;
845 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
847 iter = poppler_index_iter_new (pdf_document->document);
848 /* Create the model if we have items*/
850 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
854 build_tree (pdf_document, model, NULL, iter);
855 poppler_index_iter_free (iter);
862 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
864 iface->has_document_links = pdf_document_links_has_document_links;
865 iface->get_links_model = pdf_document_links_get_links_model;
869 make_thumbnail_for_size (PdfDocument *pdf_document,
875 PopplerPage *poppler_page;
876 GdkPixbuf *pixbuf, *sub_pixbuf;
879 gdouble unscaled_width, unscaled_height;
881 poppler_page = poppler_document_get_page (pdf_document->document, page);
882 g_return_val_if_fail (poppler_page != NULL, NULL);
884 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page,
885 size, &width, &height);
886 poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
887 scale = width / unscaled_width;
890 pixbuf = ev_document_misc_get_thumbnail_frame (width, height, rotation, NULL);
892 width = gdk_pixbuf_get_width (pixbuf);
893 height = gdk_pixbuf_get_height (pixbuf);
894 sub_pixbuf = gdk_pixbuf_new_subpixbuf (pixbuf,
896 width - 1, height - 1);
899 if (rotation == 90 || rotation == 270) {
906 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
908 gdk_pixbuf_fill (pixbuf, 0xffffffff);
909 sub_pixbuf = gdk_pixbuf_new_subpixbuf (pixbuf,
914 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
916 scale, rotation, sub_pixbuf);
918 g_object_unref (G_OBJECT (sub_pixbuf));
920 g_object_unref (poppler_page);
925 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
931 PdfDocument *pdf_document;
932 PopplerPage *poppler_page;
935 pdf_document = PDF_DOCUMENT (document_thumbnails);
937 poppler_page = poppler_document_get_page (pdf_document->document, page);
938 g_return_val_if_fail (poppler_page != NULL, NULL);
940 pixbuf = poppler_page_get_thumbnail (poppler_page);
942 if (pixbuf != NULL) {
943 /* The document provides its own thumbnails. */
945 GdkPixbuf *real_pixbuf;
947 real_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, rotation, pixbuf);
948 g_object_unref (pixbuf);
949 pixbuf = real_pixbuf;
952 /* There is no provided thumbnail. We need to make one. */
953 pixbuf = make_thumbnail_for_size (pdf_document, page, rotation, size, border);
956 g_object_unref (poppler_page);
962 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
968 PdfDocument *pdf_document;
969 PopplerPage *poppler_page;
972 pdf_document = PDF_DOCUMENT (document_thumbnails);
973 poppler_page = poppler_document_get_page (pdf_document->document, page);
975 g_return_if_fail (width != NULL);
976 g_return_if_fail (height != NULL);
977 g_return_if_fail (poppler_page != NULL);
979 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
982 double page_width, page_height;
984 poppler_page_get_size (poppler_page, &page_width, &page_height);
985 if (page_width > page_height) {
987 *height = (int) (size * page_height / page_width);
989 *width = (int) (size * page_width / page_height);
993 g_object_unref (poppler_page);
997 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
999 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1000 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1005 pdf_document_search_idle_callback (void *data)
1007 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1008 PdfDocument *pdf_document = search->document;
1013 page = poppler_document_get_page (search->document->document,
1014 search->search_page);
1016 ev_document_doc_mutex_lock ();
1017 matches = poppler_page_find_text (page, search->text);
1018 ev_document_doc_mutex_unlock ();
1020 g_object_unref (page);
1022 search->pages[search->search_page] = matches;
1023 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1024 search->search_page);
1026 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1027 search->search_page += 1;
1028 if (search->search_page == n_pages) {
1030 search->search_page = 0;
1033 if (search->search_page != search->start_page) {
1038 search->idle = 0; /* will return FALSE to remove */
1043 static PdfDocumentSearch *
1044 pdf_document_search_new (PdfDocument *pdf_document,
1048 PdfDocumentSearch *search;
1052 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1054 search = g_new0 (PdfDocumentSearch, 1);
1056 search->text = g_strdup (text);
1057 search->pages = g_new0 (GList *, n_pages);
1058 for (i = 0; i < n_pages; i++) {
1059 search->pages[i] = NULL;
1062 search->document = pdf_document;
1064 /* We add at low priority so the progress bar repaints */
1065 search->idle = g_idle_add_full (G_PRIORITY_LOW,
1066 pdf_document_search_idle_callback,
1070 search->start_page = start_page;
1071 search->search_page = start_page;
1077 pdf_document_find_begin (EvDocumentFind *document,
1079 const char *search_string,
1080 gboolean case_sensitive)
1082 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1084 /* FIXME handle case_sensitive (right now XPDF
1085 * code is always case insensitive for ASCII
1086 * and case sensitive for all other languaages)
1089 if (pdf_document->search &&
1090 strcmp (search_string, pdf_document->search->text) == 0)
1093 if (pdf_document->search)
1094 pdf_document_search_free (pdf_document->search);
1096 pdf_document->search = pdf_document_search_new (pdf_document,
1102 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1104 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1107 return g_list_length (search->pages[page]);
1114 pdf_document_find_get_result (EvDocumentFind *document_find,
1117 EvRectangle *rectangle)
1119 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1120 PdfDocumentSearch *search = pdf_document->search;
1121 PopplerPage *poppler_page;
1122 PopplerRectangle *r;
1128 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1133 poppler_page = poppler_document_get_page (pdf_document->document, page);
1134 poppler_page_get_size (poppler_page, NULL, &height);
1135 rectangle->x1 = r->x1;
1136 rectangle->y1 = height - r->y2;
1137 rectangle->x2 = r->x2;
1138 rectangle->y2 = height - r->y1;
1139 g_object_unref (poppler_page);
1145 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1148 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1150 g_return_val_if_fail (search != NULL, FALSE);
1152 return search->pages[page] != NULL;
1156 pdf_document_find_get_progress (EvDocumentFind *document_find)
1158 PdfDocumentSearch *search;
1159 int n_pages, pages_done;
1161 search = PDF_DOCUMENT (document_find)->search;
1163 if (search == NULL) {
1167 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1168 if (search->search_page > search->start_page) {
1169 pages_done = search->search_page - search->start_page + 1;
1170 } else if (search->search_page == search->start_page) {
1171 pages_done = n_pages;
1173 pages_done = n_pages - search->start_page + search->search_page;
1176 return pages_done / (double) n_pages;
1180 pdf_document_find_cancel (EvDocumentFind *document)
1182 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1184 if (pdf_document->search) {
1185 pdf_document_search_free (pdf_document->search);
1186 pdf_document->search = NULL;
1191 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1193 iface->begin = pdf_document_find_begin;
1194 iface->get_n_results = pdf_document_find_get_n_results;
1195 iface->get_result = pdf_document_find_get_result;
1196 iface->page_has_results = pdf_document_find_page_has_results;
1197 iface->get_progress = pdf_document_find_get_progress;
1198 iface->cancel = pdf_document_find_cancel;
1202 pdf_document_ps_exporter_begin (EvPSExporter *exporter, const char *filename,
1203 int first_page, int last_page,
1204 double width, double height, gboolean duplex)
1206 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1208 pdf_document->ps_file = poppler_ps_file_new (pdf_document->document, filename,
1210 last_page - first_page + 1);
1211 poppler_ps_file_set_paper_size (pdf_document->ps_file, width, height);
1212 poppler_ps_file_set_duplex (pdf_document->ps_file, duplex);
1216 pdf_document_ps_exporter_do_page (EvPSExporter *exporter, EvRenderContext *rc)
1218 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1219 PopplerPage *poppler_page;
1221 g_return_if_fail (pdf_document->ps_file != NULL);
1223 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1224 poppler_page_render_to_ps (poppler_page, pdf_document->ps_file);
1225 g_object_unref (poppler_page);
1229 pdf_document_ps_exporter_end (EvPSExporter *exporter)
1231 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1233 poppler_ps_file_free (pdf_document->ps_file);
1234 pdf_document->ps_file = NULL;
1238 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1240 iface->begin = pdf_document_ps_exporter_begin;
1241 iface->do_page = pdf_document_ps_exporter_do_page;
1242 iface->end = pdf_document_ps_exporter_end;
1247 pdf_selection_render_selection (EvSelection *selection,
1248 EvRenderContext *rc,
1250 EvRectangle *points,
1251 EvRectangle *old_points,
1255 PdfDocument *pdf_document;
1256 double width_points, height_points;
1259 pdf_document = PDF_DOCUMENT (selection);
1260 set_rc_data (pdf_document, rc);
1262 poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
1263 width = (int) ((width_points * rc->scale) + 0.5);
1264 height = (int) ((height_points * rc->scale) + 0.5);
1266 if (*pixbuf == NULL) {
1267 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1272 poppler_page_render_selection (POPPLER_PAGE (rc->data),
1273 rc->scale, rc->rotation, *pixbuf,
1274 (PopplerRectangle *)points,
1275 (PopplerRectangle *)old_points,
1282 pdf_selection_get_selection_region (EvSelection *selection,
1283 EvRenderContext *rc,
1284 EvRectangle *points)
1286 PdfDocument *pdf_document;
1289 pdf_document = PDF_DOCUMENT (selection);
1291 set_rc_data (pdf_document, rc);
1293 retval = poppler_page_get_selection_region ((PopplerPage *)rc->data, rc->scale, (PopplerRectangle *) points);
1299 pdf_selection_get_selection_map (EvSelection *selection,
1300 EvRenderContext *rc)
1302 PdfDocument *pdf_document;
1303 PopplerPage *poppler_page;
1304 PopplerRectangle points;
1307 pdf_document = PDF_DOCUMENT (selection);
1308 poppler_page = poppler_document_get_page (pdf_document->document,
1313 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1314 retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1315 g_object_unref (poppler_page);
1321 pdf_selection_iface_init (EvSelectionIface *iface)
1323 iface->render_selection = pdf_selection_render_selection;
1324 iface->get_selection_region = pdf_selection_get_selection_region;
1325 iface->get_selection_map = pdf_selection_get_selection_map;
1329 pdf_document_new (void)
1331 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));