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