]> www.fi.muni.cz Git - evince.git/blob - pdf/ev-poppler.cc
Fix for Bug 309080: crash on window close.
[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
37 typedef struct {
38         PdfDocument *document;
39         char *text;
40         GList **pages;
41         guint idle;
42         int start_page;
43         int search_page;
44 } PdfDocumentSearch;
45
46 struct _PdfDocumentClass
47 {
48         GObjectClass parent_class;
49 };
50
51 struct _PdfDocument
52 {
53         GObject parent_instance;
54
55         PopplerDocument *document;
56         PopplerPSFile *ps_file;
57         gchar *password;
58
59         PopplerOrientation orientation;
60         gboolean orientation_set;
61
62         PopplerFontInfo *font_info;
63         PopplerFontsIter *fonts_iter;
64         int fonts_scanned_pages;
65
66         PdfDocumentSearch *search;
67 };
68
69 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
70 static void pdf_document_security_iface_init            (EvDocumentSecurityIface   *iface);
71 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
72 static void pdf_document_document_links_iface_init      (EvDocumentLinksIface      *iface);
73 static void pdf_document_document_fonts_iface_init      (EvDocumentFontsIface      *iface);
74 static void pdf_document_find_iface_init                (EvDocumentFindIface       *iface);
75 static void pdf_document_ps_exporter_iface_init         (EvPSExporterIface         *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 EvLink * ev_link_from_action (PopplerAction *action);
82
83
84 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
85                          {
86                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
87                                                         pdf_document_document_iface_init);
88                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
89                                                         pdf_document_security_iface_init);
90                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
91                                                         pdf_document_document_thumbnails_iface_init);
92                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
93                                                         pdf_document_document_links_iface_init);
94                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
95                                                         pdf_document_document_fonts_iface_init);
96                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
97                                                         pdf_document_find_iface_init);
98                                  G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
99                                                         pdf_document_ps_exporter_iface_init);
100                          });
101
102 static void
103 pdf_document_dispose (GObject *object)
104 {
105         PdfDocument *pdf_document = PDF_DOCUMENT(object);
106
107         if (pdf_document->search) {
108                 pdf_document_search_free (pdf_document->search);
109                 pdf_document->search = NULL;
110         }
111
112         if (pdf_document->document) {
113                 g_object_unref (pdf_document->document);
114         }
115
116         if (pdf_document->font_info) { 
117                 poppler_font_info_free (pdf_document->font_info);
118         }
119
120         if (pdf_document->fonts_iter) {
121                 poppler_fonts_iter_free (pdf_document->fonts_iter);
122         }
123 }
124
125 static void
126 pdf_document_class_init (PdfDocumentClass *klass)
127 {
128         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
129
130         g_object_class->dispose = pdf_document_dispose;
131 }
132
133 static void
134 pdf_document_init (PdfDocument *pdf_document)
135 {
136         pdf_document->password = NULL;
137 }
138
139 static void
140 convert_error (GError  *poppler_error,
141                GError **error)
142 {
143         if (poppler_error == NULL)
144                 return;
145
146         if (poppler_error->domain == POPPLER_ERROR) {
147                 /* convert poppler errors into EvDocument errors */
148                 gint code = EV_DOCUMENT_ERROR_INVALID;
149                 if (poppler_error->code == POPPLER_ERROR_INVALID)
150                         code = EV_DOCUMENT_ERROR_INVALID;
151                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
152                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
153                         
154
155                 g_set_error (error,
156                              EV_DOCUMENT_ERROR,
157                              code,
158                              poppler_error->message,
159                              NULL);
160         } else {
161                 g_propagate_error (error, poppler_error);
162         }
163 }
164
165
166 /* EvDocument */
167 static gboolean
168 pdf_document_save (EvDocument  *document,
169                    const char  *uri,
170                    GError     **error)
171 {
172         gboolean retval;
173         GError *poppler_error = NULL;
174
175         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
176                                         uri,
177                                         &poppler_error);
178         if (! retval)
179                 convert_error (poppler_error, error);
180
181         return retval;
182 }
183
184 static PopplerOrientation
185 get_document_orientation (PdfDocument *pdf_document)
186 {
187         PopplerOrientation orientation;
188         PopplerPage *page;
189
190         /* Should prolly be smarter here and check more than first page */
191         page = poppler_document_get_page (pdf_document->document, 0);
192         if (page) {
193                 orientation = poppler_page_get_orientation (page);
194         } else {
195                 orientation = POPPLER_ORIENTATION_PORTRAIT;
196         }
197         g_object_unref (page);
198
199         return orientation;
200 }
201
202 static gboolean
203 pdf_document_load (EvDocument   *document,
204                    const char   *uri,
205                    GError      **error)
206 {
207         GError *poppler_error = NULL;
208         PdfDocument *pdf_document = PDF_DOCUMENT (document);
209
210         pdf_document->document =
211                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
212
213         if (pdf_document->document == NULL) {
214                 convert_error (poppler_error, error);
215                 return FALSE;
216         }
217
218         return TRUE;
219 }
220
221 static int
222 pdf_document_get_n_pages (EvDocument *document)
223 {
224         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
225 }
226
227 /* FIXME This should not be necessary, poppler should rember it */
228 static void
229 set_page_orientation (PdfDocument *pdf_document, PopplerPage *page)
230 {
231         if (pdf_document->orientation_set) {
232                 poppler_page_set_orientation (page, pdf_document->orientation);
233         }
234 }
235
236 static void
237 pdf_document_get_page_size (EvDocument   *document,
238                             int           page,
239                             double       *width,
240                             double       *height)
241 {
242         PdfDocument *pdf_document = PDF_DOCUMENT (document);
243         PopplerPage *poppler_page;
244
245         poppler_page = poppler_document_get_page (pdf_document->document, page);
246         set_page_orientation (pdf_document, poppler_page);
247         poppler_page_get_size (poppler_page, width, height);
248         g_object_unref (poppler_page);
249 }
250
251 static char *
252 pdf_document_get_page_label (EvDocument *document,
253                              int         page)
254 {
255         PopplerPage *poppler_page;
256         char *label = NULL;
257
258         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
259                                                   page);
260
261         g_object_get (G_OBJECT (poppler_page),
262                       "label", &label,
263                       NULL);
264         g_object_unref (poppler_page);
265
266         return label;
267 }
268
269 static GList *
270 pdf_document_get_links (EvDocument *document,
271                         int         page)
272 {
273         PdfDocument *pdf_document;
274         PopplerPage *poppler_page;
275         GList *retval = NULL;
276         GList *mapping_list;
277         GList *list;
278         double height;
279
280         pdf_document = PDF_DOCUMENT (document);
281         poppler_page = poppler_document_get_page (pdf_document->document,
282                                                   page);
283         mapping_list = poppler_page_get_link_mapping (poppler_page);
284         poppler_page_get_size (poppler_page, NULL, &height);
285
286         for (list = mapping_list; list; list = list->next) {
287                 PopplerLinkMapping *link_mapping;
288                 EvLinkMapping *ev_link_mapping;
289
290                 link_mapping = (PopplerLinkMapping *)list->data;
291                 ev_link_mapping = g_new (EvLinkMapping, 1);
292                 ev_link_mapping->link = ev_link_from_action (link_mapping->action);
293                 ev_link_mapping->x1 = link_mapping->area.x1;
294                 ev_link_mapping->x2 = link_mapping->area.x2;
295                 /* Invert this for X-style coordinates */
296                 ev_link_mapping->y1 = height - link_mapping->area.y2;
297                 ev_link_mapping->y2 = height - link_mapping->area.y1;
298
299                 retval = g_list_prepend (retval, ev_link_mapping);
300         }
301
302         poppler_page_free_link_mapping (mapping_list);
303         g_object_unref (poppler_page);
304
305         return g_list_reverse (retval);
306 }
307                         
308
309 static GdkPixbuf *
310 pdf_document_render_pixbuf (EvDocument   *document,
311                             int           page,
312                             double        scale)
313 {
314         PdfDocument *pdf_document;
315         PopplerPage *poppler_page;
316         GdkPixbuf *pixbuf;
317         double width_points, height_points;
318         gint width, height;
319
320         pdf_document = PDF_DOCUMENT (document);
321         poppler_page = poppler_document_get_page (pdf_document->document,
322                                                   page);
323         set_page_orientation (pdf_document, poppler_page);
324
325         poppler_page_get_size (poppler_page, &width_points, &height_points);
326         width = (int) ((width_points * scale) + 0.5);
327         height = (int) ((height_points * scale) + 0.5);
328
329         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
330                                  FALSE, 8,
331                                  width, height);
332
333         poppler_page_render_to_pixbuf (poppler_page,
334                                        0, 0,
335                                        width, height,
336                                        scale,
337                                        pixbuf,
338                                        0, 0);
339         
340         g_object_unref (poppler_page);
341         
342         return pixbuf;
343 }
344
345 /* EvDocumentSecurity */
346
347 static gboolean
348 pdf_document_has_document_security (EvDocumentSecurity *document_security)
349 {
350         /* FIXME: do we really need to have this? */
351         return FALSE;
352 }
353
354 static void
355 pdf_document_set_password (EvDocumentSecurity *document_security,
356                            const char         *password)
357 {
358         PdfDocument *document = PDF_DOCUMENT (document_security);
359
360         if (document->password)
361                 g_free (document->password);
362
363         document->password = g_strdup (password);
364 }
365
366 static gboolean
367 pdf_document_can_get_text (EvDocument *document)
368 {
369         return TRUE;
370 }
371
372 static EvDocumentInfo *
373 pdf_document_get_info (EvDocument *document)
374 {
375         EvDocumentInfo *info;
376         PopplerPageLayout layout;
377         PopplerPageMode mode;
378         PopplerViewerPreferences view_prefs;
379         PopplerPermissions permissions;
380
381         info = g_new0 (EvDocumentInfo, 1);
382
383         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
384                             EV_DOCUMENT_INFO_FORMAT |
385                             EV_DOCUMENT_INFO_AUTHOR |
386                             EV_DOCUMENT_INFO_SUBJECT |
387                             EV_DOCUMENT_INFO_KEYWORDS |
388                             EV_DOCUMENT_INFO_LAYOUT |
389                             EV_DOCUMENT_INFO_START_MODE |
390                             EV_DOCUMENT_INFO_PERMISSIONS |
391                             EV_DOCUMENT_INFO_UI_HINTS |
392                             EV_DOCUMENT_INFO_CREATOR |
393                             EV_DOCUMENT_INFO_PRODUCER |
394                             EV_DOCUMENT_INFO_CREATION_DATE |
395                             EV_DOCUMENT_INFO_MOD_DATE |
396                             EV_DOCUMENT_INFO_LINEARIZED |
397                             EV_DOCUMENT_INFO_N_PAGES |
398                             EV_DOCUMENT_INFO_SECURITY;
399
400
401         g_object_get (PDF_DOCUMENT (document)->document,
402                       "title", &(info->title),
403                       "format", &(info->format),
404                       "author", &(info->author),
405                       "subject", &(info->subject),
406                       "keywords", &(info->keywords),
407                       "page-mode", &mode,
408                       "page-layout", &layout,
409                       "viewer-preferences", &view_prefs,
410                       "permissions", &permissions,
411                       "creator", &(info->creator),
412                       "producer", &(info->producer),
413                       "creation-date", &(info->creation_date),
414                       "mod-date", &(info->modified_date),
415                       "linearized", &(info->linearized),
416                       NULL);
417
418         switch (layout) {
419                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
420                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
421                         break;
422                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
423                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
424                         break;
425                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
426                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
427                         break;
428                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
429                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
430                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
431                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
432                         break;
433                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
434                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
435                         break;
436                 default:
437                         break;
438         }
439
440         switch (mode) {
441                 case POPPLER_PAGE_MODE_NONE:
442                         info->mode = EV_DOCUMENT_MODE_NONE;
443                         break;
444                 case POPPLER_PAGE_MODE_USE_THUMBS:
445                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
446                         break;
447                 case POPPLER_PAGE_MODE_USE_OC:
448                         info->mode = EV_DOCUMENT_MODE_USE_OC;
449                         break;
450                 case POPPLER_PAGE_MODE_FULL_SCREEN:
451                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
452                         break;
453                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
454                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
455                 default:
456                         break;
457         }
458
459         info->ui_hints = 0;
460         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
461                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
462         }
463         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
464                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
465         }
466         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
467                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
468         }
469         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
470                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
471         }
472         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
473                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
474         }
475         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
476                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
477         }
478         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
479                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
480         }
481
482         info->permissions = 0;
483         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
484                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
485         }
486         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
487                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
488         }
489         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
490                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
491         }
492         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
493                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
494         }
495
496         info->n_pages = ev_document_get_n_pages (document);
497
498         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
499                 /* translators: this is the document security state */
500                 info->security = g_strdup (_("Yes"));
501         } else {
502                 /* translators: this is the document security state */
503                 info->security = g_strdup (_("No"));
504         }
505
506         return info;
507 }
508
509 static char *
510 pdf_document_get_text (EvDocument *document, int page, EvRectangle *rect)
511 {
512         PdfDocument *pdf_document = PDF_DOCUMENT (document);
513         PopplerPage *poppler_page;
514         PopplerRectangle r;
515         double height;
516         char *text;
517         
518         poppler_page = poppler_document_get_page (pdf_document->document, page);
519         set_page_orientation (pdf_document, poppler_page);
520         g_return_val_if_fail (poppler_page != NULL, NULL);
521
522         poppler_page_get_size (poppler_page, NULL, &height);
523         r.x1 = rect->x1;
524         r.y1 = height - rect->y2;
525         r.x2 = rect->x2;
526         r.y2 = height - rect->y1;
527
528         text = poppler_page_get_text (poppler_page, &r);
529
530         g_object_unref (poppler_page);
531
532         return text;
533 }
534
535 static EvOrientation
536 pdf_document_get_orientation (EvDocument *document)
537 {
538         EvOrientation result;
539         PdfDocument *pdf_document = PDF_DOCUMENT (document);
540
541         if (!pdf_document->orientation_set) {
542                 pdf_document->orientation = get_document_orientation (pdf_document);
543         }
544         
545         switch (pdf_document->orientation) {
546                 case POPPLER_ORIENTATION_PORTRAIT:
547                         result = EV_ORIENTATION_PORTRAIT;
548                         break;
549                 case POPPLER_ORIENTATION_LANDSCAPE:
550                         result = EV_ORIENTATION_LANDSCAPE;
551                         break;
552                 case POPPLER_ORIENTATION_UPSIDEDOWN:
553                         result = EV_ORIENTATION_UPSIDEDOWN;
554                         break;
555                 case POPPLER_ORIENTATION_SEASCAPE:
556                         result = EV_ORIENTATION_SEASCAPE;
557                         break;
558         }
559
560         return result;
561 }
562
563 static void
564 pdf_document_set_orientation (EvDocument *document, EvOrientation orientation)
565 {
566         PdfDocument *pdf_document = PDF_DOCUMENT (document);
567         PopplerOrientation poppler_orientation;
568
569         switch (orientation) {
570                 case EV_ORIENTATION_PORTRAIT:
571                         poppler_orientation = POPPLER_ORIENTATION_PORTRAIT;
572                         break;
573                 case EV_ORIENTATION_LANDSCAPE:
574                         poppler_orientation = POPPLER_ORIENTATION_LANDSCAPE;
575                         break;
576                 case EV_ORIENTATION_UPSIDEDOWN:
577                         poppler_orientation = POPPLER_ORIENTATION_UPSIDEDOWN;
578                         break;
579                 case EV_ORIENTATION_SEASCAPE:
580                         poppler_orientation = POPPLER_ORIENTATION_SEASCAPE;
581                         break;
582         }
583
584         pdf_document->orientation = poppler_orientation;
585         pdf_document->orientation_set = TRUE;
586 }
587
588 static void
589 pdf_document_document_iface_init (EvDocumentIface *iface)
590 {
591         iface->save = pdf_document_save;
592         iface->load = pdf_document_load;
593         iface->get_n_pages = pdf_document_get_n_pages;
594         iface->get_page_size = pdf_document_get_page_size;
595         iface->get_page_label = pdf_document_get_page_label;
596         iface->get_links = pdf_document_get_links;
597         iface->render_pixbuf = pdf_document_render_pixbuf;
598         iface->get_text = pdf_document_get_text;
599         iface->can_get_text = pdf_document_can_get_text;
600         iface->get_info = pdf_document_get_info;
601         iface->set_orientation = pdf_document_set_orientation;
602         iface->get_orientation = pdf_document_get_orientation;
603 };
604
605 static void
606 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
607 {
608         iface->has_document_security = pdf_document_has_document_security;
609         iface->set_password = pdf_document_set_password;
610 }
611
612 static gdouble
613 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
614 {
615         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
616         int n_pages;
617
618         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
619
620         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
621 }
622
623 static gboolean
624 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
625                          int              n_pages)
626 {
627         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
628         gboolean result;
629
630         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
631
632         if (pdf_document->font_info == NULL) { 
633                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
634         }
635
636         if (pdf_document->fonts_iter) {
637                 poppler_fonts_iter_free (pdf_document->fonts_iter);
638         }
639
640         pdf_document->fonts_scanned_pages += n_pages;
641
642         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
643                                          &pdf_document->fonts_iter);
644         if (!result) {
645                 pdf_document->fonts_scanned_pages = 0;
646                 poppler_font_info_free (pdf_document->font_info);
647                 pdf_document->font_info = NULL; 
648         }
649
650         return result;
651 }
652
653 static void
654 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
655                                GtkTreeModel    *model)
656 {
657         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
658         PopplerFontsIter *iter = pdf_document->fonts_iter;
659
660         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
661
662         if (iter) {
663                 do {
664                         GtkTreeIter list_iter;
665                         PopplerIndexIter *child;
666                         const char *name;
667                 
668                         name = poppler_fonts_iter_get_name (iter);
669                         if (name == NULL) {
670                                 name = _("No name");
671                         }
672
673                         gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
674                         gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
675                                             EV_DOCUMENT_FONTS_COLUMN_NAME, name,
676                                             -1);
677                 } while (poppler_fonts_iter_next (iter));
678         }
679 }
680
681 static void
682 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
683 {
684         iface->fill_model = pdf_document_fonts_fill_model;
685         iface->scan = pdf_document_fonts_scan;
686         iface->get_progress = pdf_document_fonts_get_progress;
687 }
688
689 static gboolean
690 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
691 {
692         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
693         PopplerIndexIter *iter;
694
695         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
696
697         iter = poppler_index_iter_new (pdf_document->document);
698         if (iter == NULL)
699                 return FALSE;
700         poppler_index_iter_free (iter);
701
702         return TRUE;
703 }
704
705 static EvLink *
706 ev_link_from_action (PopplerAction *action)
707 {
708         EvLink *link;
709         const char *title;
710
711         title = action->any.title;
712         
713         if (action->type == POPPLER_ACTION_GOTO_DEST) {
714                 link = ev_link_new_page (title, action->goto_dest.dest->page_num - 1);
715         } else if (action->type == POPPLER_ACTION_URI) {
716                 link = ev_link_new_external (title, action->uri.uri);
717         } else {
718                 link = ev_link_new_title (title);
719         }
720
721         return link;    
722 }
723
724 static void
725 build_tree (PdfDocument      *pdf_document,
726             GtkTreeModel     *model,
727             GtkTreeIter      *parent,
728             PopplerIndexIter *iter)
729 {
730
731         do {
732                 GtkTreeIter tree_iter;
733                 PopplerIndexIter *child;
734                 PopplerAction *action;
735                 EvLink *link;
736                 gboolean expand;
737                 
738                 action = poppler_index_iter_get_action (iter);
739                 expand = poppler_index_iter_is_open (iter);
740                 if (action) {
741                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
742                         link = ev_link_from_action (action);
743                         poppler_action_free (action);
744
745                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
746                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, ev_link_get_title (link),
747                                             EV_DOCUMENT_LINKS_COLUMN_LINK, link,
748                                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
749                                             -1);
750                         g_object_unref (link);
751                         child = poppler_index_iter_get_child (iter);
752                         if (child)
753                                 build_tree (pdf_document, model, &tree_iter, child);
754                         poppler_index_iter_free (child);
755                 }
756         } while (poppler_index_iter_next (iter));
757 }
758
759
760 static GtkTreeModel *
761 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
762 {
763         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
764         GtkTreeModel *model = NULL;
765         PopplerIndexIter *iter;
766
767         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
768
769         iter = poppler_index_iter_new (pdf_document->document);
770         /* Create the model if we have items*/
771         if (iter != NULL) {
772                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
773                                                              G_TYPE_STRING,
774                                                              G_TYPE_OBJECT,
775                                                              G_TYPE_BOOLEAN);
776                 build_tree (pdf_document, model, NULL, iter);
777                 poppler_index_iter_free (iter);
778         }
779         
780         return model;
781 }
782
783 static void
784 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
785 {
786         iface->has_document_links = pdf_document_links_has_document_links;
787         iface->get_links_model = pdf_document_links_get_links_model;
788 }
789
790 static GdkPixbuf *
791 make_thumbnail_for_size (PdfDocument *pdf_document,
792                          gint         page,
793                          gint         size,
794                          gboolean     border)
795 {
796         PopplerPage *poppler_page;
797         GdkPixbuf *pixbuf;
798         int width, height;
799         int x_offset, y_offset;
800         double scale;
801         gdouble unscaled_width, unscaled_height;
802
803         poppler_page = poppler_document_get_page (pdf_document->document, page);
804         set_page_orientation (pdf_document, poppler_page);
805         g_return_val_if_fail (poppler_page != NULL, NULL);
806
807         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document), page, size, &width, &height);
808         poppler_page_get_size (poppler_page, &unscaled_width, &unscaled_height);
809         scale = width / unscaled_width;
810
811         if (border) {
812                 pixbuf = ev_document_misc_get_thumbnail_frame (width, height, NULL);
813                 x_offset = 1;
814                 y_offset = 1;
815         } else {
816                 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
817                                          width, height);
818                 gdk_pixbuf_fill (pixbuf, 0xffffffff);
819                 x_offset = 0;
820                 y_offset = 0;
821         }
822
823         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
824                                        width, height,
825                                        scale, pixbuf,
826                                        x_offset, y_offset);
827
828         g_object_unref (poppler_page);
829         return pixbuf;
830 }
831
832 static GdkPixbuf *
833 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
834                                        gint                  page,
835                                        gint                  size,
836                                        gboolean              border)
837 {
838         PdfDocument *pdf_document;
839         PopplerPage *poppler_page;
840         GdkPixbuf *pixbuf;
841
842         pdf_document = PDF_DOCUMENT (document_thumbnails);
843
844         poppler_page = poppler_document_get_page (pdf_document->document, page);
845         set_page_orientation (pdf_document, poppler_page);
846         g_return_val_if_fail (poppler_page != NULL, NULL);
847
848         pixbuf = poppler_page_get_thumbnail (poppler_page);
849         
850         if (pixbuf != NULL) {
851                 /* The document provides its own thumbnails. */
852                 if (border) {
853                         GdkPixbuf *real_pixbuf;
854
855                         real_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
856                         g_object_unref (pixbuf);
857                         pixbuf = real_pixbuf;
858                 }
859         } else {
860                 /* There is no provided thumbnail.  We need to make one. */
861                 pixbuf = make_thumbnail_for_size (pdf_document, page, size, border);
862         }
863
864         g_object_unref (poppler_page);
865         
866         return pixbuf;
867 }
868
869 static void
870 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
871                                         gint                  page,
872                                         gint                  size,
873                                         gint                 *width,
874                                         gint                 *height)
875 {
876         PdfDocument *pdf_document;
877         PopplerPage *poppler_page;
878         gint has_thumb;
879         
880         pdf_document = PDF_DOCUMENT (document_thumbnails);
881         poppler_page = poppler_document_get_page (pdf_document->document, page);
882         set_page_orientation (pdf_document, poppler_page);
883
884         g_return_if_fail (width != NULL);
885         g_return_if_fail (height != NULL);
886         g_return_if_fail (poppler_page != NULL);
887
888         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
889
890         if (!has_thumb) {
891                 double page_width, page_height;
892
893                 poppler_page_get_size (poppler_page, &page_width, &page_height);
894                 if (page_width > page_height) {
895                         *width = size;
896                         *height = (int) (size * page_height / page_width);
897                 } else {
898                         *width = (int) (size * page_width / page_height);
899                         *height = size;
900                 }
901         }
902         g_object_unref (poppler_page);
903 }
904
905 static void
906 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
907 {
908         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
909         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
910 }
911
912
913 static gboolean
914 pdf_document_search_idle_callback (void *data)
915 {
916         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
917         PdfDocument *pdf_document = search->document;
918         int n_pages;
919         GList *matches;
920         PopplerPage *page;
921
922         page = poppler_document_get_page (search->document->document,
923                                           search->search_page);
924         set_page_orientation (pdf_document, page);
925
926         ev_document_doc_mutex_lock ();
927         matches = poppler_page_find_text (page, search->text);
928         ev_document_doc_mutex_unlock ();
929
930         g_object_unref (page);
931
932         search->pages[search->search_page] = matches;
933         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
934                                   search->search_page);
935
936         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
937         search->search_page += 1;
938         if (search->search_page == n_pages) {
939                 /* wrap around */
940                 search->search_page = 0;
941         }
942
943         if (search->search_page != search->start_page) {
944                 return TRUE;
945         }
946
947         /* We're done. */
948         search->idle = 0; /* will return FALSE to remove */
949         return FALSE;
950 }
951
952
953 static PdfDocumentSearch *
954 pdf_document_search_new (PdfDocument *pdf_document,
955                          int          start_page,
956                          const char  *text)
957 {
958         PdfDocumentSearch *search;
959         int n_pages;
960         int i;
961
962         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
963
964         search = g_new0 (PdfDocumentSearch, 1);
965
966         search->text = g_strdup (text);
967         search->pages = g_new0 (GList *, n_pages);
968         for (i = 0; i < n_pages; i++) {
969                 search->pages[i] = NULL;
970         }
971
972         search->document = pdf_document;
973
974         /* We add at low priority so the progress bar repaints */
975         search->idle = g_idle_add_full (G_PRIORITY_LOW,
976                                         pdf_document_search_idle_callback,
977                                         search,
978                                         NULL);
979
980         search->start_page = start_page;
981         search->search_page = start_page;
982
983         return search;
984 }
985
986 static void
987 pdf_document_search_free (PdfDocumentSearch   *search)
988 {
989         PdfDocument *pdf_document = search->document;
990         int n_pages;
991         int i;
992
993         if (search->idle != 0)
994                 g_source_remove (search->idle);
995
996         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
997         for (i = 0; i < n_pages; i++) {
998                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
999                 g_list_free (search->pages[i]);
1000         }
1001         
1002         g_free (search->text);
1003 }
1004
1005 static void
1006 pdf_document_find_begin (EvDocumentFind   *document,
1007                          int               page,
1008                          const char       *search_string,
1009                          gboolean          case_sensitive)
1010 {
1011         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1012
1013         /* FIXME handle case_sensitive (right now XPDF
1014          * code is always case insensitive for ASCII
1015          * and case sensitive for all other languaages)
1016          */
1017
1018         if (pdf_document->search &&
1019             strcmp (search_string, pdf_document->search->text) == 0)
1020                 return;
1021
1022         if (pdf_document->search)
1023                 pdf_document_search_free (pdf_document->search);
1024
1025         pdf_document->search = pdf_document_search_new (pdf_document,
1026                                                         page,
1027                                                         search_string);
1028 }
1029
1030 int
1031 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1032 {
1033         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1034
1035         if (search) {
1036                 return g_list_length (search->pages[page]);
1037         } else {
1038                 return 0;
1039         }
1040 }
1041
1042 gboolean
1043 pdf_document_find_get_result (EvDocumentFind *document_find,
1044                               int             page,
1045                               int             n_result,
1046                               EvRectangle    *rectangle)
1047 {
1048         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1049         PdfDocumentSearch *search = pdf_document->search;
1050         PopplerPage *poppler_page;
1051         PopplerRectangle *r;
1052         double height;
1053
1054         if (search == NULL)
1055                 return FALSE;
1056
1057         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1058                                                   n_result);
1059         if (r == NULL)
1060                 return FALSE;
1061
1062         poppler_page = poppler_document_get_page (pdf_document->document, page);
1063         set_page_orientation (pdf_document, poppler_page);
1064         poppler_page_get_size (poppler_page, NULL, &height);
1065         rectangle->x1 = r->x1;
1066         rectangle->y1 = height - r->y2;
1067         rectangle->x2 = r->x2;
1068         rectangle->y2 = height - r->y1;
1069         g_object_unref (poppler_page);
1070                 
1071         return TRUE;
1072 }
1073
1074 int
1075 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1076                                     int             page)
1077 {
1078         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1079
1080         g_return_val_if_fail (search != NULL, FALSE);
1081
1082         return search->pages[page] != NULL;
1083 }
1084
1085 double
1086 pdf_document_find_get_progress (EvDocumentFind *document_find)
1087 {
1088         PdfDocumentSearch *search;
1089         int n_pages, pages_done;
1090
1091         search = PDF_DOCUMENT (document_find)->search;
1092
1093         if (search == NULL) {
1094                 return 0;
1095         }
1096
1097         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1098         if (search->search_page > search->start_page) {
1099                 pages_done = search->search_page - search->start_page + 1;
1100         } else if (search->search_page == search->start_page) {
1101                 pages_done = n_pages;
1102         } else {
1103                 pages_done = n_pages - search->start_page + search->search_page;
1104         }
1105
1106         return pages_done / (double) n_pages;
1107 }
1108
1109 static void
1110 pdf_document_find_cancel (EvDocumentFind *document)
1111 {
1112         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1113
1114         if (pdf_document->search) {
1115                 pdf_document_search_free (pdf_document->search);
1116                 pdf_document->search = NULL;
1117         }
1118 }
1119
1120 static void
1121 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1122 {
1123         iface->begin = pdf_document_find_begin;
1124         iface->get_n_results = pdf_document_find_get_n_results;
1125         iface->get_result = pdf_document_find_get_result;
1126         iface->page_has_results = pdf_document_find_page_has_results;
1127         iface->get_progress = pdf_document_find_get_progress;
1128         iface->cancel = pdf_document_find_cancel;
1129 }
1130
1131 static void
1132 pdf_document_ps_exporter_begin (EvPSExporter *exporter, const char *filename,
1133                                 int first_page, int last_page)
1134 {
1135         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1136         
1137         pdf_document->ps_file = poppler_ps_file_new (pdf_document->document, filename,
1138                                                      first_page,
1139                                                      last_page - first_page + 1);
1140 }
1141
1142 static void
1143 pdf_document_ps_exporter_do_page (EvPSExporter *exporter, int page)
1144 {
1145         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1146         PopplerPage *poppler_page;
1147
1148         g_return_if_fail (pdf_document->ps_file != NULL);
1149
1150         poppler_page = poppler_document_get_page (pdf_document->document, page);
1151         set_page_orientation (pdf_document, poppler_page);
1152         poppler_page_render_to_ps (poppler_page, pdf_document->ps_file);
1153         g_object_unref (poppler_page);
1154 }
1155
1156 static void
1157 pdf_document_ps_exporter_end (EvPSExporter *exporter)
1158 {
1159         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1160
1161         poppler_ps_file_free (pdf_document->ps_file);
1162         pdf_document->ps_file = NULL;
1163 }
1164
1165 static void
1166 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
1167 {
1168         iface->begin = pdf_document_ps_exporter_begin;
1169         iface->do_page = pdf_document_ps_exporter_do_page;
1170         iface->end = pdf_document_ps_exporter_end;
1171 }
1172
1173 PdfDocument *
1174 pdf_document_new (void)
1175 {
1176         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1177 }