]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
93064456aede70e507db2309163f1793144e8cba
[evince.git] / backend / pdf / ev-poppler.cc
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.
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  */
19
20 #include "config.h"
21
22 #include <math.h>
23 #include <string.h>
24 #include <gtk/gtk.h>
25 #include <poppler.h>
26 #include <poppler-document.h>
27 #include <poppler-page.h>
28 #ifdef HAVE_CAIRO_PDF
29 #include <cairo-pdf.h>
30 #endif
31 #ifdef HAVE_CAIRO_PS
32 #include <cairo-ps.h>
33 #endif
34 #include <glib/gi18n-lib.h>
35
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"
55 #include "ev-image.h"
56
57 #include <libxml/tree.h>
58 #include <libxml/parser.h>
59 #include <libxml/xpath.h>
60 #include <libxml/xpathInternals.h>
61
62 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
63 #define HAVE_CAIRO_PRINT
64 #endif
65
66 typedef struct {
67         PdfDocument *document;
68         char *text;
69         GList **pages;
70         guint idle;
71         int start_page;
72         int search_page;
73 } PdfDocumentSearch;
74
75 typedef struct {
76         EvFileExporterFormat format;
77
78         /* Pages per sheet */
79         gint pages_per_sheet;
80         gint pages_printed;
81         gint pages_x;
82         gint pages_y;
83         gdouble paper_width;
84         gdouble paper_height;
85         
86 #ifdef HAVE_CAIRO_PRINT
87         cairo_t *cr;
88 #else
89         PopplerPSFile *ps_file;
90 #endif
91 } PdfPrintContext;
92
93 struct _PdfDocumentClass
94 {
95         EvDocumentClass parent_class;
96 };
97
98 struct _PdfDocument
99 {
100         EvDocument parent_instance;
101
102         PopplerDocument *document;
103         gchar *password;
104         gboolean modified;
105
106         PopplerFontInfo *font_info;
107         PopplerFontsIter *fonts_iter;
108         int fonts_scanned_pages;
109
110         PdfDocumentSearch *search;
111         PdfPrintContext *print_ctx;
112
113         GList *layers;
114 };
115
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);
125 #endif
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,
133                                                           EvRenderContext            *rc,
134                                                           gint                       *width,
135                                                           gint                       *height);
136 static int  pdf_document_get_n_pages                     (EvDocument                 *document);
137
138 static EvLinkDest *ev_link_dest_from_dest   (PdfDocument       *pdf_document,
139                                              PopplerDest       *dest);
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);
144
145 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
146                          {
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);
164 #endif
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);
177                          });
178
179 static void
180 pdf_document_search_free (PdfDocumentSearch   *search)
181 {
182         PdfDocument *pdf_document = search->document;
183         int n_pages;
184         int i;
185
186         if (search->idle != 0)
187                 g_source_remove (search->idle);
188
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]);
193         }
194         g_free (search->pages);
195         
196         g_free (search->text);
197         g_free (search);
198 }
199
200 static void
201 pdf_document_dispose (GObject *object)
202 {
203         PdfDocument *pdf_document = PDF_DOCUMENT(object);
204
205         if (pdf_document->print_ctx) {
206                 pdf_print_context_free (pdf_document->print_ctx);
207                 pdf_document->print_ctx = NULL;
208         }
209         
210         if (pdf_document->search) {
211                 pdf_document_search_free (pdf_document->search);
212                 pdf_document->search = NULL;
213         }
214
215         if (pdf_document->document) {
216                 g_object_unref (pdf_document->document);
217         }
218
219         if (pdf_document->font_info) { 
220                 poppler_font_info_free (pdf_document->font_info);
221         }
222
223         if (pdf_document->fonts_iter) {
224                 poppler_fonts_iter_free (pdf_document->fonts_iter);
225         }
226
227         if (pdf_document->layers) {
228                 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
229                 g_list_free (pdf_document->layers);
230         }
231
232         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
233 }
234
235 static void
236 pdf_document_init (PdfDocument *pdf_document)
237 {
238         pdf_document->password = NULL;
239 }
240
241 static void
242 convert_error (GError  *poppler_error,
243                GError **error)
244 {
245         if (poppler_error == NULL)
246                 return;
247
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;
255                         
256                 g_set_error_literal (error,
257                                      EV_DOCUMENT_ERROR,
258                                      code,
259                                      poppler_error->message);
260
261                 g_error_free (poppler_error);
262         } else {
263                 g_propagate_error (error, poppler_error);
264         }
265 }
266
267
268 /* EvDocument */
269 static gboolean
270 pdf_document_save (EvDocument  *document,
271                    const char  *uri,
272                    GError     **error)
273 {
274         PdfDocument *pdf_document = PDF_DOCUMENT (document);
275         gboolean retval;
276         GError *poppler_error = NULL;
277
278         if (pdf_document->modified) {
279                 retval = poppler_document_save (pdf_document->document,
280                                                 uri, &poppler_error);
281         } else {
282                 retval = poppler_document_save_a_copy (pdf_document->document,
283                                                        uri, &poppler_error);
284         }
285                                                        
286         if (! retval)
287                 convert_error (poppler_error, error);
288
289         return retval;
290 }
291
292 static gboolean
293 pdf_document_load (EvDocument   *document,
294                    const char   *uri,
295                    GError      **error)
296 {
297         GError *poppler_error = NULL;
298         PdfDocument *pdf_document = PDF_DOCUMENT (document);
299
300         pdf_document->document =
301                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
302
303         if (pdf_document->document == NULL) {
304                 convert_error (poppler_error, error);
305                 return FALSE;
306         }
307
308         return TRUE;
309 }
310
311 static int
312 pdf_document_get_n_pages (EvDocument *document)
313 {
314         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
315 }
316
317 static EvPage *
318 pdf_document_get_page (EvDocument *document,
319                        gint        index)
320 {
321         PdfDocument *pdf_document = PDF_DOCUMENT (document);
322         PopplerPage *poppler_page;
323         EvPage      *page;
324
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);
330
331         return page;
332 }
333
334 static void
335 pdf_document_get_page_size (EvDocument *document,
336                             EvPage     *page,
337                             double     *width,
338                             double     *height)
339 {
340         g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
341         
342         poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
343 }
344
345 static char *
346 pdf_document_get_page_label (EvDocument *document,
347                              EvPage     *page)
348 {
349         char *label = NULL;
350
351         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
352
353         g_object_get (G_OBJECT (page->backend_page),
354                       "label", &label,
355                       NULL);
356         return label;
357 }
358
359 static cairo_surface_t *
360 pdf_page_render (PopplerPage     *page,
361                  gint             width,
362                  gint             height,
363                  EvRenderContext *rc)
364 {
365         cairo_surface_t *surface;
366
367 #ifdef HAVE_POPPLER_PAGE_RENDER
368         cairo_t *cr;
369
370         surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
371                                               width, height);
372         cr = cairo_create (surface);
373
374         switch (rc->rotation) {
375                 case 90:
376                         cairo_translate (cr, width, 0);
377                         break;
378                 case 180:
379                         cairo_translate (cr, width, height);
380                         break;
381                 case 270:
382                         cairo_translate (cr, 0, height);
383                         break;
384                 default:
385                         cairo_translate (cr, 0, 0);
386         }
387         cairo_scale (cr, rc->scale, rc->scale);
388         cairo_rotate (cr, rc->rotation * G_PI / 180.0);
389         poppler_page_render (page, cr);
390
391         cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
392         cairo_set_source_rgb (cr, 1., 1., 1.);
393         cairo_paint (cr);
394
395         cairo_destroy (cr);
396 #else /* HAVE_POPPLER_PAGE_RENDER */
397         GdkPixbuf *pixbuf;
398         
399         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
400                                  FALSE, 8,
401                                  width, height);
402
403         poppler_page_render_to_pixbuf (page,
404                                        0, 0,
405                                        width, height,
406                                        rc->scale,
407                                        rc->rotation,
408                                        pixbuf);
409         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
410         g_object_unref (pixbuf);
411 #endif /* HAVE_POPPLER_PAGE_RENDER */
412
413         return surface; 
414 }
415
416 static cairo_surface_t *
417 pdf_document_render (EvDocument      *document,
418                      EvRenderContext *rc)
419 {
420         PdfDocument *pdf_document;
421         PopplerPage *poppler_page;
422         double width_points, height_points;
423         gint width, height;
424
425         poppler_page = POPPLER_PAGE (rc->page->backend_page);
426
427         poppler_page_get_size (poppler_page,
428                                &width_points, &height_points);
429         
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);
433         } else {
434                 width = (int) ((width_points * rc->scale) + 0.5);
435                 height = (int) ((height_points * rc->scale) + 0.5);
436         }
437         
438         return pdf_page_render (poppler_page,
439                                 width, height, rc);
440 }
441
442 /* reference:
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 */
444 static char *
445 pdf_document_get_format_from_metadata (const char *metadata)
446 {
447         xmlDocPtr doc;
448         xmlXPathContextPtr xpathCtx;
449         xmlXPathObjectPtr xpathObj;
450         xmlChar *part = NULL;
451         xmlChar *conf = NULL;
452         char *result = NULL;
453         int i;
454
455         doc = xmlParseMemory (metadata, strlen (metadata));
456         if (doc == NULL)
457                 return NULL;      /* invalid xml metadata */
458
459         xpathCtx = xmlXPathNewContext (doc);
460         if (xpathCtx == NULL) {
461                 xmlFreeDoc (doc);
462                 return NULL;      /* invalid xpath context */
463         }
464
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/");
469
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]);
476
477                 xmlXPathFreeObject (xpathObj);
478         }
479         if (part == NULL) {
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]);
485
486                         xmlXPathFreeObject (xpathObj);
487                 }
488         }
489
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]);
496
497                 xmlXPathFreeObject (xpathObj);
498         }
499         if (conf == NULL) {
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]);
505
506                         xmlXPathFreeObject (xpathObj);
507                 }
508         }
509
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]);
514
515                 /* return buffer */
516                 result = g_strdup_printf ("PDF/A - %s%s", part, conf);
517         }
518
519         /* Cleanup */
520         xmlFree (part);
521         xmlFree (conf);
522         xmlXPathFreeContext (xpathCtx);
523         xmlFreeDoc (doc);
524
525         return result;
526 }
527
528
529 static EvDocumentInfo *
530 pdf_document_get_info (EvDocument *document)
531 {
532         EvDocumentInfo *info;
533         PopplerPageLayout layout;
534         PopplerPageMode mode;
535         PopplerViewerPreferences view_prefs;
536         PopplerPermissions permissions;
537         EvPage *page;
538         char *metadata;
539         char *fmt;
540
541         info = g_new0 (EvDocumentInfo, 1);
542
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;
560
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),
567                       "page-mode", &mode,
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,
577                       NULL);
578
579         if (metadata != NULL) {
580                 fmt = pdf_document_get_format_from_metadata(metadata);
581                 if (fmt != NULL) {
582                         g_free (info->format);
583                         info->format = fmt;
584                 }
585                 g_free (metadata);
586         }
587
588         info->n_pages = ev_document_get_n_pages (document);
589
590         if (info->n_pages > 0) {
591                 ev_document_get_page_size (document, 0,
592                                            &(info->paper_width),
593                                            &(info->paper_height));
594                 // Convert to mm.
595                 info->paper_width = info->paper_width / 72.0f * 25.4f;
596                 info->paper_height = info->paper_height / 72.0f * 25.4f;
597         }
598
599         switch (layout) {
600                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
601                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
602                         break;
603                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
604                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
605                         break;
606                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
607                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
608                         break;
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;
613                         break;
614                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
615                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
616                         break;
617                 default:
618                         break;
619         }
620
621         switch (mode) {
622                 case POPPLER_PAGE_MODE_NONE:
623                         info->mode = EV_DOCUMENT_MODE_NONE;
624                         break;
625                 case POPPLER_PAGE_MODE_USE_THUMBS:
626                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
627                         break;
628                 case POPPLER_PAGE_MODE_USE_OC:
629                         info->mode = EV_DOCUMENT_MODE_USE_OC;
630                         break;
631                 case POPPLER_PAGE_MODE_FULL_SCREEN:
632                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
633                         break;
634                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
635                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
636                 default:
637                         break;
638         }
639
640         info->ui_hints = 0;
641         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
642                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
643         }
644         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
645                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
646         }
647         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
648                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
649         }
650         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
651                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
652         }
653         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
654                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
655         }
656         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
657                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
658         }
659         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
660                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
661         }
662
663         info->permissions = 0;
664         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
665                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
666         }
667         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
668                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
669         }
670         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
671                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
672         }
673         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
674                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
675         }
676
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"));
680         } else {
681                 /* translators: this is the document security state */
682                 info->security = g_strdup (_("No"));
683         }
684
685         return info;
686 }
687
688 static void
689 pdf_document_class_init (PdfDocumentClass *klass)
690 {
691         GObjectClass    *g_object_class = G_OBJECT_CLASS (klass);
692         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
693
694         g_object_class->dispose = pdf_document_dispose;
695
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;
704 }
705
706 /* EvDocumentSecurity */
707 static gboolean
708 pdf_document_has_document_security (EvDocumentSecurity *document_security)
709 {
710         /* FIXME: do we really need to have this? */
711         return FALSE;
712 }
713
714 static void
715 pdf_document_set_password (EvDocumentSecurity *document_security,
716                            const char         *password)
717 {
718         PdfDocument *document = PDF_DOCUMENT (document_security);
719
720         if (document->password)
721                 g_free (document->password);
722
723         document->password = g_strdup (password);
724 }
725
726 static void
727 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
728 {
729         iface->has_document_security = pdf_document_has_document_security;
730         iface->set_password = pdf_document_set_password;
731 }
732
733 static gdouble
734 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
735 {
736         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
737         int n_pages;
738
739         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
740
741         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
742 }
743
744 static gboolean
745 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
746                          int              n_pages)
747 {
748         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
749         gboolean result;
750
751         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
752
753         if (pdf_document->font_info == NULL) { 
754                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
755         }
756
757         if (pdf_document->fonts_iter) {
758                 poppler_fonts_iter_free (pdf_document->fonts_iter);
759         }
760
761         pdf_document->fonts_scanned_pages += n_pages;
762
763         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
764                                          &pdf_document->fonts_iter);
765         if (!result) {
766                 pdf_document->fonts_scanned_pages = 0;
767                 poppler_font_info_free (pdf_document->font_info);
768                 pdf_document->font_info = NULL; 
769         }
770
771         return result;
772 }
773
774 static const char *
775 font_type_to_string (PopplerFontType type)
776 {
777         switch (type) {
778                 case POPPLER_FONT_TYPE_TYPE1:
779                         return _("Type 1");
780                 case POPPLER_FONT_TYPE_TYPE1C:
781                         return _("Type 1C");
782                 case POPPLER_FONT_TYPE_TYPE3:
783                         return _("Type 3");
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)");
792                 default:
793                         return _("Unknown font type");
794         }
795 }
796
797 static void
798 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
799                                GtkTreeModel    *model)
800 {
801         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
802         PopplerFontsIter *iter = pdf_document->fonts_iter;
803
804         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
805
806         if (!iter)
807                 return;
808
809         do {
810                 GtkTreeIter list_iter;
811                 const char *name;
812                 const char *type;
813                 const char *embedded;
814                 char *details;
815                 
816                 name = poppler_fonts_iter_get_name (iter);
817
818                 if (name == NULL) {
819                         name = _("No name");
820                 }
821
822                 type = font_type_to_string (
823                         poppler_fonts_iter_get_font_type (iter));
824
825                 if (poppler_fonts_iter_is_embedded (iter)) {
826                         if (poppler_fonts_iter_is_subset (iter))
827                                 embedded = _("Embedded subset");
828                         else
829                                 embedded = _("Embedded");
830                 } else {
831                         embedded = _("Not embedded");
832                 }
833
834                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
835
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,
840                                     -1);
841
842                 g_free (details);
843         } while (poppler_fonts_iter_next (iter));
844 }
845
846 static void
847 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
848 {
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;
852 }
853
854 static gboolean
855 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
856 {
857         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
858         PopplerIndexIter *iter;
859
860         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
861
862         iter = poppler_index_iter_new (pdf_document->document);
863         if (iter == NULL)
864                 return FALSE;
865         poppler_index_iter_free (iter);
866
867         return TRUE;
868 }
869
870 static EvLinkDest *
871 ev_link_dest_from_dest (PdfDocument *pdf_document,
872                         PopplerDest *dest)
873 {
874         EvLinkDest *ev_dest = NULL;
875         const char *unimplemented_dest = NULL;
876
877         g_assert (dest != NULL);
878
879         switch (dest->type) {
880                 case POPPLER_DEST_XYZ: {
881                         PopplerPage *poppler_page;
882                         double height;
883
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,
888                                                         dest->left,
889                                                         height - MIN (height, dest->top),
890                                                         dest->zoom,
891                                                         dest->change_left,
892                                                         dest->change_top,
893                                                         dest->change_zoom);
894                         g_object_unref (poppler_page);
895                 }
896                         break;
897                 case POPPLER_DEST_FITB:
898                 case POPPLER_DEST_FIT:
899                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
900                         break;
901                 case POPPLER_DEST_FITBH:
902                 case POPPLER_DEST_FITH: {
903                         PopplerPage *poppler_page;
904                         double height;
905
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),
911                                                          dest->change_top);
912                         g_object_unref (poppler_page);
913                 }
914                         break;
915                 case POPPLER_DEST_FITBV:
916                 case POPPLER_DEST_FITV:
917                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
918                                                          dest->left,
919                                                          dest->change_left);
920                         break;
921                 case POPPLER_DEST_FITR: {
922                         PopplerPage *poppler_page;
923                         double height;
924
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,
929                                                          dest->left,
930                                                          height - MIN (height, dest->bottom),
931                                                          dest->right,
932                                                          height - MIN (height, dest->top));
933                         g_object_unref (poppler_page);
934                 }
935                         break;
936                 case POPPLER_DEST_NAMED:
937                         ev_dest = ev_link_dest_new_named (dest->named_dest);
938                         break;
939                 case POPPLER_DEST_UNKNOWN:
940                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
941                         break;
942         }
943
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.",
948                            unimplemented_dest);
949         }
950
951         if (!ev_dest)
952                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
953         
954         return ev_dest;
955 }
956
957 static EvLink *
958 ev_link_from_action (PdfDocument   *pdf_document,
959                      PopplerAction *action)
960 {
961         EvLink       *link = NULL;
962         EvLinkAction *ev_action = NULL;
963         const char   *unimplemented_action = NULL;
964
965         switch (action->type) {
966                 case POPPLER_ACTION_NONE:
967                         break;
968                 case POPPLER_ACTION_GOTO_DEST: {
969                         EvLinkDest *dest;
970                         
971                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
972                         ev_action = ev_link_action_new_dest (dest);
973                 }
974                         break;
975                 case POPPLER_ACTION_GOTO_REMOTE: {
976                         EvLinkDest *dest;
977                         
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);
981                         
982                 }
983                         break;
984                 case POPPLER_ACTION_LAUNCH:
985                         ev_action = ev_link_action_new_launch (action->launch.file_name,
986                                                                action->launch.params);
987                         break;
988                 case POPPLER_ACTION_URI:
989                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
990                         break;
991                 case POPPLER_ACTION_NAMED:
992                         ev_action = ev_link_action_new_named (action->named.named_dest);
993                         break;
994                 case POPPLER_ACTION_MOVIE:
995                         unimplemented_action = "POPPLER_ACTION_MOVIE";
996                         break;
997                 case POPPLER_ACTION_UNKNOWN:
998                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
999         }
1000         
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);
1005         }
1006         
1007         link = ev_link_new (action->any.title, ev_action);
1008         
1009         return link;    
1010 }
1011
1012 static void
1013 build_tree (PdfDocument      *pdf_document,
1014             GtkTreeModel     *model,
1015             GtkTreeIter      *parent,
1016             PopplerIndexIter *iter)
1017 {
1018         
1019         do {
1020                 GtkTreeIter tree_iter;
1021                 PopplerIndexIter *child;
1022                 PopplerAction *action;
1023                 EvLink *link = NULL;
1024                 gboolean expand;
1025                 char *title_markup;
1026                 
1027                 action = poppler_index_iter_get_action (iter);
1028                 expand = poppler_index_iter_is_open (iter);
1029
1030                 if (!action)
1031                         continue;
1032
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) {
1037                                         PopplerDest *dest;
1038                                         EvLinkDest *ev_dest = NULL;
1039                                         EvLinkAction *ev_action;
1040                                         
1041                                         dest = poppler_document_find_dest (pdf_document->document,
1042                                                                            action->goto_dest.dest->named_dest);
1043                                         if (!dest) {
1044                                                 link = ev_link_from_action (pdf_document, action);
1045                                                 break;
1046                                         }
1047                                         
1048                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1049                                         poppler_dest_free (dest);
1050                                         
1051                                         ev_action = ev_link_action_new_dest (ev_dest);
1052                                         link = ev_link_new (action->any.title, ev_action);
1053                                 } else {
1054                                         link = ev_link_from_action (pdf_document, action);
1055                                 }
1056                         }
1057                                 break;
1058                         default:
1059                                 link = ev_link_from_action (pdf_document, action);
1060                                 break;
1061                 }
1062                 
1063                 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1064                         poppler_action_free (action);
1065                         if (link)
1066                                 g_object_unref (link);
1067                         
1068                         continue;
1069                 }
1070
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);
1073                 
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,
1078                                     -1);
1079                 
1080                 g_free (title_markup);
1081                 g_object_unref (link);
1082                 
1083                 child = poppler_index_iter_get_child (iter);
1084                 if (child)
1085                         build_tree (pdf_document, model, &tree_iter, child);
1086                 poppler_index_iter_free (child);
1087                 poppler_action_free (action);
1088                 
1089         } while (poppler_index_iter_next (iter));
1090 }
1091
1092 static GtkTreeModel *
1093 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1094 {
1095         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1096         GtkTreeModel *model = NULL;
1097         PopplerIndexIter *iter;
1098         
1099         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1100
1101         iter = poppler_index_iter_new (pdf_document->document);
1102         /* Create the model if we have items*/
1103         if (iter != NULL) {
1104                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1105                                                              G_TYPE_STRING,
1106                                                              G_TYPE_OBJECT,
1107                                                              G_TYPE_BOOLEAN,
1108                                                              G_TYPE_STRING);
1109                 build_tree (pdf_document, model, NULL, iter);
1110                 poppler_index_iter_free (iter);
1111         }
1112         
1113         return model;
1114 }
1115
1116 static GList *
1117 pdf_document_links_get_links (EvDocumentLinks *document_links,
1118                               gint             page)
1119 {
1120         PdfDocument *pdf_document;
1121         PopplerPage *poppler_page;
1122         GList *retval = NULL;
1123         GList *mapping_list;
1124         GList *list;
1125         double height;
1126
1127         pdf_document = PDF_DOCUMENT (document_links);
1128         poppler_page = poppler_document_get_page (pdf_document->document,
1129                                                   page);
1130         mapping_list = poppler_page_get_link_mapping (poppler_page);
1131         poppler_page_get_size (poppler_page, NULL, &height);
1132
1133         for (list = mapping_list; list; list = list->next) {
1134                 PopplerLinkMapping *link_mapping;
1135                 EvMapping *ev_link_mapping;
1136
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;
1146
1147                 retval = g_list_prepend (retval, ev_link_mapping);
1148         }
1149
1150         poppler_page_free_link_mapping (mapping_list);
1151         g_object_unref (poppler_page);
1152
1153         return g_list_reverse (retval);
1154 }
1155
1156 static EvLinkDest *
1157 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1158                                    const gchar      *link_name)
1159 {
1160         PdfDocument *pdf_document;
1161         PopplerDest *dest;
1162         EvLinkDest *ev_dest = NULL;
1163
1164         pdf_document = PDF_DOCUMENT (document_links);
1165         dest = poppler_document_find_dest (pdf_document->document,
1166                                            link_name);
1167         if (dest) {
1168                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1169                 poppler_dest_free (dest);
1170         }
1171
1172         return ev_dest;
1173 }
1174
1175 static void
1176 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1177 {
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;
1182 }
1183
1184 static GList *
1185 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1186                                        gint              page)
1187 {
1188         GList *retval = NULL;
1189         PdfDocument *pdf_document;
1190         PopplerPage *poppler_page;
1191         GList *mapping_list;
1192         GList *list;
1193
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);
1197
1198         for (list = mapping_list; list; list = list->next) {
1199                 PopplerImageMapping *image_mapping;
1200                 EvMapping *ev_image_mapping;
1201
1202                 image_mapping = (PopplerImageMapping *)list->data;
1203
1204                 ev_image_mapping = g_new (EvMapping, 1);
1205                 
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;
1211
1212                 retval = g_list_prepend (retval, ev_image_mapping);
1213         }
1214
1215         poppler_page_free_image_mapping (mapping_list);
1216         g_object_unref (poppler_page);
1217
1218         return g_list_reverse (retval);
1219 }
1220
1221 GdkPixbuf *
1222 pdf_document_images_get_image (EvDocumentImages *document_images,
1223                                EvImage          *image)
1224 {
1225         GdkPixbuf       *retval = NULL;
1226 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1227         PdfDocument     *pdf_document;
1228         PopplerPage     *poppler_page;
1229         cairo_surface_t *surface;
1230
1231         pdf_document = PDF_DOCUMENT (document_images);
1232         poppler_page = poppler_document_get_page (pdf_document->document,
1233                                                   ev_image_get_page (image));
1234
1235         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1236         if (surface) {
1237                 retval = ev_document_misc_pixbuf_from_surface (surface);
1238                 cairo_surface_destroy (surface);
1239         }
1240
1241         g_object_unref (poppler_page);
1242 #endif
1243         return retval;
1244 }
1245
1246 static void
1247 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1248 {
1249         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1250         iface->get_image = pdf_document_images_get_image;
1251 }
1252
1253 static GdkPixbuf *
1254 make_thumbnail_for_page (PopplerPage     *poppler_page,
1255                          EvRenderContext *rc,
1256                          gint             width,
1257                          gint             height)
1258 {
1259         GdkPixbuf *pixbuf;
1260
1261 #ifdef POPPLER_WITH_GDK
1262         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1263                                  width, height);
1264         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1265
1266         ev_document_fc_mutex_lock ();
1267         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1268                                        width, height,
1269                                        rc->scale, rc->rotation, pixbuf);
1270         ev_document_fc_mutex_unlock ();
1271 #else
1272         cairo_surface_t *surface;
1273
1274         ev_document_fc_mutex_lock ();
1275         surface = pdf_page_render (poppler_page, width, height, rc);
1276         ev_document_fc_mutex_unlock ();
1277         
1278         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1279         cairo_surface_destroy (surface);
1280 #endif /* POPPLER_WITH_GDK */
1281
1282         return pixbuf;
1283 }
1284
1285 static GdkPixbuf *
1286 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1287                                        EvRenderContext      *rc, 
1288                                        gboolean              border)
1289 {
1290         PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1291         PopplerPage *poppler_page;
1292         GdkPixbuf *pixbuf = NULL;
1293         GdkPixbuf *border_pixbuf;
1294         gint width, height;
1295
1296         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1297
1298         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1299                                                 rc, &width, &height);
1300         
1301 #ifdef POPPLER_WITH_GDK
1302         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1303 #else
1304         cairo_surface_t *surface;
1305         
1306         surface = poppler_page_get_thumbnail (poppler_page);
1307         if (surface) {
1308                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1309                 cairo_surface_destroy (surface);
1310         }
1311 #endif /* POPPLER_WITH_GDK */
1312
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);
1317
1318                 if (thumb_width == width) {
1319                         GdkPixbuf *rotated_pixbuf;
1320
1321                         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1322                                                                    (GdkPixbufRotation) (360 - rc->rotation));
1323                         g_object_unref (pixbuf);
1324                         pixbuf = rotated_pixbuf;
1325                 } else {
1326                         /* The provided thumbnail has a different size */
1327                         g_object_unref (pixbuf);
1328                         pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1329                 }
1330         } else {
1331                 /* There is no provided thumbnail. We need to make one. */
1332                 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1333         }
1334
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;
1339         }               
1340
1341         return pixbuf;
1342 }
1343
1344 static void
1345 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1346                                         EvRenderContext      *rc,
1347                                         gint                 *width,
1348                                         gint                 *height)
1349 {
1350         double page_width, page_height;
1351         
1352         poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1353                                &page_width, &page_height);
1354         
1355         *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
1356         *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
1357
1358         if (rc->rotation == 90 || rc->rotation == 270) {
1359                 gint  temp;
1360
1361                 temp = *width;
1362                 *width = *height;
1363                 *height = temp;
1364         }
1365 }
1366
1367 static void
1368 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1369 {
1370         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1371         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1372 }
1373
1374
1375 static GList *
1376 pdf_document_find_find_text (EvDocumentFind *document_find,
1377                              EvPage         *page,
1378                              const gchar    *text,
1379                              gboolean        case_sensitive)
1380 {
1381         GList *matches, *l;
1382         PopplerPage *poppler_page;
1383         gdouble height;
1384         
1385         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1386         g_return_val_if_fail (text != NULL, NULL);
1387
1388         poppler_page = POPPLER_PAGE (page->backend_page);
1389         
1390         matches = poppler_page_find_text (poppler_page, text);
1391         if (!matches)
1392                 return NULL;
1393
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;
1397                 gdouble           tmp;
1398
1399                 tmp = rect->y1;
1400                 rect->y1 = height - rect->y2;
1401                 rect->y2 = height - tmp;
1402         }
1403         
1404         return matches;
1405 }
1406
1407 static void
1408 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1409 {
1410         iface->find_text = pdf_document_find_find_text;
1411 }
1412
1413 static void
1414 pdf_print_context_free (PdfPrintContext *ctx)
1415 {
1416         if (!ctx)
1417                 return;
1418
1419 #ifdef HAVE_CAIRO_PRINT
1420         if (ctx->cr) {
1421                 cairo_destroy (ctx->cr);
1422                 ctx->cr = NULL;
1423         }
1424 #else
1425         if (ctx->ps_file) {
1426                 poppler_ps_file_free (ctx->ps_file);
1427                 ctx->ps_file = NULL;
1428         }
1429 #endif
1430         g_free (ctx);
1431 }
1432
1433 static void
1434 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1435                                   EvFileExporterContext *fc)
1436 {
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;
1442 #endif
1443         
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;
1449         
1450 #ifdef HAVE_CAIRO_PRINT
1451         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1452
1453         ctx->paper_width = fc->paper_width;
1454         ctx->paper_height = fc->paper_height;
1455         
1456         switch (fc->pages_per_sheet) {
1457                 default:
1458                 case 1:
1459                         ctx->pages_x = 1;
1460                         ctx->pages_y = 1;
1461                         break;
1462                 case 2:
1463                         ctx->pages_x = 1;
1464                         ctx->pages_y = 2;
1465                         break;
1466                 case 4:
1467                         ctx->pages_x = 2;
1468                         ctx->pages_y = 2;
1469                         break;
1470                 case 6:
1471                         ctx->pages_x = 2;
1472                         ctx->pages_y = 3;
1473                         break;
1474                 case 9:
1475                         ctx->pages_x = 3;
1476                         ctx->pages_y = 3;
1477                         break;
1478                 case 16:
1479                         ctx->pages_x = 4;
1480                         ctx->pages_y = 4;
1481                         break;
1482         }
1483
1484         ctx->pages_printed = 0;
1485         
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);
1490 #endif
1491                         break;
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);
1495 #endif
1496                         break;
1497                 default:
1498                         g_assert_not_reached ();
1499         }
1500
1501         ctx->cr = cairo_create (surface);
1502         cairo_surface_destroy (surface);
1503
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);
1511         }
1512 #endif /* HAVE_CAIRO_PRINT */
1513 }
1514
1515 static void
1516 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1517 {
1518         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1519         PdfPrintContext *ctx = pdf_document->print_ctx;
1520         
1521         g_return_if_fail (pdf_document->print_ctx != NULL);
1522
1523         ctx->pages_printed = 0;
1524         
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),
1529                                                    ctx->paper_height,
1530                                                    ctx->paper_width);
1531                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1532                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1533                                                     ctx->paper_height,
1534                                                     ctx->paper_width);
1535                 }
1536         }
1537 #endif /* HAVE_CAIRO_PRINT */
1538 }
1539
1540 static void
1541 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1542                                     EvRenderContext *rc)
1543 {
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;
1549         gint     x, y;
1550         gboolean rotate;
1551         gdouble  width, height;
1552         gdouble  pwidth, pheight;
1553         gdouble  xscale, yscale;
1554 #endif
1555
1556         g_return_if_fail (pdf_document->print_ctx != NULL);
1557
1558         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1559         
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);
1564
1565         if (page_width > page_height && page_width > ctx->paper_width) {
1566                 rotate = TRUE;
1567         } else {
1568                 rotate = FALSE;
1569         }
1570
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;
1575                 rotate = !rotate;
1576         } else {
1577                 width = ctx->paper_width;
1578                 height = ctx->paper_height;
1579         }
1580
1581         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1582                 rotate = !rotate;
1583         }       
1584
1585         if (rotate) {
1586                 gint tmp1;
1587                 gdouble tmp2;
1588
1589                 tmp1 = x;
1590                 x = y;
1591                 y = tmp1;
1592
1593                 tmp2 = page_width;
1594                 page_width = page_height;
1595                 page_height = tmp2;
1596         }
1597
1598         pwidth = width / ctx->pages_x;
1599         pheight = height / ctx->pages_y;
1600
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;
1605                 
1606                 if (yscale < xscale) {
1607                         xscale = yscale;
1608                 } else {
1609                         yscale = xscale;
1610                 }
1611                 
1612         } else {        
1613                 xscale = yscale = 1;
1614         }
1615
1616         /* TODO: center */
1617
1618         cairo_save (ctx->cr);
1619         if (rotate) {
1620                 cairo_matrix_t matrix;
1621                 
1622                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1623                 cairo_matrix_init (&matrix,
1624                                    0,  1,
1625                                    -1,  0,
1626                                    0,  0);
1627                 cairo_transform (ctx->cr, &matrix);
1628         }
1629         
1630         cairo_translate (ctx->cr,
1631                          x * (rotate ? pheight : pwidth),
1632                          y * (rotate ? pwidth : pheight));
1633         cairo_scale (ctx->cr, xscale, yscale);
1634
1635         poppler_page_render_for_printing (poppler_page, ctx->cr);
1636
1637         ctx->pages_printed++;
1638                         
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 */
1644 }
1645
1646 static void
1647 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1648 {
1649         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1650         PdfPrintContext *ctx = pdf_document->print_ctx;
1651         
1652         g_return_if_fail (pdf_document->print_ctx != NULL);
1653
1654 #ifdef HAVE_CAIRO_PRINT
1655         cairo_show_page (ctx->cr);
1656 #endif
1657 }
1658
1659 static void
1660 pdf_document_file_exporter_end (EvFileExporter *exporter)
1661 {
1662         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1663
1664         pdf_print_context_free (pdf_document->print_ctx);
1665         pdf_document->print_ctx = NULL;
1666 }
1667
1668 static EvFileExporterCapabilities
1669 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1670 {
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 |
1680 #endif
1681 #endif
1682                 
1683 #ifdef HAVE_CAIRO_PDF
1684 #ifdef HAVE_POPPLER_PAGE_RENDER
1685                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1686 #endif
1687 #endif
1688                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1689 }
1690
1691 static void
1692 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1693 {
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;
1700 }
1701
1702 #ifdef HAVE_POPPLER_PAGE_RENDER
1703 /* EvDocumentPrint */
1704 static void
1705 pdf_document_print_print_page (EvDocumentPrint *document,
1706                                EvPage          *page,
1707                                cairo_t         *cr)
1708 {
1709         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1710
1711         poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
1712 }
1713
1714 static void
1715 pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
1716 {
1717         iface->print_page = pdf_document_print_print_page;
1718 }
1719 #endif /* HAVE_POPPLER_PAGE_RENDER */
1720
1721 static void
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,
1728                                 GdkColor         *text,
1729                                 GdkColor         *base)
1730 {
1731         PopplerPage *poppler_page;
1732         double width_points, height_points;
1733         gint width, height;
1734
1735         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1736
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);
1741
1742 #ifdef HAVE_POPPLER_PAGE_RENDER
1743         cairo_t *cr;
1744         PopplerColor text_color, base_color;
1745         
1746         text_color.red = text->red;
1747         text_color.green = text->green;
1748         text_color.blue = text->blue;
1749
1750         base_color.red = base->red;
1751         base_color.green = base->green;
1752         base_color.blue = base->blue;
1753
1754         if (*surface == NULL) {
1755                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1756                                                        width, height);
1757                 
1758         }
1759
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,
1767                                        cr,
1768                                        (PopplerRectangle *)points,
1769                                        (PopplerRectangle *)old_points,
1770                                        (PopplerSelectionStyle)style,
1771                                        &text_color,
1772                                        &base_color);
1773         cairo_destroy (cr);
1774 #else /* HAVE_POPPLER_PAGE_RENDER */
1775         GdkPixbuf *pixbuf;
1776         
1777         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1778                                  TRUE, 8,
1779                                  width, height);
1780
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,
1786                                                  text,
1787                                                  base);
1788         if (*surface)
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 */
1793 }
1794
1795 static gchar *
1796 pdf_selection_get_selected_text (EvSelection     *selection,
1797                                  EvRenderContext *rc,
1798                                  EvSelectionStyle style,
1799                                  EvRectangle     *points)
1800 {
1801         PopplerPage *poppler_page;
1802         PopplerRectangle r;
1803         double height;
1804         char *retval;
1805         
1806         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1807
1808         poppler_page_get_size (poppler_page, NULL, &height);
1809         r.x1 = points->x1;
1810         r.y1 = height - points->y2;
1811         r.x2 = points->x2;
1812         r.y2 = height - points->y1;
1813
1814         retval = poppler_page_get_text (poppler_page,
1815                                         (PopplerSelectionStyle)style,
1816                                         &r);
1817
1818         return retval;
1819 }
1820
1821 static GdkRegion *
1822 create_gdk_region_from_poppler_region (GList *region)
1823 {
1824         GList *l;
1825         GdkRegion *retval;
1826         
1827         retval = gdk_region_new ();
1828         
1829         for (l = region; l; l = g_list_next (l)) {
1830                 PopplerRectangle *rectangle;
1831                 GdkRectangle      rect;
1832                 
1833                 rectangle = (PopplerRectangle *)l->data;
1834                 
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);
1840                 
1841                 poppler_rectangle_free (rectangle);
1842         }
1843
1844         return retval;
1845 }
1846
1847 static GdkRegion *
1848 pdf_selection_get_selection_region (EvSelection     *selection,
1849                                     EvRenderContext *rc,
1850                                     EvSelectionStyle style,
1851                                     EvRectangle     *points)
1852 {
1853         PopplerPage *poppler_page;
1854         GdkRegion   *retval;
1855         GList       *region;
1856
1857         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1858         
1859         region = poppler_page_get_selection_region (poppler_page,
1860                                                     rc->scale,
1861                                                     (PopplerSelectionStyle)style,
1862                                                     (PopplerRectangle *) points);
1863         retval = create_gdk_region_from_poppler_region (region);
1864         g_list_free (region);
1865         
1866         return retval;
1867 }
1868
1869 static GdkRegion *
1870 pdf_selection_get_selection_map (EvSelection     *selection,
1871                                  EvRenderContext *rc)
1872 {
1873         PopplerPage *poppler_page;
1874         PopplerRectangle points;
1875         GList *region;
1876         GdkRegion *retval;
1877
1878         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1879
1880         points.x1 = 0.0;
1881         points.y1 = 0.0;
1882         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1883         
1884         region = poppler_page_get_selection_region (poppler_page, 1.0,
1885                                                     POPPLER_SELECTION_GLYPH,
1886                                                     &points);
1887         retval = create_gdk_region_from_poppler_region (region);
1888         g_list_free (region);
1889
1890         return retval;
1891 }
1892
1893 static void
1894 pdf_selection_iface_init (EvSelectionIface *iface)
1895 {
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;
1900 }
1901
1902 /* Page Transitions */
1903 static gdouble
1904 pdf_document_get_page_duration (EvDocumentTransition *trans,
1905                                 gint                  page)
1906 {
1907         PdfDocument *pdf_document;
1908         PopplerPage *poppler_page;
1909         gdouble      duration = -1;
1910
1911         pdf_document = PDF_DOCUMENT (trans);
1912         poppler_page = poppler_document_get_page (pdf_document->document, page);
1913         if (!poppler_page)
1914                 return -1;
1915
1916         duration = poppler_page_get_duration (poppler_page);
1917         g_object_unref (poppler_page);
1918
1919         return duration;
1920 }
1921
1922 static EvTransitionEffect *
1923 pdf_document_get_effect (EvDocumentTransition *trans,
1924                          gint                  page)
1925 {
1926         PdfDocument            *pdf_document;
1927         PopplerPage            *poppler_page;
1928         PopplerPageTransition  *page_transition;
1929         EvTransitionEffect     *effect;
1930
1931         pdf_document = PDF_DOCUMENT (trans);
1932         poppler_page = poppler_document_get_page (pdf_document->document, page);
1933
1934         if (!poppler_page)
1935                 return NULL;
1936
1937         page_transition = poppler_page_get_transition (poppler_page);
1938
1939         if (!page_transition) {
1940                 g_object_unref (poppler_page);
1941                 return NULL;
1942         }
1943
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,
1952                                            NULL);
1953
1954         poppler_page_transition_free (page_transition);
1955         g_object_unref (poppler_page);
1956
1957         return effect;
1958 }
1959
1960 static void
1961 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1962 {
1963         iface->get_page_duration = pdf_document_get_page_duration;
1964         iface->get_effect = pdf_document_get_effect;
1965 }
1966
1967 /* Forms */
1968 static void
1969 pdf_document_get_crop_box (EvDocument  *document, 
1970                            int          page, 
1971                            EvRectangle *rect)
1972 {
1973         PdfDocument *pdf_document;
1974         PopplerPage *poppler_page;
1975         PopplerRectangle poppler_rect;
1976
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;
1984 }
1985
1986 static EvFormField *
1987 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1988 {
1989         EvFormField *ev_field = NULL;
1990         gint         id;
1991         gdouble      font_size;
1992         gboolean     is_read_only;
1993
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);
1997
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;
2002
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;
2006                                         break;
2007                                 case POPPLER_FORM_TEXT_MULTILINE:
2008                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2009                                         break;
2010                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2011                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2012                                         break;
2013                         }
2014                         
2015                         ev_field = ev_form_field_text_new (id, ev_text_type);
2016                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2017
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);
2024
2025                 }
2026                         break;
2027                 case POPPLER_FORM_FIELD_BUTTON: {
2028                         EvFormFieldButton    *field_button;
2029                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2030
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;
2034                                         break;
2035                                 case POPPLER_FORM_BUTTON_CHECK:
2036                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2037                                         break;
2038                                 case POPPLER_FORM_BUTTON_RADIO:
2039                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2040                                         break;
2041                         }
2042
2043                         ev_field = ev_form_field_button_new (id, ev_button_type);
2044                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2045                         
2046                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2047                 }
2048                         break;
2049                 case POPPLER_FORM_FIELD_CHOICE: {
2050                         EvFormFieldChoice    *field_choice;
2051                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2052
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;
2056                                         break;
2057                                 case EV_FORM_FIELD_CHOICE_LIST:
2058                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2059                                         break;
2060                         }
2061
2062                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2063                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2064
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);
2069
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);
2074                 }
2075                         break;
2076                 case POPPLER_FORM_FIELD_SIGNATURE:
2077                         /* TODO */
2078                         ev_field = ev_form_field_signature_new (id);
2079                         break;
2080                 case POPPLER_FORM_FIELD_UNKNOWN:
2081                         return NULL;
2082         }
2083
2084         ev_field->font_size = font_size;
2085         ev_field->is_read_only = is_read_only;
2086
2087         return ev_field;
2088 }
2089
2090 static GList *
2091 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2092                                     EvPage          *page)
2093 {
2094         PopplerPage *poppler_page;
2095         GList *retval = NULL;
2096         GList *fields;
2097         GList *list;
2098         double height;
2099
2100         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2101         
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);
2105
2106         for (list = fields; list; list = list->next) {
2107                 PopplerFormFieldMapping *mapping;
2108                 EvMapping *field_mapping;
2109                 EvFormField *ev_field;
2110
2111                 mapping = (PopplerFormFieldMapping *)list->data;
2112
2113                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2114                 if (!ev_field)
2115                         continue;
2116
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));
2124
2125                 g_object_set_data_full (G_OBJECT (ev_field),
2126                                         "poppler-field",
2127                                         g_object_ref (mapping->field),
2128                                         (GDestroyNotify) g_object_unref);
2129                 
2130                 retval = g_list_prepend (retval, field_mapping);
2131         }
2132         
2133         poppler_page_free_form_field_mapping (fields);
2134
2135         return g_list_reverse (retval);
2136 }
2137
2138 static gchar *
2139 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2140                                              EvFormField     *field)
2141         
2142 {
2143         PopplerFormField *poppler_field;
2144         gchar *text;
2145
2146         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2147         if (!poppler_field)
2148                 return NULL;
2149         
2150         text = poppler_form_field_text_get_text (poppler_field);
2151
2152         return text;
2153 }
2154
2155 static void
2156 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2157                                              EvFormField     *field,
2158                                              const gchar     *text)
2159 {
2160         PopplerFormField *poppler_field;
2161
2162         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2163         if (!poppler_field)
2164                 return;
2165         
2166         poppler_form_field_text_set_text (poppler_field, text);
2167         PDF_DOCUMENT (document)->modified = TRUE;
2168 }
2169
2170 static void
2171 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2172                                                 EvFormField     *field,
2173                                                 gboolean         state)
2174 {
2175         PopplerFormField *poppler_field;
2176
2177         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2178         if (!poppler_field)
2179                 return;
2180         
2181         poppler_form_field_button_set_state (poppler_field, state);
2182         PDF_DOCUMENT (document)->modified = TRUE;
2183 }
2184
2185 static gboolean
2186 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2187                                                 EvFormField     *field)
2188 {
2189         PopplerFormField *poppler_field;
2190         gboolean state;
2191
2192         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2193         if (!poppler_field)
2194                 return FALSE;
2195
2196         state = poppler_form_field_button_get_state (poppler_field);
2197
2198         return state;
2199 }
2200
2201 static gchar *
2202 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2203                                                EvFormField     *field,
2204                                                gint             index)
2205 {
2206         PopplerFormField *poppler_field;
2207         gchar *text;
2208
2209         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2210         if (!poppler_field)
2211                 return NULL;
2212
2213         text = poppler_form_field_choice_get_item (poppler_field, index);
2214
2215         return text;
2216 }
2217
2218 static int
2219 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2220                                                   EvFormField     *field)
2221 {
2222         PopplerFormField *poppler_field;
2223         gint n_items;
2224
2225         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2226         if (!poppler_field)
2227                 return -1;
2228         
2229         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2230
2231         return n_items;
2232 }
2233
2234 static gboolean
2235 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2236                                                        EvFormField     *field,
2237                                                        gint             index)
2238 {
2239         PopplerFormField *poppler_field;
2240         gboolean selected;
2241
2242         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2243         if (!poppler_field)
2244                 return FALSE;
2245
2246         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2247
2248         return selected;
2249 }
2250
2251 static void
2252 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2253                                                   EvFormField     *field,
2254                                                   gint             index)
2255 {
2256         PopplerFormField *poppler_field;
2257
2258         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2259         if (!poppler_field)
2260                 return;
2261
2262         poppler_form_field_choice_select_item (poppler_field, index);
2263         PDF_DOCUMENT (document)->modified = TRUE;
2264 }
2265
2266 static void
2267 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2268                                                   EvFormField     *field,
2269                                                   gint             index)
2270 {
2271         PopplerFormField *poppler_field;
2272
2273         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2274         if (!poppler_field)
2275                 return;
2276
2277         poppler_form_field_choice_toggle_item (poppler_field, index);
2278         PDF_DOCUMENT (document)->modified = TRUE;
2279 }
2280
2281 static void
2282 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2283                                                    EvFormField     *field)
2284 {
2285         PopplerFormField *poppler_field;
2286
2287         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2288         if (!poppler_field)
2289                 return;
2290         
2291         poppler_form_field_choice_unselect_all (poppler_field);
2292         PDF_DOCUMENT (document)->modified = TRUE;
2293 }
2294
2295 static void
2296 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2297                                                EvFormField     *field,
2298                                                const gchar     *text)
2299 {
2300         PopplerFormField *poppler_field;
2301
2302         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2303         if (!poppler_field)
2304                 return;
2305         
2306         poppler_form_field_choice_set_text (poppler_field, text);
2307         PDF_DOCUMENT (document)->modified = TRUE;
2308 }
2309
2310 static gchar *
2311 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2312                                                EvFormField     *field)
2313 {
2314         PopplerFormField *poppler_field;
2315         gchar *text;
2316
2317         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2318         if (!poppler_field)
2319                 return NULL;
2320
2321         text = poppler_form_field_choice_get_text (poppler_field);
2322
2323         return text;
2324 }
2325
2326 static void
2327 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2328 {
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;
2342 }
2343
2344 /* Annotations */
2345 static void
2346 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2347                                   GdkColor     *color)
2348 {
2349         PopplerColor *poppler_color;
2350
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;
2356
2357                 g_free (poppler_color);
2358         } /* TODO: else use a default color */
2359 }
2360
2361 static EvAnnotation *
2362 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2363                              EvPage       *page)
2364 {
2365         EvAnnotation *ev_annot = NULL;
2366         const gchar  *unimplemented_annot = NULL;
2367
2368         switch (poppler_annot_get_annot_type (poppler_annot)) {
2369                 case POPPLER_ANNOT_TEXT:
2370                         PopplerAnnotText *poppler_text;
2371                         EvAnnotationText *ev_annot_text;
2372
2373                         poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2374
2375                         ev_annot = ev_annotation_text_new (page);
2376
2377                         ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2378                         ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2379
2380                         break;
2381                 case POPPLER_ANNOT_LINK:
2382                 case POPPLER_ANNOT_WIDGET:
2383                         /* Ignore link and widgets annots since they are already handled */
2384                         break;
2385                 default: {
2386                         GEnumValue *enum_value;
2387
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";
2391                 }
2392         }
2393
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);
2399         }
2400
2401         if (ev_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);
2406
2407                 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2408                         PopplerAnnotMarkup *markup;
2409                         gchar *label;
2410                         gdouble opacity;
2411                         PopplerRectangle poppler_rect;
2412
2413                         markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2414
2415                         if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2416                                 EvRectangle ev_rect;
2417                                 gboolean is_open;
2418                                 gdouble height;
2419
2420                                 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2421                                                        NULL, &height);
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;
2426
2427                                 is_open = poppler_annot_markup_get_popup_is_open (markup);
2428
2429                                 g_object_set (ev_annot,
2430                                               "rectangle", &ev_rect,
2431                                               "is_open", is_open,
2432                                               "has_popup", TRUE,
2433                                               NULL);
2434                         } else {
2435                                 /* FIXME: Use poppler_annot_markup_has_popup() when
2436                                  * new poppler is released.
2437                                  */
2438                                 g_object_set (ev_annot,
2439                                               "has_popup", FALSE,
2440                                               NULL);
2441                         }
2442
2443                         label = poppler_annot_markup_get_label (markup);
2444                         opacity = poppler_annot_markup_get_opacity (markup);
2445
2446                         g_object_set (ev_annot,
2447                                       "label", label,
2448                                       "opacity", opacity,
2449                                       NULL);
2450
2451                         g_free (label);
2452                 }
2453         }
2454
2455         return ev_annot;
2456 }
2457
2458 static GList *
2459 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2460                                           EvPage                *page)
2461 {
2462         GList *retval = NULL;
2463         PdfDocument *pdf_document;
2464         PopplerPage *poppler_page;
2465         GList *annots;
2466         GList *list;
2467         gdouble height;
2468         gint i = 0;
2469
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);
2474
2475         for (list = annots; list; list = list->next) {
2476                 PopplerAnnotMapping *mapping;
2477                 EvMapping *annot_mapping;
2478                 EvAnnotation        *ev_annot;
2479
2480                 mapping = (PopplerAnnotMapping *)list->data;
2481
2482                 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2483                 if (!ev_annot)
2484                         continue;
2485
2486                 i++;
2487
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);
2491
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;
2498
2499                 g_object_set_data_full (G_OBJECT (ev_annot),
2500                                         "poppler-annot",
2501                                         g_object_ref (mapping->annot),
2502                                         (GDestroyNotify) g_object_unref);
2503
2504                 retval = g_list_prepend (retval, annot_mapping);
2505         }
2506
2507         poppler_page_free_annot_mapping (annots);
2508
2509         return g_list_reverse (retval);
2510 }
2511
2512 static void
2513 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2514                                                   EvAnnotation          *annot,
2515                                                   const gchar           *contents)
2516 {
2517         PopplerAnnot *poppler_annot;
2518
2519         poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2520         if (!poppler_annot)
2521                 return;
2522
2523         poppler_annot_set_contents (poppler_annot, contents);
2524         PDF_DOCUMENT (document)->modified = TRUE;
2525 }
2526
2527 static void
2528 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2529 {
2530         iface->get_annotations = pdf_document_annotations_get_annotations;
2531         iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2532 }
2533
2534 /* Attachments */
2535 struct SaveToBufferData {
2536         gchar *buffer;
2537         gsize len, max;
2538 };
2539
2540 static gboolean
2541 attachment_save_to_buffer_callback (const gchar  *buf,
2542                                     gsize         count,
2543                                     gpointer      user_data,
2544                                     GError      **error)
2545 {
2546         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
2547         gchar *new_buffer;
2548         gsize new_max;
2549
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);
2553
2554                 sdata->buffer = new_buffer;
2555                 sdata->max = new_max;
2556         }
2557
2558         memcpy (sdata->buffer + sdata->len, buf, count);
2559         sdata->len += count;
2560
2561         return TRUE;
2562 }
2563
2564 static gboolean
2565 attachment_save_to_buffer (PopplerAttachment  *attachment,
2566                            gchar             **buffer,
2567                            gsize              *buffer_size,
2568                            GError            **error)
2569 {
2570         static const gint initial_max = 1024;
2571         struct SaveToBufferData sdata;
2572
2573         *buffer = NULL;
2574         *buffer_size = 0;
2575
2576         sdata.buffer = (gchar *) g_malloc (initial_max);
2577         sdata.max = initial_max;
2578         sdata.len = 0;
2579
2580         if (! poppler_attachment_save_to_callback (attachment,
2581                                                    attachment_save_to_buffer_callback,
2582                                                    &sdata,
2583                                                    error)) {
2584                 g_free (sdata.buffer);
2585                 return FALSE;
2586         }
2587
2588         *buffer = sdata.buffer;
2589         *buffer_size = sdata.len;
2590
2591         return TRUE;
2592 }
2593
2594 static GList *
2595 pdf_document_attachments_get_attachments (EvDocumentAttachments *document)
2596 {
2597         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2598         GList *attachments;
2599         GList *list;
2600         GList *retval = NULL;
2601
2602         attachments = poppler_document_get_attachments (pdf_document->document);
2603
2604         for (list = attachments; list; list = list->next) {
2605                 PopplerAttachment *attachment;
2606                 EvAttachment *ev_attachment;
2607                 gchar *data = NULL;
2608                 gsize size;
2609                 GError *error = NULL;
2610
2611                 attachment = (PopplerAttachment *) list->data;
2612
2613                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
2614                         ev_attachment = ev_attachment_new (attachment->name,
2615                                                            attachment->description,
2616                                                            attachment->mtime,
2617                                                            attachment->ctime,
2618                                                            size, data);
2619
2620                         retval = g_list_prepend (retval, ev_attachment);
2621                 } else {
2622                         if (error) {
2623                                 g_warning ("%s", error->message);
2624                                 g_error_free (error);
2625
2626                                 g_free (data);
2627                         }
2628                 }
2629
2630                 g_object_unref (attachment);
2631         }
2632
2633         return g_list_reverse (retval);
2634 }
2635
2636 static gboolean
2637 pdf_document_attachments_has_attachments (EvDocumentAttachments *document)
2638 {
2639         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2640
2641         return poppler_document_has_attachments (pdf_document->document);
2642 }
2643
2644 static void
2645 pdf_document_document_attachments_iface_init (EvDocumentAttachmentsIface *iface)
2646 {
2647         iface->has_attachments = pdf_document_attachments_has_attachments;
2648         iface->get_attachments = pdf_document_attachments_get_attachments;
2649 }
2650
2651 /* Layers */
2652 static gboolean
2653 pdf_document_layers_has_layers (EvDocumentLayers *document)
2654 {
2655         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2656         PopplerLayersIter *iter;
2657
2658         iter = poppler_layers_iter_new (pdf_document->document);
2659         if (!iter)
2660                 return FALSE;
2661         poppler_layers_iter_free (iter);
2662
2663         return TRUE;
2664 }
2665
2666 static void
2667 build_layers_tree (PdfDocument       *pdf_document,
2668                    GtkTreeModel      *model,
2669                    GtkTreeIter       *parent,
2670                    PopplerLayersIter *iter)
2671 {
2672         do {
2673                 GtkTreeIter        tree_iter;
2674                 PopplerLayersIter *child;
2675                 PopplerLayer      *layer;
2676                 EvLayer           *ev_layer = NULL;
2677                 gboolean           visible;
2678                 gchar             *markup;
2679                 gint               rb_group = 0;
2680
2681                 layer = poppler_layers_iter_get_layer (iter);
2682                 if (layer) {
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),
2690                                                  rb_group);
2691                 } else {
2692                         gchar *title;
2693
2694                         title = poppler_layers_iter_get_title (iter);
2695                         markup = g_markup_escape_text (title, -1);
2696                         g_free (title);
2697
2698                         visible = FALSE;
2699                         layer = NULL;
2700                 }
2701
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,
2710                                     -1);
2711                 if (ev_layer)
2712                         g_object_unref (ev_layer);
2713                 g_free (markup);
2714
2715                 child = poppler_layers_iter_get_child (iter);
2716                 if (child)
2717                         build_layers_tree (pdf_document, model, &tree_iter, child);
2718                 poppler_layers_iter_free (child);
2719         } while (poppler_layers_iter_next (iter));
2720 }
2721
2722 static GtkTreeModel *
2723 pdf_document_layers_get_layers (EvDocumentLayers *document)
2724 {
2725         GtkTreeModel *model = NULL;
2726         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2727         PopplerLayersIter *iter;
2728
2729         iter = poppler_layers_iter_new (pdf_document->document);
2730         if (iter) {
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);
2740         }
2741         return model;
2742 }
2743
2744 static void
2745 pdf_document_layers_show_layer (EvDocumentLayers *document,
2746                                 EvLayer          *layer)
2747 {
2748         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2749         guint        layer_id = ev_layer_get_id (layer);
2750
2751         poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2752 }
2753
2754 static void
2755 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2756                                 EvLayer          *layer)
2757 {
2758         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2759         guint        layer_id = ev_layer_get_id (layer);
2760
2761         poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2762 }
2763
2764 static gboolean
2765 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2766                                       EvLayer          *layer)
2767 {
2768         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2769         guint        layer_id = ev_layer_get_id (layer);
2770
2771         return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2772 }
2773
2774 static void
2775 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2776 {
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;
2782 }