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