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;
104 gboolean forms_modified;
105 gboolean annots_modified;
107 PopplerFontInfo *font_info;
108 PopplerFontsIter *fonts_iter;
109 int fonts_scanned_pages;
111 PdfPrintContext *print_ctx;
117 static void pdf_document_security_iface_init (EvDocumentSecurityInterface *iface);
118 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface);
119 static void pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface);
120 static void pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface);
121 static void pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface);
122 static void pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface);
123 static void pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface);
124 static void pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface);
125 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface);
126 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface);
127 static void pdf_document_find_iface_init (EvDocumentFindInterface *iface);
128 static void pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface);
129 static void pdf_selection_iface_init (EvSelectionInterface *iface);
130 static void pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface);
131 static void pdf_document_text_iface_init (EvDocumentTextInterface *iface);
132 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
136 static int pdf_document_get_n_pages (EvDocument *document);
138 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
140 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
141 PopplerAction *action);
142 static void pdf_print_context_free (PdfPrintContext *ctx);
143 static gboolean attachment_save_to_buffer (PopplerAttachment *attachment,
148 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
150 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
151 pdf_document_security_iface_init);
152 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
153 pdf_document_document_thumbnails_iface_init);
154 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
155 pdf_document_document_links_iface_init);
156 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
157 pdf_document_document_images_iface_init);
158 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
159 pdf_document_document_forms_iface_init);
160 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
161 pdf_document_document_fonts_iface_init);
162 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
163 pdf_document_document_layers_iface_init);
164 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
165 pdf_document_document_print_iface_init);
166 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
167 pdf_document_document_annotations_iface_init);
168 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
169 pdf_document_document_attachments_iface_init);
170 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
171 pdf_document_find_iface_init);
172 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
173 pdf_document_file_exporter_iface_init);
174 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
175 pdf_selection_iface_init);
176 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
177 pdf_document_page_transition_iface_init);
178 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TEXT,
179 pdf_document_text_iface_init);
183 pdf_document_dispose (GObject *object)
185 PdfDocument *pdf_document = PDF_DOCUMENT(object);
187 if (pdf_document->print_ctx) {
188 pdf_print_context_free (pdf_document->print_ctx);
189 pdf_document->print_ctx = NULL;
192 if (pdf_document->annots) {
193 g_hash_table_destroy (pdf_document->annots);
194 pdf_document->annots = NULL;
197 if (pdf_document->document) {
198 g_object_unref (pdf_document->document);
201 if (pdf_document->font_info) {
202 poppler_font_info_free (pdf_document->font_info);
205 if (pdf_document->fonts_iter) {
206 poppler_fonts_iter_free (pdf_document->fonts_iter);
209 if (pdf_document->layers) {
210 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
211 g_list_free (pdf_document->layers);
214 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
218 pdf_document_init (PdfDocument *pdf_document)
220 pdf_document->password = NULL;
224 convert_error (GError *poppler_error,
227 if (poppler_error == NULL)
230 if (poppler_error->domain == POPPLER_ERROR) {
231 /* convert poppler errors into EvDocument errors */
232 gint code = EV_DOCUMENT_ERROR_INVALID;
233 if (poppler_error->code == POPPLER_ERROR_INVALID)
234 code = EV_DOCUMENT_ERROR_INVALID;
235 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
236 code = EV_DOCUMENT_ERROR_ENCRYPTED;
238 g_set_error_literal (error,
241 poppler_error->message);
243 g_error_free (poppler_error);
245 g_propagate_error (error, poppler_error);
252 pdf_document_save (EvDocument *document,
256 PdfDocument *pdf_document = PDF_DOCUMENT (document);
258 GError *poppler_error = NULL;
260 if (pdf_document->forms_modified || pdf_document->annots_modified) {
261 retval = poppler_document_save (pdf_document->document,
262 uri, &poppler_error);
264 pdf_document->forms_modified = FALSE;
265 pdf_document->annots_modified = FALSE;
268 retval = poppler_document_save_a_copy (pdf_document->document,
269 uri, &poppler_error);
273 convert_error (poppler_error, error);
279 pdf_document_load (EvDocument *document,
283 GError *poppler_error = NULL;
284 PdfDocument *pdf_document = PDF_DOCUMENT (document);
286 pdf_document->document =
287 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
289 if (pdf_document->document == NULL) {
290 convert_error (poppler_error, error);
298 pdf_document_get_n_pages (EvDocument *document)
300 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
304 pdf_document_get_page (EvDocument *document,
307 PdfDocument *pdf_document = PDF_DOCUMENT (document);
308 PopplerPage *poppler_page;
311 poppler_page = poppler_document_get_page (pdf_document->document, index);
312 page = ev_page_new (index);
313 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
314 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
315 g_object_unref (poppler_page);
321 pdf_document_get_page_size (EvDocument *document,
326 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
328 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
332 pdf_document_get_page_label (EvDocument *document,
337 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
339 g_object_get (G_OBJECT (page->backend_page),
345 static cairo_surface_t *
346 pdf_page_render (PopplerPage *page,
351 cairo_surface_t *surface;
354 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
356 cr = cairo_create (surface);
358 switch (rc->rotation) {
360 cairo_translate (cr, width, 0);
363 cairo_translate (cr, width, height);
366 cairo_translate (cr, 0, height);
369 cairo_translate (cr, 0, 0);
371 cairo_scale (cr, rc->scale, rc->scale);
372 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
373 poppler_page_render (page, cr);
375 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
376 cairo_set_source_rgb (cr, 1., 1., 1.);
384 static cairo_surface_t *
385 pdf_document_render (EvDocument *document,
388 PdfDocument *pdf_document;
389 PopplerPage *poppler_page;
390 double width_points, height_points;
393 poppler_page = POPPLER_PAGE (rc->page->backend_page);
395 poppler_page_get_size (poppler_page,
396 &width_points, &height_points);
398 if (rc->rotation == 90 || rc->rotation == 270) {
399 width = (int) ((height_points * rc->scale) + 0.5);
400 height = (int) ((width_points * rc->scale) + 0.5);
402 width = (int) ((width_points * rc->scale) + 0.5);
403 height = (int) ((height_points * rc->scale) + 0.5);
406 return pdf_page_render (poppler_page,
411 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 */
413 pdf_document_get_format_from_metadata (xmlDocPtr doc,
414 xmlXPathContextPtr xpathCtx)
416 xmlXPathObjectPtr xpathObj;
417 xmlChar *part = NULL;
418 xmlChar *conf = NULL;
422 /* add pdf/a namespaces */
423 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
424 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
425 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
427 /* reads pdf/a part */
428 /* first syntax: child node */
429 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
430 if (xpathObj != NULL) {
431 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
432 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
434 xmlXPathFreeObject (xpathObj);
437 /* second syntax: attribute */
438 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
439 if (xpathObj != NULL) {
440 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
441 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
443 xmlXPathFreeObject (xpathObj);
447 /* reads pdf/a conformance */
448 /* first syntax: child node */
449 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
450 if (xpathObj != NULL) {
451 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
452 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
454 xmlXPathFreeObject (xpathObj);
457 /* second syntax: attribute */
458 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
459 if (xpathObj != NULL) {
460 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
461 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
463 xmlXPathFreeObject (xpathObj);
467 if (part != NULL && conf != NULL) {
468 /* makes conf lowercase */
469 for (i = 0; conf[i]; i++)
470 conf[i] = g_ascii_tolower (conf[i]);
473 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
483 static EvDocumentLicense *
484 pdf_document_get_license_from_metadata (xmlDocPtr doc,
485 xmlXPathContextPtr xpathCtx)
487 xmlXPathObjectPtr xpathObj;
488 xmlChar *marked = NULL;
489 const char *language_string;
492 gchar *tag, *tag_aux;
494 EvDocumentLicense *license;
496 /* register namespaces */
497 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
498 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
499 xmlXPathRegisterNs (xpathCtx, BAD_CAST "dc", BAD_CAST "http://purl.org/dc/elements/1.1/");
500 /* XMP Rights Management Schema */
501 xmlXPathRegisterNs (xpathCtx, BAD_CAST "xmpRights", BAD_CAST "http://ns.adobe.com/xap/1.0/rights/");
502 /* Creative Commons Schema */
503 xmlXPathRegisterNs (xpathCtx, BAD_CAST "cc", BAD_CAST "http://creativecommons.org/ns#");
505 /* checking if the document has been marked as defined on the XMP Rights
506 * Management Schema */
507 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_MARKED, xpathCtx);
508 if (xpathObj != NULL) {
509 if (xpathObj->nodesetval != NULL &&
510 xpathObj->nodesetval->nodeNr != 0)
511 marked = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
512 xmlXPathFreeObject (xpathObj);
515 /* a) Not marked => No XMP Rights information */
521 license = ev_document_license_new ();
523 /* b) Marked False => Public Domain, no copyrighted material and no
525 if (g_strrstr ((char *) marked, "False") != NULL) {
526 license->text = g_strdup (_("This work is in the Public Domain"));
527 /* c) Marked True => Copyrighted material */
529 /* Checking usage terms as defined by the XMP Rights Management
530 * Schema. This field is recomended to be checked by Creative
532 /* 1) checking for a suitable localized string */
533 language_string = pango_language_to_string (gtk_get_default_language ());
534 tags = g_strsplit (language_string, "-", -1);
535 i = g_strv_length (tags);
536 while (i-- && !license->text) {
537 tag = g_strdup (tags[0]);
538 for (j = 1; j <= i; j++) {
539 tag_aux = g_strdup_printf ("%s-%s", tag, tags[j]);
543 aux = g_strdup_printf (LICENSE_TEXT, tag);
544 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
545 if (xpathObj != NULL) {
546 if (xpathObj->nodesetval != NULL &&
547 xpathObj->nodesetval->nodeNr != 0)
548 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
549 xmlXPathFreeObject (xpathObj);
556 /* 2) if not, use the default string */
557 if (!license->text) {
558 aux = g_strdup_printf (LICENSE_TEXT, "x-default");
559 xpathObj = xmlXPathEvalExpression (BAD_CAST aux, xpathCtx);
560 if (xpathObj != NULL) {
561 if (xpathObj->nodesetval != NULL &&
562 xpathObj->nodesetval->nodeNr != 0)
563 license->text = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
564 xmlXPathFreeObject (xpathObj);
569 /* Checking the license URI as defined by the Creative Commons
570 * Schema. This field is recomended to be checked by Creative
572 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_URI, xpathCtx);
573 if (xpathObj != NULL) {
574 if (xpathObj->nodesetval != NULL &&
575 xpathObj->nodesetval->nodeNr != 0)
576 license->uri = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
577 xmlXPathFreeObject (xpathObj);
580 /* Checking the web statement as defined by the XMP Rights
581 * Management Schema. Checking it out is a sort of above-and-beyond
582 * the basic recommendations by Creative Commons. It can be
583 * considered as a "reinforcement" approach to add certainty. */
584 xpathObj = xmlXPathEvalExpression (BAD_CAST LICENSE_WEB_STATEMENT, xpathCtx);
585 if (xpathObj != NULL) {
586 if (xpathObj->nodesetval != NULL &&
587 xpathObj->nodesetval->nodeNr != 0)
588 license->web_statement = (gchar *)xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
589 xmlXPathFreeObject (xpathObj);
594 if (!license->text && !license->uri && !license->web_statement) {
595 ev_document_license_free (license);
603 pdf_document_parse_metadata (const gchar *metadata,
604 EvDocumentInfo *info)
607 xmlXPathContextPtr xpathCtx;
610 doc = xmlParseMemory (metadata, strlen (metadata));
612 return; /* invalid xml metadata */
614 xpathCtx = xmlXPathNewContext (doc);
615 if (xpathCtx == NULL) {
617 return; /* invalid xpath context */
620 fmt = pdf_document_get_format_from_metadata (doc, xpathCtx);
622 g_free (info->format);
626 info->license = pdf_document_get_license_from_metadata (doc, xpathCtx);
628 xmlXPathFreeContext (xpathCtx);
633 static EvDocumentInfo *
634 pdf_document_get_info (EvDocument *document)
636 EvDocumentInfo *info;
637 PopplerPageLayout layout;
638 PopplerPageMode mode;
639 PopplerViewerPreferences view_prefs;
640 PopplerPermissions permissions;
644 info = g_new0 (EvDocumentInfo, 1);
646 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
647 EV_DOCUMENT_INFO_FORMAT |
648 EV_DOCUMENT_INFO_AUTHOR |
649 EV_DOCUMENT_INFO_SUBJECT |
650 EV_DOCUMENT_INFO_KEYWORDS |
651 EV_DOCUMENT_INFO_LAYOUT |
652 EV_DOCUMENT_INFO_START_MODE |
653 EV_DOCUMENT_INFO_PERMISSIONS |
654 EV_DOCUMENT_INFO_UI_HINTS |
655 EV_DOCUMENT_INFO_CREATOR |
656 EV_DOCUMENT_INFO_PRODUCER |
657 EV_DOCUMENT_INFO_CREATION_DATE |
658 EV_DOCUMENT_INFO_MOD_DATE |
659 EV_DOCUMENT_INFO_LINEARIZED |
660 EV_DOCUMENT_INFO_N_PAGES |
661 EV_DOCUMENT_INFO_SECURITY |
662 EV_DOCUMENT_INFO_PAPER_SIZE |
663 EV_DOCUMENT_INFO_LICENSE;
665 g_object_get (PDF_DOCUMENT (document)->document,
666 "title", &(info->title),
667 "format", &(info->format),
668 "author", &(info->author),
669 "subject", &(info->subject),
670 "keywords", &(info->keywords),
672 "page-layout", &layout,
673 "viewer-preferences", &view_prefs,
674 "permissions", &permissions,
675 "creator", &(info->creator),
676 "producer", &(info->producer),
677 "creation-date", &(info->creation_date),
678 "mod-date", &(info->modified_date),
679 "linearized", &(info->linearized),
680 "metadata", &metadata,
683 if (metadata != NULL) {
684 pdf_document_parse_metadata (metadata, info);
688 info->n_pages = ev_document_get_n_pages (document);
690 if (info->n_pages > 0) {
691 ev_document_get_page_size (document, 0,
692 &(info->paper_width),
693 &(info->paper_height));
695 info->paper_width = info->paper_width / 72.0f * 25.4f;
696 info->paper_height = info->paper_height / 72.0f * 25.4f;
700 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
701 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
703 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
704 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
706 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
707 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
709 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
710 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
711 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
712 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
714 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
715 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
722 case POPPLER_PAGE_MODE_NONE:
723 info->mode = EV_DOCUMENT_MODE_NONE;
725 case POPPLER_PAGE_MODE_USE_THUMBS:
726 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
728 case POPPLER_PAGE_MODE_USE_OC:
729 info->mode = EV_DOCUMENT_MODE_USE_OC;
731 case POPPLER_PAGE_MODE_FULL_SCREEN:
732 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
734 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
735 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
741 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
742 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
744 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
745 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
747 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
748 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
750 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
751 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
753 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
754 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
756 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
757 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
759 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
760 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
763 info->permissions = 0;
764 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
765 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
767 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
768 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
770 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
771 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
773 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
774 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
777 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
778 /* translators: this is the document security state */
779 info->security = g_strdup (_("Yes"));
781 /* translators: this is the document security state */
782 info->security = g_strdup (_("No"));
789 pdf_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
791 PopplerBackend backend;
793 backend = poppler_get_backend ();
795 case POPPLER_BACKEND_CAIRO:
796 info->name = "poppler/cairo";
798 case POPPLER_BACKEND_SPLASH:
799 info->name = "poppler/splash";
802 info->name = "poppler/unknown";
806 info->version = poppler_get_version ();
812 pdf_document_support_synctex (EvDocument *document)
818 pdf_document_class_init (PdfDocumentClass *klass)
820 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
821 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
823 g_object_class->dispose = pdf_document_dispose;
825 ev_document_class->save = pdf_document_save;
826 ev_document_class->load = pdf_document_load;
827 ev_document_class->get_n_pages = pdf_document_get_n_pages;
828 ev_document_class->get_page = pdf_document_get_page;
829 ev_document_class->get_page_size = pdf_document_get_page_size;
830 ev_document_class->get_page_label = pdf_document_get_page_label;
831 ev_document_class->render = pdf_document_render;
832 ev_document_class->get_info = pdf_document_get_info;
833 ev_document_class->get_backend_info = pdf_document_get_backend_info;
834 ev_document_class->support_synctex = pdf_document_support_synctex;
837 /* EvDocumentSecurity */
839 pdf_document_has_document_security (EvDocumentSecurity *document_security)
841 /* FIXME: do we really need to have this? */
846 pdf_document_set_password (EvDocumentSecurity *document_security,
847 const char *password)
849 PdfDocument *document = PDF_DOCUMENT (document_security);
851 if (document->password)
852 g_free (document->password);
854 document->password = g_strdup (password);
858 pdf_document_security_iface_init (EvDocumentSecurityInterface *iface)
860 iface->has_document_security = pdf_document_has_document_security;
861 iface->set_password = pdf_document_set_password;
865 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
867 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
870 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
872 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
876 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
879 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
882 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
884 if (pdf_document->font_info == NULL) {
885 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
888 if (pdf_document->fonts_iter) {
889 poppler_fonts_iter_free (pdf_document->fonts_iter);
892 pdf_document->fonts_scanned_pages += n_pages;
894 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
895 &pdf_document->fonts_iter);
897 pdf_document->fonts_scanned_pages = 0;
898 poppler_font_info_free (pdf_document->font_info);
899 pdf_document->font_info = NULL;
906 font_type_to_string (PopplerFontType type)
909 case POPPLER_FONT_TYPE_TYPE1:
911 case POPPLER_FONT_TYPE_TYPE1C:
913 case POPPLER_FONT_TYPE_TYPE3:
915 case POPPLER_FONT_TYPE_TRUETYPE:
916 return _("TrueType");
917 case POPPLER_FONT_TYPE_CID_TYPE0:
918 return _("Type 1 (CID)");
919 case POPPLER_FONT_TYPE_CID_TYPE0C:
920 return _("Type 1C (CID)");
921 case POPPLER_FONT_TYPE_CID_TYPE2:
922 return _("TrueType (CID)");
924 return _("Unknown font type");
929 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
932 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
933 PopplerFontsIter *iter = pdf_document->fonts_iter;
935 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
941 GtkTreeIter list_iter;
944 const char *embedded;
947 name = poppler_fonts_iter_get_name (iter);
953 type = font_type_to_string (
954 poppler_fonts_iter_get_font_type (iter));
956 if (poppler_fonts_iter_is_embedded (iter)) {
957 if (poppler_fonts_iter_is_subset (iter))
958 embedded = _("Embedded subset");
960 embedded = _("Embedded");
962 embedded = _("Not embedded");
965 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
967 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
968 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
969 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
970 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
974 } while (poppler_fonts_iter_next (iter));
978 pdf_document_document_fonts_iface_init (EvDocumentFontsInterface *iface)
980 iface->fill_model = pdf_document_fonts_fill_model;
981 iface->scan = pdf_document_fonts_scan;
982 iface->get_progress = pdf_document_fonts_get_progress;
986 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
988 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
989 PopplerIndexIter *iter;
991 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
993 iter = poppler_index_iter_new (pdf_document->document);
996 poppler_index_iter_free (iter);
1002 ev_link_dest_from_dest (PdfDocument *pdf_document,
1005 EvLinkDest *ev_dest = NULL;
1006 const char *unimplemented_dest = NULL;
1008 g_assert (dest != NULL);
1010 switch (dest->type) {
1011 case POPPLER_DEST_XYZ: {
1012 PopplerPage *poppler_page;
1015 poppler_page = poppler_document_get_page (pdf_document->document,
1016 MAX (0, dest->page_num - 1));
1017 poppler_page_get_size (poppler_page, NULL, &height);
1018 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1020 height - MIN (height, dest->top),
1025 g_object_unref (poppler_page);
1028 case POPPLER_DEST_FITB:
1029 case POPPLER_DEST_FIT:
1030 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1032 case POPPLER_DEST_FITBH:
1033 case POPPLER_DEST_FITH: {
1034 PopplerPage *poppler_page;
1037 poppler_page = poppler_document_get_page (pdf_document->document,
1038 MAX (0, dest->page_num - 1));
1039 poppler_page_get_size (poppler_page, NULL, &height);
1040 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1041 height - MIN (height, dest->top),
1043 g_object_unref (poppler_page);
1046 case POPPLER_DEST_FITBV:
1047 case POPPLER_DEST_FITV:
1048 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1052 case POPPLER_DEST_FITR: {
1053 PopplerPage *poppler_page;
1056 poppler_page = poppler_document_get_page (pdf_document->document,
1057 MAX (0, dest->page_num - 1));
1058 poppler_page_get_size (poppler_page, NULL, &height);
1059 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1061 height - MIN (height, dest->bottom),
1063 height - MIN (height, dest->top));
1064 g_object_unref (poppler_page);
1067 case POPPLER_DEST_NAMED:
1068 ev_dest = ev_link_dest_new_named (dest->named_dest);
1070 case POPPLER_DEST_UNKNOWN:
1071 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1075 if (unimplemented_dest) {
1076 g_warning ("Unimplemented destination: %s, please post a "
1077 "bug report in Evince bugzilla "
1078 "(http://bugzilla.gnome.org) with a testcase.",
1079 unimplemented_dest);
1083 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1089 ev_link_from_action (PdfDocument *pdf_document,
1090 PopplerAction *action)
1092 EvLink *link = NULL;
1093 EvLinkAction *ev_action = NULL;
1094 const char *unimplemented_action = NULL;
1096 switch (action->type) {
1097 case POPPLER_ACTION_NONE:
1099 case POPPLER_ACTION_GOTO_DEST: {
1102 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1103 ev_action = ev_link_action_new_dest (dest);
1106 case POPPLER_ACTION_GOTO_REMOTE: {
1109 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1110 ev_action = ev_link_action_new_remote (dest,
1111 action->goto_remote.file_name);
1115 case POPPLER_ACTION_LAUNCH:
1116 ev_action = ev_link_action_new_launch (action->launch.file_name,
1117 action->launch.params);
1119 case POPPLER_ACTION_URI:
1120 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1122 case POPPLER_ACTION_NAMED:
1123 ev_action = ev_link_action_new_named (action->named.named_dest);
1125 case POPPLER_ACTION_MOVIE:
1126 unimplemented_action = "POPPLER_ACTION_MOVIE";
1128 #if POPPLER_CHECK_VERSION (0, 13, 2)
1129 case POPPLER_ACTION_RENDITION:
1130 unimplemented_action = "POPPLER_ACTION_RENDITION";
1132 case POPPLER_ACTION_OCG_STATE:
1133 unimplemented_action = "POPPLER_ACTION_OCG_STATE";
1136 case POPPLER_ACTION_UNKNOWN:
1137 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1140 if (unimplemented_action) {
1141 g_warning ("Unimplemented action: %s, please post a bug report "
1142 "in Evince bugzilla (http://bugzilla.gnome.org) "
1143 "with a testcase.", unimplemented_action);
1146 link = ev_link_new (action->any.title, ev_action);
1152 build_tree (PdfDocument *pdf_document,
1153 GtkTreeModel *model,
1154 GtkTreeIter *parent,
1155 PopplerIndexIter *iter)
1159 GtkTreeIter tree_iter;
1160 PopplerIndexIter *child;
1161 PopplerAction *action;
1162 EvLink *link = NULL;
1166 action = poppler_index_iter_get_action (iter);
1167 expand = poppler_index_iter_is_open (iter);
1172 switch (action->type) {
1173 case POPPLER_ACTION_GOTO_DEST: {
1174 /* For bookmarks, solve named destinations */
1175 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1177 EvLinkDest *ev_dest = NULL;
1178 EvLinkAction *ev_action;
1180 dest = poppler_document_find_dest (pdf_document->document,
1181 action->goto_dest.dest->named_dest);
1183 link = ev_link_from_action (pdf_document, action);
1187 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1188 poppler_dest_free (dest);
1190 ev_action = ev_link_action_new_dest (ev_dest);
1191 link = ev_link_new (action->any.title, ev_action);
1193 link = ev_link_from_action (pdf_document, action);
1198 link = ev_link_from_action (pdf_document, action);
1202 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1203 poppler_action_free (action);
1205 g_object_unref (link);
1210 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1211 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1213 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1214 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1215 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1216 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1219 g_free (title_markup);
1220 g_object_unref (link);
1222 child = poppler_index_iter_get_child (iter);
1224 build_tree (pdf_document, model, &tree_iter, child);
1225 poppler_index_iter_free (child);
1226 poppler_action_free (action);
1228 } while (poppler_index_iter_next (iter));
1231 static GtkTreeModel *
1232 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1234 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1235 GtkTreeModel *model = NULL;
1236 PopplerIndexIter *iter;
1238 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1240 iter = poppler_index_iter_new (pdf_document->document);
1241 /* Create the model if we have items*/
1243 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1248 build_tree (pdf_document, model, NULL, iter);
1249 poppler_index_iter_free (iter);
1255 static EvMappingList *
1256 pdf_document_links_get_links (EvDocumentLinks *document_links,
1259 PdfDocument *pdf_document;
1260 PopplerPage *poppler_page;
1261 GList *retval = NULL;
1262 GList *mapping_list;
1266 pdf_document = PDF_DOCUMENT (document_links);
1267 poppler_page = POPPLER_PAGE (page->backend_page);
1268 mapping_list = poppler_page_get_link_mapping (poppler_page);
1269 poppler_page_get_size (poppler_page, NULL, &height);
1271 for (list = mapping_list; list; list = list->next) {
1272 PopplerLinkMapping *link_mapping;
1273 EvMapping *ev_link_mapping;
1275 link_mapping = (PopplerLinkMapping *)list->data;
1276 ev_link_mapping = g_new (EvMapping, 1);
1277 ev_link_mapping->data = ev_link_from_action (pdf_document,
1278 link_mapping->action);
1279 ev_link_mapping->area.x1 = link_mapping->area.x1;
1280 ev_link_mapping->area.x2 = link_mapping->area.x2;
1281 /* Invert this for X-style coordinates */
1282 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1283 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1285 retval = g_list_prepend (retval, ev_link_mapping);
1288 poppler_page_free_link_mapping (mapping_list);
1290 return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
1294 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1295 const gchar *link_name)
1297 PdfDocument *pdf_document;
1299 EvLinkDest *ev_dest = NULL;
1301 pdf_document = PDF_DOCUMENT (document_links);
1302 dest = poppler_document_find_dest (pdf_document->document,
1305 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1306 poppler_dest_free (dest);
1313 pdf_document_document_links_iface_init (EvDocumentLinksInterface *iface)
1315 iface->has_document_links = pdf_document_links_has_document_links;
1316 iface->get_links_model = pdf_document_links_get_links_model;
1317 iface->get_links = pdf_document_links_get_links;
1318 iface->find_link_dest = pdf_document_links_find_link_dest;
1321 static EvMappingList *
1322 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1325 GList *retval = NULL;
1326 PdfDocument *pdf_document;
1327 PopplerPage *poppler_page;
1328 GList *mapping_list;
1331 pdf_document = PDF_DOCUMENT (document_images);
1332 poppler_page = POPPLER_PAGE (page->backend_page);
1333 mapping_list = poppler_page_get_image_mapping (poppler_page);
1335 for (list = mapping_list; list; list = list->next) {
1336 PopplerImageMapping *image_mapping;
1337 EvMapping *ev_image_mapping;
1339 image_mapping = (PopplerImageMapping *)list->data;
1341 ev_image_mapping = g_new (EvMapping, 1);
1343 ev_image_mapping->data = ev_image_new (page->index, image_mapping->image_id);
1344 ev_image_mapping->area.x1 = image_mapping->area.x1;
1345 ev_image_mapping->area.y1 = image_mapping->area.y1;
1346 ev_image_mapping->area.x2 = image_mapping->area.x2;
1347 ev_image_mapping->area.y2 = image_mapping->area.y2;
1349 retval = g_list_prepend (retval, ev_image_mapping);
1352 poppler_page_free_image_mapping (mapping_list);
1354 return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
1358 pdf_document_images_get_image (EvDocumentImages *document_images,
1361 GdkPixbuf *retval = NULL;
1362 PdfDocument *pdf_document;
1363 PopplerPage *poppler_page;
1364 cairo_surface_t *surface;
1366 pdf_document = PDF_DOCUMENT (document_images);
1367 poppler_page = poppler_document_get_page (pdf_document->document,
1368 ev_image_get_page (image));
1370 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1372 retval = ev_document_misc_pixbuf_from_surface (surface);
1373 cairo_surface_destroy (surface);
1376 g_object_unref (poppler_page);
1382 pdf_document_document_images_iface_init (EvDocumentImagesInterface *iface)
1384 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1385 iface->get_image = pdf_document_images_get_image;
1389 make_thumbnail_for_page (PopplerPage *poppler_page,
1390 EvRenderContext *rc,
1396 #ifdef POPPLER_WITH_GDK
1397 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1399 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1401 ev_document_fc_mutex_lock ();
1402 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1404 rc->scale, rc->rotation, pixbuf);
1405 ev_document_fc_mutex_unlock ();
1407 cairo_surface_t *surface;
1409 ev_document_fc_mutex_lock ();
1410 surface = pdf_page_render (poppler_page, width, height, rc);
1411 ev_document_fc_mutex_unlock ();
1413 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1414 cairo_surface_destroy (surface);
1415 #endif /* POPPLER_WITH_GDK */
1421 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1422 EvRenderContext *rc,
1425 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1426 PopplerPage *poppler_page;
1427 GdkPixbuf *pixbuf = NULL;
1428 GdkPixbuf *border_pixbuf;
1431 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1433 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1434 rc, &width, &height);
1436 #ifdef POPPLER_WITH_GDK
1437 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1439 cairo_surface_t *surface;
1441 surface = poppler_page_get_thumbnail (poppler_page);
1443 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1444 cairo_surface_destroy (surface);
1446 #endif /* POPPLER_WITH_GDK */
1448 if (pixbuf != NULL) {
1449 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1450 gdk_pixbuf_get_height (pixbuf) :
1451 gdk_pixbuf_get_width (pixbuf);
1453 if (thumb_width == width) {
1454 GdkPixbuf *rotated_pixbuf;
1456 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1457 (GdkPixbufRotation) (360 - rc->rotation));
1458 g_object_unref (pixbuf);
1459 pixbuf = rotated_pixbuf;
1461 /* The provided thumbnail has a different size */
1462 g_object_unref (pixbuf);
1463 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1466 /* There is no provided thumbnail. We need to make one. */
1467 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1470 if (border && pixbuf) {
1471 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1472 g_object_unref (pixbuf);
1473 pixbuf = border_pixbuf;
1480 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1481 EvRenderContext *rc,
1485 double page_width, page_height;
1487 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1488 &page_width, &page_height);
1490 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1491 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1493 if (rc->rotation == 90 || rc->rotation == 270) {
1503 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
1505 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1506 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1511 pdf_document_find_find_text (EvDocumentFind *document_find,
1514 gboolean case_sensitive)
1517 PopplerPage *poppler_page;
1519 GList *retval = NULL;
1521 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1522 g_return_val_if_fail (text != NULL, NULL);
1524 poppler_page = POPPLER_PAGE (page->backend_page);
1526 matches = poppler_page_find_text (poppler_page, text);
1530 poppler_page_get_size (poppler_page, NULL, &height);
1531 for (l = matches; l && l->data; l = g_list_next (l)) {
1532 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1533 EvRectangle *ev_rect;
1535 ev_rect = ev_rectangle_new ();
1536 ev_rect->x1 = rect->x1;
1537 ev_rect->x2 = rect->x2;
1538 /* Invert this for X-style coordinates */
1539 ev_rect->y1 = height - rect->y2;
1540 ev_rect->y2 = height - rect->y1;
1542 retval = g_list_prepend (retval, ev_rect);
1545 g_list_foreach (matches, (GFunc)poppler_rectangle_free, NULL);
1546 g_list_free (matches);
1548 return g_list_reverse (retval);
1552 pdf_document_find_iface_init (EvDocumentFindInterface *iface)
1554 iface->find_text = pdf_document_find_find_text;
1558 pdf_print_context_free (PdfPrintContext *ctx)
1563 #ifdef HAVE_CAIRO_PRINT
1565 cairo_destroy (ctx->cr);
1570 poppler_ps_file_free (ctx->ps_file);
1571 ctx->ps_file = NULL;
1578 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1579 EvFileExporterContext *fc)
1581 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1582 PdfPrintContext *ctx;
1583 #ifdef HAVE_CAIRO_PRINT
1584 gdouble width, height;
1585 cairo_surface_t *surface = NULL;
1588 if (pdf_document->print_ctx)
1589 pdf_print_context_free (pdf_document->print_ctx);
1590 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1591 ctx = pdf_document->print_ctx;
1592 ctx->format = fc->format;
1594 #ifdef HAVE_CAIRO_PRINT
1595 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1597 ctx->paper_width = fc->paper_width;
1598 ctx->paper_height = fc->paper_height;
1600 switch (fc->pages_per_sheet) {
1628 ctx->pages_printed = 0;
1630 switch (fc->format) {
1631 case EV_FILE_FORMAT_PS:
1632 #ifdef HAVE_CAIRO_PS
1633 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1636 case EV_FILE_FORMAT_PDF:
1637 #ifdef HAVE_CAIRO_PDF
1638 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1642 g_assert_not_reached ();
1645 ctx->cr = cairo_create (surface);
1646 cairo_surface_destroy (surface);
1648 #else /* HAVE_CAIRO_PRINT */
1649 if (ctx->format == EV_FILE_FORMAT_PS) {
1650 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1651 fc->filename, fc->first_page,
1652 fc->last_page - fc->first_page + 1);
1653 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1654 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1656 #endif /* HAVE_CAIRO_PRINT */
1660 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1662 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1663 PdfPrintContext *ctx = pdf_document->print_ctx;
1665 g_return_if_fail (pdf_document->print_ctx != NULL);
1667 ctx->pages_printed = 0;
1669 #ifdef HAVE_CAIRO_PRINT
1670 if (ctx->paper_width > ctx->paper_height) {
1671 if (ctx->format == EV_FILE_FORMAT_PS) {
1672 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1675 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1676 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1681 #endif /* HAVE_CAIRO_PRINT */
1685 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1686 EvRenderContext *rc)
1688 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1689 PdfPrintContext *ctx = pdf_document->print_ctx;
1690 PopplerPage *poppler_page;
1691 #ifdef HAVE_CAIRO_PRINT
1692 gdouble page_width, page_height;
1695 gdouble width, height;
1696 gdouble pwidth, pheight;
1697 gdouble xscale, yscale;
1700 g_return_if_fail (pdf_document->print_ctx != NULL);
1702 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1704 #ifdef HAVE_CAIRO_PRINT
1705 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1706 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1707 poppler_page_get_size (poppler_page, &page_width, &page_height);
1709 if (page_width > page_height && page_width > ctx->paper_width) {
1715 /* Use always portrait mode and rotate when necessary */
1716 if (ctx->paper_width > ctx->paper_height) {
1717 width = ctx->paper_height;
1718 height = ctx->paper_width;
1721 width = ctx->paper_width;
1722 height = ctx->paper_height;
1725 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1738 page_width = page_height;
1742 pwidth = width / ctx->pages_x;
1743 pheight = height / ctx->pages_y;
1745 if ((page_width > pwidth || page_height > pheight) ||
1746 (page_width < pwidth && page_height < pheight)) {
1747 xscale = pwidth / page_width;
1748 yscale = pheight / page_height;
1750 if (yscale < xscale) {
1757 xscale = yscale = 1;
1762 cairo_save (ctx->cr);
1764 cairo_matrix_t matrix;
1766 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1767 cairo_matrix_init (&matrix,
1771 cairo_transform (ctx->cr, &matrix);
1774 cairo_translate (ctx->cr,
1775 x * (rotate ? pheight : pwidth),
1776 y * (rotate ? pwidth : pheight));
1777 cairo_scale (ctx->cr, xscale, yscale);
1779 poppler_page_render_for_printing (poppler_page, ctx->cr);
1781 ctx->pages_printed++;
1783 cairo_restore (ctx->cr);
1784 #else /* HAVE_CAIRO_PRINT */
1785 if (ctx->format == EV_FILE_FORMAT_PS)
1786 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1787 #endif /* HAVE_CAIRO_PRINT */
1791 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1793 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1794 PdfPrintContext *ctx = pdf_document->print_ctx;
1796 g_return_if_fail (pdf_document->print_ctx != NULL);
1798 #ifdef HAVE_CAIRO_PRINT
1799 cairo_show_page (ctx->cr);
1804 pdf_document_file_exporter_end (EvFileExporter *exporter)
1806 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1808 pdf_print_context_free (pdf_document->print_ctx);
1809 pdf_document->print_ctx = NULL;
1812 static EvFileExporterCapabilities
1813 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1815 return (EvFileExporterCapabilities) (
1816 EV_FILE_EXPORTER_CAN_PAGE_SET |
1817 EV_FILE_EXPORTER_CAN_COPIES |
1818 EV_FILE_EXPORTER_CAN_COLLATE |
1819 EV_FILE_EXPORTER_CAN_REVERSE |
1820 EV_FILE_EXPORTER_CAN_SCALE |
1821 #ifdef HAVE_CAIRO_PRINT
1822 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1825 #ifdef HAVE_CAIRO_PDF
1826 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1828 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1832 pdf_document_file_exporter_iface_init (EvFileExporterInterface *iface)
1834 iface->begin = pdf_document_file_exporter_begin;
1835 iface->begin_page = pdf_document_file_exporter_begin_page;
1836 iface->do_page = pdf_document_file_exporter_do_page;
1837 iface->end_page = pdf_document_file_exporter_end_page;
1838 iface->end = pdf_document_file_exporter_end;
1839 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1842 /* EvDocumentPrint */
1844 pdf_document_print_print_page (EvDocumentPrint *document,
1848 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1850 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1854 pdf_document_document_print_iface_init (EvDocumentPrintInterface *iface)
1856 iface->print_page = pdf_document_print_print_page;
1860 pdf_selection_render_selection (EvSelection *selection,
1861 EvRenderContext *rc,
1862 cairo_surface_t **surface,
1863 EvRectangle *points,
1864 EvRectangle *old_points,
1865 EvSelectionStyle style,
1869 PopplerPage *poppler_page;
1871 PopplerColor text_color, base_color;
1872 double width_points, height_points;
1875 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1877 poppler_page_get_size (poppler_page,
1878 &width_points, &height_points);
1879 width = (int) ((width_points * rc->scale) + 0.5);
1880 height = (int) ((height_points * rc->scale) + 0.5);
1882 text_color.red = text->red;
1883 text_color.green = text->green;
1884 text_color.blue = text->blue;
1886 base_color.red = base->red;
1887 base_color.green = base->green;
1888 base_color.blue = base->blue;
1890 if (*surface == NULL) {
1891 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1896 cr = cairo_create (*surface);
1897 cairo_scale (cr, rc->scale, rc->scale);
1898 cairo_surface_set_device_offset (*surface, 0, 0);
1899 memset (cairo_image_surface_get_data (*surface), 0x00,
1900 cairo_image_surface_get_height (*surface) *
1901 cairo_image_surface_get_stride (*surface));
1902 poppler_page_render_selection (poppler_page,
1904 (PopplerRectangle *)points,
1905 (PopplerRectangle *)old_points,
1906 (PopplerSelectionStyle)style,
1913 pdf_selection_get_selected_text (EvSelection *selection,
1915 EvSelectionStyle style,
1916 EvRectangle *points)
1918 PopplerPage *poppler_page;
1921 poppler_page = POPPLER_PAGE (page->backend_page);
1923 #ifdef HAVE_POPPLER_PAGE_GET_SELECTED_TEXT
1924 retval = poppler_page_get_selected_text (poppler_page,
1925 (PopplerSelectionStyle)style,
1926 (PopplerRectangle *)points);
1931 poppler_page_get_size (poppler_page, NULL, &height);
1933 r.y1 = height - points->y2;
1935 r.y2 = height - points->y1;
1937 retval = poppler_page_get_text (poppler_page,
1938 (PopplerSelectionStyle)style,
1940 #endif /* HAVE_POPPLER_PAGE_GET_SELECTED_TEXT */
1945 static cairo_region_t *
1946 create_region_from_poppler_region (GList *region, gdouble scale)
1949 cairo_region_t *retval;
1951 retval = cairo_region_create ();
1953 for (l = region; l; l = g_list_next (l)) {
1954 PopplerRectangle *rectangle;
1955 cairo_rectangle_int_t rect;
1957 rectangle = (PopplerRectangle *)l->data;
1959 rect.x = (gint) ((rectangle->x1 * scale) + 0.5);
1960 rect.y = (gint) ((rectangle->y1 * scale) + 0.5);
1961 rect.width = (gint) (((rectangle->x2 - rectangle->x1) * scale) + 0.5);
1962 rect.height = (gint) (((rectangle->y2 - rectangle->y1) * scale) + 0.5);
1963 cairo_region_union_rectangle (retval, &rect);
1965 poppler_rectangle_free (rectangle);
1971 static cairo_region_t *
1972 pdf_selection_get_selection_region (EvSelection *selection,
1973 EvRenderContext *rc,
1974 EvSelectionStyle style,
1975 EvRectangle *points)
1977 PopplerPage *poppler_page;
1978 cairo_region_t *retval;
1981 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1982 region = poppler_page_get_selection_region (poppler_page,
1984 (PopplerSelectionStyle)style,
1985 (PopplerRectangle *) points);
1986 retval = create_region_from_poppler_region (region, rc->scale);
1987 g_list_free (region);
1993 pdf_selection_iface_init (EvSelectionInterface *iface)
1995 iface->render_selection = pdf_selection_render_selection;
1996 iface->get_selected_text = pdf_selection_get_selected_text;
1997 iface->get_selection_region = pdf_selection_get_selection_region;
2001 /* EvDocumentText */
2002 static cairo_region_t *
2003 pdf_document_text_get_text_mapping (EvDocumentText *document_text,
2006 PopplerPage *poppler_page;
2007 PopplerRectangle points;
2009 cairo_region_t *retval;
2011 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2013 poppler_page = POPPLER_PAGE (page->backend_page);
2017 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2019 region = poppler_page_get_selection_region (poppler_page, 1.0,
2020 POPPLER_SELECTION_GLYPH,
2022 retval = create_region_from_poppler_region (region, 1.0);
2023 g_list_free (region);
2028 #ifdef HAVE_POPPLER_PAGE_GET_SELECTED_TEXT
2030 pdf_document_text_get_text (EvDocumentText *selection,
2033 PopplerPage *poppler_page;
2035 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2037 poppler_page = POPPLER_PAGE (page->backend_page);
2039 return poppler_page_get_text (poppler_page);
2043 pdf_document_text_get_text (EvDocumentText *selection,
2046 PopplerPage *poppler_page;
2049 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2051 poppler_page = POPPLER_PAGE (page->backend_page);
2055 poppler_page_get_size (poppler_page, &(r.x2), &(r.y2));
2057 return poppler_page_get_text (poppler_page,
2058 POPPLER_SELECTION_WORD,
2061 #endif /* HAVE_POPPLER_PAGE_GET_SELECTED_TEXT */
2063 #ifdef HAVE_POPPLER_PAGE_GET_TEXT_LAYOUT
2065 pdf_document_text_get_text_layout (EvDocumentText *selection,
2067 EvRectangle **areas,
2070 PopplerPage *poppler_page;
2072 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2074 poppler_page = POPPLER_PAGE (page->backend_page);
2076 return poppler_page_get_text_layout (poppler_page, (PopplerRectangle **)areas, n_areas);
2081 pdf_document_text_iface_init (EvDocumentTextInterface *iface)
2083 iface->get_text_mapping = pdf_document_text_get_text_mapping;
2084 iface->get_text = pdf_document_text_get_text;
2085 #ifdef HAVE_POPPLER_PAGE_GET_TEXT_LAYOUT
2086 iface->get_text_layout = pdf_document_text_get_text_layout;
2090 /* Page Transitions */
2092 pdf_document_get_page_duration (EvDocumentTransition *trans,
2095 PdfDocument *pdf_document;
2096 PopplerPage *poppler_page;
2097 gdouble duration = -1;
2099 pdf_document = PDF_DOCUMENT (trans);
2100 poppler_page = poppler_document_get_page (pdf_document->document, page);
2104 duration = poppler_page_get_duration (poppler_page);
2105 g_object_unref (poppler_page);
2110 static EvTransitionEffect *
2111 pdf_document_get_effect (EvDocumentTransition *trans,
2114 PdfDocument *pdf_document;
2115 PopplerPage *poppler_page;
2116 PopplerPageTransition *page_transition;
2117 EvTransitionEffect *effect;
2119 pdf_document = PDF_DOCUMENT (trans);
2120 poppler_page = poppler_document_get_page (pdf_document->document, page);
2125 page_transition = poppler_page_get_transition (poppler_page);
2127 if (!page_transition) {
2128 g_object_unref (poppler_page);
2132 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2133 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2134 "alignment", page_transition->alignment,
2135 "direction", page_transition->direction,
2136 "duration", page_transition->duration,
2137 "angle", page_transition->angle,
2138 "scale", page_transition->scale,
2139 "rectangular", page_transition->rectangular,
2142 poppler_page_transition_free (page_transition);
2143 g_object_unref (poppler_page);
2149 pdf_document_page_transition_iface_init (EvDocumentTransitionInterface *iface)
2151 iface->get_page_duration = pdf_document_get_page_duration;
2152 iface->get_effect = pdf_document_get_effect;
2157 pdf_document_get_crop_box (EvDocument *document,
2161 PdfDocument *pdf_document;
2162 PopplerPage *poppler_page;
2163 PopplerRectangle poppler_rect;
2165 pdf_document = PDF_DOCUMENT (document);
2166 poppler_page = poppler_document_get_page (pdf_document->document, page);
2167 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2168 rect->x1 = poppler_rect.x1;
2169 rect->x2 = poppler_rect.x2;
2170 rect->y1 = poppler_rect.y1;
2171 rect->y2 = poppler_rect.y2;
2174 static EvFormField *
2175 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2177 EvFormField *ev_field = NULL;
2180 gboolean is_read_only;
2182 id = poppler_form_field_get_id (poppler_field);
2183 font_size = poppler_form_field_get_font_size (poppler_field);
2184 is_read_only = poppler_form_field_is_read_only (poppler_field);
2186 switch (poppler_form_field_get_field_type (poppler_field)) {
2187 case POPPLER_FORM_FIELD_TEXT: {
2188 EvFormFieldText *field_text;
2189 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2191 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2192 case POPPLER_FORM_TEXT_NORMAL:
2193 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2195 case POPPLER_FORM_TEXT_MULTILINE:
2196 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2198 case POPPLER_FORM_TEXT_FILE_SELECT:
2199 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2203 ev_field = ev_form_field_text_new (id, ev_text_type);
2204 field_text = EV_FORM_FIELD_TEXT (ev_field);
2206 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2207 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2208 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2209 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2210 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2211 field_text->text = poppler_form_field_text_get_text (poppler_field);
2215 case POPPLER_FORM_FIELD_BUTTON: {
2216 EvFormFieldButton *field_button;
2217 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2219 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2220 case POPPLER_FORM_BUTTON_PUSH:
2221 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2223 case POPPLER_FORM_BUTTON_CHECK:
2224 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2226 case POPPLER_FORM_BUTTON_RADIO:
2227 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2231 ev_field = ev_form_field_button_new (id, ev_button_type);
2232 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2234 field_button->state = poppler_form_field_button_get_state (poppler_field);
2237 case POPPLER_FORM_FIELD_CHOICE: {
2238 EvFormFieldChoice *field_choice;
2239 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2241 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2242 case POPPLER_FORM_CHOICE_COMBO:
2243 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2245 case EV_FORM_FIELD_CHOICE_LIST:
2246 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2250 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2251 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2253 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2254 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2255 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2256 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2258 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2259 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2260 if (field_choice->is_editable)
2261 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2264 case POPPLER_FORM_FIELD_SIGNATURE:
2266 ev_field = ev_form_field_signature_new (id);
2268 case POPPLER_FORM_FIELD_UNKNOWN:
2272 ev_field->font_size = font_size;
2273 ev_field->is_read_only = is_read_only;
2278 static EvMappingList *
2279 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2282 PopplerPage *poppler_page;
2283 GList *retval = NULL;
2288 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2290 poppler_page = POPPLER_PAGE (page->backend_page);
2291 fields = poppler_page_get_form_field_mapping (poppler_page);
2292 poppler_page_get_size (poppler_page, NULL, &height);
2294 for (list = fields; list; list = list->next) {
2295 PopplerFormFieldMapping *mapping;
2296 EvMapping *field_mapping;
2297 EvFormField *ev_field;
2299 mapping = (PopplerFormFieldMapping *)list->data;
2301 ev_field = ev_form_field_from_poppler_field (mapping->field);
2305 field_mapping = g_new0 (EvMapping, 1);
2306 field_mapping->area.x1 = mapping->area.x1;
2307 field_mapping->area.x2 = mapping->area.x2;
2308 field_mapping->area.y1 = height - mapping->area.y2;
2309 field_mapping->area.y2 = height - mapping->area.y1;
2310 field_mapping->data = ev_field;
2311 ev_field->page = EV_PAGE (g_object_ref (page));
2313 g_object_set_data_full (G_OBJECT (ev_field),
2315 g_object_ref (mapping->field),
2316 (GDestroyNotify) g_object_unref);
2318 retval = g_list_prepend (retval, field_mapping);
2321 poppler_page_free_form_field_mapping (fields);
2323 return retval ? ev_mapping_list_new (page->index,
2324 g_list_reverse (retval),
2325 (GDestroyNotify)g_object_unref) : NULL;
2329 pdf_document_forms_document_is_modified (EvDocumentForms *document)
2331 return PDF_DOCUMENT (document)->forms_modified;
2335 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2339 PopplerFormField *poppler_field;
2342 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2346 text = poppler_form_field_text_get_text (poppler_field);
2352 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2356 PopplerFormField *poppler_field;
2358 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2362 poppler_form_field_text_set_text (poppler_field, text);
2363 PDF_DOCUMENT (document)->forms_modified = TRUE;
2367 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2371 PopplerFormField *poppler_field;
2373 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2377 poppler_form_field_button_set_state (poppler_field, state);
2378 PDF_DOCUMENT (document)->forms_modified = TRUE;
2382 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2385 PopplerFormField *poppler_field;
2388 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2392 state = poppler_form_field_button_get_state (poppler_field);
2398 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2402 PopplerFormField *poppler_field;
2405 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2409 text = poppler_form_field_choice_get_item (poppler_field, index);
2415 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2418 PopplerFormField *poppler_field;
2421 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2425 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2431 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2435 PopplerFormField *poppler_field;
2438 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2442 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2448 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2452 PopplerFormField *poppler_field;
2454 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2458 poppler_form_field_choice_select_item (poppler_field, index);
2459 PDF_DOCUMENT (document)->forms_modified = TRUE;
2463 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2467 PopplerFormField *poppler_field;
2469 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2473 poppler_form_field_choice_toggle_item (poppler_field, index);
2474 PDF_DOCUMENT (document)->forms_modified = TRUE;
2478 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2481 PopplerFormField *poppler_field;
2483 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2487 poppler_form_field_choice_unselect_all (poppler_field);
2488 PDF_DOCUMENT (document)->forms_modified = TRUE;
2492 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2496 PopplerFormField *poppler_field;
2498 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2502 poppler_form_field_choice_set_text (poppler_field, text);
2503 PDF_DOCUMENT (document)->forms_modified = TRUE;
2507 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2510 PopplerFormField *poppler_field;
2513 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2517 text = poppler_form_field_choice_get_text (poppler_field);
2523 pdf_document_document_forms_iface_init (EvDocumentFormsInterface *iface)
2525 iface->get_form_fields = pdf_document_forms_get_form_fields;
2526 iface->document_is_modified = pdf_document_forms_document_is_modified;
2527 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2528 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2529 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2530 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2531 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2532 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2533 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2534 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2535 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2536 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2537 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2538 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2543 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2546 PopplerColor *poppler_color;
2548 poppler_color = poppler_annot_get_color (poppler_annot);
2549 if (poppler_color) {
2550 color->red = poppler_color->red;
2551 color->green = poppler_color->green;
2552 color->blue = poppler_color->blue;
2554 g_free (poppler_color);
2555 } /* TODO: else use a default color */
2558 static EvAnnotationTextIcon
2559 get_annot_text_icon (PopplerAnnotText *poppler_annot)
2561 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2562 gchar *icon = poppler_annot_text_get_icon (poppler_annot);
2563 EvAnnotationTextIcon retval;
2566 return EV_ANNOTATION_TEXT_ICON_UNKNOWN;
2568 if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_NOTE) == 0)
2569 retval = EV_ANNOTATION_TEXT_ICON_NOTE;
2570 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_COMMENT) == 0)
2571 retval = EV_ANNOTATION_TEXT_ICON_COMMENT;
2572 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_KEY) == 0)
2573 retval = EV_ANNOTATION_TEXT_ICON_KEY;
2574 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_HELP) == 0)
2575 retval = EV_ANNOTATION_TEXT_ICON_HELP;
2576 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_NEW_PARAGRAPH) == 0)
2577 retval = EV_ANNOTATION_TEXT_ICON_NEW_PARAGRAPH;
2578 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_PARAGRAPH) == 0)
2579 retval = EV_ANNOTATION_TEXT_ICON_PARAGRAPH;
2580 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_INSERT) == 0)
2581 retval = EV_ANNOTATION_TEXT_ICON_INSERT;
2582 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_CROSS) == 0)
2583 retval = EV_ANNOTATION_TEXT_ICON_CROSS;
2584 else if (strcmp (icon, POPPLER_ANNOT_TEXT_ICON_CIRCLE) == 0)
2585 retval = EV_ANNOTATION_TEXT_ICON_CIRCLE;
2587 retval = EV_ANNOTATION_TEXT_ICON_UNKNOWN;
2593 return EV_ANNOTATION_TEXT_ICON_UNKNOWN;
2597 static const gchar *
2598 get_poppler_annot_text_icon (EvAnnotationTextIcon icon)
2600 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2602 case EV_ANNOTATION_TEXT_ICON_NOTE:
2603 return POPPLER_ANNOT_TEXT_ICON_NOTE;
2604 case EV_ANNOTATION_TEXT_ICON_COMMENT:
2605 return POPPLER_ANNOT_TEXT_ICON_COMMENT;
2606 case EV_ANNOTATION_TEXT_ICON_KEY:
2607 return POPPLER_ANNOT_TEXT_ICON_KEY;
2608 case EV_ANNOTATION_TEXT_ICON_HELP:
2609 return POPPLER_ANNOT_TEXT_ICON_HELP;
2610 case EV_ANNOTATION_TEXT_ICON_NEW_PARAGRAPH:
2611 return POPPLER_ANNOT_TEXT_ICON_NEW_PARAGRAPH;
2612 case EV_ANNOTATION_TEXT_ICON_PARAGRAPH:
2613 return POPPLER_ANNOT_TEXT_ICON_PARAGRAPH;
2614 case EV_ANNOTATION_TEXT_ICON_INSERT:
2615 return POPPLER_ANNOT_TEXT_ICON_INSERT;
2616 case EV_ANNOTATION_TEXT_ICON_CROSS:
2617 return POPPLER_ANNOT_TEXT_ICON_CROSS;
2618 case EV_ANNOTATION_TEXT_ICON_CIRCLE:
2619 return POPPLER_ANNOT_TEXT_ICON_CIRCLE;
2620 case EV_ANNOTATION_TEXT_ICON_UNKNOWN:
2622 return POPPLER_ANNOT_TEXT_ICON_NOTE;
2629 static EvAnnotation *
2630 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2633 EvAnnotation *ev_annot = NULL;
2634 const gchar *unimplemented_annot = NULL;
2636 switch (poppler_annot_get_annot_type (poppler_annot)) {
2637 case POPPLER_ANNOT_TEXT: {
2638 PopplerAnnotText *poppler_text;
2639 EvAnnotationText *ev_annot_text;
2641 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2643 ev_annot = ev_annotation_text_new (page);
2645 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2646 ev_annotation_text_set_is_open (ev_annot_text,
2647 poppler_annot_text_get_is_open (poppler_text));
2648 ev_annotation_text_set_icon (ev_annot_text, get_annot_text_icon (poppler_text));
2651 case POPPLER_ANNOT_FILE_ATTACHMENT: {
2652 PopplerAnnotFileAttachment *poppler_annot_attachment;
2653 EvAnnotationAttachment *ev_annot_attachment;
2654 PopplerAttachment *poppler_attachment;
2657 GError *error = NULL;
2659 poppler_annot_attachment = POPPLER_ANNOT_FILE_ATTACHMENT (poppler_annot);
2660 poppler_attachment = poppler_annot_file_attachment_get_attachment (poppler_annot_attachment);
2662 if (poppler_attachment &&
2663 attachment_save_to_buffer (poppler_attachment, &data, &size, &error)) {
2664 EvAttachment *ev_attachment;
2666 ev_attachment = ev_attachment_new (poppler_attachment->name,
2667 poppler_attachment->description,
2668 poppler_attachment->mtime,
2669 poppler_attachment->ctime,
2671 ev_annot = ev_annotation_attachment_new (page, ev_attachment);
2672 g_object_unref (ev_attachment);
2674 g_warning ("%s", error->message);
2675 g_error_free (error);
2678 if (poppler_attachment)
2679 g_object_unref (poppler_attachment);
2682 case POPPLER_ANNOT_LINK:
2683 case POPPLER_ANNOT_WIDGET:
2684 /* Ignore link and widgets annots since they are already handled */
2687 GEnumValue *enum_value;
2689 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2690 poppler_annot_get_annot_type (poppler_annot));
2691 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2695 if (unimplemented_annot) {
2696 g_warning ("Unimplemented annotation: %s, please post a "
2697 "bug report in Evince bugzilla "
2698 "(http://bugzilla.gnome.org) with a testcase.",
2699 unimplemented_annot);
2709 contents = poppler_annot_get_contents (poppler_annot);
2711 ev_annotation_set_contents (ev_annot, contents);
2715 name = poppler_annot_get_name (poppler_annot);
2717 ev_annotation_set_name (ev_annot, name);
2721 modified = poppler_annot_get_modified (poppler_annot);
2722 if (poppler_date_parse (modified, &utime)) {
2723 ev_annotation_set_modified_from_time (ev_annot, utime);
2725 ev_annotation_set_modified (ev_annot, modified);
2729 poppler_annot_color_to_gdk_color (poppler_annot, &color);
2730 ev_annotation_set_color (ev_annot, &color);
2732 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2733 PopplerAnnotMarkup *markup;
2736 PopplerRectangle poppler_rect;
2738 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2740 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2741 EvRectangle ev_rect;
2745 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2747 ev_rect.x1 = poppler_rect.x1;
2748 ev_rect.x2 = poppler_rect.x2;
2749 ev_rect.y1 = height - poppler_rect.y2;
2750 ev_rect.y2 = height - poppler_rect.y1;
2752 is_open = poppler_annot_markup_get_popup_is_open (markup);
2754 g_object_set (ev_annot,
2755 "rectangle", &ev_rect,
2756 "popup_is_open", is_open,
2760 g_object_set (ev_annot,
2765 label = poppler_annot_markup_get_label (markup);
2766 opacity = poppler_annot_markup_get_opacity (markup);
2768 g_object_set (ev_annot,
2780 static EvMappingList *
2781 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2784 GList *retval = NULL;
2785 PdfDocument *pdf_document;
2786 PopplerPage *poppler_page;
2787 EvMappingList *mapping_list;
2793 pdf_document = PDF_DOCUMENT (document_annotations);
2794 poppler_page = POPPLER_PAGE (page->backend_page);
2796 if (pdf_document->annots) {
2797 mapping_list = (EvMappingList *)g_hash_table_lookup (pdf_document->annots,
2798 GINT_TO_POINTER (page->index));
2800 return ev_mapping_list_ref (mapping_list);
2803 annots = poppler_page_get_annot_mapping (poppler_page);
2804 poppler_page_get_size (poppler_page, NULL, &height);
2806 for (list = annots; list; list = list->next) {
2807 PopplerAnnotMapping *mapping;
2808 EvMapping *annot_mapping;
2809 EvAnnotation *ev_annot;
2811 mapping = (PopplerAnnotMapping *)list->data;
2813 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2819 /* Make sure annot has a unique name */
2820 if (!ev_annotation_get_name (ev_annot)) {
2821 gchar *name = g_strdup_printf ("annot-%d-%d", page->index, i);
2823 ev_annotation_set_name (ev_annot, name);
2827 annot_mapping = g_new (EvMapping, 1);
2828 annot_mapping->area.x1 = mapping->area.x1;
2829 annot_mapping->area.x2 = mapping->area.x2;
2830 annot_mapping->area.y1 = height - mapping->area.y2;
2831 annot_mapping->area.y2 = height - mapping->area.y1;
2832 annot_mapping->data = ev_annot;
2834 g_object_set_data_full (G_OBJECT (ev_annot),
2836 g_object_ref (mapping->annot),
2837 (GDestroyNotify) g_object_unref);
2839 retval = g_list_prepend (retval, annot_mapping);
2842 poppler_page_free_annot_mapping (annots);
2847 if (!pdf_document->annots) {
2848 pdf_document->annots = g_hash_table_new_full (g_direct_hash,
2850 (GDestroyNotify)NULL,
2851 (GDestroyNotify)ev_mapping_list_unref);
2854 mapping_list = ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
2855 g_hash_table_insert (pdf_document->annots,
2856 GINT_TO_POINTER (page->index),
2857 ev_mapping_list_ref (mapping_list));
2859 return mapping_list;
2863 pdf_document_annotations_document_is_modified (EvDocumentAnnotations *document_annotations)
2865 return PDF_DOCUMENT (document_annotations)->annots_modified;
2868 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2870 pdf_document_annotations_add_annotation (EvDocumentAnnotations *document_annotations,
2871 EvAnnotation *annot,
2874 PopplerAnnot *poppler_annot;
2875 PdfDocument *pdf_document;
2877 PopplerPage *poppler_page;
2879 EvMappingList *mapping_list;
2880 EvMapping *annot_mapping;
2881 PopplerRectangle poppler_rect;
2883 PopplerColor poppler_color;
2889 pdf_document = PDF_DOCUMENT (document_annotations);
2890 page = ev_annotation_get_page (annot);
2891 poppler_page = POPPLER_PAGE (page->backend_page);
2893 poppler_page_get_size (poppler_page, NULL, &height);
2894 poppler_rect.x1 = rect->x1;
2895 poppler_rect.x2 = rect->x2;
2896 poppler_rect.y1 = height - rect->y2;
2897 poppler_rect.y2 = height - rect->y1;
2898 poppler_annot = poppler_annot_text_new (pdf_document->document, &poppler_rect);
2900 ev_annotation_get_color (annot, &color);
2901 poppler_color.red = color.red;
2902 poppler_color.green = color.green;
2903 poppler_color.blue = color.blue;
2904 poppler_annot_set_color (poppler_annot, &poppler_color);
2906 if (EV_IS_ANNOTATION_MARKUP (annot)) {
2907 EvAnnotationMarkup *markup = EV_ANNOTATION_MARKUP (annot);
2910 if (ev_annotation_markup_has_popup (markup)) {
2911 EvRectangle popup_rect;
2913 ev_annotation_markup_get_rectangle (markup, &popup_rect);
2914 poppler_rect.x1 = popup_rect.x1;
2915 poppler_rect.x2 = popup_rect.x2;
2916 poppler_rect.y1 = height - popup_rect.y2;
2917 poppler_rect.y2 = height - popup_rect.y1;
2918 poppler_annot_markup_set_popup (POPPLER_ANNOT_MARKUP (poppler_annot), &poppler_rect);
2919 poppler_annot_markup_set_popup_is_open (POPPLER_ANNOT_MARKUP (poppler_annot),
2920 ev_annotation_markup_get_popup_is_open (markup));
2923 label = ev_annotation_markup_get_label (markup);
2925 poppler_annot_markup_set_label (POPPLER_ANNOT_MARKUP (poppler_annot), label);
2928 if (EV_IS_ANNOTATION_TEXT (annot)) {
2929 EvAnnotationText *text = EV_ANNOTATION_TEXT (annot);
2930 EvAnnotationTextIcon icon;
2932 icon = ev_annotation_text_get_icon (text);
2933 poppler_annot_text_set_icon (POPPLER_ANNOT_TEXT (poppler_annot),
2934 get_poppler_annot_text_icon (icon));
2936 poppler_page_add_annot (poppler_page, poppler_annot);
2938 annot_mapping = g_new (EvMapping, 1);
2939 annot_mapping->area = *rect;
2940 annot_mapping->data = annot;
2941 g_object_set_data_full (G_OBJECT (annot),
2943 g_object_ref (poppler_annot),
2944 (GDestroyNotify) g_object_unref);
2946 if (pdf_document->annots) {
2947 mapping_list = (EvMappingList *)g_hash_table_lookup (pdf_document->annots,
2948 GINT_TO_POINTER (page->index));
2949 list = ev_mapping_list_get_list (mapping_list);
2950 name = g_strdup_printf ("annot-%d-%d", page->index, g_list_length (list) + 1);
2951 ev_annotation_set_name (annot, name);
2953 list = g_list_append (list, annot_mapping);
2955 pdf_document->annots = g_hash_table_new_full (g_direct_hash,
2957 (GDestroyNotify)NULL,
2958 (GDestroyNotify)ev_mapping_list_unref);
2959 name = g_strdup_printf ("annot-%d-0", page->index);
2960 ev_annotation_set_name (annot, name);
2962 list = g_list_append (list, annot_mapping);
2963 mapping_list = ev_mapping_list_new (page->index, list, (GDestroyNotify)g_object_unref);
2964 g_hash_table_insert (pdf_document->annots,
2965 GINT_TO_POINTER (page->index),
2966 ev_mapping_list_ref (mapping_list));
2969 pdf_document->annots_modified = TRUE;
2971 #endif /* HAVE_POPPLER_PAGE_ADD_ANNOT */
2974 pdf_document_annotations_save_annotation (EvDocumentAnnotations *document_annotations,
2975 EvAnnotation *annot,
2976 EvAnnotationsSaveMask mask)
2978 PopplerAnnot *poppler_annot;
2980 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2984 if (mask & EV_ANNOTATIONS_SAVE_CONTENTS)
2985 poppler_annot_set_contents (poppler_annot,
2986 ev_annotation_get_contents (annot));
2988 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
2989 if (mask & EV_ANNOTATIONS_SAVE_COLOR) {
2993 ev_annotation_get_color (annot, &ev_color);
2994 color.red = ev_color.red;
2995 color.green = ev_color.green;
2996 color.blue = ev_color.blue;
2997 poppler_annot_set_color (poppler_annot, &color);
3000 if (EV_IS_ANNOTATION_MARKUP (annot)) {
3001 EvAnnotationMarkup *ev_markup = EV_ANNOTATION_MARKUP (annot);
3002 PopplerAnnotMarkup *markup = POPPLER_ANNOT_MARKUP (poppler_annot);
3004 if (mask & EV_ANNOTATIONS_SAVE_LABEL)
3005 poppler_annot_markup_set_label (markup, ev_annotation_markup_get_label (ev_markup));
3006 if (mask & EV_ANNOTATIONS_SAVE_OPACITY)
3007 poppler_annot_markup_set_opacity (markup, ev_annotation_markup_get_opacity (ev_markup));
3008 if (mask & EV_ANNOTATIONS_SAVE_POPUP_IS_OPEN)
3009 poppler_annot_markup_set_popup_is_open (markup, ev_annotation_markup_get_popup_is_open (ev_markup));
3012 if (EV_IS_ANNOTATION_TEXT (annot)) {
3013 EvAnnotationText *ev_text = EV_ANNOTATION_TEXT (annot);
3014 PopplerAnnotText *text = POPPLER_ANNOT_TEXT (poppler_annot);
3016 if (mask & EV_ANNOTATIONS_SAVE_TEXT_IS_OPEN) {
3017 poppler_annot_text_set_is_open (text,
3018 ev_annotation_text_get_is_open (ev_text));
3020 if (mask & EV_ANNOTATIONS_SAVE_TEXT_ICON) {
3021 EvAnnotationTextIcon icon;
3023 icon = ev_annotation_text_get_icon (ev_text);
3024 poppler_annot_text_set_icon (text, get_poppler_annot_text_icon (icon));
3027 #endif /* HAVE_POPPLER_PAGE_ADD_ANNOT */
3028 PDF_DOCUMENT (document_annotations)->annots_modified = TRUE;
3032 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsInterface *iface)
3034 iface->get_annotations = pdf_document_annotations_get_annotations;
3035 iface->document_is_modified = pdf_document_annotations_document_is_modified;
3036 #ifdef HAVE_POPPLER_PAGE_ADD_ANNOT
3037 iface->add_annotation = pdf_document_annotations_add_annotation;
3039 iface->save_annotation = pdf_document_annotations_save_annotation;
3043 struct SaveToBufferData {
3049 attachment_save_to_buffer_callback (const gchar *buf,
3054 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
3058 if (sdata->len + count > sdata->max) {
3059 new_max = MAX (sdata->max * 2, sdata->len + count);
3060 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
3062 sdata->buffer = new_buffer;
3063 sdata->max = new_max;
3066 memcpy (sdata->buffer + sdata->len, buf, count);
3067 sdata->len += count;
3073 attachment_save_to_buffer (PopplerAttachment *attachment,
3078 static const gint initial_max = 1024;
3079 struct SaveToBufferData sdata;
3084 sdata.buffer = (gchar *) g_malloc (initial_max);
3085 sdata.max = initial_max;
3088 if (! poppler_attachment_save_to_callback (attachment,
3089 attachment_save_to_buffer_callback,
3092 g_free (sdata.buffer);
3096 *buffer = sdata.buffer;
3097 *buffer_size = sdata.len;
3103 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
3105 PdfDocument *pdf_document = PDF_DOCUMENT (document);
3108 GList *retval = NULL;
3110 attachments = poppler_document_get_attachments (pdf_document->document);
3112 for (list = attachments; list; list = list->next) {
3113 PopplerAttachment *attachment;
3114 EvAttachment *ev_attachment;
3117 GError *error = NULL;
3119 attachment = (PopplerAttachment *) list->data;
3121 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
3122 ev_attachment = ev_attachment_new (attachment->name,
3123 attachment->description,
3128 retval = g_list_prepend (retval, ev_attachment);
3131 g_warning ("%s", error->message);
3132 g_error_free (error);
3138 g_object_unref (attachment);
3141 return g_list_reverse (retval);
3145 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
3147 PdfDocument *pdf_document = PDF_DOCUMENT (document);
3149 return poppler_document_has_attachments (pdf_document->document);
3153 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsInterface *iface)
3155 iface->has_attachments = pdf_document_attachments_has_attachments;
3156 iface->get_attachments = pdf_document_attachments_get_attachments;
3161 pdf_document_layers_has_layers (EvDocumentLayers *document)
3163 PdfDocument *pdf_document = PDF_DOCUMENT (document);
3164 PopplerLayersIter *iter;
3166 iter = poppler_layers_iter_new (pdf_document->document);
3169 poppler_layers_iter_free (iter);
3175 build_layers_tree (PdfDocument *pdf_document,
3176 GtkTreeModel *model,
3177 GtkTreeIter *parent,
3178 PopplerLayersIter *iter)
3181 GtkTreeIter tree_iter;
3182 PopplerLayersIter *child;
3183 PopplerLayer *layer;
3184 EvLayer *ev_layer = NULL;
3189 layer = poppler_layers_iter_get_layer (iter);
3191 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
3192 visible = poppler_layer_is_visible (layer);
3193 rb_group = poppler_layer_get_radio_button_group_id (layer);
3194 pdf_document->layers = g_list_append (pdf_document->layers,
3195 g_object_ref (layer));
3196 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
3197 poppler_layer_is_parent (layer),
3202 title = poppler_layers_iter_get_title (iter);
3203 markup = g_markup_escape_text (title, -1);
3210 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
3211 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
3212 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
3213 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
3214 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
3215 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
3216 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
3217 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
3220 g_object_unref (ev_layer);
3223 child = poppler_layers_iter_get_child (iter);
3225 build_layers_tree (pdf_document, model, &tree_iter, child);
3226 poppler_layers_iter_free (child);
3227 } while (poppler_layers_iter_next (iter));
3230 static GtkTreeModel *
3231 pdf_document_layers_get_layers (EvDocumentLayers *document)
3233 GtkTreeModel *model = NULL;
3234 PdfDocument *pdf_document = PDF_DOCUMENT (document);
3235 PopplerLayersIter *iter;
3237 iter = poppler_layers_iter_new (pdf_document->document);
3239 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
3240 G_TYPE_STRING, /* TITLE */
3241 G_TYPE_OBJECT, /* LAYER */
3242 G_TYPE_BOOLEAN, /* VISIBLE */
3243 G_TYPE_BOOLEAN, /* ENABLED */
3244 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
3245 G_TYPE_INT); /* RBGROUP */
3246 build_layers_tree (pdf_document, model, NULL, iter);
3247 poppler_layers_iter_free (iter);
3253 pdf_document_layers_show_layer (EvDocumentLayers *document,
3256 PdfDocument *pdf_document = PDF_DOCUMENT (document);
3257 guint layer_id = ev_layer_get_id (layer);
3259 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
3263 pdf_document_layers_hide_layer (EvDocumentLayers *document,
3266 PdfDocument *pdf_document = PDF_DOCUMENT (document);
3267 guint layer_id = ev_layer_get_id (layer);
3269 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
3273 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
3276 PdfDocument *pdf_document = PDF_DOCUMENT (document);
3277 guint layer_id = ev_layer_get_id (layer);
3279 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
3283 pdf_document_document_layers_iface_init (EvDocumentLayersInterface *iface)
3285 iface->has_layers = pdf_document_layers_has_layers;
3286 iface->get_layers = pdf_document_layers_get_layers;
3287 iface->show_layer = pdf_document_layers_show_layer;
3288 iface->hide_layer = pdf_document_layers_hide_layer;
3289 iface->layer_is_visible = pdf_document_layers_layer_is_visible;