]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
Add missing call to parent class dispose method.
[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 #include <glib/gi18n.h>
32
33 #include "ev-poppler.h"
34 #include "ev-file-exporter.h"
35 #include "ev-document-find.h"
36 #include "ev-document-misc.h"
37 #include "ev-document-links.h"
38 #include "ev-document-images.h"
39 #include "ev-document-fonts.h"
40 #include "ev-document-security.h"
41 #include "ev-document-thumbnails.h"
42 #include "ev-document-transition.h"
43 #include "ev-selection.h"
44 #include "ev-attachment.h"
45 #include "ev-image.h"
46
47 typedef struct {
48         PdfDocument *document;
49         char *text;
50         GList **pages;
51         guint idle;
52         int start_page;
53         int search_page;
54 } PdfDocumentSearch;
55
56 typedef struct {
57         EvFileExporterFormat format;
58         PopplerPSFile *ps_file;
59 #ifdef HAVE_CAIRO_PDF
60         cairo_t *pdf_cairo;
61 #endif
62 } PdfPrintContext;
63
64 struct _PdfDocumentClass
65 {
66         GObjectClass parent_class;
67 };
68
69 struct _PdfDocument
70 {
71         GObject parent_instance;
72
73         PopplerDocument *document;
74         gchar *password;
75
76         PopplerFontInfo *font_info;
77         PopplerFontsIter *fonts_iter;
78         int fonts_scanned_pages;
79
80         PdfDocumentSearch *search;
81         PdfPrintContext *print_ctx;
82 };
83
84 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
85 static void pdf_document_security_iface_init            (EvDocumentSecurityIface   *iface);
86 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
87 static void pdf_document_document_links_iface_init      (EvDocumentLinksIface      *iface);
88 static void pdf_document_document_images_iface_init     (EvDocumentImagesIface     *iface);
89 static void pdf_document_document_fonts_iface_init      (EvDocumentFontsIface      *iface);
90 static void pdf_document_find_iface_init                (EvDocumentFindIface       *iface);
91 static void pdf_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
92 static void pdf_selection_iface_init                    (EvSelectionIface          *iface);
93 static void pdf_document_page_transition_iface_init     (EvDocumentTransitionIface *iface);
94 static void pdf_document_thumbnails_get_dimensions      (EvDocumentThumbnails      *document_thumbnails,
95                                                          EvRenderContext           *rc,
96                                                          gint                      *width,
97                                                          gint                      *height);
98 static int  pdf_document_get_n_pages                    (EvDocument                *document);
99
100 static EvLinkDest *ev_link_dest_from_dest   (PdfDocument       *pdf_document,
101                                              PopplerDest       *dest);
102 static EvLink     *ev_link_from_action      (PdfDocument       *pdf_document,
103                                              PopplerAction     *action);
104 static void        pdf_document_search_free (PdfDocumentSearch *search);
105 static void        pdf_print_context_free   (PdfPrintContext   *ctx);
106
107
108 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
109                          {
110                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
111                                                         pdf_document_document_iface_init);
112                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
113                                                         pdf_document_security_iface_init);
114                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
115                                                         pdf_document_document_thumbnails_iface_init);
116                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
117                                                         pdf_document_document_links_iface_init);
118                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
119                                                         pdf_document_document_images_iface_init);
120                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
121                                                         pdf_document_document_fonts_iface_init);
122                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
123                                                         pdf_document_find_iface_init);
124                                  G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
125                                                         pdf_document_file_exporter_iface_init);
126                                  G_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
127                                                         pdf_selection_iface_init);
128                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
129                                                         pdf_document_page_transition_iface_init);
130                          });
131
132
133 static void
134 set_rc_data (PdfDocument     *pdf_document,
135              EvRenderContext *rc)
136 {
137         if (rc->data == NULL) {
138                 rc->data = poppler_document_get_page (pdf_document->document,
139                                                       rc->page);
140                 rc->destroy = g_object_unref;
141         } else {
142                 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
143         }
144 }
145
146 static void
147 pdf_document_search_free (PdfDocumentSearch   *search)
148 {
149         PdfDocument *pdf_document = search->document;
150         int n_pages;
151         int i;
152
153         if (search->idle != 0)
154                 g_source_remove (search->idle);
155
156         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
157         for (i = 0; i < n_pages; i++) {
158                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
159                 g_list_free (search->pages[i]);
160         }
161         g_free (search->pages);
162         
163         g_free (search->text);
164         g_free (search);
165 }
166
167 static void
168 pdf_document_dispose (GObject *object)
169 {
170         PdfDocument *pdf_document = PDF_DOCUMENT(object);
171
172         if (pdf_document->print_ctx) {
173                 pdf_print_context_free (pdf_document->print_ctx);
174                 pdf_document->print_ctx = NULL;
175         }
176         
177         if (pdf_document->search) {
178                 pdf_document_search_free (pdf_document->search);
179                 pdf_document->search = NULL;
180         }
181
182         if (pdf_document->document) {
183                 g_object_unref (pdf_document->document);
184         }
185
186         if (pdf_document->font_info) { 
187                 poppler_font_info_free (pdf_document->font_info);
188         }
189
190         if (pdf_document->fonts_iter) {
191                 poppler_fonts_iter_free (pdf_document->fonts_iter);
192         }
193
194         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
195 }
196
197 static void
198 pdf_document_class_init (PdfDocumentClass *klass)
199 {
200         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
201
202         g_object_class->dispose = pdf_document_dispose;
203 }
204
205 static void
206 pdf_document_init (PdfDocument *pdf_document)
207 {
208         pdf_document->password = NULL;
209 }
210
211 static void
212 convert_error (GError  *poppler_error,
213                GError **error)
214 {
215         if (poppler_error == NULL)
216                 return;
217
218         if (poppler_error->domain == POPPLER_ERROR) {
219                 /* convert poppler errors into EvDocument errors */
220                 gint code = EV_DOCUMENT_ERROR_INVALID;
221                 if (poppler_error->code == POPPLER_ERROR_INVALID)
222                         code = EV_DOCUMENT_ERROR_INVALID;
223                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
224                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
225                         
226
227                 g_set_error (error,
228                              EV_DOCUMENT_ERROR,
229                              code,
230                              poppler_error->message,
231                              NULL);
232         } else {
233                 g_propagate_error (error, poppler_error);
234         }
235 }
236
237
238 /* EvDocument */
239 static gboolean
240 pdf_document_save (EvDocument  *document,
241                    const char  *uri,
242                    GError     **error)
243 {
244         gboolean retval;
245         GError *poppler_error = NULL;
246
247         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
248                                         uri,
249                                         &poppler_error);
250         if (! retval)
251                 convert_error (poppler_error, error);
252
253         return retval;
254 }
255
256 static gboolean
257 pdf_document_load (EvDocument   *document,
258                    const char   *uri,
259                    GError      **error)
260 {
261         GError *poppler_error = NULL;
262         PdfDocument *pdf_document = PDF_DOCUMENT (document);
263
264         pdf_document->document =
265                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
266
267         if (pdf_document->document == NULL) {
268                 convert_error (poppler_error, error);
269                 return FALSE;
270         }
271
272         return TRUE;
273 }
274
275 static int
276 pdf_document_get_n_pages (EvDocument *document)
277 {
278         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
279 }
280
281 static void
282 pdf_document_get_page_size (EvDocument   *document,
283                             int           page,
284                             double       *width,
285                             double       *height)
286 {
287         PdfDocument *pdf_document = PDF_DOCUMENT (document);
288         PopplerPage *poppler_page;
289
290         poppler_page = poppler_document_get_page (pdf_document->document, page);
291         poppler_page_get_size (poppler_page, width, height);
292         g_object_unref (poppler_page);
293 }
294
295 static char *
296 pdf_document_get_page_label (EvDocument *document,
297                              int         page)
298 {
299         PopplerPage *poppler_page;
300         char *label = NULL;
301
302         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
303                                                   page);
304
305         g_object_get (G_OBJECT (poppler_page),
306                       "label", &label,
307                       NULL);
308         g_object_unref (poppler_page);
309
310         return label;
311 }
312
313 static gboolean
314 pdf_document_has_attachments (EvDocument *document)
315 {
316         PdfDocument *pdf_document;
317
318         pdf_document = PDF_DOCUMENT (document);
319
320         return poppler_document_has_attachments (pdf_document->document);
321 }
322
323 struct SaveToBufferData {
324         gchar *buffer;
325         gsize len, max;
326 };
327
328 static gboolean
329 attachment_save_to_buffer_callback (const gchar  *buf,
330                                     gsize         count,
331                                     gpointer      user_data,
332                                     GError      **error)
333 {
334         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
335         gchar *new_buffer;
336         gsize new_max;
337
338         if (sdata->len + count > sdata->max) {
339                 new_max = MAX (sdata->max * 2, sdata->len + count);
340                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
341
342                 sdata->buffer = new_buffer;
343                 sdata->max = new_max;
344         }
345         
346         memcpy (sdata->buffer + sdata->len, buf, count);
347         sdata->len += count;
348         
349         return TRUE;
350 }
351
352 static gboolean
353 attachment_save_to_buffer (PopplerAttachment  *attachment,
354                            gchar             **buffer,
355                            gsize              *buffer_size,
356                            GError            **error)
357 {
358         static const gint initial_max = 1024;
359         struct SaveToBufferData sdata;
360
361         *buffer = NULL;
362         *buffer_size = 0;
363
364         sdata.buffer = (gchar *) g_malloc (initial_max);
365         sdata.max = initial_max;
366         sdata.len = 0;
367
368         if (! poppler_attachment_save_to_callback (attachment,
369                                                    attachment_save_to_buffer_callback,
370                                                    &sdata,
371                                                    error)) {
372                 g_free (sdata.buffer);
373                 return FALSE;
374         }
375
376         *buffer = sdata.buffer;
377         *buffer_size = sdata.len;
378         
379         return TRUE;
380 }
381
382 static GList *
383 pdf_document_get_attachments (EvDocument *document)
384 {
385         PdfDocument *pdf_document;
386         GList *attachments;
387         GList *list;
388         GList *retval = NULL;
389
390         pdf_document = PDF_DOCUMENT (document);
391
392         if (!pdf_document_has_attachments (document))
393                 return NULL;
394
395         attachments = poppler_document_get_attachments (pdf_document->document);
396         
397         for (list = attachments; list; list = list->next) {
398                 PopplerAttachment *attachment;
399                 EvAttachment *ev_attachment;
400                 gchar *data = NULL;
401                 gsize size;
402                 GError *error = NULL;
403
404                 attachment = (PopplerAttachment *) list->data;
405
406                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
407                         ev_attachment = ev_attachment_new (attachment->name,
408                                                            attachment->description,
409                                                            attachment->mtime,
410                                                            attachment->ctime,
411                                                            size, data);
412                         
413                         retval = g_list_prepend (retval, ev_attachment);
414                 } else {
415                         if (error) {
416                                 g_warning ("%s", error->message);
417                                 g_error_free (error);
418
419                                 g_free (data);
420                         }
421                 }
422
423                 g_object_unref (attachment);
424         }
425
426         return g_list_reverse (retval);
427 }
428
429 static GdkPixbuf *
430 pdf_document_render_pixbuf (EvDocument   *document,
431                             EvRenderContext *rc)
432 {
433         PdfDocument *pdf_document;
434         GdkPixbuf *pixbuf;
435         double width_points, height_points;
436         gint width, height;
437
438         pdf_document = PDF_DOCUMENT (document);
439
440         set_rc_data (pdf_document, rc);
441
442         poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
443
444         if (rc->rotation == 90 || rc->rotation == 270) {
445                 width = (int) ((height_points * rc->scale) + 0.5);
446                 height = (int) ((width_points * rc->scale) + 0.5);
447         } else {
448                 width = (int) ((width_points * rc->scale) + 0.5);
449                 height = (int) ((height_points * rc->scale) + 0.5);
450         }
451
452         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
453                                  FALSE, 8,
454                                  width, height);
455
456         poppler_page_render_to_pixbuf (POPPLER_PAGE (rc->data),
457                                        0, 0,
458                                        width, height,
459                                        rc->scale,
460                                        rc->rotation,
461                                        pixbuf);
462         
463         
464         return pixbuf;
465 }
466
467 /* EvDocumentSecurity */
468
469 static gboolean
470 pdf_document_has_document_security (EvDocumentSecurity *document_security)
471 {
472         /* FIXME: do we really need to have this? */
473         return FALSE;
474 }
475
476 static void
477 pdf_document_set_password (EvDocumentSecurity *document_security,
478                            const char         *password)
479 {
480         PdfDocument *document = PDF_DOCUMENT (document_security);
481
482         if (document->password)
483                 g_free (document->password);
484
485         document->password = g_strdup (password);
486 }
487
488 static gboolean
489 pdf_document_can_get_text (EvDocument *document)
490 {
491         return TRUE;
492 }
493
494 static EvDocumentInfo *
495 pdf_document_get_info (EvDocument *document)
496 {
497         EvDocumentInfo *info;
498         PopplerPageLayout layout;
499         PopplerPageMode mode;
500         PopplerViewerPreferences view_prefs;
501         PopplerPermissions permissions;
502
503         info = g_new0 (EvDocumentInfo, 1);
504
505         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
506                             EV_DOCUMENT_INFO_FORMAT |
507                             EV_DOCUMENT_INFO_AUTHOR |
508                             EV_DOCUMENT_INFO_SUBJECT |
509                             EV_DOCUMENT_INFO_KEYWORDS |
510                             EV_DOCUMENT_INFO_LAYOUT |
511                             EV_DOCUMENT_INFO_START_MODE |
512                             EV_DOCUMENT_INFO_PERMISSIONS |
513                             EV_DOCUMENT_INFO_UI_HINTS |
514                             EV_DOCUMENT_INFO_CREATOR |
515                             EV_DOCUMENT_INFO_PRODUCER |
516                             EV_DOCUMENT_INFO_CREATION_DATE |
517                             EV_DOCUMENT_INFO_MOD_DATE |
518                             EV_DOCUMENT_INFO_LINEARIZED |
519                             EV_DOCUMENT_INFO_N_PAGES |
520                             EV_DOCUMENT_INFO_SECURITY | 
521                             EV_DOCUMENT_INFO_PAPER_SIZE;
522
523         g_object_get (PDF_DOCUMENT (document)->document,
524                       "title", &(info->title),
525                       "format", &(info->format),
526                       "author", &(info->author),
527                       "subject", &(info->subject),
528                       "keywords", &(info->keywords),
529                       "page-mode", &mode,
530                       "page-layout", &layout,
531                       "viewer-preferences", &view_prefs,
532                       "permissions", &permissions,
533                       "creator", &(info->creator),
534                       "producer", &(info->producer),
535                       "creation-date", &(info->creation_date),
536                       "mod-date", &(info->modified_date),
537                       "linearized", &(info->linearized),
538                       NULL);
539
540         pdf_document_get_page_size(document, 0,
541                                    &(info->paper_width),
542                                    &(info->paper_height));
543
544         // Convert to mm.
545         info->paper_width = info->paper_width / 72.0f * 25.4f;
546         info->paper_height = info->paper_height / 72.0f * 25.4f;
547
548         switch (layout) {
549                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
550                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
551                         break;
552                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
553                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
554                         break;
555                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
556                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
557                         break;
558                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
559                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
560                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
561                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
562                         break;
563                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
564                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
565                         break;
566                 default:
567                         break;
568         }
569
570         switch (mode) {
571                 case POPPLER_PAGE_MODE_NONE:
572                         info->mode = EV_DOCUMENT_MODE_NONE;
573                         break;
574                 case POPPLER_PAGE_MODE_USE_THUMBS:
575                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
576                         break;
577                 case POPPLER_PAGE_MODE_USE_OC:
578                         info->mode = EV_DOCUMENT_MODE_USE_OC;
579                         break;
580                 case POPPLER_PAGE_MODE_FULL_SCREEN:
581                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
582                         break;
583                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
584                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
585                 default:
586                         break;
587         }
588
589         info->ui_hints = 0;
590         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
591                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
592         }
593         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
594                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
595         }
596         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
597                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
598         }
599         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
600                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
601         }
602         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
603                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
604         }
605         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
606                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
607         }
608         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
609                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
610         }
611
612         info->permissions = 0;
613         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
614                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
615         }
616         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
617                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
618         }
619         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
620                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
621         }
622         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
623                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
624         }
625
626         info->n_pages = ev_document_get_n_pages (document);
627
628         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
629                 /* translators: this is the document security state */
630                 info->security = g_strdup (_("Yes"));
631         } else {
632                 /* translators: this is the document security state */
633                 info->security = g_strdup (_("No"));
634         }
635
636         return info;
637 }
638
639 static char *
640 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
641 {
642         PdfDocument *pdf_document = PDF_DOCUMENT (document);
643         PopplerPage *poppler_page;
644         PopplerRectangle r;
645         double height;
646         char *text;
647         
648         poppler_page = poppler_document_get_page (pdf_document->document, page);
649         g_return_val_if_fail (poppler_page != NULL, NULL);
650
651         poppler_page_get_size (poppler_page, NULL, &height);
652         r.x1 = rect->x1;
653         r.y1 = height - rect->y2;
654         r.x2 = rect->x2;
655         r.y2 = height - rect->y1;
656
657         text = poppler_page_get_text (poppler_page, &r);
658
659         g_object_unref (poppler_page);
660
661         return text;
662 }
663
664 static void
665 pdf_document_document_iface_init (EvDocumentIface *iface)
666 {
667         iface->save = pdf_document_save;
668         iface->load = pdf_document_load;
669         iface->get_n_pages = pdf_document_get_n_pages;
670         iface->get_page_size = pdf_document_get_page_size;
671         iface->get_page_label = pdf_document_get_page_label;
672         iface->has_attachments = pdf_document_has_attachments;
673         iface->get_attachments = pdf_document_get_attachments;
674         iface->render_pixbuf = pdf_document_render_pixbuf;
675         iface->get_text = pdf_document_get_text;
676         iface->can_get_text = pdf_document_can_get_text;
677         iface->get_info = pdf_document_get_info;
678 };
679
680 static void
681 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
682 {
683         iface->has_document_security = pdf_document_has_document_security;
684         iface->set_password = pdf_document_set_password;
685 }
686
687 static gdouble
688 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
689 {
690         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
691         int n_pages;
692
693         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
694
695         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
696 }
697
698 static gboolean
699 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
700                          int              n_pages)
701 {
702         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
703         gboolean result;
704
705         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
706
707         if (pdf_document->font_info == NULL) { 
708                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
709         }
710
711         if (pdf_document->fonts_iter) {
712                 poppler_fonts_iter_free (pdf_document->fonts_iter);
713         }
714
715         pdf_document->fonts_scanned_pages += n_pages;
716
717         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
718                                          &pdf_document->fonts_iter);
719         if (!result) {
720                 pdf_document->fonts_scanned_pages = 0;
721                 poppler_font_info_free (pdf_document->font_info);
722                 pdf_document->font_info = NULL; 
723         }
724
725         return result;
726 }
727
728 static const char *
729 font_type_to_string (PopplerFontType type)
730 {
731         switch (type) {
732                 case POPPLER_FONT_TYPE_TYPE1:
733                         return _("Type 1");
734                 case POPPLER_FONT_TYPE_TYPE1C:
735                         return _("Type 1C");
736                 case POPPLER_FONT_TYPE_TYPE3:
737                         return _("Type 3");
738                 case POPPLER_FONT_TYPE_TRUETYPE:
739                         return _("TrueType");
740                 case POPPLER_FONT_TYPE_CID_TYPE0:
741                         return _("Type 1 (CID)");
742                 case POPPLER_FONT_TYPE_CID_TYPE0C:
743                         return _("Type 1C (CID)");
744                 case POPPLER_FONT_TYPE_CID_TYPE2:
745                         return _("TrueType (CID)");
746                 default:
747                         return _("Unknown font type");
748         }
749 }
750
751 static void
752 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
753                                GtkTreeModel    *model)
754 {
755         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
756         PopplerFontsIter *iter = pdf_document->fonts_iter;
757
758         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
759
760         if (!iter)
761                 return;
762
763         do {
764                 GtkTreeIter list_iter;
765                 const char *name;
766                 const char *type;
767                 const char *embedded;
768                 char *details;
769                 
770                 name = poppler_fonts_iter_get_name (iter);
771
772                 if (name == NULL) {
773                         name = _("No name");
774                 }
775
776                 type = font_type_to_string (
777                         poppler_fonts_iter_get_font_type (iter));
778
779                 if (poppler_fonts_iter_is_embedded (iter)) {
780                         if (poppler_fonts_iter_is_subset (iter))
781                                 embedded = _("Embedded subset");
782                         else
783                                 embedded = _("Embedded");
784                 } else {
785                         embedded = _("Not embedded");
786                 }
787
788                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
789
790                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
791                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
792                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
793                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
794                                     -1);
795
796                 g_free (details);
797         } while (poppler_fonts_iter_next (iter));
798 }
799
800 static void
801 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
802 {
803         iface->fill_model = pdf_document_fonts_fill_model;
804         iface->scan = pdf_document_fonts_scan;
805         iface->get_progress = pdf_document_fonts_get_progress;
806 }
807
808 static gboolean
809 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
810 {
811         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
812         PopplerIndexIter *iter;
813
814         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
815
816         iter = poppler_index_iter_new (pdf_document->document);
817         if (iter == NULL)
818                 return FALSE;
819         poppler_index_iter_free (iter);
820
821         return TRUE;
822 }
823
824 static EvLinkDest *
825 ev_link_dest_from_dest (PdfDocument *pdf_document,
826                         PopplerDest *dest)
827 {
828         EvLinkDest *ev_dest = NULL;
829         const char *unimplemented_dest = NULL;
830
831         g_assert (dest != NULL);
832
833         switch (dest->type) {
834                 case POPPLER_DEST_XYZ: {
835                         PopplerPage *poppler_page;
836                         double height;
837
838                         poppler_page = poppler_document_get_page (pdf_document->document,
839                                                                   MAX (0, dest->page_num - 1));
840                         poppler_page_get_size (poppler_page, NULL, &height);
841                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
842                                                         dest->left,
843                                                         height - dest->top,
844                                                         dest->zoom);
845                         g_object_unref (poppler_page);
846                 }
847                         break;
848                 case POPPLER_DEST_FIT:
849                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
850                         break;
851                 case POPPLER_DEST_FITH: {
852                         PopplerPage *poppler_page;
853                         double height;
854
855                         poppler_page = poppler_document_get_page (pdf_document->document,
856                                                                   MAX (0, dest->page_num - 1));
857                         poppler_page_get_size (poppler_page, NULL, &height);
858                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
859                                                          height - dest->top);
860                         g_object_unref (poppler_page);
861                 }
862                         break;
863                 case POPPLER_DEST_FITV:
864                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
865                                                          dest->left);
866                         break;
867                 case POPPLER_DEST_FITR: {
868                         PopplerPage *poppler_page;
869                         double height;
870
871                         poppler_page = poppler_document_get_page (pdf_document->document,
872                                                                   MAX (0, dest->page_num - 1));
873                         poppler_page_get_size (poppler_page, NULL, &height);
874                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
875                                                          dest->left,
876                                                          height - dest->bottom,
877                                                          dest->right,
878                                                          height - dest->top);
879                         g_object_unref (poppler_page);
880                 }
881                         break;
882                 case POPPLER_DEST_FITB:
883                         unimplemented_dest = "POPPLER_DEST_FITB";
884                         break;
885                 case POPPLER_DEST_FITBH:
886                         unimplemented_dest = "POPPLER_DEST_FITBH";
887                         break;
888                 case POPPLER_DEST_FITBV:
889                         unimplemented_dest = "POPPLER_DEST_FITBV";
890                         break;
891                 case POPPLER_DEST_NAMED:
892                         ev_dest = ev_link_dest_new_named (dest->named_dest);
893                         break;
894                 case POPPLER_DEST_UNKNOWN:
895                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
896                         break;
897         }
898
899         if (unimplemented_dest) {
900                 g_warning ("Unimplemented named action: %s, please post a "
901                            "bug report in Evince bugzilla "
902                            "(http://bugzilla.gnome.org) with a testcase.",
903                            unimplemented_dest);
904         }
905
906         if (!ev_dest)
907                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
908         
909         return ev_dest;
910 }
911
912 static EvLink *
913 ev_link_from_action (PdfDocument   *pdf_document,
914                      PopplerAction *action)
915 {
916         EvLink       *link = NULL;
917         EvLinkAction *ev_action = NULL;
918         const char   *unimplemented_action = NULL;
919
920         switch (action->type) {
921                 case POPPLER_ACTION_GOTO_DEST: {
922                         EvLinkDest *dest;
923                         
924                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
925                         ev_action = ev_link_action_new_dest (dest);
926                 }
927                         break;
928                 case POPPLER_ACTION_GOTO_REMOTE: {
929                         EvLinkDest *dest;
930                         
931                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
932                         ev_action = ev_link_action_new_remote (dest, 
933                                                                action->goto_remote.file_name);
934                         
935                 }
936                         break;
937                 case POPPLER_ACTION_LAUNCH:
938                         ev_action = ev_link_action_new_launch (action->launch.file_name,
939                                                                action->launch.params);
940                         break;
941                 case POPPLER_ACTION_URI:
942                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
943                         break;
944                 case POPPLER_ACTION_NAMED:
945                         ev_action = ev_link_action_new_named (action->named.named_dest);
946                         break;
947                 case POPPLER_ACTION_MOVIE:
948                         unimplemented_action = "POPPLER_ACTION_MOVIE";
949                         break;
950                 case POPPLER_ACTION_UNKNOWN:
951                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
952         }
953         
954         if (unimplemented_action) {
955                 g_warning ("Unimplemented action: %s, please post a bug report with a testcase.",
956                            unimplemented_action);
957         }
958         
959         link = ev_link_new (action->any.title, ev_action);
960         
961         return link;    
962 }
963
964 static void
965 build_tree (PdfDocument      *pdf_document,
966             GtkTreeModel     *model,
967             GtkTreeIter      *parent,
968             PopplerIndexIter *iter)
969 {
970         
971         do {
972                 GtkTreeIter tree_iter;
973                 PopplerIndexIter *child;
974                 PopplerAction *action;
975                 EvLink *link = NULL;
976                 gboolean expand;
977                 char *title_markup;
978                 
979                 action = poppler_index_iter_get_action (iter);
980                 expand = poppler_index_iter_is_open (iter);
981
982                 if (!action)
983                         continue;
984
985                 switch (action->type) {
986                         case POPPLER_ACTION_GOTO_DEST: {
987                                 /* For bookmarks, solve named destinations */
988                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
989                                         PopplerDest *dest;
990                                         EvLinkDest *ev_dest = NULL;
991                                         EvLinkAction *ev_action;
992                                         
993                                         dest = poppler_document_find_dest (pdf_document->document,
994                                                                            action->goto_dest.dest->named_dest);
995                                         if (!dest) {
996                                                 link = ev_link_from_action (pdf_document, action);
997                                                 break;
998                                         }
999                                         
1000                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1001                                         poppler_dest_free (dest);
1002                                         
1003                                         ev_action = ev_link_action_new_dest (ev_dest);
1004                                         link = ev_link_new (action->any.title, ev_action);
1005                                 } else {
1006                                         link = ev_link_from_action (pdf_document, action);
1007                                 }
1008                         }
1009                                 break;
1010                         default:
1011                                 link = ev_link_from_action (pdf_document, action);
1012                                 break;
1013                 }
1014                 
1015                 if (!link) {
1016                         poppler_action_free (action);
1017                         continue;
1018                 }
1019
1020                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1021                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1022                 
1023                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1024                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1025                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1026                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1027                                     -1);
1028                 
1029                 g_free (title_markup);
1030                 g_object_unref (link);
1031                 
1032                 child = poppler_index_iter_get_child (iter);
1033                 if (child)
1034                         build_tree (pdf_document, model, &tree_iter, child);
1035                 poppler_index_iter_free (child);
1036                 poppler_action_free (action);
1037                 
1038         } while (poppler_index_iter_next (iter));
1039 }
1040
1041 static GtkTreeModel *
1042 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1043 {
1044         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1045         GtkTreeModel *model = NULL;
1046         PopplerIndexIter *iter;
1047         
1048         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1049
1050         iter = poppler_index_iter_new (pdf_document->document);
1051         /* Create the model if we have items*/
1052         if (iter != NULL) {
1053                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1054                                                              G_TYPE_STRING,
1055                                                              G_TYPE_OBJECT,
1056                                                              G_TYPE_BOOLEAN,
1057                                                              G_TYPE_STRING);
1058                 build_tree (pdf_document, model, NULL, iter);
1059                 poppler_index_iter_free (iter);
1060         }
1061         
1062         return model;
1063 }
1064
1065 static GList *
1066 pdf_document_links_get_links (EvDocumentLinks *document_links,
1067                               gint             page)
1068 {
1069         PdfDocument *pdf_document;
1070         PopplerPage *poppler_page;
1071         GList *retval = NULL;
1072         GList *mapping_list;
1073         GList *list;
1074         double height;
1075
1076         pdf_document = PDF_DOCUMENT (document_links);
1077         poppler_page = poppler_document_get_page (pdf_document->document,
1078                                                   page);
1079         mapping_list = poppler_page_get_link_mapping (poppler_page);
1080         poppler_page_get_size (poppler_page, NULL, &height);
1081
1082         for (list = mapping_list; list; list = list->next) {
1083                 PopplerLinkMapping *link_mapping;
1084                 EvLinkMapping *ev_link_mapping;
1085
1086                 link_mapping = (PopplerLinkMapping *)list->data;
1087                 ev_link_mapping = g_new (EvLinkMapping, 1);
1088                 ev_link_mapping->link = ev_link_from_action (pdf_document,
1089                                                              link_mapping->action);
1090                 ev_link_mapping->x1 = link_mapping->area.x1;
1091                 ev_link_mapping->x2 = link_mapping->area.x2;
1092                 /* Invert this for X-style coordinates */
1093                 ev_link_mapping->y1 = height - link_mapping->area.y2;
1094                 ev_link_mapping->y2 = height - link_mapping->area.y1;
1095
1096                 retval = g_list_prepend (retval, ev_link_mapping);
1097         }
1098
1099         poppler_page_free_link_mapping (mapping_list);
1100         g_object_unref (poppler_page);
1101
1102         return g_list_reverse (retval);
1103 }
1104
1105 static EvLinkDest *
1106 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1107                                    const gchar      *link_name)
1108 {
1109         PdfDocument *pdf_document;
1110         PopplerDest *dest;
1111         EvLinkDest *ev_dest = NULL;
1112
1113         pdf_document = PDF_DOCUMENT (document_links);
1114         dest = poppler_document_find_dest (pdf_document->document,
1115                                            link_name);
1116         if (dest) {
1117                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1118                 poppler_dest_free (dest);
1119         }
1120
1121         return ev_dest;
1122 }
1123
1124 static void
1125 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1126 {
1127         iface->has_document_links = pdf_document_links_has_document_links;
1128         iface->get_links_model = pdf_document_links_get_links_model;
1129         iface->get_links = pdf_document_links_get_links;
1130         iface->find_link_dest = pdf_document_links_find_link_dest;
1131 }
1132
1133 static GList *
1134 pdf_document_images_get_images (EvDocumentImages *document_images,
1135                                 gint              page)
1136 {
1137         GList *retval = NULL;
1138 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE_MAPPING
1139         PdfDocument *pdf_document;
1140         PopplerPage *poppler_page;
1141         GList *mapping_list;
1142         GList *list;
1143
1144         pdf_document = PDF_DOCUMENT (document_images);
1145         poppler_page = poppler_document_get_page (pdf_document->document, page);
1146         mapping_list = poppler_page_get_image_mapping (poppler_page);
1147
1148         for (list = mapping_list; list; list = list->next) {
1149                 PopplerImageMapping *image_mapping;
1150                 EvImageMapping *ev_image_mapping;
1151
1152                 image_mapping = (PopplerImageMapping *)list->data;
1153
1154                 ev_image_mapping = g_new (EvImageMapping, 1);
1155                 
1156                 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1157                 ev_image_mapping->x1 = image_mapping->area.x1;
1158                 ev_image_mapping->x2 = image_mapping->area.x2;
1159                 ev_image_mapping->y1 = image_mapping->area.y1;
1160                 ev_image_mapping->y2 = image_mapping->area.y2;
1161
1162                 retval = g_list_prepend (retval, ev_image_mapping);
1163         }
1164
1165         poppler_page_free_image_mapping (mapping_list);
1166         g_object_unref (poppler_page);
1167 #endif /* HAVE_POPPLER_PAGE_GET_IMAGE_MAPPING */
1168         return retval;
1169 }
1170
1171 static void
1172 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1173 {
1174         iface->get_images = pdf_document_images_get_images;
1175 }
1176
1177 static GdkPixbuf *
1178 make_thumbnail_for_page (PdfDocument     *pdf_document,
1179                          PopplerPage     *poppler_page, 
1180                          EvRenderContext *rc)
1181 {
1182         GdkPixbuf *pixbuf;
1183         int width, height;
1184
1185         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1186                                                 rc, &width, &height);
1187
1188         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1189                                  width, height);
1190         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1191
1192         ev_document_fc_mutex_lock ();
1193         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1194                                        width, height,
1195                                        rc->scale, rc->rotation, pixbuf);
1196         ev_document_fc_mutex_unlock ();
1197
1198         return pixbuf;
1199 }
1200
1201 static GdkPixbuf *
1202 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1203                                        EvRenderContext      *rc, 
1204                                        gboolean              border)
1205 {
1206         PdfDocument *pdf_document;
1207         PopplerPage *poppler_page;
1208         GdkPixbuf *pixbuf;
1209         GdkPixbuf *border_pixbuf;
1210
1211         pdf_document = PDF_DOCUMENT (document_thumbnails);
1212
1213         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1214         g_return_val_if_fail (poppler_page != NULL, NULL);
1215
1216         pixbuf = poppler_page_get_thumbnail (poppler_page);
1217         if (!pixbuf) {
1218                 /* There is no provided thumbnail.  We need to make one. */
1219                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1220         }
1221
1222         if (border) {           
1223                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1224                 g_object_unref (pixbuf);
1225                 pixbuf = border_pixbuf;
1226         }               
1227
1228         g_object_unref (poppler_page);
1229         
1230         return pixbuf;
1231 }
1232
1233 static void
1234 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1235                                         EvRenderContext      *rc,
1236                                         gint                 *width,
1237                                         gint                 *height)
1238 {
1239         PdfDocument *pdf_document;
1240         PopplerPage *poppler_page;
1241         gint has_thumb;
1242         
1243         pdf_document = PDF_DOCUMENT (document_thumbnails);
1244         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1245
1246         g_return_if_fail (poppler_page != NULL);
1247
1248         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1249
1250         if (!has_thumb) {
1251                 double page_width, page_height;
1252
1253                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1254
1255                 *width = (gint) (page_width * rc->scale);
1256                 *height = (gint) (page_height * rc->scale);
1257         }
1258
1259         if (rc->rotation == 90 || rc->rotation == 270) {
1260                 gint  temp;
1261
1262                 temp = *width;
1263                 *width = *height;
1264                 *height = temp;
1265         }
1266         
1267         g_object_unref (poppler_page);
1268 }
1269
1270 static void
1271 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1272 {
1273         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1274         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1275 }
1276
1277
1278 static gboolean
1279 pdf_document_search_idle_callback (void *data)
1280 {
1281         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1282         PdfDocument *pdf_document = search->document;
1283         int n_pages;
1284         GList *matches;
1285         PopplerPage *page;
1286
1287         page = poppler_document_get_page (search->document->document,
1288                                           search->search_page);
1289
1290         ev_document_doc_mutex_lock ();
1291         matches = poppler_page_find_text (page, search->text);
1292         ev_document_doc_mutex_unlock ();
1293
1294         g_object_unref (page);
1295
1296         search->pages[search->search_page] = matches;
1297         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1298                                   search->search_page);
1299
1300         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1301         search->search_page += 1;
1302         if (search->search_page == n_pages) {
1303                 /* wrap around */
1304                 search->search_page = 0;
1305         }
1306
1307         if (search->search_page != search->start_page) {
1308                 return TRUE;
1309         }
1310
1311         /* We're done. */
1312         search->idle = 0; /* will return FALSE to remove */
1313         return FALSE;
1314 }
1315
1316
1317 static PdfDocumentSearch *
1318 pdf_document_search_new (PdfDocument *pdf_document,
1319                          int          start_page,
1320                          const char  *text)
1321 {
1322         PdfDocumentSearch *search;
1323         int n_pages;
1324         int i;
1325
1326         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1327
1328         search = g_new0 (PdfDocumentSearch, 1);
1329
1330         search->text = g_strdup (text);
1331         search->pages = g_new0 (GList *, n_pages);
1332         search->document = pdf_document;
1333
1334         /* We add at low priority so the progress bar repaints */
1335         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1336                                         pdf_document_search_idle_callback,
1337                                         search,
1338                                         NULL);
1339
1340         search->start_page = start_page;
1341         search->search_page = start_page;
1342
1343         return search;
1344 }
1345
1346 static void
1347 pdf_document_find_begin (EvDocumentFind   *document,
1348                          int               page,
1349                          const char       *search_string,
1350                          gboolean          case_sensitive)
1351 {
1352         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1353
1354         /* FIXME handle case_sensitive (right now XPDF
1355          * code is always case insensitive for ASCII
1356          * and case sensitive for all other languaages)
1357          */
1358
1359         if (pdf_document->search &&
1360             strcmp (search_string, pdf_document->search->text) == 0)
1361                 return;
1362
1363         if (pdf_document->search)
1364                 pdf_document_search_free (pdf_document->search);
1365
1366         pdf_document->search = pdf_document_search_new (pdf_document,
1367                                                         page,
1368                                                         search_string);
1369 }
1370
1371 static int
1372 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1373 {
1374         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1375
1376         if (search) {
1377                 return g_list_length (search->pages[page]);
1378         } else {
1379                 return 0;
1380         }
1381 }
1382
1383 static gboolean
1384 pdf_document_find_get_result (EvDocumentFind *document_find,
1385                               int             page,
1386                               int             n_result,
1387                               EvRectangle    *rectangle)
1388 {
1389         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1390         PdfDocumentSearch *search = pdf_document->search;
1391         PopplerPage *poppler_page;
1392         PopplerRectangle *r;
1393         double height;
1394
1395         if (search == NULL)
1396                 return FALSE;
1397
1398         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1399                                                   n_result);
1400         if (r == NULL)
1401                 return FALSE;
1402
1403         poppler_page = poppler_document_get_page (pdf_document->document, page);
1404         poppler_page_get_size (poppler_page, NULL, &height);
1405         rectangle->x1 = r->x1;
1406         rectangle->y1 = height - r->y2;
1407         rectangle->x2 = r->x2;
1408         rectangle->y2 = height - r->y1;
1409         g_object_unref (poppler_page);
1410                 
1411         return TRUE;
1412 }
1413
1414 static int
1415 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1416                                     int             page)
1417 {
1418         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1419
1420         return search && search->pages[page] != NULL;
1421 }
1422
1423 static double
1424 pdf_document_find_get_progress (EvDocumentFind *document_find)
1425 {
1426         PdfDocumentSearch *search;
1427         int n_pages, pages_done;
1428
1429         search = PDF_DOCUMENT (document_find)->search;
1430
1431         if (search == NULL) {
1432                 return 0;
1433         }
1434
1435         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1436         if (search->search_page > search->start_page) {
1437                 pages_done = search->search_page - search->start_page + 1;
1438         } else if (search->search_page == search->start_page) {
1439                 pages_done = n_pages;
1440         } else {
1441                 pages_done = n_pages - search->start_page + search->search_page;
1442         }
1443
1444         return pages_done / (double) n_pages;
1445 }
1446
1447 static void
1448 pdf_document_find_cancel (EvDocumentFind *document)
1449 {
1450         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1451
1452         if (pdf_document->search) {
1453                 pdf_document_search_free (pdf_document->search);
1454                 pdf_document->search = NULL;
1455         }
1456 }
1457
1458 static void
1459 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1460 {
1461         iface->begin = pdf_document_find_begin;
1462         iface->get_n_results = pdf_document_find_get_n_results;
1463         iface->get_result = pdf_document_find_get_result;
1464         iface->page_has_results = pdf_document_find_page_has_results;
1465         iface->get_progress = pdf_document_find_get_progress;
1466         iface->cancel = pdf_document_find_cancel;
1467 }
1468
1469 static const gboolean supported_formats[] = {
1470         TRUE, /* EV_FILE_FORMAT_PS */
1471 #ifdef HAVE_CAIRO_PDF
1472 #ifdef HAVE_POPPLER_PAGE_RENDER
1473         TRUE, /* EV_FILE_FORMAT_PDF */
1474 #else
1475         FALSE, /* EV_FILE_FORMAT_PDF */
1476 #endif
1477 #endif
1478 };
1479
1480 static void
1481 pdf_print_context_free (PdfPrintContext *ctx)
1482 {
1483         if (!ctx)
1484                 return;
1485
1486         if (ctx->ps_file) {
1487                 poppler_ps_file_free (ctx->ps_file);
1488                 ctx->ps_file = NULL;
1489         }
1490 #ifdef HAVE_CAIRO_PDF
1491         if (ctx->pdf_cairo) {
1492                 cairo_destroy (ctx->pdf_cairo);
1493                 ctx->pdf_cairo = NULL;
1494         }
1495 #endif
1496         g_free (ctx);
1497 }
1498
1499 static gboolean
1500 pdf_document_file_exporter_format_supported (EvFileExporter      *exporter,
1501                                              EvFileExporterFormat format)
1502 {
1503         return supported_formats[format];
1504 }
1505
1506 static void
1507 pdf_document_file_exporter_begin (EvFileExporter      *exporter,
1508                                   EvFileExporterFormat format,
1509                                   const char          *filename,
1510                                   int                  first_page,
1511                                   int                  last_page,
1512                                   double               width,
1513                                   double               height,
1514                                   gboolean             duplex)
1515 {
1516         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1517         PdfPrintContext *ctx;
1518
1519         if (pdf_document->print_ctx)
1520                 pdf_print_context_free (pdf_document->print_ctx);
1521         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1522         ctx = pdf_document->print_ctx;
1523         ctx->format = format;
1524         
1525         switch (format) {
1526                 case EV_FILE_FORMAT_PS:
1527                         ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1528                                                             filename, first_page,
1529                                                             last_page - first_page + 1);
1530                         poppler_ps_file_set_paper_size (ctx->ps_file, width, height);
1531                         poppler_ps_file_set_duplex (ctx->ps_file, duplex);
1532
1533                         break;
1534                 case EV_FILE_FORMAT_PDF: {
1535 #ifdef HAVE_CAIRO_PDF
1536                         cairo_surface_t *surface;
1537                         
1538                         surface = cairo_pdf_surface_create (filename, width, height);
1539                         ctx->pdf_cairo = cairo_create (surface);
1540                         cairo_surface_destroy (surface);
1541 #endif
1542                 }
1543                         break;
1544                 default:
1545                         g_assert_not_reached ();
1546         }
1547 }
1548
1549 static void
1550 pdf_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
1551 {
1552         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1553         PdfPrintContext *ctx = pdf_document->print_ctx;
1554         PopplerPage *poppler_page;
1555
1556         g_return_if_fail (pdf_document->print_ctx != NULL);
1557
1558         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1559
1560         switch (ctx->format) {
1561                 case EV_FILE_FORMAT_PS:
1562                         poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1563                         break;
1564                 case EV_FILE_FORMAT_PDF:
1565 #ifdef HAVE_CAIRO_PDF
1566                         cairo_save (ctx->pdf_cairo);
1567 #endif
1568 #ifdef HAVE_POPPLER_PAGE_RENDER
1569                         poppler_page_render (poppler_page, ctx->pdf_cairo);
1570 #endif
1571 #ifdef HAVE_CAIRO_PDF
1572                         cairo_show_page (ctx->pdf_cairo);
1573                         cairo_restore (ctx->pdf_cairo);
1574 #endif
1575                         break;
1576                 default:
1577                         g_assert_not_reached ();
1578         }
1579         
1580         g_object_unref (poppler_page);
1581 }
1582
1583 static void
1584 pdf_document_file_exporter_end (EvFileExporter *exporter)
1585 {
1586         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1587
1588         pdf_print_context_free (pdf_document->print_ctx);
1589         pdf_document->print_ctx = NULL;
1590 }
1591
1592 static void
1593 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1594 {
1595         iface->format_supported = pdf_document_file_exporter_format_supported;
1596         iface->begin = pdf_document_file_exporter_begin;
1597         iface->do_page = pdf_document_file_exporter_do_page;
1598         iface->end = pdf_document_file_exporter_end;
1599 }
1600
1601 static void
1602 pdf_selection_render_selection (EvSelection      *selection,
1603                                 EvRenderContext  *rc,
1604                                 GdkPixbuf       **pixbuf,
1605                                 EvRectangle      *points,
1606                                 EvRectangle      *old_points,
1607                                 GdkColor        *text,
1608                                 GdkColor        *base)
1609 {
1610         PdfDocument *pdf_document;
1611         double width_points, height_points;
1612         gint width, height;
1613
1614         pdf_document = PDF_DOCUMENT (selection);
1615         set_rc_data (pdf_document, rc);
1616
1617         poppler_page_get_size (POPPLER_PAGE (rc->data), &width_points, &height_points);
1618         width = (int) ((width_points * rc->scale) + 0.5);
1619         height = (int) ((height_points * rc->scale) + 0.5);
1620
1621         if (*pixbuf == NULL) {
1622                 * pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1623                                            TRUE, 8,
1624                                            width, height);
1625         }
1626         
1627         poppler_page_render_selection (POPPLER_PAGE (rc->data),
1628                                        rc->scale, rc->rotation, *pixbuf,
1629                                        (PopplerRectangle *)points,
1630                                        (PopplerRectangle *)old_points,
1631                                        text,
1632                                        base);
1633 }
1634
1635
1636 static GdkRegion *
1637 pdf_selection_get_selection_region (EvSelection     *selection,
1638                                     EvRenderContext *rc,
1639                                     EvRectangle     *points)
1640 {
1641         PdfDocument *pdf_document;
1642         GdkRegion *retval;
1643
1644         pdf_document = PDF_DOCUMENT (selection);
1645
1646         set_rc_data (pdf_document, rc);
1647
1648         retval = poppler_page_get_selection_region ((PopplerPage *)rc->data, rc->scale, (PopplerRectangle *) points);
1649
1650         return retval;
1651 }
1652
1653 static GdkRegion *
1654 pdf_selection_get_selection_map (EvSelection     *selection,
1655                                  EvRenderContext *rc)
1656 {
1657         PdfDocument *pdf_document;
1658         PopplerPage *poppler_page;
1659         PopplerRectangle points;
1660         GdkRegion *retval;
1661
1662         pdf_document = PDF_DOCUMENT (selection);
1663         poppler_page = poppler_document_get_page (pdf_document->document,
1664                                                   rc->page);
1665
1666         points.x1 = 0.0;
1667         points.y1 = 0.0;
1668         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1669         retval = poppler_page_get_selection_region (poppler_page, 1.0, &points);
1670         g_object_unref (poppler_page);
1671
1672         return retval;
1673 }
1674
1675 static void
1676 pdf_selection_iface_init (EvSelectionIface *iface)
1677 {
1678         iface->render_selection = pdf_selection_render_selection;
1679         iface->get_selection_region = pdf_selection_get_selection_region;
1680         iface->get_selection_map = pdf_selection_get_selection_map;
1681 }
1682
1683 /* Page Transitions */
1684 static gdouble
1685 pdf_document_get_page_duration (EvDocumentTransition *trans,
1686                                 gint                  page)
1687 {
1688 #ifdef HAVE_POPPLER_PAGE_GET_DURATION   
1689         PdfDocument *pdf_document;
1690         PopplerPage *poppler_page;
1691         gdouble      duration = -1;
1692
1693         pdf_document = PDF_DOCUMENT (trans);
1694         poppler_page = poppler_document_get_page (pdf_document->document, page);
1695         if (!poppler_page)
1696                 return -1;
1697
1698         duration = poppler_page_get_duration (poppler_page);
1699         g_object_unref (poppler_page);
1700
1701         return duration;
1702 #else
1703         return -1;
1704 #endif /* HAVE_POPPLER_PAGE_GET_DURATION */
1705 }
1706
1707 static void
1708 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1709 {
1710         iface->get_page_duration = pdf_document_get_page_duration;
1711 }
1712
1713 PdfDocument *
1714 pdf_document_new (void)
1715 {
1716         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1717 }