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 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 PdfPrintContext *print_ctx;
115 static void pdf_document_security_iface_init (EvDocumentSecurityInterface *iface);
116 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface);
117 static void pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface);
118 static void pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface);
119 static void pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface);
120 static void pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface);
121 static void pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface);
122 #ifdef HAVE_POPPLER_PAGE_RENDER
123 static void pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface);
125 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface);
126 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface);
127 static void pdf_document_find_iface_init (EvDocumentFindInterface *iface);
128 static void pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface);
129 static void pdf_selection_iface_init (EvSelectionInterface *iface);
130 static void pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface);
131 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
135 static int pdf_document_get_n_pages (EvDocument *document);
137 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
139 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
140 PopplerAction *action);
141 static void pdf_print_context_free (PdfPrintContext *ctx);
142 static gboolean attachment_save_to_buffer (PopplerAttachment *attachment,
147 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
149 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
150 pdf_document_security_iface_init);
151 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
152 pdf_document_document_thumbnails_iface_init);
153 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
154 pdf_document_document_links_iface_init);
155 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
156 pdf_document_document_images_iface_init);
157 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
158 pdf_document_document_forms_iface_init);
159 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
160 pdf_document_document_fonts_iface_init);
161 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
162 pdf_document_document_layers_iface_init);
163 #ifdef HAVE_POPPLER_PAGE_RENDER
164 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
165 pdf_document_document_print_iface_init);
167 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
168 pdf_document_document_annotations_iface_init);
169 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
170 pdf_document_document_attachments_iface_init);
171 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
172 pdf_document_find_iface_init);
173 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
174 pdf_document_file_exporter_iface_init);
175 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
176 pdf_selection_iface_init);
177 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
178 pdf_document_page_transition_iface_init);
182 pdf_document_dispose (GObject *object)
184 PdfDocument *pdf_document = PDF_DOCUMENT(object);
186 if (pdf_document->print_ctx) {
187 pdf_print_context_free (pdf_document->print_ctx);
188 pdf_document->print_ctx = NULL;
191 if (pdf_document->document) {
192 g_object_unref (pdf_document->document);
195 if (pdf_document->font_info) {
196 poppler_font_info_free (pdf_document->font_info);
199 if (pdf_document->fonts_iter) {
200 poppler_fonts_iter_free (pdf_document->fonts_iter);
203 if (pdf_document->layers) {
204 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
205 g_list_free (pdf_document->layers);
208 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
212 pdf_document_init (PdfDocument *pdf_document)
214 pdf_document->password = NULL;
218 convert_error (GError *poppler_error,
221 if (poppler_error == NULL)
224 if (poppler_error->domain == POPPLER_ERROR) {
225 /* convert poppler errors into EvDocument errors */
226 gint code = EV_DOCUMENT_ERROR_INVALID;
227 if (poppler_error->code == POPPLER_ERROR_INVALID)
228 code = EV_DOCUMENT_ERROR_INVALID;
229 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
230 code = EV_DOCUMENT_ERROR_ENCRYPTED;
232 g_set_error_literal (error,
235 poppler_error->message);
237 g_error_free (poppler_error);
239 g_propagate_error (error, poppler_error);
246 pdf_document_save (EvDocument *document,
250 PdfDocument *pdf_document = PDF_DOCUMENT (document);
252 GError *poppler_error = NULL;
254 if (pdf_document->modified) {
255 retval = poppler_document_save (pdf_document->document,
256 uri, &poppler_error);
258 retval = poppler_document_save_a_copy (pdf_document->document,
259 uri, &poppler_error);
263 convert_error (poppler_error, error);
269 pdf_document_load (EvDocument *document,
273 GError *poppler_error = NULL;
274 PdfDocument *pdf_document = PDF_DOCUMENT (document);
276 pdf_document->document =
277 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
279 if (pdf_document->document == NULL) {
280 convert_error (poppler_error, error);
288 pdf_document_get_n_pages (EvDocument *document)
290 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
294 pdf_document_get_page (EvDocument *document,
297 PdfDocument *pdf_document = PDF_DOCUMENT (document);
298 PopplerPage *poppler_page;
301 poppler_page = poppler_document_get_page (pdf_document->document, index);
302 page = ev_page_new (index);
303 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
304 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
305 g_object_unref (poppler_page);
311 pdf_document_get_page_size (EvDocument *document,
316 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
318 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
322 pdf_document_get_page_label (EvDocument *document,
327 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
329 g_object_get (G_OBJECT (page->backend_page),
335 static cairo_surface_t *
336 pdf_page_render (PopplerPage *page,
341 cairo_surface_t *surface;
343 #ifdef HAVE_POPPLER_PAGE_RENDER
346 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
348 cr = cairo_create (surface);
350 switch (rc->rotation) {
352 cairo_translate (cr, width, 0);
355 cairo_translate (cr, width, height);
358 cairo_translate (cr, 0, height);
361 cairo_translate (cr, 0, 0);
363 cairo_scale (cr, rc->scale, rc->scale);
364 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
365 poppler_page_render (page, cr);
367 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
368 cairo_set_source_rgb (cr, 1., 1., 1.);
372 #else /* HAVE_POPPLER_PAGE_RENDER */
375 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
379 poppler_page_render_to_pixbuf (page,
385 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
386 g_object_unref (pixbuf);
387 #endif /* HAVE_POPPLER_PAGE_RENDER */
392 static cairo_surface_t *
393 pdf_document_render (EvDocument *document,
396 PdfDocument *pdf_document;
397 PopplerPage *poppler_page;
398 double width_points, height_points;
401 poppler_page = POPPLER_PAGE (rc->page->backend_page);
403 poppler_page_get_size (poppler_page,
404 &width_points, &height_points);
406 if (rc->rotation == 90 || rc->rotation == 270) {
407 width = (int) ((height_points * rc->scale) + 0.5);
408 height = (int) ((width_points * rc->scale) + 0.5);
410 width = (int) ((width_points * rc->scale) + 0.5);
411 height = (int) ((height_points * rc->scale) + 0.5);
414 return pdf_page_render (poppler_page,
419 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 */
421 pdf_document_get_format_from_metadata (xmlDocPtr doc,
422 xmlXPathContextPtr xpathCtx)
424 xmlXPathObjectPtr xpathObj;
425 xmlChar *part = NULL;
426 xmlChar *conf = NULL;
430 /* add pdf/a namespaces */
431 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
432 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
433 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
435 /* reads pdf/a part */
436 /* first syntax: child node */
437 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
438 if (xpathObj != NULL) {
439 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
440 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
442 xmlXPathFreeObject (xpathObj);
445 /* second syntax: attribute */
446 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
447 if (xpathObj != NULL) {
448 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
449 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
451 xmlXPathFreeObject (xpathObj);
455 /* reads pdf/a conformance */
456 /* first syntax: child node */
457 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
458 if (xpathObj != NULL) {
459 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
460 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
462 xmlXPathFreeObject (xpathObj);
465 /* second syntax: attribute */
466 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
467 if (xpathObj != NULL) {
468 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
469 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
471 xmlXPathFreeObject (xpathObj);
475 if (part != NULL && conf != NULL) {
476 /* makes conf lowercase */
477 for (i = 0; conf[i]; i++)
478 conf[i] = g_ascii_tolower (conf[i]);
481 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
491 static EvDocumentLicense *
492 pdf_document_get_license_from_metadata (xmlDocPtr doc,
493 xmlXPathContextPtr xpathCtx)
495 xmlXPathObjectPtr xpathObj;
496 xmlChar *marked = NULL;
497 const char *language_string;
500 gchar *tag, *tag_aux;
502 EvDocumentLicense *license;
504 /* register namespaces */
505 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
506 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
507 xmlXPathRegisterNs (xpathCtx, BAD_CAST "dc", BAD_CAST "http://purl.org/dc/elements/1.1/");
508 /* XMP Rights Management Schema */
509 xmlXPathRegisterNs (xpathCtx, BAD_CAST "xmpRights", BAD_CAST "http://ns.adobe.com/xap/1.0/rights/");
510 /* Creative Commons Schema */
511 xmlXPathRegisterNs (xpathCtx, BAD_CAST "cc", BAD_CAST "http://creativecommons.org/ns#");
513 /* checking if the document has been marked as defined on the XMP Rights
514 * Management Schema */
515 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_MARKED, xpathCtx);
516 if (xpathObj != NULL) {
517 if (xpathObj->nodesetval != NULL &&
518 xpathObj->nodesetval->nodeNr != 0)
519 marked = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
520 xmlXPathFreeObject (xpathObj);
523 /* a) Not marked => No XMP Rights information */
529 license = ev_document_license_new ();
531 /* b) Marked False => Public Domain, no copyrighted material and no
533 if (g_strrstr ((char *) marked, "False") != NULL) {
534 license->text = g_strdup (_("This work is in the Public Domain"));
535 /* c) Marked True => Copyrighted material */
537 /* Checking usage terms as defined by the XMP Rights Management
538 * Schema. This field is recomended to be checked by Creative
540 /* 1) checking for a suitable localized string */
541 language_string = pango_language_to_string (gtk_get_default_language ());
542 tags = g_strsplit (language_string, "-", -1);
543 i = g_strv_length (tags);
544 while (i-- && !license->text) {
545 tag = g_strdup (tags[0]);
546 for (j = 1; j <= i; j++) {
547 tag_aux = g_strdup_printf ("%s-%s", tag, tags[j]);
551 aux = g_strdup_printf (LICENSE_TEXT, tag);
552 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
553 if (xpathObj != NULL) {
554 if (xpathObj->nodesetval != NULL &&
555 xpathObj->nodesetval->nodeNr != 0)
556 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
557 xmlXPathFreeObject (xpathObj);
564 /* 2) if not, use the default string */
565 if (!license->text) {
566 aux = g_strdup_printf (LICENSE_TEXT, "x-default");
567 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
568 if (xpathObj != NULL) {
569 if (xpathObj->nodesetval != NULL &&
570 xpathObj->nodesetval->nodeNr != 0)
571 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
572 xmlXPathFreeObject (xpathObj);
577 /* Checking the license URI as defined by the Creative Commons
578 * Schema. This field is recomended to be checked by Creative
580 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_URI, xpathCtx);
581 if (xpathObj != NULL) {
582 if (xpathObj->nodesetval != NULL &&
583 xpathObj->nodesetval->nodeNr != 0)
584 license->uri = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
585 xmlXPathFreeObject (xpathObj);
588 /* Checking the web statement as defined by the XMP Rights
589 * Management Schema. Checking it out is a sort of above-and-beyond
590 * the basic recommendations by Creative Commons. It can be
591 * considered as a "reinforcement" approach to add certainty. */
592 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_WEB_STATEMENT, xpathCtx);
593 if (xpathObj != NULL) {
594 if (xpathObj->nodesetval != NULL &&
595 xpathObj->nodesetval->nodeNr != 0)
596 license->web_statement = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
597 xmlXPathFreeObject (xpathObj);
602 if (!license->text && !license->uri && !license->web_statement) {
603 ev_document_license_free (license);
611 pdf_document_parse_metadata (const gchar *metadata,
612 EvDocumentInfo *info)
615 xmlXPathContextPtr xpathCtx;
618 doc = xmlParseMemory (metadata, strlen (metadata));
620 return; /* invalid xml metadata */
622 xpathCtx = xmlXPathNewContext (doc);
623 if (xpathCtx == NULL) {
625 return; /* invalid xpath context */
628 fmt = pdf_document_get_format_from_metadata (doc, xpathCtx);
630 g_free (info->format);
634 info->license = pdf_document_get_license_from_metadata (doc, xpathCtx);
636 xmlXPathFreeContext (xpathCtx);
641 static EvDocumentInfo *
642 pdf_document_get_info (EvDocument *document)
644 EvDocumentInfo *info;
645 PopplerPageLayout layout;
646 PopplerPageMode mode;
647 PopplerViewerPreferences view_prefs;
648 PopplerPermissions permissions;
652 info = g_new0 (EvDocumentInfo, 1);
654 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
655 EV_DOCUMENT_INFO_FORMAT |
656 EV_DOCUMENT_INFO_AUTHOR |
657 EV_DOCUMENT_INFO_SUBJECT |
658 EV_DOCUMENT_INFO_KEYWORDS |
659 EV_DOCUMENT_INFO_LAYOUT |
660 EV_DOCUMENT_INFO_START_MODE |
661 EV_DOCUMENT_INFO_PERMISSIONS |
662 EV_DOCUMENT_INFO_UI_HINTS |
663 EV_DOCUMENT_INFO_CREATOR |
664 EV_DOCUMENT_INFO_PRODUCER |
665 EV_DOCUMENT_INFO_CREATION_DATE |
666 EV_DOCUMENT_INFO_MOD_DATE |
667 EV_DOCUMENT_INFO_LINEARIZED |
668 EV_DOCUMENT_INFO_N_PAGES |
669 EV_DOCUMENT_INFO_SECURITY |
670 EV_DOCUMENT_INFO_PAPER_SIZE |
671 EV_DOCUMENT_INFO_LICENSE;
673 g_object_get (PDF_DOCUMENT (document)->document,
674 "title", &(info->title),
675 "format", &(info->format),
676 "author", &(info->author),
677 "subject", &(info->subject),
678 "keywords", &(info->keywords),
680 "page-layout", &layout,
681 "viewer-preferences", &view_prefs,
682 "permissions", &permissions,
683 "creator", &(info->creator),
684 "producer", &(info->producer),
685 "creation-date", &(info->creation_date),
686 "mod-date", &(info->modified_date),
687 "linearized", &(info->linearized),
688 "metadata", &metadata,
691 if (metadata != NULL) {
692 pdf_document_parse_metadata (metadata, info);
696 info->n_pages = ev_document_get_n_pages (document);
698 if (info->n_pages > 0) {
699 ev_document_get_page_size (document, 0,
700 &(info->paper_width),
701 &(info->paper_height));
703 info->paper_width = info->paper_width / 72.0f * 25.4f;
704 info->paper_height = info->paper_height / 72.0f * 25.4f;
708 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
709 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
711 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
712 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
714 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
715 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
717 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
718 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
719 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
720 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
722 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
723 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
730 case POPPLER_PAGE_MODE_NONE:
731 info->mode = EV_DOCUMENT_MODE_NONE;
733 case POPPLER_PAGE_MODE_USE_THUMBS:
734 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
736 case POPPLER_PAGE_MODE_USE_OC:
737 info->mode = EV_DOCUMENT_MODE_USE_OC;
739 case POPPLER_PAGE_MODE_FULL_SCREEN:
740 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
742 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
743 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
749 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
750 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
752 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
753 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
755 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
756 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
758 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
759 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
761 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
762 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
764 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
765 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
767 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
768 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
771 info->permissions = 0;
772 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
773 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
775 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
776 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
778 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
779 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
781 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
782 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
785 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
786 /* translators: this is the document security state */
787 info->security = g_strdup (_("Yes"));
789 /* translators: this is the document security state */
790 info->security = g_strdup (_("No"));
797 pdf_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
799 PopplerBackend backend;
801 backend = poppler_get_backend ();
803 case POPPLER_BACKEND_CAIRO:
804 info->name = "poppler/cairo";
806 case POPPLER_BACKEND_SPLASH:
807 info->name = "poppler/splash";
810 info->name = "poppler/unknown";
814 info->version = poppler_get_version ();
820 pdf_document_class_init (PdfDocumentClass *klass)
822 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
823 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
825 g_object_class->dispose = pdf_document_dispose;
827 ev_document_class->save = pdf_document_save;
828 ev_document_class->load = pdf_document_load;
829 ev_document_class->get_n_pages = pdf_document_get_n_pages;
830 ev_document_class->get_page = pdf_document_get_page;
831 ev_document_class->get_page_size = pdf_document_get_page_size;
832 ev_document_class->get_page_label = pdf_document_get_page_label;
833 ev_document_class->render = pdf_document_render;
834 ev_document_class->get_info = pdf_document_get_info;
835 ev_document_class->get_backend_info = pdf_document_get_backend_info;
838 /* EvDocumentSecurity */
840 pdf_document_has_document_security (EvDocumentSecurity *document_security)
842 /* FIXME: do we really need to have this? */
847 pdf_document_set_password (EvDocumentSecurity *document_security,
848 const char *password)
850 PdfDocument *document = PDF_DOCUMENT (document_security);
852 if (document->password)
853 g_free (document->password);
855 document->password = g_strdup (password);
859 pdf_document_security_iface_init (EvDocumentSecurityInterface *iface)
861 iface->has_document_security = pdf_document_has_document_security;
862 iface->set_password = pdf_document_set_password;
866 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
868 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
871 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
873 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
877 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
880 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
883 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
885 if (pdf_document->font_info == NULL) {
886 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
889 if (pdf_document->fonts_iter) {
890 poppler_fonts_iter_free (pdf_document->fonts_iter);
893 pdf_document->fonts_scanned_pages += n_pages;
895 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
896 &pdf_document->fonts_iter);
898 pdf_document->fonts_scanned_pages = 0;
899 poppler_font_info_free (pdf_document->font_info);
900 pdf_document->font_info = NULL;
907 font_type_to_string (PopplerFontType type)
910 case POPPLER_FONT_TYPE_TYPE1:
912 case POPPLER_FONT_TYPE_TYPE1C:
914 case POPPLER_FONT_TYPE_TYPE3:
916 case POPPLER_FONT_TYPE_TRUETYPE:
917 return _("TrueType");
918 case POPPLER_FONT_TYPE_CID_TYPE0:
919 return _("Type 1 (CID)");
920 case POPPLER_FONT_TYPE_CID_TYPE0C:
921 return _("Type 1C (CID)");
922 case POPPLER_FONT_TYPE_CID_TYPE2:
923 return _("TrueType (CID)");
925 return _("Unknown font type");
930 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
933 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
934 PopplerFontsIter *iter = pdf_document->fonts_iter;
936 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
942 GtkTreeIter list_iter;
945 const char *embedded;
948 name = poppler_fonts_iter_get_name (iter);
954 type = font_type_to_string (
955 poppler_fonts_iter_get_font_type (iter));
957 if (poppler_fonts_iter_is_embedded (iter)) {
958 if (poppler_fonts_iter_is_subset (iter))
959 embedded = _("Embedded subset");
961 embedded = _("Embedded");
963 embedded = _("Not embedded");
966 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
968 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
969 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
970 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
971 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
975 } while (poppler_fonts_iter_next (iter));
979 pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface)
981 iface->fill_model = pdf_document_fonts_fill_model;
982 iface->scan = pdf_document_fonts_scan;
983 iface->get_progress = pdf_document_fonts_get_progress;
987 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
989 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
990 PopplerIndexIter *iter;
992 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
994 iter = poppler_index_iter_new (pdf_document->document);
997 poppler_index_iter_free (iter);
1003 ev_link_dest_from_dest (PdfDocument *pdf_document,
1006 EvLinkDest *ev_dest = NULL;
1007 const char *unimplemented_dest = NULL;
1009 g_assert (dest != NULL);
1011 switch (dest->type) {
1012 case POPPLER_DEST_XYZ: {
1013 PopplerPage *poppler_page;
1016 poppler_page = poppler_document_get_page (pdf_document->document,
1017 MAX (0, dest->page_num - 1));
1018 poppler_page_get_size (poppler_page, NULL, &height);
1019 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1021 height - MIN (height, dest->top),
1026 g_object_unref (poppler_page);
1029 case POPPLER_DEST_FITB:
1030 case POPPLER_DEST_FIT:
1031 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1033 case POPPLER_DEST_FITBH:
1034 case POPPLER_DEST_FITH: {
1035 PopplerPage *poppler_page;
1038 poppler_page = poppler_document_get_page (pdf_document->document,
1039 MAX (0, dest->page_num - 1));
1040 poppler_page_get_size (poppler_page, NULL, &height);
1041 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1042 height - MIN (height, dest->top),
1044 g_object_unref (poppler_page);
1047 case POPPLER_DEST_FITBV:
1048 case POPPLER_DEST_FITV:
1049 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1053 case POPPLER_DEST_FITR: {
1054 PopplerPage *poppler_page;
1057 poppler_page = poppler_document_get_page (pdf_document->document,
1058 MAX (0, dest->page_num - 1));
1059 poppler_page_get_size (poppler_page, NULL, &height);
1060 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1062 height - MIN (height, dest->bottom),
1064 height - MIN (height, dest->top));
1065 g_object_unref (poppler_page);
1068 case POPPLER_DEST_NAMED:
1069 ev_dest = ev_link_dest_new_named (dest->named_dest);
1071 case POPPLER_DEST_UNKNOWN:
1072 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1076 if (unimplemented_dest) {
1077 g_warning ("Unimplemented destination: %s, please post a "
1078 "bug report in Evince bugzilla "
1079 "(http://bugzilla.gnome.org) with a testcase.",
1080 unimplemented_dest);
1084 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1090 ev_link_from_action (PdfDocument *pdf_document,
1091 PopplerAction *action)
1093 EvLink *link = NULL;
1094 EvLinkAction *ev_action = NULL;
1095 const char *unimplemented_action = NULL;
1097 switch (action->type) {
1098 case POPPLER_ACTION_NONE:
1100 case POPPLER_ACTION_GOTO_DEST: {
1103 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1104 ev_action = ev_link_action_new_dest (dest);
1107 case POPPLER_ACTION_GOTO_REMOTE: {
1110 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1111 ev_action = ev_link_action_new_remote (dest,
1112 action->goto_remote.file_name);
1116 case POPPLER_ACTION_LAUNCH:
1117 ev_action = ev_link_action_new_launch (action->launch.file_name,
1118 action->launch.params);
1120 case POPPLER_ACTION_URI:
1121 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1123 case POPPLER_ACTION_NAMED:
1124 ev_action = ev_link_action_new_named (action->named.named_dest);
1126 case POPPLER_ACTION_MOVIE:
1127 unimplemented_action = "POPPLER_ACTION_MOVIE";
1129 #if POPPLER_CHECK_VERSION (0, 13, 2)
1130 case POPPLER_ACTION_RENDITION:
1131 unimplemented_action = "POPPLER_ACTION_RENDITION";
1133 case POPPLER_ACTION_OCG_STATE:
1134 unimplemented_action = "POPPLER_ACTION_OCG_STATE";
1137 case POPPLER_ACTION_UNKNOWN:
1138 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1141 if (unimplemented_action) {
1142 g_warning ("Unimplemented action: %s, please post a bug report "
1143 "in Evince bugzilla (http://bugzilla.gnome.org) "
1144 "with a testcase.", unimplemented_action);
1147 link = ev_link_new (action->any.title, ev_action);
1153 build_tree (PdfDocument *pdf_document,
1154 GtkTreeModel *model,
1155 GtkTreeIter *parent,
1156 PopplerIndexIter *iter)
1160 GtkTreeIter tree_iter;
1161 PopplerIndexIter *child;
1162 PopplerAction *action;
1163 EvLink *link = NULL;
1167 action = poppler_index_iter_get_action (iter);
1168 expand = poppler_index_iter_is_open (iter);
1173 switch (action->type) {
1174 case POPPLER_ACTION_GOTO_DEST: {
1175 /* For bookmarks, solve named destinations */
1176 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1178 EvLinkDest *ev_dest = NULL;
1179 EvLinkAction *ev_action;
1181 dest = poppler_document_find_dest (pdf_document->document,
1182 action->goto_dest.dest->named_dest);
1184 link = ev_link_from_action (pdf_document, action);
1188 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1189 poppler_dest_free (dest);
1191 ev_action = ev_link_action_new_dest (ev_dest);
1192 link = ev_link_new (action->any.title, ev_action);
1194 link = ev_link_from_action (pdf_document, action);
1199 link = ev_link_from_action (pdf_document, action);
1203 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1204 poppler_action_free (action);
1206 g_object_unref (link);
1211 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1212 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1214 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1215 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1216 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1217 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1220 g_free (title_markup);
1221 g_object_unref (link);
1223 child = poppler_index_iter_get_child (iter);
1225 build_tree (pdf_document, model, &tree_iter, child);
1226 poppler_index_iter_free (child);
1227 poppler_action_free (action);
1229 } while (poppler_index_iter_next (iter));
1232 static GtkTreeModel *
1233 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1235 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1236 GtkTreeModel *model = NULL;
1237 PopplerIndexIter *iter;
1239 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1241 iter = poppler_index_iter_new (pdf_document->document);
1242 /* Create the model if we have items*/
1244 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1249 build_tree (pdf_document, model, NULL, iter);
1250 poppler_index_iter_free (iter);
1257 pdf_document_links_get_links (EvDocumentLinks *document_links,
1260 PdfDocument *pdf_document;
1261 PopplerPage *poppler_page;
1262 GList *retval = NULL;
1263 GList *mapping_list;
1267 pdf_document = PDF_DOCUMENT (document_links);
1268 poppler_page = POPPLER_PAGE (page->backend_page);
1269 mapping_list = poppler_page_get_link_mapping (poppler_page);
1270 poppler_page_get_size (poppler_page, NULL, &height);
1272 for (list = mapping_list; list; list = list->next) {
1273 PopplerLinkMapping *link_mapping;
1274 EvMapping *ev_link_mapping;
1276 link_mapping = (PopplerLinkMapping *)list->data;
1277 ev_link_mapping = g_new (EvMapping, 1);
1278 ev_link_mapping->data = ev_link_from_action (pdf_document,
1279 link_mapping->action);
1280 ev_link_mapping->area.x1 = link_mapping->area.x1;
1281 ev_link_mapping->area.x2 = link_mapping->area.x2;
1282 /* Invert this for X-style coordinates */
1283 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1284 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1286 retval = g_list_prepend (retval, ev_link_mapping);
1289 poppler_page_free_link_mapping (mapping_list);
1291 return g_list_reverse (retval);
1295 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1296 const gchar *link_name)
1298 PdfDocument *pdf_document;
1300 EvLinkDest *ev_dest = NULL;
1302 pdf_document = PDF_DOCUMENT (document_links);
1303 dest = poppler_document_find_dest (pdf_document->document,
1306 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1307 poppler_dest_free (dest);
1314 pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface)
1316 iface->has_document_links = pdf_document_links_has_document_links;
1317 iface->get_links_model = pdf_document_links_get_links_model;
1318 iface->get_links = pdf_document_links_get_links;
1319 iface->find_link_dest = pdf_document_links_find_link_dest;
1323 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1326 GList *retval = NULL;
1327 PdfDocument *pdf_document;
1328 PopplerPage *poppler_page;
1329 GList *mapping_list;
1332 pdf_document = PDF_DOCUMENT (document_images);
1333 poppler_page = POPPLER_PAGE (page->backend_page);
1334 mapping_list = poppler_page_get_image_mapping (poppler_page);
1336 for (list = mapping_list; list; list = list->next) {
1337 PopplerImageMapping *image_mapping;
1338 EvMapping *ev_image_mapping;
1340 image_mapping = (PopplerImageMapping *)list->data;
1342 ev_image_mapping = g_new (EvMapping, 1);
1344 ev_image_mapping->data = ev_image_new (page->index, image_mapping->image_id);
1345 ev_image_mapping->area.x1 = image_mapping->area.x1;
1346 ev_image_mapping->area.y1 = image_mapping->area.y1;
1347 ev_image_mapping->area.x2 = image_mapping->area.x2;
1348 ev_image_mapping->area.y2 = image_mapping->area.y2;
1350 retval = g_list_prepend (retval, ev_image_mapping);
1353 poppler_page_free_image_mapping (mapping_list);
1355 return g_list_reverse (retval);
1359 pdf_document_images_get_image (EvDocumentImages *document_images,
1362 GdkPixbuf *retval = NULL;
1363 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1364 PdfDocument *pdf_document;
1365 PopplerPage *poppler_page;
1366 cairo_surface_t *surface;
1368 pdf_document = PDF_DOCUMENT (document_images);
1369 poppler_page = poppler_document_get_page (pdf_document->document,
1370 ev_image_get_page (image));
1372 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1374 retval = ev_document_misc_pixbuf_from_surface (surface);
1375 cairo_surface_destroy (surface);
1378 g_object_unref (poppler_page);
1384 pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface)
1386 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1387 iface->get_image = pdf_document_images_get_image;
1391 make_thumbnail_for_page (PopplerPage *poppler_page,
1392 EvRenderContext *rc,
1398 #ifdef POPPLER_WITH_GDK
1399 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1401 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1403 ev_document_fc_mutex_lock ();
1404 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1406 rc->scale, rc->rotation, pixbuf);
1407 ev_document_fc_mutex_unlock ();
1409 cairo_surface_t *surface;
1411 ev_document_fc_mutex_lock ();
1412 surface = pdf_page_render (poppler_page, width, height, rc);
1413 ev_document_fc_mutex_unlock ();
1415 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1416 cairo_surface_destroy (surface);
1417 #endif /* POPPLER_WITH_GDK */
1423 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1424 EvRenderContext *rc,
1427 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1428 PopplerPage *poppler_page;
1429 GdkPixbuf *pixbuf = NULL;
1430 GdkPixbuf *border_pixbuf;
1433 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1435 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1436 rc, &width, &height);
1438 #ifdef POPPLER_WITH_GDK
1439 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1441 cairo_surface_t *surface;
1443 surface = poppler_page_get_thumbnail (poppler_page);
1445 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1446 cairo_surface_destroy (surface);
1448 #endif /* POPPLER_WITH_GDK */
1450 if (pixbuf != NULL) {
1451 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1452 gdk_pixbuf_get_height (pixbuf) :
1453 gdk_pixbuf_get_width (pixbuf);
1455 if (thumb_width == width) {
1456 GdkPixbuf *rotated_pixbuf;
1458 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1459 (GdkPixbufRotation) (360 - rc->rotation));
1460 g_object_unref (pixbuf);
1461 pixbuf = rotated_pixbuf;
1463 /* The provided thumbnail has a different size */
1464 g_object_unref (pixbuf);
1465 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1468 /* There is no provided thumbnail. We need to make one. */
1469 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1472 if (border && pixbuf) {
1473 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1474 g_object_unref (pixbuf);
1475 pixbuf = border_pixbuf;
1482 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1483 EvRenderContext *rc,
1487 double page_width, page_height;
1489 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1490 &page_width, &page_height);
1492 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1493 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1495 if (rc->rotation == 90 || rc->rotation == 270) {
1505 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
1507 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1508 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1513 pdf_document_find_find_text (EvDocumentFind *document_find,
1516 gboolean case_sensitive)
1519 PopplerPage *poppler_page;
1521 GList *retval = NULL;
1523 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1524 g_return_val_if_fail (text != NULL, NULL);
1526 poppler_page = POPPLER_PAGE (page->backend_page);
1528 matches = poppler_page_find_text (poppler_page, text);
1532 poppler_page_get_size (poppler_page, NULL, &height);
1533 for (l = matches; l && l->data; l = g_list_next (l)) {
1534 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1535 EvRectangle *ev_rect;
1537 ev_rect = ev_rectangle_new ();
1538 ev_rect->x1 = rect->x1;
1539 ev_rect->x2 = rect->x2;
1540 /* Invert this for X-style coordinates */
1541 ev_rect->y1 = height - rect->y2;
1542 ev_rect->y2 = height - rect->y1;
1544 retval = g_list_prepend (retval, ev_rect);
1547 g_list_foreach (matches, (GFunc)poppler_rectangle_free, NULL);
1548 g_list_free (matches);
1550 return g_list_reverse (retval);
1554 pdf_document_find_iface_init (EvDocumentFindInterface *iface)
1556 iface->find_text = pdf_document_find_find_text;
1560 pdf_print_context_free (PdfPrintContext *ctx)
1565 #ifdef HAVE_CAIRO_PRINT
1567 cairo_destroy (ctx->cr);
1572 poppler_ps_file_free (ctx->ps_file);
1573 ctx->ps_file = NULL;
1580 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1581 EvFileExporterContext *fc)
1583 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1584 PdfPrintContext *ctx;
1585 #ifdef HAVE_CAIRO_PRINT
1586 gdouble width, height;
1587 cairo_surface_t *surface = NULL;
1590 if (pdf_document->print_ctx)
1591 pdf_print_context_free (pdf_document->print_ctx);
1592 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1593 ctx = pdf_document->print_ctx;
1594 ctx->format = fc->format;
1596 #ifdef HAVE_CAIRO_PRINT
1597 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1599 ctx->paper_width = fc->paper_width;
1600 ctx->paper_height = fc->paper_height;
1602 switch (fc->pages_per_sheet) {
1630 ctx->pages_printed = 0;
1632 switch (fc->format) {
1633 case EV_FILE_FORMAT_PS:
1634 #ifdef HAVE_CAIRO_PS
1635 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1638 case EV_FILE_FORMAT_PDF:
1639 #ifdef HAVE_CAIRO_PDF
1640 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1644 g_assert_not_reached ();
1647 ctx->cr = cairo_create (surface);
1648 cairo_surface_destroy (surface);
1650 #else /* HAVE_CAIRO_PRINT */
1651 if (ctx->format == EV_FILE_FORMAT_PS) {
1652 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1653 fc->filename, fc->first_page,
1654 fc->last_page - fc->first_page + 1);
1655 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1656 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1658 #endif /* HAVE_CAIRO_PRINT */
1662 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1664 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1665 PdfPrintContext *ctx = pdf_document->print_ctx;
1667 g_return_if_fail (pdf_document->print_ctx != NULL);
1669 ctx->pages_printed = 0;
1671 #ifdef HAVE_CAIRO_PRINT
1672 if (ctx->paper_width > ctx->paper_height) {
1673 if (ctx->format == EV_FILE_FORMAT_PS) {
1674 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1677 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1678 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1683 #endif /* HAVE_CAIRO_PRINT */
1687 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1688 EvRenderContext *rc)
1690 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1691 PdfPrintContext *ctx = pdf_document->print_ctx;
1692 PopplerPage *poppler_page;
1693 #ifdef HAVE_CAIRO_PRINT
1694 gdouble page_width, page_height;
1697 gdouble width, height;
1698 gdouble pwidth, pheight;
1699 gdouble xscale, yscale;
1702 g_return_if_fail (pdf_document->print_ctx != NULL);
1704 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1706 #ifdef HAVE_CAIRO_PRINT
1707 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1708 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1709 poppler_page_get_size (poppler_page, &page_width, &page_height);
1711 if (page_width > page_height && page_width > ctx->paper_width) {
1717 /* Use always portrait mode and rotate when necessary */
1718 if (ctx->paper_width > ctx->paper_height) {
1719 width = ctx->paper_height;
1720 height = ctx->paper_width;
1723 width = ctx->paper_width;
1724 height = ctx->paper_height;
1727 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1740 page_width = page_height;
1744 pwidth = width / ctx->pages_x;
1745 pheight = height / ctx->pages_y;
1747 if ((page_width > pwidth || page_height > pheight) ||
1748 (page_width < pwidth && page_height < pheight)) {
1749 xscale = pwidth / page_width;
1750 yscale = pheight / page_height;
1752 if (yscale < xscale) {
1759 xscale = yscale = 1;
1764 cairo_save (ctx->cr);
1766 cairo_matrix_t matrix;
1768 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1769 cairo_matrix_init (&matrix,
1773 cairo_transform (ctx->cr, &matrix);
1776 cairo_translate (ctx->cr,
1777 x * (rotate ? pheight : pwidth),
1778 y * (rotate ? pwidth : pheight));
1779 cairo_scale (ctx->cr, xscale, yscale);
1781 poppler_page_render_for_printing (poppler_page, ctx->cr);
1783 ctx->pages_printed++;
1785 cairo_restore (ctx->cr);
1786 #else /* HAVE_CAIRO_PRINT */
1787 if (ctx->format == EV_FILE_FORMAT_PS)
1788 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1789 #endif /* HAVE_CAIRO_PRINT */
1793 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1795 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1796 PdfPrintContext *ctx = pdf_document->print_ctx;
1798 g_return_if_fail (pdf_document->print_ctx != NULL);
1800 #ifdef HAVE_CAIRO_PRINT
1801 cairo_show_page (ctx->cr);
1806 pdf_document_file_exporter_end (EvFileExporter *exporter)
1808 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1810 pdf_print_context_free (pdf_document->print_ctx);
1811 pdf_document->print_ctx = NULL;
1814 static EvFileExporterCapabilities
1815 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1817 return (EvFileExporterCapabilities) (
1818 EV_FILE_EXPORTER_CAN_PAGE_SET |
1819 EV_FILE_EXPORTER_CAN_COPIES |
1820 EV_FILE_EXPORTER_CAN_COLLATE |
1821 EV_FILE_EXPORTER_CAN_REVERSE |
1822 EV_FILE_EXPORTER_CAN_SCALE |
1823 #ifdef HAVE_CAIRO_PRINT
1824 #ifdef HAVE_POPPLER_PAGE_RENDER
1825 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1829 #ifdef HAVE_CAIRO_PDF
1830 #ifdef HAVE_POPPLER_PAGE_RENDER
1831 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1834 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1838 pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface)
1840 iface->begin = pdf_document_file_exporter_begin;
1841 iface->begin_page = pdf_document_file_exporter_begin_page;
1842 iface->do_page = pdf_document_file_exporter_do_page;
1843 iface->end_page = pdf_document_file_exporter_end_page;
1844 iface->end = pdf_document_file_exporter_end;
1845 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1848 #ifdef HAVE_POPPLER_PAGE_RENDER
1849 /* EvDocumentPrint */
1851 pdf_document_print_print_page (EvDocumentPrint *document,
1855 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1857 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1861 pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface)
1863 iface->print_page = pdf_document_print_print_page;
1865 #endif /* HAVE_POPPLER_PAGE_RENDER */
1868 pdf_selection_render_selection (EvSelection *selection,
1869 EvRenderContext *rc,
1870 cairo_surface_t **surface,
1871 EvRectangle *points,
1872 EvRectangle *old_points,
1873 EvSelectionStyle style,
1877 PopplerPage *poppler_page;
1878 double width_points, height_points;
1881 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1883 poppler_page_get_size (poppler_page,
1884 &width_points, &height_points);
1885 width = (int) ((width_points * rc->scale) + 0.5);
1886 height = (int) ((height_points * rc->scale) + 0.5);
1888 #ifdef HAVE_POPPLER_PAGE_RENDER
1890 PopplerColor text_color, base_color;
1892 text_color.red = text->red;
1893 text_color.green = text->green;
1894 text_color.blue = text->blue;
1896 base_color.red = base->red;
1897 base_color.green = base->green;
1898 base_color.blue = base->blue;
1900 if (*surface == NULL) {
1901 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1906 cr = cairo_create (*surface);
1907 cairo_scale (cr, rc->scale, rc->scale);
1908 cairo_surface_set_device_offset (*surface, 0, 0);
1909 memset (cairo_image_surface_get_data (*surface), 0x00,
1910 cairo_image_surface_get_height (*surface) *
1911 cairo_image_surface_get_stride (*surface));
1912 poppler_page_render_selection (poppler_page,
1914 (PopplerRectangle *)points,
1915 (PopplerRectangle *)old_points,
1916 (PopplerSelectionStyle)style,
1920 #else /* HAVE_POPPLER_PAGE_RENDER */
1923 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1927 poppler_page_render_selection_to_pixbuf (poppler_page,
1928 rc->scale, rc->rotation, pixbuf,
1929 (PopplerRectangle *)points,
1930 (PopplerRectangle *)old_points,
1931 (PopplerSelectionStyle)style,
1935 cairo_surface_destroy (*surface);
1936 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1937 g_object_unref (pixbuf);
1938 #endif /* HAVE_POPPLER_PAGE_RENDER */
1942 pdf_selection_get_selected_text (EvSelection *selection,
1944 EvSelectionStyle style,
1945 EvRectangle *points)
1947 PopplerPage *poppler_page;
1952 poppler_page = POPPLER_PAGE (page->backend_page);
1954 poppler_page_get_size (poppler_page, NULL, &height);
1956 r.y1 = height - points->y2;
1958 r.y2 = height - points->y1;
1960 retval = poppler_page_get_text (poppler_page,
1961 (PopplerSelectionStyle)style,
1968 create_gdk_region_from_poppler_region (GList *region)
1973 retval = gdk_region_new ();
1975 for (l = region; l; l = g_list_next (l)) {
1976 PopplerRectangle *rectangle;
1979 rectangle = (PopplerRectangle *)l->data;
1981 rect.x = (gint) rectangle->x1;
1982 rect.y = (gint) rectangle->y1;
1983 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1984 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1985 gdk_region_union_with_rect (retval, &rect);
1987 poppler_rectangle_free (rectangle);
1994 pdf_selection_get_selection_region (EvSelection *selection,
1995 EvRenderContext *rc,
1996 EvSelectionStyle style,
1997 EvRectangle *points)
1999 PopplerPage *poppler_page;
2003 poppler_page = POPPLER_PAGE (rc->page->backend_page);
2005 region = poppler_page_get_selection_region (poppler_page,
2007 (PopplerSelectionStyle)style,
2008 (PopplerRectangle *) points);
2009 retval = create_gdk_region_from_poppler_region (region);
2010 g_list_free (region);
2016 pdf_selection_get_selection_map (EvSelection *selection,
2019 PopplerPage *poppler_page;
2020 PopplerRectangle points;
2024 poppler_page = POPPLER_PAGE (page->backend_page);
2028 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2030 region = poppler_page_get_selection_region (poppler_page, 1.0,
2031 POPPLER_SELECTION_GLYPH,
2033 retval = create_gdk_region_from_poppler_region (region);
2034 g_list_free (region);
2040 pdf_selection_iface_init (EvSelectionInterface *iface)
2042 iface->render_selection = pdf_selection_render_selection;
2043 iface->get_selected_text = pdf_selection_get_selected_text;
2044 iface->get_selection_region = pdf_selection_get_selection_region;
2045 iface->get_selection_map = pdf_selection_get_selection_map;
2048 /* Page Transitions */
2050 pdf_document_get_page_duration (EvDocumentTransition *trans,
2053 PdfDocument *pdf_document;
2054 PopplerPage *poppler_page;
2055 gdouble duration = -1;
2057 pdf_document = PDF_DOCUMENT (trans);
2058 poppler_page = poppler_document_get_page (pdf_document->document, page);
2062 duration = poppler_page_get_duration (poppler_page);
2063 g_object_unref (poppler_page);
2068 static EvTransitionEffect *
2069 pdf_document_get_effect (EvDocumentTransition *trans,
2072 PdfDocument *pdf_document;
2073 PopplerPage *poppler_page;
2074 PopplerPageTransition *page_transition;
2075 EvTransitionEffect *effect;
2077 pdf_document = PDF_DOCUMENT (trans);
2078 poppler_page = poppler_document_get_page (pdf_document->document, page);
2083 page_transition = poppler_page_get_transition (poppler_page);
2085 if (!page_transition) {
2086 g_object_unref (poppler_page);
2090 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2091 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2092 "alignment", page_transition->alignment,
2093 "direction", page_transition->direction,
2094 "duration", page_transition->duration,
2095 "angle", page_transition->angle,
2096 "scale", page_transition->scale,
2097 "rectangular", page_transition->rectangular,
2100 poppler_page_transition_free (page_transition);
2101 g_object_unref (poppler_page);
2107 pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface)
2109 iface->get_page_duration = pdf_document_get_page_duration;
2110 iface->get_effect = pdf_document_get_effect;
2115 pdf_document_get_crop_box (EvDocument *document,
2119 PdfDocument *pdf_document;
2120 PopplerPage *poppler_page;
2121 PopplerRectangle poppler_rect;
2123 pdf_document = PDF_DOCUMENT (document);
2124 poppler_page = poppler_document_get_page (pdf_document->document, page);
2125 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2126 rect->x1 = poppler_rect.x1;
2127 rect->x2 = poppler_rect.x2;
2128 rect->y1 = poppler_rect.y1;
2129 rect->y2 = poppler_rect.y2;
2132 static EvFormField *
2133 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2135 EvFormField *ev_field = NULL;
2138 gboolean is_read_only;
2140 id = poppler_form_field_get_id (poppler_field);
2141 font_size = poppler_form_field_get_font_size (poppler_field);
2142 is_read_only = poppler_form_field_is_read_only (poppler_field);
2144 switch (poppler_form_field_get_field_type (poppler_field)) {
2145 case POPPLER_FORM_FIELD_TEXT: {
2146 EvFormFieldText *field_text;
2147 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2149 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2150 case POPPLER_FORM_TEXT_NORMAL:
2151 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2153 case POPPLER_FORM_TEXT_MULTILINE:
2154 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2156 case POPPLER_FORM_TEXT_FILE_SELECT:
2157 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2161 ev_field = ev_form_field_text_new (id, ev_text_type);
2162 field_text = EV_FORM_FIELD_TEXT (ev_field);
2164 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2165 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2166 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2167 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2168 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2169 field_text->text = poppler_form_field_text_get_text (poppler_field);
2173 case POPPLER_FORM_FIELD_BUTTON: {
2174 EvFormFieldButton *field_button;
2175 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2177 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2178 case POPPLER_FORM_BUTTON_PUSH:
2179 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2181 case POPPLER_FORM_BUTTON_CHECK:
2182 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2184 case POPPLER_FORM_BUTTON_RADIO:
2185 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2189 ev_field = ev_form_field_button_new (id, ev_button_type);
2190 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2192 field_button->state = poppler_form_field_button_get_state (poppler_field);
2195 case POPPLER_FORM_FIELD_CHOICE: {
2196 EvFormFieldChoice *field_choice;
2197 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2199 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2200 case POPPLER_FORM_CHOICE_COMBO:
2201 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2203 case EV_FORM_FIELD_CHOICE_LIST:
2204 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2208 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2209 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2211 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2212 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2213 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2214 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2216 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2217 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2218 if (field_choice->is_editable)
2219 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2222 case POPPLER_FORM_FIELD_SIGNATURE:
2224 ev_field = ev_form_field_signature_new (id);
2226 case POPPLER_FORM_FIELD_UNKNOWN:
2230 ev_field->font_size = font_size;
2231 ev_field->is_read_only = is_read_only;
2237 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2240 PopplerPage *poppler_page;
2241 GList *retval = NULL;
2246 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2248 poppler_page = POPPLER_PAGE (page->backend_page);
2249 fields = poppler_page_get_form_field_mapping (poppler_page);
2250 poppler_page_get_size (poppler_page, NULL, &height);
2252 for (list = fields; list; list = list->next) {
2253 PopplerFormFieldMapping *mapping;
2254 EvMapping *field_mapping;
2255 EvFormField *ev_field;
2257 mapping = (PopplerFormFieldMapping *)list->data;
2259 ev_field = ev_form_field_from_poppler_field (mapping->field);
2263 field_mapping = g_new0 (EvMapping, 1);
2264 field_mapping->area.x1 = mapping->area.x1;
2265 field_mapping->area.x2 = mapping->area.x2;
2266 field_mapping->area.y1 = height - mapping->area.y2;
2267 field_mapping->area.y2 = height - mapping->area.y1;
2268 field_mapping->data = ev_field;
2269 ev_field->page = EV_PAGE (g_object_ref (page));
2271 g_object_set_data_full (G_OBJECT (ev_field),
2273 g_object_ref (mapping->field),
2274 (GDestroyNotify) g_object_unref);
2276 retval = g_list_prepend (retval, field_mapping);
2279 poppler_page_free_form_field_mapping (fields);
2281 return g_list_reverse (retval);
2285 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2289 PopplerFormField *poppler_field;
2292 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2296 text = poppler_form_field_text_get_text (poppler_field);
2302 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2306 PopplerFormField *poppler_field;
2308 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2312 poppler_form_field_text_set_text (poppler_field, text);
2313 PDF_DOCUMENT (document)->modified = TRUE;
2317 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2321 PopplerFormField *poppler_field;
2323 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2327 poppler_form_field_button_set_state (poppler_field, state);
2328 PDF_DOCUMENT (document)->modified = TRUE;
2332 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2335 PopplerFormField *poppler_field;
2338 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2342 state = poppler_form_field_button_get_state (poppler_field);
2348 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2352 PopplerFormField *poppler_field;
2355 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2359 text = poppler_form_field_choice_get_item (poppler_field, index);
2365 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2368 PopplerFormField *poppler_field;
2371 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2375 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2381 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2385 PopplerFormField *poppler_field;
2388 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2392 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2398 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2402 PopplerFormField *poppler_field;
2404 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2408 poppler_form_field_choice_select_item (poppler_field, index);
2409 PDF_DOCUMENT (document)->modified = TRUE;
2413 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2417 PopplerFormField *poppler_field;
2419 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2423 poppler_form_field_choice_toggle_item (poppler_field, index);
2424 PDF_DOCUMENT (document)->modified = TRUE;
2428 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2431 PopplerFormField *poppler_field;
2433 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2437 poppler_form_field_choice_unselect_all (poppler_field);
2438 PDF_DOCUMENT (document)->modified = TRUE;
2442 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2446 PopplerFormField *poppler_field;
2448 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2452 poppler_form_field_choice_set_text (poppler_field, text);
2453 PDF_DOCUMENT (document)->modified = TRUE;
2457 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2460 PopplerFormField *poppler_field;
2463 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2467 text = poppler_form_field_choice_get_text (poppler_field);
2473 pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface)
2475 iface->get_form_fields = pdf_document_forms_get_form_fields;
2476 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2477 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2478 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2479 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2480 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2481 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2482 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2483 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2484 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2485 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2486 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2487 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2492 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2495 PopplerColor *poppler_color;
2497 poppler_color = poppler_annot_get_color (poppler_annot);
2498 if (poppler_color) {
2499 color->red = poppler_color->red;
2500 color->green = poppler_color->green;
2501 color->blue = poppler_color->blue;
2503 g_free (poppler_color);
2504 } /* TODO: else use a default color */
2507 static EvAnnotation *
2508 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2511 EvAnnotation *ev_annot = NULL;
2512 const gchar *unimplemented_annot = NULL;
2514 switch (poppler_annot_get_annot_type (poppler_annot)) {
2515 case POPPLER_ANNOT_TEXT: {
2516 PopplerAnnotText *poppler_text;
2517 EvAnnotationText *ev_annot_text;
2519 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2521 ev_annot = ev_annotation_text_new (page);
2523 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2524 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2527 #ifdef HAVE_POPPLER_ANNOT_FILE_ATTACHMENT_GET_ATTACHMENT
2528 case POPPLER_ANNOT_FILE_ATTACHMENT: {
2529 PopplerAnnotFileAttachment *poppler_annot_attachment;
2530 EvAnnotationAttachment *ev_annot_attachment;
2531 PopplerAttachment *poppler_attachment;
2534 GError *error = NULL;
2536 poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
2537 poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
2539 if (poppler_attachment &&
2540 attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
2541 EvAttachment *ev_attachment;
2543 ev_attachment = ev_attachment_new (poppler_attachment->name,
2544 poppler_attachment->description,
2545 poppler_attachment->mtime,
2546 poppler_attachment->ctime,
2548 ev_annot = ev_annotation_attachment_new (page, ev_attachment);
2549 g_object_unref (ev_attachment);
2551 g_warning ("%s", error->message);
2552 g_error_free (error);
2555 if (poppler_attachment)
2556 g_object_unref (poppler_attachment);
2559 #endif /* HAVE_POPPLER_ANNOT_FILE_ATTACHMENT_GET_ATTACHMENT */
2560 case POPPLER_ANNOT_LINK:
2561 case POPPLER_ANNOT_WIDGET:
2562 /* Ignore link and widgets annots since they are already handled */
2565 GEnumValue *enum_value;
2567 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2568 poppler_annot_get_annot_type (poppler_annot));
2569 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2573 if (unimplemented_annot) {
2574 g_warning ("Unimplemented annotation: %s, please post a "
2575 "bug report in Evince bugzilla "
2576 "(http://bugzilla.gnome.org) with a testcase.",
2577 unimplemented_annot);
2581 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2582 ev_annot->name = poppler_annot_get_name (poppler_annot);
2583 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2584 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2586 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2587 PopplerAnnotMarkup *markup;
2590 PopplerRectangle poppler_rect;
2592 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2594 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2595 EvRectangle ev_rect;
2599 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2601 ev_rect.x1 = poppler_rect.x1;
2602 ev_rect.x2 = poppler_rect.x2;
2603 ev_rect.y1 = height - poppler_rect.y2;
2604 ev_rect.y2 = height - poppler_rect.y1;
2606 is_open = poppler_annot_markup_get_popup_is_open (markup);
2608 g_object_set (ev_annot,
2609 "rectangle", &ev_rect,
2614 /* FIXME: Use poppler_annot_markup_has_popup() when
2615 * new poppler is released.
2617 g_object_set (ev_annot,
2622 label = poppler_annot_markup_get_label (markup);
2623 opacity = poppler_annot_markup_get_opacity (markup);
2625 g_object_set (ev_annot,
2638 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2641 GList *retval = NULL;
2642 PdfDocument *pdf_document;
2643 PopplerPage *poppler_page;
2649 pdf_document = PDF_DOCUMENT (document_annotations);
2650 poppler_page = POPPLER_PAGE (page->backend_page);
2651 annots = poppler_page_get_annot_mapping (poppler_page);
2652 poppler_page_get_size (poppler_page, NULL, &height);
2654 for (list = annots; list; list = list->next) {
2655 PopplerAnnotMapping *mapping;
2656 EvMapping *annot_mapping;
2657 EvAnnotation *ev_annot;
2659 mapping = (PopplerAnnotMapping *)list->data;
2661 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2667 /* Make sure annot has a unique name */
2668 if (!ev_annot->name)
2669 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2671 annot_mapping = g_new (EvMapping, 1);
2672 annot_mapping->area.x1 = mapping->area.x1;
2673 annot_mapping->area.x2 = mapping->area.x2;
2674 annot_mapping->area.y1 = height - mapping->area.y2;
2675 annot_mapping->area.y2 = height - mapping->area.y1;
2676 annot_mapping->data = ev_annot;
2678 g_object_set_data_full (G_OBJECT (ev_annot),
2680 g_object_ref (mapping->annot),
2681 (GDestroyNotify) g_object_unref);
2683 retval = g_list_prepend (retval, annot_mapping);
2686 poppler_page_free_annot_mapping (annots);
2688 return g_list_reverse (retval);
2692 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2693 EvAnnotation *annot,
2694 const gchar *contents)
2696 PopplerAnnot *poppler_annot;
2698 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2702 poppler_annot_set_contents (poppler_annot, contents);
2703 PDF_DOCUMENT (document)->modified = TRUE;
2707 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface)
2709 iface->get_annotations = pdf_document_annotations_get_annotations;
2710 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2714 struct SaveToBufferData {
2720 attachment_save_to_buffer_callback (const gchar *buf,
2725 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2729 if (sdata->len + count > sdata->max) {
2730 new_max = MAX (sdata->max * 2, sdata->len + count);
2731 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2733 sdata->buffer = new_buffer;
2734 sdata->max = new_max;
2737 memcpy (sdata->buffer + sdata->len, buf, count);
2738 sdata->len += count;
2744 attachment_save_to_buffer (PopplerAttachment *attachment,
2749 static const gint initial_max = 1024;
2750 struct SaveToBufferData sdata;
2755 sdata.buffer = (gchar *) g_malloc (initial_max);
2756 sdata.max = initial_max;
2759 if (! poppler_attachment_save_to_callback (attachment,
2760 attachment_save_to_buffer_callback,
2763 g_free (sdata.buffer);
2767 *buffer = sdata.buffer;
2768 *buffer_size = sdata.len;
2774 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2776 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2779 GList *retval = NULL;
2781 attachments = poppler_document_get_attachments (pdf_document->document);
2783 for (list = attachments; list; list = list->next) {
2784 PopplerAttachment *attachment;
2785 EvAttachment *ev_attachment;
2788 GError *error = NULL;
2790 attachment = (PopplerAttachment *) list->data;
2792 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2793 ev_attachment = ev_attachment_new (attachment->name,
2794 attachment->description,
2799 retval = g_list_prepend (retval, ev_attachment);
2802 g_warning ("%s", error->message);
2803 g_error_free (error);
2809 g_object_unref (attachment);
2812 return g_list_reverse (retval);
2816 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2818 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2820 return poppler_document_has_attachments (pdf_document->document);
2824 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface)
2826 iface->has_attachments = pdf_document_attachments_has_attachments;
2827 iface->get_attachments = pdf_document_attachments_get_attachments;
2832 pdf_document_layers_has_layers (EvDocumentLayers *document)
2834 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2835 PopplerLayersIter *iter;
2837 iter = poppler_layers_iter_new (pdf_document->document);
2840 poppler_layers_iter_free (iter);
2846 build_layers_tree (PdfDocument *pdf_document,
2847 GtkTreeModel *model,
2848 GtkTreeIter *parent,
2849 PopplerLayersIter *iter)
2852 GtkTreeIter tree_iter;
2853 PopplerLayersIter *child;
2854 PopplerLayer *layer;
2855 EvLayer *ev_layer = NULL;
2860 layer = poppler_layers_iter_get_layer (iter);
2862 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2863 visible = poppler_layer_is_visible (layer);
2864 rb_group = poppler_layer_get_radio_button_group_id (layer);
2865 pdf_document->layers = g_list_append (pdf_document->layers,
2866 g_object_ref (layer));
2867 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2868 poppler_layer_is_parent (layer),
2873 title = poppler_layers_iter_get_title (iter);
2874 markup = g_markup_escape_text (title, -1);
2881 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2882 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2883 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2884 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2885 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2886 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2887 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2888 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2891 g_object_unref (ev_layer);
2894 child = poppler_layers_iter_get_child (iter);
2896 build_layers_tree (pdf_document, model, &tree_iter, child);
2897 poppler_layers_iter_free (child);
2898 } while (poppler_layers_iter_next (iter));
2901 static GtkTreeModel *
2902 pdf_document_layers_get_layers (EvDocumentLayers *document)
2904 GtkTreeModel *model = NULL;
2905 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2906 PopplerLayersIter *iter;
2908 iter = poppler_layers_iter_new (pdf_document->document);
2910 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2911 G_TYPE_STRING, /* TITLE */
2912 G_TYPE_OBJECT, /* LAYER */
2913 G_TYPE_BOOLEAN, /* VISIBLE */
2914 G_TYPE_BOOLEAN, /* ENABLED */
2915 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2916 G_TYPE_INT); /* RBGROUP */
2917 build_layers_tree (pdf_document, model, NULL, iter);
2918 poppler_layers_iter_free (iter);
2924 pdf_document_layers_show_layer (EvDocumentLayers *document,
2927 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2928 guint layer_id = ev_layer_get_id (layer);
2930 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2934 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2937 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2938 guint layer_id = ev_layer_get_id (layer);
2940 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2944 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2947 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2948 guint layer_id = ev_layer_get_id (layer);
2950 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2954 pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface)
2956 iface->has_layers = pdf_document_layers_has_layers;
2957 iface->get_layers = pdf_document_layers_get_layers;
2958 iface->show_layer = pdf_document_layers_show_layer;
2959 iface->hide_layer = pdf_document_layers_hide_layer;
2960 iface->layer_is_visible = pdf_document_layers_layer_is_visible;