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 EvDocumentClass parent_class;
100 EvDocument 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_security_iface_init (EvDocumentSecurityIface *iface);
117 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
118 static void pdf_document_document_links_iface_init (EvDocumentLinksIface *iface);
119 static void pdf_document_document_images_iface_init (EvDocumentImagesIface *iface);
120 static void pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface);
121 static void pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface);
122 static void pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface);
123 #ifdef HAVE_POPPLER_PAGE_RENDER
124 static void pdf_document_document_print_iface_init (EvDocumentPrintIface *iface);
126 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
127 static void pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface);
128 static void pdf_document_find_iface_init (EvDocumentFindIface *iface);
129 static void pdf_document_file_exporter_iface_init (EvFileExporterIface *iface);
130 static void pdf_selection_iface_init (EvSelectionIface *iface);
131 static void pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface);
132 static void pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
136 static int pdf_document_get_n_pages (EvDocument *document);
138 static EvLinkDest *ev_link_dest_from_dest (PdfDocument *pdf_document,
140 static EvLink *ev_link_from_action (PdfDocument *pdf_document,
141 PopplerAction *action);
142 static void pdf_document_search_free (PdfDocumentSearch *search);
143 static void pdf_print_context_free (PdfPrintContext *ctx);
145 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
147 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
148 pdf_document_security_iface_init);
149 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
150 pdf_document_document_thumbnails_iface_init);
151 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
152 pdf_document_document_links_iface_init);
153 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
154 pdf_document_document_images_iface_init);
155 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
156 pdf_document_document_forms_iface_init);
157 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
158 pdf_document_document_fonts_iface_init);
159 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
160 pdf_document_document_layers_iface_init);
161 #ifdef HAVE_POPPLER_PAGE_RENDER
162 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
163 pdf_document_document_print_iface_init);
165 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
166 pdf_document_document_annotations_iface_init);
167 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ATTACHMENTS,
168 pdf_document_document_attachments_iface_init);
169 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
170 pdf_document_find_iface_init);
171 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
172 pdf_document_file_exporter_iface_init);
173 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
174 pdf_selection_iface_init);
175 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
176 pdf_document_page_transition_iface_init);
180 pdf_document_search_free (PdfDocumentSearch *search)
182 PdfDocument *pdf_document = search->document;
186 if (search->idle != 0)
187 g_source_remove (search->idle);
189 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
190 for (i = 0; i < n_pages; i++) {
191 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
192 g_list_free (search->pages[i]);
194 g_free (search->pages);
196 g_free (search->text);
201 pdf_document_dispose (GObject *object)
203 PdfDocument *pdf_document = PDF_DOCUMENT(object);
205 if (pdf_document->print_ctx) {
206 pdf_print_context_free (pdf_document->print_ctx);
207 pdf_document->print_ctx = NULL;
210 if (pdf_document->search) {
211 pdf_document_search_free (pdf_document->search);
212 pdf_document->search = NULL;
215 if (pdf_document->document) {
216 g_object_unref (pdf_document->document);
219 if (pdf_document->font_info) {
220 poppler_font_info_free (pdf_document->font_info);
223 if (pdf_document->fonts_iter) {
224 poppler_fonts_iter_free (pdf_document->fonts_iter);
227 if (pdf_document->layers) {
228 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
229 g_list_free (pdf_document->layers);
232 G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
236 pdf_document_init (PdfDocument *pdf_document)
238 pdf_document->password = NULL;
242 convert_error (GError *poppler_error,
245 if (poppler_error == NULL)
248 if (poppler_error->domain == POPPLER_ERROR) {
249 /* convert poppler errors into EvDocument errors */
250 gint code = EV_DOCUMENT_ERROR_INVALID;
251 if (poppler_error->code == POPPLER_ERROR_INVALID)
252 code = EV_DOCUMENT_ERROR_INVALID;
253 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
254 code = EV_DOCUMENT_ERROR_ENCRYPTED;
256 g_set_error_literal (error,
259 poppler_error->message);
261 g_error_free (poppler_error);
263 g_propagate_error (error, poppler_error);
270 pdf_document_save (EvDocument *document,
274 PdfDocument *pdf_document = PDF_DOCUMENT (document);
276 GError *poppler_error = NULL;
278 if (pdf_document->modified) {
279 retval = poppler_document_save (pdf_document->document,
280 uri, &poppler_error);
282 retval = poppler_document_save_a_copy (pdf_document->document,
283 uri, &poppler_error);
287 convert_error (poppler_error, error);
293 pdf_document_load (EvDocument *document,
297 GError *poppler_error = NULL;
298 PdfDocument *pdf_document = PDF_DOCUMENT (document);
300 pdf_document->document =
301 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
303 if (pdf_document->document == NULL) {
304 convert_error (poppler_error, error);
312 pdf_document_get_n_pages (EvDocument *document)
314 return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
318 pdf_document_get_page (EvDocument *document,
321 PdfDocument *pdf_document = PDF_DOCUMENT (document);
322 PopplerPage *poppler_page;
325 poppler_page = poppler_document_get_page (pdf_document->document, index);
326 page = ev_page_new (index);
327 page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
328 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
329 g_object_unref (poppler_page);
335 pdf_document_get_page_size (EvDocument *document,
340 g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
342 poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
346 pdf_document_get_page_label (EvDocument *document,
351 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
353 g_object_get (G_OBJECT (page->backend_page),
359 static cairo_surface_t *
360 pdf_page_render (PopplerPage *page,
365 cairo_surface_t *surface;
367 #ifdef HAVE_POPPLER_PAGE_RENDER
370 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
372 cr = cairo_create (surface);
374 switch (rc->rotation) {
376 cairo_translate (cr, width, 0);
379 cairo_translate (cr, width, height);
382 cairo_translate (cr, 0, height);
385 cairo_translate (cr, 0, 0);
387 cairo_scale (cr, rc->scale, rc->scale);
388 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
389 poppler_page_render (page, cr);
391 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
392 cairo_set_source_rgb (cr, 1., 1., 1.);
396 #else /* HAVE_POPPLER_PAGE_RENDER */
399 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
403 poppler_page_render_to_pixbuf (page,
409 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
410 g_object_unref (pixbuf);
411 #endif /* HAVE_POPPLER_PAGE_RENDER */
416 static cairo_surface_t *
417 pdf_document_render (EvDocument *document,
420 PdfDocument *pdf_document;
421 PopplerPage *poppler_page;
422 double width_points, height_points;
425 poppler_page = POPPLER_PAGE (rc->page->backend_page);
427 poppler_page_get_size (poppler_page,
428 &width_points, &height_points);
430 if (rc->rotation == 90 || rc->rotation == 270) {
431 width = (int) ((height_points * rc->scale) + 0.5);
432 height = (int) ((width_points * rc->scale) + 0.5);
434 width = (int) ((width_points * rc->scale) + 0.5);
435 height = (int) ((height_points * rc->scale) + 0.5);
438 return pdf_page_render (poppler_page,
443 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 */
445 pdf_document_get_format_from_metadata (const char *metadata)
448 xmlXPathContextPtr xpathCtx;
449 xmlXPathObjectPtr xpathObj;
450 xmlChar *part = NULL;
451 xmlChar *conf = NULL;
455 doc = xmlParseMemory (metadata, strlen (metadata));
457 return NULL; /* invalid xml metadata */
459 xpathCtx = xmlXPathNewContext (doc);
460 if (xpathCtx == NULL) {
462 return NULL; /* invalid xpath context */
465 /* add pdf/a namespaces */
466 xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
467 xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
468 xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
470 /* reads pdf/a part */
471 /* first syntax: child node */
472 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
473 if (xpathObj != NULL) {
474 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
475 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
477 xmlXPathFreeObject (xpathObj);
480 /* second syntax: attribute */
481 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
482 if (xpathObj != NULL) {
483 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
484 part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
486 xmlXPathFreeObject (xpathObj);
490 /* reads pdf/a conformance */
491 /* first syntax: child node */
492 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
493 if (xpathObj != NULL) {
494 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
495 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
497 xmlXPathFreeObject (xpathObj);
500 /* second syntax: attribute */
501 xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
502 if (xpathObj != NULL) {
503 if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
504 conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
506 xmlXPathFreeObject (xpathObj);
510 if (part != NULL && conf != NULL) {
511 /* makes conf lowercase */
512 for (i = 0; conf[i]; i++)
513 conf[i] = g_ascii_tolower (conf[i]);
516 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
522 xmlXPathFreeContext (xpathCtx);
529 static EvDocumentInfo *
530 pdf_document_get_info (EvDocument *document)
532 EvDocumentInfo *info;
533 PopplerPageLayout layout;
534 PopplerPageMode mode;
535 PopplerViewerPreferences view_prefs;
536 PopplerPermissions permissions;
541 info = g_new0 (EvDocumentInfo, 1);
543 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
544 EV_DOCUMENT_INFO_FORMAT |
545 EV_DOCUMENT_INFO_AUTHOR |
546 EV_DOCUMENT_INFO_SUBJECT |
547 EV_DOCUMENT_INFO_KEYWORDS |
548 EV_DOCUMENT_INFO_LAYOUT |
549 EV_DOCUMENT_INFO_START_MODE |
550 EV_DOCUMENT_INFO_PERMISSIONS |
551 EV_DOCUMENT_INFO_UI_HINTS |
552 EV_DOCUMENT_INFO_CREATOR |
553 EV_DOCUMENT_INFO_PRODUCER |
554 EV_DOCUMENT_INFO_CREATION_DATE |
555 EV_DOCUMENT_INFO_MOD_DATE |
556 EV_DOCUMENT_INFO_LINEARIZED |
557 EV_DOCUMENT_INFO_N_PAGES |
558 EV_DOCUMENT_INFO_SECURITY |
559 EV_DOCUMENT_INFO_PAPER_SIZE;
561 g_object_get (PDF_DOCUMENT (document)->document,
562 "title", &(info->title),
563 "format", &(info->format),
564 "author", &(info->author),
565 "subject", &(info->subject),
566 "keywords", &(info->keywords),
568 "page-layout", &layout,
569 "viewer-preferences", &view_prefs,
570 "permissions", &permissions,
571 "creator", &(info->creator),
572 "producer", &(info->producer),
573 "creation-date", &(info->creation_date),
574 "mod-date", &(info->modified_date),
575 "linearized", &(info->linearized),
576 "metadata", &metadata,
579 if (metadata != NULL) {
580 fmt = pdf_document_get_format_from_metadata(metadata);
582 g_free (info->format);
588 info->n_pages = ev_document_get_n_pages (document);
590 if (info->n_pages > 0) {
591 ev_document_get_page_size (document, 0,
592 &(info->paper_width),
593 &(info->paper_height));
595 info->paper_width = info->paper_width / 72.0f * 25.4f;
596 info->paper_height = info->paper_height / 72.0f * 25.4f;
600 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
601 info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
603 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
604 info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
606 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
607 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
609 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
610 info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
611 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
612 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
614 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
615 info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
622 case POPPLER_PAGE_MODE_NONE:
623 info->mode = EV_DOCUMENT_MODE_NONE;
625 case POPPLER_PAGE_MODE_USE_THUMBS:
626 info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
628 case POPPLER_PAGE_MODE_USE_OC:
629 info->mode = EV_DOCUMENT_MODE_USE_OC;
631 case POPPLER_PAGE_MODE_FULL_SCREEN:
632 info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
634 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
635 info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
641 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
642 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
644 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
645 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
647 if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
648 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
650 if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
651 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
653 if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
654 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
656 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
657 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
659 if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
660 info->ui_hints |= EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
663 info->permissions = 0;
664 if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
665 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
667 if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
668 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
670 if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
671 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
673 if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
674 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
677 if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
678 /* translators: this is the document security state */
679 info->security = g_strdup (_("Yes"));
681 /* translators: this is the document security state */
682 info->security = g_strdup (_("No"));
689 pdf_document_class_init (PdfDocumentClass *klass)
691 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
692 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
694 g_object_class->dispose = pdf_document_dispose;
696 ev_document_class->save = pdf_document_save;
697 ev_document_class->load = pdf_document_load;
698 ev_document_class->get_n_pages = pdf_document_get_n_pages;
699 ev_document_class->get_page = pdf_document_get_page;
700 ev_document_class->get_page_size = pdf_document_get_page_size;
701 ev_document_class->get_page_label = pdf_document_get_page_label;
702 ev_document_class->render = pdf_document_render;
703 ev_document_class->get_info = pdf_document_get_info;
706 /* EvDocumentSecurity */
708 pdf_document_has_document_security (EvDocumentSecurity *document_security)
710 /* FIXME: do we really need to have this? */
715 pdf_document_set_password (EvDocumentSecurity *document_security,
716 const char *password)
718 PdfDocument *document = PDF_DOCUMENT (document_security);
720 if (document->password)
721 g_free (document->password);
723 document->password = g_strdup (password);
727 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
729 iface->has_document_security = pdf_document_has_document_security;
730 iface->set_password = pdf_document_set_password;
734 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
736 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
739 n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
741 return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
745 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
748 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
751 g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
753 if (pdf_document->font_info == NULL) {
754 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
757 if (pdf_document->fonts_iter) {
758 poppler_fonts_iter_free (pdf_document->fonts_iter);
761 pdf_document->fonts_scanned_pages += n_pages;
763 result = poppler_font_info_scan (pdf_document->font_info, n_pages,
764 &pdf_document->fonts_iter);
766 pdf_document->fonts_scanned_pages = 0;
767 poppler_font_info_free (pdf_document->font_info);
768 pdf_document->font_info = NULL;
775 font_type_to_string (PopplerFontType type)
778 case POPPLER_FONT_TYPE_TYPE1:
780 case POPPLER_FONT_TYPE_TYPE1C:
782 case POPPLER_FONT_TYPE_TYPE3:
784 case POPPLER_FONT_TYPE_TRUETYPE:
785 return _("TrueType");
786 case POPPLER_FONT_TYPE_CID_TYPE0:
787 return _("Type 1 (CID)");
788 case POPPLER_FONT_TYPE_CID_TYPE0C:
789 return _("Type 1C (CID)");
790 case POPPLER_FONT_TYPE_CID_TYPE2:
791 return _("TrueType (CID)");
793 return _("Unknown font type");
798 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
801 PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
802 PopplerFontsIter *iter = pdf_document->fonts_iter;
804 g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
810 GtkTreeIter list_iter;
813 const char *embedded;
816 name = poppler_fonts_iter_get_name (iter);
822 type = font_type_to_string (
823 poppler_fonts_iter_get_font_type (iter));
825 if (poppler_fonts_iter_is_embedded (iter)) {
826 if (poppler_fonts_iter_is_subset (iter))
827 embedded = _("Embedded subset");
829 embedded = _("Embedded");
831 embedded = _("Not embedded");
834 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
836 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
837 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
838 EV_DOCUMENT_FONTS_COLUMN_NAME, name,
839 EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
843 } while (poppler_fonts_iter_next (iter));
847 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
849 iface->fill_model = pdf_document_fonts_fill_model;
850 iface->scan = pdf_document_fonts_scan;
851 iface->get_progress = pdf_document_fonts_get_progress;
855 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
857 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
858 PopplerIndexIter *iter;
860 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
862 iter = poppler_index_iter_new (pdf_document->document);
865 poppler_index_iter_free (iter);
871 ev_link_dest_from_dest (PdfDocument *pdf_document,
874 EvLinkDest *ev_dest = NULL;
875 const char *unimplemented_dest = NULL;
877 g_assert (dest != NULL);
879 switch (dest->type) {
880 case POPPLER_DEST_XYZ: {
881 PopplerPage *poppler_page;
884 poppler_page = poppler_document_get_page (pdf_document->document,
885 MAX (0, dest->page_num - 1));
886 poppler_page_get_size (poppler_page, NULL, &height);
887 ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
889 height - MIN (height, dest->top),
894 g_object_unref (poppler_page);
897 case POPPLER_DEST_FITB:
898 case POPPLER_DEST_FIT:
899 ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
901 case POPPLER_DEST_FITBH:
902 case POPPLER_DEST_FITH: {
903 PopplerPage *poppler_page;
906 poppler_page = poppler_document_get_page (pdf_document->document,
907 MAX (0, dest->page_num - 1));
908 poppler_page_get_size (poppler_page, NULL, &height);
909 ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
910 height - MIN (height, dest->top),
912 g_object_unref (poppler_page);
915 case POPPLER_DEST_FITBV:
916 case POPPLER_DEST_FITV:
917 ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
921 case POPPLER_DEST_FITR: {
922 PopplerPage *poppler_page;
925 poppler_page = poppler_document_get_page (pdf_document->document,
926 MAX (0, dest->page_num - 1));
927 poppler_page_get_size (poppler_page, NULL, &height);
928 ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
930 height - MIN (height, dest->bottom),
932 height - MIN (height, dest->top));
933 g_object_unref (poppler_page);
936 case POPPLER_DEST_NAMED:
937 ev_dest = ev_link_dest_new_named (dest->named_dest);
939 case POPPLER_DEST_UNKNOWN:
940 unimplemented_dest = "POPPLER_DEST_UNKNOWN";
944 if (unimplemented_dest) {
945 g_warning ("Unimplemented destination: %s, please post a "
946 "bug report in Evince bugzilla "
947 "(http://bugzilla.gnome.org) with a testcase.",
952 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
958 ev_link_from_action (PdfDocument *pdf_document,
959 PopplerAction *action)
962 EvLinkAction *ev_action = NULL;
963 const char *unimplemented_action = NULL;
965 switch (action->type) {
966 case POPPLER_ACTION_NONE:
968 case POPPLER_ACTION_GOTO_DEST: {
971 dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
972 ev_action = ev_link_action_new_dest (dest);
975 case POPPLER_ACTION_GOTO_REMOTE: {
978 dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
979 ev_action = ev_link_action_new_remote (dest,
980 action->goto_remote.file_name);
984 case POPPLER_ACTION_LAUNCH:
985 ev_action = ev_link_action_new_launch (action->launch.file_name,
986 action->launch.params);
988 case POPPLER_ACTION_URI:
989 ev_action = ev_link_action_new_external_uri (action->uri.uri);
991 case POPPLER_ACTION_NAMED:
992 ev_action = ev_link_action_new_named (action->named.named_dest);
994 case POPPLER_ACTION_MOVIE:
995 unimplemented_action = "POPPLER_ACTION_MOVIE";
997 case POPPLER_ACTION_UNKNOWN:
998 unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1001 if (unimplemented_action) {
1002 g_warning ("Unimplemented action: %s, please post a bug report "
1003 "in Evince bugzilla (http://bugzilla.gnome.org) "
1004 "with a testcase.", unimplemented_action);
1007 link = ev_link_new (action->any.title, ev_action);
1013 build_tree (PdfDocument *pdf_document,
1014 GtkTreeModel *model,
1015 GtkTreeIter *parent,
1016 PopplerIndexIter *iter)
1020 GtkTreeIter tree_iter;
1021 PopplerIndexIter *child;
1022 PopplerAction *action;
1023 EvLink *link = NULL;
1027 action = poppler_index_iter_get_action (iter);
1028 expand = poppler_index_iter_is_open (iter);
1033 switch (action->type) {
1034 case POPPLER_ACTION_GOTO_DEST: {
1035 /* For bookmarks, solve named destinations */
1036 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1038 EvLinkDest *ev_dest = NULL;
1039 EvLinkAction *ev_action;
1041 dest = poppler_document_find_dest (pdf_document->document,
1042 action->goto_dest.dest->named_dest);
1044 link = ev_link_from_action (pdf_document, action);
1048 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1049 poppler_dest_free (dest);
1051 ev_action = ev_link_action_new_dest (ev_dest);
1052 link = ev_link_new (action->any.title, ev_action);
1054 link = ev_link_from_action (pdf_document, action);
1059 link = ev_link_from_action (pdf_document, action);
1063 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1064 poppler_action_free (action);
1066 g_object_unref (link);
1071 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1072 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1074 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1075 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1076 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1077 EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1080 g_free (title_markup);
1081 g_object_unref (link);
1083 child = poppler_index_iter_get_child (iter);
1085 build_tree (pdf_document, model, &tree_iter, child);
1086 poppler_index_iter_free (child);
1087 poppler_action_free (action);
1089 } while (poppler_index_iter_next (iter));
1092 static GtkTreeModel *
1093 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1095 PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1096 GtkTreeModel *model = NULL;
1097 PopplerIndexIter *iter;
1099 g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1101 iter = poppler_index_iter_new (pdf_document->document);
1102 /* Create the model if we have items*/
1104 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1109 build_tree (pdf_document, model, NULL, iter);
1110 poppler_index_iter_free (iter);
1117 pdf_document_links_get_links (EvDocumentLinks *document_links,
1120 PdfDocument *pdf_document;
1121 PopplerPage *poppler_page;
1122 GList *retval = NULL;
1123 GList *mapping_list;
1127 pdf_document = PDF_DOCUMENT (document_links);
1128 poppler_page = poppler_document_get_page (pdf_document->document,
1130 mapping_list = poppler_page_get_link_mapping (poppler_page);
1131 poppler_page_get_size (poppler_page, NULL, &height);
1133 for (list = mapping_list; list; list = list->next) {
1134 PopplerLinkMapping *link_mapping;
1135 EvMapping *ev_link_mapping;
1137 link_mapping = (PopplerLinkMapping *)list->data;
1138 ev_link_mapping = g_new (EvMapping, 1);
1139 ev_link_mapping->data = ev_link_from_action (pdf_document,
1140 link_mapping->action);
1141 ev_link_mapping->area.x1 = link_mapping->area.x1;
1142 ev_link_mapping->area.x2 = link_mapping->area.x2;
1143 /* Invert this for X-style coordinates */
1144 ev_link_mapping->area.y1 = height - link_mapping->area.y2;
1145 ev_link_mapping->area.y2 = height - link_mapping->area.y1;
1147 retval = g_list_prepend (retval, ev_link_mapping);
1150 poppler_page_free_link_mapping (mapping_list);
1151 g_object_unref (poppler_page);
1153 return g_list_reverse (retval);
1157 pdf_document_links_find_link_dest (EvDocumentLinks *document_links,
1158 const gchar *link_name)
1160 PdfDocument *pdf_document;
1162 EvLinkDest *ev_dest = NULL;
1164 pdf_document = PDF_DOCUMENT (document_links);
1165 dest = poppler_document_find_dest (pdf_document->document,
1168 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1169 poppler_dest_free (dest);
1176 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1178 iface->has_document_links = pdf_document_links_has_document_links;
1179 iface->get_links_model = pdf_document_links_get_links_model;
1180 iface->get_links = pdf_document_links_get_links;
1181 iface->find_link_dest = pdf_document_links_find_link_dest;
1185 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1188 GList *retval = NULL;
1189 PdfDocument *pdf_document;
1190 PopplerPage *poppler_page;
1191 GList *mapping_list;
1194 pdf_document = PDF_DOCUMENT (document_images);
1195 poppler_page = poppler_document_get_page (pdf_document->document, page);
1196 mapping_list = poppler_page_get_image_mapping (poppler_page);
1198 for (list = mapping_list; list; list = list->next) {
1199 PopplerImageMapping *image_mapping;
1200 EvMapping *ev_image_mapping;
1202 image_mapping = (PopplerImageMapping *)list->data;
1204 ev_image_mapping = g_new (EvMapping, 1);
1206 ev_image_mapping->data = ev_image_new (page, image_mapping->image_id);
1207 ev_image_mapping->area.x1 = image_mapping->area.x1;
1208 ev_image_mapping->area.y1 = image_mapping->area.y1;
1209 ev_image_mapping->area.x2 = image_mapping->area.x2;
1210 ev_image_mapping->area.y2 = image_mapping->area.y2;
1212 retval = g_list_prepend (retval, ev_image_mapping);
1215 poppler_page_free_image_mapping (mapping_list);
1216 g_object_unref (poppler_page);
1218 return g_list_reverse (retval);
1222 pdf_document_images_get_image (EvDocumentImages *document_images,
1225 GdkPixbuf *retval = NULL;
1226 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1227 PdfDocument *pdf_document;
1228 PopplerPage *poppler_page;
1229 cairo_surface_t *surface;
1231 pdf_document = PDF_DOCUMENT (document_images);
1232 poppler_page = poppler_document_get_page (pdf_document->document,
1233 ev_image_get_page (image));
1235 surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1237 retval = ev_document_misc_pixbuf_from_surface (surface);
1238 cairo_surface_destroy (surface);
1241 g_object_unref (poppler_page);
1247 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1249 iface->get_image_mapping = pdf_document_images_get_image_mapping;
1250 iface->get_image = pdf_document_images_get_image;
1254 make_thumbnail_for_page (PopplerPage *poppler_page,
1255 EvRenderContext *rc,
1261 #ifdef POPPLER_WITH_GDK
1262 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1264 gdk_pixbuf_fill (pixbuf, 0xffffffff);
1266 ev_document_fc_mutex_lock ();
1267 poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1269 rc->scale, rc->rotation, pixbuf);
1270 ev_document_fc_mutex_unlock ();
1272 cairo_surface_t *surface;
1274 ev_document_fc_mutex_lock ();
1275 surface = pdf_page_render (poppler_page, width, height, rc);
1276 ev_document_fc_mutex_unlock ();
1278 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1279 cairo_surface_destroy (surface);
1280 #endif /* POPPLER_WITH_GDK */
1286 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1287 EvRenderContext *rc,
1290 PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1291 PopplerPage *poppler_page;
1292 GdkPixbuf *pixbuf = NULL;
1293 GdkPixbuf *border_pixbuf;
1296 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1298 pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1299 rc, &width, &height);
1301 #ifdef POPPLER_WITH_GDK
1302 pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1304 cairo_surface_t *surface;
1306 surface = poppler_page_get_thumbnail (poppler_page);
1308 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1309 cairo_surface_destroy (surface);
1311 #endif /* POPPLER_WITH_GDK */
1313 if (pixbuf != NULL) {
1314 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1315 gdk_pixbuf_get_height (pixbuf) :
1316 gdk_pixbuf_get_width (pixbuf);
1318 if (thumb_width == width) {
1319 GdkPixbuf *rotated_pixbuf;
1321 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1322 (GdkPixbufRotation) (360 - rc->rotation));
1323 g_object_unref (pixbuf);
1324 pixbuf = rotated_pixbuf;
1326 /* The provided thumbnail has a different size */
1327 g_object_unref (pixbuf);
1328 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1331 /* There is no provided thumbnail. We need to make one. */
1332 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1335 if (border && pixbuf) {
1336 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1337 g_object_unref (pixbuf);
1338 pixbuf = border_pixbuf;
1345 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1346 EvRenderContext *rc,
1350 double page_width, page_height;
1352 poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1353 &page_width, &page_height);
1355 *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1356 *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1358 if (rc->rotation == 90 || rc->rotation == 270) {
1368 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1370 iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1371 iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1376 pdf_document_find_find_text (EvDocumentFind *document_find,
1379 gboolean case_sensitive)
1382 PopplerPage *poppler_page;
1385 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1386 g_return_val_if_fail (text != NULL, NULL);
1388 poppler_page = POPPLER_PAGE (page->backend_page);
1390 matches = poppler_page_find_text (poppler_page, text);
1394 poppler_page_get_size (poppler_page, NULL, &height);
1395 for (l = matches; l && l->data; l = g_list_next (l)) {
1396 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1400 rect->y1 = height - rect->y2;
1401 rect->y2 = height - tmp;
1408 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1410 iface->find_text = pdf_document_find_find_text;
1414 pdf_print_context_free (PdfPrintContext *ctx)
1419 #ifdef HAVE_CAIRO_PRINT
1421 cairo_destroy (ctx->cr);
1426 poppler_ps_file_free (ctx->ps_file);
1427 ctx->ps_file = NULL;
1434 pdf_document_file_exporter_begin (EvFileExporter *exporter,
1435 EvFileExporterContext *fc)
1437 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1438 PdfPrintContext *ctx;
1439 #ifdef HAVE_CAIRO_PRINT
1440 gdouble width, height;
1441 cairo_surface_t *surface = NULL;
1444 if (pdf_document->print_ctx)
1445 pdf_print_context_free (pdf_document->print_ctx);
1446 pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1447 ctx = pdf_document->print_ctx;
1448 ctx->format = fc->format;
1450 #ifdef HAVE_CAIRO_PRINT
1451 ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1453 ctx->paper_width = fc->paper_width;
1454 ctx->paper_height = fc->paper_height;
1456 switch (fc->pages_per_sheet) {
1484 ctx->pages_printed = 0;
1486 switch (fc->format) {
1487 case EV_FILE_FORMAT_PS:
1488 #ifdef HAVE_CAIRO_PS
1489 surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1492 case EV_FILE_FORMAT_PDF:
1493 #ifdef HAVE_CAIRO_PDF
1494 surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1498 g_assert_not_reached ();
1501 ctx->cr = cairo_create (surface);
1502 cairo_surface_destroy (surface);
1504 #else /* HAVE_CAIRO_PRINT */
1505 if (ctx->format == EV_FILE_FORMAT_PS) {
1506 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1507 fc->filename, fc->first_page,
1508 fc->last_page - fc->first_page + 1);
1509 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1510 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1512 #endif /* HAVE_CAIRO_PRINT */
1516 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1518 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1519 PdfPrintContext *ctx = pdf_document->print_ctx;
1521 g_return_if_fail (pdf_document->print_ctx != NULL);
1523 ctx->pages_printed = 0;
1525 #ifdef HAVE_CAIRO_PRINT
1526 if (ctx->paper_width > ctx->paper_height) {
1527 if (ctx->format == EV_FILE_FORMAT_PS) {
1528 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1531 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1532 cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1537 #endif /* HAVE_CAIRO_PRINT */
1541 pdf_document_file_exporter_do_page (EvFileExporter *exporter,
1542 EvRenderContext *rc)
1544 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1545 PdfPrintContext *ctx = pdf_document->print_ctx;
1546 PopplerPage *poppler_page;
1547 #ifdef HAVE_CAIRO_PRINT
1548 gdouble page_width, page_height;
1551 gdouble width, height;
1552 gdouble pwidth, pheight;
1553 gdouble xscale, yscale;
1556 g_return_if_fail (pdf_document->print_ctx != NULL);
1558 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1560 #ifdef HAVE_CAIRO_PRINT
1561 x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1562 y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1563 poppler_page_get_size (poppler_page, &page_width, &page_height);
1565 if (page_width > page_height && page_width > ctx->paper_width) {
1571 /* Use always portrait mode and rotate when necessary */
1572 if (ctx->paper_width > ctx->paper_height) {
1573 width = ctx->paper_height;
1574 height = ctx->paper_width;
1577 width = ctx->paper_width;
1578 height = ctx->paper_height;
1581 if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1594 page_width = page_height;
1598 pwidth = width / ctx->pages_x;
1599 pheight = height / ctx->pages_y;
1601 if ((page_width > pwidth || page_height > pheight) ||
1602 (page_width < pwidth && page_height < pheight)) {
1603 xscale = pwidth / page_width;
1604 yscale = pheight / page_height;
1606 if (yscale < xscale) {
1613 xscale = yscale = 1;
1618 cairo_save (ctx->cr);
1620 cairo_matrix_t matrix;
1622 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1623 cairo_matrix_init (&matrix,
1627 cairo_transform (ctx->cr, &matrix);
1630 cairo_translate (ctx->cr,
1631 x * (rotate ? pheight : pwidth),
1632 y * (rotate ? pwidth : pheight));
1633 cairo_scale (ctx->cr, xscale, yscale);
1635 poppler_page_render_for_printing (poppler_page, ctx->cr);
1637 ctx->pages_printed++;
1639 cairo_restore (ctx->cr);
1640 #else /* HAVE_CAIRO_PRINT */
1641 if (ctx->format == EV_FILE_FORMAT_PS)
1642 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1643 #endif /* HAVE_CAIRO_PRINT */
1647 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1649 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1650 PdfPrintContext *ctx = pdf_document->print_ctx;
1652 g_return_if_fail (pdf_document->print_ctx != NULL);
1654 #ifdef HAVE_CAIRO_PRINT
1655 cairo_show_page (ctx->cr);
1660 pdf_document_file_exporter_end (EvFileExporter *exporter)
1662 PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1664 pdf_print_context_free (pdf_document->print_ctx);
1665 pdf_document->print_ctx = NULL;
1668 static EvFileExporterCapabilities
1669 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1671 return (EvFileExporterCapabilities) (
1672 EV_FILE_EXPORTER_CAN_PAGE_SET |
1673 EV_FILE_EXPORTER_CAN_COPIES |
1674 EV_FILE_EXPORTER_CAN_COLLATE |
1675 EV_FILE_EXPORTER_CAN_REVERSE |
1676 EV_FILE_EXPORTER_CAN_SCALE |
1677 #ifdef HAVE_CAIRO_PRINT
1678 #ifdef HAVE_POPPLER_PAGE_RENDER
1679 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1683 #ifdef HAVE_CAIRO_PDF
1684 #ifdef HAVE_POPPLER_PAGE_RENDER
1685 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1688 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1692 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1694 iface->begin = pdf_document_file_exporter_begin;
1695 iface->begin_page = pdf_document_file_exporter_begin_page;
1696 iface->do_page = pdf_document_file_exporter_do_page;
1697 iface->end_page = pdf_document_file_exporter_end_page;
1698 iface->end = pdf_document_file_exporter_end;
1699 iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1702 #ifdef HAVE_POPPLER_PAGE_RENDER
1703 /* EvDocumentPrint */
1705 pdf_document_print_print_page (EvDocumentPrint *document,
1709 PdfDocument *pdf_document = PDF_DOCUMENT (document);
1711 poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1715 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1717 iface->print_page = pdf_document_print_print_page;
1719 #endif /* HAVE_POPPLER_PAGE_RENDER */
1722 pdf_selection_render_selection (EvSelection *selection,
1723 EvRenderContext *rc,
1724 cairo_surface_t **surface,
1725 EvRectangle *points,
1726 EvRectangle *old_points,
1727 EvSelectionStyle style,
1731 PopplerPage *poppler_page;
1732 double width_points, height_points;
1735 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1737 poppler_page_get_size (poppler_page,
1738 &width_points, &height_points);
1739 width = (int) ((width_points * rc->scale) + 0.5);
1740 height = (int) ((height_points * rc->scale) + 0.5);
1742 #ifdef HAVE_POPPLER_PAGE_RENDER
1744 PopplerColor text_color, base_color;
1746 text_color.red = text->red;
1747 text_color.green = text->green;
1748 text_color.blue = text->blue;
1750 base_color.red = base->red;
1751 base_color.green = base->green;
1752 base_color.blue = base->blue;
1754 if (*surface == NULL) {
1755 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1760 cr = cairo_create (*surface);
1761 cairo_scale (cr, rc->scale, rc->scale);
1762 cairo_surface_set_device_offset (*surface, 0, 0);
1763 memset (cairo_image_surface_get_data (*surface), 0x00,
1764 cairo_image_surface_get_height (*surface) *
1765 cairo_image_surface_get_stride (*surface));
1766 poppler_page_render_selection (poppler_page,
1768 (PopplerRectangle *)points,
1769 (PopplerRectangle *)old_points,
1770 (PopplerSelectionStyle)style,
1774 #else /* HAVE_POPPLER_PAGE_RENDER */
1777 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1781 poppler_page_render_selection_to_pixbuf (poppler_page,
1782 rc->scale, rc->rotation, pixbuf,
1783 (PopplerRectangle *)points,
1784 (PopplerRectangle *)old_points,
1785 (PopplerSelectionStyle)style,
1789 cairo_surface_destroy (*surface);
1790 *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1791 g_object_unref (pixbuf);
1792 #endif /* HAVE_POPPLER_PAGE_RENDER */
1796 pdf_selection_get_selected_text (EvSelection *selection,
1797 EvRenderContext *rc,
1798 EvSelectionStyle style,
1799 EvRectangle *points)
1801 PopplerPage *poppler_page;
1806 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1808 poppler_page_get_size (poppler_page, NULL, &height);
1810 r.y1 = height - points->y2;
1812 r.y2 = height - points->y1;
1814 retval = poppler_page_get_text (poppler_page,
1815 (PopplerSelectionStyle)style,
1822 create_gdk_region_from_poppler_region (GList *region)
1827 retval = gdk_region_new ();
1829 for (l = region; l; l = g_list_next (l)) {
1830 PopplerRectangle *rectangle;
1833 rectangle = (PopplerRectangle *)l->data;
1835 rect.x = (gint) rectangle->x1;
1836 rect.y = (gint) rectangle->y1;
1837 rect.width = (gint) (rectangle->x2 - rectangle->x1);
1838 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1839 gdk_region_union_with_rect (retval, &rect);
1841 poppler_rectangle_free (rectangle);
1848 pdf_selection_get_selection_region (EvSelection *selection,
1849 EvRenderContext *rc,
1850 EvSelectionStyle style,
1851 EvRectangle *points)
1853 PopplerPage *poppler_page;
1857 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1859 region = poppler_page_get_selection_region (poppler_page,
1861 (PopplerSelectionStyle)style,
1862 (PopplerRectangle *) points);
1863 retval = create_gdk_region_from_poppler_region (region);
1864 g_list_free (region);
1870 pdf_selection_get_selection_map (EvSelection *selection,
1871 EvRenderContext *rc)
1873 PopplerPage *poppler_page;
1874 PopplerRectangle points;
1878 poppler_page = POPPLER_PAGE (rc->page->backend_page);
1882 poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1884 region = poppler_page_get_selection_region (poppler_page, 1.0,
1885 POPPLER_SELECTION_GLYPH,
1887 retval = create_gdk_region_from_poppler_region (region);
1888 g_list_free (region);
1894 pdf_selection_iface_init (EvSelectionIface *iface)
1896 iface->render_selection = pdf_selection_render_selection;
1897 iface->get_selected_text = pdf_selection_get_selected_text;
1898 iface->get_selection_region = pdf_selection_get_selection_region;
1899 iface->get_selection_map = pdf_selection_get_selection_map;
1902 /* Page Transitions */
1904 pdf_document_get_page_duration (EvDocumentTransition *trans,
1907 PdfDocument *pdf_document;
1908 PopplerPage *poppler_page;
1909 gdouble duration = -1;
1911 pdf_document = PDF_DOCUMENT (trans);
1912 poppler_page = poppler_document_get_page (pdf_document->document, page);
1916 duration = poppler_page_get_duration (poppler_page);
1917 g_object_unref (poppler_page);
1922 static EvTransitionEffect *
1923 pdf_document_get_effect (EvDocumentTransition *trans,
1926 PdfDocument *pdf_document;
1927 PopplerPage *poppler_page;
1928 PopplerPageTransition *page_transition;
1929 EvTransitionEffect *effect;
1931 pdf_document = PDF_DOCUMENT (trans);
1932 poppler_page = poppler_document_get_page (pdf_document->document, page);
1937 page_transition = poppler_page_get_transition (poppler_page);
1939 if (!page_transition) {
1940 g_object_unref (poppler_page);
1944 /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1945 effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1946 "alignment", page_transition->alignment,
1947 "direction", page_transition->direction,
1948 "duration", page_transition->duration,
1949 "angle", page_transition->angle,
1950 "scale", page_transition->scale,
1951 "rectangular", page_transition->rectangular,
1954 poppler_page_transition_free (page_transition);
1955 g_object_unref (poppler_page);
1961 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1963 iface->get_page_duration = pdf_document_get_page_duration;
1964 iface->get_effect = pdf_document_get_effect;
1969 pdf_document_get_crop_box (EvDocument *document,
1973 PdfDocument *pdf_document;
1974 PopplerPage *poppler_page;
1975 PopplerRectangle poppler_rect;
1977 pdf_document = PDF_DOCUMENT (document);
1978 poppler_page = poppler_document_get_page (pdf_document->document, page);
1979 poppler_page_get_crop_box (poppler_page, &poppler_rect);
1980 rect->x1 = poppler_rect.x1;
1981 rect->x2 = poppler_rect.x2;
1982 rect->y1 = poppler_rect.y1;
1983 rect->y2 = poppler_rect.y2;
1986 static EvFormField *
1987 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1989 EvFormField *ev_field = NULL;
1992 gboolean is_read_only;
1994 id = poppler_form_field_get_id (poppler_field);
1995 font_size = poppler_form_field_get_font_size (poppler_field);
1996 is_read_only = poppler_form_field_is_read_only (poppler_field);
1998 switch (poppler_form_field_get_field_type (poppler_field)) {
1999 case POPPLER_FORM_FIELD_TEXT: {
2000 EvFormFieldText *field_text;
2001 EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2003 switch (poppler_form_field_text_get_text_type (poppler_field)) {
2004 case POPPLER_FORM_TEXT_NORMAL:
2005 ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2007 case POPPLER_FORM_TEXT_MULTILINE:
2008 ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2010 case POPPLER_FORM_TEXT_FILE_SELECT:
2011 ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2015 ev_field = ev_form_field_text_new (id, ev_text_type);
2016 field_text = EV_FORM_FIELD_TEXT (ev_field);
2018 field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2019 field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2020 field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2021 field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2022 field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2023 field_text->text = poppler_form_field_text_get_text (poppler_field);
2027 case POPPLER_FORM_FIELD_BUTTON: {
2028 EvFormFieldButton *field_button;
2029 EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2031 switch (poppler_form_field_button_get_button_type (poppler_field)) {
2032 case POPPLER_FORM_BUTTON_PUSH:
2033 ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2035 case POPPLER_FORM_BUTTON_CHECK:
2036 ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2038 case POPPLER_FORM_BUTTON_RADIO:
2039 ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2043 ev_field = ev_form_field_button_new (id, ev_button_type);
2044 field_button = EV_FORM_FIELD_BUTTON (ev_field);
2046 field_button->state = poppler_form_field_button_get_state (poppler_field);
2049 case POPPLER_FORM_FIELD_CHOICE: {
2050 EvFormFieldChoice *field_choice;
2051 EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2053 switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2054 case POPPLER_FORM_CHOICE_COMBO:
2055 ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2057 case EV_FORM_FIELD_CHOICE_LIST:
2058 ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2062 ev_field = ev_form_field_choice_new (id, ev_choice_type);
2063 field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2065 field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2066 field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2067 field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2068 field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2070 /* TODO: we need poppler_form_field_choice_get_selected_items in poppler
2071 field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2072 if (field_choice->is_editable)
2073 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2076 case POPPLER_FORM_FIELD_SIGNATURE:
2078 ev_field = ev_form_field_signature_new (id);
2080 case POPPLER_FORM_FIELD_UNKNOWN:
2084 ev_field->font_size = font_size;
2085 ev_field->is_read_only = is_read_only;
2091 pdf_document_forms_get_form_fields (EvDocumentForms *document,
2094 PopplerPage *poppler_page;
2095 GList *retval = NULL;
2100 g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2102 poppler_page = POPPLER_PAGE (page->backend_page);
2103 fields = poppler_page_get_form_field_mapping (poppler_page);
2104 poppler_page_get_size (poppler_page, NULL, &height);
2106 for (list = fields; list; list = list->next) {
2107 PopplerFormFieldMapping *mapping;
2108 EvMapping *field_mapping;
2109 EvFormField *ev_field;
2111 mapping = (PopplerFormFieldMapping *)list->data;
2113 ev_field = ev_form_field_from_poppler_field (mapping->field);
2117 field_mapping = g_new0 (EvMapping, 1);
2118 field_mapping->area.x1 = mapping->area.x1;
2119 field_mapping->area.x2 = mapping->area.x2;
2120 field_mapping->area.y1 = height - mapping->area.y2;
2121 field_mapping->area.y2 = height - mapping->area.y1;
2122 field_mapping->data = ev_field;
2123 ev_field->page = EV_PAGE (g_object_ref (page));
2125 g_object_set_data_full (G_OBJECT (ev_field),
2127 g_object_ref (mapping->field),
2128 (GDestroyNotify) g_object_unref);
2130 retval = g_list_prepend (retval, field_mapping);
2133 poppler_page_free_form_field_mapping (fields);
2135 return g_list_reverse (retval);
2139 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2143 PopplerFormField *poppler_field;
2146 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2150 text = poppler_form_field_text_get_text (poppler_field);
2156 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document,
2160 PopplerFormField *poppler_field;
2162 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2166 poppler_form_field_text_set_text (poppler_field, text);
2167 PDF_DOCUMENT (document)->modified = TRUE;
2171 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document,
2175 PopplerFormField *poppler_field;
2177 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2181 poppler_form_field_button_set_state (poppler_field, state);
2182 PDF_DOCUMENT (document)->modified = TRUE;
2186 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document,
2189 PopplerFormField *poppler_field;
2192 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2196 state = poppler_form_field_button_get_state (poppler_field);
2202 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document,
2206 PopplerFormField *poppler_field;
2209 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2213 text = poppler_form_field_choice_get_item (poppler_field, index);
2219 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document,
2222 PopplerFormField *poppler_field;
2225 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2229 n_items = poppler_form_field_choice_get_n_items (poppler_field);
2235 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document,
2239 PopplerFormField *poppler_field;
2242 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2246 selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2252 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document,
2256 PopplerFormField *poppler_field;
2258 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2262 poppler_form_field_choice_select_item (poppler_field, index);
2263 PDF_DOCUMENT (document)->modified = TRUE;
2267 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document,
2271 PopplerFormField *poppler_field;
2273 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2277 poppler_form_field_choice_toggle_item (poppler_field, index);
2278 PDF_DOCUMENT (document)->modified = TRUE;
2282 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document,
2285 PopplerFormField *poppler_field;
2287 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2291 poppler_form_field_choice_unselect_all (poppler_field);
2292 PDF_DOCUMENT (document)->modified = TRUE;
2296 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2300 PopplerFormField *poppler_field;
2302 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2306 poppler_form_field_choice_set_text (poppler_field, text);
2307 PDF_DOCUMENT (document)->modified = TRUE;
2311 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2314 PopplerFormField *poppler_field;
2317 poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2321 text = poppler_form_field_choice_get_text (poppler_field);
2327 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2329 iface->get_form_fields = pdf_document_forms_get_form_fields;
2330 iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2331 iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2332 iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2333 iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2334 iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2335 iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2336 iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2337 iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2338 iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2339 iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2340 iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2341 iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2346 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2349 PopplerColor *poppler_color;
2351 poppler_color = poppler_annot_get_color (poppler_annot);
2352 if (poppler_color) {
2353 color->red = poppler_color->red;
2354 color->green = poppler_color->green;
2355 color->blue = poppler_color->blue;
2357 g_free (poppler_color);
2358 } /* TODO: else use a default color */
2361 static EvAnnotation *
2362 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2365 EvAnnotation *ev_annot = NULL;
2366 const gchar *unimplemented_annot = NULL;
2368 switch (poppler_annot_get_annot_type (poppler_annot)) {
2369 case POPPLER_ANNOT_TEXT:
2370 PopplerAnnotText *poppler_text;
2371 EvAnnotationText *ev_annot_text;
2373 poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2375 ev_annot = ev_annotation_text_new (page);
2377 ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2378 ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2381 case POPPLER_ANNOT_LINK:
2382 case POPPLER_ANNOT_WIDGET:
2383 /* Ignore link and widgets annots since they are already handled */
2386 GEnumValue *enum_value;
2388 enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2389 poppler_annot_get_annot_type (poppler_annot));
2390 unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2394 if (unimplemented_annot) {
2395 g_warning ("Unimplemented annotation: %s, please post a "
2396 "bug report in Evince bugzilla "
2397 "(http://bugzilla.gnome.org) with a testcase.",
2398 unimplemented_annot);
2402 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2403 ev_annot->name = poppler_annot_get_name (poppler_annot);
2404 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2405 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2407 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2408 PopplerAnnotMarkup *markup;
2411 PopplerRectangle poppler_rect;
2413 markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2415 if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2416 EvRectangle ev_rect;
2420 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2422 ev_rect.x1 = poppler_rect.x1;
2423 ev_rect.x2 = poppler_rect.x2;
2424 ev_rect.y1 = height - poppler_rect.y2;
2425 ev_rect.y2 = height - poppler_rect.y1;
2427 is_open = poppler_annot_markup_get_popup_is_open (markup);
2429 g_object_set (ev_annot,
2430 "rectangle", &ev_rect,
2435 /* FIXME: Use poppler_annot_markup_has_popup() when
2436 * new poppler is released.
2438 g_object_set (ev_annot,
2443 label = poppler_annot_markup_get_label (markup);
2444 opacity = poppler_annot_markup_get_opacity (markup);
2446 g_object_set (ev_annot,
2459 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2462 GList *retval = NULL;
2463 PdfDocument *pdf_document;
2464 PopplerPage *poppler_page;
2470 pdf_document = PDF_DOCUMENT (document_annotations);
2471 poppler_page = POPPLER_PAGE (page->backend_page);
2472 annots = poppler_page_get_annot_mapping (poppler_page);
2473 poppler_page_get_size (poppler_page, NULL, &height);
2475 for (list = annots; list; list = list->next) {
2476 PopplerAnnotMapping *mapping;
2477 EvMapping *annot_mapping;
2478 EvAnnotation *ev_annot;
2480 mapping = (PopplerAnnotMapping *)list->data;
2482 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2488 /* Make sure annot has a unique name */
2489 if (!ev_annot->name)
2490 ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2492 annot_mapping = g_new (EvMapping, 1);
2493 annot_mapping->area.x1 = mapping->area.x1;
2494 annot_mapping->area.x2 = mapping->area.x2;
2495 annot_mapping->area.y1 = height - mapping->area.y2;
2496 annot_mapping->area.y2 = height - mapping->area.y1;
2497 annot_mapping->data = ev_annot;
2499 g_object_set_data_full (G_OBJECT (ev_annot),
2501 g_object_ref (mapping->annot),
2502 (GDestroyNotify) g_object_unref);
2504 retval = g_list_prepend (retval, annot_mapping);
2507 poppler_page_free_annot_mapping (annots);
2509 return g_list_reverse (retval);
2513 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2514 EvAnnotation *annot,
2515 const gchar *contents)
2517 PopplerAnnot *poppler_annot;
2519 poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2523 poppler_annot_set_contents (poppler_annot, contents);
2524 PDF_DOCUMENT (document)->modified = TRUE;
2528 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2530 iface->get_annotations = pdf_document_annotations_get_annotations;
2531 iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2535 struct SaveToBufferData {
2541 attachment_save_to_buffer_callback (const gchar *buf,
2546 struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2550 if (sdata->len + count > sdata->max) {
2551 new_max = MAX (sdata->max * 2, sdata->len + count);
2552 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
2554 sdata->buffer = new_buffer;
2555 sdata->max = new_max;
2558 memcpy (sdata->buffer + sdata->len, buf, count);
2559 sdata->len += count;
2565 attachment_save_to_buffer (PopplerAttachment *attachment,
2570 static const gint initial_max = 1024;
2571 struct SaveToBufferData sdata;
2576 sdata.buffer = (gchar *) g_malloc (initial_max);
2577 sdata.max = initial_max;
2580 if (! poppler_attachment_save_to_callback (attachment,
2581 attachment_save_to_buffer_callback,
2584 g_free (sdata.buffer);
2588 *buffer = sdata.buffer;
2589 *buffer_size = sdata.len;
2595 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2597 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2600 GList *retval = NULL;
2602 attachments = poppler_document_get_attachments (pdf_document->document);
2604 for (list = attachments; list; list = list->next) {
2605 PopplerAttachment *attachment;
2606 EvAttachment *ev_attachment;
2609 GError *error = NULL;
2611 attachment = (PopplerAttachment *) list->data;
2613 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2614 ev_attachment = ev_attachment_new (attachment->name,
2615 attachment->description,
2620 retval = g_list_prepend (retval, ev_attachment);
2623 g_warning ("%s", error->message);
2624 g_error_free (error);
2630 g_object_unref (attachment);
2633 return g_list_reverse (retval);
2637 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2639 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2641 return poppler_document_has_attachments (pdf_document->document);
2645 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface)
2647 iface->has_attachments = pdf_document_attachments_has_attachments;
2648 iface->get_attachments = pdf_document_attachments_get_attachments;
2653 pdf_document_layers_has_layers (EvDocumentLayers *document)
2655 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2656 PopplerLayersIter *iter;
2658 iter = poppler_layers_iter_new (pdf_document->document);
2661 poppler_layers_iter_free (iter);
2667 build_layers_tree (PdfDocument *pdf_document,
2668 GtkTreeModel *model,
2669 GtkTreeIter *parent,
2670 PopplerLayersIter *iter)
2673 GtkTreeIter tree_iter;
2674 PopplerLayersIter *child;
2675 PopplerLayer *layer;
2676 EvLayer *ev_layer = NULL;
2681 layer = poppler_layers_iter_get_layer (iter);
2683 markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2684 visible = poppler_layer_is_visible (layer);
2685 rb_group = poppler_layer_get_radio_button_group_id (layer);
2686 pdf_document->layers = g_list_append (pdf_document->layers,
2687 g_object_ref (layer));
2688 ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2689 poppler_layer_is_parent (layer),
2694 title = poppler_layers_iter_get_title (iter);
2695 markup = g_markup_escape_text (title, -1);
2702 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2703 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2704 EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2705 EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2706 EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2707 EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2708 EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2709 EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2712 g_object_unref (ev_layer);
2715 child = poppler_layers_iter_get_child (iter);
2717 build_layers_tree (pdf_document, model, &tree_iter, child);
2718 poppler_layers_iter_free (child);
2719 } while (poppler_layers_iter_next (iter));
2722 static GtkTreeModel *
2723 pdf_document_layers_get_layers (EvDocumentLayers *document)
2725 GtkTreeModel *model = NULL;
2726 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2727 PopplerLayersIter *iter;
2729 iter = poppler_layers_iter_new (pdf_document->document);
2731 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2732 G_TYPE_STRING, /* TITLE */
2733 G_TYPE_OBJECT, /* LAYER */
2734 G_TYPE_BOOLEAN, /* VISIBLE */
2735 G_TYPE_BOOLEAN, /* ENABLED */
2736 G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2737 G_TYPE_INT); /* RBGROUP */
2738 build_layers_tree (pdf_document, model, NULL, iter);
2739 poppler_layers_iter_free (iter);
2745 pdf_document_layers_show_layer (EvDocumentLayers *document,
2748 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2749 guint layer_id = ev_layer_get_id (layer);
2751 poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2755 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2758 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2759 guint layer_id = ev_layer_get_id (layer);
2761 poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2765 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2768 PdfDocument *pdf_document = PDF_DOCUMENT (document);
2769 guint layer_id = ev_layer_get_id (layer);
2771 return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2775 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2777 iface->has_layers = pdf_document_layers_has_layers;
2778 iface->get_layers = pdf_document_layers_get_layers;
2779 iface->show_layer = pdf_document_layers_show_layer;
2780 iface->hide_layer = pdf_document_layers_hide_layer;
2781 iface->layer_is_visible = pdf_document_layers_layer_is_visible;