1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
4 * Copyright (C) 2009, Juanjo MarĂn <juanj.marin@juntadeandalucia.es>
5 * Copyright (C) 2004, Red Hat, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <poppler-document.h>
29 #include <poppler-page.h>
31 #include <cairo-pdf.h>
36 #include <glib/gi18n-lib.h>
38 #include "ev-poppler.h"
39 #include "ev-file-exporter.h"
40 #include "ev-mapping.h"
41 #include "ev-document-find.h"
42 #include "ev-document-misc.h"
43 #include "ev-document-links.h"
44 #include "ev-document-images.h"
45 #include "ev-document-fonts.h"
46 #include "ev-document-security.h"
47 #include "ev-document-thumbnails.h"
48 #include "ev-document-transition.h"
49 #include "ev-document-forms.h"
50 #include "ev-document-layers.h"
51 #include "ev-document-print.h"
52 #include "ev-document-annotations.h"
53 #include "ev-document-attachments.h"
54 #include "ev-selection.h"
55 #include "ev-transition-effect.h"
56 #include "ev-attachment.h"
59 #include <libxml/tree.h>
60 #include <libxml/parser.h>
61 #include <libxml/xpath.h>
62 #include <libxml/xpathInternals.h>
64 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
65 #define HAVE_CAIRO_PRINT
68 /* fields from the XMP Rights Management Schema, XMP Specification Sept 2005, pag. 45 */
69 #define LICENSE_MARKED "/x:xmpmeta/rdf:RDF/rdf:Description/xmpRights:Marked"
70 #define LICENSE_TEXT "/x:xmpmeta/rdf:RDF/rdf:Description/dc:rights/rdf:Alt/rdf:li[lang('%s')]"
71 #define LICENSE_WEB_STATEMENT "/x:xmpmeta/rdf:RDF/rdf:Description/xmpRights:WebStatement"
72 /* license field from Creative Commons schema, http://creativecommons.org/ns */
73 #define LICENSE_URI "/x:xmpmeta/rdf:RDF/rdf:Description/cc:license/@rdf:resource"
76 PdfDocument *document;
85 EvFileExporterFormat format;
95 #ifdef HAVE_CAIRO_PRINT
98 PopplerPSFile *ps_file;
102 struct _PdfDocumentClass
104 EvDocumentClass parent_class;
109 EvDocument parent_instance;
111 PopplerDocument *document;
115 PopplerFontInfo *font_info;
116 PopplerFontsIter *fonts_iter;
117 int fonts_scanned_pages;
119 PdfDocumentSearch *search;
120 PdfPrintContext *print_ctx;
125 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
126 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
127 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
128 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
129 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
130 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
131 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
132 #ifdef HAVE_POPPLER_PAGE_RENDER
133 static void pdf_document_document_print_iface_init (EvDocumentPrintIface *iface);
135 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
136 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface);
137 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
138 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
139 static void pdf_selection_iface_init (EvSelectionIface *iface);
140 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
141 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
145 static int pdf_document_get_n_pages (EvDocument *document);
147 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
149 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
150 PopplerAction *action);
151 static void pdf_document_search_free (PdfDocumentSearch *search);
152 static void pdf_print_context_free (PdfPrintContext *ctx);
153 static gboolean attachment_save_to_buffer (PopplerAttachment *attachment,
158 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
160 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
161 pdf_document_security_iface_init);
162 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
163 pdf_document_document_thumbnails_iface_init);
164 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
165 pdf_document_document_links_iface_init);
166 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
167 pdf_document_document_images_iface_init);
168 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
169 pdf_document_document_forms_iface_init);
170 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
171 pdf_document_document_fonts_iface_init);
172 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
173 pdf_document_document_layers_iface_init);
174 #ifdef HAVE_POPPLER_PAGE_RENDER
175 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
176 pdf_document_document_print_iface_init);
178 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
179 pdf_document_document_annotations_iface_init);
180 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
181 pdf_document_document_attachments_iface_init);
182 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
183 pdf_document_find_iface_init);
184 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
185 pdf_document_file_exporter_iface_init);
186 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
187 pdf_selection_iface_init);
188 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
189 pdf_document_page_transition_iface_init);
193 pdf_document_search_free (PdfDocumentSearch *search)
195 PdfDocument *pdf_document = search->document;
199 if (search->idle != 0)
200 g_source_remove (search->idle);
202 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
203 for (i = 0; i < n_pages; i++) {
204 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
205 g_list_free (search->pages[i]);
207 g_free (search->pages);
209 g_free (search->text);
214 pdf_document_dispose (GObject *object)
216 PdfDocument *pdf_document = PDF_DOCUMENT(object);
218 if (pdf_document->print_ctx) {
219 pdf_print_context_free (pdf_document->print_ctx);
220 pdf_document->print_ctx = NULL;
223 if (pdf_document->search) {
224 pdf_document_search_free (pdf_document->search);
225 pdf_document->search = NULL;
228 if (pdf_document->document) {
229 g_object_unref (pdf_document->document);
232 if (pdf_document->font_info) {
233 poppler_font_info_free (pdf_document->font_info);
236 if (pdf_document->fonts_iter) {
237 poppler_fonts_iter_free (pdf_document->fonts_iter);
240 if (pdf_document->layers) {
241 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
242 g_list_free (pdf_document->layers);
245 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
249 pdf_document_init (PdfDocument *pdf_document)
251 pdf_document->password = NULL;
255 convert_error (GError *poppler_error,
258 if (poppler_error == NULL)
261 if (poppler_error->domain == POPPLER_ERROR) {
262 /* convert poppler errors into EvDocument errors */
263 gint code = EV_DOCUMENT_ERROR_INVALID;
264 if (poppler_error->code == POPPLER_ERROR_INVALID)
265 code = EV_DOCUMENT_ERROR_INVALID;
266 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
267 code = EV_DOCUMENT_ERROR_ENCRYPTED;
269 g_set_error_literal (error,
272 poppler_error->message);
274 g_error_free (poppler_error);
276 g_propagate_error (error, poppler_error);
283 pdf_document_save (EvDocument *document,
287 PdfDocument *pdf_document = PDF_DOCUMENT (document);
289 GError *poppler_error = NULL;
291 if (pdf_document->modified) {
292 retval = poppler_document_save (pdf_document->document,
293 uri, &poppler_error);
295 retval = poppler_document_save_a_copy (pdf_document->document,
296 uri, &poppler_error);
300 convert_error (poppler_error, error);
306 pdf_document_load (EvDocument *document,
310 GError *poppler_error = NULL;
311 PdfDocument *pdf_document = PDF_DOCUMENT (document);
313 pdf_document->document =
314 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
316 if (pdf_document->document == NULL) {
317 convert_error (poppler_error, error);
325 pdf_document_get_n_pages (EvDocument *document)
327 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
331 pdf_document_get_page (EvDocument *document,
334 PdfDocument *pdf_document = PDF_DOCUMENT (document);
335 PopplerPage *poppler_page;
338 poppler_page = poppler_document_get_page (pdf_document->document, index);
339 page = ev_page_new (index);
340 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
341 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
342 g_object_unref (poppler_page);
348 pdf_document_get_page_size (EvDocument *document,
353 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
355 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
359 pdf_document_get_page_label (EvDocument *document,
364 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
366 g_object_get (G_OBJECT (page->backend_page),
372 static cairo_surface_t *
373 pdf_page_render (PopplerPage *page,
378 cairo_surface_t *surface;
380 #ifdef HAVE_POPPLER_PAGE_RENDER
383 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
385 cr = cairo_create (surface);
387 switch (rc->rotation) {
389 cairo_translate (cr, width, 0);
392 cairo_translate (cr, width, height);
395 cairo_translate (cr, 0, height);
398 cairo_translate (cr, 0, 0);
400 cairo_scale (cr, rc->scale, rc->scale);
401 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
402 poppler_page_render (page, cr);
404 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
405 cairo_set_source_rgb (cr, 1., 1., 1.);
409 #else /* HAVE_POPPLER_PAGE_RENDER */
412 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
416 poppler_page_render_to_pixbuf (page,
422 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
423 g_object_unref (pixbuf);
424 #endif /* HAVE_POPPLER_PAGE_RENDER */
429 static cairo_surface_t *
430 pdf_document_render (EvDocument *document,
433 PdfDocument *pdf_document;
434 PopplerPage *poppler_page;
435 double width_points, height_points;
438 poppler_page = POPPLER_PAGE (rc->page->backend_page);
440 poppler_page_get_size (poppler_page,
441 &width_points, &height_points);
443 if (rc->rotation == 90 || rc->rotation == 270) {
444 width = (int) ((height_points * rc->scale) + 0.5);
445 height = (int) ((width_points * rc->scale) + 0.5);
447 width = (int) ((width_points * rc->scale) + 0.5);
448 height = (int) ((height_points * rc->scale) + 0.5);
451 return pdf_page_render (poppler_page,
456 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 */
458 pdf_document_get_format_from_metadata (xmlDocPtr doc,
459 xmlXPathContextPtr xpathCtx)
461 xmlXPathObjectPtr xpathObj;
462 xmlChar *part = NULL;
463 xmlChar *conf = NULL;
467 /* add pdf/a namespaces */
468 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
469 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
470 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
472 /* reads pdf/a part */
473 /* first syntax: child node */
474 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
475 if (xpathObj != NULL) {
476 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
477 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
479 xmlXPathFreeObject (xpathObj);
482 /* second syntax: attribute */
483 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
484 if (xpathObj != NULL) {
485 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
486 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
488 xmlXPathFreeObject (xpathObj);
492 /* reads pdf/a conformance */
493 /* first syntax: child node */
494 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
495 if (xpathObj != NULL) {
496 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
497 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
499 xmlXPathFreeObject (xpathObj);
502 /* second syntax: attribute */
503 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
504 if (xpathObj != NULL) {
505 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
506 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
508 xmlXPathFreeObject (xpathObj);
512 if (part != NULL && conf != NULL) {
513 /* makes conf lowercase */
514 for (i = 0; conf[i]; i++)
515 conf[i] = g_ascii_tolower (conf[i]);
518 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
528 static EvDocumentLicense *
529 pdf_document_get_license_from_metadata (xmlDocPtr doc,
530 xmlXPathContextPtr xpathCtx)
532 xmlXPathObjectPtr xpathObj;
533 xmlChar *marked = NULL;
534 const char *language_string;
537 gchar *tag, *tag_aux;
539 EvDocumentLicense *license;
541 /* register namespaces */
542 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
543 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
544 xmlXPathRegisterNs (xpathCtx, BAD_CAST "dc", BAD_CAST "http://purl.org/dc/elements/1.1/");
545 /* XMP Rights Management Schema */
546 xmlXPathRegisterNs (xpathCtx, BAD_CAST "xmpRights", BAD_CAST "http://ns.adobe.com/xap/1.0/rights/");
547 /* Creative Commons Schema */
548 xmlXPathRegisterNs (xpathCtx, BAD_CAST "cc", BAD_CAST "http://creativecommons.org/ns#");
550 /* checking if the document has been marked as defined on the XMP Rights
551 * Management Schema */
552 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_MARKED, xpathCtx);
553 if (xpathObj != NULL) {
554 if (xpathObj->nodesetval != NULL &&
555 xpathObj->nodesetval->nodeNr != 0)
556 marked = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
557 xmlXPathFreeObject (xpathObj);
560 /* a) Not marked => No XMP Rights information */
566 license = ev_document_license_new ();
568 /* b) Marked False => Public Domain, no copyrighted material and no
570 if (g_strrstr ((char *) marked, "False") != NULL) {
571 license->text = g_strdup (_("This work is in the Public Domain"));
572 /* c) Marked True => Copyrighted material */
574 /* Checking usage terms as defined by the XMP Rights Management
575 * Schema. This field is recomended to be checked by Creative
577 /* 1) checking for a suitable localized string */
578 language_string = pango_language_to_string (gtk_get_default_language ());
579 tags = g_strsplit (language_string, "-", -1);
580 i = g_strv_length (tags);
581 while (i-- && !license->text) {
582 tag = g_strdup (tags[0]);
583 for (j = 1; j <= i; j++) {
584 tag_aux = g_strdup_printf ("%s-%s", tag, tags[j]);
588 aux = g_strdup_printf (LICENSE_TEXT, tag);
589 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
590 if (xpathObj != NULL) {
591 if (xpathObj->nodesetval != NULL &&
592 xpathObj->nodesetval->nodeNr != 0)
593 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
594 xmlXPathFreeObject (xpathObj);
601 /* 2) if not, use the default string */
602 if (!license->text) {
603 aux = g_strdup_printf (LICENSE_TEXT, "x-default");
604 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
605 if (xpathObj != NULL) {
606 if (xpathObj->nodesetval != NULL &&
607 xpathObj->nodesetval->nodeNr != 0)
608 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
609 xmlXPathFreeObject (xpathObj);
614 /* Checking the license URI as defined by the Creative Commons
615 * Schema. This field is recomended to be checked by Creative
617 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_URI, xpathCtx);
618 if (xpathObj != NULL) {
619 if (xpathObj->nodesetval != NULL &&
620 xpathObj->nodesetval->nodeNr != 0)
621 license->uri = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
622 xmlXPathFreeObject (xpathObj);
625 /* Checking the web statement as defined by the XMP Rights
626 * Management Schema. Checking it out is a sort of above-and-beyond
627 * the basic recommendations by Creative Commons. It can be
628 * considered as a "reinforcement" approach to add certainty. */
629 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_WEB_STATEMENT, xpathCtx);
630 if (xpathObj != NULL) {
631 if (xpathObj->nodesetval != NULL &&
632 xpathObj->nodesetval->nodeNr != 0)
633 license->web_statement = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
634 xmlXPathFreeObject (xpathObj);
639 if (!license->text && !license->uri && !license->web_statement) {
640 ev_document_license_free (license);
648 pdf_document_parse_metadata (const gchar *metadata,
649 EvDocumentInfo *info)
652 xmlXPathContextPtr xpathCtx;
655 doc = xmlParseMemory (metadata, strlen (metadata));
657 return; /* invalid xml metadata */
659 xpathCtx = xmlXPathNewContext (doc);
660 if (xpathCtx == NULL) {
662 return; /* invalid xpath context */
665 fmt = pdf_document_get_format_from_metadata (doc, xpathCtx);
667 g_free (info->format);
671 info->license = pdf_document_get_license_from_metadata (doc, xpathCtx);
673 xmlXPathFreeContext (xpathCtx);
678 static EvDocumentInfo *
679 pdf_document_get_info (EvDocument *document)
681 EvDocumentInfo *info;
682 PopplerPageLayout layout;
683 PopplerPageMode mode;
684 PopplerViewerPreferences view_prefs;
685 PopplerPermissions permissions;
689 info = g_new0 (EvDocumentInfo, 1);
691 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
692 EV_DOCUMENT_INFO_FORMAT |
693 EV_DOCUMENT_INFO_AUTHOR |
694 EV_DOCUMENT_INFO_SUBJECT |
695 EV_DOCUMENT_INFO_KEYWORDS |
696 EV_DOCUMENT_INFO_LAYOUT |
697 EV_DOCUMENT_INFO_START_MODE |
698 EV_DOCUMENT_INFO_PERMISSIONS |
699 EV_DOCUMENT_INFO_UI_HINTS |
700 EV_DOCUMENT_INFO_CREATOR |
701 EV_DOCUMENT_INFO_PRODUCER |
702 EV_DOCUMENT_INFO_CREATION_DATE |
703 EV_DOCUMENT_INFO_MOD_DATE |
704 EV_DOCUMENT_INFO_LINEARIZED |
705 EV_DOCUMENT_INFO_N_PAGES |
706 EV_DOCUMENT_INFO_SECURITY |
707 EV_DOCUMENT_INFO_PAPER_SIZE |
708 EV_DOCUMENT_INFO_LICENSE;
710 g_object_get (PDF_DOCUMENT (document)->document,
711 "title", &(info->title),
712 "format", &(info->format),
713 "author", &(info->author),
714 "subject", &(info->subject),
715 "keywords", &(info->keywords),
717 "page-layout", &layout,
718 "viewer-preferences", &view_prefs,
719 "permissions", &permissions,
720 "creator", &(info->creator),
721 "producer", &(info->producer),
722 "creation-date", &(info->creation_date),
723 "mod-date", &(info->modified_date),
724 "linearized", &(info->linearized),
725 "metadata", &metadata,
728 if (metadata != NULL) {
729 pdf_document_parse_metadata (metadata, info);
733 info->n_pages = ev_document_get_n_pages (document);
735 if (info->n_pages > 0) {
736 ev_document_get_page_size (document, 0,
737 &(info->paper_width),
738 &(info->paper_height));
740 info->paper_width = info->paper_width / 72.0f * 25.4f;
741 info->paper_height = info->paper_height / 72.0f * 25.4f;
745 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
746 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
748 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
749 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
751 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
752 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
754 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
755 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
756 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
757 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
759 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
760 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
767 case POPPLER_PAGE_MODE_NONE:
768 info->mode = EV_DOCUMENT_MODE_NONE;
770 case POPPLER_PAGE_MODE_USE_THUMBS:
771 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
773 case POPPLER_PAGE_MODE_USE_OC:
774 info->mode = EV_DOCUMENT_MODE_USE_OC;
776 case POPPLER_PAGE_MODE_FULL_SCREEN:
777 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
779 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
780 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
786 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
787 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
789 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
790 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
792 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
793 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
795 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
796 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
798 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
799 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
801 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
802 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
804 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
805 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
808 info->permissions = 0;
809 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
810 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
812 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
813 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
815 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
816 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
818 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
819 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
822 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
823 /* translators: this is the document security state */
824 info->security = g_strdup (_("Yes"));
826 /* translators: this is the document security state */
827 info->security = g_strdup (_("No"));
834 pdf_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
836 PopplerBackend backend;
838 backend = poppler_get_backend ();
840 case POPPLER_BACKEND_CAIRO:
841 info->name = "poppler/cairo";
843 case POPPLER_BACKEND_SPLASH:
844 info->name = "poppler/splash";
847 info->name = "poppler/unknown";
851 info->version = poppler_get_version ();
857 pdf_document_class_init (PdfDocumentClass *klass)
859 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
860 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
862 g_object_class->dispose = pdf_document_dispose;
864 ev_document_class->save = pdf_document_save;
865 ev_document_class->load = pdf_document_load;
866 ev_document_class->get_n_pages = pdf_document_get_n_pages;
867 ev_document_class->get_page = pdf_document_get_page;
868 ev_document_class->get_page_size = pdf_document_get_page_size;
869 ev_document_class->get_page_label = pdf_document_get_page_label;
870 ev_document_class->render = pdf_document_render;
871 ev_document_class->get_info = pdf_document_get_info;
872 ev_document_class->get_backend_info = pdf_document_get_backend_info;
875 /* EvDocumentSecurity */
877 pdf_document_has_document_security (EvDocumentSecurity *document_security)
879 /* FIXME: do we really need to have this? */
884 pdf_document_set_password (EvDocumentSecurity *document_security,
885 const char *password)
887 PdfDocument *document = PDF_DOCUMENT (document_security);
889 if (document->password)
890 g_free (document->password);
892 document->password = g_strdup (password);
896 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
898 iface->has_document_security = pdf_document_has_document_security;
899 iface->set_password = pdf_document_set_password;
903 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
905 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
908 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
910 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
914 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
917 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
920 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
922 if (pdf_document->font_info == NULL) {
923 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
926 if (pdf_document->fonts_iter) {
927 poppler_fonts_iter_free (pdf_document->fonts_iter);
930 pdf_document->fonts_scanned_pages += n_pages;
932 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
933 &pdf_document->fonts_iter);
935 pdf_document->fonts_scanned_pages = 0;
936 poppler_font_info_free (pdf_document->font_info);
937 pdf_document->font_info = NULL;
944 font_type_to_string (PopplerFontType type)
947 case POPPLER_FONT_TYPE_TYPE1:
949 case POPPLER_FONT_TYPE_TYPE1C:
951 case POPPLER_FONT_TYPE_TYPE3:
953 case POPPLER_FONT_TYPE_TRUETYPE:
954 return _("TrueType");
955 case POPPLER_FONT_TYPE_CID_TYPE0:
956 return _("Type 1 (CID)");
957 case POPPLER_FONT_TYPE_CID_TYPE0C:
958 return _("Type 1C (CID)");
959 case POPPLER_FONT_TYPE_CID_TYPE2:
960 return _("TrueType (CID)");
962 return _("Unknown font type");
967 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
970 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
971 PopplerFontsIter *iter = pdf_document->fonts_iter;
973 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
979 GtkTreeIter list_iter;
982 const char *embedded;
985 name = poppler_fonts_iter_get_name (iter);
991 type = font_type_to_string (
992 poppler_fonts_iter_get_font_type (iter));
994 if (poppler_fonts_iter_is_embedded (iter)) {
995 if (poppler_fonts_iter_is_subset (iter))
996 embedded = _("Embedded subset");
998 embedded = _("Embedded");
1000 embedded = _("Not embedded");
1003 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
1005 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
1006 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
1007 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
1008 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
1012 } while (poppler_fonts_iter_next (iter));
1016 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
1018 iface->fill_model = pdf_document_fonts_fill_model;
1019 iface->scan = pdf_document_fonts_scan;
1020 iface->get_progress = pdf_document_fonts_get_progress;
1024 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
1026 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1027 PopplerIndexIter *iter;
1029 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
1031 iter = poppler_index_iter_new (pdf_document->document);
1034 poppler_index_iter_free (iter);
1040 ev_link_dest_from_dest (PdfDocument *pdf_document,
1043 EvLinkDest *ev_dest = NULL;
1044 const char *unimplemented_dest = NULL;
1046 g_assert (dest != NULL);
1048 switch (dest->type) {
1049 case POPPLER_DEST_XYZ: {
1050 PopplerPage *poppler_page;
1053 poppler_page = poppler_document_get_page (pdf_document->document,
1054 MAX (0, dest->page_num - 1));
1055 poppler_page_get_size (poppler_page, NULL, &height);
1056 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1058 height - MIN (height, dest->top),
1063 g_object_unref (poppler_page);
1066 case POPPLER_DEST_FITB:
1067 case POPPLER_DEST_FIT:
1068 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1070 case POPPLER_DEST_FITBH:
1071 case POPPLER_DEST_FITH: {
1072 PopplerPage *poppler_page;
1075 poppler_page = poppler_document_get_page (pdf_document->document,
1076 MAX (0, dest->page_num - 1));
1077 poppler_page_get_size (poppler_page, NULL, &height);
1078 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1079 height - MIN (height, dest->top),
1081 g_object_unref (poppler_page);
1084 case POPPLER_DEST_FITBV:
1085 case POPPLER_DEST_FITV:
1086 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1090 case POPPLER_DEST_FITR: {
1091 PopplerPage *poppler_page;
1094 poppler_page = poppler_document_get_page (pdf_document->document,
1095 MAX (0, dest->page_num - 1));
1096 poppler_page_get_size (poppler_page, NULL, &height);
1097 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1099 height - MIN (height, dest->bottom),
1101 height - MIN (height, dest->top));
1102 g_object_unref (poppler_page);
1105 case POPPLER_DEST_NAMED:
1106 ev_dest = ev_link_dest_new_named (dest->named_dest);
1108 case POPPLER_DEST_UNKNOWN:
1109 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1113 if (unimplemented_dest) {
1114 g_warning ("Unimplemented destination: %s, please post a "
1115 "bug report in Evince bugzilla "
1116 "(http://bugzilla.gnome.org) with a testcase.",
1117 unimplemented_dest);
1121 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1127 ev_link_from_action (PdfDocument *pdf_document,
1128 PopplerAction *action)
1130 EvLink *link = NULL;
1131 EvLinkAction *ev_action = NULL;
1132 const char *unimplemented_action = NULL;
1134 switch (action->type) {
1135 case POPPLER_ACTION_NONE:
1137 case POPPLER_ACTION_GOTO_DEST: {
1140 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1141 ev_action = ev_link_action_new_dest (dest);
1144 case POPPLER_ACTION_GOTO_REMOTE: {
1147 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1148 ev_action = ev_link_action_new_remote (dest,
1149 action->goto_remote.file_name);
1153 case POPPLER_ACTION_LAUNCH:
1154 ev_action = ev_link_action_new_launch (action->launch.file_name,
1155 action->launch.params);
1157 case POPPLER_ACTION_URI:
1158 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1160 case POPPLER_ACTION_NAMED:
1161 ev_action = ev_link_action_new_named (action->named.named_dest);
1163 case POPPLER_ACTION_MOVIE:
1164 unimplemented_action = "POPPLER_ACTION_MOVIE";
1166 case POPPLER_ACTION_UNKNOWN:
1167 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1170 if (unimplemented_action) {
1171 g_warning ("Unimplemented action: %s, please post a bug report "
1172 "in Evince bugzilla (http://bugzilla.gnome.org) "
1173 "with a testcase.", unimplemented_action);
1176 link = ev_link_new (action->any.title, ev_action);
1182 build_tree (PdfDocument *pdf_document,
1183 GtkTreeModel *model,
1184 GtkTreeIter *parent,
1185 PopplerIndexIter *iter)
1189 GtkTreeIter tree_iter;
1190 PopplerIndexIter *child;
1191 PopplerAction *action;
1192 EvLink *link = NULL;
1196 action = poppler_index_iter_get_action (iter);
1197 expand = poppler_index_iter_is_open (iter);
1202 switch (action->type) {
1203 case POPPLER_ACTION_GOTO_DEST: {
1204 /* For bookmarks, solve named destinations */
1205 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1207 EvLinkDest *ev_dest = NULL;
1208 EvLinkAction *ev_action;
1210 dest = poppler_document_find_dest (pdf_document->document,
1211 action->goto_dest.dest->named_dest);
1213 link = ev_link_from_action (pdf_document, action);
1217 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1218 poppler_dest_free (dest);
1220 ev_action = ev_link_action_new_dest (ev_dest);
1221 link = ev_link_new (action->any.title, ev_action);
1223 link = ev_link_from_action (pdf_document, action);
1228 link = ev_link_from_action (pdf_document, action);
1232 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1233 poppler_action_free (action);
1235 g_object_unref (link);
1240 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1241 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1243 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1244 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1245 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1246 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1249 g_free (title_markup);
1250 g_object_unref (link);
1252 child = poppler_index_iter_get_child (iter);
1254 build_tree (pdf_document, model, &tree_iter, child);
1255 poppler_index_iter_free (child);
1256 poppler_action_free (action);
1258 } while (poppler_index_iter_next (iter));
1261 static GtkTreeModel *
1262 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1264 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1265 GtkTreeModel *model = NULL;
1266 PopplerIndexIter *iter;
1268 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1270 iter = poppler_index_iter_new (pdf_document->document);
1271 /* Create the model if we have items*/
1273 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1278 build_tree (pdf_document, model, NULL, iter);
1279 poppler_index_iter_free (iter);
1286 pdf_document_links_get_links (EvDocumentLinks *document_links,
1289 PdfDocument *pdf_document;
1290 PopplerPage *poppler_page;
1291 GList *retval = NULL;
1292 GList *mapping_list;
1296 pdf_document = PDF_DOCUMENT (document_links);
1297 poppler_page = POPPLER_PAGE (page->backend_page);
1298 mapping_list = poppler_page_get_link_mapping (poppler_page);
1299 poppler_page_get_size (poppler_page, NULL, &height);
1301 for (list = mapping_list; list; list = list->next) {
1302 PopplerLinkMapping *link_mapping;
1303 EvMapping *ev_link_mapping;
1305 link_mapping = (PopplerLinkMapping *)list->data;
1306 ev_link_mapping = g_new (EvMapping, 1);
1307 ev_link_mapping->data = ev_link_from_action (pdf_document,
1308 link_mapping->action);
1309 ev_link_mapping->area.x1 = link_mapping->area.x1;
1310 ev_link_mapping->area.x2 = link_mapping->area.x2;
1311 /* Invert this for X-style coordinates */
1312 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1313 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1315 retval = g_list_prepend (retval, ev_link_mapping);
1318 poppler_page_free_link_mapping (mapping_list);
1320 return g_list_reverse (retval);
1324 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1325 const gchar *link_name)
1327 PdfDocument *pdf_document;
1329 EvLinkDest *ev_dest = NULL;
1331 pdf_document = PDF_DOCUMENT (document_links);
1332 dest = poppler_document_find_dest (pdf_document->document,
1335 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1336 poppler_dest_free (dest);
1343 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1345 iface->has_document_links = pdf_document_links_has_document_links;
1346 iface->get_links_model = pdf_document_links_get_links_model;
1347 iface->get_links = pdf_document_links_get_links;
1348 iface->find_link_dest = pdf_document_links_find_link_dest;
1352 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1355 GList *retval = NULL;
1356 PdfDocument *pdf_document;
1357 PopplerPage *poppler_page;
1358 GList *mapping_list;
1361 pdf_document = PDF_DOCUMENT (document_images);
1362 poppler_page = POPPLER_PAGE (page->backend_page);
1363 mapping_list = poppler_page_get_image_mapping (poppler_page);
1365 for (list = mapping_list; list; list = list->next) {
1366 PopplerImageMapping *image_mapping;
1367 EvMapping *ev_image_mapping;
1369 image_mapping = (PopplerImageMapping *)list->data;
1371 ev_image_mapping = g_new (EvMapping, 1);
1373 ev_image_mapping->data = ev_image_new (page->index, image_mapping->image_id);
1374 ev_image_mapping->area.x1 = image_mapping->area.x1;
1375 ev_image_mapping->area.y1 = image_mapping->area.y1;
1376 ev_image_mapping->area.x2 = image_mapping->area.x2;
1377 ev_image_mapping->area.y2 = image_mapping->area.y2;
1379 retval = g_list_prepend (retval, ev_image_mapping);
1382 poppler_page_free_image_mapping (mapping_list);
1384 return g_list_reverse (retval);
1388 pdf_document_images_get_image (EvDocumentImages *document_images,
1391 GdkPixbuf *retval = NULL;
1392 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1393 PdfDocument *pdf_document;
1394 PopplerPage *poppler_page;
1395 cairo_surface_t *surface;
1397 pdf_document = PDF_DOCUMENT (document_images);
1398 poppler_page = poppler_document_get_page (pdf_document->document,
1399 ev_image_get_page (image));
1401 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1403 retval = ev_document_misc_pixbuf_from_surface (surface);
1404 cairo_surface_destroy (surface);
1407 g_object_unref (poppler_page);
1413 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1415 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1416 iface->get_image = pdf_document_images_get_image;
1420 make_thumbnail_for_page (PopplerPage *poppler_page,
1421 EvRenderContext *rc,
1427 #ifdef POPPLER_WITH_GDK
1428 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1430 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1432 ev_document_fc_mutex_lock ();
1433 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1435 rc->scale, rc->rotation, pixbuf);
1436 ev_document_fc_mutex_unlock ();
1438 cairo_surface_t *surface;
1440 ev_document_fc_mutex_lock ();
1441 surface = pdf_page_render (poppler_page, width, height, rc);
1442 ev_document_fc_mutex_unlock ();
1444 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1445 cairo_surface_destroy (surface);
1446 #endif /* POPPLER_WITH_GDK */
1452 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1453 EvRenderContext *rc,
1456 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1457 PopplerPage *poppler_page;
1458 GdkPixbuf *pixbuf = NULL;
1459 GdkPixbuf *border_pixbuf;
1462 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1464 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1465 rc, &width, &height);
1467 #ifdef POPPLER_WITH_GDK
1468 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1470 cairo_surface_t *surface;
1472 surface = poppler_page_get_thumbnail (poppler_page);
1474 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1475 cairo_surface_destroy (surface);
1477 #endif /* POPPLER_WITH_GDK */
1479 if (pixbuf != NULL) {
1480 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1481 gdk_pixbuf_get_height (pixbuf) :
1482 gdk_pixbuf_get_width (pixbuf);
1484 if (thumb_width == width) {
1485 GdkPixbuf *rotated_pixbuf;
1487 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1488 (GdkPixbufRotation) (360 - rc->rotation));
1489 g_object_unref (pixbuf);
1490 pixbuf = rotated_pixbuf;
1492 /* The provided thumbnail has a different size */
1493 g_object_unref (pixbuf);
1494 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1497 /* There is no provided thumbnail. We need to make one. */
1498 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1501 if (border && pixbuf) {
1502 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1503 g_object_unref (pixbuf);
1504 pixbuf = border_pixbuf;
1511 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1512 EvRenderContext *rc,
1516 double page_width, page_height;
1518 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1519 &page_width, &page_height);
1521 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1522 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1524 if (rc->rotation == 90 || rc->rotation == 270) {
1534 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1536 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1537 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1542 pdf_document_find_find_text (EvDocumentFind *document_find,
1545 gboolean case_sensitive)
1548 PopplerPage *poppler_page;
1551 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1552 g_return_val_if_fail (text != NULL, NULL);
1554 poppler_page = POPPLER_PAGE (page->backend_page);
1556 matches = poppler_page_find_text (poppler_page, text);
1560 poppler_page_get_size (poppler_page, NULL, &height);
1561 for (l = matches; l && l->data; l = g_list_next (l)) {
1562 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1566 rect->y1 = height - rect->y2;
1567 rect->y2 = height - tmp;
1574 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1576 iface->find_text = pdf_document_find_find_text;
1580 pdf_print_context_free (PdfPrintContext *ctx)
1585 #ifdef HAVE_CAIRO_PRINT
1587 cairo_destroy (ctx->cr);
1592 poppler_ps_file_free (ctx->ps_file);
1593 ctx->ps_file = NULL;
1600 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1601 EvFileExporterContext *fc)
1603 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1604 PdfPrintContext *ctx;
1605 #ifdef HAVE_CAIRO_PRINT
1606 gdouble width, height;
1607 cairo_surface_t *surface = NULL;
1610 if (pdf_document->print_ctx)
1611 pdf_print_context_free (pdf_document->print_ctx);
1612 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1613 ctx = pdf_document->print_ctx;
1614 ctx->format = fc->format;
1616 #ifdef HAVE_CAIRO_PRINT
1617 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1619 ctx->paper_width = fc->paper_width;
1620 ctx->paper_height = fc->paper_height;
1622 switch (fc->pages_per_sheet) {
1650 ctx->pages_printed = 0;
1652 switch (fc->format) {
1653 case EV_FILE_FORMAT_PS:
1654 #ifdef HAVE_CAIRO_PS
1655 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1658 case EV_FILE_FORMAT_PDF:
1659 #ifdef HAVE_CAIRO_PDF
1660 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1664 g_assert_not_reached ();
1667 ctx->cr = cairo_create (surface);
1668 cairo_surface_destroy (surface);
1670 #else /* HAVE_CAIRO_PRINT */
1671 if (ctx->format == EV_FILE_FORMAT_PS) {
1672 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1673 fc->filename, fc->first_page,
1674 fc->last_page - fc->first_page + 1);
1675 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1676 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1678 #endif /* HAVE_CAIRO_PRINT */
1682 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1684 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1685 PdfPrintContext *ctx = pdf_document->print_ctx;
1687 g_return_if_fail (pdf_document->print_ctx != NULL);
1689 ctx->pages_printed = 0;
1691 #ifdef HAVE_CAIRO_PRINT
1692 if (ctx->paper_width > ctx->paper_height) {
1693 if (ctx->format == EV_FILE_FORMAT_PS) {
1694 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1697 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1698 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1703 #endif /* HAVE_CAIRO_PRINT */
1707 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1708 EvRenderContext *rc)
1710 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1711 PdfPrintContext *ctx = pdf_document->print_ctx;
1712 PopplerPage *poppler_page;
1713 #ifdef HAVE_CAIRO_PRINT
1714 gdouble page_width, page_height;
1717 gdouble width, height;
1718 gdouble pwidth, pheight;
1719 gdouble xscale, yscale;
1722 g_return_if_fail (pdf_document->print_ctx != NULL);
1724 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1726 #ifdef HAVE_CAIRO_PRINT
1727 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1728 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1729 poppler_page_get_size (poppler_page, &page_width, &page_height);
1731 if (page_width > page_height && page_width > ctx->paper_width) {
1737 /* Use always portrait mode and rotate when necessary */
1738 if (ctx->paper_width > ctx->paper_height) {
1739 width = ctx->paper_height;
1740 height = ctx->paper_width;
1743 width = ctx->paper_width;
1744 height = ctx->paper_height;
1747 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1760 page_width = page_height;
1764 pwidth = width / ctx->pages_x;
1765 pheight = height / ctx->pages_y;
1767 if ((page_width > pwidth || page_height > pheight) ||
1768 (page_width < pwidth && page_height < pheight)) {
1769 xscale = pwidth / page_width;
1770 yscale = pheight / page_height;
1772 if (yscale < xscale) {
1779 xscale = yscale = 1;
1784 cairo_save (ctx->cr);
1786 cairo_matrix_t matrix;
1788 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1789 cairo_matrix_init (&matrix,
1793 cairo_transform (ctx->cr, &matrix);
1796 cairo_translate (ctx->cr,
1797 x * (rotate ? pheight : pwidth),
1798 y * (rotate ? pwidth : pheight));
1799 cairo_scale (ctx->cr, xscale, yscale);
1801 poppler_page_render_for_printing (poppler_page, ctx->cr);
1803 ctx->pages_printed++;
1805 cairo_restore (ctx->cr);
1806 #else /* HAVE_CAIRO_PRINT */
1807 if (ctx->format == EV_FILE_FORMAT_PS)
1808 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1809 #endif /* HAVE_CAIRO_PRINT */
1813 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1815 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1816 PdfPrintContext *ctx = pdf_document->print_ctx;
1818 g_return_if_fail (pdf_document->print_ctx != NULL);
1820 #ifdef HAVE_CAIRO_PRINT
1821 cairo_show_page (ctx->cr);
1826 pdf_document_file_exporter_end (EvFileExporter *exporter)
1828 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1830 pdf_print_context_free (pdf_document->print_ctx);
1831 pdf_document->print_ctx = NULL;
1834 static EvFileExporterCapabilities
1835 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1837 return (EvFileExporterCapabilities) (
1838 EV_FILE_EXPORTER_CAN_PAGE_SET |
1839 EV_FILE_EXPORTER_CAN_COPIES |
1840 EV_FILE_EXPORTER_CAN_COLLATE |
1841 EV_FILE_EXPORTER_CAN_REVERSE |
1842 EV_FILE_EXPORTER_CAN_SCALE |
1843 #ifdef HAVE_CAIRO_PRINT
1844 #ifdef HAVE_POPPLER_PAGE_RENDER
1845 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1849 #ifdef HAVE_CAIRO_PDF
1850 #ifdef HAVE_POPPLER_PAGE_RENDER
1851 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1854 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1858 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1860 iface->begin = pdf_document_file_exporter_begin;
1861 iface->begin_page = pdf_document_file_exporter_begin_page;
1862 iface->do_page = pdf_document_file_exporter_do_page;
1863 iface->end_page = pdf_document_file_exporter_end_page;
1864 iface->end = pdf_document_file_exporter_end;
1865 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1868 #ifdef HAVE_POPPLER_PAGE_RENDER
1869 /* EvDocumentPrint */
1871 pdf_document_print_print_page (EvDocumentPrint *document,
1875 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1877 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1881 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1883 iface->print_page = pdf_document_print_print_page;
1885 #endif /* HAVE_POPPLER_PAGE_RENDER */
1888 pdf_selection_render_selection (EvSelection *selection,
1889 EvRenderContext *rc,
1890 cairo_surface_t **surface,
1891 EvRectangle *points,
1892 EvRectangle *old_points,
1893 EvSelectionStyle style,
1897 PopplerPage *poppler_page;
1898 double width_points, height_points;
1901 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1903 poppler_page_get_size (poppler_page,
1904 &width_points, &height_points);
1905 width = (int) ((width_points * rc->scale) + 0.5);
1906 height = (int) ((height_points * rc->scale) + 0.5);
1908 #ifdef HAVE_POPPLER_PAGE_RENDER
1910 PopplerColor text_color, base_color;
1912 text_color.red = text->red;
1913 text_color.green = text->green;
1914 text_color.blue = text->blue;
1916 base_color.red = base->red;
1917 base_color.green = base->green;
1918 base_color.blue = base->blue;
1920 if (*surface == NULL) {
1921 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1926 cr = cairo_create (*surface);
1927 cairo_scale (cr, rc->scale, rc->scale);
1928 cairo_surface_set_device_offset (*surface, 0, 0);
1929 memset (cairo_image_surface_get_data (*surface), 0x00,
1930 cairo_image_surface_get_height (*surface) *
1931 cairo_image_surface_get_stride (*surface));
1932 poppler_page_render_selection (poppler_page,
1934 (PopplerRectangle *)points,
1935 (PopplerRectangle *)old_points,
1936 (PopplerSelectionStyle)style,
1940 #else /* HAVE_POPPLER_PAGE_RENDER */
1943 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1947 poppler_page_render_selection_to_pixbuf (poppler_page,
1948 rc->scale, rc->rotation, pixbuf,
1949 (PopplerRectangle *)points,
1950 (PopplerRectangle *)old_points,
1951 (PopplerSelectionStyle)style,
1955 cairo_surface_destroy (*surface);
1956 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1957 g_object_unref (pixbuf);
1958 #endif /* HAVE_POPPLER_PAGE_RENDER */
1962 pdf_selection_get_selected_text (EvSelection *selection,
1963 EvRenderContext *rc,
1964 EvSelectionStyle style,
1965 EvRectangle *points)
1967 PopplerPage *poppler_page;
1972 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1974 poppler_page_get_size (poppler_page, NULL, &height);
1976 r.y1 = height - points->y2;
1978 r.y2 = height - points->y1;
1980 retval = poppler_page_get_text (poppler_page,
1981 (PopplerSelectionStyle)style,
1988 create_gdk_region_from_poppler_region (GList *region)
1993 retval = gdk_region_new ();
1995 for (l = region; l; l = g_list_next (l)) {
1996 PopplerRectangle *rectangle;
1999 rectangle = (PopplerRectangle *)l->data;
2001 rect.x = (gint) rectangle->x1;
2002 rect.y = (gint) rectangle->y1;
2003 rect.width = (gint) (rectangle->x2 - rectangle->x1);
2004 rect.height = (gint) (rectangle->y2 - rectangle->y1);
2005 gdk_region_union_with_rect (retval, &rect);
2007 poppler_rectangle_free (rectangle);
2014 pdf_selection_get_selection_region (EvSelection *selection,
2015 EvRenderContext *rc,
2016 EvSelectionStyle style,
2017 EvRectangle *points)
2019 PopplerPage *poppler_page;
2023 poppler_page = POPPLER_PAGE (rc->page->backend_page);
2025 region = poppler_page_get_selection_region (poppler_page,
2027 (PopplerSelectionStyle)style,
2028 (PopplerRectangle *) points);
2029 retval = create_gdk_region_from_poppler_region (region);
2030 g_list_free (region);
2036 pdf_selection_get_selection_map (EvSelection *selection,
2039 PopplerPage *poppler_page;
2040 PopplerRectangle points;
2044 poppler_page = POPPLER_PAGE (page->backend_page);
2048 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2050 region = poppler_page_get_selection_region (poppler_page, 1.0,
2051 POPPLER_SELECTION_GLYPH,
2053 retval = create_gdk_region_from_poppler_region (region);
2054 g_list_free (region);
2060 pdf_selection_iface_init (EvSelectionIface *iface)
2062 iface->render_selection = pdf_selection_render_selection;
2063 iface->get_selected_text = pdf_selection_get_selected_text;
2064 iface->get_selection_region = pdf_selection_get_selection_region;
2065 iface->get_selection_map = pdf_selection_get_selection_map;
2068 /* Page Transitions */
2070 pdf_document_get_page_duration (EvDocumentTransition *trans,
2073 PdfDocument *pdf_document;
2074 PopplerPage *poppler_page;
2075 gdouble duration = -1;
2077 pdf_document = PDF_DOCUMENT (trans);
2078 poppler_page = poppler_document_get_page (pdf_document->document, page);
2082 duration = poppler_page_get_duration (poppler_page);
2083 g_object_unref (poppler_page);
2088 static EvTransitionEffect *
2089 pdf_document_get_effect (EvDocumentTransition *trans,
2092 PdfDocument *pdf_document;
2093 PopplerPage *poppler_page;
2094 PopplerPageTransition *page_transition;
2095 EvTransitionEffect *effect;
2097 pdf_document = PDF_DOCUMENT (trans);
2098 poppler_page = poppler_document_get_page (pdf_document->document, page);
2103 page_transition = poppler_page_get_transition (poppler_page);
2105 if (!page_transition) {
2106 g_object_unref (poppler_page);
2110 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2111 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2112 "alignment", page_transition->alignment,
2113 "direction", page_transition->direction,
2114 "duration", page_transition->duration,
2115 "angle", page_transition->angle,
2116 "scale", page_transition->scale,
2117 "rectangular", page_transition->rectangular,
2120 poppler_page_transition_free (page_transition);
2121 g_object_unref (poppler_page);
2127 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2129 iface->get_page_duration = pdf_document_get_page_duration;
2130 iface->get_effect = pdf_document_get_effect;
2135 pdf_document_get_crop_box (EvDocument *document,
2139 PdfDocument *pdf_document;
2140 PopplerPage *poppler_page;
2141 PopplerRectangle poppler_rect;
2143 pdf_document = PDF_DOCUMENT (document);
2144 poppler_page = poppler_document_get_page (pdf_document->document, page);
2145 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2146 rect->x1 = poppler_rect.x1;
2147 rect->x2 = poppler_rect.x2;
2148 rect->y1 = poppler_rect.y1;
2149 rect->y2 = poppler_rect.y2;
2152 static EvFormField *
2153 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2155 EvFormField *ev_field = NULL;
2158 gboolean is_read_only;
2160 id = poppler_form_field_get_id (poppler_field);
2161 font_size = poppler_form_field_get_font_size (poppler_field);
2162 is_read_only = poppler_form_field_is_read_only (poppler_field);
2164 switch (poppler_form_field_get_field_type (poppler_field)) {
2165 case POPPLER_FORM_FIELD_TEXT: {
2166 EvFormFieldText *field_text;
2167 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2169 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2170 case POPPLER_FORM_TEXT_NORMAL:
2171 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2173 case POPPLER_FORM_TEXT_MULTILINE:
2174 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2176 case POPPLER_FORM_TEXT_FILE_SELECT:
2177 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2181 ev_field = ev_form_field_text_new (id, ev_text_type);
2182 field_text = EV_FORM_FIELD_TEXT (ev_field);
2184 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2185 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2186 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2187 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2188 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2189 field_text->text = poppler_form_field_text_get_text (poppler_field);
2193 case POPPLER_FORM_FIELD_BUTTON: {
2194 EvFormFieldButton *field_button;
2195 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2197 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2198 case POPPLER_FORM_BUTTON_PUSH:
2199 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2201 case POPPLER_FORM_BUTTON_CHECK:
2202 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2204 case POPPLER_FORM_BUTTON_RADIO:
2205 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2209 ev_field = ev_form_field_button_new (id, ev_button_type);
2210 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2212 field_button->state = poppler_form_field_button_get_state (poppler_field);
2215 case POPPLER_FORM_FIELD_CHOICE: {
2216 EvFormFieldChoice *field_choice;
2217 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2219 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2220 case POPPLER_FORM_CHOICE_COMBO:
2221 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2223 case EV_FORM_FIELD_CHOICE_LIST:
2224 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2228 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2229 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2231 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2232 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2233 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2234 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2236 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2237 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2238 if (field_choice->is_editable)
2239 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2242 case POPPLER_FORM_FIELD_SIGNATURE:
2244 ev_field = ev_form_field_signature_new (id);
2246 case POPPLER_FORM_FIELD_UNKNOWN:
2250 ev_field->font_size = font_size;
2251 ev_field->is_read_only = is_read_only;
2257 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2260 PopplerPage *poppler_page;
2261 GList *retval = NULL;
2266 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2268 poppler_page = POPPLER_PAGE (page->backend_page);
2269 fields = poppler_page_get_form_field_mapping (poppler_page);
2270 poppler_page_get_size (poppler_page, NULL, &height);
2272 for (list = fields; list; list = list->next) {
2273 PopplerFormFieldMapping *mapping;
2274 EvMapping *field_mapping;
2275 EvFormField *ev_field;
2277 mapping = (PopplerFormFieldMapping *)list->data;
2279 ev_field = ev_form_field_from_poppler_field (mapping->field);
2283 field_mapping = g_new0 (EvMapping, 1);
2284 field_mapping->area.x1 = mapping->area.x1;
2285 field_mapping->area.x2 = mapping->area.x2;
2286 field_mapping->area.y1 = height - mapping->area.y2;
2287 field_mapping->area.y2 = height - mapping->area.y1;
2288 field_mapping->data = ev_field;
2289 ev_field->page = EV_PAGE (g_object_ref (page));
2291 g_object_set_data_full (G_OBJECT (ev_field),
2293 g_object_ref (mapping->field),
2294 (GDestroyNotify) g_object_unref);
2296 retval = g_list_prepend (retval, field_mapping);
2299 poppler_page_free_form_field_mapping (fields);
2301 return g_list_reverse (retval);
2305 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2309 PopplerFormField *poppler_field;
2312 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2316 text = poppler_form_field_text_get_text (poppler_field);
2322 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2326 PopplerFormField *poppler_field;
2328 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2332 poppler_form_field_text_set_text (poppler_field, text);
2333 PDF_DOCUMENT (document)->modified = TRUE;
2337 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2341 PopplerFormField *poppler_field;
2343 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2347 poppler_form_field_button_set_state (poppler_field, state);
2348 PDF_DOCUMENT (document)->modified = TRUE;
2352 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2355 PopplerFormField *poppler_field;
2358 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2362 state = poppler_form_field_button_get_state (poppler_field);
2368 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2372 PopplerFormField *poppler_field;
2375 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2379 text = poppler_form_field_choice_get_item (poppler_field, index);
2385 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2388 PopplerFormField *poppler_field;
2391 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2395 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2401 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2405 PopplerFormField *poppler_field;
2408 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2412 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2418 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2422 PopplerFormField *poppler_field;
2424 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2428 poppler_form_field_choice_select_item (poppler_field, index);
2429 PDF_DOCUMENT (document)->modified = TRUE;
2433 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2437 PopplerFormField *poppler_field;
2439 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2443 poppler_form_field_choice_toggle_item (poppler_field, index);
2444 PDF_DOCUMENT (document)->modified = TRUE;
2448 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2451 PopplerFormField *poppler_field;
2453 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2457 poppler_form_field_choice_unselect_all (poppler_field);
2458 PDF_DOCUMENT (document)->modified = TRUE;
2462 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2466 PopplerFormField *poppler_field;
2468 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2472 poppler_form_field_choice_set_text (poppler_field, text);
2473 PDF_DOCUMENT (document)->modified = TRUE;
2477 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2480 PopplerFormField *poppler_field;
2483 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2487 text = poppler_form_field_choice_get_text (poppler_field);
2493 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2495 iface->get_form_fields = pdf_document_forms_get_form_fields;
2496 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2497 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2498 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2499 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2500 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2501 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2502 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2503 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2504 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2505 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2506 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2507 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2512 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2515 PopplerColor *poppler_color;
2517 poppler_color = poppler_annot_get_color (poppler_annot);
2518 if (poppler_color) {
2519 color->red = poppler_color->red;
2520 color->green = poppler_color->green;
2521 color->blue = poppler_color->blue;
2523 g_free (poppler_color);
2524 } /* TODO: else use a default color */
2527 static EvAnnotation *
2528 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2531 EvAnnotation *ev_annot = NULL;
2532 const gchar *unimplemented_annot = NULL;
2534 switch (poppler_annot_get_annot_type (poppler_annot)) {
2535 case POPPLER_ANNOT_TEXT: {
2536 PopplerAnnotText *poppler_text;
2537 EvAnnotationText *ev_annot_text;
2539 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2541 ev_annot = ev_annotation_text_new (page);
2543 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2544 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2547 #ifdef HAVE_POPPLER_ANNOT_FILE_ATTACHMENT_GET_ATTACHMENT
2548 case POPPLER_ANNOT_FILE_ATTACHMENT: {
2549 PopplerAnnotFileAttachment *poppler_annot_attachment;
2550 EvAnnotationAttachment *ev_annot_attachment;
2551 PopplerAttachment *poppler_attachment;
2554 GError *error = NULL;
2556 poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
2557 poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
2559 if (poppler_attachment &&
2560 attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
2561 EvAttachment *ev_attachment;
2563 ev_attachment = ev_attachment_new (poppler_attachment->name,
2564 poppler_attachment->description,
2565 poppler_attachment->mtime,
2566 poppler_attachment->ctime,
2568 ev_annot = ev_annotation_attachment_new (page, ev_attachment);
2569 g_object_unref (ev_attachment);
2571 g_warning ("%s", error->message);
2572 g_error_free (error);
2575 if (poppler_attachment)
2576 g_object_unref (poppler_attachment);
2579 #endif /* HAVE_POPPLER_ANNOT_FILE_ATTACHMENT_GET_ATTACHMENT */
2580 case POPPLER_ANNOT_LINK:
2581 case POPPLER_ANNOT_WIDGET:
2582 /* Ignore link and widgets annots since they are already handled */
2585 GEnumValue *enum_value;
2587 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2588 poppler_annot_get_annot_type (poppler_annot));
2589 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2593 if (unimplemented_annot) {
2594 g_warning ("Unimplemented annotation: %s, please post a "
2595 "bug report in Evince bugzilla "
2596 "(http://bugzilla.gnome.org) with a testcase.",
2597 unimplemented_annot);
2601 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2602 ev_annot->name = poppler_annot_get_name (poppler_annot);
2603 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2604 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2606 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2607 PopplerAnnotMarkup *markup;
2610 PopplerRectangle poppler_rect;
2612 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2614 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2615 EvRectangle ev_rect;
2619 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2621 ev_rect.x1 = poppler_rect.x1;
2622 ev_rect.x2 = poppler_rect.x2;
2623 ev_rect.y1 = height - poppler_rect.y2;
2624 ev_rect.y2 = height - poppler_rect.y1;
2626 is_open = poppler_annot_markup_get_popup_is_open (markup);
2628 g_object_set (ev_annot,
2629 "rectangle", &ev_rect,
2634 /* FIXME: Use poppler_annot_markup_has_popup() when
2635 * new poppler is released.
2637 g_object_set (ev_annot,
2642 label = poppler_annot_markup_get_label (markup);
2643 opacity = poppler_annot_markup_get_opacity (markup);
2645 g_object_set (ev_annot,
2658 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2661 GList *retval = NULL;
2662 PdfDocument *pdf_document;
2663 PopplerPage *poppler_page;
2669 pdf_document = PDF_DOCUMENT (document_annotations);
2670 poppler_page = POPPLER_PAGE (page->backend_page);
2671 annots = poppler_page_get_annot_mapping (poppler_page);
2672 poppler_page_get_size (poppler_page, NULL, &height);
2674 for (list = annots; list; list = list->next) {
2675 PopplerAnnotMapping *mapping;
2676 EvMapping *annot_mapping;
2677 EvAnnotation *ev_annot;
2679 mapping = (PopplerAnnotMapping *)list->data;
2681 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2687 /* Make sure annot has a unique name */
2688 if (!ev_annot->name)
2689 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2691 annot_mapping = g_new (EvMapping, 1);
2692 annot_mapping->area.x1 = mapping->area.x1;
2693 annot_mapping->area.x2 = mapping->area.x2;
2694 annot_mapping->area.y1 = height - mapping->area.y2;
2695 annot_mapping->area.y2 = height - mapping->area.y1;
2696 annot_mapping->data = ev_annot;
2698 g_object_set_data_full (G_OBJECT (ev_annot),
2700 g_object_ref (mapping->annot),
2701 (GDestroyNotify) g_object_unref);
2703 retval = g_list_prepend (retval, annot_mapping);
2706 poppler_page_free_annot_mapping (annots);
2708 return g_list_reverse (retval);
2712 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2713 EvAnnotation *annot,
2714 const gchar *contents)
2716 PopplerAnnot *poppler_annot;
2718 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2722 poppler_annot_set_contents (poppler_annot, contents);
2723 PDF_DOCUMENT (document)->modified = TRUE;
2727 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2729 iface->get_annotations = pdf_document_annotations_get_annotations;
2730 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2734 struct SaveToBufferData {
2740 attachment_save_to_buffer_callback (const gchar *buf,
2745 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2749 if (sdata->len + count > sdata->max) {
2750 new_max = MAX (sdata->max * 2, sdata->len + count);
2751 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2753 sdata->buffer = new_buffer;
2754 sdata->max = new_max;
2757 memcpy (sdata->buffer + sdata->len, buf, count);
2758 sdata->len += count;
2764 attachment_save_to_buffer (PopplerAttachment *attachment,
2769 static const gint initial_max = 1024;
2770 struct SaveToBufferData sdata;
2775 sdata.buffer = (gchar *) g_malloc (initial_max);
2776 sdata.max = initial_max;
2779 if (! poppler_attachment_save_to_callback (attachment,
2780 attachment_save_to_buffer_callback,
2783 g_free (sdata.buffer);
2787 *buffer = sdata.buffer;
2788 *buffer_size = sdata.len;
2794 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2796 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2799 GList *retval = NULL;
2801 attachments = poppler_document_get_attachments (pdf_document->document);
2803 for (list = attachments; list; list = list->next) {
2804 PopplerAttachment *attachment;
2805 EvAttachment *ev_attachment;
2808 GError *error = NULL;
2810 attachment = (PopplerAttachment *) list->data;
2812 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2813 ev_attachment = ev_attachment_new (attachment->name,
2814 attachment->description,
2819 retval = g_list_prepend (retval, ev_attachment);
2822 g_warning ("%s", error->message);
2823 g_error_free (error);
2829 g_object_unref (attachment);
2832 return g_list_reverse (retval);
2836 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2838 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2840 return poppler_document_has_attachments (pdf_document->document);
2844 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface)
2846 iface->has_attachments = pdf_document_attachments_has_attachments;
2847 iface->get_attachments = pdf_document_attachments_get_attachments;
2852 pdf_document_layers_has_layers (EvDocumentLayers *document)
2854 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2855 PopplerLayersIter *iter;
2857 iter = poppler_layers_iter_new (pdf_document->document);
2860 poppler_layers_iter_free (iter);
2866 build_layers_tree (PdfDocument *pdf_document,
2867 GtkTreeModel *model,
2868 GtkTreeIter *parent,
2869 PopplerLayersIter *iter)
2872 GtkTreeIter tree_iter;
2873 PopplerLayersIter *child;
2874 PopplerLayer *layer;
2875 EvLayer *ev_layer = NULL;
2880 layer = poppler_layers_iter_get_layer (iter);
2882 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2883 visible = poppler_layer_is_visible (layer);
2884 rb_group = poppler_layer_get_radio_button_group_id (layer);
2885 pdf_document->layers = g_list_append (pdf_document->layers,
2886 g_object_ref (layer));
2887 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2888 poppler_layer_is_parent (layer),
2893 title = poppler_layers_iter_get_title (iter);
2894 markup = g_markup_escape_text (title, -1);
2901 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2902 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2903 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2904 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2905 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2906 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2907 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2908 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2911 g_object_unref (ev_layer);
2914 child = poppler_layers_iter_get_child (iter);
2916 build_layers_tree (pdf_document, model, &tree_iter, child);
2917 poppler_layers_iter_free (child);
2918 } while (poppler_layers_iter_next (iter));
2921 static GtkTreeModel *
2922 pdf_document_layers_get_layers (EvDocumentLayers *document)
2924 GtkTreeModel *model = NULL;
2925 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2926 PopplerLayersIter *iter;
2928 iter = poppler_layers_iter_new (pdf_document->document);
2930 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2931 G_TYPE_STRING, /* TITLE */
2932 G_TYPE_OBJECT, /* LAYER */
2933 G_TYPE_BOOLEAN, /* VISIBLE */
2934 G_TYPE_BOOLEAN, /* ENABLED */
2935 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2936 G_TYPE_INT); /* RBGROUP */
2937 build_layers_tree (pdf_document, model, NULL, iter);
2938 poppler_layers_iter_free (iter);
2944 pdf_document_layers_show_layer (EvDocumentLayers *document,
2947 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2948 guint layer_id = ev_layer_get_id (layer);
2950 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2954 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2957 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2958 guint layer_id = ev_layer_get_id (layer);
2960 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2964 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2967 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2968 guint layer_id = ev_layer_get_id (layer);
2970 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2974 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2976 iface->has_layers = pdf_document_layers_has_layers;
2977 iface->get_layers = pdf_document_layers_get_layers;
2978 iface->show_layer = pdf_document_layers_show_layer;
2979 iface->hide_layer = pdf_document_layers_hide_layer;
2980 iface->layer_is_visible = pdf_document_layers_layer_is_visible;