1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
4 * Copyright (C) 2009, Juanjo MarĂn <juanj.marin@juntadeandalucia.es>
5 * Copyright (C) 2004, Red Hat, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <poppler-document.h>
29 #include <poppler-page.h>
31 #include <cairo-pdf.h>
36 #include <glib/gi18n-lib.h>
38 #include "ev-poppler.h"
39 #include "ev-file-exporter.h"
40 #include "ev-mapping.h"
41 #include "ev-document-find.h"
42 #include "ev-document-misc.h"
43 #include "ev-document-links.h"
44 #include "ev-document-images.h"
45 #include "ev-document-fonts.h"
46 #include "ev-document-security.h"
47 #include "ev-document-thumbnails.h"
48 #include "ev-document-transition.h"
49 #include "ev-document-forms.h"
50 #include "ev-document-layers.h"
51 #include "ev-document-print.h"
52 #include "ev-document-annotations.h"
53 #include "ev-document-attachments.h"
54 #include "ev-selection.h"
55 #include "ev-transition-effect.h"
56 #include "ev-attachment.h"
59 #include <libxml/tree.h>
60 #include <libxml/parser.h>
61 #include <libxml/xpath.h>
62 #include <libxml/xpathInternals.h>
64 #if (defined (HAVE_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_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
133 static int pdf_document_get_n_pages (EvDocument *document);
135 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
137 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
138 PopplerAction *action);
139 static void pdf_print_context_free (PdfPrintContext *ctx);
140 static gboolean attachment_save_to_buffer (PopplerAttachment *attachment,
145 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
147 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
148 pdf_document_security_iface_init);
149 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
150 pdf_document_document_thumbnails_iface_init);
151 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
152 pdf_document_document_links_iface_init);
153 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
154 pdf_document_document_images_iface_init);
155 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
156 pdf_document_document_forms_iface_init);
157 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
158 pdf_document_document_fonts_iface_init);
159 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
160 pdf_document_document_layers_iface_init);
161 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
162 pdf_document_document_print_iface_init);
163 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
164 pdf_document_document_annotations_iface_init);
165 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
166 pdf_document_document_attachments_iface_init);
167 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
168 pdf_document_find_iface_init);
169 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
170 pdf_document_file_exporter_iface_init);
171 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
172 pdf_selection_iface_init);
173 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
174 pdf_document_page_transition_iface_init);
178 pdf_document_dispose (GObject *object)
180 PdfDocument *pdf_document = PDF_DOCUMENT(object);
182 if (pdf_document->print_ctx) {
183 pdf_print_context_free (pdf_document->print_ctx);
184 pdf_document->print_ctx = NULL;
187 if (pdf_document->document) {
188 g_object_unref (pdf_document->document);
191 if (pdf_document->font_info) {
192 poppler_font_info_free (pdf_document->font_info);
195 if (pdf_document->fonts_iter) {
196 poppler_fonts_iter_free (pdf_document->fonts_iter);
199 if (pdf_document->layers) {
200 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
201 g_list_free (pdf_document->layers);
204 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
208 pdf_document_init (PdfDocument *pdf_document)
210 pdf_document->password = NULL;
214 convert_error (GError *poppler_error,
217 if (poppler_error == NULL)
220 if (poppler_error->domain == POPPLER_ERROR) {
221 /* convert poppler errors into EvDocument errors */
222 gint code = EV_DOCUMENT_ERROR_INVALID;
223 if (poppler_error->code == POPPLER_ERROR_INVALID)
224 code = EV_DOCUMENT_ERROR_INVALID;
225 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
226 code = EV_DOCUMENT_ERROR_ENCRYPTED;
228 g_set_error_literal (error,
231 poppler_error->message);
233 g_error_free (poppler_error);
235 g_propagate_error (error, poppler_error);
242 pdf_document_save (EvDocument *document,
246 PdfDocument *pdf_document = PDF_DOCUMENT (document);
248 GError *poppler_error = NULL;
250 if (pdf_document->modified) {
251 retval = poppler_document_save (pdf_document->document,
252 uri, &poppler_error);
254 retval = poppler_document_save_a_copy (pdf_document->document,
255 uri, &poppler_error);
259 convert_error (poppler_error, error);
265 pdf_document_load (EvDocument *document,
269 GError *poppler_error = NULL;
270 PdfDocument *pdf_document = PDF_DOCUMENT (document);
272 pdf_document->document =
273 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
275 if (pdf_document->document == NULL) {
276 convert_error (poppler_error, error);
284 pdf_document_get_n_pages (EvDocument *document)
286 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
290 pdf_document_get_page (EvDocument *document,
293 PdfDocument *pdf_document = PDF_DOCUMENT (document);
294 PopplerPage *poppler_page;
297 poppler_page = poppler_document_get_page (pdf_document->document, index);
298 page = ev_page_new (index);
299 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
300 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
301 g_object_unref (poppler_page);
307 pdf_document_get_page_size (EvDocument *document,
312 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
314 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
318 pdf_document_get_page_label (EvDocument *document,
323 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
325 g_object_get (G_OBJECT (page->backend_page),
331 static cairo_surface_t *
332 pdf_page_render (PopplerPage *page,
337 cairo_surface_t *surface;
340 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
342 cr = cairo_create (surface);
344 switch (rc->rotation) {
346 cairo_translate (cr, width, 0);
349 cairo_translate (cr, width, height);
352 cairo_translate (cr, 0, height);
355 cairo_translate (cr, 0, 0);
357 cairo_scale (cr, rc->scale, rc->scale);
358 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
359 poppler_page_render (page, cr);
361 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
362 cairo_set_source_rgb (cr, 1., 1., 1.);
370 static cairo_surface_t *
371 pdf_document_render (EvDocument *document,
374 PdfDocument *pdf_document;
375 PopplerPage *poppler_page;
376 double width_points, height_points;
379 poppler_page = POPPLER_PAGE (rc->page->backend_page);
381 poppler_page_get_size (poppler_page,
382 &width_points, &height_points);
384 if (rc->rotation == 90 || rc->rotation == 270) {
385 width = (int) ((height_points * rc->scale) + 0.5);
386 height = (int) ((width_points * rc->scale) + 0.5);
388 width = (int) ((width_points * rc->scale) + 0.5);
389 height = (int) ((height_points * rc->scale) + 0.5);
392 return pdf_page_render (poppler_page,
397 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 */
399 pdf_document_get_format_from_metadata (xmlDocPtr doc,
400 xmlXPathContextPtr xpathCtx)
402 xmlXPathObjectPtr xpathObj;
403 xmlChar *part = NULL;
404 xmlChar *conf = NULL;
408 /* add pdf/a namespaces */
409 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
410 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
411 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
413 /* reads pdf/a part */
414 /* first syntax: child node */
415 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
416 if (xpathObj != NULL) {
417 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
418 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
420 xmlXPathFreeObject (xpathObj);
423 /* second syntax: attribute */
424 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
425 if (xpathObj != NULL) {
426 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
427 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
429 xmlXPathFreeObject (xpathObj);
433 /* reads pdf/a conformance */
434 /* first syntax: child node */
435 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
436 if (xpathObj != NULL) {
437 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
438 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
440 xmlXPathFreeObject (xpathObj);
443 /* second syntax: attribute */
444 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
445 if (xpathObj != NULL) {
446 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
447 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
449 xmlXPathFreeObject (xpathObj);
453 if (part != NULL && conf != NULL) {
454 /* makes conf lowercase */
455 for (i = 0; conf[i]; i++)
456 conf[i] = g_ascii_tolower (conf[i]);
459 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
469 static EvDocumentLicense *
470 pdf_document_get_license_from_metadata (xmlDocPtr doc,
471 xmlXPathContextPtr xpathCtx)
473 xmlXPathObjectPtr xpathObj;
474 xmlChar *marked = NULL;
475 const char *language_string;
478 gchar *tag, *tag_aux;
480 EvDocumentLicense *license;
482 /* register namespaces */
483 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
484 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
485 xmlXPathRegisterNs (xpathCtx, BAD_CAST "dc", BAD_CAST "http://purl.org/dc/elements/1.1/");
486 /* XMP Rights Management Schema */
487 xmlXPathRegisterNs (xpathCtx, BAD_CAST "xmpRights", BAD_CAST "http://ns.adobe.com/xap/1.0/rights/");
488 /* Creative Commons Schema */
489 xmlXPathRegisterNs (xpathCtx, BAD_CAST "cc", BAD_CAST "http://creativecommons.org/ns#");
491 /* checking if the document has been marked as defined on the XMP Rights
492 * Management Schema */
493 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_MARKED, xpathCtx);
494 if (xpathObj != NULL) {
495 if (xpathObj->nodesetval != NULL &&
496 xpathObj->nodesetval->nodeNr != 0)
497 marked = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
498 xmlXPathFreeObject (xpathObj);
501 /* a) Not marked => No XMP Rights information */
507 license = ev_document_license_new ();
509 /* b) Marked False => Public Domain, no copyrighted material and no
511 if (g_strrstr ((char *) marked, "False") != NULL) {
512 license->text = g_strdup (_("This work is in the Public Domain"));
513 /* c) Marked True => Copyrighted material */
515 /* Checking usage terms as defined by the XMP Rights Management
516 * Schema. This field is recomended to be checked by Creative
518 /* 1) checking for a suitable localized string */
519 language_string = pango_language_to_string (gtk_get_default_language ());
520 tags = g_strsplit (language_string, "-", -1);
521 i = g_strv_length (tags);
522 while (i-- && !license->text) {
523 tag = g_strdup (tags[0]);
524 for (j = 1; j <= i; j++) {
525 tag_aux = g_strdup_printf ("%s-%s", tag, tags[j]);
529 aux = g_strdup_printf (LICENSE_TEXT, tag);
530 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
531 if (xpathObj != NULL) {
532 if (xpathObj->nodesetval != NULL &&
533 xpathObj->nodesetval->nodeNr != 0)
534 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
535 xmlXPathFreeObject (xpathObj);
542 /* 2) if not, use the default string */
543 if (!license->text) {
544 aux = g_strdup_printf (LICENSE_TEXT, "x-default");
545 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
546 if (xpathObj != NULL) {
547 if (xpathObj->nodesetval != NULL &&
548 xpathObj->nodesetval->nodeNr != 0)
549 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
550 xmlXPathFreeObject (xpathObj);
555 /* Checking the license URI as defined by the Creative Commons
556 * Schema. This field is recomended to be checked by Creative
558 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_URI, xpathCtx);
559 if (xpathObj != NULL) {
560 if (xpathObj->nodesetval != NULL &&
561 xpathObj->nodesetval->nodeNr != 0)
562 license->uri = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
563 xmlXPathFreeObject (xpathObj);
566 /* Checking the web statement as defined by the XMP Rights
567 * Management Schema. Checking it out is a sort of above-and-beyond
568 * the basic recommendations by Creative Commons. It can be
569 * considered as a "reinforcement" approach to add certainty. */
570 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_WEB_STATEMENT, xpathCtx);
571 if (xpathObj != NULL) {
572 if (xpathObj->nodesetval != NULL &&
573 xpathObj->nodesetval->nodeNr != 0)
574 license->web_statement = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
575 xmlXPathFreeObject (xpathObj);
580 if (!license->text && !license->uri && !license->web_statement) {
581 ev_document_license_free (license);
589 pdf_document_parse_metadata (const gchar *metadata,
590 EvDocumentInfo *info)
593 xmlXPathContextPtr xpathCtx;
596 doc = xmlParseMemory (metadata, strlen (metadata));
598 return; /* invalid xml metadata */
600 xpathCtx = xmlXPathNewContext (doc);
601 if (xpathCtx == NULL) {
603 return; /* invalid xpath context */
606 fmt = pdf_document_get_format_from_metadata (doc, xpathCtx);
608 g_free (info->format);
612 info->license = pdf_document_get_license_from_metadata (doc, xpathCtx);
614 xmlXPathFreeContext (xpathCtx);
619 static EvDocumentInfo *
620 pdf_document_get_info (EvDocument *document)
622 EvDocumentInfo *info;
623 PopplerPageLayout layout;
624 PopplerPageMode mode;
625 PopplerViewerPreferences view_prefs;
626 PopplerPermissions permissions;
630 info = g_new0 (EvDocumentInfo, 1);
632 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
633 EV_DOCUMENT_INFO_FORMAT |
634 EV_DOCUMENT_INFO_AUTHOR |
635 EV_DOCUMENT_INFO_SUBJECT |
636 EV_DOCUMENT_INFO_KEYWORDS |
637 EV_DOCUMENT_INFO_LAYOUT |
638 EV_DOCUMENT_INFO_START_MODE |
639 EV_DOCUMENT_INFO_PERMISSIONS |
640 EV_DOCUMENT_INFO_UI_HINTS |
641 EV_DOCUMENT_INFO_CREATOR |
642 EV_DOCUMENT_INFO_PRODUCER |
643 EV_DOCUMENT_INFO_CREATION_DATE |
644 EV_DOCUMENT_INFO_MOD_DATE |
645 EV_DOCUMENT_INFO_LINEARIZED |
646 EV_DOCUMENT_INFO_N_PAGES |
647 EV_DOCUMENT_INFO_SECURITY |
648 EV_DOCUMENT_INFO_PAPER_SIZE |
649 EV_DOCUMENT_INFO_LICENSE;
651 g_object_get (PDF_DOCUMENT (document)->document,
652 "title", &(info->title),
653 "format", &(info->format),
654 "author", &(info->author),
655 "subject", &(info->subject),
656 "keywords", &(info->keywords),
658 "page-layout", &layout,
659 "viewer-preferences", &view_prefs,
660 "permissions", &permissions,
661 "creator", &(info->creator),
662 "producer", &(info->producer),
663 "creation-date", &(info->creation_date),
664 "mod-date", &(info->modified_date),
665 "linearized", &(info->linearized),
666 "metadata", &metadata,
669 if (metadata != NULL) {
670 pdf_document_parse_metadata (metadata, info);
674 info->n_pages = ev_document_get_n_pages (document);
676 if (info->n_pages > 0) {
677 ev_document_get_page_size (document, 0,
678 &(info->paper_width),
679 &(info->paper_height));
681 info->paper_width = info->paper_width / 72.0f * 25.4f;
682 info->paper_height = info->paper_height / 72.0f * 25.4f;
686 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
687 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
689 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
690 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
692 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
693 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
695 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
696 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
697 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
698 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
700 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
701 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
708 case POPPLER_PAGE_MODE_NONE:
709 info->mode = EV_DOCUMENT_MODE_NONE;
711 case POPPLER_PAGE_MODE_USE_THUMBS:
712 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
714 case POPPLER_PAGE_MODE_USE_OC:
715 info->mode = EV_DOCUMENT_MODE_USE_OC;
717 case POPPLER_PAGE_MODE_FULL_SCREEN:
718 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
720 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
721 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
727 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
728 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
730 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
731 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
733 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
734 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
736 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
737 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
739 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
740 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
742 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
743 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
745 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
746 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
749 info->permissions = 0;
750 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
751 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
753 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
754 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
756 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
757 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
759 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
760 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
763 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
764 /* translators: this is the document security state */
765 info->security = g_strdup (_("Yes"));
767 /* translators: this is the document security state */
768 info->security = g_strdup (_("No"));
775 pdf_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
777 PopplerBackend backend;
779 backend = poppler_get_backend ();
781 case POPPLER_BACKEND_CAIRO:
782 info->name = "poppler/cairo";
784 case POPPLER_BACKEND_SPLASH:
785 info->name = "poppler/splash";
788 info->name = "poppler/unknown";
792 info->version = poppler_get_version ();
798 pdf_document_class_init (PdfDocumentClass *klass)
800 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
801 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
803 g_object_class->dispose = pdf_document_dispose;
805 ev_document_class->save = pdf_document_save;
806 ev_document_class->load = pdf_document_load;
807 ev_document_class->get_n_pages = pdf_document_get_n_pages;
808 ev_document_class->get_page = pdf_document_get_page;
809 ev_document_class->get_page_size = pdf_document_get_page_size;
810 ev_document_class->get_page_label = pdf_document_get_page_label;
811 ev_document_class->render = pdf_document_render;
812 ev_document_class->get_info = pdf_document_get_info;
813 ev_document_class->get_backend_info = pdf_document_get_backend_info;
816 /* EvDocumentSecurity */
818 pdf_document_has_document_security (EvDocumentSecurity *document_security)
820 /* FIXME: do we really need to have this? */
825 pdf_document_set_password (EvDocumentSecurity *document_security,
826 const char *password)
828 PdfDocument *document = PDF_DOCUMENT (document_security);
830 if (document->password)
831 g_free (document->password);
833 document->password = g_strdup (password);
837 pdf_document_security_iface_init (EvDocumentSecurityInterface *iface)
839 iface->has_document_security = pdf_document_has_document_security;
840 iface->set_password = pdf_document_set_password;
844 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
846 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
849 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
851 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
855 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
858 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
861 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
863 if (pdf_document->font_info == NULL) {
864 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
867 if (pdf_document->fonts_iter) {
868 poppler_fonts_iter_free (pdf_document->fonts_iter);
871 pdf_document->fonts_scanned_pages += n_pages;
873 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
874 &pdf_document->fonts_iter);
876 pdf_document->fonts_scanned_pages = 0;
877 poppler_font_info_free (pdf_document->font_info);
878 pdf_document->font_info = NULL;
885 font_type_to_string (PopplerFontType type)
888 case POPPLER_FONT_TYPE_TYPE1:
890 case POPPLER_FONT_TYPE_TYPE1C:
892 case POPPLER_FONT_TYPE_TYPE3:
894 case POPPLER_FONT_TYPE_TRUETYPE:
895 return _("TrueType");
896 case POPPLER_FONT_TYPE_CID_TYPE0:
897 return _("Type 1 (CID)");
898 case POPPLER_FONT_TYPE_CID_TYPE0C:
899 return _("Type 1C (CID)");
900 case POPPLER_FONT_TYPE_CID_TYPE2:
901 return _("TrueType (CID)");
903 return _("Unknown font type");
908 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
911 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
912 PopplerFontsIter *iter = pdf_document->fonts_iter;
914 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
920 GtkTreeIter list_iter;
923 const char *embedded;
926 name = poppler_fonts_iter_get_name (iter);
932 type = font_type_to_string (
933 poppler_fonts_iter_get_font_type (iter));
935 if (poppler_fonts_iter_is_embedded (iter)) {
936 if (poppler_fonts_iter_is_subset (iter))
937 embedded = _("Embedded subset");
939 embedded = _("Embedded");
941 embedded = _("Not embedded");
944 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
946 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
947 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
948 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
949 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
953 } while (poppler_fonts_iter_next (iter));
957 pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface)
959 iface->fill_model = pdf_document_fonts_fill_model;
960 iface->scan = pdf_document_fonts_scan;
961 iface->get_progress = pdf_document_fonts_get_progress;
965 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
967 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
968 PopplerIndexIter *iter;
970 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
972 iter = poppler_index_iter_new (pdf_document->document);
975 poppler_index_iter_free (iter);
981 ev_link_dest_from_dest (PdfDocument *pdf_document,
984 EvLinkDest *ev_dest = NULL;
985 const char *unimplemented_dest = NULL;
987 g_assert (dest != NULL);
989 switch (dest->type) {
990 case POPPLER_DEST_XYZ: {
991 PopplerPage *poppler_page;
994 poppler_page = poppler_document_get_page (pdf_document->document,
995 MAX (0, dest->page_num - 1));
996 poppler_page_get_size (poppler_page, NULL, &height);
997 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
999 height - MIN (height, dest->top),
1004 g_object_unref (poppler_page);
1007 case POPPLER_DEST_FITB:
1008 case POPPLER_DEST_FIT:
1009 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1011 case POPPLER_DEST_FITBH:
1012 case POPPLER_DEST_FITH: {
1013 PopplerPage *poppler_page;
1016 poppler_page = poppler_document_get_page (pdf_document->document,
1017 MAX (0, dest->page_num - 1));
1018 poppler_page_get_size (poppler_page, NULL, &height);
1019 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1020 height - MIN (height, dest->top),
1022 g_object_unref (poppler_page);
1025 case POPPLER_DEST_FITBV:
1026 case POPPLER_DEST_FITV:
1027 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1031 case POPPLER_DEST_FITR: {
1032 PopplerPage *poppler_page;
1035 poppler_page = poppler_document_get_page (pdf_document->document,
1036 MAX (0, dest->page_num - 1));
1037 poppler_page_get_size (poppler_page, NULL, &height);
1038 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1040 height - MIN (height, dest->bottom),
1042 height - MIN (height, dest->top));
1043 g_object_unref (poppler_page);
1046 case POPPLER_DEST_NAMED:
1047 ev_dest = ev_link_dest_new_named (dest->named_dest);
1049 case POPPLER_DEST_UNKNOWN:
1050 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1054 if (unimplemented_dest) {
1055 g_warning ("Unimplemented destination: %s, please post a "
1056 "bug report in Evince bugzilla "
1057 "(http://bugzilla.gnome.org) with a testcase.",
1058 unimplemented_dest);
1062 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1068 ev_link_from_action (PdfDocument *pdf_document,
1069 PopplerAction *action)
1071 EvLink *link = NULL;
1072 EvLinkAction *ev_action = NULL;
1073 const char *unimplemented_action = NULL;
1075 switch (action->type) {
1076 case POPPLER_ACTION_NONE:
1078 case POPPLER_ACTION_GOTO_DEST: {
1081 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1082 ev_action = ev_link_action_new_dest (dest);
1085 case POPPLER_ACTION_GOTO_REMOTE: {
1088 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1089 ev_action = ev_link_action_new_remote (dest,
1090 action->goto_remote.file_name);
1094 case POPPLER_ACTION_LAUNCH:
1095 ev_action = ev_link_action_new_launch (action->launch.file_name,
1096 action->launch.params);
1098 case POPPLER_ACTION_URI:
1099 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1101 case POPPLER_ACTION_NAMED:
1102 ev_action = ev_link_action_new_named (action->named.named_dest);
1104 case POPPLER_ACTION_MOVIE:
1105 unimplemented_action = "POPPLER_ACTION_MOVIE";
1107 #if POPPLER_CHECK_VERSION (0, 13, 2)
1108 case POPPLER_ACTION_RENDITION:
1109 unimplemented_action = "POPPLER_ACTION_RENDITION";
1111 case POPPLER_ACTION_OCG_STATE:
1112 unimplemented_action = "POPPLER_ACTION_OCG_STATE";
1115 case POPPLER_ACTION_UNKNOWN:
1116 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1119 if (unimplemented_action) {
1120 g_warning ("Unimplemented action: %s, please post a bug report "
1121 "in Evince bugzilla (http://bugzilla.gnome.org) "
1122 "with a testcase.", unimplemented_action);
1125 link = ev_link_new (action->any.title, ev_action);
1131 build_tree (PdfDocument *pdf_document,
1132 GtkTreeModel *model,
1133 GtkTreeIter *parent,
1134 PopplerIndexIter *iter)
1138 GtkTreeIter tree_iter;
1139 PopplerIndexIter *child;
1140 PopplerAction *action;
1141 EvLink *link = NULL;
1145 action = poppler_index_iter_get_action (iter);
1146 expand = poppler_index_iter_is_open (iter);
1151 switch (action->type) {
1152 case POPPLER_ACTION_GOTO_DEST: {
1153 /* For bookmarks, solve named destinations */
1154 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1156 EvLinkDest *ev_dest = NULL;
1157 EvLinkAction *ev_action;
1159 dest = poppler_document_find_dest (pdf_document->document,
1160 action->goto_dest.dest->named_dest);
1162 link = ev_link_from_action (pdf_document, action);
1166 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1167 poppler_dest_free (dest);
1169 ev_action = ev_link_action_new_dest (ev_dest);
1170 link = ev_link_new (action->any.title, ev_action);
1172 link = ev_link_from_action (pdf_document, action);
1177 link = ev_link_from_action (pdf_document, action);
1181 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1182 poppler_action_free (action);
1184 g_object_unref (link);
1189 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1190 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1192 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1193 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1194 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1195 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1198 g_free (title_markup);
1199 g_object_unref (link);
1201 child = poppler_index_iter_get_child (iter);
1203 build_tree (pdf_document, model, &tree_iter, child);
1204 poppler_index_iter_free (child);
1205 poppler_action_free (action);
1207 } while (poppler_index_iter_next (iter));
1210 static GtkTreeModel *
1211 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1213 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1214 GtkTreeModel *model = NULL;
1215 PopplerIndexIter *iter;
1217 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1219 iter = poppler_index_iter_new (pdf_document->document);
1220 /* Create the model if we have items*/
1222 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1227 build_tree (pdf_document, model, NULL, iter);
1228 poppler_index_iter_free (iter);
1235 pdf_document_links_get_links (EvDocumentLinks *document_links,
1238 PdfDocument *pdf_document;
1239 PopplerPage *poppler_page;
1240 GList *retval = NULL;
1241 GList *mapping_list;
1245 pdf_document = PDF_DOCUMENT (document_links);
1246 poppler_page = POPPLER_PAGE (page->backend_page);
1247 mapping_list = poppler_page_get_link_mapping (poppler_page);
1248 poppler_page_get_size (poppler_page, NULL, &height);
1250 for (list = mapping_list; list; list = list->next) {
1251 PopplerLinkMapping *link_mapping;
1252 EvMapping *ev_link_mapping;
1254 link_mapping = (PopplerLinkMapping *)list->data;
1255 ev_link_mapping = g_new (EvMapping, 1);
1256 ev_link_mapping->data = ev_link_from_action (pdf_document,
1257 link_mapping->action);
1258 ev_link_mapping->area.x1 = link_mapping->area.x1;
1259 ev_link_mapping->area.x2 = link_mapping->area.x2;
1260 /* Invert this for X-style coordinates */
1261 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1262 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1264 retval = g_list_prepend (retval, ev_link_mapping);
1267 poppler_page_free_link_mapping (mapping_list);
1269 return g_list_reverse (retval);
1273 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1274 const gchar *link_name)
1276 PdfDocument *pdf_document;
1278 EvLinkDest *ev_dest = NULL;
1280 pdf_document = PDF_DOCUMENT (document_links);
1281 dest = poppler_document_find_dest (pdf_document->document,
1284 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1285 poppler_dest_free (dest);
1292 pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface)
1294 iface->has_document_links = pdf_document_links_has_document_links;
1295 iface->get_links_model = pdf_document_links_get_links_model;
1296 iface->get_links = pdf_document_links_get_links;
1297 iface->find_link_dest = pdf_document_links_find_link_dest;
1301 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1304 GList *retval = NULL;
1305 PdfDocument *pdf_document;
1306 PopplerPage *poppler_page;
1307 GList *mapping_list;
1310 pdf_document = PDF_DOCUMENT (document_images);
1311 poppler_page = POPPLER_PAGE (page->backend_page);
1312 mapping_list = poppler_page_get_image_mapping (poppler_page);
1314 for (list = mapping_list; list; list = list->next) {
1315 PopplerImageMapping *image_mapping;
1316 EvMapping *ev_image_mapping;
1318 image_mapping = (PopplerImageMapping *)list->data;
1320 ev_image_mapping = g_new (EvMapping, 1);
1322 ev_image_mapping->data = ev_image_new (page->index, image_mapping->image_id);
1323 ev_image_mapping->area.x1 = image_mapping->area.x1;
1324 ev_image_mapping->area.y1 = image_mapping->area.y1;
1325 ev_image_mapping->area.x2 = image_mapping->area.x2;
1326 ev_image_mapping->area.y2 = image_mapping->area.y2;
1328 retval = g_list_prepend (retval, ev_image_mapping);
1331 poppler_page_free_image_mapping (mapping_list);
1333 return g_list_reverse (retval);
1337 pdf_document_images_get_image (EvDocumentImages *document_images,
1340 GdkPixbuf *retval = NULL;
1341 PdfDocument *pdf_document;
1342 PopplerPage *poppler_page;
1343 cairo_surface_t *surface;
1345 pdf_document = PDF_DOCUMENT (document_images);
1346 poppler_page = poppler_document_get_page (pdf_document->document,
1347 ev_image_get_page (image));
1349 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1351 retval = ev_document_misc_pixbuf_from_surface (surface);
1352 cairo_surface_destroy (surface);
1355 g_object_unref (poppler_page);
1361 pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface)
1363 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1364 iface->get_image = pdf_document_images_get_image;
1368 make_thumbnail_for_page (PopplerPage *poppler_page,
1369 EvRenderContext *rc,
1375 #ifdef POPPLER_WITH_GDK
1376 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1378 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1380 ev_document_fc_mutex_lock ();
1381 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1383 rc->scale, rc->rotation, pixbuf);
1384 ev_document_fc_mutex_unlock ();
1386 cairo_surface_t *surface;
1388 ev_document_fc_mutex_lock ();
1389 surface = pdf_page_render (poppler_page, width, height, rc);
1390 ev_document_fc_mutex_unlock ();
1392 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1393 cairo_surface_destroy (surface);
1394 #endif /* POPPLER_WITH_GDK */
1400 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1401 EvRenderContext *rc,
1404 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1405 PopplerPage *poppler_page;
1406 GdkPixbuf *pixbuf = NULL;
1407 GdkPixbuf *border_pixbuf;
1410 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1412 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1413 rc, &width, &height);
1415 #ifdef POPPLER_WITH_GDK
1416 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1418 cairo_surface_t *surface;
1420 surface = poppler_page_get_thumbnail (poppler_page);
1422 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1423 cairo_surface_destroy (surface);
1425 #endif /* POPPLER_WITH_GDK */
1427 if (pixbuf != NULL) {
1428 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1429 gdk_pixbuf_get_height (pixbuf) :
1430 gdk_pixbuf_get_width (pixbuf);
1432 if (thumb_width == width) {
1433 GdkPixbuf *rotated_pixbuf;
1435 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1436 (GdkPixbufRotation) (360 - rc->rotation));
1437 g_object_unref (pixbuf);
1438 pixbuf = rotated_pixbuf;
1440 /* The provided thumbnail has a different size */
1441 g_object_unref (pixbuf);
1442 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1445 /* There is no provided thumbnail. We need to make one. */
1446 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1449 if (border && pixbuf) {
1450 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1451 g_object_unref (pixbuf);
1452 pixbuf = border_pixbuf;
1459 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1460 EvRenderContext *rc,
1464 double page_width, page_height;
1466 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1467 &page_width, &page_height);
1469 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1470 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1472 if (rc->rotation == 90 || rc->rotation == 270) {
1482 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
1484 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1485 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1490 pdf_document_find_find_text (EvDocumentFind *document_find,
1493 gboolean case_sensitive)
1496 PopplerPage *poppler_page;
1498 GList *retval = NULL;
1500 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1501 g_return_val_if_fail (text != NULL, NULL);
1503 poppler_page = POPPLER_PAGE (page->backend_page);
1505 matches = poppler_page_find_text (poppler_page, text);
1509 poppler_page_get_size (poppler_page, NULL, &height);
1510 for (l = matches; l && l->data; l = g_list_next (l)) {
1511 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1512 EvRectangle *ev_rect;
1514 ev_rect = ev_rectangle_new ();
1515 ev_rect->x1 = rect->x1;
1516 ev_rect->x2 = rect->x2;
1517 /* Invert this for X-style coordinates */
1518 ev_rect->y1 = height - rect->y2;
1519 ev_rect->y2 = height - rect->y1;
1521 retval = g_list_prepend (retval, ev_rect);
1524 g_list_foreach (matches, (GFunc)poppler_rectangle_free, NULL);
1525 g_list_free (matches);
1527 return g_list_reverse (retval);
1531 pdf_document_find_iface_init (EvDocumentFindInterface *iface)
1533 iface->find_text = pdf_document_find_find_text;
1537 pdf_print_context_free (PdfPrintContext *ctx)
1542 #ifdef HAVE_CAIRO_PRINT
1544 cairo_destroy (ctx->cr);
1549 poppler_ps_file_free (ctx->ps_file);
1550 ctx->ps_file = NULL;
1557 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1558 EvFileExporterContext *fc)
1560 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1561 PdfPrintContext *ctx;
1562 #ifdef HAVE_CAIRO_PRINT
1563 gdouble width, height;
1564 cairo_surface_t *surface = NULL;
1567 if (pdf_document->print_ctx)
1568 pdf_print_context_free (pdf_document->print_ctx);
1569 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1570 ctx = pdf_document->print_ctx;
1571 ctx->format = fc->format;
1573 #ifdef HAVE_CAIRO_PRINT
1574 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1576 ctx->paper_width = fc->paper_width;
1577 ctx->paper_height = fc->paper_height;
1579 switch (fc->pages_per_sheet) {
1607 ctx->pages_printed = 0;
1609 switch (fc->format) {
1610 case EV_FILE_FORMAT_PS:
1611 #ifdef HAVE_CAIRO_PS
1612 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1615 case EV_FILE_FORMAT_PDF:
1616 #ifdef HAVE_CAIRO_PDF
1617 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1621 g_assert_not_reached ();
1624 ctx->cr = cairo_create (surface);
1625 cairo_surface_destroy (surface);
1627 #else /* HAVE_CAIRO_PRINT */
1628 if (ctx->format == EV_FILE_FORMAT_PS) {
1629 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1630 fc->filename, fc->first_page,
1631 fc->last_page - fc->first_page + 1);
1632 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1633 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1635 #endif /* HAVE_CAIRO_PRINT */
1639 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1641 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1642 PdfPrintContext *ctx = pdf_document->print_ctx;
1644 g_return_if_fail (pdf_document->print_ctx != NULL);
1646 ctx->pages_printed = 0;
1648 #ifdef HAVE_CAIRO_PRINT
1649 if (ctx->paper_width > ctx->paper_height) {
1650 if (ctx->format == EV_FILE_FORMAT_PS) {
1651 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1654 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1655 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1660 #endif /* HAVE_CAIRO_PRINT */
1664 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1665 EvRenderContext *rc)
1667 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1668 PdfPrintContext *ctx = pdf_document->print_ctx;
1669 PopplerPage *poppler_page;
1670 #ifdef HAVE_CAIRO_PRINT
1671 gdouble page_width, page_height;
1674 gdouble width, height;
1675 gdouble pwidth, pheight;
1676 gdouble xscale, yscale;
1679 g_return_if_fail (pdf_document->print_ctx != NULL);
1681 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1683 #ifdef HAVE_CAIRO_PRINT
1684 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1685 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1686 poppler_page_get_size (poppler_page, &page_width, &page_height);
1688 if (page_width > page_height && page_width > ctx->paper_width) {
1694 /* Use always portrait mode and rotate when necessary */
1695 if (ctx->paper_width > ctx->paper_height) {
1696 width = ctx->paper_height;
1697 height = ctx->paper_width;
1700 width = ctx->paper_width;
1701 height = ctx->paper_height;
1704 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1717 page_width = page_height;
1721 pwidth = width / ctx->pages_x;
1722 pheight = height / ctx->pages_y;
1724 if ((page_width > pwidth || page_height > pheight) ||
1725 (page_width < pwidth && page_height < pheight)) {
1726 xscale = pwidth / page_width;
1727 yscale = pheight / page_height;
1729 if (yscale < xscale) {
1736 xscale = yscale = 1;
1741 cairo_save (ctx->cr);
1743 cairo_matrix_t matrix;
1745 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1746 cairo_matrix_init (&matrix,
1750 cairo_transform (ctx->cr, &matrix);
1753 cairo_translate (ctx->cr,
1754 x * (rotate ? pheight : pwidth),
1755 y * (rotate ? pwidth : pheight));
1756 cairo_scale (ctx->cr, xscale, yscale);
1758 poppler_page_render_for_printing (poppler_page, ctx->cr);
1760 ctx->pages_printed++;
1762 cairo_restore (ctx->cr);
1763 #else /* HAVE_CAIRO_PRINT */
1764 if (ctx->format == EV_FILE_FORMAT_PS)
1765 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1766 #endif /* HAVE_CAIRO_PRINT */
1770 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1772 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1773 PdfPrintContext *ctx = pdf_document->print_ctx;
1775 g_return_if_fail (pdf_document->print_ctx != NULL);
1777 #ifdef HAVE_CAIRO_PRINT
1778 cairo_show_page (ctx->cr);
1783 pdf_document_file_exporter_end (EvFileExporter *exporter)
1785 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1787 pdf_print_context_free (pdf_document->print_ctx);
1788 pdf_document->print_ctx = NULL;
1791 static EvFileExporterCapabilities
1792 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1794 return (EvFileExporterCapabilities) (
1795 EV_FILE_EXPORTER_CAN_PAGE_SET |
1796 EV_FILE_EXPORTER_CAN_COPIES |
1797 EV_FILE_EXPORTER_CAN_COLLATE |
1798 EV_FILE_EXPORTER_CAN_REVERSE |
1799 EV_FILE_EXPORTER_CAN_SCALE |
1800 #ifdef HAVE_CAIRO_PRINT
1801 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1804 #ifdef HAVE_CAIRO_PDF
1805 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1807 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1811 pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface)
1813 iface->begin = pdf_document_file_exporter_begin;
1814 iface->begin_page = pdf_document_file_exporter_begin_page;
1815 iface->do_page = pdf_document_file_exporter_do_page;
1816 iface->end_page = pdf_document_file_exporter_end_page;
1817 iface->end = pdf_document_file_exporter_end;
1818 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1821 /* EvDocumentPrint */
1823 pdf_document_print_print_page (EvDocumentPrint *document,
1827 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1829 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1833 pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface)
1835 iface->print_page = pdf_document_print_print_page;
1839 pdf_selection_render_selection (EvSelection *selection,
1840 EvRenderContext *rc,
1841 cairo_surface_t **surface,
1842 EvRectangle *points,
1843 EvRectangle *old_points,
1844 EvSelectionStyle style,
1848 PopplerPage *poppler_page;
1850 PopplerColor text_color, base_color;
1851 double width_points, height_points;
1854 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1856 poppler_page_get_size (poppler_page,
1857 &width_points, &height_points);
1858 width = (int) ((width_points * rc->scale) + 0.5);
1859 height = (int) ((height_points * rc->scale) + 0.5);
1861 text_color.red = text->red;
1862 text_color.green = text->green;
1863 text_color.blue = text->blue;
1865 base_color.red = base->red;
1866 base_color.green = base->green;
1867 base_color.blue = base->blue;
1869 if (*surface == NULL) {
1870 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1875 cr = cairo_create (*surface);
1876 cairo_scale (cr, rc->scale, rc->scale);
1877 cairo_surface_set_device_offset (*surface, 0, 0);
1878 memset (cairo_image_surface_get_data (*surface), 0x00,
1879 cairo_image_surface_get_height (*surface) *
1880 cairo_image_surface_get_stride (*surface));
1881 poppler_page_render_selection (poppler_page,
1883 (PopplerRectangle *)points,
1884 (PopplerRectangle *)old_points,
1885 (PopplerSelectionStyle)style,
1892 pdf_selection_get_selected_text (EvSelection *selection,
1894 EvSelectionStyle style,
1895 EvRectangle *points)
1897 PopplerPage *poppler_page;
1902 poppler_page = POPPLER_PAGE (page->backend_page);
1904 poppler_page_get_size (poppler_page, NULL, &height);
1906 r.y1 = height - points->y2;
1908 r.y2 = height - points->y1;
1910 retval = poppler_page_get_text (poppler_page,
1911 (PopplerSelectionStyle)style,
1918 create_gdk_region_from_poppler_region (GList *region)
1923 retval = gdk_region_new ();
1925 for (l = region; l; l = g_list_next (l)) {
1926 PopplerRectangle *rectangle;
1929 rectangle = (PopplerRectangle *)l->data;
1931 rect.x = (gint) rectangle->x1;
1932 rect.y = (gint) rectangle->y1;
1933 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1934 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1935 gdk_region_union_with_rect (retval, &rect);
1937 poppler_rectangle_free (rectangle);
1944 pdf_selection_get_selection_region (EvSelection *selection,
1945 EvRenderContext *rc,
1946 EvSelectionStyle style,
1947 EvRectangle *points)
1949 PopplerPage *poppler_page;
1953 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1955 region = poppler_page_get_selection_region (poppler_page,
1957 (PopplerSelectionStyle)style,
1958 (PopplerRectangle *) points);
1959 retval = create_gdk_region_from_poppler_region (region);
1960 g_list_free (region);
1966 pdf_selection_get_selection_map (EvSelection *selection,
1969 PopplerPage *poppler_page;
1970 PopplerRectangle points;
1974 poppler_page = POPPLER_PAGE (page->backend_page);
1978 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1980 region = poppler_page_get_selection_region (poppler_page, 1.0,
1981 POPPLER_SELECTION_GLYPH,
1983 retval = create_gdk_region_from_poppler_region (region);
1984 g_list_free (region);
1990 pdf_selection_iface_init (EvSelectionInterface *iface)
1992 iface->render_selection = pdf_selection_render_selection;
1993 iface->get_selected_text = pdf_selection_get_selected_text;
1994 iface->get_selection_region = pdf_selection_get_selection_region;
1995 iface->get_selection_map = pdf_selection_get_selection_map;
1998 /* Page Transitions */
2000 pdf_document_get_page_duration (EvDocumentTransition *trans,
2003 PdfDocument *pdf_document;
2004 PopplerPage *poppler_page;
2005 gdouble duration = -1;
2007 pdf_document = PDF_DOCUMENT (trans);
2008 poppler_page = poppler_document_get_page (pdf_document->document, page);
2012 duration = poppler_page_get_duration (poppler_page);
2013 g_object_unref (poppler_page);
2018 static EvTransitionEffect *
2019 pdf_document_get_effect (EvDocumentTransition *trans,
2022 PdfDocument *pdf_document;
2023 PopplerPage *poppler_page;
2024 PopplerPageTransition *page_transition;
2025 EvTransitionEffect *effect;
2027 pdf_document = PDF_DOCUMENT (trans);
2028 poppler_page = poppler_document_get_page (pdf_document->document, page);
2033 page_transition = poppler_page_get_transition (poppler_page);
2035 if (!page_transition) {
2036 g_object_unref (poppler_page);
2040 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2041 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2042 "alignment", page_transition->alignment,
2043 "direction", page_transition->direction,
2044 "duration", page_transition->duration,
2045 "angle", page_transition->angle,
2046 "scale", page_transition->scale,
2047 "rectangular", page_transition->rectangular,
2050 poppler_page_transition_free (page_transition);
2051 g_object_unref (poppler_page);
2057 pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface)
2059 iface->get_page_duration = pdf_document_get_page_duration;
2060 iface->get_effect = pdf_document_get_effect;
2065 pdf_document_get_crop_box (EvDocument *document,
2069 PdfDocument *pdf_document;
2070 PopplerPage *poppler_page;
2071 PopplerRectangle poppler_rect;
2073 pdf_document = PDF_DOCUMENT (document);
2074 poppler_page = poppler_document_get_page (pdf_document->document, page);
2075 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2076 rect->x1 = poppler_rect.x1;
2077 rect->x2 = poppler_rect.x2;
2078 rect->y1 = poppler_rect.y1;
2079 rect->y2 = poppler_rect.y2;
2082 static EvFormField *
2083 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2085 EvFormField *ev_field = NULL;
2088 gboolean is_read_only;
2090 id = poppler_form_field_get_id (poppler_field);
2091 font_size = poppler_form_field_get_font_size (poppler_field);
2092 is_read_only = poppler_form_field_is_read_only (poppler_field);
2094 switch (poppler_form_field_get_field_type (poppler_field)) {
2095 case POPPLER_FORM_FIELD_TEXT: {
2096 EvFormFieldText *field_text;
2097 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2099 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2100 case POPPLER_FORM_TEXT_NORMAL:
2101 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2103 case POPPLER_FORM_TEXT_MULTILINE:
2104 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2106 case POPPLER_FORM_TEXT_FILE_SELECT:
2107 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2111 ev_field = ev_form_field_text_new (id, ev_text_type);
2112 field_text = EV_FORM_FIELD_TEXT (ev_field);
2114 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2115 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2116 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2117 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2118 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2119 field_text->text = poppler_form_field_text_get_text (poppler_field);
2123 case POPPLER_FORM_FIELD_BUTTON: {
2124 EvFormFieldButton *field_button;
2125 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2127 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2128 case POPPLER_FORM_BUTTON_PUSH:
2129 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2131 case POPPLER_FORM_BUTTON_CHECK:
2132 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2134 case POPPLER_FORM_BUTTON_RADIO:
2135 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2139 ev_field = ev_form_field_button_new (id, ev_button_type);
2140 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2142 field_button->state = poppler_form_field_button_get_state (poppler_field);
2145 case POPPLER_FORM_FIELD_CHOICE: {
2146 EvFormFieldChoice *field_choice;
2147 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2149 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2150 case POPPLER_FORM_CHOICE_COMBO:
2151 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2153 case EV_FORM_FIELD_CHOICE_LIST:
2154 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2158 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2159 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2161 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2162 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2163 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2164 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2166 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2167 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2168 if (field_choice->is_editable)
2169 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2172 case POPPLER_FORM_FIELD_SIGNATURE:
2174 ev_field = ev_form_field_signature_new (id);
2176 case POPPLER_FORM_FIELD_UNKNOWN:
2180 ev_field->font_size = font_size;
2181 ev_field->is_read_only = is_read_only;
2187 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2190 PopplerPage *poppler_page;
2191 GList *retval = NULL;
2196 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2198 poppler_page = POPPLER_PAGE (page->backend_page);
2199 fields = poppler_page_get_form_field_mapping (poppler_page);
2200 poppler_page_get_size (poppler_page, NULL, &height);
2202 for (list = fields; list; list = list->next) {
2203 PopplerFormFieldMapping *mapping;
2204 EvMapping *field_mapping;
2205 EvFormField *ev_field;
2207 mapping = (PopplerFormFieldMapping *)list->data;
2209 ev_field = ev_form_field_from_poppler_field (mapping->field);
2213 field_mapping = g_new0 (EvMapping, 1);
2214 field_mapping->area.x1 = mapping->area.x1;
2215 field_mapping->area.x2 = mapping->area.x2;
2216 field_mapping->area.y1 = height - mapping->area.y2;
2217 field_mapping->area.y2 = height - mapping->area.y1;
2218 field_mapping->data = ev_field;
2219 ev_field->page = EV_PAGE (g_object_ref (page));
2221 g_object_set_data_full (G_OBJECT (ev_field),
2223 g_object_ref (mapping->field),
2224 (GDestroyNotify) g_object_unref);
2226 retval = g_list_prepend (retval, field_mapping);
2229 poppler_page_free_form_field_mapping (fields);
2231 return g_list_reverse (retval);
2235 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2239 PopplerFormField *poppler_field;
2242 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2246 text = poppler_form_field_text_get_text (poppler_field);
2252 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2256 PopplerFormField *poppler_field;
2258 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2262 poppler_form_field_text_set_text (poppler_field, text);
2263 PDF_DOCUMENT (document)->modified = TRUE;
2267 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2271 PopplerFormField *poppler_field;
2273 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2277 poppler_form_field_button_set_state (poppler_field, state);
2278 PDF_DOCUMENT (document)->modified = TRUE;
2282 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2285 PopplerFormField *poppler_field;
2288 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2292 state = poppler_form_field_button_get_state (poppler_field);
2298 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2302 PopplerFormField *poppler_field;
2305 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2309 text = poppler_form_field_choice_get_item (poppler_field, index);
2315 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2318 PopplerFormField *poppler_field;
2321 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2325 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2331 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2335 PopplerFormField *poppler_field;
2338 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2342 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2348 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2352 PopplerFormField *poppler_field;
2354 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2358 poppler_form_field_choice_select_item (poppler_field, index);
2359 PDF_DOCUMENT (document)->modified = TRUE;
2363 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2367 PopplerFormField *poppler_field;
2369 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2373 poppler_form_field_choice_toggle_item (poppler_field, index);
2374 PDF_DOCUMENT (document)->modified = TRUE;
2378 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2381 PopplerFormField *poppler_field;
2383 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2387 poppler_form_field_choice_unselect_all (poppler_field);
2388 PDF_DOCUMENT (document)->modified = TRUE;
2392 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2396 PopplerFormField *poppler_field;
2398 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2402 poppler_form_field_choice_set_text (poppler_field, text);
2403 PDF_DOCUMENT (document)->modified = TRUE;
2407 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2410 PopplerFormField *poppler_field;
2413 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2417 text = poppler_form_field_choice_get_text (poppler_field);
2423 pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface)
2425 iface->get_form_fields = pdf_document_forms_get_form_fields;
2426 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2427 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2428 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2429 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2430 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2431 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2432 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2433 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2434 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2435 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2436 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2437 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2442 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2445 PopplerColor *poppler_color;
2447 poppler_color = poppler_annot_get_color (poppler_annot);
2448 if (poppler_color) {
2449 color->red = poppler_color->red;
2450 color->green = poppler_color->green;
2451 color->blue = poppler_color->blue;
2453 g_free (poppler_color);
2454 } /* TODO: else use a default color */
2457 static EvAnnotation *
2458 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2461 EvAnnotation *ev_annot = NULL;
2462 const gchar *unimplemented_annot = NULL;
2464 switch (poppler_annot_get_annot_type (poppler_annot)) {
2465 case POPPLER_ANNOT_TEXT: {
2466 PopplerAnnotText *poppler_text;
2467 EvAnnotationText *ev_annot_text;
2469 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2471 ev_annot = ev_annotation_text_new (page);
2473 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2474 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2477 case POPPLER_ANNOT_FILE_ATTACHMENT: {
2478 PopplerAnnotFileAttachment *poppler_annot_attachment;
2479 EvAnnotationAttachment *ev_annot_attachment;
2480 PopplerAttachment *poppler_attachment;
2483 GError *error = NULL;
2485 poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
2486 poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
2488 if (poppler_attachment &&
2489 attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
2490 EvAttachment *ev_attachment;
2492 ev_attachment = ev_attachment_new (poppler_attachment->name,
2493 poppler_attachment->description,
2494 poppler_attachment->mtime,
2495 poppler_attachment->ctime,
2497 ev_annot = ev_annotation_attachment_new (page, ev_attachment);
2498 g_object_unref (ev_attachment);
2500 g_warning ("%s", error->message);
2501 g_error_free (error);
2504 if (poppler_attachment)
2505 g_object_unref (poppler_attachment);
2508 case POPPLER_ANNOT_LINK:
2509 case POPPLER_ANNOT_WIDGET:
2510 /* Ignore link and widgets annots since they are already handled */
2513 GEnumValue *enum_value;
2515 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2516 poppler_annot_get_annot_type (poppler_annot));
2517 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2521 if (unimplemented_annot) {
2522 g_warning ("Unimplemented annotation: %s, please post a "
2523 "bug report in Evince bugzilla "
2524 "(http://bugzilla.gnome.org) with a testcase.",
2525 unimplemented_annot);
2529 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2530 ev_annot->name = poppler_annot_get_name (poppler_annot);
2531 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2532 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2534 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2535 PopplerAnnotMarkup *markup;
2538 PopplerRectangle poppler_rect;
2540 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2542 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2543 EvRectangle ev_rect;
2547 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2549 ev_rect.x1 = poppler_rect.x1;
2550 ev_rect.x2 = poppler_rect.x2;
2551 ev_rect.y1 = height - poppler_rect.y2;
2552 ev_rect.y2 = height - poppler_rect.y1;
2554 is_open = poppler_annot_markup_get_popup_is_open (markup);
2556 g_object_set (ev_annot,
2557 "rectangle", &ev_rect,
2562 /* FIXME: Use poppler_annot_markup_has_popup() when
2563 * new poppler is released.
2565 g_object_set (ev_annot,
2570 label = poppler_annot_markup_get_label (markup);
2571 opacity = poppler_annot_markup_get_opacity (markup);
2573 g_object_set (ev_annot,
2586 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2589 GList *retval = NULL;
2590 PdfDocument *pdf_document;
2591 PopplerPage *poppler_page;
2597 pdf_document = PDF_DOCUMENT (document_annotations);
2598 poppler_page = POPPLER_PAGE (page->backend_page);
2599 annots = poppler_page_get_annot_mapping (poppler_page);
2600 poppler_page_get_size (poppler_page, NULL, &height);
2602 for (list = annots; list; list = list->next) {
2603 PopplerAnnotMapping *mapping;
2604 EvMapping *annot_mapping;
2605 EvAnnotation *ev_annot;
2607 mapping = (PopplerAnnotMapping *)list->data;
2609 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2615 /* Make sure annot has a unique name */
2616 if (!ev_annot->name)
2617 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2619 annot_mapping = g_new (EvMapping, 1);
2620 annot_mapping->area.x1 = mapping->area.x1;
2621 annot_mapping->area.x2 = mapping->area.x2;
2622 annot_mapping->area.y1 = height - mapping->area.y2;
2623 annot_mapping->area.y2 = height - mapping->area.y1;
2624 annot_mapping->data = ev_annot;
2626 g_object_set_data_full (G_OBJECT (ev_annot),
2628 g_object_ref (mapping->annot),
2629 (GDestroyNotify) g_object_unref);
2631 retval = g_list_prepend (retval, annot_mapping);
2634 poppler_page_free_annot_mapping (annots);
2636 return g_list_reverse (retval);
2640 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2641 EvAnnotation *annot,
2642 const gchar *contents)
2644 PopplerAnnot *poppler_annot;
2646 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2650 poppler_annot_set_contents (poppler_annot, contents);
2651 PDF_DOCUMENT (document)->modified = TRUE;
2655 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface)
2657 iface->get_annotations = pdf_document_annotations_get_annotations;
2658 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2662 struct SaveToBufferData {
2668 attachment_save_to_buffer_callback (const gchar *buf,
2673 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2677 if (sdata->len + count > sdata->max) {
2678 new_max = MAX (sdata->max * 2, sdata->len + count);
2679 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2681 sdata->buffer = new_buffer;
2682 sdata->max = new_max;
2685 memcpy (sdata->buffer + sdata->len, buf, count);
2686 sdata->len += count;
2692 attachment_save_to_buffer (PopplerAttachment *attachment,
2697 static const gint initial_max = 1024;
2698 struct SaveToBufferData sdata;
2703 sdata.buffer = (gchar *) g_malloc (initial_max);
2704 sdata.max = initial_max;
2707 if (! poppler_attachment_save_to_callback (attachment,
2708 attachment_save_to_buffer_callback,
2711 g_free (sdata.buffer);
2715 *buffer = sdata.buffer;
2716 *buffer_size = sdata.len;
2722 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2724 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2727 GList *retval = NULL;
2729 attachments = poppler_document_get_attachments (pdf_document->document);
2731 for (list = attachments; list; list = list->next) {
2732 PopplerAttachment *attachment;
2733 EvAttachment *ev_attachment;
2736 GError *error = NULL;
2738 attachment = (PopplerAttachment *) list->data;
2740 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2741 ev_attachment = ev_attachment_new (attachment->name,
2742 attachment->description,
2747 retval = g_list_prepend (retval, ev_attachment);
2750 g_warning ("%s", error->message);
2751 g_error_free (error);
2757 g_object_unref (attachment);
2760 return g_list_reverse (retval);
2764 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2766 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2768 return poppler_document_has_attachments (pdf_document->document);
2772 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface)
2774 iface->has_attachments = pdf_document_attachments_has_attachments;
2775 iface->get_attachments = pdf_document_attachments_get_attachments;
2780 pdf_document_layers_has_layers (EvDocumentLayers *document)
2782 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2783 PopplerLayersIter *iter;
2785 iter = poppler_layers_iter_new (pdf_document->document);
2788 poppler_layers_iter_free (iter);
2794 build_layers_tree (PdfDocument *pdf_document,
2795 GtkTreeModel *model,
2796 GtkTreeIter *parent,
2797 PopplerLayersIter *iter)
2800 GtkTreeIter tree_iter;
2801 PopplerLayersIter *child;
2802 PopplerLayer *layer;
2803 EvLayer *ev_layer = NULL;
2808 layer = poppler_layers_iter_get_layer (iter);
2810 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2811 visible = poppler_layer_is_visible (layer);
2812 rb_group = poppler_layer_get_radio_button_group_id (layer);
2813 pdf_document->layers = g_list_append (pdf_document->layers,
2814 g_object_ref (layer));
2815 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2816 poppler_layer_is_parent (layer),
2821 title = poppler_layers_iter_get_title (iter);
2822 markup = g_markup_escape_text (title, -1);
2829 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2830 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2831 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2832 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2833 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2834 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2835 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2836 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2839 g_object_unref (ev_layer);
2842 child = poppler_layers_iter_get_child (iter);
2844 build_layers_tree (pdf_document, model, &tree_iter, child);
2845 poppler_layers_iter_free (child);
2846 } while (poppler_layers_iter_next (iter));
2849 static GtkTreeModel *
2850 pdf_document_layers_get_layers (EvDocumentLayers *document)
2852 GtkTreeModel *model = NULL;
2853 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2854 PopplerLayersIter *iter;
2856 iter = poppler_layers_iter_new (pdf_document->document);
2858 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2859 G_TYPE_STRING, /* TITLE */
2860 G_TYPE_OBJECT, /* LAYER */
2861 G_TYPE_BOOLEAN, /* VISIBLE */
2862 G_TYPE_BOOLEAN, /* ENABLED */
2863 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2864 G_TYPE_INT); /* RBGROUP */
2865 build_layers_tree (pdf_document, model, NULL, iter);
2866 poppler_layers_iter_free (iter);
2872 pdf_document_layers_show_layer (EvDocumentLayers *document,
2875 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2876 guint layer_id = ev_layer_get_id (layer);
2878 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2882 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2885 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2886 guint layer_id = ev_layer_get_id (layer);
2888 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2892 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2895 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2896 guint layer_id = ev_layer_get_id (layer);
2898 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2902 pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface)
2904 iface->has_layers = pdf_document_layers_has_layers;
2905 iface->get_layers = pdf_document_layers_get_layers;
2906 iface->show_layer = pdf_document_layers_show_layer;
2907 iface->hide_layer = pdf_document_layers_hide_layer;
2908 iface->layer_is_visible = pdf_document_layers_layer_is_visible;