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-document-find.h"
41 #include "ev-document-misc.h"
42 #include "ev-document-links.h"
43 #include "ev-document-images.h"
44 #include "ev-document-fonts.h"
45 #include "ev-document-security.h"
46 #include "ev-document-thumbnails.h"
47 #include "ev-document-transition.h"
48 #include "ev-document-forms.h"
49 #include "ev-document-layers.h"
50 #include "ev-document-print.h"
51 #include "ev-document-annotations.h"
52 #include "ev-document-attachments.h"
53 #include "ev-document-text.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_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 static void pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface);
123 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface);
124 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface);
125 static void pdf_document_find_iface_init (EvDocumentFindInterface *iface);
126 static void pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface);
127 static void pdf_selection_iface_init (EvSelectionInterface *iface);
128 static void pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface);
129 static void pdf_document_text_iface_init (EvDocumentTextInterface *iface);
130 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
134 static int pdf_document_get_n_pages (EvDocument *document);
136 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
138 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
139 PopplerAction *action);
140 static void pdf_print_context_free (PdfPrintContext *ctx);
141 static gboolean attachment_save_to_buffer (PopplerAttachment *attachment,
146 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
148 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
149 pdf_document_security_iface_init);
150 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
151 pdf_document_document_thumbnails_iface_init);
152 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
153 pdf_document_document_links_iface_init);
154 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
155 pdf_document_document_images_iface_init);
156 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
157 pdf_document_document_forms_iface_init);
158 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
159 pdf_document_document_fonts_iface_init);
160 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
161 pdf_document_document_layers_iface_init);
162 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
163 pdf_document_document_print_iface_init);
164 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
165 pdf_document_document_annotations_iface_init);
166 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
167 pdf_document_document_attachments_iface_init);
168 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
169 pdf_document_find_iface_init);
170 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
171 pdf_document_file_exporter_iface_init);
172 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
173 pdf_selection_iface_init);
174 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
175 pdf_document_page_transition_iface_init);
176 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TEXT,
177 pdf_document_text_iface_init);
181 pdf_document_dispose (GObject *object)
183 PdfDocument *pdf_document = PDF_DOCUMENT(object);
185 if (pdf_document->print_ctx) {
186 pdf_print_context_free (pdf_document->print_ctx);
187 pdf_document->print_ctx = NULL;
190 if (pdf_document->document) {
191 g_object_unref (pdf_document->document);
194 if (pdf_document->font_info) {
195 poppler_font_info_free (pdf_document->font_info);
198 if (pdf_document->fonts_iter) {
199 poppler_fonts_iter_free (pdf_document->fonts_iter);
202 if (pdf_document->layers) {
203 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
204 g_list_free (pdf_document->layers);
207 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
211 pdf_document_init (PdfDocument *pdf_document)
213 pdf_document->password = NULL;
217 convert_error (GError *poppler_error,
220 if (poppler_error == NULL)
223 if (poppler_error->domain == POPPLER_ERROR) {
224 /* convert poppler errors into EvDocument errors */
225 gint code = EV_DOCUMENT_ERROR_INVALID;
226 if (poppler_error->code == POPPLER_ERROR_INVALID)
227 code = EV_DOCUMENT_ERROR_INVALID;
228 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
229 code = EV_DOCUMENT_ERROR_ENCRYPTED;
231 g_set_error_literal (error,
234 poppler_error->message);
236 g_error_free (poppler_error);
238 g_propagate_error (error, poppler_error);
245 pdf_document_save (EvDocument *document,
249 PdfDocument *pdf_document = PDF_DOCUMENT (document);
251 GError *poppler_error = NULL;
253 if (pdf_document->modified) {
254 retval = poppler_document_save (pdf_document->document,
255 uri, &poppler_error);
257 retval = poppler_document_save_a_copy (pdf_document->document,
258 uri, &poppler_error);
262 convert_error (poppler_error, error);
268 pdf_document_load (EvDocument *document,
272 GError *poppler_error = NULL;
273 PdfDocument *pdf_document = PDF_DOCUMENT (document);
275 pdf_document->document =
276 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
278 if (pdf_document->document == NULL) {
279 convert_error (poppler_error, error);
287 pdf_document_get_n_pages (EvDocument *document)
289 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
293 pdf_document_get_page (EvDocument *document,
296 PdfDocument *pdf_document = PDF_DOCUMENT (document);
297 PopplerPage *poppler_page;
300 poppler_page = poppler_document_get_page (pdf_document->document, index);
301 page = ev_page_new (index);
302 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
303 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
304 g_object_unref (poppler_page);
310 pdf_document_get_page_size (EvDocument *document,
315 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
317 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
321 pdf_document_get_page_label (EvDocument *document,
326 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
328 g_object_get (G_OBJECT (page->backend_page),
334 static cairo_surface_t *
335 pdf_page_render (PopplerPage *page,
340 cairo_surface_t *surface;
343 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
345 cr = cairo_create (surface);
347 switch (rc->rotation) {
349 cairo_translate (cr, width, 0);
352 cairo_translate (cr, width, height);
355 cairo_translate (cr, 0, height);
358 cairo_translate (cr, 0, 0);
360 cairo_scale (cr, rc->scale, rc->scale);
361 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
362 poppler_page_render (page, cr);
364 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
365 cairo_set_source_rgb (cr, 1., 1., 1.);
373 static cairo_surface_t *
374 pdf_document_render (EvDocument *document,
377 PdfDocument *pdf_document;
378 PopplerPage *poppler_page;
379 double width_points, height_points;
382 poppler_page = POPPLER_PAGE (rc->page->backend_page);
384 poppler_page_get_size (poppler_page,
385 &width_points, &height_points);
387 if (rc->rotation == 90 || rc->rotation == 270) {
388 width = (int) ((height_points * rc->scale) + 0.5);
389 height = (int) ((width_points * rc->scale) + 0.5);
391 width = (int) ((width_points * rc->scale) + 0.5);
392 height = (int) ((height_points * rc->scale) + 0.5);
395 return pdf_page_render (poppler_page,
400 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 */
402 pdf_document_get_format_from_metadata (xmlDocPtr doc,
403 xmlXPathContextPtr xpathCtx)
405 xmlXPathObjectPtr xpathObj;
406 xmlChar *part = NULL;
407 xmlChar *conf = NULL;
411 /* add pdf/a namespaces */
412 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
413 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
414 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
416 /* reads pdf/a part */
417 /* first syntax: child node */
418 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
419 if (xpathObj != NULL) {
420 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
421 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
423 xmlXPathFreeObject (xpathObj);
426 /* second syntax: attribute */
427 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
428 if (xpathObj != NULL) {
429 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
430 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
432 xmlXPathFreeObject (xpathObj);
436 /* reads pdf/a conformance */
437 /* first syntax: child node */
438 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
439 if (xpathObj != NULL) {
440 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
441 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
443 xmlXPathFreeObject (xpathObj);
446 /* second syntax: attribute */
447 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
448 if (xpathObj != NULL) {
449 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
450 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
452 xmlXPathFreeObject (xpathObj);
456 if (part != NULL && conf != NULL) {
457 /* makes conf lowercase */
458 for (i = 0; conf[i]; i++)
459 conf[i] = g_ascii_tolower (conf[i]);
462 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
472 static EvDocumentLicense *
473 pdf_document_get_license_from_metadata (xmlDocPtr doc,
474 xmlXPathContextPtr xpathCtx)
476 xmlXPathObjectPtr xpathObj;
477 xmlChar *marked = NULL;
478 const char *language_string;
481 gchar *tag, *tag_aux;
483 EvDocumentLicense *license;
485 /* register namespaces */
486 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
487 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
488 xmlXPathRegisterNs (xpathCtx, BAD_CAST "dc", BAD_CAST "http://purl.org/dc/elements/1.1/");
489 /* XMP Rights Management Schema */
490 xmlXPathRegisterNs (xpathCtx, BAD_CAST "xmpRights", BAD_CAST "http://ns.adobe.com/xap/1.0/rights/");
491 /* Creative Commons Schema */
492 xmlXPathRegisterNs (xpathCtx, BAD_CAST "cc", BAD_CAST "http://creativecommons.org/ns#");
494 /* checking if the document has been marked as defined on the XMP Rights
495 * Management Schema */
496 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_MARKED, xpathCtx);
497 if (xpathObj != NULL) {
498 if (xpathObj->nodesetval != NULL &&
499 xpathObj->nodesetval->nodeNr != 0)
500 marked = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
501 xmlXPathFreeObject (xpathObj);
504 /* a) Not marked => No XMP Rights information */
510 license = ev_document_license_new ();
512 /* b) Marked False => Public Domain, no copyrighted material and no
514 if (g_strrstr ((char *) marked, "False") != NULL) {
515 license->text = g_strdup (_("This work is in the Public Domain"));
516 /* c) Marked True => Copyrighted material */
518 /* Checking usage terms as defined by the XMP Rights Management
519 * Schema. This field is recomended to be checked by Creative
521 /* 1) checking for a suitable localized string */
522 language_string = pango_language_to_string (gtk_get_default_language ());
523 tags = g_strsplit (language_string, "-", -1);
524 i = g_strv_length (tags);
525 while (i-- && !license->text) {
526 tag = g_strdup (tags[0]);
527 for (j = 1; j <= i; j++) {
528 tag_aux = g_strdup_printf ("%s-%s", tag, tags[j]);
532 aux = g_strdup_printf (LICENSE_TEXT, tag);
533 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
534 if (xpathObj != NULL) {
535 if (xpathObj->nodesetval != NULL &&
536 xpathObj->nodesetval->nodeNr != 0)
537 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
538 xmlXPathFreeObject (xpathObj);
545 /* 2) if not, use the default string */
546 if (!license->text) {
547 aux = g_strdup_printf (LICENSE_TEXT, "x-default");
548 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
549 if (xpathObj != NULL) {
550 if (xpathObj->nodesetval != NULL &&
551 xpathObj->nodesetval->nodeNr != 0)
552 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
553 xmlXPathFreeObject (xpathObj);
558 /* Checking the license URI as defined by the Creative Commons
559 * Schema. This field is recomended to be checked by Creative
561 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_URI, xpathCtx);
562 if (xpathObj != NULL) {
563 if (xpathObj->nodesetval != NULL &&
564 xpathObj->nodesetval->nodeNr != 0)
565 license->uri = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
566 xmlXPathFreeObject (xpathObj);
569 /* Checking the web statement as defined by the XMP Rights
570 * Management Schema. Checking it out is a sort of above-and-beyond
571 * the basic recommendations by Creative Commons. It can be
572 * considered as a "reinforcement" approach to add certainty. */
573 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_WEB_STATEMENT, xpathCtx);
574 if (xpathObj != NULL) {
575 if (xpathObj->nodesetval != NULL &&
576 xpathObj->nodesetval->nodeNr != 0)
577 license->web_statement = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
578 xmlXPathFreeObject (xpathObj);
583 if (!license->text && !license->uri && !license->web_statement) {
584 ev_document_license_free (license);
592 pdf_document_parse_metadata (const gchar *metadata,
593 EvDocumentInfo *info)
596 xmlXPathContextPtr xpathCtx;
599 doc = xmlParseMemory (metadata, strlen (metadata));
601 return; /* invalid xml metadata */
603 xpathCtx = xmlXPathNewContext (doc);
604 if (xpathCtx == NULL) {
606 return; /* invalid xpath context */
609 fmt = pdf_document_get_format_from_metadata (doc, xpathCtx);
611 g_free (info->format);
615 info->license = pdf_document_get_license_from_metadata (doc, xpathCtx);
617 xmlXPathFreeContext (xpathCtx);
622 static EvDocumentInfo *
623 pdf_document_get_info (EvDocument *document)
625 EvDocumentInfo *info;
626 PopplerPageLayout layout;
627 PopplerPageMode mode;
628 PopplerViewerPreferences view_prefs;
629 PopplerPermissions permissions;
633 info = g_new0 (EvDocumentInfo, 1);
635 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
636 EV_DOCUMENT_INFO_FORMAT |
637 EV_DOCUMENT_INFO_AUTHOR |
638 EV_DOCUMENT_INFO_SUBJECT |
639 EV_DOCUMENT_INFO_KEYWORDS |
640 EV_DOCUMENT_INFO_LAYOUT |
641 EV_DOCUMENT_INFO_START_MODE |
642 EV_DOCUMENT_INFO_PERMISSIONS |
643 EV_DOCUMENT_INFO_UI_HINTS |
644 EV_DOCUMENT_INFO_CREATOR |
645 EV_DOCUMENT_INFO_PRODUCER |
646 EV_DOCUMENT_INFO_CREATION_DATE |
647 EV_DOCUMENT_INFO_MOD_DATE |
648 EV_DOCUMENT_INFO_LINEARIZED |
649 EV_DOCUMENT_INFO_N_PAGES |
650 EV_DOCUMENT_INFO_SECURITY |
651 EV_DOCUMENT_INFO_PAPER_SIZE |
652 EV_DOCUMENT_INFO_LICENSE;
654 g_object_get (PDF_DOCUMENT (document)->document,
655 "title", &(info->title),
656 "format", &(info->format),
657 "author", &(info->author),
658 "subject", &(info->subject),
659 "keywords", &(info->keywords),
661 "page-layout", &layout,
662 "viewer-preferences", &view_prefs,
663 "permissions", &permissions,
664 "creator", &(info->creator),
665 "producer", &(info->producer),
666 "creation-date", &(info->creation_date),
667 "mod-date", &(info->modified_date),
668 "linearized", &(info->linearized),
669 "metadata", &metadata,
672 if (metadata != NULL) {
673 pdf_document_parse_metadata (metadata, info);
677 info->n_pages = ev_document_get_n_pages (document);
679 if (info->n_pages > 0) {
680 ev_document_get_page_size (document, 0,
681 &(info->paper_width),
682 &(info->paper_height));
684 info->paper_width = info->paper_width / 72.0f * 25.4f;
685 info->paper_height = info->paper_height / 72.0f * 25.4f;
689 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
690 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
692 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
693 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
695 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
696 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
698 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
699 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
700 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
701 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
703 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
704 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
711 case POPPLER_PAGE_MODE_NONE:
712 info->mode = EV_DOCUMENT_MODE_NONE;
714 case POPPLER_PAGE_MODE_USE_THUMBS:
715 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
717 case POPPLER_PAGE_MODE_USE_OC:
718 info->mode = EV_DOCUMENT_MODE_USE_OC;
720 case POPPLER_PAGE_MODE_FULL_SCREEN:
721 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
723 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
724 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
730 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
731 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
733 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
734 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
736 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
737 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
739 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
740 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
742 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
743 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
745 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
746 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
748 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
749 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
752 info->permissions = 0;
753 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
754 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
756 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
757 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
759 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
760 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
762 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
763 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
766 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
767 /* translators: this is the document security state */
768 info->security = g_strdup (_("Yes"));
770 /* translators: this is the document security state */
771 info->security = g_strdup (_("No"));
778 pdf_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
780 PopplerBackend backend;
782 backend = poppler_get_backend ();
784 case POPPLER_BACKEND_CAIRO:
785 info->name = "poppler/cairo";
787 case POPPLER_BACKEND_SPLASH:
788 info->name = "poppler/splash";
791 info->name = "poppler/unknown";
795 info->version = poppler_get_version ();
801 pdf_document_support_synctex (EvDocument *document)
807 pdf_document_class_init (PdfDocumentClass *klass)
809 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
810 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
812 g_object_class->dispose = pdf_document_dispose;
814 ev_document_class->save = pdf_document_save;
815 ev_document_class->load = pdf_document_load;
816 ev_document_class->get_n_pages = pdf_document_get_n_pages;
817 ev_document_class->get_page = pdf_document_get_page;
818 ev_document_class->get_page_size = pdf_document_get_page_size;
819 ev_document_class->get_page_label = pdf_document_get_page_label;
820 ev_document_class->render = pdf_document_render;
821 ev_document_class->get_info = pdf_document_get_info;
822 ev_document_class->get_backend_info = pdf_document_get_backend_info;
823 ev_document_class->support_synctex = pdf_document_support_synctex;
826 /* EvDocumentSecurity */
828 pdf_document_has_document_security (EvDocumentSecurity *document_security)
830 /* FIXME: do we really need to have this? */
835 pdf_document_set_password (EvDocumentSecurity *document_security,
836 const char *password)
838 PdfDocument *document = PDF_DOCUMENT (document_security);
840 if (document->password)
841 g_free (document->password);
843 document->password = g_strdup (password);
847 pdf_document_security_iface_init (EvDocumentSecurityInterface *iface)
849 iface->has_document_security = pdf_document_has_document_security;
850 iface->set_password = pdf_document_set_password;
854 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
856 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
859 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
861 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
865 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
868 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
871 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
873 if (pdf_document->font_info == NULL) {
874 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
877 if (pdf_document->fonts_iter) {
878 poppler_fonts_iter_free (pdf_document->fonts_iter);
881 pdf_document->fonts_scanned_pages += n_pages;
883 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
884 &pdf_document->fonts_iter);
886 pdf_document->fonts_scanned_pages = 0;
887 poppler_font_info_free (pdf_document->font_info);
888 pdf_document->font_info = NULL;
895 font_type_to_string (PopplerFontType type)
898 case POPPLER_FONT_TYPE_TYPE1:
900 case POPPLER_FONT_TYPE_TYPE1C:
902 case POPPLER_FONT_TYPE_TYPE3:
904 case POPPLER_FONT_TYPE_TRUETYPE:
905 return _("TrueType");
906 case POPPLER_FONT_TYPE_CID_TYPE0:
907 return _("Type 1 (CID)");
908 case POPPLER_FONT_TYPE_CID_TYPE0C:
909 return _("Type 1C (CID)");
910 case POPPLER_FONT_TYPE_CID_TYPE2:
911 return _("TrueType (CID)");
913 return _("Unknown font type");
918 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
921 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
922 PopplerFontsIter *iter = pdf_document->fonts_iter;
924 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
930 GtkTreeIter list_iter;
933 const char *embedded;
936 name = poppler_fonts_iter_get_name (iter);
942 type = font_type_to_string (
943 poppler_fonts_iter_get_font_type (iter));
945 if (poppler_fonts_iter_is_embedded (iter)) {
946 if (poppler_fonts_iter_is_subset (iter))
947 embedded = _("Embedded subset");
949 embedded = _("Embedded");
951 embedded = _("Not embedded");
954 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
956 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
957 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
958 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
959 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
963 } while (poppler_fonts_iter_next (iter));
967 pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface)
969 iface->fill_model = pdf_document_fonts_fill_model;
970 iface->scan = pdf_document_fonts_scan;
971 iface->get_progress = pdf_document_fonts_get_progress;
975 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
977 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
978 PopplerIndexIter *iter;
980 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
982 iter = poppler_index_iter_new (pdf_document->document);
985 poppler_index_iter_free (iter);
991 ev_link_dest_from_dest (PdfDocument *pdf_document,
994 EvLinkDest *ev_dest = NULL;
995 const char *unimplemented_dest = NULL;
997 g_assert (dest != NULL);
999 switch (dest->type) {
1000 case POPPLER_DEST_XYZ: {
1001 PopplerPage *poppler_page;
1004 poppler_page = poppler_document_get_page (pdf_document->document,
1005 MAX (0, dest->page_num - 1));
1006 poppler_page_get_size (poppler_page, NULL, &height);
1007 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1009 height - MIN (height, dest->top),
1014 g_object_unref (poppler_page);
1017 case POPPLER_DEST_FITB:
1018 case POPPLER_DEST_FIT:
1019 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1021 case POPPLER_DEST_FITBH:
1022 case POPPLER_DEST_FITH: {
1023 PopplerPage *poppler_page;
1026 poppler_page = poppler_document_get_page (pdf_document->document,
1027 MAX (0, dest->page_num - 1));
1028 poppler_page_get_size (poppler_page, NULL, &height);
1029 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1030 height - MIN (height, dest->top),
1032 g_object_unref (poppler_page);
1035 case POPPLER_DEST_FITBV:
1036 case POPPLER_DEST_FITV:
1037 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1041 case POPPLER_DEST_FITR: {
1042 PopplerPage *poppler_page;
1045 poppler_page = poppler_document_get_page (pdf_document->document,
1046 MAX (0, dest->page_num - 1));
1047 poppler_page_get_size (poppler_page, NULL, &height);
1048 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1050 height - MIN (height, dest->bottom),
1052 height - MIN (height, dest->top));
1053 g_object_unref (poppler_page);
1056 case POPPLER_DEST_NAMED:
1057 ev_dest = ev_link_dest_new_named (dest->named_dest);
1059 case POPPLER_DEST_UNKNOWN:
1060 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1064 if (unimplemented_dest) {
1065 g_warning ("Unimplemented destination: %s, please post a "
1066 "bug report in Evince bugzilla "
1067 "(http://bugzilla.gnome.org) with a testcase.",
1068 unimplemented_dest);
1072 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1078 ev_link_from_action (PdfDocument *pdf_document,
1079 PopplerAction *action)
1081 EvLink *link = NULL;
1082 EvLinkAction *ev_action = NULL;
1083 const char *unimplemented_action = NULL;
1085 switch (action->type) {
1086 case POPPLER_ACTION_NONE:
1088 case POPPLER_ACTION_GOTO_DEST: {
1091 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1092 ev_action = ev_link_action_new_dest (dest);
1095 case POPPLER_ACTION_GOTO_REMOTE: {
1098 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1099 ev_action = ev_link_action_new_remote (dest,
1100 action->goto_remote.file_name);
1104 case POPPLER_ACTION_LAUNCH:
1105 ev_action = ev_link_action_new_launch (action->launch.file_name,
1106 action->launch.params);
1108 case POPPLER_ACTION_URI:
1109 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1111 case POPPLER_ACTION_NAMED:
1112 ev_action = ev_link_action_new_named (action->named.named_dest);
1114 case POPPLER_ACTION_MOVIE:
1115 unimplemented_action = "POPPLER_ACTION_MOVIE";
1117 #if POPPLER_CHECK_VERSION (0, 13, 2)
1118 case POPPLER_ACTION_RENDITION:
1119 unimplemented_action = "POPPLER_ACTION_RENDITION";
1121 case POPPLER_ACTION_OCG_STATE:
1122 unimplemented_action = "POPPLER_ACTION_OCG_STATE";
1125 case POPPLER_ACTION_UNKNOWN:
1126 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1129 if (unimplemented_action) {
1130 g_warning ("Unimplemented action: %s, please post a bug report "
1131 "in Evince bugzilla (http://bugzilla.gnome.org) "
1132 "with a testcase.", unimplemented_action);
1135 link = ev_link_new (action->any.title, ev_action);
1141 build_tree (PdfDocument *pdf_document,
1142 GtkTreeModel *model,
1143 GtkTreeIter *parent,
1144 PopplerIndexIter *iter)
1148 GtkTreeIter tree_iter;
1149 PopplerIndexIter *child;
1150 PopplerAction *action;
1151 EvLink *link = NULL;
1155 action = poppler_index_iter_get_action (iter);
1156 expand = poppler_index_iter_is_open (iter);
1161 switch (action->type) {
1162 case POPPLER_ACTION_GOTO_DEST: {
1163 /* For bookmarks, solve named destinations */
1164 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1166 EvLinkDest *ev_dest = NULL;
1167 EvLinkAction *ev_action;
1169 dest = poppler_document_find_dest (pdf_document->document,
1170 action->goto_dest.dest->named_dest);
1172 link = ev_link_from_action (pdf_document, action);
1176 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1177 poppler_dest_free (dest);
1179 ev_action = ev_link_action_new_dest (ev_dest);
1180 link = ev_link_new (action->any.title, ev_action);
1182 link = ev_link_from_action (pdf_document, action);
1187 link = ev_link_from_action (pdf_document, action);
1191 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1192 poppler_action_free (action);
1194 g_object_unref (link);
1199 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1200 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1202 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1203 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1204 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1205 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1208 g_free (title_markup);
1209 g_object_unref (link);
1211 child = poppler_index_iter_get_child (iter);
1213 build_tree (pdf_document, model, &tree_iter, child);
1214 poppler_index_iter_free (child);
1215 poppler_action_free (action);
1217 } while (poppler_index_iter_next (iter));
1220 static GtkTreeModel *
1221 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1223 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1224 GtkTreeModel *model = NULL;
1225 PopplerIndexIter *iter;
1227 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1229 iter = poppler_index_iter_new (pdf_document->document);
1230 /* Create the model if we have items*/
1232 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1237 build_tree (pdf_document, model, NULL, iter);
1238 poppler_index_iter_free (iter);
1244 static EvMappingList *
1245 pdf_document_links_get_links (EvDocumentLinks *document_links,
1248 PdfDocument *pdf_document;
1249 PopplerPage *poppler_page;
1250 GList *retval = NULL;
1251 GList *mapping_list;
1255 pdf_document = PDF_DOCUMENT (document_links);
1256 poppler_page = POPPLER_PAGE (page->backend_page);
1257 mapping_list = poppler_page_get_link_mapping (poppler_page);
1258 poppler_page_get_size (poppler_page, NULL, &height);
1260 for (list = mapping_list; list; list = list->next) {
1261 PopplerLinkMapping *link_mapping;
1262 EvMapping *ev_link_mapping;
1264 link_mapping = (PopplerLinkMapping *)list->data;
1265 ev_link_mapping = g_new (EvMapping, 1);
1266 ev_link_mapping->data = ev_link_from_action (pdf_document,
1267 link_mapping->action);
1268 ev_link_mapping->area.x1 = link_mapping->area.x1;
1269 ev_link_mapping->area.x2 = link_mapping->area.x2;
1270 /* Invert this for X-style coordinates */
1271 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1272 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1274 retval = g_list_prepend (retval, ev_link_mapping);
1277 poppler_page_free_link_mapping (mapping_list);
1279 return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
1283 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1284 const gchar *link_name)
1286 PdfDocument *pdf_document;
1288 EvLinkDest *ev_dest = NULL;
1290 pdf_document = PDF_DOCUMENT (document_links);
1291 dest = poppler_document_find_dest (pdf_document->document,
1294 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1295 poppler_dest_free (dest);
1302 pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface)
1304 iface->has_document_links = pdf_document_links_has_document_links;
1305 iface->get_links_model = pdf_document_links_get_links_model;
1306 iface->get_links = pdf_document_links_get_links;
1307 iface->find_link_dest = pdf_document_links_find_link_dest;
1310 static EvMappingList *
1311 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1314 GList *retval = NULL;
1315 PdfDocument *pdf_document;
1316 PopplerPage *poppler_page;
1317 GList *mapping_list;
1320 pdf_document = PDF_DOCUMENT (document_images);
1321 poppler_page = POPPLER_PAGE (page->backend_page);
1322 mapping_list = poppler_page_get_image_mapping (poppler_page);
1324 for (list = mapping_list; list; list = list->next) {
1325 PopplerImageMapping *image_mapping;
1326 EvMapping *ev_image_mapping;
1328 image_mapping = (PopplerImageMapping *)list->data;
1330 ev_image_mapping = g_new (EvMapping, 1);
1332 ev_image_mapping->data = ev_image_new (page->index, image_mapping->image_id);
1333 ev_image_mapping->area.x1 = image_mapping->area.x1;
1334 ev_image_mapping->area.y1 = image_mapping->area.y1;
1335 ev_image_mapping->area.x2 = image_mapping->area.x2;
1336 ev_image_mapping->area.y2 = image_mapping->area.y2;
1338 retval = g_list_prepend (retval, ev_image_mapping);
1341 poppler_page_free_image_mapping (mapping_list);
1343 return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
1347 pdf_document_images_get_image (EvDocumentImages *document_images,
1350 GdkPixbuf *retval = NULL;
1351 PdfDocument *pdf_document;
1352 PopplerPage *poppler_page;
1353 cairo_surface_t *surface;
1355 pdf_document = PDF_DOCUMENT (document_images);
1356 poppler_page = poppler_document_get_page (pdf_document->document,
1357 ev_image_get_page (image));
1359 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1361 retval = ev_document_misc_pixbuf_from_surface (surface);
1362 cairo_surface_destroy (surface);
1365 g_object_unref (poppler_page);
1371 pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface)
1373 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1374 iface->get_image = pdf_document_images_get_image;
1378 make_thumbnail_for_page (PopplerPage *poppler_page,
1379 EvRenderContext *rc,
1385 #ifdef POPPLER_WITH_GDK
1386 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1388 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1390 ev_document_fc_mutex_lock ();
1391 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1393 rc->scale, rc->rotation, pixbuf);
1394 ev_document_fc_mutex_unlock ();
1396 cairo_surface_t *surface;
1398 ev_document_fc_mutex_lock ();
1399 surface = pdf_page_render (poppler_page, width, height, rc);
1400 ev_document_fc_mutex_unlock ();
1402 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1403 cairo_surface_destroy (surface);
1404 #endif /* POPPLER_WITH_GDK */
1410 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1411 EvRenderContext *rc,
1414 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1415 PopplerPage *poppler_page;
1416 GdkPixbuf *pixbuf = NULL;
1417 GdkPixbuf *border_pixbuf;
1420 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1422 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1423 rc, &width, &height);
1425 #ifdef POPPLER_WITH_GDK
1426 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1428 cairo_surface_t *surface;
1430 surface = poppler_page_get_thumbnail (poppler_page);
1432 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1433 cairo_surface_destroy (surface);
1435 #endif /* POPPLER_WITH_GDK */
1437 if (pixbuf != NULL) {
1438 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1439 gdk_pixbuf_get_height (pixbuf) :
1440 gdk_pixbuf_get_width (pixbuf);
1442 if (thumb_width == width) {
1443 GdkPixbuf *rotated_pixbuf;
1445 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1446 (GdkPixbufRotation) (360 - rc->rotation));
1447 g_object_unref (pixbuf);
1448 pixbuf = rotated_pixbuf;
1450 /* The provided thumbnail has a different size */
1451 g_object_unref (pixbuf);
1452 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1455 /* There is no provided thumbnail. We need to make one. */
1456 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1459 if (border && pixbuf) {
1460 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1461 g_object_unref (pixbuf);
1462 pixbuf = border_pixbuf;
1469 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1470 EvRenderContext *rc,
1474 double page_width, page_height;
1476 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1477 &page_width, &page_height);
1479 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1480 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1482 if (rc->rotation == 90 || rc->rotation == 270) {
1492 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
1494 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1495 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1500 pdf_document_find_find_text (EvDocumentFind *document_find,
1503 gboolean case_sensitive)
1506 PopplerPage *poppler_page;
1508 GList *retval = NULL;
1510 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1511 g_return_val_if_fail (text != NULL, NULL);
1513 poppler_page = POPPLER_PAGE (page->backend_page);
1515 matches = poppler_page_find_text (poppler_page, text);
1519 poppler_page_get_size (poppler_page, NULL, &height);
1520 for (l = matches; l && l->data; l = g_list_next (l)) {
1521 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1522 EvRectangle *ev_rect;
1524 ev_rect = ev_rectangle_new ();
1525 ev_rect->x1 = rect->x1;
1526 ev_rect->x2 = rect->x2;
1527 /* Invert this for X-style coordinates */
1528 ev_rect->y1 = height - rect->y2;
1529 ev_rect->y2 = height - rect->y1;
1531 retval = g_list_prepend (retval, ev_rect);
1534 g_list_foreach (matches, (GFunc)poppler_rectangle_free, NULL);
1535 g_list_free (matches);
1537 return g_list_reverse (retval);
1541 pdf_document_find_iface_init (EvDocumentFindInterface *iface)
1543 iface->find_text = pdf_document_find_find_text;
1547 pdf_print_context_free (PdfPrintContext *ctx)
1552 #ifdef HAVE_CAIRO_PRINT
1554 cairo_destroy (ctx->cr);
1559 poppler_ps_file_free (ctx->ps_file);
1560 ctx->ps_file = NULL;
1567 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1568 EvFileExporterContext *fc)
1570 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1571 PdfPrintContext *ctx;
1572 #ifdef HAVE_CAIRO_PRINT
1573 gdouble width, height;
1574 cairo_surface_t *surface = NULL;
1577 if (pdf_document->print_ctx)
1578 pdf_print_context_free (pdf_document->print_ctx);
1579 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1580 ctx = pdf_document->print_ctx;
1581 ctx->format = fc->format;
1583 #ifdef HAVE_CAIRO_PRINT
1584 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1586 ctx->paper_width = fc->paper_width;
1587 ctx->paper_height = fc->paper_height;
1589 switch (fc->pages_per_sheet) {
1617 ctx->pages_printed = 0;
1619 switch (fc->format) {
1620 case EV_FILE_FORMAT_PS:
1621 #ifdef HAVE_CAIRO_PS
1622 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1625 case EV_FILE_FORMAT_PDF:
1626 #ifdef HAVE_CAIRO_PDF
1627 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1631 g_assert_not_reached ();
1634 ctx->cr = cairo_create (surface);
1635 cairo_surface_destroy (surface);
1637 #else /* HAVE_CAIRO_PRINT */
1638 if (ctx->format == EV_FILE_FORMAT_PS) {
1639 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1640 fc->filename, fc->first_page,
1641 fc->last_page - fc->first_page + 1);
1642 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1643 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1645 #endif /* HAVE_CAIRO_PRINT */
1649 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1651 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1652 PdfPrintContext *ctx = pdf_document->print_ctx;
1654 g_return_if_fail (pdf_document->print_ctx != NULL);
1656 ctx->pages_printed = 0;
1658 #ifdef HAVE_CAIRO_PRINT
1659 if (ctx->paper_width > ctx->paper_height) {
1660 if (ctx->format == EV_FILE_FORMAT_PS) {
1661 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1664 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1665 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1670 #endif /* HAVE_CAIRO_PRINT */
1674 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1675 EvRenderContext *rc)
1677 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1678 PdfPrintContext *ctx = pdf_document->print_ctx;
1679 PopplerPage *poppler_page;
1680 #ifdef HAVE_CAIRO_PRINT
1681 gdouble page_width, page_height;
1684 gdouble width, height;
1685 gdouble pwidth, pheight;
1686 gdouble xscale, yscale;
1689 g_return_if_fail (pdf_document->print_ctx != NULL);
1691 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1693 #ifdef HAVE_CAIRO_PRINT
1694 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1695 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1696 poppler_page_get_size (poppler_page, &page_width, &page_height);
1698 if (page_width > page_height && page_width > ctx->paper_width) {
1704 /* Use always portrait mode and rotate when necessary */
1705 if (ctx->paper_width > ctx->paper_height) {
1706 width = ctx->paper_height;
1707 height = ctx->paper_width;
1710 width = ctx->paper_width;
1711 height = ctx->paper_height;
1714 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1727 page_width = page_height;
1731 pwidth = width / ctx->pages_x;
1732 pheight = height / ctx->pages_y;
1734 if ((page_width > pwidth || page_height > pheight) ||
1735 (page_width < pwidth && page_height < pheight)) {
1736 xscale = pwidth / page_width;
1737 yscale = pheight / page_height;
1739 if (yscale < xscale) {
1746 xscale = yscale = 1;
1751 cairo_save (ctx->cr);
1753 cairo_matrix_t matrix;
1755 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1756 cairo_matrix_init (&matrix,
1760 cairo_transform (ctx->cr, &matrix);
1763 cairo_translate (ctx->cr,
1764 x * (rotate ? pheight : pwidth),
1765 y * (rotate ? pwidth : pheight));
1766 cairo_scale (ctx->cr, xscale, yscale);
1768 poppler_page_render_for_printing (poppler_page, ctx->cr);
1770 ctx->pages_printed++;
1772 cairo_restore (ctx->cr);
1773 #else /* HAVE_CAIRO_PRINT */
1774 if (ctx->format == EV_FILE_FORMAT_PS)
1775 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1776 #endif /* HAVE_CAIRO_PRINT */
1780 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1782 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1783 PdfPrintContext *ctx = pdf_document->print_ctx;
1785 g_return_if_fail (pdf_document->print_ctx != NULL);
1787 #ifdef HAVE_CAIRO_PRINT
1788 cairo_show_page (ctx->cr);
1793 pdf_document_file_exporter_end (EvFileExporter *exporter)
1795 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1797 pdf_print_context_free (pdf_document->print_ctx);
1798 pdf_document->print_ctx = NULL;
1801 static EvFileExporterCapabilities
1802 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1804 return (EvFileExporterCapabilities) (
1805 EV_FILE_EXPORTER_CAN_PAGE_SET |
1806 EV_FILE_EXPORTER_CAN_COPIES |
1807 EV_FILE_EXPORTER_CAN_COLLATE |
1808 EV_FILE_EXPORTER_CAN_REVERSE |
1809 EV_FILE_EXPORTER_CAN_SCALE |
1810 #ifdef HAVE_CAIRO_PRINT
1811 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1814 #ifdef HAVE_CAIRO_PDF
1815 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1817 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1821 pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface)
1823 iface->begin = pdf_document_file_exporter_begin;
1824 iface->begin_page = pdf_document_file_exporter_begin_page;
1825 iface->do_page = pdf_document_file_exporter_do_page;
1826 iface->end_page = pdf_document_file_exporter_end_page;
1827 iface->end = pdf_document_file_exporter_end;
1828 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1831 /* EvDocumentPrint */
1833 pdf_document_print_print_page (EvDocumentPrint *document,
1837 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1839 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1843 pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface)
1845 iface->print_page = pdf_document_print_print_page;
1849 pdf_selection_render_selection (EvSelection *selection,
1850 EvRenderContext *rc,
1851 cairo_surface_t **surface,
1852 EvRectangle *points,
1853 EvRectangle *old_points,
1854 EvSelectionStyle style,
1858 PopplerPage *poppler_page;
1860 PopplerColor text_color, base_color;
1861 double width_points, height_points;
1864 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1866 poppler_page_get_size (poppler_page,
1867 &width_points, &height_points);
1868 width = (int) ((width_points * rc->scale) + 0.5);
1869 height = (int) ((height_points * rc->scale) + 0.5);
1871 text_color.red = text->red;
1872 text_color.green = text->green;
1873 text_color.blue = text->blue;
1875 base_color.red = base->red;
1876 base_color.green = base->green;
1877 base_color.blue = base->blue;
1879 if (*surface == NULL) {
1880 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1885 cr = cairo_create (*surface);
1886 cairo_scale (cr, rc->scale, rc->scale);
1887 cairo_surface_set_device_offset (*surface, 0, 0);
1888 memset (cairo_image_surface_get_data (*surface), 0x00,
1889 cairo_image_surface_get_height (*surface) *
1890 cairo_image_surface_get_stride (*surface));
1891 poppler_page_render_selection (poppler_page,
1893 (PopplerRectangle *)points,
1894 (PopplerRectangle *)old_points,
1895 (PopplerSelectionStyle)style,
1902 pdf_selection_get_selected_text (EvSelection *selection,
1904 EvSelectionStyle style,
1905 EvRectangle *points)
1907 PopplerPage *poppler_page;
1910 poppler_page = POPPLER_PAGE (page->backend_page);
1912 #ifdef HAVE_POPPLER_PAGE_GET_SELECTED_TEXT
1913 retval = poppler_page_get_selected_text (poppler_page,
1914 (PopplerSelectionStyle)style,
1915 (PopplerRectangle *)points);
1920 poppler_page_get_size (poppler_page, NULL, &height);
1922 r.y1 = height - points->y2;
1924 r.y2 = height - points->y1;
1926 retval = poppler_page_get_text (poppler_page,
1927 (PopplerSelectionStyle)style,
1929 #endif /* HAVE_POPPLER_PAGE_GET_SELECTED_TEXT */
1934 static cairo_region_t *
1935 create_region_from_poppler_region (GList *region, gdouble scale)
1938 cairo_region_t *retval;
1940 retval = cairo_region_create ();
1942 for (l = region; l; l = g_list_next (l)) {
1943 PopplerRectangle *rectangle;
1944 cairo_rectangle_int_t rect;
1946 rectangle = (PopplerRectangle *)l->data;
1948 rect.x = (gint) ((rectangle->x1 * scale) + 0.5);
1949 rect.y = (gint) ((rectangle->y1 * scale) + 0.5);
1950 rect.width = (gint) (((rectangle->x2 - rectangle->x1) * scale) + 0.5);
1951 rect.height = (gint) (((rectangle->y2 - rectangle->y1) * scale) + 0.5);
1952 cairo_region_union_rectangle (retval, &rect);
1954 poppler_rectangle_free (rectangle);
1960 static cairo_region_t *
1961 pdf_selection_get_selection_region (EvSelection *selection,
1962 EvRenderContext *rc,
1963 EvSelectionStyle style,
1964 EvRectangle *points)
1966 PopplerPage *poppler_page;
1967 cairo_region_t *retval;
1970 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1971 region = poppler_page_get_selection_region (poppler_page,
1973 (PopplerSelectionStyle)style,
1974 (PopplerRectangle *) points);
1975 retval = create_region_from_poppler_region (region, rc->scale);
1976 g_list_free (region);
1982 pdf_selection_iface_init (EvSelectionInterface *iface)
1984 iface->render_selection = pdf_selection_render_selection;
1985 iface->get_selected_text = pdf_selection_get_selected_text;
1986 iface->get_selection_region = pdf_selection_get_selection_region;
1990 /* EvDocumentText */
1991 static cairo_region_t *
1992 pdf_document_text_get_text_mapping (EvDocumentText *document_text,
1995 PopplerPage *poppler_page;
1996 PopplerRectangle points;
1998 cairo_region_t *retval;
2000 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2002 poppler_page = POPPLER_PAGE (page->backend_page);
2006 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2008 region = poppler_page_get_selection_region (poppler_page, 1.0,
2009 POPPLER_SELECTION_GLYPH,
2011 retval = create_region_from_poppler_region (region, 1.0);
2012 g_list_free (region);
2017 #ifdef HAVE_POPPLER_PAGE_GET_SELECTED_TEXT
2019 pdf_document_text_get_text (EvDocumentText *selection,
2022 PopplerPage *poppler_page;
2024 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2026 poppler_page = POPPLER_PAGE (page->backend_page);
2028 return poppler_page_get_text (poppler_page);
2032 pdf_document_text_get_text (EvDocumentText *selection,
2035 PopplerPage *poppler_page;
2038 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2040 poppler_page = POPPLER_PAGE (page->backend_page);
2044 poppler_page_get_size (poppler_page, &(r.x2), &(r.y2));
2046 return poppler_page_get_text (poppler_page,
2047 POPPLER_SELECTION_WORD,
2050 #endif /* HAVE_POPPLER_PAGE_GET_SELECTED_TEXT */
2052 #ifdef HAVE_POPPLER_PAGE_GET_TEXT_LAYOUT
2054 pdf_document_text_get_text_layout (EvDocumentText *selection,
2056 EvRectangle **areas,
2059 PopplerPage *poppler_page;
2061 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2063 poppler_page = POPPLER_PAGE (page->backend_page);
2065 return poppler_page_get_text_layout (poppler_page, (PopplerRectangle **)areas, n_areas);
2070 pdf_document_text_iface_init (EvDocumentTextInterface *iface)
2072 iface->get_text_mapping = pdf_document_text_get_text_mapping;
2073 iface->get_text = pdf_document_text_get_text;
2074 #ifdef HAVE_POPPLER_PAGE_GET_TEXT_LAYOUT
2075 iface->get_text_layout = pdf_document_text_get_text_layout;
2079 /* Page Transitions */
2081 pdf_document_get_page_duration (EvDocumentTransition *trans,
2084 PdfDocument *pdf_document;
2085 PopplerPage *poppler_page;
2086 gdouble duration = -1;
2088 pdf_document = PDF_DOCUMENT (trans);
2089 poppler_page = poppler_document_get_page (pdf_document->document, page);
2093 duration = poppler_page_get_duration (poppler_page);
2094 g_object_unref (poppler_page);
2099 static EvTransitionEffect *
2100 pdf_document_get_effect (EvDocumentTransition *trans,
2103 PdfDocument *pdf_document;
2104 PopplerPage *poppler_page;
2105 PopplerPageTransition *page_transition;
2106 EvTransitionEffect *effect;
2108 pdf_document = PDF_DOCUMENT (trans);
2109 poppler_page = poppler_document_get_page (pdf_document->document, page);
2114 page_transition = poppler_page_get_transition (poppler_page);
2116 if (!page_transition) {
2117 g_object_unref (poppler_page);
2121 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2122 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2123 "alignment", page_transition->alignment,
2124 "direction", page_transition->direction,
2125 "duration", page_transition->duration,
2126 "angle", page_transition->angle,
2127 "scale", page_transition->scale,
2128 "rectangular", page_transition->rectangular,
2131 poppler_page_transition_free (page_transition);
2132 g_object_unref (poppler_page);
2138 pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface)
2140 iface->get_page_duration = pdf_document_get_page_duration;
2141 iface->get_effect = pdf_document_get_effect;
2146 pdf_document_get_crop_box (EvDocument *document,
2150 PdfDocument *pdf_document;
2151 PopplerPage *poppler_page;
2152 PopplerRectangle poppler_rect;
2154 pdf_document = PDF_DOCUMENT (document);
2155 poppler_page = poppler_document_get_page (pdf_document->document, page);
2156 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2157 rect->x1 = poppler_rect.x1;
2158 rect->x2 = poppler_rect.x2;
2159 rect->y1 = poppler_rect.y1;
2160 rect->y2 = poppler_rect.y2;
2163 static EvFormField *
2164 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2166 EvFormField *ev_field = NULL;
2169 gboolean is_read_only;
2171 id = poppler_form_field_get_id (poppler_field);
2172 font_size = poppler_form_field_get_font_size (poppler_field);
2173 is_read_only = poppler_form_field_is_read_only (poppler_field);
2175 switch (poppler_form_field_get_field_type (poppler_field)) {
2176 case POPPLER_FORM_FIELD_TEXT: {
2177 EvFormFieldText *field_text;
2178 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2180 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2181 case POPPLER_FORM_TEXT_NORMAL:
2182 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2184 case POPPLER_FORM_TEXT_MULTILINE:
2185 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2187 case POPPLER_FORM_TEXT_FILE_SELECT:
2188 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2192 ev_field = ev_form_field_text_new (id, ev_text_type);
2193 field_text = EV_FORM_FIELD_TEXT (ev_field);
2195 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2196 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2197 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2198 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2199 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2200 field_text->text = poppler_form_field_text_get_text (poppler_field);
2204 case POPPLER_FORM_FIELD_BUTTON: {
2205 EvFormFieldButton *field_button;
2206 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2208 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2209 case POPPLER_FORM_BUTTON_PUSH:
2210 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2212 case POPPLER_FORM_BUTTON_CHECK:
2213 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2215 case POPPLER_FORM_BUTTON_RADIO:
2216 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2220 ev_field = ev_form_field_button_new (id, ev_button_type);
2221 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2223 field_button->state = poppler_form_field_button_get_state (poppler_field);
2226 case POPPLER_FORM_FIELD_CHOICE: {
2227 EvFormFieldChoice *field_choice;
2228 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2230 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2231 case POPPLER_FORM_CHOICE_COMBO:
2232 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2234 case EV_FORM_FIELD_CHOICE_LIST:
2235 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2239 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2240 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2242 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2243 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2244 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2245 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2247 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2248 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2249 if (field_choice->is_editable)
2250 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2253 case POPPLER_FORM_FIELD_SIGNATURE:
2255 ev_field = ev_form_field_signature_new (id);
2257 case POPPLER_FORM_FIELD_UNKNOWN:
2261 ev_field->font_size = font_size;
2262 ev_field->is_read_only = is_read_only;
2267 static EvMappingList *
2268 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2271 PopplerPage *poppler_page;
2272 GList *retval = NULL;
2277 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2279 poppler_page = POPPLER_PAGE (page->backend_page);
2280 fields = poppler_page_get_form_field_mapping (poppler_page);
2281 poppler_page_get_size (poppler_page, NULL, &height);
2283 for (list = fields; list; list = list->next) {
2284 PopplerFormFieldMapping *mapping;
2285 EvMapping *field_mapping;
2286 EvFormField *ev_field;
2288 mapping = (PopplerFormFieldMapping *)list->data;
2290 ev_field = ev_form_field_from_poppler_field (mapping->field);
2294 field_mapping = g_new0 (EvMapping, 1);
2295 field_mapping->area.x1 = mapping->area.x1;
2296 field_mapping->area.x2 = mapping->area.x2;
2297 field_mapping->area.y1 = height - mapping->area.y2;
2298 field_mapping->area.y2 = height - mapping->area.y1;
2299 field_mapping->data = ev_field;
2300 ev_field->page = EV_PAGE (g_object_ref (page));
2302 g_object_set_data_full (G_OBJECT (ev_field),
2304 g_object_ref (mapping->field),
2305 (GDestroyNotify) g_object_unref);
2307 retval = g_list_prepend (retval, field_mapping);
2310 poppler_page_free_form_field_mapping (fields);
2312 return retval ? ev_mapping_list_new (page->index,
2313 g_list_reverse (retval),
2314 (GDestroyNotify)g_object_unref) : NULL;
2318 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2322 PopplerFormField *poppler_field;
2325 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2329 text = poppler_form_field_text_get_text (poppler_field);
2335 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2339 PopplerFormField *poppler_field;
2341 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2345 poppler_form_field_text_set_text (poppler_field, text);
2346 PDF_DOCUMENT (document)->modified = TRUE;
2350 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2354 PopplerFormField *poppler_field;
2356 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2360 poppler_form_field_button_set_state (poppler_field, state);
2361 PDF_DOCUMENT (document)->modified = TRUE;
2365 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2368 PopplerFormField *poppler_field;
2371 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2375 state = poppler_form_field_button_get_state (poppler_field);
2381 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2385 PopplerFormField *poppler_field;
2388 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2392 text = poppler_form_field_choice_get_item (poppler_field, index);
2398 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2401 PopplerFormField *poppler_field;
2404 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2408 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2414 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2418 PopplerFormField *poppler_field;
2421 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2425 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2431 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2435 PopplerFormField *poppler_field;
2437 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2441 poppler_form_field_choice_select_item (poppler_field, index);
2442 PDF_DOCUMENT (document)->modified = TRUE;
2446 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2450 PopplerFormField *poppler_field;
2452 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2456 poppler_form_field_choice_toggle_item (poppler_field, index);
2457 PDF_DOCUMENT (document)->modified = TRUE;
2461 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2464 PopplerFormField *poppler_field;
2466 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2470 poppler_form_field_choice_unselect_all (poppler_field);
2471 PDF_DOCUMENT (document)->modified = TRUE;
2475 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2479 PopplerFormField *poppler_field;
2481 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2485 poppler_form_field_choice_set_text (poppler_field, text);
2486 PDF_DOCUMENT (document)->modified = TRUE;
2490 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2493 PopplerFormField *poppler_field;
2496 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2500 text = poppler_form_field_choice_get_text (poppler_field);
2506 pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface)
2508 iface->get_form_fields = pdf_document_forms_get_form_fields;
2509 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2510 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2511 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2512 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2513 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2514 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2515 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2516 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2517 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2518 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2519 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2520 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2525 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2528 PopplerColor *poppler_color;
2530 poppler_color = poppler_annot_get_color (poppler_annot);
2531 if (poppler_color) {
2532 color->red = poppler_color->red;
2533 color->green = poppler_color->green;
2534 color->blue = poppler_color->blue;
2536 g_free (poppler_color);
2537 } /* TODO: else use a default color */
2540 static EvAnnotation *
2541 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2544 EvAnnotation *ev_annot = NULL;
2545 const gchar *unimplemented_annot = NULL;
2547 switch (poppler_annot_get_annot_type (poppler_annot)) {
2548 case POPPLER_ANNOT_TEXT: {
2549 PopplerAnnotText *poppler_text;
2550 EvAnnotationText *ev_annot_text;
2552 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2554 ev_annot = ev_annotation_text_new (page);
2556 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2557 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2560 case POPPLER_ANNOT_FILE_ATTACHMENT: {
2561 PopplerAnnotFileAttachment *poppler_annot_attachment;
2562 EvAnnotationAttachment *ev_annot_attachment;
2563 PopplerAttachment *poppler_attachment;
2566 GError *error = NULL;
2568 poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
2569 poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
2571 if (poppler_attachment &&
2572 attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
2573 EvAttachment *ev_attachment;
2575 ev_attachment = ev_attachment_new (poppler_attachment->name,
2576 poppler_attachment->description,
2577 poppler_attachment->mtime,
2578 poppler_attachment->ctime,
2580 ev_annot = ev_annotation_attachment_new (page, ev_attachment);
2581 g_object_unref (ev_attachment);
2583 g_warning ("%s", error->message);
2584 g_error_free (error);
2587 if (poppler_attachment)
2588 g_object_unref (poppler_attachment);
2591 case POPPLER_ANNOT_LINK:
2592 case POPPLER_ANNOT_WIDGET:
2593 /* Ignore link and widgets annots since they are already handled */
2596 GEnumValue *enum_value;
2598 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2599 poppler_annot_get_annot_type (poppler_annot));
2600 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2604 if (unimplemented_annot) {
2605 g_warning ("Unimplemented annotation: %s, please post a "
2606 "bug report in Evince bugzilla "
2607 "(http://bugzilla.gnome.org) with a testcase.",
2608 unimplemented_annot);
2612 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2613 ev_annot->name = poppler_annot_get_name (poppler_annot);
2614 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2615 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2617 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2618 PopplerAnnotMarkup *markup;
2621 PopplerRectangle poppler_rect;
2623 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2625 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2626 EvRectangle ev_rect;
2630 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2632 ev_rect.x1 = poppler_rect.x1;
2633 ev_rect.x2 = poppler_rect.x2;
2634 ev_rect.y1 = height - poppler_rect.y2;
2635 ev_rect.y2 = height - poppler_rect.y1;
2637 is_open = poppler_annot_markup_get_popup_is_open (markup);
2639 g_object_set (ev_annot,
2640 "rectangle", &ev_rect,
2645 /* FIXME: Use poppler_annot_markup_has_popup() when
2646 * new poppler is released.
2648 g_object_set (ev_annot,
2653 label = poppler_annot_markup_get_label (markup);
2654 opacity = poppler_annot_markup_get_opacity (markup);
2656 g_object_set (ev_annot,
2668 static EvMappingList *
2669 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2672 GList *retval = NULL;
2673 PdfDocument *pdf_document;
2674 PopplerPage *poppler_page;
2680 pdf_document = PDF_DOCUMENT (document_annotations);
2681 poppler_page = POPPLER_PAGE (page->backend_page);
2682 annots = poppler_page_get_annot_mapping (poppler_page);
2683 poppler_page_get_size (poppler_page, NULL, &height);
2685 for (list = annots; list; list = list->next) {
2686 PopplerAnnotMapping *mapping;
2687 EvMapping *annot_mapping;
2688 EvAnnotation *ev_annot;
2690 mapping = (PopplerAnnotMapping *)list->data;
2692 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2698 /* Make sure annot has a unique name */
2699 if (!ev_annot->name)
2700 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2702 annot_mapping = g_new (EvMapping, 1);
2703 annot_mapping->area.x1 = mapping->area.x1;
2704 annot_mapping->area.x2 = mapping->area.x2;
2705 annot_mapping->area.y1 = height - mapping->area.y2;
2706 annot_mapping->area.y2 = height - mapping->area.y1;
2707 annot_mapping->data = ev_annot;
2709 g_object_set_data_full (G_OBJECT (ev_annot),
2711 g_object_ref (mapping->annot),
2712 (GDestroyNotify) g_object_unref);
2714 retval = g_list_prepend (retval, annot_mapping);
2717 poppler_page_free_annot_mapping (annots);
2719 return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
2723 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2724 EvAnnotation *annot,
2725 const gchar *contents)
2727 PopplerAnnot *poppler_annot;
2729 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2733 poppler_annot_set_contents (poppler_annot, contents);
2734 PDF_DOCUMENT (document)->modified = TRUE;
2738 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface)
2740 iface->get_annotations = pdf_document_annotations_get_annotations;
2741 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2745 struct SaveToBufferData {
2751 attachment_save_to_buffer_callback (const gchar *buf,
2756 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2760 if (sdata->len + count > sdata->max) {
2761 new_max = MAX (sdata->max * 2, sdata->len + count);
2762 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2764 sdata->buffer = new_buffer;
2765 sdata->max = new_max;
2768 memcpy (sdata->buffer + sdata->len, buf, count);
2769 sdata->len += count;
2775 attachment_save_to_buffer (PopplerAttachment *attachment,
2780 static const gint initial_max = 1024;
2781 struct SaveToBufferData sdata;
2786 sdata.buffer = (gchar *) g_malloc (initial_max);
2787 sdata.max = initial_max;
2790 if (! poppler_attachment_save_to_callback (attachment,
2791 attachment_save_to_buffer_callback,
2794 g_free (sdata.buffer);
2798 *buffer = sdata.buffer;
2799 *buffer_size = sdata.len;
2805 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2807 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2810 GList *retval = NULL;
2812 attachments = poppler_document_get_attachments (pdf_document->document);
2814 for (list = attachments; list; list = list->next) {
2815 PopplerAttachment *attachment;
2816 EvAttachment *ev_attachment;
2819 GError *error = NULL;
2821 attachment = (PopplerAttachment *) list->data;
2823 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2824 ev_attachment = ev_attachment_new (attachment->name,
2825 attachment->description,
2830 retval = g_list_prepend (retval, ev_attachment);
2833 g_warning ("%s", error->message);
2834 g_error_free (error);
2840 g_object_unref (attachment);
2843 return g_list_reverse (retval);
2847 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2849 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2851 return poppler_document_has_attachments (pdf_document->document);
2855 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface)
2857 iface->has_attachments = pdf_document_attachments_has_attachments;
2858 iface->get_attachments = pdf_document_attachments_get_attachments;
2863 pdf_document_layers_has_layers (EvDocumentLayers *document)
2865 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2866 PopplerLayersIter *iter;
2868 iter = poppler_layers_iter_new (pdf_document->document);
2871 poppler_layers_iter_free (iter);
2877 build_layers_tree (PdfDocument *pdf_document,
2878 GtkTreeModel *model,
2879 GtkTreeIter *parent,
2880 PopplerLayersIter *iter)
2883 GtkTreeIter tree_iter;
2884 PopplerLayersIter *child;
2885 PopplerLayer *layer;
2886 EvLayer *ev_layer = NULL;
2891 layer = poppler_layers_iter_get_layer (iter);
2893 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2894 visible = poppler_layer_is_visible (layer);
2895 rb_group = poppler_layer_get_radio_button_group_id (layer);
2896 pdf_document->layers = g_list_append (pdf_document->layers,
2897 g_object_ref (layer));
2898 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2899 poppler_layer_is_parent (layer),
2904 title = poppler_layers_iter_get_title (iter);
2905 markup = g_markup_escape_text (title, -1);
2912 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2913 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2914 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2915 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2916 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2917 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2918 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2919 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2922 g_object_unref (ev_layer);
2925 child = poppler_layers_iter_get_child (iter);
2927 build_layers_tree (pdf_document, model, &tree_iter, child);
2928 poppler_layers_iter_free (child);
2929 } while (poppler_layers_iter_next (iter));
2932 static GtkTreeModel *
2933 pdf_document_layers_get_layers (EvDocumentLayers *document)
2935 GtkTreeModel *model = NULL;
2936 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2937 PopplerLayersIter *iter;
2939 iter = poppler_layers_iter_new (pdf_document->document);
2941 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2942 G_TYPE_STRING, /* TITLE */
2943 G_TYPE_OBJECT, /* LAYER */
2944 G_TYPE_BOOLEAN, /* VISIBLE */
2945 G_TYPE_BOOLEAN, /* ENABLED */
2946 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2947 G_TYPE_INT); /* RBGROUP */
2948 build_layers_tree (pdf_document, model, NULL, iter);
2949 poppler_layers_iter_free (iter);
2955 pdf_document_layers_show_layer (EvDocumentLayers *document,
2958 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2959 guint layer_id = ev_layer_get_id (layer);
2961 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2965 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2968 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2969 guint layer_id = ev_layer_get_id (layer);
2971 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2975 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2978 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2979 guint layer_id = ev_layer_get_id (layer);
2981 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2985 pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface)
2987 iface->has_layers = pdf_document_layers_has_layers;
2988 iface->get_layers = pdf_document_layers_get_layers;
2989 iface->show_layer = pdf_document_layers_show_layer;
2990 iface->hide_layer = pdf_document_layers_hide_layer;
2991 iface->layer_is_visible = pdf_document_layers_layer_is_visible;