]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
Use poppler_document_save_a_copy(), which never saves the document
[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-document-find.h"
39 #include "ev-document-misc.h"
40 #include "ev-document-links.h"
41 #include "ev-document-images.h"
42 #include "ev-document-fonts.h"
43 #include "ev-document-security.h"
44 #include "ev-document-thumbnails.h"
45 #include "ev-document-transition.h"
46 #include "ev-document-forms.h"
47 #include "ev-document-layers.h"
48 #include "ev-selection.h"
49 #include "ev-transition-effect.h"
50 #include "ev-attachment.h"
51 #include "ev-image.h"
52
53 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
54 #define HAVE_CAIRO_PRINT
55 #endif
56
57 typedef struct {
58         PdfDocument *document;
59         char *text;
60         GList **pages;
61         guint idle;
62         int start_page;
63         int search_page;
64 } PdfDocumentSearch;
65
66 typedef struct {
67         EvFileExporterFormat format;
68
69         /* Pages per sheet */
70         gint pages_per_sheet;
71         gint pages_printed;
72         gint pages_x;
73         gint pages_y;
74         gdouble paper_width;
75         gdouble paper_height;
76         
77 #ifdef HAVE_CAIRO_PRINT
78         cairo_t *cr;
79 #else
80         PopplerPSFile *ps_file;
81 #endif
82 } PdfPrintContext;
83
84 struct _PdfDocumentClass
85 {
86         GObjectClass parent_class;
87 };
88
89 struct _PdfDocument
90 {
91         GObject parent_instance;
92
93         PopplerDocument *document;
94         gchar *password;
95         gboolean modified;
96
97         PopplerFontInfo *font_info;
98         PopplerFontsIter *fonts_iter;
99         int fonts_scanned_pages;
100
101         PdfDocumentSearch *search;
102         PdfPrintContext *print_ctx;
103
104         GList *layers;
105 };
106
107 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
108 static void pdf_document_security_iface_init            (EvDocumentSecurityIface   *iface);
109 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
110 static void pdf_document_document_links_iface_init      (EvDocumentLinksIface      *iface);
111 static void pdf_document_document_images_iface_init     (EvDocumentImagesIface     *iface);
112 static void pdf_document_document_forms_iface_init      (EvDocumentFormsIface      *iface);
113 static void pdf_document_document_fonts_iface_init      (EvDocumentFontsIface      *iface);
114 static void pdf_document_document_layers_iface_init     (EvDocumentLayersIface     *iface);
115 static void pdf_document_find_iface_init                (EvDocumentFindIface       *iface);
116 static void pdf_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
117 static void pdf_selection_iface_init                    (EvSelectionIface          *iface);
118 static void pdf_document_page_transition_iface_init     (EvDocumentTransitionIface *iface);
119 static void pdf_document_thumbnails_get_dimensions      (EvDocumentThumbnails      *document_thumbnails,
120                                                          EvRenderContext           *rc,
121                                                          gint                      *width,
122                                                          gint                      *height);
123 static int  pdf_document_get_n_pages                    (EvDocument                *document);
124
125 static EvLinkDest *ev_link_dest_from_dest   (PdfDocument       *pdf_document,
126                                              PopplerDest       *dest);
127 static EvLink     *ev_link_from_action      (PdfDocument       *pdf_document,
128                                              PopplerAction     *action);
129 static void        pdf_document_search_free (PdfDocumentSearch *search);
130 static void        pdf_print_context_free   (PdfPrintContext   *ctx);
131
132 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
133                          {
134                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
135                                                                  pdf_document_security_iface_init);
136                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
137                                                                  pdf_document_document_thumbnails_iface_init);
138                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
139                                                                  pdf_document_document_links_iface_init);
140                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
141                                                                  pdf_document_document_images_iface_init);
142                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
143                                                                  pdf_document_document_forms_iface_init);
144                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
145                                                                  pdf_document_document_fonts_iface_init);
146                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
147                                                                  pdf_document_document_layers_iface_init);
148                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
149                                                                  pdf_document_find_iface_init);
150                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
151                                                                  pdf_document_file_exporter_iface_init);
152                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
153                                                                  pdf_selection_iface_init);
154                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
155                                                                  pdf_document_page_transition_iface_init);
156                          });
157
158 static void
159 pdf_document_search_free (PdfDocumentSearch   *search)
160 {
161         PdfDocument *pdf_document = search->document;
162         int n_pages;
163         int i;
164
165         if (search->idle != 0)
166                 g_source_remove (search->idle);
167
168         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
169         for (i = 0; i < n_pages; i++) {
170                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
171                 g_list_free (search->pages[i]);
172         }
173         g_free (search->pages);
174         
175         g_free (search->text);
176         g_free (search);
177 }
178
179 static void
180 pdf_document_dispose (GObject *object)
181 {
182         PdfDocument *pdf_document = PDF_DOCUMENT(object);
183
184         if (pdf_document->print_ctx) {
185                 pdf_print_context_free (pdf_document->print_ctx);
186                 pdf_document->print_ctx = NULL;
187         }
188         
189         if (pdf_document->search) {
190                 pdf_document_search_free (pdf_document->search);
191                 pdf_document->search = NULL;
192         }
193
194         if (pdf_document->document) {
195                 g_object_unref (pdf_document->document);
196         }
197
198         if (pdf_document->font_info) { 
199                 poppler_font_info_free (pdf_document->font_info);
200         }
201
202         if (pdf_document->fonts_iter) {
203                 poppler_fonts_iter_free (pdf_document->fonts_iter);
204         }
205
206         if (pdf_document->layers) {
207                 g_list_foreach (pdf_document->layers, (GFunc)g_object_unref, NULL);
208                 g_list_free (pdf_document->layers);
209         }
210
211         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
212 }
213
214 static void
215 pdf_document_class_init (PdfDocumentClass *klass)
216 {
217         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
218
219         g_object_class->dispose = pdf_document_dispose;
220 }
221
222 static void
223 pdf_document_init (PdfDocument *pdf_document)
224 {
225         pdf_document->password = NULL;
226 }
227
228 static void
229 convert_error (GError  *poppler_error,
230                GError **error)
231 {
232         if (poppler_error == NULL)
233                 return;
234
235         if (poppler_error->domain == POPPLER_ERROR) {
236                 /* convert poppler errors into EvDocument errors */
237                 gint code = EV_DOCUMENT_ERROR_INVALID;
238                 if (poppler_error->code == POPPLER_ERROR_INVALID)
239                         code = EV_DOCUMENT_ERROR_INVALID;
240                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
241                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
242                         
243                 g_set_error_literal (error,
244                                      EV_DOCUMENT_ERROR,
245                                      code,
246                                      poppler_error->message);
247
248                 g_error_free (poppler_error);
249         } else {
250                 g_propagate_error (error, poppler_error);
251         }
252 }
253
254
255 /* EvDocument */
256 static gboolean
257 pdf_document_save (EvDocument  *document,
258                    const char  *uri,
259                    GError     **error)
260 {
261         PdfDocument *pdf_document = PDF_DOCUMENT (document);
262         gboolean retval;
263         GError *poppler_error = NULL;
264
265         if (pdf_document->modified) {
266                 retval = poppler_document_save (pdf_document->document,
267                                                 uri, &poppler_error);
268         } else {
269                 retval = poppler_document_save_a_copy (pdf_document->document,
270                                                        uri, &poppler_error);
271         }
272                                                        
273         if (! retval)
274                 convert_error (poppler_error, error);
275
276         return retval;
277 }
278
279 static gboolean
280 pdf_document_load (EvDocument   *document,
281                    const char   *uri,
282                    GError      **error)
283 {
284         GError *poppler_error = NULL;
285         PdfDocument *pdf_document = PDF_DOCUMENT (document);
286
287         pdf_document->document =
288                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
289
290         if (pdf_document->document == NULL) {
291                 convert_error (poppler_error, error);
292                 return FALSE;
293         }
294
295         return TRUE;
296 }
297
298 static int
299 pdf_document_get_n_pages (EvDocument *document)
300 {
301         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
302 }
303
304 static EvPage *
305 pdf_document_get_page (EvDocument *document,
306                        gint        index)
307 {
308         PdfDocument *pdf_document = PDF_DOCUMENT (document);
309         PopplerPage *poppler_page;
310         EvPage      *page;
311
312         poppler_page = poppler_document_get_page (pdf_document->document, index);
313         page = ev_page_new (index);
314         page->backend_page = (EvBackendPage)g_object_ref (poppler_page);
315         page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
316         g_object_unref (poppler_page);
317
318         return page;
319 }
320
321 static void
322 pdf_document_get_page_size (EvDocument *document,
323                             EvPage     *page,
324                             double     *width,
325                             double     *height)
326 {
327         g_return_if_fail (POPPLER_IS_PAGE (page->backend_page));
328         
329         poppler_page_get_size (POPPLER_PAGE (page->backend_page), width, height);
330 }
331
332 static char *
333 pdf_document_get_page_label (EvDocument *document,
334                              EvPage     *page)
335 {
336         char *label = NULL;
337
338         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
339
340         g_object_get (G_OBJECT (page->backend_page),
341                       "label", &label,
342                       NULL);
343         return label;
344 }
345
346 static gboolean
347 pdf_document_has_attachments (EvDocument *document)
348 {
349         PdfDocument *pdf_document;
350
351         pdf_document = PDF_DOCUMENT (document);
352
353         return poppler_document_has_attachments (pdf_document->document);
354 }
355
356 struct SaveToBufferData {
357         gchar *buffer;
358         gsize len, max;
359 };
360
361 static gboolean
362 attachment_save_to_buffer_callback (const gchar  *buf,
363                                     gsize         count,
364                                     gpointer      user_data,
365                                     GError      **error)
366 {
367         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
368         gchar *new_buffer;
369         gsize new_max;
370
371         if (sdata->len + count > sdata->max) {
372                 new_max = MAX (sdata->max * 2, sdata->len + count);
373                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
374
375                 sdata->buffer = new_buffer;
376                 sdata->max = new_max;
377         }
378         
379         memcpy (sdata->buffer + sdata->len, buf, count);
380         sdata->len += count;
381         
382         return TRUE;
383 }
384
385 static gboolean
386 attachment_save_to_buffer (PopplerAttachment  *attachment,
387                            gchar             **buffer,
388                            gsize              *buffer_size,
389                            GError            **error)
390 {
391         static const gint initial_max = 1024;
392         struct SaveToBufferData sdata;
393
394         *buffer = NULL;
395         *buffer_size = 0;
396
397         sdata.buffer = (gchar *) g_malloc (initial_max);
398         sdata.max = initial_max;
399         sdata.len = 0;
400
401         if (! poppler_attachment_save_to_callback (attachment,
402                                                    attachment_save_to_buffer_callback,
403                                                    &sdata,
404                                                    error)) {
405                 g_free (sdata.buffer);
406                 return FALSE;
407         }
408
409         *buffer = sdata.buffer;
410         *buffer_size = sdata.len;
411         
412         return TRUE;
413 }
414
415 static GList *
416 pdf_document_get_attachments (EvDocument *document)
417 {
418         PdfDocument *pdf_document;
419         GList *attachments;
420         GList *list;
421         GList *retval = NULL;
422
423         pdf_document = PDF_DOCUMENT (document);
424
425         if (!pdf_document_has_attachments (document))
426                 return NULL;
427
428         attachments = poppler_document_get_attachments (pdf_document->document);
429         
430         for (list = attachments; list; list = list->next) {
431                 PopplerAttachment *attachment;
432                 EvAttachment *ev_attachment;
433                 gchar *data = NULL;
434                 gsize size;
435                 GError *error = NULL;
436
437                 attachment = (PopplerAttachment *) list->data;
438
439                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
440                         ev_attachment = ev_attachment_new (attachment->name,
441                                                            attachment->description,
442                                                            attachment->mtime,
443                                                            attachment->ctime,
444                                                            size, data);
445                         
446                         retval = g_list_prepend (retval, ev_attachment);
447                 } else {
448                         if (error) {
449                                 g_warning ("%s", error->message);
450                                 g_error_free (error);
451
452                                 g_free (data);
453                         }
454                 }
455
456                 g_object_unref (attachment);
457         }
458
459         return g_list_reverse (retval);
460 }
461
462 static cairo_surface_t *
463 pdf_page_render (PopplerPage     *page,
464                  gint             width,
465                  gint             height,
466                  EvRenderContext *rc)
467 {
468         cairo_surface_t *surface;
469
470 #ifdef HAVE_POPPLER_PAGE_RENDER
471         cairo_t *cr;
472         
473         surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
474                                               width, height);
475         memset (cairo_image_surface_get_data (surface), 0xff,
476                 cairo_image_surface_get_height (surface) *
477                 cairo_image_surface_get_stride (surface));
478         
479         cr = cairo_create (surface);
480         switch (rc->rotation) {
481                 case 90:
482                         cairo_translate (cr, width, 0);
483                         break;
484                 case 180:
485                         cairo_translate (cr, width, height);
486                         break;
487                 case 270:
488                         cairo_translate (cr, 0, height);
489                         break;
490                 default:
491                         cairo_translate (cr, 0, 0);
492         }
493         cairo_scale (cr, rc->scale, rc->scale);
494         cairo_rotate (cr, rc->rotation * G_PI / 180.0);
495         poppler_page_render (page, cr);
496         cairo_destroy (cr);
497 #else /* HAVE_POPPLER_PAGE_RENDER */
498         GdkPixbuf *pixbuf;
499         
500         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
501                                  FALSE, 8,
502                                  width, height);
503
504         poppler_page_render_to_pixbuf (page,
505                                        0, 0,
506                                        width, height,
507                                        rc->scale,
508                                        rc->rotation,
509                                        pixbuf);
510         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
511         g_object_unref (pixbuf);
512 #endif /* HAVE_POPPLER_PAGE_RENDER */
513
514         return surface; 
515 }
516
517 static cairo_surface_t *
518 pdf_document_render (EvDocument      *document,
519                      EvRenderContext *rc)
520 {
521         PdfDocument *pdf_document;
522         PopplerPage *poppler_page;
523         double width_points, height_points;
524         gint width, height;
525
526         poppler_page = POPPLER_PAGE (rc->page->backend_page);
527
528         poppler_page_get_size (poppler_page,
529                                &width_points, &height_points);
530         
531         if (rc->rotation == 90 || rc->rotation == 270) {
532                 width = (int) ((height_points * rc->scale) + 0.5);
533                 height = (int) ((width_points * rc->scale) + 0.5);
534         } else {
535                 width = (int) ((width_points * rc->scale) + 0.5);
536                 height = (int) ((height_points * rc->scale) + 0.5);
537         }
538         
539         return pdf_page_render (poppler_page,
540                                 width, height, rc);
541 }
542
543 /* EvDocumentSecurity */
544
545 static gboolean
546 pdf_document_has_document_security (EvDocumentSecurity *document_security)
547 {
548         /* FIXME: do we really need to have this? */
549         return FALSE;
550 }
551
552 static void
553 pdf_document_set_password (EvDocumentSecurity *document_security,
554                            const char         *password)
555 {
556         PdfDocument *document = PDF_DOCUMENT (document_security);
557
558         if (document->password)
559                 g_free (document->password);
560
561         document->password = g_strdup (password);
562 }
563
564 static EvDocumentInfo *
565 pdf_document_get_info (EvDocument *document)
566 {
567         EvDocumentInfo *info;
568         PopplerPageLayout layout;
569         PopplerPageMode mode;
570         PopplerViewerPreferences view_prefs;
571         PopplerPermissions permissions;
572         EvPage *page;
573
574         info = g_new0 (EvDocumentInfo, 1);
575
576         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
577                             EV_DOCUMENT_INFO_FORMAT |
578                             EV_DOCUMENT_INFO_AUTHOR |
579                             EV_DOCUMENT_INFO_SUBJECT |
580                             EV_DOCUMENT_INFO_KEYWORDS |
581                             EV_DOCUMENT_INFO_LAYOUT |
582                             EV_DOCUMENT_INFO_START_MODE |
583                             EV_DOCUMENT_INFO_PERMISSIONS |
584                             EV_DOCUMENT_INFO_UI_HINTS |
585                             EV_DOCUMENT_INFO_CREATOR |
586                             EV_DOCUMENT_INFO_PRODUCER |
587                             EV_DOCUMENT_INFO_CREATION_DATE |
588                             EV_DOCUMENT_INFO_MOD_DATE |
589                             EV_DOCUMENT_INFO_LINEARIZED |
590                             EV_DOCUMENT_INFO_N_PAGES |
591                             EV_DOCUMENT_INFO_SECURITY | 
592                             EV_DOCUMENT_INFO_PAPER_SIZE;
593
594         g_object_get (PDF_DOCUMENT (document)->document,
595                       "title", &(info->title),
596                       "format", &(info->format),
597                       "author", &(info->author),
598                       "subject", &(info->subject),
599                       "keywords", &(info->keywords),
600                       "page-mode", &mode,
601                       "page-layout", &layout,
602                       "viewer-preferences", &view_prefs,
603                       "permissions", &permissions,
604                       "creator", &(info->creator),
605                       "producer", &(info->producer),
606                       "creation-date", &(info->creation_date),
607                       "mod-date", &(info->modified_date),
608                       "linearized", &(info->linearized),
609                       NULL);
610
611         info->n_pages = ev_document_get_n_pages (document);
612
613         if (info->n_pages > 0) {
614                 page = ev_document_get_page (document, 0);
615                 ev_document_get_page_size (document, page,
616                                            &(info->paper_width),
617                                            &(info->paper_height));
618                 g_object_unref (page);
619                 
620
621                 // Convert to mm.
622                 info->paper_width = info->paper_width / 72.0f * 25.4f;
623                 info->paper_height = info->paper_height / 72.0f * 25.4f;
624         }
625
626         switch (layout) {
627                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
628                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
629                         break;
630                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
631                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
632                         break;
633                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
634                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
635                         break;
636                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
637                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
638                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
639                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
640                         break;
641                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
642                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
643                         break;
644                 default:
645                         break;
646         }
647
648         switch (mode) {
649                 case POPPLER_PAGE_MODE_NONE:
650                         info->mode = EV_DOCUMENT_MODE_NONE;
651                         break;
652                 case POPPLER_PAGE_MODE_USE_THUMBS:
653                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
654                         break;
655                 case POPPLER_PAGE_MODE_USE_OC:
656                         info->mode = EV_DOCUMENT_MODE_USE_OC;
657                         break;
658                 case POPPLER_PAGE_MODE_FULL_SCREEN:
659                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
660                         break;
661                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
662                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
663                 default:
664                         break;
665         }
666
667         info->ui_hints = 0;
668         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
669                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
670         }
671         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
672                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
673         }
674         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
675                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
676         }
677         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
678                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
679         }
680         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
681                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
682         }
683         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
684                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
685         }
686         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
687                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
688         }
689
690         info->permissions = 0;
691         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
692                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
693         }
694         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
695                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
696         }
697         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
698                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
699         }
700         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
701                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
702         }
703
704         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
705                 /* translators: this is the document security state */
706                 info->security = g_strdup (_("Yes"));
707         } else {
708                 /* translators: this is the document security state */
709                 info->security = g_strdup (_("No"));
710         }
711
712         return info;
713 }
714
715 static void
716 pdf_document_document_iface_init (EvDocumentIface *iface)
717 {
718         iface->save = pdf_document_save;
719         iface->load = pdf_document_load;
720         iface->get_n_pages = pdf_document_get_n_pages;
721         iface->get_page = pdf_document_get_page;
722         iface->get_page_size = pdf_document_get_page_size;
723         iface->get_page_label = pdf_document_get_page_label;
724         iface->has_attachments = pdf_document_has_attachments;
725         iface->get_attachments = pdf_document_get_attachments;
726         iface->render = pdf_document_render;
727         iface->get_info = pdf_document_get_info;
728 };
729
730 static void
731 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
732 {
733         iface->has_document_security = pdf_document_has_document_security;
734         iface->set_password = pdf_document_set_password;
735 }
736
737 static gdouble
738 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
739 {
740         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
741         int n_pages;
742
743         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
744
745         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
746 }
747
748 static gboolean
749 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
750                          int              n_pages)
751 {
752         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
753         gboolean result;
754
755         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
756
757         if (pdf_document->font_info == NULL) { 
758                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
759         }
760
761         if (pdf_document->fonts_iter) {
762                 poppler_fonts_iter_free (pdf_document->fonts_iter);
763         }
764
765         pdf_document->fonts_scanned_pages += n_pages;
766
767         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
768                                          &pdf_document->fonts_iter);
769         if (!result) {
770                 pdf_document->fonts_scanned_pages = 0;
771                 poppler_font_info_free (pdf_document->font_info);
772                 pdf_document->font_info = NULL; 
773         }
774
775         return result;
776 }
777
778 static const char *
779 font_type_to_string (PopplerFontType type)
780 {
781         switch (type) {
782                 case POPPLER_FONT_TYPE_TYPE1:
783                         return _("Type 1");
784                 case POPPLER_FONT_TYPE_TYPE1C:
785                         return _("Type 1C");
786                 case POPPLER_FONT_TYPE_TYPE3:
787                         return _("Type 3");
788                 case POPPLER_FONT_TYPE_TRUETYPE:
789                         return _("TrueType");
790                 case POPPLER_FONT_TYPE_CID_TYPE0:
791                         return _("Type 1 (CID)");
792                 case POPPLER_FONT_TYPE_CID_TYPE0C:
793                         return _("Type 1C (CID)");
794                 case POPPLER_FONT_TYPE_CID_TYPE2:
795                         return _("TrueType (CID)");
796                 default:
797                         return _("Unknown font type");
798         }
799 }
800
801 static void
802 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
803                                GtkTreeModel    *model)
804 {
805         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
806         PopplerFontsIter *iter = pdf_document->fonts_iter;
807
808         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
809
810         if (!iter)
811                 return;
812
813         do {
814                 GtkTreeIter list_iter;
815                 const char *name;
816                 const char *type;
817                 const char *embedded;
818                 char *details;
819                 
820                 name = poppler_fonts_iter_get_name (iter);
821
822                 if (name == NULL) {
823                         name = _("No name");
824                 }
825
826                 type = font_type_to_string (
827                         poppler_fonts_iter_get_font_type (iter));
828
829                 if (poppler_fonts_iter_is_embedded (iter)) {
830                         if (poppler_fonts_iter_is_subset (iter))
831                                 embedded = _("Embedded subset");
832                         else
833                                 embedded = _("Embedded");
834                 } else {
835                         embedded = _("Not embedded");
836                 }
837
838                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
839
840                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
841                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
842                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
843                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
844                                     -1);
845
846                 g_free (details);
847         } while (poppler_fonts_iter_next (iter));
848 }
849
850 static void
851 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
852 {
853         iface->fill_model = pdf_document_fonts_fill_model;
854         iface->scan = pdf_document_fonts_scan;
855         iface->get_progress = pdf_document_fonts_get_progress;
856 }
857
858 static gboolean
859 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
860 {
861         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
862         PopplerIndexIter *iter;
863
864         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
865
866         iter = poppler_index_iter_new (pdf_document->document);
867         if (iter == NULL)
868                 return FALSE;
869         poppler_index_iter_free (iter);
870
871         return TRUE;
872 }
873
874 static EvLinkDest *
875 ev_link_dest_from_dest (PdfDocument *pdf_document,
876                         PopplerDest *dest)
877 {
878         EvLinkDest *ev_dest = NULL;
879         const char *unimplemented_dest = NULL;
880
881         g_assert (dest != NULL);
882
883         switch (dest->type) {
884                 case POPPLER_DEST_XYZ: {
885                         PopplerPage *poppler_page;
886                         double height;
887
888                         poppler_page = poppler_document_get_page (pdf_document->document,
889                                                                   MAX (0, dest->page_num - 1));
890                         poppler_page_get_size (poppler_page, NULL, &height);
891                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
892                                                         dest->left,
893                                                         height - MIN (height, dest->top),
894                                                         dest->zoom,
895                                                         dest->change_left,
896                                                         dest->change_top,
897                                                         dest->change_zoom);
898                         g_object_unref (poppler_page);
899                 }
900                         break;
901                 case POPPLER_DEST_FIT:
902                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
903                         break;
904                 case POPPLER_DEST_FITH: {
905                         PopplerPage *poppler_page;
906                         double height;
907
908                         poppler_page = poppler_document_get_page (pdf_document->document,
909                                                                   MAX (0, dest->page_num - 1));
910                         poppler_page_get_size (poppler_page, NULL, &height);
911                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
912                                                          height - MIN (height, dest->top),
913                                                          dest->change_top);
914                         g_object_unref (poppler_page);
915                 }
916                         break;
917                 case POPPLER_DEST_FITV:
918                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
919                                                          dest->left,
920                                                          dest->change_left);
921                         break;
922                 case POPPLER_DEST_FITR: {
923                         PopplerPage *poppler_page;
924                         double height;
925
926                         poppler_page = poppler_document_get_page (pdf_document->document,
927                                                                   MAX (0, dest->page_num - 1));
928                         poppler_page_get_size (poppler_page, NULL, &height);
929                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
930                                                          dest->left,
931                                                          height - MIN (height, dest->bottom),
932                                                          dest->right,
933                                                          height - MIN (height, dest->top));
934                         g_object_unref (poppler_page);
935                 }
936                         break;
937                 case POPPLER_DEST_FITB:
938                         unimplemented_dest = "POPPLER_DEST_FITB";
939                         break;
940                 case POPPLER_DEST_FITBH:
941                         unimplemented_dest = "POPPLER_DEST_FITBH";
942                         break;
943                 case POPPLER_DEST_FITBV:
944                         unimplemented_dest = "POPPLER_DEST_FITBV";
945                         break;
946                 case POPPLER_DEST_NAMED:
947                         ev_dest = ev_link_dest_new_named (dest->named_dest);
948                         break;
949                 case POPPLER_DEST_UNKNOWN:
950                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
951                         break;
952         }
953
954         if (unimplemented_dest) {
955                 g_warning ("Unimplemented destination: %s, please post a "
956                            "bug report in Evince bugzilla "
957                            "(http://bugzilla.gnome.org) with a testcase.",
958                            unimplemented_dest);
959         }
960
961         if (!ev_dest)
962                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
963         
964         return ev_dest;
965 }
966
967 static EvLink *
968 ev_link_from_action (PdfDocument   *pdf_document,
969                      PopplerAction *action)
970 {
971         EvLink       *link = NULL;
972         EvLinkAction *ev_action = NULL;
973         const char   *unimplemented_action = NULL;
974
975         switch (action->type) {
976                 case POPPLER_ACTION_NONE:
977                         break;
978                 case POPPLER_ACTION_GOTO_DEST: {
979                         EvLinkDest *dest;
980                         
981                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
982                         ev_action = ev_link_action_new_dest (dest);
983                 }
984                         break;
985                 case POPPLER_ACTION_GOTO_REMOTE: {
986                         EvLinkDest *dest;
987                         
988                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
989                         ev_action = ev_link_action_new_remote (dest, 
990                                                                action->goto_remote.file_name);
991                         
992                 }
993                         break;
994                 case POPPLER_ACTION_LAUNCH:
995                         ev_action = ev_link_action_new_launch (action->launch.file_name,
996                                                                action->launch.params);
997                         break;
998                 case POPPLER_ACTION_URI:
999                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
1000                         break;
1001                 case POPPLER_ACTION_NAMED:
1002                         ev_action = ev_link_action_new_named (action->named.named_dest);
1003                         break;
1004                 case POPPLER_ACTION_MOVIE:
1005                         unimplemented_action = "POPPLER_ACTION_MOVIE";
1006                         break;
1007                 case POPPLER_ACTION_UNKNOWN:
1008                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
1009         }
1010         
1011         if (unimplemented_action) {
1012                 g_warning ("Unimplemented action: %s, please post a bug report "
1013                            "in Evince bugzilla (http://bugzilla.gnome.org) "
1014                            "with a testcase.", unimplemented_action);
1015         }
1016         
1017         link = ev_link_new (action->any.title, ev_action);
1018         
1019         return link;    
1020 }
1021
1022 static void
1023 build_tree (PdfDocument      *pdf_document,
1024             GtkTreeModel     *model,
1025             GtkTreeIter      *parent,
1026             PopplerIndexIter *iter)
1027 {
1028         
1029         do {
1030                 GtkTreeIter tree_iter;
1031                 PopplerIndexIter *child;
1032                 PopplerAction *action;
1033                 EvLink *link = NULL;
1034                 gboolean expand;
1035                 char *title_markup;
1036                 
1037                 action = poppler_index_iter_get_action (iter);
1038                 expand = poppler_index_iter_is_open (iter);
1039
1040                 if (!action)
1041                         continue;
1042
1043                 switch (action->type) {
1044                         case POPPLER_ACTION_GOTO_DEST: {
1045                                 /* For bookmarks, solve named destinations */
1046                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1047                                         PopplerDest *dest;
1048                                         EvLinkDest *ev_dest = NULL;
1049                                         EvLinkAction *ev_action;
1050                                         
1051                                         dest = poppler_document_find_dest (pdf_document->document,
1052                                                                            action->goto_dest.dest->named_dest);
1053                                         if (!dest) {
1054                                                 link = ev_link_from_action (pdf_document, action);
1055                                                 break;
1056                                         }
1057                                         
1058                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1059                                         poppler_dest_free (dest);
1060                                         
1061                                         ev_action = ev_link_action_new_dest (ev_dest);
1062                                         link = ev_link_new (action->any.title, ev_action);
1063                                 } else {
1064                                         link = ev_link_from_action (pdf_document, action);
1065                                 }
1066                         }
1067                                 break;
1068                         default:
1069                                 link = ev_link_from_action (pdf_document, action);
1070                                 break;
1071                 }
1072                 
1073                 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1074                         poppler_action_free (action);
1075                         if (link)
1076                                 g_object_unref (link);
1077                         
1078                         continue;
1079                 }
1080
1081                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1082                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1083                 
1084                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1085                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1086                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1087                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1088                                     -1);
1089                 
1090                 g_free (title_markup);
1091                 g_object_unref (link);
1092                 
1093                 child = poppler_index_iter_get_child (iter);
1094                 if (child)
1095                         build_tree (pdf_document, model, &tree_iter, child);
1096                 poppler_index_iter_free (child);
1097                 poppler_action_free (action);
1098                 
1099         } while (poppler_index_iter_next (iter));
1100 }
1101
1102 static GtkTreeModel *
1103 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1104 {
1105         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1106         GtkTreeModel *model = NULL;
1107         PopplerIndexIter *iter;
1108         
1109         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1110
1111         iter = poppler_index_iter_new (pdf_document->document);
1112         /* Create the model if we have items*/
1113         if (iter != NULL) {
1114                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1115                                                              G_TYPE_STRING,
1116                                                              G_TYPE_OBJECT,
1117                                                              G_TYPE_BOOLEAN,
1118                                                              G_TYPE_STRING);
1119                 build_tree (pdf_document, model, NULL, iter);
1120                 poppler_index_iter_free (iter);
1121         }
1122         
1123         return model;
1124 }
1125
1126 static GList *
1127 pdf_document_links_get_links (EvDocumentLinks *document_links,
1128                               gint             page)
1129 {
1130         PdfDocument *pdf_document;
1131         PopplerPage *poppler_page;
1132         GList *retval = NULL;
1133         GList *mapping_list;
1134         GList *list;
1135         double height;
1136
1137         pdf_document = PDF_DOCUMENT (document_links);
1138         poppler_page = poppler_document_get_page (pdf_document->document,
1139                                                   page);
1140         mapping_list = poppler_page_get_link_mapping (poppler_page);
1141         poppler_page_get_size (poppler_page, NULL, &height);
1142
1143         for (list = mapping_list; list; list = list->next) {
1144                 PopplerLinkMapping *link_mapping;
1145                 EvLinkMapping *ev_link_mapping;
1146
1147                 link_mapping = (PopplerLinkMapping *)list->data;
1148                 ev_link_mapping = g_new (EvLinkMapping, 1);
1149                 ev_link_mapping->link = ev_link_from_action (pdf_document,
1150                                                              link_mapping->action);
1151                 ev_link_mapping->x1 = link_mapping->area.x1;
1152                 ev_link_mapping->x2 = link_mapping->area.x2;
1153                 /* Invert this for X-style coordinates */
1154                 ev_link_mapping->y1 = height - link_mapping->area.y2;
1155                 ev_link_mapping->y2 = height - link_mapping->area.y1;
1156
1157                 retval = g_list_prepend (retval, ev_link_mapping);
1158         }
1159
1160         poppler_page_free_link_mapping (mapping_list);
1161         g_object_unref (poppler_page);
1162
1163         return g_list_reverse (retval);
1164 }
1165
1166 static EvLinkDest *
1167 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1168                                    const gchar      *link_name)
1169 {
1170         PdfDocument *pdf_document;
1171         PopplerDest *dest;
1172         EvLinkDest *ev_dest = NULL;
1173
1174         pdf_document = PDF_DOCUMENT (document_links);
1175         dest = poppler_document_find_dest (pdf_document->document,
1176                                            link_name);
1177         if (dest) {
1178                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1179                 poppler_dest_free (dest);
1180         }
1181
1182         return ev_dest;
1183 }
1184
1185 static void
1186 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1187 {
1188         iface->has_document_links = pdf_document_links_has_document_links;
1189         iface->get_links_model = pdf_document_links_get_links_model;
1190         iface->get_links = pdf_document_links_get_links;
1191         iface->find_link_dest = pdf_document_links_find_link_dest;
1192 }
1193
1194 static GList *
1195 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1196                                        gint              page)
1197 {
1198         GList *retval = NULL;
1199         PdfDocument *pdf_document;
1200         PopplerPage *poppler_page;
1201         GList *mapping_list;
1202         GList *list;
1203
1204         pdf_document = PDF_DOCUMENT (document_images);
1205         poppler_page = poppler_document_get_page (pdf_document->document, page);
1206         mapping_list = poppler_page_get_image_mapping (poppler_page);
1207
1208         for (list = mapping_list; list; list = list->next) {
1209                 PopplerImageMapping *image_mapping;
1210                 EvImageMapping *ev_image_mapping;
1211
1212                 image_mapping = (PopplerImageMapping *)list->data;
1213
1214                 ev_image_mapping = g_new (EvImageMapping, 1);
1215                 
1216                 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1217                 ev_image_mapping->x1 = image_mapping->area.x1;
1218                 ev_image_mapping->x2 = image_mapping->area.x2;
1219                 ev_image_mapping->y1 = image_mapping->area.y1;
1220                 ev_image_mapping->y2 = image_mapping->area.y2;
1221
1222                 retval = g_list_prepend (retval, ev_image_mapping);
1223         }
1224
1225         poppler_page_free_image_mapping (mapping_list);
1226         g_object_unref (poppler_page);
1227
1228         return g_list_reverse (retval);
1229 }
1230
1231 GdkPixbuf *
1232 pdf_document_images_get_image (EvDocumentImages *document_images,
1233                                EvImage          *image)
1234 {
1235         PdfDocument     *pdf_document;
1236         PopplerPage     *poppler_page;
1237         cairo_surface_t *surface;
1238         GdkPixbuf       *retval = NULL;
1239
1240         pdf_document = PDF_DOCUMENT (document_images);
1241         poppler_page = poppler_document_get_page (pdf_document->document,
1242                                                   ev_image_get_page (image));
1243
1244         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1245         if (surface) {
1246                 retval = ev_document_misc_pixbuf_from_surface (surface);
1247                 cairo_surface_destroy (surface);
1248         }
1249
1250         g_object_unref (poppler_page);
1251
1252         return retval;
1253 }
1254
1255 static void
1256 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1257 {
1258         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1259         iface->get_image = pdf_document_images_get_image;
1260 }
1261
1262 static GdkPixbuf *
1263 make_thumbnail_for_page (PdfDocument     *pdf_document,
1264                          PopplerPage     *poppler_page, 
1265                          EvRenderContext *rc)
1266 {
1267         GdkPixbuf *pixbuf;
1268         int width, height;
1269
1270         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1271                                                 rc, &width, &height);
1272 #ifdef POPPLER_WITH_GDK
1273         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1274                                  width, height);
1275         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1276
1277         ev_document_fc_mutex_lock ();
1278         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1279                                        width, height,
1280                                        rc->scale, rc->rotation, pixbuf);
1281         ev_document_fc_mutex_unlock ();
1282 #else
1283         cairo_surface_t *surface;
1284
1285         ev_document_fc_mutex_lock ();
1286         surface = pdf_page_render (poppler_page, width, height, rc);
1287         ev_document_fc_mutex_unlock ();
1288         
1289         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1290         cairo_surface_destroy (surface);
1291 #endif /* POPPLER_WITH_GDK */
1292
1293         return pixbuf;
1294 }
1295
1296 static GdkPixbuf *
1297 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1298                                        EvRenderContext      *rc, 
1299                                        gboolean              border)
1300 {
1301         PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1302         PopplerPage *poppler_page;
1303         GdkPixbuf *pixbuf = NULL;
1304         GdkPixbuf *border_pixbuf;
1305
1306         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1307
1308 #ifdef POPPLER_WITH_GDK
1309         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1310 #else
1311         cairo_surface_t *surface;
1312         
1313         surface = poppler_page_get_thumbnail (poppler_page);
1314         if (surface) {
1315                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1316                 cairo_surface_destroy (surface);
1317         }
1318 #endif /* POPPLER_WITH_GDK */
1319
1320         if (pixbuf) {
1321                 /* Rotate provided thumbnail if needed */
1322                 GdkPixbuf *rotated_pixbuf;
1323
1324                 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1325                                                            (GdkPixbufRotation) (360 - rc->rotation));
1326                 g_object_unref (pixbuf);
1327                 pixbuf = rotated_pixbuf;
1328         } else {
1329                 /* There is no provided thumbnail.  We need to make one. */
1330                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1331         }
1332
1333         if (border) {           
1334                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1335                 g_object_unref (pixbuf);
1336                 pixbuf = border_pixbuf;
1337         }               
1338
1339         return pixbuf;
1340 }
1341
1342 static void
1343 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1344                                         EvRenderContext      *rc,
1345                                         gint                 *width,
1346                                         gint                 *height)
1347 {
1348         PopplerPage *poppler_page;
1349         gint has_thumb;
1350         
1351         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1352
1353         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1354
1355         if (!has_thumb || *width <= 0 || *height <= 0) {
1356                 double page_width, page_height;
1357
1358                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1359
1360                 *width = (gint) MAX (page_width * rc->scale, 1);
1361                 *height = (gint) MAX (page_height * rc->scale, 1);
1362         }
1363         
1364         if (rc->rotation == 90 || rc->rotation == 270) {
1365                 gint  temp;
1366
1367                 temp = *width;
1368                 *width = *height;
1369                 *height = temp;
1370         }
1371 }
1372
1373 static void
1374 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1375 {
1376         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1377         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1378 }
1379
1380
1381 static GList *
1382 pdf_document_find_find_text (EvDocumentFind *document_find,
1383                              EvPage         *page,
1384                              const gchar    *text,
1385                              gboolean        case_sensitive)
1386 {
1387         GList *matches, *l;
1388         PopplerPage *poppler_page;
1389         gdouble height;
1390         
1391         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1392         g_return_val_if_fail (text != NULL, NULL);
1393
1394         poppler_page = POPPLER_PAGE (page->backend_page);
1395         
1396         matches = poppler_page_find_text (poppler_page, text);
1397         if (!matches)
1398                 return NULL;
1399
1400         poppler_page_get_size (poppler_page, NULL, &height);
1401         for (l = matches; l && l->data; l = g_list_next (l)) {
1402                 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1403                 gdouble           tmp;
1404
1405                 tmp = rect->y1;
1406                 rect->y1 = height - rect->y2;
1407                 rect->y2 = height - tmp;
1408         }
1409         
1410         return matches;
1411 }
1412
1413 static void
1414 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1415 {
1416         iface->find_text = pdf_document_find_find_text;
1417 }
1418
1419 static void
1420 pdf_print_context_free (PdfPrintContext *ctx)
1421 {
1422         if (!ctx)
1423                 return;
1424
1425 #ifdef HAVE_CAIRO_PRINT
1426         if (ctx->cr) {
1427                 cairo_destroy (ctx->cr);
1428                 ctx->cr = NULL;
1429         }
1430 #else
1431         if (ctx->ps_file) {
1432                 poppler_ps_file_free (ctx->ps_file);
1433                 ctx->ps_file = NULL;
1434         }
1435 #endif
1436         g_free (ctx);
1437 }
1438
1439 static void
1440 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1441                                   EvFileExporterContext *fc)
1442 {
1443         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1444         PdfPrintContext *ctx;
1445 #ifdef HAVE_CAIRO_PRINT
1446         gdouble width, height;
1447         cairo_surface_t *surface = NULL;
1448 #endif
1449         
1450         if (pdf_document->print_ctx)
1451                 pdf_print_context_free (pdf_document->print_ctx);
1452         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1453         ctx = pdf_document->print_ctx;
1454         ctx->format = fc->format;
1455         
1456 #ifdef HAVE_CAIRO_PRINT
1457         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1458
1459         ctx->paper_width = fc->paper_width;
1460         ctx->paper_height = fc->paper_height;
1461         
1462         switch (fc->pages_per_sheet) {
1463                 default:
1464                 case 1:
1465                         ctx->pages_x = 1;
1466                         ctx->pages_y = 1;
1467                         break;
1468                 case 2:
1469                         ctx->pages_x = 1;
1470                         ctx->pages_y = 2;
1471                         break;
1472                 case 4:
1473                         ctx->pages_x = 2;
1474                         ctx->pages_y = 2;
1475                         break;
1476                 case 6:
1477                         ctx->pages_x = 2;
1478                         ctx->pages_y = 3;
1479                         break;
1480                 case 9:
1481                         ctx->pages_x = 3;
1482                         ctx->pages_y = 3;
1483                         break;
1484                 case 16:
1485                         ctx->pages_x = 4;
1486                         ctx->pages_y = 4;
1487                         break;
1488         }
1489
1490         ctx->pages_printed = 0;
1491         
1492         switch (fc->format) {
1493                 case EV_FILE_FORMAT_PS:
1494 #ifdef HAVE_CAIRO_PS
1495                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1496 #endif
1497                         break;
1498                 case EV_FILE_FORMAT_PDF:
1499 #ifdef HAVE_CAIRO_PDF
1500                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1501 #endif
1502                         break;
1503                 default:
1504                         g_assert_not_reached ();
1505         }
1506
1507         ctx->cr = cairo_create (surface);
1508         cairo_surface_destroy (surface);
1509
1510 #else /* HAVE_CAIRO_PRINT */
1511         if (ctx->format == EV_FILE_FORMAT_PS) {
1512                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1513                                                     fc->filename, fc->first_page,
1514                                                     fc->last_page - fc->first_page + 1);
1515                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1516                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1517         }
1518 #endif /* HAVE_CAIRO_PRINT */
1519 }
1520
1521 static void
1522 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1523 {
1524         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1525         PdfPrintContext *ctx = pdf_document->print_ctx;
1526         
1527         g_return_if_fail (pdf_document->print_ctx != NULL);
1528
1529         ctx->pages_printed = 0;
1530         
1531 #ifdef HAVE_CAIRO_PRINT
1532         if (ctx->paper_width > ctx->paper_height) {
1533                 if (ctx->format == EV_FILE_FORMAT_PS) {
1534                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1535                                                    ctx->paper_height,
1536                                                    ctx->paper_width);
1537                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1538                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1539                                                     ctx->paper_height,
1540                                                     ctx->paper_width);
1541                 }
1542         }
1543 #endif /* HAVE_CAIRO_PRINT */
1544 }
1545
1546 static void
1547 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1548                                     EvRenderContext *rc)
1549 {
1550         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1551         PdfPrintContext *ctx = pdf_document->print_ctx;
1552         PopplerPage *poppler_page;
1553 #ifdef HAVE_CAIRO_PRINT
1554         gdouble  page_width, page_height;
1555         gint     x, y;
1556         gboolean rotate;
1557         gdouble  width, height;
1558         gdouble  pwidth, pheight;
1559         gdouble  xscale, yscale;
1560 #endif
1561
1562         g_return_if_fail (pdf_document->print_ctx != NULL);
1563
1564         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1565         
1566 #ifdef HAVE_CAIRO_PRINT
1567         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1568         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1569         poppler_page_get_size (poppler_page, &page_width, &page_height);
1570
1571         if (page_width > page_height && page_width > ctx->paper_width) {
1572                 rotate = TRUE;
1573         } else {
1574                 rotate = FALSE;
1575         }
1576
1577         /* Use always portrait mode and rotate when necessary */
1578         if (ctx->paper_width > ctx->paper_height) {
1579                 width = ctx->paper_height;
1580                 height = ctx->paper_width;
1581                 rotate = !rotate;
1582         } else {
1583                 width = ctx->paper_width;
1584                 height = ctx->paper_height;
1585         }
1586
1587         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1588                 rotate = !rotate;
1589         }       
1590
1591         if (rotate) {
1592                 gint tmp1;
1593                 gdouble tmp2;
1594
1595                 tmp1 = x;
1596                 x = y;
1597                 y = tmp1;
1598
1599                 tmp2 = page_width;
1600                 page_width = page_height;
1601                 page_height = tmp2;
1602         }
1603
1604         pwidth = width / ctx->pages_x;
1605         pheight = height / ctx->pages_y;
1606
1607         if ((page_width > pwidth || page_height > pheight) ||
1608             (page_width < pwidth && page_height < pheight)) {
1609                 xscale = pwidth / page_width;
1610                 yscale = pheight / page_height;
1611                 
1612                 if (yscale < xscale) {
1613                         xscale = yscale;
1614                 } else {
1615                         yscale = xscale;
1616                 }
1617                 
1618         } else {        
1619                 xscale = yscale = 1;
1620         }
1621
1622         /* TODO: center */
1623
1624         cairo_save (ctx->cr);
1625         if (rotate) {
1626                 cairo_matrix_t matrix;
1627                 
1628                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1629                 cairo_matrix_init (&matrix,
1630                                    0,  1,
1631                                    -1,  0,
1632                                    0,  0);
1633                 cairo_transform (ctx->cr, &matrix);
1634         }
1635         
1636         cairo_translate (ctx->cr,
1637                          x * (rotate ? pheight : pwidth),
1638                          y * (rotate ? pwidth : pheight));
1639         cairo_scale (ctx->cr, xscale, yscale);
1640
1641         poppler_page_render_for_printing (poppler_page, ctx->cr);
1642
1643         ctx->pages_printed++;
1644                         
1645         cairo_restore (ctx->cr);
1646 #else /* HAVE_CAIRO_PRINT */
1647         if (ctx->format == EV_FILE_FORMAT_PS)
1648                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1649 #endif /* HAVE_CAIRO_PRINT */
1650 }
1651
1652 static void
1653 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1654 {
1655         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1656         PdfPrintContext *ctx = pdf_document->print_ctx;
1657         
1658         g_return_if_fail (pdf_document->print_ctx != NULL);
1659
1660 #ifdef HAVE_CAIRO_PRINT
1661         cairo_show_page (ctx->cr);
1662 #endif
1663 }
1664
1665 static void
1666 pdf_document_file_exporter_end (EvFileExporter *exporter)
1667 {
1668         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1669
1670         pdf_print_context_free (pdf_document->print_ctx);
1671         pdf_document->print_ctx = NULL;
1672 }
1673
1674 static EvFileExporterCapabilities
1675 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1676 {
1677         return  (EvFileExporterCapabilities) (
1678                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1679                 EV_FILE_EXPORTER_CAN_COPIES |
1680                 EV_FILE_EXPORTER_CAN_COLLATE |
1681                 EV_FILE_EXPORTER_CAN_REVERSE |
1682                 EV_FILE_EXPORTER_CAN_SCALE |
1683 #ifdef HAVE_CAIRO_PRINT
1684 #ifdef HAVE_POPPLER_PAGE_RENDER
1685                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1686 #endif
1687 #endif
1688                 
1689 #ifdef HAVE_CAIRO_PDF
1690 #ifdef HAVE_POPPLER_PAGE_RENDER
1691                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1692 #endif
1693 #endif
1694                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1695 }
1696
1697 static void
1698 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1699 {
1700         iface->begin = pdf_document_file_exporter_begin;
1701         iface->begin_page = pdf_document_file_exporter_begin_page;
1702         iface->do_page = pdf_document_file_exporter_do_page;
1703         iface->end_page = pdf_document_file_exporter_end_page;
1704         iface->end = pdf_document_file_exporter_end;
1705         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1706 }
1707
1708 static void
1709 pdf_selection_render_selection (EvSelection      *selection,
1710                                 EvRenderContext  *rc,
1711                                 cairo_surface_t **surface,
1712                                 EvRectangle      *points,
1713                                 EvRectangle      *old_points,
1714                                 EvSelectionStyle  style,
1715                                 GdkColor         *text,
1716                                 GdkColor         *base)
1717 {
1718         PopplerPage *poppler_page;
1719         double width_points, height_points;
1720         gint width, height;
1721
1722         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1723
1724         poppler_page_get_size (poppler_page,
1725                                &width_points, &height_points);
1726         width = (int) ((width_points * rc->scale) + 0.5);
1727         height = (int) ((height_points * rc->scale) + 0.5);
1728
1729 #ifdef HAVE_POPPLER_PAGE_RENDER
1730         cairo_t *cr;
1731         PopplerColor text_color, base_color;
1732         
1733         text_color.red = text->red;
1734         text_color.green = text->green;
1735         text_color.blue = text->blue;
1736
1737         base_color.red = base->red;
1738         base_color.green = base->green;
1739         base_color.blue = base->blue;
1740
1741         if (*surface == NULL) {
1742                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1743                                                        width, height);
1744                 
1745         }
1746
1747         cr = cairo_create (*surface);
1748         cairo_scale (cr, rc->scale, rc->scale);
1749         cairo_surface_set_device_offset (*surface, 0, 0);
1750         memset (cairo_image_surface_get_data (*surface), 0x00,
1751                 cairo_image_surface_get_height (*surface) *
1752                 cairo_image_surface_get_stride (*surface));
1753         poppler_page_render_selection (poppler_page,
1754                                        cr,
1755                                        (PopplerRectangle *)points,
1756                                        (PopplerRectangle *)old_points,
1757                                        (PopplerSelectionStyle)style,
1758                                        &text_color,
1759                                        &base_color);
1760         cairo_destroy (cr);
1761 #else /* HAVE_POPPLER_PAGE_RENDER */
1762         GdkPixbuf *pixbuf;
1763         
1764         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1765                                  TRUE, 8,
1766                                  width, height);
1767
1768         poppler_page_render_selection_to_pixbuf (poppler_page,
1769                                                  rc->scale, rc->rotation, pixbuf,
1770                                                  (PopplerRectangle *)points,
1771                                                  (PopplerRectangle *)old_points,
1772                                                  (PopplerSelectionStyle)style,
1773                                                  text,
1774                                                  base);
1775         if (*surface)
1776                 cairo_surface_destroy (*surface);
1777         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1778         g_object_unref (pixbuf);
1779 #endif /* HAVE_POPPLER_PAGE_RENDER */
1780 }
1781
1782 static gchar *
1783 pdf_selection_get_selected_text (EvSelection     *selection,
1784                                  EvRenderContext *rc,
1785                                  EvSelectionStyle style,
1786                                  EvRectangle     *points)
1787 {
1788         PopplerPage *poppler_page;
1789         PopplerRectangle r;
1790         double height;
1791         char *retval;
1792         
1793         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1794
1795         poppler_page_get_size (poppler_page, NULL, &height);
1796         r.x1 = points->x1;
1797         r.y1 = height - points->y2;
1798         r.x2 = points->x2;
1799         r.y2 = height - points->y1;
1800
1801         retval = poppler_page_get_text (poppler_page,
1802                                         (PopplerSelectionStyle)style,
1803                                         &r);
1804
1805         return retval;
1806 }
1807
1808 static GdkRegion *
1809 create_gdk_region_from_poppler_region (GList *region)
1810 {
1811         GList *l;
1812         GdkRegion *retval;
1813         
1814         retval = gdk_region_new ();
1815         
1816         for (l = region; l; l = g_list_next (l)) {
1817                 PopplerRectangle *rectangle;
1818                 GdkRectangle      rect;
1819                 
1820                 rectangle = (PopplerRectangle *)l->data;
1821                 
1822                 rect.x = (gint) rectangle->x1;
1823                 rect.y = (gint) rectangle->y1;
1824                 rect.width  = (gint) (rectangle->x2 - rectangle->x1);
1825                 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1826                 gdk_region_union_with_rect (retval, &rect);
1827                 
1828                 poppler_rectangle_free (rectangle);
1829         }
1830
1831         return retval;
1832 }
1833
1834 static GdkRegion *
1835 pdf_selection_get_selection_region (EvSelection     *selection,
1836                                     EvRenderContext *rc,
1837                                     EvSelectionStyle style,
1838                                     EvRectangle     *points)
1839 {
1840         PopplerPage *poppler_page;
1841         GdkRegion   *retval;
1842         GList       *region;
1843
1844         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1845         
1846         region = poppler_page_get_selection_region (poppler_page,
1847                                                     rc->scale,
1848                                                     (PopplerSelectionStyle)style,
1849                                                     (PopplerRectangle *) points);
1850         retval = create_gdk_region_from_poppler_region (region);
1851         g_list_free (region);
1852         
1853         return retval;
1854 }
1855
1856 static GdkRegion *
1857 pdf_selection_get_selection_map (EvSelection     *selection,
1858                                  EvRenderContext *rc)
1859 {
1860         PopplerPage *poppler_page;
1861         PopplerRectangle points;
1862         GList *region;
1863         GdkRegion *retval;
1864
1865         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1866
1867         points.x1 = 0.0;
1868         points.y1 = 0.0;
1869         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1870         
1871         region = poppler_page_get_selection_region (poppler_page, 1.0,
1872                                                     POPPLER_SELECTION_GLYPH,
1873                                                     &points);
1874         retval = create_gdk_region_from_poppler_region (region);
1875         g_list_free (region);
1876
1877         return retval;
1878 }
1879
1880 static void
1881 pdf_selection_iface_init (EvSelectionIface *iface)
1882 {
1883         iface->render_selection = pdf_selection_render_selection;
1884         iface->get_selected_text = pdf_selection_get_selected_text;
1885         iface->get_selection_region = pdf_selection_get_selection_region;
1886         iface->get_selection_map = pdf_selection_get_selection_map;
1887 }
1888
1889 /* Page Transitions */
1890 static gdouble
1891 pdf_document_get_page_duration (EvDocumentTransition *trans,
1892                                 gint                  page)
1893 {
1894         PdfDocument *pdf_document;
1895         PopplerPage *poppler_page;
1896         gdouble      duration = -1;
1897
1898         pdf_document = PDF_DOCUMENT (trans);
1899         poppler_page = poppler_document_get_page (pdf_document->document, page);
1900         if (!poppler_page)
1901                 return -1;
1902
1903         duration = poppler_page_get_duration (poppler_page);
1904         g_object_unref (poppler_page);
1905
1906         return duration;
1907 }
1908
1909 static EvTransitionEffect *
1910 pdf_document_get_effect (EvDocumentTransition *trans,
1911                          gint                  page)
1912 {
1913         PdfDocument            *pdf_document;
1914         PopplerPage            *poppler_page;
1915         PopplerPageTransition  *page_transition;
1916         EvTransitionEffect     *effect;
1917
1918         pdf_document = PDF_DOCUMENT (trans);
1919         poppler_page = poppler_document_get_page (pdf_document->document, page);
1920
1921         if (!poppler_page)
1922                 return NULL;
1923
1924         page_transition = poppler_page_get_transition (poppler_page);
1925
1926         if (!page_transition) {
1927                 g_object_unref (poppler_page);
1928                 return NULL;
1929         }
1930
1931         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1932         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1933                                            "alignment", page_transition->alignment,
1934                                            "direction", page_transition->direction,
1935                                            "duration", page_transition->duration,
1936                                            "angle", page_transition->angle,
1937                                            "scale", page_transition->scale,
1938                                            "rectangular", page_transition->rectangular,
1939                                            NULL);
1940
1941         poppler_page_transition_free (page_transition);
1942         g_object_unref (poppler_page);
1943
1944         return effect;
1945 }
1946
1947 static void
1948 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1949 {
1950         iface->get_page_duration = pdf_document_get_page_duration;
1951         iface->get_effect = pdf_document_get_effect;
1952 }
1953
1954 /* Forms */
1955 static void
1956 pdf_document_get_crop_box (EvDocument  *document, 
1957                            int          page, 
1958                            EvRectangle *rect)
1959 {
1960         PdfDocument *pdf_document;
1961         PopplerPage *poppler_page;
1962         PopplerRectangle poppler_rect;
1963
1964         pdf_document = PDF_DOCUMENT (document);
1965         poppler_page = poppler_document_get_page (pdf_document->document, page);
1966         poppler_page_get_crop_box (poppler_page, &poppler_rect);
1967         rect->x1 = poppler_rect.x1;
1968         rect->x2 = poppler_rect.x2;
1969         rect->y1 = poppler_rect.y1;
1970         rect->y2 = poppler_rect.y2;
1971 }
1972
1973 static EvFormField *
1974 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1975 {
1976         EvFormField *ev_field = NULL;
1977         gint         id;
1978         gdouble      font_size;
1979         gboolean     is_read_only;
1980
1981         id = poppler_form_field_get_id (poppler_field);
1982         font_size = poppler_form_field_get_font_size (poppler_field);
1983         is_read_only = poppler_form_field_is_read_only (poppler_field);
1984
1985         switch (poppler_form_field_get_field_type (poppler_field)) {
1986                 case POPPLER_FORM_FIELD_TEXT: {
1987                         EvFormFieldText    *field_text;
1988                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1989
1990                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
1991                                 case POPPLER_FORM_TEXT_NORMAL:
1992                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1993                                         break;
1994                                 case POPPLER_FORM_TEXT_MULTILINE:
1995                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
1996                                         break;
1997                                 case POPPLER_FORM_TEXT_FILE_SELECT:
1998                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
1999                                         break;
2000                         }
2001                         
2002                         ev_field = ev_form_field_text_new (id, ev_text_type);
2003                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2004
2005                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2006                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2007                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2008                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2009                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2010                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2011
2012                 }
2013                         break;
2014                 case POPPLER_FORM_FIELD_BUTTON: {
2015                         EvFormFieldButton    *field_button;
2016                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2017
2018                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2019                                 case POPPLER_FORM_BUTTON_PUSH:
2020                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2021                                         break;
2022                                 case POPPLER_FORM_BUTTON_CHECK:
2023                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2024                                         break;
2025                                 case POPPLER_FORM_BUTTON_RADIO:
2026                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2027                                         break;
2028                         }
2029
2030                         ev_field = ev_form_field_button_new (id, ev_button_type);
2031                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2032                         
2033                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2034                 }
2035                         break;
2036                 case POPPLER_FORM_FIELD_CHOICE: {
2037                         EvFormFieldChoice    *field_choice;
2038                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2039
2040                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2041                                 case POPPLER_FORM_CHOICE_COMBO:
2042                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2043                                         break;
2044                                 case EV_FORM_FIELD_CHOICE_LIST:
2045                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2046                                         break;
2047                         }
2048
2049                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2050                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2051
2052                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2053                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2054                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2055                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2056
2057                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2058                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2059                         if (field_choice->is_editable)
2060                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2061                 }
2062                         break;
2063                 case POPPLER_FORM_FIELD_SIGNATURE:
2064                         /* TODO */
2065                         ev_field = ev_form_field_signature_new (id);
2066                         break;
2067                 case POPPLER_FORM_FIELD_UNKNOWN:
2068                         return NULL;
2069         }
2070
2071         ev_field->font_size = font_size;
2072         ev_field->is_read_only = is_read_only;
2073
2074         return ev_field;
2075 }
2076
2077 static GList *
2078 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2079                                     EvPage          *page)
2080 {
2081         PopplerPage *poppler_page;
2082         GList *retval = NULL;
2083         GList *fields;
2084         GList *list;
2085         double height;
2086
2087         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2088         
2089         poppler_page = POPPLER_PAGE (page->backend_page);
2090         fields = poppler_page_get_form_field_mapping (poppler_page);
2091         poppler_page_get_size (poppler_page, NULL, &height);
2092
2093         for (list = fields; list; list = list->next) {
2094                 PopplerFormFieldMapping *mapping;
2095                 EvFormFieldMapping *field_mapping;
2096                 EvFormField *ev_field;
2097
2098                 mapping = (PopplerFormFieldMapping *)list->data;
2099
2100                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2101                 if (!ev_field)
2102                         continue;
2103
2104                 field_mapping = g_new0 (EvFormFieldMapping, 1);
2105                 field_mapping->x1 = mapping->area.x1;
2106                 field_mapping->x2 = mapping->area.x2;
2107                 field_mapping->y1 = height - mapping->area.y2;
2108                 field_mapping->y2 = height - mapping->area.y1;
2109                 field_mapping->field = ev_field;
2110                 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2111
2112                 g_object_set_data_full (G_OBJECT (ev_field),
2113                                         "poppler-field",
2114                                         g_object_ref (mapping->field),
2115                                         (GDestroyNotify) g_object_unref);
2116                 
2117                 retval = g_list_prepend (retval, field_mapping);
2118         }
2119         
2120         poppler_page_free_form_field_mapping (fields);
2121
2122         return g_list_reverse (retval);
2123 }
2124
2125 static gchar *
2126 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2127                                              EvFormField     *field)
2128         
2129 {
2130         PopplerFormField *poppler_field;
2131         gchar *text;
2132
2133         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2134         if (!poppler_field)
2135                 return NULL;
2136         
2137         text = poppler_form_field_text_get_text (poppler_field);
2138
2139         return text;
2140 }
2141
2142 static void
2143 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2144                                              EvFormField     *field,
2145                                              const gchar     *text)
2146 {
2147         PopplerFormField *poppler_field;
2148
2149         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2150         if (!poppler_field)
2151                 return;
2152         
2153         poppler_form_field_text_set_text (poppler_field, text);
2154         PDF_DOCUMENT (document)->modified = TRUE;
2155 }
2156
2157 static void
2158 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2159                                                 EvFormField     *field,
2160                                                 gboolean         state)
2161 {
2162         PopplerFormField *poppler_field;
2163
2164         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2165         if (!poppler_field)
2166                 return;
2167         
2168         poppler_form_field_button_set_state (poppler_field, state);
2169         PDF_DOCUMENT (document)->modified = TRUE;
2170 }
2171
2172 static gboolean
2173 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2174                                                 EvFormField     *field)
2175 {
2176         PopplerFormField *poppler_field;
2177         gboolean state;
2178
2179         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2180         if (!poppler_field)
2181                 return FALSE;
2182
2183         state = poppler_form_field_button_get_state (poppler_field);
2184
2185         return state;
2186 }
2187
2188 static gchar *
2189 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2190                                                EvFormField     *field,
2191                                                gint             index)
2192 {
2193         PopplerFormField *poppler_field;
2194         gchar *text;
2195
2196         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2197         if (!poppler_field)
2198                 return NULL;
2199
2200         text = poppler_form_field_choice_get_item (poppler_field, index);
2201
2202         return text;
2203 }
2204
2205 static int
2206 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2207                                                   EvFormField     *field)
2208 {
2209         PopplerFormField *poppler_field;
2210         gint n_items;
2211
2212         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2213         if (!poppler_field)
2214                 return -1;
2215         
2216         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2217
2218         return n_items;
2219 }
2220
2221 static gboolean
2222 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2223                                                        EvFormField     *field,
2224                                                        gint             index)
2225 {
2226         PopplerFormField *poppler_field;
2227         gboolean selected;
2228
2229         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2230         if (!poppler_field)
2231                 return FALSE;
2232
2233         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2234
2235         return selected;
2236 }
2237
2238 static void
2239 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2240                                                   EvFormField     *field,
2241                                                   gint             index)
2242 {
2243         PopplerFormField *poppler_field;
2244
2245         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2246         if (!poppler_field)
2247                 return;
2248
2249         poppler_form_field_choice_select_item (poppler_field, index);
2250         PDF_DOCUMENT (document)->modified = TRUE;
2251 }
2252
2253 static void
2254 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2255                                                   EvFormField     *field,
2256                                                   gint             index)
2257 {
2258         PopplerFormField *poppler_field;
2259
2260         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2261         if (!poppler_field)
2262                 return;
2263
2264         poppler_form_field_choice_toggle_item (poppler_field, index);
2265         PDF_DOCUMENT (document)->modified = TRUE;
2266 }
2267
2268 static void
2269 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2270                                                    EvFormField     *field)
2271 {
2272         PopplerFormField *poppler_field;
2273
2274         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2275         if (!poppler_field)
2276                 return;
2277         
2278         poppler_form_field_choice_unselect_all (poppler_field);
2279         PDF_DOCUMENT (document)->modified = TRUE;
2280 }
2281
2282 static void
2283 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2284                                                EvFormField     *field,
2285                                                const gchar     *text)
2286 {
2287         PopplerFormField *poppler_field;
2288
2289         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2290         if (!poppler_field)
2291                 return;
2292         
2293         poppler_form_field_choice_set_text (poppler_field, text);
2294         PDF_DOCUMENT (document)->modified = TRUE;
2295 }
2296
2297 static gchar *
2298 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2299                                                EvFormField     *field)
2300 {
2301         PopplerFormField *poppler_field;
2302         gchar *text;
2303
2304         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2305         if (!poppler_field)
2306                 return NULL;
2307
2308         text = poppler_form_field_choice_get_text (poppler_field);
2309
2310         return text;
2311 }
2312
2313 static void
2314 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2315 {
2316         iface->get_form_fields = pdf_document_forms_get_form_fields;
2317         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2318         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2319         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2320         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2321         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2322         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2323         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2324         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2325         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2326         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2327         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2328         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2329 }
2330
2331 /* Layers */
2332 static gboolean
2333 pdf_document_layers_has_layers (EvDocumentLayers *document)
2334 {
2335 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2336         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2337         PopplerLayersIter *iter;
2338
2339         iter = poppler_layers_iter_new (pdf_document->document);
2340         if (!iter)
2341                 return FALSE;
2342         poppler_layers_iter_free (iter);
2343
2344         return TRUE;
2345 #else
2346         return FALSE;
2347 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2348 }
2349
2350 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2351 static void
2352 build_layers_tree (PdfDocument       *pdf_document,
2353                    GtkTreeModel      *model,
2354                    GtkTreeIter       *parent,
2355                    PopplerLayersIter *iter)
2356 {
2357         do {
2358                 GtkTreeIter        tree_iter;
2359                 PopplerLayersIter *child;
2360                 PopplerLayer      *layer;
2361                 EvLayer           *ev_layer = NULL;
2362                 gboolean           visible;
2363                 gchar             *markup;
2364                 gint               rb_group = 0;
2365
2366                 layer = poppler_layers_iter_get_layer (iter);
2367                 if (layer) {
2368                         markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2369                         visible = poppler_layer_is_visible (layer);
2370                         rb_group = poppler_layer_get_radio_button_group_id (layer);
2371                         pdf_document->layers = g_list_append (pdf_document->layers,
2372                                                               g_object_ref (layer));
2373                         ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2374                                                  poppler_layer_is_parent (layer),
2375                                                  rb_group);
2376                 } else {
2377                         gchar *title;
2378
2379                         title = poppler_layers_iter_get_title (iter);
2380                         markup = g_markup_escape_text (title, -1);
2381                         g_free (title);
2382
2383                         visible = FALSE;
2384                         layer = NULL;
2385                 }
2386
2387                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2388                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2389                                     EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2390                                     EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2391                                     EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2392                                     EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2393                                     EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2394                                     EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2395                                     -1);
2396                 if (ev_layer)
2397                         g_object_unref (ev_layer);
2398                 g_free (markup);
2399
2400                 child = poppler_layers_iter_get_child (iter);
2401                 if (child)
2402                         build_layers_tree (pdf_document, model, &tree_iter, child);
2403                 poppler_layers_iter_free (child);
2404         } while (poppler_layers_iter_next (iter));
2405 }
2406 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2407
2408 static GtkTreeModel *
2409 pdf_document_layers_get_layers (EvDocumentLayers *document)
2410 {
2411         GtkTreeModel *model = NULL;
2412 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2413         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2414         PopplerLayersIter *iter;
2415
2416         iter = poppler_layers_iter_new (pdf_document->document);
2417         if (iter) {
2418                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2419                                                              G_TYPE_STRING,  /* TITLE */
2420                                                              G_TYPE_OBJECT,  /* LAYER */
2421                                                              G_TYPE_BOOLEAN, /* VISIBLE */
2422                                                              G_TYPE_BOOLEAN, /* ENABLED */
2423                                                              G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2424                                                              G_TYPE_INT);    /* RBGROUP */
2425                 build_layers_tree (pdf_document, model, NULL, iter);
2426                 poppler_layers_iter_free (iter);
2427         }
2428         
2429 #endif
2430         return model;
2431 }
2432
2433 static void
2434 pdf_document_layers_show_layer (EvDocumentLayers *document,
2435                                 EvLayer          *layer)
2436 {
2437 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2438         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2439         guint        layer_id = ev_layer_get_id (layer);
2440
2441         poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2442 #else
2443         return;
2444 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2445 }
2446
2447 static void
2448 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2449                                 EvLayer          *layer)
2450 {
2451 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2452         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2453         guint        layer_id = ev_layer_get_id (layer);
2454
2455         poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2456 #else
2457         return;
2458 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2459 }
2460
2461 static gboolean
2462 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2463                                       EvLayer          *layer)
2464 {
2465 #ifdef HAVE_POPPLER_LAYERS_ITER_NEW
2466         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2467         guint        layer_id = ev_layer_get_id (layer);
2468
2469         return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2470 #else
2471         return FALSE;
2472 #endif /* HAVE_POPPLER_LAYERS_ITER_NEW */
2473 }
2474
2475 static void
2476 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2477 {
2478         iface->has_layers = pdf_document_layers_has_layers;
2479         iface->get_layers = pdf_document_layers_get_layers;
2480         iface->show_layer = pdf_document_layers_show_layer;
2481         iface->hide_layer = pdf_document_layers_hide_layer;
2482         iface->layer_is_visible = pdf_document_layers_layer_is_visible;
2483 }