]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdf-document.cc
76229dbcfedeb9b530d580f2cc30ae46b4db4553
[evince.git] / pdf / xpdf / pdf-document.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 <glib/gi18n.h>
21
22 #include "gpdf-g-switch.h"
23 #include "pdf-document.h"
24 #include "ev-ps-exporter.h"
25 #include "ev-document-find.h"
26 #include "gpdf-g-switch.h"
27 #include "ev-document-bookmarks.h"
28 #include "ev-document-misc.h"
29 #include "ev-document-thumbnails.h"
30
31 #include "GlobalParams.h"
32 #include "GDKSplashOutputDev.h"
33 #include "PDFDoc.h"
34 #include "Outline.h"
35 #include "UnicodeMap.h"
36 #include "GlobalParams.h"
37 #include "GfxState.h"
38 #include "Thumb.h"
39 #include "goo/GList.h"
40 #include "PSOutputDev.h"
41
42 enum {
43         PROP_0,
44         PROP_TITLE
45 };
46
47 typedef struct
48 {
49         PdfDocument *document;
50         gunichar *ucs4;
51         glong ucs4_len;
52         guint idle;
53         /* full results are only possible for the rendered current page */
54         int current_page;
55         GArray *current_page_results;
56         guchar *other_page_flags; /* length n_pages + 1, first element ignored */
57         int start_page;   /* skip this one as we iterate, since we did it first */
58         int search_page;  /* the page we're searching now */
59         TextOutputDev *output_dev;
60 } PdfDocumentSearch;
61
62 typedef struct _PdfDocumentClass PdfDocumentClass;
63
64 #define PDF_DOCUMENT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), PDF_TYPE_DOCUMENT, PdfDocumentClass))
65 #define PDF_IS_DOCUMENT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), PDF_TYPE_DOCUMENT))
66 #define PDF_DOCUMENT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), PDF_TYPE_DOCUMENT, PdfDocumentClass))
67
68 struct _PdfDocumentClass
69 {
70         GObjectClass parent_class;
71 };
72
73 struct _PdfDocument
74 {
75         GObject parent_instance;
76
77         int page;
78         int page_x_offset;
79         int page_y_offset;
80         double scale;
81         GdkDrawable *target;
82
83         GDKSplashOutputDev *out;
84         PSOutputDev *ps_out;
85         PDFDoc *doc;
86         UnicodeMap *umap;
87
88         gboolean page_valid;
89
90         PdfDocumentSearch *search;
91 };
92
93 static void pdf_document_document_bookmarks_iface_init  (EvDocumentBookmarksIface  *iface);
94 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
95 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
96 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface   *iface);
97 static void pdf_document_find_iface_init        (EvDocumentFindIface *iface);
98 static void pdf_document_search_free            (PdfDocumentSearch   *search);
99 static void pdf_document_search_page_changed    (PdfDocumentSearch   *search);
100
101 G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
102                          {
103                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
104                                                         pdf_document_document_iface_init);
105                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_BOOKMARKS,
106                                                         pdf_document_document_bookmarks_iface_init);
107                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
108                                                         pdf_document_document_thumbnails_iface_init);
109                                  G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
110                                                         pdf_document_ps_exporter_iface_init);
111                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
112                                                         pdf_document_find_iface_init);
113                          });
114
115 static gboolean
116 document_validate_page (PdfDocument *pdf_document)
117 {
118         if (!pdf_document->page_valid) {
119                 pdf_document->doc->displayPage (pdf_document->out, pdf_document->page,
120                                                 72 * pdf_document->scale,
121                                                 72 * pdf_document->scale,
122                                                 0, gTrue, gTrue);
123
124                 pdf_document->page_valid = TRUE;
125
126                 /* Update the search results available to the app since
127                  * we only provide full results on the current page
128                  */
129                 if (pdf_document->search)
130                         pdf_document_search_page_changed (pdf_document->search);
131         }
132
133         return pdf_document->page_valid;
134 }
135
136 static gboolean
137 pdf_document_load (EvDocument  *document,
138                    const char  *uri,
139                    GError     **error)
140 {
141         PdfDocument *pdf_document = PDF_DOCUMENT (document);
142         PDFDoc *newDoc;
143         int err;
144         char *filename;
145         GString *filename_g;
146         GString *enc;
147
148         if (!globalParams) {
149                 globalParams = new GlobalParams("/etc/xpdfrc");
150                 globalParams->setupBaseFontsFc(NULL);
151         }
152
153         if (! pdf_document->umap) {
154                 enc = new GString("UTF-8");
155                 pdf_document->umap = globalParams->getUnicodeMap(enc);
156                 pdf_document->umap->incRefCnt ();
157                 delete enc;
158         }
159
160         filename = g_filename_from_uri (uri, NULL, error);
161         if (!filename)
162                 return FALSE;
163
164         filename_g = new GString (filename);
165         g_free (filename);
166
167         // open the PDF file, assumes ownership of filename_g
168         newDoc = new PDFDoc(filename_g, 0, 0);
169
170         if (!newDoc->isOk()) {
171                 err = newDoc->getErrorCode();
172                 delete newDoc;
173
174                 /* FIXME: Add a real error enum to EvDocument */
175                 g_set_error (error, G_FILE_ERROR,
176                              G_FILE_ERROR_FAILED,
177                              "Failed to load document (error %d) '%s'\n",
178                              err,
179                              uri);
180
181                 return FALSE;
182         }
183
184         if (pdf_document->doc)
185                 delete pdf_document->doc;
186         pdf_document->doc = newDoc;
187
188         pdf_document->page = 1;
189
190         if (pdf_document->out)
191                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
192
193         pdf_document->page_valid = FALSE;
194
195         g_object_notify (G_OBJECT (pdf_document), "title");
196
197         return TRUE;
198 }
199
200 static int
201 pdf_document_get_n_pages (EvDocument  *document)
202 {
203         PdfDocument *pdf_document = PDF_DOCUMENT (document);
204
205         if (pdf_document->doc)
206                 return pdf_document->doc->getNumPages();
207         else
208                 return 1;
209 }
210
211 static void
212 pdf_document_set_page (EvDocument  *document,
213                        int          page)
214 {
215         PdfDocument *pdf_document = PDF_DOCUMENT (document);
216
217         page = CLAMP (page, 1, ev_document_get_n_pages (document));
218
219         if (page != pdf_document->page) {
220                 pdf_document->page = page;
221                 pdf_document->page_valid = FALSE;
222         }
223
224         ev_document_changed (document);
225 }
226
227 static int
228 pdf_document_get_page (EvDocument  *document)
229 {
230         PdfDocument *pdf_document = PDF_DOCUMENT (document);
231
232         return pdf_document->page;
233 }
234
235 static void
236 redraw_callback (void *data)
237 {
238         /* Need to hook up through a EvDocument callback? */
239 }
240
241 static void
242 pdf_document_set_target (EvDocument  *document,
243                          GdkDrawable *target)
244 {
245         PdfDocument *pdf_document = PDF_DOCUMENT (document);
246
247         if (pdf_document->target != target) {
248                 if (pdf_document->target)
249                         g_object_unref (pdf_document->target);
250
251                 pdf_document->target = target;
252
253                 if (pdf_document->target)
254                         g_object_ref (pdf_document->target);
255
256                 if (pdf_document->out) {
257                         delete pdf_document->out;
258                         pdf_document->out = NULL;
259                 }
260
261                 if (pdf_document->target) {
262                         pdf_document->out = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
263                                                          redraw_callback, (void*) document);
264
265                         if (pdf_document->doc)
266                                 pdf_document->out->startDoc(pdf_document->doc->getXRef());
267
268                 }
269
270                 pdf_document->page_valid = FALSE;
271         }
272 }
273
274 static void
275 pdf_document_set_scale (EvDocument  *document,
276                         double       scale)
277 {
278         PdfDocument *pdf_document = PDF_DOCUMENT (document);
279
280         if (pdf_document->scale != scale) {
281                 pdf_document->scale = scale;
282                 pdf_document->page_valid = FALSE;
283         }
284 }
285
286 static void
287 pdf_document_set_page_offset (EvDocument  *document,
288                               int          x,
289                               int          y)
290 {
291         PdfDocument *pdf_document = PDF_DOCUMENT (document);
292
293         pdf_document->page_x_offset = x;
294         pdf_document->page_y_offset = y;
295 }
296
297 static void
298 pdf_document_get_page_size (EvDocument   *document,
299                             int          *width,
300                             int          *height)
301 {
302         PdfDocument *pdf_document = PDF_DOCUMENT (document);
303
304         if (document_validate_page (pdf_document)) {
305                 if (width)
306                         *width = pdf_document->out->getBitmapWidth();
307                 if (height)
308                         *height = pdf_document->out->getBitmapHeight();
309         } else {
310                 if (width)
311                         *width = 1;
312                 if (height)
313                         *height = 1;
314         }
315 }
316
317 static void
318 pdf_document_render (EvDocument  *document,
319                      int          clip_x,
320                      int          clip_y,
321                      int          clip_width,
322                      int          clip_height)
323 {
324         PdfDocument *pdf_document = PDF_DOCUMENT (document);
325         GdkRectangle page;
326         GdkRectangle draw;
327
328         if (!document_validate_page (pdf_document) || !pdf_document->target)
329                 return;
330
331         page.x = pdf_document->page_x_offset;
332         page.y = pdf_document->page_y_offset;
333         page.width = pdf_document->out->getBitmapWidth();
334         page.height = pdf_document->out->getBitmapHeight();
335
336         draw.x = clip_x;
337         draw.y = clip_y;
338         draw.width = clip_width;
339         draw.height = clip_height;
340
341         if (gdk_rectangle_intersect (&page, &draw, &draw))
342                 pdf_document->out->redraw (draw.x - page.x, draw.y - page.y,
343                                            pdf_document->target,
344                                            draw.x, draw.y,
345                                            draw.width, draw.height);
346 }
347
348 static void
349 pdf_document_search_emit_found (PdfDocumentSearch *search)
350 {
351         PdfDocument *pdf_document = search->document;
352         int n_pages;
353         int pages_done;
354         GArray *tmp_results;
355         int i;
356
357         n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
358         if (search->search_page > search->start_page) {
359                 pages_done = search->search_page - search->start_page;
360         } else if (search->search_page == search->start_page) {
361                 pages_done = n_pages;
362         } else {
363                 pages_done = n_pages - search->start_page + search->search_page;
364         }
365
366         tmp_results = g_array_new (FALSE, FALSE, sizeof (EvFindResult));
367         g_array_append_vals (tmp_results,
368                              search->current_page_results->data,
369                              search->current_page_results->len);
370
371         /* Now append a bogus element for each page that has a result in it,
372          * that is not the current page
373          */
374         i = 1;
375         while (i <= n_pages) {
376                 if (i != pdf_document->page &&
377                     search->other_page_flags[i]) {
378                         EvFindResult result;
379
380                         result.page_num = i;
381
382                         /* Use bogus coordinates, again we can't get coordinates
383                          * until this is the current page because TextOutputDev
384                          * isn't good enough
385                          */
386                         result.highlight_area.x = -1;
387                         result.highlight_area.y = -1;
388                         result.highlight_area.width = 1;
389                         result.highlight_area.height = 1;
390
391                         g_array_append_val (tmp_results, result);
392                 }
393
394                 ++i;
395         }
396
397         ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
398                                 (EvFindResult*) tmp_results->data,
399                                 tmp_results->len,
400                                 pages_done / (double) n_pages);
401
402         g_array_free (tmp_results, TRUE);
403 }
404
405 static void
406 pdf_document_search_page_changed (PdfDocumentSearch   *search)
407 {
408         PdfDocument *pdf_document = search->document;
409         int current_page;
410         EvFindResult result;
411         int xMin, yMin, xMax, yMax;
412
413         current_page = pdf_document->page;
414
415         if (!pdf_document->page_valid) {
416                 /* we can't do anything until displayPage() */
417                 search->current_page = -1;
418                 return;
419         }
420
421         if (search->current_page == current_page)
422                 return;
423
424         /* We need to create current_page_results for the new current page */
425         g_array_set_size (search->current_page_results, 0);
426
427         if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
428                                          gTrue, gTrue, // startAtTop, stopAtBottom
429                                          gFalse, gFalse, // startAtLast, stopAtLast
430                                          &xMin, &yMin, &xMax, &yMax)) {
431                 result.page_num = pdf_document->page;
432
433                 result.highlight_area.x = xMin;
434                 result.highlight_area.y = yMin;
435                 result.highlight_area.width = xMax - xMin;
436                 result.highlight_area.height = yMax - yMin;
437
438                 g_array_append_val (search->current_page_results, result);
439                 /* Now find further results */
440
441                 while (pdf_document->out->findText (search->ucs4, search->ucs4_len,
442                                                     gFalse, gTrue,
443                                                     gTrue, gFalse,
444                                                     &xMin, &yMin, &xMax, &yMax)) {
445
446                         result.page_num = pdf_document->page;
447
448                         result.highlight_area.x = xMin;
449                         result.highlight_area.y = yMin;
450                         result.highlight_area.width = xMax - xMin;
451                         result.highlight_area.height = yMax - yMin;
452
453                         g_array_append_val (search->current_page_results, result);
454                 }
455         }
456
457         /* needed for the initial current page since we don't search
458          * it in the idle
459          */
460         search->other_page_flags[current_page] =
461                 search->current_page_results->len > 0;
462
463         pdf_document_search_emit_found (search);
464 }
465
466 static gboolean
467 pdf_document_search_idle_callback (void *data)
468 {
469         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
470         PdfDocument *pdf_document = search->document;
471         int n_pages;
472         double xMin, yMin, xMax, yMax;
473
474         /* Note that PDF page count is 1 through n_pages INCLUSIVE
475          * like a real book. We are looking to add one result for each
476          * page with a match, because the coordinates are meaningless
477          * with TextOutputDev, so we just want to flag matching pages
478          * and then when the user switches to the current page, we
479          * will emit "found" again with the real results.
480          */
481         n_pages = ev_document_get_n_pages (EV_DOCUMENT (search->document));
482
483         if (search->search_page == search->start_page) {
484                 goto end_search;
485         }
486
487         if (search->output_dev == 0) {
488                 /* First time through here... */
489                 search->output_dev = new TextOutputDev (NULL, gTrue, gFalse, gFalse);
490                 if (!search->output_dev->isOk()) {
491                         goto end_search;
492                 }
493         }
494
495         pdf_document->doc->displayPage (search->output_dev,
496                                         search->search_page,
497                                         72, 72, 0, gTrue, gFalse);
498
499         if (search->output_dev->findText (search->ucs4,
500                                           search->ucs4_len,
501                                           gTrue, gTrue, // startAtTop, stopAtBottom
502                                           gFalse, gFalse, // startAtLast, stopAtLast
503                                           &xMin, &yMin, &xMax, &yMax)) {
504                 /* This page has results */
505                 search->other_page_flags[search->search_page] = TRUE;
506         }
507
508         search->search_page += 1;
509         if (search->search_page > n_pages) {
510                 /* wrap around */
511                 search->search_page = 1;
512         }
513
514         /* We do this even if nothing was found, to update the percent complete */
515         pdf_document_search_emit_found (search);
516
517         return TRUE;
518
519  end_search:
520         /* We're done. */
521         search->idle = 0; /* will return FALSE to remove */
522         return FALSE;
523 }
524
525 static void
526 pdf_document_find_begin (EvDocumentFind   *document,
527                          const char       *search_string,
528                          gboolean          case_sensitive)
529 {
530         PdfDocument *pdf_document = PDF_DOCUMENT (document);
531         PdfDocumentSearch *search;
532         int n_pages;
533         gunichar *ucs4;
534         glong ucs4_len;
535
536         /* FIXME handle case_sensitive (right now XPDF
537          * code is always case insensitive for ASCII
538          * and case sensitive for all other languaages)
539          */
540
541         g_assert (sizeof (gunichar) == sizeof (Unicode));
542         ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
543                                     &ucs4_len);
544
545         if (pdf_document->search &&
546             pdf_document->search->ucs4_len == ucs4_len &&
547             memcmp (pdf_document->search->ucs4,
548                     ucs4,
549                     sizeof (gunichar) * ucs4_len) == 0) {
550                 /* Search is unchanged */
551                 g_free (ucs4);
552                 return;
553         }
554
555         if (pdf_document->search) {
556                 pdf_document_search_free (pdf_document->search);
557                 pdf_document->search = NULL;
558         }
559
560         search = g_new0 (PdfDocumentSearch, 1);
561
562         search->ucs4 = ucs4;
563         search->ucs4_len = ucs4_len;
564
565         search->current_page_results = g_array_new (FALSE,
566                                                     FALSE,
567                                                     sizeof (EvFindResult));
568         n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
569
570         /* This is an array of bool; with the first value ignored
571          * so we can index by the based-at-1 page numbers
572          */
573         search->other_page_flags = g_new0 (guchar, n_pages + 1);
574
575         search->document = pdf_document;
576
577         /* We add at low priority so the progress bar repaints */
578         search->idle = g_idle_add_full (G_PRIORITY_LOW,
579                                         pdf_document_search_idle_callback,
580                                         search,
581                                         NULL);
582
583         search->output_dev = 0;
584
585         search->start_page = pdf_document->page;
586         search->search_page = search->start_page + 1;
587         if (search->search_page > n_pages)
588                 search->search_page = 1;
589
590         search->current_page = -1;
591
592         pdf_document->search = search;
593
594         /* Update for the current page right away */
595         pdf_document_search_page_changed (search);
596 }
597
598 static void
599 pdf_document_find_cancel (EvDocumentFind   *document)
600 {
601         PdfDocument *pdf_document = PDF_DOCUMENT (document);
602
603         if (pdf_document->search) {
604                 pdf_document_search_free (pdf_document->search);
605                 pdf_document->search = NULL;
606         }
607 }
608
609 static void
610 pdf_document_search_free (PdfDocumentSearch   *search)
611 {
612         if (search->idle != 0)
613                 g_source_remove (search->idle);
614
615         if (search->output_dev)
616                 delete search->output_dev;
617
618         g_array_free (search->current_page_results, TRUE);
619         g_free (search->other_page_flags);
620
621         g_free (search->ucs4);
622         g_free (search);
623 }
624
625 static void
626 pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
627 {
628         PdfDocument *document = PDF_DOCUMENT (exporter);
629
630         if (document->ps_out)
631                 delete document->ps_out;
632
633         document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
634                                             document->doc->getCatalog(), 1,
635                                             ev_document_get_n_pages (EV_DOCUMENT (document)),
636                                             psModePS);
637 }
638
639 static void
640 pdf_document_ps_export_do_page (EvPSExporter *exporter, int page)
641 {
642         PdfDocument *document = PDF_DOCUMENT (exporter);
643
644         document->doc->displayPage (document->ps_out, page,
645                                     72.0, 72.0, 0, gTrue, gFalse);
646 }
647
648 static void
649 pdf_document_ps_export_end (EvPSExporter *exporter)
650 {
651         PdfDocument *document = PDF_DOCUMENT (exporter);
652
653         delete document->ps_out;
654         document->ps_out = NULL;
655 }
656
657
658 /* EvDocumentBookmarks Implementation */
659 typedef struct
660 {
661         /* goo GList, not glib */
662         GList *items;
663         int index;
664         int level;
665 } BookmarksIter;
666
667 static gchar *
668 unicode_to_char (OutlineItem *outline_item,
669                  UnicodeMap *uMap)
670 {
671         GString gstr;
672         gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
673         int i, n;
674
675         for (i = 0; i < outline_item->getTitleLength(); ++i) {
676                 n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
677                 gstr.append(buf, n);
678         }
679
680         return g_strdup (gstr.getCString ());
681 }
682
683
684 static gboolean
685 pdf_document_bookmarks_has_document_bookmarks (EvDocumentBookmarks *document_bookmarks)
686 {
687         PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
688         Outline *outline;
689
690         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
691
692         outline = pdf_document->doc->getOutline();
693         if (outline->getItems() != NULL &&
694             outline->getItems()->getLength() > 0)
695                 return TRUE;
696
697         return FALSE;
698 }
699
700 static EvDocumentBookmarksIter *
701 pdf_document_bookmarks_begin_read (EvDocumentBookmarks *document_bookmarks)
702 {
703         PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
704         Outline *outline;
705         BookmarksIter *iter;
706         GList *items;
707
708         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), NULL);
709
710         outline = pdf_document->doc->getOutline();
711         items = outline->getItems();
712         if (! items)
713                 return NULL;
714
715         iter = g_new0 (BookmarksIter, 1);
716         iter->items = items;
717         iter->index = 0;
718         iter->level = 0;
719
720         return (EvDocumentBookmarksIter *) iter;
721 }
722
723 static gboolean
724 pdf_document_bookmarks_get_values (EvDocumentBookmarks      *document_bookmarks,
725                                    EvDocumentBookmarksIter  *bookmarks_iter,
726                                    char                    **title,
727                                    EvDocumentBookmarksType  *type,
728                                    gint                     *page)
729 {
730         PdfDocument *pdf_document = PDF_DOCUMENT (document_bookmarks);
731         BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
732         OutlineItem *anItem;
733         LinkAction *link_action;
734         LinkDest *link_dest = NULL;
735         LinkURI *link_uri = NULL;
736         LinkGoTo *link_goto = NULL;
737         GString *named_dest;
738         Unicode *link_title;
739         Ref page_ref;
740         gint page_num = -1;
741
742         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
743         g_return_val_if_fail (iter != NULL, FALSE);
744         g_return_val_if_fail (title != NULL, FALSE);
745         g_return_val_if_fail (type != NULL, FALSE);
746         g_return_val_if_fail (page != NULL, FALSE);
747
748         anItem = (OutlineItem *)iter->items->get(iter->index);
749         link_action = anItem->getAction ();
750         link_title = anItem->getTitle ();
751
752         if (link_action) {
753                 switch (link_action->getKind ()) {
754
755                 case actionGoTo:
756                         link_goto = dynamic_cast <LinkGoTo *> (link_action);
757                         link_dest = link_goto->getDest ();
758                         named_dest = link_goto->getNamedDest ();
759
760                         /* Wow!  This seems excessively slow on large
761                          * documents. I need to investigate more... -jrb */
762                         if (link_dest != NULL) {
763                                 link_dest = link_dest->copy ();
764                         } else if (named_dest != NULL) {
765                                 named_dest = named_dest->copy ();
766                                 link_dest = pdf_document->doc->findDest (named_dest);
767                                 delete named_dest;
768                         }
769                         if (link_dest != NULL) {
770                                 if (link_dest->isPageRef ()) {
771                                         page_ref = link_dest->getPageRef ();
772                                         page_num = pdf_document->doc->findPage (page_ref.num, page_ref.gen);
773                                 } else {
774                                         page_num = link_dest->getPageNum ();
775                                 }
776
777                                 delete link_dest;
778                         }
779
780                         break;
781                 case actionURI:
782                         link_uri = dynamic_cast <LinkURI *> (link_action);
783                         break;
784
785                 case actionNamed:
786                         /*Skip, for now */
787                 default:
788                         g_warning ("Unknown link action type: %d", link_action->getKind ());
789                 }
790
791                 *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
792         } else if (link_title) {
793                 *title = g_strdup (unicode_to_char (anItem, pdf_document->umap));
794         }
795
796         *type = EV_DOCUMENT_BOOKMARKS_TYPE_LINK;
797         *page = page_num;
798
799         return TRUE;
800 }
801
802 static EvDocumentBookmarksIter *
803 pdf_document_bookmarks_get_child (EvDocumentBookmarks     *document_bookmarks,
804                                   EvDocumentBookmarksIter *bookmarks_iter)
805 {
806         BookmarksIter *iter = (BookmarksIter *)bookmarks_iter;
807         BookmarksIter *child_iter;
808         OutlineItem *anItem;
809
810         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
811
812         anItem = (OutlineItem *)iter->items->get(iter->index);
813         anItem->open ();
814         if (! (anItem->hasKids() && anItem->getKids()) )
815                 return NULL;
816
817         child_iter = g_new0 (BookmarksIter, 1);
818         child_iter->index = 0;
819         child_iter->level = iter->level + 1;
820         child_iter->items = anItem->getKids ();
821         g_assert (child_iter->items);
822
823         return (EvDocumentBookmarksIter *) child_iter;
824 }
825
826 static gboolean
827 pdf_document_bookmarks_next (EvDocumentBookmarks     *document_bookmarks,
828                              EvDocumentBookmarksIter *bookmarks_iter)
829 {
830         BookmarksIter *iter = (BookmarksIter *) bookmarks_iter;
831
832         g_return_val_if_fail (PDF_IS_DOCUMENT (document_bookmarks), FALSE);
833
834         iter->index++;
835         if (iter->index >= iter->items->getLength())
836                 return FALSE;
837
838         return TRUE;
839 }
840
841 static void
842 pdf_document_bookmarks_free_iter (EvDocumentBookmarks     *document_bookmarks,
843                                   EvDocumentBookmarksIter *iter)
844 {
845         g_return_if_fail (PDF_IS_DOCUMENT (document_bookmarks));
846         g_return_if_fail (iter != NULL);
847
848         /* FIXME: Should I close all the nodes?? Free them? */
849         g_free (iter);
850 }
851
852 static void
853 pdf_document_finalize (GObject *object)
854 {
855         PdfDocument *pdf_document = PDF_DOCUMENT (object);
856
857         if (pdf_document->umap) {
858                 pdf_document->umap->decRefCnt ();
859                 pdf_document->umap = NULL;
860         }
861
862         if (pdf_document->search)
863                 pdf_document_search_free (pdf_document->search);
864
865         if (pdf_document->target)
866                 g_object_unref (pdf_document->target);
867
868         if (pdf_document->out)
869                 delete pdf_document->out;
870         if (pdf_document->ps_out)
871                 delete pdf_document->ps_out;
872         if (pdf_document->doc)
873                 delete pdf_document->doc;
874
875 }
876
877 static void
878 pdf_document_set_property (GObject *object,
879                            guint prop_id,
880                            const GValue *value,
881                            GParamSpec *pspec)
882 {
883         switch (prop_id)
884
885         {
886                 case PROP_TITLE:
887                         /* read only */
888                         break;
889         }
890 }
891
892 static gboolean
893 has_unicode_marker (GString *string)
894 {
895         return ((string->getChar (0) & 0xff) == 0xfe &&
896                 (string->getChar (1) & 0xff) == 0xff);
897 }
898
899 static gchar *
900 pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
901         Object obj;
902         GString *value;
903         gchar *result;
904
905         g_return_val_if_fail (info_dict != NULL, NULL);
906         g_return_val_if_fail (key != NULL, NULL);
907
908         if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
909                 obj.free ();
910                 return g_strdup (_("Unknown"));
911         }
912
913         value = obj.getString ();
914
915         if (has_unicode_marker (value)) {
916                 result = g_convert (value->getCString () + 2,
917                                     value->getLength () - 2,
918                                     "UTF-8", "UTF-16BE", NULL, NULL, NULL);
919         } else {
920                 result = g_strndup (value->getCString (), value->getLength ());
921         }
922
923         obj.free ();
924
925         return result;
926 }
927
928 static char *
929 pdf_document_get_title (PdfDocument *pdf_document)
930 {
931         char *title = NULL;
932         Object info;
933
934         pdf_document->doc->getDocInfo (&info);
935
936         if (info.isDict ()) {
937                 title = pdf_info_dict_get_string (info.getDict(), "Title");
938         }
939
940         return title;
941 }
942
943 static void
944 pdf_document_get_property (GObject *object,
945                            guint prop_id,
946                            GValue *value,
947                            GParamSpec *pspec)
948 {
949         PdfDocument *pdf_document = PDF_DOCUMENT (object);
950         char *title;
951
952         switch (prop_id)
953         {
954                 case PROP_TITLE:
955                         title = pdf_document_get_title (pdf_document);
956                         g_value_set_string (value, title);
957                         g_free (title);
958                         break;
959         }
960 }
961
962 static void
963 pdf_document_class_init (PdfDocumentClass *klass)
964 {
965         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
966
967         gobject_class->finalize = pdf_document_finalize;
968         gobject_class->get_property = pdf_document_get_property;
969         gobject_class->set_property = pdf_document_set_property;
970
971         g_object_class_override_property (gobject_class, PROP_TITLE, "title");
972 }
973
974 static void
975 pdf_document_document_iface_init (EvDocumentIface *iface)
976 {
977         iface->load = pdf_document_load;
978         iface->get_n_pages = pdf_document_get_n_pages;
979         iface->set_page = pdf_document_set_page;
980         iface->get_page = pdf_document_get_page;
981         iface->set_scale = pdf_document_set_scale;
982         iface->set_target = pdf_document_set_target;
983         iface->set_page_offset = pdf_document_set_page_offset;
984         iface->get_page_size = pdf_document_get_page_size;
985         iface->render = pdf_document_render;
986 }
987
988 static void
989 pdf_document_ps_exporter_iface_init (EvPSExporterIface *iface)
990 {
991         iface->begin = pdf_document_ps_export_begin;
992         iface->do_page = pdf_document_ps_export_do_page;
993         iface->end = pdf_document_ps_export_end;
994 }
995
996
997 static void
998 pdf_document_find_iface_init (EvDocumentFindIface *iface)
999 {
1000         iface->begin = pdf_document_find_begin;
1001         iface->cancel = pdf_document_find_cancel;
1002 }
1003
1004 static void
1005 pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface)
1006 {
1007         iface->has_document_bookmarks = pdf_document_bookmarks_has_document_bookmarks;
1008         iface->begin_read = pdf_document_bookmarks_begin_read;
1009         iface->get_values = pdf_document_bookmarks_get_values;
1010         iface->get_child = pdf_document_bookmarks_get_child;
1011         iface->next = pdf_document_bookmarks_next;
1012         iface->free_iter = pdf_document_bookmarks_free_iter;
1013 }
1014
1015 /* Thumbnails */
1016 static GdkPixbuf *
1017 pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
1018                                          gdouble      scale_factor,
1019                                          gint         page_num,
1020                                          gint         width,
1021                                          gint         height)
1022 {
1023         GdkPixmap *pixmap;
1024         GDKSplashOutputDev *output;
1025         GdkPixbuf *pixbuf;
1026         GdkPixbuf *shadow;
1027
1028         pixmap = gdk_pixmap_new (pdf_document->target,
1029                                  width, height, -1);
1030
1031         output = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
1032                                          NULL, NULL);
1033         output->startDoc (pdf_document->doc->getXRef());
1034         pdf_document->doc->displayPage (output,
1035                                         page_num + 1,
1036                                         72*scale_factor,
1037                                         72*scale_factor,
1038                                         0, gTrue, gFalse);
1039         output->redraw (0, 0,
1040                         pixmap,
1041                         0, 0,
1042                         width, height);
1043         pixbuf = gdk_pixbuf_get_from_drawable (NULL,
1044                                                pixmap,
1045                                                NULL,
1046                                                0, 0,
1047                                                0, 0,
1048                                                width, height);
1049         gdk_drawable_unref (pixmap);
1050         delete output;
1051
1052         shadow = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1053         g_object_unref (pixbuf);
1054
1055         return shadow;
1056 }
1057
1058 static GdkPixbuf *
1059 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1060                                        gint                  page,
1061                                        gint                  width)
1062 {
1063         PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1064         GdkPixbuf *thumbnail;
1065         Page *the_page;
1066         Object the_thumb;
1067         Thumb *thumb = NULL;
1068         gboolean have_ethumbs = FALSE;
1069         gdouble page_ratio;
1070         gint dest_height;
1071
1072         /* getPage seems to want page + 1 for some reason; */
1073         the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
1074         the_page->getThumb(&the_thumb);
1075
1076         page_ratio = the_page->getHeight () / the_page->getWidth ();
1077         dest_height = (gint) (width * page_ratio);
1078
1079
1080         if (!(the_thumb.isNull () || the_thumb.isNone())) {
1081                 /* Build the thumbnail object */
1082                 thumb = new Thumb(pdf_document->doc->getXRef (),
1083                                   &the_thumb);
1084
1085                 have_ethumbs = thumb->ok();
1086         }
1087
1088         if (have_ethumbs) {
1089                 guchar *data;
1090                 GdkPixbuf *tmp_pixbuf;
1091
1092                 data = thumb->getPixbufData();
1093                 tmp_pixbuf = gdk_pixbuf_new_from_data (data,
1094                                                        GDK_COLORSPACE_RGB,
1095                                                        FALSE,
1096                                                        8,
1097                                                        thumb->getWidth (),
1098                                                        thumb->getHeight (),
1099                                                        thumb->getWidth () * 3,
1100                                                        NULL, NULL);
1101                 /* FIXME: do we want to check that the thumb's size isn't ridiculous?? */
1102                 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
1103                 g_object_unref (tmp_pixbuf);
1104         } else {
1105                 gdouble scale_factor;
1106
1107                 scale_factor = (gdouble)width / the_page->getWidth ();
1108
1109                 thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
1110                                                                      scale_factor,
1111                                                                      page,
1112                                                                      width,
1113                                                                      dest_height);
1114         }
1115
1116         return thumbnail;
1117 }
1118 static void
1119 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1120 {
1121         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1122 }
1123
1124
1125 static void
1126 pdf_document_init (PdfDocument *pdf_document)
1127 {
1128         pdf_document->page = 1;
1129         pdf_document->page_x_offset = 0;
1130         pdf_document->page_y_offset = 0;
1131         pdf_document->scale = 1.;
1132
1133         pdf_document->page_valid = FALSE;
1134 }
1135