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