1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pdfdocument.h: Implementation of EvDocument for PDF
3 * Copyright (C) 2004, Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <poppler-document.h>
27 #include <poppler-page.h>
29 #include <cairo-pdf.h>
34 #include <glib/gi18n-lib.h>
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-mapping.h"
39 #include "ev-document-find.h"
40 #include "ev-document-misc.h"
41 #include "ev-document-links.h"
42 #include "ev-document-images.h"
43 #include "ev-document-fonts.h"
44 #include "ev-document-security.h"
45 #include "ev-document-thumbnails.h"
46 #include "ev-document-transition.h"
47 #include "ev-document-forms.h"
48 #include "ev-document-layers.h"
49 #include "ev-document-print.h"
50 #include "ev-document-annotations.h"
51 #include "ev-document-attachments.h"
52 #include "ev-selection.h"
53 #include "ev-transition-effect.h"
54 #include "ev-attachment.h"
57 #include <libxml/tree.h>
58 #include <libxml/parser.h>
59 #include <libxml/xpath.h>
60 #include <libxml/xpathInternals.h>
62 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
63 #define HAVE_CAIRO_PRINT
67 PdfDocument *document;
76 EvFileExporterFormat format;
86 #ifdef HAVE_CAIRO_PRINT
89 PopplerPSFile *ps_file;
93 struct _PdfDocumentClass
95 GObjectClass parent_class;
100 GObject parent_instance;
102 PopplerDocument *document;
106 PopplerFontInfo *font_info;
107 PopplerFontsIter *fonts_iter;
108 int fonts_scanned_pages;
110 PdfDocumentSearch *search;
111 PdfPrintContext *print_ctx;
116 static void pdf_document_document_iface_init (EvDocumentIface *iface);
117 static void pdf_document_security_iface_init (EvDocumentSecurityIface *iface);
118 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
119 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
120 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
121 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
122 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
123 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
124 #ifdef HAVE_POPPLER_PAGE_RENDER
125 static void pdf_document_document_print_iface_init (EvDocumentPrintIface *iface);
127 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
128 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface);
129 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
130 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
131 static void pdf_selection_iface_init (EvSelectionIface *iface);
132 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
133 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
137 static int pdf_document_get_n_pages (EvDocument *document);
139 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
141 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
142 PopplerAction *action);
143 static void pdf_document_search_free (PdfDocumentSearch *search);
144 static void pdf_print_context_free (PdfPrintContext *ctx);
146 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
148 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
149 pdf_document_security_iface_init);
150 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
151 pdf_document_document_thumbnails_iface_init);
152 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
153 pdf_document_document_links_iface_init);
154 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
155 pdf_document_document_images_iface_init);
156 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
157 pdf_document_document_forms_iface_init);
158 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
159 pdf_document_document_fonts_iface_init);
160 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
161 pdf_document_document_layers_iface_init);
162 #ifdef HAVE_POPPLER_PAGE_RENDER
163 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
164 pdf_document_document_print_iface_init);
166 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
167 pdf_document_document_annotations_iface_init);
168 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
169 pdf_document_document_attachments_iface_init);
170 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
171 pdf_document_find_iface_init);
172 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
173 pdf_document_file_exporter_iface_init);
174 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
175 pdf_selection_iface_init);
176 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
177 pdf_document_page_transition_iface_init);
181 pdf_document_search_free (PdfDocumentSearch *search)
183 PdfDocument *pdf_document = search->document;
187 if (search->idle != 0)
188 g_source_remove (search->idle);
190 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
191 for (i = 0; i < n_pages; i++) {
192 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
193 g_list_free (search->pages[i]);
195 g_free (search->pages);
197 g_free (search->text);
202 pdf_document_dispose (GObject *object)
204 PdfDocument *pdf_document = PDF_DOCUMENT(object);
206 if (pdf_document->print_ctx) {
207 pdf_print_context_free (pdf_document->print_ctx);
208 pdf_document->print_ctx = NULL;
211 if (pdf_document->search) {
212 pdf_document_search_free (pdf_document->search);
213 pdf_document->search = NULL;
216 if (pdf_document->document) {
217 g_object_unref (pdf_document->document);
220 if (pdf_document->font_info) {
221 poppler_font_info_free (pdf_document->font_info);
224 if (pdf_document->fonts_iter) {
225 poppler_fonts_iter_free (pdf_document->fonts_iter);
228 if (pdf_document->layers) {
229 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
230 g_list_free (pdf_document->layers);
233 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
237 pdf_document_class_init (PdfDocumentClass *klass)
239 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
241 g_object_class->dispose = pdf_document_dispose;
245 pdf_document_init (PdfDocument *pdf_document)
247 pdf_document->password = NULL;
251 convert_error (GError *poppler_error,
254 if (poppler_error == NULL)
257 if (poppler_error->domain == POPPLER_ERROR) {
258 /* convert poppler errors into EvDocument errors */
259 gint code = EV_DOCUMENT_ERROR_INVALID;
260 if (poppler_error->code == POPPLER_ERROR_INVALID)
261 code = EV_DOCUMENT_ERROR_INVALID;
262 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
263 code = EV_DOCUMENT_ERROR_ENCRYPTED;
265 g_set_error_literal (error,
268 poppler_error->message);
270 g_error_free (poppler_error);
272 g_propagate_error (error, poppler_error);
279 pdf_document_save (EvDocument *document,
283 PdfDocument *pdf_document = PDF_DOCUMENT (document);
285 GError *poppler_error = NULL;
287 if (pdf_document->modified) {
288 retval = poppler_document_save (pdf_document->document,
289 uri, &poppler_error);
291 retval = poppler_document_save_a_copy (pdf_document->document,
292 uri, &poppler_error);
296 convert_error (poppler_error, error);
302 pdf_document_load (EvDocument *document,
306 GError *poppler_error = NULL;
307 PdfDocument *pdf_document = PDF_DOCUMENT (document);
309 pdf_document->document =
310 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
312 if (pdf_document->document == NULL) {
313 convert_error (poppler_error, error);
321 pdf_document_get_n_pages (EvDocument *document)
323 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
327 pdf_document_get_page (EvDocument *document,
330 PdfDocument *pdf_document = PDF_DOCUMENT (document);
331 PopplerPage *poppler_page;
334 poppler_page = poppler_document_get_page (pdf_document->document, index);
335 page = ev_page_new (index);
336 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
337 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
338 g_object_unref (poppler_page);
344 pdf_document_get_page_size (EvDocument *document,
349 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
351 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
355 pdf_document_get_page_label (EvDocument *document,
360 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
362 g_object_get (G_OBJECT (page->backend_page),
368 static cairo_surface_t *
369 pdf_page_render (PopplerPage *page,
374 cairo_surface_t *surface;
376 #ifdef HAVE_POPPLER_PAGE_RENDER
379 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
381 cr = cairo_create (surface);
383 switch (rc->rotation) {
385 cairo_translate (cr, width, 0);
388 cairo_translate (cr, width, height);
391 cairo_translate (cr, 0, height);
394 cairo_translate (cr, 0, 0);
396 cairo_scale (cr, rc->scale, rc->scale);
397 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
398 poppler_page_render (page, cr);
400 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
401 cairo_set_source_rgb (cr, 1., 1., 1.);
405 #else /* HAVE_POPPLER_PAGE_RENDER */
408 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
412 poppler_page_render_to_pixbuf (page,
418 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
419 g_object_unref (pixbuf);
420 #endif /* HAVE_POPPLER_PAGE_RENDER */
425 static cairo_surface_t *
426 pdf_document_render (EvDocument *document,
429 PdfDocument *pdf_document;
430 PopplerPage *poppler_page;
431 double width_points, height_points;
434 poppler_page = POPPLER_PAGE (rc->page->backend_page);
436 poppler_page_get_size (poppler_page,
437 &width_points, &height_points);
439 if (rc->rotation == 90 || rc->rotation == 270) {
440 width = (int) ((height_points * rc->scale) + 0.5);
441 height = (int) ((width_points * rc->scale) + 0.5);
443 width = (int) ((width_points * rc->scale) + 0.5);
444 height = (int) ((height_points * rc->scale) + 0.5);
447 return pdf_page_render (poppler_page,
451 /* EvDocumentSecurity */
454 pdf_document_has_document_security (EvDocumentSecurity *document_security)
456 /* FIXME: do we really need to have this? */
461 pdf_document_set_password (EvDocumentSecurity *document_security,
462 const char *password)
464 PdfDocument *document = PDF_DOCUMENT (document_security);
466 if (document->password)
467 g_free (document->password);
469 document->password = g_strdup (password);
474 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 */
476 pdf_document_get_format_from_metadata (const char *metadata)
479 xmlXPathContextPtr xpathCtx;
480 xmlXPathObjectPtr xpathObj;
481 xmlChar *part = NULL;
482 xmlChar *conf = NULL;
486 doc = xmlParseMemory (metadata, strlen (metadata));
488 return NULL; /* invalid xml metadata */
490 xpathCtx = xmlXPathNewContext (doc);
491 if (xpathCtx == NULL) {
493 return NULL; /* invalid xpath context */
496 /* add pdf/a namespaces */
497 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
498 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
499 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
501 /* reads pdf/a part */
502 /* first syntax: child node */
503 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
504 if (xpathObj != NULL) {
505 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
506 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
508 xmlXPathFreeObject (xpathObj);
511 /* second syntax: attribute */
512 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
513 if (xpathObj != NULL) {
514 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
515 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
517 xmlXPathFreeObject (xpathObj);
521 /* reads pdf/a conformance */
522 /* first syntax: child node */
523 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
524 if (xpathObj != NULL) {
525 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
526 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
528 xmlXPathFreeObject (xpathObj);
531 /* second syntax: attribute */
532 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
533 if (xpathObj != NULL) {
534 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
535 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
537 xmlXPathFreeObject (xpathObj);
541 if (part != NULL && conf != NULL) {
542 /* makes conf lowercase */
543 for (i = 0; conf[i]; i++)
544 conf[i] = g_ascii_tolower (conf[i]);
547 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
553 xmlXPathFreeContext (xpathCtx);
560 static EvDocumentInfo *
561 pdf_document_get_info (EvDocument *document)
563 EvDocumentInfo *info;
564 PopplerPageLayout layout;
565 PopplerPageMode mode;
566 PopplerViewerPreferences view_prefs;
567 PopplerPermissions permissions;
572 info = g_new0 (EvDocumentInfo, 1);
574 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
575 EV_DOCUMENT_INFO_FORMAT |
576 EV_DOCUMENT_INFO_AUTHOR |
577 EV_DOCUMENT_INFO_SUBJECT |
578 EV_DOCUMENT_INFO_KEYWORDS |
579 EV_DOCUMENT_INFO_LAYOUT |
580 EV_DOCUMENT_INFO_START_MODE |
581 EV_DOCUMENT_INFO_PERMISSIONS |
582 EV_DOCUMENT_INFO_UI_HINTS |
583 EV_DOCUMENT_INFO_CREATOR |
584 EV_DOCUMENT_INFO_PRODUCER |
585 EV_DOCUMENT_INFO_CREATION_DATE |
586 EV_DOCUMENT_INFO_MOD_DATE |
587 EV_DOCUMENT_INFO_LINEARIZED |
588 EV_DOCUMENT_INFO_N_PAGES |
589 EV_DOCUMENT_INFO_SECURITY |
590 EV_DOCUMENT_INFO_PAPER_SIZE;
592 g_object_get (PDF_DOCUMENT (document)->document,
593 "title", &(info->title),
594 "format", &(info->format),
595 "author", &(info->author),
596 "subject", &(info->subject),
597 "keywords", &(info->keywords),
599 "page-layout", &layout,
600 "viewer-preferences", &view_prefs,
601 "permissions", &permissions,
602 "creator", &(info->creator),
603 "producer", &(info->producer),
604 "creation-date", &(info->creation_date),
605 "mod-date", &(info->modified_date),
606 "linearized", &(info->linearized),
607 "metadata", &metadata,
610 if (metadata != NULL) {
611 fmt = pdf_document_get_format_from_metadata(metadata);
613 g_free (info->format);
619 info->n_pages = ev_document_get_n_pages (document);
621 if (info->n_pages > 0) {
622 page = ev_document_get_page (document, 0);
623 ev_document_get_page_size (document, page,
624 &(info->paper_width),
625 &(info->paper_height));
626 g_object_unref (page);
630 info->paper_width = info->paper_width / 72.0f * 25.4f;
631 info->paper_height = info->paper_height / 72.0f * 25.4f;
635 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
636 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
638 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
639 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
641 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
642 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
644 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
645 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
646 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
647 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
649 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
650 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
657 case POPPLER_PAGE_MODE_NONE:
658 info->mode = EV_DOCUMENT_MODE_NONE;
660 case POPPLER_PAGE_MODE_USE_THUMBS:
661 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
663 case POPPLER_PAGE_MODE_USE_OC:
664 info->mode = EV_DOCUMENT_MODE_USE_OC;
666 case POPPLER_PAGE_MODE_FULL_SCREEN:
667 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
669 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
670 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
676 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
677 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
679 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
680 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
682 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
683 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
685 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
686 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
688 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
689 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
691 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
692 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
694 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
695 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
698 info->permissions = 0;
699 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
700 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
702 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
703 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
705 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
706 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
708 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
709 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
712 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
713 /* translators: this is the document security state */
714 info->security = g_strdup (_("Yes"));
716 /* translators: this is the document security state */
717 info->security = g_strdup (_("No"));
724 pdf_document_document_iface_init (EvDocumentIface *iface)
726 iface->save = pdf_document_save;
727 iface->load = pdf_document_load;
728 iface->get_n_pages = pdf_document_get_n_pages;
729 iface->get_page = pdf_document_get_page;
730 iface->get_page_size = pdf_document_get_page_size;
731 iface->get_page_label = pdf_document_get_page_label;
732 iface->render = pdf_document_render;
733 iface->get_info = pdf_document_get_info;
737 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
739 iface->has_document_security = pdf_document_has_document_security;
740 iface->set_password = pdf_document_set_password;
744 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
746 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
749 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
751 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
755 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
758 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
761 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
763 if (pdf_document->font_info == NULL) {
764 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
767 if (pdf_document->fonts_iter) {
768 poppler_fonts_iter_free (pdf_document->fonts_iter);
771 pdf_document->fonts_scanned_pages += n_pages;
773 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
774 &pdf_document->fonts_iter);
776 pdf_document->fonts_scanned_pages = 0;
777 poppler_font_info_free (pdf_document->font_info);
778 pdf_document->font_info = NULL;
785 font_type_to_string (PopplerFontType type)
788 case POPPLER_FONT_TYPE_TYPE1:
790 case POPPLER_FONT_TYPE_TYPE1C:
792 case POPPLER_FONT_TYPE_TYPE3:
794 case POPPLER_FONT_TYPE_TRUETYPE:
795 return _("TrueType");
796 case POPPLER_FONT_TYPE_CID_TYPE0:
797 return _("Type 1 (CID)");
798 case POPPLER_FONT_TYPE_CID_TYPE0C:
799 return _("Type 1C (CID)");
800 case POPPLER_FONT_TYPE_CID_TYPE2:
801 return _("TrueType (CID)");
803 return _("Unknown font type");
808 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
811 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
812 PopplerFontsIter *iter = pdf_document->fonts_iter;
814 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
820 GtkTreeIter list_iter;
823 const char *embedded;
826 name = poppler_fonts_iter_get_name (iter);
832 type = font_type_to_string (
833 poppler_fonts_iter_get_font_type (iter));
835 if (poppler_fonts_iter_is_embedded (iter)) {
836 if (poppler_fonts_iter_is_subset (iter))
837 embedded = _("Embedded subset");
839 embedded = _("Embedded");
841 embedded = _("Not embedded");
844 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
846 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
847 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
848 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
849 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
853 } while (poppler_fonts_iter_next (iter));
857 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
859 iface->fill_model = pdf_document_fonts_fill_model;
860 iface->scan = pdf_document_fonts_scan;
861 iface->get_progress = pdf_document_fonts_get_progress;
865 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
867 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
868 PopplerIndexIter *iter;
870 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
872 iter = poppler_index_iter_new (pdf_document->document);
875 poppler_index_iter_free (iter);
881 ev_link_dest_from_dest (PdfDocument *pdf_document,
884 EvLinkDest *ev_dest = NULL;
885 const char *unimplemented_dest = NULL;
887 g_assert (dest != NULL);
889 switch (dest->type) {
890 case POPPLER_DEST_XYZ: {
891 PopplerPage *poppler_page;
894 poppler_page = poppler_document_get_page (pdf_document->document,
895 MAX (0, dest->page_num - 1));
896 poppler_page_get_size (poppler_page, NULL, &height);
897 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
899 height - MIN (height, dest->top),
904 g_object_unref (poppler_page);
907 case POPPLER_DEST_FITB:
908 case POPPLER_DEST_FIT:
909 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
911 case POPPLER_DEST_FITBH:
912 case POPPLER_DEST_FITH: {
913 PopplerPage *poppler_page;
916 poppler_page = poppler_document_get_page (pdf_document->document,
917 MAX (0, dest->page_num - 1));
918 poppler_page_get_size (poppler_page, NULL, &height);
919 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
920 height - MIN (height, dest->top),
922 g_object_unref (poppler_page);
925 case POPPLER_DEST_FITBV:
926 case POPPLER_DEST_FITV:
927 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
931 case POPPLER_DEST_FITR: {
932 PopplerPage *poppler_page;
935 poppler_page = poppler_document_get_page (pdf_document->document,
936 MAX (0, dest->page_num - 1));
937 poppler_page_get_size (poppler_page, NULL, &height);
938 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
940 height - MIN (height, dest->bottom),
942 height - MIN (height, dest->top));
943 g_object_unref (poppler_page);
946 case POPPLER_DEST_NAMED:
947 ev_dest = ev_link_dest_new_named (dest->named_dest);
949 case POPPLER_DEST_UNKNOWN:
950 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
954 if (unimplemented_dest) {
955 g_warning ("Unimplemented destination: %s, please post a "
956 "bug report in Evince bugzilla "
957 "(http://bugzilla.gnome.org) with a testcase.",
962 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
968 ev_link_from_action (PdfDocument *pdf_document,
969 PopplerAction *action)
972 EvLinkAction *ev_action = NULL;
973 const char *unimplemented_action = NULL;
975 switch (action->type) {
976 case POPPLER_ACTION_NONE:
978 case POPPLER_ACTION_GOTO_DEST: {
981 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
982 ev_action = ev_link_action_new_dest (dest);
985 case POPPLER_ACTION_GOTO_REMOTE: {
988 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
989 ev_action = ev_link_action_new_remote (dest,
990 action->goto_remote.file_name);
994 case POPPLER_ACTION_LAUNCH:
995 ev_action = ev_link_action_new_launch (action->launch.file_name,
996 action->launch.params);
998 case POPPLER_ACTION_URI:
999 ev_action = ev_link_action_new_external_uri (action->uri.uri);
1001 case POPPLER_ACTION_NAMED:
1002 ev_action = ev_link_action_new_named (action->named.named_dest);
1004 case POPPLER_ACTION_MOVIE:
1005 unimplemented_action = "POPPLER_ACTION_MOVIE";
1007 case POPPLER_ACTION_UNKNOWN:
1008 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1011 if (unimplemented_action) {
1012 g_warning ("Unimplemented action: %s, please post a bug report "
1013 "in Evince bugzilla (http://bugzilla.gnome.org) "
1014 "with a testcase.", unimplemented_action);
1017 link = ev_link_new (action->any.title, ev_action);
1023 build_tree (PdfDocument *pdf_document,
1024 GtkTreeModel *model,
1025 GtkTreeIter *parent,
1026 PopplerIndexIter *iter)
1030 GtkTreeIter tree_iter;
1031 PopplerIndexIter *child;
1032 PopplerAction *action;
1033 EvLink *link = NULL;
1037 action = poppler_index_iter_get_action (iter);
1038 expand = poppler_index_iter_is_open (iter);
1043 switch (action->type) {
1044 case POPPLER_ACTION_GOTO_DEST: {
1045 /* For bookmarks, solve named destinations */
1046 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1048 EvLinkDest *ev_dest = NULL;
1049 EvLinkAction *ev_action;
1051 dest = poppler_document_find_dest (pdf_document->document,
1052 action->goto_dest.dest->named_dest);
1054 link = ev_link_from_action (pdf_document, action);
1058 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1059 poppler_dest_free (dest);
1061 ev_action = ev_link_action_new_dest (ev_dest);
1062 link = ev_link_new (action->any.title, ev_action);
1064 link = ev_link_from_action (pdf_document, action);
1069 link = ev_link_from_action (pdf_document, action);
1073 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1074 poppler_action_free (action);
1076 g_object_unref (link);
1081 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1082 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1084 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1085 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1086 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1087 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1090 g_free (title_markup);
1091 g_object_unref (link);
1093 child = poppler_index_iter_get_child (iter);
1095 build_tree (pdf_document, model, &tree_iter, child);
1096 poppler_index_iter_free (child);
1097 poppler_action_free (action);
1099 } while (poppler_index_iter_next (iter));
1102 static GtkTreeModel *
1103 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1105 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1106 GtkTreeModel *model = NULL;
1107 PopplerIndexIter *iter;
1109 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1111 iter = poppler_index_iter_new (pdf_document->document);
1112 /* Create the model if we have items*/
1114 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1119 build_tree (pdf_document, model, NULL, iter);
1120 poppler_index_iter_free (iter);
1127 pdf_document_links_get_links (EvDocumentLinks *document_links,
1130 PdfDocument *pdf_document;
1131 PopplerPage *poppler_page;
1132 GList *retval = NULL;
1133 GList *mapping_list;
1137 pdf_document = PDF_DOCUMENT (document_links);
1138 poppler_page = poppler_document_get_page (pdf_document->document,
1140 mapping_list = poppler_page_get_link_mapping (poppler_page);
1141 poppler_page_get_size (poppler_page, NULL, &height);
1143 for (list = mapping_list; list; list = list->next) {
1144 PopplerLinkMapping *link_mapping;
1145 EvMapping *ev_link_mapping;
1147 link_mapping = (PopplerLinkMapping *)list->data;
1148 ev_link_mapping = g_new (EvMapping, 1);
1149 ev_link_mapping->data = ev_link_from_action (pdf_document,
1150 link_mapping->action);
1151 ev_link_mapping->area.x1 = link_mapping->area.x1;
1152 ev_link_mapping->area.x2 = link_mapping->area.x2;
1153 /* Invert this for X-style coordinates */
1154 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1155 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1157 retval = g_list_prepend (retval, ev_link_mapping);
1160 poppler_page_free_link_mapping (mapping_list);
1161 g_object_unref (poppler_page);
1163 return g_list_reverse (retval);
1167 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1168 const gchar *link_name)
1170 PdfDocument *pdf_document;
1172 EvLinkDest *ev_dest = NULL;
1174 pdf_document = PDF_DOCUMENT (document_links);
1175 dest = poppler_document_find_dest (pdf_document->document,
1178 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1179 poppler_dest_free (dest);
1186 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1188 iface->has_document_links = pdf_document_links_has_document_links;
1189 iface->get_links_model = pdf_document_links_get_links_model;
1190 iface->get_links = pdf_document_links_get_links;
1191 iface->find_link_dest = pdf_document_links_find_link_dest;
1195 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1198 GList *retval = NULL;
1199 PdfDocument *pdf_document;
1200 PopplerPage *poppler_page;
1201 GList *mapping_list;
1204 pdf_document = PDF_DOCUMENT (document_images);
1205 poppler_page = poppler_document_get_page (pdf_document->document, page);
1206 mapping_list = poppler_page_get_image_mapping (poppler_page);
1208 for (list = mapping_list; list; list = list->next) {
1209 PopplerImageMapping *image_mapping;
1210 EvMapping *ev_image_mapping;
1212 image_mapping = (PopplerImageMapping *)list->data;
1214 ev_image_mapping = g_new (EvMapping, 1);
1216 ev_image_mapping->data = ev_image_new (page, image_mapping->image_id);
1217 ev_image_mapping->area.x1 = image_mapping->area.x1;
1218 ev_image_mapping->area.y1 = image_mapping->area.y1;
1219 ev_image_mapping->area.x2 = image_mapping->area.x2;
1220 ev_image_mapping->area.y2 = image_mapping->area.y2;
1222 retval = g_list_prepend (retval, ev_image_mapping);
1225 poppler_page_free_image_mapping (mapping_list);
1226 g_object_unref (poppler_page);
1228 return g_list_reverse (retval);
1232 pdf_document_images_get_image (EvDocumentImages *document_images,
1235 GdkPixbuf *retval = NULL;
1236 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1237 PdfDocument *pdf_document;
1238 PopplerPage *poppler_page;
1239 cairo_surface_t *surface;
1241 pdf_document = PDF_DOCUMENT (document_images);
1242 poppler_page = poppler_document_get_page (pdf_document->document,
1243 ev_image_get_page (image));
1245 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1247 retval = ev_document_misc_pixbuf_from_surface (surface);
1248 cairo_surface_destroy (surface);
1251 g_object_unref (poppler_page);
1257 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1259 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1260 iface->get_image = pdf_document_images_get_image;
1264 make_thumbnail_for_page (PopplerPage *poppler_page,
1265 EvRenderContext *rc,
1271 #ifdef POPPLER_WITH_GDK
1272 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1274 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1276 ev_document_fc_mutex_lock ();
1277 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1279 rc->scale, rc->rotation, pixbuf);
1280 ev_document_fc_mutex_unlock ();
1282 cairo_surface_t *surface;
1284 ev_document_fc_mutex_lock ();
1285 surface = pdf_page_render (poppler_page, width, height, rc);
1286 ev_document_fc_mutex_unlock ();
1288 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1289 cairo_surface_destroy (surface);
1290 #endif /* POPPLER_WITH_GDK */
1296 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1297 EvRenderContext *rc,
1300 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1301 PopplerPage *poppler_page;
1302 GdkPixbuf *pixbuf = NULL;
1303 GdkPixbuf *border_pixbuf;
1306 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1308 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1309 rc, &width, &height);
1311 #ifdef POPPLER_WITH_GDK
1312 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1314 cairo_surface_t *surface;
1316 surface = poppler_page_get_thumbnail (poppler_page);
1318 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1319 cairo_surface_destroy (surface);
1321 #endif /* POPPLER_WITH_GDK */
1323 if (pixbuf != NULL) {
1324 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1325 gdk_pixbuf_get_height (pixbuf) :
1326 gdk_pixbuf_get_width (pixbuf);
1328 if (thumb_width == width) {
1329 GdkPixbuf *rotated_pixbuf;
1331 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1332 (GdkPixbufRotation) (360 - rc->rotation));
1333 g_object_unref (pixbuf);
1334 pixbuf = rotated_pixbuf;
1336 /* The provided thumbnail has a different size */
1337 g_object_unref (pixbuf);
1338 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1341 /* There is no provided thumbnail. We need to make one. */
1342 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1345 if (border && pixbuf) {
1346 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1347 g_object_unref (pixbuf);
1348 pixbuf = border_pixbuf;
1355 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1356 EvRenderContext *rc,
1360 double page_width, page_height;
1362 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1363 &page_width, &page_height);
1365 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1366 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1368 if (rc->rotation == 90 || rc->rotation == 270) {
1378 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1380 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1381 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1386 pdf_document_find_find_text (EvDocumentFind *document_find,
1389 gboolean case_sensitive)
1392 PopplerPage *poppler_page;
1395 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1396 g_return_val_if_fail (text != NULL, NULL);
1398 poppler_page = POPPLER_PAGE (page->backend_page);
1400 matches = poppler_page_find_text (poppler_page, text);
1404 poppler_page_get_size (poppler_page, NULL, &height);
1405 for (l = matches; l && l->data; l = g_list_next (l)) {
1406 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1410 rect->y1 = height - rect->y2;
1411 rect->y2 = height - tmp;
1418 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1420 iface->find_text = pdf_document_find_find_text;
1424 pdf_print_context_free (PdfPrintContext *ctx)
1429 #ifdef HAVE_CAIRO_PRINT
1431 cairo_destroy (ctx->cr);
1436 poppler_ps_file_free (ctx->ps_file);
1437 ctx->ps_file = NULL;
1444 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1445 EvFileExporterContext *fc)
1447 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1448 PdfPrintContext *ctx;
1449 #ifdef HAVE_CAIRO_PRINT
1450 gdouble width, height;
1451 cairo_surface_t *surface = NULL;
1454 if (pdf_document->print_ctx)
1455 pdf_print_context_free (pdf_document->print_ctx);
1456 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1457 ctx = pdf_document->print_ctx;
1458 ctx->format = fc->format;
1460 #ifdef HAVE_CAIRO_PRINT
1461 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1463 ctx->paper_width = fc->paper_width;
1464 ctx->paper_height = fc->paper_height;
1466 switch (fc->pages_per_sheet) {
1494 ctx->pages_printed = 0;
1496 switch (fc->format) {
1497 case EV_FILE_FORMAT_PS:
1498 #ifdef HAVE_CAIRO_PS
1499 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1502 case EV_FILE_FORMAT_PDF:
1503 #ifdef HAVE_CAIRO_PDF
1504 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1508 g_assert_not_reached ();
1511 ctx->cr = cairo_create (surface);
1512 cairo_surface_destroy (surface);
1514 #else /* HAVE_CAIRO_PRINT */
1515 if (ctx->format == EV_FILE_FORMAT_PS) {
1516 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1517 fc->filename, fc->first_page,
1518 fc->last_page - fc->first_page + 1);
1519 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1520 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1522 #endif /* HAVE_CAIRO_PRINT */
1526 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1528 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1529 PdfPrintContext *ctx = pdf_document->print_ctx;
1531 g_return_if_fail (pdf_document->print_ctx != NULL);
1533 ctx->pages_printed = 0;
1535 #ifdef HAVE_CAIRO_PRINT
1536 if (ctx->paper_width > ctx->paper_height) {
1537 if (ctx->format == EV_FILE_FORMAT_PS) {
1538 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1541 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1542 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1547 #endif /* HAVE_CAIRO_PRINT */
1551 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1552 EvRenderContext *rc)
1554 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1555 PdfPrintContext *ctx = pdf_document->print_ctx;
1556 PopplerPage *poppler_page;
1557 #ifdef HAVE_CAIRO_PRINT
1558 gdouble page_width, page_height;
1561 gdouble width, height;
1562 gdouble pwidth, pheight;
1563 gdouble xscale, yscale;
1566 g_return_if_fail (pdf_document->print_ctx != NULL);
1568 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1570 #ifdef HAVE_CAIRO_PRINT
1571 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1572 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1573 poppler_page_get_size (poppler_page, &page_width, &page_height);
1575 if (page_width > page_height && page_width > ctx->paper_width) {
1581 /* Use always portrait mode and rotate when necessary */
1582 if (ctx->paper_width > ctx->paper_height) {
1583 width = ctx->paper_height;
1584 height = ctx->paper_width;
1587 width = ctx->paper_width;
1588 height = ctx->paper_height;
1591 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1604 page_width = page_height;
1608 pwidth = width / ctx->pages_x;
1609 pheight = height / ctx->pages_y;
1611 if ((page_width > pwidth || page_height > pheight) ||
1612 (page_width < pwidth && page_height < pheight)) {
1613 xscale = pwidth / page_width;
1614 yscale = pheight / page_height;
1616 if (yscale < xscale) {
1623 xscale = yscale = 1;
1628 cairo_save (ctx->cr);
1630 cairo_matrix_t matrix;
1632 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1633 cairo_matrix_init (&matrix,
1637 cairo_transform (ctx->cr, &matrix);
1640 cairo_translate (ctx->cr,
1641 x * (rotate ? pheight : pwidth),
1642 y * (rotate ? pwidth : pheight));
1643 cairo_scale (ctx->cr, xscale, yscale);
1645 poppler_page_render_for_printing (poppler_page, ctx->cr);
1647 ctx->pages_printed++;
1649 cairo_restore (ctx->cr);
1650 #else /* HAVE_CAIRO_PRINT */
1651 if (ctx->format == EV_FILE_FORMAT_PS)
1652 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1653 #endif /* HAVE_CAIRO_PRINT */
1657 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1659 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1660 PdfPrintContext *ctx = pdf_document->print_ctx;
1662 g_return_if_fail (pdf_document->print_ctx != NULL);
1664 #ifdef HAVE_CAIRO_PRINT
1665 cairo_show_page (ctx->cr);
1670 pdf_document_file_exporter_end (EvFileExporter *exporter)
1672 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1674 pdf_print_context_free (pdf_document->print_ctx);
1675 pdf_document->print_ctx = NULL;
1678 static EvFileExporterCapabilities
1679 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1681 return (EvFileExporterCapabilities) (
1682 EV_FILE_EXPORTER_CAN_PAGE_SET |
1683 EV_FILE_EXPORTER_CAN_COPIES |
1684 EV_FILE_EXPORTER_CAN_COLLATE |
1685 EV_FILE_EXPORTER_CAN_REVERSE |
1686 EV_FILE_EXPORTER_CAN_SCALE |
1687 #ifdef HAVE_CAIRO_PRINT
1688 #ifdef HAVE_POPPLER_PAGE_RENDER
1689 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1693 #ifdef HAVE_CAIRO_PDF
1694 #ifdef HAVE_POPPLER_PAGE_RENDER
1695 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1698 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1702 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1704 iface->begin = pdf_document_file_exporter_begin;
1705 iface->begin_page = pdf_document_file_exporter_begin_page;
1706 iface->do_page = pdf_document_file_exporter_do_page;
1707 iface->end_page = pdf_document_file_exporter_end_page;
1708 iface->end = pdf_document_file_exporter_end;
1709 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1712 #ifdef HAVE_POPPLER_PAGE_RENDER
1713 /* EvDocumentPrint */
1715 pdf_document_print_print_page (EvDocumentPrint *document,
1719 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1721 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1725 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1727 iface->print_page = pdf_document_print_print_page;
1729 #endif /* HAVE_POPPLER_PAGE_RENDER */
1732 pdf_selection_render_selection (EvSelection *selection,
1733 EvRenderContext *rc,
1734 cairo_surface_t **surface,
1735 EvRectangle *points,
1736 EvRectangle *old_points,
1737 EvSelectionStyle style,
1741 PopplerPage *poppler_page;
1742 double width_points, height_points;
1745 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1747 poppler_page_get_size (poppler_page,
1748 &width_points, &height_points);
1749 width = (int) ((width_points * rc->scale) + 0.5);
1750 height = (int) ((height_points * rc->scale) + 0.5);
1752 #ifdef HAVE_POPPLER_PAGE_RENDER
1754 PopplerColor text_color, base_color;
1756 text_color.red = text->red;
1757 text_color.green = text->green;
1758 text_color.blue = text->blue;
1760 base_color.red = base->red;
1761 base_color.green = base->green;
1762 base_color.blue = base->blue;
1764 if (*surface == NULL) {
1765 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1770 cr = cairo_create (*surface);
1771 cairo_scale (cr, rc->scale, rc->scale);
1772 cairo_surface_set_device_offset (*surface, 0, 0);
1773 memset (cairo_image_surface_get_data (*surface), 0x00,
1774 cairo_image_surface_get_height (*surface) *
1775 cairo_image_surface_get_stride (*surface));
1776 poppler_page_render_selection (poppler_page,
1778 (PopplerRectangle *)points,
1779 (PopplerRectangle *)old_points,
1780 (PopplerSelectionStyle)style,
1784 #else /* HAVE_POPPLER_PAGE_RENDER */
1787 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1791 poppler_page_render_selection_to_pixbuf (poppler_page,
1792 rc->scale, rc->rotation, pixbuf,
1793 (PopplerRectangle *)points,
1794 (PopplerRectangle *)old_points,
1795 (PopplerSelectionStyle)style,
1799 cairo_surface_destroy (*surface);
1800 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1801 g_object_unref (pixbuf);
1802 #endif /* HAVE_POPPLER_PAGE_RENDER */
1806 pdf_selection_get_selected_text (EvSelection *selection,
1807 EvRenderContext *rc,
1808 EvSelectionStyle style,
1809 EvRectangle *points)
1811 PopplerPage *poppler_page;
1816 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1818 poppler_page_get_size (poppler_page, NULL, &height);
1820 r.y1 = height - points->y2;
1822 r.y2 = height - points->y1;
1824 retval = poppler_page_get_text (poppler_page,
1825 (PopplerSelectionStyle)style,
1832 create_gdk_region_from_poppler_region (GList *region)
1837 retval = gdk_region_new ();
1839 for (l = region; l; l = g_list_next (l)) {
1840 PopplerRectangle *rectangle;
1843 rectangle = (PopplerRectangle *)l->data;
1845 rect.x = (gint) rectangle->x1;
1846 rect.y = (gint) rectangle->y1;
1847 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1848 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1849 gdk_region_union_with_rect (retval, &rect);
1851 poppler_rectangle_free (rectangle);
1858 pdf_selection_get_selection_region (EvSelection *selection,
1859 EvRenderContext *rc,
1860 EvSelectionStyle style,
1861 EvRectangle *points)
1863 PopplerPage *poppler_page;
1867 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1869 region = poppler_page_get_selection_region (poppler_page,
1871 (PopplerSelectionStyle)style,
1872 (PopplerRectangle *) points);
1873 retval = create_gdk_region_from_poppler_region (region);
1874 g_list_free (region);
1880 pdf_selection_get_selection_map (EvSelection *selection,
1881 EvRenderContext *rc)
1883 PopplerPage *poppler_page;
1884 PopplerRectangle points;
1888 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1892 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1894 region = poppler_page_get_selection_region (poppler_page, 1.0,
1895 POPPLER_SELECTION_GLYPH,
1897 retval = create_gdk_region_from_poppler_region (region);
1898 g_list_free (region);
1904 pdf_selection_iface_init (EvSelectionIface *iface)
1906 iface->render_selection = pdf_selection_render_selection;
1907 iface->get_selected_text = pdf_selection_get_selected_text;
1908 iface->get_selection_region = pdf_selection_get_selection_region;
1909 iface->get_selection_map = pdf_selection_get_selection_map;
1912 /* Page Transitions */
1914 pdf_document_get_page_duration (EvDocumentTransition *trans,
1917 PdfDocument *pdf_document;
1918 PopplerPage *poppler_page;
1919 gdouble duration = -1;
1921 pdf_document = PDF_DOCUMENT (trans);
1922 poppler_page = poppler_document_get_page (pdf_document->document, page);
1926 duration = poppler_page_get_duration (poppler_page);
1927 g_object_unref (poppler_page);
1932 static EvTransitionEffect *
1933 pdf_document_get_effect (EvDocumentTransition *trans,
1936 PdfDocument *pdf_document;
1937 PopplerPage *poppler_page;
1938 PopplerPageTransition *page_transition;
1939 EvTransitionEffect *effect;
1941 pdf_document = PDF_DOCUMENT (trans);
1942 poppler_page = poppler_document_get_page (pdf_document->document, page);
1947 page_transition = poppler_page_get_transition (poppler_page);
1949 if (!page_transition) {
1950 g_object_unref (poppler_page);
1954 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1955 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1956 "alignment", page_transition->alignment,
1957 "direction", page_transition->direction,
1958 "duration", page_transition->duration,
1959 "angle", page_transition->angle,
1960 "scale", page_transition->scale,
1961 "rectangular", page_transition->rectangular,
1964 poppler_page_transition_free (page_transition);
1965 g_object_unref (poppler_page);
1971 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1973 iface->get_page_duration = pdf_document_get_page_duration;
1974 iface->get_effect = pdf_document_get_effect;
1979 pdf_document_get_crop_box (EvDocument *document,
1983 PdfDocument *pdf_document;
1984 PopplerPage *poppler_page;
1985 PopplerRectangle poppler_rect;
1987 pdf_document = PDF_DOCUMENT (document);
1988 poppler_page = poppler_document_get_page (pdf_document->document, page);
1989 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1990 rect->x1 = poppler_rect.x1;
1991 rect->x2 = poppler_rect.x2;
1992 rect->y1 = poppler_rect.y1;
1993 rect->y2 = poppler_rect.y2;
1996 static EvFormField *
1997 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1999 EvFormField *ev_field = NULL;
2002 gboolean is_read_only;
2004 id = poppler_form_field_get_id (poppler_field);
2005 font_size = poppler_form_field_get_font_size (poppler_field);
2006 is_read_only = poppler_form_field_is_read_only (poppler_field);
2008 switch (poppler_form_field_get_field_type (poppler_field)) {
2009 case POPPLER_FORM_FIELD_TEXT: {
2010 EvFormFieldText *field_text;
2011 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2013 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2014 case POPPLER_FORM_TEXT_NORMAL:
2015 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2017 case POPPLER_FORM_TEXT_MULTILINE:
2018 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2020 case POPPLER_FORM_TEXT_FILE_SELECT:
2021 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2025 ev_field = ev_form_field_text_new (id, ev_text_type);
2026 field_text = EV_FORM_FIELD_TEXT (ev_field);
2028 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2029 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2030 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2031 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2032 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2033 field_text->text = poppler_form_field_text_get_text (poppler_field);
2037 case POPPLER_FORM_FIELD_BUTTON: {
2038 EvFormFieldButton *field_button;
2039 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2041 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2042 case POPPLER_FORM_BUTTON_PUSH:
2043 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2045 case POPPLER_FORM_BUTTON_CHECK:
2046 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2048 case POPPLER_FORM_BUTTON_RADIO:
2049 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2053 ev_field = ev_form_field_button_new (id, ev_button_type);
2054 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2056 field_button->state = poppler_form_field_button_get_state (poppler_field);
2059 case POPPLER_FORM_FIELD_CHOICE: {
2060 EvFormFieldChoice *field_choice;
2061 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2063 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2064 case POPPLER_FORM_CHOICE_COMBO:
2065 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2067 case EV_FORM_FIELD_CHOICE_LIST:
2068 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2072 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2073 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2075 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2076 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2077 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2078 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2080 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2081 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2082 if (field_choice->is_editable)
2083 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2086 case POPPLER_FORM_FIELD_SIGNATURE:
2088 ev_field = ev_form_field_signature_new (id);
2090 case POPPLER_FORM_FIELD_UNKNOWN:
2094 ev_field->font_size = font_size;
2095 ev_field->is_read_only = is_read_only;
2101 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2104 PopplerPage *poppler_page;
2105 GList *retval = NULL;
2110 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2112 poppler_page = POPPLER_PAGE (page->backend_page);
2113 fields = poppler_page_get_form_field_mapping (poppler_page);
2114 poppler_page_get_size (poppler_page, NULL, &height);
2116 for (list = fields; list; list = list->next) {
2117 PopplerFormFieldMapping *mapping;
2118 EvMapping *field_mapping;
2119 EvFormField *ev_field;
2121 mapping = (PopplerFormFieldMapping *)list->data;
2123 ev_field = ev_form_field_from_poppler_field (mapping->field);
2127 field_mapping = g_new0 (EvMapping, 1);
2128 field_mapping->area.x1 = mapping->area.x1;
2129 field_mapping->area.x2 = mapping->area.x2;
2130 field_mapping->area.y1 = height - mapping->area.y2;
2131 field_mapping->area.y2 = height - mapping->area.y1;
2132 field_mapping->data = ev_field;
2133 ev_field->page = EV_PAGE (g_object_ref (page));
2135 g_object_set_data_full (G_OBJECT (ev_field),
2137 g_object_ref (mapping->field),
2138 (GDestroyNotify) g_object_unref);
2140 retval = g_list_prepend (retval, field_mapping);
2143 poppler_page_free_form_field_mapping (fields);
2145 return g_list_reverse (retval);
2149 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2153 PopplerFormField *poppler_field;
2156 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2160 text = poppler_form_field_text_get_text (poppler_field);
2166 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2170 PopplerFormField *poppler_field;
2172 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2176 poppler_form_field_text_set_text (poppler_field, text);
2177 PDF_DOCUMENT (document)->modified = TRUE;
2181 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2185 PopplerFormField *poppler_field;
2187 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2191 poppler_form_field_button_set_state (poppler_field, state);
2192 PDF_DOCUMENT (document)->modified = TRUE;
2196 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2199 PopplerFormField *poppler_field;
2202 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2206 state = poppler_form_field_button_get_state (poppler_field);
2212 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2216 PopplerFormField *poppler_field;
2219 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2223 text = poppler_form_field_choice_get_item (poppler_field, index);
2229 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2232 PopplerFormField *poppler_field;
2235 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2239 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2245 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2249 PopplerFormField *poppler_field;
2252 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2256 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2262 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2266 PopplerFormField *poppler_field;
2268 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2272 poppler_form_field_choice_select_item (poppler_field, index);
2273 PDF_DOCUMENT (document)->modified = TRUE;
2277 pdf_document_forms_form_field_choice_toggle_item (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_choice_toggle_item (poppler_field, index);
2288 PDF_DOCUMENT (document)->modified = TRUE;
2292 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2295 PopplerFormField *poppler_field;
2297 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2301 poppler_form_field_choice_unselect_all (poppler_field);
2302 PDF_DOCUMENT (document)->modified = TRUE;
2306 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2310 PopplerFormField *poppler_field;
2312 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2316 poppler_form_field_choice_set_text (poppler_field, text);
2317 PDF_DOCUMENT (document)->modified = TRUE;
2321 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2324 PopplerFormField *poppler_field;
2327 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2331 text = poppler_form_field_choice_get_text (poppler_field);
2337 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2339 iface->get_form_fields = pdf_document_forms_get_form_fields;
2340 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2341 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2342 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2343 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2344 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2345 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2346 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2347 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2348 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2349 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2350 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2351 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2356 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2359 PopplerColor *poppler_color;
2361 poppler_color = poppler_annot_get_color (poppler_annot);
2362 if (poppler_color) {
2363 color->red = poppler_color->red;
2364 color->green = poppler_color->green;
2365 color->blue = poppler_color->blue;
2367 g_free (poppler_color);
2368 } /* TODO: else use a default color */
2371 static EvAnnotation *
2372 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2375 EvAnnotation *ev_annot = NULL;
2376 const gchar *unimplemented_annot = NULL;
2378 switch (poppler_annot_get_annot_type (poppler_annot)) {
2379 case POPPLER_ANNOT_TEXT:
2380 PopplerAnnotText *poppler_text;
2381 EvAnnotationText *ev_annot_text;
2383 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2385 ev_annot = ev_annotation_text_new (page);
2387 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2388 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2391 case POPPLER_ANNOT_LINK:
2392 case POPPLER_ANNOT_WIDGET:
2393 /* Ignore link and widgets annots since they are already handled */
2396 GEnumValue *enum_value;
2398 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2399 poppler_annot_get_annot_type (poppler_annot));
2400 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2404 if (unimplemented_annot) {
2405 g_warning ("Unimplemented annotation: %s, please post a "
2406 "bug report in Evince bugzilla "
2407 "(http://bugzilla.gnome.org) with a testcase.",
2408 unimplemented_annot);
2412 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2413 ev_annot->name = poppler_annot_get_name (poppler_annot);
2414 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2415 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2417 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2418 PopplerAnnotMarkup *markup;
2421 PopplerRectangle poppler_rect;
2423 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2425 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2426 EvRectangle ev_rect;
2430 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2432 ev_rect.x1 = poppler_rect.x1;
2433 ev_rect.x2 = poppler_rect.x2;
2434 ev_rect.y1 = height - poppler_rect.y2;
2435 ev_rect.y2 = height - poppler_rect.y1;
2437 is_open = poppler_annot_markup_get_popup_is_open (markup);
2439 g_object_set (ev_annot,
2440 "rectangle", &ev_rect,
2445 /* FIXME: Use poppler_annot_markup_has_popup() when
2446 * new poppler is released.
2448 g_object_set (ev_annot,
2453 label = poppler_annot_markup_get_label (markup);
2454 opacity = poppler_annot_markup_get_opacity (markup);
2456 g_object_set (ev_annot,
2469 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2472 GList *retval = NULL;
2473 PdfDocument *pdf_document;
2474 PopplerPage *poppler_page;
2480 pdf_document = PDF_DOCUMENT (document_annotations);
2481 poppler_page = POPPLER_PAGE (page->backend_page);
2482 annots = poppler_page_get_annot_mapping (poppler_page);
2483 poppler_page_get_size (poppler_page, NULL, &height);
2485 for (list = annots; list; list = list->next) {
2486 PopplerAnnotMapping *mapping;
2487 EvMapping *annot_mapping;
2488 EvAnnotation *ev_annot;
2490 mapping = (PopplerAnnotMapping *)list->data;
2492 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2498 /* Make sure annot has a unique name */
2499 if (!ev_annot->name)
2500 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2502 annot_mapping = g_new (EvMapping, 1);
2503 annot_mapping->area.x1 = mapping->area.x1;
2504 annot_mapping->area.x2 = mapping->area.x2;
2505 annot_mapping->area.y1 = height - mapping->area.y2;
2506 annot_mapping->area.y2 = height - mapping->area.y1;
2507 annot_mapping->data = ev_annot;
2509 g_object_set_data_full (G_OBJECT (ev_annot),
2511 g_object_ref (mapping->annot),
2512 (GDestroyNotify) g_object_unref);
2514 retval = g_list_prepend (retval, annot_mapping);
2517 poppler_page_free_annot_mapping (annots);
2519 return g_list_reverse (retval);
2523 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2524 EvAnnotation *annot,
2525 const gchar *contents)
2527 PopplerAnnot *poppler_annot;
2529 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2533 poppler_annot_set_contents (poppler_annot, contents);
2534 PDF_DOCUMENT (document)->modified = TRUE;
2538 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2540 iface->get_annotations = pdf_document_annotations_get_annotations;
2541 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2545 struct SaveToBufferData {
2551 attachment_save_to_buffer_callback (const gchar *buf,
2556 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2560 if (sdata->len + count > sdata->max) {
2561 new_max = MAX (sdata->max * 2, sdata->len + count);
2562 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2564 sdata->buffer = new_buffer;
2565 sdata->max = new_max;
2568 memcpy (sdata->buffer + sdata->len, buf, count);
2569 sdata->len += count;
2575 attachment_save_to_buffer (PopplerAttachment *attachment,
2580 static const gint initial_max = 1024;
2581 struct SaveToBufferData sdata;
2586 sdata.buffer = (gchar *) g_malloc (initial_max);
2587 sdata.max = initial_max;
2590 if (! poppler_attachment_save_to_callback (attachment,
2591 attachment_save_to_buffer_callback,
2594 g_free (sdata.buffer);
2598 *buffer = sdata.buffer;
2599 *buffer_size = sdata.len;
2605 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2607 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2610 GList *retval = NULL;
2612 attachments = poppler_document_get_attachments (pdf_document->document);
2614 for (list = attachments; list; list = list->next) {
2615 PopplerAttachment *attachment;
2616 EvAttachment *ev_attachment;
2619 GError *error = NULL;
2621 attachment = (PopplerAttachment *) list->data;
2623 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2624 ev_attachment = ev_attachment_new (attachment->name,
2625 attachment->description,
2630 retval = g_list_prepend (retval, ev_attachment);
2633 g_warning ("%s", error->message);
2634 g_error_free (error);
2640 g_object_unref (attachment);
2643 return g_list_reverse (retval);
2647 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2649 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2651 return poppler_document_has_attachments (pdf_document->document);
2655 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface)
2657 iface->has_attachments = pdf_document_attachments_has_attachments;
2658 iface->get_attachments = pdf_document_attachments_get_attachments;
2663 pdf_document_layers_has_layers (EvDocumentLayers *document)
2665 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2666 PopplerLayersIter *iter;
2668 iter = poppler_layers_iter_new (pdf_document->document);
2671 poppler_layers_iter_free (iter);
2677 build_layers_tree (PdfDocument *pdf_document,
2678 GtkTreeModel *model,
2679 GtkTreeIter *parent,
2680 PopplerLayersIter *iter)
2683 GtkTreeIter tree_iter;
2684 PopplerLayersIter *child;
2685 PopplerLayer *layer;
2686 EvLayer *ev_layer = NULL;
2691 layer = poppler_layers_iter_get_layer (iter);
2693 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2694 visible = poppler_layer_is_visible (layer);
2695 rb_group = poppler_layer_get_radio_button_group_id (layer);
2696 pdf_document->layers = g_list_append (pdf_document->layers,
2697 g_object_ref (layer));
2698 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2699 poppler_layer_is_parent (layer),
2704 title = poppler_layers_iter_get_title (iter);
2705 markup = g_markup_escape_text (title, -1);
2712 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2713 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2714 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2715 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2716 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2717 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2718 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2719 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2722 g_object_unref (ev_layer);
2725 child = poppler_layers_iter_get_child (iter);
2727 build_layers_tree (pdf_document, model, &tree_iter, child);
2728 poppler_layers_iter_free (child);
2729 } while (poppler_layers_iter_next (iter));
2732 static GtkTreeModel *
2733 pdf_document_layers_get_layers (EvDocumentLayers *document)
2735 GtkTreeModel *model = NULL;
2736 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2737 PopplerLayersIter *iter;
2739 iter = poppler_layers_iter_new (pdf_document->document);
2741 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2742 G_TYPE_STRING, /* TITLE */
2743 G_TYPE_OBJECT, /* LAYER */
2744 G_TYPE_BOOLEAN, /* VISIBLE */
2745 G_TYPE_BOOLEAN, /* ENABLED */
2746 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2747 G_TYPE_INT); /* RBGROUP */
2748 build_layers_tree (pdf_document, model, NULL, iter);
2749 poppler_layers_iter_free (iter);
2755 pdf_document_layers_show_layer (EvDocumentLayers *document,
2758 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2759 guint layer_id = ev_layer_get_id (layer);
2761 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2765 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2768 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2769 guint layer_id = ev_layer_get_id (layer);
2771 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2775 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2778 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2779 guint layer_id = ev_layer_get_id (layer);
2781 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2785 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2787 iface->has_layers = pdf_document_layers_has_layers;
2788 iface->get_layers = pdf_document_layers_get_layers;
2789 iface->show_layer = pdf_document_layers_show_layer;
2790 iface->hide_layer = pdf_document_layers_hide_layer;
2791 iface->layer_is_visible = pdf_document_layers_layer_is_visible;