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