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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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);
154 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
156 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
157 pdf_document_security_iface_init);
158 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
159 pdf_document_document_thumbnails_iface_init);
160 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
161 pdf_document_document_links_iface_init);
162 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
163 pdf_document_document_images_iface_init);
164 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
165 pdf_document_document_forms_iface_init);
166 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
167 pdf_document_document_fonts_iface_init);
168 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
169 pdf_document_document_layers_iface_init);
170 #ifdef HAVE_POPPLER_PAGE_RENDER
171 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
172 pdf_document_document_print_iface_init);
174 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
175 pdf_document_document_annotations_iface_init);
176 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
177 pdf_document_document_attachments_iface_init);
178 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
179 pdf_document_find_iface_init);
180 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
181 pdf_document_file_exporter_iface_init);
182 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
183 pdf_selection_iface_init);
184 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
185 pdf_document_page_transition_iface_init);
189 pdf_document_search_free (PdfDocumentSearch *search)
191 PdfDocument *pdf_document = search->document;
195 if (search->idle != 0)
196 g_source_remove (search->idle);
198 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
199 for (i = 0; i < n_pages; i++) {
200 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
201 g_list_free (search->pages[i]);
203 g_free (search->pages);
205 g_free (search->text);
210 pdf_document_dispose (GObject *object)
212 PdfDocument *pdf_document = PDF_DOCUMENT(object);
214 if (pdf_document->print_ctx) {
215 pdf_print_context_free (pdf_document->print_ctx);
216 pdf_document->print_ctx = NULL;
219 if (pdf_document->search) {
220 pdf_document_search_free (pdf_document->search);
221 pdf_document->search = NULL;
224 if (pdf_document->document) {
225 g_object_unref (pdf_document->document);
228 if (pdf_document->font_info) {
229 poppler_font_info_free (pdf_document->font_info);
232 if (pdf_document->fonts_iter) {
233 poppler_fonts_iter_free (pdf_document->fonts_iter);
236 if (pdf_document->layers) {
237 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
238 g_list_free (pdf_document->layers);
241 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
245 pdf_document_init (PdfDocument *pdf_document)
247 pdf_document->password = NULL;
251 convert_error (GError *poppler_error,
254 if (poppler_error == NULL)
257 if (poppler_error->domain == POPPLER_ERROR) {
258 /* convert poppler errors into EvDocument errors */
259 gint code = EV_DOCUMENT_ERROR_INVALID;
260 if (poppler_error->code == POPPLER_ERROR_INVALID)
261 code = EV_DOCUMENT_ERROR_INVALID;
262 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
263 code = EV_DOCUMENT_ERROR_ENCRYPTED;
265 g_set_error_literal (error,
268 poppler_error->message);
270 g_error_free (poppler_error);
272 g_propagate_error (error, poppler_error);
279 pdf_document_save (EvDocument *document,
283 PdfDocument *pdf_document = PDF_DOCUMENT (document);
285 GError *poppler_error = NULL;
287 if (pdf_document->modified) {
288 retval = poppler_document_save (pdf_document->document,
289 uri, &poppler_error);
291 retval = poppler_document_save_a_copy (pdf_document->document,
292 uri, &poppler_error);
296 convert_error (poppler_error, error);
302 pdf_document_load (EvDocument *document,
306 GError *poppler_error = NULL;
307 PdfDocument *pdf_document = PDF_DOCUMENT (document);
309 pdf_document->document =
310 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
312 if (pdf_document->document == NULL) {
313 convert_error (poppler_error, error);
321 pdf_document_get_n_pages (EvDocument *document)
323 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
327 pdf_document_get_page (EvDocument *document,
330 PdfDocument *pdf_document = PDF_DOCUMENT (document);
331 PopplerPage *poppler_page;
334 poppler_page = poppler_document_get_page (pdf_document->document, index);
335 page = ev_page_new (index);
336 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
337 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
338 g_object_unref (poppler_page);
344 pdf_document_get_page_size (EvDocument *document,
349 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
351 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
355 pdf_document_get_page_label (EvDocument *document,
360 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
362 g_object_get (G_OBJECT (page->backend_page),
368 static cairo_surface_t *
369 pdf_page_render (PopplerPage *page,
374 cairo_surface_t *surface;
376 #ifdef HAVE_POPPLER_PAGE_RENDER
379 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
381 cr = cairo_create (surface);
383 switch (rc->rotation) {
385 cairo_translate (cr, width, 0);
388 cairo_translate (cr, width, height);
391 cairo_translate (cr, 0, height);
394 cairo_translate (cr, 0, 0);
396 cairo_scale (cr, rc->scale, rc->scale);
397 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
398 poppler_page_render (page, cr);
400 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
401 cairo_set_source_rgb (cr, 1., 1., 1.);
405 #else /* HAVE_POPPLER_PAGE_RENDER */
408 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
412 poppler_page_render_to_pixbuf (page,
418 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
419 g_object_unref (pixbuf);
420 #endif /* HAVE_POPPLER_PAGE_RENDER */
425 static cairo_surface_t *
426 pdf_document_render (EvDocument *document,
429 PdfDocument *pdf_document;
430 PopplerPage *poppler_page;
431 double width_points, height_points;
434 poppler_page = POPPLER_PAGE (rc->page->backend_page);
436 poppler_page_get_size (poppler_page,
437 &width_points, &height_points);
439 if (rc->rotation == 90 || rc->rotation == 270) {
440 width = (int) ((height_points * rc->scale) + 0.5);
441 height = (int) ((width_points * rc->scale) + 0.5);
443 width = (int) ((width_points * rc->scale) + 0.5);
444 height = (int) ((height_points * rc->scale) + 0.5);
447 return pdf_page_render (poppler_page,
452 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 */
454 pdf_document_get_format_from_metadata (xmlDocPtr doc,
455 xmlXPathContextPtr xpathCtx)
457 xmlXPathObjectPtr xpathObj;
458 xmlChar *part = NULL;
459 xmlChar *conf = NULL;
463 /* add pdf/a namespaces */
464 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
465 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
466 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
468 /* reads pdf/a part */
469 /* first syntax: child node */
470 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
471 if (xpathObj != NULL) {
472 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
473 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
475 xmlXPathFreeObject (xpathObj);
478 /* second syntax: attribute */
479 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
480 if (xpathObj != NULL) {
481 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
482 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
484 xmlXPathFreeObject (xpathObj);
488 /* reads pdf/a conformance */
489 /* first syntax: child node */
490 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
491 if (xpathObj != NULL) {
492 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
493 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
495 xmlXPathFreeObject (xpathObj);
498 /* second syntax: attribute */
499 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
500 if (xpathObj != NULL) {
501 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
502 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
504 xmlXPathFreeObject (xpathObj);
508 if (part != NULL && conf != NULL) {
509 /* makes conf lowercase */
510 for (i = 0; conf[i]; i++)
511 conf[i] = g_ascii_tolower (conf[i]);
514 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
524 static EvDocumentLicense *
525 pdf_document_get_license_from_metadata (xmlDocPtr doc,
526 xmlXPathContextPtr xpathCtx)
528 xmlXPathObjectPtr xpathObj;
529 xmlChar *marked = NULL;
530 const char *language_string;
533 gchar *tag, *tag_aux;
535 EvDocumentLicense *license;
537 /* register namespaces */
538 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
539 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
540 xmlXPathRegisterNs (xpathCtx, BAD_CAST "dc", BAD_CAST "http://purl.org/dc/elements/1.1/");
541 /* XMP Rights Management Schema */
542 xmlXPathRegisterNs (xpathCtx, BAD_CAST "xmpRights", BAD_CAST "http://ns.adobe.com/xap/1.0/rights/");
543 /* Creative Commons Schema */
544 xmlXPathRegisterNs (xpathCtx, BAD_CAST "cc", BAD_CAST "http://creativecommons.org/ns#");
546 /* checking if the document has been marked as defined on the XMP Rights
547 * Management Schema */
548 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_MARKED, xpathCtx);
549 if (xpathObj != NULL) {
550 if (xpathObj->nodesetval != NULL &&
551 xpathObj->nodesetval->nodeNr != 0)
552 marked = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
553 xmlXPathFreeObject (xpathObj);
556 /* a) Not marked => No XMP Rights information */
562 license = ev_document_license_new ();
564 /* b) Marked False => Public Domain, no copyrighted material and no
566 if (g_strrstr ((char *) marked, "False") != NULL) {
567 license->text = g_strdup (_("This work is in the Public Domain"));
568 /* c) Marked True => Copyrighted material */
570 /* Checking usage terms as defined by the XMP Rights Management
571 * Schema. This field is recomended to be checked by Creative
573 /* 1) checking for a suitable localized string */
574 language_string = pango_language_to_string (gtk_get_default_language ());
575 tags = g_strsplit (language_string, "-", -1);
576 i = g_strv_length (tags);
577 while (i-- && !license->text) {
578 tag = g_strdup (tags[0]);
579 for (j = 1; j <= i; j++) {
580 tag_aux = g_strdup_printf ("%s-%s", tag, tags[j]);
584 aux = g_strdup_printf (LICENSE_TEXT, tag);
585 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
586 if (xpathObj != NULL) {
587 if (xpathObj->nodesetval != NULL &&
588 xpathObj->nodesetval->nodeNr != 0)
589 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
590 xmlXPathFreeObject (xpathObj);
597 /* 2) if not, use the default string */
598 if (!license->text) {
599 aux = g_strdup_printf (LICENSE_TEXT, "x-default");
600 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
601 if (xpathObj != NULL) {
602 if (xpathObj->nodesetval != NULL &&
603 xpathObj->nodesetval->nodeNr != 0)
604 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
605 xmlXPathFreeObject (xpathObj);
610 /* Checking the license URI as defined by the Creative Commons
611 * Schema. This field is recomended to be checked by Creative
613 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_URI, xpathCtx);
614 if (xpathObj != NULL) {
615 if (xpathObj->nodesetval != NULL &&
616 xpathObj->nodesetval->nodeNr != 0)
617 license->uri = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
618 xmlXPathFreeObject (xpathObj);
621 /* Checking the web statement as defined by the XMP Rights
622 * Management Schema. Checking it out is a sort of above-and-beyond
623 * the basic recommendations by Creative Commons. It can be
624 * considered as a "reinforcement" approach to add certainty. */
625 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_WEB_STATEMENT, xpathCtx);
626 if (xpathObj != NULL) {
627 if (xpathObj->nodesetval != NULL &&
628 xpathObj->nodesetval->nodeNr != 0)
629 license->web_statement = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
630 xmlXPathFreeObject (xpathObj);
635 if (!license->text && !license->uri && !license->web_statement) {
636 ev_document_license_free (license);
644 pdf_document_parse_metadata (const gchar *metadata,
645 EvDocumentInfo *info)
648 xmlXPathContextPtr xpathCtx;
651 doc = xmlParseMemory (metadata, strlen (metadata));
653 return; /* invalid xml metadata */
655 xpathCtx = xmlXPathNewContext (doc);
656 if (xpathCtx == NULL) {
658 return; /* invalid xpath context */
661 fmt = pdf_document_get_format_from_metadata (doc, xpathCtx);
663 g_free (info->format);
667 info->license = pdf_document_get_license_from_metadata (doc, xpathCtx);
669 xmlXPathFreeContext (xpathCtx);
674 static EvDocumentInfo *
675 pdf_document_get_info (EvDocument *document)
677 EvDocumentInfo *info;
678 PopplerPageLayout layout;
679 PopplerPageMode mode;
680 PopplerViewerPreferences view_prefs;
681 PopplerPermissions permissions;
685 info = g_new0 (EvDocumentInfo, 1);
687 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
688 EV_DOCUMENT_INFO_FORMAT |
689 EV_DOCUMENT_INFO_AUTHOR |
690 EV_DOCUMENT_INFO_SUBJECT |
691 EV_DOCUMENT_INFO_KEYWORDS |
692 EV_DOCUMENT_INFO_LAYOUT |
693 EV_DOCUMENT_INFO_START_MODE |
694 EV_DOCUMENT_INFO_PERMISSIONS |
695 EV_DOCUMENT_INFO_UI_HINTS |
696 EV_DOCUMENT_INFO_CREATOR |
697 EV_DOCUMENT_INFO_PRODUCER |
698 EV_DOCUMENT_INFO_CREATION_DATE |
699 EV_DOCUMENT_INFO_MOD_DATE |
700 EV_DOCUMENT_INFO_LINEARIZED |
701 EV_DOCUMENT_INFO_N_PAGES |
702 EV_DOCUMENT_INFO_SECURITY |
703 EV_DOCUMENT_INFO_PAPER_SIZE |
704 EV_DOCUMENT_INFO_LICENSE;
706 g_object_get (PDF_DOCUMENT (document)->document,
707 "title", &(info->title),
708 "format", &(info->format),
709 "author", &(info->author),
710 "subject", &(info->subject),
711 "keywords", &(info->keywords),
713 "page-layout", &layout,
714 "viewer-preferences", &view_prefs,
715 "permissions", &permissions,
716 "creator", &(info->creator),
717 "producer", &(info->producer),
718 "creation-date", &(info->creation_date),
719 "mod-date", &(info->modified_date),
720 "linearized", &(info->linearized),
721 "metadata", &metadata,
724 if (metadata != NULL) {
725 pdf_document_parse_metadata (metadata, info);
729 info->n_pages = ev_document_get_n_pages (document);
731 if (info->n_pages > 0) {
732 ev_document_get_page_size (document, 0,
733 &(info->paper_width),
734 &(info->paper_height));
736 info->paper_width = info->paper_width / 72.0f * 25.4f;
737 info->paper_height = info->paper_height / 72.0f * 25.4f;
741 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
742 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
744 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
745 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
747 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
748 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
750 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
751 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
752 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
753 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
755 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
756 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
763 case POPPLER_PAGE_MODE_NONE:
764 info->mode = EV_DOCUMENT_MODE_NONE;
766 case POPPLER_PAGE_MODE_USE_THUMBS:
767 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
769 case POPPLER_PAGE_MODE_USE_OC:
770 info->mode = EV_DOCUMENT_MODE_USE_OC;
772 case POPPLER_PAGE_MODE_FULL_SCREEN:
773 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
775 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
776 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
782 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
783 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
785 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
786 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
788 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
789 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
791 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
792 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
794 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
795 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
797 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
798 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
800 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
801 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
804 info->permissions = 0;
805 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
806 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
808 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
809 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
811 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
812 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
814 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
815 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
818 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
819 /* translators: this is the document security state */
820 info->security = g_strdup (_("Yes"));
822 /* translators: this is the document security state */
823 info->security = g_strdup (_("No"));
830 pdf_document_class_init (PdfDocumentClass *klass)
832 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
833 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
835 g_object_class->dispose = pdf_document_dispose;
837 ev_document_class->save = pdf_document_save;
838 ev_document_class->load = pdf_document_load;
839 ev_document_class->get_n_pages = pdf_document_get_n_pages;
840 ev_document_class->get_page = pdf_document_get_page;
841 ev_document_class->get_page_size = pdf_document_get_page_size;
842 ev_document_class->get_page_label = pdf_document_get_page_label;
843 ev_document_class->render = pdf_document_render;
844 ev_document_class->get_info = pdf_document_get_info;
847 /* EvDocumentSecurity */
849 pdf_document_has_document_security (EvDocumentSecurity *document_security)
851 /* FIXME: do we really need to have this? */
856 pdf_document_set_password (EvDocumentSecurity *document_security,
857 const char *password)
859 PdfDocument *document = PDF_DOCUMENT (document_security);
861 if (document->password)
862 g_free (document->password);
864 document->password = g_strdup (password);
868 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
870 iface->has_document_security = pdf_document_has_document_security;
871 iface->set_password = pdf_document_set_password;
875 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
877 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
880 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
882 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
886 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
889 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
892 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
894 if (pdf_document->font_info == NULL) {
895 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
898 if (pdf_document->fonts_iter) {
899 poppler_fonts_iter_free (pdf_document->fonts_iter);
902 pdf_document->fonts_scanned_pages += n_pages;
904 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
905 &pdf_document->fonts_iter);
907 pdf_document->fonts_scanned_pages = 0;
908 poppler_font_info_free (pdf_document->font_info);
909 pdf_document->font_info = NULL;
916 font_type_to_string (PopplerFontType type)
919 case POPPLER_FONT_TYPE_TYPE1:
921 case POPPLER_FONT_TYPE_TYPE1C:
923 case POPPLER_FONT_TYPE_TYPE3:
925 case POPPLER_FONT_TYPE_TRUETYPE:
926 return _("TrueType");
927 case POPPLER_FONT_TYPE_CID_TYPE0:
928 return _("Type 1 (CID)");
929 case POPPLER_FONT_TYPE_CID_TYPE0C:
930 return _("Type 1C (CID)");
931 case POPPLER_FONT_TYPE_CID_TYPE2:
932 return _("TrueType (CID)");
934 return _("Unknown font type");
939 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
942 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
943 PopplerFontsIter *iter = pdf_document->fonts_iter;
945 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
951 GtkTreeIter list_iter;
954 const char *embedded;
957 name = poppler_fonts_iter_get_name (iter);
963 type = font_type_to_string (
964 poppler_fonts_iter_get_font_type (iter));
966 if (poppler_fonts_iter_is_embedded (iter)) {
967 if (poppler_fonts_iter_is_subset (iter))
968 embedded = _("Embedded subset");
970 embedded = _("Embedded");
972 embedded = _("Not embedded");
975 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
977 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
978 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
979 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
980 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
984 } while (poppler_fonts_iter_next (iter));
988 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
990 iface->fill_model = pdf_document_fonts_fill_model;
991 iface->scan = pdf_document_fonts_scan;
992 iface->get_progress = pdf_document_fonts_get_progress;
996 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
998 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
999 PopplerIndexIter *iter;
1001 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
1003 iter = poppler_index_iter_new (pdf_document->document);
1006 poppler_index_iter_free (iter);
1012 ev_link_dest_from_dest (PdfDocument *pdf_document,
1015 EvLinkDest *ev_dest = NULL;
1016 const char *unimplemented_dest = NULL;
1018 g_assert (dest != NULL);
1020 switch (dest->type) {
1021 case POPPLER_DEST_XYZ: {
1022 PopplerPage *poppler_page;
1025 poppler_page = poppler_document_get_page (pdf_document->document,
1026 MAX (0, dest->page_num - 1));
1027 poppler_page_get_size (poppler_page, NULL, &height);
1028 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1030 height - MIN (height, dest->top),
1035 g_object_unref (poppler_page);
1038 case POPPLER_DEST_FITB:
1039 case POPPLER_DEST_FIT:
1040 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1042 case POPPLER_DEST_FITBH:
1043 case POPPLER_DEST_FITH: {
1044 PopplerPage *poppler_page;
1047 poppler_page = poppler_document_get_page (pdf_document->document,
1048 MAX (0, dest->page_num - 1));
1049 poppler_page_get_size (poppler_page, NULL, &height);
1050 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1051 height - MIN (height, dest->top),
1053 g_object_unref (poppler_page);
1056 case POPPLER_DEST_FITBV:
1057 case POPPLER_DEST_FITV:
1058 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1062 case POPPLER_DEST_FITR: {
1063 PopplerPage *poppler_page;
1066 poppler_page = poppler_document_get_page (pdf_document->document,
1067 MAX (0, dest->page_num - 1));
1068 poppler_page_get_size (poppler_page, NULL, &height);
1069 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1071 height - MIN (height, dest->bottom),
1073 height - MIN (height, dest->top));
1074 g_object_unref (poppler_page);
1077 case POPPLER_DEST_NAMED:
1078 ev_dest = ev_link_dest_new_named (dest->named_dest);
1080 case POPPLER_DEST_UNKNOWN:
1081 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1085 if (unimplemented_dest) {
1086 g_warning ("Unimplemented destination: %s, please post a "
1087 "bug report in Evince bugzilla "
1088 "(http://bugzilla.gnome.org) with a testcase.",
1089 unimplemented_dest);
1093 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1099 ev_link_from_action (PdfDocument *pdf_document,
1100 PopplerAction *action)
1102 EvLink *link = NULL;
1103 EvLinkAction *ev_action = NULL;
1104 const char *unimplemented_action = NULL;
1106 switch (action->type) {
1107 case POPPLER_ACTION_NONE:
1109 case POPPLER_ACTION_GOTO_DEST: {
1112 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1113 ev_action = ev_link_action_new_dest (dest);
1116 case POPPLER_ACTION_GOTO_REMOTE: {
1119 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1120 ev_action = ev_link_action_new_remote (dest,
1121 action->goto_remote.file_name);
1125 case POPPLER_ACTION_LAUNCH:
1126 ev_action = ev_link_action_new_launch (action->launch.file_name,
1127 action->launch.params);
1129 case POPPLER_ACTION_URI:
1130 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1132 case POPPLER_ACTION_NAMED:
1133 ev_action = ev_link_action_new_named (action->named.named_dest);
1135 case POPPLER_ACTION_MOVIE:
1136 unimplemented_action = "POPPLER_ACTION_MOVIE";
1138 case POPPLER_ACTION_UNKNOWN:
1139 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1142 if (unimplemented_action) {
1143 g_warning ("Unimplemented action: %s, please post a bug report "
1144 "in Evince bugzilla (http://bugzilla.gnome.org) "
1145 "with a testcase.", unimplemented_action);
1148 link = ev_link_new (action->any.title, ev_action);
1154 build_tree (PdfDocument *pdf_document,
1155 GtkTreeModel *model,
1156 GtkTreeIter *parent,
1157 PopplerIndexIter *iter)
1161 GtkTreeIter tree_iter;
1162 PopplerIndexIter *child;
1163 PopplerAction *action;
1164 EvLink *link = NULL;
1168 action = poppler_index_iter_get_action (iter);
1169 expand = poppler_index_iter_is_open (iter);
1174 switch (action->type) {
1175 case POPPLER_ACTION_GOTO_DEST: {
1176 /* For bookmarks, solve named destinations */
1177 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1179 EvLinkDest *ev_dest = NULL;
1180 EvLinkAction *ev_action;
1182 dest = poppler_document_find_dest (pdf_document->document,
1183 action->goto_dest.dest->named_dest);
1185 link = ev_link_from_action (pdf_document, action);
1189 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1190 poppler_dest_free (dest);
1192 ev_action = ev_link_action_new_dest (ev_dest);
1193 link = ev_link_new (action->any.title, ev_action);
1195 link = ev_link_from_action (pdf_document, action);
1200 link = ev_link_from_action (pdf_document, action);
1204 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1205 poppler_action_free (action);
1207 g_object_unref (link);
1212 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1213 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1215 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1216 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1217 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1218 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1221 g_free (title_markup);
1222 g_object_unref (link);
1224 child = poppler_index_iter_get_child (iter);
1226 build_tree (pdf_document, model, &tree_iter, child);
1227 poppler_index_iter_free (child);
1228 poppler_action_free (action);
1230 } while (poppler_index_iter_next (iter));
1233 static GtkTreeModel *
1234 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1236 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1237 GtkTreeModel *model = NULL;
1238 PopplerIndexIter *iter;
1240 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1242 iter = poppler_index_iter_new (pdf_document->document);
1243 /* Create the model if we have items*/
1245 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1250 build_tree (pdf_document, model, NULL, iter);
1251 poppler_index_iter_free (iter);
1258 pdf_document_links_get_links (EvDocumentLinks *document_links,
1261 PdfDocument *pdf_document;
1262 PopplerPage *poppler_page;
1263 GList *retval = NULL;
1264 GList *mapping_list;
1268 pdf_document = PDF_DOCUMENT (document_links);
1269 poppler_page = poppler_document_get_page (pdf_document->document,
1271 mapping_list = poppler_page_get_link_mapping (poppler_page);
1272 poppler_page_get_size (poppler_page, NULL, &height);
1274 for (list = mapping_list; list; list = list->next) {
1275 PopplerLinkMapping *link_mapping;
1276 EvMapping *ev_link_mapping;
1278 link_mapping = (PopplerLinkMapping *)list->data;
1279 ev_link_mapping = g_new (EvMapping, 1);
1280 ev_link_mapping->data = ev_link_from_action (pdf_document,
1281 link_mapping->action);
1282 ev_link_mapping->area.x1 = link_mapping->area.x1;
1283 ev_link_mapping->area.x2 = link_mapping->area.x2;
1284 /* Invert this for X-style coordinates */
1285 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1286 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1288 retval = g_list_prepend (retval, ev_link_mapping);
1291 poppler_page_free_link_mapping (mapping_list);
1292 g_object_unref (poppler_page);
1294 return g_list_reverse (retval);
1298 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1299 const gchar *link_name)
1301 PdfDocument *pdf_document;
1303 EvLinkDest *ev_dest = NULL;
1305 pdf_document = PDF_DOCUMENT (document_links);
1306 dest = poppler_document_find_dest (pdf_document->document,
1309 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1310 poppler_dest_free (dest);
1317 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1319 iface->has_document_links = pdf_document_links_has_document_links;
1320 iface->get_links_model = pdf_document_links_get_links_model;
1321 iface->get_links = pdf_document_links_get_links;
1322 iface->find_link_dest = pdf_document_links_find_link_dest;
1326 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1329 GList *retval = NULL;
1330 PdfDocument *pdf_document;
1331 PopplerPage *poppler_page;
1332 GList *mapping_list;
1335 pdf_document = PDF_DOCUMENT (document_images);
1336 poppler_page = poppler_document_get_page (pdf_document->document, page);
1337 mapping_list = poppler_page_get_image_mapping (poppler_page);
1339 for (list = mapping_list; list; list = list->next) {
1340 PopplerImageMapping *image_mapping;
1341 EvMapping *ev_image_mapping;
1343 image_mapping = (PopplerImageMapping *)list->data;
1345 ev_image_mapping = g_new (EvMapping, 1);
1347 ev_image_mapping->data = ev_image_new (page, image_mapping->image_id);
1348 ev_image_mapping->area.x1 = image_mapping->area.x1;
1349 ev_image_mapping->area.y1 = image_mapping->area.y1;
1350 ev_image_mapping->area.x2 = image_mapping->area.x2;
1351 ev_image_mapping->area.y2 = image_mapping->area.y2;
1353 retval = g_list_prepend (retval, ev_image_mapping);
1356 poppler_page_free_image_mapping (mapping_list);
1357 g_object_unref (poppler_page);
1359 return g_list_reverse (retval);
1363 pdf_document_images_get_image (EvDocumentImages *document_images,
1366 GdkPixbuf *retval = NULL;
1367 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1368 PdfDocument *pdf_document;
1369 PopplerPage *poppler_page;
1370 cairo_surface_t *surface;
1372 pdf_document = PDF_DOCUMENT (document_images);
1373 poppler_page = poppler_document_get_page (pdf_document->document,
1374 ev_image_get_page (image));
1376 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1378 retval = ev_document_misc_pixbuf_from_surface (surface);
1379 cairo_surface_destroy (surface);
1382 g_object_unref (poppler_page);
1388 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1390 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1391 iface->get_image = pdf_document_images_get_image;
1395 make_thumbnail_for_page (PopplerPage *poppler_page,
1396 EvRenderContext *rc,
1402 #ifdef POPPLER_WITH_GDK
1403 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1405 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1407 ev_document_fc_mutex_lock ();
1408 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1410 rc->scale, rc->rotation, pixbuf);
1411 ev_document_fc_mutex_unlock ();
1413 cairo_surface_t *surface;
1415 ev_document_fc_mutex_lock ();
1416 surface = pdf_page_render (poppler_page, width, height, rc);
1417 ev_document_fc_mutex_unlock ();
1419 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1420 cairo_surface_destroy (surface);
1421 #endif /* POPPLER_WITH_GDK */
1427 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1428 EvRenderContext *rc,
1431 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1432 PopplerPage *poppler_page;
1433 GdkPixbuf *pixbuf = NULL;
1434 GdkPixbuf *border_pixbuf;
1437 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1439 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1440 rc, &width, &height);
1442 #ifdef POPPLER_WITH_GDK
1443 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1445 cairo_surface_t *surface;
1447 surface = poppler_page_get_thumbnail (poppler_page);
1449 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1450 cairo_surface_destroy (surface);
1452 #endif /* POPPLER_WITH_GDK */
1454 if (pixbuf != NULL) {
1455 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1456 gdk_pixbuf_get_height (pixbuf) :
1457 gdk_pixbuf_get_width (pixbuf);
1459 if (thumb_width == width) {
1460 GdkPixbuf *rotated_pixbuf;
1462 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1463 (GdkPixbufRotation) (360 - rc->rotation));
1464 g_object_unref (pixbuf);
1465 pixbuf = rotated_pixbuf;
1467 /* The provided thumbnail has a different size */
1468 g_object_unref (pixbuf);
1469 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1472 /* There is no provided thumbnail. We need to make one. */
1473 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1476 if (border && pixbuf) {
1477 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1478 g_object_unref (pixbuf);
1479 pixbuf = border_pixbuf;
1486 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1487 EvRenderContext *rc,
1491 double page_width, page_height;
1493 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1494 &page_width, &page_height);
1496 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1497 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1499 if (rc->rotation == 90 || rc->rotation == 270) {
1509 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1511 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1512 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1517 pdf_document_find_find_text (EvDocumentFind *document_find,
1520 gboolean case_sensitive)
1523 PopplerPage *poppler_page;
1526 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1527 g_return_val_if_fail (text != NULL, NULL);
1529 poppler_page = POPPLER_PAGE (page->backend_page);
1531 matches = poppler_page_find_text (poppler_page, text);
1535 poppler_page_get_size (poppler_page, NULL, &height);
1536 for (l = matches; l && l->data; l = g_list_next (l)) {
1537 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1541 rect->y1 = height - rect->y2;
1542 rect->y2 = height - tmp;
1549 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1551 iface->find_text = pdf_document_find_find_text;
1555 pdf_print_context_free (PdfPrintContext *ctx)
1560 #ifdef HAVE_CAIRO_PRINT
1562 cairo_destroy (ctx->cr);
1567 poppler_ps_file_free (ctx->ps_file);
1568 ctx->ps_file = NULL;
1575 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1576 EvFileExporterContext *fc)
1578 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1579 PdfPrintContext *ctx;
1580 #ifdef HAVE_CAIRO_PRINT
1581 gdouble width, height;
1582 cairo_surface_t *surface = NULL;
1585 if (pdf_document->print_ctx)
1586 pdf_print_context_free (pdf_document->print_ctx);
1587 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1588 ctx = pdf_document->print_ctx;
1589 ctx->format = fc->format;
1591 #ifdef HAVE_CAIRO_PRINT
1592 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1594 ctx->paper_width = fc->paper_width;
1595 ctx->paper_height = fc->paper_height;
1597 switch (fc->pages_per_sheet) {
1625 ctx->pages_printed = 0;
1627 switch (fc->format) {
1628 case EV_FILE_FORMAT_PS:
1629 #ifdef HAVE_CAIRO_PS
1630 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1633 case EV_FILE_FORMAT_PDF:
1634 #ifdef HAVE_CAIRO_PDF
1635 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1639 g_assert_not_reached ();
1642 ctx->cr = cairo_create (surface);
1643 cairo_surface_destroy (surface);
1645 #else /* HAVE_CAIRO_PRINT */
1646 if (ctx->format == EV_FILE_FORMAT_PS) {
1647 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1648 fc->filename, fc->first_page,
1649 fc->last_page - fc->first_page + 1);
1650 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1651 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1653 #endif /* HAVE_CAIRO_PRINT */
1657 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1659 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1660 PdfPrintContext *ctx = pdf_document->print_ctx;
1662 g_return_if_fail (pdf_document->print_ctx != NULL);
1664 ctx->pages_printed = 0;
1666 #ifdef HAVE_CAIRO_PRINT
1667 if (ctx->paper_width > ctx->paper_height) {
1668 if (ctx->format == EV_FILE_FORMAT_PS) {
1669 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1672 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1673 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1678 #endif /* HAVE_CAIRO_PRINT */
1682 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1683 EvRenderContext *rc)
1685 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1686 PdfPrintContext *ctx = pdf_document->print_ctx;
1687 PopplerPage *poppler_page;
1688 #ifdef HAVE_CAIRO_PRINT
1689 gdouble page_width, page_height;
1692 gdouble width, height;
1693 gdouble pwidth, pheight;
1694 gdouble xscale, yscale;
1697 g_return_if_fail (pdf_document->print_ctx != NULL);
1699 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1701 #ifdef HAVE_CAIRO_PRINT
1702 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1703 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1704 poppler_page_get_size (poppler_page, &page_width, &page_height);
1706 if (page_width > page_height && page_width > ctx->paper_width) {
1712 /* Use always portrait mode and rotate when necessary */
1713 if (ctx->paper_width > ctx->paper_height) {
1714 width = ctx->paper_height;
1715 height = ctx->paper_width;
1718 width = ctx->paper_width;
1719 height = ctx->paper_height;
1722 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1735 page_width = page_height;
1739 pwidth = width / ctx->pages_x;
1740 pheight = height / ctx->pages_y;
1742 if ((page_width > pwidth || page_height > pheight) ||
1743 (page_width < pwidth && page_height < pheight)) {
1744 xscale = pwidth / page_width;
1745 yscale = pheight / page_height;
1747 if (yscale < xscale) {
1754 xscale = yscale = 1;
1759 cairo_save (ctx->cr);
1761 cairo_matrix_t matrix;
1763 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1764 cairo_matrix_init (&matrix,
1768 cairo_transform (ctx->cr, &matrix);
1771 cairo_translate (ctx->cr,
1772 x * (rotate ? pheight : pwidth),
1773 y * (rotate ? pwidth : pheight));
1774 cairo_scale (ctx->cr, xscale, yscale);
1776 poppler_page_render_for_printing (poppler_page, ctx->cr);
1778 ctx->pages_printed++;
1780 cairo_restore (ctx->cr);
1781 #else /* HAVE_CAIRO_PRINT */
1782 if (ctx->format == EV_FILE_FORMAT_PS)
1783 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1784 #endif /* HAVE_CAIRO_PRINT */
1788 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1790 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1791 PdfPrintContext *ctx = pdf_document->print_ctx;
1793 g_return_if_fail (pdf_document->print_ctx != NULL);
1795 #ifdef HAVE_CAIRO_PRINT
1796 cairo_show_page (ctx->cr);
1801 pdf_document_file_exporter_end (EvFileExporter *exporter)
1803 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1805 pdf_print_context_free (pdf_document->print_ctx);
1806 pdf_document->print_ctx = NULL;
1809 static EvFileExporterCapabilities
1810 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1812 return (EvFileExporterCapabilities) (
1813 EV_FILE_EXPORTER_CAN_PAGE_SET |
1814 EV_FILE_EXPORTER_CAN_COPIES |
1815 EV_FILE_EXPORTER_CAN_COLLATE |
1816 EV_FILE_EXPORTER_CAN_REVERSE |
1817 EV_FILE_EXPORTER_CAN_SCALE |
1818 #ifdef HAVE_CAIRO_PRINT
1819 #ifdef HAVE_POPPLER_PAGE_RENDER
1820 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1824 #ifdef HAVE_CAIRO_PDF
1825 #ifdef HAVE_POPPLER_PAGE_RENDER
1826 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1829 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1833 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1835 iface->begin = pdf_document_file_exporter_begin;
1836 iface->begin_page = pdf_document_file_exporter_begin_page;
1837 iface->do_page = pdf_document_file_exporter_do_page;
1838 iface->end_page = pdf_document_file_exporter_end_page;
1839 iface->end = pdf_document_file_exporter_end;
1840 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1843 #ifdef HAVE_POPPLER_PAGE_RENDER
1844 /* EvDocumentPrint */
1846 pdf_document_print_print_page (EvDocumentPrint *document,
1850 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1852 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1856 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1858 iface->print_page = pdf_document_print_print_page;
1860 #endif /* HAVE_POPPLER_PAGE_RENDER */
1863 pdf_selection_render_selection (EvSelection *selection,
1864 EvRenderContext *rc,
1865 cairo_surface_t **surface,
1866 EvRectangle *points,
1867 EvRectangle *old_points,
1868 EvSelectionStyle style,
1872 PopplerPage *poppler_page;
1873 double width_points, height_points;
1876 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1878 poppler_page_get_size (poppler_page,
1879 &width_points, &height_points);
1880 width = (int) ((width_points * rc->scale) + 0.5);
1881 height = (int) ((height_points * rc->scale) + 0.5);
1883 #ifdef HAVE_POPPLER_PAGE_RENDER
1885 PopplerColor text_color, base_color;
1887 text_color.red = text->red;
1888 text_color.green = text->green;
1889 text_color.blue = text->blue;
1891 base_color.red = base->red;
1892 base_color.green = base->green;
1893 base_color.blue = base->blue;
1895 if (*surface == NULL) {
1896 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1901 cr = cairo_create (*surface);
1902 cairo_scale (cr, rc->scale, rc->scale);
1903 cairo_surface_set_device_offset (*surface, 0, 0);
1904 memset (cairo_image_surface_get_data (*surface), 0x00,
1905 cairo_image_surface_get_height (*surface) *
1906 cairo_image_surface_get_stride (*surface));
1907 poppler_page_render_selection (poppler_page,
1909 (PopplerRectangle *)points,
1910 (PopplerRectangle *)old_points,
1911 (PopplerSelectionStyle)style,
1915 #else /* HAVE_POPPLER_PAGE_RENDER */
1918 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1922 poppler_page_render_selection_to_pixbuf (poppler_page,
1923 rc->scale, rc->rotation, pixbuf,
1924 (PopplerRectangle *)points,
1925 (PopplerRectangle *)old_points,
1926 (PopplerSelectionStyle)style,
1930 cairo_surface_destroy (*surface);
1931 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1932 g_object_unref (pixbuf);
1933 #endif /* HAVE_POPPLER_PAGE_RENDER */
1937 pdf_selection_get_selected_text (EvSelection *selection,
1938 EvRenderContext *rc,
1939 EvSelectionStyle style,
1940 EvRectangle *points)
1942 PopplerPage *poppler_page;
1947 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1949 poppler_page_get_size (poppler_page, NULL, &height);
1951 r.y1 = height - points->y2;
1953 r.y2 = height - points->y1;
1955 retval = poppler_page_get_text (poppler_page,
1956 (PopplerSelectionStyle)style,
1963 create_gdk_region_from_poppler_region (GList *region)
1968 retval = gdk_region_new ();
1970 for (l = region; l; l = g_list_next (l)) {
1971 PopplerRectangle *rectangle;
1974 rectangle = (PopplerRectangle *)l->data;
1976 rect.x = (gint) rectangle->x1;
1977 rect.y = (gint) rectangle->y1;
1978 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1979 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1980 gdk_region_union_with_rect (retval, &rect);
1982 poppler_rectangle_free (rectangle);
1989 pdf_selection_get_selection_region (EvSelection *selection,
1990 EvRenderContext *rc,
1991 EvSelectionStyle style,
1992 EvRectangle *points)
1994 PopplerPage *poppler_page;
1998 poppler_page = POPPLER_PAGE (rc->page->backend_page);
2000 region = poppler_page_get_selection_region (poppler_page,
2002 (PopplerSelectionStyle)style,
2003 (PopplerRectangle *) points);
2004 retval = create_gdk_region_from_poppler_region (region);
2005 g_list_free (region);
2011 pdf_selection_get_selection_map (EvSelection *selection,
2012 EvRenderContext *rc)
2014 PopplerPage *poppler_page;
2015 PopplerRectangle points;
2019 poppler_page = POPPLER_PAGE (rc->page->backend_page);
2023 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2025 region = poppler_page_get_selection_region (poppler_page, 1.0,
2026 POPPLER_SELECTION_GLYPH,
2028 retval = create_gdk_region_from_poppler_region (region);
2029 g_list_free (region);
2035 pdf_selection_iface_init (EvSelectionIface *iface)
2037 iface->render_selection = pdf_selection_render_selection;
2038 iface->get_selected_text = pdf_selection_get_selected_text;
2039 iface->get_selection_region = pdf_selection_get_selection_region;
2040 iface->get_selection_map = pdf_selection_get_selection_map;
2043 /* Page Transitions */
2045 pdf_document_get_page_duration (EvDocumentTransition *trans,
2048 PdfDocument *pdf_document;
2049 PopplerPage *poppler_page;
2050 gdouble duration = -1;
2052 pdf_document = PDF_DOCUMENT (trans);
2053 poppler_page = poppler_document_get_page (pdf_document->document, page);
2057 duration = poppler_page_get_duration (poppler_page);
2058 g_object_unref (poppler_page);
2063 static EvTransitionEffect *
2064 pdf_document_get_effect (EvDocumentTransition *trans,
2067 PdfDocument *pdf_document;
2068 PopplerPage *poppler_page;
2069 PopplerPageTransition *page_transition;
2070 EvTransitionEffect *effect;
2072 pdf_document = PDF_DOCUMENT (trans);
2073 poppler_page = poppler_document_get_page (pdf_document->document, page);
2078 page_transition = poppler_page_get_transition (poppler_page);
2080 if (!page_transition) {
2081 g_object_unref (poppler_page);
2085 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2086 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2087 "alignment", page_transition->alignment,
2088 "direction", page_transition->direction,
2089 "duration", page_transition->duration,
2090 "angle", page_transition->angle,
2091 "scale", page_transition->scale,
2092 "rectangular", page_transition->rectangular,
2095 poppler_page_transition_free (page_transition);
2096 g_object_unref (poppler_page);
2102 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2104 iface->get_page_duration = pdf_document_get_page_duration;
2105 iface->get_effect = pdf_document_get_effect;
2110 pdf_document_get_crop_box (EvDocument *document,
2114 PdfDocument *pdf_document;
2115 PopplerPage *poppler_page;
2116 PopplerRectangle poppler_rect;
2118 pdf_document = PDF_DOCUMENT (document);
2119 poppler_page = poppler_document_get_page (pdf_document->document, page);
2120 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2121 rect->x1 = poppler_rect.x1;
2122 rect->x2 = poppler_rect.x2;
2123 rect->y1 = poppler_rect.y1;
2124 rect->y2 = poppler_rect.y2;
2127 static EvFormField *
2128 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2130 EvFormField *ev_field = NULL;
2133 gboolean is_read_only;
2135 id = poppler_form_field_get_id (poppler_field);
2136 font_size = poppler_form_field_get_font_size (poppler_field);
2137 is_read_only = poppler_form_field_is_read_only (poppler_field);
2139 switch (poppler_form_field_get_field_type (poppler_field)) {
2140 case POPPLER_FORM_FIELD_TEXT: {
2141 EvFormFieldText *field_text;
2142 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2144 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2145 case POPPLER_FORM_TEXT_NORMAL:
2146 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2148 case POPPLER_FORM_TEXT_MULTILINE:
2149 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2151 case POPPLER_FORM_TEXT_FILE_SELECT:
2152 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2156 ev_field = ev_form_field_text_new (id, ev_text_type);
2157 field_text = EV_FORM_FIELD_TEXT (ev_field);
2159 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2160 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2161 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2162 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2163 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2164 field_text->text = poppler_form_field_text_get_text (poppler_field);
2168 case POPPLER_FORM_FIELD_BUTTON: {
2169 EvFormFieldButton *field_button;
2170 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2172 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2173 case POPPLER_FORM_BUTTON_PUSH:
2174 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2176 case POPPLER_FORM_BUTTON_CHECK:
2177 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2179 case POPPLER_FORM_BUTTON_RADIO:
2180 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2184 ev_field = ev_form_field_button_new (id, ev_button_type);
2185 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2187 field_button->state = poppler_form_field_button_get_state (poppler_field);
2190 case POPPLER_FORM_FIELD_CHOICE: {
2191 EvFormFieldChoice *field_choice;
2192 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2194 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2195 case POPPLER_FORM_CHOICE_COMBO:
2196 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2198 case EV_FORM_FIELD_CHOICE_LIST:
2199 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2203 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2204 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2206 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2207 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2208 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2209 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2211 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2212 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2213 if (field_choice->is_editable)
2214 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2217 case POPPLER_FORM_FIELD_SIGNATURE:
2219 ev_field = ev_form_field_signature_new (id);
2221 case POPPLER_FORM_FIELD_UNKNOWN:
2225 ev_field->font_size = font_size;
2226 ev_field->is_read_only = is_read_only;
2232 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2235 PopplerPage *poppler_page;
2236 GList *retval = NULL;
2241 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2243 poppler_page = POPPLER_PAGE (page->backend_page);
2244 fields = poppler_page_get_form_field_mapping (poppler_page);
2245 poppler_page_get_size (poppler_page, NULL, &height);
2247 for (list = fields; list; list = list->next) {
2248 PopplerFormFieldMapping *mapping;
2249 EvMapping *field_mapping;
2250 EvFormField *ev_field;
2252 mapping = (PopplerFormFieldMapping *)list->data;
2254 ev_field = ev_form_field_from_poppler_field (mapping->field);
2258 field_mapping = g_new0 (EvMapping, 1);
2259 field_mapping->area.x1 = mapping->area.x1;
2260 field_mapping->area.x2 = mapping->area.x2;
2261 field_mapping->area.y1 = height - mapping->area.y2;
2262 field_mapping->area.y2 = height - mapping->area.y1;
2263 field_mapping->data = ev_field;
2264 ev_field->page = EV_PAGE (g_object_ref (page));
2266 g_object_set_data_full (G_OBJECT (ev_field),
2268 g_object_ref (mapping->field),
2269 (GDestroyNotify) g_object_unref);
2271 retval = g_list_prepend (retval, field_mapping);
2274 poppler_page_free_form_field_mapping (fields);
2276 return g_list_reverse (retval);
2280 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2284 PopplerFormField *poppler_field;
2287 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2291 text = poppler_form_field_text_get_text (poppler_field);
2297 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2301 PopplerFormField *poppler_field;
2303 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2307 poppler_form_field_text_set_text (poppler_field, text);
2308 PDF_DOCUMENT (document)->modified = TRUE;
2312 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2316 PopplerFormField *poppler_field;
2318 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2322 poppler_form_field_button_set_state (poppler_field, state);
2323 PDF_DOCUMENT (document)->modified = TRUE;
2327 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2330 PopplerFormField *poppler_field;
2333 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2337 state = poppler_form_field_button_get_state (poppler_field);
2343 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2347 PopplerFormField *poppler_field;
2350 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2354 text = poppler_form_field_choice_get_item (poppler_field, index);
2360 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2363 PopplerFormField *poppler_field;
2366 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2370 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2376 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2380 PopplerFormField *poppler_field;
2383 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2387 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2393 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2397 PopplerFormField *poppler_field;
2399 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2403 poppler_form_field_choice_select_item (poppler_field, index);
2404 PDF_DOCUMENT (document)->modified = TRUE;
2408 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2412 PopplerFormField *poppler_field;
2414 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2418 poppler_form_field_choice_toggle_item (poppler_field, index);
2419 PDF_DOCUMENT (document)->modified = TRUE;
2423 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2426 PopplerFormField *poppler_field;
2428 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2432 poppler_form_field_choice_unselect_all (poppler_field);
2433 PDF_DOCUMENT (document)->modified = TRUE;
2437 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2441 PopplerFormField *poppler_field;
2443 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2447 poppler_form_field_choice_set_text (poppler_field, text);
2448 PDF_DOCUMENT (document)->modified = TRUE;
2452 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2455 PopplerFormField *poppler_field;
2458 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2462 text = poppler_form_field_choice_get_text (poppler_field);
2468 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2470 iface->get_form_fields = pdf_document_forms_get_form_fields;
2471 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2472 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2473 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2474 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2475 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2476 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2477 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2478 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2479 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2480 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2481 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2482 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2487 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2490 PopplerColor *poppler_color;
2492 poppler_color = poppler_annot_get_color (poppler_annot);
2493 if (poppler_color) {
2494 color->red = poppler_color->red;
2495 color->green = poppler_color->green;
2496 color->blue = poppler_color->blue;
2498 g_free (poppler_color);
2499 } /* TODO: else use a default color */
2502 static EvAnnotation *
2503 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2506 EvAnnotation *ev_annot = NULL;
2507 const gchar *unimplemented_annot = NULL;
2509 switch (poppler_annot_get_annot_type (poppler_annot)) {
2510 case POPPLER_ANNOT_TEXT:
2511 PopplerAnnotText *poppler_text;
2512 EvAnnotationText *ev_annot_text;
2514 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2516 ev_annot = ev_annotation_text_new (page);
2518 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2519 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2522 case POPPLER_ANNOT_LINK:
2523 case POPPLER_ANNOT_WIDGET:
2524 /* Ignore link and widgets annots since they are already handled */
2527 GEnumValue *enum_value;
2529 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2530 poppler_annot_get_annot_type (poppler_annot));
2531 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2535 if (unimplemented_annot) {
2536 g_warning ("Unimplemented annotation: %s, please post a "
2537 "bug report in Evince bugzilla "
2538 "(http://bugzilla.gnome.org) with a testcase.",
2539 unimplemented_annot);
2543 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2544 ev_annot->name = poppler_annot_get_name (poppler_annot);
2545 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2546 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2548 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2549 PopplerAnnotMarkup *markup;
2552 PopplerRectangle poppler_rect;
2554 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2556 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2557 EvRectangle ev_rect;
2561 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2563 ev_rect.x1 = poppler_rect.x1;
2564 ev_rect.x2 = poppler_rect.x2;
2565 ev_rect.y1 = height - poppler_rect.y2;
2566 ev_rect.y2 = height - poppler_rect.y1;
2568 is_open = poppler_annot_markup_get_popup_is_open (markup);
2570 g_object_set (ev_annot,
2571 "rectangle", &ev_rect,
2576 /* FIXME: Use poppler_annot_markup_has_popup() when
2577 * new poppler is released.
2579 g_object_set (ev_annot,
2584 label = poppler_annot_markup_get_label (markup);
2585 opacity = poppler_annot_markup_get_opacity (markup);
2587 g_object_set (ev_annot,
2600 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2603 GList *retval = NULL;
2604 PdfDocument *pdf_document;
2605 PopplerPage *poppler_page;
2611 pdf_document = PDF_DOCUMENT (document_annotations);
2612 poppler_page = POPPLER_PAGE (page->backend_page);
2613 annots = poppler_page_get_annot_mapping (poppler_page);
2614 poppler_page_get_size (poppler_page, NULL, &height);
2616 for (list = annots; list; list = list->next) {
2617 PopplerAnnotMapping *mapping;
2618 EvMapping *annot_mapping;
2619 EvAnnotation *ev_annot;
2621 mapping = (PopplerAnnotMapping *)list->data;
2623 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2629 /* Make sure annot has a unique name */
2630 if (!ev_annot->name)
2631 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2633 annot_mapping = g_new (EvMapping, 1);
2634 annot_mapping->area.x1 = mapping->area.x1;
2635 annot_mapping->area.x2 = mapping->area.x2;
2636 annot_mapping->area.y1 = height - mapping->area.y2;
2637 annot_mapping->area.y2 = height - mapping->area.y1;
2638 annot_mapping->data = ev_annot;
2640 g_object_set_data_full (G_OBJECT (ev_annot),
2642 g_object_ref (mapping->annot),
2643 (GDestroyNotify) g_object_unref);
2645 retval = g_list_prepend (retval, annot_mapping);
2648 poppler_page_free_annot_mapping (annots);
2650 return g_list_reverse (retval);
2654 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2655 EvAnnotation *annot,
2656 const gchar *contents)
2658 PopplerAnnot *poppler_annot;
2660 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2664 poppler_annot_set_contents (poppler_annot, contents);
2665 PDF_DOCUMENT (document)->modified = TRUE;
2669 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2671 iface->get_annotations = pdf_document_annotations_get_annotations;
2672 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2676 struct SaveToBufferData {
2682 attachment_save_to_buffer_callback (const gchar *buf,
2687 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2691 if (sdata->len + count > sdata->max) {
2692 new_max = MAX (sdata->max * 2, sdata->len + count);
2693 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2695 sdata->buffer = new_buffer;
2696 sdata->max = new_max;
2699 memcpy (sdata->buffer + sdata->len, buf, count);
2700 sdata->len += count;
2706 attachment_save_to_buffer (PopplerAttachment *attachment,
2711 static const gint initial_max = 1024;
2712 struct SaveToBufferData sdata;
2717 sdata.buffer = (gchar *) g_malloc (initial_max);
2718 sdata.max = initial_max;
2721 if (! poppler_attachment_save_to_callback (attachment,
2722 attachment_save_to_buffer_callback,
2725 g_free (sdata.buffer);
2729 *buffer = sdata.buffer;
2730 *buffer_size = sdata.len;
2736 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2738 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2741 GList *retval = NULL;
2743 attachments = poppler_document_get_attachments (pdf_document->document);
2745 for (list = attachments; list; list = list->next) {
2746 PopplerAttachment *attachment;
2747 EvAttachment *ev_attachment;
2750 GError *error = NULL;
2752 attachment = (PopplerAttachment *) list->data;
2754 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2755 ev_attachment = ev_attachment_new (attachment->name,
2756 attachment->description,
2761 retval = g_list_prepend (retval, ev_attachment);
2764 g_warning ("%s", error->message);
2765 g_error_free (error);
2771 g_object_unref (attachment);
2774 return g_list_reverse (retval);
2778 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2780 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2782 return poppler_document_has_attachments (pdf_document->document);
2786 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface)
2788 iface->has_attachments = pdf_document_attachments_has_attachments;
2789 iface->get_attachments = pdf_document_attachments_get_attachments;
2794 pdf_document_layers_has_layers (EvDocumentLayers *document)
2796 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2797 PopplerLayersIter *iter;
2799 iter = poppler_layers_iter_new (pdf_document->document);
2802 poppler_layers_iter_free (iter);
2808 build_layers_tree (PdfDocument *pdf_document,
2809 GtkTreeModel *model,
2810 GtkTreeIter *parent,
2811 PopplerLayersIter *iter)
2814 GtkTreeIter tree_iter;
2815 PopplerLayersIter *child;
2816 PopplerLayer *layer;
2817 EvLayer *ev_layer = NULL;
2822 layer = poppler_layers_iter_get_layer (iter);
2824 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2825 visible = poppler_layer_is_visible (layer);
2826 rb_group = poppler_layer_get_radio_button_group_id (layer);
2827 pdf_document->layers = g_list_append (pdf_document->layers,
2828 g_object_ref (layer));
2829 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2830 poppler_layer_is_parent (layer),
2835 title = poppler_layers_iter_get_title (iter);
2836 markup = g_markup_escape_text (title, -1);
2843 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2844 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2845 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2846 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2847 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2848 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2849 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2850 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2853 g_object_unref (ev_layer);
2856 child = poppler_layers_iter_get_child (iter);
2858 build_layers_tree (pdf_document, model, &tree_iter, child);
2859 poppler_layers_iter_free (child);
2860 } while (poppler_layers_iter_next (iter));
2863 static GtkTreeModel *
2864 pdf_document_layers_get_layers (EvDocumentLayers *document)
2866 GtkTreeModel *model = NULL;
2867 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2868 PopplerLayersIter *iter;
2870 iter = poppler_layers_iter_new (pdf_document->document);
2872 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2873 G_TYPE_STRING, /* TITLE */
2874 G_TYPE_OBJECT, /* LAYER */
2875 G_TYPE_BOOLEAN, /* VISIBLE */
2876 G_TYPE_BOOLEAN, /* ENABLED */
2877 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2878 G_TYPE_INT); /* RBGROUP */
2879 build_layers_tree (pdf_document, model, NULL, iter);
2880 poppler_layers_iter_free (iter);
2886 pdf_document_layers_show_layer (EvDocumentLayers *document,
2889 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2890 guint layer_id = ev_layer_get_id (layer);
2892 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2896 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2899 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2900 guint layer_id = ev_layer_get_id (layer);
2902 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2906 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2909 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2910 guint layer_id = ev_layer_get_id (layer);
2912 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2916 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2918 iface->has_layers = pdf_document_layers_has_layers;
2919 iface->get_layers = pdf_document_layers_get_layers;
2920 iface->show_layer = pdf_document_layers_show_layer;
2921 iface->hide_layer = pdf_document_layers_hide_layer;
2922 iface->layer_is_visible = pdf_document_layers_layer_is_visible;