1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pdfdocument.h: Implementation of EvDocument for PDF
3 * Copyright (C) 2004, Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <poppler-document.h>
27 #include <poppler-page.h>
29 #include <cairo-pdf.h>
34 #include <glib/gi18n-lib.h>
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-mapping.h"
39 #include "ev-document-find.h"
40 #include "ev-document-misc.h"
41 #include "ev-document-links.h"
42 #include "ev-document-images.h"
43 #include "ev-document-fonts.h"
44 #include "ev-document-security.h"
45 #include "ev-document-thumbnails.h"
46 #include "ev-document-transition.h"
47 #include "ev-document-forms.h"
48 #include "ev-document-layers.h"
49 #include "ev-document-print.h"
50 #include "ev-document-annotations.h"
51 #include "ev-selection.h"
52 #include "ev-transition-effect.h"
53 #include "ev-attachment.h"
56 #include <libxml/tree.h>
57 #include <libxml/parser.h>
58 #include <libxml/xpath.h>
59 #include <libxml/xpathInternals.h>
61 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
62 #define HAVE_CAIRO_PRINT
66 PdfDocument *document;
75 EvFileExporterFormat format;
85 #ifdef HAVE_CAIRO_PRINT
88 PopplerPSFile *ps_file;
92 struct _PdfDocumentClass
94 GObjectClass parent_class;
99 GObject parent_instance;
101 PopplerDocument *document;
105 PopplerFontInfo *font_info;
106 PopplerFontsIter *fonts_iter;
107 int fonts_scanned_pages;
109 PdfDocumentSearch *search;
110 PdfPrintContext *print_ctx;
115 static void pdf_document_document_iface_init (EvDocumentIface *iface);
116 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
117 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
118 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
119 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
120 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
121 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
122 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
123 #ifdef HAVE_POPPLER_PAGE_RENDER
124 static void pdf_document_document_print_iface_init (EvDocumentPrintIface *iface);
126 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
127 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
128 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
129 static void pdf_selection_iface_init (EvSelectionIface *iface);
130 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
131 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
135 static int pdf_document_get_n_pages (EvDocument *document);
137 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
139 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
140 PopplerAction *action);
141 static void pdf_document_search_free (PdfDocumentSearch *search);
142 static void pdf_print_context_free (PdfPrintContext *ctx);
144 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
146 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
147 pdf_document_security_iface_init);
148 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
149 pdf_document_document_thumbnails_iface_init);
150 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
151 pdf_document_document_links_iface_init);
152 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
153 pdf_document_document_images_iface_init);
154 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
155 pdf_document_document_forms_iface_init);
156 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
157 pdf_document_document_fonts_iface_init);
158 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
159 pdf_document_document_layers_iface_init);
160 #ifdef HAVE_POPPLER_PAGE_RENDER
161 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
162 pdf_document_document_print_iface_init);
164 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
165 pdf_document_document_annotations_iface_init);
166 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
167 pdf_document_find_iface_init);
168 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
169 pdf_document_file_exporter_iface_init);
170 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
171 pdf_selection_iface_init);
172 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
173 pdf_document_page_transition_iface_init);
177 pdf_document_search_free (PdfDocumentSearch *search)
179 PdfDocument *pdf_document = search->document;
183 if (search->idle != 0)
184 g_source_remove (search->idle);
186 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
187 for (i = 0; i < n_pages; i++) {
188 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
189 g_list_free (search->pages[i]);
191 g_free (search->pages);
193 g_free (search->text);
198 pdf_document_dispose (GObject *object)
200 PdfDocument *pdf_document = PDF_DOCUMENT(object);
202 if (pdf_document->print_ctx) {
203 pdf_print_context_free (pdf_document->print_ctx);
204 pdf_document->print_ctx = NULL;
207 if (pdf_document->search) {
208 pdf_document_search_free (pdf_document->search);
209 pdf_document->search = NULL;
212 if (pdf_document->document) {
213 g_object_unref (pdf_document->document);
216 if (pdf_document->font_info) {
217 poppler_font_info_free (pdf_document->font_info);
220 if (pdf_document->fonts_iter) {
221 poppler_fonts_iter_free (pdf_document->fonts_iter);
224 if (pdf_document->layers) {
225 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
226 g_list_free (pdf_document->layers);
229 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
233 pdf_document_class_init (PdfDocumentClass *klass)
235 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
237 g_object_class->dispose = pdf_document_dispose;
241 pdf_document_init (PdfDocument *pdf_document)
243 pdf_document->password = NULL;
247 convert_error (GError *poppler_error,
250 if (poppler_error == NULL)
253 if (poppler_error->domain == POPPLER_ERROR) {
254 /* convert poppler errors into EvDocument errors */
255 gint code = EV_DOCUMENT_ERROR_INVALID;
256 if (poppler_error->code == POPPLER_ERROR_INVALID)
257 code = EV_DOCUMENT_ERROR_INVALID;
258 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
259 code = EV_DOCUMENT_ERROR_ENCRYPTED;
261 g_set_error_literal (error,
264 poppler_error->message);
266 g_error_free (poppler_error);
268 g_propagate_error (error, poppler_error);
275 pdf_document_save (EvDocument *document,
279 PdfDocument *pdf_document = PDF_DOCUMENT (document);
281 GError *poppler_error = NULL;
283 if (pdf_document->modified) {
284 retval = poppler_document_save (pdf_document->document,
285 uri, &poppler_error);
287 retval = poppler_document_save_a_copy (pdf_document->document,
288 uri, &poppler_error);
292 convert_error (poppler_error, error);
298 pdf_document_load (EvDocument *document,
302 GError *poppler_error = NULL;
303 PdfDocument *pdf_document = PDF_DOCUMENT (document);
305 pdf_document->document =
306 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
308 if (pdf_document->document == NULL) {
309 convert_error (poppler_error, error);
317 pdf_document_get_n_pages (EvDocument *document)
319 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
323 pdf_document_get_page (EvDocument *document,
326 PdfDocument *pdf_document = PDF_DOCUMENT (document);
327 PopplerPage *poppler_page;
330 poppler_page = poppler_document_get_page (pdf_document->document, index);
331 page = ev_page_new (index);
332 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
333 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
334 g_object_unref (poppler_page);
340 pdf_document_get_page_size (EvDocument *document,
345 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
347 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
351 pdf_document_get_page_label (EvDocument *document,
356 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
358 g_object_get (G_OBJECT (page->backend_page),
365 pdf_document_has_attachments (EvDocument *document)
367 PdfDocument *pdf_document;
369 pdf_document = PDF_DOCUMENT (document);
371 return poppler_document_has_attachments (pdf_document->document);
374 struct SaveToBufferData {
380 attachment_save_to_buffer_callback (const gchar *buf,
385 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
389 if (sdata->len + count > sdata->max) {
390 new_max = MAX (sdata->max * 2, sdata->len + count);
391 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
393 sdata->buffer = new_buffer;
394 sdata->max = new_max;
397 memcpy (sdata->buffer + sdata->len, buf, count);
404 attachment_save_to_buffer (PopplerAttachment *attachment,
409 static const gint initial_max = 1024;
410 struct SaveToBufferData sdata;
415 sdata.buffer = (gchar *) g_malloc (initial_max);
416 sdata.max = initial_max;
419 if (! poppler_attachment_save_to_callback (attachment,
420 attachment_save_to_buffer_callback,
423 g_free (sdata.buffer);
427 *buffer = sdata.buffer;
428 *buffer_size = sdata.len;
434 pdf_document_get_attachments (EvDocument *document)
436 PdfDocument *pdf_document;
439 GList *retval = NULL;
441 pdf_document = PDF_DOCUMENT (document);
443 if (!pdf_document_has_attachments (document))
446 attachments = poppler_document_get_attachments (pdf_document->document);
448 for (list = attachments; list; list = list->next) {
449 PopplerAttachment *attachment;
450 EvAttachment *ev_attachment;
453 GError *error = NULL;
455 attachment = (PopplerAttachment *) list->data;
457 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
458 ev_attachment = ev_attachment_new (attachment->name,
459 attachment->description,
464 retval = g_list_prepend (retval, ev_attachment);
467 g_warning ("%s", error->message);
468 g_error_free (error);
474 g_object_unref (attachment);
477 return g_list_reverse (retval);
480 static cairo_surface_t *
481 pdf_page_render (PopplerPage *page,
486 cairo_surface_t *surface;
488 #ifdef HAVE_POPPLER_PAGE_RENDER
491 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
493 cr = cairo_create (surface);
495 switch (rc->rotation) {
497 cairo_translate (cr, width, 0);
500 cairo_translate (cr, width, height);
503 cairo_translate (cr, 0, height);
506 cairo_translate (cr, 0, 0);
508 cairo_scale (cr, rc->scale, rc->scale);
509 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
510 poppler_page_render (page, cr);
512 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
513 cairo_set_source_rgb (cr, 1., 1., 1.);
517 #else /* HAVE_POPPLER_PAGE_RENDER */
520 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
524 poppler_page_render_to_pixbuf (page,
530 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
531 g_object_unref (pixbuf);
532 #endif /* HAVE_POPPLER_PAGE_RENDER */
537 static cairo_surface_t *
538 pdf_document_render (EvDocument *document,
541 PdfDocument *pdf_document;
542 PopplerPage *poppler_page;
543 double width_points, height_points;
546 poppler_page = POPPLER_PAGE (rc->page->backend_page);
548 poppler_page_get_size (poppler_page,
549 &width_points, &height_points);
551 if (rc->rotation == 90 || rc->rotation == 270) {
552 width = (int) ((height_points * rc->scale) + 0.5);
553 height = (int) ((width_points * rc->scale) + 0.5);
555 width = (int) ((width_points * rc->scale) + 0.5);
556 height = (int) ((height_points * rc->scale) + 0.5);
559 return pdf_page_render (poppler_page,
563 /* EvDocumentSecurity */
566 pdf_document_has_document_security (EvDocumentSecurity *document_security)
568 /* FIXME: do we really need to have this? */
573 pdf_document_set_password (EvDocumentSecurity *document_security,
574 const char *password)
576 PdfDocument *document = PDF_DOCUMENT (document_security);
578 if (document->password)
579 g_free (document->password);
581 document->password = g_strdup (password);
586 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 */
588 pdf_document_get_format_from_metadata (const char *metadata)
591 xmlXPathContextPtr xpathCtx;
592 xmlXPathObjectPtr xpathObj;
593 xmlChar *part = NULL;
594 xmlChar *conf = NULL;
598 doc = xmlParseMemory (metadata, strlen (metadata));
600 return NULL; /* invalid xml metadata */
602 xpathCtx = xmlXPathNewContext (doc);
603 if (xpathCtx == NULL) {
605 return NULL; /* invalid xpath context */
608 /* add pdf/a namespaces */
609 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
610 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
611 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
613 /* reads pdf/a part */
614 /* first syntax: child node */
615 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
616 if (xpathObj != NULL) {
617 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
618 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
620 xmlXPathFreeObject (xpathObj);
623 /* second syntax: attribute */
624 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
625 if (xpathObj != NULL) {
626 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
627 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
629 xmlXPathFreeObject (xpathObj);
633 /* reads pdf/a conformance */
634 /* first syntax: child node */
635 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
636 if (xpathObj != NULL) {
637 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
638 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
640 xmlXPathFreeObject (xpathObj);
643 /* second syntax: attribute */
644 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
645 if (xpathObj != NULL) {
646 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
647 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
649 xmlXPathFreeObject (xpathObj);
653 if (part != NULL && conf != NULL) {
654 /* makes conf lowercase */
655 for (i = 0; conf[i]; i++)
656 conf[i] = g_ascii_tolower (conf[i]);
659 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
665 xmlXPathFreeContext (xpathCtx);
672 static EvDocumentInfo *
673 pdf_document_get_info (EvDocument *document)
675 EvDocumentInfo *info;
676 PopplerPageLayout layout;
677 PopplerPageMode mode;
678 PopplerViewerPreferences view_prefs;
679 PopplerPermissions permissions;
684 info = g_new0 (EvDocumentInfo, 1);
686 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
687 EV_DOCUMENT_INFO_FORMAT |
688 EV_DOCUMENT_INFO_AUTHOR |
689 EV_DOCUMENT_INFO_SUBJECT |
690 EV_DOCUMENT_INFO_KEYWORDS |
691 EV_DOCUMENT_INFO_LAYOUT |
692 EV_DOCUMENT_INFO_START_MODE |
693 EV_DOCUMENT_INFO_PERMISSIONS |
694 EV_DOCUMENT_INFO_UI_HINTS |
695 EV_DOCUMENT_INFO_CREATOR |
696 EV_DOCUMENT_INFO_PRODUCER |
697 EV_DOCUMENT_INFO_CREATION_DATE |
698 EV_DOCUMENT_INFO_MOD_DATE |
699 EV_DOCUMENT_INFO_LINEARIZED |
700 EV_DOCUMENT_INFO_N_PAGES |
701 EV_DOCUMENT_INFO_SECURITY |
702 EV_DOCUMENT_INFO_PAPER_SIZE;
704 g_object_get (PDF_DOCUMENT (document)->document,
705 "title", &(info->title),
706 "format", &(info->format),
707 "author", &(info->author),
708 "subject", &(info->subject),
709 "keywords", &(info->keywords),
711 "page-layout", &layout,
712 "viewer-preferences", &view_prefs,
713 "permissions", &permissions,
714 "creator", &(info->creator),
715 "producer", &(info->producer),
716 "creation-date", &(info->creation_date),
717 "mod-date", &(info->modified_date),
718 "linearized", &(info->linearized),
719 "metadata", &metadata,
722 if (metadata != NULL) {
723 fmt = pdf_document_get_format_from_metadata(metadata);
725 g_free (info->format);
731 info->n_pages = ev_document_get_n_pages (document);
733 if (info->n_pages > 0) {
734 page = ev_document_get_page (document, 0);
735 ev_document_get_page_size (document, page,
736 &(info->paper_width),
737 &(info->paper_height));
738 g_object_unref (page);
742 info->paper_width = info->paper_width / 72.0f * 25.4f;
743 info->paper_height = info->paper_height / 72.0f * 25.4f;
747 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
748 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
750 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
751 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
753 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
754 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
756 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
757 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
758 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
759 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
761 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
762 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
769 case POPPLER_PAGE_MODE_NONE:
770 info->mode = EV_DOCUMENT_MODE_NONE;
772 case POPPLER_PAGE_MODE_USE_THUMBS:
773 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
775 case POPPLER_PAGE_MODE_USE_OC:
776 info->mode = EV_DOCUMENT_MODE_USE_OC;
778 case POPPLER_PAGE_MODE_FULL_SCREEN:
779 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
781 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
782 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
788 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
789 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
791 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
792 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
794 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
795 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
797 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
798 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
800 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
801 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
803 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
804 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
806 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
807 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
810 info->permissions = 0;
811 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
812 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
814 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
815 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
817 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
818 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
820 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
821 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
824 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
825 /* translators: this is the document security state */
826 info->security = g_strdup (_("Yes"));
828 /* translators: this is the document security state */
829 info->security = g_strdup (_("No"));
836 pdf_document_document_iface_init (EvDocumentIface *iface)
838 iface->save = pdf_document_save;
839 iface->load = pdf_document_load;
840 iface->get_n_pages = pdf_document_get_n_pages;
841 iface->get_page = pdf_document_get_page;
842 iface->get_page_size = pdf_document_get_page_size;
843 iface->get_page_label = pdf_document_get_page_label;
844 iface->has_attachments = pdf_document_has_attachments;
845 iface->get_attachments = pdf_document_get_attachments;
846 iface->render = pdf_document_render;
847 iface->get_info = pdf_document_get_info;
851 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
853 iface->has_document_security = pdf_document_has_document_security;
854 iface->set_password = pdf_document_set_password;
858 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
860 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
863 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
865 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
869 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
872 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
875 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
877 if (pdf_document->font_info == NULL) {
878 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
881 if (pdf_document->fonts_iter) {
882 poppler_fonts_iter_free (pdf_document->fonts_iter);
885 pdf_document->fonts_scanned_pages += n_pages;
887 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
888 &pdf_document->fonts_iter);
890 pdf_document->fonts_scanned_pages = 0;
891 poppler_font_info_free (pdf_document->font_info);
892 pdf_document->font_info = NULL;
899 font_type_to_string (PopplerFontType type)
902 case POPPLER_FONT_TYPE_TYPE1:
904 case POPPLER_FONT_TYPE_TYPE1C:
906 case POPPLER_FONT_TYPE_TYPE3:
908 case POPPLER_FONT_TYPE_TRUETYPE:
909 return _("TrueType");
910 case POPPLER_FONT_TYPE_CID_TYPE0:
911 return _("Type 1 (CID)");
912 case POPPLER_FONT_TYPE_CID_TYPE0C:
913 return _("Type 1C (CID)");
914 case POPPLER_FONT_TYPE_CID_TYPE2:
915 return _("TrueType (CID)");
917 return _("Unknown font type");
922 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
925 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
926 PopplerFontsIter *iter = pdf_document->fonts_iter;
928 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
934 GtkTreeIter list_iter;
937 const char *embedded;
940 name = poppler_fonts_iter_get_name (iter);
946 type = font_type_to_string (
947 poppler_fonts_iter_get_font_type (iter));
949 if (poppler_fonts_iter_is_embedded (iter)) {
950 if (poppler_fonts_iter_is_subset (iter))
951 embedded = _("Embedded subset");
953 embedded = _("Embedded");
955 embedded = _("Not embedded");
958 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
960 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
961 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
962 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
963 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
967 } while (poppler_fonts_iter_next (iter));
971 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
973 iface->fill_model = pdf_document_fonts_fill_model;
974 iface->scan = pdf_document_fonts_scan;
975 iface->get_progress = pdf_document_fonts_get_progress;
979 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
981 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
982 PopplerIndexIter *iter;
984 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
986 iter = poppler_index_iter_new (pdf_document->document);
989 poppler_index_iter_free (iter);
995 ev_link_dest_from_dest (PdfDocument *pdf_document,
998 EvLinkDest *ev_dest = NULL;
999 const char *unimplemented_dest = NULL;
1001 g_assert (dest != NULL);
1003 switch (dest->type) {
1004 case POPPLER_DEST_XYZ: {
1005 PopplerPage *poppler_page;
1008 poppler_page = poppler_document_get_page (pdf_document->document,
1009 MAX (0, dest->page_num - 1));
1010 poppler_page_get_size (poppler_page, NULL, &height);
1011 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1013 height - MIN (height, dest->top),
1018 g_object_unref (poppler_page);
1021 case POPPLER_DEST_FITB:
1022 case POPPLER_DEST_FIT:
1023 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1025 case POPPLER_DEST_FITBH:
1026 case POPPLER_DEST_FITH: {
1027 PopplerPage *poppler_page;
1030 poppler_page = poppler_document_get_page (pdf_document->document,
1031 MAX (0, dest->page_num - 1));
1032 poppler_page_get_size (poppler_page, NULL, &height);
1033 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1034 height - MIN (height, dest->top),
1036 g_object_unref (poppler_page);
1039 case POPPLER_DEST_FITBV:
1040 case POPPLER_DEST_FITV:
1041 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1045 case POPPLER_DEST_FITR: {
1046 PopplerPage *poppler_page;
1049 poppler_page = poppler_document_get_page (pdf_document->document,
1050 MAX (0, dest->page_num - 1));
1051 poppler_page_get_size (poppler_page, NULL, &height);
1052 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1054 height - MIN (height, dest->bottom),
1056 height - MIN (height, dest->top));
1057 g_object_unref (poppler_page);
1060 case POPPLER_DEST_NAMED:
1061 ev_dest = ev_link_dest_new_named (dest->named_dest);
1063 case POPPLER_DEST_UNKNOWN:
1064 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1068 if (unimplemented_dest) {
1069 g_warning ("Unimplemented destination: %s, please post a "
1070 "bug report in Evince bugzilla "
1071 "(http://bugzilla.gnome.org) with a testcase.",
1072 unimplemented_dest);
1076 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1082 ev_link_from_action (PdfDocument *pdf_document,
1083 PopplerAction *action)
1085 EvLink *link = NULL;
1086 EvLinkAction *ev_action = NULL;
1087 const char *unimplemented_action = NULL;
1089 switch (action->type) {
1090 case POPPLER_ACTION_NONE:
1092 case POPPLER_ACTION_GOTO_DEST: {
1095 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1096 ev_action = ev_link_action_new_dest (dest);
1099 case POPPLER_ACTION_GOTO_REMOTE: {
1102 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1103 ev_action = ev_link_action_new_remote (dest,
1104 action->goto_remote.file_name);
1108 case POPPLER_ACTION_LAUNCH:
1109 ev_action = ev_link_action_new_launch (action->launch.file_name,
1110 action->launch.params);
1112 case POPPLER_ACTION_URI:
1113 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1115 case POPPLER_ACTION_NAMED:
1116 ev_action = ev_link_action_new_named (action->named.named_dest);
1118 case POPPLER_ACTION_MOVIE:
1119 unimplemented_action = "POPPLER_ACTION_MOVIE";
1121 case POPPLER_ACTION_UNKNOWN:
1122 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1125 if (unimplemented_action) {
1126 g_warning ("Unimplemented action: %s, please post a bug report "
1127 "in Evince bugzilla (http://bugzilla.gnome.org) "
1128 "with a testcase.", unimplemented_action);
1131 link = ev_link_new (action->any.title, ev_action);
1137 build_tree (PdfDocument *pdf_document,
1138 GtkTreeModel *model,
1139 GtkTreeIter *parent,
1140 PopplerIndexIter *iter)
1144 GtkTreeIter tree_iter;
1145 PopplerIndexIter *child;
1146 PopplerAction *action;
1147 EvLink *link = NULL;
1151 action = poppler_index_iter_get_action (iter);
1152 expand = poppler_index_iter_is_open (iter);
1157 switch (action->type) {
1158 case POPPLER_ACTION_GOTO_DEST: {
1159 /* For bookmarks, solve named destinations */
1160 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1162 EvLinkDest *ev_dest = NULL;
1163 EvLinkAction *ev_action;
1165 dest = poppler_document_find_dest (pdf_document->document,
1166 action->goto_dest.dest->named_dest);
1168 link = ev_link_from_action (pdf_document, action);
1172 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1173 poppler_dest_free (dest);
1175 ev_action = ev_link_action_new_dest (ev_dest);
1176 link = ev_link_new (action->any.title, ev_action);
1178 link = ev_link_from_action (pdf_document, action);
1183 link = ev_link_from_action (pdf_document, action);
1187 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1188 poppler_action_free (action);
1190 g_object_unref (link);
1195 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1196 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1198 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1199 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1200 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1201 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1204 g_free (title_markup);
1205 g_object_unref (link);
1207 child = poppler_index_iter_get_child (iter);
1209 build_tree (pdf_document, model, &tree_iter, child);
1210 poppler_index_iter_free (child);
1211 poppler_action_free (action);
1213 } while (poppler_index_iter_next (iter));
1216 static GtkTreeModel *
1217 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1219 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1220 GtkTreeModel *model = NULL;
1221 PopplerIndexIter *iter;
1223 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1225 iter = poppler_index_iter_new (pdf_document->document);
1226 /* Create the model if we have items*/
1228 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1233 build_tree (pdf_document, model, NULL, iter);
1234 poppler_index_iter_free (iter);
1241 pdf_document_links_get_links (EvDocumentLinks *document_links,
1244 PdfDocument *pdf_document;
1245 PopplerPage *poppler_page;
1246 GList *retval = NULL;
1247 GList *mapping_list;
1251 pdf_document = PDF_DOCUMENT (document_links);
1252 poppler_page = poppler_document_get_page (pdf_document->document,
1254 mapping_list = poppler_page_get_link_mapping (poppler_page);
1255 poppler_page_get_size (poppler_page, NULL, &height);
1257 for (list = mapping_list; list; list = list->next) {
1258 PopplerLinkMapping *link_mapping;
1259 EvMapping *ev_link_mapping;
1261 link_mapping = (PopplerLinkMapping *)list->data;
1262 ev_link_mapping = g_new (EvMapping, 1);
1263 ev_link_mapping->data = ev_link_from_action (pdf_document,
1264 link_mapping->action);
1265 ev_link_mapping->area.x1 = link_mapping->area.x1;
1266 ev_link_mapping->area.x2 = link_mapping->area.x2;
1267 /* Invert this for X-style coordinates */
1268 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1269 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1271 retval = g_list_prepend (retval, ev_link_mapping);
1274 poppler_page_free_link_mapping (mapping_list);
1275 g_object_unref (poppler_page);
1277 return g_list_reverse (retval);
1281 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1282 const gchar *link_name)
1284 PdfDocument *pdf_document;
1286 EvLinkDest *ev_dest = NULL;
1288 pdf_document = PDF_DOCUMENT (document_links);
1289 dest = poppler_document_find_dest (pdf_document->document,
1292 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1293 poppler_dest_free (dest);
1300 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1302 iface->has_document_links = pdf_document_links_has_document_links;
1303 iface->get_links_model = pdf_document_links_get_links_model;
1304 iface->get_links = pdf_document_links_get_links;
1305 iface->find_link_dest = pdf_document_links_find_link_dest;
1309 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1312 GList *retval = NULL;
1313 PdfDocument *pdf_document;
1314 PopplerPage *poppler_page;
1315 GList *mapping_list;
1318 pdf_document = PDF_DOCUMENT (document_images);
1319 poppler_page = poppler_document_get_page (pdf_document->document, page);
1320 mapping_list = poppler_page_get_image_mapping (poppler_page);
1322 for (list = mapping_list; list; list = list->next) {
1323 PopplerImageMapping *image_mapping;
1324 EvMapping *ev_image_mapping;
1326 image_mapping = (PopplerImageMapping *)list->data;
1328 ev_image_mapping = g_new (EvMapping, 1);
1330 ev_image_mapping->data = ev_image_new (page, image_mapping->image_id);
1331 ev_image_mapping->area.x1 = image_mapping->area.x1;
1332 ev_image_mapping->area.y1 = image_mapping->area.y1;
1333 ev_image_mapping->area.x2 = image_mapping->area.x2;
1334 ev_image_mapping->area.y2 = image_mapping->area.y2;
1336 retval = g_list_prepend (retval, ev_image_mapping);
1339 poppler_page_free_image_mapping (mapping_list);
1340 g_object_unref (poppler_page);
1342 return g_list_reverse (retval);
1346 pdf_document_images_get_image (EvDocumentImages *document_images,
1349 GdkPixbuf *retval = NULL;
1350 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1351 PdfDocument *pdf_document;
1352 PopplerPage *poppler_page;
1353 cairo_surface_t *surface;
1355 pdf_document = PDF_DOCUMENT (document_images);
1356 poppler_page = poppler_document_get_page (pdf_document->document,
1357 ev_image_get_page (image));
1359 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1361 retval = ev_document_misc_pixbuf_from_surface (surface);
1362 cairo_surface_destroy (surface);
1365 g_object_unref (poppler_page);
1371 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1373 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1374 iface->get_image = pdf_document_images_get_image;
1378 make_thumbnail_for_page (PopplerPage *poppler_page,
1379 EvRenderContext *rc,
1385 #ifdef POPPLER_WITH_GDK
1386 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1388 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1390 ev_document_fc_mutex_lock ();
1391 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1393 rc->scale, rc->rotation, pixbuf);
1394 ev_document_fc_mutex_unlock ();
1396 cairo_surface_t *surface;
1398 ev_document_fc_mutex_lock ();
1399 surface = pdf_page_render (poppler_page, width, height, rc);
1400 ev_document_fc_mutex_unlock ();
1402 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1403 cairo_surface_destroy (surface);
1404 #endif /* POPPLER_WITH_GDK */
1410 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1411 EvRenderContext *rc,
1414 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1415 PopplerPage *poppler_page;
1416 GdkPixbuf *pixbuf = NULL;
1417 GdkPixbuf *border_pixbuf;
1420 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1422 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1423 rc, &width, &height);
1425 #ifdef POPPLER_WITH_GDK
1426 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1428 cairo_surface_t *surface;
1430 surface = poppler_page_get_thumbnail (poppler_page);
1432 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1433 cairo_surface_destroy (surface);
1435 #endif /* POPPLER_WITH_GDK */
1437 if (pixbuf != NULL) {
1438 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1439 gdk_pixbuf_get_height (pixbuf) :
1440 gdk_pixbuf_get_width (pixbuf);
1442 if (thumb_width == width) {
1443 GdkPixbuf *rotated_pixbuf;
1445 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1446 (GdkPixbufRotation) (360 - rc->rotation));
1447 g_object_unref (pixbuf);
1448 pixbuf = rotated_pixbuf;
1450 /* The provided thumbnail has a different size */
1451 g_object_unref (pixbuf);
1452 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1455 /* There is no provided thumbnail. We need to make one. */
1456 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1459 if (border && pixbuf) {
1460 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1461 g_object_unref (pixbuf);
1462 pixbuf = border_pixbuf;
1469 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1470 EvRenderContext *rc,
1474 double page_width, page_height;
1476 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1477 &page_width, &page_height);
1479 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1480 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1482 if (rc->rotation == 90 || rc->rotation == 270) {
1492 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1494 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1495 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1500 pdf_document_find_find_text (EvDocumentFind *document_find,
1503 gboolean case_sensitive)
1506 PopplerPage *poppler_page;
1509 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1510 g_return_val_if_fail (text != NULL, NULL);
1512 poppler_page = POPPLER_PAGE (page->backend_page);
1514 matches = poppler_page_find_text (poppler_page, text);
1518 poppler_page_get_size (poppler_page, NULL, &height);
1519 for (l = matches; l && l->data; l = g_list_next (l)) {
1520 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1524 rect->y1 = height - rect->y2;
1525 rect->y2 = height - tmp;
1532 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1534 iface->find_text = pdf_document_find_find_text;
1538 pdf_print_context_free (PdfPrintContext *ctx)
1543 #ifdef HAVE_CAIRO_PRINT
1545 cairo_destroy (ctx->cr);
1550 poppler_ps_file_free (ctx->ps_file);
1551 ctx->ps_file = NULL;
1558 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1559 EvFileExporterContext *fc)
1561 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1562 PdfPrintContext *ctx;
1563 #ifdef HAVE_CAIRO_PRINT
1564 gdouble width, height;
1565 cairo_surface_t *surface = NULL;
1568 if (pdf_document->print_ctx)
1569 pdf_print_context_free (pdf_document->print_ctx);
1570 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1571 ctx = pdf_document->print_ctx;
1572 ctx->format = fc->format;
1574 #ifdef HAVE_CAIRO_PRINT
1575 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1577 ctx->paper_width = fc->paper_width;
1578 ctx->paper_height = fc->paper_height;
1580 switch (fc->pages_per_sheet) {
1608 ctx->pages_printed = 0;
1610 switch (fc->format) {
1611 case EV_FILE_FORMAT_PS:
1612 #ifdef HAVE_CAIRO_PS
1613 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1616 case EV_FILE_FORMAT_PDF:
1617 #ifdef HAVE_CAIRO_PDF
1618 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1622 g_assert_not_reached ();
1625 ctx->cr = cairo_create (surface);
1626 cairo_surface_destroy (surface);
1628 #else /* HAVE_CAIRO_PRINT */
1629 if (ctx->format == EV_FILE_FORMAT_PS) {
1630 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1631 fc->filename, fc->first_page,
1632 fc->last_page - fc->first_page + 1);
1633 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1634 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1636 #endif /* HAVE_CAIRO_PRINT */
1640 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1642 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1643 PdfPrintContext *ctx = pdf_document->print_ctx;
1645 g_return_if_fail (pdf_document->print_ctx != NULL);
1647 ctx->pages_printed = 0;
1649 #ifdef HAVE_CAIRO_PRINT
1650 if (ctx->paper_width > ctx->paper_height) {
1651 if (ctx->format == EV_FILE_FORMAT_PS) {
1652 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1655 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1656 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1661 #endif /* HAVE_CAIRO_PRINT */
1665 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1666 EvRenderContext *rc)
1668 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1669 PdfPrintContext *ctx = pdf_document->print_ctx;
1670 PopplerPage *poppler_page;
1671 #ifdef HAVE_CAIRO_PRINT
1672 gdouble page_width, page_height;
1675 gdouble width, height;
1676 gdouble pwidth, pheight;
1677 gdouble xscale, yscale;
1680 g_return_if_fail (pdf_document->print_ctx != NULL);
1682 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1684 #ifdef HAVE_CAIRO_PRINT
1685 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1686 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1687 poppler_page_get_size (poppler_page, &page_width, &page_height);
1689 if (page_width > page_height && page_width > ctx->paper_width) {
1695 /* Use always portrait mode and rotate when necessary */
1696 if (ctx->paper_width > ctx->paper_height) {
1697 width = ctx->paper_height;
1698 height = ctx->paper_width;
1701 width = ctx->paper_width;
1702 height = ctx->paper_height;
1705 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1718 page_width = page_height;
1722 pwidth = width / ctx->pages_x;
1723 pheight = height / ctx->pages_y;
1725 if ((page_width > pwidth || page_height > pheight) ||
1726 (page_width < pwidth && page_height < pheight)) {
1727 xscale = pwidth / page_width;
1728 yscale = pheight / page_height;
1730 if (yscale < xscale) {
1737 xscale = yscale = 1;
1742 cairo_save (ctx->cr);
1744 cairo_matrix_t matrix;
1746 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1747 cairo_matrix_init (&matrix,
1751 cairo_transform (ctx->cr, &matrix);
1754 cairo_translate (ctx->cr,
1755 x * (rotate ? pheight : pwidth),
1756 y * (rotate ? pwidth : pheight));
1757 cairo_scale (ctx->cr, xscale, yscale);
1759 poppler_page_render_for_printing (poppler_page, ctx->cr);
1761 ctx->pages_printed++;
1763 cairo_restore (ctx->cr);
1764 #else /* HAVE_CAIRO_PRINT */
1765 if (ctx->format == EV_FILE_FORMAT_PS)
1766 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1767 #endif /* HAVE_CAIRO_PRINT */
1771 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1773 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1774 PdfPrintContext *ctx = pdf_document->print_ctx;
1776 g_return_if_fail (pdf_document->print_ctx != NULL);
1778 #ifdef HAVE_CAIRO_PRINT
1779 cairo_show_page (ctx->cr);
1784 pdf_document_file_exporter_end (EvFileExporter *exporter)
1786 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1788 pdf_print_context_free (pdf_document->print_ctx);
1789 pdf_document->print_ctx = NULL;
1792 static EvFileExporterCapabilities
1793 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1795 return (EvFileExporterCapabilities) (
1796 EV_FILE_EXPORTER_CAN_PAGE_SET |
1797 EV_FILE_EXPORTER_CAN_COPIES |
1798 EV_FILE_EXPORTER_CAN_COLLATE |
1799 EV_FILE_EXPORTER_CAN_REVERSE |
1800 EV_FILE_EXPORTER_CAN_SCALE |
1801 #ifdef HAVE_CAIRO_PRINT
1802 #ifdef HAVE_POPPLER_PAGE_RENDER
1803 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1807 #ifdef HAVE_CAIRO_PDF
1808 #ifdef HAVE_POPPLER_PAGE_RENDER
1809 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1812 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1816 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1818 iface->begin = pdf_document_file_exporter_begin;
1819 iface->begin_page = pdf_document_file_exporter_begin_page;
1820 iface->do_page = pdf_document_file_exporter_do_page;
1821 iface->end_page = pdf_document_file_exporter_end_page;
1822 iface->end = pdf_document_file_exporter_end;
1823 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1826 #ifdef HAVE_POPPLER_PAGE_RENDER
1827 /* EvDocumentPrint */
1829 pdf_document_print_print_page (EvDocumentPrint *document,
1833 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1835 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1839 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1841 iface->print_page = pdf_document_print_print_page;
1843 #endif /* HAVE_POPPLER_PAGE_RENDER */
1846 pdf_selection_render_selection (EvSelection *selection,
1847 EvRenderContext *rc,
1848 cairo_surface_t **surface,
1849 EvRectangle *points,
1850 EvRectangle *old_points,
1851 EvSelectionStyle style,
1855 PopplerPage *poppler_page;
1856 double width_points, height_points;
1859 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1861 poppler_page_get_size (poppler_page,
1862 &width_points, &height_points);
1863 width = (int) ((width_points * rc->scale) + 0.5);
1864 height = (int) ((height_points * rc->scale) + 0.5);
1866 #ifdef HAVE_POPPLER_PAGE_RENDER
1868 PopplerColor text_color, base_color;
1870 text_color.red = text->red;
1871 text_color.green = text->green;
1872 text_color.blue = text->blue;
1874 base_color.red = base->red;
1875 base_color.green = base->green;
1876 base_color.blue = base->blue;
1878 if (*surface == NULL) {
1879 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1884 cr = cairo_create (*surface);
1885 cairo_scale (cr, rc->scale, rc->scale);
1886 cairo_surface_set_device_offset (*surface, 0, 0);
1887 memset (cairo_image_surface_get_data (*surface), 0x00,
1888 cairo_image_surface_get_height (*surface) *
1889 cairo_image_surface_get_stride (*surface));
1890 poppler_page_render_selection (poppler_page,
1892 (PopplerRectangle *)points,
1893 (PopplerRectangle *)old_points,
1894 (PopplerSelectionStyle)style,
1898 #else /* HAVE_POPPLER_PAGE_RENDER */
1901 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1905 poppler_page_render_selection_to_pixbuf (poppler_page,
1906 rc->scale, rc->rotation, pixbuf,
1907 (PopplerRectangle *)points,
1908 (PopplerRectangle *)old_points,
1909 (PopplerSelectionStyle)style,
1913 cairo_surface_destroy (*surface);
1914 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1915 g_object_unref (pixbuf);
1916 #endif /* HAVE_POPPLER_PAGE_RENDER */
1920 pdf_selection_get_selected_text (EvSelection *selection,
1921 EvRenderContext *rc,
1922 EvSelectionStyle style,
1923 EvRectangle *points)
1925 PopplerPage *poppler_page;
1930 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1932 poppler_page_get_size (poppler_page, NULL, &height);
1934 r.y1 = height - points->y2;
1936 r.y2 = height - points->y1;
1938 retval = poppler_page_get_text (poppler_page,
1939 (PopplerSelectionStyle)style,
1946 create_gdk_region_from_poppler_region (GList *region)
1951 retval = gdk_region_new ();
1953 for (l = region; l; l = g_list_next (l)) {
1954 PopplerRectangle *rectangle;
1957 rectangle = (PopplerRectangle *)l->data;
1959 rect.x = (gint) rectangle->x1;
1960 rect.y = (gint) rectangle->y1;
1961 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1962 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1963 gdk_region_union_with_rect (retval, &rect);
1965 poppler_rectangle_free (rectangle);
1972 pdf_selection_get_selection_region (EvSelection *selection,
1973 EvRenderContext *rc,
1974 EvSelectionStyle style,
1975 EvRectangle *points)
1977 PopplerPage *poppler_page;
1981 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1983 region = poppler_page_get_selection_region (poppler_page,
1985 (PopplerSelectionStyle)style,
1986 (PopplerRectangle *) points);
1987 retval = create_gdk_region_from_poppler_region (region);
1988 g_list_free (region);
1994 pdf_selection_get_selection_map (EvSelection *selection,
1995 EvRenderContext *rc)
1997 PopplerPage *poppler_page;
1998 PopplerRectangle points;
2002 poppler_page = POPPLER_PAGE (rc->page->backend_page);
2006 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2008 region = poppler_page_get_selection_region (poppler_page, 1.0,
2009 POPPLER_SELECTION_GLYPH,
2011 retval = create_gdk_region_from_poppler_region (region);
2012 g_list_free (region);
2018 pdf_selection_iface_init (EvSelectionIface *iface)
2020 iface->render_selection = pdf_selection_render_selection;
2021 iface->get_selected_text = pdf_selection_get_selected_text;
2022 iface->get_selection_region = pdf_selection_get_selection_region;
2023 iface->get_selection_map = pdf_selection_get_selection_map;
2026 /* Page Transitions */
2028 pdf_document_get_page_duration (EvDocumentTransition *trans,
2031 PdfDocument *pdf_document;
2032 PopplerPage *poppler_page;
2033 gdouble duration = -1;
2035 pdf_document = PDF_DOCUMENT (trans);
2036 poppler_page = poppler_document_get_page (pdf_document->document, page);
2040 duration = poppler_page_get_duration (poppler_page);
2041 g_object_unref (poppler_page);
2046 static EvTransitionEffect *
2047 pdf_document_get_effect (EvDocumentTransition *trans,
2050 PdfDocument *pdf_document;
2051 PopplerPage *poppler_page;
2052 PopplerPageTransition *page_transition;
2053 EvTransitionEffect *effect;
2055 pdf_document = PDF_DOCUMENT (trans);
2056 poppler_page = poppler_document_get_page (pdf_document->document, page);
2061 page_transition = poppler_page_get_transition (poppler_page);
2063 if (!page_transition) {
2064 g_object_unref (poppler_page);
2068 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2069 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2070 "alignment", page_transition->alignment,
2071 "direction", page_transition->direction,
2072 "duration", page_transition->duration,
2073 "angle", page_transition->angle,
2074 "scale", page_transition->scale,
2075 "rectangular", page_transition->rectangular,
2078 poppler_page_transition_free (page_transition);
2079 g_object_unref (poppler_page);
2085 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2087 iface->get_page_duration = pdf_document_get_page_duration;
2088 iface->get_effect = pdf_document_get_effect;
2093 pdf_document_get_crop_box (EvDocument *document,
2097 PdfDocument *pdf_document;
2098 PopplerPage *poppler_page;
2099 PopplerRectangle poppler_rect;
2101 pdf_document = PDF_DOCUMENT (document);
2102 poppler_page = poppler_document_get_page (pdf_document->document, page);
2103 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2104 rect->x1 = poppler_rect.x1;
2105 rect->x2 = poppler_rect.x2;
2106 rect->y1 = poppler_rect.y1;
2107 rect->y2 = poppler_rect.y2;
2110 static EvFormField *
2111 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2113 EvFormField *ev_field = NULL;
2116 gboolean is_read_only;
2118 id = poppler_form_field_get_id (poppler_field);
2119 font_size = poppler_form_field_get_font_size (poppler_field);
2120 is_read_only = poppler_form_field_is_read_only (poppler_field);
2122 switch (poppler_form_field_get_field_type (poppler_field)) {
2123 case POPPLER_FORM_FIELD_TEXT: {
2124 EvFormFieldText *field_text;
2125 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2127 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2128 case POPPLER_FORM_TEXT_NORMAL:
2129 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2131 case POPPLER_FORM_TEXT_MULTILINE:
2132 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2134 case POPPLER_FORM_TEXT_FILE_SELECT:
2135 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2139 ev_field = ev_form_field_text_new (id, ev_text_type);
2140 field_text = EV_FORM_FIELD_TEXT (ev_field);
2142 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2143 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2144 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2145 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2146 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2147 field_text->text = poppler_form_field_text_get_text (poppler_field);
2151 case POPPLER_FORM_FIELD_BUTTON: {
2152 EvFormFieldButton *field_button;
2153 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2155 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2156 case POPPLER_FORM_BUTTON_PUSH:
2157 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2159 case POPPLER_FORM_BUTTON_CHECK:
2160 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2162 case POPPLER_FORM_BUTTON_RADIO:
2163 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2167 ev_field = ev_form_field_button_new (id, ev_button_type);
2168 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2170 field_button->state = poppler_form_field_button_get_state (poppler_field);
2173 case POPPLER_FORM_FIELD_CHOICE: {
2174 EvFormFieldChoice *field_choice;
2175 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2177 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2178 case POPPLER_FORM_CHOICE_COMBO:
2179 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2181 case EV_FORM_FIELD_CHOICE_LIST:
2182 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2186 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2187 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2189 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2190 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2191 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2192 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2194 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2195 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2196 if (field_choice->is_editable)
2197 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2200 case POPPLER_FORM_FIELD_SIGNATURE:
2202 ev_field = ev_form_field_signature_new (id);
2204 case POPPLER_FORM_FIELD_UNKNOWN:
2208 ev_field->font_size = font_size;
2209 ev_field->is_read_only = is_read_only;
2215 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2218 PopplerPage *poppler_page;
2219 GList *retval = NULL;
2224 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2226 poppler_page = POPPLER_PAGE (page->backend_page);
2227 fields = poppler_page_get_form_field_mapping (poppler_page);
2228 poppler_page_get_size (poppler_page, NULL, &height);
2230 for (list = fields; list; list = list->next) {
2231 PopplerFormFieldMapping *mapping;
2232 EvMapping *field_mapping;
2233 EvFormField *ev_field;
2235 mapping = (PopplerFormFieldMapping *)list->data;
2237 ev_field = ev_form_field_from_poppler_field (mapping->field);
2241 field_mapping = g_new0 (EvMapping, 1);
2242 field_mapping->area.x1 = mapping->area.x1;
2243 field_mapping->area.x2 = mapping->area.x2;
2244 field_mapping->area.y1 = height - mapping->area.y2;
2245 field_mapping->area.y2 = height - mapping->area.y1;
2246 field_mapping->data = ev_field;
2247 ev_field->page = EV_PAGE (g_object_ref (page));
2249 g_object_set_data_full (G_OBJECT (ev_field),
2251 g_object_ref (mapping->field),
2252 (GDestroyNotify) g_object_unref);
2254 retval = g_list_prepend (retval, field_mapping);
2257 poppler_page_free_form_field_mapping (fields);
2259 return g_list_reverse (retval);
2263 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2267 PopplerFormField *poppler_field;
2270 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2274 text = poppler_form_field_text_get_text (poppler_field);
2280 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2284 PopplerFormField *poppler_field;
2286 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2290 poppler_form_field_text_set_text (poppler_field, text);
2291 PDF_DOCUMENT (document)->modified = TRUE;
2295 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2299 PopplerFormField *poppler_field;
2301 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2305 poppler_form_field_button_set_state (poppler_field, state);
2306 PDF_DOCUMENT (document)->modified = TRUE;
2310 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2313 PopplerFormField *poppler_field;
2316 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2320 state = poppler_form_field_button_get_state (poppler_field);
2326 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2330 PopplerFormField *poppler_field;
2333 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2337 text = poppler_form_field_choice_get_item (poppler_field, index);
2343 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2346 PopplerFormField *poppler_field;
2349 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2353 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2359 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2363 PopplerFormField *poppler_field;
2366 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2370 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2376 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2380 PopplerFormField *poppler_field;
2382 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2386 poppler_form_field_choice_select_item (poppler_field, index);
2387 PDF_DOCUMENT (document)->modified = TRUE;
2391 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2395 PopplerFormField *poppler_field;
2397 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2401 poppler_form_field_choice_toggle_item (poppler_field, index);
2402 PDF_DOCUMENT (document)->modified = TRUE;
2406 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2409 PopplerFormField *poppler_field;
2411 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2415 poppler_form_field_choice_unselect_all (poppler_field);
2416 PDF_DOCUMENT (document)->modified = TRUE;
2420 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2424 PopplerFormField *poppler_field;
2426 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2430 poppler_form_field_choice_set_text (poppler_field, text);
2431 PDF_DOCUMENT (document)->modified = TRUE;
2435 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2438 PopplerFormField *poppler_field;
2441 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2445 text = poppler_form_field_choice_get_text (poppler_field);
2451 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2453 iface->get_form_fields = pdf_document_forms_get_form_fields;
2454 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2455 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2456 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2457 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2458 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2459 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2460 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2461 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2462 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2463 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2464 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2465 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2470 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2473 PopplerColor *poppler_color;
2475 poppler_color = poppler_annot_get_color (poppler_annot);
2476 if (poppler_color) {
2477 color->red = poppler_color->red;
2478 color->green = poppler_color->green;
2479 color->blue = poppler_color->blue;
2481 g_free (poppler_color);
2482 } /* TODO: else use a default color */
2485 static EvAnnotation *
2486 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2489 EvAnnotation *ev_annot = NULL;
2490 const gchar *unimplemented_annot = NULL;
2492 switch (poppler_annot_get_annot_type (poppler_annot)) {
2493 case POPPLER_ANNOT_TEXT:
2494 PopplerAnnotText *poppler_text;
2495 EvAnnotationText *ev_annot_text;
2497 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2499 ev_annot = ev_annotation_text_new (page);
2501 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2502 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2505 case POPPLER_ANNOT_LINK:
2506 case POPPLER_ANNOT_WIDGET:
2507 /* Ignore link and widgets annots since they are already handled */
2510 GEnumValue *enum_value;
2512 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2513 poppler_annot_get_annot_type (poppler_annot));
2514 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2518 if (unimplemented_annot) {
2519 g_warning ("Unimplemented annotation: %s, please post a "
2520 "bug report in Evince bugzilla "
2521 "(http://bugzilla.gnome.org) with a testcase.",
2522 unimplemented_annot);
2526 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2527 ev_annot->name = poppler_annot_get_name (poppler_annot);
2528 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2529 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2531 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2532 PopplerAnnotMarkup *markup;
2535 PopplerRectangle poppler_rect;
2537 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2539 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2540 EvRectangle ev_rect;
2544 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2546 ev_rect.x1 = poppler_rect.x1;
2547 ev_rect.x2 = poppler_rect.x2;
2548 ev_rect.y1 = height - poppler_rect.y2;
2549 ev_rect.y2 = height - poppler_rect.y1;
2551 is_open = poppler_annot_markup_get_popup_is_open (markup);
2553 g_object_set (ev_annot,
2554 "rectangle", &ev_rect,
2559 /* FIXME: Use poppler_annot_markup_has_popup() when
2560 * new poppler is released.
2562 g_object_set (ev_annot,
2567 label = poppler_annot_markup_get_label (markup);
2568 opacity = poppler_annot_markup_get_opacity (markup);
2570 g_object_set (ev_annot,
2583 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2586 GList *retval = NULL;
2587 PdfDocument *pdf_document;
2588 PopplerPage *poppler_page;
2594 pdf_document = PDF_DOCUMENT (document_annotations);
2595 poppler_page = POPPLER_PAGE (page->backend_page);
2596 annots = poppler_page_get_annot_mapping (poppler_page);
2597 poppler_page_get_size (poppler_page, NULL, &height);
2599 for (list = annots; list; list = list->next) {
2600 PopplerAnnotMapping *mapping;
2601 EvMapping *annot_mapping;
2602 EvAnnotation *ev_annot;
2604 mapping = (PopplerAnnotMapping *)list->data;
2606 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2612 /* Make sure annot has a unique name */
2613 if (!ev_annot->name)
2614 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2616 annot_mapping = g_new (EvMapping, 1);
2617 annot_mapping->area.x1 = mapping->area.x1;
2618 annot_mapping->area.x2 = mapping->area.x2;
2619 annot_mapping->area.y1 = height - mapping->area.y2;
2620 annot_mapping->area.y2 = height - mapping->area.y1;
2621 annot_mapping->data = ev_annot;
2623 g_object_set_data_full (G_OBJECT (ev_annot),
2625 g_object_ref (mapping->annot),
2626 (GDestroyNotify) g_object_unref);
2628 retval = g_list_prepend (retval, annot_mapping);
2631 poppler_page_free_annot_mapping (annots);
2633 return g_list_reverse (retval);
2637 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2638 EvAnnotation *annot,
2639 const gchar *contents)
2641 PopplerAnnot *poppler_annot;
2643 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2647 poppler_annot_set_contents (poppler_annot, contents);
2648 PDF_DOCUMENT (document)->modified = TRUE;
2652 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2654 iface->get_annotations = pdf_document_annotations_get_annotations;
2655 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2660 pdf_document_layers_has_layers (EvDocumentLayers *document)
2662 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2663 PopplerLayersIter *iter;
2665 iter = poppler_layers_iter_new (pdf_document->document);
2668 poppler_layers_iter_free (iter);
2674 build_layers_tree (PdfDocument *pdf_document,
2675 GtkTreeModel *model,
2676 GtkTreeIter *parent,
2677 PopplerLayersIter *iter)
2680 GtkTreeIter tree_iter;
2681 PopplerLayersIter *child;
2682 PopplerLayer *layer;
2683 EvLayer *ev_layer = NULL;
2688 layer = poppler_layers_iter_get_layer (iter);
2690 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2691 visible = poppler_layer_is_visible (layer);
2692 rb_group = poppler_layer_get_radio_button_group_id (layer);
2693 pdf_document->layers = g_list_append (pdf_document->layers,
2694 g_object_ref (layer));
2695 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2696 poppler_layer_is_parent (layer),
2701 title = poppler_layers_iter_get_title (iter);
2702 markup = g_markup_escape_text (title, -1);
2709 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2710 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2711 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2712 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2713 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2714 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2715 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2716 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2719 g_object_unref (ev_layer);
2722 child = poppler_layers_iter_get_child (iter);
2724 build_layers_tree (pdf_document, model, &tree_iter, child);
2725 poppler_layers_iter_free (child);
2726 } while (poppler_layers_iter_next (iter));
2729 static GtkTreeModel *
2730 pdf_document_layers_get_layers (EvDocumentLayers *document)
2732 GtkTreeModel *model = NULL;
2733 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2734 PopplerLayersIter *iter;
2736 iter = poppler_layers_iter_new (pdf_document->document);
2738 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2739 G_TYPE_STRING, /* TITLE */
2740 G_TYPE_OBJECT, /* LAYER */
2741 G_TYPE_BOOLEAN, /* VISIBLE */
2742 G_TYPE_BOOLEAN, /* ENABLED */
2743 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2744 G_TYPE_INT); /* RBGROUP */
2745 build_layers_tree (pdf_document, model, NULL, iter);
2746 poppler_layers_iter_free (iter);
2752 pdf_document_layers_show_layer (EvDocumentLayers *document,
2755 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2756 guint layer_id = ev_layer_get_id (layer);
2758 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2762 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2765 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2766 guint layer_id = ev_layer_get_id (layer);
2768 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2772 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2775 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2776 guint layer_id = ev_layer_get_id (layer);
2778 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2782 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2784 iface->has_layers = pdf_document_layers_has_layers;
2785 iface->get_layers = pdf_document_layers_get_layers;
2786 iface->show_layer = pdf_document_layers_show_layer;
2787 iface->hide_layer = pdf_document_layers_hide_layer;
2788 iface->layer_is_visible = pdf_document_layers_layer_is_visible;