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-document-find.h"
39 #include "ev-document-misc.h"
40 #include "ev-document-links.h"
41 #include "ev-document-images.h"
42 #include "ev-document-fonts.h"
43 #include "ev-document-security.h"
44 #include "ev-document-thumbnails.h"
45 #include "ev-document-transition.h"
46 #include "ev-document-forms.h"
47 #include "ev-document-layers.h"
48 #include "ev-document-print.h"
49 #include "ev-document-annotations.h"
50 #include "ev-selection.h"
51 #include "ev-transition-effect.h"
52 #include "ev-attachment.h"
55 #include <libxml/tree.h>
56 #include <libxml/parser.h>
57 #include <libxml/xpath.h>
58 #include <libxml/xpathInternals.h>
60 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
61 #define HAVE_CAIRO_PRINT
65 PdfDocument *document;
74 EvFileExporterFormat format;
84 #ifdef HAVE_CAIRO_PRINT
87 PopplerPSFile *ps_file;
91 struct _PdfDocumentClass
93 GObjectClass parent_class;
98 GObject parent_instance;
100 PopplerDocument *document;
104 PopplerFontInfo *font_info;
105 PopplerFontsIter *fonts_iter;
106 int fonts_scanned_pages;
108 PdfDocumentSearch *search;
109 PdfPrintContext *print_ctx;
114 static void pdf_document_document_iface_init (EvDocumentIface *iface);
115 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
116 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
117 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
118 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
119 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
120 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
121 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
122 #ifdef HAVE_POPPLER_PAGE_RENDER
123 static void pdf_document_document_print_iface_init (EvDocumentPrintIface *iface);
125 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
126 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
127 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
128 static void pdf_selection_iface_init (EvSelectionIface *iface);
129 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
130 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
134 static int pdf_document_get_n_pages (EvDocument *document);
136 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
138 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
139 PopplerAction *action);
140 static void pdf_document_search_free (PdfDocumentSearch *search);
141 static void pdf_print_context_free (PdfPrintContext *ctx);
143 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
145 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
146 pdf_document_security_iface_init);
147 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
148 pdf_document_document_thumbnails_iface_init);
149 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
150 pdf_document_document_links_iface_init);
151 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
152 pdf_document_document_images_iface_init);
153 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
154 pdf_document_document_forms_iface_init);
155 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
156 pdf_document_document_fonts_iface_init);
157 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
158 pdf_document_document_layers_iface_init);
159 #ifdef HAVE_POPPLER_PAGE_RENDER
160 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
161 pdf_document_document_print_iface_init);
163 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
164 pdf_document_document_annotations_iface_init);
165 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
166 pdf_document_find_iface_init);
167 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
168 pdf_document_file_exporter_iface_init);
169 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
170 pdf_selection_iface_init);
171 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
172 pdf_document_page_transition_iface_init);
176 pdf_document_search_free (PdfDocumentSearch *search)
178 PdfDocument *pdf_document = search->document;
182 if (search->idle != 0)
183 g_source_remove (search->idle);
185 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
186 for (i = 0; i < n_pages; i++) {
187 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
188 g_list_free (search->pages[i]);
190 g_free (search->pages);
192 g_free (search->text);
197 pdf_document_dispose (GObject *object)
199 PdfDocument *pdf_document = PDF_DOCUMENT(object);
201 if (pdf_document->print_ctx) {
202 pdf_print_context_free (pdf_document->print_ctx);
203 pdf_document->print_ctx = NULL;
206 if (pdf_document->search) {
207 pdf_document_search_free (pdf_document->search);
208 pdf_document->search = NULL;
211 if (pdf_document->document) {
212 g_object_unref (pdf_document->document);
215 if (pdf_document->font_info) {
216 poppler_font_info_free (pdf_document->font_info);
219 if (pdf_document->fonts_iter) {
220 poppler_fonts_iter_free (pdf_document->fonts_iter);
223 if (pdf_document->layers) {
224 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
225 g_list_free (pdf_document->layers);
228 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
232 pdf_document_class_init (PdfDocumentClass *klass)
234 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
236 g_object_class->dispose = pdf_document_dispose;
240 pdf_document_init (PdfDocument *pdf_document)
242 pdf_document->password = NULL;
246 convert_error (GError *poppler_error,
249 if (poppler_error == NULL)
252 if (poppler_error->domain == POPPLER_ERROR) {
253 /* convert poppler errors into EvDocument errors */
254 gint code = EV_DOCUMENT_ERROR_INVALID;
255 if (poppler_error->code == POPPLER_ERROR_INVALID)
256 code = EV_DOCUMENT_ERROR_INVALID;
257 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
258 code = EV_DOCUMENT_ERROR_ENCRYPTED;
260 g_set_error_literal (error,
263 poppler_error->message);
265 g_error_free (poppler_error);
267 g_propagate_error (error, poppler_error);
274 pdf_document_save (EvDocument *document,
278 PdfDocument *pdf_document = PDF_DOCUMENT (document);
280 GError *poppler_error = NULL;
282 if (pdf_document->modified) {
283 retval = poppler_document_save (pdf_document->document,
284 uri, &poppler_error);
286 retval = poppler_document_save_a_copy (pdf_document->document,
287 uri, &poppler_error);
291 convert_error (poppler_error, error);
297 pdf_document_load (EvDocument *document,
301 GError *poppler_error = NULL;
302 PdfDocument *pdf_document = PDF_DOCUMENT (document);
304 pdf_document->document =
305 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
307 if (pdf_document->document == NULL) {
308 convert_error (poppler_error, error);
316 pdf_document_get_n_pages (EvDocument *document)
318 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
322 pdf_document_get_page (EvDocument *document,
325 PdfDocument *pdf_document = PDF_DOCUMENT (document);
326 PopplerPage *poppler_page;
329 poppler_page = poppler_document_get_page (pdf_document->document, index);
330 page = ev_page_new (index);
331 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
332 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
333 g_object_unref (poppler_page);
339 pdf_document_get_page_size (EvDocument *document,
344 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
346 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
350 pdf_document_get_page_label (EvDocument *document,
355 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
357 g_object_get (G_OBJECT (page->backend_page),
364 pdf_document_has_attachments (EvDocument *document)
366 PdfDocument *pdf_document;
368 pdf_document = PDF_DOCUMENT (document);
370 return poppler_document_has_attachments (pdf_document->document);
373 struct SaveToBufferData {
379 attachment_save_to_buffer_callback (const gchar *buf,
384 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
388 if (sdata->len + count > sdata->max) {
389 new_max = MAX (sdata->max * 2, sdata->len + count);
390 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
392 sdata->buffer = new_buffer;
393 sdata->max = new_max;
396 memcpy (sdata->buffer + sdata->len, buf, count);
403 attachment_save_to_buffer (PopplerAttachment *attachment,
408 static const gint initial_max = 1024;
409 struct SaveToBufferData sdata;
414 sdata.buffer = (gchar *) g_malloc (initial_max);
415 sdata.max = initial_max;
418 if (! poppler_attachment_save_to_callback (attachment,
419 attachment_save_to_buffer_callback,
422 g_free (sdata.buffer);
426 *buffer = sdata.buffer;
427 *buffer_size = sdata.len;
433 pdf_document_get_attachments (EvDocument *document)
435 PdfDocument *pdf_document;
438 GList *retval = NULL;
440 pdf_document = PDF_DOCUMENT (document);
442 if (!pdf_document_has_attachments (document))
445 attachments = poppler_document_get_attachments (pdf_document->document);
447 for (list = attachments; list; list = list->next) {
448 PopplerAttachment *attachment;
449 EvAttachment *ev_attachment;
452 GError *error = NULL;
454 attachment = (PopplerAttachment *) list->data;
456 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
457 ev_attachment = ev_attachment_new (attachment->name,
458 attachment->description,
463 retval = g_list_prepend (retval, ev_attachment);
466 g_warning ("%s", error->message);
467 g_error_free (error);
473 g_object_unref (attachment);
476 return g_list_reverse (retval);
479 static cairo_surface_t *
480 pdf_page_render (PopplerPage *page,
485 cairo_surface_t *surface;
487 #ifdef HAVE_POPPLER_PAGE_RENDER
490 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
492 memset (cairo_image_surface_get_data (surface), 0xff,
493 cairo_image_surface_get_height (surface) *
494 cairo_image_surface_get_stride (surface));
496 cr = cairo_create (surface);
497 switch (rc->rotation) {
499 cairo_translate (cr, width, 0);
502 cairo_translate (cr, width, height);
505 cairo_translate (cr, 0, height);
508 cairo_translate (cr, 0, 0);
510 cairo_scale (cr, rc->scale, rc->scale);
511 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
512 poppler_page_render (page, cr);
514 #else /* HAVE_POPPLER_PAGE_RENDER */
517 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
521 poppler_page_render_to_pixbuf (page,
527 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
528 g_object_unref (pixbuf);
529 #endif /* HAVE_POPPLER_PAGE_RENDER */
534 static cairo_surface_t *
535 pdf_document_render (EvDocument *document,
538 PdfDocument *pdf_document;
539 PopplerPage *poppler_page;
540 double width_points, height_points;
543 poppler_page = POPPLER_PAGE (rc->page->backend_page);
545 poppler_page_get_size (poppler_page,
546 &width_points, &height_points);
548 if (rc->rotation == 90 || rc->rotation == 270) {
549 width = (int) ((height_points * rc->scale) + 0.5);
550 height = (int) ((width_points * rc->scale) + 0.5);
552 width = (int) ((width_points * rc->scale) + 0.5);
553 height = (int) ((height_points * rc->scale) + 0.5);
556 return pdf_page_render (poppler_page,
560 /* EvDocumentSecurity */
563 pdf_document_has_document_security (EvDocumentSecurity *document_security)
565 /* FIXME: do we really need to have this? */
570 pdf_document_set_password (EvDocumentSecurity *document_security,
571 const char *password)
573 PdfDocument *document = PDF_DOCUMENT (document_security);
575 if (document->password)
576 g_free (document->password);
578 document->password = g_strdup (password);
583 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 */
585 pdf_document_get_format_from_metadata (const char *metadata)
588 xmlXPathContextPtr xpathCtx;
589 xmlXPathObjectPtr xpathObj;
590 xmlChar *part = NULL;
591 xmlChar *conf = NULL;
595 doc = xmlParseMemory (metadata, strlen (metadata));
597 return NULL; /* invalid xml metadata */
599 xpathCtx = xmlXPathNewContext (doc);
600 if (xpathCtx == NULL) {
602 return NULL; /* invalid xpath context */
605 /* add pdf/a namespaces */
606 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
607 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
608 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
610 /* reads pdf/a part */
611 /* first syntax: child node */
612 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
613 if (xpathObj != NULL) {
614 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
615 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
617 xmlXPathFreeObject (xpathObj);
620 /* second syntax: attribute */
621 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
622 if (xpathObj != NULL) {
623 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
624 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
626 xmlXPathFreeObject (xpathObj);
630 /* reads pdf/a conformance */
631 /* first syntax: child node */
632 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
633 if (xpathObj != NULL) {
634 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
635 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
637 xmlXPathFreeObject (xpathObj);
640 /* second syntax: attribute */
641 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
642 if (xpathObj != NULL) {
643 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
644 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
646 xmlXPathFreeObject (xpathObj);
650 if (part != NULL && conf != NULL) {
651 /* makes conf lowercase */
652 for (i = 0; conf[i]; i++)
653 conf[i] = g_ascii_tolower (conf[i]);
656 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
662 xmlXPathFreeContext (xpathCtx);
669 static EvDocumentInfo *
670 pdf_document_get_info (EvDocument *document)
672 EvDocumentInfo *info;
673 PopplerPageLayout layout;
674 PopplerPageMode mode;
675 PopplerViewerPreferences view_prefs;
676 PopplerPermissions permissions;
681 info = g_new0 (EvDocumentInfo, 1);
683 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
684 EV_DOCUMENT_INFO_FORMAT |
685 EV_DOCUMENT_INFO_AUTHOR |
686 EV_DOCUMENT_INFO_SUBJECT |
687 EV_DOCUMENT_INFO_KEYWORDS |
688 EV_DOCUMENT_INFO_LAYOUT |
689 EV_DOCUMENT_INFO_START_MODE |
690 EV_DOCUMENT_INFO_PERMISSIONS |
691 EV_DOCUMENT_INFO_UI_HINTS |
692 EV_DOCUMENT_INFO_CREATOR |
693 EV_DOCUMENT_INFO_PRODUCER |
694 EV_DOCUMENT_INFO_CREATION_DATE |
695 EV_DOCUMENT_INFO_MOD_DATE |
696 EV_DOCUMENT_INFO_LINEARIZED |
697 EV_DOCUMENT_INFO_N_PAGES |
698 EV_DOCUMENT_INFO_SECURITY |
699 EV_DOCUMENT_INFO_PAPER_SIZE;
701 g_object_get (PDF_DOCUMENT (document)->document,
702 "title", &(info->title),
703 "format", &(info->format),
704 "author", &(info->author),
705 "subject", &(info->subject),
706 "keywords", &(info->keywords),
708 "page-layout", &layout,
709 "viewer-preferences", &view_prefs,
710 "permissions", &permissions,
711 "creator", &(info->creator),
712 "producer", &(info->producer),
713 "creation-date", &(info->creation_date),
714 "mod-date", &(info->modified_date),
715 "linearized", &(info->linearized),
716 "metadata", &metadata,
719 if (metadata != NULL) {
720 fmt = pdf_document_get_format_from_metadata(metadata);
722 g_free (info->format);
728 info->n_pages = ev_document_get_n_pages (document);
730 if (info->n_pages > 0) {
731 page = ev_document_get_page (document, 0);
732 ev_document_get_page_size (document, page,
733 &(info->paper_width),
734 &(info->paper_height));
735 g_object_unref (page);
739 info->paper_width = info->paper_width / 72.0f * 25.4f;
740 info->paper_height = info->paper_height / 72.0f * 25.4f;
744 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
745 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
747 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
748 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
750 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
751 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
753 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
754 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
755 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
756 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
758 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
759 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
766 case POPPLER_PAGE_MODE_NONE:
767 info->mode = EV_DOCUMENT_MODE_NONE;
769 case POPPLER_PAGE_MODE_USE_THUMBS:
770 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
772 case POPPLER_PAGE_MODE_USE_OC:
773 info->mode = EV_DOCUMENT_MODE_USE_OC;
775 case POPPLER_PAGE_MODE_FULL_SCREEN:
776 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
778 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
779 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
785 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
786 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
788 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
789 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
791 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
792 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
794 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
795 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
797 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
798 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
800 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
801 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
803 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
804 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
807 info->permissions = 0;
808 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
809 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
811 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
812 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
814 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
815 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
817 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
818 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
821 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
822 /* translators: this is the document security state */
823 info->security = g_strdup (_("Yes"));
825 /* translators: this is the document security state */
826 info->security = g_strdup (_("No"));
833 pdf_document_document_iface_init (EvDocumentIface *iface)
835 iface->save = pdf_document_save;
836 iface->load = pdf_document_load;
837 iface->get_n_pages = pdf_document_get_n_pages;
838 iface->get_page = pdf_document_get_page;
839 iface->get_page_size = pdf_document_get_page_size;
840 iface->get_page_label = pdf_document_get_page_label;
841 iface->has_attachments = pdf_document_has_attachments;
842 iface->get_attachments = pdf_document_get_attachments;
843 iface->render = pdf_document_render;
844 iface->get_info = pdf_document_get_info;
848 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
850 iface->has_document_security = pdf_document_has_document_security;
851 iface->set_password = pdf_document_set_password;
855 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
857 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
860 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
862 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
866 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
869 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
872 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
874 if (pdf_document->font_info == NULL) {
875 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
878 if (pdf_document->fonts_iter) {
879 poppler_fonts_iter_free (pdf_document->fonts_iter);
882 pdf_document->fonts_scanned_pages += n_pages;
884 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
885 &pdf_document->fonts_iter);
887 pdf_document->fonts_scanned_pages = 0;
888 poppler_font_info_free (pdf_document->font_info);
889 pdf_document->font_info = NULL;
896 font_type_to_string (PopplerFontType type)
899 case POPPLER_FONT_TYPE_TYPE1:
901 case POPPLER_FONT_TYPE_TYPE1C:
903 case POPPLER_FONT_TYPE_TYPE3:
905 case POPPLER_FONT_TYPE_TRUETYPE:
906 return _("TrueType");
907 case POPPLER_FONT_TYPE_CID_TYPE0:
908 return _("Type 1 (CID)");
909 case POPPLER_FONT_TYPE_CID_TYPE0C:
910 return _("Type 1C (CID)");
911 case POPPLER_FONT_TYPE_CID_TYPE2:
912 return _("TrueType (CID)");
914 return _("Unknown font type");
919 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
922 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
923 PopplerFontsIter *iter = pdf_document->fonts_iter;
925 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
931 GtkTreeIter list_iter;
934 const char *embedded;
937 name = poppler_fonts_iter_get_name (iter);
943 type = font_type_to_string (
944 poppler_fonts_iter_get_font_type (iter));
946 if (poppler_fonts_iter_is_embedded (iter)) {
947 if (poppler_fonts_iter_is_subset (iter))
948 embedded = _("Embedded subset");
950 embedded = _("Embedded");
952 embedded = _("Not embedded");
955 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
957 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
958 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
959 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
960 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
964 } while (poppler_fonts_iter_next (iter));
968 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
970 iface->fill_model = pdf_document_fonts_fill_model;
971 iface->scan = pdf_document_fonts_scan;
972 iface->get_progress = pdf_document_fonts_get_progress;
976 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
978 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
979 PopplerIndexIter *iter;
981 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
983 iter = poppler_index_iter_new (pdf_document->document);
986 poppler_index_iter_free (iter);
992 ev_link_dest_from_dest (PdfDocument *pdf_document,
995 EvLinkDest *ev_dest = NULL;
996 const char *unimplemented_dest = NULL;
998 g_assert (dest != NULL);
1000 switch (dest->type) {
1001 case POPPLER_DEST_XYZ: {
1002 PopplerPage *poppler_page;
1005 poppler_page = poppler_document_get_page (pdf_document->document,
1006 MAX (0, dest->page_num - 1));
1007 poppler_page_get_size (poppler_page, NULL, &height);
1008 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
1010 height - MIN (height, dest->top),
1015 g_object_unref (poppler_page);
1018 case POPPLER_DEST_FITB:
1019 case POPPLER_DEST_FIT:
1020 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1022 case POPPLER_DEST_FITBH:
1023 case POPPLER_DEST_FITH: {
1024 PopplerPage *poppler_page;
1027 poppler_page = poppler_document_get_page (pdf_document->document,
1028 MAX (0, dest->page_num - 1));
1029 poppler_page_get_size (poppler_page, NULL, &height);
1030 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1031 height - MIN (height, dest->top),
1033 g_object_unref (poppler_page);
1036 case POPPLER_DEST_FITBV:
1037 case POPPLER_DEST_FITV:
1038 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1042 case POPPLER_DEST_FITR: {
1043 PopplerPage *poppler_page;
1046 poppler_page = poppler_document_get_page (pdf_document->document,
1047 MAX (0, dest->page_num - 1));
1048 poppler_page_get_size (poppler_page, NULL, &height);
1049 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1051 height - MIN (height, dest->bottom),
1053 height - MIN (height, dest->top));
1054 g_object_unref (poppler_page);
1057 case POPPLER_DEST_NAMED:
1058 ev_dest = ev_link_dest_new_named (dest->named_dest);
1060 case POPPLER_DEST_UNKNOWN:
1061 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1065 if (unimplemented_dest) {
1066 g_warning ("Unimplemented destination: %s, please post a "
1067 "bug report in Evince bugzilla "
1068 "(http://bugzilla.gnome.org) with a testcase.",
1069 unimplemented_dest);
1073 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1079 ev_link_from_action (PdfDocument *pdf_document,
1080 PopplerAction *action)
1082 EvLink *link = NULL;
1083 EvLinkAction *ev_action = NULL;
1084 const char *unimplemented_action = NULL;
1086 switch (action->type) {
1087 case POPPLER_ACTION_NONE:
1089 case POPPLER_ACTION_GOTO_DEST: {
1092 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1093 ev_action = ev_link_action_new_dest (dest);
1096 case POPPLER_ACTION_GOTO_REMOTE: {
1099 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1100 ev_action = ev_link_action_new_remote (dest,
1101 action->goto_remote.file_name);
1105 case POPPLER_ACTION_LAUNCH:
1106 ev_action = ev_link_action_new_launch (action->launch.file_name,
1107 action->launch.params);
1109 case POPPLER_ACTION_URI:
1110 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1112 case POPPLER_ACTION_NAMED:
1113 ev_action = ev_link_action_new_named (action->named.named_dest);
1115 case POPPLER_ACTION_MOVIE:
1116 unimplemented_action = "POPPLER_ACTION_MOVIE";
1118 case POPPLER_ACTION_UNKNOWN:
1119 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1122 if (unimplemented_action) {
1123 g_warning ("Unimplemented action: %s, please post a bug report "
1124 "in Evince bugzilla (http://bugzilla.gnome.org) "
1125 "with a testcase.", unimplemented_action);
1128 link = ev_link_new (action->any.title, ev_action);
1134 build_tree (PdfDocument *pdf_document,
1135 GtkTreeModel *model,
1136 GtkTreeIter *parent,
1137 PopplerIndexIter *iter)
1141 GtkTreeIter tree_iter;
1142 PopplerIndexIter *child;
1143 PopplerAction *action;
1144 EvLink *link = NULL;
1148 action = poppler_index_iter_get_action (iter);
1149 expand = poppler_index_iter_is_open (iter);
1154 switch (action->type) {
1155 case POPPLER_ACTION_GOTO_DEST: {
1156 /* For bookmarks, solve named destinations */
1157 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1159 EvLinkDest *ev_dest = NULL;
1160 EvLinkAction *ev_action;
1162 dest = poppler_document_find_dest (pdf_document->document,
1163 action->goto_dest.dest->named_dest);
1165 link = ev_link_from_action (pdf_document, action);
1169 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1170 poppler_dest_free (dest);
1172 ev_action = ev_link_action_new_dest (ev_dest);
1173 link = ev_link_new (action->any.title, ev_action);
1175 link = ev_link_from_action (pdf_document, action);
1180 link = ev_link_from_action (pdf_document, action);
1184 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1185 poppler_action_free (action);
1187 g_object_unref (link);
1192 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1193 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1195 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1196 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1197 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1198 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1201 g_free (title_markup);
1202 g_object_unref (link);
1204 child = poppler_index_iter_get_child (iter);
1206 build_tree (pdf_document, model, &tree_iter, child);
1207 poppler_index_iter_free (child);
1208 poppler_action_free (action);
1210 } while (poppler_index_iter_next (iter));
1213 static GtkTreeModel *
1214 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1216 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1217 GtkTreeModel *model = NULL;
1218 PopplerIndexIter *iter;
1220 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1222 iter = poppler_index_iter_new (pdf_document->document);
1223 /* Create the model if we have items*/
1225 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1230 build_tree (pdf_document, model, NULL, iter);
1231 poppler_index_iter_free (iter);
1238 pdf_document_links_get_links (EvDocumentLinks *document_links,
1241 PdfDocument *pdf_document;
1242 PopplerPage *poppler_page;
1243 GList *retval = NULL;
1244 GList *mapping_list;
1248 pdf_document = PDF_DOCUMENT (document_links);
1249 poppler_page = poppler_document_get_page (pdf_document->document,
1251 mapping_list = poppler_page_get_link_mapping (poppler_page);
1252 poppler_page_get_size (poppler_page, NULL, &height);
1254 for (list = mapping_list; list; list = list->next) {
1255 PopplerLinkMapping *link_mapping;
1256 EvLinkMapping *ev_link_mapping;
1258 link_mapping = (PopplerLinkMapping *)list->data;
1259 ev_link_mapping = g_new (EvLinkMapping, 1);
1260 ev_link_mapping->link = ev_link_from_action (pdf_document,
1261 link_mapping->action);
1262 ev_link_mapping->x1 = link_mapping->area.x1;
1263 ev_link_mapping->x2 = link_mapping->area.x2;
1264 /* Invert this for X-style coordinates */
1265 ev_link_mapping->y1 = height - link_mapping->area.y2;
1266 ev_link_mapping->y2 = height - link_mapping->area.y1;
1268 retval = g_list_prepend (retval, ev_link_mapping);
1271 poppler_page_free_link_mapping (mapping_list);
1272 g_object_unref (poppler_page);
1274 return g_list_reverse (retval);
1278 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1279 const gchar *link_name)
1281 PdfDocument *pdf_document;
1283 EvLinkDest *ev_dest = NULL;
1285 pdf_document = PDF_DOCUMENT (document_links);
1286 dest = poppler_document_find_dest (pdf_document->document,
1289 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1290 poppler_dest_free (dest);
1297 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1299 iface->has_document_links = pdf_document_links_has_document_links;
1300 iface->get_links_model = pdf_document_links_get_links_model;
1301 iface->get_links = pdf_document_links_get_links;
1302 iface->find_link_dest = pdf_document_links_find_link_dest;
1306 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1309 GList *retval = NULL;
1310 PdfDocument *pdf_document;
1311 PopplerPage *poppler_page;
1312 GList *mapping_list;
1315 pdf_document = PDF_DOCUMENT (document_images);
1316 poppler_page = poppler_document_get_page (pdf_document->document, page);
1317 mapping_list = poppler_page_get_image_mapping (poppler_page);
1319 for (list = mapping_list; list; list = list->next) {
1320 PopplerImageMapping *image_mapping;
1321 EvImageMapping *ev_image_mapping;
1323 image_mapping = (PopplerImageMapping *)list->data;
1325 ev_image_mapping = g_new (EvImageMapping, 1);
1327 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1328 ev_image_mapping->x1 = image_mapping->area.x1;
1329 ev_image_mapping->x2 = image_mapping->area.x2;
1330 ev_image_mapping->y1 = image_mapping->area.y1;
1331 ev_image_mapping->y2 = image_mapping->area.y2;
1333 retval = g_list_prepend (retval, ev_image_mapping);
1336 poppler_page_free_image_mapping (mapping_list);
1337 g_object_unref (poppler_page);
1339 return g_list_reverse (retval);
1343 pdf_document_images_get_image (EvDocumentImages *document_images,
1346 GdkPixbuf *retval = NULL;
1347 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1348 PdfDocument *pdf_document;
1349 PopplerPage *poppler_page;
1350 cairo_surface_t *surface;
1352 pdf_document = PDF_DOCUMENT (document_images);
1353 poppler_page = poppler_document_get_page (pdf_document->document,
1354 ev_image_get_page (image));
1356 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1358 retval = ev_document_misc_pixbuf_from_surface (surface);
1359 cairo_surface_destroy (surface);
1362 g_object_unref (poppler_page);
1368 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1370 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1371 iface->get_image = pdf_document_images_get_image;
1375 make_thumbnail_for_page (PopplerPage *poppler_page,
1376 EvRenderContext *rc,
1382 #ifdef POPPLER_WITH_GDK
1383 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1385 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1387 ev_document_fc_mutex_lock ();
1388 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1390 rc->scale, rc->rotation, pixbuf);
1391 ev_document_fc_mutex_unlock ();
1393 cairo_surface_t *surface;
1395 ev_document_fc_mutex_lock ();
1396 surface = pdf_page_render (poppler_page, width, height, rc);
1397 ev_document_fc_mutex_unlock ();
1399 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1400 cairo_surface_destroy (surface);
1401 #endif /* POPPLER_WITH_GDK */
1407 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1408 EvRenderContext *rc,
1411 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1412 PopplerPage *poppler_page;
1413 GdkPixbuf *pixbuf = NULL;
1414 GdkPixbuf *border_pixbuf;
1417 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1419 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1420 rc, &width, &height);
1422 #ifdef POPPLER_WITH_GDK
1423 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1425 cairo_surface_t *surface;
1427 surface = poppler_page_get_thumbnail (poppler_page);
1429 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1430 cairo_surface_destroy (surface);
1432 #endif /* POPPLER_WITH_GDK */
1434 if (pixbuf != NULL) {
1435 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1436 gdk_pixbuf_get_height (pixbuf) :
1437 gdk_pixbuf_get_width (pixbuf);
1439 if (thumb_width == width) {
1440 GdkPixbuf *rotated_pixbuf;
1442 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1443 (GdkPixbufRotation) (360 - rc->rotation));
1444 g_object_unref (pixbuf);
1445 pixbuf = rotated_pixbuf;
1447 /* The provided thumbnail has a different size */
1448 g_object_unref (pixbuf);
1449 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1452 /* There is no provided thumbnail. We need to make one. */
1453 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1456 if (border && pixbuf) {
1457 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1458 g_object_unref (pixbuf);
1459 pixbuf = border_pixbuf;
1466 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1467 EvRenderContext *rc,
1471 double page_width, page_height;
1473 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1474 &page_width, &page_height);
1476 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1477 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1479 if (rc->rotation == 90 || rc->rotation == 270) {
1489 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1491 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1492 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1497 pdf_document_find_find_text (EvDocumentFind *document_find,
1500 gboolean case_sensitive)
1503 PopplerPage *poppler_page;
1506 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1507 g_return_val_if_fail (text != NULL, NULL);
1509 poppler_page = POPPLER_PAGE (page->backend_page);
1511 matches = poppler_page_find_text (poppler_page, text);
1515 poppler_page_get_size (poppler_page, NULL, &height);
1516 for (l = matches; l && l->data; l = g_list_next (l)) {
1517 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1521 rect->y1 = height - rect->y2;
1522 rect->y2 = height - tmp;
1529 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1531 iface->find_text = pdf_document_find_find_text;
1535 pdf_print_context_free (PdfPrintContext *ctx)
1540 #ifdef HAVE_CAIRO_PRINT
1542 cairo_destroy (ctx->cr);
1547 poppler_ps_file_free (ctx->ps_file);
1548 ctx->ps_file = NULL;
1555 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1556 EvFileExporterContext *fc)
1558 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1559 PdfPrintContext *ctx;
1560 #ifdef HAVE_CAIRO_PRINT
1561 gdouble width, height;
1562 cairo_surface_t *surface = NULL;
1565 if (pdf_document->print_ctx)
1566 pdf_print_context_free (pdf_document->print_ctx);
1567 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1568 ctx = pdf_document->print_ctx;
1569 ctx->format = fc->format;
1571 #ifdef HAVE_CAIRO_PRINT
1572 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1574 ctx->paper_width = fc->paper_width;
1575 ctx->paper_height = fc->paper_height;
1577 switch (fc->pages_per_sheet) {
1605 ctx->pages_printed = 0;
1607 switch (fc->format) {
1608 case EV_FILE_FORMAT_PS:
1609 #ifdef HAVE_CAIRO_PS
1610 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1613 case EV_FILE_FORMAT_PDF:
1614 #ifdef HAVE_CAIRO_PDF
1615 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1619 g_assert_not_reached ();
1622 ctx->cr = cairo_create (surface);
1623 cairo_surface_destroy (surface);
1625 #else /* HAVE_CAIRO_PRINT */
1626 if (ctx->format == EV_FILE_FORMAT_PS) {
1627 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1628 fc->filename, fc->first_page,
1629 fc->last_page - fc->first_page + 1);
1630 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1631 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1633 #endif /* HAVE_CAIRO_PRINT */
1637 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1639 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1640 PdfPrintContext *ctx = pdf_document->print_ctx;
1642 g_return_if_fail (pdf_document->print_ctx != NULL);
1644 ctx->pages_printed = 0;
1646 #ifdef HAVE_CAIRO_PRINT
1647 if (ctx->paper_width > ctx->paper_height) {
1648 if (ctx->format == EV_FILE_FORMAT_PS) {
1649 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1652 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1653 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1658 #endif /* HAVE_CAIRO_PRINT */
1662 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1663 EvRenderContext *rc)
1665 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1666 PdfPrintContext *ctx = pdf_document->print_ctx;
1667 PopplerPage *poppler_page;
1668 #ifdef HAVE_CAIRO_PRINT
1669 gdouble page_width, page_height;
1672 gdouble width, height;
1673 gdouble pwidth, pheight;
1674 gdouble xscale, yscale;
1677 g_return_if_fail (pdf_document->print_ctx != NULL);
1679 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1681 #ifdef HAVE_CAIRO_PRINT
1682 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1683 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1684 poppler_page_get_size (poppler_page, &page_width, &page_height);
1686 if (page_width > page_height && page_width > ctx->paper_width) {
1692 /* Use always portrait mode and rotate when necessary */
1693 if (ctx->paper_width > ctx->paper_height) {
1694 width = ctx->paper_height;
1695 height = ctx->paper_width;
1698 width = ctx->paper_width;
1699 height = ctx->paper_height;
1702 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1715 page_width = page_height;
1719 pwidth = width / ctx->pages_x;
1720 pheight = height / ctx->pages_y;
1722 if ((page_width > pwidth || page_height > pheight) ||
1723 (page_width < pwidth && page_height < pheight)) {
1724 xscale = pwidth / page_width;
1725 yscale = pheight / page_height;
1727 if (yscale < xscale) {
1734 xscale = yscale = 1;
1739 cairo_save (ctx->cr);
1741 cairo_matrix_t matrix;
1743 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1744 cairo_matrix_init (&matrix,
1748 cairo_transform (ctx->cr, &matrix);
1751 cairo_translate (ctx->cr,
1752 x * (rotate ? pheight : pwidth),
1753 y * (rotate ? pwidth : pheight));
1754 cairo_scale (ctx->cr, xscale, yscale);
1756 poppler_page_render_for_printing (poppler_page, ctx->cr);
1758 ctx->pages_printed++;
1760 cairo_restore (ctx->cr);
1761 #else /* HAVE_CAIRO_PRINT */
1762 if (ctx->format == EV_FILE_FORMAT_PS)
1763 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1764 #endif /* HAVE_CAIRO_PRINT */
1768 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1770 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1771 PdfPrintContext *ctx = pdf_document->print_ctx;
1773 g_return_if_fail (pdf_document->print_ctx != NULL);
1775 #ifdef HAVE_CAIRO_PRINT
1776 cairo_show_page (ctx->cr);
1781 pdf_document_file_exporter_end (EvFileExporter *exporter)
1783 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1785 pdf_print_context_free (pdf_document->print_ctx);
1786 pdf_document->print_ctx = NULL;
1789 static EvFileExporterCapabilities
1790 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1792 return (EvFileExporterCapabilities) (
1793 EV_FILE_EXPORTER_CAN_PAGE_SET |
1794 EV_FILE_EXPORTER_CAN_COPIES |
1795 EV_FILE_EXPORTER_CAN_COLLATE |
1796 EV_FILE_EXPORTER_CAN_REVERSE |
1797 EV_FILE_EXPORTER_CAN_SCALE |
1798 #ifdef HAVE_CAIRO_PRINT
1799 #ifdef HAVE_POPPLER_PAGE_RENDER
1800 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1804 #ifdef HAVE_CAIRO_PDF
1805 #ifdef HAVE_POPPLER_PAGE_RENDER
1806 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1809 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1813 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1815 iface->begin = pdf_document_file_exporter_begin;
1816 iface->begin_page = pdf_document_file_exporter_begin_page;
1817 iface->do_page = pdf_document_file_exporter_do_page;
1818 iface->end_page = pdf_document_file_exporter_end_page;
1819 iface->end = pdf_document_file_exporter_end;
1820 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1823 #ifdef HAVE_POPPLER_PAGE_RENDER
1824 /* EvDocumentPrint */
1826 pdf_document_print_print_page (EvDocumentPrint *document,
1830 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1832 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1836 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1838 iface->print_page = pdf_document_print_print_page;
1840 #endif /* HAVE_POPPLER_PAGE_RENDER */
1843 pdf_selection_render_selection (EvSelection *selection,
1844 EvRenderContext *rc,
1845 cairo_surface_t **surface,
1846 EvRectangle *points,
1847 EvRectangle *old_points,
1848 EvSelectionStyle style,
1852 PopplerPage *poppler_page;
1853 double width_points, height_points;
1856 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1858 poppler_page_get_size (poppler_page,
1859 &width_points, &height_points);
1860 width = (int) ((width_points * rc->scale) + 0.5);
1861 height = (int) ((height_points * rc->scale) + 0.5);
1863 #ifdef HAVE_POPPLER_PAGE_RENDER
1865 PopplerColor text_color, base_color;
1867 text_color.red = text->red;
1868 text_color.green = text->green;
1869 text_color.blue = text->blue;
1871 base_color.red = base->red;
1872 base_color.green = base->green;
1873 base_color.blue = base->blue;
1875 if (*surface == NULL) {
1876 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1881 cr = cairo_create (*surface);
1882 cairo_scale (cr, rc->scale, rc->scale);
1883 cairo_surface_set_device_offset (*surface, 0, 0);
1884 memset (cairo_image_surface_get_data (*surface), 0x00,
1885 cairo_image_surface_get_height (*surface) *
1886 cairo_image_surface_get_stride (*surface));
1887 poppler_page_render_selection (poppler_page,
1889 (PopplerRectangle *)points,
1890 (PopplerRectangle *)old_points,
1891 (PopplerSelectionStyle)style,
1895 #else /* HAVE_POPPLER_PAGE_RENDER */
1898 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1902 poppler_page_render_selection_to_pixbuf (poppler_page,
1903 rc->scale, rc->rotation, pixbuf,
1904 (PopplerRectangle *)points,
1905 (PopplerRectangle *)old_points,
1906 (PopplerSelectionStyle)style,
1910 cairo_surface_destroy (*surface);
1911 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1912 g_object_unref (pixbuf);
1913 #endif /* HAVE_POPPLER_PAGE_RENDER */
1917 pdf_selection_get_selected_text (EvSelection *selection,
1918 EvRenderContext *rc,
1919 EvSelectionStyle style,
1920 EvRectangle *points)
1922 PopplerPage *poppler_page;
1927 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1929 poppler_page_get_size (poppler_page, NULL, &height);
1931 r.y1 = height - points->y2;
1933 r.y2 = height - points->y1;
1935 retval = poppler_page_get_text (poppler_page,
1936 (PopplerSelectionStyle)style,
1943 create_gdk_region_from_poppler_region (GList *region)
1948 retval = gdk_region_new ();
1950 for (l = region; l; l = g_list_next (l)) {
1951 PopplerRectangle *rectangle;
1954 rectangle = (PopplerRectangle *)l->data;
1956 rect.x = (gint) rectangle->x1;
1957 rect.y = (gint) rectangle->y1;
1958 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1959 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1960 gdk_region_union_with_rect (retval, &rect);
1962 poppler_rectangle_free (rectangle);
1969 pdf_selection_get_selection_region (EvSelection *selection,
1970 EvRenderContext *rc,
1971 EvSelectionStyle style,
1972 EvRectangle *points)
1974 PopplerPage *poppler_page;
1978 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1980 region = poppler_page_get_selection_region (poppler_page,
1982 (PopplerSelectionStyle)style,
1983 (PopplerRectangle *) points);
1984 retval = create_gdk_region_from_poppler_region (region);
1985 g_list_free (region);
1991 pdf_selection_get_selection_map (EvSelection *selection,
1992 EvRenderContext *rc)
1994 PopplerPage *poppler_page;
1995 PopplerRectangle points;
1999 poppler_page = POPPLER_PAGE (rc->page->backend_page);
2003 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2005 region = poppler_page_get_selection_region (poppler_page, 1.0,
2006 POPPLER_SELECTION_GLYPH,
2008 retval = create_gdk_region_from_poppler_region (region);
2009 g_list_free (region);
2015 pdf_selection_iface_init (EvSelectionIface *iface)
2017 iface->render_selection = pdf_selection_render_selection;
2018 iface->get_selected_text = pdf_selection_get_selected_text;
2019 iface->get_selection_region = pdf_selection_get_selection_region;
2020 iface->get_selection_map = pdf_selection_get_selection_map;
2023 /* Page Transitions */
2025 pdf_document_get_page_duration (EvDocumentTransition *trans,
2028 PdfDocument *pdf_document;
2029 PopplerPage *poppler_page;
2030 gdouble duration = -1;
2032 pdf_document = PDF_DOCUMENT (trans);
2033 poppler_page = poppler_document_get_page (pdf_document->document, page);
2037 duration = poppler_page_get_duration (poppler_page);
2038 g_object_unref (poppler_page);
2043 static EvTransitionEffect *
2044 pdf_document_get_effect (EvDocumentTransition *trans,
2047 PdfDocument *pdf_document;
2048 PopplerPage *poppler_page;
2049 PopplerPageTransition *page_transition;
2050 EvTransitionEffect *effect;
2052 pdf_document = PDF_DOCUMENT (trans);
2053 poppler_page = poppler_document_get_page (pdf_document->document, page);
2058 page_transition = poppler_page_get_transition (poppler_page);
2060 if (!page_transition) {
2061 g_object_unref (poppler_page);
2065 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2066 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2067 "alignment", page_transition->alignment,
2068 "direction", page_transition->direction,
2069 "duration", page_transition->duration,
2070 "angle", page_transition->angle,
2071 "scale", page_transition->scale,
2072 "rectangular", page_transition->rectangular,
2075 poppler_page_transition_free (page_transition);
2076 g_object_unref (poppler_page);
2082 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2084 iface->get_page_duration = pdf_document_get_page_duration;
2085 iface->get_effect = pdf_document_get_effect;
2090 pdf_document_get_crop_box (EvDocument *document,
2094 PdfDocument *pdf_document;
2095 PopplerPage *poppler_page;
2096 PopplerRectangle poppler_rect;
2098 pdf_document = PDF_DOCUMENT (document);
2099 poppler_page = poppler_document_get_page (pdf_document->document, page);
2100 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2101 rect->x1 = poppler_rect.x1;
2102 rect->x2 = poppler_rect.x2;
2103 rect->y1 = poppler_rect.y1;
2104 rect->y2 = poppler_rect.y2;
2107 static EvFormField *
2108 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2110 EvFormField *ev_field = NULL;
2113 gboolean is_read_only;
2115 id = poppler_form_field_get_id (poppler_field);
2116 font_size = poppler_form_field_get_font_size (poppler_field);
2117 is_read_only = poppler_form_field_is_read_only (poppler_field);
2119 switch (poppler_form_field_get_field_type (poppler_field)) {
2120 case POPPLER_FORM_FIELD_TEXT: {
2121 EvFormFieldText *field_text;
2122 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2124 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2125 case POPPLER_FORM_TEXT_NORMAL:
2126 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2128 case POPPLER_FORM_TEXT_MULTILINE:
2129 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2131 case POPPLER_FORM_TEXT_FILE_SELECT:
2132 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2136 ev_field = ev_form_field_text_new (id, ev_text_type);
2137 field_text = EV_FORM_FIELD_TEXT (ev_field);
2139 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2140 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2141 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2142 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2143 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2144 field_text->text = poppler_form_field_text_get_text (poppler_field);
2148 case POPPLER_FORM_FIELD_BUTTON: {
2149 EvFormFieldButton *field_button;
2150 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2152 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2153 case POPPLER_FORM_BUTTON_PUSH:
2154 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2156 case POPPLER_FORM_BUTTON_CHECK:
2157 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2159 case POPPLER_FORM_BUTTON_RADIO:
2160 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2164 ev_field = ev_form_field_button_new (id, ev_button_type);
2165 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2167 field_button->state = poppler_form_field_button_get_state (poppler_field);
2170 case POPPLER_FORM_FIELD_CHOICE: {
2171 EvFormFieldChoice *field_choice;
2172 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2174 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2175 case POPPLER_FORM_CHOICE_COMBO:
2176 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2178 case EV_FORM_FIELD_CHOICE_LIST:
2179 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2183 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2184 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2186 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2187 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2188 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2189 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2191 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2192 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2193 if (field_choice->is_editable)
2194 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2197 case POPPLER_FORM_FIELD_SIGNATURE:
2199 ev_field = ev_form_field_signature_new (id);
2201 case POPPLER_FORM_FIELD_UNKNOWN:
2205 ev_field->font_size = font_size;
2206 ev_field->is_read_only = is_read_only;
2212 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2215 PopplerPage *poppler_page;
2216 GList *retval = NULL;
2221 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2223 poppler_page = POPPLER_PAGE (page->backend_page);
2224 fields = poppler_page_get_form_field_mapping (poppler_page);
2225 poppler_page_get_size (poppler_page, NULL, &height);
2227 for (list = fields; list; list = list->next) {
2228 PopplerFormFieldMapping *mapping;
2229 EvFormFieldMapping *field_mapping;
2230 EvFormField *ev_field;
2232 mapping = (PopplerFormFieldMapping *)list->data;
2234 ev_field = ev_form_field_from_poppler_field (mapping->field);
2238 field_mapping = g_new0 (EvFormFieldMapping, 1);
2239 field_mapping->x1 = mapping->area.x1;
2240 field_mapping->x2 = mapping->area.x2;
2241 field_mapping->y1 = height - mapping->area.y2;
2242 field_mapping->y2 = height - mapping->area.y1;
2243 field_mapping->field = ev_field;
2244 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2246 g_object_set_data_full (G_OBJECT (ev_field),
2248 g_object_ref (mapping->field),
2249 (GDestroyNotify) g_object_unref);
2251 retval = g_list_prepend (retval, field_mapping);
2254 poppler_page_free_form_field_mapping (fields);
2256 return g_list_reverse (retval);
2260 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2264 PopplerFormField *poppler_field;
2267 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2271 text = poppler_form_field_text_get_text (poppler_field);
2277 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2281 PopplerFormField *poppler_field;
2283 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2287 poppler_form_field_text_set_text (poppler_field, text);
2288 PDF_DOCUMENT (document)->modified = TRUE;
2292 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2296 PopplerFormField *poppler_field;
2298 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2302 poppler_form_field_button_set_state (poppler_field, state);
2303 PDF_DOCUMENT (document)->modified = TRUE;
2307 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2310 PopplerFormField *poppler_field;
2313 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2317 state = poppler_form_field_button_get_state (poppler_field);
2323 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2327 PopplerFormField *poppler_field;
2330 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2334 text = poppler_form_field_choice_get_item (poppler_field, index);
2340 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2343 PopplerFormField *poppler_field;
2346 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2350 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2356 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2360 PopplerFormField *poppler_field;
2363 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2367 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2373 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2377 PopplerFormField *poppler_field;
2379 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2383 poppler_form_field_choice_select_item (poppler_field, index);
2384 PDF_DOCUMENT (document)->modified = TRUE;
2388 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2392 PopplerFormField *poppler_field;
2394 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2398 poppler_form_field_choice_toggle_item (poppler_field, index);
2399 PDF_DOCUMENT (document)->modified = TRUE;
2403 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2406 PopplerFormField *poppler_field;
2408 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2412 poppler_form_field_choice_unselect_all (poppler_field);
2413 PDF_DOCUMENT (document)->modified = TRUE;
2417 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2421 PopplerFormField *poppler_field;
2423 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2427 poppler_form_field_choice_set_text (poppler_field, text);
2428 PDF_DOCUMENT (document)->modified = TRUE;
2432 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2435 PopplerFormField *poppler_field;
2438 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2442 text = poppler_form_field_choice_get_text (poppler_field);
2448 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2450 iface->get_form_fields = pdf_document_forms_get_form_fields;
2451 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2452 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2453 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2454 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2455 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2456 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2457 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2458 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2459 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2460 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2461 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2462 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2467 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2470 PopplerColor *poppler_color;
2472 poppler_color = poppler_annot_get_color (poppler_annot);
2473 if (poppler_color) {
2474 color->red = poppler_color->red;
2475 color->green = poppler_color->green;
2476 color->blue = poppler_color->blue;
2478 g_free (poppler_color);
2479 } /* TODO: else use a default color */
2482 static EvAnnotation *
2483 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2486 EvAnnotation *ev_annot = NULL;
2487 const gchar *unimplemented_annot = NULL;
2489 switch (poppler_annot_get_annot_type (poppler_annot)) {
2490 case POPPLER_ANNOT_TEXT:
2491 PopplerAnnotText *poppler_text;
2492 EvAnnotationText *ev_annot_text;
2494 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2496 ev_annot = ev_annotation_text_new (page);
2498 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2499 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2502 case POPPLER_ANNOT_LINK:
2503 case POPPLER_ANNOT_WIDGET:
2504 /* Ignore link and widgets annots since they are already handled */
2507 GEnumValue *enum_value;
2509 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2510 poppler_annot_get_annot_type (poppler_annot));
2511 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2515 if (unimplemented_annot) {
2516 g_warning ("Unimplemented annotation: %s, please post a "
2517 "bug report in Evince bugzilla "
2518 "(http://bugzilla.gnome.org) with a testcase.",
2519 unimplemented_annot);
2523 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2524 ev_annot->name = poppler_annot_get_name (poppler_annot);
2525 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2526 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2528 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2529 PopplerAnnotMarkup *markup;
2533 PopplerRectangle poppler_rect;
2535 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2537 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2538 EvRectangle ev_rect;
2541 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2543 ev_rect.x1 = poppler_rect.x1;
2544 ev_rect.x2 = poppler_rect.x2;
2545 ev_rect.y1 = height - poppler_rect.y2;
2546 ev_rect.y2 = height - poppler_rect.y1;
2548 g_object_set (ev_annot, "rectangle", &ev_rect, NULL);
2551 label = poppler_annot_markup_get_label (markup);
2552 opacity = poppler_annot_markup_get_opacity (markup);
2553 is_open = poppler_annot_markup_get_popup_is_open (markup);
2555 g_object_set (ev_annot,
2569 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2572 GList *retval = NULL;
2573 PdfDocument *pdf_document;
2574 PopplerPage *poppler_page;
2580 pdf_document = PDF_DOCUMENT (document_annotations);
2581 poppler_page = POPPLER_PAGE (page->backend_page);
2582 annots = poppler_page_get_annot_mapping (poppler_page);
2583 poppler_page_get_size (poppler_page, NULL, &height);
2585 for (list = annots; list; list = list->next) {
2586 PopplerAnnotMapping *mapping;
2587 EvAnnotationMapping *annot_mapping;
2588 EvAnnotation *ev_annot;
2590 mapping = (PopplerAnnotMapping *)list->data;
2592 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2598 /* Make sure annot has a unique name */
2599 if (!ev_annot->name)
2600 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2602 annot_mapping = g_new0 (EvAnnotationMapping, 1);
2603 annot_mapping->x1 = mapping->area.x1;
2604 annot_mapping->x2 = mapping->area.x2;
2605 annot_mapping->y1 = height - mapping->area.y2;
2606 annot_mapping->y2 = height - mapping->area.y1;
2607 annot_mapping->annotation = ev_annot;
2609 g_object_set_data_full (G_OBJECT (ev_annot),
2611 g_object_ref (mapping->annot),
2612 (GDestroyNotify) g_object_unref);
2614 retval = g_list_prepend (retval, annot_mapping);
2617 poppler_page_free_annot_mapping (annots);
2619 return g_list_reverse (retval);
2623 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2624 EvAnnotation *annot,
2625 const gchar *contents)
2627 PopplerAnnot *poppler_annot;
2629 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2633 poppler_annot_set_contents (poppler_annot, contents);
2634 PDF_DOCUMENT (document)->modified = TRUE;
2638 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2640 iface->get_annotations = pdf_document_annotations_get_annotations;
2641 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2646 pdf_document_layers_has_layers (EvDocumentLayers *document)
2648 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2649 PopplerLayersIter *iter;
2651 iter = poppler_layers_iter_new (pdf_document->document);
2654 poppler_layers_iter_free (iter);
2660 build_layers_tree (PdfDocument *pdf_document,
2661 GtkTreeModel *model,
2662 GtkTreeIter *parent,
2663 PopplerLayersIter *iter)
2666 GtkTreeIter tree_iter;
2667 PopplerLayersIter *child;
2668 PopplerLayer *layer;
2669 EvLayer *ev_layer = NULL;
2674 layer = poppler_layers_iter_get_layer (iter);
2676 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2677 visible = poppler_layer_is_visible (layer);
2678 rb_group = poppler_layer_get_radio_button_group_id (layer);
2679 pdf_document->layers = g_list_append (pdf_document->layers,
2680 g_object_ref (layer));
2681 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2682 poppler_layer_is_parent (layer),
2687 title = poppler_layers_iter_get_title (iter);
2688 markup = g_markup_escape_text (title, -1);
2695 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2696 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2697 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2698 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2699 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2700 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2701 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2702 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2705 g_object_unref (ev_layer);
2708 child = poppler_layers_iter_get_child (iter);
2710 build_layers_tree (pdf_document, model, &tree_iter, child);
2711 poppler_layers_iter_free (child);
2712 } while (poppler_layers_iter_next (iter));
2715 static GtkTreeModel *
2716 pdf_document_layers_get_layers (EvDocumentLayers *document)
2718 GtkTreeModel *model = NULL;
2719 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2720 PopplerLayersIter *iter;
2722 iter = poppler_layers_iter_new (pdf_document->document);
2724 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2725 G_TYPE_STRING, /* TITLE */
2726 G_TYPE_OBJECT, /* LAYER */
2727 G_TYPE_BOOLEAN, /* VISIBLE */
2728 G_TYPE_BOOLEAN, /* ENABLED */
2729 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2730 G_TYPE_INT); /* RBGROUP */
2731 build_layers_tree (pdf_document, model, NULL, iter);
2732 poppler_layers_iter_free (iter);
2738 pdf_document_layers_show_layer (EvDocumentLayers *document,
2741 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2742 guint layer_id = ev_layer_get_id (layer);
2744 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2748 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2751 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2752 guint layer_id = ev_layer_get_id (layer);
2754 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2758 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2761 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2762 guint layer_id = ev_layer_get_id (layer);
2764 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2768 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2770 iface->has_layers = pdf_document_layers_has_layers;
2771 iface->get_layers = pdf_document_layers_get_layers;
2772 iface->show_layer = pdf_document_layers_show_layer;
2773 iface->hide_layer = pdf_document_layers_hide_layer;
2774 iface->layer_is_visible = pdf_document_layers_layer_is_visible;