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);
107 pdf_document_search_free (PdfDocumentSearch *search)
109 PdfDocument *pdf_document = search->document;
113 if (search->idle != 0)
114 g_source_remove (search->idle);
116 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
117 for (i = 0; i < n_pages; i++) {
118 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
119 g_list_free (search->pages[i]);
122 g_free (search->text);
126 pdf_document_dispose (GObject *object)
128 PdfDocument *pdf_document = PDF_DOCUMENT(object);
130 if (pdf_document->search) {
131 pdf_document_search_free (pdf_document->search);
132 pdf_document->search = NULL;
135 if (pdf_document->document) {
136 g_object_unref (pdf_document->document);
139 if (pdf_document->font_info) {
140 poppler_font_info_free (pdf_document->font_info);
143 if (pdf_document->fonts_iter) {
144 poppler_fonts_iter_free (pdf_document->fonts_iter);
149 pdf_document_class_init (PdfDocumentClass *klass)
151 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
153 g_object_class->dispose = pdf_document_dispose;
157 pdf_document_init (PdfDocument *pdf_document)
159 pdf_document->password = NULL;
163 convert_error (GError *poppler_error,
166 if (poppler_error == NULL)
169 if (poppler_error->domain == POPPLER_ERROR) {
170 /* convert poppler errors into EvDocument errors */
171 gint code = EV_DOCUMENT_ERROR_INVALID;
172 if (poppler_error->code == POPPLER_ERROR_INVALID)
173 code = EV_DOCUMENT_ERROR_INVALID;
174 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
175 code = EV_DOCUMENT_ERROR_ENCRYPTED;
181 poppler_error->message,
184 g_propagate_error (error, poppler_error);
191 pdf_document_save (EvDocument *document,
196 GError *poppler_error = NULL;
198 retval = poppler_document_save (PDF_DOCUMENT (document)->document,
202 convert_error (poppler_error, error);
208 pdf_document_load (EvDocument *document,
212 GError *poppler_error = NULL;
213 PdfDocument *pdf_document = PDF_DOCUMENT (document);
215 pdf_document->document =
216 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
218 if (pdf_document->document == NULL) {
219 convert_error (poppler_error, error);
227 pdf_document_get_n_pages (EvDocument *document)
229 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
233 set_page_orientation (PdfDocument *pdf_document, PopplerPage *page, int rotation)
235 PopplerOrientation orientation;
238 orientation = poppler_page_get_orientation (page);
241 if (orientation == POPPLER_ORIENTATION_PORTRAIT) {
242 orientation = POPPLER_ORIENTATION_LANDSCAPE;
243 } else if (orientation == POPPLER_ORIENTATION_LANDSCAPE) {
244 orientation = POPPLER_ORIENTATION_UPSIDEDOWN;
245 } else if (orientation == POPPLER_ORIENTATION_UPSIDEDOWN) {
246 orientation = POPPLER_ORIENTATION_SEASCAPE;
248 orientation = POPPLER_ORIENTATION_PORTRAIT;
253 poppler_page_set_orientation (page, orientation);
257 pdf_document_get_page_size (EvDocument *document,
262 PdfDocument *pdf_document = PDF_DOCUMENT (document);
263 PopplerPage *poppler_page;
265 poppler_page = poppler_document_get_page (pdf_document->document, page);
266 poppler_page_get_size (poppler_page, width, height);
267 g_object_unref (poppler_page);
271 pdf_document_get_page_label (EvDocument *document,
274 PopplerPage *poppler_page;
277 poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
280 g_object_get (G_OBJECT (poppler_page),
283 g_object_unref (poppler_page);
289 pdf_document_get_links (EvDocument *document,
292 PdfDocument *pdf_document;
293 PopplerPage *poppler_page;
294 GList *retval = NULL;
299 pdf_document = PDF_DOCUMENT (document);
300 poppler_page = poppler_document_get_page (pdf_document->document,
302 mapping_list = poppler_page_get_link_mapping (poppler_page);
303 poppler_page_get_size (poppler_page, NULL, &height);
305 for (list = mapping_list; list; list = list->next) {
306 PopplerLinkMapping *link_mapping;
307 EvLinkMapping *ev_link_mapping;
309 link_mapping = (PopplerLinkMapping *)list->data;
310 ev_link_mapping = g_new (EvLinkMapping, 1);
311 ev_link_mapping->link = ev_link_from_action (link_mapping->action);
312 ev_link_mapping->x1 = link_mapping->area.x1;
313 ev_link_mapping->x2 = link_mapping->area.x2;
314 /* Invert this for X-style coordinates */
315 ev_link_mapping->y1 = height - link_mapping->area.y2;
316 ev_link_mapping->y2 = height - link_mapping->area.y1;
318 retval = g_list_prepend (retval, ev_link_mapping);
321 poppler_page_free_link_mapping (mapping_list);
322 g_object_unref (poppler_page);
324 return g_list_reverse (retval);
329 pdf_document_render_pixbuf (EvDocument *document,
332 PdfDocument *pdf_document;
333 PopplerPage *poppler_page;
335 double width_points, height_points;
338 pdf_document = PDF_DOCUMENT (document);
339 poppler_page = poppler_document_get_page (pdf_document->document,
341 set_page_orientation (pdf_document, poppler_page, rc->rotation);
343 poppler_page_get_size (poppler_page, &width_points, &height_points);
344 width = (int) ((width_points * rc->scale) + 0.5);
345 height = (int) ((height_points * rc->scale) + 0.5);
347 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
351 poppler_page_render_to_pixbuf (poppler_page,
357 g_object_unref (poppler_page);
362 /* EvDocumentSecurity */
365 pdf_document_has_document_security (EvDocumentSecurity *document_security)
367 /* FIXME: do we really need to have this? */
372 pdf_document_set_password (EvDocumentSecurity *document_security,
373 const char *password)
375 PdfDocument *document = PDF_DOCUMENT (document_security);
377 if (document->password)
378 g_free (document->password);
380 document->password = g_strdup (password);
384 pdf_document_can_get_text (EvDocument *document)
389 static EvDocumentInfo *
390 pdf_document_get_info (EvDocument *document)
392 EvDocumentInfo *info;
393 PopplerPageLayout layout;
394 PopplerPageMode mode;
395 PopplerViewerPreferences view_prefs;
396 PopplerPermissions permissions;
398 info = g_new0 (EvDocumentInfo, 1);
400 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
401 EV_DOCUMENT_INFO_FORMAT |
402 EV_DOCUMENT_INFO_AUTHOR |
403 EV_DOCUMENT_INFO_SUBJECT |
404 EV_DOCUMENT_INFO_KEYWORDS |
405 EV_DOCUMENT_INFO_LAYOUT |
406 EV_DOCUMENT_INFO_START_MODE |
407 EV_DOCUMENT_INFO_PERMISSIONS |
408 EV_DOCUMENT_INFO_UI_HINTS |
409 EV_DOCUMENT_INFO_CREATOR |
410 EV_DOCUMENT_INFO_PRODUCER |
411 EV_DOCUMENT_INFO_CREATION_DATE |
412 EV_DOCUMENT_INFO_MOD_DATE |
413 EV_DOCUMENT_INFO_LINEARIZED |
414 EV_DOCUMENT_INFO_N_PAGES |
415 EV_DOCUMENT_INFO_SECURITY;
418 g_object_get (PDF_DOCUMENT (document)->document,
419 "title", &(info->title),
420 "format", &(info->format),
421 "author", &(info->author),
422 "subject", &(info->subject),
423 "keywords", &(info->keywords),
425 "page-layout", &layout,
426 "viewer-preferences", &view_prefs,
427 "permissions", &permissions,
428 "creator", &(info->creator),
429 "producer", &(info->producer),
430 "creation-date", &(info->creation_date),
431 "mod-date", &(info->modified_date),
432 "linearized", &(info->linearized),
436 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
437 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
439 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
440 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
442 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
443 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
445 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
446 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
447 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
448 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
450 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
451 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
458 case POPPLER_PAGE_MODE_NONE:
459 info->mode = EV_DOCUMENT_MODE_NONE;
461 case POPPLER_PAGE_MODE_USE_THUMBS:
462 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
464 case POPPLER_PAGE_MODE_USE_OC:
465 info->mode = EV_DOCUMENT_MODE_USE_OC;
467 case POPPLER_PAGE_MODE_FULL_SCREEN:
468 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
470 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
471 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
477 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
478 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
480 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
481 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
483 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
484 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
486 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
487 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
489 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
490 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
492 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
493 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
495 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
496 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
499 info->permissions = 0;
500 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
501 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
503 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
504 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
506 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
507 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
509 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
510 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
513 info->n_pages = ev_document_get_n_pages (document);
515 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
516 /* translators: this is the document security state */
517 info->security = g_strdup (_("Yes"));
519 /* translators: this is the document security state */
520 info->security = g_strdup (_("No"));
527 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
529 PdfDocument *pdf_document = PDF_DOCUMENT (document);
530 PopplerPage *poppler_page;
535 poppler_page = poppler_document_get_page (pdf_document->document, page);
536 g_return_val_if_fail (poppler_page != NULL, NULL);
538 poppler_page_get_size (poppler_page, NULL, &height);
540 r.y1 = height - rect->y2;
542 r.y2 = height - rect->y1;
544 text = poppler_page_get_text (poppler_page, &r);
546 g_object_unref (poppler_page);
552 pdf_document_document_iface_init (EvDocumentIface *iface)
554 iface->save = pdf_document_save;
555 iface->load = pdf_document_load;
556 iface->get_n_pages = pdf_document_get_n_pages;
557 iface->get_page_size = pdf_document_get_page_size;
558 iface->get_page_label = pdf_document_get_page_label;
559 iface->get_links = pdf_document_get_links;
560 iface->render_pixbuf = pdf_document_render_pixbuf;
561 iface->get_text = pdf_document_get_text;
562 iface->can_get_text = pdf_document_can_get_text;
563 iface->get_info = pdf_document_get_info;
567 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
569 iface->has_document_security = pdf_document_has_document_security;
570 iface->set_password = pdf_document_set_password;
574 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
576 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
579 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
581 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
585 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
588 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
591 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
593 if (pdf_document->font_info == NULL) {
594 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
597 if (pdf_document->fonts_iter) {
598 poppler_fonts_iter_free (pdf_document->fonts_iter);
601 pdf_document->fonts_scanned_pages += n_pages;
603 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
604 &pdf_document->fonts_iter);
606 pdf_document->fonts_scanned_pages = 0;
607 poppler_font_info_free (pdf_document->font_info);
608 pdf_document->font_info = NULL;
615 font_type_to_string (PopplerFontType type)
619 case POPPLER_FONT_TYPE_TYPE1:
621 case POPPLER_FONT_TYPE_TYPE1C:
623 case POPPLER_FONT_TYPE_TYPE3:
625 case POPPLER_FONT_TYPE_TRUETYPE:
626 return _("TrueType");
627 case POPPLER_FONT_TYPE_CID_TYPE0:
628 return _("Type 1 (CID)");
629 case POPPLER_FONT_TYPE_CID_TYPE0C:
630 return _("Type 1C (CID)");
631 case POPPLER_FONT_TYPE_CID_TYPE2:
632 return _("TrueType (CID)");
634 return _("Unknown font type");
639 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
642 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
643 PopplerFontsIter *iter = pdf_document->fonts_iter;
645 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
651 GtkTreeIter list_iter;
654 const char *embedded;
657 name = poppler_fonts_iter_get_name (iter);
663 type = font_type_to_string (
664 poppler_fonts_iter_get_font_type (iter));
666 if (poppler_fonts_iter_is_embedded (iter)) {
667 if (poppler_fonts_iter_is_subset (iter))
668 embedded = _("Embedded subset");
670 embedded = _("Embedded");
672 embedded = _("Not embedded");
675 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
677 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
678 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
679 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
680 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
684 } while (poppler_fonts_iter_next (iter));
688 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
690 iface->fill_model = pdf_document_fonts_fill_model;
691 iface->scan = pdf_document_fonts_scan;
692 iface->get_progress = pdf_document_fonts_get_progress;
696 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
698 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
699 PopplerIndexIter *iter;
701 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
703 iter = poppler_index_iter_new (pdf_document->document);
706 poppler_index_iter_free (iter);
712 ev_link_from_action (PopplerAction *action)
717 title = action->any.title;
719 if (action->type == POPPLER_ACTION_GOTO_DEST) {
720 link = ev_link_new_page (title, action->goto_dest.dest->page_num - 1);
721 } else if (action->type == POPPLER_ACTION_URI) {
722 link = ev_link_new_external (title, action->uri.uri);
724 link = ev_link_new_title (title);
731 build_tree (PdfDocument *pdf_document,
734 PopplerIndexIter *iter)
738 GtkTreeIter tree_iter;
739 PopplerIndexIter *child;
740 PopplerAction *action;
744 action = poppler_index_iter_get_action (iter);
745 expand = poppler_index_iter_is_open (iter);
747 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
748 link = ev_link_from_action (action);
749 poppler_action_free (action);
751 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
752 EV_DOCUMENT_LINKS_COLUMN_MARKUP, ev_link_get_title (link),
753 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
754 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
756 g_object_unref (link);
757 child = poppler_index_iter_get_child (iter);
759 build_tree (pdf_document, model, &tree_iter, child);
760 poppler_index_iter_free (child);
762 } while (poppler_index_iter_next (iter));
766 static GtkTreeModel *
767 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
769 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
770 GtkTreeModel *model = NULL;
771 PopplerIndexIter *iter;
773 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
775 iter = poppler_index_iter_new (pdf_document->document);
776 /* Create the model if we have items*/
778 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
782 build_tree (pdf_document, model, NULL, iter);
783 poppler_index_iter_free (iter);
790 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
792 iface->has_document_links = pdf_document_links_has_document_links;
793 iface->get_links_model = pdf_document_links_get_links_model;
797 make_thumbnail_for_size (PdfDocument *pdf_document,
803 PopplerPage *poppler_page;
804 GdkPixbuf *pixbuf, *sub_pixbuf;
807 gdouble unscaled_width, unscaled_height;
809 poppler_page = poppler_document_get_page (pdf_document->document, page);
810 set_page_orientation (pdf_document, poppler_page, rotation);
811 g_return_val_if_fail (poppler_page != NULL, NULL);
813 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page,
814 size, &width, &height);
815 poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
816 scale = width / unscaled_width;
819 pixbuf = ev_document_misc_get_thumbnail_frame (width, height, NULL);
821 sub_pixbuf = gdk_pixbuf_new_subpixbuf (pixbuf,
823 width - 1, height - 1);
825 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
827 gdk_pixbuf_fill (pixbuf, 0xffffffff);
828 sub_pixbuf = gdk_pixbuf_new_subpixbuf (pixbuf,
833 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
837 g_object_unref (G_OBJECT (sub_pixbuf));
839 g_object_unref (poppler_page);
844 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
850 PdfDocument *pdf_document;
851 PopplerPage *poppler_page;
854 pdf_document = PDF_DOCUMENT (document_thumbnails);
856 poppler_page = poppler_document_get_page (pdf_document->document, page);
857 set_page_orientation (pdf_document, poppler_page, rotation);
858 g_return_val_if_fail (poppler_page != NULL, NULL);
860 pixbuf = poppler_page_get_thumbnail (poppler_page);
862 if (pixbuf != NULL) {
863 /* The document provides its own thumbnails. */
865 GdkPixbuf *real_pixbuf;
867 real_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
868 g_object_unref (pixbuf);
869 pixbuf = real_pixbuf;
872 /* There is no provided thumbnail. We need to make one. */
873 pixbuf = make_thumbnail_for_size (pdf_document, page, rotation, size, border);
876 g_object_unref (poppler_page);
882 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
888 PdfDocument *pdf_document;
889 PopplerPage *poppler_page;
892 pdf_document = PDF_DOCUMENT (document_thumbnails);
893 poppler_page = poppler_document_get_page (pdf_document->document, page);
895 g_return_if_fail (width != NULL);
896 g_return_if_fail (height != NULL);
897 g_return_if_fail (poppler_page != NULL);
899 has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
902 double page_width, page_height;
904 poppler_page_get_size (poppler_page, &page_width, &page_height);
905 if (page_width > page_height) {
907 *height = (int) (size * page_height / page_width);
909 *width = (int) (size * page_width / page_height);
913 g_object_unref (poppler_page);
917 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
919 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
920 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
925 pdf_document_search_idle_callback (void *data)
927 PdfDocumentSearch *search = (PdfDocumentSearch*) data;
928 PdfDocument *pdf_document = search->document;
933 page = poppler_document_get_page (search->document->document,
934 search->search_page);
936 ev_document_doc_mutex_lock ();
937 matches = poppler_page_find_text (page, search->text);
938 ev_document_doc_mutex_unlock ();
940 g_object_unref (page);
942 search->pages[search->search_page] = matches;
943 ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
944 search->search_page);
946 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
947 search->search_page += 1;
948 if (search->search_page == n_pages) {
950 search->search_page = 0;
953 if (search->search_page != search->start_page) {
958 search->idle = 0; /* will return FALSE to remove */
963 static PdfDocumentSearch *
964 pdf_document_search_new (PdfDocument *pdf_document,
968 PdfDocumentSearch *search;
972 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
974 search = g_new0 (PdfDocumentSearch, 1);
976 search->text = g_strdup (text);
977 search->pages = g_new0 (GList *, n_pages);
978 for (i = 0; i < n_pages; i++) {
979 search->pages[i] = NULL;
982 search->document = pdf_document;
984 /* We add at low priority so the progress bar repaints */
985 search->idle = g_idle_add_full (G_PRIORITY_LOW,
986 pdf_document_search_idle_callback,
990 search->start_page = start_page;
991 search->search_page = start_page;
997 pdf_document_find_begin (EvDocumentFind *document,
999 const char *search_string,
1000 gboolean case_sensitive)
1002 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1004 /* FIXME handle case_sensitive (right now XPDF
1005 * code is always case insensitive for ASCII
1006 * and case sensitive for all other languaages)
1009 if (pdf_document->search &&
1010 strcmp (search_string, pdf_document->search->text) == 0)
1013 if (pdf_document->search)
1014 pdf_document_search_free (pdf_document->search);
1016 pdf_document->search = pdf_document_search_new (pdf_document,
1022 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1024 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1027 return g_list_length (search->pages[page]);
1034 pdf_document_find_get_result (EvDocumentFind *document_find,
1037 EvRectangle *rectangle)
1039 PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1040 PdfDocumentSearch *search = pdf_document->search;
1041 PopplerPage *poppler_page;
1042 PopplerRectangle *r;
1048 r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1053 poppler_page = poppler_document_get_page (pdf_document->document, page);
1054 poppler_page_get_size (poppler_page, NULL, &height);
1055 rectangle->x1 = r->x1;
1056 rectangle->y1 = height - r->y2;
1057 rectangle->x2 = r->x2;
1058 rectangle->y2 = height - r->y1;
1059 g_object_unref (poppler_page);
1065 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1068 PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1070 g_return_val_if_fail (search != NULL, FALSE);
1072 return search->pages[page] != NULL;
1076 pdf_document_find_get_progress (EvDocumentFind *document_find)
1078 PdfDocumentSearch *search;
1079 int n_pages, pages_done;
1081 search = PDF_DOCUMENT (document_find)->search;
1083 if (search == NULL) {
1087 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1088 if (search->search_page > search->start_page) {
1089 pages_done = search->search_page - search->start_page + 1;
1090 } else if (search->search_page == search->start_page) {
1091 pages_done = n_pages;
1093 pages_done = n_pages - search->start_page + search->search_page;
1096 return pages_done / (double) n_pages;
1100 pdf_document_find_cancel (EvDocumentFind *document)
1102 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1104 if (pdf_document->search) {
1105 pdf_document_search_free (pdf_document->search);
1106 pdf_document->search = NULL;
1111 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1113 iface->begin = pdf_document_find_begin;
1114 iface->get_n_results = pdf_document_find_get_n_results;
1115 iface->get_result = pdf_document_find_get_result;
1116 iface->page_has_results = pdf_document_find_page_has_results;
1117 iface->get_progress = pdf_document_find_get_progress;
1118 iface->cancel = pdf_document_find_cancel;
1122 pdf_document_ps_exporter_begin (EvPSExporter *exporter, const char *filename,
1123 int first_page, int last_page,
1124 double width, double height, gboolean duplex)
1126 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1128 pdf_document->ps_file = poppler_ps_file_new (pdf_document->document, filename,
1130 last_page - first_page + 1);
1131 poppler_ps_file_set_paper_size (pdf_document->ps_file, width, height);
1132 poppler_ps_file_set_duplex (pdf_document->ps_file, duplex);
1136 pdf_document_ps_exporter_do_page (EvPSExporter *exporter, EvRenderContext *rc)
1138 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1139 PopplerPage *poppler_page;
1141 g_return_if_fail (pdf_document->ps_file != NULL);
1143 poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1144 set_page_orientation (pdf_document, poppler_page, rc->rotation);
1145 poppler_page_render_to_ps (poppler_page, pdf_document->ps_file);
1146 g_object_unref (poppler_page);
1150 pdf_document_ps_exporter_end (EvPSExporter *exporter)
1152 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1154 poppler_ps_file_free (pdf_document->ps_file);
1155 pdf_document->ps_file = NULL;
1159 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1161 iface->begin = pdf_document_ps_exporter_begin;
1162 iface->do_page = pdf_document_ps_exporter_do_page;
1163 iface->end = pdf_document_ps_exporter_end;
1168 pdf_selection_render_selection (EvSelection *selection,
1169 EvRenderContext *rc,
1171 EvRectangle *points,
1172 EvRectangle *old_points)
1174 PdfDocument *pdf_document;
1175 PopplerPage *poppler_page;
1176 double width_points, height_points;
1179 pdf_document = PDF_DOCUMENT (selection);
1180 poppler_page = poppler_document_get_page (pdf_document->document,
1182 set_page_orientation (pdf_document, poppler_page, rc->rotation);
1184 poppler_page_get_size (poppler_page, &width_points, &height_points);
1185 width = (int) ((width_points * rc->scale) + 0.5);
1186 height = (int) ((height_points * rc->scale) + 0.5);
1188 if (*pixbuf == NULL) {
1189 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1194 /* FIXME: Hardcoded clearlooks selection color. We should
1195 * track theme color changes and focus out event and update
1196 * selection color accordingly. */
1197 poppler_page_render_selection (poppler_page,
1199 (PopplerRectangle *)points,
1200 (PopplerRectangle *)old_points,
1201 0x00ffffff, 0x007c99ad);
1202 g_object_unref (poppler_page);
1208 pdf_selection_get_selection_region (EvSelection *selection,
1209 EvRenderContext *rc,
1210 EvRectangle *points)
1212 PdfDocument *pdf_document;
1213 PopplerPage *poppler_page;
1216 pdf_document = PDF_DOCUMENT (selection);
1217 poppler_page = poppler_document_get_page (pdf_document->document,
1219 set_page_orientation (pdf_document, poppler_page, rc->rotation);
1221 retval = poppler_page_get_selection_region (poppler_page, rc->scale, (PopplerRectangle *) points);
1222 g_object_unref (poppler_page);
1228 pdf_selection_get_selection_map (EvSelection *selection,
1229 EvRenderContext *rc)
1231 PdfDocument *pdf_document;
1232 PopplerPage *poppler_page;
1233 PopplerRectangle points;
1236 pdf_document = PDF_DOCUMENT (selection);
1237 poppler_page = poppler_document_get_page (pdf_document->document,
1239 set_page_orientation (pdf_document, poppler_page, rc->rotation);
1243 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1244 retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1245 g_object_unref (poppler_page);
1251 pdf_selection_iface_init (EvSelectionIface *iface)
1253 iface->render_selection = pdf_selection_render_selection;
1254 iface->get_selection_region = pdf_selection_get_selection_region;
1255 iface->get_selection_map = pdf_selection_get_selection_map;
1259 pdf_document_new (void)
1261 return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));