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.
26 #include <poppler-document.h>
27 #include <poppler-page.h>
29 #include <cairo-pdf.h>
34 #include <glib/gi18n-lib.h>
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-mapping.h"
39 #include "ev-document-find.h"
40 #include "ev-document-misc.h"
41 #include "ev-document-links.h"
42 #include "ev-document-images.h"
43 #include "ev-document-fonts.h"
44 #include "ev-document-security.h"
45 #include "ev-document-thumbnails.h"
46 #include "ev-document-transition.h"
47 #include "ev-document-forms.h"
48 #include "ev-document-layers.h"
49 #include "ev-document-print.h"
50 #include "ev-document-annotations.h"
51 #include "ev-document-attachments.h"
52 #include "ev-selection.h"
53 #include "ev-transition-effect.h"
54 #include "ev-attachment.h"
57 #include <libxml/tree.h>
58 #include <libxml/parser.h>
59 #include <libxml/xpath.h>
60 #include <libxml/xpathInternals.h>
62 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
63 #define HAVE_CAIRO_PRINT
67 PdfDocument *document;
76 EvFileExporterFormat format;
86 #ifdef HAVE_CAIRO_PRINT
89 PopplerPSFile *ps_file;
93 struct _PdfDocumentClass
95 EvDocumentClass parent_class;
100 EvDocument parent_instance;
102 PopplerDocument *document;
106 PopplerFontInfo *font_info;
107 PopplerFontsIter *fonts_iter;
108 int fonts_scanned_pages;
110 PdfDocumentSearch *search;
111 PdfPrintContext *print_ctx;
116 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
117 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
118 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
119 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
120 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
121 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
122 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
123 #ifdef HAVE_POPPLER_PAGE_RENDER
124 static void pdf_document_document_print_iface_init (EvDocumentPrintIface *iface);
126 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
127 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface);
128 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
129 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
130 static void pdf_selection_iface_init (EvSelectionIface *iface);
131 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
132 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
136 static int pdf_document_get_n_pages (EvDocument *document);
138 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
140 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
141 PopplerAction *action);
142 static void pdf_document_search_free (PdfDocumentSearch *search);
143 static void pdf_print_context_free (PdfPrintContext *ctx);
145 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
147 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
148 pdf_document_security_iface_init);
149 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
150 pdf_document_document_thumbnails_iface_init);
151 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
152 pdf_document_document_links_iface_init);
153 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
154 pdf_document_document_images_iface_init);
155 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
156 pdf_document_document_forms_iface_init);
157 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
158 pdf_document_document_fonts_iface_init);
159 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
160 pdf_document_document_layers_iface_init);
161 #ifdef HAVE_POPPLER_PAGE_RENDER
162 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
163 pdf_document_document_print_iface_init);
165 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
166 pdf_document_document_annotations_iface_init);
167 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
168 pdf_document_document_attachments_iface_init);
169 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
170 pdf_document_find_iface_init);
171 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
172 pdf_document_file_exporter_iface_init);
173 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
174 pdf_selection_iface_init);
175 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
176 pdf_document_page_transition_iface_init);
180 pdf_document_search_free (PdfDocumentSearch *search)
182 PdfDocument *pdf_document = search->document;
186 if (search->idle != 0)
187 g_source_remove (search->idle);
189 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
190 for (i = 0; i < n_pages; i++) {
191 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
192 g_list_free (search->pages[i]);
194 g_free (search->pages);
196 g_free (search->text);
201 pdf_document_dispose (GObject *object)
203 PdfDocument *pdf_document = PDF_DOCUMENT(object);
205 if (pdf_document->print_ctx) {
206 pdf_print_context_free (pdf_document->print_ctx);
207 pdf_document->print_ctx = NULL;
210 if (pdf_document->search) {
211 pdf_document_search_free (pdf_document->search);
212 pdf_document->search = NULL;
215 if (pdf_document->document) {
216 g_object_unref (pdf_document->document);
219 if (pdf_document->font_info) {
220 poppler_font_info_free (pdf_document->font_info);
223 if (pdf_document->fonts_iter) {
224 poppler_fonts_iter_free (pdf_document->fonts_iter);
227 if (pdf_document->layers) {
228 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
229 g_list_free (pdf_document->layers);
232 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
236 pdf_document_init (PdfDocument *pdf_document)
238 pdf_document->password = NULL;
242 convert_error (GError *poppler_error,
245 if (poppler_error == NULL)
248 if (poppler_error->domain == POPPLER_ERROR) {
249 /* convert poppler errors into EvDocument errors */
250 gint code = EV_DOCUMENT_ERROR_INVALID;
251 if (poppler_error->code == POPPLER_ERROR_INVALID)
252 code = EV_DOCUMENT_ERROR_INVALID;
253 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
254 code = EV_DOCUMENT_ERROR_ENCRYPTED;
256 g_set_error_literal (error,
259 poppler_error->message);
261 g_error_free (poppler_error);
263 g_propagate_error (error, poppler_error);
270 pdf_document_save (EvDocument *document,
274 PdfDocument *pdf_document = PDF_DOCUMENT (document);
276 GError *poppler_error = NULL;
278 if (pdf_document->modified) {
279 retval = poppler_document_save (pdf_document->document,
280 uri, &poppler_error);
282 retval = poppler_document_save_a_copy (pdf_document->document,
283 uri, &poppler_error);
287 convert_error (poppler_error, error);
293 pdf_document_load (EvDocument *document,
297 GError *poppler_error = NULL;
298 PdfDocument *pdf_document = PDF_DOCUMENT (document);
300 pdf_document->document =
301 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
303 if (pdf_document->document == NULL) {
304 convert_error (poppler_error, error);
312 pdf_document_get_n_pages (EvDocument *document)
314 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
318 pdf_document_get_page (EvDocument *document,
321 PdfDocument *pdf_document = PDF_DOCUMENT (document);
322 PopplerPage *poppler_page;
325 poppler_page = poppler_document_get_page (pdf_document->document, index);
326 page = ev_page_new (index);
327 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
328 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
329 g_object_unref (poppler_page);
335 pdf_document_get_page_size (EvDocument *document,
340 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
342 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
346 pdf_document_get_page_label (EvDocument *document,
351 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
353 g_object_get (G_OBJECT (page->backend_page),
359 static cairo_surface_t *
360 pdf_page_render (PopplerPage *page,
365 cairo_surface_t *surface;
367 #ifdef HAVE_POPPLER_PAGE_RENDER
370 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
372 cr = cairo_create (surface);
374 switch (rc->rotation) {
376 cairo_translate (cr, width, 0);
379 cairo_translate (cr, width, height);
382 cairo_translate (cr, 0, height);
385 cairo_translate (cr, 0, 0);
387 cairo_scale (cr, rc->scale, rc->scale);
388 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
389 poppler_page_render (page, cr);
391 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
392 cairo_set_source_rgb (cr, 1., 1., 1.);
396 #else /* HAVE_POPPLER_PAGE_RENDER */
399 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
403 poppler_page_render_to_pixbuf (page,
409 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
410 g_object_unref (pixbuf);
411 #endif /* HAVE_POPPLER_PAGE_RENDER */
416 static cairo_surface_t *
417 pdf_document_render (EvDocument *document,
420 PdfDocument *pdf_document;
421 PopplerPage *poppler_page;
422 double width_points, height_points;
425 poppler_page = POPPLER_PAGE (rc->page->backend_page);
427 poppler_page_get_size (poppler_page,
428 &width_points, &height_points);
430 if (rc->rotation == 90 || rc->rotation == 270) {
431 width = (int) ((height_points * rc->scale) + 0.5);
432 height = (int) ((width_points * rc->scale) + 0.5);
434 width = (int) ((width_points * rc->scale) + 0.5);
435 height = (int) ((height_points * rc->scale) + 0.5);
438 return pdf_page_render (poppler_page,
443 http://www.pdfa.org/lib/exe/fetch.php?id=pdfa%3Aen%3Atechdoc&cache=cache&media=pdfa:techdoc:tn0001_pdfa-1_and_namespaces_2008-03-18.pdf */
445 pdf_document_get_format_from_metadata (const char *metadata)
448 xmlXPathContextPtr xpathCtx;
449 xmlXPathObjectPtr xpathObj;
450 xmlChar *part = NULL;
451 xmlChar *conf = NULL;
455 doc = xmlParseMemory (metadata, strlen (metadata));
457 return NULL; /* invalid xml metadata */
459 xpathCtx = xmlXPathNewContext (doc);
460 if (xpathCtx == NULL) {
462 return NULL; /* invalid xpath context */
465 /* add pdf/a namespaces */
466 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
467 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
468 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
470 /* reads pdf/a part */
471 /* first syntax: child node */
472 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
473 if (xpathObj != NULL) {
474 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
475 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
477 xmlXPathFreeObject (xpathObj);
480 /* second syntax: attribute */
481 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
482 if (xpathObj != NULL) {
483 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
484 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
486 xmlXPathFreeObject (xpathObj);
490 /* reads pdf/a conformance */
491 /* first syntax: child node */
492 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
493 if (xpathObj != NULL) {
494 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
495 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
497 xmlXPathFreeObject (xpathObj);
500 /* second syntax: attribute */
501 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
502 if (xpathObj != NULL) {
503 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
504 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
506 xmlXPathFreeObject (xpathObj);
510 if (part != NULL && conf != NULL) {
511 /* makes conf lowercase */
512 for (i = 0; conf[i]; i++)
513 conf[i] = g_ascii_tolower (conf[i]);
516 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
522 xmlXPathFreeContext (xpathCtx);
529 static EvDocumentInfo *
530 pdf_document_get_info (EvDocument *document)
532 EvDocumentInfo *info;
533 PopplerPageLayout layout;
534 PopplerPageMode mode;
535 PopplerViewerPreferences view_prefs;
536 PopplerPermissions permissions;
541 info = g_new0 (EvDocumentInfo, 1);
543 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
544 EV_DOCUMENT_INFO_FORMAT |
545 EV_DOCUMENT_INFO_AUTHOR |
546 EV_DOCUMENT_INFO_SUBJECT |
547 EV_DOCUMENT_INFO_KEYWORDS |
548 EV_DOCUMENT_INFO_LAYOUT |
549 EV_DOCUMENT_INFO_START_MODE |
550 EV_DOCUMENT_INFO_PERMISSIONS |
551 EV_DOCUMENT_INFO_UI_HINTS |
552 EV_DOCUMENT_INFO_CREATOR |
553 EV_DOCUMENT_INFO_PRODUCER |
554 EV_DOCUMENT_INFO_CREATION_DATE |
555 EV_DOCUMENT_INFO_MOD_DATE |
556 EV_DOCUMENT_INFO_LINEARIZED |
557 EV_DOCUMENT_INFO_N_PAGES |
558 EV_DOCUMENT_INFO_SECURITY |
559 EV_DOCUMENT_INFO_PAPER_SIZE;
561 g_object_get (PDF_DOCUMENT (document)->document,
562 "title", &(info->title),
563 "format", &(info->format),
564 "author", &(info->author),
565 "subject", &(info->subject),
566 "keywords", &(info->keywords),
568 "page-layout", &layout,
569 "viewer-preferences", &view_prefs,
570 "permissions", &permissions,
571 "creator", &(info->creator),
572 "producer", &(info->producer),
573 "creation-date", &(info->creation_date),
574 "mod-date", &(info->modified_date),
575 "linearized", &(info->linearized),
576 "metadata", &metadata,
579 if (metadata != NULL) {
580 fmt = pdf_document_get_format_from_metadata(metadata);
582 g_free (info->format);
588 info->n_pages = ev_document_get_n_pages (document);
590 if (info->n_pages > 0) {
591 page = ev_document_get_page (document, 0);
592 ev_document_get_page_size (document, page,
593 &(info->paper_width),
594 &(info->paper_height));
595 g_object_unref (page);
599 info->paper_width = info->paper_width / 72.0f * 25.4f;
600 info->paper_height = info->paper_height / 72.0f * 25.4f;
604 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
605 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
607 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
608 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
610 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
611 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
613 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
614 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
615 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
616 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
618 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
619 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
626 case POPPLER_PAGE_MODE_NONE:
627 info->mode = EV_DOCUMENT_MODE_NONE;
629 case POPPLER_PAGE_MODE_USE_THUMBS:
630 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
632 case POPPLER_PAGE_MODE_USE_OC:
633 info->mode = EV_DOCUMENT_MODE_USE_OC;
635 case POPPLER_PAGE_MODE_FULL_SCREEN:
636 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
638 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
639 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
645 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
646 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
648 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
649 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
651 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
652 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
654 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
655 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
657 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
658 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
660 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
661 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
663 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
664 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
667 info->permissions = 0;
668 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
669 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
671 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
672 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
674 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
675 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
677 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
678 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
681 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
682 /* translators: this is the document security state */
683 info->security = g_strdup (_("Yes"));
685 /* translators: this is the document security state */
686 info->security = g_strdup (_("No"));
693 pdf_document_class_init (PdfDocumentClass *klass)
695 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
696 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
698 g_object_class->dispose = pdf_document_dispose;
700 ev_document_class->save = pdf_document_save;
701 ev_document_class->load = pdf_document_load;
702 ev_document_class->get_n_pages = pdf_document_get_n_pages;
703 ev_document_class->get_page = pdf_document_get_page;
704 ev_document_class->get_page_size = pdf_document_get_page_size;
705 ev_document_class->get_page_label = pdf_document_get_page_label;
706 ev_document_class->render = pdf_document_render;
707 ev_document_class->get_info = pdf_document_get_info;
710 /* EvDocumentSecurity */
712 pdf_document_has_document_security (EvDocumentSecurity *document_security)
714 /* FIXME: do we really need to have this? */
719 pdf_document_set_password (EvDocumentSecurity *document_security,
720 const char *password)
722 PdfDocument *document = PDF_DOCUMENT (document_security);
724 if (document->password)
725 g_free (document->password);
727 document->password = g_strdup (password);
731 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
733 iface->has_document_security = pdf_document_has_document_security;
734 iface->set_password = pdf_document_set_password;
738 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
740 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
743 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
745 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
749 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
752 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
755 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
757 if (pdf_document->font_info == NULL) {
758 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
761 if (pdf_document->fonts_iter) {
762 poppler_fonts_iter_free (pdf_document->fonts_iter);
765 pdf_document->fonts_scanned_pages += n_pages;
767 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
768 &pdf_document->fonts_iter);
770 pdf_document->fonts_scanned_pages = 0;
771 poppler_font_info_free (pdf_document->font_info);
772 pdf_document->font_info = NULL;
779 font_type_to_string (PopplerFontType type)
782 case POPPLER_FONT_TYPE_TYPE1:
784 case POPPLER_FONT_TYPE_TYPE1C:
786 case POPPLER_FONT_TYPE_TYPE3:
788 case POPPLER_FONT_TYPE_TRUETYPE:
789 return _("TrueType");
790 case POPPLER_FONT_TYPE_CID_TYPE0:
791 return _("Type 1 (CID)");
792 case POPPLER_FONT_TYPE_CID_TYPE0C:
793 return _("Type 1C (CID)");
794 case POPPLER_FONT_TYPE_CID_TYPE2:
795 return _("TrueType (CID)");
797 return _("Unknown font type");
802 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
805 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
806 PopplerFontsIter *iter = pdf_document->fonts_iter;
808 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
814 GtkTreeIter list_iter;
817 const char *embedded;
820 name = poppler_fonts_iter_get_name (iter);
826 type = font_type_to_string (
827 poppler_fonts_iter_get_font_type (iter));
829 if (poppler_fonts_iter_is_embedded (iter)) {
830 if (poppler_fonts_iter_is_subset (iter))
831 embedded = _("Embedded subset");
833 embedded = _("Embedded");
835 embedded = _("Not embedded");
838 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
840 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
841 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
842 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
843 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
847 } while (poppler_fonts_iter_next (iter));
851 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
853 iface->fill_model = pdf_document_fonts_fill_model;
854 iface->scan = pdf_document_fonts_scan;
855 iface->get_progress = pdf_document_fonts_get_progress;
859 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
861 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
862 PopplerIndexIter *iter;
864 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
866 iter = poppler_index_iter_new (pdf_document->document);
869 poppler_index_iter_free (iter);
875 ev_link_dest_from_dest (PdfDocument *pdf_document,
878 EvLinkDest *ev_dest = NULL;
879 const char *unimplemented_dest = NULL;
881 g_assert (dest != NULL);
883 switch (dest->type) {
884 case POPPLER_DEST_XYZ: {
885 PopplerPage *poppler_page;
888 poppler_page = poppler_document_get_page (pdf_document->document,
889 MAX (0, dest->page_num - 1));
890 poppler_page_get_size (poppler_page, NULL, &height);
891 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
893 height - MIN (height, dest->top),
898 g_object_unref (poppler_page);
901 case POPPLER_DEST_FITB:
902 case POPPLER_DEST_FIT:
903 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
905 case POPPLER_DEST_FITBH:
906 case POPPLER_DEST_FITH: {
907 PopplerPage *poppler_page;
910 poppler_page = poppler_document_get_page (pdf_document->document,
911 MAX (0, dest->page_num - 1));
912 poppler_page_get_size (poppler_page, NULL, &height);
913 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
914 height - MIN (height, dest->top),
916 g_object_unref (poppler_page);
919 case POPPLER_DEST_FITBV:
920 case POPPLER_DEST_FITV:
921 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
925 case POPPLER_DEST_FITR: {
926 PopplerPage *poppler_page;
929 poppler_page = poppler_document_get_page (pdf_document->document,
930 MAX (0, dest->page_num - 1));
931 poppler_page_get_size (poppler_page, NULL, &height);
932 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
934 height - MIN (height, dest->bottom),
936 height - MIN (height, dest->top));
937 g_object_unref (poppler_page);
940 case POPPLER_DEST_NAMED:
941 ev_dest = ev_link_dest_new_named (dest->named_dest);
943 case POPPLER_DEST_UNKNOWN:
944 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
948 if (unimplemented_dest) {
949 g_warning ("Unimplemented destination: %s, please post a "
950 "bug report in Evince bugzilla "
951 "(http://bugzilla.gnome.org) with a testcase.",
956 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
962 ev_link_from_action (PdfDocument *pdf_document,
963 PopplerAction *action)
966 EvLinkAction *ev_action = NULL;
967 const char *unimplemented_action = NULL;
969 switch (action->type) {
970 case POPPLER_ACTION_NONE:
972 case POPPLER_ACTION_GOTO_DEST: {
975 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
976 ev_action = ev_link_action_new_dest (dest);
979 case POPPLER_ACTION_GOTO_REMOTE: {
982 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
983 ev_action = ev_link_action_new_remote (dest,
984 action->goto_remote.file_name);
988 case POPPLER_ACTION_LAUNCH:
989 ev_action = ev_link_action_new_launch (action->launch.file_name,
990 action->launch.params);
992 case POPPLER_ACTION_URI:
993 ev_action = ev_link_action_new_external_uri (action->uri.uri);
995 case POPPLER_ACTION_NAMED:
996 ev_action = ev_link_action_new_named (action->named.named_dest);
998 case POPPLER_ACTION_MOVIE:
999 unimplemented_action = "POPPLER_ACTION_MOVIE";
1001 case POPPLER_ACTION_UNKNOWN:
1002 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1005 if (unimplemented_action) {
1006 g_warning ("Unimplemented action: %s, please post a bug report "
1007 "in Evince bugzilla (http://bugzilla.gnome.org) "
1008 "with a testcase.", unimplemented_action);
1011 link = ev_link_new (action->any.title, ev_action);
1017 build_tree (PdfDocument *pdf_document,
1018 GtkTreeModel *model,
1019 GtkTreeIter *parent,
1020 PopplerIndexIter *iter)
1024 GtkTreeIter tree_iter;
1025 PopplerIndexIter *child;
1026 PopplerAction *action;
1027 EvLink *link = NULL;
1031 action = poppler_index_iter_get_action (iter);
1032 expand = poppler_index_iter_is_open (iter);
1037 switch (action->type) {
1038 case POPPLER_ACTION_GOTO_DEST: {
1039 /* For bookmarks, solve named destinations */
1040 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1042 EvLinkDest *ev_dest = NULL;
1043 EvLinkAction *ev_action;
1045 dest = poppler_document_find_dest (pdf_document->document,
1046 action->goto_dest.dest->named_dest);
1048 link = ev_link_from_action (pdf_document, action);
1052 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1053 poppler_dest_free (dest);
1055 ev_action = ev_link_action_new_dest (ev_dest);
1056 link = ev_link_new (action->any.title, ev_action);
1058 link = ev_link_from_action (pdf_document, action);
1063 link = ev_link_from_action (pdf_document, action);
1067 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1068 poppler_action_free (action);
1070 g_object_unref (link);
1075 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1076 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1078 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1079 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1080 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1081 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1084 g_free (title_markup);
1085 g_object_unref (link);
1087 child = poppler_index_iter_get_child (iter);
1089 build_tree (pdf_document, model, &tree_iter, child);
1090 poppler_index_iter_free (child);
1091 poppler_action_free (action);
1093 } while (poppler_index_iter_next (iter));
1096 static GtkTreeModel *
1097 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1099 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1100 GtkTreeModel *model = NULL;
1101 PopplerIndexIter *iter;
1103 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1105 iter = poppler_index_iter_new (pdf_document->document);
1106 /* Create the model if we have items*/
1108 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1113 build_tree (pdf_document, model, NULL, iter);
1114 poppler_index_iter_free (iter);
1121 pdf_document_links_get_links (EvDocumentLinks *document_links,
1124 PdfDocument *pdf_document;
1125 PopplerPage *poppler_page;
1126 GList *retval = NULL;
1127 GList *mapping_list;
1131 pdf_document = PDF_DOCUMENT (document_links);
1132 poppler_page = poppler_document_get_page (pdf_document->document,
1134 mapping_list = poppler_page_get_link_mapping (poppler_page);
1135 poppler_page_get_size (poppler_page, NULL, &height);
1137 for (list = mapping_list; list; list = list->next) {
1138 PopplerLinkMapping *link_mapping;
1139 EvMapping *ev_link_mapping;
1141 link_mapping = (PopplerLinkMapping *)list->data;
1142 ev_link_mapping = g_new (EvMapping, 1);
1143 ev_link_mapping->data = ev_link_from_action (pdf_document,
1144 link_mapping->action);
1145 ev_link_mapping->area.x1 = link_mapping->area.x1;
1146 ev_link_mapping->area.x2 = link_mapping->area.x2;
1147 /* Invert this for X-style coordinates */
1148 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1149 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1151 retval = g_list_prepend (retval, ev_link_mapping);
1154 poppler_page_free_link_mapping (mapping_list);
1155 g_object_unref (poppler_page);
1157 return g_list_reverse (retval);
1161 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1162 const gchar *link_name)
1164 PdfDocument *pdf_document;
1166 EvLinkDest *ev_dest = NULL;
1168 pdf_document = PDF_DOCUMENT (document_links);
1169 dest = poppler_document_find_dest (pdf_document->document,
1172 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1173 poppler_dest_free (dest);
1180 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1182 iface->has_document_links = pdf_document_links_has_document_links;
1183 iface->get_links_model = pdf_document_links_get_links_model;
1184 iface->get_links = pdf_document_links_get_links;
1185 iface->find_link_dest = pdf_document_links_find_link_dest;
1189 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1192 GList *retval = NULL;
1193 PdfDocument *pdf_document;
1194 PopplerPage *poppler_page;
1195 GList *mapping_list;
1198 pdf_document = PDF_DOCUMENT (document_images);
1199 poppler_page = poppler_document_get_page (pdf_document->document, page);
1200 mapping_list = poppler_page_get_image_mapping (poppler_page);
1202 for (list = mapping_list; list; list = list->next) {
1203 PopplerImageMapping *image_mapping;
1204 EvMapping *ev_image_mapping;
1206 image_mapping = (PopplerImageMapping *)list->data;
1208 ev_image_mapping = g_new (EvMapping, 1);
1210 ev_image_mapping->data = ev_image_new (page, image_mapping->image_id);
1211 ev_image_mapping->area.x1 = image_mapping->area.x1;
1212 ev_image_mapping->area.y1 = image_mapping->area.y1;
1213 ev_image_mapping->area.x2 = image_mapping->area.x2;
1214 ev_image_mapping->area.y2 = image_mapping->area.y2;
1216 retval = g_list_prepend (retval, ev_image_mapping);
1219 poppler_page_free_image_mapping (mapping_list);
1220 g_object_unref (poppler_page);
1222 return g_list_reverse (retval);
1226 pdf_document_images_get_image (EvDocumentImages *document_images,
1229 GdkPixbuf *retval = NULL;
1230 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1231 PdfDocument *pdf_document;
1232 PopplerPage *poppler_page;
1233 cairo_surface_t *surface;
1235 pdf_document = PDF_DOCUMENT (document_images);
1236 poppler_page = poppler_document_get_page (pdf_document->document,
1237 ev_image_get_page (image));
1239 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1241 retval = ev_document_misc_pixbuf_from_surface (surface);
1242 cairo_surface_destroy (surface);
1245 g_object_unref (poppler_page);
1251 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1253 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1254 iface->get_image = pdf_document_images_get_image;
1258 make_thumbnail_for_page (PopplerPage *poppler_page,
1259 EvRenderContext *rc,
1265 #ifdef POPPLER_WITH_GDK
1266 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1268 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1270 ev_document_fc_mutex_lock ();
1271 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1273 rc->scale, rc->rotation, pixbuf);
1274 ev_document_fc_mutex_unlock ();
1276 cairo_surface_t *surface;
1278 ev_document_fc_mutex_lock ();
1279 surface = pdf_page_render (poppler_page, width, height, rc);
1280 ev_document_fc_mutex_unlock ();
1282 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1283 cairo_surface_destroy (surface);
1284 #endif /* POPPLER_WITH_GDK */
1290 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1291 EvRenderContext *rc,
1294 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1295 PopplerPage *poppler_page;
1296 GdkPixbuf *pixbuf = NULL;
1297 GdkPixbuf *border_pixbuf;
1300 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1302 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1303 rc, &width, &height);
1305 #ifdef POPPLER_WITH_GDK
1306 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1308 cairo_surface_t *surface;
1310 surface = poppler_page_get_thumbnail (poppler_page);
1312 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1313 cairo_surface_destroy (surface);
1315 #endif /* POPPLER_WITH_GDK */
1317 if (pixbuf != NULL) {
1318 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1319 gdk_pixbuf_get_height (pixbuf) :
1320 gdk_pixbuf_get_width (pixbuf);
1322 if (thumb_width == width) {
1323 GdkPixbuf *rotated_pixbuf;
1325 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1326 (GdkPixbufRotation) (360 - rc->rotation));
1327 g_object_unref (pixbuf);
1328 pixbuf = rotated_pixbuf;
1330 /* The provided thumbnail has a different size */
1331 g_object_unref (pixbuf);
1332 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1335 /* There is no provided thumbnail. We need to make one. */
1336 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1339 if (border && pixbuf) {
1340 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1341 g_object_unref (pixbuf);
1342 pixbuf = border_pixbuf;
1349 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1350 EvRenderContext *rc,
1354 double page_width, page_height;
1356 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1357 &page_width, &page_height);
1359 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1360 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1362 if (rc->rotation == 90 || rc->rotation == 270) {
1372 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1374 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1375 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1380 pdf_document_find_find_text (EvDocumentFind *document_find,
1383 gboolean case_sensitive)
1386 PopplerPage *poppler_page;
1389 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1390 g_return_val_if_fail (text != NULL, NULL);
1392 poppler_page = POPPLER_PAGE (page->backend_page);
1394 matches = poppler_page_find_text (poppler_page, text);
1398 poppler_page_get_size (poppler_page, NULL, &height);
1399 for (l = matches; l && l->data; l = g_list_next (l)) {
1400 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1404 rect->y1 = height - rect->y2;
1405 rect->y2 = height - tmp;
1412 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1414 iface->find_text = pdf_document_find_find_text;
1418 pdf_print_context_free (PdfPrintContext *ctx)
1423 #ifdef HAVE_CAIRO_PRINT
1425 cairo_destroy (ctx->cr);
1430 poppler_ps_file_free (ctx->ps_file);
1431 ctx->ps_file = NULL;
1438 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1439 EvFileExporterContext *fc)
1441 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1442 PdfPrintContext *ctx;
1443 #ifdef HAVE_CAIRO_PRINT
1444 gdouble width, height;
1445 cairo_surface_t *surface = NULL;
1448 if (pdf_document->print_ctx)
1449 pdf_print_context_free (pdf_document->print_ctx);
1450 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1451 ctx = pdf_document->print_ctx;
1452 ctx->format = fc->format;
1454 #ifdef HAVE_CAIRO_PRINT
1455 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1457 ctx->paper_width = fc->paper_width;
1458 ctx->paper_height = fc->paper_height;
1460 switch (fc->pages_per_sheet) {
1488 ctx->pages_printed = 0;
1490 switch (fc->format) {
1491 case EV_FILE_FORMAT_PS:
1492 #ifdef HAVE_CAIRO_PS
1493 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1496 case EV_FILE_FORMAT_PDF:
1497 #ifdef HAVE_CAIRO_PDF
1498 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1502 g_assert_not_reached ();
1505 ctx->cr = cairo_create (surface);
1506 cairo_surface_destroy (surface);
1508 #else /* HAVE_CAIRO_PRINT */
1509 if (ctx->format == EV_FILE_FORMAT_PS) {
1510 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1511 fc->filename, fc->first_page,
1512 fc->last_page - fc->first_page + 1);
1513 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1514 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1516 #endif /* HAVE_CAIRO_PRINT */
1520 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1522 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1523 PdfPrintContext *ctx = pdf_document->print_ctx;
1525 g_return_if_fail (pdf_document->print_ctx != NULL);
1527 ctx->pages_printed = 0;
1529 #ifdef HAVE_CAIRO_PRINT
1530 if (ctx->paper_width > ctx->paper_height) {
1531 if (ctx->format == EV_FILE_FORMAT_PS) {
1532 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1535 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1536 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1541 #endif /* HAVE_CAIRO_PRINT */
1545 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1546 EvRenderContext *rc)
1548 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1549 PdfPrintContext *ctx = pdf_document->print_ctx;
1550 PopplerPage *poppler_page;
1551 #ifdef HAVE_CAIRO_PRINT
1552 gdouble page_width, page_height;
1555 gdouble width, height;
1556 gdouble pwidth, pheight;
1557 gdouble xscale, yscale;
1560 g_return_if_fail (pdf_document->print_ctx != NULL);
1562 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1564 #ifdef HAVE_CAIRO_PRINT
1565 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1566 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1567 poppler_page_get_size (poppler_page, &page_width, &page_height);
1569 if (page_width > page_height && page_width > ctx->paper_width) {
1575 /* Use always portrait mode and rotate when necessary */
1576 if (ctx->paper_width > ctx->paper_height) {
1577 width = ctx->paper_height;
1578 height = ctx->paper_width;
1581 width = ctx->paper_width;
1582 height = ctx->paper_height;
1585 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1598 page_width = page_height;
1602 pwidth = width / ctx->pages_x;
1603 pheight = height / ctx->pages_y;
1605 if ((page_width > pwidth || page_height > pheight) ||
1606 (page_width < pwidth && page_height < pheight)) {
1607 xscale = pwidth / page_width;
1608 yscale = pheight / page_height;
1610 if (yscale < xscale) {
1617 xscale = yscale = 1;
1622 cairo_save (ctx->cr);
1624 cairo_matrix_t matrix;
1626 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1627 cairo_matrix_init (&matrix,
1631 cairo_transform (ctx->cr, &matrix);
1634 cairo_translate (ctx->cr,
1635 x * (rotate ? pheight : pwidth),
1636 y * (rotate ? pwidth : pheight));
1637 cairo_scale (ctx->cr, xscale, yscale);
1639 poppler_page_render_for_printing (poppler_page, ctx->cr);
1641 ctx->pages_printed++;
1643 cairo_restore (ctx->cr);
1644 #else /* HAVE_CAIRO_PRINT */
1645 if (ctx->format == EV_FILE_FORMAT_PS)
1646 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1647 #endif /* HAVE_CAIRO_PRINT */
1651 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1653 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1654 PdfPrintContext *ctx = pdf_document->print_ctx;
1656 g_return_if_fail (pdf_document->print_ctx != NULL);
1658 #ifdef HAVE_CAIRO_PRINT
1659 cairo_show_page (ctx->cr);
1664 pdf_document_file_exporter_end (EvFileExporter *exporter)
1666 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1668 pdf_print_context_free (pdf_document->print_ctx);
1669 pdf_document->print_ctx = NULL;
1672 static EvFileExporterCapabilities
1673 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1675 return (EvFileExporterCapabilities) (
1676 EV_FILE_EXPORTER_CAN_PAGE_SET |
1677 EV_FILE_EXPORTER_CAN_COPIES |
1678 EV_FILE_EXPORTER_CAN_COLLATE |
1679 EV_FILE_EXPORTER_CAN_REVERSE |
1680 EV_FILE_EXPORTER_CAN_SCALE |
1681 #ifdef HAVE_CAIRO_PRINT
1682 #ifdef HAVE_POPPLER_PAGE_RENDER
1683 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1687 #ifdef HAVE_CAIRO_PDF
1688 #ifdef HAVE_POPPLER_PAGE_RENDER
1689 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1692 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1696 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1698 iface->begin = pdf_document_file_exporter_begin;
1699 iface->begin_page = pdf_document_file_exporter_begin_page;
1700 iface->do_page = pdf_document_file_exporter_do_page;
1701 iface->end_page = pdf_document_file_exporter_end_page;
1702 iface->end = pdf_document_file_exporter_end;
1703 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1706 #ifdef HAVE_POPPLER_PAGE_RENDER
1707 /* EvDocumentPrint */
1709 pdf_document_print_print_page (EvDocumentPrint *document,
1713 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1715 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1719 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1721 iface->print_page = pdf_document_print_print_page;
1723 #endif /* HAVE_POPPLER_PAGE_RENDER */
1726 pdf_selection_render_selection (EvSelection *selection,
1727 EvRenderContext *rc,
1728 cairo_surface_t **surface,
1729 EvRectangle *points,
1730 EvRectangle *old_points,
1731 EvSelectionStyle style,
1735 PopplerPage *poppler_page;
1736 double width_points, height_points;
1739 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1741 poppler_page_get_size (poppler_page,
1742 &width_points, &height_points);
1743 width = (int) ((width_points * rc->scale) + 0.5);
1744 height = (int) ((height_points * rc->scale) + 0.5);
1746 #ifdef HAVE_POPPLER_PAGE_RENDER
1748 PopplerColor text_color, base_color;
1750 text_color.red = text->red;
1751 text_color.green = text->green;
1752 text_color.blue = text->blue;
1754 base_color.red = base->red;
1755 base_color.green = base->green;
1756 base_color.blue = base->blue;
1758 if (*surface == NULL) {
1759 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1764 cr = cairo_create (*surface);
1765 cairo_scale (cr, rc->scale, rc->scale);
1766 cairo_surface_set_device_offset (*surface, 0, 0);
1767 memset (cairo_image_surface_get_data (*surface), 0x00,
1768 cairo_image_surface_get_height (*surface) *
1769 cairo_image_surface_get_stride (*surface));
1770 poppler_page_render_selection (poppler_page,
1772 (PopplerRectangle *)points,
1773 (PopplerRectangle *)old_points,
1774 (PopplerSelectionStyle)style,
1778 #else /* HAVE_POPPLER_PAGE_RENDER */
1781 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1785 poppler_page_render_selection_to_pixbuf (poppler_page,
1786 rc->scale, rc->rotation, pixbuf,
1787 (PopplerRectangle *)points,
1788 (PopplerRectangle *)old_points,
1789 (PopplerSelectionStyle)style,
1793 cairo_surface_destroy (*surface);
1794 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1795 g_object_unref (pixbuf);
1796 #endif /* HAVE_POPPLER_PAGE_RENDER */
1800 pdf_selection_get_selected_text (EvSelection *selection,
1801 EvRenderContext *rc,
1802 EvSelectionStyle style,
1803 EvRectangle *points)
1805 PopplerPage *poppler_page;
1810 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1812 poppler_page_get_size (poppler_page, NULL, &height);
1814 r.y1 = height - points->y2;
1816 r.y2 = height - points->y1;
1818 retval = poppler_page_get_text (poppler_page,
1819 (PopplerSelectionStyle)style,
1826 create_gdk_region_from_poppler_region (GList *region)
1831 retval = gdk_region_new ();
1833 for (l = region; l; l = g_list_next (l)) {
1834 PopplerRectangle *rectangle;
1837 rectangle = (PopplerRectangle *)l->data;
1839 rect.x = (gint) rectangle->x1;
1840 rect.y = (gint) rectangle->y1;
1841 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1842 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1843 gdk_region_union_with_rect (retval, &rect);
1845 poppler_rectangle_free (rectangle);
1852 pdf_selection_get_selection_region (EvSelection *selection,
1853 EvRenderContext *rc,
1854 EvSelectionStyle style,
1855 EvRectangle *points)
1857 PopplerPage *poppler_page;
1861 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1863 region = poppler_page_get_selection_region (poppler_page,
1865 (PopplerSelectionStyle)style,
1866 (PopplerRectangle *) points);
1867 retval = create_gdk_region_from_poppler_region (region);
1868 g_list_free (region);
1874 pdf_selection_get_selection_map (EvSelection *selection,
1875 EvRenderContext *rc)
1877 PopplerPage *poppler_page;
1878 PopplerRectangle points;
1882 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1886 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1888 region = poppler_page_get_selection_region (poppler_page, 1.0,
1889 POPPLER_SELECTION_GLYPH,
1891 retval = create_gdk_region_from_poppler_region (region);
1892 g_list_free (region);
1898 pdf_selection_iface_init (EvSelectionIface *iface)
1900 iface->render_selection = pdf_selection_render_selection;
1901 iface->get_selected_text = pdf_selection_get_selected_text;
1902 iface->get_selection_region = pdf_selection_get_selection_region;
1903 iface->get_selection_map = pdf_selection_get_selection_map;
1906 /* Page Transitions */
1908 pdf_document_get_page_duration (EvDocumentTransition *trans,
1911 PdfDocument *pdf_document;
1912 PopplerPage *poppler_page;
1913 gdouble duration = -1;
1915 pdf_document = PDF_DOCUMENT (trans);
1916 poppler_page = poppler_document_get_page (pdf_document->document, page);
1920 duration = poppler_page_get_duration (poppler_page);
1921 g_object_unref (poppler_page);
1926 static EvTransitionEffect *
1927 pdf_document_get_effect (EvDocumentTransition *trans,
1930 PdfDocument *pdf_document;
1931 PopplerPage *poppler_page;
1932 PopplerPageTransition *page_transition;
1933 EvTransitionEffect *effect;
1935 pdf_document = PDF_DOCUMENT (trans);
1936 poppler_page = poppler_document_get_page (pdf_document->document, page);
1941 page_transition = poppler_page_get_transition (poppler_page);
1943 if (!page_transition) {
1944 g_object_unref (poppler_page);
1948 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1949 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1950 "alignment", page_transition->alignment,
1951 "direction", page_transition->direction,
1952 "duration", page_transition->duration,
1953 "angle", page_transition->angle,
1954 "scale", page_transition->scale,
1955 "rectangular", page_transition->rectangular,
1958 poppler_page_transition_free (page_transition);
1959 g_object_unref (poppler_page);
1965 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1967 iface->get_page_duration = pdf_document_get_page_duration;
1968 iface->get_effect = pdf_document_get_effect;
1973 pdf_document_get_crop_box (EvDocument *document,
1977 PdfDocument *pdf_document;
1978 PopplerPage *poppler_page;
1979 PopplerRectangle poppler_rect;
1981 pdf_document = PDF_DOCUMENT (document);
1982 poppler_page = poppler_document_get_page (pdf_document->document, page);
1983 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1984 rect->x1 = poppler_rect.x1;
1985 rect->x2 = poppler_rect.x2;
1986 rect->y1 = poppler_rect.y1;
1987 rect->y2 = poppler_rect.y2;
1990 static EvFormField *
1991 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1993 EvFormField *ev_field = NULL;
1996 gboolean is_read_only;
1998 id = poppler_form_field_get_id (poppler_field);
1999 font_size = poppler_form_field_get_font_size (poppler_field);
2000 is_read_only = poppler_form_field_is_read_only (poppler_field);
2002 switch (poppler_form_field_get_field_type (poppler_field)) {
2003 case POPPLER_FORM_FIELD_TEXT: {
2004 EvFormFieldText *field_text;
2005 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2007 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2008 case POPPLER_FORM_TEXT_NORMAL:
2009 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2011 case POPPLER_FORM_TEXT_MULTILINE:
2012 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2014 case POPPLER_FORM_TEXT_FILE_SELECT:
2015 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2019 ev_field = ev_form_field_text_new (id, ev_text_type);
2020 field_text = EV_FORM_FIELD_TEXT (ev_field);
2022 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2023 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2024 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2025 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2026 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2027 field_text->text = poppler_form_field_text_get_text (poppler_field);
2031 case POPPLER_FORM_FIELD_BUTTON: {
2032 EvFormFieldButton *field_button;
2033 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2035 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2036 case POPPLER_FORM_BUTTON_PUSH:
2037 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2039 case POPPLER_FORM_BUTTON_CHECK:
2040 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2042 case POPPLER_FORM_BUTTON_RADIO:
2043 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2047 ev_field = ev_form_field_button_new (id, ev_button_type);
2048 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2050 field_button->state = poppler_form_field_button_get_state (poppler_field);
2053 case POPPLER_FORM_FIELD_CHOICE: {
2054 EvFormFieldChoice *field_choice;
2055 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2057 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2058 case POPPLER_FORM_CHOICE_COMBO:
2059 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2061 case EV_FORM_FIELD_CHOICE_LIST:
2062 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2066 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2067 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2069 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2070 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2071 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2072 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2074 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2075 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2076 if (field_choice->is_editable)
2077 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2080 case POPPLER_FORM_FIELD_SIGNATURE:
2082 ev_field = ev_form_field_signature_new (id);
2084 case POPPLER_FORM_FIELD_UNKNOWN:
2088 ev_field->font_size = font_size;
2089 ev_field->is_read_only = is_read_only;
2095 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2098 PopplerPage *poppler_page;
2099 GList *retval = NULL;
2104 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2106 poppler_page = POPPLER_PAGE (page->backend_page);
2107 fields = poppler_page_get_form_field_mapping (poppler_page);
2108 poppler_page_get_size (poppler_page, NULL, &height);
2110 for (list = fields; list; list = list->next) {
2111 PopplerFormFieldMapping *mapping;
2112 EvMapping *field_mapping;
2113 EvFormField *ev_field;
2115 mapping = (PopplerFormFieldMapping *)list->data;
2117 ev_field = ev_form_field_from_poppler_field (mapping->field);
2121 field_mapping = g_new0 (EvMapping, 1);
2122 field_mapping->area.x1 = mapping->area.x1;
2123 field_mapping->area.x2 = mapping->area.x2;
2124 field_mapping->area.y1 = height - mapping->area.y2;
2125 field_mapping->area.y2 = height - mapping->area.y1;
2126 field_mapping->data = ev_field;
2127 ev_field->page = EV_PAGE (g_object_ref (page));
2129 g_object_set_data_full (G_OBJECT (ev_field),
2131 g_object_ref (mapping->field),
2132 (GDestroyNotify) g_object_unref);
2134 retval = g_list_prepend (retval, field_mapping);
2137 poppler_page_free_form_field_mapping (fields);
2139 return g_list_reverse (retval);
2143 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2147 PopplerFormField *poppler_field;
2150 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2154 text = poppler_form_field_text_get_text (poppler_field);
2160 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2164 PopplerFormField *poppler_field;
2166 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2170 poppler_form_field_text_set_text (poppler_field, text);
2171 PDF_DOCUMENT (document)->modified = TRUE;
2175 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2179 PopplerFormField *poppler_field;
2181 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2185 poppler_form_field_button_set_state (poppler_field, state);
2186 PDF_DOCUMENT (document)->modified = TRUE;
2190 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2193 PopplerFormField *poppler_field;
2196 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2200 state = poppler_form_field_button_get_state (poppler_field);
2206 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2210 PopplerFormField *poppler_field;
2213 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2217 text = poppler_form_field_choice_get_item (poppler_field, index);
2223 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2226 PopplerFormField *poppler_field;
2229 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2233 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2239 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2243 PopplerFormField *poppler_field;
2246 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2250 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2256 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2260 PopplerFormField *poppler_field;
2262 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2266 poppler_form_field_choice_select_item (poppler_field, index);
2267 PDF_DOCUMENT (document)->modified = TRUE;
2271 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2275 PopplerFormField *poppler_field;
2277 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2281 poppler_form_field_choice_toggle_item (poppler_field, index);
2282 PDF_DOCUMENT (document)->modified = TRUE;
2286 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2289 PopplerFormField *poppler_field;
2291 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2295 poppler_form_field_choice_unselect_all (poppler_field);
2296 PDF_DOCUMENT (document)->modified = TRUE;
2300 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2304 PopplerFormField *poppler_field;
2306 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2310 poppler_form_field_choice_set_text (poppler_field, text);
2311 PDF_DOCUMENT (document)->modified = TRUE;
2315 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2318 PopplerFormField *poppler_field;
2321 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2325 text = poppler_form_field_choice_get_text (poppler_field);
2331 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2333 iface->get_form_fields = pdf_document_forms_get_form_fields;
2334 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2335 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2336 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2337 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2338 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2339 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2340 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2341 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2342 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2343 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2344 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2345 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2350 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2353 PopplerColor *poppler_color;
2355 poppler_color = poppler_annot_get_color (poppler_annot);
2356 if (poppler_color) {
2357 color->red = poppler_color->red;
2358 color->green = poppler_color->green;
2359 color->blue = poppler_color->blue;
2361 g_free (poppler_color);
2362 } /* TODO: else use a default color */
2365 static EvAnnotation *
2366 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2369 EvAnnotation *ev_annot = NULL;
2370 const gchar *unimplemented_annot = NULL;
2372 switch (poppler_annot_get_annot_type (poppler_annot)) {
2373 case POPPLER_ANNOT_TEXT:
2374 PopplerAnnotText *poppler_text;
2375 EvAnnotationText *ev_annot_text;
2377 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2379 ev_annot = ev_annotation_text_new (page);
2381 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2382 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2385 case POPPLER_ANNOT_LINK:
2386 case POPPLER_ANNOT_WIDGET:
2387 /* Ignore link and widgets annots since they are already handled */
2390 GEnumValue *enum_value;
2392 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2393 poppler_annot_get_annot_type (poppler_annot));
2394 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2398 if (unimplemented_annot) {
2399 g_warning ("Unimplemented annotation: %s, please post a "
2400 "bug report in Evince bugzilla "
2401 "(http://bugzilla.gnome.org) with a testcase.",
2402 unimplemented_annot);
2406 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2407 ev_annot->name = poppler_annot_get_name (poppler_annot);
2408 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2409 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2411 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2412 PopplerAnnotMarkup *markup;
2415 PopplerRectangle poppler_rect;
2417 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2419 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2420 EvRectangle ev_rect;
2424 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2426 ev_rect.x1 = poppler_rect.x1;
2427 ev_rect.x2 = poppler_rect.x2;
2428 ev_rect.y1 = height - poppler_rect.y2;
2429 ev_rect.y2 = height - poppler_rect.y1;
2431 is_open = poppler_annot_markup_get_popup_is_open (markup);
2433 g_object_set (ev_annot,
2434 "rectangle", &ev_rect,
2439 /* FIXME: Use poppler_annot_markup_has_popup() when
2440 * new poppler is released.
2442 g_object_set (ev_annot,
2447 label = poppler_annot_markup_get_label (markup);
2448 opacity = poppler_annot_markup_get_opacity (markup);
2450 g_object_set (ev_annot,
2463 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2466 GList *retval = NULL;
2467 PdfDocument *pdf_document;
2468 PopplerPage *poppler_page;
2474 pdf_document = PDF_DOCUMENT (document_annotations);
2475 poppler_page = POPPLER_PAGE (page->backend_page);
2476 annots = poppler_page_get_annot_mapping (poppler_page);
2477 poppler_page_get_size (poppler_page, NULL, &height);
2479 for (list = annots; list; list = list->next) {
2480 PopplerAnnotMapping *mapping;
2481 EvMapping *annot_mapping;
2482 EvAnnotation *ev_annot;
2484 mapping = (PopplerAnnotMapping *)list->data;
2486 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2492 /* Make sure annot has a unique name */
2493 if (!ev_annot->name)
2494 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2496 annot_mapping = g_new (EvMapping, 1);
2497 annot_mapping->area.x1 = mapping->area.x1;
2498 annot_mapping->area.x2 = mapping->area.x2;
2499 annot_mapping->area.y1 = height - mapping->area.y2;
2500 annot_mapping->area.y2 = height - mapping->area.y1;
2501 annot_mapping->data = ev_annot;
2503 g_object_set_data_full (G_OBJECT (ev_annot),
2505 g_object_ref (mapping->annot),
2506 (GDestroyNotify) g_object_unref);
2508 retval = g_list_prepend (retval, annot_mapping);
2511 poppler_page_free_annot_mapping (annots);
2513 return g_list_reverse (retval);
2517 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2518 EvAnnotation *annot,
2519 const gchar *contents)
2521 PopplerAnnot *poppler_annot;
2523 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2527 poppler_annot_set_contents (poppler_annot, contents);
2528 PDF_DOCUMENT (document)->modified = TRUE;
2532 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2534 iface->get_annotations = pdf_document_annotations_get_annotations;
2535 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2539 struct SaveToBufferData {
2545 attachment_save_to_buffer_callback (const gchar *buf,
2550 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2554 if (sdata->len + count > sdata->max) {
2555 new_max = MAX (sdata->max * 2, sdata->len + count);
2556 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2558 sdata->buffer = new_buffer;
2559 sdata->max = new_max;
2562 memcpy (sdata->buffer + sdata->len, buf, count);
2563 sdata->len += count;
2569 attachment_save_to_buffer (PopplerAttachment *attachment,
2574 static const gint initial_max = 1024;
2575 struct SaveToBufferData sdata;
2580 sdata.buffer = (gchar *) g_malloc (initial_max);
2581 sdata.max = initial_max;
2584 if (! poppler_attachment_save_to_callback (attachment,
2585 attachment_save_to_buffer_callback,
2588 g_free (sdata.buffer);
2592 *buffer = sdata.buffer;
2593 *buffer_size = sdata.len;
2599 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2601 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2604 GList *retval = NULL;
2606 attachments = poppler_document_get_attachments (pdf_document->document);
2608 for (list = attachments; list; list = list->next) {
2609 PopplerAttachment *attachment;
2610 EvAttachment *ev_attachment;
2613 GError *error = NULL;
2615 attachment = (PopplerAttachment *) list->data;
2617 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2618 ev_attachment = ev_attachment_new (attachment->name,
2619 attachment->description,
2624 retval = g_list_prepend (retval, ev_attachment);
2627 g_warning ("%s", error->message);
2628 g_error_free (error);
2634 g_object_unref (attachment);
2637 return g_list_reverse (retval);
2641 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2643 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2645 return poppler_document_has_attachments (pdf_document->document);
2649 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface)
2651 iface->has_attachments = pdf_document_attachments_has_attachments;
2652 iface->get_attachments = pdf_document_attachments_get_attachments;
2657 pdf_document_layers_has_layers (EvDocumentLayers *document)
2659 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2660 PopplerLayersIter *iter;
2662 iter = poppler_layers_iter_new (pdf_document->document);
2665 poppler_layers_iter_free (iter);
2671 build_layers_tree (PdfDocument *pdf_document,
2672 GtkTreeModel *model,
2673 GtkTreeIter *parent,
2674 PopplerLayersIter *iter)
2677 GtkTreeIter tree_iter;
2678 PopplerLayersIter *child;
2679 PopplerLayer *layer;
2680 EvLayer *ev_layer = NULL;
2685 layer = poppler_layers_iter_get_layer (iter);
2687 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2688 visible = poppler_layer_is_visible (layer);
2689 rb_group = poppler_layer_get_radio_button_group_id (layer);
2690 pdf_document->layers = g_list_append (pdf_document->layers,
2691 g_object_ref (layer));
2692 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2693 poppler_layer_is_parent (layer),
2698 title = poppler_layers_iter_get_title (iter);
2699 markup = g_markup_escape_text (title, -1);
2706 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2707 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2708 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2709 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2710 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2711 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2712 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2713 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2716 g_object_unref (ev_layer);
2719 child = poppler_layers_iter_get_child (iter);
2721 build_layers_tree (pdf_document, model, &tree_iter, child);
2722 poppler_layers_iter_free (child);
2723 } while (poppler_layers_iter_next (iter));
2726 static GtkTreeModel *
2727 pdf_document_layers_get_layers (EvDocumentLayers *document)
2729 GtkTreeModel *model = NULL;
2730 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2731 PopplerLayersIter *iter;
2733 iter = poppler_layers_iter_new (pdf_document->document);
2735 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2736 G_TYPE_STRING, /* TITLE */
2737 G_TYPE_OBJECT, /* LAYER */
2738 G_TYPE_BOOLEAN, /* VISIBLE */
2739 G_TYPE_BOOLEAN, /* ENABLED */
2740 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2741 G_TYPE_INT); /* RBGROUP */
2742 build_layers_tree (pdf_document, model, NULL, iter);
2743 poppler_layers_iter_free (iter);
2749 pdf_document_layers_show_layer (EvDocumentLayers *document,
2752 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2753 guint layer_id = ev_layer_get_id (layer);
2755 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2759 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2762 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2763 guint layer_id = ev_layer_get_id (layer);
2765 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2769 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2772 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2773 guint layer_id = ev_layer_get_id (layer);
2775 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2779 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2781 iface->has_layers = pdf_document_layers_has_layers;
2782 iface->get_layers = pdf_document_layers_get_layers;
2783 iface->show_layer = pdf_document_layers_show_layer;
2784 iface->hide_layer = pdf_document_layers_hide_layer;
2785 iface->layer_is_visible = pdf_document_layers_layer_is_visible;