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_FIT:
1019 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
1021 case POPPLER_DEST_FITH: {
1022 PopplerPage *poppler_page;
1025 poppler_page = poppler_document_get_page (pdf_document->document,
1026 MAX (0, dest->page_num - 1));
1027 poppler_page_get_size (poppler_page, NULL, &height);
1028 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
1029 height - MIN (height, dest->top),
1031 g_object_unref (poppler_page);
1034 case POPPLER_DEST_FITV:
1035 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
1039 case POPPLER_DEST_FITR: {
1040 PopplerPage *poppler_page;
1043 poppler_page = poppler_document_get_page (pdf_document->document,
1044 MAX (0, dest->page_num - 1));
1045 poppler_page_get_size (poppler_page, NULL, &height);
1046 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
1048 height - MIN (height, dest->bottom),
1050 height - MIN (height, dest->top));
1051 g_object_unref (poppler_page);
1054 case POPPLER_DEST_FITB:
1055 unimplemented_dest = "POPPLER_DEST_FITB";
1057 case POPPLER_DEST_FITBH:
1058 unimplemented_dest = "POPPLER_DEST_FITBH";
1060 case POPPLER_DEST_FITBV:
1061 unimplemented_dest = "POPPLER_DEST_FITBV";
1063 case POPPLER_DEST_NAMED:
1064 ev_dest = ev_link_dest_new_named (dest->named_dest);
1066 case POPPLER_DEST_UNKNOWN:
1067 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
1071 if (unimplemented_dest) {
1072 g_warning ("Unimplemented destination: %s, please post a "
1073 "bug report in Evince bugzilla "
1074 "(http://bugzilla.gnome.org) with a testcase.",
1075 unimplemented_dest);
1079 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
1085 ev_link_from_action (PdfDocument *pdf_document,
1086 PopplerAction *action)
1088 EvLink *link = NULL;
1089 EvLinkAction *ev_action = NULL;
1090 const char *unimplemented_action = NULL;
1092 switch (action->type) {
1093 case POPPLER_ACTION_NONE:
1095 case POPPLER_ACTION_GOTO_DEST: {
1098 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
1099 ev_action = ev_link_action_new_dest (dest);
1102 case POPPLER_ACTION_GOTO_REMOTE: {
1105 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
1106 ev_action = ev_link_action_new_remote (dest,
1107 action->goto_remote.file_name);
1111 case POPPLER_ACTION_LAUNCH:
1112 ev_action = ev_link_action_new_launch (action->launch.file_name,
1113 action->launch.params);
1115 case POPPLER_ACTION_URI:
1116 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1118 case POPPLER_ACTION_NAMED:
1119 ev_action = ev_link_action_new_named (action->named.named_dest);
1121 case POPPLER_ACTION_MOVIE:
1122 unimplemented_action = "POPPLER_ACTION_MOVIE";
1124 case POPPLER_ACTION_UNKNOWN:
1125 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1128 if (unimplemented_action) {
1129 g_warning ("Unimplemented action: %s, please post a bug report "
1130 "in Evince bugzilla (http://bugzilla.gnome.org) "
1131 "with a testcase.", unimplemented_action);
1134 link = ev_link_new (action->any.title, ev_action);
1140 build_tree (PdfDocument *pdf_document,
1141 GtkTreeModel *model,
1142 GtkTreeIter *parent,
1143 PopplerIndexIter *iter)
1147 GtkTreeIter tree_iter;
1148 PopplerIndexIter *child;
1149 PopplerAction *action;
1150 EvLink *link = NULL;
1154 action = poppler_index_iter_get_action (iter);
1155 expand = poppler_index_iter_is_open (iter);
1160 switch (action->type) {
1161 case POPPLER_ACTION_GOTO_DEST: {
1162 /* For bookmarks, solve named destinations */
1163 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1165 EvLinkDest *ev_dest = NULL;
1166 EvLinkAction *ev_action;
1168 dest = poppler_document_find_dest (pdf_document->document,
1169 action->goto_dest.dest->named_dest);
1171 link = ev_link_from_action (pdf_document, action);
1175 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1176 poppler_dest_free (dest);
1178 ev_action = ev_link_action_new_dest (ev_dest);
1179 link = ev_link_new (action->any.title, ev_action);
1181 link = ev_link_from_action (pdf_document, action);
1186 link = ev_link_from_action (pdf_document, action);
1190 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1191 poppler_action_free (action);
1193 g_object_unref (link);
1198 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1199 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1201 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1202 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1203 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1204 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1207 g_free (title_markup);
1208 g_object_unref (link);
1210 child = poppler_index_iter_get_child (iter);
1212 build_tree (pdf_document, model, &tree_iter, child);
1213 poppler_index_iter_free (child);
1214 poppler_action_free (action);
1216 } while (poppler_index_iter_next (iter));
1219 static GtkTreeModel *
1220 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1222 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1223 GtkTreeModel *model = NULL;
1224 PopplerIndexIter *iter;
1226 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1228 iter = poppler_index_iter_new (pdf_document->document);
1229 /* Create the model if we have items*/
1231 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1236 build_tree (pdf_document, model, NULL, iter);
1237 poppler_index_iter_free (iter);
1244 pdf_document_links_get_links (EvDocumentLinks *document_links,
1247 PdfDocument *pdf_document;
1248 PopplerPage *poppler_page;
1249 GList *retval = NULL;
1250 GList *mapping_list;
1254 pdf_document = PDF_DOCUMENT (document_links);
1255 poppler_page = poppler_document_get_page (pdf_document->document,
1257 mapping_list = poppler_page_get_link_mapping (poppler_page);
1258 poppler_page_get_size (poppler_page, NULL, &height);
1260 for (list = mapping_list; list; list = list->next) {
1261 PopplerLinkMapping *link_mapping;
1262 EvLinkMapping *ev_link_mapping;
1264 link_mapping = (PopplerLinkMapping *)list->data;
1265 ev_link_mapping = g_new (EvLinkMapping, 1);
1266 ev_link_mapping->link = ev_link_from_action (pdf_document,
1267 link_mapping->action);
1268 ev_link_mapping->x1 = link_mapping->area.x1;
1269 ev_link_mapping->x2 = link_mapping->area.x2;
1270 /* Invert this for X-style coordinates */
1271 ev_link_mapping->y1 = height - link_mapping->area.y2;
1272 ev_link_mapping->y2 = height - link_mapping->area.y1;
1274 retval = g_list_prepend (retval, ev_link_mapping);
1277 poppler_page_free_link_mapping (mapping_list);
1278 g_object_unref (poppler_page);
1280 return g_list_reverse (retval);
1284 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1285 const gchar *link_name)
1287 PdfDocument *pdf_document;
1289 EvLinkDest *ev_dest = NULL;
1291 pdf_document = PDF_DOCUMENT (document_links);
1292 dest = poppler_document_find_dest (pdf_document->document,
1295 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1296 poppler_dest_free (dest);
1303 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1305 iface->has_document_links = pdf_document_links_has_document_links;
1306 iface->get_links_model = pdf_document_links_get_links_model;
1307 iface->get_links = pdf_document_links_get_links;
1308 iface->find_link_dest = pdf_document_links_find_link_dest;
1312 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1315 GList *retval = NULL;
1316 PdfDocument *pdf_document;
1317 PopplerPage *poppler_page;
1318 GList *mapping_list;
1321 pdf_document = PDF_DOCUMENT (document_images);
1322 poppler_page = poppler_document_get_page (pdf_document->document, page);
1323 mapping_list = poppler_page_get_image_mapping (poppler_page);
1325 for (list = mapping_list; list; list = list->next) {
1326 PopplerImageMapping *image_mapping;
1327 EvImageMapping *ev_image_mapping;
1329 image_mapping = (PopplerImageMapping *)list->data;
1331 ev_image_mapping = g_new (EvImageMapping, 1);
1333 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1334 ev_image_mapping->x1 = image_mapping->area.x1;
1335 ev_image_mapping->x2 = image_mapping->area.x2;
1336 ev_image_mapping->y1 = image_mapping->area.y1;
1337 ev_image_mapping->y2 = image_mapping->area.y2;
1339 retval = g_list_prepend (retval, ev_image_mapping);
1342 poppler_page_free_image_mapping (mapping_list);
1343 g_object_unref (poppler_page);
1345 return g_list_reverse (retval);
1349 pdf_document_images_get_image (EvDocumentImages *document_images,
1352 GdkPixbuf *retval = NULL;
1353 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1354 PdfDocument *pdf_document;
1355 PopplerPage *poppler_page;
1356 cairo_surface_t *surface;
1358 pdf_document = PDF_DOCUMENT (document_images);
1359 poppler_page = poppler_document_get_page (pdf_document->document,
1360 ev_image_get_page (image));
1362 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1364 retval = ev_document_misc_pixbuf_from_surface (surface);
1365 cairo_surface_destroy (surface);
1368 g_object_unref (poppler_page);
1374 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1376 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1377 iface->get_image = pdf_document_images_get_image;
1381 make_thumbnail_for_page (PopplerPage *poppler_page,
1382 EvRenderContext *rc,
1388 #ifdef POPPLER_WITH_GDK
1389 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1391 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1393 ev_document_fc_mutex_lock ();
1394 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1396 rc->scale, rc->rotation, pixbuf);
1397 ev_document_fc_mutex_unlock ();
1399 cairo_surface_t *surface;
1401 ev_document_fc_mutex_lock ();
1402 surface = pdf_page_render (poppler_page, width, height, rc);
1403 ev_document_fc_mutex_unlock ();
1405 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1406 cairo_surface_destroy (surface);
1407 #endif /* POPPLER_WITH_GDK */
1413 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1414 EvRenderContext *rc,
1417 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1418 PopplerPage *poppler_page;
1419 GdkPixbuf *pixbuf = NULL;
1420 GdkPixbuf *border_pixbuf;
1423 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1425 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1426 rc, &width, &height);
1428 #ifdef POPPLER_WITH_GDK
1429 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1431 cairo_surface_t *surface;
1433 surface = poppler_page_get_thumbnail (poppler_page);
1435 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1436 cairo_surface_destroy (surface);
1438 #endif /* POPPLER_WITH_GDK */
1440 if (pixbuf != NULL) {
1441 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1442 gdk_pixbuf_get_height (pixbuf) :
1443 gdk_pixbuf_get_width (pixbuf);
1445 if (thumb_width == width) {
1446 GdkPixbuf *rotated_pixbuf;
1448 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1449 (GdkPixbufRotation) (360 - rc->rotation));
1450 g_object_unref (pixbuf);
1451 pixbuf = rotated_pixbuf;
1453 /* The provided thumbnail has a different size */
1454 g_object_unref (pixbuf);
1455 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1458 /* There is no provided thumbnail. We need to make one. */
1459 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1462 if (border && pixbuf) {
1463 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1464 g_object_unref (pixbuf);
1465 pixbuf = border_pixbuf;
1472 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1473 EvRenderContext *rc,
1477 double page_width, page_height;
1479 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1480 &page_width, &page_height);
1482 *width = (gint) MAX (page_width * rc->scale, 1);
1483 *height = (gint) MAX (page_height * rc->scale, 1);
1485 if (rc->rotation == 90 || rc->rotation == 270) {
1495 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1497 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1498 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1503 pdf_document_find_find_text (EvDocumentFind *document_find,
1506 gboolean case_sensitive)
1509 PopplerPage *poppler_page;
1512 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1513 g_return_val_if_fail (text != NULL, NULL);
1515 poppler_page = POPPLER_PAGE (page->backend_page);
1517 matches = poppler_page_find_text (poppler_page, text);
1521 poppler_page_get_size (poppler_page, NULL, &height);
1522 for (l = matches; l && l->data; l = g_list_next (l)) {
1523 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1527 rect->y1 = height - rect->y2;
1528 rect->y2 = height - tmp;
1535 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1537 iface->find_text = pdf_document_find_find_text;
1541 pdf_print_context_free (PdfPrintContext *ctx)
1546 #ifdef HAVE_CAIRO_PRINT
1548 cairo_destroy (ctx->cr);
1553 poppler_ps_file_free (ctx->ps_file);
1554 ctx->ps_file = NULL;
1561 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1562 EvFileExporterContext *fc)
1564 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1565 PdfPrintContext *ctx;
1566 #ifdef HAVE_CAIRO_PRINT
1567 gdouble width, height;
1568 cairo_surface_t *surface = NULL;
1571 if (pdf_document->print_ctx)
1572 pdf_print_context_free (pdf_document->print_ctx);
1573 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1574 ctx = pdf_document->print_ctx;
1575 ctx->format = fc->format;
1577 #ifdef HAVE_CAIRO_PRINT
1578 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1580 ctx->paper_width = fc->paper_width;
1581 ctx->paper_height = fc->paper_height;
1583 switch (fc->pages_per_sheet) {
1611 ctx->pages_printed = 0;
1613 switch (fc->format) {
1614 case EV_FILE_FORMAT_PS:
1615 #ifdef HAVE_CAIRO_PS
1616 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1619 case EV_FILE_FORMAT_PDF:
1620 #ifdef HAVE_CAIRO_PDF
1621 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1625 g_assert_not_reached ();
1628 ctx->cr = cairo_create (surface);
1629 cairo_surface_destroy (surface);
1631 #else /* HAVE_CAIRO_PRINT */
1632 if (ctx->format == EV_FILE_FORMAT_PS) {
1633 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1634 fc->filename, fc->first_page,
1635 fc->last_page - fc->first_page + 1);
1636 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1637 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1639 #endif /* HAVE_CAIRO_PRINT */
1643 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1645 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1646 PdfPrintContext *ctx = pdf_document->print_ctx;
1648 g_return_if_fail (pdf_document->print_ctx != NULL);
1650 ctx->pages_printed = 0;
1652 #ifdef HAVE_CAIRO_PRINT
1653 if (ctx->paper_width > ctx->paper_height) {
1654 if (ctx->format == EV_FILE_FORMAT_PS) {
1655 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1658 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1659 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1664 #endif /* HAVE_CAIRO_PRINT */
1668 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1669 EvRenderContext *rc)
1671 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1672 PdfPrintContext *ctx = pdf_document->print_ctx;
1673 PopplerPage *poppler_page;
1674 #ifdef HAVE_CAIRO_PRINT
1675 gdouble page_width, page_height;
1678 gdouble width, height;
1679 gdouble pwidth, pheight;
1680 gdouble xscale, yscale;
1683 g_return_if_fail (pdf_document->print_ctx != NULL);
1685 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1687 #ifdef HAVE_CAIRO_PRINT
1688 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1689 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1690 poppler_page_get_size (poppler_page, &page_width, &page_height);
1692 if (page_width > page_height && page_width > ctx->paper_width) {
1698 /* Use always portrait mode and rotate when necessary */
1699 if (ctx->paper_width > ctx->paper_height) {
1700 width = ctx->paper_height;
1701 height = ctx->paper_width;
1704 width = ctx->paper_width;
1705 height = ctx->paper_height;
1708 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1721 page_width = page_height;
1725 pwidth = width / ctx->pages_x;
1726 pheight = height / ctx->pages_y;
1728 if ((page_width > pwidth || page_height > pheight) ||
1729 (page_width < pwidth && page_height < pheight)) {
1730 xscale = pwidth / page_width;
1731 yscale = pheight / page_height;
1733 if (yscale < xscale) {
1740 xscale = yscale = 1;
1745 cairo_save (ctx->cr);
1747 cairo_matrix_t matrix;
1749 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1750 cairo_matrix_init (&matrix,
1754 cairo_transform (ctx->cr, &matrix);
1757 cairo_translate (ctx->cr,
1758 x * (rotate ? pheight : pwidth),
1759 y * (rotate ? pwidth : pheight));
1760 cairo_scale (ctx->cr, xscale, yscale);
1762 poppler_page_render_for_printing (poppler_page, ctx->cr);
1764 ctx->pages_printed++;
1766 cairo_restore (ctx->cr);
1767 #else /* HAVE_CAIRO_PRINT */
1768 if (ctx->format == EV_FILE_FORMAT_PS)
1769 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1770 #endif /* HAVE_CAIRO_PRINT */
1774 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1776 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1777 PdfPrintContext *ctx = pdf_document->print_ctx;
1779 g_return_if_fail (pdf_document->print_ctx != NULL);
1781 #ifdef HAVE_CAIRO_PRINT
1782 cairo_show_page (ctx->cr);
1787 pdf_document_file_exporter_end (EvFileExporter *exporter)
1789 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1791 pdf_print_context_free (pdf_document->print_ctx);
1792 pdf_document->print_ctx = NULL;
1795 static EvFileExporterCapabilities
1796 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1798 return (EvFileExporterCapabilities) (
1799 EV_FILE_EXPORTER_CAN_PAGE_SET |
1800 EV_FILE_EXPORTER_CAN_COPIES |
1801 EV_FILE_EXPORTER_CAN_COLLATE |
1802 EV_FILE_EXPORTER_CAN_REVERSE |
1803 EV_FILE_EXPORTER_CAN_SCALE |
1804 #ifdef HAVE_CAIRO_PRINT
1805 #ifdef HAVE_POPPLER_PAGE_RENDER
1806 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1810 #ifdef HAVE_CAIRO_PDF
1811 #ifdef HAVE_POPPLER_PAGE_RENDER
1812 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1815 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1819 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1821 iface->begin = pdf_document_file_exporter_begin;
1822 iface->begin_page = pdf_document_file_exporter_begin_page;
1823 iface->do_page = pdf_document_file_exporter_do_page;
1824 iface->end_page = pdf_document_file_exporter_end_page;
1825 iface->end = pdf_document_file_exporter_end;
1826 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1829 #ifdef HAVE_POPPLER_PAGE_RENDER
1830 /* EvDocumentPrint */
1832 pdf_document_print_print_page (EvDocumentPrint *document,
1836 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1838 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1842 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1844 iface->print_page = pdf_document_print_print_page;
1846 #endif /* HAVE_POPPLER_PAGE_RENDER */
1849 pdf_selection_render_selection (EvSelection *selection,
1850 EvRenderContext *rc,
1851 cairo_surface_t **surface,
1852 EvRectangle *points,
1853 EvRectangle *old_points,
1854 EvSelectionStyle style,
1858 PopplerPage *poppler_page;
1859 double width_points, height_points;
1862 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1864 poppler_page_get_size (poppler_page,
1865 &width_points, &height_points);
1866 width = (int) ((width_points * rc->scale) + 0.5);
1867 height = (int) ((height_points * rc->scale) + 0.5);
1869 #ifdef HAVE_POPPLER_PAGE_RENDER
1871 PopplerColor text_color, base_color;
1873 text_color.red = text->red;
1874 text_color.green = text->green;
1875 text_color.blue = text->blue;
1877 base_color.red = base->red;
1878 base_color.green = base->green;
1879 base_color.blue = base->blue;
1881 if (*surface == NULL) {
1882 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1887 cr = cairo_create (*surface);
1888 cairo_scale (cr, rc->scale, rc->scale);
1889 cairo_surface_set_device_offset (*surface, 0, 0);
1890 memset (cairo_image_surface_get_data (*surface), 0x00,
1891 cairo_image_surface_get_height (*surface) *
1892 cairo_image_surface_get_stride (*surface));
1893 poppler_page_render_selection (poppler_page,
1895 (PopplerRectangle *)points,
1896 (PopplerRectangle *)old_points,
1897 (PopplerSelectionStyle)style,
1901 #else /* HAVE_POPPLER_PAGE_RENDER */
1904 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1908 poppler_page_render_selection_to_pixbuf (poppler_page,
1909 rc->scale, rc->rotation, pixbuf,
1910 (PopplerRectangle *)points,
1911 (PopplerRectangle *)old_points,
1912 (PopplerSelectionStyle)style,
1916 cairo_surface_destroy (*surface);
1917 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1918 g_object_unref (pixbuf);
1919 #endif /* HAVE_POPPLER_PAGE_RENDER */
1923 pdf_selection_get_selected_text (EvSelection *selection,
1924 EvRenderContext *rc,
1925 EvSelectionStyle style,
1926 EvRectangle *points)
1928 PopplerPage *poppler_page;
1933 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1935 poppler_page_get_size (poppler_page, NULL, &height);
1937 r.y1 = height - points->y2;
1939 r.y2 = height - points->y1;
1941 retval = poppler_page_get_text (poppler_page,
1942 (PopplerSelectionStyle)style,
1949 create_gdk_region_from_poppler_region (GList *region)
1954 retval = gdk_region_new ();
1956 for (l = region; l; l = g_list_next (l)) {
1957 PopplerRectangle *rectangle;
1960 rectangle = (PopplerRectangle *)l->data;
1962 rect.x = (gint) rectangle->x1;
1963 rect.y = (gint) rectangle->y1;
1964 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1965 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1966 gdk_region_union_with_rect (retval, &rect);
1968 poppler_rectangle_free (rectangle);
1975 pdf_selection_get_selection_region (EvSelection *selection,
1976 EvRenderContext *rc,
1977 EvSelectionStyle style,
1978 EvRectangle *points)
1980 PopplerPage *poppler_page;
1984 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1986 region = poppler_page_get_selection_region (poppler_page,
1988 (PopplerSelectionStyle)style,
1989 (PopplerRectangle *) points);
1990 retval = create_gdk_region_from_poppler_region (region);
1991 g_list_free (region);
1997 pdf_selection_get_selection_map (EvSelection *selection,
1998 EvRenderContext *rc)
2000 PopplerPage *poppler_page;
2001 PopplerRectangle points;
2005 poppler_page = POPPLER_PAGE (rc->page->backend_page);
2009 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2011 region = poppler_page_get_selection_region (poppler_page, 1.0,
2012 POPPLER_SELECTION_GLYPH,
2014 retval = create_gdk_region_from_poppler_region (region);
2015 g_list_free (region);
2021 pdf_selection_iface_init (EvSelectionIface *iface)
2023 iface->render_selection = pdf_selection_render_selection;
2024 iface->get_selected_text = pdf_selection_get_selected_text;
2025 iface->get_selection_region = pdf_selection_get_selection_region;
2026 iface->get_selection_map = pdf_selection_get_selection_map;
2029 /* Page Transitions */
2031 pdf_document_get_page_duration (EvDocumentTransition *trans,
2034 PdfDocument *pdf_document;
2035 PopplerPage *poppler_page;
2036 gdouble duration = -1;
2038 pdf_document = PDF_DOCUMENT (trans);
2039 poppler_page = poppler_document_get_page (pdf_document->document, page);
2043 duration = poppler_page_get_duration (poppler_page);
2044 g_object_unref (poppler_page);
2049 static EvTransitionEffect *
2050 pdf_document_get_effect (EvDocumentTransition *trans,
2053 PdfDocument *pdf_document;
2054 PopplerPage *poppler_page;
2055 PopplerPageTransition *page_transition;
2056 EvTransitionEffect *effect;
2058 pdf_document = PDF_DOCUMENT (trans);
2059 poppler_page = poppler_document_get_page (pdf_document->document, page);
2064 page_transition = poppler_page_get_transition (poppler_page);
2066 if (!page_transition) {
2067 g_object_unref (poppler_page);
2071 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2072 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2073 "alignment", page_transition->alignment,
2074 "direction", page_transition->direction,
2075 "duration", page_transition->duration,
2076 "angle", page_transition->angle,
2077 "scale", page_transition->scale,
2078 "rectangular", page_transition->rectangular,
2081 poppler_page_transition_free (page_transition);
2082 g_object_unref (poppler_page);
2088 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2090 iface->get_page_duration = pdf_document_get_page_duration;
2091 iface->get_effect = pdf_document_get_effect;
2096 pdf_document_get_crop_box (EvDocument *document,
2100 PdfDocument *pdf_document;
2101 PopplerPage *poppler_page;
2102 PopplerRectangle poppler_rect;
2104 pdf_document = PDF_DOCUMENT (document);
2105 poppler_page = poppler_document_get_page (pdf_document->document, page);
2106 poppler_page_get_crop_box (poppler_page, &poppler_rect);
2107 rect->x1 = poppler_rect.x1;
2108 rect->x2 = poppler_rect.x2;
2109 rect->y1 = poppler_rect.y1;
2110 rect->y2 = poppler_rect.y2;
2113 static EvFormField *
2114 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2116 EvFormField *ev_field = NULL;
2119 gboolean is_read_only;
2121 id = poppler_form_field_get_id (poppler_field);
2122 font_size = poppler_form_field_get_font_size (poppler_field);
2123 is_read_only = poppler_form_field_is_read_only (poppler_field);
2125 switch (poppler_form_field_get_field_type (poppler_field)) {
2126 case POPPLER_FORM_FIELD_TEXT: {
2127 EvFormFieldText *field_text;
2128 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2130 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2131 case POPPLER_FORM_TEXT_NORMAL:
2132 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2134 case POPPLER_FORM_TEXT_MULTILINE:
2135 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2137 case POPPLER_FORM_TEXT_FILE_SELECT:
2138 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2142 ev_field = ev_form_field_text_new (id, ev_text_type);
2143 field_text = EV_FORM_FIELD_TEXT (ev_field);
2145 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2146 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2147 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2148 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2149 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2150 field_text->text = poppler_form_field_text_get_text (poppler_field);
2154 case POPPLER_FORM_FIELD_BUTTON: {
2155 EvFormFieldButton *field_button;
2156 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2158 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2159 case POPPLER_FORM_BUTTON_PUSH:
2160 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2162 case POPPLER_FORM_BUTTON_CHECK:
2163 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2165 case POPPLER_FORM_BUTTON_RADIO:
2166 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2170 ev_field = ev_form_field_button_new (id, ev_button_type);
2171 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2173 field_button->state = poppler_form_field_button_get_state (poppler_field);
2176 case POPPLER_FORM_FIELD_CHOICE: {
2177 EvFormFieldChoice *field_choice;
2178 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2180 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2181 case POPPLER_FORM_CHOICE_COMBO:
2182 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2184 case EV_FORM_FIELD_CHOICE_LIST:
2185 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2189 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2190 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2192 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2193 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2194 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2195 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2197 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2198 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2199 if (field_choice->is_editable)
2200 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2203 case POPPLER_FORM_FIELD_SIGNATURE:
2205 ev_field = ev_form_field_signature_new (id);
2207 case POPPLER_FORM_FIELD_UNKNOWN:
2211 ev_field->font_size = font_size;
2212 ev_field->is_read_only = is_read_only;
2218 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2221 PopplerPage *poppler_page;
2222 GList *retval = NULL;
2227 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2229 poppler_page = POPPLER_PAGE (page->backend_page);
2230 fields = poppler_page_get_form_field_mapping (poppler_page);
2231 poppler_page_get_size (poppler_page, NULL, &height);
2233 for (list = fields; list; list = list->next) {
2234 PopplerFormFieldMapping *mapping;
2235 EvFormFieldMapping *field_mapping;
2236 EvFormField *ev_field;
2238 mapping = (PopplerFormFieldMapping *)list->data;
2240 ev_field = ev_form_field_from_poppler_field (mapping->field);
2244 field_mapping = g_new0 (EvFormFieldMapping, 1);
2245 field_mapping->x1 = mapping->area.x1;
2246 field_mapping->x2 = mapping->area.x2;
2247 field_mapping->y1 = height - mapping->area.y2;
2248 field_mapping->y2 = height - mapping->area.y1;
2249 field_mapping->field = ev_field;
2250 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2252 g_object_set_data_full (G_OBJECT (ev_field),
2254 g_object_ref (mapping->field),
2255 (GDestroyNotify) g_object_unref);
2257 retval = g_list_prepend (retval, field_mapping);
2260 poppler_page_free_form_field_mapping (fields);
2262 return g_list_reverse (retval);
2266 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2270 PopplerFormField *poppler_field;
2273 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2277 text = poppler_form_field_text_get_text (poppler_field);
2283 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2287 PopplerFormField *poppler_field;
2289 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2293 poppler_form_field_text_set_text (poppler_field, text);
2294 PDF_DOCUMENT (document)->modified = TRUE;
2298 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2302 PopplerFormField *poppler_field;
2304 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2308 poppler_form_field_button_set_state (poppler_field, state);
2309 PDF_DOCUMENT (document)->modified = TRUE;
2313 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2316 PopplerFormField *poppler_field;
2319 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2323 state = poppler_form_field_button_get_state (poppler_field);
2329 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2333 PopplerFormField *poppler_field;
2336 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2340 text = poppler_form_field_choice_get_item (poppler_field, index);
2346 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2349 PopplerFormField *poppler_field;
2352 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2356 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2362 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2366 PopplerFormField *poppler_field;
2369 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2373 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2379 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2383 PopplerFormField *poppler_field;
2385 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2389 poppler_form_field_choice_select_item (poppler_field, index);
2390 PDF_DOCUMENT (document)->modified = TRUE;
2394 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2398 PopplerFormField *poppler_field;
2400 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2404 poppler_form_field_choice_toggle_item (poppler_field, index);
2405 PDF_DOCUMENT (document)->modified = TRUE;
2409 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2412 PopplerFormField *poppler_field;
2414 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2418 poppler_form_field_choice_unselect_all (poppler_field);
2419 PDF_DOCUMENT (document)->modified = TRUE;
2423 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2427 PopplerFormField *poppler_field;
2429 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2433 poppler_form_field_choice_set_text (poppler_field, text);
2434 PDF_DOCUMENT (document)->modified = TRUE;
2438 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2441 PopplerFormField *poppler_field;
2444 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2448 text = poppler_form_field_choice_get_text (poppler_field);
2454 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2456 iface->get_form_fields = pdf_document_forms_get_form_fields;
2457 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2458 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2459 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2460 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2461 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2462 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2463 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2464 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2465 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2466 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2467 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2468 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2473 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2476 PopplerColor *poppler_color;
2478 poppler_color = poppler_annot_get_color (poppler_annot);
2479 if (poppler_color) {
2480 color->red = poppler_color->red;
2481 color->green = poppler_color->green;
2482 color->blue = poppler_color->blue;
2484 g_free (poppler_color);
2485 } /* TODO: else use a default color */
2488 static EvAnnotation *
2489 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2492 EvAnnotation *ev_annot = NULL;
2493 const gchar *unimplemented_annot = NULL;
2495 switch (poppler_annot_get_annot_type (poppler_annot)) {
2496 case POPPLER_ANNOT_TEXT:
2497 PopplerAnnotText *poppler_text;
2498 EvAnnotationText *ev_annot_text;
2500 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2502 ev_annot = ev_annotation_text_new (page);
2504 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2505 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2508 case POPPLER_ANNOT_LINK:
2509 case POPPLER_ANNOT_WIDGET:
2510 /* Ignore link and widgets annots since they are already handled */
2513 GEnumValue *enum_value;
2515 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2516 poppler_annot_get_annot_type (poppler_annot));
2517 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2521 if (unimplemented_annot) {
2522 g_warning ("Unimplemented annotation: %s, please post a "
2523 "bug report in Evince bugzilla "
2524 "(http://bugzilla.gnome.org) with a testcase.",
2525 unimplemented_annot);
2529 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2530 ev_annot->name = poppler_annot_get_name (poppler_annot);
2531 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2532 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2534 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2535 PopplerAnnotMarkup *markup;
2539 PopplerRectangle poppler_rect;
2541 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2543 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2544 EvRectangle ev_rect;
2547 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2549 ev_rect.x1 = poppler_rect.x1;
2550 ev_rect.x2 = poppler_rect.x2;
2551 ev_rect.y1 = height - poppler_rect.y2;
2552 ev_rect.y2 = height - poppler_rect.y1;
2554 g_object_set (ev_annot, "rectangle", &ev_rect, NULL);
2557 label = poppler_annot_markup_get_label (markup);
2558 opacity = poppler_annot_markup_get_opacity (markup);
2559 is_open = poppler_annot_markup_get_popup_is_open (markup);
2561 g_object_set (ev_annot,
2575 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2578 GList *retval = NULL;
2579 PdfDocument *pdf_document;
2580 PopplerPage *poppler_page;
2586 pdf_document = PDF_DOCUMENT (document_annotations);
2587 poppler_page = POPPLER_PAGE (page->backend_page);
2588 annots = poppler_page_get_annot_mapping (poppler_page);
2589 poppler_page_get_size (poppler_page, NULL, &height);
2591 for (list = annots; list; list = list->next) {
2592 PopplerAnnotMapping *mapping;
2593 EvAnnotationMapping *annot_mapping;
2594 EvAnnotation *ev_annot;
2596 mapping = (PopplerAnnotMapping *)list->data;
2598 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2604 /* Make sure annot has a unique name */
2605 if (!ev_annot->name)
2606 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2608 annot_mapping = g_new0 (EvAnnotationMapping, 1);
2609 annot_mapping->x1 = mapping->area.x1;
2610 annot_mapping->x2 = mapping->area.x2;
2611 annot_mapping->y1 = height - mapping->area.y2;
2612 annot_mapping->y2 = height - mapping->area.y1;
2613 annot_mapping->annotation = ev_annot;
2615 g_object_set_data_full (G_OBJECT (ev_annot),
2617 g_object_ref (mapping->annot),
2618 (GDestroyNotify) g_object_unref);
2620 retval = g_list_prepend (retval, annot_mapping);
2623 poppler_page_free_annot_mapping (annots);
2625 return g_list_reverse (retval);
2629 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2630 EvAnnotation *annot,
2631 const gchar *contents)
2633 PopplerAnnot *poppler_annot;
2635 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2639 poppler_annot_set_contents (poppler_annot, contents);
2640 PDF_DOCUMENT (document)->modified = TRUE;
2644 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2646 iface->get_annotations = pdf_document_annotations_get_annotations;
2647 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2652 pdf_document_layers_has_layers (EvDocumentLayers *document)
2654 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2655 PopplerLayersIter *iter;
2657 iter = poppler_layers_iter_new (pdf_document->document);
2660 poppler_layers_iter_free (iter);
2666 build_layers_tree (PdfDocument *pdf_document,
2667 GtkTreeModel *model,
2668 GtkTreeIter *parent,
2669 PopplerLayersIter *iter)
2672 GtkTreeIter tree_iter;
2673 PopplerLayersIter *child;
2674 PopplerLayer *layer;
2675 EvLayer *ev_layer = NULL;
2680 layer = poppler_layers_iter_get_layer (iter);
2682 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2683 visible = poppler_layer_is_visible (layer);
2684 rb_group = poppler_layer_get_radio_button_group_id (layer);
2685 pdf_document->layers = g_list_append (pdf_document->layers,
2686 g_object_ref (layer));
2687 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2688 poppler_layer_is_parent (layer),
2693 title = poppler_layers_iter_get_title (iter);
2694 markup = g_markup_escape_text (title, -1);
2701 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2702 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2703 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2704 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2705 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2706 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2707 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2708 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2711 g_object_unref (ev_layer);
2714 child = poppler_layers_iter_get_child (iter);
2716 build_layers_tree (pdf_document, model, &tree_iter, child);
2717 poppler_layers_iter_free (child);
2718 } while (poppler_layers_iter_next (iter));
2721 static GtkTreeModel *
2722 pdf_document_layers_get_layers (EvDocumentLayers *document)
2724 GtkTreeModel *model = NULL;
2725 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2726 PopplerLayersIter *iter;
2728 iter = poppler_layers_iter_new (pdf_document->document);
2730 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2731 G_TYPE_STRING, /* TITLE */
2732 G_TYPE_OBJECT, /* LAYER */
2733 G_TYPE_BOOLEAN, /* VISIBLE */
2734 G_TYPE_BOOLEAN, /* ENABLED */
2735 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2736 G_TYPE_INT); /* RBGROUP */
2737 build_layers_tree (pdf_document, model, NULL, iter);
2738 poppler_layers_iter_free (iter);
2744 pdf_document_layers_show_layer (EvDocumentLayers *document,
2747 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2748 guint layer_id = ev_layer_get_id (layer);
2750 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2754 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2757 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2758 guint layer_id = ev_layer_get_id (layer);
2760 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2764 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2767 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2768 guint layer_id = ev_layer_get_id (layer);
2770 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2774 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2776 iface->has_layers = pdf_document_layers_has_layers;
2777 iface->get_layers = pdf_document_layers_get_layers;
2778 iface->show_layer = pdf_document_layers_show_layer;
2779 iface->hide_layer = pdf_document_layers_hide_layer;
2780 iface->layer_is_visible = pdf_document_layers_layer_is_visible;