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